diff --git a/lld/ELF/AArch64ErrataFix.cpp b/lld/ELF/AArch64ErrataFix.cpp --- a/lld/ELF/AArch64ErrataFix.cpp +++ b/lld/ELF/AArch64ErrataFix.cpp @@ -413,9 +413,7 @@ write32le(buf, read32le(patchee->data().begin() + patcheeOffset)); // Apply any relocation transferred from the original patchee section. - // For a SyntheticSection Buf already has outSecOff added, but relocateAlloc - // also adds outSecOff so we need to subtract to avoid double counting. - this->relocateAlloc(buf - outSecOff, buf - outSecOff + getSize()); + relocateAlloc(buf, buf + getSize()); // Return address is the next instruction after the one we have just copied. uint64_t s = getLDSTAddr() + 4; diff --git a/lld/ELF/ARMErrataFix.cpp b/lld/ELF/ARMErrataFix.cpp --- a/lld/ELF/ARMErrataFix.cpp +++ b/lld/ELF/ARMErrataFix.cpp @@ -173,11 +173,9 @@ write32le(buf, 0xea000000); else write32le(buf, 0x9000f000); - // If we have a relocation then apply it. For a SyntheticSection buf already - // has outSecOff added, but relocateAlloc also adds outSecOff so we need to - // subtract to avoid double counting. + // If we have a relocation then apply it. if (!relocations.empty()) { - relocateAlloc(buf - outSecOff, buf - outSecOff + getSize()); + relocateAlloc(buf, buf + getSize()); return; } diff --git a/lld/ELF/InputSection.cpp b/lld/ELF/InputSection.cpp --- a/lld/ELF/InputSection.cpp +++ b/lld/ELF/InputSection.cpp @@ -881,7 +881,7 @@ if (config->emachine == EM_386 && type == R_386_GOTPC) continue; - uint64_t offset = getOffset(rel.r_offset); + uint64_t offset = rel.r_offset; uint8_t *bufLoc = buf + offset; int64_t addend = getAddend(rel); if (!RelTy::IsRela) @@ -975,7 +975,7 @@ for (const Relocation &rel : sec->relocations) { // InputSection::copyRelocations() adds only R_ABS relocations. assert(rel.expr == R_ABS); - uint8_t *bufLoc = buf + rel.offset + sec->outSecOff; + uint8_t *bufLoc = buf + rel.offset; uint64_t targetVA = SignExtend64(rel.sym->getVA(rel.addend), bits); target->relocate(bufLoc, rel, targetVA); } @@ -1008,12 +1008,12 @@ if (rel.expr == R_NONE) continue; uint64_t offset = rel.offset; - if (auto *sec = dyn_cast(this)) - offset += sec->outSecOff; uint8_t *bufLoc = buf + offset; RelType type = rel.type; uint64_t addrLoc = getOutputSection()->addr + offset; + if (auto *sec = dyn_cast(this)) + addrLoc += sec->outSecOff; RelExpr expr = rel.expr; uint64_t targetVA = SignExtend64( getRelocTargetVA(file, type, rel.addend, addrLoc, *rel.sym, expr), @@ -1089,7 +1089,7 @@ // basic block sections. if (auto *sec = dyn_cast(this)) { for (const JumpInstrMod &jumpMod : jumpInstrMods) { - uint64_t offset = jumpMod.offset + sec->outSecOff; + uint64_t offset = jumpMod.offset; uint8_t *bufLoc = buf + offset; target->applyJumpInstrMod(bufLoc, jumpMod.original, jumpMod.size); } @@ -1187,8 +1187,8 @@ if (Defined *f = getEnclosingFunction(rel.offset)) { prologues.insert(f); - if (target->adjustPrologueForCrossSplitStack(buf + getOffset(f->value), - end, f->stOther)) + if (target->adjustPrologueForCrossSplitStack(buf + f->value, end, + f->stOther)) continue; if (!getFile()->someNoSplitStack) error(lld::toString(this) + ": " + f->getName() + @@ -1236,7 +1236,7 @@ fatal(toString(this) + ": uncompress failed: " + llvm::toString(std::move(e))); uint8_t *bufEnd = buf + outSecOff + size; - relocate(buf, bufEnd); + relocate(buf + outSecOff, bufEnd); return; } @@ -1244,7 +1244,7 @@ // and then apply relocations. memcpy(buf + outSecOff, data().data(), data().size()); uint8_t *bufEnd = buf + outSecOff + data().size(); - relocate(buf, bufEnd); + relocate(buf + outSecOff, bufEnd); } void InputSection::replace(InputSection *other) { diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp --- a/lld/ELF/SyntheticSections.cpp +++ b/lld/ELF/SyntheticSections.cpp @@ -696,11 +696,8 @@ } void GotSection::writeTo(uint8_t *buf) { - // Buf points to the start of this section's buffer, - // whereas InputSectionBase::relocateAlloc() expects its argument - // to point to the start of the output section. target->writeGotHeader(buf); - relocateAlloc(buf - outSecOff, buf - outSecOff + size); + relocateAlloc(buf, buf + size); } static uint64_t getMipsPageAddr(uint64_t addr) { @@ -3497,7 +3494,7 @@ assert(isec->getParent() != nullptr); if (InputSection *d = findExidxSection(isec)) { memcpy(buf + offset, d->data().data(), d->data().size()); - d->relocateAlloc(buf, buf + d->getSize()); + d->relocateAlloc(buf + d->outSecOff, buf + d->outSecOff + d->getSize()); offset += d->getSize(); } else { // A Linker generated CANTUNWIND section. diff --git a/lld/test/ELF/non-abs-reloc.s b/lld/test/ELF/non-abs-reloc.s --- a/lld/test/ELF/non-abs-reloc.s +++ b/lld/test/ELF/non-abs-reloc.s @@ -1,21 +1,31 @@ // REQUIRES: x86 -// RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o -// RUN: ld.lld %t.o -o %t 2>&1 | FileCheck %s -// CHECK: (.nonalloc+0x1): has non-ABS relocation R_X86_64_PC32 against symbol '_start' -// CHECK: (.nonalloc+0x6): has non-ABS relocation R_X86_64_PC32 against symbol '_start' +// RUN: split-file %s %t +// RUN: llvm-mc -filetype=obj -triple=x86_64 %t/asm -o %t.o +// RUN: ld.lld -T %t/lds %t.o -o %t.exe 2>&1 | FileCheck %s +// CHECK: warning: {{.*}}.o:(.nonalloc1+0x1): has non-ABS relocation R_X86_64_PC32 against symbol '_start' +// CHECK-NEXT: warning: {{.*}}.o:(.nonalloc1+0x6): has non-ABS relocation R_X86_64_PC32 against symbol '_start' -// RUN: llvm-objdump -D %t | FileCheck --check-prefix=DISASM %s +// RUN: llvm-objdump -D --no-show-raw-insn %t.exe | FileCheck --check-prefix=DISASM %s // DISASM: Disassembly of section .nonalloc: // DISASM-EMPTY: // DISASM-NEXT: <.nonalloc>: -// DISASM-NEXT: 0: {{.*}} callq {{.*}} <_start> -// DISASM-NEXT: 5: {{.*}} callq {{.*}} <_start> +// DISASM-NEXT: 0: nop +// DISASM-NEXT: 1: callq 0x1 +// DISASM-NEXT: 6: callq 0x1 +//--- lds +SECTIONS { + .nonalloc 0 : { *(.nonalloc*) } +} +//--- asm .globl _start _start: nop -.section .nonalloc +.section .nonalloc0 + nop + +.section .nonalloc1 .byte 0xe8 .long _start - . - 4 .byte 0xe8 diff --git a/lld/test/ELF/x86-64-split-stack-prologue-adjust-success.s b/lld/test/ELF/x86-64-split-stack-prologue-adjust-success.s --- a/lld/test/ELF/x86-64-split-stack-prologue-adjust-success.s +++ b/lld/test/ELF/x86-64-split-stack-prologue-adjust-success.s @@ -45,12 +45,12 @@ .size prologue2_calls_\function_to_call\register,. - prologue2_calls_\function_to_call\register .endm + .section .text,"ax",@progbits,unique,0 .local foo foo: - .section .text,"ax",@progbits .quad foo - .text + .section .text,"ax",@progbits,unique,1 # For split-stack code calling split-stack code, ensure prologue v1 still # calls plain __morestack, and that any raw bytes written to the prologue