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