LLVM 22.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
12namespace llvm {
13namespace objcopy {
14namespace coff {
15
16using namespace object;
17
19 for (Symbol S : NewSymbols) {
20 S.UniqueId = NextSymbolUniqueId++;
21 S.OriginalRawIndex = NextSymbolOriginalIndex;
22 NextSymbolOriginalIndex += 1 + S.Sym.NumberOfAuxSymbols;
23 Symbols.emplace_back(S);
24 }
25 updateSymbols();
26}
27
28void Object::updateSymbols() {
29 SymbolMap = DenseMap<size_t, Symbol *>(Symbols.size());
30 for (Symbol &Sym : Symbols)
31 SymbolMap[Sym.UniqueId] = &Sym;
32}
33
34const Symbol *Object::findSymbol(size_t UniqueId) const {
35 return SymbolMap.lookup(UniqueId);
36}
37
40 Error Errs = Error::success();
41 llvm::erase_if(Symbols, [ToRemove, &Errs](const Symbol &Sym) {
42 Expected<bool> ShouldRemove = ToRemove(Sym);
43 if (!ShouldRemove) {
44 Errs = joinErrors(std::move(Errs), ShouldRemove.takeError());
45 return false;
46 }
47 return *ShouldRemove;
48 });
49
50 updateSymbols();
51 return Errs;
52}
53
55 for (Symbol &Sym : Symbols)
56 Sym.Referenced = false;
57 for (const Section &Sec : Sections) {
58 for (const Relocation &R : Sec.Relocs) {
59 auto It = SymbolMap.find(R.Target);
60 if (It == SymbolMap.end())
61 return createStringError(object_error::invalid_symbol_index,
62 "relocation target %zu not found", R.Target);
63 It->second->Referenced = true;
64 }
65 }
66 return Error::success();
67}
68
70 for (Section S : NewSections) {
71 S.UniqueId = NextSectionUniqueId++;
72 Sections.emplace_back(S);
73 }
74 updateSections();
75}
76
77void Object::updateSections() {
78 SectionMap = DenseMap<ssize_t, Section *>(Sections.size());
79 size_t Index = 1;
80 for (Section &S : Sections) {
81 SectionMap[S.UniqueId] = &S;
82 S.Index = Index++;
83 }
84}
85
86const Section *Object::findSection(ssize_t UniqueId) const {
87 return SectionMap.lookup(UniqueId);
88}
89
91 DenseSet<ssize_t> AssociatedSections;
92 auto RemoveAssociated = [&AssociatedSections](const Section &Sec) {
93 return AssociatedSections.contains(Sec.UniqueId);
94 };
95 do {
96 DenseSet<ssize_t> RemovedSections;
97 llvm::erase_if(Sections, [ToRemove, &RemovedSections](const Section &Sec) {
98 bool Remove = ToRemove(Sec);
99 if (Remove)
100 RemovedSections.insert(Sec.UniqueId);
101 return Remove;
102 });
103 // Remove all symbols referring to the removed sections.
104 AssociatedSections.clear();
106 Symbols, [&RemovedSections, &AssociatedSections](const Symbol &Sym) {
107 // If there are sections that are associative to a removed
108 // section,
109 // remove those as well as nothing will include them (and we can't
110 // leave them dangling).
111 if (RemovedSections.contains(Sym.AssociativeComdatTargetSectionId))
112 AssociatedSections.insert(Sym.TargetSectionId);
113 return RemovedSections.contains(Sym.TargetSectionId);
114 });
115 ToRemove = RemoveAssociated;
116 } while (!AssociatedSections.empty());
117 updateSections();
118 updateSymbols();
119}
120
121void Object::truncateSections(function_ref<bool(const Section &)> ToTruncate) {
122 for (Section &Sec : Sections) {
123 if (ToTruncate(Sec)) {
124 Sec.clearContents();
125 Sec.Relocs.clear();
126 Sec.Header.SizeOfRawData = 0;
127 }
128 }
129}
130
131} // end namespace coff
132} // end namespace objcopy
133} // end namespace llvm
ReachingDefAnalysis InstSet & ToRemove
This file defines the DenseSet and SmallDenseSet classes.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:41
Implements a dense probed hash-table based set.
Definition DenseSet.h:269
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
Tagged union holding either a T or a Error.
Definition Error.h:485
Error takeError()
Take ownership of the stored error.
Definition Error.h:612
std::pair< iterator, bool > insert(const ValueT &V)
Definition DenseSet.h:194
bool contains(const_arg_type_t< ValueT > V) const
Check if the set contains the given element.
Definition DenseSet.h:169
An efficient, type-erasing, non-owning reference to a callable.
This is an optimization pass for GlobalISel generic memory operations.
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition Error.h:1305
Error joinErrors(Error E1, Error E2)
Concatenate errors.
Definition Error.h:442
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:2109
void truncateSections(function_ref< bool(const Section &)> ToTruncate)
const Symbol * findSymbol(size_t UniqueId) const
const Section * findSection(ssize_t UniqueId) const
void addSections(ArrayRef< Section > NewSections)
void removeSections(function_ref< bool(const Section &)> ToRemove)
void addSymbols(ArrayRef< Symbol > NewSymbols)
Error removeSymbols(function_ref< Expected< bool >(const Symbol &)> ToRemove)
ssize_t AssociativeComdatTargetSectionId
Definition COFFObject.h:88