Archive file in linker script input section ignored

Tobias Schramm t.schramm@manjaro.org
Sun May 17 21:23:46 GMT 2020
Hi,

I've ran into an issue trying to use files from archives in ld input
sections.
[1] makes it seem that files within archives can be used interchangeably
with
files directly present on disk within the context of an input section.
However, when trying to include a section from a file within an archive the
inclusion is ignored. It works flawlessly using the same file outside of the
archive.

I've created the following minimal PoC to showcase the issue:

# cat test.c
char data[0x1000] __attribute__((section(".testdata"), used));

# cat test_object.ld
SECTIONS {
    .rodata : {
        test.o(.testdata)
    }
}

# cat test_archive.ld
SECTIONS {
    .rodata : {
        test.a:(.testdata)
    }
}

# gcc -c test.c -o test.o
# ar r test.a test.o
ar: creating test.a
# ld -T test_object.ld -o test_object.out
# ld -T test_archive.ld -o test_archive.out

# readelf -S test_object.out
There are 6 section headers, starting at offset 0x20d0:

Section Headers:
  [Nr] Name              Type             Address           Offset
       Size              EntSize          Flags  Link  Info  Align
  [ 0]                   NULL             0000000000000000  00000000
       0000000000000000  0000000000000000           0     0     0
  [ 1] .rodata           PROGBITS         0000000000000000  00001000
       0000000000001000  0000000000000000  WA       0     0     32
  [ 2] .comment          PROGBITS         0000000000000000  00002000
       0000000000000012  0000000000000001  MS       0     0     1
  [ 3] .symtab           SYMTAB           0000000000000000  00002018
       0000000000000078  0000000000000018           4     4     8
  [ 4] .strtab           STRTAB           0000000000000000  00002090
       000000000000000d  0000000000000000           0     0     1
  [ 5] .shstrtab         STRTAB           0000000000000000  0000209d
       000000000000002c  0000000000000000           0     0     1

# readelf -S test_archive.out
There are 4 section headers, starting at offset 0x78:

Section Headers:
  [Nr] Name              Type             Address           Offset
       Size              EntSize          Flags  Link  Info  Align
  [ 0]                   NULL             0000000000000000  00000000
       0000000000000000  0000000000000000           0     0     0
  [ 1] .symtab           SYMTAB           0000000000000000  00000040
       0000000000000018  0000000000000018           2     1     8
  [ 2] .strtab           STRTAB           0000000000000000  00000058
       0000000000000001  0000000000000000           0     0     1
  [ 3] .shstrtab         STRTAB           0000000000000000  00000059
       000000000000001b  0000000000000000           0     0     1

As you can see the elf built with the archive is missing the whole .rodata
section. I'd expect it to be the same as the elf built using the object
file.
Am I missing something?

I'm currently running binutils version 2.34.50.20200508 on amd64.


Best regards,

Tobias Schramm

[1] https://sourceware.org/binutils/docs-2.34/ld/Input-Section-Basics.html


More information about the Binutils mailing list