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?
Please show exactly how the assembler and linker are called.
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
Please pass -no-pie to the gcc driver.
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?
(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.
Ah, thanks. Got it.