Revert r327248, "For most Targets the _GLOBAL_OFFSET_TABLE_ symbol is expected to be at"

This change broke ARM code that expects to be able to add
_GLOBAL_OFFSET_TABLE_ to the result of an R_ARM_REL32.

I will provide a reproducer on llvm-commits.

llvm-svn: 327688
This commit is contained in:
Peter Collingbourne 2018-03-16 01:01:44 +00:00
parent 87fe1a79f7
commit 5c902845e5
17 changed files with 42 additions and 85 deletions

View File

@ -50,7 +50,6 @@ template <class ELFT> MIPS<ELFT>::MIPS() {
DefaultMaxPageSize = 65536;
GotEntrySize = sizeof(typename ELFT::uint);
GotPltEntrySize = sizeof(typename ELFT::uint);
GotBaseSymInGotPlt = false;
PltEntrySize = 16;
PltHeaderSize = 32;
CopyRel = R_MIPS_COPY;

View File

@ -21,18 +21,13 @@ using namespace lld::elf;
namespace {
class PPC final : public TargetInfo {
public:
PPC();
PPC() { GotBaseSymOff = 0x8000; }
void relocateOne(uint8_t *Loc, RelType Type, uint64_t Val) const override;
RelExpr getRelExpr(RelType Type, const Symbol &S,
const uint8_t *Loc) const override;
};
} // namespace
PPC::PPC() {
GotBaseSymOff = 0x8000;
GotBaseSymInGotPlt = false;
}
RelExpr PPC::getRelExpr(RelType Type, const Symbol &S,
const uint8_t *Loc) const {
switch (Type) {

View File

@ -46,6 +46,7 @@ public:
} // namespace
X86::X86() {
GotBaseSymOff = -1;
CopyRel = R_386_COPY;
GotRel = R_386_GLOB_DAT;
PltRel = R_386_JUMP_SLOT;

View File

@ -51,6 +51,7 @@ private:
} // namespace
template <class ELFT> X86_64<ELFT>::X86_64() {
GotBaseSymOff = -1;
CopyRel = R_X86_64_COPY;
GotRel = R_X86_64_GLOB_DAT;
PltRel = R_X86_64_JUMP_SLOT;

View File

@ -626,9 +626,8 @@ void GotSection::finalizeContents() { Size = NumEntries * Config->Wordsize; }
bool GotSection::empty() const {
// We need to emit a GOT even if it's empty if there's a relocation that is
// relative to GOT(such as GOTOFFREL) or there's a symbol that points to a GOT
// (i.e. _GLOBAL_OFFSET_TABLE_) that the target defines relative to the .got.
return NumEntries == 0 && !HasGotOffRel &&
!(ElfSym::GlobalOffsetTable && !Target->GotBaseSymInGotPlt);
// (i.e. _GLOBAL_OFFSET_TABLE_).
return NumEntries == 0 && !HasGotOffRel && !ElfSym::GlobalOffsetTable;
}
void GotSection::writeTo(uint8_t *Buf) {
@ -899,14 +898,6 @@ void GotPltSection::writeTo(uint8_t *Buf) {
}
}
bool GotPltSection::empty() const {
// We need to emit a GOT.PLT even if it's empty if there's a symbol that
// references the _GLOBAL_OFFSET_TABLE_ and the Target defines the symbol
// relative to the .got.plt section.
return Entries.empty() &&
!(ElfSym::GlobalOffsetTable && Target->GotBaseSymInGotPlt);
}
// On ARM the IgotPltSection is part of the GotSection, on other Targets it is
// part of the .got.plt
IgotPltSection::IgotPltSection()

View File

@ -270,7 +270,7 @@ public:
void addEntry(Symbol &Sym);
size_t getSize() const override;
void writeTo(uint8_t *Buf) override;
bool empty() const override;
bool empty() const override { return Entries.empty(); }
private:
std::vector<const Symbol *> Entries;

View File

@ -75,10 +75,9 @@ public:
uint64_t getImageBase();
// Offset of _GLOBAL_OFFSET_TABLE_ from base of .got or .got.plt section.
// Offset of _GLOBAL_OFFSET_TABLE_ from base of .got section. Use -1 for
// end of .got
uint64_t GotBaseSymOff = 0;
// True if _GLOBAL_OFFSET_TABLE_ is relative to .got.plt, false if .got.
bool GotBaseSymInGotPlt = true;
// On systems with range extensions we place collections of Thunks at
// regular spacings that enable the majority of branches reach the Thunks.

View File

@ -869,12 +869,11 @@ void Writer<ELFT>::forEachRelSec(std::function<void(InputSectionBase &)> Fn) {
// defining these symbols explicitly in the linker script.
template <class ELFT> void Writer<ELFT>::setReservedSymbolSections() {
if (ElfSym::GlobalOffsetTable) {
// The _GLOBAL_OFFSET_TABLE_ symbol is defined by target convention usually
// to the start of the .got or .got.plt section.
InputSection *GotSection = InX::GotPlt;
if (!Target->GotBaseSymInGotPlt)
GotSection = InX::MipsGot ? cast<InputSection>(InX::MipsGot)
: cast<InputSection>(InX::Got);
// The _GLOBAL_OFFSET_TABLE_ symbol is defined by target convention to
// be at some offset from the base of the .got section, usually 0 or the end
// of the .got
InputSection *GotSection = InX::MipsGot ? cast<InputSection>(InX::MipsGot)
: cast<InputSection>(InX::Got);
ElfSym::GlobalOffsetTable->Section = GotSection;
}

View File

@ -28,17 +28,17 @@ function:
bx lr
// CHECK: Dynamic Relocations {
// CHECK-NEXT: 0x3048 R_ARM_GLOB_DAT function 0x0
// CHECK-NEXT: 0x2048 R_ARM_GLOB_DAT function 0x0
// CHECK: Name: _GLOBAL_OFFSET_TABLE_
// CHECK-NEXT: Value: 0x2000
// CHECK-NEXT: Value: 0x2048
// CHECK-NEXT: Size:
// CHECK-NEXT: Binding: Local
// CHECK-NEXT: Type: None
// CHECK-NEXT: Other [
// CHECK-NEXT: STV_HIDDEN
// CHECK-NEXT: ]
// CHECK-NEXT: Section: .got.plt
// CHECK-NEXT: Section: .got
// CODE: Disassembly of section .text:
// CODE-NEXT: _start:
@ -47,7 +47,7 @@ function:
// CODE-NEXT: 1008: 03 00 8f e0 add r0, pc, r3
// CODE-NEXT: 100c: 1e ff 2f e1 bx lr
// CODE:$d.1:
// (_GLOBAL_OFFSET_TABLE_ = 0x2000) - (0x1008 + 8) = 0xff0
// CODE-NEXT: 1010: f0 0f 00 00
// (_GLOBAL_OFFSET_TABLE_ = 0x2048) - (0x1008 + 8) 0x1038
// CODE-NEXT: 1010: 38 10 00 00
// (Got(function) - GotBase = 0x0
// CODE-NEXT: 1014: 00 00 00 00

View File

@ -3,23 +3,6 @@
// RUN: ld.lld --hash-style=sysv %t.o -o %t.so -shared
// RUN: llvm-readobj -s -l -section-data -r %t.so | FileCheck %s
// CHECK: Name: .got.plt
// CHECK-NEXT: Type: SHT_PROGBITS
// CHECK-NEXT: Flags [
// CHECK-NEXT: SHF_ALLOC
// CHECK-NEXT: SHF_WRITE
// CHECK-NEXT: ]
// CHECK-NEXT: Address:
// CHECK-NEXT: Offset:
// CHECK-NEXT: Size:
// CHECK-NEXT: Link:
// CHECK-NEXT: Info:
// CHECK-NEXT: AddressAlignment:
// CHECK-NEXT: EntrySize:
// CHECK-NEXT: SectionData (
// CHECK-NEXT: 0000: 00300000 00000000 00000000
// CHECK-NEXT: )
// CHECK: Name: .got
// CHECK-NEXT: Type: SHT_PROGBITS
// CHECK-NEXT: Flags [
@ -34,19 +17,19 @@
// CHECK-NEXT: AddressAlignment:
// CHECK-NEXT: EntrySize:
// CHECK-NEXT: SectionData (
// CHECK-NEXT: 0000: 00300000
// CHECK-NEXT: 0000: 00200000 |
// CHECK-NEXT: )
// CHECK: Relocations [
// CHECK-NEXT: Section ({{.*}}) .rel.dyn {
// CHECK-NEXT: 0x3050 R_386_RELATIVE - 0x0
// CHECK-NEXT: 0x2050 R_386_RELATIVE - 0x0
// CHECK-NEXT: }
// CHECK-NEXT: ]
// CHECK: Type: PT_DYNAMIC
// CHECK-NEXT: Offset: 0x3000
// CHECK-NEXT: VirtualAddress: 0x3000
// CHECK-NEXT: PhysicalAddress: 0x3000
// CHECK-NEXT: Offset: 0x2000
// CHECK-NEXT: VirtualAddress: 0x2000
// CHECK-NEXT: PhysicalAddress: 0x2000
calll .L0$pb
.L0$pb:

View File

@ -20,11 +20,11 @@ _start:
.long _GLOBAL_OFFSET_TABLE_ - .
// CHECK: Name: _GLOBAL_OFFSET_TABLE_ (11)
// CHECK-NEXT: Value: 0x20008
// CHECK-NEXT: Value: 0x30090
// CHECK-NEXT: Size: 0
// CHECK-NEXT: Binding: Local (0x0)
// CHECK-NEXT: Type: None (0x0)
// CHECK-NEXT: Other [ (0x2)
// CHECK-NEXT: STV_HIDDEN (0x2)
// CHECK-NEXT: ]
// CHECK-NEXT: Section: .got.plt
// CHECK-NEXT: Section: .got

View File

@ -3,8 +3,7 @@
// RUN: llvm-readobj -t %t2 | FileCheck %s
// REQUIRES: arm
// The ARM _GLOBAL_OFFSET_TABLE_ should be defined at the start of the
// .got.plt section.
// The ARM _GLOBAL_OFFSET_TABLE_ should be defined at the start of the .got
.globl a
.type a,%object
.comm a,4,4
@ -26,11 +25,11 @@ _start:
.data
// CHECK: Name: _GLOBAL_OFFSET_TABLE_
// CHECK-NEXT: Value: 0x2000
// CHECK-NEXT: Value: 0x3068
// CHECK-NEXT: Size: 0
// CHECK-NEXT: Binding: Local
// CHECK-NEXT: Type: None
// CHECK-NEXT: Other [ (0x2)
// CHECK-NEXT: STV_HIDDEN (0x2)
// CHECK-NEXT: ]
// CHECK-NEXT: Section: .got.plt
// CHECK-NEXT: Section: .got

View File

@ -3,8 +3,7 @@
// RUN: llvm-readobj -t %t2 | FileCheck %s
// REQUIRES: x86
// The X86 _GLOBAL_OFFSET_TABLE_ is defined at the start of the .got.plt
// section.
// The X86 _GLOBAL_OFFSET_TABLE_ is defined at the end of the .got section.
.globl a
.type a,@object
.comm a,4,4
@ -22,11 +21,11 @@ addl $_GLOBAL_OFFSET_TABLE_, %eax
calll f@PLT
// CHECK: Name: _GLOBAL_OFFSET_TABLE_ (1)
// CHECK-NEXT: Value: 0x2000
// CHECK-NEXT: Value: 0x306C
// CHECK-NEXT: Size: 0
// CHECK-NEXT: Binding: Local (0x0)
// CHECK-NEXT: Type: None (0x0)
// CHECK-NEXT: Other [ (0x2)
// CHECK-NEXT: STV_HIDDEN (0x2)
// CHECK-NEXT: ]
// CHECK-NEXT: Section: .got.plt
// CHECK-NEXT: Section: .got (0xA)

View File

@ -3,8 +3,7 @@
// RUN: llvm-readobj -t %t2 | FileCheck %s
// REQUIRES: x86
// The X86_64 _GLOBAL_OFFSET_TABLE_ is defined at the start of the .got.plt
// section.
// The X86_64 _GLOBAL_OFFSET_TABLE_ is defined at the end of the .got section.
.globl a
.type a,@object
.comm a,4,4
@ -22,11 +21,11 @@ callq f@PLT
.long _GLOBAL_OFFSET_TABLE_ - .
// CHECK: Name: _GLOBAL_OFFSET_TABLE_
// CHECK-NEXT: Value: 0x2008
// CHECK-NEXT: Value: 0x30D8
// CHECK-NEXT: Size: 0
// CHECK-NEXT: Binding: Local
// CHECK-NEXT: Type: None (0x0)
// CHECK-NEXT: Other [
// CHECK-NEXT: STV_HIDDEN
// CHECK-NEXT: ]
// CHECK-NEXT: Section: .got.plt
// CHECK-NEXT: Section: .got

View File

@ -4,11 +4,11 @@
.long _GLOBAL_OFFSET_TABLE_ - .
// CHECK: Name: _GLOBAL_OFFSET_TABLE_
// CHECK-NEXT: Value: 0x2000
// CHECK-NEXT: Value: 0x2060
// CHECK-NEXT: Size: 0
// CHECK-NEXT: Binding: Local
// CHECK-NEXT: Type: None
// CHECK-NEXT: Other [ (0x2)
// CHECK-NEXT: STV_HIDDEN (0x2)
// CHECK-NEXT: ]
// CHECK-NEXT: Section: .got.plt
// CHECK-NEXT: Section: .got

View File

@ -33,13 +33,13 @@
## 73728 == 0x12000 == ADDR(.got)
# CHECK: _start:
# CHECK-NEXT: 11001: 8b 05 {{.*}} movl 77824, %eax
# CHECK-NEXT: 11007: 8b 1d {{.*}} movl 77824, %ebx
# CHECK-NEXT: 11001: 8b 05 {{.*}} movl 73728, %eax
# CHECK-NEXT: 11007: 8b 1d {{.*}} movl 73728, %ebx
# CHECK-NEXT: 1100d: 8b 80 {{.*}} movl -4(%eax), %eax
# CHECK-NEXT: 11013: 8b 83 {{.*}} movl -4(%ebx), %eax
# CHECK: Sections:
# CHECK: Name Size Address
# CHECK: .got 00000004 0000000000013000
# CHECK: .got 00000004 0000000000012000
# RUN: not ld.lld %S/Inputs/i386-got32x-baseless.elf -o %t1 -pie 2>&1 | \
# RUN: FileCheck %s --check-prefix=ERR

View File

@ -6,23 +6,15 @@
movl $_GLOBAL_OFFSET_TABLE_, %eax
// CHECK: Name: .got.plt
// CHECK-NEXT: Type: SHT_PROGBITS
// CHECK-NEXT: Flags [
// CHECK-NEXT: SHF_ALLOC
// CHECK-NEXT: SHF_WRITE
// CHECK-NEXT: ]
// CHECK-NEXT: Address: 0x2000
// CHECK: Name: .got
// CHECK-NEXT: Type: SHT_PROGBITS
// CHECK-NEXT: Flags [
// CHECK-NEXT: SHF_ALLOC
// CHECK-NEXT: SHF_WRITE
// CHECK-NEXT: ]
// CHECK-NEXT: Address: 0x3030
// CHECK-NEXT: Address: 0x2030
// DISASM: Disassembly of section .text:
// DISASM-NEXT: .text:
// DISASM-NEXT: 1000: {{.*}} movl $8240, %eax
// 0x3030 - 0x1000 = 0x2030
// DISASM-NEXT: 1000: {{.*}} movl $4144, %eax
// 0x2030 - 0x1000 = 4144