[PATCH] D61296: [ELF] Place SHT_NOTE sections with the same alignments into one PT_NOTE

Fangrui Song via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 30 00:02:44 PDT 2019


MaskRay created this revision.
MaskRay added reviewers: ruiu, jakehehrlich, phosek, jhenderson, pcc.
Herald added subscribers: llvm-commits, fedor.sergeev, krytarowski, arichardson, emaste.
Herald added a reviewer: espindola.
Herald added a project: LLVM.

While the generic ABI requires notes to be 8-byte aligned in ELF64, many
vendor-specific notes (from Linux, NetBSD, Solaris, etc) use 4-byte
alignment.

In a PT_NOTE segment, if 4-byte aligned notes are followed by an 8-byte
aligned note, the probably 4-byte padding may make consumers fail to
parse the 8-byte aligned note. See PR41000 for the recent report about
.note.gnu.property (NT_GNU_PROPERTY_TYPE_0).

To fix the issue, this patches disallows notes with different alignments
from being placed in the same PT_NOTE. If compilers emit 4-byte aligned
notes before 8-byte aligned notes, we'll create at most 2 segments.

We don't have to check whether sh_size is a multiple of sh_addralign.
It is unrealistic and it shouldn't cause parsing issues anyway.

An alternative approach is to create a PT_NOTE for each SHT_NOTE, but
we'll have to incur the sizeof(Elf64_Phdr) = 56 overhead every time a
new note section is introduced.


Repository:
  rLLD LLVM Linker

https://reviews.llvm.org/D61296

Files:
  ELF/Writer.cpp
  test/ELF/build-id.s
  test/ELF/note-alignment.s


Index: test/ELF/note-alignment.s
===================================================================
--- /dev/null
+++ test/ELF/note-alignment.s
@@ -0,0 +1,33 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o %t.o
+
+# RUN: ld.lld %t.o -o %t
+# RUN: llvm-readelf -l %t | FileCheck %s
+
+# CHECK: NOTE {{0x[0-9a-f]+}} {{0x[0-9a-f]+}} {{0x[0-9a-f]+}} 0x000004 0x000004 R   0x4
+# CHECK: NOTE {{0x[0-9a-f]+}} {{0x[0-9a-f]+}} {{0x[0-9a-f]+}} 0x000010 0x000010 R   0x8
+# CHECK: NOTE {{0x[0-9a-f]+}} {{0x[0-9a-f]+}} {{0x[0-9a-f]+}} 0x000008 0x000008 R   0x4
+
+# CHECK:      03     .note.a
+# CHECK-NEXT: 04     .note.b .note.c
+# CHECK-NEXT: 05     .note.d .note.e
+
+.section .note.a, "a", @note
+.align 4
+.long 0
+
+.section .note.b, "a", @note
+.align 8
+.quad 0
+
+.section .note.c, "a", @note
+.align 8
+.quad 0
+
+.section .note.d, "a", @note
+.align 4
+.long 0
+
+.section .note.e, "a", @note
+.align 4
+.long 0
Index: test/ELF/build-id.s
===================================================================
--- test/ELF/build-id.s
+++ test/ELF/build-id.s
@@ -65,15 +65,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: ELF/Writer.cpp
===================================================================
--- ELF/Writer.cpp
+++ ELF/Writer.cpp
@@ -2000,11 +2000,12 @@
   if (Config->ZWxneeded)
     AddHdr(PT_OPENBSD_WXNEEDED, PF_X);
 
-  // Create one PT_NOTE per a group of contiguous .note sections.
+  // Create one PT_NOTE per a group of contiguous SHT_NOTE sections with the
+  // same alignment.
   PhdrEntry *Note = nullptr;
   for (OutputSection *Sec : OutputSections) {
     if (Sec->Type == SHT_NOTE && (Sec->Flags & SHF_ALLOC)) {
-      if (!Note || Sec->LMAExpr)
+      if (!Note || Sec->LMAExpr || Note->LastSec->Alignment != Sec->Alignment)
         Note = AddHdr(PT_NOTE, PF_R);
       Note->add(Sec);
     } else {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D61296.197263.patch
Type: text/x-patch
Size: 2631 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190430/184f5db3/attachment.bin>


More information about the llvm-commits mailing list