Bug 23900 - Can't detect GNU program property generated by older linkers
Summary: Can't detect GNU program property generated by older linkers
Status: RESOLVED FIXED
Alias: None
Product: binutils
Classification: Unclassified
Component: ld (show other bugs)
Version: 2.32
: P2 normal
Target Milestone: 2.33
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2018-11-20 14:19 UTC by H.J. Lu
Modified: 2023-01-23 16:39 UTC (History)
0 users

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-11-20 14:19:16 UTC
Linkers group input note sections with the same name into one output
note section with the same name.  One output note section is placed in
one PT_NOTE segment.  Since new linkers merge input .note.gnu.property
sections into one output .note.gnu.property section, there is only
one NT_GNU_PROPERTY_TYPE_0 note in one PT_NOTE segment with new linkers.
Since older linkers treat input .note.gnu.property section as a generic
note section and just concatenate all input .note.gnu.property sections
into one output .note.gnu.property section without merging them, we may
see invalid NT_GNU_PROPERTY_TYPE_0 note in PT_NOTE segment:

[hjl@gnu-4 tmp]$ cat x.S
#ifdef __LP64__
# define ALIGN 3
#else
# define ALIGN 2
#endif
	.section ".note", "a"
	.p2align ALIGN
	.long 1f - 0f		/* name length.  */
	.long 3f - 1f		/* data length.  */
	/* NT_GNU_PROPERTY_TYPE_0 */
	.long 5			/* note type.  */
0:
	.asciz "GNU"		/* vendor name.  */
1:
	.p2align ALIGN
	/* GNU_PROPERTY_X86_ISA_1_USED */
	.long 0xc0010000	/* pr_type.  */
	.long 5f - 4f		/* pr_datasz.  */
4:
	.long 0x80000002
5:
	.p2align ALIGN
	/* GNU_PROPERTY_X86_FEATURE_2_USED */
	.long 0xc0010001	/* pr_type.  */
	.long 5f - 4f		/* pr_datasz.  */
4:
	.long 0x9
5:
	.p2align ALIGN
3:

	.text
	.globl _start
_start:
	ret
[hjl@gnu-4 tmp]$ gcc -c x.S -Wa,-mx86-used-note=no
[hjl@gnu-4 tmp]$ ld -o x x.o
[hjl@gnu-4 tmp]$ cat y.s 
	.text
	.globl _start
_start:
	movaps	%xmm0, %xmm1
[hjl@gnu-4 tmp]$ gcc -c y.s -Wa,-mx86-used-note=yes 
[hjl@gnu-4 tmp]$ ld -o y y.o
[hjl@gnu-4 tmp]$ readelf -n x

Displaying notes found in: .note
  Owner                 Data size	Description
  GNU                  0x00000020	NT_GNU_PROPERTY_TYPE_0
      Properties: x86 ISA used: SSE
	Invalid x86 feature used: x86, XMM
[hjl@gnu-4 tmp]$ readelf -n y

Displaying notes found in: .note.gnu.property
  Owner                 Data size	Description
  GNU                  0x00000020	NT_GNU_PROPERTY_TYPE_0
      Properties: x86 ISA used: SSE
	x86 feature used: x86, XMM
[hjl@gnu-4 tmp]$ 

'x' has invalid NT_GNU_PROPERTY_TYPE_0 note. But GNU_PROPERTY_X86_ISA_1_USED
has the GNU_PROPERTY_X86_UINT32_VALID bit set, which can also be set by
-mx86-used-note=yes.  We need a way to mark NT_GNU_PROPERTY_TYPE_0 note in
PT_NOTE segment is valid.
Comment 1 Sourceware Commits 2018-12-14 12:56:37 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=0a59decbb81676ac30deede1bb6b6e241cd75502

commit 0a59decbb81676ac30deede1bb6b6e241cd75502
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Fri Dec 14 04:55:08 2018 -0800

    elf: Add PT_GNU_PROPERTY segment type
    
    Linkers group input note sections with the same name into one output
    note section with the same name.  One output note section is placed in
    one PT_NOTE segment.  New linkers merge all input .note.gnu.property
    sections into one output .note.gnu.property section with a single
    NT_GNU_PROPERTY_TYPE_0 note in a single PT_NOTE segment.  Since older
    linkers treat input .note.gnu.property section as a generic note section
    and just concatenate all input .note.gnu.property sections into one
    output .note.gnu.property section without merging them, we may
    see one or more NT_GNU_PROPERTY_TYPE_0 notes in PT_NOTE segment, which
    are invalid.
    
    GNU_PROPERTY_X86_UINT32_VALID was defined to address this issue such
    that linker sets the bit for non-relocatable outputs.  But it isn't
    sufficient:
    
    1. It doesn't cover generic properties.
    2. When -mx86-used-note=yes is passed to x86 assembler, the
    GNU_PROPERTY_X86_UINT32_VALID bit is set in GNU_PROPERTY_X86_ISA_1_USED
    property in object file and older linkers generate invalid
    NT_GNU_PROPERTY_TYPE_0 notes with the GNU_PROPERTY_X86_UINT32_VALID bit
    set.
    
    I am proposing the following changes:
    
    1. Add PT_GNU_PROPERTY segment type:
    
     # define PT_GNU_PROPERTY (PT_LOOS + 0x474e553)
    
    which covers .note.gnu.property section.
    2. Remove GNU_PROPERTY_X86_UINT32_VALID.
    
    bfd/
    
    	PR ld/23900
    	* elf.c (get_program_header_size): Add a PT_GNU_PROPERTY
    	segment for NOTE_GNU_PROPERTY_SECTION_NAME.
    	(_bfd_elf_map_sections_to_segments): Create a PT_GNU_PROPERTY
    	segment for NOTE_GNU_PROPERTY_SECTION_NAME.
    	* elfxx-x86.c (_bfd_elf_link_setup_gnu_properties): Don't set
    	GNU_PROPERTY_X86_UINT32_VALID.
    
    binutils/
    
    	PR ld/23900
    	* readelf.c (get_segment_type): Support PT_GNU_PROPERTY.
    	(decode_x86_isa): Don't check GNU_PROPERTY_X86_UINT32_VALID.
    	(decode_x86_feature_1): Likewise.
    	(decode_x86_feature_2): Likewise.
    	(print_gnu_property_note): Remove GNU_PROPERTY_X86_UINT32_VALID
    	check.
    	* testsuite/binutils-all/i386/empty.d: Updated.
    	* testsuite/binutils-all/x86-64/empty-x32.d: Likewise.
    	* testsuite/binutils-all/x86-64/empty.d: Likewise.
    	* testsuite/binutils-all/i386/pr21231b.s: Change
    	GNU_PROPERTY_X86_ISA_1_USED bits to 0x7fffffff.
    	* testsuite/binutils-all/x86-64/pr21231b.s: Likewise.
    
    gas/
    
    	PR ld/23900
    	* config/tc-i386.c (x86_cleanup): Don't set
    	GNU_PROPERTY_X86_UINT32_VALID.
    	* testsuite/gas/i386/property-1.s: Change
    	GNU_PROPERTY_X86_ISA_1_USED bits to 0.
    
    include/
    
    	PR ld/23900
    	* elf/common.h (PT_GNU_PROPERTY): New.
    	(GNU_PROPERTY_X86_UINT32_VALID): Removed.
    
    ld/
    
    	PR ld/23900
    	* testsuite/ld-elf/elf.exp: Run PR ld/23900 test.
    	* testsuite/ld-elf/pr23900-1-32.rd: New file.
    	* testsuite/ld-elf/pr23900-1-64.rd: Likewise.
    	* testsuite/ld-elf/pr23900-1.d: Likewise.
    	* testsuite/ld-elf/pr23900-1.s: Likewise.
    	* testsuite/ld-elf/pr23900-2.s: Likewise.
    	* testsuite/ld-elf/pr23900-2a.d: Likewise.
    	* testsuite/ld-elf/pr23900-2b.d: Likewise.
    	* testsuite/ld-i386/ibt-plt-1.d: Adjusted.
    	* testsuite/ld-i386/ibt-plt-2c.d: Likewise.
    	* testsuite/ld-i386/ibt-plt-2d.d: Likewise.
    	* testsuite/ld-i386/ibt-plt-3d.d: Likewise.
    	* testsuite/ld-x86-64/ibt-plt-1-x32.d: Likewise.
    	* testsuite/ld-x86-64/ibt-plt-1.d: Likewise.
    	* testsuite/ld-x86-64/ibt-plt-2c-x32.d: Likewise.
    	* testsuite/ld-x86-64/ibt-plt-2c.d: Likewise.
    	* testsuite/ld-x86-64/ibt-plt-2d-x32.d: Likewise.
    	* testsuite/ld-x86-64/ibt-plt-2c.d: Likewise.
    	* testsuite/ld-x86-64/ibt-plt-3c-x32.d: Likewise.
    	* testsuite/ld-x86-64/ibt-plt-3c.d: Likewise.
    	* testsuite/ld-x86-64/ibt-plt-3d-x32.d: Likewise.
    	* testsuite/ld-x86-64/ibt-plt-3d.d: Likewise.
    	* testsuite/ld-i386/pr23372c.d: Expect <None>
    	for GNU_PROPERTY_X86_ISA_1_USED.
    	* testsuite/ld-x86-64/pr23372c-x32.d: Likewise.
    	* testsuite/ld-x86-64/pr23372c.d: Likewise.
    	* testsuite/ld-x86-64/pr23372d-x32.d: Likewise.
    	* testsuite/ld-x86-64/pr23372d.d: Likewise.
    	* testsuite/ld-x86-64/property-x86-5a.s: Change
    	GNU_PROPERTY_X86_ISA_1_USED bits to 0.
    	* testsuite/ld-x86-64/property-x86-5b.s: Likewise.
Comment 2 Sourceware Commits 2020-01-21 13:47:52 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=b4654b109bd023d0a22f445db7d4e27f769593f4

commit b4654b109bd023d0a22f445db7d4e27f769593f4
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Tue Jan 21 05:43:59 2020 -0800

    pr23900-1.d: Also check PT_GNU_PROPERTY program header
    
    Also pass -l to readelf to check PT_GNU_PROPERTY program header.
    
    	PR ld/23900
    	* testsuite/ld-elf/pr23900-1.d: Also pass -l to readelf.
Comment 3 H.J. Lu 2023-01-23 16:39:16 UTC
Fixed.