Quuxplusone / LLVMBugzillaTest

0 stars 0 forks source link

hello world example linker error #38392

Closed Quuxplusone closed 5 years ago

Quuxplusone commented 5 years ago
Bugzilla Link PR39419
Status RESOLVED INVALID
Importance P normal
Reported by Xunlei Wu (Xunlei.wu@sas.com)
Reported on 2018-10-24 07:10:30 -0700
Last modified on 2018-10-24 15:05:53 -0700
Version 7.0
Hardware PC Linux
CC llvm-bugs@lists.llvm.org, t.p.northover@gmail.com
Fixed by commit(s)
Attachments
Blocks
Blocked by
See also
Hello,

First time user of LLVM here. My environment is Ubuntu18.04 64-bit in
VirtualBox. Host OS is Windows 10 professional.

VirtualBox:~/tmp$ uname -a
Linux VirtualBox 4.15.0-38-generic #41-Ubuntu SMP Wed Oct 10 10:59:38 UTC 2018
x86_64 x86_64 x86_64 GNU/Linux

VirtualBox:~/tmp$ gcc --version
gcc (Ubuntu 7.3.0-27ubuntu1~18.04) 7.3.0

Ubuntu18.04 has LLVM 6.0.0 pre-installed.
VirtualBox:~/tmp$ /usr/bin/clang --version
clang version 6.0.0-1ubuntu2 (tags/RELEASE_600/final)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

I followed the hello world example
https://llvm.org/docs/GettingStarted.html#an-example-using-the-llvm-tool-chain
till step 7
Assemble the native assembly language file into a program:

% /opt/SUNWspro/bin/cc -xarch=v9 hello.s -o hello.native   # On Solaris

% gcc hello.s -o hello.native                              # On others

The failed message is
"
VirtualBox:~/tmp$ gcc hello.s -o hello.native
/usr/bin/ld: /tmp/ccaIxQSy.o: relocation R_X86_64_32 against `.rodata.str1.1'
can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: final link failed: Nonrepresentable section on output
collect2: error: ld returned 1 exit status
"

I then added -fPIC to "% clang -fPIC -O3 -emit-llvm hello.c -c -o hello.bc".
However, it did not help.

I then installed clang+llvm-7.0.0-x86_64-linux-gnu-ubuntu-16.04.tar.xz. The
same issue persists.

Would you please help? Thanks a lot.
Quuxplusone commented 5 years ago

Was able to solve the issue by adding -relocation-model=pic to step 6 in the tutorial. The new version becomes

% llc -relocation-model=pic hello.bc -o hello.s

Please verify the fix is correct and update the document accordingly.

Quuxplusone commented 5 years ago

Yep, looks right to me. If you're linking a PIE executable (which your GCC seems to default to and is reasonably sensible) then all your object files need to be PIC. The way you do that on llc is via -relocation-model=pic.