Subject: ld 2.8.1/2.9.1 bug when creating -N -T0 elf32 output
To: None <tech-toolchain@netbsd.org>
From: Ignatios Souvatzis <ignatios@theory.cs.uni-bonn.de>
List: tech-toolchain
Date: 09/09/1998 15:55:30
This is a copy of a report I just sent to bugs-gnu-utils@fsf.org:

When using binutils-2.9.1 ld to create single-sectioned elf32 output,
the "memory size" reported in the (in this case single) program header
table is too small or too big, compared to the values from the section
table or the symbol table. [ELF Loaders are supposed to use the
program header table.]

This was tested on

* netbsd-m68k		crosscompiling to netbsd-powerpc, using both
			2.8.1 and 2.9.1. ld
* sparc-sun-solaris2.6	crosscompiling to netbsd-powerpc, 2.9.1 ld
* sparc-sun-solaris2.6 (native), 2.9.1 ld

[This is a problem, e.g., if somebody wants to use single-sectioned
output to make kernel boot loaders less complex]

I'm appending two testcases:

Detailed objdump output for sparc-sun-solaris2.6 native test
case. Note that _end has a value of 0x920, while memsz claims the
executable's in-memory size is only 0x91c.

ignatios@hamilton 271 % cat test.c
int foo=4711;
int bla[512] = {1,2,3,4,5};
double blubb[32];
char cafe=3;

int 
test(int a, char *b) 
{
        return a + sizeof(*b) + foo;
}
ignatios@hamilton 275 % gcc -c -O2 test.c
ignatios@hamilton 278 % /usr/local/gnu/bin/ld -v
GNU ld version 2.9.1 (with BFD 2.9.1)
ignatios@hamilton 279 % /usr/local/gnu/bin/ld -Ttext 0 -N -etest -o test test.o
ignatios@hamilton 280 % objdump --all-headers test

test:     file format elf32-sparc
test
architecture: sparc, flags 0x00000012:
EXEC_P, HAS_SYMS
start address 0x00000000

Program Header:
    LOAD off    0x00000074 vaddr 0x00000000 paddr 0x00000000 align 2**3
         filesz 0x00000819 memsz 0x0000091c flags rwx

Sections:
Idx Name          Size      VMA       LMA       File off  Algn
  0 .text         00000014  00000000  00000000  00000074  2**2
                  CONTENTS, ALLOC, LOAD, CODE
  1 .data         00000805  00000014  00000014  00000088  2**2
                  CONTENTS, ALLOC, LOAD, DATA
  2 .bss          00000100  00000820  00000820  00000890  2**3
                  ALLOC
  3 .comment      00000040  00000000  00000000  00000890  2**0
                  CONTENTS, READONLY
SYMBOL TABLE:
00000000 l    d  .text  00000000 
00000014 l    d  .data  00000000 
00000820 l    d  .bss   00000000 
00000000 l    d  .comment       00000000 
00000000 l    d  *ABS*  00000000 
00000000 l    d  *ABS*  00000000 
00000000 l    d  *ABS*  00000000 
00000000 l    df *ABS*  00000000 test.c
00000000 l       .text  00000000 gcc2_compiled.
00000014 g     O *ABS*  00000000 _etext
00000820 g     O .bss   00000100 blubb
00000000 g     F .text  00000014 test
00000018 g     O .data  00000800 bla
00000818 g     O .data  00000001 cafe
00000819 g     O *ABS*  00000000 __bss_start
00000014 g     O .data  00000004 foo
00000819 g     O *ABS*  00000000 _edata
00000920 g     O *ABS*  00000000 _end

Increasing the data section leads to a memsz bigger than the expected one:

ignatios@hamilton 309 % cat test2.c
int foo=4711;
int bla[512] = {1,2,3,4,5};
double blubb[32];
char cafe=3;
int bar = 7;

int 
test(int a, char *b) 
{
        return a + sizeof(*b) + foo;
}
ignatios@hamilton 310 % gcc -c -O2 test2.c
ignatios@hamilton 311 % /usr/local/gnu/bin/ld -Ttext 0 -N -etest -o test2 test2.o
ignatios@hamilton 312 % objdump --all-headers test2

test2:     file format elf32-sparc
test2
architecture: sparc, flags 0x00000012:
EXEC_P, HAS_SYMS
start address 0x00000000

Program Header:
    LOAD off    0x00000074 vaddr 0x00000000 paddr 0x00000000 align 2**3
         filesz 0x00000820 memsz 0x00000924 flags rwx

Sections:
Idx Name          Size      VMA       LMA       File off  Algn
  0 .text         00000014  00000000  00000000  00000074  2**2
                  CONTENTS, ALLOC, LOAD, CODE
  1 .data         0000080c  00000014  00000014  00000088  2**2
                  CONTENTS, ALLOC, LOAD, DATA
  2 .bss          00000100  00000820  00000820  00000898  2**3
                  ALLOC
  3 .comment      00000040  00000000  00000000  00000898  2**0
                  CONTENTS, READONLY
SYMBOL TABLE:
00000000 l    d  .text  00000000 
00000014 l    d  .data  00000000 
00000820 l    d  .bss   00000000 
00000000 l    d  .comment       00000000 
00000000 l    d  *ABS*  00000000 
00000000 l    d  *ABS*  00000000 
00000000 l    d  *ABS*  00000000 
00000000 l    df *ABS*  00000000 test2.c
00000000 l       .text  00000000 gcc2_compiled.
00000014 g     O *ABS*  00000000 _etext
00000820 g     O .bss   00000100 blubb
00000000 g     F .text  00000014 test
00000018 g     O .data  00000800 bla
00000818 g     O .data  00000001 cafe
00000820 g     O *ABS*  00000000 __bss_start
00000014 g     O .data  00000004 foo
00000820 g     O *ABS*  00000000 _edata
00000920 g     O *ABS*  00000000 _end
0000081c g     O .data  00000004 bar



--