Index: lld/ELF/Writer.cpp =================================================================== --- lld/ELF/Writer.cpp +++ lld/ELF/Writer.cpp @@ -2044,15 +2044,25 @@ if (Config->ZWxneeded) AddHdr(PT_OPENBSD_WXNEEDED, PF_X); - // Create one PT_NOTE per a group of contiguous .note sections. - PhdrEntry *Note = nullptr; - for (OutputSection *Sec : OutputSections) { - if (Sec->Type == SHT_NOTE && (Sec->Flags & SHF_ALLOC)) { - if (!Note || Sec->LMAExpr) - Note = AddHdr(PT_NOTE, PF_R); - Note->add(Sec); - } else { - Note = nullptr; + bool UseSimpleNoteLayout = false; + for (OutputSection *Sec : OutputSections) + if (Sec->Alignment > 4) + UseSimpleNoteLayout = true; + if (UseSimpleNoteLayout) { + // Create one PT_NOTE per note section to handle larger alignments. + for (OutputSection *Sec : OutputSections) + if (Sec->Type == SHT_NOTE && (Sec->Flags & SHF_ALLOC)) + AddHdr(PT_NOTE, PF_R)->add(Sec); + } else { + PhdrEntry *Note = nullptr; + for (OutputSection *Sec : OutputSections) { + if (Sec->Type == SHT_NOTE && (Sec->Flags & SHF_ALLOC)) { + if (!Note || Sec->LMAExpr) + Note = AddHdr(PT_NOTE, PF_R); + Note->add(Sec); + } else { + Note = nullptr; + } } } return Ret; Index: lld/test/ELF/build-id.s =================================================================== --- lld/test/ELF/build-id.s +++ lld/test/ELF/build-id.s @@ -50,6 +50,18 @@ .section .note.test, "a", @note .quad 42 +# ALIGN: Name: .note.test +# ALIGN-NEXT: Type: SHT_NOTE +# ALIGN-NEXT: Flags [ +# ALIGN-NEXT: SHF_ALLOC +# ALIGN-NEXT: ] +# ALIGN-NEXT: Address: +# ALIGN-NEXT: Offset: +# ALIGN-NEXT: Size: +# ALIGN-NEXT: Link: +# ALIGN-NEXT: Info: +# ALIGN-NEXT: AddressAlignment: 1 + # ALIGN: Name: .note.gnu.build-id # ALIGN-NEXT: Type: SHT_NOTE # ALIGN-NEXT: Flags [ @@ -65,15 +77,15 @@ # DEFAULT: Contents of section .note.test: # DEFAULT: Contents of section .note.gnu.build-id: # DEFAULT-NEXT: 04000000 08000000 03000000 474e5500 ............GNU. -# DEFAULT-NEXT: 894c04e8 fbf5556b +# DEFAULT-NEXT: 95849665 2621c734 # MD5: Contents of section .note.gnu.build-id: # MD5-NEXT: 04000000 10000000 03000000 474e5500 ............GNU. -# MD5-NEXT: 6a51bbd7 9e8ee3f9 2e02d213 711cfec9 +# MD5-NEXT: 1882c01f 71698eed 229b3994 eb554c80 # SHA1: Contents of section .note.gnu.build-id: # SHA1-NEXT: 04000000 14000000 03000000 474e5500 ............GNU. -# SHA1-NEXT: 9a8618b1 d6fd0e5c eda73dd8 76de5596 +# SHA1-NEXT: 96820adf d90d5470 0a0c32ff a88c4017 # UUID: Contents of section .note.gnu.build-id: # UUID-NEXT: 04000000 10000000 03000000 474e5500 ............GNU. Index: lld/test/ELF/multiple-notes.s =================================================================== --- /dev/null +++ lld/test/ELF/multiple-notes.s @@ -0,0 +1,58 @@ +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t + +# RUN: ld.lld %t -o %t2 +# RUN: llvm-readobj -l %t2 | FileCheck %s + +.globl _start +_start: + nop + +.section .note1, "a", @note + .align 4 + .quad 0 +.section .note2, "a", @note + .align 16 + .quad 0 +.section .note3, "a", @note + .align 4 + .quad 0 + +# Test that each section creates a + +#CHECK: ProgramHeader { +#CHECK: Type: PT_NOTE (0x4) +#CHECK-NEXT: Offset: +#CHECK-NEXT: VirtualAddress: +#CHECK-NEXT: PhysicalAddress: +#CHECK-NEXT: FileSize: 8 +#CHECK-NEXT: MemSize: 8 +#CHECK-NEXT: Flags [ (0x4) +#CHECK-NEXT: PF_R (0x4) +#CHECK-NEXT: ] +#CHECK-NEXT: Alignment: 4 +#CHECK-NEXT: } +#CHECK-NEXT: ProgramHeader { +#CHECK-NEXT: Type: PT_NOTE (0x4) +#CHECK-NEXT: Offset: +#CHECK-NEXT: VirtualAddress: +#CHECK-NEXT: PhysicalAddress: +#CHECK-NEXT: FileSize: 8 +#CHECK-NEXT: MemSize: 8 +#CHECK-NEXT: Flags [ (0x4) +#CHECK-NEXT: PF_R (0x4) +#CHECK-NEXT: ] +#CHECK-NEXT: Alignment: 16 +#CHECK-NEXT: } +#CHECK-NEXT: ProgramHeader { +#CHECK-NEXT: Type: PT_NOTE (0x4) +#CHECK-NEXT: Offset: +#CHECK-NEXT: VirtualAddress: +#CHECK-NEXT: PhysicalAddress: +#CHECK-NEXT: FileSize: 8 +#CHECK-NEXT: MemSize: 8 +#CHECK-NEXT: Flags [ (0x4) +#CHECK-NEXT: PF_R (0x4) +#CHECK-NEXT: ] +#CHECK-NEXT: Alignment: 4 +#CHECK-NEXT: } +#CHECK-NEXT:] Index: lld/test/ELF/note-contiguous.s =================================================================== --- lld/test/ELF/note-contiguous.s +++ /dev/null @@ -1,42 +0,0 @@ -// REQUIRES: x86 -// RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o - -// RUN: ld.lld %t.o -o %t1 -// RUN: llvm-readobj -program-headers %t1 | FileCheck %s - -// CHECK: Type: PT_NOTE -// CHECK-NEXT: Offset: -// CHECK-NEXT: VirtualAddress: -// CHECK-NEXT: PhysicalAddress: -// CHECK-NEXT: FileSize: 16 -// CHECK-NEXT: MemSize: 16 -// CHECK-NEXT: Flags [ -// CHECK-NEXT: PF_R -// CHECK-NEXT: ] -// CHECK-NEXT: Alignment: 1 -// CHECK-NOT: Type: PT_NOTE - -// RUN: echo "SECTIONS { .note : { *(.note.a) *(.note.b) } }" > %t.script -// RUN: ld.lld %t.o --script %t.script -o %t2 -// RUN: llvm-readobj -program-headers %t2 | FileCheck -check-prefix=SCRIPT %s - -// SCRIPT: Type: PT_NOTE -// SCRIPT-NEXT: Offset: -// SCRIPT-NEXT: VirtualAddress: -// SCRIPT-NEXT: PhysicalAddress: -// SCRIPT-NEXT: FileSize: 16 -// SCRIPT-NEXT: MemSize: 16 -// SCRIPT-NEXT: Flags [ -// SCRIPT-NEXT: PF_R -// SCRIPT-NEXT: ] -// SCRIPT-NEXT: Alignment: 1 -// SCRIPT-NOT: Type: PT_NOTE - -.section .note.a, "a", @note -.quad 0 - -.section .foo, "a" -.quad 0 - -.section .note.b, "a", @note -.quad 0