From 18ab0bc4233b6a64a61c05a60b22b43bf2bdca68 Mon Sep 17 00:00:00 2001 From: Anton Date: Fri, 15 Dec 2023 17:18:07 +0300 Subject: [PATCH 01/77] LLD Adlt Signed-off-by: Anton Volkov --- .gitignore | 3 + lld/ELF/Arch/AArch64.cpp | 89 ++++++ lld/ELF/Config.h | 2 + lld/ELF/Driver.cpp | 93 +++++- lld/ELF/DriverUtils.cpp | 8 + lld/ELF/InputFiles.cpp | 560 +++++++++++++++++++++++++++++++++- lld/ELF/InputFiles.h | 106 ++++++- lld/ELF/InputSection.cpp | 25 +- lld/ELF/InputSection.h | 11 +- lld/ELF/MapFile.cpp | 6 +- lld/ELF/MarkLive.cpp | 3 +- lld/ELF/Options.td | 2 + lld/ELF/Relocations.cpp | 163 +++++++++- lld/ELF/Symbols.h | 6 +- lld/ELF/SyntheticSections.cpp | 68 ++++- lld/ELF/Target.h | 3 + lld/ELF/Writer.cpp | 36 ++- 17 files changed, 1115 insertions(+), 69 deletions(-) diff --git a/.gitignore b/.gitignore index 20c4f52cd378..46ab415ae35f 100644 --- a/.gitignore +++ b/.gitignore @@ -70,3 +70,6 @@ pythonenv* /clang/utils/analyzer/projects/*/RefScanBuildResults # automodapi puts generated documentation files here. /lldb/docs/python_api/ + + +musl_copy_* \ No newline at end of file diff --git a/lld/ELF/Arch/AArch64.cpp b/lld/ELF/Arch/AArch64.cpp index b23684819a23..7274232d1469 100644 --- a/lld/ELF/Arch/AArch64.cpp +++ b/lld/ELF/Arch/AArch64.cpp @@ -44,6 +44,8 @@ public: uint32_t getThunkSectionSpacing() const override; bool inBranchRange(RelType type, uint64_t src, uint64_t dst) const override; bool usesOnlyLowPageBits(RelType type) const override; + void deRelocate(uint8_t *loc, const Relocation &rel, + uint64_t *val) const override; // ADLT void relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const override; RelExpr adjustTlsExpr(RelType type, RelExpr expr) const override; @@ -335,8 +337,95 @@ static void writeSMovWImm(uint8_t *loc, uint32_t imm) { write32le(loc, inst | ((imm & 0xFFFF) << 5)); } +static void adltRelocateDebugMessage(StringRef title, uint8_t *loc, + const Relocation &rel, uint64_t &val) { + lld::outs() << title + << " loc: " << loc /*<< " loc val: " << Twine::utohexstr(*loc)*/ + << " rel expr: " << rel.expr + << " rel type: " << lld::toString(rel.type) << " rel offset: 0x" + << Twine::utohexstr(rel.offset) << " rel addend: 0x" + << Twine::utohexstr(rel.addend) + << " val: 0x" << Twine::utohexstr(val) + << " rel sym: " << (rel.sym ? rel.sym->getName() : "null!"); + + if (rel.sym && rel.sym->isDefined()) { + auto d = cast(rel.sym); + lld::outs() << ": section " << d->section->name + << ", value 0x" << Twine::utohexstr(d->value); + } + lld::outs() << '\n'; +} + +void AArch64::deRelocate(uint8_t *loc, const Relocation &rel, + uint64_t *val) const { + bool isDebug = false; + if (isDebug) + adltRelocateDebugMessage("[AArch64::deRelocate]: ", loc, rel, *val); + + auto orClear32le = [&]() { + write32le(loc, read32le(loc) & ~((1 << 26) -1)); + }; + + auto orClearAArch64Imm = [&]() { + write32le(loc, read32le(loc) & ~(0xFFF << 10)); + }; + + switch (rel.type) { + case R_AARCH64_JUMP26: + case R_AARCH64_CALL26: { + /*if (*val & (1UL << 32)) { + if (isDebug) + debugMessage("found 32 bit turned on! Disabling"); + *val = *val & ~(1UL << 32); + }*/ + orClear32le(); + break; + } + case R_AARCH64_CONDBR19: + case R_AARCH64_LD_PREL_LO19: + case R_AARCH64_MOVW_UABS_G0_NC: + case R_AARCH64_MOVW_UABS_G1_NC: + case R_AARCH64_MOVW_UABS_G2_NC: + case R_AARCH64_MOVW_UABS_G3: + case R_AARCH64_TSTBR14: + orClear32le(); + break; + case R_AARCH64_ADD_ABS_LO12_NC: + case R_AARCH64_LDST8_ABS_LO12_NC: + case R_AARCH64_TLSLE_LDST8_TPREL_LO12_NC: + case R_AARCH64_LDST16_ABS_LO12_NC: + case R_AARCH64_TLSLE_LDST16_TPREL_LO12_NC: + case R_AARCH64_LDST32_ABS_LO12_NC: + case R_AARCH64_TLSLE_LDST32_TPREL_LO12_NC: + case R_AARCH64_LDST64_ABS_LO12_NC: { + orClearAArch64Imm(); + break; + } + case R_AARCH64_ADR_GOT_PAGE: + case R_AARCH64_LD64_GOT_LO12_NC: + case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC: + case R_AARCH64_TLSLE_LDST64_TPREL_LO12_NC: + case R_AARCH64_TLSDESC_LD64_LO12: + case R_AARCH64_LDST128_ABS_LO12_NC: + case R_AARCH64_TLSLE_LDST128_TPREL_LO12_NC: + case R_AARCH64_LD64_GOTPAGE_LO15: + case R_AARCH64_TLSLE_ADD_TPREL_HI12: + case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC: + case R_AARCH64_TLSDESC_ADD_LO12: + orClearAArch64Imm(); + break; + } +} + void AArch64::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const { + bool isDebug = false; + if (config->adlt) { + deRelocate(loc, rel, &val); + } + if (config->adlt && isDebug) + adltRelocateDebugMessage("[AArch64::relocate]: ", loc, rel, val); + switch (rel.type) { case R_AARCH64_ABS16: case R_AARCH64_PREL16: diff --git a/lld/ELF/Config.h b/lld/ELF/Config.h index d6babe9f74c2..94e863502a89 100644 --- a/lld/ELF/Config.h +++ b/lld/ELF/Config.h @@ -220,6 +220,7 @@ struct Configuration { std::vector> shuffleSections; bool singleRoRx; bool shared; + bool adlt = false; bool symbolic; bool isStatic = false; bool sysvHash = false; @@ -384,6 +385,7 @@ struct Ctx { SmallVector> memoryBuffers; SmallVector objectFiles; SmallVector sharedFiles; + SmallVector sharedFilesExtended; SmallVector binaryFiles; SmallVector bitcodeFiles; SmallVector lazyBitcodeFiles; diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp index daf16ed3ca44..310938182694 100644 --- a/lld/ELF/Driver.cpp +++ b/lld/ELF/Driver.cpp @@ -278,8 +278,12 @@ void LinkerDriver::addFile(StringRef path, bool withLOption) { // the directory part is ignored. Note that path may be a temporary and // cannot be stored into SharedFile::soName. path = mbref.getBufferIdentifier(); - files.push_back( - make(mbref, withLOption ? path::filename(path) : path)); + if (config->adlt) + files.push_back(createSharedFileExtended( + mbref, withLOption ? path::filename(path) : path)); + else + files.push_back( + make(mbref, withLOption ? path::filename(path) : path)); return; case file_magic::bitcode: files.push_back(make(mbref, "", 0, inLib)); @@ -847,7 +851,8 @@ static std::pair getPackDynRelocs(opt::InputArgList &args) { static void readCallGraph(MemoryBufferRef mb) { // Build a map from symbol name to section DenseMap map; - for (ELFFileBase *file : ctx->objectFiles) + auto files = config->adlt ? ctx->sharedFilesExtended : ctx->objectFiles; + for (ELFFileBase *file : files) for (Symbol *sym : file->getSymbols()) map[sym->getName()] = sym; @@ -926,6 +931,7 @@ processCallGraphRelocations(SmallVector &symbolIndices, template static void readCallGraphsFromObjectFiles() { SmallVector symbolIndices; ArrayRef cgProfile; + // ADLT TODO for (auto file : ctx->objectFiles) { auto *obj = cast>(file); if (!processCallGraphRelocations(symbolIndices, cgProfile, obj)) @@ -1091,6 +1097,7 @@ static void readConfigs(opt::InputArgList &args) { else if (arg->getOption().matches(OPT_Bsymbolic)) config->bsymbolic = BsymbolicKind::All; } + config->adlt = args.hasArg(OPT_adlt); config->checkSections = args.hasFlag(OPT_check_sections, OPT_no_check_sections, true); config->chroot = args.getLastArgValue(OPT_chroot); @@ -1106,7 +1113,7 @@ static void readConfigs(opt::InputArgList &args) { config->dwoDir = args.getLastArgValue(OPT_plugin_opt_dwo_dir_eq); config->dynamicLinker = getDynamicLinker(args); config->ehFrameHdr = - args.hasFlag(OPT_eh_frame_hdr, OPT_no_eh_frame_hdr, false); + args.hasFlag(OPT_eh_frame_hdr, OPT_no_eh_frame_hdr, config->adlt); config->emitLLVM = args.hasArg(OPT_plugin_opt_emit_llvm, false); config->emitRelocs = args.hasArg(OPT_emit_relocs); config->callGraphProfileSort = args.hasFlag( @@ -1222,7 +1229,7 @@ static void readConfigs(opt::InputArgList &args) { config->searchPaths = args::getStrings(args, OPT_library_path); config->sectionStartMap = getSectionStartMap(args); - config->shared = args.hasArg(OPT_shared); + config->shared = config->adlt ? true : args.hasArg(OPT_shared); config->singleRoRx = !args.hasFlag(OPT_rosegment, OPT_no_rosegment, true); config->soName = args.getLastArgValue(OPT_soname); config->sortSection = getSortSection(args); @@ -1824,7 +1831,8 @@ static void excludeLibs(opt::InputArgList &args) { sym->versionId = VER_NDX_LOCAL; }; - for (ELFFileBase *file : ctx->objectFiles) + auto files = config->adlt ? ctx->sharedFilesExtended : ctx->objectFiles; + for (ELFFileBase *file : files) visit(file); for (BitcodeFile *file : ctx->bitcodeFiles) @@ -2020,7 +2028,8 @@ static void writeDependencyFile() { // symbols of type CommonSymbol. static void replaceCommonSymbols() { llvm::TimeTraceScope timeScope("Replace common symbols"); - for (ELFFileBase *file : ctx->objectFiles) { + auto files = config->adlt ? ctx->sharedFilesExtended : ctx->objectFiles; + for (ELFFileBase *file : files) { if (!file->hasCommonSyms) continue; for (Symbol *sym : file->getGlobalSymbols()) { @@ -2096,7 +2105,8 @@ static void findKeepUniqueSections(opt::InputArgList &args) { // Visit the address-significance table in each object file and mark each // referenced symbol as address-significant. - for (InputFile *f : ctx->objectFiles) { + auto files = config->adlt ? ctx->sharedFilesExtended : ctx->objectFiles; + for (InputFile *f : files) { auto *obj = cast>(f); ArrayRef syms = obj->getSymbols(); if (obj->addrsigSec) { @@ -2350,7 +2360,8 @@ static void redirectSymbols(ArrayRef wrapped) { return; // Update pointers in input files. - parallelForEach(ctx->objectFiles, [&](ELFFileBase *file) { + auto files = config->adlt ? ctx->sharedFilesExtended : ctx->objectFiles; + parallelForEach(files, [&](ELFFileBase *file) { for (Symbol *&sym : file->getMutableGlobalSymbols()) if (Symbol *s = map.lookup(sym)) sym = s; @@ -2385,7 +2396,8 @@ static uint32_t getAndFeatures() { return 0; uint32_t ret = -1; - for (ELFFileBase *f : ctx->objectFiles) { + auto files = config->adlt ? ctx->sharedFilesExtended : ctx->objectFiles; + for (ELFFileBase *f : files) { uint32_t features = f->andFeatures; checkAndReportMissingFeature( @@ -2468,6 +2480,46 @@ static void postParseObjectFile(ELFFileBase *file) { } } +static void postParseSharedFileForAdlt(ELFFileBase *file) { + switch (config->ekind) { + case ELF32LEKind: + cast>(file)->postParseForAdlt(); + break; + case ELF32BEKind: + cast>(file)->postParseForAdlt(); + break; + case ELF64LEKind: + cast>(file)->postParseForAdlt(); + break; + case ELF64BEKind: + cast>(file)->postParseForAdlt(); + break; + default: + llvm_unreachable(""); + } +} + +static bool isSectionValidForAdlt(int fileIdx, InputSectionBase *s) { + if (!s || s == &InputSection::discarded) + return false; + uint32_t type = s->type; + StringRef name = s->name; + + bool isBaseType = type == SHT_NOBITS || type == SHT_NOTE || + type == SHT_INIT_ARRAY || type == SHT_FINI_ARRAY; + // TODO: fix .debug_info relocation + bool isNeededProgBits = + type == SHT_PROGBITS && + !(name.startswith(".got.plt") || name.startswith(".plt") || name.startswith(".got") || + name.startswith(".eh_frame_hdr"));// || name.startswith(".debug_")); + bool ret = isBaseType || isNeededProgBits; + + bool isDebug = false; + if (ret && isDebug) + lld::outs() << "[isSectionValidForAdlt]: " << name << "\n"; + return ret; +} + // Do actual linking. Note that when this function is called, // all linker scripts have already been parsed. void LinkerDriver::link(opt::InputArgList &args) { @@ -2537,8 +2589,9 @@ void LinkerDriver::link(opt::InputArgList &args) { // producing a shared library. // We also need one if any shared libraries are used and for pie executables // (probably because the dynamic linker needs it). - config->hasDynSymTab = - !ctx->sharedFiles.empty() || config->isPic || config->exportDynamic; + config->hasDynSymTab = (config->adlt ? !ctx->sharedFilesExtended.empty() + : !ctx->sharedFiles.empty()) || + config->isPic || config->exportDynamic; // Some symbols (such as __ehdr_start) are defined lazily only when there // are undefined symbols for them, so we add these to trigger that logic. @@ -2593,6 +2646,9 @@ void LinkerDriver::link(opt::InputArgList &args) { // No more lazy bitcode can be extracted at this point. Do post parse work // like checking duplicate symbols. + if (config->adlt) + parallelForEach(ctx->sharedFilesExtended, postParseSharedFileForAdlt); + parallelForEach(ctx->objectFiles, initializeLocalSymbols); parallelForEach(ctx->objectFiles, postParseObjectFile); parallelForEach(ctx->bitcodeFiles, @@ -2660,6 +2716,7 @@ void LinkerDriver::link(opt::InputArgList &args) { // With this the symbol table should be complete. After this, no new names // except a few linker-synthesized ones will be added to the symbol table. const size_t numObjsBeforeLTO = ctx->objectFiles.size(); + const size_t numSoBeforeLTO = ctx->sharedFilesExtended.size(); invokeELFT(compileBitcodeFiles, skipLinkedOutput); // Symbol resolution finished. Report backward reference problems, @@ -2676,6 +2733,11 @@ void LinkerDriver::link(opt::InputArgList &args) { // compileBitcodeFiles may have produced lto.tmp object files. After this, no // more file will be added. + if (config->adlt) { + auto newSharedFiles = + makeArrayRef(ctx->sharedFilesExtended).slice(numSoBeforeLTO); + parallelForEach(newSharedFiles, postParseSharedFileForAdlt); + } auto newObjectFiles = makeArrayRef(ctx->objectFiles).slice(numObjsBeforeLTO); parallelForEach(newObjectFiles, initializeLocalSymbols); parallelForEach(newObjectFiles, postParseObjectFile); @@ -2699,6 +2761,13 @@ void LinkerDriver::link(opt::InputArgList &args) { // Now that we have a complete list of input files. // Beyond this point, no new files are added. // Aggregate all input sections into one place. + if (config->adlt) + for (auto it : llvm::enumerate(ctx->sharedFilesExtended)) + for (InputSectionBase *s : it.value()->getSections()) + if (isSectionValidForAdlt(it.index(), s)) + inputSections.push_back(s); + + for (InputFile *f : ctx->objectFiles) for (InputSectionBase *s : f->getSections()) if (s && s != &InputSection::discarded) diff --git a/lld/ELF/DriverUtils.cpp b/lld/ELF/DriverUtils.cpp index 51f3dc3a056e..db42b6a36028 100644 --- a/lld/ELF/DriverUtils.cpp +++ b/lld/ELF/DriverUtils.cpp @@ -64,6 +64,13 @@ static void handleColorDiagnostics(opt::InputArgList &args) { error("unknown option: --color-diagnostics=" + s); } +static void handleAdltOption(opt::InputArgList &args) { + auto *arg = args.getLastArg(OPT_adlt); + if (!arg) + return; + lld::outs() << "adlt is active\n"; +} + static cl::TokenizerCallback getQuotingStyle(opt::InputArgList &args) { if (auto *arg = args.getLastArg(OPT_rsp_quoting)) { StringRef s = arg->getValue(); @@ -120,6 +127,7 @@ opt::InputArgList ELFOptTable::parse(ArrayRef argv) { args = this->ParseArgs(vec, missingIndex, missingCount); handleColorDiagnostics(args); + handleAdltOption(args); if (missingCount) error(Twine(args.getArgString(missingIndex)) + ": missing argument"); diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp index 2518bbaa9af6..33df282ec9e2 100644 --- a/lld/ELF/InputFiles.cpp +++ b/lld/ELF/InputFiles.cpp @@ -145,13 +145,13 @@ static bool isCompatible(InputFile *file) { } InputFile *existing = nullptr; - if (!ctx->objectFiles.empty()) - existing = ctx->objectFiles[0]; - else if (!ctx->sharedFiles.empty()) - existing = ctx->sharedFiles[0]; - else if (!ctx->bitcodeFiles.empty()) - existing = ctx->bitcodeFiles[0]; - std::string with; + if (!ctx->objectFiles.empty()) + existing = ctx->objectFiles[0]; + else if (!ctx->sharedFiles.empty()) + existing = ctx->sharedFiles[0]; + else if (!ctx->bitcodeFiles.empty()) + existing = ctx->bitcodeFiles[0]; + std::string with; if (existing) with = " with " + toString(existing); error(toString(file) + " is incompatible" + with); @@ -184,6 +184,13 @@ template static void doParseFile(InputFile *file) { message(toString(file)); // .so file + if (config->adlt) + if (auto *f = dyn_cast>(file)) { + ctx->sharedFilesExtended.push_back(cast(file)); + f->parseForAdlt(); + return; + } + if (auto *f = dyn_cast(file)) { f->parse(); return; @@ -509,6 +516,21 @@ static void handleSectionGroup(ArrayRef sections, prev->nextInSectionGroup = head; } +template +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); +} + template void ObjFile::initializeSections(bool ignoreComdats, const llvm::object::ELFFile &obj) { @@ -548,6 +570,13 @@ void ObjFile::initializeSections(bool ignoreComdats, this->sections[i] = &InputSection::discarded; continue; } + auto secName = check(obj.getSectionName(sec, shstrtab)); + if (config->adlt && sec.sh_type == SHT_NULL) { + auto name = isPatchedSecName ? markItemForAdlt(secName, archiveName) : secName; + this->sections[i] = createInputSection(i, sec, name); + this->sections[i]->address = sec.sh_addr; + this->sections[i]->size = sec.sh_size; + } switch (sec.sh_type) { case SHT_GROUP: { @@ -569,9 +598,16 @@ void ObjFile::initializeSections(bool ignoreComdats, symtab->comdatGroups.try_emplace(CachedHashStringRef(signature), this) .second; if (keepGroup) { - if (config->relocatable) - this->sections[i] = createInputSection( - i, sec, check(obj.getSectionName(sec, shstrtab))); + if (config->relocatable) { + auto name = config->adlt && isPatchedSecName + ? markItemForAdlt(secName, archiveName) + : secName; + this->sections[i] = createInputSection(i, sec, name); + if (config->adlt) { + this->sections[i]->address = sec.sh_addr; + this->sections[i]->size = sec.sh_size; + } + } selectedGroups.push_back(entries); continue; } @@ -598,8 +634,14 @@ void ObjFile::initializeSections(bool ignoreComdats, ctx->hasSympart.store(true, std::memory_order_relaxed); LLVM_FALLTHROUGH; default: - this->sections[i] = - createInputSection(i, sec, check(obj.getSectionName(sec, shstrtab))); + auto name = config->adlt && isPatchedSecName + ? markItemForAdlt(secName, archiveName) + : secName; + this->sections[i] = createInputSection(i, sec, name); + if (config->adlt) { + this->sections[i]->address = sec.sh_addr; + this->sections[i]->size = sec.sh_size; + } } } @@ -1028,6 +1070,10 @@ void ObjFile::initializeSymbols(const object::ELFFile &obj) { uint8_t stOther = eSym.st_other; uint8_t type = eSym.getType(); uint64_t value = eSym.st_value; + if (config->adlt) { + const Elf_Shdr *eSec = *obj.getSection(secIdx); + value -= eSec->sh_addr; + } uint64_t size = eSym.st_size; Symbol *sym = symbols[i]; @@ -1095,9 +1141,12 @@ template void ObjFile::initializeLocalSymbols() { if (eSym.st_shndx == SHN_UNDEF || sec == &InputSection::discarded) new (symbols[i]) Undefined(this, name, STB_LOCAL, eSym.st_other, type, /*discardedSecIdx=*/secIdx); - else + else { + const Elf_Shdr *eSec = *this->getObj().getSection(secIdx); + auto value = config->adlt ? eSym.st_value - eSec->sh_addr : eSym.st_value; new (symbols[i]) Defined(this, name, STB_LOCAL, eSym.st_other, type, - eSym.st_value, eSym.st_size, sec); + value, eSym.st_size, sec); + } symbols[i]->isUsedInRegularObj = true; } } @@ -1290,6 +1339,36 @@ std::vector SharedFile::parseVerneed(const ELFFile &obj, return verneeds; } +template +std::vector SharedFileExtended::parseVerneed(const ELFFile &obj, + const typename ELFT::Shdr *sec) { + if (!sec) + return {}; + std::vector verneeds; + ArrayRef data = CHECK(obj.getSectionContents(*sec), this); + const uint8_t *verneedBuf = data.begin(); + for (unsigned i = 0; i != sec->sh_info; ++i) { + if (verneedBuf + sizeof(typename ELFT::Verneed) > data.end()) + fatal(toString(this) + " has an invalid Verneed"); + auto *vn = reinterpret_cast(verneedBuf); + const uint8_t *vernauxBuf = verneedBuf + vn->vn_aux; + for (unsigned j = 0; j != vn->vn_cnt; ++j) { + if (vernauxBuf + sizeof(typename ELFT::Vernaux) > data.end()) + fatal(toString(this) + " has an invalid Vernaux"); + auto *aux = reinterpret_cast(vernauxBuf); + if (aux->vna_name >= this->stringTable.size()) + fatal(toString(this) + " has a Vernaux with an invalid vna_name"); + uint16_t version = aux->vna_other & VERSYM_VERSION; + if (version >= verneeds.size()) + verneeds.resize(version + 1); + verneeds[version] = aux->vna_name; + vernauxBuf += aux->vna_next; + } + verneedBuf += vn->vn_next; + } + return verneeds; +} + // We do not usually care about alignments of data in shared object // files because the loader takes care of it. However, if we promote a // DSO symbol to point to .bss due to copy relocation, we need to keep @@ -1497,6 +1576,433 @@ template void SharedFile::parse() { } } +template +void SharedFileExtended::resolveDuplicatesForAdlt() { + auto pred = [&](DuplicateSymbol dup) { + auto sym = dup.sym; + if (!sym->isDefined()) + return true; + + auto d = cast(sym); + return !d->section && !d->getOutputSection(); + }; + static std::mutex mu; + { + std::lock_guard lock(mu); + llvm::erase_if(ctx->duplicates, pred); + } + + if (ctx->duplicates.empty()) + return; + + { + std::lock_guard lock(mu); + for (DuplicateSymbol &dup : ctx->duplicates) + warn("duplicate: " + dup.file->getName() + ": " + dup.section->name + + ": " + dup.sym->getName()); + ctx->duplicates.clear(); + } +} + +template +SharedFileExtended::SharedFileExtended(MemoryBufferRef mb, + StringRef soName) + : ObjFile(mb, soName), soName(soName), + simpleSoName(path::stem(soName)) { + const_cast(this->fileKind) = InputFile::SharedKind; +} + +template void SharedFileExtended::parseForAdlt() { + this->parse(); + parseDynamics(); + parseElfSymTab(); + + bool isDebug = false; + if (!isDebug) + return; + + const ELFFile obj = this->getObj(); + ArrayRef objSections = this->template getELFShdrs(); + StringRef shstrtab = getShStrTab(objSections); + + lld::outs() << "Parse symbols from .symtab:\n"; + const Elf_Shdr *elfSymTab = *obj.getSection(symTabSecIdx); + StringRef strTable = *obj.getStringTableForSymtab(*elfSymTab); + + auto eSyms = *obj.symbols(elfSymTab); + this->symbols.resize(this->symbols.size() + eSyms.size()); + for (const Elf_Sym &sym : eSyms) { + if (!sym.isDefined()) + continue; + + auto rawSec = obj.getSection(sym.st_shndx); + if (rawSec.takeError()) + continue; + if (isDebug) + traceElfSymbol(sym, strTable); + } + if (isDebug) + lld::outs() << '\n'; + + 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") + traceElfSection(sec); + } + lld::outs() << '\n'; +} + +template void SharedFileExtended::postParseForAdlt() { + this->initializeLocalSymbols(); + this->postParse(); + resolveDuplicatesForAdlt(); +} + +template +StringRef SharedFileExtended::addAdltPrefix(StringRef input) { + return markItemForAdlt(input, soName); +} + +template +bool SharedFileExtended::addAdltPrefix(Symbol *s) { + StringRef newName = markItemForAdlt(s->getName(), soName); + if (s->getName() == newName) + return false; + s->setName(newName); + return true; +} + +template +Defined* SharedFileExtended::findSectionSymbol(uint64_t offset) { + auto predRange = [=](Symbol *sym) { + if (!sym || sym->isUndefined() || !sym->isSection()) + return false; + const Defined *d = cast(sym); + uint64_t low = d->section->address; + uint64_t high = low + d->section->size; + return (offset >= low) && (offset < high); + }; + + auto i = this->allSymbols.begin(); + auto e = this->allSymbols.end(); + auto ret = std::find_if(i, e, predRange); + if (ret != e) // item was found + return cast(*ret); + + return nullptr; +} + +template +StringRef SharedFileExtended::getShStrTab(ArrayRef elfSections) { + return CHECK(this->getObj().getSectionStringTable(elfSections), this); +} + +template +void SharedFileExtended::traceElfSymbol(const Elf_Sym &sym, + StringRef strTable) const { + const ELFFile obj = this->getObj(); + auto rawSec = obj.getSection(sym.st_shndx); + auto parsedSec = !rawSec.takeError() ? *obj.getSection(sym.st_shndx) : nullptr; + lld::outs() << "File: " << soName << " symName: " << *sym.getName(strTable) + << " val: 0x" << Twine::utohexstr(sym.st_value) << " sec of sym: " + << (parsedSec ? *obj.getSectionName(*parsedSec) : "unknown!") + << " sym type: 0x" << Twine::utohexstr(sym.getType()) + << " sym binding: 0x" << Twine::utohexstr(sym.getBinding()) + << '\n'; +} + +template +void SharedFileExtended::traceElfSection(const Elf_Shdr &sec) const { + const ELFFile obj = this->getObj(); + + auto secName = *obj.getSectionName(sec); + lld::outs() << "File: " << soName << " sec: " << secName << " sec addr: 0x" + << Twine::utohexstr(sec.sh_addr) << " sec offs: 0x" + << Twine::utohexstr(sec.sh_offset) << " sec ent size: 0x" + << Twine::utohexstr(sec.sh_entsize) << '\n'; +} + +template +void SharedFileExtended::traceSymbol(const Symbol& sym) const { + lld::outs() << "File: " << soName << " symName: " << sym.getName(); + if (!sym.isDefined()) { + lld::outs() << '\n'; + return; + } + auto &d = cast(sym); + lld::outs() << " val: 0x" << Twine::utohexstr(d.value) << " sec of sym: " + << (d.section ? d.section->name : "unknown!") + << " sym type: 0x" << Twine::utohexstr(sym.type) + << " sym binding: 0x" << Twine::utohexstr(sym.binding) + << '\n'; +} + +template +void SharedFileExtended::traceSection(const SectionBase& sec) const { + lld::outs() << "File: " << soName << " sec: " << sec.name << " sec addr: 0x" + << Twine::utohexstr(sec.address) << " sec offs: 0x" + << Twine::utohexstr(sec.getOffset(0)) << " sec ent size: 0x" + << Twine::utohexstr(sec.entsize) << '\n'; +} + +template +Symbol& SharedFileExtended::getSymbol(uint32_t symbolIndex) const { + if (symbolIndex >= this->symbols.size()) + return getSymbolFromElfSymTab(symbolIndex); + return *this->symbols[symbolIndex]; +} + +template +Defined *SharedFileExtended::findSymbolByValue(uint32_t value) const { + bool isDebug = false; + auto predValues = [=](Symbol *sym) { + if (!sym || sym->isUndefined()) + return false; + const Defined *d = cast(sym); + bool goodVal = d->section->address + d->value == value; + bool goodFile = d->file == this; + bool isGood = goodVal && goodFile; + if (isDebug && isGood) + lld::outs() << "good\n"; + return isGood; + }; + + // filter by value for .dynsym, else for .symtab + SmallVector matches; + auto i = this->symbols.begin(); + auto e = this->symbols.end(); + std::copy_if(i, e, std::back_inserter(matches), predValues); + + if (matches.empty()) { + auto i = this->allSymbols.begin(); + auto e = this->allSymbols.end(); + std::copy_if(i, e, std::back_inserter(matches), predValues); + } + + if (matches.empty()) { + // warn("Symbol not found! Value: 0x" + Twine::utohexstr(value)); + return nullptr; + } + + // filter by type else return first item + i = matches.begin(); + e = matches.end(); + auto predTypes = [=](Symbol *sym) { + return sym->type == STT_FUNC || sym->type == STT_OBJECT; + }; + auto ret = std::find_if(i, e, predTypes); + if (ret != e) // item was found + return cast(*ret); + + // todo fix it + return cast(*i); +} + +template void SharedFileExtended::parseDynamics() { + const ELFFile obj = this->getObj(); + ArrayRef sections = this->template getELFShdrs(); + + ArrayRef dynamicTags; + const Elf_Shdr *versymSec = nullptr; + const Elf_Shdr *verdefSec = nullptr; + const Elf_Shdr *verneedSec = nullptr; + + // parse external lib deps + int secIdx = 0; + for (const Elf_Shdr &sec : sections) { + switch (sec.sh_type) { + case SHT_DYNAMIC: + dynamicTags = + CHECK(obj.template getSectionContentsAsArray(sec), this); + break; + case SHT_DYNSYM: + dynSymSecIdx = secIdx; + break; + case SHT_SYMTAB: + symTabSecIdx = secIdx; + break; + case SHT_SYMTAB_SHNDX: + symTabShndxSecIdx = secIdx; + break; + case SHT_GNU_versym: + versymSec = &sec; + break; + case SHT_GNU_verdef: + verdefSec = &sec; + break; + case SHT_GNU_verneed: + verneedSec = &sec; + break; + } + secIdx++; + } + + if (versymSec && this->numELFSyms == 0) { + error("SHT_GNU_versym should be associated with symbol table"); + return; + } + + // Search for a DT_SONAME tag to initialize this->soName. + for (const Elf_Dyn &dyn : dynamicTags) { + if (dyn.d_tag == DT_NEEDED) { + uint64_t val = dyn.getVal(); + if (val >= this->stringTable.size()) + fatal(toString(this) + ": invalid DT_NEEDED entry"); + dtNeeded.push_back(this->stringTable.data() + val); + } else if (dyn.d_tag == DT_SONAME) { + uint64_t val = dyn.getVal(); + if (val >= this->stringTable.size()) + fatal(toString(this) + ": invalid DT_SONAME entry"); + soName = this->stringTable.data() + val; + } + } + + verdefs = parseVerdefs(obj.base(), verdefSec); + std::vector verneeds = parseVerneed(obj, verneedSec); + + // Parse ".gnu.version" section which is a parallel array for the symbol + // table. If a given file doesn't have a ".gnu.version" section, we use + // VER_NDX_GLOBAL. + size_t size = this->numELFSyms - this->firstGlobal; + std::vector versyms(size, VER_NDX_GLOBAL); + if (versymSec) { + ArrayRef versym = + CHECK(obj.template getSectionContentsAsArray(*versymSec), + this) + .slice(this->firstGlobal); + for (size_t i = 0; i < size; ++i) + versyms[i] = versym[i].vs_index; + } + + // System libraries can have a lot of symbols with versions. Using a + // fixed buffer for computing the versions name (foo@ver) can save a + // lot of allocations. + SmallString<0> versionedNameBuffer; + + // Add symbols to the symbol table. + SymbolTable &symtab = *elf::symtab; + ArrayRef syms = this->template getGlobalELFSyms(); + for (size_t i = 0, e = syms.size(); i != e; ++i) { + const Elf_Sym &sym = syms[i]; + + // ELF spec requires that all local symbols precede weak or global + // symbols in each symbol table, and the index of first non-local symbol + // is stored to sh_info. If a local symbol appears after some non-local + // symbol, that's a violation of the spec. + StringRef name = CHECK(sym.getName(this->stringTable), this); + if (sym.getBinding() == STB_LOCAL) { + warn("found local symbol '" + name + + "' in global part of symbol table in file " + toString(this)); + continue; + } + + uint16_t idx = versyms[i] & ~VERSYM_HIDDEN; + if (sym.isUndefined()) { + // For unversioned undefined symbols, VER_NDX_GLOBAL makes more sense but + // as of binutils 2.34, GNU ld produces VER_NDX_LOCAL. + if (idx != VER_NDX_LOCAL && idx != VER_NDX_GLOBAL) { + if (idx >= verneeds.size()) { + error("corrupt input file: version need index " + Twine(idx) + + " for symbol " + name + " is out of bounds\n>>> defined in " + + toString(this)); + continue; + } + StringRef verName = this->stringTable.data() + verneeds[idx]; + versionedNameBuffer.clear(); + name = saver().save( + (name + "@" + verName).toStringRef(versionedNameBuffer)); + } + Symbol *s = symtab.addSymbol( + Undefined{this, name, sym.getBinding(), sym.st_other, sym.getType()}); + s->exportDynamic = true; + if (s->isUndefined() && sym.getBinding() != STB_WEAK && + config->unresolvedSymbolsInShlib != UnresolvedPolicy::Ignore) + requiredSymbols.push_back(s); + continue; + } + // MIPS BFD linker puts _gp_disp symbol into DSO files and incorrectly + // assigns VER_NDX_LOCAL to this section global symbol. Here is a + // workaround for this bug. + if (config->emachine == EM_MIPS && idx == VER_NDX_LOCAL && + name == "_gp_disp") + continue; + + uint32_t alignment = getAlignment(sections, sym); + if (!(versyms[i] & VERSYM_HIDDEN)) { + auto *s = symtab.addSymbol( + SharedSymbol{*this, name, sym.getBinding(), sym.st_other, + sym.getType(), sym.st_value, sym.st_size, alignment}); + if (s->file == this) + s->verdefIndex = idx; + } + + // Also add the symbol with the versioned name to handle undefined symbols + // with explicit versions. + if (idx == VER_NDX_GLOBAL) + continue; + + if (idx >= verdefs.size() || idx == VER_NDX_LOCAL) { + error("corrupt input file: version definition index " + Twine(idx) + + " for symbol " + name + " is out of bounds\n>>> defined in " + + toString(this)); + continue; + } + + StringRef verName = + this->stringTable.data() + + reinterpret_cast(verdefs[idx])->getAux()->vda_name; + versionedNameBuffer.clear(); + name = (name + "@" + verName).toStringRef(versionedNameBuffer); + auto *s = symtab.addSymbol( + SharedSymbol{*this, saver().save(name), sym.getBinding(), sym.st_other, + sym.getType(), sym.st_value, sym.st_size, alignment}); + if (s->file == this) + s->verdefIndex = idx; + } +} + +template void SharedFileExtended::parseElfSymTab() { + const ELFFile obj = this->getObj(); + ArrayRef eSections = this->template getELFShdrs(); + + // Find a symbol table. + const Elf_Shdr *eSymtabSec = findSection(eSections, SHT_SYMTAB); + if (!eSymtabSec) + return; + + eFirstGlobal = eSymtabSec->sh_info; + + ArrayRef eSyms = CHECK(obj.symbols(eSymtabSec), this); + auto numESyms = uint32_t(eSyms.size()); + auto eStringTable = CHECK(obj.getStringTableForSymtab(*eSymtabSec, eSections), this); + this->allSymbols.resize(numESyms); + + for (size_t i = 0; i < numESyms; i++) { + const Elf_Sym &eSym = eSyms[i]; + StringRef name(eStringTable.data() + eSym.st_name); + auto bind = eSym.getBinding(); + auto type = eSym.getType(); + auto other = eSym.st_other; + if (eSym.isDefined()) { + auto secIdx = eSym.st_shndx; + if (LLVM_UNLIKELY(secIdx == SHN_XINDEX)) + secIdx = check( + getExtendedSymbolTableIndex(eSym, i, this->getShndxTable())); + else if (secIdx >= SHN_LORESERVE) + secIdx = 0; + const Elf_Shdr *eSec = *this->getObj().getSection(secIdx); + InputSectionBase *sec = this->sections[secIdx]; + auto val = eSym.st_value - eSec->sh_addr; + this->allSymbols[i] = + make(this, name, bind, other, type, val, eSym.st_size, sec); + } else { + this->allSymbols[i] = make(this, name, bind, other, type, + /*discardedSecIdx=*/i); + } + } +} + static ELFKind getBitcodeELFKind(const Triple &t) { if (t.isLittleEndian()) return t.isArch64Bit() ? ELF64LEKind : ELF32LEKind; @@ -1740,6 +2246,27 @@ ELFFileBase *elf::createObjFile(MemoryBufferRef mb, StringRef archiveName, return f; } +ELFFileBase *elf::createSharedFileExtended(MemoryBufferRef mb, StringRef soName) { + ELFFileBase *f; + switch (getELFKind(mb, soName)) { + case ELF32LEKind: + f = make>(mb, soName); + break; + case ELF32BEKind: + f = make>(mb, soName); + break; + case ELF64LEKind: + f = make>(mb, soName); + break; + case ELF64BEKind: + f = make>(mb, soName); + break; + default: + llvm_unreachable("getELFKind"); + } + return f; +} + template void ObjFile::parseLazy() { const ArrayRef eSyms = this->getELFSyms(); SymbolTable &symtab = *elf::symtab; @@ -1792,3 +2319,8 @@ template void SharedFile::parse(); template void SharedFile::parse(); template void SharedFile::parse(); template void SharedFile::parse(); + +template class elf::SharedFileExtended; +template class elf::SharedFileExtended; +template class elf::SharedFileExtended; +template class elf::SharedFileExtended; \ No newline at end of file diff --git a/lld/ELF/InputFiles.h b/lld/ELF/InputFiles.h index a24e664a7e16..6712c4841d65 100644 --- a/lld/ELF/InputFiles.h +++ b/lld/ELF/InputFiles.h @@ -76,18 +76,31 @@ public: // Returns sections. It is a runtime error to call this function // on files that don't have the notion of sections. ArrayRef getSections() const { - assert(fileKind == ObjKind || fileKind == BinaryKind); + if (config->adlt) + assert(fileKind == SharedKind); + else + assert(fileKind == ObjKind || fileKind == BinaryKind); return sections; } // Returns object file symbols. It is a runtime error to call this // function on files of other types. ArrayRef getSymbols() const { - assert(fileKind == BinaryKind || fileKind == ObjKind || - fileKind == BitcodeKind); + if (config->adlt) + assert(fileKind == SharedKind); + else + assert(fileKind == BinaryKind || fileKind == ObjKind || + fileKind == BitcodeKind); return symbols; } + // ADLT beg + ArrayRef getAllSymbols() const { return allSymbols; } + + SmallVector allSymbols; + // ADLT end + + // Get filename to use for linker script processing. StringRef getNameForScript() const; @@ -161,6 +174,7 @@ private: class ELFFileBase : public InputFile { public: ELFFileBase(Kind k, MemoryBufferRef m); + virtual ~ELFFileBase() {} static bool classof(const InputFile *f) { return f->isElf(); } template llvm::object::ELFFile getObj() const { @@ -169,7 +183,7 @@ public: StringRef getStringTable() const { return stringTable; } - ArrayRef getLocalSymbols() { + virtual ArrayRef getLocalSymbols() { if (symbols.empty()) return {}; return llvm::makeArrayRef(symbols).slice(1, firstGlobal - 1); @@ -224,6 +238,7 @@ public: ObjFile(MemoryBufferRef m, StringRef archiveName) : ELFFileBase(ObjKind, m) { this->archiveName = archiveName; } + virtual ~ObjFile() {} void parse(bool ignoreComdats = false); void parseLazy(); @@ -231,7 +246,7 @@ public: StringRef getShtGroupSignature(ArrayRef sections, const Elf_Shdr &sec); - Symbol &getSymbol(uint32_t symbolIndex) const { + virtual Symbol &getSymbol(uint32_t symbolIndex) const { if (symbolIndex >= this->symbols.size()) fatal(toString(this) + ": invalid symbol index"); return *this->symbols[symbolIndex]; @@ -247,6 +262,8 @@ public: llvm::Optional getDILineInfo(InputSectionBase *, uint64_t); llvm::Optional> getVariableLoc(StringRef name); + ArrayRef getShndxTable(); + // Name of source file obtained from STT_FILE symbol value, // or empty string if there is no such symbol in object file // symbol table. @@ -277,6 +294,8 @@ public: void initializeLocalSymbols(); void postParse(); + bool isPatchedSecName = true; // ADLT + private: void initializeSections(bool ignoreComdats, const llvm::object::ELFFile &obj); @@ -364,6 +383,81 @@ private: const typename ELFT::Shdr *sec); }; + +// ADLT +template +class SharedFileExtended : public ObjFile { + LLVM_ELF_IMPORT_TYPES_ELFT(ELFT) + +public: + SharedFileExtended(MemoryBufferRef mb, StringRef soName); + + static bool classof(const InputFile *f) { + return f->kind() == InputFile::SharedKind; + } + + void parseForAdlt(); + void postParseForAdlt(); + + StringRef addAdltPrefix(StringRef input); + bool addAdltPrefix(Symbol *s); + + Defined* findSectionSymbol(uint64_t offset); + + ArrayRef getLocalSymbols() override { + if (this->allSymbols.empty()) + return {}; + return llvm::makeArrayRef(this->allSymbols).slice(1, eFirstGlobal - 1); + } + + Symbol &getSymbol(uint32_t symbolIndex) const override; + Defined *findSymbolByValue(uint32_t value) const; + + Symbol &getSymbolFromElfSymTab(uint32_t symbolIndex) const { + if (symbolIndex >= this->allSymbols.size()) + fatal(toString(this) + ": invalid symbol index"); + return *this->allSymbols[symbolIndex]; + } + + void traceElfSymbol(const Elf_Sym &sym, StringRef strTable) const; + void traceElfSection(const Elf_Shdr &sec) const; + + void traceSymbol(const Symbol& sym) const; + void traceSection(const SectionBase& sec) const; + + int dynSymSecIdx = 0; + int symTabSecIdx = 0; + int symTabShndxSecIdx = 0; + int eFirstGlobal = 0; + + // This is actually a vector of Elf_Verdef pointers. + SmallVector verdefs; + // If the output file needs Elf_Verneed data structures for this file, this is + // a vector of Elf_Vernaux version identifiers that map onto the entries in + // Verdefs, otherwise it is empty. + SmallVector vernauxs; + static unsigned vernauxNum; + SmallVector dtNeeded; + StringRef soName; + StringRef simpleSoName; + // Used for --as-needed + // bool isNeeded; + // Non-weak undefined symbols which are not yet resolved when the SO is + // parsed. Only filled for `--no-allow-shlib-undefined`. + SmallVector requiredSymbols; + +private: + void parseDynamics(); // SharedFile compability + void parseElfSymTab(); // ObjectFile compability + + void resolveDuplicatesForAdlt(); + + StringRef getShStrTab(ArrayRef elfSections); + + std::vector parseVerneed(const llvm::object::ELFFile &obj, + const typename ELFT::Shdr *sec); +}; + class BinaryFile : public InputFile { public: explicit BinaryFile(MemoryBufferRef m) : InputFile(BinaryKind, m) {} @@ -374,6 +468,8 @@ public: ELFFileBase *createObjFile(MemoryBufferRef mb, StringRef archiveName = "", bool lazy = false); +ELFFileBase *createSharedFileExtended(MemoryBufferRef mb, StringRef soName = ""); + std::string replaceThinLTOSuffix(StringRef path); } // namespace elf diff --git a/lld/ELF/InputSection.cpp b/lld/ELF/InputSection.cpp index 1420d8ce4e58..e4b8a4dcc777 100644 --- a/lld/ELF/InputSection.cpp +++ b/lld/ELF/InputSection.cpp @@ -347,8 +347,9 @@ void InputSection::copyRelocations(uint8_t *buf, ArrayRef rels) { for (const RelTy &rel : rels) { RelType type = rel.getType(config->isMips64EL); - const ObjFile *file = getFile(); - Symbol &sym = file->getRelocTargetSym(rel); + const ELFFileBase *file = cast_or_null(file); + Symbol &sym = config->adlt ? getSharedFile()->getRelocTargetSym(rel) + : getFile()->getRelocTargetSym(rel); auto *p = reinterpret_cast(buf); buf += sizeof(RelTy); @@ -383,7 +384,7 @@ void InputSection::copyRelocations(uint8_t *buf, ArrayRef rels) { uint32_t secIdx = cast(sym).discardedSecIdx; Elf_Shdr_Impl sec = file->template getELFShdrs()[secIdx]; warn("relocation refers to a discarded section: " + - CHECK(file->getObj().getSectionName(sec), file) + + CHECK(file->getObj().getSectionName(sec), file) + "\n>>> referenced by " + getObjMsg(p->r_offset)); } p->setSymbolAndType(0, 0, false); @@ -863,7 +864,19 @@ void InputSection::relocateNonAlloc(uint8_t *buf, ArrayRef rels) { if (!RelTy::IsRela) addend += target.getImplicitAddend(bufLoc, type); - Symbol &sym = getFile()->getRelocTargetSym(rel); + Symbol &sym = config->adlt ? getSharedFile()->getSymbol( + rel.getSymbol(config->isMips64EL)) + : getFile()->getRelocTargetSym(rel); + if (config->adlt) { // TODO improve + switch (type) { + case R_AARCH64_RELATIVE: + case R_AARCH64_GLOB_DAT: + case R_AARCH64_JUMP_SLOT: + return; + default: + break; + } + } RelExpr expr = target.getRelExpr(type, sym, bufLoc); if (expr == R_NONE) continue; @@ -967,7 +980,9 @@ static void relocateNonAllocForRelocatable(InputSection *sec, uint8_t *buf) { template void InputSectionBase::relocate(uint8_t *buf, uint8_t *bufEnd) { - if ((flags & SHF_EXECINSTR) && LLVM_UNLIKELY(getFile()->splitStack)) + if (flags & SHF_EXECINSTR && + (config->adlt ? LLVM_UNLIKELY(getSharedFile()->splitStack) + : LLVM_UNLIKELY(getFile()->splitStack))) adjustSplitStackFunctionPrologues(buf, bufEnd); if (flags & SHF_ALLOC) { diff --git a/lld/ELF/InputSection.h b/lld/ELF/InputSection.h index d1b889750bbd..b9f4541e77a9 100644 --- a/lld/ELF/InputSection.h +++ b/lld/ELF/InputSection.h @@ -28,6 +28,7 @@ class Defined; struct Partition; class SyntheticSection; template class ObjFile; +template class SharedFileExtended; class OutputSection; extern std::vector partitions; @@ -74,6 +75,9 @@ public: uint32_t type; uint32_t link; uint32_t info; + uint64_t address = 0; // store input sec addr for ADLT + uint64_t size = 0; // store input sec size for ADLT + OutputSection *getOutputSection(); const OutputSection *getOutputSection() const { @@ -133,6 +137,11 @@ public: return cast_or_null>(file); } + template + SharedFileExtended *getSharedFile() const { + return cast_or_null>(file); + } + // Used by --optimize-bb-jumps and RISC-V linker relaxation temporarily to // indicate the number of bytes which is not counted in the size. This should // be reset to zero after uses. @@ -392,7 +401,7 @@ private: template void copyShtGroup(uint8_t *buf); }; -static_assert(sizeof(InputSection) <= 160, "InputSection is too big"); +static_assert(sizeof(InputSection) <= 180, "InputSection is too big"); inline bool isDebugSection(const InputSectionBase &sec) { return (sec.flags & llvm::ELF::SHF_ALLOC) == 0 && diff --git a/lld/ELF/MapFile.cpp b/lld/ELF/MapFile.cpp index 893511929a3a..51353287c90e 100644 --- a/lld/ELF/MapFile.cpp +++ b/lld/ELF/MapFile.cpp @@ -55,7 +55,8 @@ static void writeHeader(raw_ostream &os, uint64_t vma, uint64_t lma, // Returns a list of all symbols that we want to print out. static std::vector getSymbols() { std::vector v; - for (ELFFileBase *file : ctx->objectFiles) + auto files = config->adlt ? ctx->sharedFilesExtended : ctx->objectFiles; + for (ELFFileBase *file : files) for (Symbol *b : file->getSymbols()) if (auto *dr = dyn_cast(b)) if (!dr->isSection() && dr->section && dr->section->isLive() && @@ -224,7 +225,8 @@ static void writeMapFile(raw_fd_ostream &os) { static void writeCref(raw_fd_ostream &os) { // Collect symbols and files. MapVector> map; - for (ELFFileBase *file : ctx->objectFiles) { + auto files = config->adlt ? ctx->sharedFilesExtended : ctx->objectFiles; + for (ELFFileBase *file : files) { for (Symbol *sym : file->getSymbols()) { if (isa(sym)) map[sym].insert(file); diff --git a/lld/ELF/MarkLive.cpp b/lld/ELF/MarkLive.cpp index 12a23390e663..61be1e13287f 100644 --- a/lld/ELF/MarkLive.cpp +++ b/lld/ELF/MarkLive.cpp @@ -345,7 +345,8 @@ template void MarkLive::mark() { // to from __start_/__stop_ symbols because there will only be one set of // symbols for the whole program. template void MarkLive::moveToMain() { - for (ELFFileBase *file : ctx->objectFiles) + auto files = config->adlt ? ctx->sharedFilesExtended : ctx->objectFiles; + for (ELFFileBase *file : files) for (Symbol *s : file->getSymbols()) if (auto *d = dyn_cast(s)) if ((d->type == STT_GNU_IFUNC || d->type == STT_TLS) && d->section && diff --git a/lld/ELF/Options.td b/lld/ELF/Options.td index 4e6c20f5c7f6..46943047df31 100644 --- a/lld/ELF/Options.td +++ b/lld/ELF/Options.td @@ -375,6 +375,8 @@ defm section_start: Eq<"section-start", "Set address of section">, def shared: F<"shared">, HelpText<"Build a shared object">; +def adlt: F<"adlt">, HelpText<"Build adlt library">; + defm soname: Eq<"soname", "Set DT_SONAME">; defm sort_section: diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp index a617d55bfe00..4f9701887477 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -460,6 +460,9 @@ private: void processAux(RelExpr expr, RelType type, uint64_t offset, Symbol &sym, int64_t addend) const; template void scanOne(RelTy *&i); + + template + void handleRelativeReloc(const RelTy &rel, const Symbol& sym, uint64_t offset); }; } // namespace @@ -1304,7 +1307,8 @@ static unsigned handleTlsRelocation(RelType type, Symbol &sym, template void RelocationScanner::scanOne(RelTy *&i) { const RelTy &rel = *i; uint32_t symIndex = rel.getSymbol(config->isMips64EL); - Symbol &sym = sec.getFile()->getSymbol(symIndex); + auto file = config->adlt ? sec.getSharedFile() : sec.getFile(); + Symbol &sym = file->getSymbol(symIndex); RelType type; // Deal with MIPS oddity. @@ -1322,11 +1326,86 @@ template void RelocationScanner::scanOne(RelTy *&i) { // Error if the target symbol is undefined. Symbol index 0 may be used by // marker relocations, e.g. R_*_NONE and R_ARM_V4BX. Don't error on them. - if (sym.isUndefined() && symIndex != 0 && + if (symIndex != 0 && sym.isUndefined() && maybeReportUndefined(cast(sym), sec, offset)) return; const uint8_t *relocatedAddr = sec.rawData.begin() + offset; + if (config->adlt) { + auto file = sec.getSharedFile(); + bool isDebug = false; + + auto saveSymIfNeeded = [](const Defined& d) { + auto found = elf::symtab->find(d.getName()); + if (!found) + in.symTab->addSymbol(elf::symtab->addSymbol(d)); + }; + + auto handleRelativeReloc = [&]() { + RelType type = R_AARCH64_ABS64; + RelExpr expr = target.getRelExpr(type, sym, relocatedAddr); + uint64_t addend = computeAddend(rel, expr, sym.isLocal()); + StringRef title = "handle relative reloc: "; + if (isDebug) + lld::outs() << title << "offset: 0x" + Twine::utohexstr(offset) + << " addend: 0x" + Twine::utohexstr(addend) + "\n" ; + + // parse offset (where) + Defined *foundSymbol = file->findSymbolByValue(offset); + if (!foundSymbol) { + fatal(title + "sym not found! offset: 0x" + Twine::utohexstr(offset) + "\n"); + return; + } + if (!foundSymbol->exportDynamic) + file->addAdltPrefix(cast(foundSymbol)); + Defined &symWhere = *foundSymbol; + saveSymIfNeeded(symWhere); + + // parse addent (replacement) + foundSymbol = file->findSymbolByValue(addend); + if (!foundSymbol) { + Defined *d = file->findSectionSymbol(addend); + if (!d) + fatal(title + "sym not found! addend: 0x" + Twine::utohexstr(addend) + "\n"); + foundSymbol = d; + } + Defined &inputSym = *foundSymbol; + addend = inputSym.isSection() ? (addend - inputSym.section->address) : 0; + + if (isDebug) { + lld::outs() << "handle relative reloc: inputSym: "; + file->traceSymbol(inputSym); + } + if (!foundSymbol->exportDynamic) + file->addAdltPrefix(&inputSym); + saveSymIfNeeded(inputSym); + + // parse section from offset of relative reloc + addRelativeReloc(cast(*symWhere.section), + symWhere.value, inputSym, addend, expr, type); + }; + switch (type) { + case R_AARCH64_RELATIVE: + handleRelativeReloc(); + return; + case R_AARCH64_GLOB_DAT: { + Symbol &s = file->getSymbol(symIndex); + s.needsGot = 1; + if (s.isUndefined()) + s.needsPlt = 1; + return; + } + case R_AARCH64_JUMP_SLOT: { + Symbol &s = file->getSymbol(symIndex); + if (s.isUndefined()) + s.needsPlt = 1; + return; + } + default: + break; + } + } + RelExpr expr = target.getRelExpr(type, sym, relocatedAddr); // Ignore R_*_NONE and other marker relocations. @@ -1336,6 +1415,77 @@ template void RelocationScanner::scanOne(RelTy *&i) { // Read an addend. int64_t addend = computeAddend(rel, expr, sym.isLocal()); + if (config->adlt) { + auto file = sec.getSharedFile(); + auto traceSym = [&](StringRef debugTitle, Symbol& sym) { + lld::outs() << debugTitle; + lld::outs() << "type: " << lld::toString(type) << " sym: "; + Symbol* p = &sym; + if (!p) { + lld::outs() << "unknown!\n"; + return; + } + file->traceSymbol(sym); + if (!sym.isDefined()) + return; + lld::outs() << "sec: "; + file->traceSection(*(cast(sym).section)); + }; + + bool isDebug = false; + auto strType = lld::toString(type); + auto debugTitle = "[RelocationScanner::handle relocs]: " + strType + ": "; + if (isDebug) { + traceSym(debugTitle, sym); + lld::outs() << " offset before: 0x" << Twine::utohexstr(offset) << '\n'; + } + if (offset + sec.address) + offset -= sec.address; + + Symbol *s = &sym; + if (sec.name != "") + s = &file->getSymbolFromElfSymTab(symIndex); + + s = sym.getName().size() ? elf::symtab->find(sym.getName()) : &sym; + + if (!s) // if symbol was not found in elf::symtab + s = &sym; + auto pushReloc = [&]() { + sec.relocations.push_back({expr, type, offset, addend, s}); + }; + + switch (type) { + // abs relocs + case R_AARCH64_ABS32: + case R_AARCH64_ABS64: + case R_AARCH64_ADD_ABS_LO12_NC: + case R_AARCH64_ADR_PREL_PG_HI21: + case R_AARCH64_LDST8_ABS_LO12_NC: + case R_AARCH64_LDST64_ABS_LO12_NC: + pushReloc(); + return; + // plt relocs + case R_AARCH64_CALL26: + case R_AARCH64_JUMP26: { + if (s && s->isDefined()) + expr = R_PC; // prev: R_PLT_PC + pushReloc(); + return; + } + + // got relocs + case R_AARCH64_ADR_GOT_PAGE: + case R_AARCH64_LD64_GOT_LO12_NC: + pushReloc(); + return; + default: + fatal("Unhandled reloc: " + strType); + break; + } + if (isDebug) + lld::outs() << " offset after: 0x" << Twine::utohexstr(offset) << "\n\n"; + } + if (config->emachine == EM_PPC64) { // We can separate the small code model relocations into 2 categories: // 1) Those that access the compiler generated .toc sections. @@ -1640,7 +1790,8 @@ void elf::postScanRelocations() { return; if (!sym.needsDynReloc()) return; - sym.allocateAux(); + if (!sym.allocateAux()) + return; if (sym.needsGot) addGotEntry(sym); @@ -1732,7 +1883,8 @@ void elf::postScanRelocations() { // Local symbols may need the aforementioned non-preemptible ifunc and GOT // handling. They don't need regular PLT. - for (ELFFileBase *file : ctx->objectFiles) + auto files = config->adlt ? ctx->sharedFilesExtended : ctx->objectFiles; + for (ELFFileBase *file : files) for (Symbol *sym : file->getLocalSymbols()) fn(*sym); } @@ -2180,7 +2332,8 @@ bool ThunkCreator::createThunks(uint32_t pass, // original target so another Thunk can be generated. if (pass > 0 && normalizeExistingThunk(rel, src)) continue; - + if (!rel.sym) + continue; // ADLT fix if (!target->needsThunk(rel.expr, rel.type, isec->file, src, *rel.sym, rel.addend)) continue; diff --git a/lld/ELF/Symbols.h b/lld/ELF/Symbols.h index 657c19a00ba3..d65ebd486dd2 100644 --- a/lld/ELF/Symbols.h +++ b/lld/ELF/Symbols.h @@ -307,10 +307,12 @@ public: return needsCopy || needsGot || needsPlt || needsTlsDesc || needsTlsGd || needsTlsGdToIe || needsGotDtprel || needsTlsIe; } - void allocateAux() { - assert(auxIdx == uint32_t(-1)); + bool allocateAux() { + if (auxIdx != uint32_t(-1)) + return false; auxIdx = symAux.size(); symAux.emplace_back(); + return true; } bool isSection() const { return type == llvm::ELF::STT_SECTION; } diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp index f298b83bda0e..8a7208698c6d 100644 --- a/lld/ELF/SyntheticSections.cpp +++ b/lld/ELF/SyntheticSections.cpp @@ -1397,9 +1397,23 @@ DynamicSection::computeContents() { addInt(config->enableNewDtags ? DT_RUNPATH : DT_RPATH, part.dynStrTab->addString(config->rpath)); - for (SharedFile *file : ctx->sharedFiles) - if (file->isNeeded) - addInt(DT_NEEDED, part.dynStrTab->addString(file->soName)); + if (config->adlt) { + StringRef prevNeeded; + for (InputFile *file : ctx->sharedFilesExtended) { + auto *f = cast>(file); + for (size_t i = 0; i < f->dtNeeded.size(); i++) { + StringRef needed = f->dtNeeded[i]; + if (prevNeeded == needed) + continue; + addInt(DT_NEEDED, part.dynStrTab->addString(needed)); + prevNeeded = needed; + } + } + addInt(DT_SONAME, part.dynStrTab->addString(config->outputFile)); + } else + for (SharedFile *file : ctx->sharedFiles) + if (file->isNeeded) + addInt(DT_NEEDED, part.dynStrTab->addString(file->soName)); if (isMain) { if (!config->soName.empty()) @@ -1536,17 +1550,40 @@ DynamicSection::computeContents() { addInSec(DT_HASH, *part.hashTab); if (isMain) { - if (Out::preinitArray) { - addInt(DT_PREINIT_ARRAY, Out::preinitArray->addr); - addInt(DT_PREINIT_ARRAYSZ, Out::preinitArray->size); - } - if (Out::initArray) { - addInt(DT_INIT_ARRAY, Out::initArray->addr); - addInt(DT_INIT_ARRAYSZ, Out::initArray->size); - } - if (Out::finiArray) { - addInt(DT_FINI_ARRAY, Out::finiArray->addr); - addInt(DT_FINI_ARRAYSZ, Out::finiArray->size); + if (config->adlt) { + auto 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) + return &osd->osec; + return (OutputSection *)nullptr; + }; + for (InputFile *file : ctx->sharedFilesExtended) { + auto *f = cast>(file); + auto initArray = findSection(f->addAdltPrefix(".init_array")); + auto finiArray = findSection(f->addAdltPrefix(".fini_array")); + if (initArray) { + addInt(DT_INIT_ARRAY, initArray->addr); + addInt(DT_INIT_ARRAYSZ, initArray->size); + } + if (finiArray) { + addInt(DT_FINI_ARRAY, finiArray->addr); + addInt(DT_FINI_ARRAYSZ, finiArray->size); + } + } + } else { + if (Out::preinitArray) { + addInt(DT_PREINIT_ARRAY, Out::preinitArray->addr); + addInt(DT_PREINIT_ARRAYSZ, Out::preinitArray->size); + } + if (Out::initArray) { + addInt(DT_INIT_ARRAY, Out::initArray->addr); + addInt(DT_INIT_ARRAYSZ, Out::initArray->size); + } + if (Out::finiArray) { + addInt(DT_FINI_ARRAY, Out::finiArray->addr); + addInt(DT_FINI_ARRAYSZ, Out::finiArray->size); + } } if (Symbol *b = symtab->find(config->init)) @@ -3410,7 +3447,8 @@ template void elf::splitSections() { llvm::TimeTraceScope timeScope("Split sections"); // splitIntoPieces needs to be called on each MergeInputSection // before calling finalizeContents(). - parallelForEach(ctx->objectFiles, [](ELFFileBase *file) { + auto files = config->adlt ? ctx->sharedFilesExtended : ctx->objectFiles; + parallelForEach(files, [](ELFFileBase *file) { for (InputSectionBase *sec : file->getSections()) { if (!sec) continue; diff --git a/lld/ELF/Target.h b/lld/ELF/Target.h index 27ef93e00b8b..8e1a7d2665aa 100644 --- a/lld/ELF/Target.h +++ b/lld/ELF/Target.h @@ -88,6 +88,9 @@ public: virtual bool inBranchRange(RelType type, uint64_t src, uint64_t dst) const; + virtual void deRelocate(uint8_t *loc, const Relocation &rel, + uint64_t *val) const {}; // ADLT + virtual void relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const = 0; void relocateNoSym(uint8_t *loc, RelType type, uint64_t val) const { diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index e27e656d43c7..4f4658d7cc24 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -628,7 +628,8 @@ template static void markUsedLocalSymbols() { // See MarkLive::resolveReloc(). if (config->gcSections) return; - for (ELFFileBase *file : ctx->objectFiles) { + auto files = config->adlt ? ctx->sharedFilesExtended : ctx->objectFiles; + for (ELFFileBase *file : files) { ObjFile *f = cast>(file); for (InputSectionBase *s : f->getSections()) { InputSection *isec = dyn_cast_or_null(s); @@ -699,7 +700,8 @@ template void Writer::copyLocalSymbols() { llvm::TimeTraceScope timeScope("Add local symbols"); if (config->copyRelocs && config->discard != DiscardPolicy::None) markUsedLocalSymbols(); - for (ELFFileBase *file : ctx->objectFiles) { + auto files = /*config->adlt ? ctx->sharedFilesExtended :*/ ctx->objectFiles; + for (ELFFileBase *file : files) { for (Symbol *b : file->getLocalSymbols()) { assert(b->isLocal() && "should have been caught in initializeSymbols()"); auto *dr = dyn_cast(b); @@ -1303,7 +1305,8 @@ static DenseMap buildSectionOrder() { for (Symbol *sym : symtab->symbols()) addSym(*sym); - for (ELFFileBase *file : ctx->objectFiles) + auto files = config->adlt ? ctx->sharedFilesExtended : ctx->objectFiles; + for (ELFFileBase *file : files) for (Symbol *sym : file->getLocalSymbols()) addSym(*sym); @@ -1719,7 +1722,8 @@ template void Writer::finalizeAddressDependentContent() { // block sections, input sections can shrink when the jump instructions at // the end of the section are relaxed. static void fixSymbolsAfterShrinking() { - for (InputFile *File : ctx->objectFiles) { + auto files = config->adlt ? ctx->sharedFilesExtended : ctx->objectFiles; + for (InputFile *File : files) { parallelForEach(File->getSymbols(), [&](Symbol *Sym) { auto *def = dyn_cast(Sym); if (!def) @@ -1937,9 +1941,19 @@ template void Writer::finalizeSections() { // determine if it needs special treatment, such as creating GOT, PLT, // copy relocations, etc. Note that relocations for non-alloc sections are // directly processed by InputSection::relocateNonAlloc. - for (InputSectionBase *sec : inputSections) - if (sec->isLive() && isa(sec) && (sec->flags & SHF_ALLOC)) - scanRelocations(*sec); + if (config->adlt) + for (InputFile *file : ctx->sharedFilesExtended) + for (InputSectionBase *sec : file->getSections()) { + bool isExclusive = sec && (sec->type == SHT_NULL || + sec->name.startswith(".got.plt") || + !sec->name.startswith(".debug_")); + if (isExclusive) + scanRelocations(*sec); + } + if (!config->adlt) + for (InputSectionBase *sec : inputSections) + if (sec->isLive() && isa(sec) && (sec->flags & SHF_ALLOC)) + scanRelocations(*sec); for (Partition &part : partitions) { for (EhInputSection *sec : part.ehFrame->sections) scanRelocations(*sec); @@ -2474,6 +2488,9 @@ template void Writer::fixSectionAlignments() { OutputSection *cmd = p->firstSec; if (!cmd) return; + if (config->adlt &&cmd->name == ".text") { + lld::outs() << "Writer::fixSectionAlignments()\n"; + } cmd->alignExpr = [align = cmd->alignment]() { return align; }; if (!cmd->addrExpr) { // Prefer advancing to align(dot, maxPageSize) + dot%maxPageSize to avoid @@ -2611,6 +2628,11 @@ template void Writer::assignFileOffsets() { if (config->zSeparate != SeparateSegmentKind::None && lastRX && lastRX->lastSec == sec) off = alignToPowerOf2(off, config->maxPageSize); + bool debug = false; + if (debug) + lld::outs() << "assignFileOffsets() Sec: " << sec->name + << " Off: " << std::to_string(off) + << " Offset: " << std::to_string(sec->offset) << '\n'; } for (OutputSection *osec : outputSections) if (!(osec->flags & SHF_ALLOC)) { -- Gitee From c0e0f81177806bc4555dc22019fe28a9f5e59943 Mon Sep 17 00:00:00 2001 From: Khomutov Nikita Date: Wed, 20 Dec 2023 15:11:41 +0300 Subject: [PATCH 02/77] force debug for active development Signed-off-by: Khomutov Nikita --- llvm-build/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/llvm-build/Makefile b/llvm-build/Makefile index 22e78063318b..058932158f1c 100644 --- a/llvm-build/Makefile +++ b/llvm-build/Makefile @@ -43,7 +43,7 @@ MULTILIB = $(patsubst $(dir $(shell $(filter-out $(ARCH_CFLAGS),$(CC)) -print-li endif MUSLBUILDDIR = build_$(or $(TARGET),$(ARCH))$(subst /,_,$(MULTILIB:%/=%)) HIDE = @ -BUILD_DEBUG = false +BUILD_DEBUG = true SED_ARGS = -e '/install-libs:/s/if/and/g' TOPDIR = $(shell pwd)/../../../.. -- Gitee From 3661e0d53bbf59ad5054661ca1b9c778697acb0c Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Wed, 27 Dec 2023 13:39:14 -0500 Subject: [PATCH 03/77] Fix crash case. More relocs support. Minor change. Change-Id: I2e869aeaab1cc5d5f8d6cdb4a5f7d137d8647046 Signed-off-by: Anton Volkov --- lld/ELF/InputFiles.cpp | 2 +- lld/ELF/Relocations.cpp | 24 +++++++++++++++++++++--- lld/ELF/Writer.cpp | 5 +---- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp index 33df282ec9e2..ce20f4445e4b 100644 --- a/lld/ELF/InputFiles.cpp +++ b/lld/ELF/InputFiles.cpp @@ -1070,7 +1070,7 @@ void ObjFile::initializeSymbols(const object::ELFFile &obj) { uint8_t stOther = eSym.st_other; uint8_t type = eSym.getType(); uint64_t value = eSym.st_value; - if (config->adlt) { + if (config->adlt && secIdx != SHN_ABS) { const Elf_Shdr *eSec = *obj.getSection(secIdx); value -= eSec->sh_addr; } diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp index 4f9701887477..fd93ec64dff6 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -1443,13 +1443,22 @@ template void RelocationScanner::scanOne(RelTy *&i) { offset -= sec.address; Symbol *s = &sym; - if (sec.name != "") - s = &file->getSymbolFromElfSymTab(symIndex); + 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; s = sym.getName().size() ? elf::symtab->find(sym.getName()) : &sym; - if (!s) // if symbol was not found in elf::symtab s = &sym; + + if (s->isUndefined()) { + if (expr == R_PLT || expr == R_PLT_PC) + s->needsPlt = 1; + else if (expr == R_GOT || expr == R_GOT_PC) + s->needsGot = 1; + } + auto pushReloc = [&]() { sec.relocations.push_back({expr, type, offset, addend, s}); }; @@ -1461,7 +1470,11 @@ template void RelocationScanner::scanOne(RelTy *&i) { case R_AARCH64_ADD_ABS_LO12_NC: case R_AARCH64_ADR_PREL_PG_HI21: case R_AARCH64_LDST8_ABS_LO12_NC: + case R_AARCH64_LDST32_ABS_LO12_NC: case R_AARCH64_LDST64_ABS_LO12_NC: + case R_AARCH64_LDST128_ABS_LO12_NC: + case R_AARCH64_PREL32: + case R_AARCH64_PREL64: pushReloc(); return; // plt relocs @@ -1698,6 +1711,11 @@ void RelocationScanner::scan(ArrayRef rels) { template void elf::scanRelocations(InputSectionBase &s) { RelocationScanner scanner(s); + if (config->adlt) { + bool isDebug = false; + if (isDebug) + lld::outs() << s.file->getName().str() + ": " + s.name.str() + '\n'; + } const RelsOrRelas rels = s.template relsOrRelas(); if (rels.areRelocsRel()) scanner.template scan(rels.rels); diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index 4f4658d7cc24..a3db65a4732c 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -1944,10 +1944,7 @@ template void Writer::finalizeSections() { if (config->adlt) for (InputFile *file : ctx->sharedFilesExtended) for (InputSectionBase *sec : file->getSections()) { - bool isExclusive = sec && (sec->type == SHT_NULL || - sec->name.startswith(".got.plt") || - !sec->name.startswith(".debug_")); - if (isExclusive) + if (sec && sec->isLive()) scanRelocations(*sec); } if (!config->adlt) -- Gitee From e663e27f6279b87162b6e354dcdd6f08bba9c558 Mon Sep 17 00:00:00 2001 From: peshkovivan Date: Wed, 27 Dec 2023 17:31:42 +0300 Subject: [PATCH 04/77] Add option to enable debug build for adlt Signed-off-by: peshkovivan --- llvm-build/Makefile | 2 +- llvm-build/build.py | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/llvm-build/Makefile b/llvm-build/Makefile index 058932158f1c..22e78063318b 100644 --- a/llvm-build/Makefile +++ b/llvm-build/Makefile @@ -43,7 +43,7 @@ MULTILIB = $(patsubst $(dir $(shell $(filter-out $(ARCH_CFLAGS),$(CC)) -print-li endif MUSLBUILDDIR = build_$(or $(TARGET),$(ARCH))$(subst /,_,$(MULTILIB:%/=%)) HIDE = @ -BUILD_DEBUG = true +BUILD_DEBUG = false SED_ARGS = -e '/install-libs:/s/if/and/g' TOPDIR = $(shell pwd)/../../../.. diff --git a/llvm-build/build.py b/llvm-build/build.py index 11f19925a9dd..b04b7218851a 100755 --- a/llvm-build/build.py +++ b/llvm-build/build.py @@ -288,6 +288,12 @@ class BuildConfig(): action='store_true', default=False, help='Enable lldb performance monitoring') + + parser.add_argument( + '--adlt-debug-build', + action='store_true', + default=False, + help='Build adlt with debug flags') compression_formats = ['bz2', 'gz'] -- Gitee From 631514d40e06ca645fbe08ba7f9e9d3b6338948e Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Mon, 15 Jan 2024 03:53:34 -0500 Subject: [PATCH 05/77] Fix not found sym Change-Id: Ibd2885c74ca48ec62ef0354782d72ee79c1ee014 Signed-off-by: Anton Volkov --- lld/ELF/InputFiles.cpp | 15 +++++++++++++-- lld/ELF/InputFiles.h | 3 ++- lld/ELF/Relocations.cpp | 31 +++++++++++++------------------ 3 files changed, 28 insertions(+), 21 deletions(-) diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp index ce20f4445e4b..59fb79b585a0 100644 --- a/lld/ELF/InputFiles.cpp +++ b/lld/ELF/InputFiles.cpp @@ -1693,6 +1693,17 @@ Defined* SharedFileExtended::findSectionSymbol(uint64_t offset) { return nullptr; } +template +Defined* SharedFileExtended::findDefinedSymbol(uint64_t offset) { + Defined *foundSymbol = findSymbolByValue(offset); + if (foundSymbol) + return foundSymbol; + Defined *d = findSectionSymbol(offset); + if (d) + return d; + return static_cast(nullptr); +} + template StringRef SharedFileExtended::getShStrTab(ArrayRef elfSections) { return CHECK(this->getObj().getSectionStringTable(elfSections), this); @@ -1724,8 +1735,8 @@ void SharedFileExtended::traceElfSection(const Elf_Shdr &sec) const { } template -void SharedFileExtended::traceSymbol(const Symbol& sym) const { - lld::outs() << "File: " << soName << " symName: " << sym.getName(); +void SharedFileExtended::traceSymbol(const Symbol& sym, StringRef title) const { + lld::outs() << "File: " << soName << ": " + title << " symName: " << sym.getName(); if (!sym.isDefined()) { lld::outs() << '\n'; return; diff --git a/lld/ELF/InputFiles.h b/lld/ELF/InputFiles.h index 6712c4841d65..58be116cf5d6 100644 --- a/lld/ELF/InputFiles.h +++ b/lld/ELF/InputFiles.h @@ -403,6 +403,7 @@ public: bool addAdltPrefix(Symbol *s); Defined* findSectionSymbol(uint64_t offset); + Defined* findDefinedSymbol(uint64_t offset); ArrayRef getLocalSymbols() override { if (this->allSymbols.empty()) @@ -422,7 +423,7 @@ public: void traceElfSymbol(const Elf_Sym &sym, StringRef strTable) const; void traceElfSection(const Elf_Shdr &sec) const; - void traceSymbol(const Symbol& sym) const; + void traceSymbol(const Symbol& sym, StringRef title = "") const; void traceSection(const SectionBase& sec) const; int dynSymSecIdx = 0; diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp index fd93ec64dff6..69017241e9f5 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -1335,7 +1335,7 @@ template void RelocationScanner::scanOne(RelTy *&i) { auto file = sec.getSharedFile(); bool isDebug = false; - auto saveSymIfNeeded = [](const Defined& d) { + auto saveSymIfNeeded = [&](const Defined& d) { auto found = elf::symtab->find(d.getName()); if (!found) in.symTab->addSymbol(elf::symtab->addSymbol(d)); @@ -1351,31 +1351,24 @@ template void RelocationScanner::scanOne(RelTy *&i) { << " addend: 0x" + Twine::utohexstr(addend) + "\n" ; // parse offset (where) - Defined *foundSymbol = file->findSymbolByValue(offset); - if (!foundSymbol) { + Defined *foundSymbol = file->findDefinedSymbol(offset); + if (!foundSymbol) fatal(title + "sym not found! offset: 0x" + Twine::utohexstr(offset) + "\n"); - return; - } - if (!foundSymbol->exportDynamic) + if (!foundSymbol->exportDynamic && !foundSymbol->isSection()) file->addAdltPrefix(cast(foundSymbol)); Defined &symWhere = *foundSymbol; saveSymIfNeeded(symWhere); + offset = symWhere.isSection() ? (offset - symWhere.section->address) : 0; // parse addent (replacement) - foundSymbol = file->findSymbolByValue(addend); - if (!foundSymbol) { - Defined *d = file->findSectionSymbol(addend); - if (!d) - fatal(title + "sym not found! addend: 0x" + Twine::utohexstr(addend) + "\n"); - foundSymbol = d; - } + foundSymbol = file->findDefinedSymbol(addend); + if (!foundSymbol) + fatal(title + "sym not found! addend: 0x" + Twine::utohexstr(addend) + "\n"); Defined &inputSym = *foundSymbol; addend = inputSym.isSection() ? (addend - inputSym.section->address) : 0; - if (isDebug) { - lld::outs() << "handle relative reloc: inputSym: "; - file->traceSymbol(inputSym); - } + if (isDebug) + file->traceSymbol(inputSym, "handle relative reloc: inputSym: "); if (!foundSymbol->exportDynamic) file->addAdltPrefix(&inputSym); saveSymIfNeeded(inputSym); @@ -1463,6 +1456,8 @@ template void RelocationScanner::scanOne(RelTy *&i) { sec.relocations.push_back({expr, type, offset, addend, s}); }; + bool isSymDefined = s && s->isDefined(); + switch (type) { // abs relocs case R_AARCH64_ABS32: @@ -1480,7 +1475,7 @@ template void RelocationScanner::scanOne(RelTy *&i) { // plt relocs case R_AARCH64_CALL26: case R_AARCH64_JUMP26: { - if (s && s->isDefined()) + if (isSymDefined) expr = R_PC; // prev: R_PLT_PC pushReloc(); return; -- Gitee From e81814694bfcb102ccb05cedf90afdecb4ed80b0 Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Thu, 18 Jan 2024 04:27:29 -0500 Subject: [PATCH 06/77] Better search symbols for relocs. Fix preemtible sym. Fix ADR_GOT_PAGE out range. Change-Id: Ia4c728f2c86fada9c41fd7e4285a3cfacdb358f3 Signed-off-by: Anton Volkov --- lld/ELF/InputFiles.cpp | 106 +++++++++++++++++----------------------- lld/ELF/InputFiles.h | 7 +-- lld/ELF/Relocations.cpp | 54 +++++++++++--------- 3 files changed, 82 insertions(+), 85 deletions(-) diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp index 59fb79b585a0..7866b5a26c6c 100644 --- a/lld/ELF/InputFiles.cpp +++ b/lld/ELF/InputFiles.cpp @@ -1209,6 +1209,14 @@ template void ObjFile::postParse() { if (sym.binding == STB_WEAK || binding == STB_WEAK) continue; std::lock_guard lock(mu); + if (config->adlt) { + auto *f = cast>(this); + bool isDebug = false; + if (isDebug) { + lld::outs() << "Put to duplicates for: " << archiveName << "\n"; + f->traceSymbol(sym); + } + } ctx->duplicates.push_back({&sym, this, sec, eSym.st_value}); } } @@ -1674,7 +1682,8 @@ bool SharedFileExtended::addAdltPrefix(Symbol *s) { } template -Defined* SharedFileExtended::findSectionSymbol(uint64_t offset) { +Defined *SharedFileExtended::findSectionSymbol(uint64_t offset) const { + bool isDebug = false; auto predRange = [=](Symbol *sym) { if (!sym || sym->isUndefined() || !sym->isSection()) return false; @@ -1687,21 +1696,43 @@ Defined* SharedFileExtended::findSectionSymbol(uint64_t offset) { auto i = this->allSymbols.begin(); auto e = this->allSymbols.end(); auto ret = std::find_if(i, e, predRange); - if (ret != e) // item was found - return cast(*ret); + if (ret == e) // item was not found + return nullptr; - return nullptr; + auto d = cast(*ret); + if (isDebug) + traceSymbol(*d, "found section sym: "); + + return d; } template -Defined* SharedFileExtended::findDefinedSymbol(uint64_t offset) { - Defined *foundSymbol = findSymbolByValue(offset); - if (foundSymbol) - return foundSymbol; - Defined *d = findSectionSymbol(offset); - if (d) - return d; - return static_cast(nullptr); +Defined *SharedFileExtended::findDefinedSymbol( + uint64_t offset, llvm::function_ref extraCond) const { + bool isDebug = false; + SmallVector matches; + for (Symbol *sym : this->allSymbols) { + if (!sym || sym->isUndefined()) + continue; + + Defined *d = cast(sym); + if (d->file != this) + continue; + + bool goodVal = d->section->address + d->value == offset; + bool goodType = d->type == STT_FUNC || d->type == STT_OBJECT; + if (!(goodVal && goodType && extraCond(d))) + continue; + + if (isDebug) + traceSymbol(*sym, "found sym: "); + matches.push_back(d); + } + + if (!matches.empty()) + return matches[0]; // TODO: resolve multiple variants + + return findSectionSymbol(offset); } template @@ -1744,8 +1775,8 @@ void SharedFileExtended::traceSymbol(const Symbol& sym, StringRef title) c auto &d = cast(sym); lld::outs() << " val: 0x" << Twine::utohexstr(d.value) << " sec of sym: " << (d.section ? d.section->name : "unknown!") - << " sym type: 0x" << Twine::utohexstr(sym.type) - << " sym binding: 0x" << Twine::utohexstr(sym.binding) + << " sym type: 0x" << Twine::utohexstr(d.type) + << " sym binding: 0x" << Twine::utohexstr(d.binding) << '\n'; } @@ -1764,51 +1795,6 @@ Symbol& SharedFileExtended::getSymbol(uint32_t symbolIndex) const { return *this->symbols[symbolIndex]; } -template -Defined *SharedFileExtended::findSymbolByValue(uint32_t value) const { - bool isDebug = false; - auto predValues = [=](Symbol *sym) { - if (!sym || sym->isUndefined()) - return false; - const Defined *d = cast(sym); - bool goodVal = d->section->address + d->value == value; - bool goodFile = d->file == this; - bool isGood = goodVal && goodFile; - if (isDebug && isGood) - lld::outs() << "good\n"; - return isGood; - }; - - // filter by value for .dynsym, else for .symtab - SmallVector matches; - auto i = this->symbols.begin(); - auto e = this->symbols.end(); - std::copy_if(i, e, std::back_inserter(matches), predValues); - - if (matches.empty()) { - auto i = this->allSymbols.begin(); - auto e = this->allSymbols.end(); - std::copy_if(i, e, std::back_inserter(matches), predValues); - } - - if (matches.empty()) { - // warn("Symbol not found! Value: 0x" + Twine::utohexstr(value)); - return nullptr; - } - - // filter by type else return first item - i = matches.begin(); - e = matches.end(); - auto predTypes = [=](Symbol *sym) { - return sym->type == STT_FUNC || sym->type == STT_OBJECT; - }; - auto ret = std::find_if(i, e, predTypes); - if (ret != e) // item was found - return cast(*ret); - - // todo fix it - return cast(*i); -} template void SharedFileExtended::parseDynamics() { const ELFFile obj = this->getObj(); @@ -2334,4 +2320,4 @@ template void SharedFile::parse(); template class elf::SharedFileExtended; template class elf::SharedFileExtended; template class elf::SharedFileExtended; -template class elf::SharedFileExtended; \ No newline at end of file +template class elf::SharedFileExtended; diff --git a/lld/ELF/InputFiles.h b/lld/ELF/InputFiles.h index 58be116cf5d6..5207ef34344c 100644 --- a/lld/ELF/InputFiles.h +++ b/lld/ELF/InputFiles.h @@ -402,8 +402,10 @@ public: StringRef addAdltPrefix(StringRef input); bool addAdltPrefix(Symbol *s); - Defined* findSectionSymbol(uint64_t offset); - Defined* findDefinedSymbol(uint64_t offset); + Defined *findSectionSymbol(uint64_t offset) const; + Defined *findDefinedSymbol( + uint64_t offset, llvm::function_ref extraCond = + [](Defined *) { return true; }) const; ArrayRef getLocalSymbols() override { if (this->allSymbols.empty()) @@ -412,7 +414,6 @@ public: } Symbol &getSymbol(uint32_t symbolIndex) const override; - Defined *findSymbolByValue(uint32_t value) const; Symbol &getSymbolFromElfSymTab(uint32_t symbolIndex) const { if (symbolIndex >= this->allSymbols.size()) diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp index 69017241e9f5..c1c95d8d4ca6 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -1348,34 +1348,34 @@ template void RelocationScanner::scanOne(RelTy *&i) { StringRef title = "handle relative reloc: "; if (isDebug) lld::outs() << title << "offset: 0x" + Twine::utohexstr(offset) - << " addend: 0x" + Twine::utohexstr(addend) + "\n" ; + << " addend: 0x" + Twine::utohexstr(addend) + "\n"; // parse offset (where) - Defined *foundSymbol = file->findDefinedSymbol(offset); - if (!foundSymbol) - fatal(title + "sym not found! offset: 0x" + Twine::utohexstr(offset) + "\n"); - if (!foundSymbol->exportDynamic && !foundSymbol->isSection()) - file->addAdltPrefix(cast(foundSymbol)); - Defined &symWhere = *foundSymbol; - saveSymIfNeeded(symWhere); - offset = symWhere.isSection() ? (offset - symWhere.section->address) : 0; + const Defined *symWhere = file->findDefinedSymbol(offset); + if (!symWhere) + fatal(title + "symWhere not found! offset: 0x" + + Twine::utohexstr(offset) + "\n"); + auto iSec = cast(symWhere->section); + offset = + symWhere->isSection() ? (offset - symWhere->section->address) : 0; + saveSymIfNeeded(*symWhere); // parse addent (replacement) - foundSymbol = file->findDefinedSymbol(addend); - if (!foundSymbol) - fatal(title + "sym not found! addend: 0x" + Twine::utohexstr(addend) + "\n"); - Defined &inputSym = *foundSymbol; - addend = inputSym.isSection() ? (addend - inputSym.section->address) : 0; + Defined *inputSym = file->findDefinedSymbol( + addend, [](Defined *d) { return !d->isPreemptible; }); + + if (!inputSym) + fatal(title + "inputSym not found! addend: 0x" + + Twine::utohexstr(addend) + "\n"); + addend = + inputSym->isSection() ? (addend - inputSym->section->address) : 0; if (isDebug) - file->traceSymbol(inputSym, "handle relative reloc: inputSym: "); - if (!foundSymbol->exportDynamic) - file->addAdltPrefix(&inputSym); - saveSymIfNeeded(inputSym); - - // parse section from offset of relative reloc - addRelativeReloc(cast(*symWhere.section), - symWhere.value, inputSym, addend, expr, type); + file->traceSymbol(*inputSym, "handle relative reloc: inputSym: "); + saveSymIfNeeded(*inputSym); + + // finally + addRelativeReloc(*iSec, symWhere->value, *inputSym, addend, expr, type); }; switch (type) { case R_AARCH64_RELATIVE: @@ -1465,6 +1465,7 @@ template void RelocationScanner::scanOne(RelTy *&i) { case R_AARCH64_ADD_ABS_LO12_NC: case R_AARCH64_ADR_PREL_PG_HI21: case R_AARCH64_LDST8_ABS_LO12_NC: + case R_AARCH64_LDST16_ABS_LO12_NC: case R_AARCH64_LDST32_ABS_LO12_NC: case R_AARCH64_LDST64_ABS_LO12_NC: case R_AARCH64_LDST128_ABS_LO12_NC: @@ -1483,6 +1484,15 @@ template void RelocationScanner::scanOne(RelTy *&i) { // got relocs case R_AARCH64_ADR_GOT_PAGE: + if (isSymDefined && !s->needsGot) { + if (isDebug) { + lld::outs() << "[ADLT] R_AARCH64_ADR_GOT_PAGE: sym not in GOT! "; + file->traceSymbol(*s); + } + expr = R_PC; // prev: R_AARCH64_GOT_PAGE_PC || R_AARCH64_GOT_PAGE || + // R_GOT || R_GOT_PC + } + LLVM_FALLTHROUGH; case R_AARCH64_LD64_GOT_LO12_NC: pushReloc(); return; -- Gitee From 341051afcca5558c2e7369dc82f5b5349823dda0 Mon Sep 17 00:00:00 2001 From: Olshevsky Vladimir Date: Fri, 19 Jan 2024 15:21:30 +0300 Subject: [PATCH 07/77] [ADLT] fixed assertion errors in ld.lld ld.lld aborts, if toolchain was built with--enable-assertions. The reason is in unchecked error state for getSection() return value. The given commit adds such a check. Signed-off-by: Olshevsky Vladimir Change-Id: Ic1a40edf3fda3b2fb7101a5565b1af3d6d05dc13 --- lld/ELF/InputFiles.cpp | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp index 7866b5a26c6c..07467f1811cd 100644 --- a/lld/ELF/InputFiles.cpp +++ b/lld/ELF/InputFiles.cpp @@ -1071,7 +1071,11 @@ void ObjFile::initializeSymbols(const object::ELFFile &obj) { uint8_t type = eSym.getType(); uint64_t value = eSym.st_value; if (config->adlt && secIdx != SHN_ABS) { - const Elf_Shdr *eSec = *obj.getSection(secIdx); + auto p = obj.getSection(secIdx); + if (p.takeError()) { + fatal("getSection failed: " + llvm::toString(p.takeError())); + } + const Elf_Shdr *eSec = *p; value -= eSec->sh_addr; } uint64_t size = eSym.st_size; @@ -1142,7 +1146,11 @@ template void ObjFile::initializeLocalSymbols() { new (symbols[i]) Undefined(this, name, STB_LOCAL, eSym.st_other, type, /*discardedSecIdx=*/secIdx); else { - const Elf_Shdr *eSec = *this->getObj().getSection(secIdx); + auto p = this->getObj().getSection(secIdx); + if (p.takeError()) { + fatal("getSection failed: " + llvm::toString(p.takeError())); + } + const Elf_Shdr *eSec = *p; auto value = config->adlt ? eSym.st_value - eSec->sh_addr : eSym.st_value; new (symbols[i]) Defined(this, name, STB_LOCAL, eSym.st_other, type, value, eSym.st_size, sec); @@ -1634,7 +1642,11 @@ template void SharedFileExtended::parseForAdlt() { StringRef shstrtab = getShStrTab(objSections); lld::outs() << "Parse symbols from .symtab:\n"; - const Elf_Shdr *elfSymTab = *obj.getSection(symTabSecIdx); + auto p = obj.getSection(symTabSecIdx); + if (p.takeError()) { + fatal("getSection failed: " + llvm::toString(p.takeError())); + } + const Elf_Shdr *elfSymTab = *p; StringRef strTable = *obj.getStringTableForSymtab(*elfSymTab); auto eSyms = *obj.symbols(elfSymTab); @@ -1988,7 +2000,11 @@ template void SharedFileExtended::parseElfSymTab() { getExtendedSymbolTableIndex(eSym, i, this->getShndxTable())); else if (secIdx >= SHN_LORESERVE) secIdx = 0; - const Elf_Shdr *eSec = *this->getObj().getSection(secIdx); + auto p = obj.getSection(secIdx); + if (p.takeError()) { + fatal("getSection failed: " + llvm::toString(p.takeError())); + } + const Elf_Shdr *eSec = *p; InputSectionBase *sec = this->sections[secIdx]; auto val = eSym.st_value - eSec->sh_addr; this->allSymbols[i] = -- Gitee From 4e56c7b8ca868b1c96ca84fde2143a38d1e61ab2 Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Thu, 25 Jan 2024 09:40:52 -0500 Subject: [PATCH 08/77] 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 07467f1811cd..89225727f23e 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 c1c95d8d4ca6..1c5e3fecbe01 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -1437,13 +1437,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 From e24c78f543721a213ade9b019c5b9ad2af8cd30d Mon Sep 17 00:00:00 2001 From: Olshevsky Vladimir Date: Fri, 26 Jan 2024 11:28:23 +0000 Subject: [PATCH 09/77] Added option and function to build gtest libraries needed by llvm-adlt Signed-off-by: Olshevsky Vladimir Change-Id: I6392c6cc40fe1aae754dbf6503f3625f8c8642de --- llvm-build/build.py | 54 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/llvm-build/build.py b/llvm-build/build.py index b04b7218851a..3481b8f90196 100755 --- a/llvm-build/build.py +++ b/llvm-build/build.py @@ -2248,6 +2248,60 @@ class LlvmLibs(BuildUtils): os.path.join(llvm_install, 'include', 'gtest'), dirs_exist_ok = True) + def copy_gtest_to_sysroot(self, build_dir): + build_lib_dir = os.path.join(build_dir, 'lib') + self.logger().info('LlvmPackage copy_gtest_to_sysroot from %s', build_lib_dir) + libs = [ "libLLVMSupport.so", "libLLVMDemangle.so", "libllvm_gtest.so" ] + sysroot_lib_dir = self.merge_out_path('sysroot', 'aarch64-linux-ohos', 'usr', 'lib') + + os.chdir(build_lib_dir) + for f in libs: + self.check_copy_file(f'{f}.15', sysroot_lib_dir) + os.chdir(sysroot_lib_dir) + os.symlink(f'{f}.15', f) + os.chdir(build_lib_dir) + + def build_gtest_defines(self, llvm_install): + gtest_defines = {} + gtest_defines['BUILD_SHARED_LIBS'] = 'YES' + gtest_defines['CMAKE_BUILD_TYPE'] = 'Release' + gtest_defines['CMAKE_C_COMPILER'] = os.path.join(llvm_install, 'bin', 'clang') + gtest_defines['CMAKE_CXX_COMPILER'] = os.path.join(llvm_install, 'bin', 'clang++') + gtest_defines['LLVM_TABLEGEN'] = os.path.join(llvm_install, 'bin', 'llvm-tblgen') + gtest_defines['CMAKE_LINKER'] = os.path.join(llvm_install, 'bin', 'ld.lld') + gtest_defines['CMAKE_C_FLAGS'] = '--target=aarch64-linux-ohos' + gtest_defines['CMAKE_CXX_FLAGS'] = '--target=aarch64-linux-ohos' + + return gtest_defines + + def build_gtest(self, llvm_install): + gtest_defines = self.build_gtest_defines(llvm_install) + gtest_cmake_path = os.path.abspath(os.path.join(self.build_config.LLVM_PROJECT_DIR, 'llvm')) + + gtest_build_path = self.merge_out_path('gtest') + + self.rm_cmake_cache(gtest_build_path) + + self.invoke_cmake(gtest_cmake_path, + gtest_build_path, + gtest_defines, + env=dict(self.build_config.ORIG_ENV)) + + self.invoke_ninja(out_path=gtest_build_path, + env=dict(self.build_config.ORIG_ENV), + target=[ "LLVMSupport", "LLVMDemangle", "llvm_gtest" ], + install=False) + + self.copy_gtest_to_sysroot(gtest_build_path) + shutil.copytree( + os.path.join(self.build_config.LLVM_PROJECT_DIR, 'llvm', 'utils', 'unittest', 'googlemock', 'include', 'gmock'), + os.path.join(llvm_install, 'include', 'gmock'), + dirs_exist_ok = True) + shutil.copytree( + os.path.join(self.build_config.LLVM_PROJECT_DIR, 'llvm', 'utils', 'unittest', 'googletest', 'include', 'gtest'), + os.path.join(llvm_install, 'include', 'gtest'), + dirs_exist_ok = True) + def build_libxml2_defines(self): libxml2_defines = {} libxml2_defines['LIBXML2_WITH_PYTHON'] = 'OFF' -- Gitee From 71f64c164f3bcd0de425ab90829b05fedfb24b33 Mon Sep 17 00:00:00 2001 From: Likholatov Evgeny Date: Thu, 8 Feb 2024 16:29:28 +0300 Subject: [PATCH 10/77] Added --build-only flag. Signed-off-by: Likholatov Evgeny --- llvm-build/build.py | 43 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/llvm-build/build.py b/llvm-build/build.py index 3481b8f90196..db0e4dc4f672 100755 --- a/llvm-build/build.py +++ b/llvm-build/build.py @@ -2262,6 +2262,9 @@ class LlvmLibs(BuildUtils): os.chdir(build_lib_dir) def build_gtest_defines(self, llvm_install): + sysroot = f'{self.build_config.OUT_PATH}/sysroot/aarch64-linux-ohos/usr' + common_flags = f'--target=aarch64-linux-ohos -B{sysroot}/lib -L{sysroot}/lib' + gtest_defines = {} gtest_defines['BUILD_SHARED_LIBS'] = 'YES' gtest_defines['CMAKE_BUILD_TYPE'] = 'Release' @@ -2269,13 +2272,16 @@ class LlvmLibs(BuildUtils): gtest_defines['CMAKE_CXX_COMPILER'] = os.path.join(llvm_install, 'bin', 'clang++') gtest_defines['LLVM_TABLEGEN'] = os.path.join(llvm_install, 'bin', 'llvm-tblgen') gtest_defines['CMAKE_LINKER'] = os.path.join(llvm_install, 'bin', 'ld.lld') - gtest_defines['CMAKE_C_FLAGS'] = '--target=aarch64-linux-ohos' - gtest_defines['CMAKE_CXX_FLAGS'] = '--target=aarch64-linux-ohos' + gtest_defines['CMAKE_EXE_LINKER_FLAGS'] = f'{common_flags}' + gtest_defines['CMAKE_C_FLAGS'] = f'{common_flags} -I{sysroot}/include' + gtest_defines['CMAKE_CXX_FLAGS'] = f'{common_flags} \ + -I{self.build_config.OUT_PATH}/llvm-install/include/libcxx-ohos/include/c++/v1 \ + -I{sysroot}/include' return gtest_defines - def build_gtest(self, llvm_install): - gtest_defines = self.build_gtest_defines(llvm_install) + def build_gtest(self, compiler_path, llvm_install): + gtest_defines = self.build_gtest_defines(compiler_path) gtest_cmake_path = os.path.abspath(os.path.join(self.build_config.LLVM_PROJECT_DIR, 'llvm')) gtest_build_path = self.merge_out_path('gtest') @@ -3216,6 +3222,35 @@ def main(): for (_, target) in configs: llvm_libs.build_libs_by_type(llvm_path, llvm_install, target, 'runtimes', False, False) + if build_config.build_only: + if "musl" in build_config.build_only_libs: + # change compiller path to prebuilds in clang.gni file + file = os.path.join(build_config.REPOROOT_DIR, "build", "config", "clang", "clang.gni") + file_tmp = f"{file}_tmp" + shutil.move(file, file_tmp) + with open(file_tmp, 'r') as f1: + data = f1.read() + with open(file, 'w') as f2: + data = data.replace("//out/llvm-install", "${toolchains_dir}/${host_platform_dir}/" + f"clang-{build_config.CLANG_VERSION}") + f2.write(data) + # build musl + for (arch, target) in configs: + sysroot_composer.build_musl_header(arch, target) + sysroot_composer.build_musl(arch, target) + # return original version of clang.gni + shutil.move(file_tmp, file) + + if "compiler-rt" in build_config.build_only_libs: + assert os.path.exists(os.path.join(build_config.REPOROOT_DIR, "out", "sysroot")), "Error! Compiler-rt require musl!" + for (_, target) in configs: + llvm_libs.build_libs_by_type(llvm_path, llvm_install, target, 'crts', True, False) + llvm_libs.build_libs_by_type(llvm_path, llvm_install, target, 'crts', False, False) + + if "libcxx" in build_config.build_only_libs: + assert os.path.exists(os.path.join(build_config.REPOROOT_DIR, "out", "sysroot")), "Error! Libcxx require musl!" + for (_, target) in configs: + llvm_libs.build_libs_by_type(llvm_path, llvm_install, target, 'runtimes', False, False) + windows_python_builder = None if build_config.do_build and need_windows: -- Gitee From 28b2e8c2c7f41966d950a20349c724b49a1261a1 Mon Sep 17 00:00:00 2001 From: Likholatov Evgeny Date: Fri, 9 Feb 2024 12:41:42 +0300 Subject: [PATCH 11/77] Added description to --build-only flag in README. Signed-off-by: Likholatov Evgeny --- llvm-build/build.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/llvm-build/build.py b/llvm-build/build.py index db0e4dc4f672..2ccbf2aa796f 100755 --- a/llvm-build/build.py +++ b/llvm-build/build.py @@ -2262,8 +2262,10 @@ class LlvmLibs(BuildUtils): os.chdir(build_lib_dir) def build_gtest_defines(self, llvm_install): - sysroot = f'{self.build_config.OUT_PATH}/sysroot/aarch64-linux-ohos/usr' + sysroot = self.merge_out_path('sysroot', 'aarch64-linux-ohos', 'usr') common_flags = f'--target=aarch64-linux-ohos -B{sysroot}/lib -L{sysroot}/lib' + libcxx = self.merge_out_path('llvm-install', 'include', 'libcxx-ohos', 'include', 'c++', 'v1') + libc = os.path.join(sysroot, "include") gtest_defines = {} gtest_defines['BUILD_SHARED_LIBS'] = 'YES' @@ -2273,10 +2275,8 @@ class LlvmLibs(BuildUtils): gtest_defines['LLVM_TABLEGEN'] = os.path.join(llvm_install, 'bin', 'llvm-tblgen') gtest_defines['CMAKE_LINKER'] = os.path.join(llvm_install, 'bin', 'ld.lld') gtest_defines['CMAKE_EXE_LINKER_FLAGS'] = f'{common_flags}' - gtest_defines['CMAKE_C_FLAGS'] = f'{common_flags} -I{sysroot}/include' - gtest_defines['CMAKE_CXX_FLAGS'] = f'{common_flags} \ - -I{self.build_config.OUT_PATH}/llvm-install/include/libcxx-ohos/include/c++/v1 \ - -I{sysroot}/include' + gtest_defines['CMAKE_C_FLAGS'] = f'{common_flags} -I{libc}' + gtest_defines['CMAKE_CXX_FLAGS'] = f'{common_flags} -I{libcxx} -I{libc}' return gtest_defines -- Gitee From d66cc7f986cb7762d774810b36cb6cd3dc48d517 Mon Sep 17 00:00:00 2001 From: Likholatov Evgeny Date: Wed, 14 Feb 2024 14:28:39 +0300 Subject: [PATCH 12/77] Added check of usage --build-only with --no-build. Signed-off-by: Likholatov Evgeny --- llvm-build/build.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/llvm-build/build.py b/llvm-build/build.py index 2ccbf2aa796f..5cdb470d00b1 100755 --- a/llvm-build/build.py +++ b/llvm-build/build.py @@ -3225,12 +3225,12 @@ def main(): if build_config.build_only: if "musl" in build_config.build_only_libs: # change compiller path to prebuilds in clang.gni file - file = os.path.join(build_config.REPOROOT_DIR, "build", "config", "clang", "clang.gni") - file_tmp = f"{file}_tmp" - shutil.move(file, file_tmp) - with open(file_tmp, 'r') as f1: + clang_gni = os.path.join(build_config.REPOROOT_DIR, "build", "config", "clang", "clang.gni") + clang_gni_tmp = f"{clang_gni}_tmp" + shutil.move(clang_gni, clang_gni_tmp) + with open(clang_gni_tmp, 'r') as f1: data = f1.read() - with open(file, 'w') as f2: + with open(clang_gni, 'w') as f2: data = data.replace("//out/llvm-install", "${toolchains_dir}/${host_platform_dir}/" + f"clang-{build_config.CLANG_VERSION}") f2.write(data) # build musl @@ -3238,7 +3238,7 @@ def main(): sysroot_composer.build_musl_header(arch, target) sysroot_composer.build_musl(arch, target) # return original version of clang.gni - shutil.move(file_tmp, file) + shutil.move(clang_gni_tmp, clang_gni) if "compiler-rt" in build_config.build_only_libs: assert os.path.exists(os.path.join(build_config.REPOROOT_DIR, "out", "sysroot")), "Error! Compiler-rt require musl!" -- Gitee From ee0915e1f65f3852c52adb4d7d3e61d2d765b7b2 Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Wed, 31 Jan 2024 09:43:27 -0500 Subject: [PATCH 13/77] Proc relocs: better algorithm. Gen extra local syms. Resolve relocs in sep method. Change-Id: I619281879407d2d7275344528cb9c36b2ec598be Signed-off-by: Anton Volkov --- lld/ELF/Arch/AArch64.cpp | 10 ++ lld/ELF/InputFiles.cpp | 108 +++++++++---- lld/ELF/InputFiles.h | 17 +- lld/ELF/Relocations.cpp | 341 ++++++++++++++++++++------------------- 4 files changed, 277 insertions(+), 199 deletions(-) diff --git a/lld/ELF/Arch/AArch64.cpp b/lld/ELF/Arch/AArch64.cpp index 4c3e5aff9ef2..5a0c9963d5a6 100644 --- a/lld/ELF/Arch/AArch64.cpp +++ b/lld/ELF/Arch/AArch64.cpp @@ -81,6 +81,16 @@ AArch64::AArch64() { RelExpr AArch64::getRelExpr(RelType type, const Symbol &s, const uint8_t *loc) const { + if (config->adlt) + switch (type) { + case R_AARCH64_GLOB_DAT: + case R_AARCH64_JUMP_SLOT: + case R_AARCH64_RELATIVE: + return R_ABS; + default: + break; + } + switch (type) { case R_AARCH64_ABS16: case R_AARCH64_ABS32: diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp index 89225727f23e..dc83afb14596 100644 --- a/lld/ELF/InputFiles.cpp +++ b/lld/ELF/InputFiles.cpp @@ -1631,7 +1631,15 @@ SharedFileExtended::SharedFileExtended(MemoryBufferRef mb, template void SharedFileExtended::parseForAdlt() { this->parse(); parseDynamics(); + parseElfSymTab(); + // add additional section symbols + for (InputSectionBase *sec : this->sections) + for (StringRef prefix : {".got", ".got.plt"}) + if (sec && sec->name.startswith(prefix)) { + auto *d = make(this, "", ELF::STB_LOCAL, 0, ELF::STT_SECTION, 0, 0, sec); + this->allSymbols.push_back(cast(d)); + } bool isDebug = false; if (!isDebug) @@ -1694,15 +1702,32 @@ bool SharedFileExtended::addAdltPostfix(Symbol *s) { return true; } +template +bool SharedFileExtended::saveSymbol(const Defined& d) const { + auto found = elf::symtab->find(d.getName()); + if (found) + return false; + in.symTab->addSymbol(elf::symtab->addSymbol(d)); + return true; +} + template Defined *SharedFileExtended::findSectionSymbol(uint64_t offset) const { bool isDebug = false; + // if (offset == 0x43e0) + // isDebug = true; auto predRange = [=](Symbol *sym) { if (!sym || sym->isUndefined() || !sym->isSection()) return false; const Defined *d = cast(sym); uint64_t low = d->section->address; uint64_t high = low + d->section->size; + + if (isDebug) + lld::outs() << "offset: 0x" + Twine::utohexstr(offset) + + "sect name: " + d->section->name + "low 0x" + + Twine::utohexstr(low) + " high: 0x" + + Twine::utohexstr(offset) + "\n"; return (offset >= low) && (offset < high); }; @@ -1719,33 +1744,56 @@ Defined *SharedFileExtended::findSectionSymbol(uint64_t offset) const { return d; } +template +InputSectionBase * +SharedFileExtended::findInputSection(StringRef name) const { + for (InputSectionBase *sec : this->sections) + if (sec && sec->name == name) + return sec; + return nullptr; +} + +template +InputSectionBase * +SharedFileExtended::findInputSection(uint64_t offset) const { + for (InputSectionBase *sec : this->sections) + if (sec && sec->address == offset) + return sec; + return nullptr; +} + template Defined *SharedFileExtended::findDefinedSymbol( - uint64_t offset, llvm::function_ref extraCond) const { + uint64_t offset, StringRef fatalTitle, + llvm::function_ref extraCond) const { bool isDebug = false; - SmallVector matches; - for (Symbol *sym : this->allSymbols) { + auto predRange = [=](Symbol *sym) { if (!sym || sym->isUndefined()) - continue; + return false; Defined *d = cast(sym); if (d->file != this) - continue; + return false; bool goodVal = d->section->address + d->value == offset; bool goodType = d->type == STT_FUNC || d->type == STT_OBJECT; - if (!(goodVal && goodType && extraCond(d))) - continue; + return goodVal && goodType && extraCond(d); + }; + auto i = this->allSymbols.begin(); + auto e = this->allSymbols.end(); + auto ret = std::find_if(i, e, predRange); + if (ret != e) { // item was found + Defined* d = cast(*ret); if (isDebug) - traceSymbol(*sym, "found sym: "); - matches.push_back(d); + traceSymbol(*d, "found defined sym: "); + return d; } - if (!matches.empty()) - return matches[0]; // TODO: resolve multiple variants - - return findSectionSymbol(offset); + auto* sectionSym = findSectionSymbol(offset); + if (!sectionSym) + fatal(fatalTitle + " 0x" + Twine::utohexstr(offset) + "\n"); + return sectionSym; } template @@ -1779,35 +1827,41 @@ void SharedFileExtended::traceElfSection(const Elf_Shdr &sec) const { } template -void SharedFileExtended::traceSymbol(const Symbol& sym, StringRef title) const { - lld::outs() << "File: " << soName << ": " + title << " symName: " << sym.getName(); +void SharedFileExtended::traceSymbol(const Symbol &sym, + StringRef title) const { + lld::outs() << "File: " << soName << ": " + title + << " symName: " << sym.getName(); if (!sym.isDefined()) { lld::outs() << '\n'; return; } auto &d = cast(sym); - lld::outs() << " val: 0x" << Twine::utohexstr(d.value) << " sec of sym: " - << (d.section ? d.section->name : "unknown!") + lld::outs() << " val: 0x" << Twine::utohexstr(d.value) + << " sec of sym: " << (d.section ? d.section->name : "unknown!") << " sym type: 0x" << Twine::utohexstr(d.type) - << " sym binding: 0x" << Twine::utohexstr(d.binding) - << '\n'; + << " sym binding: 0x" << Twine::utohexstr(d.binding) << '\n'; } template -void SharedFileExtended::traceSection(const SectionBase& sec) const { - lld::outs() << "File: " << soName << " sec: " << sec.name << " sec addr: 0x" - << Twine::utohexstr(sec.address) << " sec offs: 0x" - << Twine::utohexstr(sec.getOffset(0)) << " sec ent size: 0x" - << Twine::utohexstr(sec.entsize) << '\n'; +void SharedFileExtended::traceSection(const SectionBase &sec, + StringRef title) const { + lld::outs() << "File: " << soName << ": " + title << " sec: " << sec.name + << " sec addr: 0x" << Twine::utohexstr(sec.address) + << " sec offs: 0x" << Twine::utohexstr(sec.getOffset(0)) + << " sec ent size: 0x" << Twine::utohexstr(sec.entsize) << '\n'; } template -Symbol& SharedFileExtended::getSymbol(uint32_t symbolIndex) const { - if (symbolIndex >= this->symbols.size()) - return getSymbolFromElfSymTab(symbolIndex); +Symbol &SharedFileExtended::getDynamicSymbol(uint32_t symbolIndex) const { return *this->symbols[symbolIndex]; } +template +Symbol &SharedFileExtended::getSymbol(uint32_t symbolIndex) const { + if (symbolIndex >= this->symbols.size()) + return getSymbolFromElfSymTab(symbolIndex); + return getDynamicSymbol(symbolIndex); +} template void SharedFileExtended::parseDynamics() { const ELFFile obj = this->getObj(); diff --git a/lld/ELF/InputFiles.h b/lld/ELF/InputFiles.h index 821e5f369db3..94c75a0bcea1 100644 --- a/lld/ELF/InputFiles.h +++ b/lld/ELF/InputFiles.h @@ -402,10 +402,18 @@ public: StringRef addAdltPostfix(StringRef input); bool addAdltPostfix(Symbol *s); + bool saveSymbol(const Defined& d) const; + Defined *findSectionSymbol(uint64_t offset) const; Defined *findDefinedSymbol( - uint64_t offset, llvm::function_ref extraCond = - [](Defined *) { return true; }) const; + uint64_t offset, + StringRef fatalTitle = "defined symbol not found! offset:", + llvm::function_ref extraCond = [](Defined *) { + return true; + }) const; + + InputSectionBase *findInputSection(StringRef name) const; + InputSectionBase *findInputSection(uint64_t offset) const; ArrayRef getLocalSymbols() override { if (this->allSymbols.empty()) @@ -413,6 +421,7 @@ public: return llvm::makeArrayRef(this->allSymbols).slice(1, eFirstGlobal - 1); } + Symbol &getDynamicSymbol(uint32_t symbolIndex) const; Symbol &getSymbol(uint32_t symbolIndex) const override; Symbol &getSymbolFromElfSymTab(uint32_t symbolIndex) const { @@ -424,8 +433,8 @@ public: void traceElfSymbol(const Elf_Sym &sym, StringRef strTable) const; void traceElfSection(const Elf_Shdr &sec) const; - void traceSymbol(const Symbol& sym, StringRef title = "") const; - void traceSection(const SectionBase& sec) const; + void traceSymbol(const Symbol &sym, StringRef title = "") const; + void traceSection(const SectionBase &sec, StringRef title = "") const; int dynSymSecIdx = 0; int symTabSecIdx = 0; diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp index 1c5e3fecbe01..c632a03c4af7 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -461,8 +461,16 @@ private: int64_t addend) const; template void scanOne(RelTy *&i); + // ADLT template - void handleRelativeReloc(const RelTy &rel, const Symbol& sym, uint64_t offset); + void processForADLT(const RelTy &rel, Relocation *r); + + template + void addRelativeRelocForAdlt(const RelTy &rel, Defined &symWhere, + Defined &inputSym); + + template + void tracePushRelocADLT(Defined &symWhere, Relocation &r) const; }; } // namespace @@ -1304,6 +1312,167 @@ static unsigned handleTlsRelocation(RelType type, Symbol &sym, return 0; } +// ADLT BEGIN +template +void RelocationScanner::addRelativeRelocForAdlt(const RelTy &rel, + Defined &symWhere, + Defined &inputSym) { + // auto file = sec.getSharedFile(); + std::string title = "addRelativeRelocForAdlt: "; + std::string failTitle = ""; + + RelType type = R_AARCH64_ABS64; + RelExpr expr = R_ABS; + int64_t addend = computeAddend(rel, expr, false); + + // 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, + inputSym, addend, expr, type); +} + +template +void RelocationScanner::tracePushRelocADLT(Defined &symWhere, + Relocation &r) const { + auto file = sec.getSharedFile(); + lld::outs() << "[ADLT] Before push: type: " + toString(r.type) + + " expr: " + toString(r.expr) + " offset: 0x" + + utohexstr(r.offset) + " addend: 0x" + utohexstr(r.addend) + + ".\n"; + lld::outs() << "symWhere: "; + file->traceSymbol(symWhere); + + lld::outs() << "inputSym: "; + file->traceSymbol(*r.sym); + lld::outs() << "\n"; +} + +template +void RelocationScanner::processForADLT(const RelTy &rel, Relocation *r) { + auto file = sec.getSharedFile(); + // uint32_t symIndex = rel.getSymbol(config->isMips64EL); + std::string title = "processForADLT: "; + bool isDebug = false; + + // parse offset (where) + std::string failTitle = title + "symWhere not found! offset: "; + Defined *symWhere = file->findDefinedSymbol(r->offset, failTitle); + file->saveSymbol(*symWhere); + + /*if (r->offset == 0x22c4) // 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 + auto pushReloc = [=](Symbol *s) { + r->sym = s; + if (isDebug) + tracePushRelocADLT(*symWhere, *r); + sec.relocations.push_back(*r); + }; + + auto findGlobalSymbol = [=]() { + StringRef name = r->sym->getName(); + if (name.empty()) + return static_cast(nullptr); + auto res = elf::symtab->find(name); + if (res && res->exportDynamic) { + if (isDebug) + lld::outs() << "found glob sym: " + name + "\n"; + return res; + } + return static_cast(nullptr); + }; + + Symbol *inputSym = findGlobalSymbol(); + if (!inputSym) { // if symbol was not found in elf::symtab + inputSym = r->sym; // assign local sym + auto found = llvm::find_if( + in.symTab->getSymbols(), [&](const SymbolTableEntry &entry) { + return entry.sym->getName() == inputSym->getName(); + }); + // add a local sym if it was not previously added + if (found == in.symTab->getSymbols().end()) + in.symTab->addSymbol(inputSym); + } + + // resolve relocs + switch (r->type) { + // dyn relocs + case R_AARCH64_RELATIVE: + failTitle = + title + " " + toString(r->type) + ": inputSym not found! addend: "; + if (auto *d = file->findDefinedSymbol( + r->addend, failTitle, [](Defined *d) { return !d->isPreemptible; })) + addRelativeRelocForAdlt(rel, *symWhere, *d); + return; + case R_AARCH64_GLOB_DAT: + inputSym->needsGot = 1; + if (inputSym->isUndefined()) + inputSym->needsPlt = 1; + return; + case R_AARCH64_JUMP_SLOT: + if (inputSym->isUndefined()) + inputSym->needsPlt = 1; + return; + // abs relocs + case R_AARCH64_ABS32: + case R_AARCH64_ABS64: + case R_AARCH64_ADD_ABS_LO12_NC: + case R_AARCH64_ADR_PREL_PG_HI21: + case R_AARCH64_LDST8_ABS_LO12_NC: + case R_AARCH64_LDST16_ABS_LO12_NC: + case R_AARCH64_LDST32_ABS_LO12_NC: + case R_AARCH64_LDST64_ABS_LO12_NC: + case R_AARCH64_LDST128_ABS_LO12_NC: + case R_AARCH64_PREL32: + case R_AARCH64_PREL64: + pushReloc(inputSym); + return; + // plt relocs + case R_AARCH64_CALL26: + case R_AARCH64_JUMP26: + if (inputSym->isDefined()) + r->expr = R_PC; // prev: R_PLT_PC + pushReloc(inputSym); + return; + // got relocs + case R_AARCH64_ADR_GOT_PAGE: + if (inputSym->isDefined() && !inputSym->needsGot) { + if (isDebug) { + lld::outs() << "[ADLT] R_AARCH64_ADR_GOT_PAGE: sym not in GOT! "; + file->traceSymbol(*inputSym); + } + r->expr = R_PC; // prev: R_AARCH64_GOT_PAGE_PC || R_AARCH64_GOT_PAGE || + // R_GOT || R_GOT_PC + } + LLVM_FALLTHROUGH; + case R_AARCH64_LD64_GOT_LO12_NC: + pushReloc(inputSym); + return; + default: + fatal("Unhandled reloc: " + toString(r->type)); + break; + } +} +// ADLT END + template void RelocationScanner::scanOne(RelTy *&i) { const RelTy &rel = *i; uint32_t symIndex = rel.getSymbol(config->isMips64EL); @@ -1331,74 +1500,6 @@ template void RelocationScanner::scanOne(RelTy *&i) { return; const uint8_t *relocatedAddr = sec.rawData.begin() + offset; - if (config->adlt) { - auto file = sec.getSharedFile(); - bool isDebug = false; - - auto saveSymIfNeeded = [&](const Defined& d) { - auto found = elf::symtab->find(d.getName()); - if (!found) - in.symTab->addSymbol(elf::symtab->addSymbol(d)); - }; - - auto handleRelativeReloc = [&]() { - RelType type = R_AARCH64_ABS64; - RelExpr expr = target.getRelExpr(type, sym, relocatedAddr); - uint64_t addend = computeAddend(rel, expr, sym.isLocal()); - StringRef title = "handle relative reloc: "; - if (isDebug) - lld::outs() << title << "offset: 0x" + Twine::utohexstr(offset) - << " addend: 0x" + Twine::utohexstr(addend) + "\n"; - - // parse offset (where) - const Defined *symWhere = file->findDefinedSymbol(offset); - if (!symWhere) - fatal(title + "symWhere not found! offset: 0x" + - Twine::utohexstr(offset) + "\n"); - auto iSec = cast(symWhere->section); - offset = - symWhere->isSection() ? (offset - symWhere->section->address) : 0; - saveSymIfNeeded(*symWhere); - - // parse addent (replacement) - Defined *inputSym = file->findDefinedSymbol( - addend, [](Defined *d) { return !d->isPreemptible; }); - - if (!inputSym) - fatal(title + "inputSym not found! addend: 0x" + - Twine::utohexstr(addend) + "\n"); - addend = - inputSym->isSection() ? (addend - inputSym->section->address) : 0; - - if (isDebug) - file->traceSymbol(*inputSym, "handle relative reloc: inputSym: "); - saveSymIfNeeded(*inputSym); - - // finally - addRelativeReloc(*iSec, symWhere->value, *inputSym, addend, expr, type); - }; - switch (type) { - case R_AARCH64_RELATIVE: - handleRelativeReloc(); - return; - case R_AARCH64_GLOB_DAT: { - Symbol &s = file->getSymbol(symIndex); - s.needsGot = 1; - if (s.isUndefined()) - s.needsPlt = 1; - return; - } - case R_AARCH64_JUMP_SLOT: { - Symbol &s = file->getSymbol(symIndex); - if (s.isUndefined()) - s.needsPlt = 1; - return; - } - default: - break; - } - } - RelExpr expr = target.getRelExpr(type, sym, relocatedAddr); // Ignore R_*_NONE and other marker relocations. @@ -1409,105 +1510,9 @@ template void RelocationScanner::scanOne(RelTy *&i) { int64_t addend = computeAddend(rel, expr, sym.isLocal()); if (config->adlt) { - auto file = sec.getSharedFile(); - auto traceSym = [&](StringRef debugTitle, Symbol& sym) { - lld::outs() << debugTitle; - lld::outs() << "type: " << lld::toString(type) << " sym: "; - Symbol* p = &sym; - if (!p) { - lld::outs() << "unknown!\n"; - return; - } - file->traceSymbol(sym); - if (!sym.isDefined()) - return; - lld::outs() << "sec: "; - file->traceSection(*(cast(sym).section)); - }; - - bool isDebug = false; - auto strType = lld::toString(type); - auto debugTitle = "[RelocationScanner::handle relocs]: " + strType + ": "; - if (isDebug) { - traceSym(debugTitle, sym); - lld::outs() << " offset before: 0x" << Twine::utohexstr(offset) << '\n'; - } - if (offset + sec.address) - offset -= sec.address; - - 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 (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 - s = &sym; - in.symTab->addSymbol(s); - } - if (isDebug) - traceSym(debugTitle + "before push: ", *s); - - if (s->isUndefined()) { - if (expr == R_PLT || expr == R_PLT_PC) - s->needsPlt = 1; - else if (expr == R_GOT || expr == R_GOT_PC) - s->needsGot = 1; - } - - auto pushReloc = [&]() { - sec.relocations.push_back({expr, type, offset, addend, s}); - }; - - bool isSymDefined = s && s->isDefined(); - - switch (type) { - // abs relocs - case R_AARCH64_ABS32: - case R_AARCH64_ABS64: - case R_AARCH64_ADD_ABS_LO12_NC: - case R_AARCH64_ADR_PREL_PG_HI21: - case R_AARCH64_LDST8_ABS_LO12_NC: - case R_AARCH64_LDST16_ABS_LO12_NC: - case R_AARCH64_LDST32_ABS_LO12_NC: - case R_AARCH64_LDST64_ABS_LO12_NC: - case R_AARCH64_LDST128_ABS_LO12_NC: - case R_AARCH64_PREL32: - case R_AARCH64_PREL64: - pushReloc(); - return; - // plt relocs - case R_AARCH64_CALL26: - case R_AARCH64_JUMP26: { - if (isSymDefined) - expr = R_PC; // prev: R_PLT_PC - pushReloc(); - return; - } - - // got relocs - case R_AARCH64_ADR_GOT_PAGE: - if (isSymDefined && !s->needsGot) { - if (isDebug) { - lld::outs() << "[ADLT] R_AARCH64_ADR_GOT_PAGE: sym not in GOT! "; - file->traceSymbol(*s); - } - expr = R_PC; // prev: R_AARCH64_GOT_PAGE_PC || R_AARCH64_GOT_PAGE || - // R_GOT || R_GOT_PC - } - LLVM_FALLTHROUGH; - case R_AARCH64_LD64_GOT_LO12_NC: - pushReloc(); - return; - default: - fatal("Unhandled reloc: " + strType); - break; - } - if (isDebug) - lld::outs() << " offset after: 0x" << Twine::utohexstr(offset) << "\n\n"; + Relocation r = {expr, type, offset, addend, &sym}; + processForADLT(rel, &r); + return; } if (config->emachine == EM_PPC64) { -- Gitee From 4b04379fd3c7892ebd5f8aa6e2b4bc2bae7401e8 Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Tue, 20 Feb 2024 02:36:54 -0500 Subject: [PATCH 14/77] Proc relocs: correct getting symbols at scan relocs. Cleanup proc relocs. Change-Id: Id32b41008789ffdb63f63d400500bf4a5e277e13 Signed-off-by: Anton Volkov --- lld/ELF/InputFiles.cpp | 30 +++++++++++--- lld/ELF/InputFiles.h | 2 +- lld/ELF/InputSection.cpp | 2 +- lld/ELF/Relocations.cpp | 84 ++++++++++++++++------------------------ 4 files changed, 60 insertions(+), 58 deletions(-) diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp index dc83afb14596..58b94857a385 100644 --- a/lld/ELF/InputFiles.cpp +++ b/lld/ELF/InputFiles.cpp @@ -1702,6 +1702,7 @@ bool SharedFileExtended::addAdltPostfix(Symbol *s) { return true; } +// TODO: optimize 2 lookups template bool SharedFileExtended::saveSymbol(const Defined& d) const { auto found = elf::symtab->find(d.getName()); @@ -1714,7 +1715,7 @@ bool SharedFileExtended::saveSymbol(const Defined& d) const { template Defined *SharedFileExtended::findSectionSymbol(uint64_t offset) const { bool isDebug = false; - // if (offset == 0x43e0) + // if (offset == 0x43e0) // debug hint // isDebug = true; auto predRange = [=](Symbol *sym) { if (!sym || sym->isUndefined() || !sym->isSection()) @@ -1857,10 +1858,29 @@ Symbol &SharedFileExtended::getDynamicSymbol(uint32_t symbolIndex) const { } template -Symbol &SharedFileExtended::getSymbol(uint32_t symbolIndex) const { - if (symbolIndex >= this->symbols.size()) - return getSymbolFromElfSymTab(symbolIndex); - return getDynamicSymbol(symbolIndex); +Symbol &SharedFileExtended::getSymbolADLT(uint32_t symbolIndex, + bool fromDynamic) const { + Symbol &sym = fromDynamic ? getDynamicSymbol(symbolIndex) + : getSymbolFromElfSymTab(symbolIndex); + + StringRef name = sym.getName(); + if (name.empty()) + return sym; + + // check SymbolTable + auto res = elf::symtab->find(name); + if (res && res->exportDynamic) + return *res; + + // check SymbolTableBaseSection + auto found = llvm::find_if( + in.symTab->getSymbols(), [&](const SymbolTableEntry &entry) { + return entry.sym->getName() == name; + }); + // add a local sym if it was not previously added + if (found == in.symTab->getSymbols().end()) + in.symTab->addSymbol(&sym); + return sym; } template void SharedFileExtended::parseDynamics() { diff --git a/lld/ELF/InputFiles.h b/lld/ELF/InputFiles.h index 94c75a0bcea1..bc3d594189ee 100644 --- a/lld/ELF/InputFiles.h +++ b/lld/ELF/InputFiles.h @@ -422,7 +422,7 @@ public: } Symbol &getDynamicSymbol(uint32_t symbolIndex) const; - Symbol &getSymbol(uint32_t symbolIndex) const override; + Symbol &getSymbolADLT(uint32_t symbolIndex, bool fromDynamic) const; Symbol &getSymbolFromElfSymTab(uint32_t symbolIndex) const { if (symbolIndex >= this->allSymbols.size()) diff --git a/lld/ELF/InputSection.cpp b/lld/ELF/InputSection.cpp index e4b8a4dcc777..7b9ed5ad0574 100644 --- a/lld/ELF/InputSection.cpp +++ b/lld/ELF/InputSection.cpp @@ -864,7 +864,7 @@ void InputSection::relocateNonAlloc(uint8_t *buf, ArrayRef rels) { if (!RelTy::IsRela) addend += target.getImplicitAddend(bufLoc, type); - Symbol &sym = config->adlt ? getSharedFile()->getSymbol( + Symbol &sym = config->adlt ? getSharedFile()->getSymbolFromElfSymTab( rel.getSymbol(config->isMips64EL)) : getFile()->getRelocTargetSym(rel); if (config->adlt) { // TODO improve diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp index c632a03c4af7..31023f3b9ae1 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -467,7 +467,8 @@ private: template void addRelativeRelocForAdlt(const RelTy &rel, Defined &symWhere, - Defined &inputSym); + Defined &inputSym) const; + void pushRelocAdlt(Relocation *r) const; template void tracePushRelocADLT(Defined &symWhere, Relocation &r) const; @@ -1316,7 +1317,7 @@ static unsigned handleTlsRelocation(RelType type, Symbol &sym, template void RelocationScanner::addRelativeRelocForAdlt(const RelTy &rel, Defined &symWhere, - Defined &inputSym) { + Defined &inputSym) const { // auto file = sec.getSharedFile(); std::string title = "addRelativeRelocForAdlt: "; std::string failTitle = ""; @@ -1341,6 +1342,10 @@ void RelocationScanner::addRelativeRelocForAdlt(const RelTy &rel, inputSym, addend, expr, type); } +void RelocationScanner::pushRelocAdlt(Relocation *r) const { + sec.relocations.push_back(*r); +} + template void RelocationScanner::tracePushRelocADLT(Defined &symWhere, Relocation &r) const { @@ -1352,7 +1357,7 @@ void RelocationScanner::tracePushRelocADLT(Defined &symWhere, lld::outs() << "symWhere: "; file->traceSymbol(symWhere); - lld::outs() << "inputSym: "; + lld::outs() << "r->sym: "; file->traceSymbol(*r.sym); lld::outs() << "\n"; } @@ -1360,8 +1365,8 @@ void RelocationScanner::tracePushRelocADLT(Defined &symWhere, template void RelocationScanner::processForADLT(const RelTy &rel, Relocation *r) { auto file = sec.getSharedFile(); - // uint32_t symIndex = rel.getSymbol(config->isMips64EL); - std::string title = "processForADLT: "; + uint32_t symIndex = rel.getSymbol(config->isMips64EL); + std::string title = "processForADLT: symIndex: " + toString(symIndex) + " "; bool isDebug = false; // parse offset (where) @@ -1369,8 +1374,8 @@ void RelocationScanner::processForADLT(const RelTy &rel, Relocation *r) { Defined *symWhere = file->findDefinedSymbol(r->offset, failTitle); file->saveSymbol(*symWhere); - /*if (r->offset == 0x22c4) // debug hint - isDebug = true;*/ + /*if (r->offset == 0x23dc) // debug hint + isDebug = true; */ // process offset failTitle = "offset: 0x" + utohexstr(r->offset) + " was not decreased!"; @@ -1380,56 +1385,30 @@ void RelocationScanner::processForADLT(const RelTy &rel, Relocation *r) { file->traceSection(sec, failTitle); // prepare to resolve relocs - auto pushReloc = [=](Symbol *s) { - r->sym = s; - if (isDebug) - tracePushRelocADLT(*symWhere, *r); - sec.relocations.push_back(*r); - }; - - auto findGlobalSymbol = [=]() { - StringRef name = r->sym->getName(); - if (name.empty()) - return static_cast(nullptr); - auto res = elf::symtab->find(name); - if (res && res->exportDynamic) { - if (isDebug) - lld::outs() << "found glob sym: " + name + "\n"; - return res; - } - return static_cast(nullptr); - }; + /*if (r->sym->getName() == "__ubsan_handle_cfi_check_fail") // debug hint + isDebug = true;*/ - Symbol *inputSym = findGlobalSymbol(); - if (!inputSym) { // if symbol was not found in elf::symtab - inputSym = r->sym; // assign local sym - auto found = llvm::find_if( - in.symTab->getSymbols(), [&](const SymbolTableEntry &entry) { - return entry.sym->getName() == inputSym->getName(); - }); - // add a local sym if it was not previously added - if (found == in.symTab->getSymbols().end()) - in.symTab->addSymbol(inputSym); - } + if (isDebug) + tracePushRelocADLT(*symWhere, *r); // resolve relocs switch (r->type) { // dyn relocs case R_AARCH64_RELATIVE: failTitle = - title + " " + toString(r->type) + ": inputSym not found! addend: "; + 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); return; case R_AARCH64_GLOB_DAT: - inputSym->needsGot = 1; - if (inputSym->isUndefined()) - inputSym->needsPlt = 1; + r->sym->needsGot = 1; + if (r->sym->isUndefined()) + r->sym->needsPlt = 1; return; case R_AARCH64_JUMP_SLOT: - if (inputSym->isUndefined()) - inputSym->needsPlt = 1; + if (r->sym->isUndefined()) + r->sym->needsPlt = 1; return; // abs relocs case R_AARCH64_ABS32: @@ -1443,28 +1422,28 @@ void RelocationScanner::processForADLT(const RelTy &rel, Relocation *r) { case R_AARCH64_LDST128_ABS_LO12_NC: case R_AARCH64_PREL32: case R_AARCH64_PREL64: - pushReloc(inputSym); + pushRelocAdlt(r); return; // plt relocs case R_AARCH64_CALL26: case R_AARCH64_JUMP26: - if (inputSym->isDefined()) + if (r->sym->isDefined()) r->expr = R_PC; // prev: R_PLT_PC - pushReloc(inputSym); + pushRelocAdlt(r); return; // got relocs case R_AARCH64_ADR_GOT_PAGE: - if (inputSym->isDefined() && !inputSym->needsGot) { + if (r->sym->isDefined() && !r->sym->needsGot) { if (isDebug) { lld::outs() << "[ADLT] R_AARCH64_ADR_GOT_PAGE: sym not in GOT! "; - file->traceSymbol(*inputSym); + file->traceSymbol(*r->sym); } r->expr = R_PC; // prev: R_AARCH64_GOT_PAGE_PC || R_AARCH64_GOT_PAGE || // R_GOT || R_GOT_PC } LLVM_FALLTHROUGH; case R_AARCH64_LD64_GOT_LO12_NC: - pushReloc(inputSym); + pushRelocAdlt(r); return; default: fatal("Unhandled reloc: " + toString(r->type)); @@ -1476,8 +1455,11 @@ void RelocationScanner::processForADLT(const RelTy &rel, Relocation *r) { template void RelocationScanner::scanOne(RelTy *&i) { const RelTy &rel = *i; uint32_t symIndex = rel.getSymbol(config->isMips64EL); - auto file = config->adlt ? sec.getSharedFile() : sec.getFile(); - Symbol &sym = file->getSymbol(symIndex); + bool fromDynamic = sec.type == SHT_NULL || sec.name.startswith(".got.plt"); + Symbol &sym = + config->adlt + ? sec.getSharedFile()->getSymbolADLT(symIndex, fromDynamic) + : sec.getFile()->getSymbol(symIndex); RelType type; // Deal with MIPS oddity. -- Gitee From 51ece89963e434461e5bfcdb440b1f133fcdc3af Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Thu, 22 Feb 2024 18:09:57 +0800 Subject: [PATCH 15/77] ADLT: Fix scan ver symbols. Change-Id: Ib0cd6cdf9eda15a2e4af94b69d872eb937a950d2 Signed-off-by: Anton Volkov --- lld/ELF/InputFiles.cpp | 53 ++++++++++++++++++++++------------------- lld/ELF/Relocations.cpp | 9 ++++--- 2 files changed, 35 insertions(+), 27 deletions(-) diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp index 58b94857a385..56e6507ace6b 100644 --- a/lld/ELF/InputFiles.cpp +++ b/lld/ELF/InputFiles.cpp @@ -831,7 +831,7 @@ template static uint32_t readAndFeatures(const InputSection &sec) { ArrayRef data = sec.rawData; auto reportFatal = [&](const uint8_t *place, const char *msg) { fatal(toString(sec.file) + ":(" + sec.name + "+0x" + - Twine::utohexstr(place - sec.rawData.data()) + "): " + msg); + utohexstr(place - sec.rawData.data()) + "): " + msg); }; while (!data.empty()) { // Read one NOTE record. @@ -1725,10 +1725,10 @@ Defined *SharedFileExtended::findSectionSymbol(uint64_t offset) const { uint64_t high = low + d->section->size; if (isDebug) - lld::outs() << "offset: 0x" + Twine::utohexstr(offset) + + lld::outs() << "offset: 0x" + utohexstr(offset) + "sect name: " + d->section->name + "low 0x" + - Twine::utohexstr(low) + " high: 0x" + - Twine::utohexstr(offset) + "\n"; + utohexstr(low) + " high: 0x" + + utohexstr(offset) + "\n"; return (offset >= low) && (offset < high); }; @@ -1785,20 +1785,21 @@ Defined *SharedFileExtended::findDefinedSymbol( auto e = this->allSymbols.end(); auto ret = std::find_if(i, e, predRange); if (ret != e) { // item was found - Defined* d = cast(*ret); + Defined *d = cast(*ret); if (isDebug) traceSymbol(*d, "found defined sym: "); return d; } - auto* sectionSym = findSectionSymbol(offset); + auto *sectionSym = findSectionSymbol(offset); if (!sectionSym) - fatal(fatalTitle + " 0x" + Twine::utohexstr(offset) + "\n"); + fatal(fatalTitle + " 0x" + utohexstr(offset) + "\n"); return sectionSym; } template -StringRef SharedFileExtended::getShStrTab(ArrayRef elfSections) { +StringRef +SharedFileExtended::getShStrTab(ArrayRef elfSections) { return CHECK(this->getObj().getSectionStringTable(elfSections), this); } @@ -1807,13 +1808,13 @@ void SharedFileExtended::traceElfSymbol(const Elf_Sym &sym, StringRef strTable) const { const ELFFile obj = this->getObj(); auto rawSec = obj.getSection(sym.st_shndx); - auto parsedSec = !rawSec.takeError() ? *obj.getSection(sym.st_shndx) : nullptr; + auto parsedSec = + !rawSec.takeError() ? *obj.getSection(sym.st_shndx) : nullptr; lld::outs() << "File: " << soName << " symName: " << *sym.getName(strTable) - << " val: 0x" << Twine::utohexstr(sym.st_value) << " sec of sym: " + << " val: 0x" << utohexstr(sym.st_value) << " sec of sym: " << (parsedSec ? *obj.getSectionName(*parsedSec) : "unknown!") - << " sym type: 0x" << Twine::utohexstr(sym.getType()) - << " sym binding: 0x" << Twine::utohexstr(sym.getBinding()) - << '\n'; + << " sym type: 0x" << utohexstr(sym.getType()) + << " sym binding: 0x" << utohexstr(sym.getBinding()) << '\n'; } template @@ -1822,34 +1823,35 @@ void SharedFileExtended::traceElfSection(const Elf_Shdr &sec) const { auto secName = *obj.getSectionName(sec); lld::outs() << "File: " << soName << " sec: " << secName << " sec addr: 0x" - << Twine::utohexstr(sec.sh_addr) << " sec offs: 0x" - << Twine::utohexstr(sec.sh_offset) << " sec ent size: 0x" - << Twine::utohexstr(sec.sh_entsize) << '\n'; + << utohexstr(sec.sh_addr) << " sec offs: 0x" + << utohexstr(sec.sh_offset) << " sec ent size: 0x" + << utohexstr(sec.sh_entsize) << '\n'; } template void SharedFileExtended::traceSymbol(const Symbol &sym, StringRef title) const { lld::outs() << "File: " << soName << ": " + title - << " symName: " << sym.getName(); + << " symName: " << sym.getName() << " exportDynamic: 0x" + << utohexstr(sym.exportDynamic); if (!sym.isDefined()) { lld::outs() << '\n'; return; } auto &d = cast(sym); - lld::outs() << " val: 0x" << Twine::utohexstr(d.value) + lld::outs() << " val: 0x" << utohexstr(d.value) << " sec of sym: " << (d.section ? d.section->name : "unknown!") - << " sym type: 0x" << Twine::utohexstr(d.type) - << " sym binding: 0x" << Twine::utohexstr(d.binding) << '\n'; + << " sym type: 0x" << utohexstr(d.type) << " sym binding: 0x" + << utohexstr(d.binding) << '\n'; } template void SharedFileExtended::traceSection(const SectionBase &sec, StringRef title) const { lld::outs() << "File: " << soName << ": " + title << " sec: " << sec.name - << " sec addr: 0x" << Twine::utohexstr(sec.address) - << " sec offs: 0x" << Twine::utohexstr(sec.getOffset(0)) - << " sec ent size: 0x" << Twine::utohexstr(sec.entsize) << '\n'; + << " sec addr: 0x" << utohexstr(sec.address) << " sec offs: 0x" + << utohexstr(sec.getOffset(0)) << " sec ent size: 0x" + << utohexstr(sec.entsize) << '\n'; } template @@ -1867,9 +1869,12 @@ Symbol &SharedFileExtended::getSymbolADLT(uint32_t symbolIndex, if (name.empty()) return sym; + /*if (name.contains("_ZdlPv")) // debug hint + lld::outs() << "debug getSymbolADLT(): " << name << "\n";*/ + // check SymbolTable auto res = elf::symtab->find(name); - if (res && res->exportDynamic) + if (res && (res->exportDynamic || res->versionId)) return *res; // check SymbolTableBaseSection diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp index 31023f3b9ae1..d5d52e4d99c8 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -100,7 +100,8 @@ void elf::reportRangeError(uint8_t *loc, const Relocation &rel, const Twine &v, ErrorPlace errPlace = getErrorPlace(loc); std::string hint; if (rel.sym && !rel.sym->isSection()) - hint = "; references " + lld::toString(*rel.sym); + hint = "; references " + lld::toString(*rel.sym) + + (config->adlt ? ": raw-name: " + rel.sym->getName().str() + " " : ""); if (!errPlace.srcLoc.empty()) hint += "\n>>> referenced by " + errPlace.srcLoc; if (rel.sym && !rel.sym->isSection()) @@ -1350,7 +1351,9 @@ template void RelocationScanner::tracePushRelocADLT(Defined &symWhere, Relocation &r) const { auto file = sec.getSharedFile(); - lld::outs() << "[ADLT] Before push: type: " + toString(r.type) + + auto fullOffset = symWhere.section->address + r.offset; + lld::outs() << "[ADLT] Before push: [" + utohexstr(fullOffset) + + "] type: " + toString(r.type) + " expr: " + toString(r.expr) + " offset: 0x" + utohexstr(r.offset) + " addend: 0x" + utohexstr(r.addend) + ".\n"; @@ -1385,7 +1388,7 @@ void RelocationScanner::processForADLT(const RelTy &rel, Relocation *r) { file->traceSection(sec, failTitle); // prepare to resolve relocs - /*if (r->sym->getName() == "__ubsan_handle_cfi_check_fail") // debug hint + /*if (r->sym->getName().contains("_ZdlPv")) // debug hint isDebug = true;*/ if (isDebug) -- Gitee From d2b9f84f2aed1edfa08cb98ff3dd8f0eb27207cf Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Thu, 29 Feb 2024 04:15:50 -0500 Subject: [PATCH 16/77] Fix add postfix: resolve getting emutls symbols. Change-Id: I20bda9ef4ef71a5a7570580594941e9141eeb7d7 Signed-off-by: Anton Volkov --- lld/ELF/InputFiles.cpp | 8 ++++++-- lld/ELF/Relocations.cpp | 10 +++++----- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp index 56e6507ace6b..904efda64fd4 100644 --- a/lld/ELF/InputFiles.cpp +++ b/lld/ELF/InputFiles.cpp @@ -1869,7 +1869,7 @@ Symbol &SharedFileExtended::getSymbolADLT(uint32_t symbolIndex, if (name.empty()) return sym; - /*if (name.contains("_ZdlPv")) // debug hint + /*if (name.contains("__emutls_v.TLS_data1")) // debug hint lld::outs() << "debug getSymbolADLT(): " << name << "\n";*/ // check SymbolTable @@ -2087,7 +2087,11 @@ 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("__")) + auto dynSym = llvm::find_if(this->symbols, [=](const Symbol *s) { + return s && s->getName() == name; + }); + bool isInDynSym = dynSym != this->symbols.end(); + if (!isInDynSym) name = addAdltPostfix(name); this->allSymbols[i] = make(this, name, bind, other, type, val, eSym.st_size, sec); diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp index d5d52e4d99c8..794b7aa2c553 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -1377,8 +1377,10 @@ void RelocationScanner::processForADLT(const RelTy &rel, Relocation *r) { Defined *symWhere = file->findDefinedSymbol(r->offset, failTitle); file->saveSymbol(*symWhere); - /*if (r->offset == 0x23dc) // debug hint - isDebug = true; */ + /*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!"; @@ -1437,10 +1439,8 @@ void RelocationScanner::processForADLT(const RelTy &rel, Relocation *r) { // got relocs case R_AARCH64_ADR_GOT_PAGE: if (r->sym->isDefined() && !r->sym->needsGot) { - if (isDebug) { + if (isDebug) lld::outs() << "[ADLT] R_AARCH64_ADR_GOT_PAGE: sym not in GOT! "; - file->traceSymbol(*r->sym); - } r->expr = R_PC; // prev: R_AARCH64_GOT_PAGE_PC || R_AARCH64_GOT_PAGE || // R_GOT || R_GOT_PC } -- Gitee From f09de14a36adaccb2cca7ed27f8bd7cccfff35c9 Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Tue, 5 Mar 2024 09:28:08 -0500 Subject: [PATCH 17/77] Debug musl: no optimize. Fix trailing whitespaces. Change-Id: Ibe1fe4c9c49c27a48e9349a6803f7f15814871cc Signed-off-by: Anton Volkov --- llvm-build/build.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/llvm-build/build.py b/llvm-build/build.py index 5cdb470d00b1..432ae4d17a8f 100755 --- a/llvm-build/build.py +++ b/llvm-build/build.py @@ -288,7 +288,7 @@ class BuildConfig(): action='store_true', default=False, help='Enable lldb performance monitoring') - + parser.add_argument( '--adlt-debug-build', action='store_true', @@ -2301,11 +2301,11 @@ class LlvmLibs(BuildUtils): self.copy_gtest_to_sysroot(gtest_build_path) shutil.copytree( os.path.join(self.build_config.LLVM_PROJECT_DIR, 'llvm', 'utils', 'unittest', 'googlemock', 'include', 'gmock'), - os.path.join(llvm_install, 'include', 'gmock'), + os.path.join(llvm_install, 'include', 'gmock'), dirs_exist_ok = True) shutil.copytree( os.path.join(self.build_config.LLVM_PROJECT_DIR, 'llvm', 'utils', 'unittest', 'googletest', 'include', 'gtest'), - os.path.join(llvm_install, 'include', 'gtest'), + os.path.join(llvm_install, 'include', 'gtest'), dirs_exist_ok = True) def build_libxml2_defines(self): -- Gitee From 4c59f80e4d3f0a590d2a5c8d260b6b5effe74010 Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Wed, 6 Mar 2024 08:48:02 -0500 Subject: [PATCH 18/77] 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 904efda64fd4..dfafc64795ff 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 e0b164136dc696957cabd60cf3c055d2f192969f Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Wed, 6 Mar 2024 08:57:29 -0500 Subject: [PATCH 19/77] 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 794b7aa2c553..03e605668b8e 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -464,11 +464,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 @@ -1318,6 +1318,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: "; @@ -1326,20 +1327,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); } @@ -1366,36 +1357,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 @@ -1404,7 +1398,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; @@ -1496,7 +1490,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 f757b5e9b202135d304310735dd98c2cf4a0dbdb Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Wed, 13 Mar 2024 03:59:30 -0400 Subject: [PATCH 20/77] 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 03e605668b8e..4e5a385d4574 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -1382,14 +1382,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 @@ -1401,17 +1393,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: @@ -1443,7 +1445,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 From 7fc160aef70f036086c5b985680f494b6e7a998b Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Fri, 15 Mar 2024 05:09:19 -0400 Subject: [PATCH 21/77] [ADLT] TLS support. Fix search secWhere. Minor changes. Change-Id: I3b03341bd1d8461b727d2f541507caf7e5ad77f7 Signed-off-by: Anton Volkov --- lld/ELF/Arch/AArch64.cpp | 19 +++---- lld/ELF/InputFiles.cpp | 21 +++++--- lld/ELF/InputSection.cpp | 2 + lld/ELF/Relocations.cpp | 107 +++++++++++++++++++-------------------- 4 files changed, 74 insertions(+), 75 deletions(-) diff --git a/lld/ELF/Arch/AArch64.cpp b/lld/ELF/Arch/AArch64.cpp index 5a0c9963d5a6..810ff3f5d9d9 100644 --- a/lld/ELF/Arch/AArch64.cpp +++ b/lld/ELF/Arch/AArch64.cpp @@ -87,6 +87,10 @@ RelExpr AArch64::getRelExpr(RelType type, const Symbol &s, case R_AARCH64_JUMP_SLOT: case R_AARCH64_RELATIVE: return R_ABS; + case R_AARCH64_TLSDESC: + return R_TLSDESC; + case R_AARCH64_TLS_TPREL64: + return R_TPREL; default: break; } @@ -382,15 +386,7 @@ void AArch64::deRelocate(uint8_t *loc, const Relocation &rel, switch (rel.type) { case R_AARCH64_JUMP26: - case R_AARCH64_CALL26: { - /*if (*val & (1UL << 32)) { - if (isDebug) - debugMessage("found 32 bit turned on! Disabling"); - *val = *val & ~(1UL << 32); - }*/ - orClear32le(); - break; - } + case R_AARCH64_CALL26: case R_AARCH64_CONDBR19: case R_AARCH64_LD_PREL_LO19: case R_AARCH64_MOVW_UABS_G0_NC: @@ -407,10 +403,7 @@ void AArch64::deRelocate(uint8_t *loc, const Relocation &rel, case R_AARCH64_TLSLE_LDST16_TPREL_LO12_NC: case R_AARCH64_LDST32_ABS_LO12_NC: case R_AARCH64_TLSLE_LDST32_TPREL_LO12_NC: - case R_AARCH64_LDST64_ABS_LO12_NC: { - orClearAArch64Imm(); - break; - } + case R_AARCH64_LDST64_ABS_LO12_NC: case R_AARCH64_ADR_GOT_PAGE: case R_AARCH64_LD64_GOT_LO12_NC: case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC: diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp index dfafc64795ff..2f39f0ef9697 100644 --- a/lld/ELF/InputFiles.cpp +++ b/lld/ELF/InputFiles.cpp @@ -1076,7 +1076,8 @@ void ObjFile::initializeSymbols(const object::ELFFile &obj) { fatal("getSection failed: " + llvm::toString(p.takeError())); } const Elf_Shdr *eSec = *p; - value -= eSec->sh_addr; + if (type != STT_TLS) + value -= eSec->sh_addr; } uint64_t size = eSym.st_size; @@ -1715,6 +1716,8 @@ bool SharedFileExtended::saveSymbol(const Defined& d) const { template Defined *SharedFileExtended::findSectionSymbol(uint64_t offset) const { bool isDebug = false; + /*if (offset == 0x7738) // debug hint + isDebug = true;*/ auto predRange = [=](Symbol *sym) { if (!sym || sym->isUndefined() || !sym->isSection()) return false; @@ -1724,16 +1727,14 @@ Defined *SharedFileExtended::findSectionSymbol(uint64_t offset) const { if (isDebug) lld::outs() << "offset: 0x" + utohexstr(offset) + - "sect name: " + d->section->name + "low 0x" + + " sect name: " + d->section->name + "low 0x" + utohexstr(low) + " high: 0x" + - utohexstr(offset) + "\n"; + utohexstr(high) + "\n"; return (offset >= low) && (offset < high); }; 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; @@ -1768,6 +1769,8 @@ Defined *SharedFileExtended::findDefinedSymbol( uint64_t offset, StringRef fatalTitle, llvm::function_ref extraCond) const { bool isDebug = false; + /*if (offset == 0x7738) // debug hint + isDebug = true;*/ auto predRange = [=](Symbol *sym) { if (!sym || sym->isUndefined()) return false; @@ -1782,8 +1785,6 @@ Defined *SharedFileExtended::findDefinedSymbol( 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); @@ -2090,13 +2091,17 @@ template void SharedFileExtended::parseElfSymTab() { } const Elf_Shdr *eSec = *p; InputSectionBase *sec = this->sections[secIdx]; - auto val = eSym.st_value - eSec->sh_addr; + auto val = eSym.st_value; + if (type != STT_TLS) + val -= eSec->sh_addr; auto dynSym = llvm::find_if(this->symbols, [=](const Symbol *s) { return s && s->getName() == name; }); bool isInDynSym = dynSym != this->symbols.end(); if (!isInDynSym) name = addAdltPostfix(name); + /*if (name == "TLS_data1") // debug hint + lld::outs() << name << '\n'; */ this->allSymbols[i] = make(this, name, bind, other, type, val, eSym.st_size, sec); } else { diff --git a/lld/ELF/InputSection.cpp b/lld/ELF/InputSection.cpp index 7b9ed5ad0574..ef02393f0ec6 100644 --- a/lld/ELF/InputSection.cpp +++ b/lld/ELF/InputSection.cpp @@ -1019,6 +1019,8 @@ void InputSectionBase::relocateAlloc(uint8_t *buf, uint8_t *bufEnd) { if (auto *sec = dyn_cast(this)) secAddr += sec->outSecOff; const uint64_t addrLoc = secAddr + offset; + /*if (config->adlt && addrLoc == 68600) // debug hint + lld::outs() << "addrLoc 0x" << utohexstr(addrLoc) << "\n";*/ const uint64_t targetVA = SignExtend64(getRelocTargetVA(file, rel.type, rel.addend, addrLoc, *rel.sym, rel.expr), diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp index 4e5a385d4574..61f8d8108f08 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -111,6 +111,11 @@ void elf::reportRangeError(uint8_t *loc, const Relocation &rel, const Twine &v, hint += "; consider recompiling with -fdebug-types-section to reduce size " "of debug sections"; + if (config->adlt) { + auto offset = errPlace.isec->address + rel.offset; // debug hint: put bkpt here + hint += " [ADLT] Offset: 0x" + utohexstr(offset) + "\n"; + } + errorOrWarn(errPlace.loc + "relocation " + lld::toString(rel.type) + " out of range: " + v.str() + " is not in [" + Twine(min).str() + ", " + Twine(max).str() + "]" + hint); @@ -466,13 +471,8 @@ private: template void processForADLT(const RelTy &rel, Relocation *r, bool fromDynamic); - template - void addRelativeRelocForAdlt(const RelTy &rel, Defined &symWhere, - uint64_t offset, Defined &inputSym) const; - void pushRelocAdlt(Relocation *r) const; - template - void tracePushRelocADLT(Defined &symWhere, Relocation &r) const; + void tracePushRelocADLT(InputSectionBase &isec, Relocation &r) const; }; } // namespace @@ -1315,41 +1315,18 @@ static unsigned handleTlsRelocation(RelType type, Symbol &sym, } // ADLT BEGIN -template -void RelocationScanner::addRelativeRelocForAdlt(const RelTy &rel, - Defined &symWhere, - uint64_t offset, - Defined &inputSym) const { - // auto file = sec.getSharedFile(); - std::string title = "addRelativeRelocForAdlt: "; - std::string failTitle = ""; - - RelType type = R_AARCH64_ABS64; - RelExpr expr = R_ABS; - int64_t addend = computeAddend(rel, expr, false); - addend -= inputSym.section->address + inputSym.value; - - // finally - addRelativeReloc(*cast(symWhere.section), offset, - inputSym, addend, expr, type); -} - -void RelocationScanner::pushRelocAdlt(Relocation *r) const { - sec.relocations.push_back(*r); -} - template -void RelocationScanner::tracePushRelocADLT(Defined &symWhere, +void RelocationScanner::tracePushRelocADLT(InputSectionBase &isec, Relocation &r) const { auto file = sec.getSharedFile(); - auto fullOffset = symWhere.section->address + r.offset; - lld::outs() << "[ADLT] Before push: [" + utohexstr(fullOffset) + + auto fullOffset = isec.address + r.offset; + lld::outs() << "[ADLT] Before push: [0x" + utohexstr(fullOffset) + "] type: " + toString(r.type) + - " expr: " + toString(r.expr) + " offset: 0x" + + " expr: " + std::to_string(r.expr) + " offset: 0x" + utohexstr(r.offset) + " addend: 0x" + utohexstr(r.addend) + ".\n"; - lld::outs() << "symWhere: "; - file->traceSymbol(symWhere); + lld::outs() << "section where: "; + file->traceSection(isec); lld::outs() << "r->sym: "; file->traceSymbol(*r.sym); @@ -1365,33 +1342,39 @@ void RelocationScanner::processForADLT(const RelTy &rel, Relocation *r, "processForADLT: symIndex: " + std::to_string(symIndex) + " "; bool isDebug = false; - /*if (r->offset == 0x296) // debug hint + /*if (r->offset == 0x21F) // debug hint isDebug = true; - if (r->sym->getName() == "__emutls_t.TLS_data1") + /*if (r->type == R_AARCH64_ABS64 && + 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); + InputSectionBase *secWhere = cast( + fromDynamic + ? file->findDefinedSymbol(r->offset, failTitle)->section + : (r->sym->isDefined() ? cast(r->sym)->section : &sec)); // process offset - r->offset -= fromDynamic ? symWhere->section->address : sec.address; + r->offset -= fromDynamic ? secWhere->address : sec.address; + assert(r->type); if (isDebug) - tracePushRelocADLT(*symWhere, *r); + tracePushRelocADLT(*secWhere, *r); // resolve relocs switch (r->type) { // dyn relocs - case R_AARCH64_RELATIVE: + case R_AARCH64_RELATIVE: { failTitle = 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, r->offset, *d); + Defined *d = file->findDefinedSymbol(r->addend, failTitle); + assert(d); + r->addend -= d->section->address + d->value; + addRelativeReloc(*secWhere, r->offset, *d, r->addend, r->expr, + r->type); return; + } case R_AARCH64_GLOB_DAT: assert(r->sym->exportDynamic); r->sym->needsGot = 1; @@ -1405,15 +1388,18 @@ void RelocationScanner::processForADLT(const RelTy &rel, Relocation *r, return; // abs relocs case R_AARCH64_ABS32: + sec.relocations.push_back(*r); + return; 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); + sec.getPartition().relaDyn->addSymbolReloc(target.symbolicRel, *secWhere, + r->offset, *r->sym, r->addend, + r->type); return; } - LLVM_FALLTHROUGH; + sec.relocations.push_back(*r); + return; case R_AARCH64_ADD_ABS_LO12_NC: case R_AARCH64_ADR_PREL_PG_HI21: case R_AARCH64_LDST8_ABS_LO12_NC: @@ -1423,14 +1409,14 @@ void RelocationScanner::processForADLT(const RelTy &rel, Relocation *r, case R_AARCH64_LDST128_ABS_LO12_NC: case R_AARCH64_PREL32: case R_AARCH64_PREL64: - pushRelocAdlt(r); + processAux(r->expr, r->type, r->offset, *r->sym, r->addend); return; // plt relocs case R_AARCH64_CALL26: case R_AARCH64_JUMP26: if (r->sym->isDefined()) r->expr = R_PC; // prev: R_PLT_PC - pushRelocAdlt(r); + sec.relocations.push_back(*r); return; // got relocs case R_AARCH64_ADR_GOT_PAGE: @@ -1442,10 +1428,23 @@ void RelocationScanner::processForADLT(const RelTy &rel, Relocation *r, } LLVM_FALLTHROUGH; case R_AARCH64_LD64_GOT_LO12_NC: - pushRelocAdlt(r); + processAux(r->expr, r->type, r->offset, *r->sym, r->addend); + return; + // tls relocs + case R_AARCH64_TLSDESC: + case R_AARCH64_TLSDESC_CALL: + case R_AARCH64_TLS_TPREL64: + case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21: + case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC: + case R_AARCH64_TLSDESC_ADR_PAGE21: + case R_AARCH64_TLSDESC_LD64_LO12: + case R_AARCH64_TLSDESC_ADD_LO12: + case R_AARCH64_TLSLE_ADD_TPREL_HI12: + case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC: + handleTlsRelocation(r->type, *r->sym, sec, r->offset, r->addend, r->expr); return; default: - fatal("Unhandled " + toString(fromDynamic ? "dynamic" : "") + + fatal("[ADLT] Unhandled " + toString(fromDynamic ? "dynamic " : "") + "reloc: " + toString(r->type)); break; } -- Gitee From e2eb560adffefbfa3da3d8f78d3ddc9268bf3d38 Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Thu, 21 Mar 2024 20:24:29 +0800 Subject: [PATCH 22/77] Disable adlt activation print. Change-Id: I6744fd00cf5da84076bdfada8df28d76170f8533 Signed-off-by: Anton Volkov --- lld/ELF/DriverUtils.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lld/ELF/DriverUtils.cpp b/lld/ELF/DriverUtils.cpp index db42b6a36028..0145d9b9ba0c 100644 --- a/lld/ELF/DriverUtils.cpp +++ b/lld/ELF/DriverUtils.cpp @@ -68,7 +68,7 @@ static void handleAdltOption(opt::InputArgList &args) { auto *arg = args.getLastArg(OPT_adlt); if (!arg) return; - lld::outs() << "adlt is active\n"; + // slld::outs() << "adlt is active\n"; } static cl::TokenizerCallback getQuotingStyle(opt::InputArgList &args) { -- Gitee From a9670ae25172d5eaad1762c03edfb8c8415ee1a3 Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Thu, 21 Mar 2024 21:59:46 +0800 Subject: [PATCH 23/77] [ADLT] Fix duplicated DT_NEEDED. Change-Id: I6c415d656a34220501ba3eff1413bcd390d3e8a5 Signed-off-by: Anton Volkov --- lld/ELF/SyntheticSections.cpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp index 29135ddb16d7..8bbfb06d0fe2 100644 --- a/lld/ELF/SyntheticSections.cpp +++ b/lld/ELF/SyntheticSections.cpp @@ -1398,15 +1398,14 @@ DynamicSection::computeContents() { part.dynStrTab->addString(config->rpath)); if (config->adlt) { - StringRef prevNeeded; for (InputFile *file : ctx->sharedFilesExtended) { auto *f = cast>(file); for (size_t i = 0; i < f->dtNeeded.size(); i++) { - StringRef needed = f->dtNeeded[i]; - if (prevNeeded == needed) - continue; - addInt(DT_NEEDED, part.dynStrTab->addString(needed)); - prevNeeded = needed; + auto tag = DT_NEEDED; + auto val = part.dynStrTab->addString(f->dtNeeded[i]); + if (llvm::find(entries, std::pair{tag, val}) == + entries.end()) + addInt(tag, val); } } addInt(DT_SONAME, part.dynStrTab->addString(config->outputFile)); -- Gitee From ec4e13cdb4425d325864b34cf9ecb8938ae287f2 Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Thu, 21 Mar 2024 22:02:36 +0800 Subject: [PATCH 24/77] [ADLT] Fix set export dynamic. Change-Id: Ib50f2c8bdb7a41fe091b7507ab918a494b5d1bd9 Signed-off-by: Anton Volkov --- lld/ELF/InputFiles.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp index 2f39f0ef9697..d3108ea34694 100644 --- a/lld/ELF/InputFiles.cpp +++ b/lld/ELF/InputFiles.cpp @@ -1083,6 +1083,8 @@ void ObjFile::initializeSymbols(const object::ELFFile &obj) { Symbol *sym = symbols[i]; sym->isUsedInRegularObj = true; + if (config->adlt) + sym->exportDynamic = true; if (LLVM_UNLIKELY(eSym.st_shndx == SHN_COMMON)) { if (value == 0 || value >= UINT32_MAX) fatal(toString(this) + ": common symbol '" + sym->getName() + @@ -1111,6 +1113,8 @@ void ObjFile::initializeSymbols(const object::ELFFile &obj) { eSym.getType()}); sym->isUsedInRegularObj = true; sym->referenced = true; + if (config->adlt) + sym->exportDynamic = true; } } -- Gitee From 62850e31e2698cf8c9c827a03743b6a15172537f Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Thu, 21 Mar 2024 23:03:22 +0800 Subject: [PATCH 25/77] [ADLT] Fix push reloc after PLT opt Change-Id: I2b7c07be3b11089f4d030b97a169c306e23f4fc7 Signed-off-by: Anton Volkov --- lld/ELF/Relocations.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp index 61f8d8108f08..b31e637ab532 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -1425,6 +1425,9 @@ void RelocationScanner::processForADLT(const RelTy &rel, Relocation *r, lld::outs() << "[ADLT] R_AARCH64_ADR_GOT_PAGE: sym not in GOT! "; r->expr = R_PC; // prev: R_AARCH64_GOT_PAGE_PC || R_AARCH64_GOT_PAGE || // R_GOT || R_GOT_PC + // TODO: replace reloc R_AARCH64_ADR_GOT_PAGE + sec.relocations.push_back(*r); + return; } LLVM_FALLTHROUGH; case R_AARCH64_LD64_GOT_LO12_NC: -- Gitee From a133b237a77dfb24d5e5de25dddcb9980fa4e575 Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Wed, 27 Mar 2024 05:45:56 -0400 Subject: [PATCH 26/77] Minor adlt changes. Change-Id: I66551650d6f8a76ad038d68b08899e8a08530c3c Signed-off-by: Anton Volkov --- lld/ELF/InputFiles.cpp | 14 +++++++------- lld/ELF/Relocations.cpp | 2 -- lld/ELF/Writer.cpp | 2 +- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp index d3108ea34694..c66bf1e5ce43 100644 --- a/lld/ELF/InputFiles.cpp +++ b/lld/ELF/InputFiles.cpp @@ -145,13 +145,13 @@ static bool isCompatible(InputFile *file) { } InputFile *existing = nullptr; - if (!ctx->objectFiles.empty()) - existing = ctx->objectFiles[0]; - else if (!ctx->sharedFiles.empty()) - existing = ctx->sharedFiles[0]; - else if (!ctx->bitcodeFiles.empty()) - existing = ctx->bitcodeFiles[0]; - std::string with; + if (!ctx->objectFiles.empty()) + existing = ctx->objectFiles[0]; + else if (!ctx->sharedFiles.empty()) + existing = ctx->sharedFiles[0]; + else if (!ctx->bitcodeFiles.empty()) + existing = ctx->bitcodeFiles[0]; + std::string with; if (existing) with = " with " + toString(existing); error(toString(file) + " is incompatible" + with); diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp index b31e637ab532..0d0a4b87662b 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -2350,8 +2350,6 @@ bool ThunkCreator::createThunks(uint32_t pass, // original target so another Thunk can be generated. if (pass > 0 && normalizeExistingThunk(rel, src)) continue; - if (!rel.sym) - continue; // ADLT fix if (!target->needsThunk(rel.expr, rel.type, isec->file, src, *rel.sym, rel.addend)) continue; diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index de16dec8e609..51dee08eb6ad 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -1955,7 +1955,7 @@ template void Writer::finalizeSections() { if (sec && sec->isLive()) scanRelocations(*sec); } - if (!config->adlt) + else for (InputSectionBase *sec : inputSections) if (sec->isLive() && isa(sec) && (sec->flags & SHF_ALLOC)) scanRelocations(*sec); -- Gitee From 93c2b88eb23265e9246afeb78cddbbcab95a2fee Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Wed, 27 Mar 2024 07:24:04 -0400 Subject: [PATCH 27/77] [ADLT] Fix copyRelocations. Change-Id: I6ad3fb48ecfd577effc6e3bc8f28ada540debb32 Signed-off-by: Anton Volkov --- lld/ELF/InputFiles.cpp | 5 +++++ lld/ELF/InputFiles.h | 7 +++++++ lld/ELF/InputSection.cpp | 10 ++++++---- lld/ELF/Relocations.cpp | 2 +- 4 files changed, 19 insertions(+), 5 deletions(-) diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp index c66bf1e5ce43..ec5732fac568 100644 --- a/lld/ELF/InputFiles.cpp +++ b/lld/ELF/InputFiles.cpp @@ -1768,6 +1768,11 @@ SharedFileExtended::findInputSection(uint64_t offset) const { return nullptr; } +template +bool SharedFileExtended::isDynamicSection(InputSectionBase &sec) const { + return sec.type == llvm::ELF::SHT_NULL || sec.name.startswith(".got.plt"); +} + template Defined *SharedFileExtended::findDefinedSymbol( uint64_t offset, StringRef fatalTitle, diff --git a/lld/ELF/InputFiles.h b/lld/ELF/InputFiles.h index bc3d594189ee..da44677d5e6d 100644 --- a/lld/ELF/InputFiles.h +++ b/lld/ELF/InputFiles.h @@ -414,6 +414,13 @@ public: InputSectionBase *findInputSection(StringRef name) const; InputSectionBase *findInputSection(uint64_t offset) const; + bool isDynamicSection(InputSectionBase &sec) const; + + template + Symbol &getRelocTargetSymADLT(const RelT &rel, InputSectionBase &sec) const { + uint32_t symIndex = rel.getSymbol(config->isMips64EL); + return getSymbolADLT(symIndex, isDynamicSection(sec)); + } ArrayRef getLocalSymbols() override { if (this->allSymbols.empty()) diff --git a/lld/ELF/InputSection.cpp b/lld/ELF/InputSection.cpp index ef02393f0ec6..9489965a7fd1 100644 --- a/lld/ELF/InputSection.cpp +++ b/lld/ELF/InputSection.cpp @@ -347,9 +347,11 @@ void InputSection::copyRelocations(uint8_t *buf, ArrayRef rels) { for (const RelTy &rel : rels) { RelType type = rel.getType(config->isMips64EL); - const ELFFileBase *file = cast_or_null(file); - Symbol &sym = config->adlt ? getSharedFile()->getRelocTargetSym(rel) - : getFile()->getRelocTargetSym(rel); + const ObjFile *file = + config->adlt ? getSharedFile() : getFile(); + Symbol &sym = config->adlt + ? getSharedFile()->getRelocTargetSymADLT(rel, *sec) + : file->getRelocTargetSym(rel); auto *p = reinterpret_cast(buf); buf += sizeof(RelTy); @@ -384,7 +386,7 @@ void InputSection::copyRelocations(uint8_t *buf, ArrayRef rels) { uint32_t secIdx = cast(sym).discardedSecIdx; Elf_Shdr_Impl sec = file->template getELFShdrs()[secIdx]; warn("relocation refers to a discarded section: " + - CHECK(file->getObj().getSectionName(sec), file) + + CHECK(file->getObj().getSectionName(sec), file) + "\n>>> referenced by " + getObjMsg(p->r_offset)); } p->setSymbolAndType(0, 0, false); diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp index 0d0a4b87662b..5ccbe3d7cbf5 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -1457,7 +1457,7 @@ void RelocationScanner::processForADLT(const RelTy &rel, Relocation *r, template void RelocationScanner::scanOne(RelTy *&i) { const RelTy &rel = *i; uint32_t symIndex = rel.getSymbol(config->isMips64EL); - bool fromDynamic = sec.type == SHT_NULL || sec.name.startswith(".got.plt"); + bool fromDynamic = sec.getSharedFile()->isDynamicSection(sec); Symbol &sym = config->adlt ? sec.getSharedFile()->getSymbolADLT(symIndex, fromDynamic) -- Gitee From 3c7c1fd93fabaa7de8abedb31af2ef327e06c50e Mon Sep 17 00:00:00 2001 From: Pavel Kosov Date: Sat, 30 Mar 2024 02:09:38 +0000 Subject: [PATCH 28/77] Revert 'Pull Request !426 : [ADLT] Fix copyRelocations' Signed-off-by: Pavel Kosov --- lld/ELF/InputFiles.cpp | 19 +++++++------------ lld/ELF/InputFiles.h | 7 ------- lld/ELF/InputSection.cpp | 10 ++++------ lld/ELF/Relocations.cpp | 4 +++- lld/ELF/Writer.cpp | 2 +- 5 files changed, 15 insertions(+), 27 deletions(-) diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp index ec5732fac568..d3108ea34694 100644 --- a/lld/ELF/InputFiles.cpp +++ b/lld/ELF/InputFiles.cpp @@ -145,13 +145,13 @@ static bool isCompatible(InputFile *file) { } InputFile *existing = nullptr; - if (!ctx->objectFiles.empty()) - existing = ctx->objectFiles[0]; - else if (!ctx->sharedFiles.empty()) - existing = ctx->sharedFiles[0]; - else if (!ctx->bitcodeFiles.empty()) - existing = ctx->bitcodeFiles[0]; - std::string with; + if (!ctx->objectFiles.empty()) + existing = ctx->objectFiles[0]; + else if (!ctx->sharedFiles.empty()) + existing = ctx->sharedFiles[0]; + else if (!ctx->bitcodeFiles.empty()) + existing = ctx->bitcodeFiles[0]; + std::string with; if (existing) with = " with " + toString(existing); error(toString(file) + " is incompatible" + with); @@ -1768,11 +1768,6 @@ SharedFileExtended::findInputSection(uint64_t offset) const { return nullptr; } -template -bool SharedFileExtended::isDynamicSection(InputSectionBase &sec) const { - return sec.type == llvm::ELF::SHT_NULL || sec.name.startswith(".got.plt"); -} - template Defined *SharedFileExtended::findDefinedSymbol( uint64_t offset, StringRef fatalTitle, diff --git a/lld/ELF/InputFiles.h b/lld/ELF/InputFiles.h index da44677d5e6d..bc3d594189ee 100644 --- a/lld/ELF/InputFiles.h +++ b/lld/ELF/InputFiles.h @@ -414,13 +414,6 @@ public: InputSectionBase *findInputSection(StringRef name) const; InputSectionBase *findInputSection(uint64_t offset) const; - bool isDynamicSection(InputSectionBase &sec) const; - - template - Symbol &getRelocTargetSymADLT(const RelT &rel, InputSectionBase &sec) const { - uint32_t symIndex = rel.getSymbol(config->isMips64EL); - return getSymbolADLT(symIndex, isDynamicSection(sec)); - } ArrayRef getLocalSymbols() override { if (this->allSymbols.empty()) diff --git a/lld/ELF/InputSection.cpp b/lld/ELF/InputSection.cpp index 9489965a7fd1..ef02393f0ec6 100644 --- a/lld/ELF/InputSection.cpp +++ b/lld/ELF/InputSection.cpp @@ -347,11 +347,9 @@ void InputSection::copyRelocations(uint8_t *buf, ArrayRef rels) { for (const RelTy &rel : rels) { RelType type = rel.getType(config->isMips64EL); - const ObjFile *file = - config->adlt ? getSharedFile() : getFile(); - Symbol &sym = config->adlt - ? getSharedFile()->getRelocTargetSymADLT(rel, *sec) - : file->getRelocTargetSym(rel); + const ELFFileBase *file = cast_or_null(file); + Symbol &sym = config->adlt ? getSharedFile()->getRelocTargetSym(rel) + : getFile()->getRelocTargetSym(rel); auto *p = reinterpret_cast(buf); buf += sizeof(RelTy); @@ -386,7 +384,7 @@ void InputSection::copyRelocations(uint8_t *buf, ArrayRef rels) { uint32_t secIdx = cast(sym).discardedSecIdx; Elf_Shdr_Impl sec = file->template getELFShdrs()[secIdx]; warn("relocation refers to a discarded section: " + - CHECK(file->getObj().getSectionName(sec), file) + + CHECK(file->getObj().getSectionName(sec), file) + "\n>>> referenced by " + getObjMsg(p->r_offset)); } p->setSymbolAndType(0, 0, false); diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp index 5ccbe3d7cbf5..b31e637ab532 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -1457,7 +1457,7 @@ void RelocationScanner::processForADLT(const RelTy &rel, Relocation *r, template void RelocationScanner::scanOne(RelTy *&i) { const RelTy &rel = *i; uint32_t symIndex = rel.getSymbol(config->isMips64EL); - bool fromDynamic = sec.getSharedFile()->isDynamicSection(sec); + bool fromDynamic = sec.type == SHT_NULL || sec.name.startswith(".got.plt"); Symbol &sym = config->adlt ? sec.getSharedFile()->getSymbolADLT(symIndex, fromDynamic) @@ -2350,6 +2350,8 @@ bool ThunkCreator::createThunks(uint32_t pass, // original target so another Thunk can be generated. if (pass > 0 && normalizeExistingThunk(rel, src)) continue; + if (!rel.sym) + continue; // ADLT fix if (!target->needsThunk(rel.expr, rel.type, isec->file, src, *rel.sym, rel.addend)) continue; diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index 51dee08eb6ad..de16dec8e609 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -1955,7 +1955,7 @@ template void Writer::finalizeSections() { if (sec && sec->isLive()) scanRelocations(*sec); } - else + if (!config->adlt) for (InputSectionBase *sec : inputSections) if (sec->isLive() && isa(sec) && (sec->flags & SHF_ALLOC)) scanRelocations(*sec); -- Gitee From a46899206ae7c61ef213108d4f78f321d6afcfc2 Mon Sep 17 00:00:00 2001 From: Kaipov Rustam Date: Thu, 28 Mar 2024 09:17:27 -0400 Subject: [PATCH 29/77] [lld][ADLT] Improve find section algorithm for debug builds Signed-off-by: Kaipov Rustam --- lld/ELF/InputFiles.cpp | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp index d3108ea34694..40361326fe0c 100644 --- a/lld/ELF/InputFiles.cpp +++ b/lld/ELF/InputFiles.cpp @@ -1722,31 +1722,37 @@ Defined *SharedFileExtended::findSectionSymbol(uint64_t offset) const { bool isDebug = false; /*if (offset == 0x7738) // debug hint isDebug = true;*/ - auto predRange = [=](Symbol *sym) { - if (!sym || sym->isUndefined() || !sym->isSection()) - return false; - const Defined *d = cast(sym); + llvm::SmallVector candidates; + for (auto *sym : this->allSymbols) { + if (!sym || !sym->isSection()) + continue; + assert(sym->isDefined()); + Defined *d = cast(sym); uint64_t low = d->section->address; uint64_t high = low + d->section->size; if (isDebug) lld::outs() << "offset: 0x" + utohexstr(offset) + " sect name: " + d->section->name + "low 0x" + - utohexstr(low) + " high: 0x" + - utohexstr(high) + "\n"; - return (offset >= low) && (offset < high); - }; - - auto i = this->allSymbols.begin(); - auto e = this->allSymbols.end(); - auto ret = std::find_if(i, e, predRange); - if (ret == e) // item was not found + utohexstr(low) + " high: 0x" + utohexstr(high) + "\n"; + bool isGood = (offset >= low) && (offset < high); + if (!isGood) + continue; + candidates.push_back(d); + } + if (candidates.empty()) // no suitable items found return nullptr; - auto d = cast(*ret); + if (candidates.size() > 1) + llvm::sort(candidates, [=](Defined *d1, Defined *d2) { + auto a1 = d1->section->address; + auto a2 = d2->section->address; + return (offset - a1 < offset - a2); + }); + + auto d = *candidates.begin(); if (isDebug) traceSymbol(*d, "found section sym: "); - return d; } -- Gitee From e29837fdf5e0e6584b7dc3c40bf428fe0623c5cf Mon Sep 17 00:00:00 2001 From: Kaipov Rustam Date: Mon, 1 Apr 2024 05:39:00 -0400 Subject: [PATCH 30/77] comments fixed Signed-off-by: Kaipov Rustam --- lld/ELF/InputFiles.cpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp index 40361326fe0c..b7fc40c53a0d 100644 --- a/lld/ELF/InputFiles.cpp +++ b/lld/ELF/InputFiles.cpp @@ -1743,12 +1743,11 @@ Defined *SharedFileExtended::findSectionSymbol(uint64_t offset) const { if (candidates.empty()) // no suitable items found return nullptr; - if (candidates.size() > 1) - llvm::sort(candidates, [=](Defined *d1, Defined *d2) { - auto a1 = d1->section->address; - auto a2 = d2->section->address; - return (offset - a1 < offset - a2); - }); + llvm::sort(candidates, [offset](Defined *d1, Defined *d2) { + auto a1 = d1->section->address; + auto a2 = d2->section->address; + return (offset - a1 < offset - a2); + }); auto d = *candidates.begin(); if (isDebug) -- Gitee From 0647085d6dc371e87754aeaeab0fb796954387da Mon Sep 17 00:00:00 2001 From: Khomutov Nikita Date: Mon, 1 Apr 2024 12:48:41 +0000 Subject: [PATCH 31/77] !431 [ADLT] .adlt section generation * review fixes: * review fix: * review fix: cover llvm_unreachable in the fallback swich case * review fix: * [ADLT] .adlt section generation Signed-off-by: Khomutov Nikita --- lld/ELF/InputFiles.cpp | 3 +- lld/ELF/InputFiles.h | 3 +- lld/ELF/SyntheticSections.cpp | 265 ++++++++++++++++++++++++++++++++++ lld/ELF/SyntheticSections.h | 146 +++++++++++++++++++ lld/ELF/Writer.cpp | 12 ++ 5 files changed, 427 insertions(+), 2 deletions(-) diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp index b7fc40c53a0d..d777209a2769 100644 --- a/lld/ELF/InputFiles.cpp +++ b/lld/ELF/InputFiles.cpp @@ -1693,8 +1693,9 @@ template void SharedFileExtended::postParseForAdlt() { resolveDuplicatesForAdlt(); } + template -StringRef SharedFileExtended::addAdltPostfix(StringRef input) { +StringRef SharedFileExtended::addAdltPostfix(StringRef input) const { return markItemForAdlt(input, soName); } diff --git a/lld/ELF/InputFiles.h b/lld/ELF/InputFiles.h index bc3d594189ee..68d76d5fb48e 100644 --- a/lld/ELF/InputFiles.h +++ b/lld/ELF/InputFiles.h @@ -399,7 +399,7 @@ public: void parseForAdlt(); void postParseForAdlt(); - StringRef addAdltPostfix(StringRef input); + StringRef addAdltPostfix(StringRef input) const; bool addAdltPostfix(Symbol *s); bool saveSymbol(const Defined& d) const; @@ -436,6 +436,7 @@ public: void traceSymbol(const Symbol &sym, StringRef title = "") const; void traceSection(const SectionBase &sec, StringRef title = "") const; +public: int dynSymSecIdx = 0; int symTabSecIdx = 0; int symTabShndxSecIdx = 0; diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp index 8bbfb06d0fe2..35f163c5d2ba 100644 --- a/lld/ELF/SyntheticSections.cpp +++ b/lld/ELF/SyntheticSections.cpp @@ -3949,6 +3949,8 @@ void InStruct::reset() { strTab.reset(); symTab.reset(); symTabShndx.reset(); + adltData.reset(); + adltStrTab.reset(); } constexpr char kMemtagAndroidNoteName[] = "Android"; @@ -3995,6 +3997,264 @@ size_t PackageMetadataNote::getSize() const { alignTo(config->packageMetadata.size() + 1, 4); } + +// OHOS_LOCAL begin +namespace lld { namespace elf { namespace adlt { + +template +AdltSection::AdltSection(StringTableSection& strTabSec) + : SyntheticSection(SHF_ALLOC, SHT_PROGBITS, 4, ".adlt") + , strTabSec(strTabSec) +{ + assert(config->adlt); +} + +template +void AdltSection::finalizeContents() { + soInputs.clear(); + soInputs.reserve(ctx->sharedFilesExtended.size()); + for (InputFile* file : ctx->sharedFilesExtended) { + auto* soext = cast>(file); + soInputs.push_back(makeSoData(soext)); + } + + assert((soInputs.size() < 1<<16) && + "the number of input libs exeeds ELF limit on number of sections"); + const Elf64_Half soNum = soInputs.size(); + + header = AdltSectionHeader{ + {1, 0, 0}, // .schemaVersion + sizeof(PSOD), // .schemaPSODSize + soNum, // .sharedObjectsNum + HashType::MUSL_GNU_HASH, // .stringHashType + getBlobStartOffset(), // .blobStart + estimateBlobSize(), // .blobSize + }; + + buildSonameIndex(); + linkInternalDtNeeded(); + extractInitFiniArray(); +} + +template +void AdltSection::buildSonameIndex() { + for (const auto& it : llvm::enumerate(soInputs)) { + const auto& soData = it.value(); + auto res = sonameToIndexMap.try_emplace( + CachedHashStringRef(soData.soName.ref), it.index()); + if (!res.second) { + warn(Twine(".adlt-section: duplicated soname: ") + soData.soName.ref + + " at pos=" + Twine(it.index()) + + " collided with pos=" + Twine(res.first->second)); + } + } +} + +template +void AdltSection::linkInternalDtNeeded() { + for (auto it : llvm::enumerate(soInputs)) { + auto& soData = it.value(); + for (auto& needed : soData.dtNeededs) { + auto cref = CachedHashStringRef(needed.str.ref); + auto res = sonameToIndexMap.find(cref); + if (res != sonameToIndexMap.end()) { + needed.psodIndex = res->second; + } + } + } +} + +template +OutputSection* AdltSection::findOutSection(StringRef name) { + if (name.empty()) return nullptr; + const unsigned partition = 1; + + for (SectionCommand* cmd : script->sectionCommands) { + if (auto* osd = dyn_cast(cmd)) + if (osd->osec.name == name && osd->osec.partition == partition) + return &osd->osec; + } + return nullptr; +} + +template +void AdltSection::extractInitFiniArray() { + for (auto& soData : soInputs) { + auto initArrayName = soData.initArrayName; + auto finiArrayName = soData.finiArrayName; + soData.initArraySec = findOutSection(initArrayName); + soData.finiArraySec = findOutSection(finiArrayName); + } +} + +template +typename AdltSection::SoData +AdltSection::makeSoData(const SharedFileExtended* soext) { + assert(soext); + SoData data = {}; + StringRef soname = soext->soName; + data.soName = SectionString{soname, strTabSec.addString(soname)}; + + for (const auto& neededName: soext->dtNeeded) { + data.dtNeededs.push_back({ + SectionString{neededName, strTabSec.addString(neededName)}, + {}, // internal PSOD index is uknown by the moment, will be linked + }); + } + + data.initArrayName = soext->addAdltPostfix(".init_array"); + data.finiArrayName = soext->addAdltPostfix(".fini_array"); + + // TODO: + // sharedLocalIndex + // sharedGlobalIndex + + // TODO: get section index for sharedLocalIndex + // TODO: get section index for sharedGlobalIndex + + return data; +} + +template +Elf64_Xword AdltSection::calculateHash(StringRef str) const { + switch(header.stringHashType) { + case HashType::NONE: + return 0x0; + case HashType::MUSL_GNU_HASH: + return hashGnu(str); + case HashType::DEBUG_CONST: + return 0xdeadbeef1337c0de; + default: + llvm_unreachable(".adlt hash type not implemented"); + } +} + +template +PSOD AdltSection::serialize(const SoData& soData) const { + return PSOD { + soData.soName.strtabOff, // .soName + calculateHash(soData.soName.ref), // .soNameHash + soData.initArraySec ? CrossSectionVec{ // .initArray + soData.initArraySec->sectionIndex, + soData.initArraySec->size / sizeof(Elf64_Addr), // size + soData.initArraySec->addr, + } : CrossSectionVec{0, 0, 0}, + soData.finiArraySec ? CrossSectionVec{ // .finiArray + soData.finiArraySec->sectionIndex, + soData.finiArraySec->size / sizeof(Elf64_Addr), // size + soData.finiArraySec->addr, + } : CrossSectionVec{0, 0, 0}, + 0x0, // .dtNeeded // filled by blob serialization + soData.dtNeededs.size(), // .dtNeededSz + CrossSectionRef { + 0, // .sectionIndex + soData.sharedLocalIndex, // .offsetFromStart + }, // .sharedLocalSymbolIndex + CrossSectionRef { + 0, // .sectionIndex + soData.sharedGlobalIndex, // .offsetFromStart + }, // .sharedGlobalSymbolIndex + }; +} + +template +Elf64_Off AdltSection::getBlobStartOffset() const { + return sizeof(header) + sizeof(PSOD) * soInputs.size(); +} + +template +size_t AdltSection::estimateBlobSize() const { + size_t blobSize = sizeof(BlobStartMark); + + for (const auto& soData: soInputs) { + blobSize += sizeof(DtNeededIndex) * soData.dtNeededs.size(); + }; + + return blobSize; +} + +template +size_t AdltSection::writeDtNeededVec( + uint8_t* buff, const DtNeededsVec& neededVec) const { + if (neededVec.empty()) return 0; + + SmallVector needIndexes; + needIndexes.reserve(neededVec.size()); + for (const auto& need_data : neededVec) { + needIndexes.push_back(DtNeededIndex{ + need_data.psodIndex.has_value(), // .hasInternalPSOD + need_data.psodIndex.value_or(0), // .PSODindex + need_data.str.strtabOff, // .sonameOffset + }); + } + + memcpy(buff, needIndexes.data(), needIndexes.size_in_bytes()); + return needIndexes.size_in_bytes(); +} + +template +void AdltSection::writeTo(uint8_t* buf) { + // TODO: take care of endianness, use write32 / write64 etc. + // pre-serialized SoData, enriched with offsets during blob writing + SmallVector psods; + + for (const auto& it: llvm::enumerate(soInputs)) { + const SoData& soData = it.value(); + PSOD psod = serialize(soData); + psods.push_back(psod); + } + + // serialize blob data + { + uint8_t* const blob_buf = buf + header.blobStart; + size_t blob_off = 0; + memcpy(blob_buf + blob_off, &BlobStartMark, sizeof(BlobStartMark)); + blob_off += sizeof(BlobStartMark); + + // dt-needed + for(const auto& it : llvm::enumerate(soInputs)) { + const auto& soData = it.value(); + PSOD& psod = psods[it.index()]; + + psod.dtNeededSz = soData.dtNeededs.size(); + psod.dtNeeded = blob_off; + blob_off += writeDtNeededVec(blob_buf + blob_off, soData.dtNeededs); + } + + // finalize header.blobSize + assert((blob_off <= header.blobSize) && + ".adlt-section: blob output exeeds its initial estimation"); + header.blobSize = blob_off; + } + + // header + { + memcpy(buf, &header, sizeof(header)); + } + + // PSODs + { + uint8_t* const psods_buf = buf + sizeof(header); + size_t psods_off = 0; + for (const auto& it: llvm::enumerate(soInputs)) { + PSOD& psod = psods[it.index()]; + memcpy(psods_buf + psods_off, &psod, sizeof(PSOD)); + psods_off += sizeof(PSOD); + } + } +} + +template +size_t AdltSection::getSize() const { + const size_t pre_blob_size = sizeof(header) + sizeof(PSOD) * soInputs.size(); + assert(pre_blob_size <= header.blobStart); + return header.blobStart + header.blobSize; +} + +}}} // namespace lld::elf::adlt +// OHOS_LOCAL end + + InStruct elf::in; std::vector elf::partitions; @@ -4083,3 +4343,8 @@ template class elf::PartitionProgramHeadersSection; template class elf::PartitionProgramHeadersSection; template class elf::PartitionProgramHeadersSection; template class elf::PartitionProgramHeadersSection; + +template class elf::adlt::AdltSection; +template class elf::adlt::AdltSection; +template class elf::adlt::AdltSection; +template class elf::adlt::AdltSection; diff --git a/lld/ELF/SyntheticSections.h b/lld/ELF/SyntheticSections.h index 83a2bd7d34df..48edc79a718d 100644 --- a/lld/ELF/SyntheticSections.h +++ b/lld/ELF/SyntheticSections.h @@ -34,6 +34,7 @@ namespace elf { class Defined; struct PhdrEntry; class SymbolTableBaseSection; +template class SharedFileExtended; class SyntheticSection : public InputSection { public: @@ -1220,6 +1221,149 @@ public: size_t getSize() const override; }; + +namespace adlt { + +using llvm::ELF::Elf64_Half; +using llvm::ELF::Elf64_Off; +using llvm::ELF::Elf64_Addr; +using llvm::ELF::Elf64_Word; +using llvm::ELF::Elf64_Xword; +using Elf64_Byte = uint8_t; + +struct CrossSectionRef { + Elf64_Word sectionIndex; + Elf64_Off offsetFromStart; +}; + +struct SemanticVersion { + Elf64_Half major: 6; + Elf64_Half minor: 6; + Elf64_Half patch: 4; +}; + +static_assert( + sizeof(SemanticVersion) == sizeof(Elf64_Half), + ".adlt semantic version is designed to occupy uint16_t" +); + +enum HashType : Elf64_Byte { + NONE = 0, + MUSL_GNU_HASH = 1, + MUSL_SYSV_HASH = 2, + MUSL_KEYHASH = 3, + DEBUG_CONST = 0xff, +}; + +struct DtNeededIndex { + bool hasInternalPSOD : 1; // true if soname + Elf64_Off PSODindex : 16; // PSOD in the current ADLT image + Elf64_Off sonameOffset : 47; // string start in bound strTabSec +}; + +static_assert( + sizeof(DtNeededIndex) == sizeof(Elf64_Off), + "DtNeededIndex have to be an offset with intrused flags" +); + +struct CrossSectionVec { + Elf64_Xword secIndex; + Elf64_Xword numElem; + Elf64_Addr addr; +}; + +// Serializable representation per-shared-object-data in .adlt section +struct PSOD { + Elf64_Off soName; // offset in strTabSec + Elf64_Xword soNameHash; + CrossSectionVec initArray; + CrossSectionVec finiArray; + Elf64_Off dtNeeded; // offset to DtNeededIndex[] in blob + Elf64_Xword dtNeededSz; + CrossSectionRef sharedLocalSymbolIndex; + CrossSectionRef sharedGlobalSymbolIndex; +}; + +struct AdltSectionHeader { + SemanticVersion schemaVersion = {1, 0, 0}; + Elf64_Half schemaPSODSize = sizeof(PSOD); + Elf64_Half sharedObjectsNum; + HashType stringHashType = HashType::NONE; + Elf64_Off blobStart; // binary blob start offset from the .adlt start + Elf64_Xword blobSize; +}; + +constexpr char BlobStartMark[4] = {0xA, 0xD, 0x1, 0x7}; + + +template +class AdltSection final : public SyntheticSection { +public: + struct SectionString { + StringRef ref; + Elf64_Off strtabOff; // offset in strTabSec + }; + + // will be serialized to DtNeededIndex + struct DtNeededData { + SectionString str; + llvm::Optional psodIndex; + }; + + using SectionStringVector = SmallVector; + using DtNeededsVec = SmallVector; + + // will be serialized to PSOD + struct SoData { + SectionString soName; + DtNeededsVec dtNeededs; + + StringRef initArrayName; + StringRef finiArrayName; + + OutputSection* initArraySec; + OutputSection* finiArraySec; + + Elf64_Off sharedLocalIndex; // TODO + Elf64_Off sharedGlobalIndex; // TODO + }; + + +public: + AdltSection(StringTableSection& strTabSec); + + void writeTo(uint8_t* buf) override; + size_t getSize() const override; + void finalizeContents() override; + + Elf64_Off getBlobStartOffset() const; + +private: + static OutputSection* findOutSection(StringRef name); + + void buildSonameIndex(); + void linkInternalDtNeeded(); + void extractInitFiniArray(); + + Elf64_Xword calculateHash(StringRef str) const; + SoData makeSoData(const SharedFileExtended*); + PSOD serialize(const SoData&) const; + + size_t estimateBlobSize() const; + size_t writeDtNeededVec(uint8_t* buff, const DtNeededsVec& neededVec) const; + +private: + StringTableSection& strTabSec; + + AdltSectionHeader header = {}; + SmallVector soInputs; + llvm::DenseMap sonameToIndexMap; +}; + +} // namespace adlt + + + InputSection *createInterpSection(); MergeInputSection *createCommentSection(); template void splitSections(); @@ -1295,6 +1439,8 @@ struct InStruct { std::unique_ptr strTab; std::unique_ptr symTab; std::unique_ptr symTabShndx; + std::unique_ptr adltData; + std::unique_ptr adltStrTab; void reset(); }; diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index de16dec8e609..318a9bf2d86d 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -312,6 +312,13 @@ template void elf::createSyntheticSections() { in.symTabShndx = std::make_unique(); } + if (config->adlt) { + in.adltStrTab = std::make_unique(".adlt.strtab", false); + in.adltData = std::make_unique>(*in.adltStrTab); + add(*in.adltStrTab); + add(*in.adltData); + } + in.bss = std::make_unique(".bss", 0, 1); add(*in.bss); @@ -2127,6 +2134,11 @@ template void Writer::finalizeSections() { finalizeSynthetic(in.iplt.get()); finalizeSynthetic(in.ppc32Got2.get()); finalizeSynthetic(in.partIndex.get()); + + if (config->adlt) { + finalizeSynthetic(in.adltData.get()); + finalizeSynthetic(in.adltStrTab.get()); + } // Dynamic section must be the last one in this list and dynamic // symbol table section (dynSymTab) must be the first one. -- Gitee From 1c84537f80aa0e21f9b364e423a982e6214e8c39 Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Tue, 2 Apr 2024 08:23:56 -0400 Subject: [PATCH 32/77] Fix scanRelocations. Minor changes. Change-Id: Idb45c7070d5ed4b2ac2bbf6e82addc5cbe67dec3 Signed-off-by: Anton Volkov --- lld/ELF/InputFiles.cpp | 19 ++++++++++++------- lld/ELF/InputFiles.h | 7 +++++++ lld/ELF/InputSection.cpp | 10 ++++++---- lld/ELF/Relocations.cpp | 6 +++--- lld/ELF/Writer.cpp | 2 +- 5 files changed, 29 insertions(+), 15 deletions(-) diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp index d777209a2769..0b140aa8aa07 100644 --- a/lld/ELF/InputFiles.cpp +++ b/lld/ELF/InputFiles.cpp @@ -145,13 +145,13 @@ static bool isCompatible(InputFile *file) { } InputFile *existing = nullptr; - if (!ctx->objectFiles.empty()) - existing = ctx->objectFiles[0]; - else if (!ctx->sharedFiles.empty()) - existing = ctx->sharedFiles[0]; - else if (!ctx->bitcodeFiles.empty()) - existing = ctx->bitcodeFiles[0]; - std::string with; + if (!ctx->objectFiles.empty()) + existing = ctx->objectFiles[0]; + else if (!ctx->sharedFiles.empty()) + existing = ctx->sharedFiles[0]; + else if (!ctx->bitcodeFiles.empty()) + existing = ctx->bitcodeFiles[0]; + std::string with; if (existing) with = " with " + toString(existing); error(toString(file) + " is incompatible" + with); @@ -1774,6 +1774,11 @@ SharedFileExtended::findInputSection(uint64_t offset) const { return nullptr; } +template +bool SharedFileExtended::isDynamicSection(InputSectionBase &sec) const { + return sec.type == llvm::ELF::SHT_NULL || sec.name.startswith(".got.plt"); +} + template Defined *SharedFileExtended::findDefinedSymbol( uint64_t offset, StringRef fatalTitle, diff --git a/lld/ELF/InputFiles.h b/lld/ELF/InputFiles.h index 68d76d5fb48e..1688c8ab9308 100644 --- a/lld/ELF/InputFiles.h +++ b/lld/ELF/InputFiles.h @@ -414,6 +414,13 @@ public: InputSectionBase *findInputSection(StringRef name) const; InputSectionBase *findInputSection(uint64_t offset) const; + bool isDynamicSection(InputSectionBase &sec) const; + + template + Symbol &getRelocTargetSymADLT(const RelT &rel, InputSectionBase &sec) const { + uint32_t symIndex = rel.getSymbol(config->isMips64EL); + return getSymbolADLT(symIndex, isDynamicSection(sec)); + } ArrayRef getLocalSymbols() override { if (this->allSymbols.empty()) diff --git a/lld/ELF/InputSection.cpp b/lld/ELF/InputSection.cpp index ef02393f0ec6..9489965a7fd1 100644 --- a/lld/ELF/InputSection.cpp +++ b/lld/ELF/InputSection.cpp @@ -347,9 +347,11 @@ void InputSection::copyRelocations(uint8_t *buf, ArrayRef rels) { for (const RelTy &rel : rels) { RelType type = rel.getType(config->isMips64EL); - const ELFFileBase *file = cast_or_null(file); - Symbol &sym = config->adlt ? getSharedFile()->getRelocTargetSym(rel) - : getFile()->getRelocTargetSym(rel); + const ObjFile *file = + config->adlt ? getSharedFile() : getFile(); + Symbol &sym = config->adlt + ? getSharedFile()->getRelocTargetSymADLT(rel, *sec) + : file->getRelocTargetSym(rel); auto *p = reinterpret_cast(buf); buf += sizeof(RelTy); @@ -384,7 +386,7 @@ void InputSection::copyRelocations(uint8_t *buf, ArrayRef rels) { uint32_t secIdx = cast(sym).discardedSecIdx; Elf_Shdr_Impl sec = file->template getELFShdrs()[secIdx]; warn("relocation refers to a discarded section: " + - CHECK(file->getObj().getSectionName(sec), file) + + CHECK(file->getObj().getSectionName(sec), file) + "\n>>> referenced by " + getObjMsg(p->r_offset)); } p->setSymbolAndType(0, 0, false); diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp index b31e637ab532..cdf252425273 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -1457,7 +1457,9 @@ void RelocationScanner::processForADLT(const RelTy &rel, Relocation *r, template void RelocationScanner::scanOne(RelTy *&i) { const RelTy &rel = *i; uint32_t symIndex = rel.getSymbol(config->isMips64EL); - bool fromDynamic = sec.type == SHT_NULL || sec.name.startswith(".got.plt"); + bool fromDynamic = false; + if (config->adlt) + fromDynamic = sec.getSharedFile()->isDynamicSection(sec); Symbol &sym = config->adlt ? sec.getSharedFile()->getSymbolADLT(symIndex, fromDynamic) @@ -2350,8 +2352,6 @@ bool ThunkCreator::createThunks(uint32_t pass, // original target so another Thunk can be generated. if (pass > 0 && normalizeExistingThunk(rel, src)) continue; - if (!rel.sym) - continue; // ADLT fix if (!target->needsThunk(rel.expr, rel.type, isec->file, src, *rel.sym, rel.addend)) continue; diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index 318a9bf2d86d..a087d61ae59f 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -1962,7 +1962,7 @@ template void Writer::finalizeSections() { if (sec && sec->isLive()) scanRelocations(*sec); } - if (!config->adlt) + else for (InputSectionBase *sec : inputSections) if (sec->isLive() && isa(sec) && (sec->flags & SHF_ALLOC)) scanRelocations(*sec); -- Gitee From a8e321c04980d57258542b5071aa0a0a971dc29e Mon Sep 17 00:00:00 2001 From: Likholatov Evgeny Date: Mon, 1 Apr 2024 10:46:24 +0300 Subject: [PATCH 33/77] Added handling for CONDBR19, TSTBR14 and ADR_GOT_PAGE relocs. Signed-off-by: Likholatov Evgeny --- lld/ELF/Relocations.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp index cdf252425273..55379fe9feb0 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -1414,6 +1414,8 @@ void RelocationScanner::processForADLT(const RelTy &rel, Relocation *r, // plt relocs case R_AARCH64_CALL26: case R_AARCH64_JUMP26: + case R_AARCH64_CONDBR19: + case R_AARCH64_TSTBR14: if (r->sym->isDefined()) r->expr = R_PC; // prev: R_PLT_PC sec.relocations.push_back(*r); @@ -1431,6 +1433,7 @@ void RelocationScanner::processForADLT(const RelTy &rel, Relocation *r, } LLVM_FALLTHROUGH; case R_AARCH64_LD64_GOT_LO12_NC: + case R_AARCH64_LD64_GOTPAGE_LO15: processAux(r->expr, r->type, r->offset, *r->sym, r->addend); return; // tls relocs -- Gitee From 916ce9087335077ada71f590b8fcef38b672db41 Mon Sep 17 00:00:00 2001 From: Khomutov Nikita Date: Thu, 4 Apr 2024 09:33:16 +0000 Subject: [PATCH 34/77] !441 [ADLT] .adlt section C-style header * simplify ifdef * .adlt sec header: use proper includes for C/C++ * [ADLT] add header size to .adlt header * lint and add namespace under C++ macro * [ADLT] .adlt section C-style header Signed-off-by: Khomutov Nikita --- lld/ELF/ADLTSection.h | 106 ++++++++++++++++++++++++++++++++++ lld/ELF/SyntheticSections.cpp | 66 +++++++++++++++------ lld/ELF/SyntheticSections.h | 78 +++---------------------- 3 files changed, 161 insertions(+), 89 deletions(-) create mode 100644 lld/ELF/ADLTSection.h diff --git a/lld/ELF/ADLTSection.h b/lld/ELF/ADLTSection.h new file mode 100644 index 000000000000..74fc48600554 --- /dev/null +++ b/lld/ELF/ADLTSection.h @@ -0,0 +1,106 @@ +//===- ADLTSection.h - ADLT Section data types --------------------*- C -*-===// +// +// Copyright (C) 2024 Huawei Device Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//===----------------------------------------------------------------------===// + +#ifndef LLD_ELF_ADLT_SECTION_H +#define LLD_ELF_ADLT_SECTION_H + +#ifdef __cplusplus +#include +namespace lld { +namespace elf { +namespace adlt { + +#else // __cplusplus +#include +#endif // __cplusplus + +// part of #include +typedef uint16_t Elf64_Half; +typedef uint64_t Elf64_Off; +typedef uint64_t Elf64_Addr; +typedef uint32_t Elf64_Word; +typedef uint64_t Elf64_Xword; +typedef uint8_t Elf64_Byte; + +typedef struct { + Elf64_Word secIndex; + Elf64_Off offset; // in-section offset from start +} adlt_cross_section_ref_t; + +typedef struct { + Elf64_Xword secIndex; + Elf64_Xword numElem; + Elf64_Addr addr; +} adlt_cross_section_vec_t; + +typedef struct { + Elf64_Half major: 6; + Elf64_Half minor: 6; + Elf64_Half patch: 4; +} adlt_semver_t; + +// DT_NEEDED string index with embedded PSOD index if available +typedef struct { + Elf64_Off hasInternalPSOD : 1; // true if soname + Elf64_Off PSODindex : 16; // PSOD in the current ADLT image + Elf64_Off sonameOffset : 47; // string start in bound .adlt.strtab +} adlt_dt_needed_index_t; + +typedef enum { + ADLT_HASH_TYPE_NONE = 0, + ADLT_HASH_TYPE_GNU_HASH = 1, + ADLT_HASH_TYPE_SYSV_HASH = 2, + ADLT_HASH_TYPE_DEBUG_CONST = 0xfe, + ADLT_HASH_TYPE_MAX = 0xff, +} adlt_hash_type_enum_t; + +typedef uint8_t adlt_hash_type_t; + +// Serializable representation per-shared-object-data in .adlt section +typedef struct { + Elf64_Off soName; // offset in .adlt.strtab + Elf64_Xword soNameHash; // algorith according to header.stringHashType value + adlt_cross_section_vec_t initArray; + adlt_cross_section_vec_t finiArray; + Elf64_Off dtNeeded; // offset to adlt_dt_needed_index_t[] array in blob + Elf64_Xword dtNeededSz; + adlt_cross_section_ref_t sharedLocalSymbolIndex; + adlt_cross_section_ref_t sharedGlobalSymbolIndex; +} adlt_psod_t; + +typedef struct { + adlt_semver_t schemaVersion; // {major, minor, patch} + Elf64_Half schemaHeaderSize; // >= sizeof(adlt_section_header_t) if comp + Elf64_Half schemaPSODSize; // >= sizeof(adlt_psod_t) if compatible + Elf64_Half sharedObjectsNum; // number of PSOD entries + adlt_hash_type_t stringHashType; // contains adlt_hash_type_enum_t value + Elf64_Off blobStart; // offset of binary blob start relative to .adlt + Elf64_Xword blobSize; +} adlt_section_header_t; + +static const char adltBlobStartMark[4] = { 0xA, 0xD, 0x1, 0x7 }; + +static const adlt_semver_t adltSchemaVersion = {1, 0, 0}; + +#ifdef __cplusplus +} // namespace adlt +} // namespace elf +} // namespace lld +#endif // __cplusplus + +#endif // LLD_ELF_ADLT_SECTION_H diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp index 35f163c5d2ba..28d699a13f3b 100644 --- a/lld/ELF/SyntheticSections.cpp +++ b/lld/ELF/SyntheticSections.cpp @@ -4001,6 +4001,34 @@ size_t PackageMetadataNote::getSize() const { // OHOS_LOCAL begin namespace lld { namespace elf { namespace adlt { +static_assert( + sizeof(SemanticVersion) == sizeof(Elf64_Half), + ".adlt semantic version is designed to occupy uint16_t" +); + +static_assert( + sizeof(DtNeededIndex) == sizeof(Elf64_Off), + "DtNeededIndex have to be an offset with intrused flags" +); + +static_assert( + sizeof(adlt_hash_type_t) == sizeof(Elf64_Byte), + "String hash type enum should occupy only one byte" +); + +static_assert(sizeof(adltBlobStartMark) == 4, + "0xad17 consist of 4 bytes" +); + +static_assert(sizeof(AdltSectionHeader) == 32, + "please udpate major version if header has been changed" +); + +static_assert(sizeof(PSOD) == 112, + "please udpate version if PSOD layout or content changed" +); + + template AdltSection::AdltSection(StringTableSection& strTabSec) : SyntheticSection(SHF_ALLOC, SHT_PROGBITS, 4, ".adlt") @@ -4023,12 +4051,13 @@ void AdltSection::finalizeContents() { const Elf64_Half soNum = soInputs.size(); header = AdltSectionHeader{ - {1, 0, 0}, // .schemaVersion - sizeof(PSOD), // .schemaPSODSize - soNum, // .sharedObjectsNum - HashType::MUSL_GNU_HASH, // .stringHashType - getBlobStartOffset(), // .blobStart - estimateBlobSize(), // .blobSize + adltSchemaVersion, // .schemaVersion + sizeof(AdltSectionHeader), // .schemaHeaderSize + sizeof(PSOD), // .schemaPSODSize + soNum, // .sharedObjectsNum + ADLT_HASH_TYPE_GNU_HASH, // .stringHashType + getBlobStartOffset(), // .blobStart + estimateBlobSize(), // .blobSize }; buildSonameIndex(); @@ -4117,12 +4146,12 @@ AdltSection::makeSoData(const SharedFileExtended* soext) { template Elf64_Xword AdltSection::calculateHash(StringRef str) const { - switch(header.stringHashType) { - case HashType::NONE: + switch(static_cast(header.stringHashType)) { + case ADLT_HASH_TYPE_NONE: return 0x0; - case HashType::MUSL_GNU_HASH: + case ADLT_HASH_TYPE_GNU_HASH: return hashGnu(str); - case HashType::DEBUG_CONST: + case ADLT_HASH_TYPE_DEBUG_CONST: return 0xdeadbeef1337c0de; default: llvm_unreachable(".adlt hash type not implemented"); @@ -4147,12 +4176,12 @@ PSOD AdltSection::serialize(const SoData& soData) const { 0x0, // .dtNeeded // filled by blob serialization soData.dtNeededs.size(), // .dtNeededSz CrossSectionRef { - 0, // .sectionIndex - soData.sharedLocalIndex, // .offsetFromStart + 0, // .secIndex + soData.sharedLocalIndex, // .offset }, // .sharedLocalSymbolIndex CrossSectionRef { - 0, // .sectionIndex - soData.sharedGlobalIndex, // .offsetFromStart + 0, // .secIndex + soData.sharedGlobalIndex, // .offset }, // .sharedGlobalSymbolIndex }; } @@ -4164,7 +4193,7 @@ Elf64_Off AdltSection::getBlobStartOffset() const { template size_t AdltSection::estimateBlobSize() const { - size_t blobSize = sizeof(BlobStartMark); + size_t blobSize = sizeof(adltBlobStartMark); for (const auto& soData: soInputs) { blobSize += sizeof(DtNeededIndex) * soData.dtNeededs.size(); @@ -4208,8 +4237,8 @@ void AdltSection::writeTo(uint8_t* buf) { { uint8_t* const blob_buf = buf + header.blobStart; size_t blob_off = 0; - memcpy(blob_buf + blob_off, &BlobStartMark, sizeof(BlobStartMark)); - blob_off += sizeof(BlobStartMark); + memcpy(blob_buf + blob_off, &adltBlobStartMark, sizeof(adltBlobStartMark)); + blob_off += sizeof(adltBlobStartMark); // dt-needed for(const auto& it : llvm::enumerate(soInputs)) { @@ -4246,8 +4275,7 @@ void AdltSection::writeTo(uint8_t* buf) { template size_t AdltSection::getSize() const { - const size_t pre_blob_size = sizeof(header) + sizeof(PSOD) * soInputs.size(); - assert(pre_blob_size <= header.blobStart); + assert(sizeof(header) + sizeof(PSOD) * soInputs.size() <= header.blobStart); return header.blobStart + header.blobSize; } diff --git a/lld/ELF/SyntheticSections.h b/lld/ELF/SyntheticSections.h index 48edc79a718d..ebf03b332d4b 100644 --- a/lld/ELF/SyntheticSections.h +++ b/lld/ELF/SyntheticSections.h @@ -22,6 +22,7 @@ #include "Config.h" #include "EhFrame.h" // OHOS_LOCAL +#include "ADLTSection.h" // OHOS_LOCAL #include "InputSection.h" #include "llvm/ADT/DenseSet.h" #include "llvm/ADT/MapVector.h" @@ -1224,76 +1225,13 @@ public: namespace adlt { -using llvm::ELF::Elf64_Half; -using llvm::ELF::Elf64_Off; -using llvm::ELF::Elf64_Addr; -using llvm::ELF::Elf64_Word; -using llvm::ELF::Elf64_Xword; -using Elf64_Byte = uint8_t; - -struct CrossSectionRef { - Elf64_Word sectionIndex; - Elf64_Off offsetFromStart; -}; - -struct SemanticVersion { - Elf64_Half major: 6; - Elf64_Half minor: 6; - Elf64_Half patch: 4; -}; - -static_assert( - sizeof(SemanticVersion) == sizeof(Elf64_Half), - ".adlt semantic version is designed to occupy uint16_t" -); - -enum HashType : Elf64_Byte { - NONE = 0, - MUSL_GNU_HASH = 1, - MUSL_SYSV_HASH = 2, - MUSL_KEYHASH = 3, - DEBUG_CONST = 0xff, -}; - -struct DtNeededIndex { - bool hasInternalPSOD : 1; // true if soname - Elf64_Off PSODindex : 16; // PSOD in the current ADLT image - Elf64_Off sonameOffset : 47; // string start in bound strTabSec -}; - -static_assert( - sizeof(DtNeededIndex) == sizeof(Elf64_Off), - "DtNeededIndex have to be an offset with intrused flags" -); - -struct CrossSectionVec { - Elf64_Xword secIndex; - Elf64_Xword numElem; - Elf64_Addr addr; -}; - -// Serializable representation per-shared-object-data in .adlt section -struct PSOD { - Elf64_Off soName; // offset in strTabSec - Elf64_Xword soNameHash; - CrossSectionVec initArray; - CrossSectionVec finiArray; - Elf64_Off dtNeeded; // offset to DtNeededIndex[] in blob - Elf64_Xword dtNeededSz; - CrossSectionRef sharedLocalSymbolIndex; - CrossSectionRef sharedGlobalSymbolIndex; -}; - -struct AdltSectionHeader { - SemanticVersion schemaVersion = {1, 0, 0}; - Elf64_Half schemaPSODSize = sizeof(PSOD); - Elf64_Half sharedObjectsNum; - HashType stringHashType = HashType::NONE; - Elf64_Off blobStart; // binary blob start offset from the .adlt start - Elf64_Xword blobSize; -}; - -constexpr char BlobStartMark[4] = {0xA, 0xD, 0x1, 0x7}; +using CrossSectionRef = adlt_cross_section_ref_t; +using CrossSectionVec = adlt_cross_section_vec_t; +using SemanticVersion = adlt_semver_t; +using DtNeededIndex = adlt_dt_needed_index_t; +using PSOD = adlt_psod_t; +using AdltSectionHeader = adlt_section_header_t; +using HashType = adlt_hash_type_enum_t; template -- Gitee From b916b6f4170c52b3b457223738ef919b19ebacf3 Mon Sep 17 00:00:00 2001 From: Khomutov Nikita Date: Sun, 7 Apr 2024 11:36:27 +0300 Subject: [PATCH 35/77] [ADLT] refactor types in section generation Signed-off-by: Khomutov Nikita Change-Id: I2e9b2836abbbfdbd38d4de014b277ccf920eef5f --- lld/ELF/SyntheticSections.cpp | 76 +++++++++++++++++------------------ lld/ELF/SyntheticSections.h | 22 +++------- 2 files changed, 43 insertions(+), 55 deletions(-) diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp index 28d699a13f3b..81a4d94977df 100644 --- a/lld/ELF/SyntheticSections.cpp +++ b/lld/ELF/SyntheticSections.cpp @@ -4002,13 +4002,13 @@ size_t PackageMetadataNote::getSize() const { namespace lld { namespace elf { namespace adlt { static_assert( - sizeof(SemanticVersion) == sizeof(Elf64_Half), + sizeof(adlt_semver_t) == sizeof(Elf64_Half), ".adlt semantic version is designed to occupy uint16_t" ); static_assert( - sizeof(DtNeededIndex) == sizeof(Elf64_Off), - "DtNeededIndex have to be an offset with intrused flags" + sizeof(adlt_dt_needed_index_t) == sizeof(Elf64_Off), + "adlt_dt_needed_index_t have to be an offset with intrused flags" ); static_assert( @@ -4020,12 +4020,12 @@ static_assert(sizeof(adltBlobStartMark) == 4, "0xad17 consist of 4 bytes" ); -static_assert(sizeof(AdltSectionHeader) == 32, +static_assert(sizeof(adlt_section_header_t) == 32, "please udpate major version if header has been changed" ); -static_assert(sizeof(PSOD) == 112, - "please udpate version if PSOD layout or content changed" +static_assert(sizeof(adlt_psod_t) == 112, + "please udpate version if adlt_psod_t layout or content changed" ); @@ -4050,14 +4050,14 @@ void AdltSection::finalizeContents() { "the number of input libs exeeds ELF limit on number of sections"); const Elf64_Half soNum = soInputs.size(); - header = AdltSectionHeader{ - adltSchemaVersion, // .schemaVersion - sizeof(AdltSectionHeader), // .schemaHeaderSize - sizeof(PSOD), // .schemaPSODSize - soNum, // .sharedObjectsNum - ADLT_HASH_TYPE_GNU_HASH, // .stringHashType - getBlobStartOffset(), // .blobStart - estimateBlobSize(), // .blobSize + header = adlt_section_header_t{ + adltSchemaVersion, // .schemaVersion + sizeof(adlt_section_header_t), // .schemaHeaderSize + sizeof(adlt_psod_t), // .schemaPSODSize + soNum, // .sharedObjectsNum + ADLT_HASH_TYPE_GNU_HASH, // .stringHashType + getBlobStartOffset(), // .blobStart + estimateBlobSize(), // .blobSize }; buildSonameIndex(); @@ -4127,17 +4127,13 @@ AdltSection::makeSoData(const SharedFileExtended* soext) { for (const auto& neededName: soext->dtNeeded) { data.dtNeededs.push_back({ SectionString{neededName, strTabSec.addString(neededName)}, - {}, // internal PSOD index is uknown by the moment, will be linked + {}, // unknown now, filled by linkInternalDtNeeded }); } data.initArrayName = soext->addAdltPostfix(".init_array"); data.finiArrayName = soext->addAdltPostfix(".fini_array"); - // TODO: - // sharedLocalIndex - // sharedGlobalIndex - // TODO: get section index for sharedLocalIndex // TODO: get section index for sharedGlobalIndex @@ -4146,7 +4142,7 @@ AdltSection::makeSoData(const SharedFileExtended* soext) { template Elf64_Xword AdltSection::calculateHash(StringRef str) const { - switch(static_cast(header.stringHashType)) { + switch(static_cast(header.stringHashType)) { case ADLT_HASH_TYPE_NONE: return 0x0; case ADLT_HASH_TYPE_GNU_HASH: @@ -4159,27 +4155,27 @@ Elf64_Xword AdltSection::calculateHash(StringRef str) const { } template -PSOD AdltSection::serialize(const SoData& soData) const { - return PSOD { +adlt_psod_t AdltSection::serialize(const SoData& soData) const { + return adlt_psod_t { soData.soName.strtabOff, // .soName calculateHash(soData.soName.ref), // .soNameHash - soData.initArraySec ? CrossSectionVec{ // .initArray + soData.initArraySec ? adlt_cross_section_vec_t{ // .initArray soData.initArraySec->sectionIndex, soData.initArraySec->size / sizeof(Elf64_Addr), // size soData.initArraySec->addr, - } : CrossSectionVec{0, 0, 0}, - soData.finiArraySec ? CrossSectionVec{ // .finiArray + } : adlt_cross_section_vec_t{0, 0, 0}, + soData.finiArraySec ? adlt_cross_section_vec_t{ // .finiArray soData.finiArraySec->sectionIndex, soData.finiArraySec->size / sizeof(Elf64_Addr), // size soData.finiArraySec->addr, - } : CrossSectionVec{0, 0, 0}, + } : adlt_cross_section_vec_t{0, 0, 0}, 0x0, // .dtNeeded // filled by blob serialization soData.dtNeededs.size(), // .dtNeededSz - CrossSectionRef { + adlt_cross_section_ref_t { 0, // .secIndex soData.sharedLocalIndex, // .offset }, // .sharedLocalSymbolIndex - CrossSectionRef { + adlt_cross_section_ref_t { 0, // .secIndex soData.sharedGlobalIndex, // .offset }, // .sharedGlobalSymbolIndex @@ -4188,7 +4184,7 @@ PSOD AdltSection::serialize(const SoData& soData) const { template Elf64_Off AdltSection::getBlobStartOffset() const { - return sizeof(header) + sizeof(PSOD) * soInputs.size(); + return sizeof(adlt_section_header_t) + sizeof(adlt_psod_t) * soInputs.size(); } template @@ -4196,7 +4192,7 @@ size_t AdltSection::estimateBlobSize() const { size_t blobSize = sizeof(adltBlobStartMark); for (const auto& soData: soInputs) { - blobSize += sizeof(DtNeededIndex) * soData.dtNeededs.size(); + blobSize += sizeof(adlt_dt_needed_index_t) * soData.dtNeededs.size(); }; return blobSize; @@ -4207,10 +4203,10 @@ size_t AdltSection::writeDtNeededVec( uint8_t* buff, const DtNeededsVec& neededVec) const { if (neededVec.empty()) return 0; - SmallVector needIndexes; + SmallVector needIndexes; needIndexes.reserve(neededVec.size()); for (const auto& need_data : neededVec) { - needIndexes.push_back(DtNeededIndex{ + needIndexes.push_back(adlt_dt_needed_index_t{ need_data.psodIndex.has_value(), // .hasInternalPSOD need_data.psodIndex.value_or(0), // .PSODindex need_data.str.strtabOff, // .sonameOffset @@ -4225,11 +4221,11 @@ template void AdltSection::writeTo(uint8_t* buf) { // TODO: take care of endianness, use write32 / write64 etc. // pre-serialized SoData, enriched with offsets during blob writing - SmallVector psods; + SmallVector psods; for (const auto& it: llvm::enumerate(soInputs)) { const SoData& soData = it.value(); - PSOD psod = serialize(soData); + adlt_psod_t psod = serialize(soData); psods.push_back(psod); } @@ -4243,7 +4239,7 @@ void AdltSection::writeTo(uint8_t* buf) { // dt-needed for(const auto& it : llvm::enumerate(soInputs)) { const auto& soData = it.value(); - PSOD& psod = psods[it.index()]; + adlt_psod_t& psod = psods[it.index()]; psod.dtNeededSz = soData.dtNeededs.size(); psod.dtNeeded = blob_off; @@ -4266,16 +4262,18 @@ void AdltSection::writeTo(uint8_t* buf) { uint8_t* const psods_buf = buf + sizeof(header); size_t psods_off = 0; for (const auto& it: llvm::enumerate(soInputs)) { - PSOD& psod = psods[it.index()]; - memcpy(psods_buf + psods_off, &psod, sizeof(PSOD)); - psods_off += sizeof(PSOD); + adlt_psod_t& psod = psods[it.index()]; + memcpy(psods_buf + psods_off, &psod, sizeof(adlt_psod_t)); + psods_off += sizeof(adlt_psod_t); } } } template size_t AdltSection::getSize() const { - assert(sizeof(header) + sizeof(PSOD) * soInputs.size() <= header.blobStart); + assert(sizeof(adlt_section_header_t) + + sizeof(adlt_psod_t) * soInputs.size() + <= header.blobStart); return header.blobStart + header.blobSize; } diff --git a/lld/ELF/SyntheticSections.h b/lld/ELF/SyntheticSections.h index ebf03b332d4b..f325ca02759a 100644 --- a/lld/ELF/SyntheticSections.h +++ b/lld/ELF/SyntheticSections.h @@ -1225,15 +1225,6 @@ public: namespace adlt { -using CrossSectionRef = adlt_cross_section_ref_t; -using CrossSectionVec = adlt_cross_section_vec_t; -using SemanticVersion = adlt_semver_t; -using DtNeededIndex = adlt_dt_needed_index_t; -using PSOD = adlt_psod_t; -using AdltSectionHeader = adlt_section_header_t; -using HashType = adlt_hash_type_enum_t; - - template class AdltSection final : public SyntheticSection { public: @@ -1242,16 +1233,16 @@ public: Elf64_Off strtabOff; // offset in strTabSec }; - // will be serialized to DtNeededIndex + // will be serialized to adlt_dt_needed_index_t struct DtNeededData { SectionString str; llvm::Optional psodIndex; }; - using SectionStringVector = SmallVector; - using DtNeededsVec = SmallVector; + using SectionStringVector = SmallVector; + using DtNeededsVec = SmallVector; - // will be serialized to PSOD + // will be serialized to adlt_psod_t struct SoData { SectionString soName; DtNeededsVec dtNeededs; @@ -1266,7 +1257,6 @@ public: Elf64_Off sharedGlobalIndex; // TODO }; - public: AdltSection(StringTableSection& strTabSec); @@ -1285,7 +1275,7 @@ private: Elf64_Xword calculateHash(StringRef str) const; SoData makeSoData(const SharedFileExtended*); - PSOD serialize(const SoData&) const; + adlt_psod_t serialize(const SoData&) const; size_t estimateBlobSize() const; size_t writeDtNeededVec(uint8_t* buff, const DtNeededsVec& neededVec) const; @@ -1293,7 +1283,7 @@ private: private: StringTableSection& strTabSec; - AdltSectionHeader header = {}; + adlt_section_header_t header = {}; SmallVector soInputs; llvm::DenseMap sonameToIndexMap; }; -- Gitee From fd67cd776cd88937a44924e305b4e5c68421c268 Mon Sep 17 00:00:00 2001 From: Khomutov Nikita Date: Sun, 7 Apr 2024 12:53:32 +0300 Subject: [PATCH 36/77] [ADLT] refactor .adlt types - add array type - replace add in cs-adday to relocatable offset - union dtNeeded Signed-off-by: Khomutov Nikita Change-Id: I88bd48e32ad562b151a955a74b073b0596870c93 --- lld/ELF/ADLTSection.h | 27 +++++++++++++++-------- lld/ELF/SyntheticSections.cpp | 41 +++++++++++++++++++---------------- 2 files changed, 40 insertions(+), 28 deletions(-) diff --git a/lld/ELF/ADLTSection.h b/lld/ELF/ADLTSection.h index 74fc48600554..6b029c4dd0f1 100644 --- a/lld/ELF/ADLTSection.h +++ b/lld/ELF/ADLTSection.h @@ -39,14 +39,24 @@ typedef uint8_t Elf64_Byte; typedef struct { Elf64_Word secIndex; - Elf64_Off offset; // in-section offset from start + Elf64_Off offset; // from section start } adlt_cross_section_ref_t; typedef struct { - Elf64_Xword secIndex; - Elf64_Xword numElem; - Elf64_Addr addr; -} adlt_cross_section_vec_t; + Elf64_Word secIndex; + Elf64_Off offset; // from section start + Elf64_Xword size; // size in bytes +} adlt_cross_section_array_t; + +typedef struct { + Elf64_Off offset; // relative to header.blobStart + Elf64_Xword size; // size in bytes, make convertions for data type +} adlt_blob_array_t; + +typedef adlt_blob_array_t adlt_blob_u8_array_t; +typedef adlt_blob_array_t adlt_blob_u16_array_t; +typedef adlt_blob_array_t adlt_blob_u32_array_t; +typedef adlt_blob_array_t adlt_blob_u64_array_t; typedef struct { Elf64_Half major: 6; @@ -75,10 +85,9 @@ typedef uint8_t adlt_hash_type_t; typedef struct { Elf64_Off soName; // offset in .adlt.strtab Elf64_Xword soNameHash; // algorith according to header.stringHashType value - adlt_cross_section_vec_t initArray; - adlt_cross_section_vec_t finiArray; - Elf64_Off dtNeeded; // offset to adlt_dt_needed_index_t[] array in blob - Elf64_Xword dtNeededSz; + adlt_cross_section_array_t initArray; + adlt_cross_section_array_t finiArray; + adlt_blob_array_t dtNeeded; // array of adlt_dt_needed_index_t[] elems adlt_cross_section_ref_t sharedLocalSymbolIndex; adlt_cross_section_ref_t sharedGlobalSymbolIndex; } adlt_psod_t; diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp index 81a4d94977df..5a3b997ccbdc 100644 --- a/lld/ELF/SyntheticSections.cpp +++ b/lld/ELF/SyntheticSections.cpp @@ -4159,26 +4159,28 @@ adlt_psod_t AdltSection::serialize(const SoData& soData) const { return adlt_psod_t { soData.soName.strtabOff, // .soName calculateHash(soData.soName.ref), // .soNameHash - soData.initArraySec ? adlt_cross_section_vec_t{ // .initArray + soData.initArraySec ? adlt_cross_section_array_t{ // .initArray soData.initArraySec->sectionIndex, - soData.initArraySec->size / sizeof(Elf64_Addr), // size - soData.initArraySec->addr, - } : adlt_cross_section_vec_t{0, 0, 0}, - soData.finiArraySec ? adlt_cross_section_vec_t{ // .finiArray + 0x0, // .init_array function addresses are located at the start + soData.initArraySec->size, // size + } : adlt_cross_section_array_t{0, 0, 0}, + soData.finiArraySec ? adlt_cross_section_array_t{ // .finiArray soData.finiArraySec->sectionIndex, - soData.finiArraySec->size / sizeof(Elf64_Addr), // size - soData.finiArraySec->addr, - } : adlt_cross_section_vec_t{0, 0, 0}, - 0x0, // .dtNeeded // filled by blob serialization - soData.dtNeededs.size(), // .dtNeededSz - adlt_cross_section_ref_t { - 0, // .secIndex + 0x0, // .fini_array function addresses are located at the start + soData.finiArraySec->size, // size + } : adlt_cross_section_array_t{0, 0, 0}, + adlt_blob_array_t { // .dtNeeded + 0x0, // offset, filled array write in blob + soData.dtNeededs.size() * sizeof(adlt_dt_needed_index_t) + }, + adlt_cross_section_ref_t { // .sharedLocalSymbolIndex + 0, // .secIndex // TODO: dynsym? soData.sharedLocalIndex, // .offset - }, // .sharedLocalSymbolIndex - adlt_cross_section_ref_t { - 0, // .secIndex + }, + adlt_cross_section_ref_t { // .sharedGlobalSymbolIndex + 0, // .secIndex // TODO: dynsym? soData.sharedGlobalIndex, // .offset - }, // .sharedGlobalSymbolIndex + }, }; } @@ -4241,9 +4243,10 @@ void AdltSection::writeTo(uint8_t* buf) { const auto& soData = it.value(); adlt_psod_t& psod = psods[it.index()]; - psod.dtNeededSz = soData.dtNeededs.size(); - psod.dtNeeded = blob_off; - blob_off += writeDtNeededVec(blob_buf + blob_off, soData.dtNeededs); + size_t written = writeDtNeededVec(blob_buf + blob_off, soData.dtNeededs); + psod.dtNeeded.offset = blob_off; + psod.dtNeeded.size = written; + blob_off += written; } // finalize header.blobSize -- Gitee From 37df10766dacb91da578939f6c03488d6d3a07e7 Mon Sep 17 00:00:00 2001 From: Khomutov Nikita Date: Sun, 7 Apr 2024 13:21:52 +0300 Subject: [PATCH 37/77] [ADLT] add ph indexes to .adlt Signed-off-by: Khomutov Nikita Change-Id: Ia72cb2e0aac44f7b27061f35de1f79fc9132218c --- lld/ELF/ADLTSection.h | 5 +++-- lld/ELF/SyntheticSections.cpp | 30 ++++++++++++++++++++++++------ lld/ELF/SyntheticSections.h | 2 ++ 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/lld/ELF/ADLTSection.h b/lld/ELF/ADLTSection.h index 6b029c4dd0f1..e8513a9d50f2 100644 --- a/lld/ELF/ADLTSection.h +++ b/lld/ELF/ADLTSection.h @@ -83,13 +83,14 @@ typedef uint8_t adlt_hash_type_t; // Serializable representation per-shared-object-data in .adlt section typedef struct { - Elf64_Off soName; // offset in .adlt.strtab - Elf64_Xword soNameHash; // algorith according to header.stringHashType value + Elf64_Off soName; // offset in .adlt.strtab + Elf64_Xword soNameHash; // algorithm according to header.stringHashType value adlt_cross_section_array_t initArray; adlt_cross_section_array_t finiArray; adlt_blob_array_t dtNeeded; // array of adlt_dt_needed_index_t[] elems adlt_cross_section_ref_t sharedLocalSymbolIndex; adlt_cross_section_ref_t sharedGlobalSymbolIndex; + adlt_blob_u16_array_t phIndexes; // program header index array, typeof(e_phnum) } adlt_psod_t; typedef struct { diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp index 5a3b997ccbdc..6ac5296c1530 100644 --- a/lld/ELF/SyntheticSections.cpp +++ b/lld/ELF/SyntheticSections.cpp @@ -4024,7 +4024,7 @@ static_assert(sizeof(adlt_section_header_t) == 32, "please udpate major version if header has been changed" ); -static_assert(sizeof(adlt_psod_t) == 112, +static_assert(sizeof(adlt_psod_t) == 128, "please udpate version if adlt_psod_t layout or content changed" ); @@ -4134,8 +4134,9 @@ AdltSection::makeSoData(const SharedFileExtended* soext) { data.initArrayName = soext->addAdltPostfix(".init_array"); data.finiArrayName = soext->addAdltPostfix(".fini_array"); - // TODO: get section index for sharedLocalIndex - // TODO: get section index for sharedGlobalIndex + // TODO: fill data.sharedLocalIndex + // TODO: fill data.sharedGlobalIndex + // TODO: fill data.phIndexes return data; } @@ -4169,7 +4170,7 @@ adlt_psod_t AdltSection::serialize(const SoData& soData) const { 0x0, // .fini_array function addresses are located at the start soData.finiArraySec->size, // size } : adlt_cross_section_array_t{0, 0, 0}, - adlt_blob_array_t { // .dtNeeded + adlt_blob_array_t { // .dtNeeded 0x0, // offset, filled array write in blob soData.dtNeededs.size() * sizeof(adlt_dt_needed_index_t) }, @@ -4181,6 +4182,9 @@ adlt_psod_t AdltSection::serialize(const SoData& soData) const { 0, // .secIndex // TODO: dynsym? soData.sharedGlobalIndex, // .offset }, + adlt_blob_u16_array_t { // .phIndexes + 0x0, 0 + } }; } @@ -4195,6 +4199,7 @@ size_t AdltSection::estimateBlobSize() const { for (const auto& soData: soInputs) { blobSize += sizeof(adlt_dt_needed_index_t) * soData.dtNeededs.size(); + blobSize += sizeof(uint16_t) * soData.phIndexes.size(); }; return blobSize; @@ -4240,15 +4245,28 @@ void AdltSection::writeTo(uint8_t* buf) { // dt-needed for(const auto& it : llvm::enumerate(soInputs)) { - const auto& soData = it.value(); + const SoData& soData = it.value(); adlt_psod_t& psod = psods[it.index()]; - size_t written = writeDtNeededVec(blob_buf + blob_off, soData.dtNeededs); + const size_t written = writeDtNeededVec(blob_buf + blob_off, soData.dtNeededs); psod.dtNeeded.offset = blob_off; psod.dtNeeded.size = written; blob_off += written; } + // phIndexes + for (const auto& it : llvm::enumerate(soInputs)) { + const SoData& soData = it.value(); + adlt_psod_t& psod = psods[it.index()]; + + const size_t written = soData.phIndexes.size_in_bytes();; + memcpy(blob_buf + blob_off, + soData.phIndexes.data(), soData.phIndexes.size_in_bytes()); + psod.phIndexes.offset = blob_off; + psod.phIndexes.size = written; + blob_off += written; + } + // finalize header.blobSize assert((blob_off <= header.blobSize) && ".adlt-section: blob output exeeds its initial estimation"); diff --git a/lld/ELF/SyntheticSections.h b/lld/ELF/SyntheticSections.h index f325ca02759a..8292495940fa 100644 --- a/lld/ELF/SyntheticSections.h +++ b/lld/ELF/SyntheticSections.h @@ -1255,6 +1255,8 @@ public: Elf64_Off sharedLocalIndex; // TODO Elf64_Off sharedGlobalIndex; // TODO + + SmallVector phIndexes; // TODO }; public: -- Gitee From 8ca04e5b0b7d1f20212fc8da15096ede604d5c32 Mon Sep 17 00:00:00 2001 From: Khomutov Nikita Date: Mon, 8 Apr 2024 12:38:09 +0300 Subject: [PATCH 38/77] [ADLT] added .adlt typedef reasoning Signed-off-by: Khomutov Nikita Change-Id: I1e4711a60ae8f641a63288763ec47acde4eef051 Signed-off-by: Khomutov Nikita --- lld/ELF/ADLTSection.h | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lld/ELF/ADLTSection.h b/lld/ELF/ADLTSection.h index e8513a9d50f2..b4ba5966741d 100644 --- a/lld/ELF/ADLTSection.h +++ b/lld/ELF/ADLTSection.h @@ -53,10 +53,11 @@ typedef struct { Elf64_Xword size; // size in bytes, make convertions for data type } adlt_blob_array_t; -typedef adlt_blob_array_t adlt_blob_u8_array_t; -typedef adlt_blob_array_t adlt_blob_u16_array_t; -typedef adlt_blob_array_t adlt_blob_u32_array_t; -typedef adlt_blob_array_t adlt_blob_u64_array_t; +// plain-C has no strict typedefs, but aliases used to interpred underlying data +typedef adlt_blob_array_t adlt_blob_u8_array_t; // uint8_t[] +typedef adlt_blob_array_t adlt_blob_u16_array_t; // uint16_t[] +typedef adlt_blob_array_t adlt_blob_u32_array_t; // uint32_t[] +typedef adlt_blob_array_t adlt_blob_u64_array_t; // uint64_t[] typedef struct { Elf64_Half major: 6; -- Gitee From e5835e50ea68d086359c6d0a83873c0019faa6f9 Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Mon, 8 Apr 2024 08:29:28 -0400 Subject: [PATCH 39/77] [ADLT] Collect program header indexes. Change-Id: Ideecb08e8079faed3015b30fd28f25367635f9c1 Signed-off-by: Anton Volkov --- lld/ELF/InputFiles.h | 5 +++++ lld/ELF/Writer.cpp | 48 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/lld/ELF/InputFiles.h b/lld/ELF/InputFiles.h index 1688c8ab9308..5517c153a9b0 100644 --- a/lld/ELF/InputFiles.h +++ b/lld/ELF/InputFiles.h @@ -449,6 +449,11 @@ public: int symTabShndxSecIdx = 0; int eFirstGlobal = 0; + // Output information data: + llvm::SetVector programHeaderIndexes; + // TODO: dynamic relocation indexes + + // SharedFile compability layer: // This is actually a vector of Elf_Verdef pointers. SmallVector verdefs; // If the output file needs Elf_Verneed data structures for this file, this is diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index a087d61ae59f..de6fb6eefb0b 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -2134,7 +2134,7 @@ template void Writer::finalizeSections() { finalizeSynthetic(in.iplt.get()); finalizeSynthetic(in.ppc32Got2.get()); finalizeSynthetic(in.partIndex.get()); - + if (config->adlt) { finalizeSynthetic(in.adltData.get()); finalizeSynthetic(in.adltStrTab.get()); @@ -2312,6 +2312,19 @@ static uint64_t computeFlags(uint64_t flags) { return flags; } +static InputSection *getInputSection(OutputSection *sec) { + if (!sec || !sec->hasInputSections || sec->commands.empty()) + return nullptr; + SectionCommand *cmd = sec->commands.front(); + InputSectionDescription *isd = cast(cmd); + return isd->sections.front(); +} + +struct LoadInfoForADLT { + int phIndex; // input + SmallVector phSections; +}; + // Decide which program headers to create and which sections to include in each // one. template @@ -2322,6 +2335,10 @@ SmallVector Writer::createPhdrs(Partition &part) { return ret.back(); }; + auto getCurrentIndex = [&]() -> int { + return static_cast(ret.size()) - 1; + }; + unsigned partNo = part.getNumber(); bool isMain = partNo == 1; @@ -2376,6 +2393,9 @@ SmallVector Writer::createPhdrs(Partition &part) { } } + // ADLT: Collect load headers info + SmallVector adltLoadInfo; + for (OutputSection *sec : outputSections) { if (!needsPtLoad(sec)) continue; @@ -2407,9 +2427,35 @@ SmallVector Writer::createPhdrs(Partition &part) { (sameLMARegion || load->lastSec == Out::programHeaders))) { load = addHdr(PT_LOAD, newFlags); flags = newFlags; + if (config->adlt) { + adltLoadInfo.emplace_back(); + adltLoadInfo.back().phIndex = getCurrentIndex(); + } } load->add(sec); + if (config->adlt && !adltLoadInfo.empty()) + adltLoadInfo.back().phSections.push_back(sec); + } + + // ADLT: Fill load headers info + if (config->adlt) { + for (LoadInfoForADLT &info : adltLoadInfo) + for (OutputSection *sec : info.phSections) { + auto *isec = getInputSection(sec); + assert(isec); + auto *file = isec->file; + if (!file) // skip generated sections. + continue; // TODO: fix .rodata_$ADLT_POSTFIX sections. + auto *soFile = cast>(file); + soFile->programHeaderIndexes.insert(info.phIndex); + } + + // Check outputs + for (ELFFileBase *baseFile : ctx->sharedFilesExtended) { + auto *soFile = cast>(baseFile); + assert(!soFile->programHeaderIndexes.empty()); + } } // Add a TLS segment if any. -- Gitee From 0059e7c09ecf63c89b5e776f1292dc46e7da17f6 Mon Sep 17 00:00:00 2001 From: Khomutov Nikita Date: Mon, 8 Apr 2024 14:45:27 +0300 Subject: [PATCH 40/77] [ADLT] write overall mapped size to .adlt Signed-off-by: Khomutov Nikita Change-Id: I193b751098295e45502b1e4a64b063361a52d58b Signed-off-by: Khomutov Nikita --- lld/ELF/ADLTSection.h | 1 + lld/ELF/SyntheticSections.cpp | 7 +++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/lld/ELF/ADLTSection.h b/lld/ELF/ADLTSection.h index b4ba5966741d..3441ddb8af29 100644 --- a/lld/ELF/ADLTSection.h +++ b/lld/ELF/ADLTSection.h @@ -102,6 +102,7 @@ typedef struct { adlt_hash_type_t stringHashType; // contains adlt_hash_type_enum_t value Elf64_Off blobStart; // offset of binary blob start relative to .adlt Elf64_Xword blobSize; + Elf64_Xword overallMappedSize; // bytes, required to map the whole ADLT image } adlt_section_header_t; static const char adltBlobStartMark[4] = { 0xA, 0xD, 0x1, 0x7 }; diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp index 6ac5296c1530..8f51cc9600a6 100644 --- a/lld/ELF/SyntheticSections.cpp +++ b/lld/ELF/SyntheticSections.cpp @@ -4020,8 +4020,8 @@ static_assert(sizeof(adltBlobStartMark) == 4, "0xad17 consist of 4 bytes" ); -static_assert(sizeof(adlt_section_header_t) == 32, - "please udpate major version if header has been changed" +static_assert(sizeof(adlt_section_header_t) == 40, + "please udpate version if header has been changed" ); static_assert(sizeof(adlt_psod_t) == 128, @@ -4058,8 +4058,11 @@ void AdltSection::finalizeContents() { ADLT_HASH_TYPE_GNU_HASH, // .stringHashType getBlobStartOffset(), // .blobStart estimateBlobSize(), // .blobSize + 0, // .overallMappedSize }; + // TODO: estimate and fill overallMappedSize + buildSonameIndex(); linkInternalDtNeeded(); extractInitFiniArray(); -- Gitee From f6d526311178926c89ef0abd42bab38f26de3573 Mon Sep 17 00:00:00 2001 From: likholatovevgeny Date: Mon, 8 Apr 2024 11:17:54 +0300 Subject: [PATCH 41/77] Added adlt segment type Signed-off-by: likholatovevgeny --- lld/ELF/Writer.cpp | 4 ++++ llvm/tools/llvm-objdump/ELFDump.cpp | 3 +++ 2 files changed, 7 insertions(+) diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index de6fb6eefb0b..79de07611662 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -2360,6 +2360,10 @@ SmallVector Writer::createPhdrs(Partition &part) { if (OutputSection *cmd = findSection(".interp", partNo)) addHdr(PT_INTERP, cmd->getPhdrFlags())->add(cmd); + // PT_ADLT info. + if (OutputSection *adlt = findSection(".adlt", partNo)) + addHdr(PT_ADLT, adlt->getPhdrFlags())->add(adlt); + // Add the headers. We will remove them if they don't fit. // In the other partitions the headers are ordinary sections, so they don't // need to be added here. diff --git a/llvm/tools/llvm-objdump/ELFDump.cpp b/llvm/tools/llvm-objdump/ELFDump.cpp index 68c635abf99c..f0d1e092ad55 100644 --- a/llvm/tools/llvm-objdump/ELFDump.cpp +++ b/llvm/tools/llvm-objdump/ELFDump.cpp @@ -261,6 +261,9 @@ static void printProgramHeaders(const ELFFile &Obj, StringRef FileName) { outs() << " OHOS_RANDOMDATA "; break; // OHOS_LOCAL end + case ELF::PT_ADLT: + outs() << " ADLT "; + break; case ELF::PT_PHDR: outs() << " PHDR "; break; -- Gitee From 6c8a7371af93ace99a5d5148b5f764c13a8db990 Mon Sep 17 00:00:00 2001 From: Khomutov Nikita Date: Wed, 10 Apr 2024 11:03:48 +0300 Subject: [PATCH 42/77] [ADLT] filll program headers to .adlt section Signed-off-by: Khomutov Nikita Change-Id: Ie27268acce063a98cf3cac7dbd4781c0c2e881e8 --- lld/ELF/SyntheticSections.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp index 8f51cc9600a6..2e2204392008 100644 --- a/lld/ELF/SyntheticSections.cpp +++ b/lld/ELF/SyntheticSections.cpp @@ -4139,7 +4139,10 @@ AdltSection::makeSoData(const SharedFileExtended* soext) { // TODO: fill data.sharedLocalIndex // TODO: fill data.sharedGlobalIndex - // TODO: fill data.phIndexes + + std::copy(soext->programHeaderIndexes.begin(), + soext->programHeaderIndexes.end(), + std::back_inserter(data.phIndexes)); return data; } -- Gitee From 95b6ddaf1f596a9306a3f01e61b94fd66c3a701e Mon Sep 17 00:00:00 2001 From: Khomutov Nikita Date: Thu, 11 Apr 2024 02:52:53 +0000 Subject: [PATCH 43/77] !446 [ADLT] add .rela.dyn minimap to .adlt section * review fix: add ifdef for elf.h include * review fix: better naming for common data * [ADLT] .adlt section: * [ADLT] add relaDyn segments to .adlt section * [ADLT] cleanups Signed-off-by: Khomutov Nikita --- lld/ELF/ADLTSection.h | 18 ++++- lld/ELF/SyntheticSections.cpp | 134 ++++++++++++++++++++++------------ lld/ELF/SyntheticSections.h | 25 ++++++- 3 files changed, 123 insertions(+), 54 deletions(-) diff --git a/lld/ELF/ADLTSection.h b/lld/ELF/ADLTSection.h index 3441ddb8af29..1327de5950ec 100644 --- a/lld/ELF/ADLTSection.h +++ b/lld/ELF/ADLTSection.h @@ -29,6 +29,7 @@ namespace adlt { #include #endif // __cplusplus +#ifndef _ELF_H // part of #include typedef uint16_t Elf64_Half; typedef uint64_t Elf64_Off; @@ -36,6 +37,7 @@ typedef uint64_t Elf64_Addr; typedef uint32_t Elf64_Word; typedef uint64_t Elf64_Xword; typedef uint8_t Elf64_Byte; +#endif // _ELF_H typedef struct { Elf64_Word secIndex; @@ -66,12 +68,18 @@ typedef struct { } adlt_semver_t; // DT_NEEDED string index with embedded PSOD index if available -typedef struct { +typedef struct { Elf64_Off hasInternalPSOD : 1; // true if soname Elf64_Off PSODindex : 16; // PSOD in the current ADLT image Elf64_Off sonameOffset : 47; // string start in bound .adlt.strtab } adlt_dt_needed_index_t; +typedef struct { + Elf64_Word relType; // relocation type + Elf64_Word count; // segment length + Elf64_Off startOffset; // segment start offset in .rela.dyn +} adlt_relocations_segment_t; + typedef enum { ADLT_HASH_TYPE_NONE = 0, ADLT_HASH_TYPE_GNU_HASH = 1, @@ -85,13 +93,15 @@ typedef uint8_t adlt_hash_type_t; // Serializable representation per-shared-object-data in .adlt section typedef struct { Elf64_Off soName; // offset in .adlt.strtab - Elf64_Xword soNameHash; // algorithm according to header.stringHashType value + Elf64_Xword soNameHash; // algorithm according to hdr.stringHashType value adlt_cross_section_array_t initArray; adlt_cross_section_array_t finiArray; adlt_blob_array_t dtNeeded; // array of adlt_dt_needed_index_t[] elems adlt_cross_section_ref_t sharedLocalSymbolIndex; adlt_cross_section_ref_t sharedGlobalSymbolIndex; - adlt_blob_u16_array_t phIndexes; // program header index array, typeof(e_phnum) + adlt_blob_u16_array_t phIndexes; // program header indexes, typeof(e_phnum) + adlt_blob_array_t relaDynSegs; // lib's adlt_relocations_segment_t[] + adlt_blob_array_t relaPltSegs; // lib's adlt_relocations_segment_t[] } adlt_psod_t; typedef struct { @@ -103,6 +113,8 @@ typedef struct { Elf64_Off blobStart; // offset of binary blob start relative to .adlt Elf64_Xword blobSize; Elf64_Xword overallMappedSize; // bytes, required to map the whole ADLT image + adlt_blob_array_t relaDynSegs; // common adlt_relocations_segment_t[] + adlt_blob_array_t relaPltSegs; // common adlt_relocations_segment_t[] } adlt_section_header_t; static const char adltBlobStartMark[4] = { 0xA, 0xD, 0x1, 0x7 }; diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp index 2e2204392008..33ae89538d56 100644 --- a/lld/ELF/SyntheticSections.cpp +++ b/lld/ELF/SyntheticSections.cpp @@ -4011,20 +4011,30 @@ static_assert( "adlt_dt_needed_index_t have to be an offset with intrused flags" ); +static_assert( + sizeof(adlt_relocations_segment_t) == 16, + "adlt_relocations_segment_t size is 16 bytes" +); + static_assert( sizeof(adlt_hash_type_t) == sizeof(Elf64_Byte), "String hash type enum should occupy only one byte" ); +static_assert( + sizeof(adlt_blob_array_t) == 16, + "blob array reference occupies 16 bytes in PSOD" +); + static_assert(sizeof(adltBlobStartMark) == 4, "0xad17 consist of 4 bytes" ); -static_assert(sizeof(adlt_section_header_t) == 40, - "please udpate version if header has been changed" +static_assert(sizeof(adlt_section_header_t) == 72, + "please update version if header has been changed" ); -static_assert(sizeof(adlt_psod_t) == 128, +static_assert(sizeof(adlt_psod_t) == 160, "please udpate version if adlt_psod_t layout or content changed" ); @@ -4050,6 +4060,8 @@ void AdltSection::finalizeContents() { "the number of input libs exeeds ELF limit on number of sections"); const Elf64_Half soNum = soInputs.size(); + common = makeCommonData(); + header = adlt_section_header_t{ adltSchemaVersion, // .schemaVersion sizeof(adlt_section_header_t), // .schemaHeaderSize @@ -4059,6 +4071,8 @@ void AdltSection::finalizeContents() { getBlobStartOffset(), // .blobStart estimateBlobSize(), // .blobSize 0, // .overallMappedSize + {0, 0}, // .relaDynSegs, filled in writeTo + {0, 0}, // .relaPltSegs, filled in writeTo }; // TODO: estimate and fill overallMappedSize @@ -4119,6 +4133,18 @@ void AdltSection::extractInitFiniArray() { } } +template +typename AdltSection::CommonData +AdltSection::makeCommonData() { + // TODO: fill relaDynSegs + // TODO: fill relaPltSegs + + return CommonData { + {}, // .relaDynSegs + {}, // .relaPltSegs + }; +} + template typename AdltSection::SoData AdltSection::makeSoData(const SharedFileExtended* soext) { @@ -4139,6 +4165,8 @@ AdltSection::makeSoData(const SharedFileExtended* soext) { // TODO: fill data.sharedLocalIndex // TODO: fill data.sharedGlobalIndex + // TODO: fill data.relaDynSegs + // TODO: fill data.relaPltSegs std::copy(soext->programHeaderIndexes.begin(), soext->programHeaderIndexes.end(), @@ -4170,27 +4198,18 @@ adlt_psod_t AdltSection::serialize(const SoData& soData) const { soData.initArraySec->sectionIndex, 0x0, // .init_array function addresses are located at the start soData.initArraySec->size, // size - } : adlt_cross_section_array_t{0, 0, 0}, + } : adlt_cross_section_array_t{}, soData.finiArraySec ? adlt_cross_section_array_t{ // .finiArray soData.finiArraySec->sectionIndex, 0x0, // .fini_array function addresses are located at the start soData.finiArraySec->size, // size - } : adlt_cross_section_array_t{0, 0, 0}, - adlt_blob_array_t { // .dtNeeded - 0x0, // offset, filled array write in blob - soData.dtNeededs.size() * sizeof(adlt_dt_needed_index_t) - }, - adlt_cross_section_ref_t { // .sharedLocalSymbolIndex - 0, // .secIndex // TODO: dynsym? - soData.sharedLocalIndex, // .offset - }, - adlt_cross_section_ref_t { // .sharedGlobalSymbolIndex - 0, // .secIndex // TODO: dynsym? - soData.sharedGlobalIndex, // .offset - }, - adlt_blob_u16_array_t { // .phIndexes - 0x0, 0 - } + } : adlt_cross_section_array_t{}, + adlt_blob_array_t {}, // .dtNeeded, filled in writeTo + adlt_cross_section_ref_t {}, // .sharedLocalSymbolIndex TODO + adlt_cross_section_ref_t {}, // .sharedGlobalSymbolIndex TOOD + adlt_blob_u16_array_t {}, // .phIndexes, filled in writeTo + adlt_blob_array_t {}, // .relaDynSegs, filled in writeTo + adlt_blob_array_t {}, // .relaPltSegs, filled in writeTo }; } @@ -4206,15 +4225,32 @@ size_t AdltSection::estimateBlobSize() const { for (const auto& soData: soInputs) { blobSize += sizeof(adlt_dt_needed_index_t) * soData.dtNeededs.size(); blobSize += sizeof(uint16_t) * soData.phIndexes.size(); + blobSize += sizeof(adlt_relocations_segment_t) * soData.relaDynSegs.size(); + blobSize += sizeof(adlt_relocations_segment_t) * soData.relaPltSegs.size(); }; + blobSize += sizeof(adlt_relocations_segment_t) * common.relaDynSegs.size(); + blobSize += sizeof(adlt_relocations_segment_t) * common.relaPltSegs.size(); + return blobSize; } template -size_t AdltSection::writeDtNeededVec( - uint8_t* buff, const DtNeededsVec& neededVec) const { - if (neededVec.empty()) return 0; +template +adlt_blob_array_t AdltSection::writeArray( + uint8_t* buff, size_t offset, const SmallVector& data) { + if (data.empty()) return {0x0, 0}; + + const size_t to_write = data.size_in_bytes(); + memcpy(buff + offset, data.data(), to_write); + + return {offset, to_write}; +} + +template +adlt_blob_array_t AdltSection::writeDtNeeded( + uint8_t* buff, size_t offset, const DtNeededsVec& neededVec) { + if (neededVec.empty()) return {0x0, 0}; SmallVector needIndexes; needIndexes.reserve(neededVec.size()); @@ -4226,8 +4262,7 @@ size_t AdltSection::writeDtNeededVec( }); } - memcpy(buff, needIndexes.data(), needIndexes.size_in_bytes()); - return needIndexes.size_in_bytes(); + return writeArray(buff, offset, needIndexes); } template @@ -4244,39 +4279,42 @@ void AdltSection::writeTo(uint8_t* buf) { // serialize blob data { - uint8_t* const blob_buf = buf + header.blobStart; - size_t blob_off = 0; - memcpy(blob_buf + blob_off, &adltBlobStartMark, sizeof(adltBlobStartMark)); - blob_off += sizeof(adltBlobStartMark); + uint8_t* const blobBuf = buf + header.blobStart; + size_t blobOff = 0; + memcpy(blobBuf + blobOff, &adltBlobStartMark, sizeof(adltBlobStartMark)); + blobOff += sizeof(adltBlobStartMark); - // dt-needed + // psod-related data for(const auto& it : llvm::enumerate(soInputs)) { - const SoData& soData = it.value(); - adlt_psod_t& psod = psods[it.index()]; + const auto& soData = it.value(); + auto& psod = psods[it.index()]; + + psod.dtNeeded = writeDtNeeded(blobBuf, blobOff, soData.dtNeededs); + blobOff += psod.dtNeeded.size; + + psod.phIndexes = writeArray(blobBuf, blobOff, soData.phIndexes); + blobOff += psod.phIndexes.size; - const size_t written = writeDtNeededVec(blob_buf + blob_off, soData.dtNeededs); - psod.dtNeeded.offset = blob_off; - psod.dtNeeded.size = written; - blob_off += written; + psod.relaDynSegs = writeArray(blobBuf, blobOff, soData.relaDynSegs); + blobOff += psod.relaDynSegs.size; + + psod.relaPltSegs = writeArray(blobBuf, blobOff, soData.relaPltSegs); + blobOff += psod.relaPltSegs.size; } - // phIndexes - for (const auto& it : llvm::enumerate(soInputs)) { - const SoData& soData = it.value(); - adlt_psod_t& psod = psods[it.index()]; + // common data + { + header.relaDynSegs = writeArray(blobBuf, blobOff, common.relaDynSegs); + blobOff += header.relaDynSegs.size; - const size_t written = soData.phIndexes.size_in_bytes();; - memcpy(blob_buf + blob_off, - soData.phIndexes.data(), soData.phIndexes.size_in_bytes()); - psod.phIndexes.offset = blob_off; - psod.phIndexes.size = written; - blob_off += written; + header.relaPltSegs = writeArray(blobBuf, blobOff, common.relaPltSegs); + blobOff += header.relaPltSegs.size; } // finalize header.blobSize - assert((blob_off <= header.blobSize) && + assert((blobOff <= header.blobSize) && ".adlt-section: blob output exeeds its initial estimation"); - header.blobSize = blob_off; + header.blobSize = blobOff; } // header diff --git a/lld/ELF/SyntheticSections.h b/lld/ELF/SyntheticSections.h index 8292495940fa..c69b95704406 100644 --- a/lld/ELF/SyntheticSections.h +++ b/lld/ELF/SyntheticSections.h @@ -1257,6 +1257,14 @@ public: Elf64_Off sharedGlobalIndex; // TODO SmallVector phIndexes; // TODO + SmallVector relaDynSegs; // TODO + SmallVector relaPltSegs; // TODO + }; + + // will be used to form some header data + struct CommonData { + SmallVector relaDynSegs; // TODO + SmallVector relaPltSegs; // TODO }; public: @@ -1275,18 +1283,29 @@ private: void linkInternalDtNeeded(); void extractInitFiniArray(); - Elf64_Xword calculateHash(StringRef str) const; + CommonData makeCommonData(); SoData makeSoData(const SharedFileExtended*); + + Elf64_Xword calculateHash(StringRef str) const; adlt_psod_t serialize(const SoData&) const; size_t estimateBlobSize() const; - size_t writeDtNeededVec(uint8_t* buff, const DtNeededsVec& neededVec) const; + + template + adlt_blob_array_t writeArray(uint8_t* buff, size_t offset, + const SmallVector& data); + + adlt_blob_array_t writeDtNeeded(uint8_t* buff, size_t offset, + const DtNeededsVec& neededVec); private: StringTableSection& strTabSec; adlt_section_header_t header = {}; - SmallVector soInputs; + + CommonData common = {}; + SmallVector soInputs; + llvm::DenseMap sonameToIndexMap; }; -- Gitee From d7e8b042f25e86db7b834efbd69040daa0683f9b Mon Sep 17 00:00:00 2001 From: Khomutov Nikita Date: Thu, 11 Apr 2024 10:04:43 +0300 Subject: [PATCH 44/77] [ADLT] write overall mapped image size to .adlt Signed-off-by: Khomutov Nikita Change-Id: I83b40245dc6700f0714a0910fd8f4878cdbceeb1 --- lld/ELF/SyntheticSections.cpp | 21 ++++++++++++++++++--- lld/ELF/SyntheticSections.h | 7 ++++--- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp index 33ae89538d56..4e368aadf62a 100644 --- a/lld/ELF/SyntheticSections.cpp +++ b/lld/ELF/SyntheticSections.cpp @@ -4070,13 +4070,11 @@ void AdltSection::finalizeContents() { ADLT_HASH_TYPE_GNU_HASH, // .stringHashType getBlobStartOffset(), // .blobStart estimateBlobSize(), // .blobSize - 0, // .overallMappedSize + 0, // .overallMappedSize, known on writeTo {0, 0}, // .relaDynSegs, filled in writeTo {0, 0}, // .relaPltSegs, filled in writeTo }; - // TODO: estimate and fill overallMappedSize - buildSonameIndex(); linkInternalDtNeeded(); extractInitFiniArray(); @@ -4133,6 +4131,15 @@ void AdltSection::extractInitFiniArray() { } } +template +size_t AdltSection::estimateOverallMappedSize() { + size_t totalMemsz = 0; + for (PhdrEntry* ph : mainPart->phdrs) + if (ph->p_type == PT_LOAD) + totalMemsz += ph->p_memsz; + return totalMemsz; +} + template typename AdltSection::CommonData AdltSection::makeCommonData() { @@ -4265,6 +4272,12 @@ adlt_blob_array_t AdltSection::writeDtNeeded( return writeArray(buff, offset, needIndexes); } +template +void AdltSection::finalizeOnWrite() { + // require Writer::setPhdrs previously been called + header.overallMappedSize = estimateOverallMappedSize(); +} + template void AdltSection::writeTo(uint8_t* buf) { // TODO: take care of endianness, use write32 / write64 etc. @@ -4277,6 +4290,8 @@ void AdltSection::writeTo(uint8_t* buf) { psods.push_back(psod); } + finalizeOnWrite(); + // serialize blob data { uint8_t* const blobBuf = buf + header.blobStart; diff --git a/lld/ELF/SyntheticSections.h b/lld/ELF/SyntheticSections.h index c69b95704406..a93ff052cfe3 100644 --- a/lld/ELF/SyntheticSections.h +++ b/lld/ELF/SyntheticSections.h @@ -1282,15 +1282,16 @@ private: void buildSonameIndex(); void linkInternalDtNeeded(); void extractInitFiniArray(); + size_t estimateOverallMappedSize(); + Elf64_Xword calculateHash(StringRef str) const; CommonData makeCommonData(); SoData makeSoData(const SharedFileExtended*); - - Elf64_Xword calculateHash(StringRef str) const; adlt_psod_t serialize(const SoData&) const; - size_t estimateBlobSize() const; + void finalizeOnWrite(); + template adlt_blob_array_t writeArray(uint8_t* buff, size_t offset, const SmallVector& data); -- Gitee From e67b837d650672b1c7e53e60d6dc8a3abecca3cd Mon Sep 17 00:00:00 2001 From: Khomutov Nikita Date: Fri, 12 Apr 2024 06:19:27 +0300 Subject: [PATCH 45/77] [ADLT] add assert on ph-indexes shifting Calling removeEmptyPTLoad may shift generated program header indexes prepared for .adlt section. The case if optimization applied requires another implementation of extracting program header indexes and not supported by the moment. Signed-off-by: Khomutov Nikita Change-Id: I11f3a43b5fe88124b60bdb36ef1e8fbfc74a2951 --- lld/ELF/Writer.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index 79de07611662..3b176ab56020 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -116,6 +116,11 @@ static void removeEmptyPTLoad(SmallVector &phdrs) { for (OutputSection *sec : outputSections) if (removed.count(sec->ptLoad)) sec->ptLoad = nullptr; + + if (config->adlt) { + assert(it == phdrs.end() && "adlt: ph-index in .adlt invalid due to shift"); + } + phdrs.erase(it, phdrs.end()); } -- Gitee From e7e7f529de78dbc9da69317c12e8058873aea643 Mon Sep 17 00:00:00 2001 From: Khomutov Nikita Date: Tue, 16 Apr 2024 10:32:17 +0300 Subject: [PATCH 46/77] [ADLT] shared-symbol-indexes - impelemted SO-local symbol sorting in .symtab - extracted shared-local-symbol-index and shared-global-symbol-index, written to .adlt - impl sysv hash for .adlt - cleaups Signed-off-by: Khomutov Nikita Change-Id: I8127bc662d0f4b9a6cccf9c48a6247db7a5cbde4 Signed-off-by: Khomutov Nikita --- lld/ELF/InputFiles.cpp | 1 + lld/ELF/InputFiles.h | 7 +++ lld/ELF/SyntheticSections.cpp | 100 +++++++++++++++++++++++++++++++--- lld/ELF/SyntheticSections.h | 9 ++- lld/ELF/Writer.cpp | 2 +- 5 files changed, 106 insertions(+), 13 deletions(-) diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp index 0b140aa8aa07..bbdd4f6b4a26 100644 --- a/lld/ELF/InputFiles.cpp +++ b/lld/ELF/InputFiles.cpp @@ -186,6 +186,7 @@ template static void doParseFile(InputFile *file) { // .so file if (config->adlt) if (auto *f = dyn_cast>(file)) { + f->orderIdx = ctx->sharedFilesExtended.size(); ctx->sharedFilesExtended.push_back(cast(file)); f->parseForAdlt(); return; diff --git a/lld/ELF/InputFiles.h b/lld/ELF/InputFiles.h index 5517c153a9b0..6279f81ad150 100644 --- a/lld/ELF/InputFiles.h +++ b/lld/ELF/InputFiles.h @@ -444,11 +444,18 @@ public: void traceSection(const SectionBase &sec, StringRef title = "") const; public: + // the input order of the file as it presented in ADLT image + size_t orderIdx; int dynSymSecIdx = 0; int symTabSecIdx = 0; int symTabShndxSecIdx = 0; int eFirstGlobal = 0; + // .symtab's start of local symbols owned by library + llvm::Optional sharedLocalSymbolIndex; + // .symtab's start of global symbols owned by library + llvm::Optional sharedGlobalSymbolIndex; + // Output information data: llvm::SetVector programHeaderIndexes; // TODO: dynamic relocation indexes diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp index 4e368aadf62a..73a11c1aedd7 100644 --- a/lld/ELF/SyntheticSections.cpp +++ b/lld/ELF/SyntheticSections.cpp @@ -2243,6 +2243,11 @@ void SymbolTableBaseSection::sortSymTabSymbols() { size_t numLocals = e - symbols.begin(); getParent()->info = numLocals + 1; + if (config->adlt && this->type == SHT_SYMTAB) { + invokeELFT(sortSymTabSymbolsInAdlt, numLocals); + return; + } + // We want to group the local symbols by file. For that we rebuild the local // part of the symbols vector. We do not need to care about the STT_FILE // symbols, they are already naturally placed first in each group. That @@ -2258,6 +2263,50 @@ void SymbolTableBaseSection::sortSymTabSymbols() { *i++ = entry; } +template +void SymbolTableBaseSection::sortSymTabSymbolsInAdlt(size_t numLocals) { + auto localEnd = symbols.begin() + numLocals; + + using SortKey = std::tuple; + auto makeKey = [](const SymbolTableEntry& ent) -> SortKey { + const InputFile* file = ent.sym->file; + if(auto* soext = dyn_cast_or_null>(file)) { + return {static_cast(soext->orderIdx), file}; + } + return {-1, file}; + }; + + for (InputFile* file : ctx->sharedFilesExtended) { + auto* soext = cast>(file); + soext->sharedLocalSymbolIndex = llvm::None; + soext->sharedGlobalSymbolIndex = llvm::None; + } + + // sort local symbols + llvm::stable_sort(llvm::make_range(symbols.begin(), localEnd), + [makeKey](const SymbolTableEntry& lhs, const SymbolTableEntry& rhs) { + return makeKey(lhs) <= makeKey(rhs); + }); + + // sort global symbols + llvm::stable_sort(llvm::make_range(localEnd, symbols.end()), + [makeKey](const SymbolTableEntry& lhs, const SymbolTableEntry& rhs) { + return makeKey(lhs) <= makeKey(rhs); + }); + + // extract file boundaries for local symbols + for (auto iter = symbols.begin(); iter != localEnd; ++iter) + if (auto* soext = dyn_cast_or_null>(iter->sym->file)) + if (!soext->sharedLocalSymbolIndex) + soext->sharedLocalSymbolIndex = std::distance(symbols.begin(), iter); + + // extract file boundaries for global symbols + for (auto iter = localEnd; iter != symbols.end(); ++iter) + if (auto* soext = dyn_cast_or_null>(iter->sym->file)) + if (!soext->sharedGlobalSymbolIndex) + soext->sharedGlobalSymbolIndex = std::distance(symbols.begin(), iter); +} + void SymbolTableBaseSection::addSymbol(Symbol *b) { // Adding a local symbol to a .dynsym is a bug. assert(this->type != SHT_DYNSYM || !b->isLocal()); @@ -4016,6 +4065,16 @@ static_assert( "adlt_relocations_segment_t size is 16 bytes" ); +static_assert( + sizeof(adlt_cross_section_ref_t) == 16, + "adlt_cross_section_ref_t size is 16 bytes" +); + +static_assert( + sizeof(adlt_cross_section_array_t) == 24, + "adlt_cross_section_ref_t size is 24 bytes" +); + static_assert( sizeof(adlt_hash_type_t) == sizeof(Elf64_Byte), "String hash type enum should occupy only one byte" @@ -4062,6 +4121,7 @@ void AdltSection::finalizeContents() { common = makeCommonData(); + std::memset(&header, 0, sizeof(header)); header = adlt_section_header_t{ adltSchemaVersion, // .schemaVersion sizeof(adlt_section_header_t), // .schemaHeaderSize @@ -4149,6 +4209,7 @@ AdltSection::makeCommonData() { return CommonData { {}, // .relaDynSegs {}, // .relaPltSegs + UINT32_MAX, // .symtabSecIndex, filled in writeTo }; } @@ -4170,8 +4231,6 @@ AdltSection::makeSoData(const SharedFileExtended* soext) { data.initArrayName = soext->addAdltPostfix(".init_array"); data.finiArrayName = soext->addAdltPostfix(".fini_array"); - // TODO: fill data.sharedLocalIndex - // TODO: fill data.sharedGlobalIndex // TODO: fill data.relaDynSegs // TODO: fill data.relaPltSegs @@ -4189,6 +4248,8 @@ Elf64_Xword AdltSection::calculateHash(StringRef str) const { return 0x0; case ADLT_HASH_TYPE_GNU_HASH: return hashGnu(str); + case ADLT_HASH_TYPE_SYSV_HASH: + return hashSysV(str); case ADLT_HASH_TYPE_DEBUG_CONST: return 0xdeadbeef1337c0de; default: @@ -4212,8 +4273,14 @@ adlt_psod_t AdltSection::serialize(const SoData& soData) const { soData.finiArraySec->size, // size } : adlt_cross_section_array_t{}, adlt_blob_array_t {}, // .dtNeeded, filled in writeTo - adlt_cross_section_ref_t {}, // .sharedLocalSymbolIndex TODO - adlt_cross_section_ref_t {}, // .sharedGlobalSymbolIndex TOOD + adlt_cross_section_ref_t { + soData.sharedLocalIndex ? common.symtabSecIndex : UINT32_MAX, + soData.sharedLocalIndex.value_or(0), + }, // .sharedLocalSymbolIndex + adlt_cross_section_ref_t { + soData.sharedGlobalIndex ? common.symtabSecIndex : UINT32_MAX, + soData.sharedGlobalIndex.value_or(0), + }, // .sharedGlobalSymbolIndex adlt_blob_u16_array_t {}, // .phIndexes, filled in writeTo adlt_blob_array_t {}, // .relaDynSegs, filled in writeTo adlt_blob_array_t {}, // .relaPltSegs, filled in writeTo @@ -4276,22 +4343,37 @@ template void AdltSection::finalizeOnWrite() { // require Writer::setPhdrs previously been called header.overallMappedSize = estimateOverallMappedSize(); + + if (OutputSection* osec = findOutSection(".symtab")) { + common.symtabSecIndex = osec->sectionIndex; + } + + for (auto& it: llvm::enumerate(soInputs)) { + finalizeOnWrite(it.index(), it.value()); + } +} + +template +void AdltSection::finalizeOnWrite(size_t idx, SoData& soData) { + auto* soext = cast>(ctx->sharedFilesExtended[idx]); + + // require SymbolTableBaseSection::sortSymTabSymbolsInAdlt for .symtab called + soData.sharedLocalIndex = soext->sharedLocalSymbolIndex; + soData.sharedGlobalIndex = soext->sharedGlobalSymbolIndex; } template void AdltSection::writeTo(uint8_t* buf) { // TODO: take care of endianness, use write32 / write64 etc. + finalizeOnWrite(); + // pre-serialized SoData, enriched with offsets during blob writing SmallVector psods; - for (const auto& it: llvm::enumerate(soInputs)) { const SoData& soData = it.value(); - adlt_psod_t psod = serialize(soData); - psods.push_back(psod); + psods.push_back(serialize(soData)); } - finalizeOnWrite(); - // serialize blob data { uint8_t* const blobBuf = buf + header.blobStart; diff --git a/lld/ELF/SyntheticSections.h b/lld/ELF/SyntheticSections.h index a93ff052cfe3..9afb747f29c2 100644 --- a/lld/ELF/SyntheticSections.h +++ b/lld/ELF/SyntheticSections.h @@ -645,6 +645,7 @@ public: protected: void sortSymTabSymbols(); + template void sortSymTabSymbolsInAdlt(size_t numLocals); // A vector of symbols and their string table offsets. SmallVector symbols; @@ -1253,10 +1254,10 @@ public: OutputSection* initArraySec; OutputSection* finiArraySec; - Elf64_Off sharedLocalIndex; // TODO - Elf64_Off sharedGlobalIndex; // TODO + llvm::Optional sharedLocalIndex; + llvm::Optional sharedGlobalIndex; - SmallVector phIndexes; // TODO + SmallVector phIndexes; SmallVector relaDynSegs; // TODO SmallVector relaPltSegs; // TODO }; @@ -1265,6 +1266,7 @@ public: struct CommonData { SmallVector relaDynSegs; // TODO SmallVector relaPltSegs; // TODO + uint32_t symtabSecIndex = UINT32_MAX; }; public: @@ -1291,6 +1293,7 @@ private: size_t estimateBlobSize() const; void finalizeOnWrite(); + void finalizeOnWrite(size_t idx, SoData& sodata); template adlt_blob_array_t writeArray(uint8_t* buff, size_t offset, diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index 3b176ab56020..fb2f260f3d94 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -2462,7 +2462,7 @@ SmallVector Writer::createPhdrs(Partition &part) { // Check outputs for (ELFFileBase *baseFile : ctx->sharedFilesExtended) { - auto *soFile = cast>(baseFile); + __attribute__((unused)) auto *soFile = cast>(baseFile); assert(!soFile->programHeaderIndexes.empty()); } } -- Gitee From 364eecb93d9aa7b745cfb13c7b136aef1174ce5b Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Fri, 19 Apr 2024 03:53:02 -0400 Subject: [PATCH 47/77] Added common header + debug trace. Change-Id: Idfaa883c681454581be7fb563261368079a59ba5 Signed-off-by: Anton Volkov --- lld/ELF/Writer.cpp | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index fb2f260f3d94..11e3abf34a71 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -116,7 +116,7 @@ static void removeEmptyPTLoad(SmallVector &phdrs) { for (OutputSection *sec : outputSections) if (removed.count(sec->ptLoad)) sec->ptLoad = nullptr; - + if (config->adlt) { assert(it == phdrs.end() && "adlt: ph-index in .adlt invalid due to shift"); } @@ -2376,6 +2376,11 @@ SmallVector Writer::createPhdrs(Partition &part) { load = addHdr(PT_LOAD, flags); load->add(Out::elfHeader); load->add(Out::programHeaders); + if (config->adlt) // add common header for all libs + for (auto *base : ctx->sharedFilesExtended) { + auto *soFile = cast>(base); + soFile->programHeaderIndexes.insert(getCurrentIndex()); + } } } @@ -2461,9 +2466,19 @@ SmallVector Writer::createPhdrs(Partition &part) { } // Check outputs + auto adltTraceNeeded = false; // debug hint + auto trace = [&](auto *file, auto &indexes) { + lld::outs() << "ADLT: " << file->soName << ":\n"; + for (auto &it : indexes) + lld::outs() << it << " "; + lld::outs() << '\n'; + }; for (ELFFileBase *baseFile : ctx->sharedFilesExtended) { - __attribute__((unused)) auto *soFile = cast>(baseFile); - assert(!soFile->programHeaderIndexes.empty()); + auto *soFile = cast>(baseFile); + auto &indexes = soFile->programHeaderIndexes; + assert(!indexes.empty()); + if (adltTraceNeeded) + trace(soFile, indexes); } } -- Gitee From 5aa0fb8c2acb75cee763a165812f12f7d175d133 Mon Sep 17 00:00:00 2001 From: Khomutov Nikita Date: Fri, 19 Apr 2024 11:31:29 +0300 Subject: [PATCH 48/77] [ADLT][readelf] add --adlt-section option for pretty .adlt section dump - added --adlt-section for readobj tool (readelf with LLVM style output) - ADLTSection header moved to the common space as a part of binary interface - looking and extracting .adlt and .adlt.strtab sections - parsing .adlt binary format, dumping it in a pretty way Signed-off-by: Khomutov Nikita Change-Id: I816f02f17a3d983e3b737494c79d2c006874d337 --- lld/ELF/ADLTSection.h | 130 ------------------- lld/ELF/SyntheticSections.h | 5 +- llvm/include/llvm/BinaryFormat/ADLTSection.h | 16 +-- 3 files changed, 11 insertions(+), 140 deletions(-) delete mode 100644 lld/ELF/ADLTSection.h diff --git a/lld/ELF/ADLTSection.h b/lld/ELF/ADLTSection.h deleted file mode 100644 index 1327de5950ec..000000000000 --- a/lld/ELF/ADLTSection.h +++ /dev/null @@ -1,130 +0,0 @@ -//===- ADLTSection.h - ADLT Section data types --------------------*- C -*-===// -// -// Copyright (C) 2024 Huawei Device Co., Ltd. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -//===----------------------------------------------------------------------===// - -#ifndef LLD_ELF_ADLT_SECTION_H -#define LLD_ELF_ADLT_SECTION_H - -#ifdef __cplusplus -#include -namespace lld { -namespace elf { -namespace adlt { - -#else // __cplusplus -#include -#endif // __cplusplus - -#ifndef _ELF_H -// part of #include -typedef uint16_t Elf64_Half; -typedef uint64_t Elf64_Off; -typedef uint64_t Elf64_Addr; -typedef uint32_t Elf64_Word; -typedef uint64_t Elf64_Xword; -typedef uint8_t Elf64_Byte; -#endif // _ELF_H - -typedef struct { - Elf64_Word secIndex; - Elf64_Off offset; // from section start -} adlt_cross_section_ref_t; - -typedef struct { - Elf64_Word secIndex; - Elf64_Off offset; // from section start - Elf64_Xword size; // size in bytes -} adlt_cross_section_array_t; - -typedef struct { - Elf64_Off offset; // relative to header.blobStart - Elf64_Xword size; // size in bytes, make convertions for data type -} adlt_blob_array_t; - -// plain-C has no strict typedefs, but aliases used to interpred underlying data -typedef adlt_blob_array_t adlt_blob_u8_array_t; // uint8_t[] -typedef adlt_blob_array_t adlt_blob_u16_array_t; // uint16_t[] -typedef adlt_blob_array_t adlt_blob_u32_array_t; // uint32_t[] -typedef adlt_blob_array_t adlt_blob_u64_array_t; // uint64_t[] - -typedef struct { - Elf64_Half major: 6; - Elf64_Half minor: 6; - Elf64_Half patch: 4; -} adlt_semver_t; - -// DT_NEEDED string index with embedded PSOD index if available -typedef struct { - Elf64_Off hasInternalPSOD : 1; // true if soname - Elf64_Off PSODindex : 16; // PSOD in the current ADLT image - Elf64_Off sonameOffset : 47; // string start in bound .adlt.strtab -} adlt_dt_needed_index_t; - -typedef struct { - Elf64_Word relType; // relocation type - Elf64_Word count; // segment length - Elf64_Off startOffset; // segment start offset in .rela.dyn -} adlt_relocations_segment_t; - -typedef enum { - ADLT_HASH_TYPE_NONE = 0, - ADLT_HASH_TYPE_GNU_HASH = 1, - ADLT_HASH_TYPE_SYSV_HASH = 2, - ADLT_HASH_TYPE_DEBUG_CONST = 0xfe, - ADLT_HASH_TYPE_MAX = 0xff, -} adlt_hash_type_enum_t; - -typedef uint8_t adlt_hash_type_t; - -// Serializable representation per-shared-object-data in .adlt section -typedef struct { - Elf64_Off soName; // offset in .adlt.strtab - Elf64_Xword soNameHash; // algorithm according to hdr.stringHashType value - adlt_cross_section_array_t initArray; - adlt_cross_section_array_t finiArray; - adlt_blob_array_t dtNeeded; // array of adlt_dt_needed_index_t[] elems - adlt_cross_section_ref_t sharedLocalSymbolIndex; - adlt_cross_section_ref_t sharedGlobalSymbolIndex; - adlt_blob_u16_array_t phIndexes; // program header indexes, typeof(e_phnum) - adlt_blob_array_t relaDynSegs; // lib's adlt_relocations_segment_t[] - adlt_blob_array_t relaPltSegs; // lib's adlt_relocations_segment_t[] -} adlt_psod_t; - -typedef struct { - adlt_semver_t schemaVersion; // {major, minor, patch} - Elf64_Half schemaHeaderSize; // >= sizeof(adlt_section_header_t) if comp - Elf64_Half schemaPSODSize; // >= sizeof(adlt_psod_t) if compatible - Elf64_Half sharedObjectsNum; // number of PSOD entries - adlt_hash_type_t stringHashType; // contains adlt_hash_type_enum_t value - Elf64_Off blobStart; // offset of binary blob start relative to .adlt - Elf64_Xword blobSize; - Elf64_Xword overallMappedSize; // bytes, required to map the whole ADLT image - adlt_blob_array_t relaDynSegs; // common adlt_relocations_segment_t[] - adlt_blob_array_t relaPltSegs; // common adlt_relocations_segment_t[] -} adlt_section_header_t; - -static const char adltBlobStartMark[4] = { 0xA, 0xD, 0x1, 0x7 }; - -static const adlt_semver_t adltSchemaVersion = {1, 0, 0}; - -#ifdef __cplusplus -} // namespace adlt -} // namespace elf -} // namespace lld -#endif // __cplusplus - -#endif // LLD_ELF_ADLT_SECTION_H diff --git a/lld/ELF/SyntheticSections.h b/lld/ELF/SyntheticSections.h index 9afb747f29c2..09189e39f1d6 100644 --- a/lld/ELF/SyntheticSections.h +++ b/lld/ELF/SyntheticSections.h @@ -22,10 +22,10 @@ #include "Config.h" #include "EhFrame.h" // OHOS_LOCAL -#include "ADLTSection.h" // OHOS_LOCAL #include "InputSection.h" #include "llvm/ADT/DenseSet.h" #include "llvm/ADT/MapVector.h" +#include "llvm/BinaryFormat/ADLTSection.h" // OHOS_LOCAL #include "llvm/MC/StringTableBuilder.h" #include "llvm/Support/Endian.h" #include "llvm/Support/Threading.h" @@ -1226,6 +1226,8 @@ public: namespace adlt { +using namespace llvm::adlt; + template class AdltSection final : public SyntheticSection { public: @@ -1316,7 +1318,6 @@ private: } // namespace adlt - InputSection *createInterpSection(); MergeInputSection *createCommentSection(); template void splitSections(); diff --git a/llvm/include/llvm/BinaryFormat/ADLTSection.h b/llvm/include/llvm/BinaryFormat/ADLTSection.h index 6d57903cdd09..79b7eeab634d 100644 --- a/llvm/include/llvm/BinaryFormat/ADLTSection.h +++ b/llvm/include/llvm/BinaryFormat/ADLTSection.h @@ -69,17 +69,17 @@ typedef struct { // DT_NEEDED string index with embedded PSOD index if available typedef struct { - Elf64_Off hasInternalPSOD : 1; // true if soname - Elf64_Off PSODindex : 16; // PSOD in the current ADLT image - Elf64_Off sonameOffset : 47; // string start in bound .adlt.strtab + Elf64_Off hasInternalPSOD : 1; // true if soname + Elf64_Off PSODindex : 16; // PSOD in the current ADLT image + Elf64_Off sonameOffset : 47; // string start in bound .adlt.strtab } adlt_dt_needed_index_t; typedef enum { - ADLT_HASH_TYPE_NONE = 0, - ADLT_HASH_TYPE_GNU_HASH = 1, - ADLT_HASH_TYPE_SYSV_HASH = 2, - ADLT_HASH_TYPE_DEBUG_CONST = 0xfe, - ADLT_HASH_TYPE_MAX = 0xff, + ADLT_HASH_TYPE_NONE = 0, + ADLT_HASH_TYPE_GNU_HASH = 1, + ADLT_HASH_TYPE_SYSV_HASH = 2, + ADLT_HASH_TYPE_DEBUG_CONST = 0xfe, + ADLT_HASH_TYPE_MAX = 0xff, } adlt_hash_type_enum_t; typedef uint8_t adlt_hash_type_t; -- Gitee From c5e72785f5af796de5859eec97bf35c2474bdd47 Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Wed, 17 Apr 2024 07:37:58 -0400 Subject: [PATCH 49/77] [ADLT] Generate output rel info. Change-Id: I9dbdb2dfef29e28a87685ed33884dd0260a1ecb7 Signed-off-by: Anton Volkov --- lld/ELF/Config.h | 6 ++++ lld/ELF/InputFiles.cpp | 8 +++-- lld/ELF/InputFiles.h | 9 +++-- lld/ELF/Relocations.cpp | 49 ++++++++++++++++++++++---- lld/ELF/SyntheticSections.cpp | 10 +++--- lld/ELF/SyntheticSections.h | 2 +- lld/ELF/Writer.cpp | 65 ++++++++++++++++++++++++++++++++--- 7 files changed, 126 insertions(+), 23 deletions(-) diff --git a/lld/ELF/Config.h b/lld/ELF/Config.h index 94e863502a89..9d93f0cfa638 100644 --- a/lld/ELF/Config.h +++ b/lld/ELF/Config.h @@ -404,6 +404,12 @@ struct Ctx { llvm::DenseMap> backwardReferences; + + // ADLT stuff. + // From input .rela.dyn, .rela.plt: + // Keep input library indexes that are needed for got/plt symbol + llvm::DenseMap> + gotPltInfoAdlt; // sym, soFile->ctxIdx array; }; // The only instance of Ctx struct. diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp index bbdd4f6b4a26..f29e0e622f2c 100644 --- a/lld/ELF/InputFiles.cpp +++ b/lld/ELF/InputFiles.cpp @@ -1647,13 +1647,12 @@ template void SharedFileExtended::parseForAdlt() { this->allSymbols.push_back(cast(d)); } - bool isDebug = false; + bool isDebug = false; // debug hint if (!isDebug) return; const ELFFile obj = this->getObj(); ArrayRef objSections = this->template getELFShdrs(); - StringRef shstrtab = getShStrTab(objSections); lld::outs() << "Parse symbols from .symtab:\n"; auto p = obj.getSection(symTabSecIdx); @@ -1680,7 +1679,7 @@ 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)); + auto name = check(obj.getSectionName(sec)); if (name == ".init_array" || name == ".fini_array" || name == ".data" || name == ".data.rel.ro" || name == ".bss.rel.ro" || name == ".bss") traceElfSection(sec); @@ -1945,6 +1944,9 @@ template void SharedFileExtended::parseDynamics() { verneedSec = &sec; break; } + auto secName = check(this->getObj().getSectionName(sec)); + if (secName == ".got.plt") + gotPltSecIdx = secIdx; secIdx++; } diff --git a/lld/ELF/InputFiles.h b/lld/ELF/InputFiles.h index 6279f81ad150..dce2d1bda741 100644 --- a/lld/ELF/InputFiles.h +++ b/lld/ELF/InputFiles.h @@ -450,15 +450,20 @@ public: int symTabSecIdx = 0; int symTabShndxSecIdx = 0; int eFirstGlobal = 0; + int gotPltSecIdx = 0; // .symtab's start of local symbols owned by library - llvm::Optional sharedLocalSymbolIndex; + llvm::Optional sharedLocalSymbolIndex; // .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; - // TODO: dynamic relocation indexes + // From input .rela.dyn, .rela.plt: + llvm::SetVector dynRelIndexes; + llvm::SetVector pltRelIndexes; // SharedFile compability layer: // This is actually a vector of Elf_Verdef pointers. diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp index 55379fe9feb0..7bb80d0c43e1 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -1314,6 +1314,16 @@ static unsigned handleTlsRelocation(RelType type, Symbol &sym, return 0; } +template +static void trackDynRelocAdlt(SharedFileExtended *soFile) { + soFile->dynRelIndexes.insert(mainPart->relaDyn->relocs.size() - 1); +} + +template +static void trackGotPltAdlt(Symbol *sym, SharedFileExtended *soFile) { + ctx->gotPltInfoAdlt[sym].push_back(soFile->ctxIdx); +} + // ADLT BEGIN template void RelocationScanner::tracePushRelocADLT(InputSectionBase &isec, @@ -1370,21 +1380,24 @@ void RelocationScanner::processForADLT(const RelTy &rel, Relocation *r, title + " " + toString(r->type) + ": r->sym not found! addend: "; Defined *d = file->findDefinedSymbol(r->addend, failTitle); assert(d); + r->sym = d; r->addend -= d->section->address + d->value; - addRelativeReloc(*secWhere, r->offset, *d, r->addend, r->expr, + addRelativeReloc(*secWhere, r->offset, *r->sym, r->addend, r->expr, r->type); + trackDynRelocAdlt(file); return; } case R_AARCH64_GLOB_DAT: assert(r->sym->exportDynamic); - r->sym->needsGot = 1; - if (r->sym->isUndefined()) - r->sym->needsPlt = 1; + if (!r->sym->needsGot) + r->sym->needsGot = 1; + trackGotPltAdlt(r->sym, file); return; case R_AARCH64_JUMP_SLOT: assert(r->sym->exportDynamic); - if (r->sym->isUndefined()) + if (r->sym->isUndefined() && !r->sym->needsPlt) r->sym->needsPlt = 1; + trackGotPltAdlt(r->sym, file); return; // abs relocs case R_AARCH64_ABS32: @@ -1396,6 +1409,7 @@ void RelocationScanner::processForADLT(const RelTy &rel, Relocation *r, sec.getPartition().relaDyn->addSymbolReloc(target.symbolicRel, *secWhere, r->offset, *r->sym, r->addend, r->type); + trackDynRelocAdlt(file); return; } sec.relocations.push_back(*r); @@ -1438,6 +1452,10 @@ void RelocationScanner::processForADLT(const RelTy &rel, Relocation *r, return; // tls relocs case R_AARCH64_TLSDESC: + if (fromDynamic && !r->sym->needsTlsDesc) + r->sym->needsTlsDesc = 1; + trackDynRelocAdlt(file); + return; case R_AARCH64_TLSDESC_CALL: case R_AARCH64_TLS_TPREL64: case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21: @@ -1807,6 +1825,16 @@ static bool handleNonPreemptibleIfunc(Symbol &sym) { return true; } +template static void addGotPltIndexAdlt(Symbol *s, bool isPlt) { + auto &vec = ctx->gotPltInfoAdlt[s]; + for (auto &it : vec) { + auto *soFile = cast>(ctx->sharedFilesExtended[it]); + auto &entries = isPlt ? in.relaPlt->relocs : mainPart->relaDyn->relocs; + auto &output = isPlt ? soFile->pltRelIndexes : soFile->dynRelIndexes; + output.insert(entries.size() - 1); + } +} + void elf::postScanRelocations() { auto fn = [](Symbol &sym) { if (handleNonPreemptibleIfunc(sym)) @@ -1816,10 +1844,16 @@ void elf::postScanRelocations() { if (!sym.allocateAux()) return; - if (sym.needsGot) + if (sym.needsGot) { addGotEntry(sym); - if (sym.needsPlt) + if (config->adlt) + invokeELFT(addGotPltIndexAdlt, &sym, false); + } + if (sym.needsPlt) { addPltEntry(*in.plt, *in.gotPlt, *in.relaPlt, target->pltRel, sym); + if (config->adlt) + invokeELFT(addGotPltIndexAdlt, &sym, true); + } if (sym.needsCopy) { if (sym.isObject()) { invokeELFT(addCopyRelSymbol, cast(sym)); @@ -1853,6 +1887,7 @@ void elf::postScanRelocations() { mainPart->relaDyn->addAddendOnlyRelocIfNonPreemptible( target->tlsDescRel, *in.got, in.got->getTlsDescOffset(sym), sym, target->tlsDescRel); + invokeELFT(addGotPltIndexAdlt, &sym, false); } if (sym.needsTlsGd) { in.got->addDynTlsEntry(sym); diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp index 73a11c1aedd7..ebf5d9fe056a 100644 --- a/lld/ELF/SyntheticSections.cpp +++ b/lld/ELF/SyntheticSections.cpp @@ -4085,7 +4085,7 @@ static_assert( "blob array reference occupies 16 bytes in PSOD" ); -static_assert(sizeof(adltBlobStartMark) == 4, +static_assert(sizeof(adltBlobStartMark) == 4, "0xad17 consist of 4 bytes" ); @@ -4110,12 +4110,12 @@ template void AdltSection::finalizeContents() { soInputs.clear(); soInputs.reserve(ctx->sharedFilesExtended.size()); - for (InputFile* file : ctx->sharedFilesExtended) { + for (InputFile* file : ctx->sharedFilesExtended) { auto* soext = cast>(file); soInputs.push_back(makeSoData(soext)); } - assert((soInputs.size() < 1<<16) && + assert((soInputs.size() < 1<<16) && "the number of input libs exeeds ELF limit on number of sections"); const Elf64_Half soNum = soInputs.size(); @@ -4332,7 +4332,7 @@ adlt_blob_array_t AdltSection::writeDtNeeded( needIndexes.push_back(adlt_dt_needed_index_t{ need_data.psodIndex.has_value(), // .hasInternalPSOD need_data.psodIndex.value_or(0), // .PSODindex - need_data.str.strtabOff, // .sonameOffset + need_data.str.strtabOff, // .sonameOffset }); } @@ -4380,7 +4380,7 @@ void AdltSection::writeTo(uint8_t* buf) { size_t blobOff = 0; memcpy(blobBuf + blobOff, &adltBlobStartMark, sizeof(adltBlobStartMark)); blobOff += sizeof(adltBlobStartMark); - + // psod-related data for(const auto& it : llvm::enumerate(soInputs)) { const auto& soData = it.value(); diff --git a/lld/ELF/SyntheticSections.h b/lld/ELF/SyntheticSections.h index 09189e39f1d6..3078f26ac5c9 100644 --- a/lld/ELF/SyntheticSections.h +++ b/lld/ELF/SyntheticSections.h @@ -1241,7 +1241,7 @@ public: SectionString str; llvm::Optional psodIndex; }; - + using SectionStringVector = SmallVector; using DtNeededsVec = SmallVector; diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index 11e3abf34a71..0ac9bb292050 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -1871,6 +1871,45 @@ static void removeUnusedSyntheticSections() { }); } +template static void adltTraceRelocationIndexes() { + auto &relaDyn = mainPart->relaDyn; + assert(!relaDyn->relocs.empty() && "ADLT: relaDyn can't be empty!"); + assert(!in.relaPlt->relocs.empty() && "ADLT: relaPlt can't be empty!"); + + bool adltRelocsTrace = false; // debug hint + if (adltRelocsTrace) { + auto traceRelocInfo = [&](auto *relSec, auto &outIndexes) -> void { + bool isPlt = relSec == in.relaPlt.get(); + lld::outs() << (isPlt ? "Plt" : "Dyn") << " indexes: "; + for (auto &it : outIndexes) // simple print + lld::outs() << it << ' '; + lld::outs() << '\n'; + for (auto &it : outIndexes) // print relocs + if (DynamicReloc *rel = &relSec->relocs[it]) + lld::outs() << it << ": " << rel->inputSec->name << " + 0x" + << utohexstr(rel->offsetInSec) << "\t" + << toString(rel->type) << "\t" << rel->sym->getName() + << " + 0x" << utohexstr(rel->addend) << '\n'; + }; + lld::outs() << "[ADLT]\n"; + lld::outs() << "Dyn relocs (" << relaDyn->relocs.size() << ")\n"; + lld::outs() << "Plt relocs (" << in.relaPlt->relocs.size() << ")\n"; + for (auto *file : ctx->sharedFilesExtended) + if (auto *soFile = cast>(file)) { + lld::outs() << "[ADLT] " << soFile->soName << ":\n"; + traceRelocInfo(relaDyn.get(), soFile->dynRelIndexes); + traceRelocInfo(in.relaPlt.get(), soFile->pltRelIndexes); + } + } + for (auto *file : ctx->sharedFilesExtended) { + __attribute__((unused)) auto *soFile = cast>(file); + assert(!soFile->dynRelIndexes.empty() && + "ADLT: relaDyn indexes can't be empty!"); + assert(!soFile->pltRelIndexes.empty() && + "ADLT: relaPlt indexes can't be empty!"); + } +} + // Create output section objects and add them to OutputSections. template void Writer::finalizeSections() { Out::preinitArray = findSection(".preinit_array"); @@ -1962,11 +2001,25 @@ template void Writer::finalizeSections() { // copy relocations, etc. Note that relocations for non-alloc sections are // directly processed by InputSection::relocateNonAlloc. if (config->adlt) - for (InputFile *file : ctx->sharedFilesExtended) - for (InputSectionBase *sec : file->getSections()) { - if (sec && sec->isLive()) - scanRelocations(*sec); - } + 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]); + // scan .rela.plt (base: .got.plt) + scanRelocations(*sections[soFile->gotPltSecIdx]); + // scan other sections + // excluding .rela.plt and .rela.dyn + auto gotPltIdx = static_cast(soFile->gotPltSecIdx); + auto isNotExcludedIdx = [&](auto idx) { + return idx != 0 && idx != gotPltIdx; + }; + for (auto &it : llvm::enumerate(soFile->getSections())) + if (InputSectionBase *sec = it.value()) + if (sec && sec->isLive() && isNotExcludedIdx(it.index())) + scanRelocations(*sec); + } else for (InputSectionBase *sec : inputSections) if (sec->isLive() && isa(sec) && (sec->flags & SHF_ALLOC)) @@ -2164,6 +2217,8 @@ template void Writer::finalizeSections() { finalizeSynthetic(part.verNeed.get()); finalizeSynthetic(part.dynamic.get()); } + if (config->adlt) // check ouput relocation indexes + adltTraceRelocationIndexes(); } if (!script->hasSectionsCommand && !config->relocatable) -- Gitee From 5431e1ad0d066e5af5c5882300ba72e52f80d381 Mon Sep 17 00:00:00 2001 From: Khomutov Nikita Date: Fri, 19 Apr 2024 16:13:38 +0300 Subject: [PATCH 50/77] [ADLT] update reloc index types in .adlt section Signed-off-by: Khomutov Nikita Change-Id: Ie25e40ee257af416fef3cb942df6b02ab57b65e0 --- lld/ELF/SyntheticSections.cpp | 63 ++++++++++++++++------------------- lld/ELF/SyntheticSections.h | 10 +++--- 2 files changed, 35 insertions(+), 38 deletions(-) diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp index ebf5d9fe056a..a8edcb568875 100644 --- a/lld/ELF/SyntheticSections.cpp +++ b/lld/ELF/SyntheticSections.cpp @@ -4060,11 +4060,6 @@ static_assert( "adlt_dt_needed_index_t have to be an offset with intrused flags" ); -static_assert( - sizeof(adlt_relocations_segment_t) == 16, - "adlt_relocations_segment_t size is 16 bytes" -); - static_assert( sizeof(adlt_cross_section_ref_t) == 16, "adlt_cross_section_ref_t size is 16 bytes" @@ -4089,7 +4084,7 @@ static_assert(sizeof(adltBlobStartMark) == 4, "0xad17 consist of 4 bytes" ); -static_assert(sizeof(adlt_section_header_t) == 72, +static_assert(sizeof(adlt_section_header_t) == 40, "please update version if header has been changed" ); @@ -4131,8 +4126,6 @@ void AdltSection::finalizeContents() { getBlobStartOffset(), // .blobStart estimateBlobSize(), // .blobSize 0, // .overallMappedSize, known on writeTo - {0, 0}, // .relaDynSegs, filled in writeTo - {0, 0}, // .relaPltSegs, filled in writeTo }; buildSonameIndex(); @@ -4203,12 +4196,7 @@ size_t AdltSection::estimateOverallMappedSize() { template typename AdltSection::CommonData AdltSection::makeCommonData() { - // TODO: fill relaDynSegs - // TODO: fill relaPltSegs - return CommonData { - {}, // .relaDynSegs - {}, // .relaPltSegs UINT32_MAX, // .symtabSecIndex, filled in writeTo }; } @@ -4231,8 +4219,8 @@ AdltSection::makeSoData(const SharedFileExtended* soext) { data.initArrayName = soext->addAdltPostfix(".init_array"); data.finiArrayName = soext->addAdltPostfix(".fini_array"); - // TODO: fill data.relaDynSegs - // TODO: fill data.relaPltSegs + // TODO: fill data.relaDynIndx + // TODO: fill data.relaPltIndx std::copy(soext->programHeaderIndexes.begin(), soext->programHeaderIndexes.end(), @@ -4282,8 +4270,8 @@ adlt_psod_t AdltSection::serialize(const SoData& soData) const { soData.sharedGlobalIndex.value_or(0), }, // .sharedGlobalSymbolIndex adlt_blob_u16_array_t {}, // .phIndexes, filled in writeTo - adlt_blob_array_t {}, // .relaDynSegs, filled in writeTo - adlt_blob_array_t {}, // .relaPltSegs, filled in writeTo + adlt_blob_u32_array_t {}, // .relaDynIndx, filled in writeTo + adlt_blob_u32_array_t {}, // .relaPltIndx, filled in writeTo }; } @@ -4299,13 +4287,10 @@ size_t AdltSection::estimateBlobSize() const { for (const auto& soData: soInputs) { blobSize += sizeof(adlt_dt_needed_index_t) * soData.dtNeededs.size(); blobSize += sizeof(uint16_t) * soData.phIndexes.size(); - blobSize += sizeof(adlt_relocations_segment_t) * soData.relaDynSegs.size(); - blobSize += sizeof(adlt_relocations_segment_t) * soData.relaPltSegs.size(); + blobSize += sizeof(uint32_t) * soData.relaDynIndx.size(); + blobSize += sizeof(uint32_t) * soData.relaPltIndx.size(); }; - blobSize += sizeof(adlt_relocations_segment_t) * common.relaDynSegs.size(); - blobSize += sizeof(adlt_relocations_segment_t) * common.relaPltSegs.size(); - return blobSize; } @@ -4321,6 +4306,19 @@ adlt_blob_array_t AdltSection::writeArray( return {offset, to_write}; } +template +template +adlt_blob_array_t AdltSection::writeArray( + uint8_t* buff, size_t offset, const ArrayRef& data) { + if (data.empty()) return {0x0, 0}; + + const size_t to_write = data.size() * sizeof(T); + memcpy(buff + offset, data.data(), to_write); + + return {offset, to_write}; +} + + template adlt_blob_array_t AdltSection::writeDtNeeded( uint8_t* buff, size_t offset, const DtNeededsVec& neededVec) { @@ -4391,21 +4389,18 @@ void AdltSection::writeTo(uint8_t* buf) { psod.phIndexes = writeArray(blobBuf, blobOff, soData.phIndexes); blobOff += psod.phIndexes.size; - - psod.relaDynSegs = writeArray(blobBuf, blobOff, soData.relaDynSegs); - blobOff += psod.relaDynSegs.size; - - psod.relaPltSegs = writeArray(blobBuf, blobOff, soData.relaPltSegs); - blobOff += psod.relaPltSegs.size; } - // common data - { - header.relaDynSegs = writeArray(blobBuf, blobOff, common.relaDynSegs); - blobOff += header.relaDynSegs.size; + // group up relaDyn and relaPlt idxs, it tends to be size-consuming + for(const auto& it : llvm::enumerate(soInputs)) { + const auto& soData = it.value(); + auto& psod = psods[it.index()]; + + psod.relaDynIndx = writeArray(blobBuf, blobOff, soData.relaDynIndx); + blobOff += psod.relaDynIndx.size; - header.relaPltSegs = writeArray(blobBuf, blobOff, common.relaPltSegs); - blobOff += header.relaPltSegs.size; + psod.relaPltIndx = writeArray(blobBuf, blobOff, soData.relaPltIndx); + blobOff += psod.relaPltIndx.size; } // finalize header.blobSize diff --git a/lld/ELF/SyntheticSections.h b/lld/ELF/SyntheticSections.h index 3078f26ac5c9..c16389cb31ec 100644 --- a/lld/ELF/SyntheticSections.h +++ b/lld/ELF/SyntheticSections.h @@ -1260,14 +1260,12 @@ public: llvm::Optional sharedGlobalIndex; SmallVector phIndexes; - SmallVector relaDynSegs; // TODO - SmallVector relaPltSegs; // TODO + ArrayRef relaDynIndx; // TODO + ArrayRef relaPltIndx; // TODO }; // will be used to form some header data struct CommonData { - SmallVector relaDynSegs; // TODO - SmallVector relaPltSegs; // TODO uint32_t symtabSecIndex = UINT32_MAX; }; @@ -1301,6 +1299,10 @@ private: adlt_blob_array_t writeArray(uint8_t* buff, size_t offset, const SmallVector& data); + template + adlt_blob_array_t writeArray(uint8_t* buff, size_t offset, + const ArrayRef& data); + adlt_blob_array_t writeDtNeeded(uint8_t* buff, size_t offset, const DtNeededsVec& neededVec); -- Gitee From c535b7328ad047da872aa81de7fcb28dc1bf7071 Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Mon, 22 Apr 2024 06:14:33 -0400 Subject: [PATCH 51/77] [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 f29e0e622f2c..fa635c700883 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 7bb80d0c43e1..1e25f8745d5a 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -1348,8 +1348,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 @@ -1359,11 +1357,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; @@ -1376,10 +1374,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 From a4664e71dd98b33a53c43dac5ad03dc9b054cc46 Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Tue, 23 Apr 2024 10:22:10 -0400 Subject: [PATCH 52/77] [ADLT] Fix some GOT rels proc. Minor changes. Change-Id: I6fef7cb7bc49111d9fbcf191bf7fdb20c19ffa1b Signed-off-by: Anton Volkov --- lld/ELF/InputFiles.cpp | 31 ++++++++++++++++++++----------- lld/ELF/Relocations.cpp | 12 +++++------- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp index fa635c700883..190fe7832fd3 100644 --- a/lld/ELF/InputFiles.cpp +++ b/lld/ELF/InputFiles.cpp @@ -1652,15 +1652,7 @@ SharedFileExtended::SharedFileExtended(MemoryBufferRef mb, template void SharedFileExtended::parseForAdlt() { this->parse(); parseDynamics(); - parseElfSymTab(); - // add additional section symbols - for (InputSectionBase *sec : this->sections) - for (StringRef prefix : {".got", ".got.plt"}) - if (sec && sec->name.startswith(prefix)) { - auto *d = make(this, "", ELF::STB_LOCAL, 0, ELF::STT_SECTION, 0, 0, sec); - this->allSymbols.push_back(cast(d)); - } bool isDebug = false; // debug hint if (!isDebug) @@ -1779,10 +1771,27 @@ SharedFileExtended::findInputSection(StringRef name) const { template InputSectionBase * SharedFileExtended::findInputSection(uint64_t offset) const { - for (InputSectionBase *sec : this->sections) - if (sec && sec->address == offset) + llvm::SmallVector candidates; + for (InputSectionBase *sec : this->sections) { + if (!sec) + continue; + if (sec->address == offset) return sec; - return nullptr; + uint64_t low = sec->address; + uint64_t high = low + sec->size; + bool isGood = (offset >= low) && (offset < high); + if (!isGood) + continue; + candidates.push_back(sec); + } + if (candidates.empty()) // no suitable items found + return nullptr; + + auto lessAddr = [&](auto *s1, auto *s2) { return s1->address < s2->address; }; + auto i = candidates.begin(); + auto e = candidates.end(); + auto ret = std::min_element(i, e, lessAddr); + return *ret; } template diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp index 1e25f8745d5a..9a2b1d5c94bd 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -1347,7 +1347,6 @@ template void RelocationScanner::processForADLT(const RelTy &rel, Relocation *r, bool fromDynamic) { auto file = sec.getSharedFile(); - uint32_t symIndex = rel.getSymbol(config->isMips64EL); bool isDebug = false; /*if (r->offset == 0x21F) // debug hint @@ -1357,10 +1356,7 @@ void RelocationScanner::processForADLT(const RelTy &rel, Relocation *r, isDebug = true;*/ // parse offset (where) - InputSectionBase *secWhere = cast( - fromDynamic - ? file->findDefinedSymbol(r->offset)->section - : (r->sym->isDefined() ? cast(r->sym)->section : &sec)); + InputSectionBase *secWhere = file->findInputSection(r->offset); assert(secWhere && "not found!"); // process offset @@ -1410,8 +1406,6 @@ void RelocationScanner::processForADLT(const RelTy &rel, Relocation *r, } sec.relocations.push_back(*r); return; - case R_AARCH64_ADD_ABS_LO12_NC: - case R_AARCH64_ADR_PREL_PG_HI21: case R_AARCH64_LDST8_ABS_LO12_NC: case R_AARCH64_LDST16_ABS_LO12_NC: case R_AARCH64_LDST32_ABS_LO12_NC: @@ -1431,6 +1425,10 @@ void RelocationScanner::processForADLT(const RelTy &rel, Relocation *r, sec.relocations.push_back(*r); return; // got relocs + case R_AARCH64_ADD_ABS_LO12_NC: + case R_AARCH64_ADR_PREL_PG_HI21: + sec.relocations.push_back(*r); // TODO: optimize GOT + return; case R_AARCH64_ADR_GOT_PAGE: if (r->sym->isDefined() && !r->sym->needsGot) { if (isDebug) -- Gitee From ef89c36a81a1332d49a15c59de3e16802659f466 Mon Sep 17 00:00:00 2001 From: Khomutov Nikita Date: Mon, 22 Apr 2024 14:53:27 +0300 Subject: [PATCH 53/77] [ADLT] write reloc indexes to .adlt section Signed-off-by: Khomutov Nikita Change-Id: I1f539dc39a1a82ff4ff5db1dc0d97d0f001e1925 Signed-off-by: Khomutov Nikita --- lld/ELF/InputFiles.h | 4 ++-- lld/ELF/SyntheticSections.cpp | 4 ++-- lld/ELF/SyntheticSections.h | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lld/ELF/InputFiles.h b/lld/ELF/InputFiles.h index bd880dc712a8..f496af714985 100644 --- a/lld/ELF/InputFiles.h +++ b/lld/ELF/InputFiles.h @@ -461,8 +461,8 @@ public: // Output information data: llvm::SetVector programHeaderIndexes; // From input .rela.dyn, .rela.plt: - llvm::SetVector dynRelIndexes; - llvm::SetVector pltRelIndexes; + llvm::SetVector dynRelIndexes; + llvm::SetVector pltRelIndexes; // SharedFile compability layer: // This is actually a vector of Elf_Verdef pointers. diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp index a8edcb568875..8d1f83e80c43 100644 --- a/lld/ELF/SyntheticSections.cpp +++ b/lld/ELF/SyntheticSections.cpp @@ -4219,8 +4219,8 @@ AdltSection::makeSoData(const SharedFileExtended* soext) { data.initArrayName = soext->addAdltPostfix(".init_array"); data.finiArrayName = soext->addAdltPostfix(".fini_array"); - // TODO: fill data.relaDynIndx - // TODO: fill data.relaPltIndx + data.relaDynIndx = soext->dynRelIndexes.getArrayRef(); + data.relaPltIndx = soext->pltRelIndexes.getArrayRef(); std::copy(soext->programHeaderIndexes.begin(), soext->programHeaderIndexes.end(), diff --git a/lld/ELF/SyntheticSections.h b/lld/ELF/SyntheticSections.h index c16389cb31ec..1a72f89c8edb 100644 --- a/lld/ELF/SyntheticSections.h +++ b/lld/ELF/SyntheticSections.h @@ -1260,8 +1260,8 @@ public: llvm::Optional sharedGlobalIndex; SmallVector phIndexes; - ArrayRef relaDynIndx; // TODO - ArrayRef relaPltIndx; // TODO + ArrayRef relaDynIndx; + ArrayRef relaPltIndx; }; // will be used to form some header data -- Gitee From 24d4d499fca7eeb185aa1022b713a7ec2778e9ce Mon Sep 17 00:00:00 2001 From: Khomutov Nikita Date: Mon, 22 Apr 2024 14:27:23 +0300 Subject: [PATCH 54/77] [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 - 5 files changed, 29 insertions(+), 35 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 190fe7832fd3..b4900abfb543 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; } @@ -1700,15 +1683,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 f496af714985..b7099e5d6ae4 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 9a2b1d5c94bd..322a88eb59d0 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -1321,7 +1321,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]); -- Gitee From e657b9396b9051091e5ef7813ec2278b886cb0d4 Mon Sep 17 00:00:00 2001 From: likholatovevgeny Date: Wed, 24 Apr 2024 14:33:38 +0300 Subject: [PATCH 55/77] Changed generation of TLS headers. Signed-off-by: likholatovevgeny --- lld/ELF/Writer.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index 36d222caf77c..6fa71695d4a0 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -2542,9 +2542,16 @@ SmallVector Writer::createPhdrs(Partition &part) { // Add a TLS segment if any. PhdrEntry *tlsHdr = make(PT_TLS, PF_R); for (OutputSection *sec : outputSections) - if (sec->partition == partNo && sec->flags & SHF_TLS) + if (sec->partition == partNo && sec->flags & SHF_TLS) { + // It making TLS hdr for each TLS section, for adlt. + if (config->adlt && tlsHdr->firstSec) + tlsHdr = make(PT_TLS, PF_R); tlsHdr->add(sec); - if (tlsHdr->firstSec) + + if (config->adlt) + ret.push_back(tlsHdr); + } + if (!config->adlt && tlsHdr->firstSec) ret.push_back(tlsHdr); // Add an entry for .dynamic. -- Gitee From d7d21b4bb9c73d83c9f5226ba08b2f89bf4c04b6 Mon Sep 17 00:00:00 2001 From: likholatovevgeny Date: Thu, 25 Apr 2024 15:15:43 +0300 Subject: [PATCH 56/77] Used addHdr func for making hdr in adlt. Signed-off-by: likholatovevgeny --- lld/ELF/Writer.cpp | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index 6fa71695d4a0..ee310d888053 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -2544,12 +2544,9 @@ SmallVector Writer::createPhdrs(Partition &part) { for (OutputSection *sec : outputSections) if (sec->partition == partNo && sec->flags & SHF_TLS) { // It making TLS hdr for each TLS section, for adlt. - if (config->adlt && tlsHdr->firstSec) - tlsHdr = make(PT_TLS, PF_R); - tlsHdr->add(sec); - if (config->adlt) - ret.push_back(tlsHdr); + tlsHdr = addHdr(PT_TLS, PF_R); + tlsHdr->add(sec); } if (!config->adlt && tlsHdr->firstSec) ret.push_back(tlsHdr); -- Gitee From 30c5ae9cb06b43d466431f29c96d311f5d4242ae Mon Sep 17 00:00:00 2001 From: Khomutov Nikita Date: Thu, 2 May 2024 15:35:24 +0300 Subject: [PATCH 57/77] [ADLT] fix program header indexing lifecycle for .adlt section Signed-off-by: Khomutov Nikita Change-Id: Ie4e6c7b0561f4a45311b7a8df4e1edac5cbdde37 Signed-off-by: Khomutov Nikita --- lld/ELF/InputFiles.h | 4 +- lld/ELF/SyntheticSections.cpp | 34 +++++++++++-- lld/ELF/SyntheticSections.h | 5 ++ lld/ELF/Writer.cpp | 95 +++++++++++------------------------ 4 files changed, 68 insertions(+), 70 deletions(-) diff --git a/lld/ELF/InputFiles.h b/lld/ELF/InputFiles.h index b7099e5d6ae4..1db53c987f94 100644 --- a/lld/ELF/InputFiles.h +++ b/lld/ELF/InputFiles.h @@ -38,6 +38,7 @@ namespace elf { class InputSection; class Symbol; +struct PhdrEntry; // OHOS_LOCAL // If --reproduce is specified, all input files are written to this tar archive. extern std::unique_ptr tar; @@ -458,7 +459,8 @@ public: llvm::Optional sharedGlobalSymbolIndex; // Output information data: - llvm::SetVector programHeaderIndexes; + llvm::SetVector programHeaders; + // From input .rela.dyn, .rela.plt: llvm::SetVector dynRelIndexes; llvm::SetVector pltRelIndexes; diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp index 8d1f83e80c43..83a593200170 100644 --- a/lld/ELF/SyntheticSections.cpp +++ b/lld/ELF/SyntheticSections.cpp @@ -4222,9 +4222,8 @@ AdltSection::makeSoData(const SharedFileExtended* soext) { data.relaDynIndx = soext->dynRelIndexes.getArrayRef(); data.relaPltIndx = soext->pltRelIndexes.getArrayRef(); - std::copy(soext->programHeaderIndexes.begin(), - soext->programHeaderIndexes.end(), - std::back_inserter(data.phIndexes)); + data.programHeadersAllocated = soext->programHeaders.size(); + data.programHeaders = soext->programHeaders.getArrayRef(); return data; } @@ -4286,7 +4285,7 @@ size_t AdltSection::estimateBlobSize() const { for (const auto& soData: soInputs) { blobSize += sizeof(adlt_dt_needed_index_t) * soData.dtNeededs.size(); - blobSize += sizeof(uint16_t) * soData.phIndexes.size(); + blobSize += sizeof(uint16_t) * soData.programHeadersAllocated; blobSize += sizeof(uint32_t) * soData.relaDynIndx.size(); blobSize += sizeof(uint32_t) * soData.relaPltIndx.size(); }; @@ -4337,8 +4336,18 @@ adlt_blob_array_t AdltSection::writeDtNeeded( return writeArray(buff, offset, needIndexes); } +template +void AdltSection::extractProgramHeaderIndexes() { + phdrsIndexes.clear(); + for (const auto& it : llvm::enumerate(mainPart->phdrs)) { + phdrsIndexes[it.value()] = static_cast(it.index()); + }; +} + template void AdltSection::finalizeOnWrite() { + // require optimizing Writer::removeEmptyPTLoad been called + extractProgramHeaderIndexes(); // require Writer::setPhdrs previously been called header.overallMappedSize = estimateOverallMappedSize(); @@ -4358,6 +4367,23 @@ void AdltSection::finalizeOnWrite(size_t idx, SoData& soData) { // require SymbolTableBaseSection::sortSymTabSymbolsInAdlt for .symtab called soData.sharedLocalIndex = soext->sharedLocalSymbolIndex; soData.sharedGlobalIndex = soext->sharedGlobalSymbolIndex; + + // phIndexes may shift if called before Writer::removeEmptyPTLoad + soData.phIndexes.clear(); + for (const PhdrEntry* phentry : soData.programHeaders) { + auto it = phdrsIndexes.find(phentry); + if (it != phdrsIndexes.end()) { + soData.phIndexes.push_back(it->second); + } + } + + assert(soData.phIndexes.size() <= soData.programHeadersAllocated); + if (soData.phIndexes.size() != soData.programHeadersAllocated) { + warn(Twine(".adlt section: overallocated ph-indexes for psod-") + Twine(idx) + + " allocated=" + Twine(soData.programHeadersAllocated) + + " actual=" + Twine(soData.phIndexes.size()) + ); + } } template diff --git a/lld/ELF/SyntheticSections.h b/lld/ELF/SyntheticSections.h index 1a72f89c8edb..873f244d68bb 100644 --- a/lld/ELF/SyntheticSections.h +++ b/lld/ELF/SyntheticSections.h @@ -1259,7 +1259,10 @@ public: llvm::Optional sharedLocalIndex; llvm::Optional sharedGlobalIndex; + size_t programHeadersAllocated; + ArrayRef programHeaders; SmallVector phIndexes; + ArrayRef relaDynIndx; ArrayRef relaPltIndx; }; @@ -1285,6 +1288,7 @@ private: void linkInternalDtNeeded(); void extractInitFiniArray(); size_t estimateOverallMappedSize(); + void extractProgramHeaderIndexes(); Elf64_Xword calculateHash(StringRef str) const; CommonData makeCommonData(); @@ -1315,6 +1319,7 @@ private: SmallVector soInputs; llvm::DenseMap sonameToIndexMap; + llvm::DenseMap phdrsIndexes; }; } // namespace adlt diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index ee310d888053..7439ed63c644 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -117,10 +117,6 @@ static void removeEmptyPTLoad(SmallVector &phdrs) { if (removed.count(sec->ptLoad)) sec->ptLoad = nullptr; - if (config->adlt) { - assert(it == phdrs.end() && "adlt: ph-index in .adlt invalid due to shift"); - } - phdrs.erase(it, phdrs.end()); } @@ -1029,6 +1025,27 @@ static bool compareSections(const SectionCommand *aCmd, return false; } +// OHOS_LOCAL begin +namespace { + +InputSection* getInputSection(OutputSection* sec) { + if (!sec || !sec->hasInputSections || sec->commands.empty()) + return nullptr; + SectionCommand* cmd = sec->commands.front(); + InputSectionDescription* isd = cast(cmd); + return isd->sections.front(); +} + +template +void addPhdrToSharedFilesExtendedContext(InputFile* file, PhdrEntry* phdr) { + if (auto* soFile = dyn_cast>(file)) { + soFile->programHeaders.insert(phdr); + } +} + +} // namespace +// OHOS_LOCAL end + void PhdrEntry::add(OutputSection *sec) { lastSec = sec; if (!firstSec) @@ -1039,6 +1056,15 @@ void PhdrEntry::add(OutputSection *sec) { p_align = std::max(p_align, sec->alignment); if (p_type == PT_LOAD) sec->ptLoad = this; + + if (config->adlt && (p_type == PT_LOAD || p_type == PT_TLS)) { + auto* insec = getInputSection(sec); + // skip generated sections. + // TODO: fix .rodata_$ADLT_POSTFIX sections. + if (insec && insec->file) { + invokeELFT(addPhdrToSharedFilesExtendedContext, insec->file, this); + } + } } // The beginning and the ending of .rel[a].plt section are marked @@ -2374,19 +2400,6 @@ static uint64_t computeFlags(uint64_t flags) { return flags; } -static InputSection *getInputSection(OutputSection *sec) { - if (!sec || !sec->hasInputSections || sec->commands.empty()) - return nullptr; - SectionCommand *cmd = sec->commands.front(); - InputSectionDescription *isd = cast(cmd); - return isd->sections.front(); -} - -struct LoadInfoForADLT { - int phIndex; // input - SmallVector phSections; -}; - // Decide which program headers to create and which sections to include in each // one. template @@ -2397,10 +2410,6 @@ SmallVector Writer::createPhdrs(Partition &part) { return ret.back(); }; - auto getCurrentIndex = [&]() -> int { - return static_cast(ret.size()) - 1; - }; - unsigned partNo = part.getNumber(); bool isMain = partNo == 1; @@ -2433,11 +2442,6 @@ SmallVector Writer::createPhdrs(Partition &part) { load = addHdr(PT_LOAD, flags); load->add(Out::elfHeader); load->add(Out::programHeaders); - if (config->adlt) // add common header for all libs - for (auto *base : ctx->sharedFilesExtended) { - auto *soFile = cast>(base); - soFile->programHeaderIndexes.insert(getCurrentIndex()); - } } } @@ -2464,9 +2468,6 @@ SmallVector Writer::createPhdrs(Partition &part) { } } - // ADLT: Collect load headers info - SmallVector adltLoadInfo; - for (OutputSection *sec : outputSections) { if (!needsPtLoad(sec)) continue; @@ -2498,45 +2499,9 @@ SmallVector Writer::createPhdrs(Partition &part) { (sameLMARegion || load->lastSec == Out::programHeaders))) { load = addHdr(PT_LOAD, newFlags); flags = newFlags; - if (config->adlt) { - adltLoadInfo.emplace_back(); - adltLoadInfo.back().phIndex = getCurrentIndex(); - } } load->add(sec); - if (config->adlt && !adltLoadInfo.empty()) - adltLoadInfo.back().phSections.push_back(sec); - } - - // ADLT: Fill load headers info - if (config->adlt) { - for (LoadInfoForADLT &info : adltLoadInfo) - for (OutputSection *sec : info.phSections) { - auto *isec = getInputSection(sec); - assert(isec); - auto *file = isec->file; - if (!file) // skip generated sections. - continue; // TODO: fix .rodata_$ADLT_POSTFIX sections. - auto *soFile = cast>(file); - soFile->programHeaderIndexes.insert(info.phIndex); - } - - // Check outputs - auto adltTraceNeeded = false; // debug hint - auto trace = [&](auto *file, auto &indexes) { - lld::outs() << "ADLT: " << file->soName << ":\n"; - for (auto &it : indexes) - lld::outs() << it << " "; - lld::outs() << '\n'; - }; - for (ELFFileBase *baseFile : ctx->sharedFilesExtended) { - auto *soFile = cast>(baseFile); - auto &indexes = soFile->programHeaderIndexes; - assert(!indexes.empty()); - if (adltTraceNeeded) - trace(soFile, indexes); - } } // Add a TLS segment if any. -- Gitee From af6ca9e62a239de6d009829a0883cde4157114e0 Mon Sep 17 00:00:00 2001 From: likholatovevgeny Date: Fri, 3 May 2024 16:27:35 +0300 Subject: [PATCH 58/77] Resolved duplicates for adlt. Signed-off-by: likholatovevgeny --- lld/ELF/Config.h | 2 ++ lld/ELF/Driver.cpp | 5 +++++ lld/ELF/InputFiles.cpp | 27 +++++++++++++++++++++++++++ lld/ELF/InputFiles.h | 2 ++ lld/ELF/Writer.cpp | 7 ++++--- 5 files changed, 40 insertions(+), 3 deletions(-) diff --git a/lld/ELF/Config.h b/lld/ELF/Config.h index 6583d7d8e562..d96a9d0d229f 100644 --- a/lld/ELF/Config.h +++ b/lld/ELF/Config.h @@ -399,6 +399,8 @@ struct Ctx { // A tuple of (reference, extractedFile, sym). Used by --why-extract=. SmallVector, 0> whyExtractRecords; + // Map of symbols frequency (only defined) + llvm::DenseMap eSymsFreqMap; // A mapping from a symbol to an InputFile referencing it backward. Used by // --warn-backrefs. llvm::DenseMapundefined) addUnusedUndefined(name)->referenced = true; + // Fill sym frequensy map for defined syms, This will help to find duplicates. + if (config->adlt) + for (auto *file : files) + fillSymsFreqMap(file); + // Add all files to the symbol table. This will add almost all // symbols that we need to the symbol table. This process might // add files to the link, via autolinking, these files are always diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp index b4900abfb543..18e2f6e87beb 100644 --- a/lld/ELF/InputFiles.cpp +++ b/lld/ELF/InputFiles.cpp @@ -158,6 +158,18 @@ static bool isCompatible(InputFile *file) { return false; } +template static void doFillSymsFreqMap(InputFile *file) { + if (!isCompatible(file)) + return; + if (auto *f = dyn_cast>(file)) { + f->fillSymsFreqMap(); + } +} + +void elf::fillSymsFreqMap(InputFile *file) { + invokeELFT(doFillSymsFreqMap, file); +} + template static void doParseFile(InputFile *file) { if (!isCompatible(file)) return; @@ -1034,6 +1046,16 @@ InputSectionBase *ObjFile::createInputSection(uint32_t idx, return make(*this, sec, name); } +template void ObjFile::fillSymsFreqMap() { + ArrayRef eSyms = this->getELFSyms(); + for (size_t i = firstGlobal, end = eSyms.size(); i != end; ++i) { + if (!eSyms[i].isDefined()) + continue; + StringRef name = CHECK(eSyms[i].getName(stringTable), this); + ctx->eSymsFreqMap[name]++; + } +} + // Initialize this->Symbols. this->Symbols is a parallel array as // its corresponding ELF symbol table. template @@ -1052,6 +1074,11 @@ void ObjFile::initializeSymbols(const object::ELFFile &obj) { if (!ctx->adltWithCfi) ctx->adltWithCfi = true; } + if (config->adlt && eSyms[i].isDefined() && + ctx->eSymsFreqMap[name] > 1) { + auto *f = cast>(this); + name = f->addAdltPostfix(name); + } symbols[i] = symtab.insert(name); } diff --git a/lld/ELF/InputFiles.h b/lld/ELF/InputFiles.h index 1db53c987f94..3366ca5c0348 100644 --- a/lld/ELF/InputFiles.h +++ b/lld/ELF/InputFiles.h @@ -47,6 +47,7 @@ extern std::unique_ptr tar; llvm::Optional readFile(StringRef path); // Add symbols in File to the symbol table. +void fillSymsFreqMap(InputFile *file); void parseFile(InputFile *file); // The root class of input files. @@ -292,6 +293,7 @@ public: // Get cached DWARF information. DWARFCache *getDwarf(); + void fillSymsFreqMap(); void initializeLocalSymbols(); void postParse(); diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index 7439ed63c644..47a6bef1a9b0 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -2110,9 +2110,10 @@ template void Writer::finalizeSections() { if (sym->includeInDynsym()) { partitions[sym->partition - 1].dynSymTab->addSymbol(sym); - if (auto *file = dyn_cast_or_null(sym->file)) - if (file->isNeeded && !sym->isUndefined()) - addVerneed(sym); + if(!config->adlt) // TODO: fix for adlt. + if (auto *file = dyn_cast_or_null(sym->file)) + if (file->isNeeded && !sym->isUndefined()) + addVerneed(sym); } } -- Gitee From dee581f59ed6d4950cc1d5058b6b3bf7d85ba8fd Mon Sep 17 00:00:00 2001 From: likholatovevgeny Date: Fri, 3 May 2024 17:25:35 +0300 Subject: [PATCH 59/77] Changed resolve of duplicates. Signed-off-by: likholatovevgeny --- lld/ELF/InputFiles.cpp | 20 +++++++++----------- lld/ELF/Writer.cpp | 7 +++---- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp index 18e2f6e87beb..824530aeac60 100644 --- a/lld/ELF/InputFiles.cpp +++ b/lld/ELF/InputFiles.cpp @@ -161,9 +161,8 @@ static bool isCompatible(InputFile *file) { template static void doFillSymsFreqMap(InputFile *file) { if (!isCompatible(file)) return; - if (auto *f = dyn_cast>(file)) { + if (auto *f = dyn_cast>(file)) f->fillSymsFreqMap(); - } } void elf::fillSymsFreqMap(InputFile *file) { @@ -1069,15 +1068,14 @@ void ObjFile::initializeSymbols(const object::ELFFile &obj) { for (size_t i = firstGlobal, end = eSyms.size(); i != end; ++i) if (!symbols[i]) { StringRef name = CHECK(eSyms[i].getName(stringTable), this); - if (config->adlt && name == "__cfi_check") { - name = this->getUniqueName(name); - if (!ctx->adltWithCfi) - ctx->adltWithCfi = true; - } - if (config->adlt && eSyms[i].isDefined() && - ctx->eSymsFreqMap[name] > 1) { - auto *f = cast>(this); - name = f->addAdltPostfix(name); + if (config->adlt) { + if (name == "__cfi_check") { + name = this->getUniqueName(name); + if (!ctx->adltWithCfi) + ctx->adltWithCfi = true; + } else if (eSyms[i].isDefined() && ctx->eSymsFreqMap[name] > 1) { + name = this->getUniqueName(name); + } } symbols[i] = symtab.insert(name); } diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index 47a6bef1a9b0..7439ed63c644 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -2110,10 +2110,9 @@ template void Writer::finalizeSections() { if (sym->includeInDynsym()) { partitions[sym->partition - 1].dynSymTab->addSymbol(sym); - if(!config->adlt) // TODO: fix for adlt. - if (auto *file = dyn_cast_or_null(sym->file)) - if (file->isNeeded && !sym->isUndefined()) - addVerneed(sym); + if (auto *file = dyn_cast_or_null(sym->file)) + if (file->isNeeded && !sym->isUndefined()) + addVerneed(sym); } } -- Gitee From 3a8cd8c36095ad14ae54775697c6e4146703d765 Mon Sep 17 00:00:00 2001 From: likholatovevgeny Date: Fri, 3 May 2024 17:49:02 +0300 Subject: [PATCH 60/77] United resolving duplicates for cfi and other symbols. Signed-off-by: likholatovevgeny --- lld/ELF/InputFiles.cpp | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp index 824530aeac60..08343c85e8e1 100644 --- a/lld/ELF/InputFiles.cpp +++ b/lld/ELF/InputFiles.cpp @@ -1068,18 +1068,16 @@ void ObjFile::initializeSymbols(const object::ELFFile &obj) { for (size_t i = firstGlobal, end = eSyms.size(); i != end; ++i) if (!symbols[i]) { StringRef name = CHECK(eSyms[i].getName(stringTable), this); - if (config->adlt) { - if (name == "__cfi_check") { - name = this->getUniqueName(name); - if (!ctx->adltWithCfi) - ctx->adltWithCfi = true; - } else if (eSyms[i].isDefined() && ctx->eSymsFreqMap[name] > 1) { - name = this->getUniqueName(name); - } + if (config->adlt && eSyms[i].isDefined() && ctx->eSymsFreqMap[name] > 1) { + if (name == "__cfi_check") + ctx->adltWithCfi = true; + name = this->getUniqueName(name); } symbols[i] = symtab.insert(name); } + + // Perform symbol resolution on non-local symbols. SmallVector undefineds; for (size_t i = firstGlobal, end = eSyms.size(); i != end; ++i) { -- Gitee From f7e613f283b6059ae35b854354a40cc0276c1126 Mon Sep 17 00:00:00 2001 From: likholatovevgeny Date: Mon, 6 May 2024 13:52:48 +0300 Subject: [PATCH 61/77] Fixed bugs of copying same files and creating existing symlink. Signed-off-by: likholatovevgeny --- llvm-build/build.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/llvm-build/build.py b/llvm-build/build.py index 432ae4d17a8f..fd7cddd21853 100755 --- a/llvm-build/build.py +++ b/llvm-build/build.py @@ -2258,7 +2258,7 @@ class LlvmLibs(BuildUtils): for f in libs: self.check_copy_file(f'{f}.15', sysroot_lib_dir) os.chdir(sysroot_lib_dir) - os.symlink(f'{f}.15', f) + self.force_symlink(f'{f}.15', f) os.chdir(build_lib_dir) def build_gtest_defines(self, llvm_install): -- Gitee From 47e092cdbe27a364d514788ce39a244912af4925 Mon Sep 17 00:00:00 2001 From: likholatovevgeny Date: Tue, 7 May 2024 11:25:18 +0300 Subject: [PATCH 62/77] Changed DenseMap to DenseSet in Ctx. Signed-off-by: likholatovevgeny --- lld/ELF/Config.h | 5 +++-- lld/ELF/Driver.cpp | 12 +++++++++--- lld/ELF/InputFiles.cpp | 21 +++++++++++---------- lld/ELF/InputFiles.h | 4 ++-- 4 files changed, 25 insertions(+), 17 deletions(-) diff --git a/lld/ELF/Config.h b/lld/ELF/Config.h index d96a9d0d229f..1ea2f4dbe39f 100644 --- a/lld/ELF/Config.h +++ b/lld/ELF/Config.h @@ -399,8 +399,9 @@ struct Ctx { // A tuple of (reference, extractedFile, sym). Used by --why-extract=. SmallVector, 0> whyExtractRecords; - // Map of symbols frequency (only defined) - llvm::DenseMap eSymsFreqMap; + // Store duplicate symbols (only defined). + typedef llvm::DenseMap eSymsCntMap; + llvm::DenseSet eSymsHist; // A mapping from a symbol to an InputFile referencing it backward. Used by // --warn-backrefs. llvm::DenseMapundefined) addUnusedUndefined(name)->referenced = true; - // Fill sym frequensy map for defined syms, This will help to find duplicates. - if (config->adlt) + // Fill eSymsHist for defined syms. This will help to find duplicates. + if (config->adlt) { + Ctx::eSymsCntMap eSymsHist; for (auto *file : files) - fillSymsFreqMap(file); + buildSymsHist(file, eSymsHist); + for (auto eSym : eSymsHist) + if (eSym.second > 1) + ctx->eSymsHist.insert(eSym.first); + eSymsHist.clear(); + } // Add all files to the symbol table. This will add almost all // symbols that we need to the symbol table. This process might diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp index 08343c85e8e1..000c8d5ec0a1 100644 --- a/lld/ELF/InputFiles.cpp +++ b/lld/ELF/InputFiles.cpp @@ -158,15 +158,16 @@ static bool isCompatible(InputFile *file) { return false; } -template static void doFillSymsFreqMap(InputFile *file) { +template +static void doBuildSymsHist(InputFile *file, Ctx::eSymsCntMap &eSymsHist) { if (!isCompatible(file)) return; if (auto *f = dyn_cast>(file)) - f->fillSymsFreqMap(); + f->buildSymsHist(eSymsHist); } -void elf::fillSymsFreqMap(InputFile *file) { - invokeELFT(doFillSymsFreqMap, file); +void elf::buildSymsHist(InputFile *file, Ctx::eSymsCntMap &eSymsHist) { + invokeELFT(doBuildSymsHist, file, eSymsHist); } template static void doParseFile(InputFile *file) { @@ -1045,13 +1046,14 @@ InputSectionBase *ObjFile::createInputSection(uint32_t idx, return make(*this, sec, name); } -template void ObjFile::fillSymsFreqMap() { +template +void ObjFile::buildSymsHist(Ctx::eSymsCntMap &eSymsHist) { ArrayRef eSyms = this->getELFSyms(); for (size_t i = firstGlobal, end = eSyms.size(); i != end; ++i) { if (!eSyms[i].isDefined()) continue; StringRef name = CHECK(eSyms[i].getName(stringTable), this); - ctx->eSymsFreqMap[name]++; + eSymsHist[CachedHashStringRef(name)]++; } } @@ -1068,16 +1070,15 @@ void ObjFile::initializeSymbols(const object::ELFFile &obj) { for (size_t i = firstGlobal, end = eSyms.size(); i != end; ++i) if (!symbols[i]) { StringRef name = CHECK(eSyms[i].getName(stringTable), this); - if (config->adlt && eSyms[i].isDefined() && ctx->eSymsFreqMap[name] > 1) { - if (name == "__cfi_check") + if (config->adlt && eSyms[i].isDefined() && + ctx->eSymsHist.count(CachedHashStringRef(name)) != 0) { + if (!ctx->adltWithCfi && name == "__cfi_check") ctx->adltWithCfi = true; name = this->getUniqueName(name); } symbols[i] = symtab.insert(name); } - - // Perform symbol resolution on non-local symbols. SmallVector undefineds; for (size_t i = firstGlobal, end = eSyms.size(); i != end; ++i) { diff --git a/lld/ELF/InputFiles.h b/lld/ELF/InputFiles.h index 3366ca5c0348..65152c337279 100644 --- a/lld/ELF/InputFiles.h +++ b/lld/ELF/InputFiles.h @@ -47,7 +47,7 @@ extern std::unique_ptr tar; llvm::Optional readFile(StringRef path); // Add symbols in File to the symbol table. -void fillSymsFreqMap(InputFile *file); +void buildSymsHist(InputFile *file, Ctx::eSymsCntMap &eSymsHist); void parseFile(InputFile *file); // The root class of input files. @@ -293,7 +293,7 @@ public: // Get cached DWARF information. DWARFCache *getDwarf(); - void fillSymsFreqMap(); + void buildSymsHist(Ctx::eSymsCntMap &eSymsHist); void initializeLocalSymbols(); void postParse(); -- Gitee From b0fc2a3536ade6acd383d4d90dafaa93524e778e Mon Sep 17 00:00:00 2001 From: Khomutov Nikita Date: Wed, 8 May 2024 12:20:39 +0300 Subject: [PATCH 63/77] [ADLT] Generate psod-local program headers for loadable sections - added --adlt-trace option for logs - .rodata is discarded from sections merging - fix CFI alignment (if CFI found) - store common (w/o owner) loadable/tls program headers in .adlt - update .adlt section schema and version - update readobj to new .adlt section schema Signed-off-by: Khomutov Nikita Change-Id: Ic205ceb3df9e95e979e9f54e8b6928f887e364ac Signed-off-by: Khomutov Nikita --- lld/ELF/Config.h | 20 +++++++---- lld/ELF/Driver.cpp | 1 + lld/ELF/InputFiles.cpp | 7 ++-- lld/ELF/Options.td | 2 ++ lld/ELF/Relocations.cpp | 4 +-- lld/ELF/SyntheticSections.cpp | 28 ++++++++++++++- lld/ELF/SyntheticSections.h | 2 ++ lld/ELF/Writer.cpp | 51 ++++++++++++++++++++++----- llvm/tools/llvm-readobj/ELFDumper.cpp | 10 ++++++ 9 files changed, 104 insertions(+), 21 deletions(-) diff --git a/lld/ELF/Config.h b/lld/ELF/Config.h index 1ea2f4dbe39f..cd768db8a7ff 100644 --- a/lld/ELF/Config.h +++ b/lld/ELF/Config.h @@ -34,6 +34,7 @@ class BinaryFile; class BitcodeFile; class ELFFileBase; class SharedFile; +struct PhdrEntry; // OHOS_LOCAL class InputSectionBase; class Symbol; @@ -220,7 +221,10 @@ struct Configuration { std::vector> shuffleSections; bool singleRoRx; bool shared; + // OHOS_LOCAL begin bool adlt = false; + bool adltTrace = false; + // OHOS_LOCAL end bool symbolic; bool isStatic = false; bool sysvHash = false; @@ -408,12 +412,16 @@ struct Ctx { std::pair> 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> - gotPltInfoAdlt; // sym, soFile->orderIdx array; + // OHOS_LOCAL begin + struct AdltCtx { + llvm::SetVector commonProgramHeaders; + bool withCfi = false; + // From input .rela.dyn, .rela.plt: + // Keep input library indexes that are needed for got/plt symbol + llvm::DenseMap> + gotPltInfo; // sym, soFile->orderIdx array; + } adlt; + // OHOS_LOCAL end }; // The only instance of Ctx struct. diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp index 45d4cfec609b..8510ac21e692 100644 --- a/lld/ELF/Driver.cpp +++ b/lld/ELF/Driver.cpp @@ -1098,6 +1098,7 @@ static void readConfigs(opt::InputArgList &args) { config->bsymbolic = BsymbolicKind::All; } config->adlt = args.hasArg(OPT_adlt); + config->adltTrace = args.hasArg(OPT_adlt_trace); config->checkSections = args.hasFlag(OPT_check_sections, OPT_no_check_sections, true); config->chroot = args.getLastArgValue(OPT_chroot); diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp index 000c8d5ec0a1..2792e1a82dd2 100644 --- a/lld/ELF/InputFiles.cpp +++ b/lld/ELF/InputFiles.cpp @@ -1041,8 +1041,10 @@ InputSectionBase *ObjFile::createInputSection(uint32_t idx, if (name == ".eh_frame" && !config->relocatable) return make(*this, sec, name); - if ((sec.sh_flags & SHF_MERGE) && shouldMerge(sec, name)) + const bool doNotMerge = config->adlt && name.startswith(".rodata"); + if ((sec.sh_flags & SHF_MERGE) && !doNotMerge && shouldMerge(sec, name)) return make(*this, sec, name); + return make(*this, sec, name); } @@ -1072,8 +1074,7 @@ void ObjFile::initializeSymbols(const object::ELFFile &obj) { StringRef name = CHECK(eSyms[i].getName(stringTable), this); if (config->adlt && eSyms[i].isDefined() && ctx->eSymsHist.count(CachedHashStringRef(name)) != 0) { - if (!ctx->adltWithCfi && name == "__cfi_check") - ctx->adltWithCfi = true; + ctx->adlt.withCfi = ctx->adlt.withCfi || name == "__cfi_check"; name = this->getUniqueName(name); } symbols[i] = symtab.insert(name); diff --git a/lld/ELF/Options.td b/lld/ELF/Options.td index 46943047df31..8342d7be88d1 100644 --- a/lld/ELF/Options.td +++ b/lld/ELF/Options.td @@ -377,6 +377,8 @@ def shared: F<"shared">, HelpText<"Build a shared object">; def adlt: F<"adlt">, HelpText<"Build adlt library">; +def adlt_trace: F<"adlt-trace">, HelpText<"Enable ADLT trace logs">; + defm soname: Eq<"soname", "Set DT_SONAME">; defm sort_section: diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp index 322a88eb59d0..e46ffe21d0b8 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -1321,7 +1321,7 @@ static void trackDynRelocAdlt(SharedFileExtended *soFile) { template static void trackGotPltAdlt(Symbol *sym, SharedFileExtended *soFile) { - ctx->gotPltInfoAdlt[sym].push_back(soFile->orderIdx); + ctx->adlt.gotPltInfo[sym].push_back(soFile->orderIdx); } // ADLT BEGIN @@ -1820,7 +1820,7 @@ static bool handleNonPreemptibleIfunc(Symbol &sym) { } template static void addGotPltIndexAdlt(Symbol *s, bool isPlt) { - auto &vec = ctx->gotPltInfoAdlt[s]; + auto &vec = ctx->adlt.gotPltInfo[s]; for (auto &it : vec) { auto *soFile = cast>(ctx->sharedFilesExtended[it]); auto &entries = isPlt ? in.relaPlt->relocs : mainPart->relaDyn->relocs; diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp index 83a593200170..61d52dc894b5 100644 --- a/lld/ELF/SyntheticSections.cpp +++ b/lld/ELF/SyntheticSections.cpp @@ -4084,7 +4084,7 @@ static_assert(sizeof(adltBlobStartMark) == 4, "0xad17 consist of 4 bytes" ); -static_assert(sizeof(adlt_section_header_t) == 40, +static_assert(sizeof(adlt_section_header_t) == 56, "please update version if header has been changed" ); @@ -4126,6 +4126,7 @@ void AdltSection::finalizeContents() { getBlobStartOffset(), // .blobStart estimateBlobSize(), // .blobSize 0, // .overallMappedSize, known on writeTo + adlt_blob_u16_array_t{}, // .phIndexes, filled in writeTo }; buildSonameIndex(); @@ -4198,6 +4199,8 @@ typename AdltSection::CommonData AdltSection::makeCommonData() { return CommonData { UINT32_MAX, // .symtabSecIndex, filled in writeTo + ctx->adlt.commonProgramHeaders.size(), // .programHeadersAllocated + {}, // .phIndexes filled in writeTo }; } @@ -4283,6 +4286,8 @@ template size_t AdltSection::estimateBlobSize() const { size_t blobSize = sizeof(adltBlobStartMark); + blobSize += sizeof(uint16_t) * common.programHeadersAllocated; + for (const auto& soData: soInputs) { blobSize += sizeof(adlt_dt_needed_index_t) * soData.dtNeededs.size(); blobSize += sizeof(uint16_t) * soData.programHeadersAllocated; @@ -4342,6 +4347,21 @@ void AdltSection::extractProgramHeaderIndexes() { for (const auto& it : llvm::enumerate(mainPart->phdrs)) { phdrsIndexes[it.value()] = static_cast(it.index()); }; + + for (const PhdrEntry* phentry : ctx->adlt.commonProgramHeaders) { + auto it = phdrsIndexes.find(phentry); + if (it != phdrsIndexes.end()) { + common.phIndexes.push_back(it->second); + } + } + + assert(common.phIndexes.size() <= common.programHeadersAllocated); + if (common.phIndexes.size() != common.programHeadersAllocated) { + warn(Twine(".adlt section: overallocated common ph-indexes") + + " allocated=" + Twine(common.programHeadersAllocated) + + " actual=" + Twine(common.phIndexes.size()) + ); + } } template @@ -4405,6 +4425,12 @@ void AdltSection::writeTo(uint8_t* buf) { memcpy(blobBuf + blobOff, &adltBlobStartMark, sizeof(adltBlobStartMark)); blobOff += sizeof(adltBlobStartMark); + // common header-related data + { + header.phIndexes = writeArray(blobBuf, blobOff, common.phIndexes); + blobOff += header.phIndexes.size; + } + // psod-related data for(const auto& it : llvm::enumerate(soInputs)) { const auto& soData = it.value(); diff --git a/lld/ELF/SyntheticSections.h b/lld/ELF/SyntheticSections.h index 873f244d68bb..b51c85ce2ea8 100644 --- a/lld/ELF/SyntheticSections.h +++ b/lld/ELF/SyntheticSections.h @@ -1270,6 +1270,8 @@ public: // will be used to form some header data struct CommonData { uint32_t symtabSecIndex = UINT32_MAX; + size_t programHeadersAllocated; + SmallVector phIndexes; }; public: diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index 7439ed63c644..dd74a8bc98bf 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -1050,21 +1050,33 @@ 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; - if (config->adlt && (p_type == PT_LOAD || p_type == PT_TLS)) { - auto* insec = getInputSection(sec); - // skip generated sections. - // TODO: fix .rodata_$ADLT_POSTFIX sections. - if (insec && insec->file) { - invokeELFT(addPhdrToSharedFilesExtendedContext, insec->file, this); + // OHOS_LOCAL begin + if (config->adlt) { + if (ctx->adlt.withCfi && p_type == PT_LOAD) { + // check cfi.h: LIBRARY_ALIGNMENT and _BITS + constexpr uint32_t kCFILibraryAlignment = 1UL << 18; + firstSec->alignment = std::max(firstSec->alignment, kCFILibraryAlignment); + p_align = std::max(p_align, kCFILibraryAlignment); + } + + if (p_type == PT_LOAD || p_type == PT_TLS) { + auto* insec = getInputSection(sec); + // skip generated sections. + // TODO: fix .rodata_$ADLT_POSTFIX sections. + if (insec && insec->file) { + invokeELFT(addPhdrToSharedFilesExtendedContext, insec->file, this); + } else { + ctx->adlt.commonProgramHeaders.insert(this); + } } } + // OHOS_LOCAL end } // The beginning and the ending of .rel[a].plt section are marked @@ -2410,12 +2422,22 @@ SmallVector Writer::createPhdrs(Partition &part) { return ret.back(); }; + // OHOS_LOCAL begin + auto getOwnerFileIdx = [](OutputSection* sec) -> llvm::Optional { + auto file = getInputSection(sec)->file; + if (!file) + return llvm::None; + return cast>(file)->orderIdx; + }; + // OHOS_LOCAL end + unsigned partNo = part.getNumber(); bool isMain = partNo == 1; // Add the first PT_LOAD segment for regular output sections. uint64_t flags = computeFlags(PF_R); PhdrEntry *load = nullptr; + llvm::Optional lastOwnerIdx = llvm::None; // OHOS_LOCAL // nmagic or omagic output does not have PT_PHDR, PT_INTERP, or the readonly // PT_LOAD. @@ -2494,8 +2516,19 @@ SmallVector Writer::createPhdrs(Partition &part) { uint64_t newFlags = computeFlags(sec->getPhdrFlags()); bool sameLMARegion = load && !sec->lmaExpr && sec->lmaRegion == load->firstSec->lmaRegion; + + // OHOS_LOCAL begin + // ALDT image sections have additional addribute: input file owner. + // The ownership contiguousity allows to map only related sections + // when processing program headers that + bool sameFileOwner = config->adlt && lastOwnerIdx == getOwnerFileIdx(sec); + if (config->adlt) + lastOwnerIdx = getOwnerFileIdx(sec); + // OHOS_LOCAL end + if (!(load && newFlags == flags && sec != relroEnd && sec->memRegion == load->firstSec->memRegion && + (!config->adlt || sameFileOwner) && (sameLMARegion || load->lastSec == Out::programHeaders))) { load = addHdr(PT_LOAD, newFlags); flags = newFlags; diff --git a/llvm/tools/llvm-readobj/ELFDumper.cpp b/llvm/tools/llvm-readobj/ELFDumper.cpp index cf69601eab93..dea84138ddd5 100644 --- a/llvm/tools/llvm-readobj/ELFDumper.cpp +++ b/llvm/tools/llvm-readobj/ELFDumper.cpp @@ -7059,6 +7059,16 @@ template void LLVMELFDumper::printAdltSection() { return this->reportUniqueWarning("invalid .adlt section: " "blob is out of section range"); + ArrayRef psodsRaw; + ArrayRef blob; + + if (psodsRaw.data() + psodsRaw.size() > blob.data()) + return this->reportUniqueWarning("invalid .adlt section: " + "PSOD and blob entries are overlapped"); + if (blob.data() + blob.size() > adltRaw.data() + adltRaw.size()) + return this->reportUniqueWarning("invalid .adlt section: " + "blob is out of section range"); + DictScope DSec(W, "ADLT"); do { -- Gitee From 8c6b940b865e81beb66dd77d0e7bb2ad756580d3 Mon Sep 17 00:00:00 2001 From: likholatovevgeny Date: Mon, 13 May 2024 14:41:17 +0300 Subject: [PATCH 64/77] Fixed bug of building compiler-rt and libcxx. Signed-off-by: likholatovevgeny --- llvm-build/LLVMExports.cmake | 1150 ++++++++++++++++++++++++++++++++++ llvm-build/build.py | 12 + 2 files changed, 1162 insertions(+) create mode 100644 llvm-build/LLVMExports.cmake diff --git a/llvm-build/LLVMExports.cmake b/llvm-build/LLVMExports.cmake new file mode 100644 index 000000000000..b64013dc5858 --- /dev/null +++ b/llvm-build/LLVMExports.cmake @@ -0,0 +1,1150 @@ +# Generated by CMake + +if("${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}" LESS 2.8) + message(FATAL_ERROR "CMake >= 2.8.0 required") +endif() +if(CMAKE_VERSION VERSION_LESS "2.8.3") + message(FATAL_ERROR "CMake >= 2.8.3 required") +endif() +cmake_policy(PUSH) +cmake_policy(VERSION 2.8.3...3.26) +#---------------------------------------------------------------- +# Generated CMake target import file. +#---------------------------------------------------------------- + +# Commands may need to know the format version. +set(CMAKE_IMPORT_FILE_VERSION 1) + +# Protect against multiple inclusion, which would fail when already imported targets are added once more. +set(_cmake_targets_defined "") +set(_cmake_targets_not_defined "") +set(_cmake_expected_targets "") +foreach(_cmake_expected_target IN ITEMS LLVMDemangle LLVMSupport LLVMTableGen LLVMTableGenGlobalISel llvm-tblgen LLVMCore LLVMFuzzerCLI LLVMFuzzMutate LLVMFileCheck LLVMInterfaceStub LLVMIRReader LLVMCodeGen LLVMSelectionDAG LLVMAsmPrinter LLVMMIRParser LLVMGlobalISel LLVMBinaryFormat LLVMBitReader LLVMBitWriter LLVMBitstreamReader LLVMDWARFLinker LLVMExtensions LLVMFrontendOpenACC LLVMFrontendOpenMP LLVMTransformUtils LLVMInstrumentation LLVMAggressiveInstCombine LLVMInstCombine LLVMScalarOpts LLVMipo LLVMVectorize LLVMObjCARCOpts LLVMCoroutines LLVMCFGuard LLVMLinker LLVMAnalysis LLVMLTO LLVMMC LLVMMCParser LLVMMCDisassembler LLVMMCA LLVMObjCopy LLVMObject LLVMObjectYAML LLVMOption LLVMRemarks LLVMDebuginfod LLVMDebugInfoDWARF LLVMDebugInfoGSYM LLVMDebugInfoMSF LLVMDebugInfoCodeView LLVMDebugInfoPDB LLVMSymbolize LLVMDWP LLVMExecutionEngine LLVMInterpreter LLVMJITLink LLVMMCJIT LLVMOrcJIT LLVMOrcShared LLVMOrcTargetProcess LLVMRuntimeDyld LLVMTarget LLVMAArch64CodeGen LLVMAArch64AsmParser LLVMAArch64Disassembler LLVMAArch64Desc LLVMAArch64Info LLVMAArch64Utils LLVMARMCodeGen LLVMARMAsmParser LLVMARMDisassembler LLVMARMDesc LLVMARMInfo LLVMARMUtils LLVMBPFCodeGen LLVMBPFAsmParser LLVMBPFDisassembler LLVMBPFDesc LLVMBPFInfo LLVMMipsCodeGen LLVMMipsAsmParser LLVMMipsDisassembler LLVMMipsDesc LLVMMipsInfo LLVMRISCVCodeGen LLVMRISCVAsmParser LLVMRISCVDisassembler LLVMRISCVDesc LLVMRISCVInfo LLVMX86CodeGen LLVMX86AsmParser LLVMX86Disassembler LLVMX86TargetMCA LLVMX86Desc LLVMX86Info LLVMAsmParser LLVMLineEditor LLVMProfileData LLVMCoverage LLVMPasses LLVMTextAPI LLVMDlltoolDriver LLVMLibDriver LLVMXRay LLVMWindowsDriver LLVMWindowsManifest LLVMParts FileCheck llvm-PerfectShuffle count not UnicodeNameMappingGenerator yaml-bench omptarget omptarget.rtl.amdgpu omptarget.rtl.cuda omptarget.rtl.x86_64 llvm-omp-device-info LTO LLVMgold llvm-ar llvm-config llvm-lto llvm-profdata bugpoint dsymutil llc lli-child-target lli llvm-as llvm-bcanalyzer llvm-c-test llvm-cat llvm-cfi-verify LLVMCFIVerify llvm-cov llvm-cvtres llvm-cxxdump llvm-cxxfilt llvm-cxxmap llvm-debuginfod llvm-debuginfod-find llvm-diff LLVMDiff llvm-dis llvm-dwarfdump llvm-dwarfutil llvm-dwp llvm-exegesis LLVMExegesisX86 LLVMExegesisAArch64 LLVMExegesisMips LLVMExegesis llvm-extract llvm-gsymutil llvm-ifs llvm-jitlink-executor llvm-jitlink llvm-libtool-darwin llvm-link llvm-lipo llvm-lto2 llvm-mc llvm-mca llvm-ml llvm-modextract llvm-mt llvm-nm llvm-objcopy llvm-objdump llvm-opt-report llvm-pdbutil llvm-profgen llvm-rc llvm-readobj llvm-reduce llvm-remark-size-diff llvm-rtdyld LLVM llvm-sim llvm-size llvm-split llvm-stress llvm-strings llvm-symbolizer llvm-tapi-diff llvm-tli-checker llvm-undname llvm-xray obj2yaml opt Remarks sancov sanstats split-file verify-uselistorder yaml2obj) + list(APPEND _cmake_expected_targets "${_cmake_expected_target}") + if(TARGET "${_cmake_expected_target}") + list(APPEND _cmake_targets_defined "${_cmake_expected_target}") + else() + list(APPEND _cmake_targets_not_defined "${_cmake_expected_target}") + endif() +endforeach() +unset(_cmake_expected_target) +if(_cmake_targets_defined STREQUAL _cmake_expected_targets) + unset(_cmake_targets_defined) + unset(_cmake_targets_not_defined) + unset(_cmake_expected_targets) + unset(CMAKE_IMPORT_FILE_VERSION) + cmake_policy(POP) + return() +endif() +if(NOT _cmake_targets_defined STREQUAL "") + string(REPLACE ";" ", " _cmake_targets_defined_text "${_cmake_targets_defined}") + string(REPLACE ";" ", " _cmake_targets_not_defined_text "${_cmake_targets_not_defined}") + message(FATAL_ERROR "Some (but not all) targets in this export set were already defined.\nTargets Defined: ${_cmake_targets_defined_text}\nTargets not yet defined: ${_cmake_targets_not_defined_text}\n") +endif() +unset(_cmake_targets_defined) +unset(_cmake_targets_not_defined) +unset(_cmake_expected_targets) + + +# Compute the installation prefix relative to this file. +get_filename_component(_IMPORT_PREFIX "${CMAKE_CURRENT_LIST_FILE}" PATH) +get_filename_component(_IMPORT_PREFIX "${_IMPORT_PREFIX}" PATH) +get_filename_component(_IMPORT_PREFIX "${_IMPORT_PREFIX}" PATH) +get_filename_component(_IMPORT_PREFIX "${_IMPORT_PREFIX}" PATH) +if(_IMPORT_PREFIX STREQUAL "/") + set(_IMPORT_PREFIX "") +endif() + +# Create imported target LLVMDemangle +add_library(LLVMDemangle STATIC IMPORTED) + +# Create imported target LLVMSupport +add_library(LLVMSupport STATIC IMPORTED) + +set_target_properties(LLVMSupport PROPERTIES + INTERFACE_LINK_LIBRARIES "rt;dl;-lpthread;m;ZLIB::ZLIB;LLVMDemangle" +) + +# Create imported target LLVMTableGen +add_library(LLVMTableGen STATIC IMPORTED) + +set_target_properties(LLVMTableGen PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMSupport" +) + +# Create imported target LLVMTableGenGlobalISel +add_library(LLVMTableGenGlobalISel STATIC IMPORTED) + +set_target_properties(LLVMTableGenGlobalISel PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMSupport;LLVMTableGen" +) + +# Create imported target llvm-tblgen +add_executable(llvm-tblgen IMPORTED) + +# Create imported target LLVMCore +add_library(LLVMCore STATIC IMPORTED) + +set_target_properties(LLVMCore PROPERTIES + INTERFACE_LINK_LIBRARIES "-lpthread;LLVMBinaryFormat;LLVMRemarks;LLVMSupport" +) + +# Create imported target LLVMFuzzerCLI +add_library(LLVMFuzzerCLI STATIC IMPORTED) + +set_target_properties(LLVMFuzzerCLI PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMSupport" +) + +# Create imported target LLVMFuzzMutate +add_library(LLVMFuzzMutate STATIC IMPORTED) + +set_target_properties(LLVMFuzzMutate PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMAnalysis;LLVMBitReader;LLVMBitWriter;LLVMCore;LLVMScalarOpts;LLVMSupport;LLVMTarget" +) + +# Create imported target LLVMFileCheck +add_library(LLVMFileCheck STATIC IMPORTED) + +set_target_properties(LLVMFileCheck PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMSupport" +) + +# Create imported target LLVMInterfaceStub +add_library(LLVMInterfaceStub STATIC IMPORTED) + +set_target_properties(LLVMInterfaceStub PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMBinaryFormat;LLVMMC;LLVMObject;LLVMSupport" +) + +# Create imported target LLVMIRReader +add_library(LLVMIRReader STATIC IMPORTED) + +set_target_properties(LLVMIRReader PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMAsmParser;LLVMBitReader;LLVMCore;LLVMSupport" +) + +# Create imported target LLVMCodeGen +add_library(LLVMCodeGen STATIC IMPORTED) + +set_target_properties(LLVMCodeGen PROPERTIES + INTERFACE_LINK_LIBRARIES "-lpthread;LLVMAnalysis;LLVMBitReader;LLVMBitWriter;LLVMCore;LLVMMC;LLVMProfileData;LLVMScalarOpts;LLVMSupport;LLVMTarget;LLVMTransformUtils" +) + +# Create imported target LLVMSelectionDAG +add_library(LLVMSelectionDAG STATIC IMPORTED) + +set_target_properties(LLVMSelectionDAG PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMAnalysis;LLVMCodeGen;LLVMCore;LLVMMC;LLVMSupport;LLVMTarget;LLVMTransformUtils" +) + +# Create imported target LLVMAsmPrinter +add_library(LLVMAsmPrinter STATIC IMPORTED) + +set_target_properties(LLVMAsmPrinter PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMAnalysis;LLVMBinaryFormat;LLVMCodeGen;LLVMCore;LLVMDebugInfoCodeView;LLVMDebugInfoDWARF;LLVMDebugInfoMSF;LLVMMC;LLVMMCParser;LLVMRemarks;LLVMSupport;LLVMTarget" +) + +# Create imported target LLVMMIRParser +add_library(LLVMMIRParser STATIC IMPORTED) + +set_target_properties(LLVMMIRParser PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMAsmParser;LLVMBinaryFormat;LLVMCodeGen;LLVMCore;LLVMMC;LLVMSupport;LLVMTarget" +) + +# Create imported target LLVMGlobalISel +add_library(LLVMGlobalISel STATIC IMPORTED) + +set_target_properties(LLVMGlobalISel PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMAnalysis;LLVMCodeGen;LLVMCore;LLVMMC;LLVMSelectionDAG;LLVMSupport;LLVMTarget;LLVMTransformUtils" +) + +# Create imported target LLVMBinaryFormat +add_library(LLVMBinaryFormat STATIC IMPORTED) + +set_target_properties(LLVMBinaryFormat PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMSupport" +) + +# Create imported target LLVMBitReader +add_library(LLVMBitReader STATIC IMPORTED) + +set_target_properties(LLVMBitReader PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMBitstreamReader;LLVMCore;LLVMSupport" +) + +# Create imported target LLVMBitWriter +add_library(LLVMBitWriter STATIC IMPORTED) + +set_target_properties(LLVMBitWriter PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMAnalysis;LLVMCore;LLVMMC;LLVMObject;LLVMSupport" +) + +# Create imported target LLVMBitstreamReader +add_library(LLVMBitstreamReader STATIC IMPORTED) + +set_target_properties(LLVMBitstreamReader PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMSupport" +) + +# Create imported target LLVMDWARFLinker +add_library(LLVMDWARFLinker STATIC IMPORTED) + +set_target_properties(LLVMDWARFLinker PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMBinaryFormat;LLVMDebugInfoDWARF;LLVMAsmPrinter;LLVMCodeGen;LLVMMC;LLVMObject;LLVMSupport" +) + +# Create imported target LLVMExtensions +add_library(LLVMExtensions STATIC IMPORTED) + +set_target_properties(LLVMExtensions PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMSupport" +) + +# Create imported target LLVMFrontendOpenACC +add_library(LLVMFrontendOpenACC STATIC IMPORTED) + +set_target_properties(LLVMFrontendOpenACC PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMSupport" +) + +# Create imported target LLVMFrontendOpenMP +add_library(LLVMFrontendOpenMP STATIC IMPORTED) + +set_target_properties(LLVMFrontendOpenMP PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMCore;LLVMSupport;LLVMTransformUtils;LLVMAnalysis;LLVMMC;LLVMScalarOpts" +) + +# Create imported target LLVMTransformUtils +add_library(LLVMTransformUtils STATIC IMPORTED) + +set_target_properties(LLVMTransformUtils PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMAnalysis;LLVMCore;LLVMSupport" +) + +# Create imported target LLVMInstrumentation +add_library(LLVMInstrumentation STATIC IMPORTED) + +set_target_properties(LLVMInstrumentation PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMAnalysis;LLVMCore;LLVMDemangle;LLVMMC;LLVMSupport;LLVMTransformUtils;LLVMProfileData" +) + +# Create imported target LLVMAggressiveInstCombine +add_library(LLVMAggressiveInstCombine STATIC IMPORTED) + +set_target_properties(LLVMAggressiveInstCombine PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMAnalysis;LLVMCore;LLVMSupport;LLVMTransformUtils" +) + +# Create imported target LLVMInstCombine +add_library(LLVMInstCombine STATIC IMPORTED) + +set_target_properties(LLVMInstCombine PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMAnalysis;LLVMCore;LLVMSupport;LLVMTransformUtils" +) + +# Create imported target LLVMScalarOpts +add_library(LLVMScalarOpts STATIC IMPORTED) + +set_target_properties(LLVMScalarOpts PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMAggressiveInstCombine;LLVMAnalysis;LLVMCore;LLVMInstCombine;LLVMSupport;LLVMTransformUtils" +) + +# Create imported target LLVMipo +add_library(LLVMipo STATIC IMPORTED) + +set_target_properties(LLVMipo PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMAggressiveInstCombine;LLVMAnalysis;LLVMBitReader;LLVMBitWriter;LLVMCore;LLVMFrontendOpenMP;LLVMInstCombine;LLVMIRReader;LLVMLinker;LLVMObject;LLVMProfileData;LLVMScalarOpts;LLVMSupport;LLVMTransformUtils;LLVMVectorize;LLVMInstrumentation;LLVMScalarOpts" +) + +# Create imported target LLVMVectorize +add_library(LLVMVectorize STATIC IMPORTED) + +set_target_properties(LLVMVectorize PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMAnalysis;LLVMCore;LLVMSupport;LLVMTransformUtils" +) + +# Create imported target LLVMObjCARCOpts +add_library(LLVMObjCARCOpts STATIC IMPORTED) + +set_target_properties(LLVMObjCARCOpts PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMAnalysis;LLVMCore;LLVMSupport;LLVMTransformUtils" +) + +# Create imported target LLVMCoroutines +add_library(LLVMCoroutines STATIC IMPORTED) + +set_target_properties(LLVMCoroutines PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMAnalysis;LLVMCore;LLVMipo;LLVMScalarOpts;LLVMSupport;LLVMTransformUtils" +) + +# Create imported target LLVMCFGuard +add_library(LLVMCFGuard STATIC IMPORTED) + +set_target_properties(LLVMCFGuard PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMCore;LLVMSupport" +) + +# Create imported target LLVMLinker +add_library(LLVMLinker STATIC IMPORTED) + +set_target_properties(LLVMLinker PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMCore;LLVMObject;LLVMSupport;LLVMTransformUtils" +) + +# Create imported target LLVMAnalysis +add_library(LLVMAnalysis STATIC IMPORTED) + +set_target_properties(LLVMAnalysis PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMBinaryFormat;LLVMCore;LLVMObject;LLVMProfileData;LLVMSupport" +) + +# Create imported target LLVMLTO +add_library(LLVMLTO STATIC IMPORTED) + +set_target_properties(LLVMLTO PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMAggressiveInstCombine;LLVMAnalysis;LLVMBinaryFormat;LLVMBitReader;LLVMBitWriter;LLVMCodeGen;LLVMCore;LLVMExtensions;LLVMipo;LLVMInstCombine;LLVMInstrumentation;LLVMLinker;LLVMMC;LLVMObjCARCOpts;LLVMObject;LLVMPasses;LLVMRemarks;LLVMScalarOpts;LLVMSupport;LLVMTarget;LLVMTransformUtils" +) + +# Create imported target LLVMMC +add_library(LLVMMC STATIC IMPORTED) + +set_target_properties(LLVMMC PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMSupport;LLVMBinaryFormat;LLVMDebugInfoCodeView" +) + +# Create imported target LLVMMCParser +add_library(LLVMMCParser STATIC IMPORTED) + +set_target_properties(LLVMMCParser PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMMC;LLVMSupport" +) + +# Create imported target LLVMMCDisassembler +add_library(LLVMMCDisassembler STATIC IMPORTED) + +set_target_properties(LLVMMCDisassembler PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMMC;LLVMSupport" +) + +# Create imported target LLVMMCA +add_library(LLVMMCA STATIC IMPORTED) + +set_target_properties(LLVMMCA PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMMC;LLVMSupport" +) + +# Create imported target LLVMObjCopy +add_library(LLVMObjCopy STATIC IMPORTED) + +set_target_properties(LLVMObjCopy PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMBinaryFormat;LLVMObject;LLVMSupport;LLVMMC" +) + +# Create imported target LLVMObject +add_library(LLVMObject STATIC IMPORTED) + +set_target_properties(LLVMObject PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMBitReader;LLVMCore;LLVMMC;LLVMBinaryFormat;LLVMMCParser;LLVMSupport;LLVMTextAPI" +) + +# Create imported target LLVMObjectYAML +add_library(LLVMObjectYAML STATIC IMPORTED) + +set_target_properties(LLVMObjectYAML PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMBinaryFormat;LLVMObject;LLVMSupport;LLVMDebugInfoCodeView;LLVMMC" +) + +# Create imported target LLVMOption +add_library(LLVMOption STATIC IMPORTED) + +set_target_properties(LLVMOption PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMSupport" +) + +# Create imported target LLVMRemarks +add_library(LLVMRemarks STATIC IMPORTED) + +set_target_properties(LLVMRemarks PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMBitstreamReader;LLVMSupport" +) + +# Create imported target LLVMDebuginfod +add_library(LLVMDebuginfod STATIC IMPORTED) + +set_target_properties(LLVMDebuginfod PROPERTIES + INTERFACE_LINK_LIBRARIES "-lpthread;LLVMSupport;LLVMSymbolize;LLVMDebugInfoDWARF;LLVMBinaryFormat;LLVMObject" +) + +# Create imported target LLVMDebugInfoDWARF +add_library(LLVMDebugInfoDWARF STATIC IMPORTED) + +set_target_properties(LLVMDebugInfoDWARF PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMBinaryFormat;LLVMObject;LLVMMC;LLVMSupport" +) + +# Create imported target LLVMDebugInfoGSYM +add_library(LLVMDebugInfoGSYM STATIC IMPORTED) + +set_target_properties(LLVMDebugInfoGSYM PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMMC;LLVMObject;LLVMSupport;LLVMDebugInfoDWARF" +) + +# Create imported target LLVMDebugInfoMSF +add_library(LLVMDebugInfoMSF STATIC IMPORTED) + +set_target_properties(LLVMDebugInfoMSF PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMSupport" +) + +# Create imported target LLVMDebugInfoCodeView +add_library(LLVMDebugInfoCodeView STATIC IMPORTED) + +set_target_properties(LLVMDebugInfoCodeView PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMSupport" +) + +# Create imported target LLVMDebugInfoPDB +add_library(LLVMDebugInfoPDB STATIC IMPORTED) + +set_target_properties(LLVMDebugInfoPDB PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMBinaryFormat;LLVMObject;LLVMSupport;LLVMDebugInfoCodeView;LLVMDebugInfoMSF" +) + +# Create imported target LLVMSymbolize +add_library(LLVMSymbolize STATIC IMPORTED) + +set_target_properties(LLVMSymbolize PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMDebugInfoDWARF;LLVMDebugInfoPDB;LLVMObject;LLVMSupport;LLVMDemangle" +) + +# Create imported target LLVMDWP +add_library(LLVMDWP STATIC IMPORTED) + +set_target_properties(LLVMDWP PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMDebugInfoDWARF;LLVMMC;LLVMObject;LLVMSupport;LLVMTarget" +) + +# Create imported target LLVMExecutionEngine +add_library(LLVMExecutionEngine STATIC IMPORTED) + +set_target_properties(LLVMExecutionEngine PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMCore;LLVMMC;LLVMObject;LLVMOrcTargetProcess;LLVMRuntimeDyld;LLVMSupport;LLVMTarget" +) + +# Create imported target LLVMInterpreter +add_library(LLVMInterpreter STATIC IMPORTED) + +set_target_properties(LLVMInterpreter PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMCodeGen;LLVMCore;LLVMExecutionEngine;LLVMSupport" +) + +# Create imported target LLVMJITLink +add_library(LLVMJITLink STATIC IMPORTED) + +set_target_properties(LLVMJITLink PROPERTIES + INTERFACE_LINK_LIBRARIES "\$;\$;\$;\$;LLVMBinaryFormat;LLVMObject;LLVMOrcTargetProcess;LLVMSupport" +) + +# Create imported target LLVMMCJIT +add_library(LLVMMCJIT STATIC IMPORTED) + +set_target_properties(LLVMMCJIT PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMCore;LLVMExecutionEngine;LLVMObject;LLVMRuntimeDyld;LLVMSupport;LLVMTarget" +) + +# Create imported target LLVMOrcJIT +add_library(LLVMOrcJIT STATIC IMPORTED) + +set_target_properties(LLVMOrcJIT PROPERTIES + INTERFACE_LINK_LIBRARIES "-lpthread;rt;\$;\$;\$;\$;LLVMCore;LLVMExecutionEngine;LLVMJITLink;LLVMObject;LLVMOrcShared;LLVMOrcTargetProcess;LLVMMC;LLVMMCDisassembler;LLVMPasses;LLVMRuntimeDyld;LLVMSupport;LLVMTarget;LLVMTransformUtils" +) + +# Create imported target LLVMOrcShared +add_library(LLVMOrcShared STATIC IMPORTED) + +set_target_properties(LLVMOrcShared PROPERTIES + INTERFACE_LINK_LIBRARIES "-lpthread;LLVMSupport" +) + +# Create imported target LLVMOrcTargetProcess +add_library(LLVMOrcTargetProcess STATIC IMPORTED) + +set_target_properties(LLVMOrcTargetProcess PROPERTIES + INTERFACE_LINK_LIBRARIES "-lpthread;rt;LLVMOrcShared;LLVMSupport" +) + +# Create imported target LLVMRuntimeDyld +add_library(LLVMRuntimeDyld STATIC IMPORTED) + +set_target_properties(LLVMRuntimeDyld PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMCore;LLVMMC;LLVMObject;LLVMSupport" +) + +# Create imported target LLVMTarget +add_library(LLVMTarget STATIC IMPORTED) + +set_target_properties(LLVMTarget PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMAnalysis;LLVMCore;LLVMMC;LLVMSupport" +) + +# Create imported target LLVMAArch64CodeGen +add_library(LLVMAArch64CodeGen STATIC IMPORTED) + +set_target_properties(LLVMAArch64CodeGen PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMParts;LLVMAArch64Desc;LLVMAArch64Info;LLVMAArch64Utils;LLVMAnalysis;LLVMAsmPrinter;LLVMCodeGen;LLVMCore;LLVMMC;LLVMScalarOpts;LLVMSelectionDAG;LLVMSupport;LLVMTarget;LLVMTransformUtils;LLVMGlobalISel;LLVMCFGuard" +) + +# Create imported target LLVMAArch64AsmParser +add_library(LLVMAArch64AsmParser STATIC IMPORTED) + +set_target_properties(LLVMAArch64AsmParser PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMAArch64Desc;LLVMAArch64Info;LLVMAArch64Utils;LLVMMC;LLVMMCParser;LLVMSupport" +) + +# Create imported target LLVMAArch64Disassembler +add_library(LLVMAArch64Disassembler STATIC IMPORTED) + +set_target_properties(LLVMAArch64Disassembler PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMAArch64Desc;LLVMAArch64Info;LLVMAArch64Utils;LLVMMC;LLVMMCDisassembler;LLVMSupport" +) + +# Create imported target LLVMAArch64Desc +add_library(LLVMAArch64Desc STATIC IMPORTED) + +set_target_properties(LLVMAArch64Desc PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMAArch64Info;LLVMAArch64Utils;LLVMMC;LLVMBinaryFormat;LLVMSupport" +) + +# Create imported target LLVMAArch64Info +add_library(LLVMAArch64Info STATIC IMPORTED) + +set_target_properties(LLVMAArch64Info PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMMC;LLVMSupport" +) + +# Create imported target LLVMAArch64Utils +add_library(LLVMAArch64Utils STATIC IMPORTED) + +set_target_properties(LLVMAArch64Utils PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMSupport" +) + +# Create imported target LLVMARMCodeGen +add_library(LLVMARMCodeGen STATIC IMPORTED) + +set_target_properties(LLVMARMCodeGen PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMARMDesc;LLVMARMInfo;LLVMAnalysis;LLVMAsmPrinter;LLVMCodeGen;LLVMCore;LLVMipo;LLVMMC;LLVMScalarOpts;LLVMSelectionDAG;LLVMSupport;LLVMTarget;LLVMGlobalISel;LLVMARMUtils;LLVMTransformUtils;LLVMCFGuard" +) + +# Create imported target LLVMARMAsmParser +add_library(LLVMARMAsmParser STATIC IMPORTED) + +set_target_properties(LLVMARMAsmParser PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMARMDesc;LLVMARMInfo;LLVMMC;LLVMMCParser;LLVMSupport;LLVMARMUtils" +) + +# Create imported target LLVMARMDisassembler +add_library(LLVMARMDisassembler STATIC IMPORTED) + +set_target_properties(LLVMARMDisassembler PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMARMDesc;LLVMARMInfo;LLVMMCDisassembler;LLVMSupport;LLVMARMUtils" +) + +# Create imported target LLVMARMDesc +add_library(LLVMARMDesc STATIC IMPORTED) + +set_target_properties(LLVMARMDesc PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMARMInfo;LLVMARMUtils;LLVMMC;LLVMMCDisassembler;LLVMSupport;LLVMBinaryFormat" +) + +# Create imported target LLVMARMInfo +add_library(LLVMARMInfo STATIC IMPORTED) + +set_target_properties(LLVMARMInfo PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMMC;LLVMSupport" +) + +# Create imported target LLVMARMUtils +add_library(LLVMARMUtils STATIC IMPORTED) + +set_target_properties(LLVMARMUtils PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMSupport" +) + +# Create imported target LLVMBPFCodeGen +add_library(LLVMBPFCodeGen STATIC IMPORTED) + +set_target_properties(LLVMBPFCodeGen PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMAnalysis;LLVMAsmPrinter;LLVMCodeGen;LLVMCore;LLVMMC;LLVMBPFDesc;LLVMBPFInfo;LLVMipo;LLVMScalarOpts;LLVMSelectionDAG;LLVMSupport;LLVMTarget;LLVMTransformUtils" +) + +# Create imported target LLVMBPFAsmParser +add_library(LLVMBPFAsmParser STATIC IMPORTED) + +set_target_properties(LLVMBPFAsmParser PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMMC;LLVMMCParser;LLVMBPFDesc;LLVMBPFInfo;LLVMSupport" +) + +# Create imported target LLVMBPFDisassembler +add_library(LLVMBPFDisassembler STATIC IMPORTED) + +set_target_properties(LLVMBPFDisassembler PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMMCDisassembler;LLVMBPFInfo;LLVMSupport" +) + +# Create imported target LLVMBPFDesc +add_library(LLVMBPFDesc STATIC IMPORTED) + +set_target_properties(LLVMBPFDesc PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMMC;LLVMBPFInfo;LLVMSupport" +) + +# Create imported target LLVMBPFInfo +add_library(LLVMBPFInfo STATIC IMPORTED) + +set_target_properties(LLVMBPFInfo PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMMC;LLVMSupport" +) + +# Create imported target LLVMMipsCodeGen +add_library(LLVMMipsCodeGen STATIC IMPORTED) + +set_target_properties(LLVMMipsCodeGen PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMAnalysis;LLVMAsmPrinter;LLVMCodeGen;LLVMCore;LLVMMC;LLVMMipsDesc;LLVMMipsInfo;LLVMSelectionDAG;LLVMSupport;LLVMTarget;LLVMGlobalISel" +) + +# Create imported target LLVMMipsAsmParser +add_library(LLVMMipsAsmParser STATIC IMPORTED) + +set_target_properties(LLVMMipsAsmParser PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMMC;LLVMMCParser;LLVMMipsDesc;LLVMMipsInfo;LLVMSupport" +) + +# Create imported target LLVMMipsDisassembler +add_library(LLVMMipsDisassembler STATIC IMPORTED) + +set_target_properties(LLVMMipsDisassembler PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMMCDisassembler;LLVMMipsInfo;LLVMSupport" +) + +# Create imported target LLVMMipsDesc +add_library(LLVMMipsDesc STATIC IMPORTED) + +set_target_properties(LLVMMipsDesc PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMMC;LLVMMipsInfo;LLVMSupport" +) + +# Create imported target LLVMMipsInfo +add_library(LLVMMipsInfo STATIC IMPORTED) + +set_target_properties(LLVMMipsInfo PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMMC;LLVMSupport" +) + +# Create imported target LLVMRISCVCodeGen +add_library(LLVMRISCVCodeGen STATIC IMPORTED) + +set_target_properties(LLVMRISCVCodeGen PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMAnalysis;LLVMAsmPrinter;LLVMCore;LLVMipo;LLVMCodeGen;LLVMMC;LLVMRISCVDesc;LLVMRISCVInfo;LLVMSelectionDAG;LLVMSupport;LLVMTarget;LLVMTransformUtils;LLVMGlobalISel" +) + +# Create imported target LLVMRISCVAsmParser +add_library(LLVMRISCVAsmParser STATIC IMPORTED) + +set_target_properties(LLVMRISCVAsmParser PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMMC;LLVMMCParser;LLVMRISCVDesc;LLVMRISCVInfo;LLVMSupport" +) + +# Create imported target LLVMRISCVDisassembler +add_library(LLVMRISCVDisassembler STATIC IMPORTED) + +set_target_properties(LLVMRISCVDisassembler PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMMC;LLVMMCDisassembler;LLVMRISCVDesc;LLVMRISCVInfo;LLVMSupport" +) + +# Create imported target LLVMRISCVDesc +add_library(LLVMRISCVDesc STATIC IMPORTED) + +set_target_properties(LLVMRISCVDesc PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMMC;LLVMRISCVInfo;LLVMSupport" +) + +# Create imported target LLVMRISCVInfo +add_library(LLVMRISCVInfo STATIC IMPORTED) + +set_target_properties(LLVMRISCVInfo PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMMC;LLVMSupport" +) + +# Create imported target LLVMX86CodeGen +add_library(LLVMX86CodeGen STATIC IMPORTED) + +set_target_properties(LLVMX86CodeGen PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMAnalysis;LLVMAsmPrinter;LLVMCodeGen;LLVMCore;LLVMInstrumentation;LLVMMC;LLVMSelectionDAG;LLVMSupport;LLVMTarget;LLVMTransformUtils;LLVMX86Desc;LLVMX86Info;LLVMGlobalISel;LLVMProfileData;LLVMCFGuard" +) + +# Create imported target LLVMX86AsmParser +add_library(LLVMX86AsmParser STATIC IMPORTED) + +set_target_properties(LLVMX86AsmParser PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMMC;LLVMMCParser;LLVMSupport;LLVMX86Desc;LLVMX86Info" +) + +# Create imported target LLVMX86Disassembler +add_library(LLVMX86Disassembler STATIC IMPORTED) + +set_target_properties(LLVMX86Disassembler PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMMCDisassembler;LLVMSupport;LLVMX86Info" +) + +# Create imported target LLVMX86TargetMCA +add_library(LLVMX86TargetMCA STATIC IMPORTED) + +set_target_properties(LLVMX86TargetMCA PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMMC;LLVMMCParser;LLVMX86Desc;LLVMX86Info;LLVMSupport;LLVMMCA" +) + +# Create imported target LLVMX86Desc +add_library(LLVMX86Desc STATIC IMPORTED) + +set_target_properties(LLVMX86Desc PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMMC;LLVMMCDisassembler;LLVMSupport;LLVMX86Info;LLVMBinaryFormat" +) + +# Create imported target LLVMX86Info +add_library(LLVMX86Info STATIC IMPORTED) + +set_target_properties(LLVMX86Info PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMMC;LLVMSupport" +) + +# Create imported target LLVMAsmParser +add_library(LLVMAsmParser STATIC IMPORTED) + +set_target_properties(LLVMAsmParser PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMBinaryFormat;LLVMCore;LLVMSupport" +) + +# Create imported target LLVMLineEditor +add_library(LLVMLineEditor STATIC IMPORTED) + +set_target_properties(LLVMLineEditor PROPERTIES + INTERFACE_LINK_LIBRARIES "LibEdit::LibEdit;LLVMSupport" +) + +# Create imported target LLVMProfileData +add_library(LLVMProfileData STATIC IMPORTED) + +set_target_properties(LLVMProfileData PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMCore;LLVMObject;LLVMSupport;LLVMDemangle;LLVMSymbolize;LLVMDebugInfoDWARF" +) + +# Create imported target LLVMCoverage +add_library(LLVMCoverage STATIC IMPORTED) + +set_target_properties(LLVMCoverage PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMCore;LLVMObject;LLVMProfileData;LLVMSupport" +) + +# Create imported target LLVMPasses +add_library(LLVMPasses STATIC IMPORTED) + +set_target_properties(LLVMPasses PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMAggressiveInstCombine;LLVMAnalysis;LLVMCore;LLVMCoroutines;LLVMipo;LLVMInstCombine;LLVMObjCARCOpts;LLVMScalarOpts;LLVMSupport;LLVMTarget;LLVMTransformUtils;LLVMVectorize;LLVMInstrumentation" +) + +# Create imported target LLVMTextAPI +add_library(LLVMTextAPI STATIC IMPORTED) + +set_target_properties(LLVMTextAPI PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMSupport;LLVMBinaryFormat" +) + +# Create imported target LLVMDlltoolDriver +add_library(LLVMDlltoolDriver STATIC IMPORTED) + +set_target_properties(LLVMDlltoolDriver PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMObject;LLVMOption;LLVMSupport" +) + +# Create imported target LLVMLibDriver +add_library(LLVMLibDriver STATIC IMPORTED) + +set_target_properties(LLVMLibDriver PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMBinaryFormat;LLVMBitReader;LLVMObject;LLVMOption;LLVMSupport;LLVMBinaryFormat;LLVMBitReader;LLVMObject;LLVMOption;LLVMSupport" +) + +# Create imported target LLVMXRay +add_library(LLVMXRay STATIC IMPORTED) + +set_target_properties(LLVMXRay PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMSupport;LLVMObject" +) + +# Create imported target LLVMWindowsDriver +add_library(LLVMWindowsDriver STATIC IMPORTED) + +set_target_properties(LLVMWindowsDriver PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMOption;LLVMSupport" +) + +# Create imported target LLVMWindowsManifest +add_library(LLVMWindowsManifest STATIC IMPORTED) + +set_target_properties(LLVMWindowsManifest PROPERTIES + INTERFACE_LINK_LIBRARIES "LibXml2::LibXml2;LLVMSupport" +) + +# Create imported target LLVMParts +add_library(LLVMParts STATIC IMPORTED) + +set_target_properties(LLVMParts PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMCore;LLVMSupport;LLVMTransformUtils;LLVMCodeGen" +) + +# Create imported target FileCheck +add_executable(FileCheck IMPORTED) + +# Create imported target llvm-PerfectShuffle +add_executable(llvm-PerfectShuffle IMPORTED) + +# Create imported target count +add_executable(count IMPORTED) + +# Create imported target not +add_executable(not IMPORTED) + +# Create imported target UnicodeNameMappingGenerator +add_executable(UnicodeNameMappingGenerator IMPORTED) + +# Create imported target yaml-bench +add_executable(yaml-bench IMPORTED) + +# Create imported target omptarget +add_library(omptarget SHARED IMPORTED) + +# Create imported target omptarget.rtl.amdgpu +add_library(omptarget.rtl.amdgpu SHARED IMPORTED) + +# Create imported target omptarget.rtl.cuda +add_library(omptarget.rtl.cuda SHARED IMPORTED) + +# Create imported target omptarget.rtl.x86_64 +add_library(omptarget.rtl.x86_64 SHARED IMPORTED) + +# Create imported target llvm-omp-device-info +add_executable(llvm-omp-device-info IMPORTED) + +# Create imported target LTO +add_library(LTO SHARED IMPORTED) + +# Create imported target LLVMgold +add_library(LLVMgold MODULE IMPORTED) + +# Create imported target llvm-ar +add_executable(llvm-ar IMPORTED) + +# Create imported target llvm-config +add_executable(llvm-config IMPORTED) + +# Create imported target llvm-lto +add_executable(llvm-lto IMPORTED) + +# Create imported target llvm-profdata +add_executable(llvm-profdata IMPORTED) + +# Create imported target bugpoint +add_executable(bugpoint IMPORTED) +set_property(TARGET bugpoint PROPERTY ENABLE_EXPORTS 1) + +# Create imported target dsymutil +add_executable(dsymutil IMPORTED) + +# Create imported target llc +add_executable(llc IMPORTED) +set_property(TARGET llc PROPERTY ENABLE_EXPORTS 1) + +# Create imported target lli-child-target +add_executable(lli-child-target IMPORTED) +set_property(TARGET lli-child-target PROPERTY ENABLE_EXPORTS 1) + +# Create imported target lli +add_executable(lli IMPORTED) +set_property(TARGET lli PROPERTY ENABLE_EXPORTS 1) + +# Create imported target llvm-as +add_executable(llvm-as IMPORTED) + +# Create imported target llvm-bcanalyzer +add_executable(llvm-bcanalyzer IMPORTED) + +# Create imported target llvm-c-test +add_executable(llvm-c-test IMPORTED) + +# Create imported target llvm-cat +add_executable(llvm-cat IMPORTED) + +# Create imported target llvm-cfi-verify +add_executable(llvm-cfi-verify IMPORTED) + +# Create imported target LLVMCFIVerify +add_library(LLVMCFIVerify STATIC IMPORTED) + +set_target_properties(LLVMCFIVerify PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMDebugInfoDWARF;LLVMMC;LLVMMCParser;LLVMObject;LLVMSupport;LLVMSymbolize" +) + +# Create imported target llvm-cov +add_executable(llvm-cov IMPORTED) + +# Create imported target llvm-cvtres +add_executable(llvm-cvtres IMPORTED) + +# Create imported target llvm-cxxdump +add_executable(llvm-cxxdump IMPORTED) + +# Create imported target llvm-cxxfilt +add_executable(llvm-cxxfilt IMPORTED) + +# Create imported target llvm-cxxmap +add_executable(llvm-cxxmap IMPORTED) + +# Create imported target llvm-debuginfod +add_executable(llvm-debuginfod IMPORTED) + +# Create imported target llvm-debuginfod-find +add_executable(llvm-debuginfod-find IMPORTED) + +# Create imported target llvm-diff +add_executable(llvm-diff IMPORTED) + +# Create imported target LLVMDiff +add_library(LLVMDiff STATIC IMPORTED) + +set_target_properties(LLVMDiff PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMCore;LLVMSupport" +) + +# Create imported target llvm-dis +add_executable(llvm-dis IMPORTED) + +# Create imported target llvm-dwarfdump +add_executable(llvm-dwarfdump IMPORTED) + +# Create imported target llvm-dwarfutil +add_executable(llvm-dwarfutil IMPORTED) + +# Create imported target llvm-dwp +add_executable(llvm-dwp IMPORTED) + +# Create imported target llvm-exegesis +add_executable(llvm-exegesis IMPORTED) + +# Create imported target LLVMExegesisX86 +add_library(LLVMExegesisX86 STATIC IMPORTED) + +set_target_properties(LLVMExegesisX86 PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMX86CodeGen;LLVMX86AsmParser;LLVMX86Desc;LLVMX86Disassembler;LLVMX86Info;LLVMExegesis;LLVMCore;LLVMSupport;LLVMCodeGen" +) + +# Create imported target LLVMExegesisAArch64 +add_library(LLVMExegesisAArch64 STATIC IMPORTED) + +set_target_properties(LLVMExegesisAArch64 PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMAArch64CodeGen;LLVMAArch64AsmParser;LLVMAArch64Desc;LLVMAArch64Disassembler;LLVMAArch64Info;LLVMAArch64Utils;LLVMExegesis;LLVMCore;LLVMSupport" +) + +# Create imported target LLVMExegesisMips +add_library(LLVMExegesisMips STATIC IMPORTED) + +set_target_properties(LLVMExegesisMips PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMMipsCodeGen;LLVMMipsAsmParser;LLVMMipsDesc;LLVMMipsDisassembler;LLVMMipsInfo;LLVMExegesis;LLVMCore;LLVMSupport" +) + +# Create imported target LLVMExegesis +add_library(LLVMExegesis STATIC IMPORTED) + +set_target_properties(LLVMExegesis PROPERTIES + INTERFACE_LINK_LIBRARIES "LLVMAnalysis;LLVMCodeGen;LLVMCore;LLVMExecutionEngine;LLVMGlobalISel;LLVMMC;LLVMMCDisassembler;LLVMMCJIT;LLVMMCParser;LLVMObject;LLVMObjectYAML;LLVMRuntimeDyld;LLVMSupport" +) + +# Create imported target llvm-extract +add_executable(llvm-extract IMPORTED) + +# Create imported target llvm-gsymutil +add_executable(llvm-gsymutil IMPORTED) + +# Create imported target llvm-ifs +add_executable(llvm-ifs IMPORTED) + +# Create imported target llvm-jitlink-executor +add_executable(llvm-jitlink-executor IMPORTED) +set_property(TARGET llvm-jitlink-executor PROPERTY ENABLE_EXPORTS 1) + +# Create imported target llvm-jitlink +add_executable(llvm-jitlink IMPORTED) +set_property(TARGET llvm-jitlink PROPERTY ENABLE_EXPORTS 1) + +# Create imported target llvm-libtool-darwin +add_executable(llvm-libtool-darwin IMPORTED) + +# Create imported target llvm-link +add_executable(llvm-link IMPORTED) + +# Create imported target llvm-lipo +add_executable(llvm-lipo IMPORTED) + +# Create imported target llvm-lto2 +add_executable(llvm-lto2 IMPORTED) +set_property(TARGET llvm-lto2 PROPERTY ENABLE_EXPORTS 1) + +# Create imported target llvm-mc +add_executable(llvm-mc IMPORTED) + +# Create imported target llvm-mca +add_executable(llvm-mca IMPORTED) + +# Create imported target llvm-ml +add_executable(llvm-ml IMPORTED) + +# Create imported target llvm-modextract +add_executable(llvm-modextract IMPORTED) + +# Create imported target llvm-mt +add_executable(llvm-mt IMPORTED) + +# Create imported target llvm-nm +add_executable(llvm-nm IMPORTED) + +# Create imported target llvm-objcopy +add_executable(llvm-objcopy IMPORTED) + +# Create imported target llvm-objdump +add_executable(llvm-objdump IMPORTED) + +# Create imported target llvm-opt-report +add_executable(llvm-opt-report IMPORTED) + +# Create imported target llvm-pdbutil +add_executable(llvm-pdbutil IMPORTED) + +# Create imported target llvm-profgen +add_executable(llvm-profgen IMPORTED) + +# Create imported target llvm-rc +add_executable(llvm-rc IMPORTED) + +# Create imported target llvm-readobj +add_executable(llvm-readobj IMPORTED) + +# Create imported target llvm-reduce +add_executable(llvm-reduce IMPORTED) + +# Create imported target llvm-remark-size-diff +add_executable(llvm-remark-size-diff IMPORTED) + +# Create imported target llvm-rtdyld +add_executable(llvm-rtdyld IMPORTED) + +# Create imported target LLVM +add_library(LLVM SHARED IMPORTED) + +# Create imported target llvm-sim +add_executable(llvm-sim IMPORTED) + +# Create imported target llvm-size +add_executable(llvm-size IMPORTED) + +# Create imported target llvm-split +add_executable(llvm-split IMPORTED) + +# Create imported target llvm-stress +add_executable(llvm-stress IMPORTED) + +# Create imported target llvm-strings +add_executable(llvm-strings IMPORTED) + +# Create imported target llvm-symbolizer +add_executable(llvm-symbolizer IMPORTED) + +# Create imported target llvm-tapi-diff +add_executable(llvm-tapi-diff IMPORTED) + +# Create imported target llvm-tli-checker +add_executable(llvm-tli-checker IMPORTED) + +# Create imported target llvm-undname +add_executable(llvm-undname IMPORTED) + +# Create imported target llvm-xray +add_executable(llvm-xray IMPORTED) + +# Create imported target obj2yaml +add_executable(obj2yaml IMPORTED) + +# Create imported target opt +add_executable(opt IMPORTED) +set_property(TARGET opt PROPERTY ENABLE_EXPORTS 1) + +# Create imported target Remarks +add_library(Remarks SHARED IMPORTED) + +# Create imported target sancov +add_executable(sancov IMPORTED) + +# Create imported target sanstats +add_executable(sanstats IMPORTED) + +# Create imported target split-file +add_executable(split-file IMPORTED) + +# Create imported target verify-uselistorder +add_executable(verify-uselistorder IMPORTED) + +# Create imported target yaml2obj +add_executable(yaml2obj IMPORTED) + +if(CMAKE_VERSION VERSION_LESS 2.8.12) + message(FATAL_ERROR "This file relies on consumers using CMake 2.8.12 or greater.") +endif() + +# Load information for each installed configuration. +file(GLOB _cmake_config_files "${CMAKE_CURRENT_LIST_DIR}/LLVMExports-*.cmake") +foreach(_cmake_config_file IN LISTS _cmake_config_files) + include("${_cmake_config_file}") +endforeach() +unset(_cmake_config_file) +unset(_cmake_config_files) + +# Cleanup temporary variables. +set(_IMPORT_PREFIX) + +# Cut checking of llvm libs for sdk-partly +# # Loop over all imported files and verify that they actually exist +# foreach(_cmake_target IN LISTS _cmake_import_check_targets) +# foreach(_cmake_file IN LISTS "_cmake_import_check_files_for_${_cmake_target}") +# if(NOT EXISTS "${_cmake_file}") +# message(FATAL_ERROR "The imported target \"${_cmake_target}\" references the file +# \"${_cmake_file}\" +# but this file does not exist. Possible reasons include: +# * The file was deleted, renamed, or moved to another location. +# * An install or uninstall procedure did not complete successfully. +# * The installation package was faulty and contained +# \"${CMAKE_CURRENT_LIST_FILE}\" +# but not all the files it references. +# ") +# endif() +# endforeach() +# unset(_cmake_file) +# unset("_cmake_import_check_files_for_${_cmake_target}") +# endforeach() +# unset(_cmake_target) +# unset(_cmake_import_check_targets) + +# This file does not depend on other imported targets which have +# been exported from the same project but in a separate export set. + +# Commands beyond this point should not need to know the version. +set(CMAKE_IMPORT_FILE_VERSION) +cmake_policy(POP) diff --git a/llvm-build/build.py b/llvm-build/build.py index fd7cddd21853..29e0b2180420 100755 --- a/llvm-build/build.py +++ b/llvm-build/build.py @@ -1285,6 +1285,18 @@ class SysrootComposer(BuildUtils): def __init__(self, build_config): super(SysrootComposer, self).__init__(build_config) + def replace_cmake_llvm_exports(self, llvm_install): + + # Skip checking of llvm libs for sdk partly build. + # Prebuilt clang doesn't have it. + llvm_exports_cmake = 'LLVMExports.cmake' + src = os.path.join(self.build_config.LLVM_BUILD_DIR, llvm_exports_cmake) + dst = os.path.join(self.build_config.REPOROOT_DIR, 'prebuilts/clang/ohos', self.use_platform(), + 'clang-%s' % self.build_config.CLANG_VERSION, "lib/cmake/llvm", llvm_exports_cmake) + if os.path.exists(os.path.join(dst, llvm_exports_cmake)): + os.remove(os.path.join(dst, llvm_exports_cmake)) + shutil.copy2(src, dst) + def setup_cmake_platform(self, llvm_install): # OHOS.cmake already exsit on cmake prebuilts, -- Gitee From 1498d07533bf306e98f44c3f891f57b645d8e786 Mon Sep 17 00:00:00 2001 From: likholatovevgeny Date: Mon, 13 May 2024 16:44:31 +0300 Subject: [PATCH 65/77] removed LLVMExports.cmake Signed-off-by: likholatovevgeny --- llvm-build/LLVMExports.cmake | 1150 ---------------------------------- llvm-build/build.py | 14 +- 2 files changed, 2 insertions(+), 1162 deletions(-) delete mode 100644 llvm-build/LLVMExports.cmake diff --git a/llvm-build/LLVMExports.cmake b/llvm-build/LLVMExports.cmake deleted file mode 100644 index b64013dc5858..000000000000 --- a/llvm-build/LLVMExports.cmake +++ /dev/null @@ -1,1150 +0,0 @@ -# Generated by CMake - -if("${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}" LESS 2.8) - message(FATAL_ERROR "CMake >= 2.8.0 required") -endif() -if(CMAKE_VERSION VERSION_LESS "2.8.3") - message(FATAL_ERROR "CMake >= 2.8.3 required") -endif() -cmake_policy(PUSH) -cmake_policy(VERSION 2.8.3...3.26) -#---------------------------------------------------------------- -# Generated CMake target import file. -#---------------------------------------------------------------- - -# Commands may need to know the format version. -set(CMAKE_IMPORT_FILE_VERSION 1) - -# Protect against multiple inclusion, which would fail when already imported targets are added once more. -set(_cmake_targets_defined "") -set(_cmake_targets_not_defined "") -set(_cmake_expected_targets "") -foreach(_cmake_expected_target IN ITEMS LLVMDemangle LLVMSupport LLVMTableGen LLVMTableGenGlobalISel llvm-tblgen LLVMCore LLVMFuzzerCLI LLVMFuzzMutate LLVMFileCheck LLVMInterfaceStub LLVMIRReader LLVMCodeGen LLVMSelectionDAG LLVMAsmPrinter LLVMMIRParser LLVMGlobalISel LLVMBinaryFormat LLVMBitReader LLVMBitWriter LLVMBitstreamReader LLVMDWARFLinker LLVMExtensions LLVMFrontendOpenACC LLVMFrontendOpenMP LLVMTransformUtils LLVMInstrumentation LLVMAggressiveInstCombine LLVMInstCombine LLVMScalarOpts LLVMipo LLVMVectorize LLVMObjCARCOpts LLVMCoroutines LLVMCFGuard LLVMLinker LLVMAnalysis LLVMLTO LLVMMC LLVMMCParser LLVMMCDisassembler LLVMMCA LLVMObjCopy LLVMObject LLVMObjectYAML LLVMOption LLVMRemarks LLVMDebuginfod LLVMDebugInfoDWARF LLVMDebugInfoGSYM LLVMDebugInfoMSF LLVMDebugInfoCodeView LLVMDebugInfoPDB LLVMSymbolize LLVMDWP LLVMExecutionEngine LLVMInterpreter LLVMJITLink LLVMMCJIT LLVMOrcJIT LLVMOrcShared LLVMOrcTargetProcess LLVMRuntimeDyld LLVMTarget LLVMAArch64CodeGen LLVMAArch64AsmParser LLVMAArch64Disassembler LLVMAArch64Desc LLVMAArch64Info LLVMAArch64Utils LLVMARMCodeGen LLVMARMAsmParser LLVMARMDisassembler LLVMARMDesc LLVMARMInfo LLVMARMUtils LLVMBPFCodeGen LLVMBPFAsmParser LLVMBPFDisassembler LLVMBPFDesc LLVMBPFInfo LLVMMipsCodeGen LLVMMipsAsmParser LLVMMipsDisassembler LLVMMipsDesc LLVMMipsInfo LLVMRISCVCodeGen LLVMRISCVAsmParser LLVMRISCVDisassembler LLVMRISCVDesc LLVMRISCVInfo LLVMX86CodeGen LLVMX86AsmParser LLVMX86Disassembler LLVMX86TargetMCA LLVMX86Desc LLVMX86Info LLVMAsmParser LLVMLineEditor LLVMProfileData LLVMCoverage LLVMPasses LLVMTextAPI LLVMDlltoolDriver LLVMLibDriver LLVMXRay LLVMWindowsDriver LLVMWindowsManifest LLVMParts FileCheck llvm-PerfectShuffle count not UnicodeNameMappingGenerator yaml-bench omptarget omptarget.rtl.amdgpu omptarget.rtl.cuda omptarget.rtl.x86_64 llvm-omp-device-info LTO LLVMgold llvm-ar llvm-config llvm-lto llvm-profdata bugpoint dsymutil llc lli-child-target lli llvm-as llvm-bcanalyzer llvm-c-test llvm-cat llvm-cfi-verify LLVMCFIVerify llvm-cov llvm-cvtres llvm-cxxdump llvm-cxxfilt llvm-cxxmap llvm-debuginfod llvm-debuginfod-find llvm-diff LLVMDiff llvm-dis llvm-dwarfdump llvm-dwarfutil llvm-dwp llvm-exegesis LLVMExegesisX86 LLVMExegesisAArch64 LLVMExegesisMips LLVMExegesis llvm-extract llvm-gsymutil llvm-ifs llvm-jitlink-executor llvm-jitlink llvm-libtool-darwin llvm-link llvm-lipo llvm-lto2 llvm-mc llvm-mca llvm-ml llvm-modextract llvm-mt llvm-nm llvm-objcopy llvm-objdump llvm-opt-report llvm-pdbutil llvm-profgen llvm-rc llvm-readobj llvm-reduce llvm-remark-size-diff llvm-rtdyld LLVM llvm-sim llvm-size llvm-split llvm-stress llvm-strings llvm-symbolizer llvm-tapi-diff llvm-tli-checker llvm-undname llvm-xray obj2yaml opt Remarks sancov sanstats split-file verify-uselistorder yaml2obj) - list(APPEND _cmake_expected_targets "${_cmake_expected_target}") - if(TARGET "${_cmake_expected_target}") - list(APPEND _cmake_targets_defined "${_cmake_expected_target}") - else() - list(APPEND _cmake_targets_not_defined "${_cmake_expected_target}") - endif() -endforeach() -unset(_cmake_expected_target) -if(_cmake_targets_defined STREQUAL _cmake_expected_targets) - unset(_cmake_targets_defined) - unset(_cmake_targets_not_defined) - unset(_cmake_expected_targets) - unset(CMAKE_IMPORT_FILE_VERSION) - cmake_policy(POP) - return() -endif() -if(NOT _cmake_targets_defined STREQUAL "") - string(REPLACE ";" ", " _cmake_targets_defined_text "${_cmake_targets_defined}") - string(REPLACE ";" ", " _cmake_targets_not_defined_text "${_cmake_targets_not_defined}") - message(FATAL_ERROR "Some (but not all) targets in this export set were already defined.\nTargets Defined: ${_cmake_targets_defined_text}\nTargets not yet defined: ${_cmake_targets_not_defined_text}\n") -endif() -unset(_cmake_targets_defined) -unset(_cmake_targets_not_defined) -unset(_cmake_expected_targets) - - -# Compute the installation prefix relative to this file. -get_filename_component(_IMPORT_PREFIX "${CMAKE_CURRENT_LIST_FILE}" PATH) -get_filename_component(_IMPORT_PREFIX "${_IMPORT_PREFIX}" PATH) -get_filename_component(_IMPORT_PREFIX "${_IMPORT_PREFIX}" PATH) -get_filename_component(_IMPORT_PREFIX "${_IMPORT_PREFIX}" PATH) -if(_IMPORT_PREFIX STREQUAL "/") - set(_IMPORT_PREFIX "") -endif() - -# Create imported target LLVMDemangle -add_library(LLVMDemangle STATIC IMPORTED) - -# Create imported target LLVMSupport -add_library(LLVMSupport STATIC IMPORTED) - -set_target_properties(LLVMSupport PROPERTIES - INTERFACE_LINK_LIBRARIES "rt;dl;-lpthread;m;ZLIB::ZLIB;LLVMDemangle" -) - -# Create imported target LLVMTableGen -add_library(LLVMTableGen STATIC IMPORTED) - -set_target_properties(LLVMTableGen PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMSupport" -) - -# Create imported target LLVMTableGenGlobalISel -add_library(LLVMTableGenGlobalISel STATIC IMPORTED) - -set_target_properties(LLVMTableGenGlobalISel PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMSupport;LLVMTableGen" -) - -# Create imported target llvm-tblgen -add_executable(llvm-tblgen IMPORTED) - -# Create imported target LLVMCore -add_library(LLVMCore STATIC IMPORTED) - -set_target_properties(LLVMCore PROPERTIES - INTERFACE_LINK_LIBRARIES "-lpthread;LLVMBinaryFormat;LLVMRemarks;LLVMSupport" -) - -# Create imported target LLVMFuzzerCLI -add_library(LLVMFuzzerCLI STATIC IMPORTED) - -set_target_properties(LLVMFuzzerCLI PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMSupport" -) - -# Create imported target LLVMFuzzMutate -add_library(LLVMFuzzMutate STATIC IMPORTED) - -set_target_properties(LLVMFuzzMutate PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMAnalysis;LLVMBitReader;LLVMBitWriter;LLVMCore;LLVMScalarOpts;LLVMSupport;LLVMTarget" -) - -# Create imported target LLVMFileCheck -add_library(LLVMFileCheck STATIC IMPORTED) - -set_target_properties(LLVMFileCheck PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMSupport" -) - -# Create imported target LLVMInterfaceStub -add_library(LLVMInterfaceStub STATIC IMPORTED) - -set_target_properties(LLVMInterfaceStub PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMBinaryFormat;LLVMMC;LLVMObject;LLVMSupport" -) - -# Create imported target LLVMIRReader -add_library(LLVMIRReader STATIC IMPORTED) - -set_target_properties(LLVMIRReader PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMAsmParser;LLVMBitReader;LLVMCore;LLVMSupport" -) - -# Create imported target LLVMCodeGen -add_library(LLVMCodeGen STATIC IMPORTED) - -set_target_properties(LLVMCodeGen PROPERTIES - INTERFACE_LINK_LIBRARIES "-lpthread;LLVMAnalysis;LLVMBitReader;LLVMBitWriter;LLVMCore;LLVMMC;LLVMProfileData;LLVMScalarOpts;LLVMSupport;LLVMTarget;LLVMTransformUtils" -) - -# Create imported target LLVMSelectionDAG -add_library(LLVMSelectionDAG STATIC IMPORTED) - -set_target_properties(LLVMSelectionDAG PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMAnalysis;LLVMCodeGen;LLVMCore;LLVMMC;LLVMSupport;LLVMTarget;LLVMTransformUtils" -) - -# Create imported target LLVMAsmPrinter -add_library(LLVMAsmPrinter STATIC IMPORTED) - -set_target_properties(LLVMAsmPrinter PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMAnalysis;LLVMBinaryFormat;LLVMCodeGen;LLVMCore;LLVMDebugInfoCodeView;LLVMDebugInfoDWARF;LLVMDebugInfoMSF;LLVMMC;LLVMMCParser;LLVMRemarks;LLVMSupport;LLVMTarget" -) - -# Create imported target LLVMMIRParser -add_library(LLVMMIRParser STATIC IMPORTED) - -set_target_properties(LLVMMIRParser PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMAsmParser;LLVMBinaryFormat;LLVMCodeGen;LLVMCore;LLVMMC;LLVMSupport;LLVMTarget" -) - -# Create imported target LLVMGlobalISel -add_library(LLVMGlobalISel STATIC IMPORTED) - -set_target_properties(LLVMGlobalISel PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMAnalysis;LLVMCodeGen;LLVMCore;LLVMMC;LLVMSelectionDAG;LLVMSupport;LLVMTarget;LLVMTransformUtils" -) - -# Create imported target LLVMBinaryFormat -add_library(LLVMBinaryFormat STATIC IMPORTED) - -set_target_properties(LLVMBinaryFormat PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMSupport" -) - -# Create imported target LLVMBitReader -add_library(LLVMBitReader STATIC IMPORTED) - -set_target_properties(LLVMBitReader PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMBitstreamReader;LLVMCore;LLVMSupport" -) - -# Create imported target LLVMBitWriter -add_library(LLVMBitWriter STATIC IMPORTED) - -set_target_properties(LLVMBitWriter PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMAnalysis;LLVMCore;LLVMMC;LLVMObject;LLVMSupport" -) - -# Create imported target LLVMBitstreamReader -add_library(LLVMBitstreamReader STATIC IMPORTED) - -set_target_properties(LLVMBitstreamReader PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMSupport" -) - -# Create imported target LLVMDWARFLinker -add_library(LLVMDWARFLinker STATIC IMPORTED) - -set_target_properties(LLVMDWARFLinker PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMBinaryFormat;LLVMDebugInfoDWARF;LLVMAsmPrinter;LLVMCodeGen;LLVMMC;LLVMObject;LLVMSupport" -) - -# Create imported target LLVMExtensions -add_library(LLVMExtensions STATIC IMPORTED) - -set_target_properties(LLVMExtensions PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMSupport" -) - -# Create imported target LLVMFrontendOpenACC -add_library(LLVMFrontendOpenACC STATIC IMPORTED) - -set_target_properties(LLVMFrontendOpenACC PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMSupport" -) - -# Create imported target LLVMFrontendOpenMP -add_library(LLVMFrontendOpenMP STATIC IMPORTED) - -set_target_properties(LLVMFrontendOpenMP PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMCore;LLVMSupport;LLVMTransformUtils;LLVMAnalysis;LLVMMC;LLVMScalarOpts" -) - -# Create imported target LLVMTransformUtils -add_library(LLVMTransformUtils STATIC IMPORTED) - -set_target_properties(LLVMTransformUtils PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMAnalysis;LLVMCore;LLVMSupport" -) - -# Create imported target LLVMInstrumentation -add_library(LLVMInstrumentation STATIC IMPORTED) - -set_target_properties(LLVMInstrumentation PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMAnalysis;LLVMCore;LLVMDemangle;LLVMMC;LLVMSupport;LLVMTransformUtils;LLVMProfileData" -) - -# Create imported target LLVMAggressiveInstCombine -add_library(LLVMAggressiveInstCombine STATIC IMPORTED) - -set_target_properties(LLVMAggressiveInstCombine PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMAnalysis;LLVMCore;LLVMSupport;LLVMTransformUtils" -) - -# Create imported target LLVMInstCombine -add_library(LLVMInstCombine STATIC IMPORTED) - -set_target_properties(LLVMInstCombine PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMAnalysis;LLVMCore;LLVMSupport;LLVMTransformUtils" -) - -# Create imported target LLVMScalarOpts -add_library(LLVMScalarOpts STATIC IMPORTED) - -set_target_properties(LLVMScalarOpts PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMAggressiveInstCombine;LLVMAnalysis;LLVMCore;LLVMInstCombine;LLVMSupport;LLVMTransformUtils" -) - -# Create imported target LLVMipo -add_library(LLVMipo STATIC IMPORTED) - -set_target_properties(LLVMipo PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMAggressiveInstCombine;LLVMAnalysis;LLVMBitReader;LLVMBitWriter;LLVMCore;LLVMFrontendOpenMP;LLVMInstCombine;LLVMIRReader;LLVMLinker;LLVMObject;LLVMProfileData;LLVMScalarOpts;LLVMSupport;LLVMTransformUtils;LLVMVectorize;LLVMInstrumentation;LLVMScalarOpts" -) - -# Create imported target LLVMVectorize -add_library(LLVMVectorize STATIC IMPORTED) - -set_target_properties(LLVMVectorize PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMAnalysis;LLVMCore;LLVMSupport;LLVMTransformUtils" -) - -# Create imported target LLVMObjCARCOpts -add_library(LLVMObjCARCOpts STATIC IMPORTED) - -set_target_properties(LLVMObjCARCOpts PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMAnalysis;LLVMCore;LLVMSupport;LLVMTransformUtils" -) - -# Create imported target LLVMCoroutines -add_library(LLVMCoroutines STATIC IMPORTED) - -set_target_properties(LLVMCoroutines PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMAnalysis;LLVMCore;LLVMipo;LLVMScalarOpts;LLVMSupport;LLVMTransformUtils" -) - -# Create imported target LLVMCFGuard -add_library(LLVMCFGuard STATIC IMPORTED) - -set_target_properties(LLVMCFGuard PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMCore;LLVMSupport" -) - -# Create imported target LLVMLinker -add_library(LLVMLinker STATIC IMPORTED) - -set_target_properties(LLVMLinker PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMCore;LLVMObject;LLVMSupport;LLVMTransformUtils" -) - -# Create imported target LLVMAnalysis -add_library(LLVMAnalysis STATIC IMPORTED) - -set_target_properties(LLVMAnalysis PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMBinaryFormat;LLVMCore;LLVMObject;LLVMProfileData;LLVMSupport" -) - -# Create imported target LLVMLTO -add_library(LLVMLTO STATIC IMPORTED) - -set_target_properties(LLVMLTO PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMAggressiveInstCombine;LLVMAnalysis;LLVMBinaryFormat;LLVMBitReader;LLVMBitWriter;LLVMCodeGen;LLVMCore;LLVMExtensions;LLVMipo;LLVMInstCombine;LLVMInstrumentation;LLVMLinker;LLVMMC;LLVMObjCARCOpts;LLVMObject;LLVMPasses;LLVMRemarks;LLVMScalarOpts;LLVMSupport;LLVMTarget;LLVMTransformUtils" -) - -# Create imported target LLVMMC -add_library(LLVMMC STATIC IMPORTED) - -set_target_properties(LLVMMC PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMSupport;LLVMBinaryFormat;LLVMDebugInfoCodeView" -) - -# Create imported target LLVMMCParser -add_library(LLVMMCParser STATIC IMPORTED) - -set_target_properties(LLVMMCParser PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMMC;LLVMSupport" -) - -# Create imported target LLVMMCDisassembler -add_library(LLVMMCDisassembler STATIC IMPORTED) - -set_target_properties(LLVMMCDisassembler PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMMC;LLVMSupport" -) - -# Create imported target LLVMMCA -add_library(LLVMMCA STATIC IMPORTED) - -set_target_properties(LLVMMCA PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMMC;LLVMSupport" -) - -# Create imported target LLVMObjCopy -add_library(LLVMObjCopy STATIC IMPORTED) - -set_target_properties(LLVMObjCopy PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMBinaryFormat;LLVMObject;LLVMSupport;LLVMMC" -) - -# Create imported target LLVMObject -add_library(LLVMObject STATIC IMPORTED) - -set_target_properties(LLVMObject PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMBitReader;LLVMCore;LLVMMC;LLVMBinaryFormat;LLVMMCParser;LLVMSupport;LLVMTextAPI" -) - -# Create imported target LLVMObjectYAML -add_library(LLVMObjectYAML STATIC IMPORTED) - -set_target_properties(LLVMObjectYAML PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMBinaryFormat;LLVMObject;LLVMSupport;LLVMDebugInfoCodeView;LLVMMC" -) - -# Create imported target LLVMOption -add_library(LLVMOption STATIC IMPORTED) - -set_target_properties(LLVMOption PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMSupport" -) - -# Create imported target LLVMRemarks -add_library(LLVMRemarks STATIC IMPORTED) - -set_target_properties(LLVMRemarks PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMBitstreamReader;LLVMSupport" -) - -# Create imported target LLVMDebuginfod -add_library(LLVMDebuginfod STATIC IMPORTED) - -set_target_properties(LLVMDebuginfod PROPERTIES - INTERFACE_LINK_LIBRARIES "-lpthread;LLVMSupport;LLVMSymbolize;LLVMDebugInfoDWARF;LLVMBinaryFormat;LLVMObject" -) - -# Create imported target LLVMDebugInfoDWARF -add_library(LLVMDebugInfoDWARF STATIC IMPORTED) - -set_target_properties(LLVMDebugInfoDWARF PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMBinaryFormat;LLVMObject;LLVMMC;LLVMSupport" -) - -# Create imported target LLVMDebugInfoGSYM -add_library(LLVMDebugInfoGSYM STATIC IMPORTED) - -set_target_properties(LLVMDebugInfoGSYM PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMMC;LLVMObject;LLVMSupport;LLVMDebugInfoDWARF" -) - -# Create imported target LLVMDebugInfoMSF -add_library(LLVMDebugInfoMSF STATIC IMPORTED) - -set_target_properties(LLVMDebugInfoMSF PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMSupport" -) - -# Create imported target LLVMDebugInfoCodeView -add_library(LLVMDebugInfoCodeView STATIC IMPORTED) - -set_target_properties(LLVMDebugInfoCodeView PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMSupport" -) - -# Create imported target LLVMDebugInfoPDB -add_library(LLVMDebugInfoPDB STATIC IMPORTED) - -set_target_properties(LLVMDebugInfoPDB PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMBinaryFormat;LLVMObject;LLVMSupport;LLVMDebugInfoCodeView;LLVMDebugInfoMSF" -) - -# Create imported target LLVMSymbolize -add_library(LLVMSymbolize STATIC IMPORTED) - -set_target_properties(LLVMSymbolize PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMDebugInfoDWARF;LLVMDebugInfoPDB;LLVMObject;LLVMSupport;LLVMDemangle" -) - -# Create imported target LLVMDWP -add_library(LLVMDWP STATIC IMPORTED) - -set_target_properties(LLVMDWP PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMDebugInfoDWARF;LLVMMC;LLVMObject;LLVMSupport;LLVMTarget" -) - -# Create imported target LLVMExecutionEngine -add_library(LLVMExecutionEngine STATIC IMPORTED) - -set_target_properties(LLVMExecutionEngine PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMCore;LLVMMC;LLVMObject;LLVMOrcTargetProcess;LLVMRuntimeDyld;LLVMSupport;LLVMTarget" -) - -# Create imported target LLVMInterpreter -add_library(LLVMInterpreter STATIC IMPORTED) - -set_target_properties(LLVMInterpreter PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMCodeGen;LLVMCore;LLVMExecutionEngine;LLVMSupport" -) - -# Create imported target LLVMJITLink -add_library(LLVMJITLink STATIC IMPORTED) - -set_target_properties(LLVMJITLink PROPERTIES - INTERFACE_LINK_LIBRARIES "\$;\$;\$;\$;LLVMBinaryFormat;LLVMObject;LLVMOrcTargetProcess;LLVMSupport" -) - -# Create imported target LLVMMCJIT -add_library(LLVMMCJIT STATIC IMPORTED) - -set_target_properties(LLVMMCJIT PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMCore;LLVMExecutionEngine;LLVMObject;LLVMRuntimeDyld;LLVMSupport;LLVMTarget" -) - -# Create imported target LLVMOrcJIT -add_library(LLVMOrcJIT STATIC IMPORTED) - -set_target_properties(LLVMOrcJIT PROPERTIES - INTERFACE_LINK_LIBRARIES "-lpthread;rt;\$;\$;\$;\$;LLVMCore;LLVMExecutionEngine;LLVMJITLink;LLVMObject;LLVMOrcShared;LLVMOrcTargetProcess;LLVMMC;LLVMMCDisassembler;LLVMPasses;LLVMRuntimeDyld;LLVMSupport;LLVMTarget;LLVMTransformUtils" -) - -# Create imported target LLVMOrcShared -add_library(LLVMOrcShared STATIC IMPORTED) - -set_target_properties(LLVMOrcShared PROPERTIES - INTERFACE_LINK_LIBRARIES "-lpthread;LLVMSupport" -) - -# Create imported target LLVMOrcTargetProcess -add_library(LLVMOrcTargetProcess STATIC IMPORTED) - -set_target_properties(LLVMOrcTargetProcess PROPERTIES - INTERFACE_LINK_LIBRARIES "-lpthread;rt;LLVMOrcShared;LLVMSupport" -) - -# Create imported target LLVMRuntimeDyld -add_library(LLVMRuntimeDyld STATIC IMPORTED) - -set_target_properties(LLVMRuntimeDyld PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMCore;LLVMMC;LLVMObject;LLVMSupport" -) - -# Create imported target LLVMTarget -add_library(LLVMTarget STATIC IMPORTED) - -set_target_properties(LLVMTarget PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMAnalysis;LLVMCore;LLVMMC;LLVMSupport" -) - -# Create imported target LLVMAArch64CodeGen -add_library(LLVMAArch64CodeGen STATIC IMPORTED) - -set_target_properties(LLVMAArch64CodeGen PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMParts;LLVMAArch64Desc;LLVMAArch64Info;LLVMAArch64Utils;LLVMAnalysis;LLVMAsmPrinter;LLVMCodeGen;LLVMCore;LLVMMC;LLVMScalarOpts;LLVMSelectionDAG;LLVMSupport;LLVMTarget;LLVMTransformUtils;LLVMGlobalISel;LLVMCFGuard" -) - -# Create imported target LLVMAArch64AsmParser -add_library(LLVMAArch64AsmParser STATIC IMPORTED) - -set_target_properties(LLVMAArch64AsmParser PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMAArch64Desc;LLVMAArch64Info;LLVMAArch64Utils;LLVMMC;LLVMMCParser;LLVMSupport" -) - -# Create imported target LLVMAArch64Disassembler -add_library(LLVMAArch64Disassembler STATIC IMPORTED) - -set_target_properties(LLVMAArch64Disassembler PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMAArch64Desc;LLVMAArch64Info;LLVMAArch64Utils;LLVMMC;LLVMMCDisassembler;LLVMSupport" -) - -# Create imported target LLVMAArch64Desc -add_library(LLVMAArch64Desc STATIC IMPORTED) - -set_target_properties(LLVMAArch64Desc PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMAArch64Info;LLVMAArch64Utils;LLVMMC;LLVMBinaryFormat;LLVMSupport" -) - -# Create imported target LLVMAArch64Info -add_library(LLVMAArch64Info STATIC IMPORTED) - -set_target_properties(LLVMAArch64Info PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMMC;LLVMSupport" -) - -# Create imported target LLVMAArch64Utils -add_library(LLVMAArch64Utils STATIC IMPORTED) - -set_target_properties(LLVMAArch64Utils PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMSupport" -) - -# Create imported target LLVMARMCodeGen -add_library(LLVMARMCodeGen STATIC IMPORTED) - -set_target_properties(LLVMARMCodeGen PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMARMDesc;LLVMARMInfo;LLVMAnalysis;LLVMAsmPrinter;LLVMCodeGen;LLVMCore;LLVMipo;LLVMMC;LLVMScalarOpts;LLVMSelectionDAG;LLVMSupport;LLVMTarget;LLVMGlobalISel;LLVMARMUtils;LLVMTransformUtils;LLVMCFGuard" -) - -# Create imported target LLVMARMAsmParser -add_library(LLVMARMAsmParser STATIC IMPORTED) - -set_target_properties(LLVMARMAsmParser PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMARMDesc;LLVMARMInfo;LLVMMC;LLVMMCParser;LLVMSupport;LLVMARMUtils" -) - -# Create imported target LLVMARMDisassembler -add_library(LLVMARMDisassembler STATIC IMPORTED) - -set_target_properties(LLVMARMDisassembler PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMARMDesc;LLVMARMInfo;LLVMMCDisassembler;LLVMSupport;LLVMARMUtils" -) - -# Create imported target LLVMARMDesc -add_library(LLVMARMDesc STATIC IMPORTED) - -set_target_properties(LLVMARMDesc PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMARMInfo;LLVMARMUtils;LLVMMC;LLVMMCDisassembler;LLVMSupport;LLVMBinaryFormat" -) - -# Create imported target LLVMARMInfo -add_library(LLVMARMInfo STATIC IMPORTED) - -set_target_properties(LLVMARMInfo PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMMC;LLVMSupport" -) - -# Create imported target LLVMARMUtils -add_library(LLVMARMUtils STATIC IMPORTED) - -set_target_properties(LLVMARMUtils PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMSupport" -) - -# Create imported target LLVMBPFCodeGen -add_library(LLVMBPFCodeGen STATIC IMPORTED) - -set_target_properties(LLVMBPFCodeGen PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMAnalysis;LLVMAsmPrinter;LLVMCodeGen;LLVMCore;LLVMMC;LLVMBPFDesc;LLVMBPFInfo;LLVMipo;LLVMScalarOpts;LLVMSelectionDAG;LLVMSupport;LLVMTarget;LLVMTransformUtils" -) - -# Create imported target LLVMBPFAsmParser -add_library(LLVMBPFAsmParser STATIC IMPORTED) - -set_target_properties(LLVMBPFAsmParser PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMMC;LLVMMCParser;LLVMBPFDesc;LLVMBPFInfo;LLVMSupport" -) - -# Create imported target LLVMBPFDisassembler -add_library(LLVMBPFDisassembler STATIC IMPORTED) - -set_target_properties(LLVMBPFDisassembler PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMMCDisassembler;LLVMBPFInfo;LLVMSupport" -) - -# Create imported target LLVMBPFDesc -add_library(LLVMBPFDesc STATIC IMPORTED) - -set_target_properties(LLVMBPFDesc PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMMC;LLVMBPFInfo;LLVMSupport" -) - -# Create imported target LLVMBPFInfo -add_library(LLVMBPFInfo STATIC IMPORTED) - -set_target_properties(LLVMBPFInfo PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMMC;LLVMSupport" -) - -# Create imported target LLVMMipsCodeGen -add_library(LLVMMipsCodeGen STATIC IMPORTED) - -set_target_properties(LLVMMipsCodeGen PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMAnalysis;LLVMAsmPrinter;LLVMCodeGen;LLVMCore;LLVMMC;LLVMMipsDesc;LLVMMipsInfo;LLVMSelectionDAG;LLVMSupport;LLVMTarget;LLVMGlobalISel" -) - -# Create imported target LLVMMipsAsmParser -add_library(LLVMMipsAsmParser STATIC IMPORTED) - -set_target_properties(LLVMMipsAsmParser PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMMC;LLVMMCParser;LLVMMipsDesc;LLVMMipsInfo;LLVMSupport" -) - -# Create imported target LLVMMipsDisassembler -add_library(LLVMMipsDisassembler STATIC IMPORTED) - -set_target_properties(LLVMMipsDisassembler PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMMCDisassembler;LLVMMipsInfo;LLVMSupport" -) - -# Create imported target LLVMMipsDesc -add_library(LLVMMipsDesc STATIC IMPORTED) - -set_target_properties(LLVMMipsDesc PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMMC;LLVMMipsInfo;LLVMSupport" -) - -# Create imported target LLVMMipsInfo -add_library(LLVMMipsInfo STATIC IMPORTED) - -set_target_properties(LLVMMipsInfo PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMMC;LLVMSupport" -) - -# Create imported target LLVMRISCVCodeGen -add_library(LLVMRISCVCodeGen STATIC IMPORTED) - -set_target_properties(LLVMRISCVCodeGen PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMAnalysis;LLVMAsmPrinter;LLVMCore;LLVMipo;LLVMCodeGen;LLVMMC;LLVMRISCVDesc;LLVMRISCVInfo;LLVMSelectionDAG;LLVMSupport;LLVMTarget;LLVMTransformUtils;LLVMGlobalISel" -) - -# Create imported target LLVMRISCVAsmParser -add_library(LLVMRISCVAsmParser STATIC IMPORTED) - -set_target_properties(LLVMRISCVAsmParser PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMMC;LLVMMCParser;LLVMRISCVDesc;LLVMRISCVInfo;LLVMSupport" -) - -# Create imported target LLVMRISCVDisassembler -add_library(LLVMRISCVDisassembler STATIC IMPORTED) - -set_target_properties(LLVMRISCVDisassembler PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMMC;LLVMMCDisassembler;LLVMRISCVDesc;LLVMRISCVInfo;LLVMSupport" -) - -# Create imported target LLVMRISCVDesc -add_library(LLVMRISCVDesc STATIC IMPORTED) - -set_target_properties(LLVMRISCVDesc PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMMC;LLVMRISCVInfo;LLVMSupport" -) - -# Create imported target LLVMRISCVInfo -add_library(LLVMRISCVInfo STATIC IMPORTED) - -set_target_properties(LLVMRISCVInfo PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMMC;LLVMSupport" -) - -# Create imported target LLVMX86CodeGen -add_library(LLVMX86CodeGen STATIC IMPORTED) - -set_target_properties(LLVMX86CodeGen PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMAnalysis;LLVMAsmPrinter;LLVMCodeGen;LLVMCore;LLVMInstrumentation;LLVMMC;LLVMSelectionDAG;LLVMSupport;LLVMTarget;LLVMTransformUtils;LLVMX86Desc;LLVMX86Info;LLVMGlobalISel;LLVMProfileData;LLVMCFGuard" -) - -# Create imported target LLVMX86AsmParser -add_library(LLVMX86AsmParser STATIC IMPORTED) - -set_target_properties(LLVMX86AsmParser PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMMC;LLVMMCParser;LLVMSupport;LLVMX86Desc;LLVMX86Info" -) - -# Create imported target LLVMX86Disassembler -add_library(LLVMX86Disassembler STATIC IMPORTED) - -set_target_properties(LLVMX86Disassembler PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMMCDisassembler;LLVMSupport;LLVMX86Info" -) - -# Create imported target LLVMX86TargetMCA -add_library(LLVMX86TargetMCA STATIC IMPORTED) - -set_target_properties(LLVMX86TargetMCA PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMMC;LLVMMCParser;LLVMX86Desc;LLVMX86Info;LLVMSupport;LLVMMCA" -) - -# Create imported target LLVMX86Desc -add_library(LLVMX86Desc STATIC IMPORTED) - -set_target_properties(LLVMX86Desc PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMMC;LLVMMCDisassembler;LLVMSupport;LLVMX86Info;LLVMBinaryFormat" -) - -# Create imported target LLVMX86Info -add_library(LLVMX86Info STATIC IMPORTED) - -set_target_properties(LLVMX86Info PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMMC;LLVMSupport" -) - -# Create imported target LLVMAsmParser -add_library(LLVMAsmParser STATIC IMPORTED) - -set_target_properties(LLVMAsmParser PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMBinaryFormat;LLVMCore;LLVMSupport" -) - -# Create imported target LLVMLineEditor -add_library(LLVMLineEditor STATIC IMPORTED) - -set_target_properties(LLVMLineEditor PROPERTIES - INTERFACE_LINK_LIBRARIES "LibEdit::LibEdit;LLVMSupport" -) - -# Create imported target LLVMProfileData -add_library(LLVMProfileData STATIC IMPORTED) - -set_target_properties(LLVMProfileData PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMCore;LLVMObject;LLVMSupport;LLVMDemangle;LLVMSymbolize;LLVMDebugInfoDWARF" -) - -# Create imported target LLVMCoverage -add_library(LLVMCoverage STATIC IMPORTED) - -set_target_properties(LLVMCoverage PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMCore;LLVMObject;LLVMProfileData;LLVMSupport" -) - -# Create imported target LLVMPasses -add_library(LLVMPasses STATIC IMPORTED) - -set_target_properties(LLVMPasses PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMAggressiveInstCombine;LLVMAnalysis;LLVMCore;LLVMCoroutines;LLVMipo;LLVMInstCombine;LLVMObjCARCOpts;LLVMScalarOpts;LLVMSupport;LLVMTarget;LLVMTransformUtils;LLVMVectorize;LLVMInstrumentation" -) - -# Create imported target LLVMTextAPI -add_library(LLVMTextAPI STATIC IMPORTED) - -set_target_properties(LLVMTextAPI PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMSupport;LLVMBinaryFormat" -) - -# Create imported target LLVMDlltoolDriver -add_library(LLVMDlltoolDriver STATIC IMPORTED) - -set_target_properties(LLVMDlltoolDriver PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMObject;LLVMOption;LLVMSupport" -) - -# Create imported target LLVMLibDriver -add_library(LLVMLibDriver STATIC IMPORTED) - -set_target_properties(LLVMLibDriver PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMBinaryFormat;LLVMBitReader;LLVMObject;LLVMOption;LLVMSupport;LLVMBinaryFormat;LLVMBitReader;LLVMObject;LLVMOption;LLVMSupport" -) - -# Create imported target LLVMXRay -add_library(LLVMXRay STATIC IMPORTED) - -set_target_properties(LLVMXRay PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMSupport;LLVMObject" -) - -# Create imported target LLVMWindowsDriver -add_library(LLVMWindowsDriver STATIC IMPORTED) - -set_target_properties(LLVMWindowsDriver PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMOption;LLVMSupport" -) - -# Create imported target LLVMWindowsManifest -add_library(LLVMWindowsManifest STATIC IMPORTED) - -set_target_properties(LLVMWindowsManifest PROPERTIES - INTERFACE_LINK_LIBRARIES "LibXml2::LibXml2;LLVMSupport" -) - -# Create imported target LLVMParts -add_library(LLVMParts STATIC IMPORTED) - -set_target_properties(LLVMParts PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMCore;LLVMSupport;LLVMTransformUtils;LLVMCodeGen" -) - -# Create imported target FileCheck -add_executable(FileCheck IMPORTED) - -# Create imported target llvm-PerfectShuffle -add_executable(llvm-PerfectShuffle IMPORTED) - -# Create imported target count -add_executable(count IMPORTED) - -# Create imported target not -add_executable(not IMPORTED) - -# Create imported target UnicodeNameMappingGenerator -add_executable(UnicodeNameMappingGenerator IMPORTED) - -# Create imported target yaml-bench -add_executable(yaml-bench IMPORTED) - -# Create imported target omptarget -add_library(omptarget SHARED IMPORTED) - -# Create imported target omptarget.rtl.amdgpu -add_library(omptarget.rtl.amdgpu SHARED IMPORTED) - -# Create imported target omptarget.rtl.cuda -add_library(omptarget.rtl.cuda SHARED IMPORTED) - -# Create imported target omptarget.rtl.x86_64 -add_library(omptarget.rtl.x86_64 SHARED IMPORTED) - -# Create imported target llvm-omp-device-info -add_executable(llvm-omp-device-info IMPORTED) - -# Create imported target LTO -add_library(LTO SHARED IMPORTED) - -# Create imported target LLVMgold -add_library(LLVMgold MODULE IMPORTED) - -# Create imported target llvm-ar -add_executable(llvm-ar IMPORTED) - -# Create imported target llvm-config -add_executable(llvm-config IMPORTED) - -# Create imported target llvm-lto -add_executable(llvm-lto IMPORTED) - -# Create imported target llvm-profdata -add_executable(llvm-profdata IMPORTED) - -# Create imported target bugpoint -add_executable(bugpoint IMPORTED) -set_property(TARGET bugpoint PROPERTY ENABLE_EXPORTS 1) - -# Create imported target dsymutil -add_executable(dsymutil IMPORTED) - -# Create imported target llc -add_executable(llc IMPORTED) -set_property(TARGET llc PROPERTY ENABLE_EXPORTS 1) - -# Create imported target lli-child-target -add_executable(lli-child-target IMPORTED) -set_property(TARGET lli-child-target PROPERTY ENABLE_EXPORTS 1) - -# Create imported target lli -add_executable(lli IMPORTED) -set_property(TARGET lli PROPERTY ENABLE_EXPORTS 1) - -# Create imported target llvm-as -add_executable(llvm-as IMPORTED) - -# Create imported target llvm-bcanalyzer -add_executable(llvm-bcanalyzer IMPORTED) - -# Create imported target llvm-c-test -add_executable(llvm-c-test IMPORTED) - -# Create imported target llvm-cat -add_executable(llvm-cat IMPORTED) - -# Create imported target llvm-cfi-verify -add_executable(llvm-cfi-verify IMPORTED) - -# Create imported target LLVMCFIVerify -add_library(LLVMCFIVerify STATIC IMPORTED) - -set_target_properties(LLVMCFIVerify PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMDebugInfoDWARF;LLVMMC;LLVMMCParser;LLVMObject;LLVMSupport;LLVMSymbolize" -) - -# Create imported target llvm-cov -add_executable(llvm-cov IMPORTED) - -# Create imported target llvm-cvtres -add_executable(llvm-cvtres IMPORTED) - -# Create imported target llvm-cxxdump -add_executable(llvm-cxxdump IMPORTED) - -# Create imported target llvm-cxxfilt -add_executable(llvm-cxxfilt IMPORTED) - -# Create imported target llvm-cxxmap -add_executable(llvm-cxxmap IMPORTED) - -# Create imported target llvm-debuginfod -add_executable(llvm-debuginfod IMPORTED) - -# Create imported target llvm-debuginfod-find -add_executable(llvm-debuginfod-find IMPORTED) - -# Create imported target llvm-diff -add_executable(llvm-diff IMPORTED) - -# Create imported target LLVMDiff -add_library(LLVMDiff STATIC IMPORTED) - -set_target_properties(LLVMDiff PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMCore;LLVMSupport" -) - -# Create imported target llvm-dis -add_executable(llvm-dis IMPORTED) - -# Create imported target llvm-dwarfdump -add_executable(llvm-dwarfdump IMPORTED) - -# Create imported target llvm-dwarfutil -add_executable(llvm-dwarfutil IMPORTED) - -# Create imported target llvm-dwp -add_executable(llvm-dwp IMPORTED) - -# Create imported target llvm-exegesis -add_executable(llvm-exegesis IMPORTED) - -# Create imported target LLVMExegesisX86 -add_library(LLVMExegesisX86 STATIC IMPORTED) - -set_target_properties(LLVMExegesisX86 PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMX86CodeGen;LLVMX86AsmParser;LLVMX86Desc;LLVMX86Disassembler;LLVMX86Info;LLVMExegesis;LLVMCore;LLVMSupport;LLVMCodeGen" -) - -# Create imported target LLVMExegesisAArch64 -add_library(LLVMExegesisAArch64 STATIC IMPORTED) - -set_target_properties(LLVMExegesisAArch64 PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMAArch64CodeGen;LLVMAArch64AsmParser;LLVMAArch64Desc;LLVMAArch64Disassembler;LLVMAArch64Info;LLVMAArch64Utils;LLVMExegesis;LLVMCore;LLVMSupport" -) - -# Create imported target LLVMExegesisMips -add_library(LLVMExegesisMips STATIC IMPORTED) - -set_target_properties(LLVMExegesisMips PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMMipsCodeGen;LLVMMipsAsmParser;LLVMMipsDesc;LLVMMipsDisassembler;LLVMMipsInfo;LLVMExegesis;LLVMCore;LLVMSupport" -) - -# Create imported target LLVMExegesis -add_library(LLVMExegesis STATIC IMPORTED) - -set_target_properties(LLVMExegesis PROPERTIES - INTERFACE_LINK_LIBRARIES "LLVMAnalysis;LLVMCodeGen;LLVMCore;LLVMExecutionEngine;LLVMGlobalISel;LLVMMC;LLVMMCDisassembler;LLVMMCJIT;LLVMMCParser;LLVMObject;LLVMObjectYAML;LLVMRuntimeDyld;LLVMSupport" -) - -# Create imported target llvm-extract -add_executable(llvm-extract IMPORTED) - -# Create imported target llvm-gsymutil -add_executable(llvm-gsymutil IMPORTED) - -# Create imported target llvm-ifs -add_executable(llvm-ifs IMPORTED) - -# Create imported target llvm-jitlink-executor -add_executable(llvm-jitlink-executor IMPORTED) -set_property(TARGET llvm-jitlink-executor PROPERTY ENABLE_EXPORTS 1) - -# Create imported target llvm-jitlink -add_executable(llvm-jitlink IMPORTED) -set_property(TARGET llvm-jitlink PROPERTY ENABLE_EXPORTS 1) - -# Create imported target llvm-libtool-darwin -add_executable(llvm-libtool-darwin IMPORTED) - -# Create imported target llvm-link -add_executable(llvm-link IMPORTED) - -# Create imported target llvm-lipo -add_executable(llvm-lipo IMPORTED) - -# Create imported target llvm-lto2 -add_executable(llvm-lto2 IMPORTED) -set_property(TARGET llvm-lto2 PROPERTY ENABLE_EXPORTS 1) - -# Create imported target llvm-mc -add_executable(llvm-mc IMPORTED) - -# Create imported target llvm-mca -add_executable(llvm-mca IMPORTED) - -# Create imported target llvm-ml -add_executable(llvm-ml IMPORTED) - -# Create imported target llvm-modextract -add_executable(llvm-modextract IMPORTED) - -# Create imported target llvm-mt -add_executable(llvm-mt IMPORTED) - -# Create imported target llvm-nm -add_executable(llvm-nm IMPORTED) - -# Create imported target llvm-objcopy -add_executable(llvm-objcopy IMPORTED) - -# Create imported target llvm-objdump -add_executable(llvm-objdump IMPORTED) - -# Create imported target llvm-opt-report -add_executable(llvm-opt-report IMPORTED) - -# Create imported target llvm-pdbutil -add_executable(llvm-pdbutil IMPORTED) - -# Create imported target llvm-profgen -add_executable(llvm-profgen IMPORTED) - -# Create imported target llvm-rc -add_executable(llvm-rc IMPORTED) - -# Create imported target llvm-readobj -add_executable(llvm-readobj IMPORTED) - -# Create imported target llvm-reduce -add_executable(llvm-reduce IMPORTED) - -# Create imported target llvm-remark-size-diff -add_executable(llvm-remark-size-diff IMPORTED) - -# Create imported target llvm-rtdyld -add_executable(llvm-rtdyld IMPORTED) - -# Create imported target LLVM -add_library(LLVM SHARED IMPORTED) - -# Create imported target llvm-sim -add_executable(llvm-sim IMPORTED) - -# Create imported target llvm-size -add_executable(llvm-size IMPORTED) - -# Create imported target llvm-split -add_executable(llvm-split IMPORTED) - -# Create imported target llvm-stress -add_executable(llvm-stress IMPORTED) - -# Create imported target llvm-strings -add_executable(llvm-strings IMPORTED) - -# Create imported target llvm-symbolizer -add_executable(llvm-symbolizer IMPORTED) - -# Create imported target llvm-tapi-diff -add_executable(llvm-tapi-diff IMPORTED) - -# Create imported target llvm-tli-checker -add_executable(llvm-tli-checker IMPORTED) - -# Create imported target llvm-undname -add_executable(llvm-undname IMPORTED) - -# Create imported target llvm-xray -add_executable(llvm-xray IMPORTED) - -# Create imported target obj2yaml -add_executable(obj2yaml IMPORTED) - -# Create imported target opt -add_executable(opt IMPORTED) -set_property(TARGET opt PROPERTY ENABLE_EXPORTS 1) - -# Create imported target Remarks -add_library(Remarks SHARED IMPORTED) - -# Create imported target sancov -add_executable(sancov IMPORTED) - -# Create imported target sanstats -add_executable(sanstats IMPORTED) - -# Create imported target split-file -add_executable(split-file IMPORTED) - -# Create imported target verify-uselistorder -add_executable(verify-uselistorder IMPORTED) - -# Create imported target yaml2obj -add_executable(yaml2obj IMPORTED) - -if(CMAKE_VERSION VERSION_LESS 2.8.12) - message(FATAL_ERROR "This file relies on consumers using CMake 2.8.12 or greater.") -endif() - -# Load information for each installed configuration. -file(GLOB _cmake_config_files "${CMAKE_CURRENT_LIST_DIR}/LLVMExports-*.cmake") -foreach(_cmake_config_file IN LISTS _cmake_config_files) - include("${_cmake_config_file}") -endforeach() -unset(_cmake_config_file) -unset(_cmake_config_files) - -# Cleanup temporary variables. -set(_IMPORT_PREFIX) - -# Cut checking of llvm libs for sdk-partly -# # Loop over all imported files and verify that they actually exist -# foreach(_cmake_target IN LISTS _cmake_import_check_targets) -# foreach(_cmake_file IN LISTS "_cmake_import_check_files_for_${_cmake_target}") -# if(NOT EXISTS "${_cmake_file}") -# message(FATAL_ERROR "The imported target \"${_cmake_target}\" references the file -# \"${_cmake_file}\" -# but this file does not exist. Possible reasons include: -# * The file was deleted, renamed, or moved to another location. -# * An install or uninstall procedure did not complete successfully. -# * The installation package was faulty and contained -# \"${CMAKE_CURRENT_LIST_FILE}\" -# but not all the files it references. -# ") -# endif() -# endforeach() -# unset(_cmake_file) -# unset("_cmake_import_check_files_for_${_cmake_target}") -# endforeach() -# unset(_cmake_target) -# unset(_cmake_import_check_targets) - -# This file does not depend on other imported targets which have -# been exported from the same project but in a separate export set. - -# Commands beyond this point should not need to know the version. -set(CMAKE_IMPORT_FILE_VERSION) -cmake_policy(POP) diff --git a/llvm-build/build.py b/llvm-build/build.py index 29e0b2180420..19247a79c812 100755 --- a/llvm-build/build.py +++ b/llvm-build/build.py @@ -1285,18 +1285,6 @@ class SysrootComposer(BuildUtils): def __init__(self, build_config): super(SysrootComposer, self).__init__(build_config) - def replace_cmake_llvm_exports(self, llvm_install): - - # Skip checking of llvm libs for sdk partly build. - # Prebuilt clang doesn't have it. - llvm_exports_cmake = 'LLVMExports.cmake' - src = os.path.join(self.build_config.LLVM_BUILD_DIR, llvm_exports_cmake) - dst = os.path.join(self.build_config.REPOROOT_DIR, 'prebuilts/clang/ohos', self.use_platform(), - 'clang-%s' % self.build_config.CLANG_VERSION, "lib/cmake/llvm", llvm_exports_cmake) - if os.path.exists(os.path.join(dst, llvm_exports_cmake)): - os.remove(os.path.join(dst, llvm_exports_cmake)) - shutil.copy2(src, dst) - def setup_cmake_platform(self, llvm_install): # OHOS.cmake already exsit on cmake prebuilts, @@ -3262,6 +3250,8 @@ def main(): assert os.path.exists(os.path.join(build_config.REPOROOT_DIR, "out", "sysroot")), "Error! Libcxx require musl!" for (_, target) in configs: llvm_libs.build_libs_by_type(llvm_path, llvm_install, target, 'runtimes', False, False) + # return original lib/cmake dir + shutil.move(lib_cmake_tmp, lib_cmake) windows_python_builder = None -- Gitee From fbd598dc6f3c569a7faf7b4a83aceada88d40516 Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Wed, 15 May 2024 06:04:00 -0400 Subject: [PATCH 66/77] [ADLT] Refactor Writer. Tracing implement. Change-Id: If7b76dca3a84d5895c003a6cffe446f865eab1fc Signed-off-by: Anton Volkov --- lld/ELF/Config.h | 7 +- lld/ELF/Writer.cpp | 245 +++++++++++++++++++++++++++++++-------------- 2 files changed, 175 insertions(+), 77 deletions(-) diff --git a/lld/ELF/Config.h b/lld/ELF/Config.h index cd768db8a7ff..701df85adde9 100644 --- a/lld/ELF/Config.h +++ b/lld/ELF/Config.h @@ -34,6 +34,7 @@ class BinaryFile; class BitcodeFile; class ELFFileBase; class SharedFile; +template class SharedFileExtended; struct PhdrEntry; // OHOS_LOCAL class InputSectionBase; class Symbol; @@ -414,7 +415,11 @@ struct Ctx { // OHOS_LOCAL begin struct AdltCtx { - llvm::SetVector commonProgramHeaders; + template SharedFileExtended *getSoExt(InputFile *file) { + return cast>(file); + } + + llvm::SetVector commonProgramHeaders; bool withCfi = false; // From input .rela.dyn, .rela.plt: // Keep input library indexes that are needed for got/plt symbol diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index dd74a8bc98bf..6743de95cd2f 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -546,6 +546,158 @@ template void elf::createSyntheticSections() { add(*in.strTab); } +// OHOS_LOCAL begin +namespace { +static struct AdltWriter { + InputSection *getInputSection(OutputSection *sec); + StringRef phdrTypeToStr(uint32_t p_type); + + template void checkPhdrs(); + template void checkRelocs(); + + template void trackPhdr(OutputSection *sec, PhdrEntry *phdr); + template void traceRelocs(); + template void tracePhdrs(); +} adltWriter; +} // namespace + +InputSection *AdltWriter::getInputSection(OutputSection *sec) { + if (!sec || !sec->hasInputSections || sec->commands.empty()) + return nullptr; + SectionCommand *cmd = sec->commands.front(); + InputSectionDescription *isd = cast(cmd); + return isd->sections.front(); +} + +template void AdltWriter::checkPhdrs() { + for (auto *file : ctx->sharedFilesExtended) + if (auto *soFile = ctx->adlt.getSoExt(file)) + assert(!soFile->programHeaders.empty() && + "ADLT: PHdr indexes can't be empty!"); +} + +template void AdltWriter::checkRelocs() { + assert(!mainPart->relaDyn->relocs.empty() && "ADLT: relaDyn can't be empty!"); + assert(!in.relaPlt->relocs.empty() && "ADLT: relaPlt can't be empty!"); + + for (auto *file : ctx->sharedFilesExtended) + if (auto *soFile = ctx->adlt.getSoExt(file)) { + assert(!soFile->dynRelIndexes.empty() && + "ADLT: relaDyn indexes can't be empty!"); + assert(!soFile->pltRelIndexes.empty() && + "ADLT: relaPlt indexes can't be empty!"); + } +} + +template +void AdltWriter::trackPhdr(OutputSection *sec, PhdrEntry *phdr) { + auto *isec = getInputSection(sec); + if (isec && isec->file) + if (auto *soFile = ctx->adlt.getSoExt(isec->file)) { + soFile->programHeaders.insert(phdr); + return; + } + ctx->adlt.commonProgramHeaders.insert(phdr); +} + +template void AdltWriter::traceRelocs() { + lld::outs() << "[ADLT]\n"; + lld::outs() << "Dyn relocs (" << mainPart->relaDyn->relocs.size() << ")\n"; + lld::outs() << "Plt relocs (" << in.relaPlt->relocs.size() << ")\n"; + + auto printIndexes = [&](auto &vec) { + lld::outs() << ": "; + for (auto &it : vec) + lld::outs() << it << ' '; + lld::outs() << "\n"; + }; + + auto printRelocTable = [&](auto *relSec, auto &outIndexes) { + for (auto &it : outIndexes) // print relocs + if (DynamicReloc *rel = &relSec->relocs[it]) + lld::outs() << it << ":\t" << rel->inputSec->name << " + 0x" + << utohexstr(rel->offsetInSec) << "\t" + << toString(rel->type) << "\t" << rel->sym->getName() + << " + 0x" << utohexstr(rel->addend) << '\n'; + }; + + for (auto *file : ctx->sharedFilesExtended) + if (auto *soFile = ctx->adlt.getSoExt(file)) { + lld::outs() << soFile->soName << ":\n"; + lld::outs() << "Dyn relocs (" << soFile->dynRelIndexes.size() << ")"; + printIndexes(soFile->dynRelIndexes); + printRelocTable(mainPart->relaDyn.get(), soFile->dynRelIndexes); + + lld::outs() << "Plt relocs (" << soFile->pltRelIndexes.size() << ")"; + printIndexes(soFile->pltRelIndexes); + printRelocTable(in.relaPlt.get(), soFile->pltRelIndexes); + } +} + +template void AdltWriter::tracePhdrs() { + lld::outs() << "[ADLT]\n"; + lld::outs() << "Program Headers (" << mainPart->phdrs.size() << ")\n"; + + llvm::DenseMap phIndexMap; + for (auto &it : llvm::enumerate(mainPart->phdrs)) + phIndexMap[it.value()] = it.index(); + + + auto printIndexes = [&](auto &vec) { + lld::outs() << ": "; + for (auto &it : vec) + lld::outs() << phIndexMap[it] << ' '; + lld::outs() << "\n"; + }; + + auto printPhTable = [&](auto &vec) { + lld::outs() << "Idx\tType\tOffset\tVirtAddr\tAlign\tFirstSec\n"; + for (const PhdrEntry *p : vec) + lld::outs() << phIndexMap[p] << ":\t" << phdrTypeToStr(p->p_type) + << "\t0x" << utohexstr(p->p_offset) << "\t0x" + << utohexstr(p->p_vaddr) << " \t0x" << utohexstr(p->p_align) + << "\t" << p->firstSec->name << '\n'; + }; + + auto &common = ctx->adlt.commonProgramHeaders; + lld::outs() << "Common Program Headers (" << common.size() << ")"; + printIndexes(common); + printPhTable(common); + + for (auto *file : ctx->sharedFilesExtended) + if (auto *soFile = ctx->adlt.getSoExt(file)) { + auto &headers = soFile->programHeaders; + lld::outs() << soFile->soName << "\n"; + lld::outs() << "Program headers (" << headers.size() << ")"; + printIndexes(headers); + printPhTable(headers); + } +} + +StringRef AdltWriter::phdrTypeToStr(uint32_t p_type) { + switch (p_type) { + case PT_PHDR: + return "PT_PHDR"; + case PT_ADLT: + return "PT_ADLT"; + case PT_LOAD: + return "PT_LOAD"; + case PT_TLS: + return "PT_TLS"; + case PT_DYNAMIC: + return "PT_DYNAMIC"; + case PT_GNU_RELRO: + return "PT_GNU_RELRO"; + case PT_GNU_STACK: + return "PT_GNU_STACK"; + case PT_NOTE: + return "PT_NOTE"; + } + llvm_unreachable("UNKNOWN TYPE"); + return ""; +} +// OHOS_LOCAL end + // The main function of the writer. template void Writer::run() { copyLocalSymbols(); @@ -1025,27 +1177,6 @@ static bool compareSections(const SectionCommand *aCmd, return false; } -// OHOS_LOCAL begin -namespace { - -InputSection* getInputSection(OutputSection* sec) { - if (!sec || !sec->hasInputSections || sec->commands.empty()) - return nullptr; - SectionCommand* cmd = sec->commands.front(); - InputSectionDescription* isd = cast(cmd); - return isd->sections.front(); -} - -template -void addPhdrToSharedFilesExtendedContext(InputFile* file, PhdrEntry* phdr) { - if (auto* soFile = dyn_cast>(file)) { - soFile->programHeaders.insert(phdr); - } -} - -} // namespace -// OHOS_LOCAL end - void PhdrEntry::add(OutputSection *sec) { lastSec = sec; if (!firstSec) @@ -1061,20 +1192,12 @@ void PhdrEntry::add(OutputSection *sec) { if (ctx->adlt.withCfi && p_type == PT_LOAD) { // check cfi.h: LIBRARY_ALIGNMENT and _BITS constexpr uint32_t kCFILibraryAlignment = 1UL << 18; - firstSec->alignment = std::max(firstSec->alignment, kCFILibraryAlignment); - p_align = std::max(p_align, kCFILibraryAlignment); + firstSec->alignment = kCFILibraryAlignment; + p_align = kCFILibraryAlignment; } - if (p_type == PT_LOAD || p_type == PT_TLS) { - auto* insec = getInputSection(sec); - // skip generated sections. - // TODO: fix .rodata_$ADLT_POSTFIX sections. - if (insec && insec->file) { - invokeELFT(addPhdrToSharedFilesExtendedContext, insec->file, this); - } else { - ctx->adlt.commonProgramHeaders.insert(this); - } - } + if (p_type == PT_LOAD || p_type == PT_TLS) + invokeELFT(adltWriter.trackPhdr, sec, this); } // OHOS_LOCAL end } @@ -1912,44 +2035,6 @@ static void removeUnusedSyntheticSections() { }); } -template static void adltTraceRelocationIndexes() { - auto &relaDyn = mainPart->relaDyn; - assert(!relaDyn->relocs.empty() && "ADLT: relaDyn can't be empty!"); - assert(!in.relaPlt->relocs.empty() && "ADLT: relaPlt can't be empty!"); - - bool adltRelocsTrace = false; // debug hint - if (adltRelocsTrace) { - auto traceRelocInfo = [&](auto *relSec, auto &outIndexes) -> void { - bool isPlt = relSec == in.relaPlt.get(); - lld::outs() << (isPlt ? "Plt" : "Dyn") << " indexes: "; - for (auto &it : outIndexes) // simple print - lld::outs() << it << ' '; - lld::outs() << '\n'; - for (auto &it : outIndexes) // print relocs - if (DynamicReloc *rel = &relSec->relocs[it]) - lld::outs() << it << ": " << rel->inputSec->name << " + 0x" - << utohexstr(rel->offsetInSec) << "\t" - << toString(rel->type) << "\t" << rel->sym->getName() - << " + 0x" << utohexstr(rel->addend) << '\n'; - }; - lld::outs() << "[ADLT]\n"; - lld::outs() << "Dyn relocs (" << relaDyn->relocs.size() << ")\n"; - lld::outs() << "Plt relocs (" << in.relaPlt->relocs.size() << ")\n"; - for (auto *file : ctx->sharedFilesExtended) - if (auto *soFile = cast>(file)) { - lld::outs() << "[ADLT] " << soFile->soName << ":\n"; - traceRelocInfo(relaDyn.get(), soFile->dynRelIndexes); - traceRelocInfo(in.relaPlt.get(), soFile->pltRelIndexes); - } - } - for (auto *file : ctx->sharedFilesExtended) { - __attribute__((unused)) auto *soFile = cast>(file); - assert(!soFile->dynRelIndexes.empty() && - "ADLT: relaDyn indexes can't be empty!"); - assert(!soFile->pltRelIndexes.empty() && - "ADLT: relaPlt indexes can't be empty!"); - } -} // Create output section objects and add them to OutputSections. template void Writer::finalizeSections() { @@ -2257,8 +2342,16 @@ template void Writer::finalizeSections() { finalizeSynthetic(part.verNeed.get()); finalizeSynthetic(part.dynamic.get()); } - if (config->adlt) // check ouput relocation indexes - adltTraceRelocationIndexes(); + // OHOS_LOCAL begin + if (config->adlt) { // check ouput entries and indexes + adltWriter.checkPhdrs(); + adltWriter.checkRelocs(); + if (config->adltTrace) { + adltWriter.tracePhdrs(); + adltWriter.traceRelocs(); + } + } + // OHOS_LOCAL end } if (!script->hasSectionsCommand && !config->relocatable) @@ -2424,7 +2517,7 @@ SmallVector Writer::createPhdrs(Partition &part) { // OHOS_LOCAL begin auto getOwnerFileIdx = [](OutputSection* sec) -> llvm::Optional { - auto file = getInputSection(sec)->file; + auto file = adltWriter.getInputSection(sec)->file; if (!file) return llvm::None; return cast>(file)->orderIdx; @@ -2520,7 +2613,7 @@ SmallVector Writer::createPhdrs(Partition &part) { // OHOS_LOCAL begin // ALDT image sections have additional addribute: input file owner. // The ownership contiguousity allows to map only related sections - // when processing program headers that + // when processing program headers that bool sameFileOwner = config->adlt && lastOwnerIdx == getOwnerFileIdx(sec); if (config->adlt) lastOwnerIdx = getOwnerFileIdx(sec); @@ -2545,7 +2638,7 @@ SmallVector Writer::createPhdrs(Partition &part) { if (config->adlt) tlsHdr = addHdr(PT_TLS, PF_R); tlsHdr->add(sec); - } + } if (!config->adlt && tlsHdr->firstSec) ret.push_back(tlsHdr); -- Gitee From cb330e4dcdd88ca18e389507a07c25013432d831 Mon Sep 17 00:00:00 2001 From: likholatovevgeny Date: Wed, 15 May 2024 16:31:51 +0300 Subject: [PATCH 67/77] Fixed resolviong of duplicates. Signed-off-by: likholatovevgeny --- lld/ELF/InputFiles.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp index 2792e1a82dd2..585d95643170 100644 --- a/lld/ELF/InputFiles.cpp +++ b/lld/ELF/InputFiles.cpp @@ -2038,6 +2038,11 @@ template void SharedFileExtended::parseDynamics() { // is stored to sh_info. If a local symbol appears after some non-local // symbol, that's a violation of the spec. StringRef name = CHECK(sym.getName(this->stringTable), this); + // Add postfix for defined duplicates. + if (config->adlt && sym.isDefined() && + ctx->eSymsHist.count(CachedHashStringRef(name)) != 0) + name = this->getUniqueName(name); + if (sym.getBinding() == STB_LOCAL) { warn("found local symbol '" + name + "' in global part of symbol table in file " + toString(this)); -- Gitee From d9f0f359f86dec77df8975b62b45cb88fbe2cd55 Mon Sep 17 00:00:00 2001 From: likholatovevgeny Date: Wed, 15 May 2024 17:57:22 +0300 Subject: [PATCH 68/77] Added exists funtion for searching duplicates. Signed-off-by: likholatovevgeny --- lld/ELF/Config.h | 9 ++++++--- lld/ELF/Driver.cpp | 4 ++-- lld/ELF/InputFiles.cpp | 12 +++++------- lld/ELF/InputFiles.h | 5 +++-- 4 files changed, 16 insertions(+), 14 deletions(-) diff --git a/lld/ELF/Config.h b/lld/ELF/Config.h index 701df85adde9..47eaa4caebf6 100644 --- a/lld/ELF/Config.h +++ b/lld/ELF/Config.h @@ -404,9 +404,6 @@ struct Ctx { // A tuple of (reference, extractedFile, sym). Used by --why-extract=. SmallVector, 0> whyExtractRecords; - // Store duplicate symbols (only defined). - typedef llvm::DenseMap eSymsCntMap; - llvm::DenseSet eSymsHist; // A mapping from a symbol to an InputFile referencing it backward. Used by // --warn-backrefs. llvm::DenseMap> gotPltInfo; // sym, soFile->orderIdx array; + // Store duplicate symbols (only defined). + llvm::DenseSet eSymsHist; + + bool exists(StringRef name) { + return eSymsHist.count(llvm::CachedHashStringRef(name)) != 0; + } } adlt; // OHOS_LOCAL end }; diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp index 8510ac21e692..6f52d37456e5 100644 --- a/lld/ELF/Driver.cpp +++ b/lld/ELF/Driver.cpp @@ -2574,12 +2574,12 @@ void LinkerDriver::link(opt::InputArgList &args) { // Fill eSymsHist for defined syms. This will help to find duplicates. if (config->adlt) { - Ctx::eSymsCntMap eSymsHist; + eSymsCntMap eSymsHist; for (auto *file : files) buildSymsHist(file, eSymsHist); for (auto eSym : eSymsHist) if (eSym.second > 1) - ctx->eSymsHist.insert(eSym.first); + ctx->adlt.eSymsHist.insert(eSym.first); eSymsHist.clear(); } diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp index 585d95643170..519f66821e59 100644 --- a/lld/ELF/InputFiles.cpp +++ b/lld/ELF/InputFiles.cpp @@ -159,14 +159,14 @@ static bool isCompatible(InputFile *file) { } template -static void doBuildSymsHist(InputFile *file, Ctx::eSymsCntMap &eSymsHist) { +static void doBuildSymsHist(InputFile *file, eSymsCntMap &eSymsHist) { if (!isCompatible(file)) return; if (auto *f = dyn_cast>(file)) f->buildSymsHist(eSymsHist); } -void elf::buildSymsHist(InputFile *file, Ctx::eSymsCntMap &eSymsHist) { +void elf::buildSymsHist(InputFile *file, eSymsCntMap &eSymsHist) { invokeELFT(doBuildSymsHist, file, eSymsHist); } @@ -1049,7 +1049,7 @@ InputSectionBase *ObjFile::createInputSection(uint32_t idx, } template -void ObjFile::buildSymsHist(Ctx::eSymsCntMap &eSymsHist) { +void ObjFile::buildSymsHist(eSymsCntMap &eSymsHist) { ArrayRef eSyms = this->getELFSyms(); for (size_t i = firstGlobal, end = eSyms.size(); i != end; ++i) { if (!eSyms[i].isDefined()) @@ -1072,8 +1072,7 @@ void ObjFile::initializeSymbols(const object::ELFFile &obj) { for (size_t i = firstGlobal, end = eSyms.size(); i != end; ++i) if (!symbols[i]) { StringRef name = CHECK(eSyms[i].getName(stringTable), this); - if (config->adlt && eSyms[i].isDefined() && - ctx->eSymsHist.count(CachedHashStringRef(name)) != 0) { + if (config->adlt && eSyms[i].isDefined() && ctx->adlt.exists(name)) { ctx->adlt.withCfi = ctx->adlt.withCfi || name == "__cfi_check"; name = this->getUniqueName(name); } @@ -2039,8 +2038,7 @@ template void SharedFileExtended::parseDynamics() { // symbol, that's a violation of the spec. StringRef name = CHECK(sym.getName(this->stringTable), this); // Add postfix for defined duplicates. - if (config->adlt && sym.isDefined() && - ctx->eSymsHist.count(CachedHashStringRef(name)) != 0) + if (config->adlt && sym.isDefined() && ctx->adlt.exists(name)) name = this->getUniqueName(name); if (sym.getBinding() == STB_LOCAL) { diff --git a/lld/ELF/InputFiles.h b/lld/ELF/InputFiles.h index 65152c337279..3b8324ca75e2 100644 --- a/lld/ELF/InputFiles.h +++ b/lld/ELF/InputFiles.h @@ -47,7 +47,8 @@ extern std::unique_ptr tar; llvm::Optional readFile(StringRef path); // Add symbols in File to the symbol table. -void buildSymsHist(InputFile *file, Ctx::eSymsCntMap &eSymsHist); +typedef llvm::DenseMap eSymsCntMap; +void buildSymsHist(InputFile *file, eSymsCntMap &eSymsHist); void parseFile(InputFile *file); // The root class of input files. @@ -293,7 +294,7 @@ public: // Get cached DWARF information. DWARFCache *getDwarf(); - void buildSymsHist(Ctx::eSymsCntMap &eSymsHist); + void buildSymsHist(eSymsCntMap &eSymsHist); void initializeLocalSymbols(); void postParse(); -- Gitee From 5883625ddce759ca105877a6faca8b0e07e2917f Mon Sep 17 00:00:00 2001 From: likholatovevgeny Date: Thu, 16 May 2024 14:45:17 +0300 Subject: [PATCH 69/77] changed name of exists method in ctx. Signed-off-by: likholatovevgeny --- lld/ELF/Config.h | 2 +- lld/ELF/InputFiles.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lld/ELF/Config.h b/lld/ELF/Config.h index 47eaa4caebf6..8aa2926df688 100644 --- a/lld/ELF/Config.h +++ b/lld/ELF/Config.h @@ -425,7 +425,7 @@ struct Ctx { // Store duplicate symbols (only defined). llvm::DenseSet eSymsHist; - bool exists(StringRef name) { + bool symExists(StringRef name) { return eSymsHist.count(llvm::CachedHashStringRef(name)) != 0; } } adlt; diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp index 519f66821e59..7ca983580868 100644 --- a/lld/ELF/InputFiles.cpp +++ b/lld/ELF/InputFiles.cpp @@ -1072,7 +1072,7 @@ void ObjFile::initializeSymbols(const object::ELFFile &obj) { for (size_t i = firstGlobal, end = eSyms.size(); i != end; ++i) if (!symbols[i]) { StringRef name = CHECK(eSyms[i].getName(stringTable), this); - if (config->adlt && eSyms[i].isDefined() && ctx->adlt.exists(name)) { + if (config->adlt && eSyms[i].isDefined() && ctx->adlt.symExists(name)) { ctx->adlt.withCfi = ctx->adlt.withCfi || name == "__cfi_check"; name = this->getUniqueName(name); } @@ -2038,7 +2038,7 @@ template void SharedFileExtended::parseDynamics() { // symbol, that's a violation of the spec. StringRef name = CHECK(sym.getName(this->stringTable), this); // Add postfix for defined duplicates. - if (config->adlt && sym.isDefined() && ctx->adlt.exists(name)) + if (config->adlt && sym.isDefined() && ctx->adlt.symExists(name)) name = this->getUniqueName(name); if (sym.getBinding() == STB_LOCAL) { -- Gitee From 4d887cd64dbef70671a7961d484b9f51928bf93a Mon Sep 17 00:00:00 2001 From: likholatovevgeny Date: Fri, 17 May 2024 10:33:42 +0300 Subject: [PATCH 70/77] Removed symExists method from ctx. Signed-off-by: likholatovevgeny --- lld/ELF/Config.h | 6 +----- lld/ELF/Driver.cpp | 6 +++--- lld/ELF/InputFiles.cpp | 12 +++++++----- lld/ELF/InputFiles.h | 6 +++--- 4 files changed, 14 insertions(+), 16 deletions(-) diff --git a/lld/ELF/Config.h b/lld/ELF/Config.h index 8aa2926df688..0d220c6afdf3 100644 --- a/lld/ELF/Config.h +++ b/lld/ELF/Config.h @@ -423,11 +423,7 @@ struct Ctx { llvm::DenseMap> gotPltInfo; // sym, soFile->orderIdx array; // Store duplicate symbols (only defined). - llvm::DenseSet eSymsHist; - - bool symExists(StringRef name) { - return eSymsHist.count(llvm::CachedHashStringRef(name)) != 0; - } + llvm::DenseSet duplicatedSymNames; } adlt; // OHOS_LOCAL end }; diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp index 6f52d37456e5..8daf57cb41ac 100644 --- a/lld/ELF/Driver.cpp +++ b/lld/ELF/Driver.cpp @@ -2572,14 +2572,14 @@ void LinkerDriver::link(opt::InputArgList &args) { for (StringRef name : config->undefined) addUnusedUndefined(name)->referenced = true; - // Fill eSymsHist for defined syms. This will help to find duplicates. + // Fill duplicatedSymNames for defined syms. This will help to find duplicates. if (config->adlt) { - eSymsCntMap eSymsHist; + ESymsCntMap eSymsHist; for (auto *file : files) buildSymsHist(file, eSymsHist); for (auto eSym : eSymsHist) if (eSym.second > 1) - ctx->adlt.eSymsHist.insert(eSym.first); + ctx->adlt.duplicatedSymNames.insert(eSym.first); eSymsHist.clear(); } diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp index 7ca983580868..aca3df13e843 100644 --- a/lld/ELF/InputFiles.cpp +++ b/lld/ELF/InputFiles.cpp @@ -159,14 +159,14 @@ static bool isCompatible(InputFile *file) { } template -static void doBuildSymsHist(InputFile *file, eSymsCntMap &eSymsHist) { +static void doBuildSymsHist(InputFile *file, ESymsCntMap &eSymsHist) { if (!isCompatible(file)) return; if (auto *f = dyn_cast>(file)) f->buildSymsHist(eSymsHist); } -void elf::buildSymsHist(InputFile *file, eSymsCntMap &eSymsHist) { +void elf::buildSymsHist(InputFile *file, ESymsCntMap &eSymsHist) { invokeELFT(doBuildSymsHist, file, eSymsHist); } @@ -1049,7 +1049,7 @@ InputSectionBase *ObjFile::createInputSection(uint32_t idx, } template -void ObjFile::buildSymsHist(eSymsCntMap &eSymsHist) { +void ObjFile::buildSymsHist(ESymsCntMap &eSymsHist) { ArrayRef eSyms = this->getELFSyms(); for (size_t i = firstGlobal, end = eSyms.size(); i != end; ++i) { if (!eSyms[i].isDefined()) @@ -1072,7 +1072,8 @@ void ObjFile::initializeSymbols(const object::ELFFile &obj) { for (size_t i = firstGlobal, end = eSyms.size(); i != end; ++i) if (!symbols[i]) { StringRef name = CHECK(eSyms[i].getName(stringTable), this); - if (config->adlt && eSyms[i].isDefined() && ctx->adlt.symExists(name)) { + if (config->adlt && eSyms[i].isDefined() && + ctx->adlt.duplicatedSymNames.count(CachedHashStringRef(name)) != 0) { ctx->adlt.withCfi = ctx->adlt.withCfi || name == "__cfi_check"; name = this->getUniqueName(name); } @@ -2038,7 +2039,8 @@ template void SharedFileExtended::parseDynamics() { // symbol, that's a violation of the spec. StringRef name = CHECK(sym.getName(this->stringTable), this); // Add postfix for defined duplicates. - if (config->adlt && sym.isDefined() && ctx->adlt.symExists(name)) + if (config->adlt && sym.isDefined() && + ctx->adlt.duplicatedSymNames.count(CachedHashStringRef(name)) != 0) name = this->getUniqueName(name); if (sym.getBinding() == STB_LOCAL) { diff --git a/lld/ELF/InputFiles.h b/lld/ELF/InputFiles.h index 3b8324ca75e2..b8d47779488d 100644 --- a/lld/ELF/InputFiles.h +++ b/lld/ELF/InputFiles.h @@ -47,8 +47,8 @@ extern std::unique_ptr tar; llvm::Optional readFile(StringRef path); // Add symbols in File to the symbol table. -typedef llvm::DenseMap eSymsCntMap; -void buildSymsHist(InputFile *file, eSymsCntMap &eSymsHist); +typedef llvm::DenseMap ESymsCntMap; +void buildSymsHist(InputFile *file, ESymsCntMap &eSymsHist); void parseFile(InputFile *file); // The root class of input files. @@ -294,7 +294,7 @@ public: // Get cached DWARF information. DWARFCache *getDwarf(); - void buildSymsHist(eSymsCntMap &eSymsHist); + void buildSymsHist(ESymsCntMap &eSymsHist); void initializeLocalSymbols(); void postParse(); -- Gitee From 9626496551c6d89ee693957dbaae5f0a4813d6bc Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Thu, 23 May 2024 10:16:57 -0400 Subject: [PATCH 71/77] Drop unused actions as objfile Change-Id: Ic88e38269d663acc81d06bc129eb663753033d4b Signed-off-by: Anton Volkov --- lld/ELF/Driver.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp index 8daf57cb41ac..b0e6e534cc17 100644 --- a/lld/ELF/Driver.cpp +++ b/lld/ELF/Driver.cpp @@ -851,7 +851,7 @@ static std::pair getPackDynRelocs(opt::InputArgList &args) { static void readCallGraph(MemoryBufferRef mb) { // Build a map from symbol name to section DenseMap map; - auto files = config->adlt ? ctx->sharedFilesExtended : ctx->objectFiles; + auto files = ctx->objectFiles; for (ELFFileBase *file : files) for (Symbol *sym : file->getSymbols()) map[sym->getName()] = sym; @@ -1832,7 +1832,7 @@ static void excludeLibs(opt::InputArgList &args) { sym->versionId = VER_NDX_LOCAL; }; - auto files = config->adlt ? ctx->sharedFilesExtended : ctx->objectFiles; + auto files = ctx->objectFiles; for (ELFFileBase *file : files) visit(file); @@ -2029,7 +2029,7 @@ static void writeDependencyFile() { // symbols of type CommonSymbol. static void replaceCommonSymbols() { llvm::TimeTraceScope timeScope("Replace common symbols"); - auto files = config->adlt ? ctx->sharedFilesExtended : ctx->objectFiles; + auto files = ctx->objectFiles; for (ELFFileBase *file : files) { if (!file->hasCommonSyms) continue; @@ -2106,7 +2106,7 @@ static void findKeepUniqueSections(opt::InputArgList &args) { // Visit the address-significance table in each object file and mark each // referenced symbol as address-significant. - auto files = config->adlt ? ctx->sharedFilesExtended : ctx->objectFiles; + auto files = ctx->objectFiles; for (InputFile *f : files) { auto *obj = cast>(f); ArrayRef syms = obj->getSymbols(); @@ -2361,7 +2361,7 @@ static void redirectSymbols(ArrayRef wrapped) { return; // Update pointers in input files. - auto files = config->adlt ? ctx->sharedFilesExtended : ctx->objectFiles; + auto files = ctx->objectFiles; parallelForEach(files, [&](ELFFileBase *file) { for (Symbol *&sym : file->getMutableGlobalSymbols()) if (Symbol *s = map.lookup(sym)) @@ -2397,7 +2397,7 @@ static uint32_t getAndFeatures() { return 0; uint32_t ret = -1; - auto files = config->adlt ? ctx->sharedFilesExtended : ctx->objectFiles; + auto files = ctx->objectFiles; for (ELFFileBase *f : files) { uint32_t features = f->andFeatures; -- Gitee From ae06c6b3157a275fb730b5adab38e087ef69467e Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Fri, 24 May 2024 03:17:26 -0400 Subject: [PATCH 72/77] Drop unused actions as objfile #2 Change-Id: I1766e0c2fa6bd4752a39ff7373fcbe3b40a6e2b5 Signed-off-by: Anton Volkov --- lld/ELF/Driver.cpp | 8 +------- lld/ELF/InputSection.cpp | 5 ++--- lld/ELF/MapFile.cpp | 4 ++-- lld/ELF/MarkLive.cpp | 15 ++++++++------- lld/ELF/SyntheticSections.cpp | 7 ++++--- lld/ELF/Writer.cpp | 17 +++++++++-------- 6 files changed, 26 insertions(+), 30 deletions(-) diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp index b0e6e534cc17..f577f352533e 100644 --- a/lld/ELF/Driver.cpp +++ b/lld/ELF/Driver.cpp @@ -2512,7 +2512,7 @@ static bool isSectionValidForAdlt(int fileIdx, InputSectionBase *s) { bool isNeededProgBits = type == SHT_PROGBITS && !(name.startswith(".got.plt") || name.startswith(".plt") || name.startswith(".got") || - name.startswith(".eh_frame_hdr"));// || name.startswith(".debug_")); + name.startswith(".eh_frame_hdr") || name.startswith(".debug_")); bool ret = isBaseType || isNeededProgBits; bool isDebug = false; @@ -2728,7 +2728,6 @@ void LinkerDriver::link(opt::InputArgList &args) { // With this the symbol table should be complete. After this, no new names // except a few linker-synthesized ones will be added to the symbol table. const size_t numObjsBeforeLTO = ctx->objectFiles.size(); - const size_t numSoBeforeLTO = ctx->sharedFilesExtended.size(); invokeELFT(compileBitcodeFiles, skipLinkedOutput); // Symbol resolution finished. Report backward reference problems, @@ -2745,11 +2744,6 @@ void LinkerDriver::link(opt::InputArgList &args) { // compileBitcodeFiles may have produced lto.tmp object files. After this, no // more file will be added. - if (config->adlt) { - auto newSharedFiles = - makeArrayRef(ctx->sharedFilesExtended).slice(numSoBeforeLTO); - parallelForEach(newSharedFiles, postParseSharedFileForAdlt); - } auto newObjectFiles = makeArrayRef(ctx->objectFiles).slice(numObjsBeforeLTO); parallelForEach(newObjectFiles, initializeLocalSymbols); parallelForEach(newObjectFiles, postParseObjectFile); diff --git a/lld/ELF/InputSection.cpp b/lld/ELF/InputSection.cpp index 9489965a7fd1..e6164bfa715f 100644 --- a/lld/ELF/InputSection.cpp +++ b/lld/ELF/InputSection.cpp @@ -982,9 +982,8 @@ static void relocateNonAllocForRelocatable(InputSection *sec, uint8_t *buf) { template void InputSectionBase::relocate(uint8_t *buf, uint8_t *bufEnd) { - if (flags & SHF_EXECINSTR && - (config->adlt ? LLVM_UNLIKELY(getSharedFile()->splitStack) - : LLVM_UNLIKELY(getFile()->splitStack))) + if (!config->adlt && flags & SHF_EXECINSTR && + LLVM_UNLIKELY(getFile()->splitStack)) adjustSplitStackFunctionPrologues(buf, bufEnd); if (flags & SHF_ALLOC) { diff --git a/lld/ELF/MapFile.cpp b/lld/ELF/MapFile.cpp index 51353287c90e..5c1d109ef73b 100644 --- a/lld/ELF/MapFile.cpp +++ b/lld/ELF/MapFile.cpp @@ -55,7 +55,7 @@ static void writeHeader(raw_ostream &os, uint64_t vma, uint64_t lma, // Returns a list of all symbols that we want to print out. static std::vector getSymbols() { std::vector v; - auto files = config->adlt ? ctx->sharedFilesExtended : ctx->objectFiles; + auto files = ctx->objectFiles; for (ELFFileBase *file : files) for (Symbol *b : file->getSymbols()) if (auto *dr = dyn_cast(b)) @@ -225,7 +225,7 @@ static void writeMapFile(raw_fd_ostream &os) { static void writeCref(raw_fd_ostream &os) { // Collect symbols and files. MapVector> map; - auto files = config->adlt ? ctx->sharedFilesExtended : ctx->objectFiles; + auto files = ctx->objectFiles; for (ELFFileBase *file : files) { for (Symbol *sym : file->getSymbols()) { if (isa(sym)) diff --git a/lld/ELF/MarkLive.cpp b/lld/ELF/MarkLive.cpp index 61be1e13287f..f4fa5b90e265 100644 --- a/lld/ELF/MarkLive.cpp +++ b/lld/ELF/MarkLive.cpp @@ -345,13 +345,14 @@ template void MarkLive::mark() { // to from __start_/__stop_ symbols because there will only be one set of // symbols for the whole program. template void MarkLive::moveToMain() { - auto files = config->adlt ? ctx->sharedFilesExtended : ctx->objectFiles; - for (ELFFileBase *file : files) - for (Symbol *s : file->getSymbols()) - if (auto *d = dyn_cast(s)) - if ((d->type == STT_GNU_IFUNC || d->type == STT_TLS) && d->section && - d->section->isLive()) - markSymbol(s); + auto files = ctx->objectFiles; + if (!config->adlt) + for (ELFFileBase *file : files) + for (Symbol *s : file->getSymbols()) + if (auto *d = dyn_cast(s)) + if ((d->type == STT_GNU_IFUNC || d->type == STT_TLS) && d->section && + d->section->isLive()) + markSymbol(s); for (InputSectionBase *sec : inputSections) { if (!sec->isLive() || !isValidCIdentifier(sec->name)) diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp index 61d52dc894b5..e317d45b9162 100644 --- a/lld/ELF/SyntheticSections.cpp +++ b/lld/ELF/SyntheticSections.cpp @@ -1531,7 +1531,8 @@ DynamicSection::computeContents() { } if (config->emachine == EM_AARCH64) { - if (config->andFeatures & GNU_PROPERTY_AARCH64_FEATURE_1_BTI) + if (!config->adlt && + config->andFeatures & GNU_PROPERTY_AARCH64_FEATURE_1_BTI) addInt(DT_AARCH64_BTI_PLT, 0); if (config->zPacPlt) addInt(DT_AARCH64_PAC_PLT, 0); @@ -2293,7 +2294,7 @@ void SymbolTableBaseSection::sortSymTabSymbolsInAdlt(size_t numLocals) { [makeKey](const SymbolTableEntry& lhs, const SymbolTableEntry& rhs) { return makeKey(lhs) <= makeKey(rhs); }); - + // extract file boundaries for local symbols for (auto iter = symbols.begin(); iter != localEnd; ++iter) if (auto* soext = dyn_cast_or_null>(iter->sym->file)) @@ -3495,7 +3496,7 @@ template void elf::splitSections() { llvm::TimeTraceScope timeScope("Split sections"); // splitIntoPieces needs to be called on each MergeInputSection // before calling finalizeContents(). - auto files = config->adlt ? ctx->sharedFilesExtended : ctx->objectFiles; + auto files = ctx->objectFiles; parallelForEach(files, [](ELFFileBase *file) { for (InputSectionBase *sec : file->getSections()) { if (!sec) diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index 6743de95cd2f..3b4d2c9e513d 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -526,7 +526,7 @@ template void elf::createSyntheticSections() { in.iplt = std::make_unique(); add(*in.iplt); - if (config->andFeatures) + if (!config->adlt && config->andFeatures) add(*make()); // .note.GNU-stack is always added when we are creating a re-linkable @@ -790,7 +790,7 @@ template static void markUsedLocalSymbols() { // See MarkLive::resolveReloc(). if (config->gcSections) return; - auto files = config->adlt ? ctx->sharedFilesExtended : ctx->objectFiles; + auto files = ctx->objectFiles; for (ELFFileBase *file : files) { ObjFile *f = cast>(file); for (InputSectionBase *s : f->getSections()) { @@ -862,7 +862,7 @@ template void Writer::copyLocalSymbols() { llvm::TimeTraceScope timeScope("Add local symbols"); if (config->copyRelocs && config->discard != DiscardPolicy::None) markUsedLocalSymbols(); - auto files = /*config->adlt ? ctx->sharedFilesExtended :*/ ctx->objectFiles; + auto files = ctx->objectFiles; for (ELFFileBase *file : files) { for (Symbol *b : file->getLocalSymbols()) { assert(b->isLocal() && "should have been caught in initializeSymbols()"); @@ -1489,7 +1489,7 @@ static DenseMap buildSectionOrder() { for (Symbol *sym : symtab->symbols()) addSym(*sym); - auto files = config->adlt ? ctx->sharedFilesExtended : ctx->objectFiles; + auto files = ctx->objectFiles; for (ELFFileBase *file : files) for (Symbol *sym : file->getLocalSymbols()) addSym(*sym); @@ -1906,7 +1906,7 @@ template void Writer::finalizeAddressDependentContent() { // block sections, input sections can shrink when the jump instructions at // the end of the section are relaxed. static void fixSymbolsAfterShrinking() { - auto files = config->adlt ? ctx->sharedFilesExtended : ctx->objectFiles; + auto files = ctx->objectFiles; for (InputFile *File : files) { parallelForEach(File->getSymbols(), [&](Symbol *Sym) { auto *def = dyn_cast(Sym); @@ -2207,9 +2207,10 @@ template void Writer::finalizeSections() { if (sym->includeInDynsym()) { partitions[sym->partition - 1].dynSymTab->addSymbol(sym); - if (auto *file = dyn_cast_or_null(sym->file)) - if (file->isNeeded && !sym->isUndefined()) - addVerneed(sym); + if (!config->adlt) // ADLT support ver syms + if (auto *file = dyn_cast_or_null(sym->file)) + if (file->isNeeded && !sym->isUndefined()) + addVerneed(sym); } } -- Gitee From bf1532c6de42c7a43206caa89ef53900858d17e9 Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Fri, 24 May 2024 05:30:25 -0400 Subject: [PATCH 73/77] Adlt ctx init Change-Id: If84f5d4f2d2ec09736a6929a1ed412b69ed9ad26 Signed-off-by: Anton Volkov --- lld/ELF/Adlt.cpp | 50 ++++++++++++++++++++++++++++ lld/ELF/Adlt.h | 62 +++++++++++++++++++++++++++++++++++ lld/ELF/CMakeLists.txt | 1 + lld/ELF/Config.h | 20 ----------- lld/ELF/Driver.cpp | 11 ++++--- lld/ELF/InputFiles.cpp | 11 ++++--- lld/ELF/InputSection.cpp | 6 ++-- lld/ELF/InputSection.h | 5 +-- lld/ELF/Relocations.cpp | 22 ++++++------- lld/ELF/SyntheticSections.cpp | 23 ++++++------- lld/ELF/Writer.cpp | 46 +++++++++++++++----------- 11 files changed, 182 insertions(+), 75 deletions(-) create mode 100644 lld/ELF/Adlt.cpp create mode 100644 lld/ELF/Adlt.h diff --git a/lld/ELF/Adlt.cpp b/lld/ELF/Adlt.cpp new file mode 100644 index 000000000000..e6258cf5249a --- /dev/null +++ b/lld/ELF/Adlt.cpp @@ -0,0 +1,50 @@ +//===- Adlt.cpp -------------------------------------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// OHOS_LOCAL begin +#include "Adlt.h" +#include "llvm/Support/Casting.h" + +#include "InputFiles.h" + +using namespace llvm; + +using namespace lld; +using namespace lld::elf; +using namespace llvm::object; + +template +SharedFileExtended *AdltCtx::getSoExt(InputFile *file) { + assert(file); + return cast>(file); +} + +template +SharedFileExtended *AdltCtx::getSoExt(unsigned orderId) { + assert(orderId < sharedFilesExtended.size()); + return sharedFilesExtended[orderId]; +} + +void AdltCtx::checkDuplicatedSymbols() { + assert(!symNamesHist.empty()); + for (auto entry : symNamesHist) + if (entry.second > 1) + duplicatedSymNames.insert(entry.first); + symNamesHist.clear(); +} + +template SharedFileExtended * +AdltCtx::getSoExt(InputFile *file); +template SharedFileExtended * +AdltCtx::getSoExt(InputFile *file); +template SharedFileExtended * +AdltCtx::getSoExt(InputFile *file); +template SharedFileExtended * +AdltCtx::getSoExt(InputFile *file); + +// OHOS_LOCAL end diff --git a/lld/ELF/Adlt.h b/lld/ELF/Adlt.h new file mode 100644 index 000000000000..a1a87df03e46 --- /dev/null +++ b/lld/ELF/Adlt.h @@ -0,0 +1,62 @@ +//===- Adlt.h -------------------------------------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// OHOS_LOCAL begin +#ifndef LLD_ELF_ADLT_H +#define LLD_ELF_ADLT_H + +#include "llvm/ADT/CachedHashString.h" +#include "llvm/ADT/SetVector.h" +#include "llvm/BinaryFormat/ELF.h" +#include "llvm/Support/Endian.h" +#include + +namespace lld { +namespace elf { + +class ELFFileBase; +class InputFile; +class Symbol; +struct PhdrEntry; + +// class SharedFileExtended; // TODO: inherit from SharedFile +template class SharedFileExtended; +struct PhdrEntry; + +class AdltCtx { +public: + // TODO: inherit from SharedFile + // llvm::SmallVector sharedFilesExtended; + llvm::SmallVector sharedFilesExtended; + + void checkDuplicatedSymbols(); + + template SharedFileExtended *getSoExt(InputFile *file); + template SharedFileExtended *getSoExt(unsigned orderId); + + llvm::SetVector commonProgramHeaders; + bool withCfi = false; + // From input .rela.dyn, .rela.plt: + // Keep input library indexes that are needed for got/plt symbol + llvm::DenseMap> + gotPltInfo; // sym, soFile->orderIdx array; + + // Store duplicate symbols (only defined). + llvm::DenseMap + symNamesHist; // hash, count usages + llvm::DenseSet duplicatedSymNames; +}; + +// The only instance of Ctx struct. +extern std::unique_ptr adltCtx; + +} // namespace elf +} // namespace lld + +#endif // LLD_ELF_ADLT_H +// OHOS_LOCAL end diff --git a/lld/ELF/CMakeLists.txt b/lld/ELF/CMakeLists.txt index 6d12da1f4b08..2361f8682d27 100644 --- a/lld/ELF/CMakeLists.txt +++ b/lld/ELF/CMakeLists.txt @@ -46,6 +46,7 @@ add_lld_library(lldELF Target.cpp Thunks.cpp Writer.cpp + Adlt.cpp LINK_COMPONENTS ${LLVM_TARGETS_TO_BUILD} diff --git a/lld/ELF/Config.h b/lld/ELF/Config.h index 0d220c6afdf3..001f714454bc 100644 --- a/lld/ELF/Config.h +++ b/lld/ELF/Config.h @@ -34,8 +34,6 @@ class BinaryFile; class BitcodeFile; class ELFFileBase; class SharedFile; -template class SharedFileExtended; -struct PhdrEntry; // OHOS_LOCAL class InputSectionBase; class Symbol; @@ -390,7 +388,6 @@ struct Ctx { SmallVector> memoryBuffers; SmallVector objectFiles; SmallVector sharedFiles; - SmallVector sharedFilesExtended; SmallVector binaryFiles; SmallVector bitcodeFiles; SmallVector lazyBitcodeFiles; @@ -409,23 +406,6 @@ struct Ctx { llvm::DenseMap> backwardReferences; - - // OHOS_LOCAL begin - struct AdltCtx { - template SharedFileExtended *getSoExt(InputFile *file) { - return cast>(file); - } - - llvm::SetVector commonProgramHeaders; - bool withCfi = false; - // From input .rela.dyn, .rela.plt: - // Keep input library indexes that are needed for got/plt symbol - llvm::DenseMap> - gotPltInfo; // sym, soFile->orderIdx array; - // Store duplicate symbols (only defined). - llvm::DenseSet duplicatedSymNames; - } adlt; - // OHOS_LOCAL end }; // The only instance of Ctx struct. diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp index f577f352533e..ff3c49f2483b 100644 --- a/lld/ELF/Driver.cpp +++ b/lld/ELF/Driver.cpp @@ -24,6 +24,7 @@ #include "Driver.h" #include "Config.h" +#include "Adlt.h" #include "ICF.h" #include "InputFiles.h" #include "InputSection.h" @@ -76,6 +77,7 @@ using namespace lld::elf; std::unique_ptr elf::config; std::unique_ptr elf::ctx; +std::unique_ptr elf::adltCtx; // OHOS_LOCAL std::unique_ptr elf::driver; static void setConfigs(opt::InputArgList &args); @@ -2574,12 +2576,13 @@ void LinkerDriver::link(opt::InputArgList &args) { // Fill duplicatedSymNames for defined syms. This will help to find duplicates. if (config->adlt) { + elf::adltCtx = std::make_unique(); ESymsCntMap eSymsHist; for (auto *file : files) buildSymsHist(file, eSymsHist); for (auto eSym : eSymsHist) if (eSym.second > 1) - ctx->adlt.duplicatedSymNames.insert(eSym.first); + adltCtx->duplicatedSymNames.insert(CachedHashStringRef(eSym.first)); eSymsHist.clear(); } @@ -2601,7 +2604,7 @@ void LinkerDriver::link(opt::InputArgList &args) { // producing a shared library. // We also need one if any shared libraries are used and for pie executables // (probably because the dynamic linker needs it). - config->hasDynSymTab = (config->adlt ? !ctx->sharedFilesExtended.empty() + config->hasDynSymTab = (config->adlt ? !adltCtx->sharedFilesExtended.empty() : !ctx->sharedFiles.empty()) || config->isPic || config->exportDynamic; @@ -2659,7 +2662,7 @@ void LinkerDriver::link(opt::InputArgList &args) { // No more lazy bitcode can be extracted at this point. Do post parse work // like checking duplicate symbols. if (config->adlt) - parallelForEach(ctx->sharedFilesExtended, postParseSharedFileForAdlt); + parallelForEach(adltCtx->sharedFilesExtended, postParseSharedFileForAdlt); parallelForEach(ctx->objectFiles, initializeLocalSymbols); parallelForEach(ctx->objectFiles, postParseObjectFile); @@ -2768,7 +2771,7 @@ void LinkerDriver::link(opt::InputArgList &args) { // Beyond this point, no new files are added. // Aggregate all input sections into one place. if (config->adlt) - for (auto it : llvm::enumerate(ctx->sharedFilesExtended)) + for (auto it : llvm::enumerate(adltCtx->sharedFilesExtended)) for (InputSectionBase *s : it.value()->getSections()) if (isSectionValidForAdlt(it.index(), s)) inputSections.push_back(s); diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp index aca3df13e843..07c6d3738243 100644 --- a/lld/ELF/InputFiles.cpp +++ b/lld/ELF/InputFiles.cpp @@ -8,6 +8,7 @@ #include "InputFiles.h" #include "Config.h" +#include "Adlt.h" #include "DWARF.h" #include "Driver.h" #include "InputSection.h" @@ -198,8 +199,8 @@ template static void doParseFile(InputFile *file) { // .so file if (config->adlt) if (auto *f = dyn_cast>(file)) { - f->orderIdx = ctx->sharedFilesExtended.size(); - ctx->sharedFilesExtended.push_back(cast(file)); + f->orderIdx = adltCtx->sharedFilesExtended.size(); + adltCtx->sharedFilesExtended.push_back(cast(file)); f->parseForAdlt(); return; } @@ -1073,8 +1074,8 @@ void ObjFile::initializeSymbols(const object::ELFFile &obj) { if (!symbols[i]) { StringRef name = CHECK(eSyms[i].getName(stringTable), this); if (config->adlt && eSyms[i].isDefined() && - ctx->adlt.duplicatedSymNames.count(CachedHashStringRef(name)) != 0) { - ctx->adlt.withCfi = ctx->adlt.withCfi || name == "__cfi_check"; + adltCtx->duplicatedSymNames.count(CachedHashStringRef(name)) != 0) { + adltCtx->withCfi = adltCtx->withCfi || name == "__cfi_check"; name = this->getUniqueName(name); } symbols[i] = symtab.insert(name); @@ -2040,7 +2041,7 @@ template void SharedFileExtended::parseDynamics() { StringRef name = CHECK(sym.getName(this->stringTable), this); // Add postfix for defined duplicates. if (config->adlt && sym.isDefined() && - ctx->adlt.duplicatedSymNames.count(CachedHashStringRef(name)) != 0) + adltCtx->duplicatedSymNames.count(CachedHashStringRef(name)) != 0) name = this->getUniqueName(name); if (sym.getBinding() == STB_LOCAL) { diff --git a/lld/ELF/InputSection.cpp b/lld/ELF/InputSection.cpp index e6164bfa715f..f8574068724d 100644 --- a/lld/ELF/InputSection.cpp +++ b/lld/ELF/InputSection.cpp @@ -348,9 +348,9 @@ void InputSection::copyRelocations(uint8_t *buf, ArrayRef rels) { for (const RelTy &rel : rels) { RelType type = rel.getType(config->isMips64EL); const ObjFile *file = - config->adlt ? getSharedFile() : getFile(); + config->adlt ? getSoExt() : getFile(); Symbol &sym = config->adlt - ? getSharedFile()->getRelocTargetSymADLT(rel, *sec) + ? getSoExt()->getRelocTargetSymADLT(rel, *sec) : file->getRelocTargetSym(rel); auto *p = reinterpret_cast(buf); @@ -866,7 +866,7 @@ void InputSection::relocateNonAlloc(uint8_t *buf, ArrayRef rels) { if (!RelTy::IsRela) addend += target.getImplicitAddend(bufLoc, type); - Symbol &sym = config->adlt ? getSharedFile()->getSymbolFromElfSymTab( + Symbol &sym = config->adlt ? getSoExt()->getSymbolFromElfSymTab( rel.getSymbol(config->isMips64EL)) : getFile()->getRelocTargetSym(rel); if (config->adlt) { // TODO improve diff --git a/lld/ELF/InputSection.h b/lld/ELF/InputSection.h index b9f4541e77a9..1bcf8b4eca20 100644 --- a/lld/ELF/InputSection.h +++ b/lld/ELF/InputSection.h @@ -17,6 +17,7 @@ #include "llvm/ADT/DenseSet.h" #include "llvm/ADT/TinyPtrVector.h" #include "llvm/Object/ELF.h" +#include "Adlt.h" namespace lld { namespace elf { @@ -138,8 +139,8 @@ public: } template - SharedFileExtended *getSharedFile() const { - return cast_or_null>(file); + SharedFileExtended *getSoExt() const { + return adltCtx->getSoExt(file); } // Used by --optimize-bb-jumps and RISC-V linker relaxation temporarily to diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp index e46ffe21d0b8..b6aeabacb4cb 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -42,6 +42,7 @@ #include "Relocations.h" #include "Config.h" +#include "Adlt.h" #include "InputFiles.h" #include "LinkerScript.h" #include "OutputSections.h" @@ -1321,14 +1322,14 @@ static void trackDynRelocAdlt(SharedFileExtended *soFile) { template static void trackGotPltAdlt(Symbol *sym, SharedFileExtended *soFile) { - ctx->adlt.gotPltInfo[sym].push_back(soFile->orderIdx); + adltCtx->gotPltInfo[sym].push_back(soFile->orderIdx); } // ADLT BEGIN template void RelocationScanner::tracePushRelocADLT(InputSectionBase &isec, Relocation &r) const { - auto file = sec.getSharedFile(); + auto file = sec.getSoExt(); auto fullOffset = isec.address + r.offset; lld::outs() << "[ADLT] Before push: [0x" + utohexstr(fullOffset) + "] type: " + toString(r.type) + @@ -1346,7 +1347,7 @@ void RelocationScanner::tracePushRelocADLT(InputSectionBase &isec, template void RelocationScanner::processForADLT(const RelTy &rel, Relocation *r, bool fromDynamic) { - auto file = sec.getSharedFile(); + auto file = sec.getSoExt(); bool isDebug = false; /*if (r->offset == 0x21F) // debug hint @@ -1474,11 +1475,10 @@ template void RelocationScanner::scanOne(RelTy *&i) { uint32_t symIndex = rel.getSymbol(config->isMips64EL); bool fromDynamic = false; if (config->adlt) - fromDynamic = sec.getSharedFile()->isDynamicSection(sec); - Symbol &sym = - config->adlt - ? sec.getSharedFile()->getSymbolADLT(symIndex, fromDynamic) - : sec.getFile()->getSymbol(symIndex); + fromDynamic = sec.getSoExt()->isDynamicSection(sec); + Symbol &sym = config->adlt + ? sec.getSoExt()->getSymbolADLT(symIndex, fromDynamic) + : sec.getFile()->getSymbol(symIndex); RelType type; // Deal with MIPS oddity. @@ -1820,9 +1820,9 @@ static bool handleNonPreemptibleIfunc(Symbol &sym) { } template static void addGotPltIndexAdlt(Symbol *s, bool isPlt) { - auto &vec = ctx->adlt.gotPltInfo[s]; + auto &vec = adltCtx->gotPltInfo[s]; for (auto &it : vec) { - auto *soFile = cast>(ctx->sharedFilesExtended[it]); + auto *soFile = cast>(adltCtx->sharedFilesExtended[it]); auto &entries = isPlt ? in.relaPlt->relocs : mainPart->relaDyn->relocs; auto &output = isPlt ? soFile->pltRelIndexes : soFile->dynRelIndexes; output.insert(entries.size() - 1); @@ -1935,7 +1935,7 @@ void elf::postScanRelocations() { // Local symbols may need the aforementioned non-preemptible ifunc and GOT // handling. They don't need regular PLT. - auto files = config->adlt ? ctx->sharedFilesExtended : ctx->objectFiles; + auto files = config->adlt ? adltCtx->sharedFilesExtended : ctx->objectFiles; for (ELFFileBase *file : files) for (Symbol *sym : file->getLocalSymbols()) fn(*sym); diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp index e317d45b9162..4120a2e00dd1 100644 --- a/lld/ELF/SyntheticSections.cpp +++ b/lld/ELF/SyntheticSections.cpp @@ -15,6 +15,7 @@ #include "SyntheticSections.h" #include "Config.h" +#include "Adlt.h" #include "DWARF.h" #include "EhFrame.h" #include "InputFiles.h" @@ -1398,11 +1399,11 @@ DynamicSection::computeContents() { part.dynStrTab->addString(config->rpath)); if (config->adlt) { - for (InputFile *file : ctx->sharedFilesExtended) { - auto *f = cast>(file); - for (size_t i = 0; i < f->dtNeeded.size(); i++) { + for (InputFile *file : adltCtx->sharedFilesExtended) { + auto *soExt = adltCtx->getSoExt(file); + for (size_t i = 0; i < soExt->dtNeeded.size(); i++) { auto tag = DT_NEEDED; - auto val = part.dynStrTab->addString(f->dtNeeded[i]); + auto val = part.dynStrTab->addString(soExt->dtNeeded[i]); if (llvm::find(entries, std::pair{tag, val}) == entries.end()) addInt(tag, val); @@ -1558,7 +1559,7 @@ DynamicSection::computeContents() { return &osd->osec; return (OutputSection *)nullptr; }; - for (InputFile *file : ctx->sharedFilesExtended) { + for (InputFile *file : adltCtx->sharedFilesExtended) { auto *f = cast>(file); auto initArray = findSection(f->addAdltPostfix(".init_array")); auto finiArray = findSection(f->addAdltPostfix(".fini_array")); @@ -2277,7 +2278,7 @@ void SymbolTableBaseSection::sortSymTabSymbolsInAdlt(size_t numLocals) { return {-1, file}; }; - for (InputFile* file : ctx->sharedFilesExtended) { + for (InputFile* file : adltCtx->sharedFilesExtended) { auto* soext = cast>(file); soext->sharedLocalSymbolIndex = llvm::None; soext->sharedGlobalSymbolIndex = llvm::None; @@ -4105,8 +4106,8 @@ AdltSection::AdltSection(StringTableSection& strTabSec) template void AdltSection::finalizeContents() { soInputs.clear(); - soInputs.reserve(ctx->sharedFilesExtended.size()); - for (InputFile* file : ctx->sharedFilesExtended) { + soInputs.reserve(adltCtx->sharedFilesExtended.size()); + for (InputFile* file : adltCtx->sharedFilesExtended) { auto* soext = cast>(file); soInputs.push_back(makeSoData(soext)); } @@ -4200,7 +4201,7 @@ typename AdltSection::CommonData AdltSection::makeCommonData() { return CommonData { UINT32_MAX, // .symtabSecIndex, filled in writeTo - ctx->adlt.commonProgramHeaders.size(), // .programHeadersAllocated + adltCtx->commonProgramHeaders.size(), // .programHeadersAllocated {}, // .phIndexes filled in writeTo }; } @@ -4349,7 +4350,7 @@ void AdltSection::extractProgramHeaderIndexes() { phdrsIndexes[it.value()] = static_cast(it.index()); }; - for (const PhdrEntry* phentry : ctx->adlt.commonProgramHeaders) { + for (const PhdrEntry* phentry : adltCtx->commonProgramHeaders) { auto it = phdrsIndexes.find(phentry); if (it != phdrsIndexes.end()) { common.phIndexes.push_back(it->second); @@ -4383,7 +4384,7 @@ void AdltSection::finalizeOnWrite() { template void AdltSection::finalizeOnWrite(size_t idx, SoData& soData) { - auto* soext = cast>(ctx->sharedFilesExtended[idx]); + auto* soext = cast>(adltCtx->sharedFilesExtended[idx]); // require SymbolTableBaseSection::sortSymTabSymbolsInAdlt for .symtab called soData.sharedLocalIndex = soext->sharedLocalSymbolIndex; diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index 3b4d2c9e513d..2c36a542a244 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -11,6 +11,7 @@ #include "ARMErrataFix.h" #include "CallGraphSort.h" #include "Config.h" +#include "Adlt.h" #include "InputFiles.h" #include "LinkerScript.h" #include "MapFile.h" @@ -570,8 +571,8 @@ InputSection *AdltWriter::getInputSection(OutputSection *sec) { } template void AdltWriter::checkPhdrs() { - for (auto *file : ctx->sharedFilesExtended) - if (auto *soFile = ctx->adlt.getSoExt(file)) + for (auto *file : adltCtx->sharedFilesExtended) + if (auto *soFile = adltCtx->getSoExt(file)) assert(!soFile->programHeaders.empty() && "ADLT: PHdr indexes can't be empty!"); } @@ -580,8 +581,8 @@ template void AdltWriter::checkRelocs() { assert(!mainPart->relaDyn->relocs.empty() && "ADLT: relaDyn can't be empty!"); assert(!in.relaPlt->relocs.empty() && "ADLT: relaPlt can't be empty!"); - for (auto *file : ctx->sharedFilesExtended) - if (auto *soFile = ctx->adlt.getSoExt(file)) { + for (auto *file : adltCtx->sharedFilesExtended) + if (auto *soFile = adltCtx->getSoExt(file)) { assert(!soFile->dynRelIndexes.empty() && "ADLT: relaDyn indexes can't be empty!"); assert(!soFile->pltRelIndexes.empty() && @@ -592,12 +593,11 @@ template void AdltWriter::checkRelocs() { template void AdltWriter::trackPhdr(OutputSection *sec, PhdrEntry *phdr) { auto *isec = getInputSection(sec); - if (isec && isec->file) - if (auto *soFile = ctx->adlt.getSoExt(isec->file)) { - soFile->programHeaders.insert(phdr); - return; - } - ctx->adlt.commonProgramHeaders.insert(phdr); + if (isec && isec->file) { + isec->getSoExt()->programHeaders.insert(phdr); + return; + } + adltCtx->commonProgramHeaders.insert(phdr); } template void AdltWriter::traceRelocs() { @@ -612,17 +612,25 @@ template void AdltWriter::traceRelocs() { lld::outs() << "\n"; }; + auto printSym = [&](Symbol *sym) { + if (sym->getName().empty() && sym->isSection()) { + Defined *d = cast(sym); + return d->section->name; + } + return sym->getName(); + }; + auto printRelocTable = [&](auto *relSec, auto &outIndexes) { for (auto &it : outIndexes) // print relocs if (DynamicReloc *rel = &relSec->relocs[it]) lld::outs() << it << ":\t" << rel->inputSec->name << " + 0x" << utohexstr(rel->offsetInSec) << "\t" - << toString(rel->type) << "\t" << rel->sym->getName() + << toString(rel->type) << "\t" << printSym(rel->sym) << " + 0x" << utohexstr(rel->addend) << '\n'; }; - for (auto *file : ctx->sharedFilesExtended) - if (auto *soFile = ctx->adlt.getSoExt(file)) { + for (auto *file : adltCtx->sharedFilesExtended) + if (auto *soFile = adltCtx->getSoExt(file)) { lld::outs() << soFile->soName << ":\n"; lld::outs() << "Dyn relocs (" << soFile->dynRelIndexes.size() << ")"; printIndexes(soFile->dynRelIndexes); @@ -659,13 +667,13 @@ template void AdltWriter::tracePhdrs() { << "\t" << p->firstSec->name << '\n'; }; - auto &common = ctx->adlt.commonProgramHeaders; + auto &common = adltCtx->commonProgramHeaders; lld::outs() << "Common Program Headers (" << common.size() << ")"; printIndexes(common); printPhTable(common); - for (auto *file : ctx->sharedFilesExtended) - if (auto *soFile = ctx->adlt.getSoExt(file)) { + for (auto *file : adltCtx->sharedFilesExtended) + if (auto *soFile = adltCtx->getSoExt(file)) { auto &headers = soFile->programHeaders; lld::outs() << soFile->soName << "\n"; lld::outs() << "Program headers (" << headers.size() << ")"; @@ -1189,7 +1197,7 @@ void PhdrEntry::add(OutputSection *sec) { // OHOS_LOCAL begin if (config->adlt) { - if (ctx->adlt.withCfi && p_type == PT_LOAD) { + if (adltCtx->withCfi && p_type == PT_LOAD) { // check cfi.h: LIBRARY_ALIGNMENT and _BITS constexpr uint32_t kCFILibraryAlignment = 1UL << 18; firstSec->alignment = kCFILibraryAlignment; @@ -2127,7 +2135,7 @@ template void Writer::finalizeSections() { // copy relocations, etc. Note that relocations for non-alloc sections are // directly processed by InputSection::relocateNonAlloc. if (config->adlt) - for (auto &it : llvm::enumerate(ctx->sharedFilesExtended)) { + for (auto &it : llvm::enumerate(adltCtx->sharedFilesExtended)) { auto *soFile = cast>(it.value()); auto sections = soFile->getSections(); // scan .rela.dyn (base: SHT_NULL) @@ -2521,7 +2529,7 @@ SmallVector Writer::createPhdrs(Partition &part) { auto file = adltWriter.getInputSection(sec)->file; if (!file) return llvm::None; - return cast>(file)->orderIdx; + return adltCtx->getSoExt(file)->orderIdx; }; // OHOS_LOCAL end -- Gitee From 189907682017abb822db409946aa0ddf74370096 Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Fri, 24 May 2024 07:48:22 -0400 Subject: [PATCH 74/77] Adlt build hist. Remove old trace. Change-Id: Iee134ed4d135f5e5ff6968d107577711973eea1f Signed-off-by: Anton Volkov --- lld/ELF/Adlt.cpp | 16 +++- lld/ELF/Adlt.h | 2 + lld/ELF/Driver.cpp | 41 ++-------- lld/ELF/InputFiles.cpp | 160 ++++++++-------------------------------- lld/ELF/InputFiles.h | 27 +++---- lld/ELF/Relocations.cpp | 24 ------ 6 files changed, 64 insertions(+), 206 deletions(-) diff --git a/lld/ELF/Adlt.cpp b/lld/ELF/Adlt.cpp index e6258cf5249a..b5e68b4c2e08 100644 --- a/lld/ELF/Adlt.cpp +++ b/lld/ELF/Adlt.cpp @@ -30,14 +30,23 @@ SharedFileExtended *AdltCtx::getSoExt(unsigned orderId) { return sharedFilesExtended[orderId]; } +template +void AdltCtx::buildSymbolsHist(std::vector &files) { + for (auto *file : files) + if (auto *soExt = getSoExt(file)) + soExt->buildSymbolsHist(); + assert(!symNamesHist.empty()); +} + void AdltCtx::checkDuplicatedSymbols() { assert(!symNamesHist.empty()); for (auto entry : symNamesHist) if (entry.second > 1) - duplicatedSymNames.insert(entry.first); + duplicatedSymNames.insert(CachedHashStringRef(entry.first)); symNamesHist.clear(); } +// TODO: inherit from SharedFile template SharedFileExtended * AdltCtx::getSoExt(InputFile *file); template SharedFileExtended * @@ -47,4 +56,9 @@ AdltCtx::getSoExt(InputFile *file); template SharedFileExtended * AdltCtx::getSoExt(InputFile *file); +template void AdltCtx::buildSymbolsHist(std::vector &); +template void AdltCtx::buildSymbolsHist(std::vector &); +template void AdltCtx::buildSymbolsHist(std::vector &); +template void AdltCtx::buildSymbolsHist(std::vector &); + // OHOS_LOCAL end diff --git a/lld/ELF/Adlt.h b/lld/ELF/Adlt.h index a1a87df03e46..4fefb1ca036a 100644 --- a/lld/ELF/Adlt.h +++ b/lld/ELF/Adlt.h @@ -36,6 +36,8 @@ public: void checkDuplicatedSymbols(); + template void buildSymbolsHist(std::vector &files); + template SharedFileExtended *getSoExt(InputFile *file); template SharedFileExtended *getSoExt(unsigned orderId); diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp index ff3c49f2483b..55583fb8ecfc 100644 --- a/lld/ELF/Driver.cpp +++ b/lld/ELF/Driver.cpp @@ -2502,27 +2502,6 @@ static void postParseSharedFileForAdlt(ELFFileBase *file) { } } -static bool isSectionValidForAdlt(int fileIdx, InputSectionBase *s) { - if (!s || s == &InputSection::discarded) - return false; - uint32_t type = s->type; - StringRef name = s->name; - - bool isBaseType = type == SHT_NOBITS || type == SHT_NOTE || - type == SHT_INIT_ARRAY || type == SHT_FINI_ARRAY; - // TODO: fix .debug_info relocation - bool isNeededProgBits = - type == SHT_PROGBITS && - !(name.startswith(".got.plt") || name.startswith(".plt") || name.startswith(".got") || - name.startswith(".eh_frame_hdr") || name.startswith(".debug_")); - bool ret = isBaseType || isNeededProgBits; - - bool isDebug = false; - if (ret && isDebug) - lld::outs() << "[isSectionValidForAdlt]: " << name << "\n"; - return ret; -} - // Do actual linking. Note that when this function is called, // all linker scripts have already been parsed. void LinkerDriver::link(opt::InputArgList &args) { @@ -2569,6 +2548,9 @@ void LinkerDriver::link(opt::InputArgList &args) { for (auto *arg : args.filtered(OPT_trace_symbol)) symtab->insert(arg->getValue())->traced = true; + if (config->adlt) // Init ADLT context + elf::adltCtx = std::make_unique(); + // Handle -u/--undefined before input files. If both a.a and b.so define foo, // -u foo a.a b.so will extract a.a. for (StringRef name : config->undefined) @@ -2576,14 +2558,8 @@ void LinkerDriver::link(opt::InputArgList &args) { // Fill duplicatedSymNames for defined syms. This will help to find duplicates. if (config->adlt) { - elf::adltCtx = std::make_unique(); - ESymsCntMap eSymsHist; - for (auto *file : files) - buildSymsHist(file, eSymsHist); - for (auto eSym : eSymsHist) - if (eSym.second > 1) - adltCtx->duplicatedSymNames.insert(CachedHashStringRef(eSym.first)); - eSymsHist.clear(); + invokeELFT(adltCtx->buildSymbolsHist, files); + adltCtx->checkDuplicatedSymbols(); } // Add all files to the symbol table. This will add almost all @@ -2771,12 +2747,11 @@ void LinkerDriver::link(opt::InputArgList &args) { // Beyond this point, no new files are added. // Aggregate all input sections into one place. if (config->adlt) - for (auto it : llvm::enumerate(adltCtx->sharedFilesExtended)) - for (InputSectionBase *s : it.value()->getSections()) - if (isSectionValidForAdlt(it.index(), s)) + for (auto *file : adltCtx->sharedFilesExtended) + for (InputSectionBase *s : file->getSections()) + if (file->isValidSection(s)) inputSections.push_back(s); - for (InputFile *f : ctx->objectFiles) for (InputSectionBase *s : f->getSections()) if (s && s != &InputSection::discarded) diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp index 07c6d3738243..6ea173582fe0 100644 --- a/lld/ELF/InputFiles.cpp +++ b/lld/ELF/InputFiles.cpp @@ -159,18 +159,6 @@ static bool isCompatible(InputFile *file) { return false; } -template -static void doBuildSymsHist(InputFile *file, ESymsCntMap &eSymsHist) { - if (!isCompatible(file)) - return; - if (auto *f = dyn_cast>(file)) - f->buildSymsHist(eSymsHist); -} - -void elf::buildSymsHist(InputFile *file, ESymsCntMap &eSymsHist) { - invokeELFT(doBuildSymsHist, file, eSymsHist); -} - template static void doParseFile(InputFile *file) { if (!isCompatible(file)) return; @@ -1048,18 +1036,6 @@ InputSectionBase *ObjFile::createInputSection(uint32_t idx, return make(*this, sec, name); } - -template -void ObjFile::buildSymsHist(ESymsCntMap &eSymsHist) { - ArrayRef eSyms = this->getELFSyms(); - for (size_t i = firstGlobal, end = eSyms.size(); i != end; ++i) { - if (!eSyms[i].isDefined()) - continue; - StringRef name = CHECK(eSyms[i].getName(stringTable), this); - eSymsHist[CachedHashStringRef(name)]++; - } -} - // Initialize this->Symbols. this->Symbols is a parallel array as // its corresponding ELF symbol table. template @@ -1247,14 +1223,6 @@ template void ObjFile::postParse() { if (sym.binding == STB_WEAK || binding == STB_WEAK) continue; std::lock_guard lock(mu); - if (config->adlt) { - auto *f = cast>(this); - bool isDebug = false; - if (isDebug) { - lld::outs() << "Put to duplicates for: " << archiveName << "\n"; - f->traceSymbol(sym); - } - } ctx->duplicates.push_back({&sym, this, sec, eSym.st_value}); } } @@ -1658,49 +1626,39 @@ SharedFileExtended::SharedFileExtended(MemoryBufferRef mb, const_cast(this->fileKind) = InputFile::SharedKind; } +template void SharedFileExtended::buildSymbolsHist() { + ArrayRef eSyms = this->template getELFSyms(); + for (size_t i = this->firstGlobal, end = eSyms.size(); i != end; ++i) { + if (!eSyms[i].isDefined()) + continue; + StringRef name = CHECK(eSyms[i].getName(this->stringTable), this); + adltCtx->symNamesHist[CachedHashStringRef(name)]++; + } +} + +template +bool SharedFileExtended::isValidSection(InputSectionBase *s) { + if (!s || s == &InputSection::discarded) + return false; + uint32_t type = s->type; + StringRef name = s->name; + + bool isBaseType = type == SHT_NOBITS || type == SHT_NOTE || + type == SHT_INIT_ARRAY || type == SHT_FINI_ARRAY; + // TODO: fix .debug-* sections relocation + bool isNeededProgBits = + type == SHT_PROGBITS && + !(name.startswith(".got.plt") || name.startswith(".plt") || + name.startswith(".got") || name.startswith(".eh_frame_hdr") || + name.startswith(".debug_")); + bool ret = isBaseType || isNeededProgBits; + return ret; +} + template void SharedFileExtended::parseForAdlt() { this->parse(); parseDynamics(); parseElfSymTab(); - - bool isDebug = false; // debug hint - if (!isDebug) - return; - - const ELFFile obj = this->getObj(); - ArrayRef objSections = this->template getELFShdrs(); - - lld::outs() << "Parse symbols from .symtab:\n"; - auto p = obj.getSection(symTabSecIdx); - if (p.takeError()) { - fatal("getSection failed: " + llvm::toString(p.takeError())); - } - const Elf_Shdr *elfSymTab = *p; - StringRef strTable = *obj.getStringTableForSymtab(*elfSymTab); - - auto eSyms = *obj.symbols(elfSymTab); - this->symbols.resize(this->symbols.size() + eSyms.size()); - for (const Elf_Sym &sym : eSyms) { - if (!sym.isDefined()) - continue; - - auto rawSec = obj.getSection(sym.st_shndx); - if (rawSec.takeError()) - continue; - if (isDebug) - traceElfSymbol(sym, strTable); - } - if (isDebug) - lld::outs() << '\n'; - - lld::outs() << "Parse offsets of some sections:\n"; - for (const Elf_Shdr &sec : objSections) { - auto name = check(obj.getSectionName(sec)); - if (name == ".init_array" || name == ".fini_array" || name == ".data" || - name == ".data.rel.ro" || name == ".bss.rel.ro" || name == ".bss") - traceElfSection(sec); - } - lld::outs() << '\n'; } template void SharedFileExtended::postParseForAdlt() { @@ -1773,8 +1731,6 @@ Defined *SharedFileExtended::findSectionSymbol(uint64_t offset) const { }); auto d = *candidates.begin(); - if (isDebug) - traceSymbol(*d, "found section sym: "); return d; } @@ -1821,7 +1777,6 @@ bool SharedFileExtended::isDynamicSection(InputSectionBase &sec) const { template Defined *SharedFileExtended::findDefinedSymbol( uint64_t offset, llvm::function_ref extraCond) const { - bool isDebug = false; /*if (offset == 0x7738) // debug hint isDebug = true;*/ auto predRange = [=](Symbol *sym) { @@ -1841,15 +1796,9 @@ Defined *SharedFileExtended::findDefinedSymbol( auto ret = std::find_if(i, e, predRange); if (ret != e) { // item was found Defined *d = cast(*ret); - if (isDebug) - traceSymbol(*d, d->isSection() ? "found section sym: " - : "found defined sym: "); return d; } - auto *sectionSym = findSectionSymbol(offset); - if (isDebug && sectionSym) - traceSymbol(*sectionSym, "found section sym: "); return sectionSym; } @@ -1859,57 +1808,6 @@ SharedFileExtended::getShStrTab(ArrayRef elfSections) { return CHECK(this->getObj().getSectionStringTable(elfSections), this); } -template -void SharedFileExtended::traceElfSymbol(const Elf_Sym &sym, - StringRef strTable) const { - const ELFFile obj = this->getObj(); - auto rawSec = obj.getSection(sym.st_shndx); - auto parsedSec = - !rawSec.takeError() ? *obj.getSection(sym.st_shndx) : nullptr; - lld::outs() << "File: " << soName << " symName: " << *sym.getName(strTable) - << " val: 0x" << utohexstr(sym.st_value) << " sec of sym: " - << (parsedSec ? *obj.getSectionName(*parsedSec) : "unknown!") - << " sym type: 0x" << utohexstr(sym.getType()) - << " sym binding: 0x" << utohexstr(sym.getBinding()) << '\n'; -} - -template -void SharedFileExtended::traceElfSection(const Elf_Shdr &sec) const { - const ELFFile obj = this->getObj(); - - auto secName = *obj.getSectionName(sec); - lld::outs() << "File: " << soName << " sec: " << secName << " sec addr: 0x" - << utohexstr(sec.sh_addr) << " sec offs: 0x" - << utohexstr(sec.sh_offset) << " sec ent size: 0x" - << utohexstr(sec.sh_entsize) << '\n'; -} - -template -void SharedFileExtended::traceSymbol(const Symbol &sym, - StringRef title) const { - lld::outs() << "File: " << soName << ": " + title - << " symName: " << sym.getName() << " exportDynamic: 0x" - << utohexstr(sym.exportDynamic); - if (!sym.isDefined()) { - lld::outs() << '\n'; - return; - } - auto &d = cast(sym); - lld::outs() << " val: 0x" << utohexstr(d.value) - << " sec of sym: " << (d.section ? d.section->name : "unknown!") - << " sym type: 0x" << utohexstr(d.type) << " sym binding: 0x" - << utohexstr(d.binding) << '\n'; -} - -template -void SharedFileExtended::traceSection(const SectionBase &sec, - StringRef title) const { - lld::outs() << "File: " << soName << ": " + title << " sec: " << sec.name - << " sec addr: 0x" << utohexstr(sec.address) << " sec offs: 0x" - << utohexstr(sec.getOffset(0)) << " sec ent size: 0x" - << utohexstr(sec.entsize) << '\n'; -} - template Symbol &SharedFileExtended::getDynamicSymbol(uint32_t symbolIndex) const { return *this->symbols[symbolIndex]; diff --git a/lld/ELF/InputFiles.h b/lld/ELF/InputFiles.h index b8d47779488d..42db1858130e 100644 --- a/lld/ELF/InputFiles.h +++ b/lld/ELF/InputFiles.h @@ -47,8 +47,6 @@ extern std::unique_ptr tar; llvm::Optional readFile(StringRef path); // Add symbols in File to the symbol table. -typedef llvm::DenseMap ESymsCntMap; -void buildSymsHist(InputFile *file, ESymsCntMap &eSymsHist); void parseFile(InputFile *file); // The root class of input files. @@ -96,14 +94,6 @@ public: fileKind == BitcodeKind); return symbols; } - - // ADLT beg - ArrayRef getAllSymbols() const { return allSymbols; } - - SmallVector allSymbols; - // ADLT end - - // Get filename to use for linker script processing. StringRef getNameForScript() const; @@ -211,6 +201,11 @@ public: return getELFSyms().slice(firstGlobal); } + // OHOS_LOCAL begin + virtual void buildSymbolsHist() {} + virtual bool isValidSection(InputSectionBase *s) { return true; } + // OHOS_LOCAL end + protected: // Initializes this class's member variables. template void init(); @@ -294,7 +289,6 @@ public: // Get cached DWARF information. DWARFCache *getDwarf(); - void buildSymsHist(ESymsCntMap &eSymsHist); void initializeLocalSymbols(); void postParse(); @@ -401,6 +395,9 @@ public: return f->kind() == InputFile::SharedKind; } + void buildSymbolsHist() override; + bool isValidSection(InputSectionBase *s) override; + void parseForAdlt(); void postParseForAdlt(); @@ -441,12 +438,6 @@ public: return *this->allSymbols[symbolIndex]; } - void traceElfSymbol(const Elf_Sym &sym, StringRef strTable) const; - void traceElfSection(const Elf_Shdr &sec) const; - - void traceSymbol(const Symbol &sym, StringRef title = "") const; - void traceSection(const SectionBase &sec, StringRef title = "") const; - public: // the input order of the file as it presented in ADLT image size_t orderIdx; @@ -489,6 +480,8 @@ protected: virtual StringRef getUniqueName(StringRef origName) const override; private: + SmallVector allSymbols; + void parseDynamics(); // SharedFile compability void parseElfSymTab(); // ObjectFile compability diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp index b6aeabacb4cb..e8067c133232 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -471,9 +471,6 @@ private: // ADLT template void processForADLT(const RelTy &rel, Relocation *r, bool fromDynamic); - - template - void tracePushRelocADLT(InputSectionBase &isec, Relocation &r) const; }; } // namespace @@ -1326,24 +1323,6 @@ static void trackGotPltAdlt(Symbol *sym, SharedFileExtended *soFile) { } // ADLT BEGIN -template -void RelocationScanner::tracePushRelocADLT(InputSectionBase &isec, - Relocation &r) const { - auto file = sec.getSoExt(); - auto fullOffset = isec.address + r.offset; - lld::outs() << "[ADLT] Before push: [0x" + utohexstr(fullOffset) + - "] type: " + toString(r.type) + - " expr: " + std::to_string(r.expr) + " offset: 0x" + - utohexstr(r.offset) + " addend: 0x" + utohexstr(r.addend) + - ".\n"; - lld::outs() << "section where: "; - file->traceSection(isec); - - lld::outs() << "r->sym: "; - file->traceSymbol(*r.sym); - lld::outs() << "\n"; -} - template void RelocationScanner::processForADLT(const RelTy &rel, Relocation *r, bool fromDynamic) { @@ -1364,9 +1343,6 @@ void RelocationScanner::processForADLT(const RelTy &rel, Relocation *r, r->offset -= fromDynamic ? secWhere->address : sec.address; assert(r->type); - if (isDebug) - tracePushRelocADLT(*secWhere, *r); - // resolve relocs switch (r->type) { // dyn relocs -- Gitee From b1327ff262fa068160cec747c7ba74f0fabcbcd4 Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Fri, 24 May 2024 11:03:35 -0400 Subject: [PATCH 75/77] Adlt cleanup SharedFileExtended Change-Id: I99a1ec2696f000fd9dd7284cc896f5678e05635f Signed-off-by: Anton Volkov --- lld/ELF/Adlt.cpp | 12 +- lld/ELF/Driver.cpp | 12 +- lld/ELF/InputFiles.cpp | 240 ++++++++++------------------------ lld/ELF/InputFiles.h | 67 ++++------ lld/ELF/InputSection.cpp | 8 +- lld/ELF/Relocations.cpp | 16 +-- lld/ELF/SyntheticSections.cpp | 10 +- 7 files changed, 121 insertions(+), 244 deletions(-) diff --git a/lld/ELF/Adlt.cpp b/lld/ELF/Adlt.cpp index b5e68b4c2e08..277d24154024 100644 --- a/lld/ELF/Adlt.cpp +++ b/lld/ELF/Adlt.cpp @@ -47,14 +47,10 @@ void AdltCtx::checkDuplicatedSymbols() { } // TODO: inherit from SharedFile -template SharedFileExtended * -AdltCtx::getSoExt(InputFile *file); -template SharedFileExtended * -AdltCtx::getSoExt(InputFile *file); -template SharedFileExtended * -AdltCtx::getSoExt(InputFile *file); -template SharedFileExtended * -AdltCtx::getSoExt(InputFile *file); +template SharedFileExtended *AdltCtx::getSoExt(InputFile *); +template SharedFileExtended *AdltCtx::getSoExt(InputFile *); +template SharedFileExtended *AdltCtx::getSoExt(InputFile *); +template SharedFileExtended *AdltCtx::getSoExt(InputFile *); template void AdltCtx::buildSymbolsHist(std::vector &); template void AdltCtx::buildSymbolsHist(std::vector &); diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp index 55583fb8ecfc..47024766d0b9 100644 --- a/lld/ELF/Driver.cpp +++ b/lld/ELF/Driver.cpp @@ -2483,19 +2483,19 @@ static void postParseObjectFile(ELFFileBase *file) { } } -static void postParseSharedFileForAdlt(ELFFileBase *file) { +static void postParseSharedFile(ELFFileBase *file) { switch (config->ekind) { case ELF32LEKind: - cast>(file)->postParseForAdlt(); + adltCtx->getSoExt(file)->postParse(); break; case ELF32BEKind: - cast>(file)->postParseForAdlt(); + adltCtx->getSoExt(file)->postParse(); break; case ELF64LEKind: - cast>(file)->postParseForAdlt(); + adltCtx->getSoExt(file)->postParse(); break; case ELF64BEKind: - cast>(file)->postParseForAdlt(); + adltCtx->getSoExt(file)->postParse(); break; default: llvm_unreachable(""); @@ -2638,7 +2638,7 @@ void LinkerDriver::link(opt::InputArgList &args) { // No more lazy bitcode can be extracted at this point. Do post parse work // like checking duplicate symbols. if (config->adlt) - parallelForEach(adltCtx->sharedFilesExtended, postParseSharedFileForAdlt); + parallelForEach(adltCtx->sharedFilesExtended, postParseSharedFile); parallelForEach(ctx->objectFiles, initializeLocalSymbols); parallelForEach(ctx->objectFiles, postParseObjectFile); diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp index 6ea173582fe0..a69d5ff11d99 100644 --- a/lld/ELF/InputFiles.cpp +++ b/lld/ELF/InputFiles.cpp @@ -188,8 +188,8 @@ template static void doParseFile(InputFile *file) { if (config->adlt) if (auto *f = dyn_cast>(file)) { f->orderIdx = adltCtx->sharedFilesExtended.size(); - adltCtx->sharedFilesExtended.push_back(cast(file)); - f->parseForAdlt(); + adltCtx->sharedFilesExtended.push_back(f); + f->parse(); return; } @@ -327,6 +327,8 @@ ELFFileBase::ELFFileBase(Kind k, MemoryBufferRef mb) : InputFile(k, mb) { } } +// namespace +// { template static const Elf_Shdr *findSection(ArrayRef sections, uint32_t type) { for (const Elf_Shdr &sec : sections) @@ -334,6 +336,9 @@ static const Elf_Shdr *findSection(ArrayRef sections, uint32_t type) { return &sec; return nullptr; } +// } // namespace + + template void ELFFileBase::init() { using Elf_Shdr = typename ELFT::Shdr; @@ -573,6 +578,7 @@ void ObjFile::initializeSections(bool ignoreComdats, this->sections[i] = createInputSection(i, sec, name); this->sections[i]->address = sec.sh_addr; this->sections[i]->size = sec.sh_size; + sectionsMap[sec.sh_addr] = i; } switch (sec.sh_type) { @@ -1590,39 +1596,10 @@ template void SharedFile::parse() { } } -template -void SharedFileExtended::resolveDuplicatesForAdlt() { - auto pred = [&](DuplicateSymbol dup) { - auto sym = dup.sym; - if (!sym->isDefined()) - return true; - - auto d = cast(sym); - return !d->section && !d->getOutputSection(); - }; - static std::mutex mu; - { - std::lock_guard lock(mu); - llvm::erase_if(ctx->duplicates, pred); - } - - if (ctx->duplicates.empty()) - return; - - { - std::lock_guard lock(mu); - for (DuplicateSymbol &dup : ctx->duplicates) - warn("duplicate: " + dup.file->getName() + ": " + dup.section->name + - ": " + dup.sym->getName()); - ctx->duplicates.clear(); - } -} - template SharedFileExtended::SharedFileExtended(MemoryBufferRef mb, StringRef soName) - : ObjFile(mb, soName), soName(soName), - simpleSoName(path::stem(soName)) { + : ObjFile(mb, soName), soName(soName) { const_cast(this->fileKind) = InputFile::SharedKind; } @@ -1655,177 +1632,103 @@ bool SharedFileExtended::isValidSection(InputSectionBase *s) { return ret; } -template void SharedFileExtended::parseForAdlt() { - this->parse(); +template +bool SharedFileExtended::isDynamicSection(InputSectionBase *s) const { + return s->type == llvm::ELF::SHT_NULL || s->name.startswith(".got.plt"); +} + +template void SharedFileExtended::parse(bool ignoreComdats) { + ObjFile::parse(ignoreComdats); parseDynamics(); parseElfSymTab(); } -template void SharedFileExtended::postParseForAdlt() { - this->initializeLocalSymbols(); - this->postParse(); - resolveDuplicatesForAdlt(); +template void SharedFileExtended::postParse() { + ObjFile::initializeLocalSymbols(); + ObjFile::postParse(); } template -StringRef SharedFileExtended::addAdltPostfix(StringRef input) const { +StringRef SharedFileExtended::getUniqueName(StringRef input) const { if (input.empty()) return input; auto suffix = Twine("__") + Twine::utohexstr(this->orderIdx); return saver().save(input + suffix); } template -bool SharedFileExtended::addAdltPostfix(Symbol *s) { - 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); +Defined &SharedFileExtended::getDefinedLocalSym(unsigned offset) { + auto getSym = [&](unsigned offs) { + return localSymbols[this->definedSymbolsMap[offs]]; + }; + auto *s = getSym(offset); + if (!s || s == localSymbols[0]) { + auto sec = findSection(offset); + s = getSym(sec->address); + } + assert(s && s->isDefined()); + return cast(*s); } -// TODO: optimize 2 lookups template -bool SharedFileExtended::saveSymbol(const Defined& d) const { - auto found = elf::symtab->find(d.getName()); - if (found) - return false; - in.symTab->addSymbol(elf::symtab->addSymbol(d)); - return true; +InputSectionBase &SharedFileExtended::getSection(unsigned offset) { + auto *sec = this->sections[this->sectionsMap[offset]]; + if (offset && sec->type == SHT_NULL) + sec = findSection(offset); + return *sec; } template -Defined *SharedFileExtended::findSectionSymbol(uint64_t offset) const { - bool isDebug = false; - /*if (offset == 0x7738) // debug hint - isDebug = true;*/ - llvm::SmallVector candidates; - for (auto *sym : this->allSymbols) { - if (!sym || !sym->isSection()) - continue; - assert(sym->isDefined()); - Defined *d = cast(sym); - uint64_t low = d->section->address; - uint64_t high = low + d->section->size; - - if (isDebug) - lld::outs() << "offset: 0x" + utohexstr(offset) + - " sect name: " + d->section->name + "low 0x" + - utohexstr(low) + " high: 0x" + utohexstr(high) + "\n"; - bool isGood = (offset >= low) && (offset < high); - if (!isGood) - continue; - candidates.push_back(d); - } - if (candidates.empty()) // no suitable items found - return nullptr; - - llvm::sort(candidates, [offset](Defined *d1, Defined *d2) { - auto a1 = d1->section->address; - auto a2 = d2->section->address; - return (offset - a1 < offset - a2); - }); - - auto d = *candidates.begin(); - return d; -} - -template -InputSectionBase * -SharedFileExtended::findInputSection(StringRef name) const { - for (InputSectionBase *sec : this->sections) - if (sec && sec->name == name) - return sec; - return nullptr; +InputSectionBase &SharedFileExtended::getSectionByOrder(unsigned idx) { + auto *sec = this->sections[idx]; + if (!sec) + sec = &InputSection::discarded; + return *sec; } template -InputSectionBase * -SharedFileExtended::findInputSection(uint64_t offset) const { +InputSectionBase *SharedFileExtended::findSection(unsigned offset) { llvm::SmallVector candidates; for (InputSectionBase *sec : this->sections) { if (!sec) continue; - if (sec->address == offset) - return sec; - uint64_t low = sec->address; - uint64_t high = low + sec->size; - bool isGood = (offset >= low) && (offset < high); - if (!isGood) - continue; - candidates.push_back(sec); + auto low = sec->address, high = low + sec->size; + if (offset >= low && offset < high) + candidates.push_back(sec); } if (candidates.empty()) // no suitable items found return nullptr; - - auto lessAddr = [&](auto *s1, auto *s2) { return s1->address < s2->address; }; - auto i = candidates.begin(); - auto e = candidates.end(); - auto ret = std::min_element(i, e, lessAddr); - return *ret; -} - -template -bool SharedFileExtended::isDynamicSection(InputSectionBase &sec) const { - return sec.type == llvm::ELF::SHT_NULL || sec.name.startswith(".got.plt"); -} - -template -Defined *SharedFileExtended::findDefinedSymbol( - uint64_t offset, llvm::function_ref extraCond) const { - /*if (offset == 0x7738) // debug hint - isDebug = true;*/ - auto predRange = [=](Symbol *sym) { - if (!sym || sym->isUndefined()) - return false; - - Defined *d = cast(sym); - if (d->file != this) - return false; - - bool goodVal = d->section->address + d->value == offset; - return goodVal && extraCond(d); - }; - - auto i = this->allSymbols.begin(); - auto e = this->allSymbols.end(); - auto ret = std::find_if(i, e, predRange); - if (ret != e) { // item was found - Defined *d = cast(*ret); - return d; - } - auto *sectionSym = findSectionSymbol(offset); - return sectionSym; -} - -template -StringRef -SharedFileExtended::getShStrTab(ArrayRef elfSections) { - return CHECK(this->getObj().getSectionStringTable(elfSections), this); + llvm::sort(candidates, [offset](InputSectionBase *a, InputSectionBase *b) { + return (offset - a->address < offset - b->address); + }); + return *candidates.begin(); } template Symbol &SharedFileExtended::getDynamicSymbol(uint32_t symbolIndex) const { return *this->symbols[symbolIndex]; } +template +Symbol &SharedFileExtended::getLocalSymbol(uint32_t symbolIndex) const{ + assert(symbolIndex && (symbolIndex < localSymbols.size())); + return *localSymbols[symbolIndex]; +} template -Symbol &SharedFileExtended::getSymbolADLT(uint32_t symbolIndex, - bool fromDynamic) const { - Symbol &sym = fromDynamic ? getDynamicSymbol(symbolIndex) - : getSymbolFromElfSymTab(symbolIndex); +Symbol &SharedFileExtended::getSymbol(uint32_t symbolIndex, + bool fromDynamic) { + if (!symbolIndex) + return *localSymbols[0]; + /* TODO + if (symbolIndex > static_cast(symTabFirstGlobal)) + return getDynamicSymbol(symbolIndex - (symTabFirstGlobal + 1)); + return fromDynamic ? getDynamicSymbol(symbolIndex) + : getLocalSymbol(symbolIndex); */ + Symbol &sym = fromDynamic ? getDynamicSymbol(symbolIndex) + : getLocalSymbol(symbolIndex); StringRef name = sym.getName(); if (name.empty()) return sym; - - /*if (name.contains("__emutls_v.TLS_data1")) // debug hint - lld::outs() << "debug getSymbolADLT(): " << name << "\n";*/ - // check SymbolTable auto res = elf::symtab->find(name); if (res && (res->exportDynamic || res->versionId)) @@ -2018,16 +1921,16 @@ template void SharedFileExtended::parseElfSymTab() { ArrayRef eSections = this->template getELFShdrs(); // Find a symbol table. - const Elf_Shdr *eSymtabSec = findSection(eSections, SHT_SYMTAB); + const Elf_Shdr *eSymtabSec = ::findSection(eSections, SHT_SYMTAB); if (!eSymtabSec) return; - eFirstGlobal = eSymtabSec->sh_info; + symTabFirstGlobal = eSymtabSec->sh_info; ArrayRef eSyms = CHECK(obj.symbols(eSymtabSec), this); auto numESyms = uint32_t(eSyms.size()); auto eStringTable = CHECK(obj.getStringTableForSymtab(*eSymtabSec, eSections), this); - this->allSymbols.resize(numESyms); + this->localSymbols.resize(numESyms); for (size_t i = 0; i < numESyms; i++) { const Elf_Sym &eSym = eSyms[i]; @@ -2056,13 +1959,14 @@ template void SharedFileExtended::parseElfSymTab() { }); bool isInDynSym = dynSym != this->symbols.end(); if (!isInDynSym) - name = addAdltPostfix(name); + name = getUniqueName(name); /*if (name == "TLS_data1") // debug hint lld::outs() << name << '\n'; */ - this->allSymbols[i] = + this->localSymbols[i] = make(this, name, bind, other, type, val, eSym.st_size, sec); + this->definedSymbolsMap[eSym.st_value] = i; } else { - this->allSymbols[i] = make(this, name, bind, other, type, + this->localSymbols[i] = make(this, name, bind, other, type, /*discardedSecIdx=*/i); } } diff --git a/lld/ELF/InputFiles.h b/lld/ELF/InputFiles.h index 42db1858130e..3167372e42eb 100644 --- a/lld/ELF/InputFiles.h +++ b/lld/ELF/InputFiles.h @@ -238,13 +238,13 @@ public: } virtual ~ObjFile() {} - void parse(bool ignoreComdats = false); + virtual void parse(bool ignoreComdats = false); void parseLazy(); StringRef getShtGroupSignature(ArrayRef sections, const Elf_Shdr &sec); - virtual Symbol &getSymbol(uint32_t symbolIndex) const { + Symbol &getSymbol(uint32_t symbolIndex) const { if (symbolIndex >= this->symbols.size()) fatal(toString(this) + ": invalid symbol index"); return *this->symbols[symbolIndex]; @@ -290,11 +290,16 @@ public: DWARFCache *getDwarf(); void initializeLocalSymbols(); - void postParse(); + virtual void postParse(); -protected: + // OHOS_LOCAL virtual StringRef getUniqueName(StringRef origName) const; +protected: + // OHOS_LOCAL + llvm::DenseMap sectionsMap; // offset, orderIdx + llvm::DenseMap definedSymbolsMap; // abs offset, orderIdx + private: void initializeSections(bool ignoreComdats, const llvm::object::ELFFile &obj); @@ -394,49 +399,28 @@ public: static bool classof(const InputFile *f) { return f->kind() == InputFile::SharedKind; } - void buildSymbolsHist() override; bool isValidSection(InputSectionBase *s) override; + bool isDynamicSection(InputSectionBase *s) const; - void parseForAdlt(); - void postParseForAdlt(); - - StringRef addAdltPostfix(StringRef input) const; - bool addAdltPostfix(Symbol *s); + void parse(bool ignoreComdats = false) override; + void postParse() override; - bool saveSymbol(const Defined& d) const; + Defined &getDefinedLocalSym(unsigned offset); - Defined *findSectionSymbol(uint64_t offset) const; - Defined *findDefinedSymbol( - uint64_t offset, - llvm::function_ref extraCond = [](Defined *) { - return true; - }) const; + InputSectionBase &getSection(unsigned offset); + InputSectionBase &getSectionByOrder(unsigned idx); + InputSectionBase *findSection(unsigned offset); - InputSectionBase *findInputSection(StringRef name) const; - InputSectionBase *findInputSection(uint64_t offset) const; - bool isDynamicSection(InputSectionBase &sec) const; - - template - Symbol &getRelocTargetSymADLT(const RelT &rel, InputSectionBase &sec) const { + template Symbol &getRelocTargetSym(const RelT &rel) { uint32_t symIndex = rel.getSymbol(config->isMips64EL); - return getSymbolADLT(symIndex, isDynamicSection(sec)); - } - - ArrayRef getLocalSymbols() override { - if (this->allSymbols.empty()) - return {}; - return llvm::makeArrayRef(this->allSymbols).slice(1, eFirstGlobal - 1); + return getSymbol(symIndex, false); } Symbol &getDynamicSymbol(uint32_t symbolIndex) const; - Symbol &getSymbolADLT(uint32_t symbolIndex, bool fromDynamic) const; + Symbol &getLocalSymbol(uint32_t symbolIndex) const; + Symbol &getSymbol(uint32_t symbolIndex, bool fromDynamic); - Symbol &getSymbolFromElfSymTab(uint32_t symbolIndex) const { - if (symbolIndex >= this->allSymbols.size()) - fatal(toString(this) + ": invalid symbol index"); - return *this->allSymbols[symbolIndex]; - } public: // the input order of the file as it presented in ADLT image @@ -444,7 +428,7 @@ public: int dynSymSecIdx = 0; int symTabSecIdx = 0; int symTabShndxSecIdx = 0; - int eFirstGlobal = 0; + int symTabFirstGlobal = 0; int gotPltSecIdx = 0; // .symtab's start of local symbols owned by library @@ -476,19 +460,14 @@ public: // parsed. Only filled for `--no-allow-shlib-undefined`. SmallVector requiredSymbols; -protected: - virtual StringRef getUniqueName(StringRef origName) const override; + StringRef getUniqueName(StringRef origName) const override; private: - SmallVector allSymbols; + SmallVector localSymbols; void parseDynamics(); // SharedFile compability void parseElfSymTab(); // ObjectFile compability - void resolveDuplicatesForAdlt(); - - StringRef getShStrTab(ArrayRef elfSections); - std::vector parseVerneed(const llvm::object::ELFFile &obj, const typename ELFT::Shdr *sec); }; diff --git a/lld/ELF/InputSection.cpp b/lld/ELF/InputSection.cpp index f8574068724d..782127aece1c 100644 --- a/lld/ELF/InputSection.cpp +++ b/lld/ELF/InputSection.cpp @@ -347,10 +347,9 @@ void InputSection::copyRelocations(uint8_t *buf, ArrayRef rels) { for (const RelTy &rel : rels) { RelType type = rel.getType(config->isMips64EL); - const ObjFile *file = - config->adlt ? getSoExt() : getFile(); + const ObjFile *file = getFile(); Symbol &sym = config->adlt - ? getSoExt()->getRelocTargetSymADLT(rel, *sec) + ? getSoExt()->getRelocTargetSym(rel) : file->getRelocTargetSym(rel); auto *p = reinterpret_cast(buf); @@ -866,8 +865,7 @@ void InputSection::relocateNonAlloc(uint8_t *buf, ArrayRef rels) { if (!RelTy::IsRela) addend += target.getImplicitAddend(bufLoc, type); - Symbol &sym = config->adlt ? getSoExt()->getSymbolFromElfSymTab( - rel.getSymbol(config->isMips64EL)) + Symbol &sym = config->adlt ? getSoExt()->getRelocTargetSym(rel) : getFile()->getRelocTargetSym(rel); if (config->adlt) { // TODO improve switch (type) { diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp index e8067c133232..ee518b20c0ef 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -1336,22 +1336,22 @@ void RelocationScanner::processForADLT(const RelTy &rel, Relocation *r, isDebug = true;*/ // parse offset (where) - InputSectionBase *secWhere = file->findInputSection(r->offset); - assert(secWhere && "not found!"); + InputSectionBase &secWhere = + (sec.type != SHT_NULL) ? sec : file->getSection(r->offset); // process offset - r->offset -= fromDynamic ? secWhere->address : sec.address; + r->offset -= fromDynamic ? secWhere.address : sec.address; assert(r->type); // resolve relocs switch (r->type) { // dyn relocs case R_AARCH64_RELATIVE: { - Defined *d = file->findDefinedSymbol(r->addend); + Defined *d = &file->getDefinedLocalSym(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, + addRelativeReloc(secWhere, r->offset, *r->sym, r->addend, r->expr, r->type); trackDynRelocAdlt(file); return; @@ -1375,7 +1375,7 @@ void RelocationScanner::processForADLT(const RelTy &rel, Relocation *r, case R_AARCH64_ABS64: if (fromDynamic) { assert(r->sym->exportDynamic); - sec.getPartition().relaDyn->addSymbolReloc(target.symbolicRel, *secWhere, + sec.getPartition().relaDyn->addSymbolReloc(target.symbolicRel, secWhere, r->offset, *r->sym, r->addend, r->type); trackDynRelocAdlt(file); @@ -1451,9 +1451,9 @@ template void RelocationScanner::scanOne(RelTy *&i) { uint32_t symIndex = rel.getSymbol(config->isMips64EL); bool fromDynamic = false; if (config->adlt) - fromDynamic = sec.getSoExt()->isDynamicSection(sec); + fromDynamic = sec.getSoExt()->isDynamicSection(&sec); Symbol &sym = config->adlt - ? sec.getSoExt()->getSymbolADLT(symIndex, fromDynamic) + ? sec.getSoExt()->getSymbol(symIndex, fromDynamic) : sec.getFile()->getSymbol(symIndex); RelType type; diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp index 4120a2e00dd1..5a4abd964dbf 100644 --- a/lld/ELF/SyntheticSections.cpp +++ b/lld/ELF/SyntheticSections.cpp @@ -1560,9 +1560,9 @@ DynamicSection::computeContents() { return (OutputSection *)nullptr; }; for (InputFile *file : adltCtx->sharedFilesExtended) { - auto *f = cast>(file); - auto initArray = findSection(f->addAdltPostfix(".init_array")); - auto finiArray = findSection(f->addAdltPostfix(".fini_array")); + auto *f = adltCtx->getSoExt(file); + auto initArray = findSection(f->getUniqueName(".init_array")); + auto finiArray = findSection(f->getUniqueName(".fini_array")); if (initArray) { addInt(DT_INIT_ARRAY, initArray->addr); addInt(DT_INIT_ARRAYSZ, initArray->size); @@ -4221,8 +4221,8 @@ AdltSection::makeSoData(const SharedFileExtended* soext) { }); } - data.initArrayName = soext->addAdltPostfix(".init_array"); - data.finiArrayName = soext->addAdltPostfix(".fini_array"); + data.initArrayName = soext->getUniqueName(".init_array"); + data.finiArrayName = soext->getUniqueName(".fini_array"); data.relaDynIndx = soext->dynRelIndexes.getArrayRef(); data.relaPltIndx = soext->pltRelIndexes.getArrayRef(); -- Gitee From a33b24b725003cb7ae82cea1bb4b204ee4a8cfe8 Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Fri, 24 May 2024 11:21:28 -0400 Subject: [PATCH 76/77] Adlt cleanup RelocationScanner Change-Id: Idd74f5fc81f4cc4213430ec412ab43ee6ef6e499 Signed-off-by: Anton Volkov --- lld/ELF/Adlt.cpp | 7 ++- lld/ELF/Relocations.cpp | 126 +++++++++++++++++++++++++++------------- 2 files changed, 92 insertions(+), 41 deletions(-) diff --git a/lld/ELF/Adlt.cpp b/lld/ELF/Adlt.cpp index 277d24154024..308706eabba8 100644 --- a/lld/ELF/Adlt.cpp +++ b/lld/ELF/Adlt.cpp @@ -27,7 +27,7 @@ SharedFileExtended *AdltCtx::getSoExt(InputFile *file) { template SharedFileExtended *AdltCtx::getSoExt(unsigned orderId) { assert(orderId < sharedFilesExtended.size()); - return sharedFilesExtended[orderId]; + return cast>(sharedFilesExtended[orderId]); } template @@ -52,6 +52,11 @@ template SharedFileExtended *AdltCtx::getSoExt(InputFile *); template SharedFileExtended *AdltCtx::getSoExt(InputFile *); template SharedFileExtended *AdltCtx::getSoExt(InputFile *); +template SharedFileExtended *AdltCtx::getSoExt(unsigned); +template SharedFileExtended *AdltCtx::getSoExt(unsigned); +template SharedFileExtended *AdltCtx::getSoExt(unsigned); +template SharedFileExtended *AdltCtx::getSoExt(unsigned); + template void AdltCtx::buildSymbolsHist(std::vector &); template void AdltCtx::buildSymbolsHist(std::vector &); template void AdltCtx::buildSymbolsHist(std::vector &); diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp index ee518b20c0ef..7d0e5ac30c74 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -439,6 +439,51 @@ private: size_t i = 0; }; + +// OHOS_LOCAL begin +template class AdltRelocationScanner { +public: + AdltRelocationScanner(InputSectionBase &isec) + : file(adltCtx->getSoExt(isec.file)), + fromDynamic(file->isDynamicSection(&isec)), sec(isec) {} + + SharedFileExtended *getSoExt() { return file; } + + Symbol &getSymbol(uint32_t idx) { return file->getSymbol(idx, fromDynamic); } + + void process(Relocation *r); + void trackGotPlt(Symbol *sym); + void trackDynReloc(); + + unsigned getDynamicRelocsCount(); + bool isRelr() { return config->adlt && relSecType == SHT_RELR; } + bool toProcessAux = false; + +private: + SharedFileExtended *file = nullptr; + bool fromDynamic = false; + InputSectionBase &sec; + uint32_t relSecType = SHT_NULL; +}; + +template +void AdltRelocationScanner::trackGotPlt(Symbol *sym) { + adltCtx->gotPltInfo[sym].push_back(file->orderIdx); +} + +template +void AdltRelocationScanner::trackDynReloc() { + file->dynRelIndexes.insert(getDynamicRelocsCount() - 1); +} + +template +unsigned AdltRelocationScanner::getDynamicRelocsCount() { + auto currentSize = isRelr() ? mainPart->relrDyn->relocs.size() + : mainPart->relaDyn->relocs.size(); + return currentSize; +} +// OHOS_LOCAL end + // This class encapsulates states needed to scan relocations for one // InputSectionBase. class RelocationScanner { @@ -464,6 +509,7 @@ private: int64_t computeAddend(const RelTy &rel, RelExpr expr, bool isLocal) const; bool isStaticLinkTimeConstant(RelExpr e, RelType type, const Symbol &sym, uint64_t relOff) const; + void processAux(Relocation &r); // OHOS_LOCAL void processAux(RelExpr expr, RelType type, uint64_t offset, Symbol &sym, int64_t addend) const; template void scanOne(RelTy *&i); @@ -1041,6 +1087,11 @@ bool RelocationScanner::isStaticLinkTimeConstant(RelExpr e, RelType type, // sections. Given that it is ro, we will need an extra PT_LOAD. This // complicates things for the dynamic linker and means we would have to reserve // space for the extra PT_LOAD even if we end up not using it. + +void RelocationScanner::processAux(Relocation &r) { // OHOS_LOCAL + processAux(r.expr, r.type, r.offset, *r.sym, r.addend); +} + void RelocationScanner::processAux(RelExpr expr, RelType type, uint64_t offset, Symbol &sym, int64_t addend) const { // If the relocation is known to be a link-time constant, we know no dynamic @@ -1312,23 +1363,9 @@ static unsigned handleTlsRelocation(RelType type, Symbol &sym, return 0; } +// OHOS_LOCAL begin template -static void trackDynRelocAdlt(SharedFileExtended *soFile) { - soFile->dynRelIndexes.insert(mainPart->relaDyn->relocs.size() - 1); -} - -template -static void trackGotPltAdlt(Symbol *sym, SharedFileExtended *soFile) { - adltCtx->gotPltInfo[sym].push_back(soFile->orderIdx); -} - -// ADLT BEGIN -template -void RelocationScanner::processForADLT(const RelTy &rel, Relocation *r, - bool fromDynamic) { - auto file = sec.getSoExt(); - bool isDebug = false; - +void AdltRelocationScanner::process(Relocation *r) { /*if (r->offset == 0x21F) // debug hint isDebug = true; /*if (r->type == R_AARCH64_ABS64 && @@ -1340,7 +1377,7 @@ void RelocationScanner::processForADLT(const RelTy &rel, Relocation *r, (sec.type != SHT_NULL) ? sec : file->getSection(r->offset); // process offset - r->offset -= fromDynamic ? secWhere.address : sec.address; + r->offset -= secWhere.address; assert(r->type); // resolve relocs @@ -1353,20 +1390,21 @@ void RelocationScanner::processForADLT(const RelTy &rel, Relocation *r, r->addend -= d->section->address + d->value; addRelativeReloc(secWhere, r->offset, *r->sym, r->addend, r->expr, r->type); - trackDynRelocAdlt(file); + assert(getDynamicRelocsCount()); + trackDynReloc(); return; } case R_AARCH64_GLOB_DAT: assert(r->sym->exportDynamic); if (!r->sym->needsGot) r->sym->needsGot = 1; - trackGotPltAdlt(r->sym, file); + trackGotPlt(r->sym); return; case R_AARCH64_JUMP_SLOT: assert(r->sym->exportDynamic); if (r->sym->isUndefined() && !r->sym->needsPlt) r->sym->needsPlt = 1; - trackGotPltAdlt(r->sym, file); + trackGotPlt(r->sym); return; // abs relocs case R_AARCH64_ABS32: @@ -1375,10 +1413,10 @@ void RelocationScanner::processForADLT(const RelTy &rel, Relocation *r, case R_AARCH64_ABS64: if (fromDynamic) { assert(r->sym->exportDynamic); - sec.getPartition().relaDyn->addSymbolReloc(target.symbolicRel, secWhere, + sec.getPartition().relaDyn->addSymbolReloc(r->type, secWhere, r->offset, *r->sym, r->addend, r->type); - trackDynRelocAdlt(file); + trackDynReloc(); return; } sec.relocations.push_back(*r); @@ -1390,7 +1428,7 @@ void RelocationScanner::processForADLT(const RelTy &rel, Relocation *r, case R_AARCH64_LDST128_ABS_LO12_NC: case R_AARCH64_PREL32: case R_AARCH64_PREL64: - processAux(r->expr, r->type, r->offset, *r->sym, r->addend); + toProcessAux = true; return; // plt relocs case R_AARCH64_CALL26: @@ -1408,24 +1446,21 @@ void RelocationScanner::processForADLT(const RelTy &rel, Relocation *r, return; case R_AARCH64_ADR_GOT_PAGE: if (r->sym->isDefined() && !r->sym->needsGot) { - if (isDebug) - lld::outs() << "[ADLT] R_AARCH64_ADR_GOT_PAGE: sym not in GOT! "; - r->expr = R_PC; // prev: R_AARCH64_GOT_PAGE_PC || R_AARCH64_GOT_PAGE || - // R_GOT || R_GOT_PC - // TODO: replace reloc R_AARCH64_ADR_GOT_PAGE + // prev: R_AARCH64_GOT_PAGE_PC || R_AARCH64_GOT_PAGE || R_GOT || R_GOT_PC + r->expr = R_PC; sec.relocations.push_back(*r); return; } LLVM_FALLTHROUGH; case R_AARCH64_LD64_GOT_LO12_NC: case R_AARCH64_LD64_GOTPAGE_LO15: - processAux(r->expr, r->type, r->offset, *r->sym, r->addend); + toProcessAux = true; return; // tls relocs case R_AARCH64_TLSDESC: if (fromDynamic && !r->sym->needsTlsDesc) r->sym->needsTlsDesc = 1; - trackDynRelocAdlt(file); + trackGotPlt(r->sym); return; case R_AARCH64_TLSDESC_CALL: case R_AARCH64_TLS_TPREL64: @@ -1449,11 +1484,11 @@ void RelocationScanner::processForADLT(const RelTy &rel, Relocation *r, template void RelocationScanner::scanOne(RelTy *&i) { const RelTy &rel = *i; uint32_t symIndex = rel.getSymbol(config->isMips64EL); - bool fromDynamic = false; + std::unique_ptr> adltScanner = nullptr; if (config->adlt) - fromDynamic = sec.getSoExt()->isDynamicSection(&sec); + adltScanner = std::make_unique>(sec); Symbol &sym = config->adlt - ? sec.getSoExt()->getSymbol(symIndex, fromDynamic) + ? adltScanner->getSymbol(symIndex) : sec.getFile()->getSymbol(symIndex); RelType type; @@ -1488,7 +1523,9 @@ template void RelocationScanner::scanOne(RelTy *&i) { if (config->adlt) { Relocation r = {expr, type, offset, addend, &sym}; - processForADLT(rel, &r, fromDynamic); + adltScanner->process(&r); + if (adltScanner->toProcessAux) + processAux(r); return; } @@ -1795,15 +1832,24 @@ static bool handleNonPreemptibleIfunc(Symbol &sym) { return true; } -template static void addGotPltIndexAdlt(Symbol *s, bool isPlt) { +// OHOS_LOCAL begin +namespace lld { +namespace elf { +namespace adlt { +template static void addGotPltIndex(Symbol *s, bool isPlt) { auto &vec = adltCtx->gotPltInfo[s]; - for (auto &it : vec) { - auto *soFile = cast>(adltCtx->sharedFilesExtended[it]); + for (auto &orderId : vec) { + auto *soFile = adltCtx->getSoExt(orderId); auto &entries = isPlt ? in.relaPlt->relocs : mainPart->relaDyn->relocs; auto &output = isPlt ? soFile->pltRelIndexes : soFile->dynRelIndexes; output.insert(entries.size() - 1); } } +} // namespace adlt +} // namespace elf +} // namespace lld + +// OHOS_LOCAL end void elf::postScanRelocations() { auto fn = [](Symbol &sym) { @@ -1817,12 +1863,12 @@ void elf::postScanRelocations() { if (sym.needsGot) { addGotEntry(sym); if (config->adlt) - invokeELFT(addGotPltIndexAdlt, &sym, false); + invokeELFT(adlt::addGotPltIndex, &sym, false); } if (sym.needsPlt) { addPltEntry(*in.plt, *in.gotPlt, *in.relaPlt, target->pltRel, sym); if (config->adlt) - invokeELFT(addGotPltIndexAdlt, &sym, true); + invokeELFT(adlt::addGotPltIndex, &sym, true); } if (sym.needsCopy) { if (sym.isObject()) { @@ -1857,7 +1903,7 @@ void elf::postScanRelocations() { mainPart->relaDyn->addAddendOnlyRelocIfNonPreemptible( target->tlsDescRel, *in.got, in.got->getTlsDescOffset(sym), sym, target->tlsDescRel); - invokeELFT(addGotPltIndexAdlt, &sym, false); + invokeELFT(adlt::addGotPltIndex, &sym, false); } if (sym.needsTlsGd) { in.got->addDynTlsEntry(sym); -- Gitee From e1987f276ce65c447a897c06f8452dc20868165d Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Wed, 5 Jun 2024 03:53:41 -0400 Subject: [PATCH 77/77] Adlt minor refactoring changes. Change-Id: If97b6604390a24c0ffd77ef849897e2793b5ead4 Signed-off-by: Anton Volkov --- lld/ELF/Adlt.cpp | 12 ++--- lld/ELF/Adlt.h | 12 +++-- lld/ELF/Config.h | 2 +- lld/ELF/Driver.cpp | 2 +- lld/ELF/InputFiles.cpp | 63 ++++++++++++++++----------- lld/ELF/InputFiles.h | 45 +++++++++++-------- lld/ELF/Relocations.cpp | 48 ++++++++------------ lld/ELF/SyntheticSections.cpp | 49 ++++++--------------- lld/ELF/Writer.cpp | 24 +++++----- llvm-build/build.py | 10 ++--- llvm/tools/llvm-readobj/ELFDumper.cpp | 10 ----- 11 files changed, 127 insertions(+), 150 deletions(-) diff --git a/lld/ELF/Adlt.cpp b/lld/ELF/Adlt.cpp index 308706eabba8..b3d8f4f56e79 100644 --- a/lld/ELF/Adlt.cpp +++ b/lld/ELF/Adlt.cpp @@ -25,7 +25,7 @@ SharedFileExtended *AdltCtx::getSoExt(InputFile *file) { } template -SharedFileExtended *AdltCtx::getSoExt(unsigned orderId) { +SharedFileExtended *AdltCtx::getSoExt(size_t orderId) { assert(orderId < sharedFilesExtended.size()); return cast>(sharedFilesExtended[orderId]); } @@ -38,7 +38,7 @@ void AdltCtx::buildSymbolsHist(std::vector &files) { assert(!symNamesHist.empty()); } -void AdltCtx::checkDuplicatedSymbols() { +void AdltCtx::scanDuplicatedSymbols() { assert(!symNamesHist.empty()); for (auto entry : symNamesHist) if (entry.second > 1) @@ -52,10 +52,10 @@ template SharedFileExtended *AdltCtx::getSoExt(InputFile *); template SharedFileExtended *AdltCtx::getSoExt(InputFile *); template SharedFileExtended *AdltCtx::getSoExt(InputFile *); -template SharedFileExtended *AdltCtx::getSoExt(unsigned); -template SharedFileExtended *AdltCtx::getSoExt(unsigned); -template SharedFileExtended *AdltCtx::getSoExt(unsigned); -template SharedFileExtended *AdltCtx::getSoExt(unsigned); +template SharedFileExtended *AdltCtx::getSoExt(size_t); +template SharedFileExtended *AdltCtx::getSoExt(size_t); +template SharedFileExtended *AdltCtx::getSoExt(size_t); +template SharedFileExtended *AdltCtx::getSoExt(size_t); template void AdltCtx::buildSymbolsHist(std::vector &); template void AdltCtx::buildSymbolsHist(std::vector &); diff --git a/lld/ELF/Adlt.h b/lld/ELF/Adlt.h index 4fefb1ca036a..b6751cf54607 100644 --- a/lld/ELF/Adlt.h +++ b/lld/ELF/Adlt.h @@ -24,31 +24,29 @@ class InputFile; class Symbol; struct PhdrEntry; -// class SharedFileExtended; // TODO: inherit from SharedFile template class SharedFileExtended; struct PhdrEntry; class AdltCtx { public: - // TODO: inherit from SharedFile - // llvm::SmallVector sharedFilesExtended; - llvm::SmallVector sharedFilesExtended; + llvm::SmallVector sharedFilesExtended; - void checkDuplicatedSymbols(); + void scanDuplicatedSymbols(); template void buildSymbolsHist(std::vector &files); template SharedFileExtended *getSoExt(InputFile *file); - template SharedFileExtended *getSoExt(unsigned orderId); + template SharedFileExtended *getSoExt(size_t orderId); llvm::SetVector commonProgramHeaders; + bool withCfi = false; + // From input .rela.dyn, .rela.plt: // Keep input library indexes that are needed for got/plt symbol llvm::DenseMap> gotPltInfo; // sym, soFile->orderIdx array; - // Store duplicate symbols (only defined). llvm::DenseMap symNamesHist; // hash, count usages llvm::DenseSet duplicatedSymNames; diff --git a/lld/ELF/Config.h b/lld/ELF/Config.h index 001f714454bc..2eaee71cca74 100644 --- a/lld/ELF/Config.h +++ b/lld/ELF/Config.h @@ -386,7 +386,7 @@ struct DuplicateSymbol { struct Ctx { SmallVector> memoryBuffers; - SmallVector objectFiles; + SmallVector objectFiles; SmallVector sharedFiles; SmallVector binaryFiles; SmallVector bitcodeFiles; diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp index 47024766d0b9..492956ef3221 100644 --- a/lld/ELF/Driver.cpp +++ b/lld/ELF/Driver.cpp @@ -2559,7 +2559,7 @@ void LinkerDriver::link(opt::InputArgList &args) { // Fill duplicatedSymNames for defined syms. This will help to find duplicates. if (config->adlt) { invokeELFT(adltCtx->buildSymbolsHist, files); - adltCtx->checkDuplicatedSymbols(); + adltCtx->scanDuplicatedSymbols(); } // Add all files to the symbol table. This will add almost all diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp index a69d5ff11d99..c4e0962dfa83 100644 --- a/lld/ELF/InputFiles.cpp +++ b/lld/ELF/InputFiles.cpp @@ -327,8 +327,6 @@ ELFFileBase::ELFFileBase(Kind k, MemoryBufferRef mb) : InputFile(k, mb) { } } -// namespace -// { template static const Elf_Shdr *findSection(ArrayRef sections, uint32_t type) { for (const Elf_Shdr &sec : sections) @@ -336,9 +334,11 @@ static const Elf_Shdr *findSection(ArrayRef sections, uint32_t type) { return &sec; return nullptr; } -// } // namespace - +// OHOS_LOCAL begin +void ELFFileBase::buildSymbolsHist() {} +bool ELFFileBase::isValidSection(InputSectionBase *s) const { return true; } +// OHOS_LOCAL end template void ELFFileBase::init() { using Elf_Shdr = typename ELFT::Shdr; @@ -574,6 +574,7 @@ void ObjFile::initializeSections(bool ignoreComdats, } auto secName = check(obj.getSectionName(sec, shstrtab)); if (config->adlt && sec.sh_type == SHT_NULL) { + // TODO: add unique for output only auto name = getUniqueName(secName); this->sections[i] = createInputSection(i, sec, name); this->sections[i]->address = sec.sh_addr; @@ -602,6 +603,7 @@ void ObjFile::initializeSections(bool ignoreComdats, .second; if (keepGroup) { if (config->relocatable) { + // TODO: add unique for output only auto name = config->adlt ? getUniqueName(secName) : secName; this->sections[i] = createInputSection(i, sec, name); if (config->adlt) { @@ -635,6 +637,7 @@ void ObjFile::initializeSections(bool ignoreComdats, ctx->hasSympart.store(true, std::memory_order_relaxed); LLVM_FALLTHROUGH; default: + // TODO: add unique for output only auto name = config->adlt ? getUniqueName(secName) : secName; this->sections[i] = createInputSection(i, sec, name); if (config->adlt) { @@ -1057,7 +1060,12 @@ void ObjFile::initializeSymbols(const object::ELFFile &obj) { StringRef name = CHECK(eSyms[i].getName(stringTable), this); if (config->adlt && eSyms[i].isDefined() && adltCtx->duplicatedSymNames.count(CachedHashStringRef(name)) != 0) { - adltCtx->withCfi = adltCtx->withCfi || name == "__cfi_check"; + if (name == "__cfi_check" && !adltCtx->withCfi) { + adltCtx->withCfi = true; + if (config->adltTrace) + lld::outs() << "[ADLT] Cfi mode is ON\n"; + } + // TODO: add unique for output only name = this->getUniqueName(name); } symbols[i] = symtab.insert(name); @@ -1599,9 +1607,7 @@ template void SharedFile::parse() { template SharedFileExtended::SharedFileExtended(MemoryBufferRef mb, StringRef soName) - : ObjFile(mb, soName), soName(soName) { - const_cast(this->fileKind) = InputFile::SharedKind; -} + : ObjFile(InputFile::SharedKind, mb, soName), soName(soName) {} template void SharedFileExtended::buildSymbolsHist() { ArrayRef eSyms = this->template getELFSyms(); @@ -1614,7 +1620,7 @@ template void SharedFileExtended::buildSymbolsHist() { } template -bool SharedFileExtended::isValidSection(InputSectionBase *s) { +bool SharedFileExtended::isValidSection(InputSectionBase *s) const { if (!s || s == &InputSection::discarded) return false; uint32_t type = s->type; @@ -1623,17 +1629,18 @@ bool SharedFileExtended::isValidSection(InputSectionBase *s) { bool isBaseType = type == SHT_NOBITS || type == SHT_NOTE || type == SHT_INIT_ARRAY || type == SHT_FINI_ARRAY; // TODO: fix .debug-* sections relocation + // TODO: rename sections at outputStage. bool isNeededProgBits = type == SHT_PROGBITS && - !(name.startswith(".got.plt") || name.startswith(".plt") || - name.startswith(".got") || name.startswith(".eh_frame_hdr") || - name.startswith(".debug_")); + !(name.startswith(".got") || name.startswith(".plt") || + name.startswith(".eh_frame_hdr") || name.startswith(".debug_")); bool ret = isBaseType || isNeededProgBits; return ret; } template bool SharedFileExtended::isDynamicSection(InputSectionBase *s) const { + // TODO: rename sections at outputStage. return s->type == llvm::ELF::SHT_NULL || s->name.startswith(".got.plt"); } @@ -1656,7 +1663,7 @@ StringRef SharedFileExtended::getUniqueName(StringRef input) const { } template -Defined &SharedFileExtended::getDefinedLocalSym(unsigned offset) { +Defined &SharedFileExtended::getDefinedLocalSym(uint64_t offset) { auto getSym = [&](unsigned offs) { return localSymbols[this->definedSymbolsMap[offs]]; }; @@ -1670,7 +1677,7 @@ Defined &SharedFileExtended::getDefinedLocalSym(unsigned offset) { } template -InputSectionBase &SharedFileExtended::getSection(unsigned offset) { +InputSectionBase &SharedFileExtended::getSection(uint64_t offset) { auto *sec = this->sections[this->sectionsMap[offset]]; if (offset && sec->type == SHT_NULL) sec = findSection(offset); @@ -1678,7 +1685,7 @@ InputSectionBase &SharedFileExtended::getSection(unsigned offset) { } template -InputSectionBase &SharedFileExtended::getSectionByOrder(unsigned idx) { +InputSectionBase &SharedFileExtended::getSectionByOrder(size_t idx) { auto *sec = this->sections[idx]; if (!sec) sec = &InputSection::discarded; @@ -1686,35 +1693,35 @@ InputSectionBase &SharedFileExtended::getSectionByOrder(unsigned idx) { } template -InputSectionBase *SharedFileExtended::findSection(unsigned offset) { +InputSectionBase *SharedFileExtended::findSection(uint64_t offset) { llvm::SmallVector candidates; for (InputSectionBase *sec : this->sections) { if (!sec) continue; - auto low = sec->address, high = low + sec->size; + auto low = sec->address; + auto high = low + sec->size; if (offset >= low && offset < high) candidates.push_back(sec); } if (candidates.empty()) // no suitable items found return nullptr; - llvm::sort(candidates, [offset](InputSectionBase *a, InputSectionBase *b) { - return (offset - a->address < offset - b->address); - }); + llvm::sort(candidates, + [](auto *a, auto *b) { return a->address < b->address; }); return *candidates.begin(); } template -Symbol &SharedFileExtended::getDynamicSymbol(uint32_t symbolIndex) const { +Symbol &SharedFileExtended::getDynamicSymbol(size_t symbolIndex) const { return *this->symbols[symbolIndex]; } template -Symbol &SharedFileExtended::getLocalSymbol(uint32_t symbolIndex) const{ +Symbol &SharedFileExtended::getLocalSymbol(size_t symbolIndex) const{ assert(symbolIndex && (symbolIndex < localSymbols.size())); return *localSymbols[symbolIndex]; } template -Symbol &SharedFileExtended::getSymbol(uint32_t symbolIndex, +Symbol &SharedFileExtended::getSymbol(size_t symbolIndex, bool fromDynamic) { if (!symbolIndex) return *localSymbols[0]; @@ -1745,6 +1752,12 @@ Symbol &SharedFileExtended::getSymbol(uint32_t symbolIndex, return sym; } +template +ArrayRef SharedFileExtended::getLocalSymbols() { + assert(!localSymbols.empty()); + return llvm::makeArrayRef(localSymbols).slice(0, symTabFirstGlobal); +} + template void SharedFileExtended::parseDynamics() { const ELFFile obj = this->getObj(); ArrayRef sections = this->template getELFShdrs(); @@ -1843,7 +1856,7 @@ template void SharedFileExtended::parseDynamics() { // Add postfix for defined duplicates. if (config->adlt && sym.isDefined() && adltCtx->duplicatedSymNames.count(CachedHashStringRef(name)) != 0) - name = this->getUniqueName(name); + name = getUniqueName(name); // TODO: add unique for output only if (sym.getBinding() == STB_LOCAL) { warn("found local symbol '" + name + @@ -1959,7 +1972,7 @@ template void SharedFileExtended::parseElfSymTab() { }); bool isInDynSym = dynSym != this->symbols.end(); if (!isInDynSym) - name = getUniqueName(name); + name = getUniqueName(name); // TODO: add unique for output only /*if (name == "TLS_data1") // debug hint lld::outs() << name << '\n'; */ this->localSymbols[i] = diff --git a/lld/ELF/InputFiles.h b/lld/ELF/InputFiles.h index 3167372e42eb..41f1561bb545 100644 --- a/lld/ELF/InputFiles.h +++ b/lld/ELF/InputFiles.h @@ -202,8 +202,8 @@ public: } // OHOS_LOCAL begin - virtual void buildSymbolsHist() {} - virtual bool isValidSection(InputSectionBase *s) { return true; } + virtual void buildSymbolsHist(); + virtual bool isValidSection(InputSectionBase *s) const; // OHOS_LOCAL end protected: @@ -236,6 +236,10 @@ public: ObjFile(MemoryBufferRef m, StringRef archiveName) : ELFFileBase(ObjKind, m) { this->archiveName = archiveName; } + ObjFile(Kind k, MemoryBufferRef m, StringRef archiveName) // OHOS_LOCAL + : ELFFileBase(k, m) { + this->archiveName = archiveName; + } virtual ~ObjFile() {} virtual void parse(bool ignoreComdats = false); @@ -292,13 +296,15 @@ public: void initializeLocalSymbols(); virtual void postParse(); - // OHOS_LOCAL + // OHOS_LOCAL begin + // TODO: move to SharedFileExtended virtual StringRef getUniqueName(StringRef origName) const; protected: - // OHOS_LOCAL - llvm::DenseMap sectionsMap; // offset, orderIdx - llvm::DenseMap definedSymbolsMap; // abs offset, orderIdx + // TODO: move to SharedFileExtended + llvm::DenseMap sectionsMap; // offset, orderIdx + llvm::DenseMap definedSymbolsMap; // abs offset, orderIdx + // OHOS_LOCAL end private: void initializeSections(bool ignoreComdats, @@ -400,31 +406,33 @@ public: return f->kind() == InputFile::SharedKind; } void buildSymbolsHist() override; - bool isValidSection(InputSectionBase *s) override; + bool isValidSection(InputSectionBase *s) const override; bool isDynamicSection(InputSectionBase *s) const; void parse(bool ignoreComdats = false) override; void postParse() override; - Defined &getDefinedLocalSym(unsigned offset); + Defined &getDefinedLocalSym(uint64_t offset); - InputSectionBase &getSection(unsigned offset); - InputSectionBase &getSectionByOrder(unsigned idx); - InputSectionBase *findSection(unsigned offset); + InputSectionBase &getSection(uint64_t offset); + InputSectionBase &getSectionByOrder(size_t idx); + InputSectionBase *findSection(uint64_t offset); template Symbol &getRelocTargetSym(const RelT &rel) { uint32_t symIndex = rel.getSymbol(config->isMips64EL); return getSymbol(symIndex, false); } - Symbol &getDynamicSymbol(uint32_t symbolIndex) const; - Symbol &getLocalSymbol(uint32_t symbolIndex) const; - Symbol &getSymbol(uint32_t symbolIndex, bool fromDynamic); + Symbol &getDynamicSymbol(size_t symbolIndex) const; + Symbol &getLocalSymbol(size_t symbolIndex) const; + Symbol &getSymbol(size_t symbolIndex, bool fromDynamic); + + ArrayRef getLocalSymbols() override; public: // the input order of the file as it presented in ADLT image - size_t orderIdx; + size_t orderIdx = 0; int dynSymSecIdx = 0; int symTabSecIdx = 0; int symTabShndxSecIdx = 0; @@ -440,8 +448,10 @@ public: llvm::SetVector programHeaders; // From input .rela.dyn, .rela.plt: - llvm::SetVector dynRelIndexes; - llvm::SetVector pltRelIndexes; + llvm::SetVector + relaDynIndexes; // If not .relr.dyn exists, contains only Got/Abs/Tls + // relocs. Otherwise contains relative relocs also. + llvm::SetVector relaPltIndexes; // .got.plt relocs // SharedFile compability layer: // This is actually a vector of Elf_Verdef pointers. @@ -460,6 +470,7 @@ public: // parsed. Only filled for `--no-allow-shlib-undefined`. SmallVector requiredSymbols; + // TODO: add unique for output only StringRef getUniqueName(StringRef origName) const override; private: diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp index 7d0e5ac30c74..2a40434722d6 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -449,21 +449,19 @@ public: SharedFileExtended *getSoExt() { return file; } - Symbol &getSymbol(uint32_t idx) { return file->getSymbol(idx, fromDynamic); } + Symbol &getSymbol(unsigned idx) { return file->getSymbol(idx, fromDynamic); } void process(Relocation *r); void trackGotPlt(Symbol *sym); - void trackDynReloc(); + void trackRelatives(); + void trackDynamics(); - unsigned getDynamicRelocsCount(); - bool isRelr() { return config->adlt && relSecType == SHT_RELR; } bool toProcessAux = false; private: SharedFileExtended *file = nullptr; bool fromDynamic = false; InputSectionBase &sec; - uint32_t relSecType = SHT_NULL; }; template @@ -471,16 +469,14 @@ void AdltRelocationScanner::trackGotPlt(Symbol *sym) { adltCtx->gotPltInfo[sym].push_back(file->orderIdx); } -template -void AdltRelocationScanner::trackDynReloc() { - file->dynRelIndexes.insert(getDynamicRelocsCount() - 1); +template void AdltRelocationScanner::trackRelatives() { + auto count = mainPart->relaDyn->relocs.size(); + file->relaDynIndexes.insert(count - 1); } -template -unsigned AdltRelocationScanner::getDynamicRelocsCount() { - auto currentSize = isRelr() ? mainPart->relrDyn->relocs.size() - : mainPart->relaDyn->relocs.size(); - return currentSize; +template void AdltRelocationScanner::trackDynamics() { + auto count = mainPart->relaDyn->relocs.size(); + file->relaDynIndexes.insert(count - 1); } // OHOS_LOCAL end @@ -513,10 +509,6 @@ private: void processAux(RelExpr expr, RelType type, uint64_t offset, Symbol &sym, int64_t addend) const; template void scanOne(RelTy *&i); - - // ADLT - template - void processForADLT(const RelTy &rel, Relocation *r, bool fromDynamic); }; } // namespace @@ -1389,9 +1381,8 @@ void AdltRelocationScanner::process(Relocation *r) { r->sym = d; r->addend -= d->section->address + d->value; addRelativeReloc(secWhere, r->offset, *r->sym, r->addend, r->expr, - r->type); - assert(getDynamicRelocsCount()); - trackDynReloc(); + r->type); + trackRelatives(); return; } case R_AARCH64_GLOB_DAT: @@ -1416,7 +1407,7 @@ void AdltRelocationScanner::process(Relocation *r) { sec.getPartition().relaDyn->addSymbolReloc(r->type, secWhere, r->offset, *r->sym, r->addend, r->type); - trackDynReloc(); + trackDynamics(); return; } sec.relocations.push_back(*r); @@ -1483,10 +1474,10 @@ void AdltRelocationScanner::process(Relocation *r) { template void RelocationScanner::scanOne(RelTy *&i) { const RelTy &rel = *i; - uint32_t symIndex = rel.getSymbol(config->isMips64EL); std::unique_ptr> adltScanner = nullptr; if (config->adlt) adltScanner = std::make_unique>(sec); + uint32_t symIndex = rel.getSymbol(config->isMips64EL); Symbol &sym = config->adlt ? adltScanner->getSymbol(symIndex) : sec.getFile()->getSymbol(symIndex); @@ -1741,11 +1732,6 @@ void RelocationScanner::scan(ArrayRef rels) { template void elf::scanRelocations(InputSectionBase &s) { RelocationScanner scanner(s); - if (config->adlt) { - bool isDebug = false; - if (isDebug) - lld::outs() << s.file->getName().str() + ": " + s.name.str() + '\n'; - } const RelsOrRelas rels = s.template relsOrRelas(); if (rels.areRelocsRel()) scanner.template scan(rels.rels); @@ -1841,8 +1827,9 @@ template static void addGotPltIndex(Symbol *s, bool isPlt) { for (auto &orderId : vec) { auto *soFile = adltCtx->getSoExt(orderId); auto &entries = isPlt ? in.relaPlt->relocs : mainPart->relaDyn->relocs; - auto &output = isPlt ? soFile->pltRelIndexes : soFile->dynRelIndexes; - output.insert(entries.size() - 1); + auto &output = isPlt ? soFile->relaPltIndexes : soFile->relaDynIndexes; + auto index = entries.size() - 1; + output.insert(index); } } } // namespace adlt @@ -1903,7 +1890,8 @@ void elf::postScanRelocations() { mainPart->relaDyn->addAddendOnlyRelocIfNonPreemptible( target->tlsDescRel, *in.got, in.got->getTlsDescOffset(sym), sym, target->tlsDescRel); - invokeELFT(adlt::addGotPltIndex, &sym, false); + if (config->adlt) + invokeELFT(adlt::addGotPltIndex, &sym, false); } if (sym.needsTlsGd) { in.got->addDynTlsEntry(sym); diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp index 5a4abd964dbf..491a61dd15da 100644 --- a/lld/ELF/SyntheticSections.cpp +++ b/lld/ELF/SyntheticSections.cpp @@ -1551,40 +1551,17 @@ DynamicSection::computeContents() { addInSec(DT_HASH, *part.hashTab); if (isMain) { - if (config->adlt) { - auto 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) - return &osd->osec; - return (OutputSection *)nullptr; - }; - for (InputFile *file : adltCtx->sharedFilesExtended) { - auto *f = adltCtx->getSoExt(file); - auto initArray = findSection(f->getUniqueName(".init_array")); - auto finiArray = findSection(f->getUniqueName(".fini_array")); - if (initArray) { - addInt(DT_INIT_ARRAY, initArray->addr); - addInt(DT_INIT_ARRAYSZ, initArray->size); - } - if (finiArray) { - addInt(DT_FINI_ARRAY, finiArray->addr); - addInt(DT_FINI_ARRAYSZ, finiArray->size); - } - } - } else { - if (Out::preinitArray) { - addInt(DT_PREINIT_ARRAY, Out::preinitArray->addr); - addInt(DT_PREINIT_ARRAYSZ, Out::preinitArray->size); - } - if (Out::initArray) { - addInt(DT_INIT_ARRAY, Out::initArray->addr); - addInt(DT_INIT_ARRAYSZ, Out::initArray->size); - } - if (Out::finiArray) { - addInt(DT_FINI_ARRAY, Out::finiArray->addr); - addInt(DT_FINI_ARRAYSZ, Out::finiArray->size); - } + if (Out::preinitArray) { + addInt(DT_PREINIT_ARRAY, Out::preinitArray->addr); + addInt(DT_PREINIT_ARRAYSZ, Out::preinitArray->size); + } + if (Out::initArray) { + addInt(DT_INIT_ARRAY, Out::initArray->addr); + addInt(DT_INIT_ARRAYSZ, Out::initArray->size); + } + if (Out::finiArray) { + addInt(DT_FINI_ARRAY, Out::finiArray->addr); + addInt(DT_FINI_ARRAYSZ, Out::finiArray->size); } if (Symbol *b = symtab->find(config->init)) @@ -4224,8 +4201,8 @@ AdltSection::makeSoData(const SharedFileExtended* soext) { data.initArrayName = soext->getUniqueName(".init_array"); data.finiArrayName = soext->getUniqueName(".fini_array"); - data.relaDynIndx = soext->dynRelIndexes.getArrayRef(); - data.relaPltIndx = soext->pltRelIndexes.getArrayRef(); + data.relaDynIndx = soext->relaDynIndexes.getArrayRef(); + data.relaPltIndx = soext->relaPltIndexes.getArrayRef(); data.programHeadersAllocated = soext->programHeaders.size(); data.programHeaders = soext->programHeaders.getArrayRef(); diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index 2c36a542a244..93fd33ccb2b9 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -315,6 +315,7 @@ template void elf::createSyntheticSections() { } if (config->adlt) { + assert(adltCtx && "ADLT: ctx not initialized!"); in.adltStrTab = std::make_unique(".adlt.strtab", false); in.adltData = std::make_unique>(*in.adltStrTab); add(*in.adltStrTab); @@ -571,6 +572,8 @@ InputSection *AdltWriter::getInputSection(OutputSection *sec) { } template void AdltWriter::checkPhdrs() { + assert(!adltCtx->commonProgramHeaders.empty() && + "Common headers can't be empty!"); for (auto *file : adltCtx->sharedFilesExtended) if (auto *soFile = adltCtx->getSoExt(file)) assert(!soFile->programHeaders.empty() && @@ -583,9 +586,9 @@ template void AdltWriter::checkRelocs() { for (auto *file : adltCtx->sharedFilesExtended) if (auto *soFile = adltCtx->getSoExt(file)) { - assert(!soFile->dynRelIndexes.empty() && + assert(!soFile->relaDynIndexes.empty() && "ADLT: relaDyn indexes can't be empty!"); - assert(!soFile->pltRelIndexes.empty() && + assert(!soFile->relaPltIndexes.empty() && "ADLT: relaPlt indexes can't be empty!"); } } @@ -602,7 +605,8 @@ void AdltWriter::trackPhdr(OutputSection *sec, PhdrEntry *phdr) { template void AdltWriter::traceRelocs() { lld::outs() << "[ADLT]\n"; - lld::outs() << "Dyn relocs (" << mainPart->relaDyn->relocs.size() << ")\n"; + auto dynRelsSize = mainPart->relaDyn->relocs.size(); + lld::outs() << "Dyn relocs (" << dynRelsSize << ")\n"; lld::outs() << "Plt relocs (" << in.relaPlt->relocs.size() << ")\n"; auto printIndexes = [&](auto &vec) { @@ -620,7 +624,7 @@ template void AdltWriter::traceRelocs() { return sym->getName(); }; - auto printRelocTable = [&](auto *relSec, auto &outIndexes) { + auto printRelaTable = [&](auto *relSec, auto &outIndexes) { for (auto &it : outIndexes) // print relocs if (DynamicReloc *rel = &relSec->relocs[it]) lld::outs() << it << ":\t" << rel->inputSec->name << " + 0x" @@ -632,13 +636,13 @@ template void AdltWriter::traceRelocs() { for (auto *file : adltCtx->sharedFilesExtended) if (auto *soFile = adltCtx->getSoExt(file)) { lld::outs() << soFile->soName << ":\n"; - lld::outs() << "Dyn relocs (" << soFile->dynRelIndexes.size() << ")"; - printIndexes(soFile->dynRelIndexes); - printRelocTable(mainPart->relaDyn.get(), soFile->dynRelIndexes); + lld::outs() << ".rela.dyn relocs (" << soFile->relaDynIndexes.size() << ")"; + printIndexes(soFile->relaDynIndexes); + printRelaTable(mainPart->relaDyn.get(), soFile->relaDynIndexes); - lld::outs() << "Plt relocs (" << soFile->pltRelIndexes.size() << ")"; - printIndexes(soFile->pltRelIndexes); - printRelocTable(in.relaPlt.get(), soFile->pltRelIndexes); + lld::outs() << ".rela.plt relocs (" << soFile->relaPltIndexes.size() << ")"; + printIndexes(soFile->relaPltIndexes); + printRelaTable(in.relaPlt.get(), soFile->relaPltIndexes); } } diff --git a/llvm-build/build.py b/llvm-build/build.py index 19247a79c812..d18201444e0e 100755 --- a/llvm-build/build.py +++ b/llvm-build/build.py @@ -310,12 +310,6 @@ class BuildConfig(): default=False, help='Append -g to build flags in build_libs') - parser.add_argument( - '--adlt-debug-build', - action='store_true', - default=False, - help='Build adlt with debug flags') - parser.add_argument( '--enable-check-abi', nargs='?', @@ -569,7 +563,9 @@ class BuildUtils(object): """subprocess.check_call with logging.""" self.logger().info('check_call:%s %s', datetime.datetime.now().strftime("%H:%M:%S"), subprocess.list2cmdline(cmd)) - + print("=======cmd: " + str(cmd) + "\n") + # print("=======args: " + args + "\n") + # print("=======kwargs: " + kwargs + "\n") subprocess.check_call(cmd, *args, **kwargs) @staticmethod diff --git a/llvm/tools/llvm-readobj/ELFDumper.cpp b/llvm/tools/llvm-readobj/ELFDumper.cpp index dea84138ddd5..cf69601eab93 100644 --- a/llvm/tools/llvm-readobj/ELFDumper.cpp +++ b/llvm/tools/llvm-readobj/ELFDumper.cpp @@ -7059,16 +7059,6 @@ template void LLVMELFDumper::printAdltSection() { return this->reportUniqueWarning("invalid .adlt section: " "blob is out of section range"); - ArrayRef psodsRaw; - ArrayRef blob; - - if (psodsRaw.data() + psodsRaw.size() > blob.data()) - return this->reportUniqueWarning("invalid .adlt section: " - "PSOD and blob entries are overlapped"); - if (blob.data() + blob.size() > adltRaw.data() + adltRaw.size()) - return this->reportUniqueWarning("invalid .adlt section: " - "blob is out of section range"); - DictScope DSec(W, "ADLT"); do { -- Gitee