LLVM  15.0.0git
COFFObject.cpp
Go to the documentation of this file.
1 //===- COFFObject.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 "COFFObject.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 
20  for (Symbol S : NewSymbols) {
21  S.UniqueId = NextSymbolUniqueId++;
22  Symbols.emplace_back(S);
23  }
24  updateSymbols();
25 }
26 
27 void Object::updateSymbols() {
28  SymbolMap = DenseMap<size_t, Symbol *>(Symbols.size());
29  for (Symbol &Sym : Symbols)
30  SymbolMap[Sym.UniqueId] = &Sym;
31 }
32 
33 const Symbol *Object::findSymbol(size_t UniqueId) const {
34  return SymbolMap.lookup(UniqueId);
35 }
36 
39  Error Errs = Error::success();
40  llvm::erase_if(Symbols, [ToRemove, &Errs](const Symbol &Sym) {
41  Expected<bool> ShouldRemove = ToRemove(Sym);
42  if (!ShouldRemove) {
43  Errs = joinErrors(std::move(Errs), ShouldRemove.takeError());
44  return false;
45  }
46  return *ShouldRemove;
47  });
48 
49  updateSymbols();
50  return Errs;
51 }
52 
54  for (Symbol &Sym : Symbols)
55  Sym.Referenced = false;
56  for (const Section &Sec : Sections) {
57  for (const Relocation &R : Sec.Relocs) {
58  auto It = SymbolMap.find(R.Target);
59  if (It == SymbolMap.end())
60  return createStringError(object_error::invalid_symbol_index,
61  "relocation target %zu not found", R.Target);
62  It->second->Referenced = true;
63  }
64  }
65  return Error::success();
66 }
67 
69  for (Section S : NewSections) {
70  S.UniqueId = NextSectionUniqueId++;
71  Sections.emplace_back(S);
72  }
73  updateSections();
74 }
75 
76 void 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 
85 const Section *Object::findSection(ssize_t UniqueId) const {
86  return SectionMap.lookup(UniqueId);
87 }
88 
90  DenseSet<ssize_t> AssociatedSections;
91  auto RemoveAssociated = [&AssociatedSections](const Section &Sec) {
92  return AssociatedSections.contains(Sec.UniqueId);
93  };
94  do {
95  DenseSet<ssize_t> RemovedSections;
96  llvm::erase_if(Sections, [ToRemove, &RemovedSections](const Section &Sec) {
97  bool Remove = ToRemove(Sec);
98  if (Remove)
99  RemovedSections.insert(Sec.UniqueId);
100  return Remove;
101  });
102  // Remove all symbols referring to the removed sections.
103  AssociatedSections.clear();
105  Symbols, [&RemovedSections, &AssociatedSections](const Symbol &Sym) {
106  // If there are sections that are associative to a removed
107  // section,
108  // remove those as well as nothing will include them (and we can't
109  // leave them dangling).
110  if (RemovedSections.contains(Sym.AssociativeComdatTargetSectionId))
111  AssociatedSections.insert(Sym.TargetSectionId);
112  return RemovedSections.contains(Sym.TargetSectionId);
113  });
114  ToRemove = RemoveAssociated;
115  } while (!AssociatedSections.empty());
116  updateSections();
117  updateSymbols();
118 }
119 
120 void Object::truncateSections(function_ref<bool(const Section &)> ToTruncate) {
121  for (Section &Sec : Sections) {
122  if (ToTruncate(Sec)) {
123  Sec.clearContents();
124  Sec.Relocs.clear();
125  Sec.Header.SizeOfRawData = 0;
126  }
127  }
128 }
129 
130 } // end namespace coff
131 } // end namespace objcopy
132 } // end namespace llvm
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::DenseMapBase::lookup
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
Definition: DenseMap.h:199
ToRemove
ReachingDefAnalysis InstSet & ToRemove
Definition: ARMLowOverheadLoops.cpp:542
llvm::Error::success
static ErrorSuccess success()
Create a success value.
Definition: Error.h:329
llvm::erase_if
void erase_if(Container &C, UnaryPredicate P)
Provide a container algorithm similar to C++ Library Fundamentals v2's erase_if which is equivalent t...
Definition: STLExtras.h:1807
llvm::objcopy::coff::Object::removeSymbols
Error removeSymbols(function_ref< Expected< bool >(const Symbol &)> ToRemove)
Definition: COFFObject.cpp:37
llvm::objcopy::coff::Object::truncateSections
void truncateSections(function_ref< bool(const Section &)> ToTruncate)
Definition: COFFObject.cpp:120
llvm::objcopy::coff::Section
Definition: COFFObject.h:36
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:41
llvm::detail::DenseSetImpl< ValueT, DenseMap< ValueT, detail::DenseSetEmpty, DenseMapInfo< ValueT >, detail::DenseSetPair< ValueT > >, DenseMapInfo< ValueT > >::insert
std::pair< iterator, bool > insert(const ValueT &V)
Definition: DenseSet.h:206
COFFObject.h
llvm::objcopy::coff::Relocation
Definition: COFFObject.h:27
llvm::objcopy::coff::Object::removeSections
void removeSections(function_ref< bool(const Section &)> ToRemove)
Definition: COFFObject.cpp:89
llvm::objcopy::coff::Object::markSymbols
Error markSymbols()
Definition: COFFObject.cpp:53
llvm::objcopy::coff::Object::addSections
void addSections(ArrayRef< Section > NewSections)
Definition: COFFObject.cpp:68
DenseSet.h
llvm::orc::SymbolMap
DenseMap< SymbolStringPtr, JITEvaluatedSymbol > SymbolMap
A map from symbol names (as SymbolStringPtrs) to JITSymbols (address/flags pairs).
Definition: Core.h:113
llvm::dwarf::Index
Index
Definition: Dwarf.h:472
llvm::function_ref
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLFunctionalExtras.h:36
llvm::DenseSet
Implements a dense probed hash-table based set.
Definition: DenseSet.h:268
llvm::objcopy::coff::Object::findSection
const Section * findSection(ssize_t UniqueId) const
Definition: COFFObject.cpp:85
object
bar al al movzbl eax ret Missed when stored in a memory object
Definition: README.txt:1411
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::DenseMap
Definition: DenseMap.h:716
llvm::detail::DenseSetImpl< ValueT, DenseMap< ValueT, detail::DenseSetEmpty, DenseMapInfo< ValueT >, detail::DenseSetPair< ValueT > >, DenseMapInfo< ValueT > >::empty
bool empty() const
Definition: DenseSet.h:80
llvm::joinErrors
Error joinErrors(Error E1, Error E2)
Concatenate errors.
Definition: Error.h:426
llvm::DenseMapBase::find
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:152
llvm::detail::DenseSetImpl< ValueT, DenseMap< ValueT, detail::DenseSetEmpty, DenseMapInfo< ValueT >, detail::DenseSetPair< ValueT > >, DenseMapInfo< ValueT > >::clear
void clear()
Definition: DenseSet.h:92
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
llvm::detail::DenseSetImpl< ValueT, DenseMap< ValueT, detail::DenseSetEmpty, DenseMapInfo< ValueT >, detail::DenseSetPair< ValueT > >, DenseMapInfo< ValueT > >::contains
bool contains(const_arg_type_t< ValueT > V) const
Check if the set contains the given element.
Definition: DenseSet.h:185
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
llvm::objcopy::coff::Symbol::TargetSectionId
ssize_t TargetSectionId
Definition: COFFObject.h:88
llvm::objcopy::coff::Object::findSymbol
const Symbol * findSymbol(size_t UniqueId) const
Definition: COFFObject.cpp:33
llvm::createStringError
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition: Error.h:1239
llvm::objcopy::coff::Symbol::AssociativeComdatTargetSectionId
ssize_t AssociativeComdatTargetSectionId
Definition: COFFObject.h:89
llvm::DenseMapBase::end
iterator end()
Definition: DenseMap.h:84
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:155
llvm::objcopy::coff::Object::addSymbols
void addSymbols(ArrayRef< Symbol > NewSymbols)
Definition: COFFObject.cpp:19
llvm::Expected::takeError
Error takeError()
Take ownership of the stored error.
Definition: Error.h:597
llvm::objcopy::coff::Section::UniqueId
ssize_t UniqueId
Definition: COFFObject.h:40
llvm::objcopy::coff::Symbol
Definition: COFFObject.h:83