[lld] r331973 - [ELF] Omit PT_NOTE for SHT_NOTE without SHF_ALLOC

Ed Maste via llvm-commits llvm-commits at lists.llvm.org
Thu May 10 04:12:18 PDT 2018


Author: emaste
Date: Thu May 10 04:12:18 2018
New Revision: 331973

URL: http://llvm.org/viewvc/llvm-project?rev=331973&view=rev
Log:
[ELF] Omit PT_NOTE for SHT_NOTE without SHF_ALLOC

A non-alloc note section should not have a PT_NOTE program header.

Found while linking ghc (Haskell compiler) with lld on FreeBSD.
ghc emits a .debug-ghc-link-info note section (as the name suggests, it
contains link information) as a SHT_NOTE section without SHF_ALLOC set.

For this case ld.bfd does not emit a PT_NOTE segment for the
.debug-ghc-link-info section.  lld previously emitted a PT_NOTE with
p_vaddr = 0 and FreeBSD's rtld segfaulted when trying to parse a note at
address 0.

llvm.org/pr37361

Differential Revision:	https://reviews.llvm.org/D46623

Added:
    lld/trunk/test/ELF/note-noalloc.s
Modified:
    lld/trunk/ELF/Writer.cpp

Modified: lld/trunk/ELF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=331973&r1=331972&r2=331973&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Thu May 10 04:12:18 2018
@@ -1866,7 +1866,7 @@ template <class ELFT> std::vector<PhdrEn
   // Create one PT_NOTE per a group of contiguous .note sections.
   PhdrEntry *Note = nullptr;
   for (OutputSection *Sec : OutputSections) {
-    if (Sec->Type == SHT_NOTE) {
+    if (Sec->Type == SHT_NOTE && (Sec->Flags & SHF_ALLOC)) {
       if (!Note || Sec->LMAExpr)
         Note = AddHdr(PT_NOTE, PF_R);
       Note->add(Sec);

Added: lld/trunk/test/ELF/note-noalloc.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/note-noalloc.s?rev=331973&view=auto
==============================================================================
--- lld/trunk/test/ELF/note-noalloc.s (added)
+++ lld/trunk/test/ELF/note-noalloc.s Thu May 10 04:12:18 2018
@@ -0,0 +1,22 @@
+// REQUIRES: x86
+// RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-freebsd %s -o %t.o
+// RUN: ld.lld %t.o -o %t -shared
+// RUN: llvm-readobj -program-headers -sections %t | FileCheck %s
+
+// PR37361: A note without SHF_ALLOC should not create a PT_NOTE program
+// header (but should have a SHT_NOTE section).
+
+// CHECK: Name: .note.tag
+// CHECK: Type: SHT_NOTE
+// CHECK: Name: .debug.ghc-link-info
+// CHECK: Type: SHT_NOTE
+// CHECK-NOT: Type: SHT_NOTE
+
+// CHECK: Type: PT_NOTE
+// CHECK-NOT: Type: PT_NOTE
+
+        .section        .note.tag,"a", at note
+        .quad 1234
+
+        .section        .debug.ghc-link-info,"", at note
+        .quad 5678




More information about the llvm-commits mailing list