From 4ecafd8d6bd0429fe7c929d0a1d8aad2fefe0098 Mon Sep 17 00:00:00 2001 From: Anton Date: Fri, 15 Dec 2023 17:18:07 +0300 Subject: [PATCH 01/82] LLD Adlt --- .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 abcc8a984a65..b3a387c160d9 100644 --- a/lld/ELF/Driver.cpp +++ b/lld/ELF/Driver.cpp @@ -274,8 +274,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)); @@ -843,7 +847,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; @@ -922,6 +927,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)) @@ -1086,6 +1092,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); @@ -1101,7 +1108,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( @@ -1217,7 +1224,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); @@ -1818,7 +1825,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) @@ -2014,7 +2022,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()) { @@ -2090,7 +2099,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) { @@ -2344,7 +2354,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; @@ -2379,7 +2390,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( @@ -2462,6 +2474,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) { @@ -2531,8 +2583,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. @@ -2587,6 +2640,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, @@ -2654,6 +2710,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, @@ -2670,6 +2727,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); @@ -2693,6 +2755,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 473809b05e9c..ae24890ea57c 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; @@ -1733,6 +2239,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; @@ -1785,3 +2312,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 8fe36eca6a4b..8e772a6a683e 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); @@ -844,7 +845,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; @@ -948,7 +961,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 277c57505bb2..6ef2dd6187c5 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -455,6 +455,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 @@ -1294,7 +1297,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. @@ -1312,11 +1316,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. @@ -1326,6 +1405,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. @@ -1628,7 +1778,8 @@ void elf::postScanRelocations() { return; if (!sym.needsDynReloc()) return; - sym.allocateAux(); + if (!sym.allocateAux()) + return; if (sym.needsGot) addGotEntry(sym); @@ -1720,7 +1871,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); } @@ -2168,7 +2320,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 5ec2d85f64e8..4ed885c2edd4 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 94b665aa757342d3c0e23059becb2a9a927b8777 Mon Sep 17 00:00:00 2001 From: Khomutov Nikita Date: Wed, 20 Dec 2023 15:11:41 +0300 Subject: [PATCH 02/82] force debug for active development --- llvm-build/Makefile | 6 +++--- llvm-build/build.py | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/llvm-build/Makefile b/llvm-build/Makefile index ecb982228b0e..bc63938d787d 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)/../../../.. @@ -92,7 +92,7 @@ endif endif ifeq ($(ARCH),aarch64) -CFLAGS = -march=armv8 -O2 -Wall -fstack-protector-strong -D_FORTIFY_SOURCE=2 -Wl,-z,relro,-z,now,-z,noexecstack +CFLAGS = -march=armv8 -g -gdwarf-4 -O0 -Wall -fstack-protector-strong -D_FORTIFY_SOURCE=2 -Wl,-z,relro,-z,now,-z,noexecstack else ifeq ($(ARCH),riscv64) CFLAGS = -march=rv64gc -O2 -Wall -fstack-protector-strong -D_FORTIFY_SOURCE=2 -Wl,-z,relro,-z,now,-z,noexecstack @@ -225,7 +225,7 @@ musl_install_for_linux_user: musl_patch_for_linux_user make -sj install clean: - $(HIDE) rm -rf musl_copy_for_* linux_header_install_for_* + # $(HIDE) rm -rf musl_copy_for_* linux_header_install_for_* distclean: clean $(HIDE) rm -rf $(SYSROOTDIR)/lib $(SYSROOTDIR)/usr diff --git a/llvm-build/build.py b/llvm-build/build.py index 98a9a9f9475e..0307d70af634 100755 --- a/llvm-build/build.py +++ b/llvm-build/build.py @@ -1221,7 +1221,7 @@ class LlvmLibs(BuildUtils): defines = self.base_cmake_defines() ldflags = [] - cflags = [] + cflags = ["-g -gdwarf-4 -O0"] self.logger().info('Build libs for %s', llvm_triple) if self.build_config.target_debug: defines['CMAKE_BUILD_TYPE'] = 'Debug' -- Gitee From 08931da40b67b5592ccbf57708ccb7fe1e4782d8 Mon Sep 17 00:00:00 2001 From: peshkovivan Date: Wed, 27 Dec 2023 17:31:42 +0300 Subject: [PATCH 03/82] Add option to enable debug build for adlt Signed-off-by: peshkovivan --- llvm-build/Makefile | 10 ++++++++-- llvm-build/build.py | 11 ++++++++++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/llvm-build/Makefile b/llvm-build/Makefile index bc63938d787d..79c3c413ccb9 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)/../../../.. @@ -92,8 +92,12 @@ endif endif ifeq ($(ARCH),aarch64) +ifeq ($(BUILD_DEBUG),true) CFLAGS = -march=armv8 -g -gdwarf-4 -O0 -Wall -fstack-protector-strong -D_FORTIFY_SOURCE=2 -Wl,-z,relro,-z,now,-z,noexecstack else +CFLAGS = -march=armv8 -O2 -Wall -fstack-protector-strong -D_FORTIFY_SOURCE=2 -Wl,-z,relro,-z,now,-z,noexecstack +endif +else ifeq ($(ARCH),riscv64) CFLAGS = -march=rv64gc -O2 -Wall -fstack-protector-strong -D_FORTIFY_SOURCE=2 -Wl,-z,relro,-z,now,-z,noexecstack else @@ -225,7 +229,9 @@ musl_install_for_linux_user: musl_patch_for_linux_user make -sj install clean: - # $(HIDE) rm -rf musl_copy_for_* linux_header_install_for_* + ifeq ($(BUILD_DEBUG),false) + $(HIDE) rm -rf musl_copy_for_* linux_header_install_for_* + endif distclean: clean $(HIDE) rm -rf $(SYSROOTDIR)/lib $(SYSROOTDIR)/usr diff --git a/llvm-build/build.py b/llvm-build/build.py index 0307d70af634..a595a11c64c4 100755 --- a/llvm-build/build.py +++ b/llvm-build/build.py @@ -66,6 +66,7 @@ class BuildConfig(): self.enable_monitoring = args.enable_monitoring self.build_libs = args.build_libs self.build_libs_flags = args.build_libs_flags + self.adlt_debug_build = args.adlt_debug_build self.discover_paths() @@ -230,6 +231,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') def parse_args(self): @@ -1221,7 +1228,9 @@ class LlvmLibs(BuildUtils): defines = self.base_cmake_defines() ldflags = [] - cflags = ["-g -gdwarf-4 -O0"] + cflags = [] + if self.build_config.adlt_debug_build: + cflags.append("-g -gdwarf-4 -O0") self.logger().info('Build libs for %s', llvm_triple) if self.build_config.target_debug: defines['CMAKE_BUILD_TYPE'] = 'Debug' -- Gitee From fd54c970658351ce4aac604c080de77d9c2fe519 Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Wed, 27 Dec 2023 13:39:14 -0500 Subject: [PATCH 04/82] 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 ae24890ea57c..ef9e1cfc030d 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 6ef2dd6187c5..a0ef192d6711 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -1433,13 +1433,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}); }; @@ -1451,7 +1460,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 @@ -1686,6 +1699,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 25339f6101efd223e70d90762ca7df1416419523 Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Mon, 15 Jan 2024 03:53:34 -0500 Subject: [PATCH 05/82] Fix not found sym Change-Id: Ibd2885c74ca48ec62ef0354782d72ee79c1ee014 --- 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 ef9e1cfc030d..65c51fd7d59b 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 a0ef192d6711..0b6bc74bbec3 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -1325,7 +1325,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)); @@ -1341,31 +1341,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); @@ -1453,6 +1446,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: @@ -1470,7 +1465,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 7dd56417fa85f666d2b3ebcbef363aa9fe872968 Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Thu, 18 Jan 2024 04:27:29 -0500 Subject: [PATCH 06/82] 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 65c51fd7d59b..e08eab190715 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(); @@ -2327,4 +2313,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 0b6bc74bbec3..278c4927bccf 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -1338,34 +1338,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: @@ -1455,6 +1455,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: @@ -1473,6 +1474,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 f1f32ed906af301cd9f25d3795861fa7e222cc2d Mon Sep 17 00:00:00 2001 From: liuyaning Date: Thu, 18 Jan 2024 12:03:55 +0800 Subject: [PATCH 07/82] Update gn version Update gn version to 20240115 Issue:https://gitee.com/openharmony/third_party_llvm-project/issues/I8X469 Test:prebuilts_download Signed-off-by: liuyaning --- llvm-build/env_prepare.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/llvm-build/env_prepare.sh b/llvm-build/env_prepare.sh index c9996de3b34a..2557ece0be32 100755 --- a/llvm-build/env_prepare.sh +++ b/llvm-build/env_prepare.sh @@ -67,7 +67,7 @@ copy_config_linux_x86_64=""" prebuilts/cmake,https://mirrors.huaweicloud.com/harmonyos/compiler/cmake/3.16.5/${host_platform}/cmake-${host_platform}-x86-3.16.5.tar.gz prebuilts/clang/ohos/${host_platform}-${host_cpu},https://mirrors.huaweicloud.com/openharmony/compiler/clang/${CLANG_PACKAGE_VERSION}/linux/${CLANG_LINUX_BUILD}.tar.bz2 prebuilts/python3,https://mirrors.huaweicloud.com/harmonyos/compiler/python/3.10.2/${host_platform}/python-${host_platform}-x86-3.10.2_20230604.tar.gz -prebuilts/build-tools/${host_platform}-x86/bin,https://repo.huaweicloud.com/openharmony/compiler/gn/2024/linux/gn-linux-x86-20230426.tar.gz +prebuilts/build-tools/${host_platform}-x86/bin,https://repo.huaweicloud.com/openharmony/compiler/gn/20240115/linux/gn-linux-x86-20240115.tar.gz prebuilts/build-tools/${host_platform}-x86/bin,https://repo.huaweicloud.com/openharmony/compiler/ninja/1.11.0/linux/ninja-linux-x86-1.11.0.tar.gz """ @@ -77,7 +77,7 @@ copy_config_darwin_x86_64=""" prebuilts/cmake,https://mirrors.huaweicloud.com/harmonyos/compiler/cmake/3.16.5/${host_platform}/cmake-${host_platform}-x86-3.16.5.tar.gz prebuilts/clang/ohos/${host_platform}-${host_cpu},http://mirrors.huaweicloud.com/harmonyos/compiler/clang/15.0.4-8e906c/darwin/${CLANG_DARWIN_BUILD}.tar.bz2 prebuilts/python3,https://mirrors.huaweicloud.com/harmonyos/compiler/python/3.10.2/${host_platform}/python-${host_platform}-x86-3.10.2_20230604.tar.gz -prebuilts/build-tools/${host_platform}-x86/bin,https://repo.huaweicloud.com/openharmony/compiler/gn/2024/darwin/gn-darwin-x86-20230425.tar.gz +prebuilts/build-tools/${host_platform}-x86/bin,https://repo.huaweicloud.com/openharmony/compiler/gn/20240115/darwin/gn-darwin-x86-20240115.tar.gz prebuilts/build-tools/${host_platform}-x86/bin,https://repo.huaweicloud.com/openharmony/compiler/ninja/1.11.0/darwin/ninja-darwin-x86-1.11.0.tar.gz """ -- Gitee From d29823f0529b893b3d54f4ec37b516d468c92a15 Mon Sep 17 00:00:00 2001 From: Olshevsky Vladimir Date: Fri, 19 Jan 2024 15:21:30 +0300 Subject: [PATCH 08/82] [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 e08eab190715..ffd2511d0b1c 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 5c36934f74cc05cbe989b335685708ac2d6ba3c8 Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Thu, 25 Jan 2024 09:40:52 -0500 Subject: [PATCH 09/82] Gen .data.rel.ro. Fix nop. Minor changes. Change-Id: Iddd207437d8f65d29953ec347806ce7507244b7b Signed-off-by: Anton Volkov --- lld/ELF/Arch/AArch64.cpp | 2 ++ lld/ELF/InputFiles.cpp | 9 ++++++--- lld/ELF/InputFiles.h | 4 ++-- lld/ELF/LinkerScript.cpp | 4 +++- lld/ELF/Relocations.cpp | 14 ++++++++++---- lld/ELF/SyntheticSections.cpp | 4 ++-- lld/ELF/Writer.cpp | 10 +++++++++- 7 files changed, 34 insertions(+), 13 deletions(-) diff --git a/lld/ELF/Arch/AArch64.cpp b/lld/ELF/Arch/AArch64.cpp index 7274232d1469..4c3e5aff9ef2 100644 --- a/lld/ELF/Arch/AArch64.cpp +++ b/lld/ELF/Arch/AArch64.cpp @@ -455,6 +455,8 @@ void AArch64::relocate(uint8_t *loc, const Relocation &rel, checkInt(loc, val, 33, rel); LLVM_FALLTHROUGH; case R_AARCH64_ADR_PREL_PG_HI21_NC: + if (config->adlt && *(uint32_t*)loc == 0xd503201f) // ignore nop + break; write32AArch64Addr(loc, val >> 12); break; case R_AARCH64_ADR_PREL_LO21: diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp index ffd2511d0b1c..f4f8b81b018f 100644 --- a/lld/ELF/InputFiles.cpp +++ b/lld/ELF/InputFiles.cpp @@ -1667,7 +1667,8 @@ template void SharedFileExtended::parseForAdlt() { lld::outs() << "Parse offsets of some sections:\n"; for (const Elf_Shdr &sec : objSections) { auto name = check(obj.getSectionName(sec, shstrtab)); - if (name == ".init_array" || name == ".fini_array" || name == ".data") + if (name == ".init_array" || name == ".fini_array" || name == ".data" || + name == ".data.rel.ro" || name == ".bss.rel.ro" || name == ".bss") traceElfSection(sec); } lld::outs() << '\n'; @@ -1680,12 +1681,12 @@ template void SharedFileExtended::postParseForAdlt() { } template -StringRef SharedFileExtended::addAdltPrefix(StringRef input) { +StringRef SharedFileExtended::addAdltPostfix(StringRef input) { return markItemForAdlt(input, soName); } template -bool SharedFileExtended::addAdltPrefix(Symbol *s) { +bool SharedFileExtended::addAdltPostfix(Symbol *s) { StringRef newName = markItemForAdlt(s->getName(), soName); if (s->getName() == newName) return false; @@ -2007,6 +2008,8 @@ template void SharedFileExtended::parseElfSymTab() { const Elf_Shdr *eSec = *p; InputSectionBase *sec = this->sections[secIdx]; auto val = eSym.st_value - eSec->sh_addr; + if (name.startswith("__")) + name = addAdltPostfix(name); this->allSymbols[i] = make(this, name, bind, other, type, val, eSym.st_size, sec); } else { diff --git a/lld/ELF/InputFiles.h b/lld/ELF/InputFiles.h index 5207ef34344c..821e5f369db3 100644 --- a/lld/ELF/InputFiles.h +++ b/lld/ELF/InputFiles.h @@ -399,8 +399,8 @@ public: void parseForAdlt(); void postParseForAdlt(); - StringRef addAdltPrefix(StringRef input); - bool addAdltPrefix(Symbol *s); + StringRef addAdltPostfix(StringRef input); + bool addAdltPostfix(Symbol *s); Defined *findSectionSymbol(uint64_t offset) const; Defined *findDefinedSymbol( diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp index 51c505d159d5..68729e4ebc4b 100644 --- a/lld/ELF/LinkerScript.cpp +++ b/lld/ELF/LinkerScript.cpp @@ -104,7 +104,9 @@ static StringRef getOutputSectionName(const InputSectionBase *s) { {".data.rel.ro", ".data", ".rodata", ".bss.rel.ro", ".bss", ".gcc_except_table", ".init_array", ".fini_array", ".tbss", ".tdata", ".ARM.exidx", ".ARM.extab", ".ctors", ".dtors", ".ohos.randomdata"}) // OHOS_LOCAL - if (isSectionPrefix(v, s->name)) + if (config->adlt && s->name.startswith(v)) + return s->name; + else if (isSectionPrefix(v, s->name)) return v; return s->name; diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp index 278c4927bccf..de31d8cc2af4 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -1427,13 +1427,19 @@ template void RelocationScanner::scanOne(RelTy *&i) { Symbol *s = &sym; if (!(expr == R_PLT || expr == R_PLT_PC || expr == R_GOT || expr == R_GOT_PC)) - if (Symbol *tmp = &file->getSymbolFromElfSymTab(symIndex)) - if (tmp->getName() != s->getName()) - s = tmp; + if (Symbol *tmp = &file->getSymbolFromElfSymTab(symIndex)) { + if (isDebug) + traceSym(debugTitle + "changed to tmp: ", *tmp); + s = tmp; + } s = sym.getName().size() ? elf::symtab->find(sym.getName()) : &sym; - if (!s) // if symbol was not found in elf::symtab + if (!s) { // if symbol was not found in elf::symtab s = &sym; + in.symTab->addSymbol(s); + } + if (isDebug) + traceSym(debugTitle + "before push: ", *s); if (s->isUndefined()) { if (expr == R_PLT || expr == R_PLT_PC) diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp index 8a7208698c6d..29135ddb16d7 100644 --- a/lld/ELF/SyntheticSections.cpp +++ b/lld/ELF/SyntheticSections.cpp @@ -1560,8 +1560,8 @@ DynamicSection::computeContents() { }; for (InputFile *file : ctx->sharedFilesExtended) { auto *f = cast>(file); - auto initArray = findSection(f->addAdltPrefix(".init_array")); - auto finiArray = findSection(f->addAdltPrefix(".fini_array")); + auto initArray = findSection(f->addAdltPostfix(".init_array")); + auto finiArray = findSection(f->addAdltPostfix(".fini_array")); if (initArray) { addInt(DT_INIT_ARRAY, initArray->addr); addInt(DT_INIT_ARRAYSZ, initArray->size); diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index a3db65a4732c..de16dec8e609 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -273,7 +273,9 @@ void elf::addReservedSymbols() { static OutputSection *findSection(StringRef name, unsigned partition = 1) { for (SectionCommand *cmd : script->sectionCommands) if (auto *osd = dyn_cast(cmd)) - if (osd->osec.name == name && osd->osec.partition == partition) + if ((config->adlt ? osd->osec.name.startswith(name) + : osd->osec.name == name) && + osd->osec.partition == partition) return &osd->osec; return nullptr; } @@ -831,6 +833,12 @@ static bool isRelroSection(const OutputSection *sec) { // ELF in spirit. But in reality many linker features depend on // magic section names. StringRef s = sec->name; + if (config->adlt) + return s.startswith(".data.rel.ro") || s.startswith(".bss.rel.ro") || + s.startswith(".ctors") || s.startswith(".dtors") || + s.startswith(".eh_frame") || s.startswith(".fini_array") || + s.startswith(".init_array") || s.startswith(".preinit_array"); + return s == ".data.rel.ro" || s == ".bss.rel.ro" || s == ".ctors" || s == ".dtors" || s == ".jcr" || s == ".eh_frame" || s == ".fini_array" || s == ".init_array" || -- Gitee From 98978d019323a3c1d41dc83e08fdde2cb1ea3318 Mon Sep 17 00:00:00 2001 From: Olshevsky Vladimir Date: Fri, 26 Jan 2024 11:28:23 +0000 Subject: [PATCH 10/82] Added option and function to build gtest libraries needed by llvm-adlt Signed-off-by: Olshevsky Vladimir Change-Id: I6392c6cc40fe1aae754dbf6503f3625f8c8642de --- llvm-build/build.py | 64 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) diff --git a/llvm-build/build.py b/llvm-build/build.py index ebca2328c0c2..8b1939989a99 100755 --- a/llvm-build/build.py +++ b/llvm-build/build.py @@ -47,6 +47,7 @@ class BuildConfig(): self.build_instrumented = args.build_instrumented self.xunit_xml_output = args.xunit_xml_output self.enable_assertions = args.enable_assertions + self.build_gtest_libs = args.build_gtest_libs self.build_clean = args.build_clean self.need_libs = self.do_build and 'libs' not in args.no_build self.need_lldb_server = self.do_build and 'lldb-server' not in args.no_build @@ -114,6 +115,12 @@ class BuildConfig(): default=False, help='Apply assertions, some parameters are affected.') + parser.add_argument( + '--build-gtest-libs', + action='store_true', + default=False, + help='Build gtest libraries.') + parser.add_argument( '--build-name', default='dev', @@ -1722,6 +1729,60 @@ class LlvmLibs(BuildUtils): self.llvm_package.copy_libedit_to_llvm(llvm_make) self.llvm_package.copy_libedit_to_llvm(llvm_install) + 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' @@ -2320,7 +2381,6 @@ class LlvmPackage(BuildUtils): return - def main(): build_config = BuildConfig() build_utils = BuildUtils(build_config) @@ -2420,6 +2480,8 @@ def main(): build_config.enable_assertions, build_config.build_name) + if build_config.build_gtest_libs: + llvm_libs.build_gtest(llvm_install) if build_config.do_package: if build_utils.host_is_linux(): -- Gitee From 670caee530a76ea02f2958337bdac8bb24f50210 Mon Sep 17 00:00:00 2001 From: Likholatov Evgeny Date: Thu, 8 Feb 2024 16:29:28 +0300 Subject: [PATCH 11/82] Added --build-only flag. Signed-off-by: Likholatov Evgeny --- llvm-build/build.py | 125 ++++++++++++++++++++++++++++++++------ llvm-build/env_prepare.sh | 2 + 2 files changed, 110 insertions(+), 17 deletions(-) diff --git a/llvm-build/build.py b/llvm-build/build.py index 8b1939989a99..49e06952fc0e 100755 --- a/llvm-build/build.py +++ b/llvm-build/build.py @@ -38,7 +38,7 @@ class BuildConfig(): args = self.parse_args() self.no_strip_libs = args.no_strip_libs self.do_build = not args.skip_build - self.do_package = not args.skip_package + self.do_package = not args.skip_package and not args.build_only self.build_name = args.build_name self.debug = args.debug self.target_debug = args.target_debug @@ -50,9 +50,13 @@ class BuildConfig(): self.build_gtest_libs = args.build_gtest_libs self.build_clean = args.build_clean self.need_libs = self.do_build and 'libs' not in args.no_build - self.need_lldb_server = self.do_build and 'lldb-server' not in args.no_build + self.need_lldb_server = self.do_build and 'lldb-server' not in args.no_build and not args.build_only self.build_python = args.build_python + self.build_only = True if args.build_only else False + self.build_only_llvm = args.build_only["llvm"] if self.build_only else [] + self.build_only_libs = args.build_only["libs"] if self.build_only else [] + self.no_build_arm = args.skip_build or args.no_build_arm self.no_build_aarch64 = args.skip_build or args.no_build_aarch64 self.no_build_riscv64 = args.skip_build or args.no_build_riscv64 @@ -301,6 +305,31 @@ class BuildConfig(): default="LLVM", help='which kind of flags for build_crts and build_runtimes, Choices:' + ', '.join(known_libs_flags)) + llvm_components = ("lld", "llvm-readelf", "llvm-objdump") + libs_components = ("musl", "compiler-rt", "libcxx") + llvm_components_str = ', '.join(llvm_components) + libs_components_str = ', '.join(libs_components) + + class SeparatedListByCommaToDictAction(argparse.Action): + def __call__(self, parser, namespace, vals, option_string): + vals_dct = {"llvm": [], "libs": []} + for val in vals.split(','): + if val in llvm_components: + vals_dct["llvm"].append(val) + continue + elif val in libs_components: + vals_dct["libs"].append(val) + continue + else: + error = '\'{}\' invalid. Choose from {}, {}'.format(val, llvm_components, libs_components) + raise argparse.ArgumentError(self, error) + setattr(namespace, self.dest, vals_dct) + + parser.add_argument( + '--build-only', + action=SeparatedListByCommaToDictAction, + default=dict(), + help=f'Build only {llvm_components_str} llvm components and {libs_components_str} lib components.') return parser.parse_args() @@ -567,6 +596,7 @@ class LlvmCore(BuildUtils): build_dir, install_dir, build_name, + build_target=None, extra_defines=None, extra_env=None): @@ -597,6 +627,7 @@ class LlvmCore(BuildUtils): if extra_env is not None: env.update(extra_env) + install = False if build_target else True llvm_project_path = os.path.abspath(os.path.join(self.build_config.LLVM_PROJECT_DIR, 'llvm')) self.invoke_cmake(llvm_project_path, @@ -606,8 +637,28 @@ class LlvmCore(BuildUtils): self.invoke_ninja(out_path=build_dir, env=env, - target=None, - install=True) + target=build_target, + install=install) + if not install: + self.llvm_python_install(build_dir, install_dir) + + def llvm_python_install(self, build_dir, install_dir): + target_dirs = ["bin", "include", "lib", "python3", "libexec", "share"] + for target_dir in target_dirs: + target_dir = f"{build_dir}/{target_dir}" + if not os.path.exists(target_dir): + continue + for (root, dirs, files) in os.walk(target_dir): + src_path = root + dst_path = root.replace(build_dir, install_dir) + for file in files: + if file.endswith(".cpp.o") or file == "cmake_install.cmake": + continue + os.makedirs(dst_path, exist_ok=True) + shutil.copy2( + os.path.join(src_path, file), + os.path.join(dst_path, file) + ) def llvm_compile_darwin_defines(self, llvm_defines): if self.host_is_darwin(): @@ -728,6 +779,7 @@ class LlvmCore(BuildUtils): debug_build=False, no_lto=False, build_instrumented=False, + build_target=None, xunit_xml_output=None): llvm_clang_install = os.path.abspath(os.path.join(self.build_config.REPOROOT_DIR, @@ -785,6 +837,7 @@ class LlvmCore(BuildUtils): build_dir=llvm_path, install_dir=out_dir, build_name=build_name, + build_target=build_target, extra_defines=llvm_defines, extra_env=llvm_extra_env) @@ -1279,8 +1332,8 @@ class LlvmLibs(BuildUtils): self.build_lldb_tools(llvm_install, llvm_path, arch, llvm_triple, cflags, ldflags, defines) seen_arch_list.append(llvm_triple) - def build_libs_by_type(self, llvm_install, llvm_build, libs_type, is_first_time, is_ndk_install): - configs_list, cc, cxx, ar, llvm_config = self.libs_argument(llvm_install) + def build_libs_by_type(self, compiler_path, llvm_install, llvm_build, libs_type, is_first_time, is_ndk_install): + configs_list, cc, cxx, ar, llvm_config = self.libs_argument(compiler_path) for (arch, llvm_triple, extra_flags, multilib_suffix) in configs_list: if llvm_build != llvm_triple: @@ -1743,6 +1796,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' @@ -1750,13 +1806,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') @@ -2391,12 +2450,14 @@ def main(): args = build_config.parse_args() need_host = build_utils.host_is_darwin() or ('linux' not in args.no_build) - need_windows = build_utils.host_is_linux() and \ + need_windows = build_utils.host_is_linux() and not build_config.build_only and \ ('windows' not in args.no_build) llvm_install = build_utils.merge_out_path('llvm-install') llvm_make = build_utils.merge_out_path('llvm_make') windows64_install = build_utils.merge_out_path('windows-x86_64-install') + llvm_path = llvm_install if not build_config.build_only else \ + os.path.join(build_config.REPOROOT_DIR, 'prebuilts', 'clang', 'ohos', 'linux-x86_64', f'clang-{build_config.CLANG_VERSION}') configs = [] if not build_config.no_build_arm: @@ -2424,21 +2485,22 @@ def main(): if build_config.build_libxml2: build_utils.get_libxml2_version() llvm_libs.build_libxml2(llvm_make, llvm_install) - - if build_config.do_build and need_host: + + if build_config.do_build and need_host and (build_config.build_only_llvm or not build_config.build_only): llvm_core.llvm_compile( build_config.build_name, llvm_install, build_config.debug, build_config.no_lto, build_config.build_instrumented, + build_config.build_only_llvm, build_config.xunit_xml_output) llvm_package.copy_python_to_host(llvm_make) llvm_package.copy_python_to_host(llvm_install) - llvm_core.set_clang_version(llvm_install) + llvm_core.set_clang_version(llvm_path) - if build_config.build_libs: + if build_config.build_libs and not build_config.build_only: libs_type = 'crts' if 'crts' in build_config.build_libs else 'runtimes' is_first_time = True if build_config.build_libs in ['crts_first_time', 'runtimes_libunwind'] else False is_ndk_install = True if build_config.build_libs == 'runtimes_libcxx_ndk' else False @@ -2447,7 +2509,7 @@ def main(): llvm_libs.build_libs_by_type(llvm_install, target, libs_type, is_first_time, is_ndk_install) return - if build_config.do_build and build_utils.host_is_linux(): + if build_config.do_build and build_utils.host_is_linux() and not build_config.build_only: sysroot_composer.setup_cmake_platform(llvm_install) llvm_libs.build_crt_libs(configs, llvm_install) @@ -2461,6 +2523,35 @@ def main(): for (arch, target) in configs: llvm_libs.build_libs(llvm_install, target) + 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: @@ -2481,7 +2572,7 @@ def main(): build_config.build_name) if build_config.build_gtest_libs: - llvm_libs.build_gtest(llvm_install) + llvm_libs.build_gtest(llvm_path, llvm_install) if build_config.do_package: if build_utils.host_is_linux(): diff --git a/llvm-build/env_prepare.sh b/llvm-build/env_prepare.sh index 2557ece0be32..1f4578ebb0ce 100755 --- a/llvm-build/env_prepare.sh +++ b/llvm-build/env_prepare.sh @@ -118,11 +118,13 @@ done if [ -d "${code_dir}/prebuilts/clang/ohos/linux-x86_64/${CLANG_LINUX_BUILD}" ]; then SET_CLANG_VERSION='15.0.4' mv "${code_dir}/prebuilts/clang/ohos/linux-x86_64/${CLANG_LINUX_BUILD}" "${code_dir}/prebuilts/clang/ohos/linux-x86_64/clang-${SET_CLANG_VERSION}" + cp -rf "${code_dir}/prebuilts/clang/ohos/linux-x86_64/clang-${SET_CLANG_VERSION}" "${code_dir}/prebuilts/clang/ohos/linux-x86_64/llvm" fi if [ -d "${code_dir}/prebuilts/clang/ohos/darwin-x86_64/${CLANG_DARWIN_BUILD}" ]; then SET_CLANG_VERSION='15.0.4' mv "${code_dir}/prebuilts/clang/ohos/darwin-x86_64/${CLANG_DARWIN_BUILD}" "${code_dir}/prebuilts/clang/ohos/darwin-x86_64/clang-${SET_CLANG_VERSION}" + cp -rf "${code_dir}/prebuilts/clang/ohos/darwin-x86_64/clang-${SET_CLANG_VERSION}" "${code_dir}/prebuilts/clang/ohos/linux-x86_64/llvm" fi # try to detect version ... -- Gitee From 112d8b897a3054b02e69024982d66501e0d7188c Mon Sep 17 00:00:00 2001 From: Likholatov Evgeny Date: Fri, 9 Feb 2024 12:41:42 +0300 Subject: [PATCH 12/82] Added description to --build-only flag in README. Signed-off-by: Likholatov Evgeny --- llvm-build/README.md | 7 +++++++ llvm-build/build.py | 14 ++++++-------- llvm-build/env_prepare.sh | 4 ++-- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/llvm-build/README.md b/llvm-build/README.md index 1a256676fd34..7f776991e70a 100644 --- a/llvm-build/README.md +++ b/llvm-build/README.md @@ -78,6 +78,13 @@ build.py options: OH LLVM BOTH +--build-only # build only some targets, skip building windows, lldb-server and package step + lld + llvm-readelf + llvm-objdump + musl + compiler-rt + libcxx ```
diff --git a/llvm-build/build.py b/llvm-build/build.py index 49e06952fc0e..d67cdf347a6d 100755 --- a/llvm-build/build.py +++ b/llvm-build/build.py @@ -307,8 +307,6 @@ class BuildConfig(): llvm_components = ("lld", "llvm-readelf", "llvm-objdump") libs_components = ("musl", "compiler-rt", "libcxx") - llvm_components_str = ', '.join(llvm_components) - libs_components_str = ', '.join(libs_components) class SeparatedListByCommaToDictAction(argparse.Action): def __call__(self, parser, namespace, vals, option_string): @@ -329,7 +327,7 @@ class BuildConfig(): '--build-only', action=SeparatedListByCommaToDictAction, default=dict(), - help=f'Build only {llvm_components_str} llvm components and {libs_components_str} lib components.') + help=f'Build only {", ".join(llvm_components)} llvm components and {", ".join(libs_components)} lib components.') return parser.parse_args() @@ -1796,8 +1794,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' @@ -1807,10 +1807,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 diff --git a/llvm-build/env_prepare.sh b/llvm-build/env_prepare.sh index 1f4578ebb0ce..813f1a1d2e7b 100755 --- a/llvm-build/env_prepare.sh +++ b/llvm-build/env_prepare.sh @@ -118,13 +118,13 @@ done if [ -d "${code_dir}/prebuilts/clang/ohos/linux-x86_64/${CLANG_LINUX_BUILD}" ]; then SET_CLANG_VERSION='15.0.4' mv "${code_dir}/prebuilts/clang/ohos/linux-x86_64/${CLANG_LINUX_BUILD}" "${code_dir}/prebuilts/clang/ohos/linux-x86_64/clang-${SET_CLANG_VERSION}" - cp -rf "${code_dir}/prebuilts/clang/ohos/linux-x86_64/clang-${SET_CLANG_VERSION}" "${code_dir}/prebuilts/clang/ohos/linux-x86_64/llvm" + ln -s "${code_dir}/prebuilts/clang/ohos/linux-x86_64/clang-${SET_CLANG_VERSION}" "${code_dir}/prebuilts/clang/ohos/linux-x86_64/llvm" fi if [ -d "${code_dir}/prebuilts/clang/ohos/darwin-x86_64/${CLANG_DARWIN_BUILD}" ]; then SET_CLANG_VERSION='15.0.4' mv "${code_dir}/prebuilts/clang/ohos/darwin-x86_64/${CLANG_DARWIN_BUILD}" "${code_dir}/prebuilts/clang/ohos/darwin-x86_64/clang-${SET_CLANG_VERSION}" - cp -rf "${code_dir}/prebuilts/clang/ohos/darwin-x86_64/clang-${SET_CLANG_VERSION}" "${code_dir}/prebuilts/clang/ohos/linux-x86_64/llvm" + ln -s "${code_dir}/prebuilts/clang/ohos/darwin-x86_64/clang-${SET_CLANG_VERSION}" "${code_dir}/prebuilts/clang/ohos/linux-x86_64/llvm" fi # try to detect version ... -- Gitee From a45dcad98c8ea37f41beb6c0cd9888804d6d02d3 Mon Sep 17 00:00:00 2001 From: Likholatov Evgeny Date: Wed, 14 Feb 2024 14:28:39 +0300 Subject: [PATCH 13/82] Added check of usage --build-only with --no-build. Signed-off-by: Likholatov Evgeny --- llvm-build/build.py | 17 ++++++++++------- llvm-build/env_prepare.sh | 2 +- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/llvm-build/build.py b/llvm-build/build.py index d67cdf347a6d..fd497f7c77c5 100755 --- a/llvm-build/build.py +++ b/llvm-build/build.py @@ -36,6 +36,9 @@ class BuildConfig(): def __init__(self): args = self.parse_args() + + assert not(args.no_build and args.build_only), "Error! --no-build and --build-only flags aren't incompatible." + self.no_strip_libs = args.no_strip_libs self.do_build = not args.skip_build self.do_package = not args.skip_package and not args.build_only @@ -625,7 +628,7 @@ class LlvmCore(BuildUtils): if extra_env is not None: env.update(extra_env) - install = False if build_target else True + install = not self.build_config.build_only llvm_project_path = os.path.abspath(os.path.join(self.build_config.LLVM_PROJECT_DIR, 'llvm')) self.invoke_cmake(llvm_project_path, @@ -2524,12 +2527,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 @@ -2537,7 +2540,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!" diff --git a/llvm-build/env_prepare.sh b/llvm-build/env_prepare.sh index 813f1a1d2e7b..1172eb4ac2d1 100755 --- a/llvm-build/env_prepare.sh +++ b/llvm-build/env_prepare.sh @@ -124,7 +124,7 @@ fi if [ -d "${code_dir}/prebuilts/clang/ohos/darwin-x86_64/${CLANG_DARWIN_BUILD}" ]; then SET_CLANG_VERSION='15.0.4' mv "${code_dir}/prebuilts/clang/ohos/darwin-x86_64/${CLANG_DARWIN_BUILD}" "${code_dir}/prebuilts/clang/ohos/darwin-x86_64/clang-${SET_CLANG_VERSION}" - ln -s "${code_dir}/prebuilts/clang/ohos/darwin-x86_64/clang-${SET_CLANG_VERSION}" "${code_dir}/prebuilts/clang/ohos/linux-x86_64/llvm" + ln -s "${code_dir}/prebuilts/clang/ohos/darwin-x86_64/clang-${SET_CLANG_VERSION}" "${code_dir}/prebuilts/clang/ohos/darwin-x86_64/llvm" fi # try to detect version ... -- Gitee From a1f762457d72ca4f01f914ea76ae28aec27219df Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Wed, 31 Jan 2024 09:43:27 -0500 Subject: [PATCH 14/82] 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 f4f8b81b018f..bee399efe326 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 de31d8cc2af4..9b66bfb5843f 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -456,8 +456,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 @@ -1294,6 +1302,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); @@ -1321,74 +1490,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. @@ -1399,105 +1500,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 1e5c1fa716a7950b2297ece0604aedb26197db04 Mon Sep 17 00:00:00 2001 From: Likholatov Evgeny Date: Mon, 19 Feb 2024 10:17:04 +0300 Subject: [PATCH 15/82] Added support for --adlt-debug-build in build_libs_by_type method. Signed-off-by: Likholatov Evgeny --- llvm-build/build.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/llvm-build/build.py b/llvm-build/build.py index fd497f7c77c5..86ce99faa5b1 100755 --- a/llvm-build/build.py +++ b/llvm-build/build.py @@ -1342,6 +1342,9 @@ class LlvmLibs(BuildUtils): defines = self.base_cmake_defines() ldflags = [] cflags = [] + if self.build_config.adlt_debug_build: + cflags.append("-g -gdwarf-4 -O0") + self.logger().info('Build %slibs for %s', libs_type, llvm_triple) out_path = self.merge_out_path('llvm_build') self.logger().info('Make %s libs for %s build_libs_flags: %s', libs_type, llvm_triple, self.build_config.build_libs_flags) -- Gitee From b0023d43543ce4b4e98a58f390d8d2f77a735e57 Mon Sep 17 00:00:00 2001 From: Likholatov Evgeny Date: Mon, 19 Feb 2024 13:48:02 +0300 Subject: [PATCH 16/82] Fix assert message. Signed-off-by: Likholatov Evgeny --- llvm-build/build.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/llvm-build/build.py b/llvm-build/build.py index 86ce99faa5b1..880afb76d33b 100755 --- a/llvm-build/build.py +++ b/llvm-build/build.py @@ -37,7 +37,7 @@ class BuildConfig(): def __init__(self): args = self.parse_args() - assert not(args.no_build and args.build_only), "Error! --no-build and --build-only flags aren't incompatible." + assert not(args.no_build and args.build_only), "Error! --no-build and --build-only flags aren't compatible." self.no_strip_libs = args.no_strip_libs self.do_build = not args.skip_build @@ -1344,7 +1344,7 @@ class LlvmLibs(BuildUtils): cflags = [] if self.build_config.adlt_debug_build: cflags.append("-g -gdwarf-4 -O0") - self.logger().info('Build %slibs for %s', libs_type, llvm_triple) + self.logger().info('Build %s libs for %s', libs_type, llvm_triple) out_path = self.merge_out_path('llvm_build') self.logger().info('Make %s libs for %s build_libs_flags: %s', libs_type, llvm_triple, self.build_config.build_libs_flags) -- Gitee From a9dce0502b608db631e1ec7f520f177270eb457f Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Tue, 20 Feb 2024 02:36:54 -0500 Subject: [PATCH 17/82] 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 bee399efe326..24224c8cce54 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 8e772a6a683e..a025cb2bc0fc 100644 --- a/lld/ELF/InputSection.cpp +++ b/lld/ELF/InputSection.cpp @@ -845,7 +845,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 9b66bfb5843f..1adda8cd4c25 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -462,7 +462,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; @@ -1306,7 +1307,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 = ""; @@ -1331,6 +1332,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 { @@ -1342,7 +1347,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"; } @@ -1350,8 +1355,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) @@ -1359,8 +1364,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!"; @@ -1370,56 +1375,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: @@ -1433,28 +1412,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)); @@ -1466,8 +1445,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 2c41993adc5b2b426b5d347cc8c17e7c88d2fb57 Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Thu, 22 Feb 2024 18:09:57 +0800 Subject: [PATCH 18/82] 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 24224c8cce54..c052caf38e96 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 1adda8cd4c25..3e3874a4ddce 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()) @@ -1340,7 +1341,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"; @@ -1375,7 +1378,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 e7c711179f12feba828fae56131529b643a43138 Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Thu, 29 Feb 2024 04:15:50 -0500 Subject: [PATCH 19/82] 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 c052caf38e96..8bd05c5a253f 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 3e3874a4ddce..27a9bd789ee5 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -1367,8 +1367,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!"; @@ -1427,10 +1429,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 d3958881e55d8809f0f4e32e5a1abe7960d33094 Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Tue, 5 Mar 2024 09:28:08 -0500 Subject: [PATCH 20/82] Debug musl: no optimize. Fix trailing whitespaces. Change-Id: Ibe1fe4c9c49c27a48e9349a6803f7f15814871cc Signed-off-by: Anton Volkov --- llvm-build/build.py | 39 +++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/llvm-build/build.py b/llvm-build/build.py index 880afb76d33b..e8a06e08b5b7 100755 --- a/llvm-build/build.py +++ b/llvm-build/build.py @@ -36,7 +36,7 @@ class BuildConfig(): def __init__(self): args = self.parse_args() - + assert not(args.no_build and args.build_only), "Error! --no-build and --build-only flags aren't compatible." self.no_strip_libs = args.no_strip_libs @@ -239,13 +239,13 @@ class BuildConfig(): action='store_true', default=False, help='Automatically exit when timeout (currently effective for lldb-server)') - + parser.add_argument( '--enable-monitoring', action='store_true', default=False, help='Enable lldb performance monitoring') - + parser.add_argument( '--adlt-debug-build', action='store_true', @@ -770,7 +770,7 @@ class LlvmCore(BuildUtils): if self.build_config.build_libxml2: llvm_defines['LLDB_ENABLE_LIBXML2'] = 'ON' llvm_defines['LIBXML2_INCLUDE_DIR'] = os.path.join(self.get_prebuilts_dir('libxml2'), self.use_platform(), 'include', 'libxml2') - + if self.build_config.enable_monitoring: llvm_defines['LLDB_ENABLE_PERFORMANCE'] = 'ON' @@ -1034,12 +1034,15 @@ class SysrootComposer(BuildUtils): self.get_python_dir(), 'bin', self.build_config.LLDB_PYTHON) llvm_gn_args = 'is_llvm_build=true startup_init_with_param_base=true use_thin_lto=false' subprocess.run([python_execute_dir, hb_build_py, 'build', '--product-name', product_name, '--target-cpu', - target_cpu, '--build-target', target_name, '--gn-args', + target_cpu, '--build-target', target_name, '--gn-args', gn_args, llvm_gn_args, '--deps-guard=false'], shell=False, stdout=subprocess.PIPE, cwd=self.build_config.REPOROOT_DIR) - + def build_musl_libs(self, product_name, target_cpu, target_name, ohos_lib_dir, sysroot_lib_dir, ld_musl_lib, gn_args=''): + if (self.build_config.debug): + gn_args = f"is_debug=true ohos_extra_cflags=-O0 {gn_args}" + self.run_hb_build(product_name, target_cpu, target_name, gn_args) libc_name = 'libc.so' crtplus_lib = self.merge_out_path('llvm_build', 'obj', 'out', 'llvm_build', 'obj', 'third_party', 'musl', @@ -1232,18 +1235,18 @@ class LlvmLibs(BuildUtils): ldflag.append('-Wl,-z,relro,-z,now -pie') if self.build_config.strip and not self.build_config.no_strip_libs: ldflag.append('-s') - + ldflags.extend(ldflag) cflag = [ - '-fstack-protector-strong', + '-fstack-protector-strong', '--target=%s' % llvm_triple, '-ffunction-sections', '-fdata-sections', extra_flags, ] cflags.extend(cflag) - + def run_hb_build_libs(self, libs_name): gn_args = 'build_libs_flags={} llvm_lib={}'.format(self.build_config.build_libs_flags, libs_name) self.sysroot_composer.run_hb_build('llvm_build', 'arm', 'build_libs', gn_args) @@ -1306,7 +1309,7 @@ class LlvmLibs(BuildUtils): self.open_ohos_triple('mipsel'), self.open_ohos_triple('x86_64')] libcxx_ndk_install = self.merge_out_path('libcxx-ndk') self.check_create_dir(libcxx_ndk_install) - + if precompilation: self.build_crts(llvm_install, arch, llvm_triple, cflags, ldflags, multilib_suffix, defines) continue @@ -1440,10 +1443,10 @@ class LlvmLibs(BuildUtils): rt_defines['LIBUNWIND_INSTALL_LIBRARY'] = 'OFF' else: rt_defines['LIBCXX_ABI_NAMESPACE'] = '__h' - + self.check_rm_tree(out_path) cmake_rt = os.path.abspath(os.path.join(self.build_config.LLVM_PROJECT_DIR, 'runtimes')) - + self.invoke_cmake(cmake_rt, out_path, rt_defines, @@ -1839,11 +1842,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): @@ -1916,7 +1919,7 @@ class LlvmLibs(BuildUtils): libxml2_defines['XML_INCLUDEDIR'] = os.path.join(windows_sysroot, 'include') libxml2_cmake_path = os.path.abspath(os.path.join(self.build_config.REPOROOT_DIR, 'third_party', 'libxml2')) - + self.invoke_cmake(libxml2_cmake_path, libxml2_build_path, libxml2_defines, @@ -1926,7 +1929,7 @@ class LlvmLibs(BuildUtils): env=dict(self.build_config.ORIG_ENV), target=None, install=True) - + if not os.path.exists(os.path.join(windows64_install, 'bin')): os.makedirs(os.path.join(windows64_install, 'bin')) shutil.copyfile(os.path.join(libxml2_build_path, 'libxml2.dll'), os.path.join(windows64_install, 'bin', 'libxml2.dll')) @@ -2094,7 +2097,7 @@ class LlvmPackage(BuildUtils): 'opt%s' % ext, ] - + necessary_bin_files.extend(necessary_bin_file) @staticmethod @@ -2414,7 +2417,7 @@ class LlvmPackage(BuildUtils): # Strip lldb-server self.strip_lldb_server(host, install_dir) - + # Copy lldb script lldb_script_file = 'lldb.cmd' if host.startswith('windows') else 'lldb.sh' self.check_copy_file(os.path.join(self.build_config.LLVM_PROJECT_DIR, 'lldb', 'scripts', lldb_script_file), os.path.join(install_dir, 'bin')) -- Gitee From 286f2de0d446bbc8ad8f297b5de4333b37bc5759 Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Wed, 6 Mar 2024 08:48:02 -0500 Subject: [PATCH 21/82] 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 8bd05c5a253f..a30bf61d36c6 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 2bec3e89f7051bf5aa18b47f9eca6290df157f23 Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Wed, 6 Mar 2024 08:57:29 -0500 Subject: [PATCH 22/82] 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 27a9bd789ee5..1bd79908d441 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -459,11 +459,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 @@ -1308,6 +1308,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: "; @@ -1316,20 +1317,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); } @@ -1356,36 +1347,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 @@ -1394,7 +1388,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; @@ -1486,7 +1480,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 07f825412d16a458f6f69f08aa2a44da77eab34c Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Wed, 13 Mar 2024 03:59:30 -0400 Subject: [PATCH 23/82] 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 1bd79908d441..3218b5a1272e 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -1372,14 +1372,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 @@ -1391,17 +1383,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: @@ -1433,7 +1435,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 75d668909552f6f855c0ae23e8563c69e4ad2098 Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Fri, 15 Mar 2024 05:09:19 -0400 Subject: [PATCH 24/82] [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 a30bf61d36c6..25d5fe886bd5 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 a025cb2bc0fc..7357297af5b0 100644 --- a/lld/ELF/InputSection.cpp +++ b/lld/ELF/InputSection.cpp @@ -1000,6 +1000,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 3218b5a1272e..ead89a3f74bc 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); @@ -461,13 +466,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 @@ -1305,41 +1305,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); @@ -1355,33 +1332,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; @@ -1395,15 +1378,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: @@ -1413,14 +1399,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: @@ -1432,10 +1418,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 db06d3213eb30934f3ebd6fe7a43c10353f52a59 Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Thu, 21 Mar 2024 20:24:29 +0800 Subject: [PATCH 25/82] 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 15e1b621c71de5bcf3ae5e23fcd5be903eb63cb9 Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Thu, 21 Mar 2024 21:59:46 +0800 Subject: [PATCH 26/82] [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 5587bb007a7e08bf18646376852ead291e620ba2 Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Thu, 21 Mar 2024 22:02:36 +0800 Subject: [PATCH 27/82] [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 25d5fe886bd5..82b685c86a2f 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 fd56c1d37c4db358e63e26b3af964d2557ba8970 Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Thu, 21 Mar 2024 23:03:22 +0800 Subject: [PATCH 28/82] [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 ead89a3f74bc..ec3228a986cf 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -1415,6 +1415,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 66c3b4a0b53eb841b1558e797fc9b3ecb6698193 Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Wed, 27 Mar 2024 05:45:56 -0400 Subject: [PATCH 29/82] 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 82b685c86a2f..e910e6661f8f 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 ec3228a986cf..2341fdc025c4 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -2338,8 +2338,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 56c823e22d4d889271bf1ed4a8b7889c2ef0d545 Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Wed, 27 Mar 2024 07:24:04 -0400 Subject: [PATCH 30/82] [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 e910e6661f8f..9ae3bccee1e9 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 7357297af5b0..0eb2968413bd 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 2341fdc025c4..455ab20e0333 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -1447,7 +1447,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 3d6015c31e9914b6baa315bd369be53436880115 Mon Sep 17 00:00:00 2001 From: Pavel Kosov Date: Sat, 30 Mar 2024 02:09:38 +0000 Subject: [PATCH 31/82] Revert 'Pull Request !426 : [ADLT] Fix copyRelocations' --- 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 9ae3bccee1e9..82b685c86a2f 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 0eb2968413bd..7357297af5b0 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 455ab20e0333..ec3228a986cf 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -1447,7 +1447,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) @@ -2338,6 +2338,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 2561c2e2f3e45dc680a240692c8926b29a60958e Mon Sep 17 00:00:00 2001 From: Likholatov Evgeny Date: Mon, 1 Apr 2024 10:46:24 +0300 Subject: [PATCH 32/82] 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 455ab20e0333..d14748417c81 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -1404,6 +1404,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); @@ -1421,6 +1423,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 496244a197cb376488fbe8ad1a6fd514f4d4e6b1 Mon Sep 17 00:00:00 2001 From: Kaipov Rustam Date: Thu, 28 Mar 2024 09:17:27 -0400 Subject: [PATCH 33/82] [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 82b685c86a2f..953c1b79f787 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 53d394be3fde5d0399f850d60e55e1f2f8440bba Mon Sep 17 00:00:00 2001 From: Kaipov Rustam Date: Mon, 1 Apr 2024 05:39:00 -0400 Subject: [PATCH 34/82] 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 953c1b79f787..0d899243c116 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 9dd876de5545ec2851f6c50a285bb92800570971 Mon Sep 17 00:00:00 2001 From: Khomutov Nikita Date: Mon, 1 Apr 2024 12:48:41 +0000 Subject: [PATCH 35/82] !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 --- 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 0d899243c116..7fc1ea93ba4e 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 221443341bf78fa14ce202d6dab27d94d33010ff Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Tue, 2 Apr 2024 08:23:56 -0400 Subject: [PATCH 36/82] 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 82b685c86a2f..9ae3bccee1e9 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,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 7357297af5b0..0eb2968413bd 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 ec3228a986cf..1bca72224fa0 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -1447,7 +1447,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) @@ -2338,8 +2340,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 8b634191593984e346a864c4aaed7c5a7064775e Mon Sep 17 00:00:00 2001 From: Khomutov Nikita Date: Thu, 4 Apr 2024 09:33:16 +0000 Subject: [PATCH 37/82] !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 --- 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 5d7adce6107f92cb42805bb0014cfc5957e92305 Mon Sep 17 00:00:00 2001 From: Khomutov Nikita Date: Sun, 7 Apr 2024 11:36:27 +0300 Subject: [PATCH 38/82] [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 6023e2273d53f49786d0de735b31b181a3f77995 Mon Sep 17 00:00:00 2001 From: Khomutov Nikita Date: Sun, 7 Apr 2024 12:53:32 +0300 Subject: [PATCH 39/82] [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 67542acfd52d83363fadda0b73e7c681ad8f14f9 Mon Sep 17 00:00:00 2001 From: Khomutov Nikita Date: Sun, 7 Apr 2024 13:21:52 +0300 Subject: [PATCH 40/82] [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 65b3885b65e70a2f688c02aa377053e617753d08 Mon Sep 17 00:00:00 2001 From: likholatovevgeny Date: Mon, 8 Apr 2024 11:17:54 +0300 Subject: [PATCH 41/82] Added adlt segment type Signed-off-by: likholatovevgeny --- lld/ELF/Writer.cpp | 4 ++++ llvm/include/llvm/BinaryFormat/ELF.h | 1 + llvm/tools/llvm-objdump/ELFDump.cpp | 3 +++ llvm/tools/llvm-readobj/ELFDumper.cpp | 1 + 4 files changed, 9 insertions(+) diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index a087d61ae59f..2625477443c6 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -2343,6 +2343,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/include/llvm/BinaryFormat/ELF.h b/llvm/include/llvm/BinaryFormat/ELF.h index 556fe9c6a1a0..c2a52a67bb5f 100644 --- a/llvm/include/llvm/BinaryFormat/ELF.h +++ b/llvm/include/llvm/BinaryFormat/ELF.h @@ -1382,6 +1382,7 @@ enum { PT_OPENBSD_BOOTDATA = 0x65a41be6, // Section for boot arguments. PT_OHOS_RANDOMDATA = 0x6788FC60, // Fill with random data. OHOS_LOCAL + PT_ADLT = 0x6788FC61, // Adlt information. // ARM program header types. PT_ARM_ARCHEXT = 0x70000000, // Platform architecture compatibility info 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; diff --git a/llvm/tools/llvm-readobj/ELFDumper.cpp b/llvm/tools/llvm-readobj/ELFDumper.cpp index c9a239f785d2..81aa58205eb0 100644 --- a/llvm/tools/llvm-readobj/ELFDumper.cpp +++ b/llvm/tools/llvm-readobj/ELFDumper.cpp @@ -1416,6 +1416,7 @@ static StringRef segmentTypeToString(unsigned Arch, unsigned Type) { LLVM_READOBJ_ENUM_CASE(ELF, PT_OPENBSD_BOOTDATA); LLVM_READOBJ_ENUM_CASE(ELF, PT_OHOS_RANDOMDATA); // OHOS_LOCAL + LLVM_READOBJ_ENUM_CASE(ELF, PT_ADLT); // ADLT info default: return ""; } -- Gitee From 9c20ab43ddd264f17a43b07c26941ffd027f0ba9 Mon Sep 17 00:00:00 2001 From: Khomutov Nikita Date: Mon, 8 Apr 2024 12:38:09 +0300 Subject: [PATCH 42/82] [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 fb50b76553aa3dd3127ecf12d901071495af1b51 Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Mon, 8 Apr 2024 08:29:28 -0400 Subject: [PATCH 43/82] [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 1afa4907f59663d91e4734aaddfc2722a61913d5 Mon Sep 17 00:00:00 2001 From: Khomutov Nikita Date: Mon, 8 Apr 2024 14:45:27 +0300 Subject: [PATCH 44/82] [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 4ed8ee803fd60109a8f3523ecb21275ac4b685f1 Mon Sep 17 00:00:00 2001 From: Khomutov Nikita Date: Wed, 10 Apr 2024 11:03:48 +0300 Subject: [PATCH 45/82] [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 03e8cfb0388c7191df03d14c48520cc9962904a4 Mon Sep 17 00:00:00 2001 From: Khomutov Nikita Date: Thu, 11 Apr 2024 02:52:53 +0000 Subject: [PATCH 46/82] !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 --- 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 87edc8b08c3f769071d77f576592f661d173055b Mon Sep 17 00:00:00 2001 From: Khomutov Nikita Date: Thu, 11 Apr 2024 10:04:43 +0300 Subject: [PATCH 47/82] [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 220cae7a832f2ece86a99c335b57b297f7d22c90 Mon Sep 17 00:00:00 2001 From: Khomutov Nikita Date: Fri, 12 Apr 2024 06:19:27 +0300 Subject: [PATCH 48/82] [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 2bb72e52afebff8466847059a79da9518dc6daa2 Mon Sep 17 00:00:00 2001 From: Khomutov Nikita Date: Tue, 16 Apr 2024 10:32:17 +0300 Subject: [PATCH 49/82] [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 30b466825036..8ff0f4361e8f 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 ee81a705f753ae168f913b11d0ac226e2892228a Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Wed, 17 Apr 2024 07:37:58 -0400 Subject: [PATCH 50/82] [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 | 67 +++++++++++++++++++++++++++++++---- 7 files changed, 127 insertions(+), 24 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 8ff0f4361e8f..8be3a255cbde 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 7ef8307d0636..ff8f1002ab44 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -1304,6 +1304,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, @@ -1360,21 +1370,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: @@ -1386,6 +1399,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); @@ -1428,6 +1442,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: @@ -1795,6 +1813,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)) @@ -1804,10 +1832,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)); @@ -1841,6 +1875,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 9afb747f29c2..2501d011715b 100644 --- a/lld/ELF/SyntheticSections.h +++ b/lld/ELF/SyntheticSections.h @@ -1239,7 +1239,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 fb2f260f3d94..57cadbb6f7e8 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"); } @@ -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 46eda4683acc4518ba0303b946800e946d2f8208 Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Fri, 19 Apr 2024 03:53:02 -0400 Subject: [PATCH 51/82] 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 d6d1ca80abd486b6bcdf70e1cd9331972406e258 Mon Sep 17 00:00:00 2001 From: Khomutov Nikita Date: Fri, 19 Apr 2024 11:31:29 +0300 Subject: [PATCH 52/82] [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/SyntheticSections.h | 5 +- .../include/llvm/BinaryFormat}/ADLTSection.h | 13 +- llvm/tools/llvm-readobj/ELFDumper.cpp | 231 +++++++++++++++++- llvm/tools/llvm-readobj/ObjDumper.h | 1 + llvm/tools/llvm-readobj/Opts.td | 1 + llvm/tools/llvm-readobj/llvm-readobj.cpp | 4 + 6 files changed, 245 insertions(+), 10 deletions(-) rename {lld/ELF => llvm/include/llvm/BinaryFormat}/ADLTSection.h (96%) diff --git a/lld/ELF/SyntheticSections.h b/lld/ELF/SyntheticSections.h index a93ff052cfe3..8832ca87bc2d 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" @@ -1225,6 +1225,8 @@ public: namespace adlt { +using namespace llvm::adlt; + template class AdltSection final : public SyntheticSection { public: @@ -1313,7 +1315,6 @@ private: } // namespace adlt - InputSection *createInterpSection(); MergeInputSection *createCommentSection(); template void splitSections(); diff --git a/lld/ELF/ADLTSection.h b/llvm/include/llvm/BinaryFormat/ADLTSection.h similarity index 96% rename from lld/ELF/ADLTSection.h rename to llvm/include/llvm/BinaryFormat/ADLTSection.h index 1327de5950ec..37656b486b77 100644 --- a/lld/ELF/ADLTSection.h +++ b/llvm/include/llvm/BinaryFormat/ADLTSection.h @@ -16,13 +16,13 @@ // //===----------------------------------------------------------------------===// -#ifndef LLD_ELF_ADLT_SECTION_H -#define LLD_ELF_ADLT_SECTION_H +#ifndef LLVM_BINARYFORMAT_ADLTSECTION_H +#define LLVM_BINARYFORMAT_ADLTSECTION_H #ifdef __cplusplus #include -namespace lld { -namespace elf { + +namespace llvm { namespace adlt { #else // __cplusplus @@ -123,8 +123,7 @@ static const adlt_semver_t adltSchemaVersion = {1, 0, 0}; #ifdef __cplusplus } // namespace adlt -} // namespace elf -} // namespace lld +} // namespace llvm #endif // __cplusplus -#endif // LLD_ELF_ADLT_SECTION_H +#endif // LLVM_BINARYFORMAT_ADLTSECTION_H diff --git a/llvm/tools/llvm-readobj/ELFDumper.cpp b/llvm/tools/llvm-readobj/ELFDumper.cpp index 81aa58205eb0..26d30094f28b 100644 --- a/llvm/tools/llvm-readobj/ELFDumper.cpp +++ b/llvm/tools/llvm-readobj/ELFDumper.cpp @@ -29,6 +29,7 @@ #include "llvm/ADT/StringExtras.h" #include "llvm/ADT/StringRef.h" #include "llvm/ADT/Twine.h" +#include "llvm/BinaryFormat/ADLTSection.h" #include "llvm/BinaryFormat/AMDGPUMetadataVerifier.h" #include "llvm/BinaryFormat/ELF.h" #include "llvm/BinaryFormat/MsgPackDocument.h" @@ -353,6 +354,8 @@ protected: void loadDynamicTable(); void parseDynamicTable(); + Expected> findAdlt(); + Expected getSymbolVersion(const Elf_Sym &Sym, bool &IsDefault) const; Expected, 0> *> getVersionMap() const; @@ -365,11 +368,14 @@ protected: DynRegionInfo DynSymTabShndxRegion; DynRegionInfo DynamicTable; StringRef DynamicStringTable; + StringRef AdltStringTable; const Elf_Hash *HashTable = nullptr; const Elf_GnuHash *GnuHashTable = nullptr; const Elf_Shdr *DotSymtabSec = nullptr; const Elf_Shdr *DotDynsymSec = nullptr; const Elf_Shdr *DotAddrsigSec = nullptr; + const Elf_Shdr *DotAdlt = nullptr; + const Elf_Shdr *DotAdltStrtab = nullptr; DenseMap> ShndxTables; Optional SONameOffset; Optional>> AddressToIndexMap; @@ -388,7 +394,9 @@ protected: Expected getSymbolSectionName(const Elf_Sym &Symbol, unsigned SectionIndex) const; std::string getStaticSymbolName(uint32_t Index) const; + StringRef getDynamicString(uint64_t Value, StringRef StringTable) const; StringRef getDynamicString(uint64_t Value) const; + StringRef getAdltDynamicString(uint64_t Value) const; void printSymbolsHelper(bool IsDynamic) const; std::string getDynamicEntry(uint64_t Type, uint64_t Value) const; @@ -572,6 +580,7 @@ public: void printVersionDefinitionSection(const Elf_Shdr *Sec) override; void printVersionDependencySection(const Elf_Shdr *Sec) override; void printHashHistograms() override; + void printAdltSection() override; void printCGProfile() override; void printBBAddrMaps() override; void printAddrsig() override; @@ -676,6 +685,7 @@ public: void printVersionDefinitionSection(const Elf_Shdr *Sec) override; void printVersionDependencySection(const Elf_Shdr *Sec) override; void printHashHistograms() override; + void printAdltSection() override; void printCGProfile() override; void printBBAddrMaps() override; void printAddrsig() override; @@ -1706,6 +1716,45 @@ static const char *getElfMipsOptionsOdkType(unsigned Odk) { } } +const EnumEntry AdltHashTypes[] = { + {"None", "NONE", llvm::adlt::ADLT_HASH_TYPE_NONE}, + {"GnuHash", "GNU_HASH", llvm::adlt::ADLT_HASH_TYPE_GNU_HASH}, + {"SysvHash", "SYSV_HASH", llvm::adlt::ADLT_HASH_TYPE_SYSV_HASH}, + {"Debug", "DEBUG", llvm::adlt::ADLT_HASH_TYPE_DEBUG_CONST}, +}; + +template +Expected> ELFDumper::findAdlt() { + // Try to locate .adlt section in the sections table + typename ELFT::ShdrRange Sections = cantFail(Obj.sections()); + for (const Elf_Shdr &Sec : Sections) { + if (DotAdlt && DotAdltStrtab) break; + + switch (Sec.sh_type) { + case ELF::SHT_STRTAB: + if (!DotAdltStrtab && getPrintableSectionName(Sec) == ".adlt.strtab") + DotAdltStrtab = &Sec; + break; + case ELF::SHT_PROGBITS: + if (!DotAdlt && getPrintableSectionName(Sec) == ".adlt") + DotAdlt = &Sec; + break; + } + } + + if (!DotAdlt) + return createError(".adlt section not found"); + if (!DotAdltStrtab) + return createError("has .adlt but .adlt.strtab section not found"); + + Expected StrTabOrErr = Obj.getStringTable(*DotAdltStrtab); + if (!StrTabOrErr) + return StrTabOrErr.takeError(); + AdltStringTable = *StrTabOrErr; + + return Obj.getSectionContents(*DotAdlt); +} + template std::pair ELFDumper::findDynamic() { @@ -2399,7 +2448,18 @@ std::string ELFDumper::getDynamicEntry(uint64_t Type, } template -StringRef ELFDumper::getDynamicString(uint64_t Value) const { +StringRef ELFDumper::getDynamicString(uint64_t Value) const { + return this->getDynamicString(Value, DynamicStringTable); +} + +template +StringRef ELFDumper::getAdltDynamicString(uint64_t Value) const { + return this->getDynamicString(Value, AdltStringTable); +} + +template +StringRef ELFDumper::getDynamicString( + uint64_t Value, StringRef DynamicStringTable) const { if (DynamicStringTable.empty() && !DynamicStringTable.data()) { reportUniqueWarning("string table was not found"); return ""; @@ -4823,6 +4883,10 @@ template void GNUELFDumper::printHashHistograms() { } } +template void GNUELFDumper::printAdltSection() { + OS << "GNUStyle::printAdltSection not implemented\n"; +} + template void GNUELFDumper::printCGProfile() { OS << "GNUStyle::printCGProfile not implemented\n"; } @@ -6955,6 +7019,171 @@ template void LLVMELFDumper::printHashHistograms() { W.startLine() << "Hash Histogram not implemented!\n"; } + +template void LLVMELFDumper::printAdltSection() { + using namespace llvm::adlt; + + Expected> ContentsOrErr = this->findAdlt(); + if (!ContentsOrErr) { + return this->reportUniqueWarning(ContentsOrErr.takeError()); + } + + ArrayRef adltRaw = ContentsOrErr.get(); + auto* header = reinterpret_cast(adltRaw.data()); + const auto& ver = header->schemaVersion; + + DictScope DSec(W, "ADLT"); + + do { + DictScope DHeader(W, "Header"); + W.printVersion("schema-version", ver.major, ver.minor, ver.patch); + + if (ver.major > 1) { + this->reportUniqueWarning(Twine("schema version not supported yet") ); + return; + } + + W.printHex("schema-header-size", header->schemaHeaderSize); + W.printHex("schema-psod-size", header->schemaPSODSize); + W.printNumber("shared-objects-num", header->sharedObjectsNum); + W.printEnum("string-hash-type", header->stringHashType, makeArrayRef(AdltHashTypes)); + W.printHex("blob-start", header->blobStart); + W.printHex("blob-size", header->blobSize); + W.printHex("overall-mapped-size", header->overallMappedSize); + + if (ver.minor < 1) break; + + { + DictScope RDEntry(W, "rela-dyn-segs"); + const auto& arr = header->relaDynSegs; + W.printHex("offset", arr.offset); + W.printHex("size", arr.size); + } + { + DictScope RPEntry(W, "rela-plt-segs"); + const auto& arr = header->relaPltSegs; + W.printHex("offset", arr.offset); + W.printHex("size", arr.size); + } + } while(0); + + ArrayRef psodsRaw = adltRaw.slice( + header->schemaHeaderSize, + header->sharedObjectsNum * header->schemaPSODSize); + ArrayRef blob = adltRaw.slice(header->blobStart, header->blobSize); + + 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 section range"); + + { + ListScope LPsods(W, "PSODs"); + + for (size_t psodIdx = 0; psodIdx < header->sharedObjectsNum; ++psodIdx) { + const adlt_psod_t& psod = *reinterpret_cast + (psodsRaw.data() + psodIdx * header->schemaPSODSize); + + DictScope LEntry(W, ("#" + Twine(psodIdx)).str()); + W.printString("soname", this->getAdltDynamicString(psod.soName)); + W.printHex("soname-hash", psod.soNameHash); + { + DictScope IAEntry(W, "init-array"); + W.printHex("size", psod.initArray.size); + W.printNumber("sec-index", psod.initArray.secIndex); + W.printHex("offset", psod.initArray.offset); + } + { + DictScope IAEntry(W, "fini-array"); + W.printHex("size", psod.finiArray.size); + W.printNumber("sec-index", psod.finiArray.secIndex); + W.printHex("offset", psod.finiArray.offset); + } + { + DictScope DNEntry(W, "dt-needed"); + W.printHex("size", psod.dtNeeded.size); + W.printHex("offset", psod.dtNeeded.offset); + + const auto chunk = blob.slice(psod.dtNeeded.offset, psod.dtNeeded.size); + if (!chunk.empty()) { + W.printBinary("raw", chunk); + + ArrayRef deps( + reinterpret_cast(chunk.data()), + chunk.size() / sizeof(adlt_dt_needed_index_t)); + + ListScope NeedList(W, "needed-libs"); + for (const auto& need : deps) { + DictScope DepEntry(W, this->getAdltDynamicString(need.sonameOffset)); + W.printBoolean("is-internal", need.hasInternalPSOD); + if (need.hasInternalPSOD) + W.printNumber("psod-id", need.PSODindex); + } + } + } + { + DictScope SLEntry(W, "shared-local-symbol"); + const auto& csref = psod.sharedLocalSymbolIndex; + W.printNumber("sec-index", csref.secIndex); + W.printHex("offset", csref.offset); + } + { + DictScope SGEntry(W, "shared-global-symbol"); + const auto& csref = psod.sharedGlobalSymbolIndex; + W.printNumber("sec-index", csref.secIndex); + W.printHex("offset", csref.offset); + } + { + DictScope PHEntry(W, "ph-index"); + const auto& arr = psod.phIndexes; + W.printHex("size", arr.size); + W.printHex("offset", arr.offset); + + const auto chunk = blob.slice(arr.offset, arr.size); + if (!chunk.empty()) { + ArrayRef phIdxs( + reinterpret_cast(chunk.data()), + chunk.size() / sizeof(uint16_t)); + W.printBinary("raw", chunk); + W.printList("values", phIdxs); + } + } + + if (ver.minor < 1) continue; + + { + DictScope RDSEntry(W, "rela-dyn-segs"); + const auto& arr = psod.relaDynSegs; + W.printHex("size", arr.size); + W.printHex("offset", arr.offset); + + const auto chunk = blob.slice(arr.offset, arr.size); + if (!chunk.empty()) + W.printBinary("raw", chunk); + } + { + DictScope RPSEntry(W, "rela-plt-segs"); + const auto& arr = psod.relaPltSegs; + W.printHex("size", arr.size); + W.printHex("offset", arr.offset); + + const auto chunk = blob.slice(arr.offset, arr.size); + if (!chunk.empty()) + W.printBinary("raw", chunk); + } + } + } + + { + DictScope DBlob (W, "Blob"); + W.printHex("start", header->blobStart); + W.printHex("size", header->blobSize); + W.printBinaryBlock("raw", blob); + } +} + // Returns true if rel/rela section exists, and populates SymbolIndices. // Otherwise returns false. template diff --git a/llvm/tools/llvm-readobj/ObjDumper.h b/llvm/tools/llvm-readobj/ObjDumper.h index 292efd2ae350..320e5972d479 100644 --- a/llvm/tools/llvm-readobj/ObjDumper.h +++ b/llvm/tools/llvm-readobj/ObjDumper.h @@ -129,6 +129,7 @@ public: virtual void printVersionInfo() {} virtual void printGroupSections() {} virtual void printHashHistograms() {} + virtual void printAdltSection() {} virtual void printCGProfile() {} virtual void printBBAddrMaps() {} virtual void printAddrsig() {} diff --git a/llvm/tools/llvm-readobj/Opts.td b/llvm/tools/llvm-readobj/Opts.td index 4687fc71245f..012a16bb99ff 100644 --- a/llvm/tools/llvm-readobj/Opts.td +++ b/llvm/tools/llvm-readobj/Opts.td @@ -55,6 +55,7 @@ def section_groups : FF<"section-groups", "Display section groups">, Group, Group; def hash_symbols : FF<"hash-symbols", "Display the dynamic symbols derived from the hash section">, Group; def hash_table : FF<"hash-table", "Display .hash section">, Group; +def adlt_section: FF<"adlt-section", "Display .adlt section in a pretty format">, Group; def needed_libs : FF<"needed-libs", "Display the needed libraries">, Group; def notes : FF<"notes", "Display notes">, Group; def program_headers : FF<"program-headers", "Display program headers">, Group; diff --git a/llvm/tools/llvm-readobj/llvm-readobj.cpp b/llvm/tools/llvm-readobj/llvm-readobj.cpp index e1ebbeb41f28..8c470a60ff9a 100644 --- a/llvm/tools/llvm-readobj/llvm-readobj.cpp +++ b/llvm/tools/llvm-readobj/llvm-readobj.cpp @@ -125,6 +125,7 @@ static cl::boolOrDefault SectionMapping; static SmallVector SortKeys; // ELF specific options. +static bool AdltSection; static bool DynamicTable; static bool ELFLinkerOptions; static bool GnuHashTable; @@ -253,6 +254,7 @@ static void parseOptions(const opt::InputArgList &Args) { OutputStyleChoice + "'"); } } + opts::AdltSection = Args.hasArg(OPT_adlt_section); opts::GnuHashTable = Args.hasArg(OPT_gnu_hash_table); opts::HashSymbols = Args.hasArg(OPT_hash_symbols); opts::HashTable = Args.hasArg(OPT_hash_table); @@ -449,6 +451,8 @@ static void dumpObject(ObjectFile &Obj, ScopedPrinter &Writer, Dumper->printGroupSections(); if (opts::HashHistogram) Dumper->printHashHistograms(); + if (opts::AdltSection) + Dumper->printAdltSection(); if (opts::CGProfile) Dumper->printCGProfile(); if (opts::BBAddrMap) -- Gitee From be93ed5544bed9a86b0b7e6705f3272a9cff581b Mon Sep 17 00:00:00 2001 From: Khomutov Nikita Date: Fri, 19 Apr 2024 16:13:38 +0300 Subject: [PATCH 53/82] [ADLT] update reloc index types in .adlt section Signed-off-by: Khomutov Nikita Change-Id: Ie25e40ee257af416fef3cb942df6b02ab57b65e0 --- lld/ELF/SyntheticSections.cpp | 73 +++++++++----------- lld/ELF/SyntheticSections.h | 12 ++-- llvm/include/llvm/BinaryFormat/ADLTSection.h | 12 +--- llvm/tools/llvm-readobj/ELFDumper.cpp | 64 ++++++++--------- 4 files changed, 73 insertions(+), 88 deletions(-) diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp index 73a11c1aedd7..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" @@ -4085,11 +4080,11 @@ 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" ); -static_assert(sizeof(adlt_section_header_t) == 72, +static_assert(sizeof(adlt_section_header_t) == 40, "please update version if header has been changed" ); @@ -4110,12 +4105,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(); @@ -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) { @@ -4332,7 +4330,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 +4378,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(); @@ -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 09189e39f1d6..c16389cb31ec 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; @@ -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); diff --git a/llvm/include/llvm/BinaryFormat/ADLTSection.h b/llvm/include/llvm/BinaryFormat/ADLTSection.h index 37656b486b77..ac883cb30da8 100644 --- a/llvm/include/llvm/BinaryFormat/ADLTSection.h +++ b/llvm/include/llvm/BinaryFormat/ADLTSection.h @@ -74,12 +74,6 @@ typedef struct { 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, @@ -100,8 +94,8 @@ typedef struct { 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_blob_u32_array_t relaDynIndx; // .rela.dyn dependent indexes, raw list + adlt_blob_u32_array_t relaPltIndx; // .rela.plt dependent indexes, raw list } adlt_psod_t; typedef struct { @@ -113,8 +107,6 @@ 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/llvm/tools/llvm-readobj/ELFDumper.cpp b/llvm/tools/llvm-readobj/ELFDumper.cpp index 26d30094f28b..48e9ec0d2c25 100644 --- a/llvm/tools/llvm-readobj/ELFDumper.cpp +++ b/llvm/tools/llvm-readobj/ELFDumper.cpp @@ -7022,6 +7022,7 @@ template void LLVMELFDumper::printHashHistograms() { template void LLVMELFDumper::printAdltSection() { using namespace llvm::adlt; + constexpr size_t kBinDumpLimit = sizeof(Elf64_Xword) * 0x80; Expected> ContentsOrErr = this->findAdlt(); if (!ContentsOrErr) { @@ -7050,21 +7051,6 @@ template void LLVMELFDumper::printAdltSection() { W.printHex("blob-start", header->blobStart); W.printHex("blob-size", header->blobSize); W.printHex("overall-mapped-size", header->overallMappedSize); - - if (ver.minor < 1) break; - - { - DictScope RDEntry(W, "rela-dyn-segs"); - const auto& arr = header->relaDynSegs; - W.printHex("offset", arr.offset); - W.printHex("size", arr.size); - } - { - DictScope RPEntry(W, "rela-plt-segs"); - const auto& arr = header->relaPltSegs; - W.printHex("offset", arr.offset); - W.printHex("size", arr.size); - } } while(0); ArrayRef psodsRaw = adltRaw.slice( @@ -7077,7 +7063,7 @@ template void LLVMELFDumper::printAdltSection() { "PSOD and blob entries are overlapped"); if (blob.data() + blob.size() > adltRaw.data() + adltRaw.size()) return this->reportUniqueWarning("invalid .adlt section: " - "blob is out section range"); + "blob is out of section range"); { ListScope LPsods(W, "PSODs"); @@ -7103,10 +7089,11 @@ template void LLVMELFDumper::printAdltSection() { } { DictScope DNEntry(W, "dt-needed"); - W.printHex("size", psod.dtNeeded.size); - W.printHex("offset", psod.dtNeeded.offset); + const auto& arr = psod.dtNeeded; + W.printHex("size", arr.size); + W.printHex("offset", arr.offset); - const auto chunk = blob.slice(psod.dtNeeded.offset, psod.dtNeeded.size); + const auto chunk = blob.slice(arr.offset, arr.size); if (!chunk.empty()) { W.printBinary("raw", chunk); @@ -7140,47 +7127,56 @@ template void LLVMELFDumper::printAdltSection() { const auto& arr = psod.phIndexes; W.printHex("size", arr.size); W.printHex("offset", arr.offset); - + const auto chunk = blob.slice(arr.offset, arr.size); if (!chunk.empty()) { + W.printBinary("raw", chunk); ArrayRef phIdxs( reinterpret_cast(chunk.data()), chunk.size() / sizeof(uint16_t)); - W.printBinary("raw", chunk); W.printList("values", phIdxs); } } - - if (ver.minor < 1) continue; - { - DictScope RDSEntry(W, "rela-dyn-segs"); - const auto& arr = psod.relaDynSegs; + DictScope RDEntry(W, "rela-dyn-idxs"); + const auto& arr = psod.relaDynIndx; W.printHex("size", arr.size); W.printHex("offset", arr.offset); const auto chunk = blob.slice(arr.offset, arr.size); - if (!chunk.empty()) - W.printBinary("raw", chunk); + if (!chunk.empty()) { + if (chunk.size() > kBinDumpLimit) + W.printString("raw", ""); + else + W.printBinary("raw", chunk); + } } { - DictScope RPSEntry(W, "rela-plt-segs"); - const auto& arr = psod.relaPltSegs; + DictScope RPEntry(W, "rela-plt-idsx"); + const auto& arr = psod.relaPltIndx; W.printHex("size", arr.size); W.printHex("offset", arr.offset); const auto chunk = blob.slice(arr.offset, arr.size); - if (!chunk.empty()) - W.printBinary("raw", chunk); + if (!chunk.empty()) { + if (chunk.size() > kBinDumpLimit) + W.printString("raw", ""); + else + W.printBinary("raw", chunk); + } } } } { - DictScope DBlob (W, "Blob"); + DictScope DBlob(W, "Blob"); W.printHex("start", header->blobStart); W.printHex("size", header->blobSize); - W.printBinaryBlock("raw", blob); + + if (blob.size() > kBinDumpLimit * 50) + W.printString("raw", ""); + else + W.printBinaryBlock("raw", blob); } } -- Gitee From 168411b9d5b164da07721439a7015a066a9831ed Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Mon, 22 Apr 2024 06:14:33 -0400 Subject: [PATCH 54/82] [ADLT] __cfi_check resolve. CFI align. Minor changes. Change-Id: I552703808967205cc562bcf0207a37e1c7fcd8f4 Signed-off-by: Anton Volkov --- lld/ELF/Config.h | 1 + lld/ELF/InputFiles.cpp | 32 ++++++++++++++++++++------------ lld/ELF/InputFiles.h | 1 - lld/ELF/Relocations.cpp | 12 ++++-------- lld/ELF/Writer.cpp | 3 +++ 5 files changed, 28 insertions(+), 21 deletions(-) diff --git a/lld/ELF/Config.h b/lld/ELF/Config.h index 9d93f0cfa638..945848b9da03 100644 --- a/lld/ELF/Config.h +++ b/lld/ELF/Config.h @@ -406,6 +406,7 @@ struct Ctx { backwardReferences; // ADLT stuff. + bool adltWithCfi = false; // From input .rela.dyn, .rela.plt: // Keep input library indexes that are needed for got/plt symbol llvm::DenseMap> diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp index 8be3a255cbde..6abd581ce33f 100644 --- a/lld/ELF/InputFiles.cpp +++ b/lld/ELF/InputFiles.cpp @@ -532,6 +532,14 @@ static StringRef markItemForAdlt(StringRef input, StringRef filePath) { return saver().save(input + mark); } +static bool markSymForAdlt(Symbol *s, StringRef filePath) { + StringRef newName = markItemForAdlt(s->getName(), filePath); + if (s->getName() == newName) + return false; + s->setName(newName); + return true; +} + template void ObjFile::initializeSections(bool ignoreComdats, const llvm::object::ELFFile &obj) { @@ -1054,8 +1062,15 @@ void ObjFile::initializeSymbols(const object::ELFFile &obj) { // Some entries have been filled by LazyObjFile. for (size_t i = firstGlobal, end = eSyms.size(); i != end; ++i) - if (!symbols[i]) - symbols[i] = symtab.insert(CHECK(eSyms[i].getName(stringTable), this)); + if (!symbols[i]) { + StringRef name = CHECK(eSyms[i].getName(stringTable), this); + if (config->adlt && name == "__cfi_check") { + name = markItemForAdlt(name, archiveName); + if (!ctx->adltWithCfi) + ctx->adltWithCfi = true; + } + symbols[i] = symtab.insert(name); + } // Perform symbol resolution on non-local symbols. SmallVector undefineds; @@ -1701,11 +1716,7 @@ StringRef SharedFileExtended::addAdltPostfix(StringRef input) const { template bool SharedFileExtended::addAdltPostfix(Symbol *s) { - StringRef newName = markItemForAdlt(s->getName(), soName); - if (s->getName() == newName) - return false; - s->setName(newName); - return true; + return markSymForAdlt(s, this->soName); } // TODO: optimize 2 lookups @@ -1781,8 +1792,7 @@ bool SharedFileExtended::isDynamicSection(InputSectionBase &sec) const { template Defined *SharedFileExtended::findDefinedSymbol( - uint64_t offset, StringRef fatalTitle, - llvm::function_ref extraCond) const { + uint64_t offset, llvm::function_ref extraCond) const { bool isDebug = false; /*if (offset == 0x7738) // debug hint isDebug = true;*/ @@ -1810,9 +1820,7 @@ Defined *SharedFileExtended::findDefinedSymbol( } auto *sectionSym = findSectionSymbol(offset); - if (!sectionSym) - fatal(fatalTitle + " 0x" + utohexstr(offset) + "\n"); - if (isDebug) + if (isDebug && sectionSym) traceSymbol(*sectionSym, "found section sym: "); return sectionSym; } diff --git a/lld/ELF/InputFiles.h b/lld/ELF/InputFiles.h index dce2d1bda741..bd880dc712a8 100644 --- a/lld/ELF/InputFiles.h +++ b/lld/ELF/InputFiles.h @@ -407,7 +407,6 @@ public: Defined *findSectionSymbol(uint64_t offset) const; Defined *findDefinedSymbol( uint64_t offset, - StringRef fatalTitle = "defined symbol not found! offset:", llvm::function_ref extraCond = [](Defined *) { return true; }) const; diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp index ff8f1002ab44..1e953665722c 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -1338,8 +1338,6 @@ void RelocationScanner::processForADLT(const RelTy &rel, Relocation *r, bool fromDynamic) { auto file = sec.getSharedFile(); uint32_t symIndex = rel.getSymbol(config->isMips64EL); - std::string title = - "processForADLT: symIndex: " + std::to_string(symIndex) + " "; bool isDebug = false; /*if (r->offset == 0x21F) // debug hint @@ -1349,11 +1347,11 @@ void RelocationScanner::processForADLT(const RelTy &rel, Relocation *r, isDebug = true;*/ // parse offset (where) - std::string failTitle = title + "symWhere not found! offset: "; InputSectionBase *secWhere = cast( fromDynamic - ? file->findDefinedSymbol(r->offset, failTitle)->section + ? file->findDefinedSymbol(r->offset)->section : (r->sym->isDefined() ? cast(r->sym)->section : &sec)); + assert(secWhere && "not found!"); // process offset r->offset -= fromDynamic ? secWhere->address : sec.address; @@ -1366,10 +1364,8 @@ void RelocationScanner::processForADLT(const RelTy &rel, Relocation *r, switch (r->type) { // dyn relocs case R_AARCH64_RELATIVE: { - failTitle = - title + " " + toString(r->type) + ": r->sym not found! addend: "; - Defined *d = file->findDefinedSymbol(r->addend, failTitle); - assert(d); + Defined *d = file->findDefinedSymbol(r->addend); + assert(d && "R_AARCH64_RELATIVE: r->sym not found by addend!"); r->sym = d; r->addend -= d->section->address + d->value; addRelativeReloc(*secWhere, r->offset, *r->sym, r->addend, r->expr, diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index 0ac9bb292050..51303efa219b 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -1033,6 +1033,9 @@ void PhdrEntry::add(OutputSection *sec) { lastSec = sec; if (!firstSec) firstSec = sec; + bool adltCFI = config->adlt && ctx->adltWithCfi && p_type == PT_LOAD; + if (adltCFI) // check cfi.h: LIBRARY_ALIGNMENT and _BITS + sec->alignment = 1UL << 18; p_align = std::max(p_align, sec->alignment); if (p_type == PT_LOAD) sec->ptLoad = this; -- Gitee From 06905d366953696994d6c87a57bb2db73a1fe664 Mon Sep 17 00:00:00 2001 From: Khomutov Nikita Date: Mon, 22 Apr 2024 14:53:27 +0300 Subject: [PATCH 55/82] [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 dce2d1bda741..2db10eb36b04 100644 --- a/lld/ELF/InputFiles.h +++ b/lld/ELF/InputFiles.h @@ -462,8 +462,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 cd864ca201f2f937221fff4d0fc2fd948a87cbda Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Tue, 23 Apr 2024 10:22:10 -0400 Subject: [PATCH 56/82] [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 6abd581ce33f..2a0ee3b04dc5 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 1e953665722c..eb21da94ce62 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -1337,7 +1337,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 @@ -1347,10 +1346,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 @@ -1400,8 +1396,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: @@ -1421,6 +1415,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 41c61ec15a9bd59b861780286e33a05061599e47 Mon Sep 17 00:00:00 2001 From: Khomutov Nikita Date: Mon, 22 Apr 2024 14:27:23 +0300 Subject: [PATCH 57/82] [ADLT] generate shorten names for libraries symbols and sections Signed-off-by: Khomutov Nikita Change-Id: I9b457e1da09a3117c1205901d797d67503605efc --- lld/ELF/Config.h | 2 +- lld/ELF/InputFiles.cpp | 51 ++++++++++++--------------- lld/ELF/InputFiles.h | 8 +++-- lld/ELF/Relocations.cpp | 2 +- lld/ELF/Writer.cpp | 1 - llvm/tools/llvm-readobj/ELFDumper.cpp | 11 +++++- 6 files changed, 39 insertions(+), 36 deletions(-) diff --git a/lld/ELF/Config.h b/lld/ELF/Config.h index 945848b9da03..6583d7d8e562 100644 --- a/lld/ELF/Config.h +++ b/lld/ELF/Config.h @@ -410,7 +410,7 @@ struct Ctx { // From input .rela.dyn, .rela.plt: // Keep input library indexes that are needed for got/plt symbol llvm::DenseMap> - gotPltInfoAdlt; // sym, soFile->ctxIdx array; + gotPltInfoAdlt; // sym, soFile->orderIdx array; }; // The only instance of Ctx struct. diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp index 6abd581ce33f..5c9b51fb2131 100644 --- a/lld/ELF/InputFiles.cpp +++ b/lld/ELF/InputFiles.cpp @@ -401,6 +401,11 @@ StringRef ObjFile::getShtGroupSignature(ArrayRef sections, return CHECK(sym.getName(this->stringTable), this); } +template +StringRef ObjFile::getUniqueName(StringRef origName) const { + return origName; +} + template bool ObjFile::shouldMerge(const Elf_Shdr &sec, StringRef name) { // On a regular link we don't merge sections if -O0 (default is -O1). This @@ -522,24 +527,6 @@ ArrayRef ObjFile::getShndxTable() { return shndxTable; } -static StringRef markItemForAdlt(StringRef input, StringRef filePath) { - if (input.empty()) - return input; - StringRef simpleFile = path::stem(filePath); - std::string mark = "_" + simpleFile.str(); - if (input.endswith(mark)) - return input; - return saver().save(input + mark); -} - -static bool markSymForAdlt(Symbol *s, StringRef filePath) { - StringRef newName = markItemForAdlt(s->getName(), filePath); - if (s->getName() == newName) - return false; - s->setName(newName); - return true; -} - template void ObjFile::initializeSections(bool ignoreComdats, const llvm::object::ELFFile &obj) { @@ -581,7 +568,7 @@ void ObjFile::initializeSections(bool ignoreComdats, } auto secName = check(obj.getSectionName(sec, shstrtab)); if (config->adlt && sec.sh_type == SHT_NULL) { - auto name = isPatchedSecName ? markItemForAdlt(secName, archiveName) : secName; + auto name = getUniqueName(secName); this->sections[i] = createInputSection(i, sec, name); this->sections[i]->address = sec.sh_addr; this->sections[i]->size = sec.sh_size; @@ -608,9 +595,7 @@ void ObjFile::initializeSections(bool ignoreComdats, .second; if (keepGroup) { if (config->relocatable) { - auto name = config->adlt && isPatchedSecName - ? markItemForAdlt(secName, archiveName) - : secName; + auto name = config->adlt ? getUniqueName(secName) : secName; this->sections[i] = createInputSection(i, sec, name); if (config->adlt) { this->sections[i]->address = sec.sh_addr; @@ -643,9 +628,7 @@ void ObjFile::initializeSections(bool ignoreComdats, ctx->hasSympart.store(true, std::memory_order_relaxed); LLVM_FALLTHROUGH; default: - auto name = config->adlt && isPatchedSecName - ? markItemForAdlt(secName, archiveName) - : secName; + auto name = config->adlt ? getUniqueName(secName) : secName; this->sections[i] = createInputSection(i, sec, name); if (config->adlt) { this->sections[i]->address = sec.sh_addr; @@ -1065,7 +1048,7 @@ void ObjFile::initializeSymbols(const object::ELFFile &obj) { if (!symbols[i]) { StringRef name = CHECK(eSyms[i].getName(stringTable), this); if (config->adlt && name == "__cfi_check") { - name = markItemForAdlt(name, archiveName); + name = this->getUniqueName(name); if (!ctx->adltWithCfi) ctx->adltWithCfi = true; } @@ -1708,15 +1691,25 @@ template void SharedFileExtended::postParseForAdlt() { resolveDuplicatesForAdlt(); } - template StringRef SharedFileExtended::addAdltPostfix(StringRef input) const { - return markItemForAdlt(input, soName); + if (input.empty()) return input; + auto suffix = Twine("__") + Twine::utohexstr(this->orderIdx); + return saver().save(input + suffix); } template bool SharedFileExtended::addAdltPostfix(Symbol *s) { - return markSymForAdlt(s, this->soName); + StringRef newName = addAdltPostfix(s->getName()); + if (s->getName() == newName) + return false; + s->setName(newName); + return true; +} + +template +StringRef SharedFileExtended::getUniqueName(StringRef origName) const { + return addAdltPostfix(origName); } // TODO: optimize 2 lookups diff --git a/lld/ELF/InputFiles.h b/lld/ELF/InputFiles.h index bd880dc712a8..5beec601526c 100644 --- a/lld/ELF/InputFiles.h +++ b/lld/ELF/InputFiles.h @@ -294,7 +294,8 @@ public: void initializeLocalSymbols(); void postParse(); - bool isPatchedSecName = true; // ADLT +protected: + virtual StringRef getUniqueName(StringRef origName) const; private: void initializeSections(bool ignoreComdats, @@ -456,8 +457,6 @@ public: // .symtab's start of global symbols owned by library llvm::Optional sharedGlobalSymbolIndex; - // Index of this file in context (struct Ctx) - unsigned ctxIdx = 0; // Output information data: llvm::SetVector programHeaderIndexes; // From input .rela.dyn, .rela.plt: @@ -481,6 +480,9 @@ public: // parsed. Only filled for `--no-allow-shlib-undefined`. SmallVector requiredSymbols; +protected: + virtual StringRef getUniqueName(StringRef origName) const override; + private: void parseDynamics(); // SharedFile compability void parseElfSymTab(); // ObjectFile compability diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp index 1e953665722c..20d232820b34 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -1311,7 +1311,7 @@ static void trackDynRelocAdlt(SharedFileExtended *soFile) { template static void trackGotPltAdlt(Symbol *sym, SharedFileExtended *soFile) { - ctx->gotPltInfoAdlt[sym].push_back(soFile->ctxIdx); + ctx->gotPltInfoAdlt[sym].push_back(soFile->orderIdx); } // ADLT BEGIN diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index 51303efa219b..36d222caf77c 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -2006,7 +2006,6 @@ template void Writer::finalizeSections() { if (config->adlt) for (auto &it : llvm::enumerate(ctx->sharedFilesExtended)) { auto *soFile = cast>(it.value()); - soFile->ctxIdx = it.index(); auto sections = soFile->getSections(); // scan .rela.dyn (base: SHT_NULL) scanRelocations(*sections[0]); diff --git a/llvm/tools/llvm-readobj/ELFDumper.cpp b/llvm/tools/llvm-readobj/ELFDumper.cpp index 48e9ec0d2c25..07e2fbaefaf1 100644 --- a/llvm/tools/llvm-readobj/ELFDumper.cpp +++ b/llvm/tools/llvm-readobj/ELFDumper.cpp @@ -7072,7 +7072,16 @@ template void LLVMELFDumper::printAdltSection() { const adlt_psod_t& psod = *reinterpret_cast (psodsRaw.data() + psodIdx * header->schemaPSODSize); - DictScope LEntry(W, ("#" + Twine(psodIdx)).str()); + auto psodName = Twine("psod-#") + Twine(psodIdx); + DictScope LEntry(W, psodName.str()); + + { + DictScope MetaEntry(W, "$-meta"); + auto psodSuffix = Twine("__") + Twine::utohexstr(psodIdx); + W.printNumber("$-order", psodIdx); + W.printString("$-symbol-suffix", psodSuffix.str()); + } + W.printString("soname", this->getAdltDynamicString(psod.soName)); W.printHex("soname-hash", psod.soNameHash); { -- Gitee From 13fae69605006ae8f74c688d308b60b1ab26fbc0 Mon Sep 17 00:00:00 2001 From: likholatovevgeny Date: Wed, 24 Apr 2024 14:33:38 +0300 Subject: [PATCH 58/82] 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 0ac9bb292050..39b49870a55c 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -2540,9 +2540,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 271055fcc2de7242e66b905bc448e2e9e8f88b5f Mon Sep 17 00:00:00 2001 From: likholatovevgeny Date: Thu, 25 Apr 2024 15:15:43 +0300 Subject: [PATCH 59/82] 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 39b49870a55c..e6e33a53367d 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -2542,12 +2542,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 c7ad42b1c0aed1527319ec087a5102398c176aec Mon Sep 17 00:00:00 2001 From: Khomutov Nikita Date: Thu, 2 May 2024 15:35:24 +0300 Subject: [PATCH 60/82] [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 ae5acc84626988d7beeb9046f72be75b39431cbc Mon Sep 17 00:00:00 2001 From: likholatovevgeny Date: Fri, 3 May 2024 16:27:35 +0300 Subject: [PATCH 61/82] 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 de98d39e075a..54aac413327b 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 6f205da4a8ee6373a3814ceff061245b06f2fb9d Mon Sep 17 00:00:00 2001 From: likholatovevgeny Date: Fri, 3 May 2024 17:25:35 +0300 Subject: [PATCH 62/82] 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 54aac413327b..58b502cc2847 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 fad33bb299e271d03c5bbf4a4fe2d462583bbc46 Mon Sep 17 00:00:00 2001 From: likholatovevgeny Date: Fri, 3 May 2024 17:49:02 +0300 Subject: [PATCH 63/82] 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 58b502cc2847..47d21853cc6d 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 9e425beed91ba64e2768c974c0d4be709be01e56 Mon Sep 17 00:00:00 2001 From: likholatovevgeny Date: Mon, 6 May 2024 13:52:48 +0300 Subject: [PATCH 64/82] Fixed bugs of copying same files and creating existing symlink. Signed-off-by: likholatovevgeny --- llvm-build/build.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/llvm-build/build.py b/llvm-build/build.py index 88209a79ff47..6cc2e6e720d2 100755 --- a/llvm-build/build.py +++ b/llvm-build/build.py @@ -530,6 +530,11 @@ class BuildUtils(object): subprocess.check_call(cmd, *args, **kwargs) + def force_symlink(self, src, dst): + if os.path.exists(dst): + os.remove(dst) + os.symlink(src, dst) + def merge_out_path(self, *args): return os.path.abspath(os.path.join(self.build_config.OUT_PATH, *args)) @@ -719,17 +724,16 @@ class LlvmCore(BuildUtils): target_dir = f"{build_dir}/{target_dir}" if not os.path.exists(target_dir): continue - for (root, dirs, files) in os.walk(target_dir): - src_path = root - dst_path = root.replace(build_dir, install_dir) + for (src_path, dirs, files) in os.walk(target_dir): + dst_path = src_path.replace(build_dir, install_dir) for file in files: if file.endswith(".cpp.o") or file == "cmake_install.cmake": continue - os.makedirs(dst_path, exist_ok=True) - shutil.copy2( - os.path.join(src_path, file), - os.path.join(dst_path, file) - ) + src = os.path.join(src_path, file) + dst = os.path.join(dst_path, file) + if os.stat(src) != os.stat(dst): + os.makedirs(dst_path, exist_ok=True) + shutil.copy2(src, dst) def llvm_compile_darwin_defines(self, llvm_defines): if self.host_is_darwin(): @@ -1938,7 +1942,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 b399c4b2da57119698995a51e7ce52dc5c743d1d Mon Sep 17 00:00:00 2001 From: likholatovevgeny Date: Mon, 6 May 2024 16:28:55 +0300 Subject: [PATCH 65/82] Fixed copying for partly build. Signed-off-by: likholatovevgeny --- llvm-build/build.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/llvm-build/build.py b/llvm-build/build.py index 6cc2e6e720d2..3af3da30e7b1 100755 --- a/llvm-build/build.py +++ b/llvm-build/build.py @@ -727,13 +727,14 @@ class LlvmCore(BuildUtils): for (src_path, dirs, files) in os.walk(target_dir): dst_path = src_path.replace(build_dir, install_dir) for file in files: - if file.endswith(".cpp.o") or file == "cmake_install.cmake": - continue src = os.path.join(src_path, file) dst = os.path.join(dst_path, file) - if os.stat(src) != os.stat(dst): - os.makedirs(dst_path, exist_ok=True) - shutil.copy2(src, dst) + if file.endswith(".cpp.o") or file == "cmake_install.cmake": + continue + if os.path.exists(dst) and os.stat(src) == os.stat(dst): + continue + os.makedirs(dst_path, exist_ok=True) + shutil.copy2(src, dst) def llvm_compile_darwin_defines(self, llvm_defines): if self.host_is_darwin(): -- Gitee From 4525a32782788726e0e5279ce84138073990fde9 Mon Sep 17 00:00:00 2001 From: likholatovevgeny Date: Tue, 7 May 2024 11:25:18 +0300 Subject: [PATCH 66/82] 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 47d21853cc6d..39a974d6aef2 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 8c2ab29e318d8b29ba00a8213d758b52fea1ff3d Mon Sep 17 00:00:00 2001 From: Khomutov Nikita Date: Wed, 8 May 2024 12:20:39 +0300 Subject: [PATCH 67/82] [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/include/llvm/BinaryFormat/ADLTSection.h | 3 +- llvm/tools/llvm-readobj/ELFDumper.cpp | 52 +++++++++++++++----- 10 files changed, 135 insertions(+), 35 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 3c57bee97e4b..af62fa7cca0b 100644 --- a/lld/ELF/Driver.cpp +++ b/lld/ELF/Driver.cpp @@ -1095,6 +1095,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 39a974d6aef2..23d9512dfb1d 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 936a4a2a4bb5..798440141025 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -1311,7 +1311,7 @@ static void trackDynRelocAdlt(SharedFileExtended *soFile) { template static void trackGotPltAdlt(Symbol *sym, SharedFileExtended *soFile) { - ctx->gotPltInfoAdlt[sym].push_back(soFile->orderIdx); + ctx->adlt.gotPltInfo[sym].push_back(soFile->orderIdx); } // ADLT BEGIN @@ -1808,7 +1808,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/include/llvm/BinaryFormat/ADLTSection.h b/llvm/include/llvm/BinaryFormat/ADLTSection.h index ac883cb30da8..073f24573768 100644 --- a/llvm/include/llvm/BinaryFormat/ADLTSection.h +++ b/llvm/include/llvm/BinaryFormat/ADLTSection.h @@ -107,11 +107,12 @@ 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_u16_array_t phIndexes; // program header indexes, typeof(e_phnum) } adlt_section_header_t; static const char adltBlobStartMark[4] = { 0xA, 0xD, 0x1, 0x7 }; -static const adlt_semver_t adltSchemaVersion = {1, 0, 0}; +static const adlt_semver_t adltSchemaVersion = {1, 1, 0}; #ifdef __cplusplus } // namespace adlt diff --git a/llvm/tools/llvm-readobj/ELFDumper.cpp b/llvm/tools/llvm-readobj/ELFDumper.cpp index 07e2fbaefaf1..b6de5e52ee58 100644 --- a/llvm/tools/llvm-readobj/ELFDumper.cpp +++ b/llvm/tools/llvm-readobj/ELFDumper.cpp @@ -7033,6 +7033,16 @@ template void LLVMELFDumper::printAdltSection() { auto* header = reinterpret_cast(adltRaw.data()); const auto& ver = header->schemaVersion; + 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 { @@ -7051,19 +7061,35 @@ template void LLVMELFDumper::printAdltSection() { W.printHex("blob-start", header->blobStart); W.printHex("blob-size", header->blobSize); W.printHex("overall-mapped-size", header->overallMappedSize); - } while(0); - - ArrayRef psodsRaw = adltRaw.slice( - header->schemaHeaderSize, - header->sharedObjectsNum * header->schemaPSODSize); - ArrayRef blob = adltRaw.slice(header->blobStart, header->blobSize); - 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"); + psodsRaw = adltRaw.slice( + header->schemaHeaderSize, + header->sharedObjectsNum * header->schemaPSODSize); + blob = adltRaw.slice(header->blobStart, header->blobSize); + + 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"); + + if (ver.minor >= 1) { + DictScope PHEntry(W, "ph-indexes"); + const auto& arr = header->phIndexes; + W.printHex("size", arr.size); + W.printHex("offset", arr.offset); + + const auto chunk = blob.slice(arr.offset, arr.size); + if (!chunk.empty()) { + W.printBinary("raw", chunk); + ArrayRef phIdxs( + reinterpret_cast(chunk.data()), + chunk.size() / sizeof(uint16_t)); + W.printList("values", phIdxs); + } + } + } while(0); { ListScope LPsods(W, "PSODs"); @@ -7132,7 +7158,7 @@ template void LLVMELFDumper::printAdltSection() { W.printHex("offset", csref.offset); } { - DictScope PHEntry(W, "ph-index"); + DictScope PHEntry(W, "ph-indexes"); const auto& arr = psod.phIndexes; W.printHex("size", arr.size); W.printHex("offset", arr.offset); -- Gitee From c16b183876a58430b2480c5e404c3915a7bcb6ab Mon Sep 17 00:00:00 2001 From: likholatovevgeny Date: Mon, 13 May 2024 14:41:17 +0300 Subject: [PATCH 68/82] Fixed bug of building compiler-rt and libcxx. Signed-off-by: likholatovevgeny --- llvm-build/LLVMExports.cmake | 1150 ++++++++++++++++++++++++++++++++++ llvm-build/build.py | 17 +- 2 files changed, 1166 insertions(+), 1 deletion(-) 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 3af3da30e7b1..75ad1d7ce6d5 100755 --- a/llvm-build/build.py +++ b/llvm-build/build.py @@ -719,7 +719,7 @@ class LlvmCore(BuildUtils): self.llvm_python_install(build_dir, install_dir) def llvm_python_install(self, build_dir, install_dir): - target_dirs = ["bin", "include", "lib", "python3", "libexec", "share"] + target_dirs = ["bin", "include", "lib", "libexec", "share"] for target_dir in target_dirs: target_dir = f"{build_dir}/{target_dir}" if not os.path.exists(target_dir): @@ -1113,6 +1113,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, @@ -2760,6 +2772,9 @@ def main(): return if build_config.build_only: + sysroot_composer.setup_cmake_platform(llvm_install) + sysroot_composer.replace_cmake_llvm_exports(llvm_install) + if "musl" in build_config.build_only_libs: # change compiller path to prebuilds in clang.gni file clang_gni = os.path.join(build_config.REPOROOT_DIR, "build", "config", "clang", "clang.gni") -- Gitee From 7aaa9cba3ab9e53bc853990d91d4697586b96c37 Mon Sep 17 00:00:00 2001 From: likholatovevgeny Date: Mon, 13 May 2024 16:44:31 +0300 Subject: [PATCH 69/82] removed LLVMExports.cmake Signed-off-by: likholatovevgeny --- llvm-build/LLVMExports.cmake | 1150 ---------------------------------- llvm-build/build.py | 24 +- 2 files changed, 9 insertions(+), 1165 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 75ad1d7ce6d5..978cf38a05e0 100755 --- a/llvm-build/build.py +++ b/llvm-build/build.py @@ -716,9 +716,9 @@ class LlvmCore(BuildUtils): target=build_target, install=install) if not install: - self.llvm_python_install(build_dir, install_dir) + self.llvm_install(build_dir, install_dir) - def llvm_python_install(self, build_dir, install_dir): + def llvm_install(self, build_dir, install_dir): target_dirs = ["bin", "include", "lib", "libexec", "share"] for target_dir in target_dirs: target_dir = f"{build_dir}/{target_dir}" @@ -1113,18 +1113,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, @@ -2773,7 +2761,11 @@ def main(): if build_config.build_only: sysroot_composer.setup_cmake_platform(llvm_install) - sysroot_composer.replace_cmake_llvm_exports(llvm_install) + # temporary hide cmake checks for sdk-partly + lib_cmake = os.path.join(build_config.REPOROOT_DIR, 'prebuilts/clang/ohos', build_utils.use_platform(), + 'clang-%s' % build_config.CLANG_VERSION, "lib/cmake") + lib_cmake_tmp = f"{lib_cmake}_tmp" + shutil.move(lib_cmake, lib_cmake_tmp) if "musl" in build_config.build_only_libs: # change compiller path to prebuilds in clang.gni file @@ -2802,6 +2794,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 79774152c4977891312a3f089440e62e9f061eeb Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Wed, 15 May 2024 06:04:00 -0400 Subject: [PATCH 70/82] [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 e4187ce13cf80f75151c7fa33f1324bee865c58d Mon Sep 17 00:00:00 2001 From: likholatovevgeny Date: Wed, 15 May 2024 16:31:51 +0300 Subject: [PATCH 71/82] 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 23d9512dfb1d..973978cb8022 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 ea70a796e7af69417f3c49532654d173b95eba7a Mon Sep 17 00:00:00 2001 From: likholatovevgeny Date: Wed, 15 May 2024 17:57:22 +0300 Subject: [PATCH 72/82] 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 cd768db8a7ff..769eb6d38fd4 100644 --- a/lld/ELF/Config.h +++ b/lld/ELF/Config.h @@ -403,9 +403,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 af62fa7cca0b..2e4a1059a870 100644 --- a/lld/ELF/Driver.cpp +++ b/lld/ELF/Driver.cpp @@ -2570,12 +2570,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 973978cb8022..cbbd8c760a85 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 7586f99a02e7ca47f8a8d16f7573b97e21bcb3c0 Mon Sep 17 00:00:00 2001 From: likholatovevgeny Date: Thu, 16 May 2024 14:45:17 +0300 Subject: [PATCH 73/82] 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 769eb6d38fd4..dcab83d6e5fc 100644 --- a/lld/ELF/Config.h +++ b/lld/ELF/Config.h @@ -420,7 +420,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 cbbd8c760a85..fd34f830eefb 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 5a97ecf91a9b307ee010d33b751c7ec09cac0bf7 Mon Sep 17 00:00:00 2001 From: likholatovevgeny Date: Fri, 17 May 2024 10:33:42 +0300 Subject: [PATCH 74/82] 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 dcab83d6e5fc..2c85d4c066f3 100644 --- a/lld/ELF/Config.h +++ b/lld/ELF/Config.h @@ -418,11 +418,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 2e4a1059a870..a4ee33280065 100644 --- a/lld/ELF/Driver.cpp +++ b/lld/ELF/Driver.cpp @@ -2568,14 +2568,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 fd34f830eefb..0b6ac03faff1 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 b2e73f8e14d0e0d46197a2f75cdfe282a913bc5a Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Thu, 23 May 2024 10:16:57 -0400 Subject: [PATCH 75/82] 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 a4ee33280065..c901197ed3bd 100644 --- a/lld/ELF/Driver.cpp +++ b/lld/ELF/Driver.cpp @@ -849,7 +849,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; @@ -1828,7 +1828,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); @@ -2025,7 +2025,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; @@ -2102,7 +2102,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(); @@ -2357,7 +2357,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)) @@ -2393,7 +2393,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 98ac76a9310deeaab1717665d516f097ee742fb1 Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Fri, 24 May 2024 03:17:26 -0400 Subject: [PATCH 76/82] 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 c901197ed3bd..ce6bed6b6776 100644 --- a/lld/ELF/Driver.cpp +++ b/lld/ELF/Driver.cpp @@ -2508,7 +2508,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; @@ -2724,7 +2724,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, @@ -2741,11 +2740,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 0eb2968413bd..7dc198d7b1a9 100644 --- a/lld/ELF/InputSection.cpp +++ b/lld/ELF/InputSection.cpp @@ -963,9 +963,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 d4081a2655121fb278912fbb8fa004fee0a96130 Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Fri, 24 May 2024 05:30:25 -0400 Subject: [PATCH 77/82] 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 b37035d3e742..de8b3bf51d58 100644 --- a/lld/ELF/CMakeLists.txt +++ b/lld/ELF/CMakeLists.txt @@ -45,6 +45,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 ce6bed6b6776..c3a8a3ba4121 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); @@ -2570,12 +2572,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(); } @@ -2597,7 +2600,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; @@ -2655,7 +2658,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); @@ -2764,7 +2767,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 0b6ac03faff1..d4b0519a11a1 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 7dc198d7b1a9..275f5a2202be 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); @@ -847,7 +847,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 798440141025..590ff31b1bcf 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" @@ -1311,14 +1312,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) + @@ -1336,7 +1337,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 @@ -1464,11 +1465,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. @@ -1808,9 +1808,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); @@ -1923,7 +1923,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 ffb574f5308b65065551ced37bac3cfbeecb2810 Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Fri, 24 May 2024 07:48:22 -0400 Subject: [PATCH 78/82] 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 c3a8a3ba4121..fa2afc8e6a6d 100644 --- a/lld/ELF/Driver.cpp +++ b/lld/ELF/Driver.cpp @@ -2498,27 +2498,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) { @@ -2565,6 +2544,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) @@ -2572,14 +2554,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 @@ -2767,12 +2743,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 d4b0519a11a1..7db5dcb675ae 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 590ff31b1bcf..fa1884c6d14c 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -466,9 +466,6 @@ private: // ADLT template void processForADLT(const RelTy &rel, Relocation *r, bool fromDynamic); - - template - void tracePushRelocADLT(InputSectionBase &isec, Relocation &r) const; }; } // namespace @@ -1316,24 +1313,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) { @@ -1354,9 +1333,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 597b2afe88456584684189eb2ed177b696fd59f5 Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Fri, 24 May 2024 11:03:35 -0400 Subject: [PATCH 79/82] 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 fa2afc8e6a6d..3f3726f2644e 100644 --- a/lld/ELF/Driver.cpp +++ b/lld/ELF/Driver.cpp @@ -2479,19 +2479,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(""); @@ -2634,7 +2634,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 7db5dcb675ae..3514782dd7c8 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 275f5a2202be..bf2b1d7c5521 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); @@ -847,8 +846,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 fa1884c6d14c..ea1acb552c81 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -1326,22 +1326,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; @@ -1365,7 +1365,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); @@ -1441,9 +1441,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 78b2947ff4330a862da3461a648abf4609a900bf Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Fri, 24 May 2024 11:21:28 -0400 Subject: [PATCH 80/82] 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 ea1acb552c81..f248007b6287 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -434,6 +434,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 { @@ -459,6 +504,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); @@ -1034,6 +1080,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 @@ -1302,23 +1353,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 && @@ -1330,7 +1367,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 @@ -1343,20 +1380,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: @@ -1365,10 +1403,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); @@ -1380,7 +1418,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: @@ -1398,24 +1436,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: @@ -1439,11 +1474,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; @@ -1478,7 +1513,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; } @@ -1783,15 +1820,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) { @@ -1805,12 +1851,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()) { @@ -1845,7 +1891,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 f08950f339899831c10b7f002769ee92d136aae7 Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Wed, 5 Jun 2024 03:53:41 -0400 Subject: [PATCH 81/82] 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 +++++++------ 9 files changed, 124 insertions(+), 133 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 3f3726f2644e..88ef20d59014 100644 --- a/lld/ELF/Driver.cpp +++ b/lld/ELF/Driver.cpp @@ -2555,7 +2555,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 3514782dd7c8..df9355f3b625 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 f248007b6287..c67ec01bf27a 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -444,21 +444,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 @@ -466,16 +464,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 @@ -508,10 +504,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 @@ -1379,9 +1371,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: @@ -1406,7 +1397,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); @@ -1473,10 +1464,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); @@ -1729,11 +1720,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); @@ -1829,8 +1815,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 @@ -1891,7 +1878,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); } } -- Gitee From 50adab9ccf12f6cdb17f6c72e669aacd00cb7728 Mon Sep 17 00:00:00 2001 From: Lyupa Anastasia Date: Mon, 13 May 2024 15:28:22 +0300 Subject: [PATCH 82/82] [build] Add script to build musl/libc-test Issue: https://gitee.com/openharmony/third_party_llvm-project/issues/I9Q7SL Signed-off-by: Lyupa Anastasia --- llvm-build/README.md | 14 +++++++++++++ llvm-build/build-libc-test.py | 38 +++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100755 llvm-build/build-libc-test.py diff --git a/llvm-build/README.md b/llvm-build/README.md index fbf67c6362eb..ca4e7d41781a 100644 --- a/llvm-build/README.md +++ b/llvm-build/README.md @@ -210,3 +210,17 @@ Despite all the components provided by LLVM community, we included several tripl For detailed definition of Small System and Standard System, please refer to [System Types](https://gitee.com/openharmony/docs/blob/master/en/device-dev/Readme-EN.md). +### Testing musl libc + +Toolchain build process includes musl libc build. libc.so is available in sysroot. +Sometimes it's needed to build libc tests. + +Here is an example of starting build process on Linux: +``` +# build +python3 ./toolchain/llvm-project/llvm-build/build-libc-test.py +``` + +When build successfully completed, artifacts will be available in `out/llvm_build/musl` directory, including test libraries, libc tests and musl_unittest. +Scripts to execute libc tests could be found in `third_party/musl/scripts` directory. +For detailed information about musl, please refer to [third_party_musl](https://gitee.com/openharmony/third_party_musl). diff --git a/llvm-build/build-libc-test.py b/llvm-build/build-libc-test.py new file mode 100755 index 000000000000..13be68413d4b --- /dev/null +++ b/llvm-build/build-libc-test.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python3 +# 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. + +import os + +from build import BuildConfig, SysrootComposer + +def main(): + print('Start building musl libc tests') + build_config = BuildConfig() + sysroot_composer = SysrootComposer(build_config) + + product_name = 'llvm_build' + target_cpu = 'arm64' + gn_args = '' + + os.chdir(build_config.LLVM_BUILD_DIR) + sysroot_composer.run_hb_build(product_name, target_cpu, 'libctest', gn_args) + sysroot_composer.run_hb_build(product_name, target_cpu, 'libc_gtest', gn_args) + + +if __name__ == "__main__": + main() + + + -- Gitee