134 lines · cpp
1//===- COFFObject.cpp -----------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#include "COFFObject.h"10#include "llvm/ADT/DenseSet.h"11 12namespace llvm {13namespace objcopy {14namespace coff {15 16using namespace object;17 18void Object::addSymbols(ArrayRef<Symbol> NewSymbols) {19 for (Symbol S : NewSymbols) {20 S.UniqueId = NextSymbolUniqueId++;21 S.OriginalRawIndex = NextSymbolOriginalIndex;22 NextSymbolOriginalIndex += 1 + S.Sym.NumberOfAuxSymbols;23 Symbols.emplace_back(S);24 }25 updateSymbols();26}27 28void Object::updateSymbols() {29 SymbolMap = DenseMap<size_t, Symbol *>(Symbols.size());30 for (Symbol &Sym : Symbols)31 SymbolMap[Sym.UniqueId] = &Sym;32}33 34const Symbol *Object::findSymbol(size_t UniqueId) const {35 return SymbolMap.lookup(UniqueId);36}37 38Error Object::removeSymbols(39 function_ref<Expected<bool>(const Symbol &)> ToRemove) {40 Error Errs = Error::success();41 llvm::erase_if(Symbols, [ToRemove, &Errs](const Symbol &Sym) {42 Expected<bool> ShouldRemove = ToRemove(Sym);43 if (!ShouldRemove) {44 Errs = joinErrors(std::move(Errs), ShouldRemove.takeError());45 return false;46 }47 return *ShouldRemove;48 });49 50 updateSymbols();51 return Errs;52}53 54Error Object::markSymbols() {55 for (Symbol &Sym : Symbols)56 Sym.Referenced = false;57 for (const Section &Sec : Sections) {58 for (const Relocation &R : Sec.Relocs) {59 auto It = SymbolMap.find(R.Target);60 if (It == SymbolMap.end())61 return createStringError(object_error::invalid_symbol_index,62 "relocation target %zu not found", R.Target);63 It->second->Referenced = true;64 }65 }66 return Error::success();67}68 69void Object::addSections(ArrayRef<Section> NewSections) {70 for (Section S : NewSections) {71 S.UniqueId = NextSectionUniqueId++;72 Sections.emplace_back(S);73 }74 updateSections();75}76 77void Object::updateSections() {78 SectionMap = DenseMap<ssize_t, Section *>(Sections.size());79 size_t Index = 1;80 for (Section &S : Sections) {81 SectionMap[S.UniqueId] = &S;82 S.Index = Index++;83 }84}85 86const Section *Object::findSection(ssize_t UniqueId) const {87 return SectionMap.lookup(UniqueId);88}89 90void Object::removeSections(function_ref<bool(const Section &)> ToRemove) {91 DenseSet<ssize_t> AssociatedSections;92 auto RemoveAssociated = [&AssociatedSections](const Section &Sec) {93 return AssociatedSections.contains(Sec.UniqueId);94 };95 do {96 DenseSet<ssize_t> RemovedSections;97 llvm::erase_if(Sections, [ToRemove, &RemovedSections](const Section &Sec) {98 bool Remove = ToRemove(Sec);99 if (Remove)100 RemovedSections.insert(Sec.UniqueId);101 return Remove;102 });103 // Remove all symbols referring to the removed sections.104 AssociatedSections.clear();105 llvm::erase_if(106 Symbols, [&RemovedSections, &AssociatedSections](const Symbol &Sym) {107 // If there are sections that are associative to a removed108 // section,109 // remove those as well as nothing will include them (and we can't110 // leave them dangling).111 if (RemovedSections.contains(Sym.AssociativeComdatTargetSectionId))112 AssociatedSections.insert(Sym.TargetSectionId);113 return RemovedSections.contains(Sym.TargetSectionId);114 });115 ToRemove = RemoveAssociated;116 } while (!AssociatedSections.empty());117 updateSections();118 updateSymbols();119}120 121void Object::truncateSections(function_ref<bool(const Section &)> ToTruncate) {122 for (Section &Sec : Sections) {123 if (ToTruncate(Sec)) {124 Sec.clearContents();125 Sec.Relocs.clear();126 Sec.Header.SizeOfRawData = 0;127 }128 }129}130 131} // end namespace coff132} // end namespace objcopy133} // end namespace llvm134