[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
This commit is contained in:
Ed Maste 2018-05-10 11:12:18 +00:00
parent d621037788
commit 6556aa6848
2 changed files with 23 additions and 1 deletions

View File

@ -1866,7 +1866,7 @@ template <class ELFT> std::vector<PhdrEntry *> Writer<ELFT>::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);

View File

@ -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