From 286f2de0d446bbc8ad8f297b5de4333b37bc5759 Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Wed, 6 Mar 2024 08:48:02 -0500 Subject: [PATCH 1/3] Better search defined symbols. Change-Id: I17f3a80c03a14d7f6f08c22a1c28aed8ccc9176c Signed-off-by: Anton Volkov --- lld/ELF/InputFiles.cpp | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp index 8bd05c5a253f..a30bf61d36c6 100644 --- a/lld/ELF/InputFiles.cpp +++ b/lld/ELF/InputFiles.cpp @@ -1715,8 +1715,6 @@ bool SharedFileExtended::saveSymbol(const Defined& d) const { template Defined *SharedFileExtended::findSectionSymbol(uint64_t offset) const { bool isDebug = false; - // if (offset == 0x43e0) // debug hint - // isDebug = true; auto predRange = [=](Symbol *sym) { if (!sym || sym->isUndefined() || !sym->isSection()) return false; @@ -1734,6 +1732,8 @@ Defined *SharedFileExtended::findSectionSymbol(uint64_t offset) const { auto i = this->allSymbols.begin(); auto e = this->allSymbols.end(); + /*if (offset == 0x4a18) // debug hint + isDebug = true; */ auto ret = std::find_if(i, e, predRange); if (ret == e) // item was not found return nullptr; @@ -1777,23 +1777,27 @@ Defined *SharedFileExtended::findDefinedSymbol( return false; bool goodVal = d->section->address + d->value == offset; - bool goodType = d->type == STT_FUNC || d->type == STT_OBJECT; - return goodVal && goodType && extraCond(d); + return goodVal && extraCond(d); }; auto i = this->allSymbols.begin(); auto e = this->allSymbols.end(); + /*if (offset == 0x4a18) // debug hint + isDebug = true; */ auto ret = std::find_if(i, e, predRange); if (ret != e) { // item was found Defined *d = cast(*ret); if (isDebug) - traceSymbol(*d, "found defined sym: "); + traceSymbol(*d, d->isSection() ? "found section sym: " + : "found defined sym: "); return d; } auto *sectionSym = findSectionSymbol(offset); if (!sectionSym) fatal(fatalTitle + " 0x" + utohexstr(offset) + "\n"); + if (isDebug) + traceSymbol(*sectionSym, "found section sym: "); return sectionSym; } -- Gitee From 2bec3e89f7051bf5aa18b47f9eca6290df157f23 Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Wed, 6 Mar 2024 08:57:29 -0500 Subject: [PATCH 2/3] Gen symbolic reloc. Other fixes. Change-Id: I3ad9611ff0cc3a98d88c19f821b8a5d03f482efe Signed-off-by: Anton Volkov --- lld/ELF/Relocations.cpp | 58 ++++++++++++++++++----------------------- 1 file changed, 26 insertions(+), 32 deletions(-) diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp index 27a9bd789ee5..1bd79908d441 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -459,11 +459,11 @@ private: // ADLT template - void processForADLT(const RelTy &rel, Relocation *r); + void processForADLT(const RelTy &rel, Relocation *r, bool fromDynamic); template void addRelativeRelocForAdlt(const RelTy &rel, Defined &symWhere, - Defined &inputSym) const; + uint64_t offset, Defined &inputSym) const; void pushRelocAdlt(Relocation *r) const; template @@ -1308,6 +1308,7 @@ static unsigned handleTlsRelocation(RelType type, Symbol &sym, template void RelocationScanner::addRelativeRelocForAdlt(const RelTy &rel, Defined &symWhere, + uint64_t offset, Defined &inputSym) const { // auto file = sec.getSharedFile(); std::string title = "addRelativeRelocForAdlt: "; @@ -1316,20 +1317,10 @@ void RelocationScanner::addRelativeRelocForAdlt(const RelTy &rel, RelType type = R_AARCH64_ABS64; RelExpr expr = R_ABS; int64_t addend = computeAddend(rel, expr, false); + addend -= inputSym.section->address + inputSym.value; - // todo: rewrite - addend = inputSym.isSection() ? (addend - inputSym.section->address) : 0; - /* - { - failTitle = "addend: 0x" + utohexstr(addend) + " was not decreased!"; - auto *ISec = inputSym.section; - if (static_cast(addend - ISec->address) >= 0) - addend -= ISec->address; - else - file->traceSection(*ISec, failTitle); - }*/ // finally - addRelativeReloc(*cast(symWhere.section), symWhere.value, + addRelativeReloc(*cast(symWhere.section), offset, inputSym, addend, expr, type); } @@ -1356,36 +1347,39 @@ void RelocationScanner::tracePushRelocADLT(Defined &symWhere, } template -void RelocationScanner::processForADLT(const RelTy &rel, Relocation *r) { +void RelocationScanner::processForADLT(const RelTy &rel, Relocation *r, + bool fromDynamic) { auto file = sec.getSharedFile(); uint32_t symIndex = rel.getSymbol(config->isMips64EL); - std::string title = "processForADLT: symIndex: " + toString(symIndex) + " "; + std::string title = + "processForADLT: symIndex: " + std::to_string(symIndex) + " "; bool isDebug = false; + /*if (r->offset == 0x296) // debug hint + isDebug = true; + if (r->sym->getName() == "__emutls_t.TLS_data1") + isDebug = true;*/ + + // parse offset (where) std::string failTitle = title + "symWhere not found! offset: "; Defined *symWhere = file->findDefinedSymbol(r->offset, failTitle); file->saveSymbol(*symWhere); - /*if (r->offset == 0x1f1c) // debug hint - isDebug = true; - if (r->offset == 0x1f20) // debug hint - isDebug = true;*/ - // process offset - failTitle = "offset: 0x" + utohexstr(r->offset) + " was not decreased!"; - if (static_cast(r->offset - sec.address) >= 0) - r->offset -= sec.address; - else - file->traceSection(sec, failTitle); - - // prepare to resolve relocs - /*if (r->sym->getName().contains("_ZdlPv")) // debug hint - isDebug = true;*/ + r->offset -= fromDynamic ? symWhere->section->address : sec.address; if (isDebug) tracePushRelocADLT(*symWhere, *r); + // read ABS relocs from .rela.dyn or .rela.plt only + if (r->type == target.symbolicRel && fromDynamic && r->sym->exportDynamic) { + sec.getPartition().relaDyn->addSymbolReloc( + target.symbolicRel, cast(*symWhere->section), + r->offset, *r->sym, r->addend, r->type); + return; + } + // resolve relocs switch (r->type) { // dyn relocs @@ -1394,7 +1388,7 @@ void RelocationScanner::processForADLT(const RelTy &rel, Relocation *r) { title + " " + toString(r->type) + ": r->sym not found! addend: "; if (auto *d = file->findDefinedSymbol( r->addend, failTitle, [](Defined *d) { return !d->isPreemptible; })) - addRelativeRelocForAdlt(rel, *symWhere, *d); + addRelativeRelocForAdlt(rel, *symWhere, r->offset, *d); return; case R_AARCH64_GLOB_DAT: r->sym->needsGot = 1; @@ -1486,7 +1480,7 @@ template void RelocationScanner::scanOne(RelTy *&i) { if (config->adlt) { Relocation r = {expr, type, offset, addend, &sym}; - processForADLT(rel, &r); + processForADLT(rel, &r, fromDynamic); return; } -- Gitee From 07f825412d16a458f6f69f08aa2a44da77eab34c Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Wed, 13 Mar 2024 03:59:30 -0400 Subject: [PATCH 3/3] Check dyn symbols before push dyn relocs. Change-Id: I50b39d3b8494e72b381cb6f116392ab99c1b9411 Signed-off-by: Anton Volkov --- lld/ELF/Relocations.cpp | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp index 1bd79908d441..3218b5a1272e 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -1372,14 +1372,6 @@ void RelocationScanner::processForADLT(const RelTy &rel, Relocation *r, if (isDebug) tracePushRelocADLT(*symWhere, *r); - // read ABS relocs from .rela.dyn or .rela.plt only - if (r->type == target.symbolicRel && fromDynamic && r->sym->exportDynamic) { - sec.getPartition().relaDyn->addSymbolReloc( - target.symbolicRel, cast(*symWhere->section), - r->offset, *r->sym, r->addend, r->type); - return; - } - // resolve relocs switch (r->type) { // dyn relocs @@ -1391,17 +1383,27 @@ void RelocationScanner::processForADLT(const RelTy &rel, Relocation *r, addRelativeRelocForAdlt(rel, *symWhere, r->offset, *d); return; case R_AARCH64_GLOB_DAT: + assert(r->sym->exportDynamic); r->sym->needsGot = 1; if (r->sym->isUndefined()) r->sym->needsPlt = 1; return; case R_AARCH64_JUMP_SLOT: + assert(r->sym->exportDynamic); if (r->sym->isUndefined()) r->sym->needsPlt = 1; return; // abs relocs case R_AARCH64_ABS32: case R_AARCH64_ABS64: + if (fromDynamic) { + assert(r->sym->exportDynamic); + sec.getPartition().relaDyn->addSymbolReloc( + target.symbolicRel, cast(*symWhere->section), + r->offset, *r->sym, r->addend, r->type); + return; + } + LLVM_FALLTHROUGH; case R_AARCH64_ADD_ABS_LO12_NC: case R_AARCH64_ADR_PREL_PG_HI21: case R_AARCH64_LDST8_ABS_LO12_NC: @@ -1433,7 +1435,8 @@ void RelocationScanner::processForADLT(const RelTy &rel, Relocation *r, pushRelocAdlt(r); return; default: - fatal("Unhandled reloc: " + toString(r->type)); + fatal("Unhandled " + toString(fromDynamic ? "dynamic" : "") + + "reloc: " + toString(r->type)); break; } } -- Gitee