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