From 41c61ec15a9bd59b861780286e33a05061599e47 Mon Sep 17 00:00:00 2001 From: Khomutov Nikita Date: Mon, 22 Apr 2024 14:27:23 +0300 Subject: [PATCH] [ADLT] generate shorten names for libraries symbols and sections Signed-off-by: Khomutov Nikita Change-Id: I9b457e1da09a3117c1205901d797d67503605efc --- lld/ELF/Config.h | 2 +- lld/ELF/InputFiles.cpp | 51 ++++++++++++--------------- lld/ELF/InputFiles.h | 8 +++-- lld/ELF/Relocations.cpp | 2 +- lld/ELF/Writer.cpp | 1 - llvm/tools/llvm-readobj/ELFDumper.cpp | 11 +++++- 6 files changed, 39 insertions(+), 36 deletions(-) diff --git a/lld/ELF/Config.h b/lld/ELF/Config.h index 945848b9da03..6583d7d8e562 100644 --- a/lld/ELF/Config.h +++ b/lld/ELF/Config.h @@ -410,7 +410,7 @@ struct Ctx { // From input .rela.dyn, .rela.plt: // Keep input library indexes that are needed for got/plt symbol llvm::DenseMap> - gotPltInfoAdlt; // sym, soFile->ctxIdx array; + gotPltInfoAdlt; // sym, soFile->orderIdx array; }; // The only instance of Ctx struct. diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp index 6abd581ce33f..5c9b51fb2131 100644 --- a/lld/ELF/InputFiles.cpp +++ b/lld/ELF/InputFiles.cpp @@ -401,6 +401,11 @@ StringRef ObjFile::getShtGroupSignature(ArrayRef sections, return CHECK(sym.getName(this->stringTable), this); } +template +StringRef ObjFile::getUniqueName(StringRef origName) const { + return origName; +} + template bool ObjFile::shouldMerge(const Elf_Shdr &sec, StringRef name) { // On a regular link we don't merge sections if -O0 (default is -O1). This @@ -522,24 +527,6 @@ ArrayRef ObjFile::getShndxTable() { return shndxTable; } -static StringRef markItemForAdlt(StringRef input, StringRef filePath) { - if (input.empty()) - return input; - StringRef simpleFile = path::stem(filePath); - std::string mark = "_" + simpleFile.str(); - if (input.endswith(mark)) - return input; - 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) { @@ -581,7 +568,7 @@ void ObjFile::initializeSections(bool ignoreComdats, } auto secName = check(obj.getSectionName(sec, shstrtab)); if (config->adlt && sec.sh_type == SHT_NULL) { - auto name = isPatchedSecName ? markItemForAdlt(secName, archiveName) : secName; + auto name = getUniqueName(secName); this->sections[i] = createInputSection(i, sec, name); this->sections[i]->address = sec.sh_addr; this->sections[i]->size = sec.sh_size; @@ -608,9 +595,7 @@ void ObjFile::initializeSections(bool ignoreComdats, .second; if (keepGroup) { if (config->relocatable) { - auto name = config->adlt && isPatchedSecName - ? markItemForAdlt(secName, archiveName) - : secName; + auto name = config->adlt ? getUniqueName(secName) : secName; this->sections[i] = createInputSection(i, sec, name); if (config->adlt) { this->sections[i]->address = sec.sh_addr; @@ -643,9 +628,7 @@ void ObjFile::initializeSections(bool ignoreComdats, ctx->hasSympart.store(true, std::memory_order_relaxed); LLVM_FALLTHROUGH; default: - auto name = config->adlt && isPatchedSecName - ? markItemForAdlt(secName, archiveName) - : secName; + auto name = config->adlt ? getUniqueName(secName) : secName; this->sections[i] = createInputSection(i, sec, name); if (config->adlt) { this->sections[i]->address = sec.sh_addr; @@ -1065,7 +1048,7 @@ void ObjFile::initializeSymbols(const object::ELFFile &obj) { if (!symbols[i]) { StringRef name = CHECK(eSyms[i].getName(stringTable), this); if (config->adlt && name == "__cfi_check") { - name = markItemForAdlt(name, archiveName); + name = this->getUniqueName(name); if (!ctx->adltWithCfi) ctx->adltWithCfi = true; } @@ -1708,15 +1691,25 @@ template void SharedFileExtended::postParseForAdlt() { resolveDuplicatesForAdlt(); } - template StringRef SharedFileExtended::addAdltPostfix(StringRef input) const { - return markItemForAdlt(input, soName); + if (input.empty()) return input; + auto suffix = Twine("__") + Twine::utohexstr(this->orderIdx); + return saver().save(input + suffix); } template bool SharedFileExtended::addAdltPostfix(Symbol *s) { - return markSymForAdlt(s, this->soName); + StringRef newName = addAdltPostfix(s->getName()); + if (s->getName() == newName) + return false; + s->setName(newName); + return true; +} + +template +StringRef SharedFileExtended::getUniqueName(StringRef origName) const { + return addAdltPostfix(origName); } // TODO: optimize 2 lookups diff --git a/lld/ELF/InputFiles.h b/lld/ELF/InputFiles.h index bd880dc712a8..5beec601526c 100644 --- a/lld/ELF/InputFiles.h +++ b/lld/ELF/InputFiles.h @@ -294,7 +294,8 @@ public: void initializeLocalSymbols(); void postParse(); - bool isPatchedSecName = true; // ADLT +protected: + virtual StringRef getUniqueName(StringRef origName) const; private: void initializeSections(bool ignoreComdats, @@ -456,8 +457,6 @@ public: // .symtab's start of global symbols owned by library llvm::Optional sharedGlobalSymbolIndex; - // Index of this file in context (struct Ctx) - unsigned ctxIdx = 0; // Output information data: llvm::SetVector programHeaderIndexes; // From input .rela.dyn, .rela.plt: @@ -481,6 +480,9 @@ public: // parsed. Only filled for `--no-allow-shlib-undefined`. SmallVector requiredSymbols; +protected: + virtual StringRef getUniqueName(StringRef origName) const override; + private: void parseDynamics(); // SharedFile compability void parseElfSymTab(); // ObjectFile compability diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp index 1e953665722c..20d232820b34 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -1311,7 +1311,7 @@ static void trackDynRelocAdlt(SharedFileExtended *soFile) { template static void trackGotPltAdlt(Symbol *sym, SharedFileExtended *soFile) { - ctx->gotPltInfoAdlt[sym].push_back(soFile->ctxIdx); + ctx->gotPltInfoAdlt[sym].push_back(soFile->orderIdx); } // ADLT BEGIN diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index 51303efa219b..36d222caf77c 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -2006,7 +2006,6 @@ template void Writer::finalizeSections() { if (config->adlt) for (auto &it : llvm::enumerate(ctx->sharedFilesExtended)) { auto *soFile = cast>(it.value()); - soFile->ctxIdx = it.index(); auto sections = soFile->getSections(); // scan .rela.dyn (base: SHT_NULL) scanRelocations(*sections[0]); diff --git a/llvm/tools/llvm-readobj/ELFDumper.cpp b/llvm/tools/llvm-readobj/ELFDumper.cpp index 48e9ec0d2c25..07e2fbaefaf1 100644 --- a/llvm/tools/llvm-readobj/ELFDumper.cpp +++ b/llvm/tools/llvm-readobj/ELFDumper.cpp @@ -7072,7 +7072,16 @@ template void LLVMELFDumper::printAdltSection() { const adlt_psod_t& psod = *reinterpret_cast (psodsRaw.data() + psodIdx * header->schemaPSODSize); - DictScope LEntry(W, ("#" + Twine(psodIdx)).str()); + auto psodName = Twine("psod-#") + Twine(psodIdx); + DictScope LEntry(W, psodName.str()); + + { + DictScope MetaEntry(W, "$-meta"); + auto psodSuffix = Twine("__") + Twine::utohexstr(psodIdx); + W.printNumber("$-order", psodIdx); + W.printString("$-symbol-suffix", psodSuffix.str()); + } + W.printString("soname", this->getAdltDynamicString(psod.soName)); W.printHex("soname-hash", psod.soNameHash); { -- Gitee