Index: ELF/InputSection.cpp =================================================================== --- ELF/InputSection.cpp +++ ELF/InputSection.cpp @@ -123,6 +123,14 @@ } template +static typename llvm::object::ELFFile::uintX_t +getSymSize(SymbolBody &Body) { + if (auto *SS = dyn_cast>(&Body)) + return SS->Sym.st_size; + return 0; +} + +template template void InputSectionBase::relocate(uint8_t *Buf, uint8_t *BufEnd, RelIteratorRange Rels) { @@ -153,7 +161,7 @@ const Elf_Shdr *SymTab = File->getSymbolTable(); if (SymIndex < SymTab->sh_info) { uintX_t SymVA = getLocalRelTarget(*File, RI); - Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc, SymVA, + Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc, SymVA, 0, findMipsPairedReloc(Buf, Type, NextRelocs)); continue; } @@ -194,8 +202,9 @@ } else if (Target->isTlsDynReloc(Type)) { continue; } - Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc, - SymVA + getAddend(RI), + uintX_t A = getAddend(RI); + uintX_t Size = getSymSize(Body); + Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc, SymVA + A, Size + A, findMipsPairedReloc(Buf, Type, NextRelocs)); } } Index: ELF/Target.h =================================================================== --- ELF/Target.h +++ ELF/Target.h @@ -57,7 +57,7 @@ virtual bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const = 0; virtual bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const = 0; virtual void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, - uint64_t P, uint64_t SA, + uint64_t P, uint64_t SA, uint64_t ZA = 0, uint8_t *PairedLoc = nullptr) const = 0; virtual bool isTlsOptimized(unsigned Type, const SymbolBody *S) const; virtual unsigned relocateTlsOptimize(uint8_t *Loc, uint8_t *BufEnd, Index: ELF/Target.cpp =================================================================== --- ELF/Target.cpp +++ ELF/Target.cpp @@ -87,7 +87,8 @@ bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override; bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override; void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P, - uint64_t SA, uint8_t *PairedLoc = nullptr) const override; + uint64_t SA, uint64_t ZA = 0, + uint8_t *PairedLoc = nullptr) const override; }; class X86_64TargetInfo final : public TargetInfo { @@ -106,7 +107,8 @@ bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override; bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override; void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P, - uint64_t SA, uint8_t *PairedLoc = nullptr) const override; + uint64_t SA, uint64_t ZA = 0, + uint8_t *PairedLoc = nullptr) const override; bool isRelRelative(uint32_t Type) const override; bool isTlsOptimized(unsigned Type, const SymbolBody *S) const override; unsigned relocateTlsOptimize(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, @@ -136,7 +138,8 @@ bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override; bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override; void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P, - uint64_t SA, uint8_t *PairedLoc = nullptr) const override; + uint64_t SA, uint64_t ZA = 0, + uint8_t *PairedLoc = nullptr) const override; bool isRelRelative(uint32_t Type) const override; }; @@ -155,7 +158,8 @@ bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override; bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override; void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P, - uint64_t SA, uint8_t *PairedLoc = nullptr) const override; + uint64_t SA, uint64_t ZA = 0, + uint8_t *PairedLoc = nullptr) const override; }; template class MipsTargetInfo final : public TargetInfo { @@ -171,7 +175,8 @@ bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override; bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override; void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P, - uint64_t SA, uint8_t *PairedLoc = nullptr) const override; + uint64_t SA, uint64_t ZA = 0, + uint8_t *PairedLoc = nullptr) const override; }; } // anonymous namespace @@ -320,7 +325,7 @@ } void X86TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, - uint64_t P, uint64_t SA, + uint64_t P, uint64_t SA, uint64_t ZA, uint8_t *PairedLoc) const { switch (Type) { case R_386_32: @@ -633,8 +638,11 @@ } void X86_64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, - uint64_t P, uint64_t SA, + uint64_t P, uint64_t SA, uint64_t ZA, uint8_t *PairedLoc) const { + if ((Type == R_X86_64_SIZE32 || Type == R_X86_64_SIZE64) && Config->Shared) + return; + switch (Type) { case R_X86_64_32: checkUInt<32>(SA, Type); @@ -660,6 +668,12 @@ case R_X86_64_TLSLD: write32le(Loc, SA - P); break; + case R_X86_64_SIZE32: + write32le(Loc, ZA); + break; + case R_X86_64_SIZE64: + write64le(Loc, ZA); + break; case R_X86_64_TPOFF32: { uint64_t Val = SA - Out::TlsPhdr->p_memsz; checkInt<32>(Val, Type); @@ -781,7 +795,7 @@ } void PPC64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, - uint64_t P, uint64_t SA, + uint64_t P, uint64_t SA, uint64_t ZA, uint8_t *PairedLoc) const { uint64_t TB = getPPC64TocBase(); @@ -1019,7 +1033,7 @@ void AArch64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P, uint64_t SA, - uint8_t *PairedLoc) const { + uint64_t ZA, uint8_t *PairedLoc) const { switch (Type) { case R_AARCH64_ABS16: checkIntUInt<16>(SA, Type); @@ -1132,7 +1146,7 @@ template void MipsTargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P, uint64_t SA, - uint8_t *PairedLoc) const { + uint64_t ZA, uint8_t *PairedLoc) const { const endianness E = ELFT::TargetEndianness; switch (Type) { case R_MIPS_32: Index: test/ELF/Inputs/relocation-size-shared.s =================================================================== --- test/ELF/Inputs/relocation-size-shared.s +++ test/ELF/Inputs/relocation-size-shared.s @@ -0,0 +1,6 @@ +.data +.global fooshared +.type fooshared,%object +.size fooshared,26 +fooshared: +.zero 26 Index: test/ELF/relocation-size-shared.s =================================================================== --- test/ELF/relocation-size-shared.s +++ test/ELF/relocation-size-shared.s @@ -0,0 +1,78 @@ +// RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o +// RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %p/Inputs/relocation-size-shared.s -o %tso.o +// RUN: ld.lld -shared %tso.o -o %tso +// RUN: ld.lld %t.o %tso -o %t1 +// RUN: llvm-readobj -r %t1 | FileCheck --check-prefix=RELOCSHARED %s +// RUN: llvm-objdump -d %t1 | FileCheck --check-prefix=DISASM %s + +// RELOCSHARED: Relocations [ +// RELOCSHARED-NEXT: Section ({{.*}}) .rela.dyn { +// RELOCSHARED-NEXT: 0x11018 R_X86_64_SIZE64 fooshared 0xFFFFFFFFFFFFFFFF +// RELOCSHARED-NEXT: 0x11020 R_X86_64_SIZE64 fooshared 0x0 +// RELOCSHARED-NEXT: 0x11028 R_X86_64_SIZE64 fooshared 0x1 +// RELOCSHARED-NEXT: 0x11048 R_X86_64_SIZE32 fooshared 0xFFFFFFFFFFFFFFFF +// RELOCSHARED-NEXT: 0x1104F R_X86_64_SIZE32 fooshared 0x0 +// RELOCSHARED-NEXT: 0x11056 R_X86_64_SIZE32 fooshared 0x1 +// RELOCSHARED-NEXT: } +// RELOCSHARED-NEXT:] + +// DISASM: Disassembly of section .text: +// DISASM: _data: +// DISASM-NEXT: 11000: 19 00 +// DISASM-NEXT: 11002: 00 00 +// DISASM-NEXT: 11004: 00 00 +// DISASM-NEXT: 11006: 00 00 +// DISASM-NEXT: 11008: 1a 00 +// DISASM-NEXT: 1100a: 00 00 +// DISASM-NEXT: 1100c: 00 00 +// DISASM-NEXT: 1100e: 00 00 +// DISASM-NEXT: 11010: 1b 00 +// DISASM-NEXT: 11012: 00 00 +// DISASM-NEXT: 11014: 00 00 +// DISASM-NEXT: 11016: 00 00 +// DISASM-NEXT: 11018: 00 00 +// DISASM-NEXT: 1101a: 00 00 +// DISASM-NEXT: 1101c: 00 00 +// DISASM-NEXT: 1101e: 00 00 +// DISASM-NEXT: 11020: 00 00 +// DISASM-NEXT: 11022: 00 00 +// DISASM-NEXT: 11024: 00 00 +// DISASM-NEXT: 11026: 00 00 +// DISASM-NEXT: 11028: 00 00 +// DISASM-NEXT: 1102a: 00 00 +// DISASM-NEXT: 1102c: 00 00 +// DISASM-NEXT: 1102e: 00 00 +// DISASM: _start: +// DISASM-NEXT: 11030: 8b 04 25 19 00 00 00 movl 25, %eax +// DISASM-NEXT: 11037: 8b 04 25 1a 00 00 00 movl 26, %eax +// DISASM-NEXT: 1103e: 8b 04 25 1b 00 00 00 movl 27, %eax +// DISASM-NEXT: 11045: 8b 04 25 00 00 00 00 movl 0, %eax +// DISASM-NEXT: 1104c: 8b 04 25 00 00 00 00 movl 0, %eax +// DISASM-NEXT: 11053: 8b 04 25 00 00 00 00 movl 0, %eax + +.data +.global foo +.type foo,%object +.size foo,26 +foo: +.zero 26 + +.text +_data: + // R_X86_64_SIZE64: + .quad foo@SIZE-1 + .quad foo@SIZE + .quad foo@SIZE+1 + .quad fooshared@SIZE-1 + .quad fooshared@SIZE + .quad fooshared@SIZE+1 + +.globl _start +_start: + // R_X86_64_SIZE32: + movl foo@SIZE-1,%eax + movl foo@SIZE,%eax + movl foo@SIZE+1,%eax + movl fooshared@SIZE-1,%eax + movl fooshared@SIZE,%eax + movl fooshared@SIZE+1,%eax Index: test/ELF/relocation-size.s =================================================================== --- test/ELF/relocation-size.s +++ test/ELF/relocation-size.s @@ -0,0 +1,80 @@ +// RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o +// RUN: ld.lld %t.o -o %t1 +// RUN: llvm-readobj -r %t1 | FileCheck --check-prefix=NORELOC %s +// RUN: llvm-objdump -d %t1 | FileCheck --check-prefix=DISASM %s +// RUN: ld.lld -shared %t.o -o %t1 +// RUN: llvm-readobj -r %t1 | FileCheck --check-prefix=RELOCSHARED %s +// RUN: llvm-objdump -d %t1 | FileCheck --check-prefix=DISASMSHARED %s + +// NORELOC: Relocations [ +// NORELOC-NEXT: ] + +// DISASM: Disassembly of section .text: +// DISASM-NEXT: _data: +// DISASM-NEXT: 11000: 19 00 +// DISASM-NEXT: 11002: 00 00 +// DISASM-NEXT: 11004: 00 00 +// DISASM-NEXT: 11006: 00 00 +// DISASM-NEXT: 11008: 1a 00 +// DISASM-NEXT: 1100a: 00 00 +// DISASM-NEXT: 1100c: 00 00 +// DISASM-NEXT: 1100e: 00 00 +// DISASM-NEXT: 11010: 1b 00 +// DISASM-NEXT: 11012: 00 00 +// DISASM-NEXT: 11014: 00 00 +// DISASM-NEXT: 11016: 00 00 +// DISASM: _start: +// DISASM-NEXT: 11018: 8b 04 25 19 00 00 00 movl 25, %eax +// DISASM-NEXT: 1101f: 8b 04 25 1a 00 00 00 movl 26, %eax +// DISASM-NEXT: 11026: 8b 04 25 1b 00 00 00 movl 27, %eax + +// RELOCSHARED: Relocations [ +// RELOCSHARED-NEXT: Section ({{.*}}) .rela.dyn { +// RELOCSHARED-NEXT: 0x1000 R_X86_64_SIZE64 foo 0xFFFFFFFFFFFFFFFF +// RELOCSHARED-NEXT: 0x1008 R_X86_64_SIZE64 foo 0x0 +// RELOCSHARED-NEXT: 0x1010 R_X86_64_SIZE64 foo 0x1 +// RELOCSHARED-NEXT: 0x101B R_X86_64_SIZE32 foo 0xFFFFFFFFFFFFFFFF +// RELOCSHARED-NEXT: 0x1022 R_X86_64_SIZE32 foo 0x0 +// RELOCSHARED-NEXT: 0x1029 R_X86_64_SIZE32 foo 0x1 +// RELOCSHARED-NEXT: } +// RELOCSHARED-NEXT: ] + +// DISASMSHARED: Disassembly of section .text: +// DISASMSHARED-NEXT: _data: +// DISASMSHARED-NEXT: 1000: 00 00 +// DISASMSHARED-NEXT: 1002: 00 00 +// DISASMSHARED-NEXT: 1004: 00 00 +// DISASMSHARED-NEXT: 1006: 00 00 +// DISASMSHARED-NEXT: 1008: 00 00 +// DISASMSHARED-NEXT: 100a: 00 00 +// DISASMSHARED-NEXT: 100c: 00 00 +// DISASMSHARED-NEXT: 100e: 00 00 +// DISASMSHARED-NEXT: 1010: 00 00 +// DISASMSHARED-NEXT: 1012: 00 00 +// DISASMSHARED-NEXT: 1014: 00 00 +// DISASMSHARED-NEXT: 1016: 00 00 +// DISASMSHARED: _start: +// DISASMSHARED-NEXT: 1018: 8b 04 25 00 00 00 00 movl 0, %eax +// DISASMSHARED-NEXT: 101f: 8b 04 25 00 00 00 00 movl 0, %eax +// DISASMSHARED-NEXT: 1026: 8b 04 25 00 00 00 00 movl 0, %eax + +.data +.global foo +.type foo,%object +.size foo,26 +foo: +.zero 26 + +.text +_data: + // R_X86_64_SIZE64: + .quad foo@SIZE-1 + .quad foo@SIZE + .quad foo@SIZE+1 + +.globl _start +_start: + // R_X86_64_SIZE32: + movl foo@SIZE-1,%eax + movl foo@SIZE,%eax + movl foo@SIZE+1,%eax