From f8f6ad74a0994dab30286830b62190185c51c9b2 Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Fri, 15 Apr 2016 12:22:22 +0000 Subject: [PATCH] Don't create relocations for non preemptable absolute symbols. llvm-svn: 266425 --- lld/ELF/Writer.cpp | 12 +++++++++++- lld/test/ELF/Inputs/abs-hidden.s | 3 +++ lld/test/ELF/abs-hidden.s | 27 +++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 lld/test/ELF/Inputs/abs-hidden.s create mode 100644 lld/test/ELF/abs-hidden.s diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index c60cc1b..bead468 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -411,6 +411,16 @@ static int32_t findMipsPairedAddend(const uint8_t *Buf, const uint8_t *BufLoc, return 0; } +// True if non-preemptable symbol always has the same value regardless of where +// the DSO is loaded. +template static bool isAbsolute(const SymbolBody &Body) { + if (Body.isUndefined() && Body.isWeak()) + return true; // always 0 + if (const auto *DR = dyn_cast>(&Body)) + return DR->Section == nullptr; // Absolute symbol. + return false; +} + // The reason we have to do this early scan is as follows // * To mmap the output file, we need to know the size // * For that, we need to know how many dynamic relocs we will have. @@ -618,7 +628,7 @@ void Writer::scanRelocs(InputSectionBase &C, ArrayRef Rels) { // can process some of it and and just ask the dynamic linker to add the // load address. if (!Config->Pic || Target->isRelRelative(Type) || Expr == R_PC || - Expr == R_SIZE || (Body.isUndefined() && Body.isWeak())) { + Expr == R_SIZE || isAbsolute(Body)) { if (Config->EMachine == EM_MIPS && Body.isLocal() && (Type == R_MIPS_GPREL16 || Type == R_MIPS_GPREL32)) Expr = R_MIPS_GP0; diff --git a/lld/test/ELF/Inputs/abs-hidden.s b/lld/test/ELF/Inputs/abs-hidden.s new file mode 100644 index 0000000..44bff38 --- /dev/null +++ b/lld/test/ELF/Inputs/abs-hidden.s @@ -0,0 +1,3 @@ +.global foo +.hidden foo +foo = 0x42 diff --git a/lld/test/ELF/abs-hidden.s b/lld/test/ELF/abs-hidden.s new file mode 100644 index 0000000..543194d --- /dev/null +++ b/lld/test/ELF/abs-hidden.s @@ -0,0 +1,27 @@ +// REQUIRES: x86 +// RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o +// RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %p/Inputs/abs-hidden.s -o %t2.o +// RUN: ld.lld %t.o %t2.o -o %t.so -shared +// RUN: llvm-readobj -r -s -section-data %t.so | FileCheck %s + + .quad foo + +// CHECK: Name: .text +// CHECK-NEXT: Type: SHT_PROGBITS +// CHECK-NEXT: Flags [ +// CHECK-NEXT: SHF_ALLOC +// CHECK-NEXT: SHF_EXECINSTR +// CHECK-NEXT: ] +// CHECK-NEXT: Address: +// CHECK-NEXT: Offset: +// CHECK-NEXT: Size: 8 +// CHECK-NEXT: Link: 0 +// CHECK-NEXT: Info: 0 +// CHECK-NEXT: AddressAlignment: 4 +// CHECK-NEXT: EntrySize: 0 +// CHECK-NEXT: SectionData ( +// CHECK-NEXT: 0000: 42000000 00000000 +// CHECK-NEXT: ) + +// CHECK: Relocations [ +// CHECK-NEXT: ] -- 2.7.4