[PATCH] Orphan sections and NOLOAD output section

Fangrui Song i@maskray.me
Sat Apr 23 21:30:16 GMT 2022


On 2022-04-21, Eric Botcazou via Binutils wrote:
>Hi,
>
>we recently got a report about a counter-intuitive behavior of the GNU linker
>for orphan sections matched by a NOLOAD output section, which turned out to be
>an exact duplicate of:
>  https://stackoverflow.com/questions/48764136/gcc-noload-directive-cause-wrong-memory-mapping
>
>What happens is that the effect of the command:
>
>  .foo (NOLOAD) : {}
>
>is not equivalent to that of:
>
>  .foo (NOLOAD) : { *(.foo) }
>
>when there is more than 1 object file containing a .foo section: the former
>will output two .foo sections, the first with PROGBITS and the second with
>NOBITS, where the latter will output only one with NOBITS.
>
>I think that the commands are essentially equivalent so the linker ought to yield
>the same outcome, namely the single output section with NOBITS.
>
>Tested on x86-64/Linux, OK for the mainline?
>
>
>2022-04-21  Eric Botcazou  <ebotcazou@adacore.com>
>
>ld/
>	* ldelf.c (ldelf_place_orphan): Match only by name for a NOLOAD
>	output section present in the script.
>	* testsuite/ld-elf/orphan-13.d: New file.
>	* testsuite/ld-elf/orphan-13.ld: Likewise.
>	* testsuite/ld-elf/orphan-13a.s: Likewise.
>	* testsuite/ld-elf/orphan-13b.s: Likewise.
>	* testsuite/ld-elf/orphan-13c.s: Likewise.
>
>-- 
>Eric Botcazou

For other folks' convenience, the patch effect is changing

% ld.bfd orphan-13[abc].o -T orphan-13.ld && readelf -S a.out | grep foo
   [ 1] .foo              PROGBITS         0000000000000078  00000078
   [ 2] .foo              NOBITS           0000000000000098  00000098

to

% ~/Dev/binutils-gdb/out/release/ld/ld-new orphan-13[abc].o -T orphan-13.ld && readelf -S a.out | grep foo
   [ 1] .foo              NOBITS           0000000000000078  00000078


I think this is a good move.
I have checked `.foo (TYPE=SHT_NOBITS) : {}` which already uses just one output section.

I think in the future it makes sense to report a warning when NOBITS changes the section type from SHT_PROGBITS to SHT_NOBITS.
This kind of section type change is likely not what the user expects.


More information about the Binutils mailing list