blob: e19cea6aa9d226a4c982f96f29d9d962db546756 [file] [log] [blame]
Martin Storsjo10b72962019-01-10 21:28:24 +00001//===- Object.cpp ---------------------------------------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Martin Storsjo10b72962019-01-10 21:28:24 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "Object.h"
Martin Storsjof9e14342019-01-19 19:42:35 +000010#include "llvm/ADT/DenseSet.h"
Martin Storsjo10b72962019-01-10 21:28:24 +000011#include <algorithm>
12
13namespace llvm {
14namespace objcopy {
15namespace coff {
16
17using namespace object;
18
19void Object::addSymbols(ArrayRef<Symbol> NewSymbols) {
20 for (Symbol S : NewSymbols) {
21 S.UniqueId = NextSymbolUniqueId++;
22 Symbols.emplace_back(S);
23 }
24 updateSymbols();
25}
26
27void Object::updateSymbols() {
28 SymbolMap = DenseMap<size_t, Symbol *>(Symbols.size());
29 size_t RawSymIndex = 0;
30 for (Symbol &Sym : Symbols) {
31 SymbolMap[Sym.UniqueId] = &Sym;
32 Sym.RawIndex = RawSymIndex;
33 RawSymIndex += 1 + Sym.Sym.NumberOfAuxSymbols;
34 }
35}
36
37const Symbol *Object::findSymbol(size_t UniqueId) const {
38 auto It = SymbolMap.find(UniqueId);
39 if (It == SymbolMap.end())
40 return nullptr;
41 return It->second;
42}
43
44void Object::removeSymbols(function_ref<bool(const Symbol &)> ToRemove) {
45 Symbols.erase(
46 std::remove_if(std::begin(Symbols), std::end(Symbols),
47 [ToRemove](const Symbol &Sym) { return ToRemove(Sym); }),
48 std::end(Symbols));
49 updateSymbols();
50}
51
52Error Object::markSymbols() {
53 for (Symbol &Sym : Symbols)
54 Sym.Referenced = false;
55 for (const Section &Sec : Sections) {
56 for (const Relocation &R : Sec.Relocs) {
57 auto It = SymbolMap.find(R.Target);
58 if (It == SymbolMap.end())
59 return make_error<StringError>("Relocation target " + Twine(R.Target) +
60 " not found",
61 object_error::invalid_symbol_index);
62 It->second->Referenced = true;
63 }
64 }
65 return Error::success();
66}
67
Martin Storsjof9e14342019-01-19 19:42:35 +000068void Object::addSections(ArrayRef<Section> NewSections) {
69 for (Section S : NewSections) {
70 S.UniqueId = NextSectionUniqueId++;
71 Sections.emplace_back(S);
72 }
73 updateSections();
74}
75
76void Object::updateSections() {
77 SectionMap = DenseMap<ssize_t, Section *>(Sections.size());
78 size_t Index = 1;
79 for (Section &S : Sections) {
80 SectionMap[S.UniqueId] = &S;
81 S.Index = Index++;
82 }
83}
84
85const Section *Object::findSection(ssize_t UniqueId) const {
86 auto It = SectionMap.find(UniqueId);
87 if (It == SectionMap.end())
88 return nullptr;
89 return It->second;
90}
91
92void Object::removeSections(function_ref<bool(const Section &)> ToRemove) {
93 DenseSet<ssize_t> AssociatedSections;
94 auto RemoveAssociated = [&AssociatedSections](const Section &Sec) {
95 return AssociatedSections.count(Sec.UniqueId) == 1;
96 };
97 do {
98 DenseSet<ssize_t> RemovedSections;
99 Sections.erase(
100 std::remove_if(std::begin(Sections), std::end(Sections),
101 [ToRemove, &RemovedSections](const Section &Sec) {
102 bool Remove = ToRemove(Sec);
103 if (Remove)
104 RemovedSections.insert(Sec.UniqueId);
105 return Remove;
106 }),
107 std::end(Sections));
108 // Remove all symbols referring to the removed sections.
109 AssociatedSections.clear();
110 Symbols.erase(
111 std::remove_if(
112 std::begin(Symbols), std::end(Symbols),
113 [&RemovedSections, &AssociatedSections](const Symbol &Sym) {
114 // If there are sections that are associative to a removed
115 // section,
116 // remove those as well as nothing will include them (and we can't
117 // leave them dangling).
118 if (RemovedSections.count(Sym.AssociativeComdatTargetSectionId) ==
119 1)
120 AssociatedSections.insert(Sym.TargetSectionId);
121 return RemovedSections.count(Sym.TargetSectionId) == 1;
122 }),
123 std::end(Symbols));
124 ToRemove = RemoveAssociated;
125 } while (!AssociatedSections.empty());
126 updateSections();
127 updateSymbols();
128}
129
Martin Storsjo10b72962019-01-10 21:28:24 +0000130} // end namespace coff
131} // end namespace objcopy
132} // end namespace llvm