Bug 22655 - gcc unable to compile assembly with -nostdlib
Summary: gcc unable to compile assembly with -nostdlib
Status: RESOLVED INVALID
Alias: None
Product: binutils
Classification: Unclassified
Component: ld (show other bugs)
Version: 2.29
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2017-12-31 07:33 UTC by Zach
Modified: 2018-03-31 12:45 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed: 2017-12-31 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Zach 2017-12-31 07:33:14 UTC
I'm trying to compile this pure x64 program to an ELF.
Source: https://github.com/calculuswhiz/Assembly-Syntax-Definition/blob/master/test/quine.sx.

I use this:
gcc -nostdlib quine.sx
And this:
gcc -nostdlib -fPIC quine.sx

But it always just gives me this:
/usr/bin/ld: /tmp/cca3rc8T.o: relocation R_X86_64_32S against `.text' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: Nonrepresentable section on output
collect2: error: ld returned 1 exit status

I know for sure this worked in the past (Last I know for sure was on 28Jan17).

However, compiling this 32-bit code still works:
https://github.com/calculuswhiz/Assembly-Syntax-Definition/blob/master/test/quine32.sx

Using:
gcc -nostdlib -m32 quine32.sx

What's going on?
Comment 1 Andreas Schwab 2017-12-31 08:10:20 UTC
Please show exactly how the assembler and linker are called.
Comment 2 Zach 2017-12-31 18:16:26 UTC
You mean use the verbose flag? Because I don't call `as` and `ld` myself. It's breaking at `ld`.
I have other test programs as well in the above repo, if you find those easier to look at. This was just the most recent one I could confirm that works. It looks like some of the assembler directives are now causing ld to break somehow.

Compilation with -v:
% gcc -nostdlib quine.sx -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-linux-gnu/7.2.1/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: /build/gcc/src/gcc/configure --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++ --enable-shared --enable-threads=posix --enable-libmpx --with-system-zlib --with-isl --enable-__cxa_atexit --disable-libunwind-exceptions --enable-clocale=gnu --disable-libstdcxx-pch --disable-libssp --enable-gnu-unique-object --enable-linker-build-id --enable-lto --enable-plugin --enable-install-libiberty --with-linker-hash-style=gnu --enable-gnu-indirect-function --enable-multilib --disable-werror --enable-checking=release --enable-default-pie --enable-default-ssp
Thread model: posix
gcc version 7.2.1 20171128 (GCC) 
COLLECT_GCC_OPTIONS='-nostdlib' '-v' '-mtune=generic' '-march=x86-64'
 /usr/lib/gcc/x86_64-pc-linux-gnu/7.2.1/cc1 -E -lang-asm -quiet -v quine.sx -mtune=generic -march=x86-64 -fno-directives-only -o /tmp/ccarmazI.s
ignoring nonexistent directory "/usr/lib/gcc/x86_64-pc-linux-gnu/7.2.1/../../../../x86_64-pc-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/x86_64-pc-linux-gnu/7.2.1/include
 /usr/local/include
 /usr/lib/gcc/x86_64-pc-linux-gnu/7.2.1/include-fixed
 /usr/include
End of search list.
COLLECT_GCC_OPTIONS='-nostdlib' '-v' '-mtune=generic' '-march=x86-64'
 as -v --64 -o /tmp/ccavrnxG.o /tmp/ccarmazI.s
GNU assembler version 2.29.1 (x86_64-pc-linux-gnu) using BFD version (GNU Binutils) 2.29.1
COMPILER_PATH=/usr/lib/gcc/x86_64-pc-linux-gnu/7.2.1/:/usr/lib/gcc/x86_64-pc-linux-gnu/7.2.1/:/usr/lib/gcc/x86_64-pc-linux-gnu/:/usr/lib/gcc/x86_64-pc-linux-gnu/7.2.1/:/usr/lib/gcc/x86_64-pc-linux-gnu/
LIBRARY_PATH=/usr/lib/gcc/x86_64-pc-linux-gnu/7.2.1/:/usr/lib/gcc/x86_64-pc-linux-gnu/7.2.1/../../../../lib/:/lib/../lib/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-pc-linux-gnu/7.2.1/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-nostdlib' '-v' '-mtune=generic' '-march=x86-64'
 /usr/lib/gcc/x86_64-pc-linux-gnu/7.2.1/collect2 -plugin /usr/lib/gcc/x86_64-pc-linux-gnu/7.2.1/liblto_plugin.so -plugin-opt=/usr/lib/gcc/x86_64-pc-linux-gnu/7.2.1/lto-wrapper -plugin-opt=-fresolution=/tmp/ccgD9dwE.res --build-id --eh-frame-hdr --hash-style=gnu -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -pie -L/usr/lib/gcc/x86_64-pc-linux-gnu/7.2.1 -L/usr/lib/gcc/x86_64-pc-linux-gnu/7.2.1/../../../../lib -L/lib/../lib -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-pc-linux-gnu/7.2.1/../../.. /tmp/ccavrnxG.o
/usr/bin/ld: /tmp/ccavrnxG.o: relocation R_X86_64_32S against `.text' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: Nonrepresentable section on output
collect2: error: ld returned 1 exit status
Comment 3 H.J. Lu 2018-01-08 02:22:33 UTC
Please pass -no-pie to the gcc driver.
Comment 4 Zach 2018-01-08 02:33:53 UTC
Ah, good. Compiles successfully with -no-pie.

I don't really understand why this works though.

Why don't I need this flag in 32 bit? Why didn't I need this in the previous version? Was this unintentional?
Comment 5 H.J. Lu 2018-01-08 03:17:25 UTC
(In reply to Zach from comment #4)
> 
> Why don't I need this flag in 32 bit? Why didn't I need this in the previous

There is no relocation overflow in 32-bit.

> version? Was this unintentional?

Your GCC is configured with --enable-default-pie.
Comment 6 Zach 2018-01-08 23:18:33 UTC
Ah, thanks. Got it.