1 //===- Object.cpp ---------------------------------------------------------===//
2 //
3 //                      The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "Object.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   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 
findSymbol(size_t UniqueId) const37 const 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 
removeSymbols(function_ref<bool (const Symbol &)> ToRemove)44 void 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 
markSymbols()52 Error 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 
68 } // end namespace coff
69 } // end namespace objcopy
70 } // end namespace llvm
71