Bug 14196

Summary: gold mistreats -Wl,-Ttext
Product: binutils Reporter: Vladimir Serbinenko <phcoder>
Component: goldAssignee: Ian Lance Taylor <ian>
Status: ASSIGNED ---    
Severity: normal CC: ccoutant, cjwatson, heiko
Priority: P2    
Version: unspecified   
Target Milestone: ---   
Host: Target:
Build: Last reconfirmed:

Description Vladimir Serbinenko 2012-06-04 10:08:11 UTC
Namely the minimal testcase is:
gcc -o 1.img -ffreestanding -Wl,-Ttext,0x8200 1.S -nostdlib -m32
The resulting file has .text at 9200 and not 8200
1.S:
.text

.globl _start
_start:

Because of this GRUB is miscompiled if attempting to link with gold
Comment 1 Ian Lance Taylor 2012-06-04 15:21:16 UTC
As I explained in e-mail, for gold the -Ttext option sets the address of the text segment, not the .text section.  This is intentional.  When I try your test case, I get these section addresses, as reported by readelf -S:


  [ 0]                   NULL            00000000 000000 000000 00      0   0  0
  [ 1] .note.gnu.build-i NOTE            00008200 000200 000024 00   A  0   0  4
  [ 2] .text             PROGBITS        00008224 000224 000000 00  AX  0   0  4
  [ 3] .data             PROGBITS        00009000 001000 000000 00  WA  0   0  4
  [ 4] .bss              NOBITS          00009000 001000 000000 00  WA  0   0  4
  [ 5] .note.gnu.gold-ve NOTE            00000000 001000 00001c 00      0   0  4
  [ 6] .symtab           SYMTAB          00000000 00101c 000050 10      7   1  4
  [ 7] .strtab           STRTAB          00000000 00106c 000020 00      0   0  1
  [ 8] .shstrtab         STRTAB          00000000 00108c 000056 00      0   0  1

The text segment starts at 0x8200.  The .text section is not at 0x9200.  If I change the command to

gcc -o 1.img -ffreestanding -Wl,-Ttext,0x8200 ~/foo.s -nostdlib -m32 -v --save-temps -Wl,--build-id=none

then the .text section is the first section in the text segment, and both are at address 0x8200.

So as far as I can see everything works as intended, and works as you want.  You need to provide more information: the version of the linker that you are using, and the command line passed to the linker, which you can get by using gcc -v.
Comment 2 Vladimir Serbinenko 2012-06-04 15:30:08 UTC
It looks like I need to retest. It was probably some old gold version.
Comment 3 Colin Watson 2013-01-02 02:42:17 UTC
Adding -Wl,-N more accurately reproduces the bug as seen by GRUB.

GNU gold (GNU Binutils for Ubuntu 2.23.1) 1.11

$ gcc -v -fuse-ld=gold -o 1.img -ffreestanding -Wl,-Ttext,0x8200 1.S -nostdlib -m32 -Wl,-N -Wl,--build-id=none
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/i686-linux-gnu/4.7/lto-wrapper
Target: i686-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.7.2-16ubuntu1' --with-bugurl=file:///usr/share/doc/gcc-4.7/README.Bugs --enable-languages=c,c++,go,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.7 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.7 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --enable-objc-gc --enable-targets=all --enable-multiarch --disable-werror --with-arch-32=i686 --with-multilib-list=m32,m64,mx32 --with-tune=generic --enable-checking=release --build=i686-linux-gnu --host=i686-linux-gnu --target=i686-linux-gnu
Thread model: posix
gcc version 4.7.2 (Ubuntu/Linaro 4.7.2-16ubuntu1)
COLLECT_GCC_OPTIONS='-v' '-fuse-ld=gold' '-o' '1.img' '-ffreestanding' '-nostdlib' '-m32' '-mtune=generic' '-march=i686'
 /usr/lib/gcc/i686-linux-gnu/4.7/cc1 -E -lang-asm -quiet -v -imultilib . -imultiarch i386-linux-gnu 1.S -m32 -mtune=generic -march=i686 -fuse-ld=gold -ffreestanding -fno-directives-only -o /tmp/cc6TeMd1.s
ignoring nonexistent directory "/usr/local/include/i386-linux-gnu"
ignoring nonexistent directory "/usr/lib/gcc/i686-linux-gnu/4.7/../../../../i686-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/i686-linux-gnu/4.7/include
 /usr/local/include
 /usr/lib/gcc/i686-linux-gnu/4.7/include-fixed
 /usr/include/i386-linux-gnu
 /usr/include
End of search list.
COLLECT_GCC_OPTIONS='-v' '-fuse-ld=gold' '-o' '1.img' '-ffreestanding' '-nostdlib' '-m32' '-mtune=generic' '-march=i686'
 as -v --32 -o /tmp/ccHEpBci.o /tmp/cc6TeMd1.s
GNU assembler version 2.23.1 (i686-linux-gnu) using BFD version (GNU Binutils for Ubuntu) 2.23.1
COMPILER_PATH=/usr/lib/gcc/i686-linux-gnu/4.7/:/usr/lib/gcc/i686-linux-gnu/4.7/:/usr/lib/gcc/i686-linux-gnu/:/usr/lib/gcc/i686-linux-gnu/4.7/:/usr/lib/gcc/i686-linux-gnu/
LIBRARY_PATH=/usr/lib/gcc/i686-linux-gnu/4.7/:/usr/lib/gcc/i686-linux-gnu/4.7/../../../i386-linux-gnu/:/usr/lib/gcc/i686-linux-gnu/4.7/../../../../lib/:/lib/i386-linux-gnu/:/lib/../lib/:/usr/lib/i386-linux-gnu/:/usr/lib/../lib/:/usr/lib/gcc/i686-linux-gnu/4.7/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-fuse-ld=gold' '-o' '1.img' '-ffreestanding' '-nostdlib' '-m32' '-mtune=generic' '-march=i686'
 /usr/lib/gcc/i686-linux-gnu/4.7/collect2 --sysroot=/ --build-id --eh-frame-hdr -m elf_i386 --hash-style=gnu --as-needed -dynamic-linker /lib/ld-linux.so.2 -z relro -fuse-ld=gold -o 1.img -L/usr/lib/gcc/i686-linux-gnu/4.7 -L/usr/lib/gcc/i686-linux-gnu/4.7/../../../i386-linux-gnu -L/usr/lib/gcc/i686-linux-gnu/4.7/../../../../lib -L/lib/i386-linux-gnu -L/lib/../lib -L/usr/lib/i386-linux-gnu -L/usr/lib/../lib -L/usr/lib/gcc/i686-linux-gnu/4.7/../../.. -Ttext 0x8200 /tmp/ccHEpBci.o -N --build-id=none

$ readelf -S 1.img                                        There are 8 section headers, starting at offset 0x10d0:

Section Headers:
  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al
  [ 0]                   NULL            00000000 000000 000000 00      0   0  0
  [ 1] .text             PROGBITS        00009000 001000 000000 00  AX  0   0  4
  [ 2] .data             PROGBITS        00009000 001000 000000 00  WA  0   0  4
  [ 3] .bss              NOBITS          00009000 001000 000000 00  WA  0   0  4
  [ 4] .note.gnu.gold-ve NOTE            00000000 001000 00001c 00      0   0  4
  [ 5] .symtab           SYMTAB          00000000 00101c 000050 10      6   1  4
  [ 6] .strtab           STRTAB          00000000 00106c 000020 00      0   0  1
  [ 7] .shstrtab         STRTAB          00000000 00108c 000043 00      0   0  1
Key to Flags:
  W (write), A (alloc), X (execute), M (merge), S (strings)
  I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown)
  O (extra OS processing required) o (OS specific), p (processor specific)

GNU ld appears to optimise away the .text section, but if I add a "nop" to avoid that, it correctly starts .text at 00008200 with these options.