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