From 168411b9d5b164da07721439a7015a066a9831ed Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Mon, 22 Apr 2024 06:14:33 -0400 Subject: [PATCH] [ADLT] __cfi_check resolve. CFI align. Minor changes. Change-Id: I552703808967205cc562bcf0207a37e1c7fcd8f4 Signed-off-by: Anton Volkov --- lld/ELF/Config.h | 1 + lld/ELF/InputFiles.cpp | 32 ++++++++++++++++++++------------ lld/ELF/InputFiles.h | 1 - lld/ELF/Relocations.cpp | 12 ++++-------- lld/ELF/Writer.cpp | 3 +++ 5 files changed, 28 insertions(+), 21 deletions(-) diff --git a/lld/ELF/Config.h b/lld/ELF/Config.h index 9d93f0cfa638..945848b9da03 100644 --- a/lld/ELF/Config.h +++ b/lld/ELF/Config.h @@ -406,6 +406,7 @@ struct Ctx { backwardReferences; // ADLT stuff. + bool adltWithCfi = false; // From input .rela.dyn, .rela.plt: // Keep input library indexes that are needed for got/plt symbol llvm::DenseMap> diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp index 8be3a255cbde..6abd581ce33f 100644 --- a/lld/ELF/InputFiles.cpp +++ b/lld/ELF/InputFiles.cpp @@ -532,6 +532,14 @@ static StringRef markItemForAdlt(StringRef input, StringRef filePath) { return saver().save(input + mark); } +static bool markSymForAdlt(Symbol *s, StringRef filePath) { + StringRef newName = markItemForAdlt(s->getName(), filePath); + if (s->getName() == newName) + return false; + s->setName(newName); + return true; +} + template void ObjFile::initializeSections(bool ignoreComdats, const llvm::object::ELFFile &obj) { @@ -1054,8 +1062,15 @@ void ObjFile::initializeSymbols(const object::ELFFile &obj) { // Some entries have been filled by LazyObjFile. for (size_t i = firstGlobal, end = eSyms.size(); i != end; ++i) - if (!symbols[i]) - symbols[i] = symtab.insert(CHECK(eSyms[i].getName(stringTable), this)); + if (!symbols[i]) { + StringRef name = CHECK(eSyms[i].getName(stringTable), this); + if (config->adlt && name == "__cfi_check") { + name = markItemForAdlt(name, archiveName); + if (!ctx->adltWithCfi) + ctx->adltWithCfi = true; + } + symbols[i] = symtab.insert(name); + } // Perform symbol resolution on non-local symbols. SmallVector undefineds; @@ -1701,11 +1716,7 @@ StringRef SharedFileExtended::addAdltPostfix(StringRef input) const { template bool SharedFileExtended::addAdltPostfix(Symbol *s) { - StringRef newName = markItemForAdlt(s->getName(), soName); - if (s->getName() == newName) - return false; - s->setName(newName); - return true; + return markSymForAdlt(s, this->soName); } // TODO: optimize 2 lookups @@ -1781,8 +1792,7 @@ bool SharedFileExtended::isDynamicSection(InputSectionBase &sec) const { template Defined *SharedFileExtended::findDefinedSymbol( - uint64_t offset, StringRef fatalTitle, - llvm::function_ref extraCond) const { + uint64_t offset, llvm::function_ref extraCond) const { bool isDebug = false; /*if (offset == 0x7738) // debug hint isDebug = true;*/ @@ -1810,9 +1820,7 @@ Defined *SharedFileExtended::findDefinedSymbol( } auto *sectionSym = findSectionSymbol(offset); - if (!sectionSym) - fatal(fatalTitle + " 0x" + utohexstr(offset) + "\n"); - if (isDebug) + if (isDebug && sectionSym) traceSymbol(*sectionSym, "found section sym: "); return sectionSym; } diff --git a/lld/ELF/InputFiles.h b/lld/ELF/InputFiles.h index dce2d1bda741..bd880dc712a8 100644 --- a/lld/ELF/InputFiles.h +++ b/lld/ELF/InputFiles.h @@ -407,7 +407,6 @@ public: Defined *findSectionSymbol(uint64_t offset) const; Defined *findDefinedSymbol( uint64_t offset, - StringRef fatalTitle = "defined symbol not found! offset:", llvm::function_ref extraCond = [](Defined *) { return true; }) const; diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp index ff8f1002ab44..1e953665722c 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -1338,8 +1338,6 @@ 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: " + std::to_string(symIndex) + " "; bool isDebug = false; /*if (r->offset == 0x21F) // debug hint @@ -1349,11 +1347,11 @@ void RelocationScanner::processForADLT(const RelTy &rel, Relocation *r, isDebug = true;*/ // parse offset (where) - std::string failTitle = title + "symWhere not found! offset: "; InputSectionBase *secWhere = cast( fromDynamic - ? file->findDefinedSymbol(r->offset, failTitle)->section + ? file->findDefinedSymbol(r->offset)->section : (r->sym->isDefined() ? cast(r->sym)->section : &sec)); + assert(secWhere && "not found!"); // process offset r->offset -= fromDynamic ? secWhere->address : sec.address; @@ -1366,10 +1364,8 @@ void RelocationScanner::processForADLT(const RelTy &rel, Relocation *r, switch (r->type) { // dyn relocs case R_AARCH64_RELATIVE: { - failTitle = - title + " " + toString(r->type) + ": r->sym not found! addend: "; - Defined *d = file->findDefinedSymbol(r->addend, failTitle); - assert(d); + Defined *d = file->findDefinedSymbol(r->addend); + assert(d && "R_AARCH64_RELATIVE: r->sym not found by addend!"); r->sym = d; r->addend -= d->section->address + d->value; addRelativeReloc(*secWhere, r->offset, *r->sym, r->addend, r->expr, diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index 0ac9bb292050..51303efa219b 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -1033,6 +1033,9 @@ void PhdrEntry::add(OutputSection *sec) { lastSec = sec; if (!firstSec) firstSec = sec; + bool adltCFI = config->adlt && ctx->adltWithCfi && p_type == PT_LOAD; + if (adltCFI) // check cfi.h: LIBRARY_ALIGNMENT and _BITS + sec->alignment = 1UL << 18; p_align = std::max(p_align, sec->alignment); if (p_type == PT_LOAD) sec->ptLoad = this; -- Gitee