From 21321e1b5fbfe79aa4b6c6686e81c65e3b43783e Mon Sep 17 00:00:00 2001 From: Anton Date: Tue, 17 Dec 2024 08:23:24 -0600 Subject: [PATCH] ADLT: Extended indexing of Got entries. Change-Id: Idf42363355e7038843a6a8eec2b70e22af984ffd Signed-off-by: Anton --- lld/ELF/Adlt/Context.cpp | 37 ++++++++++++++++++++++++- lld/ELF/Adlt/Context.h | 4 +++ lld/ELF/Relocations.cpp | 59 ++++++++++++++-------------------------- 3 files changed, 61 insertions(+), 39 deletions(-) diff --git a/lld/ELF/Adlt/Context.cpp b/lld/ELF/Adlt/Context.cpp index 141dc6f5f0b6..379e6105c639 100644 --- a/lld/ELF/Adlt/Context.cpp +++ b/lld/ELF/Adlt/Context.cpp @@ -172,7 +172,7 @@ void Impl::aggregateSections() { bool isNeededProgBits = s->type == SHT_PROGBITS && !(s->name == ".got" || s->name == ".got.plt" || s->name == ".plt" || - (adlt::ctx->withEHFrame && s->name.startswith(".eh_frame_hdr"))); + (adlt::ctx->withEHFrame && s->name.startswith(".eh_frame_hdr"))); bool isNeededRela = config->emitRelocs && s->type == SHT_RELA && !(s->name == ".rela.dyn" || s->name == ".rela.plt"); return isBaseType || isNeededProgBits || isNeededRela; @@ -394,4 +394,39 @@ void Context::generateDynamicTags(elf::Partition &part, void Context::generateRelocIndexes() { impl->generateRelocIndexes(); } void Context::copyRequiredLocalSymbols() { impl->copyRequiredLocalSymbols(); } +void Context::addRelaDynGotIndex(StringRef symName, uint64_t offsetInGot) { + auto &lastRel = mainPart->relaDyn->relocs.back(); + lastRel.offsetInSec = offsetInGot; + lastRel.r_offset = offsetInGot; + lastRel.inputSec = in.got.get(); + for (size_t orderIndx : getSoIndexesGotPlt(symName)) { + auto *file = getSharedFile(orderIndx); + lastRel.ndsoIndexes.insert(file->getOrderIndx()); + file->addRelaDynIndex(mainPart->relaDyn->relocs.size() - 1); + } +} + +void Context::addRelrDynGotIndex(StringRef symName, uint64_t offsetInGot) { + auto &lastRel = mainPart->relrDyn->relocs.back(); + lastRel.offsetInSec = offsetInGot; + lastRel.inputSec = in.got.get(); + for (size_t orderIndx : getSoIndexesGotPlt(symName)) { + auto *file = getSharedFile(orderIndx); + lastRel.ndsoIndexes.insert(file->getOrderIndx()); + file->addRelrDynIndex(mainPart->relrDyn->relocs.size() - 1); + } +} + +void Context::addRelaPltIndex(StringRef symName, uint64_t offsetInGotPlt) { + auto &lastRel = in.relaPlt->relocs.back(); + lastRel.offsetInSec = offsetInGotPlt; + lastRel.r_offset = offsetInGotPlt; + lastRel.inputSec = in.gotPlt.get(); + for (size_t orderIndx : getSoIndexesGotPlt(symName)) { + auto *file = getSharedFile(orderIndx); + lastRel.ndsoIndexes.insert(file->getOrderIndx()); + file->addRelaPltIndex(in.relaPlt->relocs.size() - 1); + } +} + // OHOS_LOCAL end diff --git a/lld/ELF/Adlt/Context.h b/lld/ELF/Adlt/Context.h index abb429328be9..2b2457b5a39a 100644 --- a/lld/ELF/Adlt/Context.h +++ b/lld/ELF/Adlt/Context.h @@ -120,6 +120,10 @@ public: std::unique_ptr adltInfo; std::unique_ptr verInfo; + void addRelaDynGotIndex(StringRef symName, uint64_t offsetInGot); + void addRelrDynGotIndex(StringRef symName, uint64_t offsetInGot); + void addRelaPltIndex(StringRef symName, uint64_t offsetInGotPlt); + private: std::unique_ptr impl; }; diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp index 43e3b9f0aefa..afa6bcfb085f 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -909,11 +909,19 @@ static void addRelativeReloc(InputSectionBase &isec, uint64_t offsetInSec, // address. if (part.relrDyn && isec.alignment >= 2 && offsetInSec % 2 == 0) { isec.relocations.push_back({expr, type, offsetInSec, addend, &sym}); - part.relrDyn->relocs.push_back({&isec, offsetInSec, {}}); // OHOS_LOCAL + // OHOS_LOCAL begin + part.relrDyn->relocs.push_back({&isec, offsetInSec, {}}); + if (config->adlt && &isec == in.got.get()) + adlt::ctx->addRelrDynGotIndex(sym.getName(), offsetInSec); + // OHOS_LOCAL end return; } part.relaDyn->addRelativeReloc(target->relativeRel, isec, offsetInSec, sym, addend, type, expr); + // OHOS_LOCAL begin + if (config->adlt && &isec == in.got.get()) + adlt::ctx->addRelaDynGotIndex(sym.getName(), offsetInSec); + // OHOS_LOCAL end } template @@ -928,16 +936,11 @@ static void addPltEntry(PltSection &plt, GotPltSection &gotPlt, : DynamicReloc::AddendOnlyWithTargetVA, sym, 0, R_ABS}); if (config->adlt) { - auto &lastRel = rel.relocs.back(); - lastRel.offsetInSec = off; - lastRel.r_offset = off; - lastRel.inputSec = &gotPlt; - using elf::adlt::ctx; - for (size_t orderIndx : ctx->getSoIndexesGotPlt(sym.getName())) { - auto *file = ctx->getSharedFile(orderIndx); - lastRel.ndsoIndexes.insert(file->getOrderIndx()); - file->addRelaPltIndex(rel.relocs.size() - 1); - } + if (cast(&rel) != in.relaPlt.get()) + fatal("ADLT: Unsuported Rela Plt section" + rel.name + "!\n"); + if (cast(&gotPlt) != in.gotPlt.get()) + fatal("ADLT: Unsuported Got Plt section" + gotPlt.name + "!\n"); + adlt::ctx->addRelaPltIndex(sym.getName(), off); } // OHOS_LOCAL end } @@ -951,18 +954,8 @@ static void addGotEntry(Symbol &sym) { mainPart->relaDyn->addReloc({target->gotRel, in.got.get(), off, DynamicReloc::AgainstSymbol, sym, 0, R_ABS}); // OHOS_LOCAL begin - if (config->adlt) { - auto &lastRel = mainPart->relaDyn->relocs.back(); - lastRel.offsetInSec = off; - lastRel.r_offset = off; - lastRel.inputSec = in.got.get(); - using elf::adlt::ctx; - for (size_t orderIndx : ctx->getSoIndexesGotPlt(sym.getName())) { - auto *file = ctx->getSharedFile(orderIndx); - lastRel.ndsoIndexes.insert(file->getOrderIndx()); - file->addRelaDynIndex(mainPart->relaDyn->relocs.size() - 1); - } - } + if (config->adlt) + adlt::ctx->addRelaDynGotIndex(sym.getName(), off); // OHOS_LOCAL end return; } @@ -985,18 +978,8 @@ static void addTpOffsetGotEntry(Symbol &sym) { mainPart->relaDyn->addAddendOnlyRelocIfNonPreemptible( target->tlsGotRel, *in.got, off, sym, target->symbolicRel); // OHOS_LOCAL begin - if (config->adlt) { - auto &lastRel = mainPart->relaDyn->relocs.back(); - lastRel.offsetInSec = off; - lastRel.r_offset = off; - lastRel.inputSec = in.got.get(); - using elf::adlt::ctx; - for (size_t orderIndx : ctx->getSoIndexesGotPlt(sym.getName())) { - auto *file = ctx->getSharedFile(orderIndx); - lastRel.ndsoIndexes.insert(file->getOrderIndx()); - file->addRelaDynIndex(mainPart->relaDyn->relocs.size() - 1); - } - } + if (config->adlt) + adlt::ctx->addRelaDynGotIndex(sym.getName(), off); // OHOS_LOCAL end } @@ -1407,10 +1390,10 @@ template void RelocationScanner::scanOne(RelTy *&i) { if (eh && config->adlt) { offset = getter.get(rel.r_offset - eh->inputAddress); if (offset != uint64_t(-1)) - offset += eh->inputAddress; + offset += eh->inputAddress; } else offset = getter.get(rel.r_offset); - // OHOS_LOCAL end + // OHOS_LOCAL end if (offset == uint64_t(-1)) return; @@ -1762,7 +1745,7 @@ void elf::postScanRelocations() { auto fn = [](Symbol &sym) { // OHOS_LOCAL begin // Debug hint - /*if (config->adlt && sym.getName() == "__hwasan_tls") + /* if (config->adlt && sym.getName() == "__build_id_start__0") asm("nop"); */ // OHOS_LOCAL end -- Gitee