Bug 23658 - Improve PT_NOTE segment creation
Summary: Improve PT_NOTE segment creation
Status: RESOLVED FIXED
Alias: None
Product: binutils
Classification: Unclassified
Component: ld (show other bugs)
Version: 2.32
: P2 normal
Target Milestone: 2.32
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2018-09-14 15:20 UTC by H.J. Lu
Modified: 2021-11-16 05:54 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description H.J. Lu 2018-09-14 15:20:59 UTC
[hjl@gnu-cfl-1 note]$ cat x.s
	.text
	.globl	_start
	.type	_start, @function
_start:
	ret

	.section ".note.1", "a"
	.p2align 3
	.long 1f - 0f		/* name length.  */
	.long 3f - 1f		/* data length.  */
	.long  12345		/* note type.  */
0:
	.asciz "GNU"		/* vendor name.  */
1:
	.p2align 3
	.long 0			/* pr_type.  */
	.long 5f - 4f		/* pr_datasz.  */
4:
	.zero 0x10
5:
	.p2align 3
3:
[hjl@gnu-cfl-1 note]$ cat y.s
	.section ".note.2", "a"
	.p2align 2
	.long 1f - 0f		/* name length.  */
	.long 3f - 1f		/* data length.  */
	.long  12345		/* note type.  */
0:
	.asciz "GNU"		/* vendor name.  */
1:
	.p2align 2
	.long 0			/* pr_type.  */
	.long 5f - 4f		/* pr_datasz.  */
4:
	.zero 0x10
5:
	.p2align 2
3:
[hjl@gnu-cfl-1 note]$ cat z.s
	.section ".note.3", "a"
	.p2align 2
	.long 1f - 0f		/* name length.  */
	.long 3f - 1f		/* data length.  */
	.long  12345		/* note type.  */
0:
	.asciz "GNU"		/* vendor name.  */
1:
	.p2align 2
	.long 0			/* pr_type.  */
	.long 5f - 4f		/* pr_datasz.  */
4:
	.zero 0x10
5:
	.p2align 2
3:
[hjl@gnu-cfl-1 note]$ make
as -mx86-used-note=yes  -o y.o y.s
as -mx86-used-note=yes  -o x.o x.s
as -mx86-used-note=yes  -o z.o z.s
./ld -z noseparate-code --build-id -o x y.o x.o z.o
readelf -lW x

Elf file type is EXEC (Executable file)
Entry point 0x400224
There are 5 program headers, starting at offset 64

Program Headers:
  Type           Offset   VirtAddr           PhysAddr           FileSiz  MemSiz   Flg Align
  LOAD           0x000000 0x0000000000400000 0x0000000000400000 0x000225 0x000225 R E 0x1000
  NOTE           0x000158 0x0000000000400158 0x0000000000400158 0x000028 0x000028 R   0x4
  NOTE           0x000180 0x0000000000400180 0x0000000000400180 0x000030 0x000030 R   0x8
  NOTE           0x0001b0 0x00000000004001b0 0x00000000004001b0 0x000028 0x000028 R   0x8
  NOTE           0x0001d8 0x00000000004001d8 0x00000000004001d8 0x00004c 0x00004c R   0x4

 Section to Segment mapping:
  Segment Sections...
   00     .note.2 .note.gnu.property .note.1 .note.3 .note.gnu.build-id .text 
   01     .note.2 
   02     .note.gnu.property 
   03     .note.1 
   04     .note.3 .note.gnu.build-id 
[hjl@gnu-cfl-1 note]$ 

We can create just 2 PT_NOTE segments, instead of 4.
Comment 1 Sourceware Commits 2018-10-03 20:23:35 UTC
The master branch has been updated by H.J. Lu <hjl@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=23e463ed7c0d289e2291aaefd576bf02efd98df8

commit 23e463ed7c0d289e2291aaefd576bf02efd98df8
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Wed Oct 3 13:22:26 2018 -0700

    ELF: Group PT_NOTE segments by section alignments
    
    Alignments of SHT_NOTE sections can be 8 bytes for 64-bit ELF files.  We
    should put all adjacent SHT_NOTE sections with the same section alignment
    into a single PT_NOTE segment even when the section alignment != 4 bytes.
    Also check SHT_NOTE section type instead of section name.
    
    	PR ld/23658
    	* elf.c (get_program_header_size): Put all adjacent SHT_NOTE
    	sections with the same section alignment into a single PT_NOTE
    	segment.  Check SHT_NOTE section type instead of section name.
    	(_bfd_elf_map_sections_to_segments): Likewise.
Comment 2 Sourceware Commits 2018-10-03 20:26:28 UTC
The master branch has been updated by H.J. Lu <hjl@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=1887ae7304dced5b081ee200a9e27fbb8f180143

commit 1887ae7304dced5b081ee200a9e27fbb8f180143
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Wed Oct 3 13:25:16 2018 -0700

    ELF: Group and sort output note sections by section alignments
    
    To support putting all adjacent SHT_NOTE sections with the same section
    alignment into a single PT_NOTE segment, lang_insert_orphan must group
    and sort output note sections by section alignments in both output
    section list as well as output section statement list.
    
    	PR ld/23658
    	* ldlang.c (lang_insert_orphan): Group and sort output note
    	sections by section alignments.
Comment 3 Sourceware Commits 2018-10-03 21:12:42 UTC
The master branch has been updated by H.J. Lu <hjl@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=fb9bbfd7f2d39a1d4850fcf0f63ef1f077f55e4b

commit fb9bbfd7f2d39a1d4850fcf0f63ef1f077f55e4b
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Wed Oct 3 14:11:43 2018 -0700

    ELF: Add testcases for PR ld/23658
    
    Add testcases to verify that all SHT_NOTE sections with the same section
    alignment are placed in a single PT_NOTE segment.
    
    	PR ld/23658
    	* testsuite/ld-elf/pr23658-1.d: New file.
    	* testsuite/ld-elf/pr23658-1a.s: Likewise.
    	* testsuite/ld-elf/pr23658-1b.s: Likewise.
    	* testsuite/ld-elf/pr23658-1c.s: Likewise.
    	* testsuite/ld-elf/pr23658-1d.s: Likewise.
    	* testsuite/ld-elf/pr23658-2.rd: Likewise.
    	* testsuite/ld-elf/pr23658-3.d: Likewise.
    	* testsuite/ld-elf/pr23658-3.s: Likewise.
    	* testsuite/ld-elf/pr23658-3.t: Likewise.
    	* testsuite/ld-elf/shared.exp: Run PR ld/23658 tests.
Comment 4 H.J. Lu 2018-10-03 21:24:29 UTC
Fixed.
Comment 5 Sourceware Commits 2018-10-04 19:08:14 UTC
The master branch has been updated by H.J. Lu <hjl@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=b35334aa98b75e4e166d30d673fd3bf1238dc7b7

commit b35334aa98b75e4e166d30d673fd3bf1238dc7b7
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Thu Oct 4 12:06:33 2018 -0700

    pr23658-1.d: Also xfail ft32-* and xgate-*
    
    Since ft32-* and xgate-* use generic linker, SHT_NOTE sections aren't
    grouped nor sorted.
    
    	PR ld/23658
    	* testsuite/ld-elf/pr23658-1.d: Also xfail ft32-* and xgate-*.
Comment 6 H.J. Lu 2018-10-04 20:06:31 UTC
It isn't right for -shared:

/export/build/gnu/binutils/build-x86_64-linux/ld/../gas/as-new  --defsym ALIGN=3 -mx86-used-note=no   -o tmpdir/pr23658-1a.o /export/gnu/import/git/sources/binutils-gdb/ld/testsuite/ld-elf/pr23658-1a.s
/export/build/gnu/binutils/build-x86_64-linux/ld/../gas/as-new  --defsym ALIGN=3 -mx86-used-note=no   -o tmpdir/pr23658-1b.o /export/gnu/import/git/sources/binutils-gdb/ld/testsuite/ld-elf/pr23658-1b.s
/export/build/gnu/binutils/build-x86_64-linux/ld/../gas/as-new  --defsym ALIGN=3 -mx86-used-note=no   -o tmpdir/pr23658-1c.o /export/gnu/import/git/sources/binutils-gdb/ld/testsuite/ld-elf/pr23658-1c.s
/export/build/gnu/binutils/build-x86_64-linux/ld/../gas/as-new  --defsym ALIGN=3 -mx86-used-note=no   -o tmpdir/pr23658-1d.o /export/gnu/import/git/sources/binutils-gdb/ld/testsuite/ld-elf/pr23658-1d.s
/export/build/gnu/binutils/build-x86_64-linux/ld/../gas/as-new  --defsym ALIGN=3 -mx86-used-note=no   -o tmpdir/start.o /export/gnu/import/git/sources/binutils-gdb/ld/testsuite/ld-elf/start.s
./ld-new -z norelro  -L/export/gnu/import/git/sources/binutils-gdb/ld/testsuite/ld-elf  --build-id -shared -o tmpdir/dump tmpdir/pr23658-1a.o tmpdir/pr23658-1b.o tmpdir/pr23658-1c.o tmpdir/pr23658-1d.o tmpdir/start.o 
/export/build/gnu/binutils/build-x86_64-linux/ld/../binutils/readelf -S  -l --wide tmpdir/dump
There are 16 section headers, starting at offset 0x2550:

Section Headers:
  [Nr] Name              Type            Address          Off    Size   ES Flg Lk Inf Al
  [ 0]                   NULL            0000000000000000 000000 000000 00      0   0  0
  [ 1] .note.4           NOTE            0000000000002000 002000 000028 00   A  0   0  8
  [ 2] .note.1           NOTE            0000000000002028 002028 000028 00   A  0   0  8
  [ 3] .note.gnu.build-id NOTE            0000000000000200 000200 000024 00   A  0   0  4
  [ 4] .note.2           NOTE            0000000000002050 002050 000028 00   A  0   0  4
  [ 5] .note.3           NOTE            0000000000002078 002078 000028 00   A  0   0  4
  [ 6] .hash             HASH            0000000000000228 000228 000030 04   A  8   0  8
  [ 7] .gnu.hash         GNU_HASH        0000000000000258 000258 00003c 00   A  8   0  8
  [ 8] .dynsym           DYNSYM          0000000000000298 000298 0000a8 18   A  9   1  8
  [ 9] .dynstr           STRTAB          0000000000000340 000340 000013 00   A  0   0  1
  [10] .text             PROGBITS        0000000000001000 001000 000010 00  AX  0   0  1
  [11] .eh_frame         PROGBITS        00000000000020a0 0020a0 000000 00   A  0   0  8
  [12] .dynamic          DYNAMIC         00000000000030a0 0020a0 0000c0 10  WA  9   0  8
  [13] .symtab           SYMTAB          0000000000000000 002160 0002e8 18     14  25  8
  [14] .strtab           STRTAB          0000000000000000 002448 000081 00      0   0  1
  [15] .shstrtab         STRTAB          0000000000000000 0024c9 000081 00      0   0  1
Key to Flags:
  W (write), A (alloc), X (execute), M (merge), S (strings), I (info),
  L (link order), O (extra OS processing required), G (group), T (TLS),
  C (compressed), x (unknown), o (OS specific), E (exclude),
  l (large), p (processor specific)

Elf file type is DYN (Shared object file)
Entry point 0x1008
There are 8 program headers, starting at offset 64

Program Headers:
  Type           Offset   VirtAddr           PhysAddr           FileSiz  MemSiz   Flg Align
  LOAD           0x000000 0x0000000000000000 0x0000000000000000 0x000353 0x000353 R   0x1000
  LOAD           0x001000 0x0000000000001000 0x0000000000001000 0x000010 0x000010 R E 0x1000
  LOAD           0x002000 0x0000000000002000 0x0000000000002000 0x0000a0 0x0000a0 R   0x1000
  LOAD           0x0020a0 0x00000000000030a0 0x00000000000030a0 0x0000c0 0x0000c0 RW  0x1000
  DYNAMIC        0x0020a0 0x00000000000030a0 0x00000000000030a0 0x0000c0 0x0000c0 RW  0x8
  NOTE           0x002000 0x0000000000002000 0x0000000000002000 0x000050 0x000050 R   0x8
  NOTE           0x000200 0x0000000000000200 0x0000000000000200 0x000024 0x000024 R   0x4
  NOTE           0x002050 0x0000000000002050 0x0000000000002050 0x000050 0x000050 R   0x4

 Section to Segment mapping:
  Segment Sections...
   00     .note.gnu.build-id .hash .gnu.hash .dynsym .dynstr 
   01     .text 
   02     .note.4 .note.1 .note.2 .note.3 
   03     .dynamic 
   04     .dynamic 
   05     .note.4 .note.1 
   06     .note.gnu.build-id 
   07     .note.2 .note.3
Comment 7 H.J. Lu 2018-10-04 21:12:25 UTC
gld${EMULATION_NAME}_place_orphan has

  else if ((flags & SEC_LOAD) != 0 
           && ((elfinput && sh_type == SHT_NOTE)
               || (!elfinput && CONST_STRNEQ (secname, ".note"))))
    place = &hold[orphan_interp];

linker script for -shared has

SECTIONS
{
  /* Read-only sections, merged into text segment: */
  . = SEGMENT_START("text-segment", 0) + SIZEOF_HEADERS;
  .note.gnu.build-id : { *(.note.gnu.build-id) }
  .hash           : { *(.hash) }
  .gnu.hash       : { *(.gnu.hash) }

There is no way to place a note section before .note.gnu.build-id in
output statements since lang_insert_orphan can only insert an output
statement after another output statement, not before another output
statement.
Comment 8 Sourceware Commits 2018-10-08 11:43:14 UTC
The master branch has been updated by H.J. Lu <hjl@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=f3e660db14a0a95f3953496d8beb7c58ef34c6d5

commit f3e660db14a0a95f3953496d8beb7c58ef34c6d5
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Mon Oct 8 04:39:09 2018 -0700

    ELF: Properly group and place orphan note sections
    
    Properly group orphan note sections.  When placing orphan note section
    as the first note section, place it after the section before all note
    sections.
    
    	PR ld/23658
    	* ldlang.c (lang_insert_orphan): Properly group and place orphan
    	note sections.  Properly handle orphan note section before all
    	note sections.
    	* testsuite/ld-elf/pr23658-1.d: Renamed to ...
    	* testsuite/ld-elf/pr23658-1a.d: This.  Updated.
    	* testsuite/ld-elf/pr23658-1b.d: New test.
    	* testsuite/ld-elf/pr23658-1c.d: Likewise.
Comment 9 H.J. Lu 2018-10-08 11:53:09 UTC
Fixed for 2.32.
Comment 10 Zak Bolton 2021-11-16 05:54:18 UTC Comment hidden (spam)
Comment 11 Zak Bolton 2021-11-16 05:54:54 UTC Comment hidden (spam)