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