[cfe-dev] Forcing default target, linker, includes

Chris Hanson via cfe-dev cfe-dev at lists.llvm.org
Sat Nov 9 15:27:43 PST 2019


Thanks for the help! I feel like I’m getting quite a bit closer with only a little bit of hacking.

This is now how I’m configuring LLVM/clang/lld master as of earlier this afternoon, it builds just fine (using clang 9 from the web site on Ubuntu 19.04):

in $LLVM/build
$ cmake \
        -G Ninja \
        "${PROJECT_DIR}/llvm-project/llvm" \
        -DCMAKE_INSTALL_PREFIX="/opt/llvm-mips" \
        -DLLVM_DEFAULT_TARGET_TRIPLE="mips-sgi-irix" \
        -DLLVM_ENABLE_PROJECTS="clang;compiler-rt;lld;libunwind" \
        -DLLVM_TARGETS_TO_BUILD="Mips" \
        -DCLANG_DEFAULT_OBJCOPY="llvm-objcopy" \
        -DCLANG_DEFAULT_LINKER="lld" \
        -DCLANG_DEFAULT_RTLIB="compiler-rt" \
        -DCLANG_DEFAULT_UNWINDLIB="libunwind"

(Incidentally, I found a single use of CLANG_DEFAULT_RTLIBS in the CMake files, I assumed it shouldn’t be plural  since there were no other uses, so I fixed it locally.)

I also set up a new Irix toolchain which has required only tiny initial modifications:
Cloned Driver/ToolChains/MipsLinux.{cpp,h} to Irix.{cpp,h}, renaming the class to just Irix (yeah it should be based on Generic_ELF but this was expedient)
Added an override of getDefaultLinker() to return “ld.lld”
Added target triple resolution for the new Irix toolchain to Basic/Targets.cpp
Cloned SolarisTargetInfo to IrixTargetInfo in Basic/Targets/OSTargets.h
Adding __sgi as a predefined macro to it
Made Driver.cpp instantiate the toolchain for an Irix target triple
Added ToolChains/MipsLinux.cpp to Driver/CMakeLists.txt

All of this lets me compile for Irix using a command like the following:

in $LLVM/build
$ ./bin/clang -g -D_LANGUAGE_C=1 \
    -D__sgi=1 -D_SGIAPI=1 -D_SGI_SOURCE=1 -D_MIPSABI_SOURCE=1 \
    --target=mips-sgi-irix march=mips4 -m64 -isysroot $IRIXSDK \
    -c ../test/hello.c

The .o this produces is recognized reasonably by file:

in $LLVM/build
$ file hello.
hello.o: ELF 64-bit MSB relocatable, MIPS, MIPS-IV version 1 (SYSV), with debug_info, not stripped

Unfortunately actually trying to produce an executable doesn’t work so well yet. It does use lld, but it tries to resolve a symbol inside the Irix libc.so which will actually be filled in by the runtime loader (rld). This combined compilation and linking line:

in $LLVM/build
$ bin/clang -v -Wl,-t -Wl,--verbose \
    -nostdlib -g -D_LANGUAGE_C=1 \
    -D__sgi=1 -D_SGIAPI=1 -D_SGI_SOURCE=1 -D_MIPSABI_SOURCE=1 \
    --target=mips-sgi-irix -march=mips4 -m64 -isysroot $IRIXSDK \
    $IRIXSDK/usr/lib64/mips4/libc.so \
    $IRIXSDK/usr/lib64/mips4/crt1.o \
    -o hello ../test/hello.c

produces this output (lightly edited for clarity, i.e. trimming paths to $LLVM and $IRIXSDK):

clang version 10.0.0 (llvm-project.git f67aec686b01518c5cf9842ad22d7113b2d50955)
Target: mips64-sgi-irix
Thread model: posix
InstalledDir: $LLVM/build/./bin
 "$LLVM/build/llvm/bin/clang-10" -cc1 -triple mips64-sgi-irix -emit-obj -mrelax-all -disable-free -main-file-name hello.c -mrelocation-model pic -pic-level 1 -mthread-model posix -mframe-pointer=all -fmath-errno -fno-rounding-math -masm-verbose -mconstructor-aliases -target-cpu mips4 -target-feature -noabicalls -target-abi n64 -mfloat-abi hard -dwarf-column-info -debug-info-kind=limited -dwarf-version=4 -debugger-tuning=gdb -v -resource-dir $LLVM/build/llvm/lib/clang/10.0.0 -isysroot $IRIXSDK -D _LANGUAGE_C=1 -D __sgi=1 -D _SGIAPI=1 -D _SGI_SOURCE=1 -D _MIPSABI_SOURCE=1 -internal-isystem $LLVM/build/llvm/lib/clang/10.0.0/include -fdebug-compilation-dir $LLVM/build -ferror-limit 19 -fmessage-length 0 -fgnuc-version=4.2.1 -fobjc-runtime=gcc -fdiagnostics-show-option -faddrsig -o /tmp/hello-190bff.o -x c ../test/hello.c
clang -cc1 version 10.0.0 based upon LLVM 10.0.0svn default target mips-sgi-irix
ignoring nonexistent directory "$IRIXSDK/usr/local/include"
ignoring duplicate directory "$LLVM/build/llvm/lib/clang/10.0.0/include"
#include "..." search starts here:
#include <...> search starts here:
 $LLVM/build/llvm/lib/clang/10.0.0/include
 $IRIXSDK/usr/include
End of search list.
 "$LLVM/build/./bin/ld.lld" -z relro --eh-frame-hdr -m elf64btsmip -dynamic-linker /lib64/ld.so.1 -o hello -L/usr/lib64 -t --verbose $IRIXSDK/usr/lib64/mips4/libc.so $IRIXSDK/usr/lib64/mips4/crt1.o /tmp/hello-190bff.o
ld.lld: $IRIXSDK/usr/lib64/mips4/libc.so
ld.lld: $IRIXSDK/usr/lib64/mips4/crt1.o
ld.lld: /tmp/hello-190bff.o
$IRIXSDK/usr/lib64/mips4/libc.so
ld.lld: warning: found local symbol '.data' in global part of symbol table in file $IRIXSDK/usr/lib64/mips4/libc.so
ld.lld: warning: found local symbol '.got' in global part of symbol table in file $IRIXSDK/usr/lib64/mips4/libc.so
ld.lld: warning: found local symbol '.sbss' in global part of symbol table in file $IRIXSDK/usr/lib64/mips4/libc.so
ld.lld: warning: found local symbol '.srdata' in global part of symbol table in file $IRIXSDK/usr/lib64/mips4/libc.so
ld.lld: warning: found local symbol '.sdata' in global part of symbol table in file $IRIXSDK/usr/lib64/mips4/libc.so
ld.lld: warning: found local symbol '.lit8' in global part of symbol table in file $IRIXSDK/usr/lib64/mips4/libc.so
ld.lld: warning: found local symbol '.lit4' in global part of symbol table in file $IRIXSDK/usr/lib64/mips4/libc.so
ld.lld: warning: found local symbol '.bss' in global part of symbol table in file $IRIXSDK/usr/lib64/mips4/libc.so
$IRIXSDK/usr/lib64/mips4/crt1.o
/tmp/hello-190bff.o
ld.lld: error: $IRIXSDK/usr/lib64/mips4/libc.so: undefined reference to _rld_new_interface
clang-10: error: linker command failed with exit code 1 (use -v to see invocation)

Why is clang cc1 saying $IRIXSDK/usr/include doesn’t exist, when it actually does? (Just to reiterate, I’m using $IRIXSDK in this email for concision, it actually gives the full path—which “ls” lists the contents of just fine.)

Why is lld trying to resolve an undefined reference within a shared library, and what can I do about it? (The _rld_new_interface symbol is filled in by rld when it loads libc, there’s no need for it to be resolved against anything.) I’m not super familiar with ELF unlike Mach-O so I’m kind of surprised by this.

Is it related to the warnings about local symbols in global parts of the symbol table in libc.so? Is there any way to suppress those too, or to have lld interpret them “properly” (as SGI intended in Irix 6.5.30, irrespective of what’s proper today)?

  — Chris

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20191109/e41de8b1/attachment.html>


More information about the cfe-dev mailing list