From 6556aa6848926a7defd52fdd80f1c25dcc6275bc Mon Sep 17 00:00:00 2001 From: Ed Maste Date: Thu, 10 May 2018 11:12:18 +0000 Subject: [PATCH] [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 llvm-svn: 331973 --- lld/ELF/Writer.cpp | 2 +- lld/test/ELF/note-noalloc.s | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 lld/test/ELF/note-noalloc.s diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index 3c76d63..bde43f3 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -1866,7 +1866,7 @@ template std::vector Writer::createPhdrs() { // 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); diff --git a/lld/test/ELF/note-noalloc.s b/lld/test/ELF/note-noalloc.s new file mode 100644 index 0000000..e43ba4f6 --- /dev/null +++ b/lld/test/ELF/note-noalloc.s @@ -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",@note + .quad 1234 + + .section .debug.ghc-link-info,"",@note + .quad 5678 -- 2.7.4