From 5c36934f74cc05cbe989b335685708ac2d6ba3c8 Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Thu, 25 Jan 2024 09:40:52 -0500 Subject: [PATCH] Gen .data.rel.ro. Fix nop. Minor changes. Change-Id: Iddd207437d8f65d29953ec347806ce7507244b7b Signed-off-by: Anton Volkov --- lld/ELF/Arch/AArch64.cpp | 2 ++ lld/ELF/InputFiles.cpp | 9 ++++++--- lld/ELF/InputFiles.h | 4 ++-- lld/ELF/LinkerScript.cpp | 4 +++- lld/ELF/Relocations.cpp | 14 ++++++++++---- lld/ELF/SyntheticSections.cpp | 4 ++-- lld/ELF/Writer.cpp | 10 +++++++++- 7 files changed, 34 insertions(+), 13 deletions(-) diff --git a/lld/ELF/Arch/AArch64.cpp b/lld/ELF/Arch/AArch64.cpp index 7274232d1469..4c3e5aff9ef2 100644 --- a/lld/ELF/Arch/AArch64.cpp +++ b/lld/ELF/Arch/AArch64.cpp @@ -455,6 +455,8 @@ void AArch64::relocate(uint8_t *loc, const Relocation &rel, checkInt(loc, val, 33, rel); LLVM_FALLTHROUGH; case R_AARCH64_ADR_PREL_PG_HI21_NC: + if (config->adlt && *(uint32_t*)loc == 0xd503201f) // ignore nop + break; write32AArch64Addr(loc, val >> 12); break; case R_AARCH64_ADR_PREL_LO21: diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp index ffd2511d0b1c..f4f8b81b018f 100644 --- a/lld/ELF/InputFiles.cpp +++ b/lld/ELF/InputFiles.cpp @@ -1667,7 +1667,8 @@ template void SharedFileExtended::parseForAdlt() { lld::outs() << "Parse offsets of some sections:\n"; for (const Elf_Shdr &sec : objSections) { auto name = check(obj.getSectionName(sec, shstrtab)); - if (name == ".init_array" || name == ".fini_array" || name == ".data") + if (name == ".init_array" || name == ".fini_array" || name == ".data" || + name == ".data.rel.ro" || name == ".bss.rel.ro" || name == ".bss") traceElfSection(sec); } lld::outs() << '\n'; @@ -1680,12 +1681,12 @@ template void SharedFileExtended::postParseForAdlt() { } template -StringRef SharedFileExtended::addAdltPrefix(StringRef input) { +StringRef SharedFileExtended::addAdltPostfix(StringRef input) { return markItemForAdlt(input, soName); } template -bool SharedFileExtended::addAdltPrefix(Symbol *s) { +bool SharedFileExtended::addAdltPostfix(Symbol *s) { StringRef newName = markItemForAdlt(s->getName(), soName); if (s->getName() == newName) return false; @@ -2007,6 +2008,8 @@ template void SharedFileExtended::parseElfSymTab() { const Elf_Shdr *eSec = *p; InputSectionBase *sec = this->sections[secIdx]; auto val = eSym.st_value - eSec->sh_addr; + if (name.startswith("__")) + name = addAdltPostfix(name); this->allSymbols[i] = make(this, name, bind, other, type, val, eSym.st_size, sec); } else { diff --git a/lld/ELF/InputFiles.h b/lld/ELF/InputFiles.h index 5207ef34344c..821e5f369db3 100644 --- a/lld/ELF/InputFiles.h +++ b/lld/ELF/InputFiles.h @@ -399,8 +399,8 @@ public: void parseForAdlt(); void postParseForAdlt(); - StringRef addAdltPrefix(StringRef input); - bool addAdltPrefix(Symbol *s); + StringRef addAdltPostfix(StringRef input); + bool addAdltPostfix(Symbol *s); Defined *findSectionSymbol(uint64_t offset) const; Defined *findDefinedSymbol( diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp index 51c505d159d5..68729e4ebc4b 100644 --- a/lld/ELF/LinkerScript.cpp +++ b/lld/ELF/LinkerScript.cpp @@ -104,7 +104,9 @@ static StringRef getOutputSectionName(const InputSectionBase *s) { {".data.rel.ro", ".data", ".rodata", ".bss.rel.ro", ".bss", ".gcc_except_table", ".init_array", ".fini_array", ".tbss", ".tdata", ".ARM.exidx", ".ARM.extab", ".ctors", ".dtors", ".ohos.randomdata"}) // OHOS_LOCAL - if (isSectionPrefix(v, s->name)) + if (config->adlt && s->name.startswith(v)) + return s->name; + else if (isSectionPrefix(v, s->name)) return v; return s->name; diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp index 278c4927bccf..de31d8cc2af4 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -1427,13 +1427,19 @@ template void RelocationScanner::scanOne(RelTy *&i) { Symbol *s = &sym; if (!(expr == R_PLT || expr == R_PLT_PC || expr == R_GOT || expr == R_GOT_PC)) - if (Symbol *tmp = &file->getSymbolFromElfSymTab(symIndex)) - if (tmp->getName() != s->getName()) - s = tmp; + if (Symbol *tmp = &file->getSymbolFromElfSymTab(symIndex)) { + if (isDebug) + traceSym(debugTitle + "changed to tmp: ", *tmp); + s = tmp; + } s = sym.getName().size() ? elf::symtab->find(sym.getName()) : &sym; - if (!s) // if symbol was not found in elf::symtab + if (!s) { // if symbol was not found in elf::symtab s = &sym; + in.symTab->addSymbol(s); + } + if (isDebug) + traceSym(debugTitle + "before push: ", *s); if (s->isUndefined()) { if (expr == R_PLT || expr == R_PLT_PC) diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp index 8a7208698c6d..29135ddb16d7 100644 --- a/lld/ELF/SyntheticSections.cpp +++ b/lld/ELF/SyntheticSections.cpp @@ -1560,8 +1560,8 @@ DynamicSection::computeContents() { }; for (InputFile *file : ctx->sharedFilesExtended) { auto *f = cast>(file); - auto initArray = findSection(f->addAdltPrefix(".init_array")); - auto finiArray = findSection(f->addAdltPrefix(".fini_array")); + auto initArray = findSection(f->addAdltPostfix(".init_array")); + auto finiArray = findSection(f->addAdltPostfix(".fini_array")); if (initArray) { addInt(DT_INIT_ARRAY, initArray->addr); addInt(DT_INIT_ARRAYSZ, initArray->size); diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index a3db65a4732c..de16dec8e609 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -273,7 +273,9 @@ void elf::addReservedSymbols() { static OutputSection *findSection(StringRef name, unsigned partition = 1) { for (SectionCommand *cmd : script->sectionCommands) if (auto *osd = dyn_cast(cmd)) - if (osd->osec.name == name && osd->osec.partition == partition) + if ((config->adlt ? osd->osec.name.startswith(name) + : osd->osec.name == name) && + osd->osec.partition == partition) return &osd->osec; return nullptr; } @@ -831,6 +833,12 @@ static bool isRelroSection(const OutputSection *sec) { // ELF in spirit. But in reality many linker features depend on // magic section names. StringRef s = sec->name; + if (config->adlt) + return s.startswith(".data.rel.ro") || s.startswith(".bss.rel.ro") || + s.startswith(".ctors") || s.startswith(".dtors") || + s.startswith(".eh_frame") || s.startswith(".fini_array") || + s.startswith(".init_array") || s.startswith(".preinit_array"); + return s == ".data.rel.ro" || s == ".bss.rel.ro" || s == ".ctors" || s == ".dtors" || s == ".jcr" || s == ".eh_frame" || s == ".fini_array" || s == ".init_array" || -- Gitee