From e0799a72683e5dd0570537755c5afea409e89484 Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Tue, 30 Oct 2018 20:54:54 +0000 Subject: [PATCH] [ELF] Fallback to sh_link=0 if neither .dynsym nor .symtab exists Summary: .rela.plt may only contain R_*_{,I}RELATIVE relocations and not need a symbol table link. bfd/gold fallbacks to sh_link=0 in this case. Without this patch, ld.lld --strip-all caused lld to dereference a null pointer. Reviewers: ruiu, grimar, espindola Reviewed By: ruiu Subscribers: emaste, arichardson, llvm-commits Differential Revision: https://reviews.llvm.org/D53881 llvm-svn: 345648 --- lld/ELF/SyntheticSections.cpp | 14 +++++++++----- lld/test/ELF/gnu-ifunc-relative.s | 16 +++++++++------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp index 6bf602ce2374..2cca1feda071 100644 --- a/lld/ELF/SyntheticSections.cpp +++ b/lld/ELF/SyntheticSections.cpp @@ -1491,11 +1491,15 @@ void RelocationBaseSection::addReloc(const DynamicReloc &Reloc) { } void RelocationBaseSection::finalizeContents() { - // If all relocations are R_*_RELATIVE they don't refer to any - // dynamic symbol and we don't need a dynamic symbol table. If that - // is the case, just use the index of the regular symbol table section. - getParent()->Link = In.DynSymTab ? In.DynSymTab->getParent()->SectionIndex - : In.SymTab->getParent()->SectionIndex; + // If all relocations are R_*_{,I}RELATIVE they don't refer to any dynamic + // symbol and we don't need a dynamic symbol table. If that is the case, use + // the index of the regular symbol table section (if exists) or 0. + if (In.DynSymTab) + getParent()->Link = In.DynSymTab->getParent()->SectionIndex; + else if (In.SymTab) + getParent()->Link = In.SymTab->getParent()->SectionIndex; + else + getParent()->Link = 0; if (In.RelaIplt == this || In.RelaPlt == this) getParent()->Info = In.GotPlt->getParent()->SectionIndex; diff --git a/lld/test/ELF/gnu-ifunc-relative.s b/lld/test/ELF/gnu-ifunc-relative.s index d797301d03ad..65750ec46f61 100644 --- a/lld/test/ELF/gnu-ifunc-relative.s +++ b/lld/test/ELF/gnu-ifunc-relative.s @@ -1,7 +1,9 @@ // REQUIRES: x86 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o -// RUN: ld.lld -static %t.o -o %tout -// RUN: llvm-readobj -r -t %tout | FileCheck %s +// RUN: ld.lld --strip-all %t.o -o %t +// RUN: llvm-readobj -r %t | FileCheck %s +// RUN: ld.lld %t.o -o %t +// RUN: llvm-readobj -r -t %t | FileCheck %s --check-prefixes=CHECK,SYM .type foo STT_GNU_IFUNC .globl foo @@ -16,8 +18,8 @@ _start: // CHECK-NEXT: R_X86_64_IRELATIVE - 0x[[ADDR:.*]] // CHECK-NEXT: } -// CHECK: Name: foo -// CHECK-NEXT: Value: 0x[[ADDR]] -// CHECK-NEXT: Size: 0 -// CHECK-NEXT: Binding: Global -// CHECK-NEXT: Type: GNU_IFunc +// SYM: Name: foo +// SYM-NEXT: Value: 0x[[ADDR]] +// SYM-NEXT: Size: 0 +// SYM-NEXT: Binding: Global +// SYM-NEXT: Type: GNU_IFunc -- 2.34.1