LLVM 22.0.0git
CommonConfig.h
Go to the documentation of this file.
1//===- CommonConfig.h -------------------------------------------*- C++ -*-===//
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#ifndef LLVM_OBJCOPY_COMMONCONFIG_H
10#define LLVM_OBJCOPY_COMMONCONFIG_H
11
12#include "llvm/ADT/ArrayRef.h"
14#include "llvm/ADT/DenseSet.h"
16#include "llvm/ADT/StringMap.h"
17#include "llvm/ADT/StringRef.h"
23#include "llvm/Support/Regex.h"
24#include <optional>
25
26namespace llvm {
27namespace objcopy {
28
30
31// This type keeps track of the machine info for various architectures. This
32// lets us map architecture names to ELF types and the e_machine value of the
33// ELF file.
35 MachineInfo(uint16_t EM, uint8_t ABI, bool Is64, bool IsLittle)
36 : EMachine(EM), OSABI(ABI), Is64Bit(Is64), IsLittleEndian(IsLittle) {}
37 // Alternative constructor that defaults to NONE for OSABI.
38 MachineInfo(uint16_t EM, bool Is64, bool IsLittle)
39 : MachineInfo(EM, ELF::ELFOSABI_NONE, Is64, IsLittle) {}
40 // Default constructor for unset fields.
44 bool Is64Bit;
46};
47
48// Flags set by --set-section-flags or --rename-section. Interpretation of these
49// is format-specific and not all flags are meaningful for all object file
50// formats. This is a bitmask; many section flags may be set.
53 SecAlloc = 1 << 0,
54 SecLoad = 1 << 1,
55 SecNoload = 1 << 2,
56 SecReadonly = 1 << 3,
57 SecDebug = 1 << 4,
58 SecCode = 1 << 5,
59 SecData = 1 << 6,
60 SecRom = 1 << 7,
61 SecMerge = 1 << 8,
62 SecStrings = 1 << 9,
63 SecContents = 1 << 10,
64 SecShare = 1 << 11,
65 SecExclude = 1 << 12,
66 SecLarge = 1 << 13,
67 LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue=*/SecLarge)
68};
69
73 std::optional<SectionFlag> NewFlags;
74};
75
79};
80
81enum class DiscardType {
82 None, // Default
83 All, // --discard-all (-x)
84 Locals, // --discard-locals (-X)
85};
86
87enum class MatchStyle {
88 Literal, // Default for symbols.
89 Wildcard, // Default for sections, or enabled with --wildcard (-w).
90 Regex, // Enabled with --regex.
91};
92
94 StringRef Name;
95 // Regex is shared between multiple CommonConfig instances.
96 std::shared_ptr<Regex> R;
97 std::shared_ptr<GlobPattern> G;
98 bool IsPositiveMatch = true;
99
100 NameOrPattern(StringRef N) : Name(N) {}
101 NameOrPattern(std::shared_ptr<Regex> R) : R(R) {}
102 NameOrPattern(std::shared_ptr<GlobPattern> G, bool IsPositiveMatch)
103 : G(G), IsPositiveMatch(IsPositiveMatch) {}
104
105public:
106 // ErrorCallback is used to handle recoverable errors. An Error returned
107 // by the callback aborts the parsing and is then returned by this function.
110 llvm::function_ref<Error(Error)> ErrorCallback);
111
112 bool isPositiveMatch() const { return IsPositiveMatch; }
113 std::optional<StringRef> getName() const {
114 if (!R && !G)
115 return Name;
116 return std::nullopt;
117 }
118 bool operator==(StringRef S) const {
119 return R ? R->match(S) : G ? G->match(S) : Name == S;
120 }
121 bool operator!=(StringRef S) const { return !operator==(S); }
122};
123
124// Matcher that checks symbol or section names against the command line flags
125// provided for that option.
130
131public:
133 if (!Matcher)
134 return Matcher.takeError();
135 if (Matcher->isPositiveMatch()) {
136 if (std::optional<StringRef> MaybeName = Matcher->getName())
137 PosNames.insert(CachedHashStringRef(*MaybeName));
138 else
139 PosPatterns.push_back(std::move(*Matcher));
140 } else {
141 NegMatchers.push_back(std::move(*Matcher));
142 }
143 return Error::success();
144 }
145 bool matches(StringRef S) const {
146 return (PosNames.contains(CachedHashStringRef(S)) ||
147 is_contained(PosPatterns, S)) &&
148 !is_contained(NegMatchers, S);
149 }
150 bool empty() const {
151 return PosNames.empty() && PosPatterns.empty() && NegMatchers.empty();
152 }
153};
154
155enum class AdjustKind { Set, Add, Subtract };
156
160};
161
165};
166
167enum class SymbolFlag {
168 Global,
169 Local,
170 Weak,
171 Default,
172 Hidden,
173 Protected,
174 File,
175 Section,
176 Object,
177 Function,
179 Debug,
181 Warning,
182 Indirect,
183 Synthetic,
185};
186
187// Symbol info specified by --add-symbol option. Symbol flags not supported
188// by a concrete format should be ignored.
195};
196
197// Specify section name and section body for newly added or updated section.
199 NewSectionInfo() = default;
200 NewSectionInfo(StringRef Name, std::unique_ptr<MemoryBuffer> &&Buffer)
201 : SectionName(Name), SectionData(std::move(Buffer)) {}
202
204 std::shared_ptr<MemoryBuffer> SectionData;
205};
206
207// Configuration for copying/stripping a single file.
209 // Main input/output options
214
215 // Only applicable when --output-format!=binary (e.g. elf64-x86-64).
216 std::optional<MachineInfo> OutputArch;
217
218 // Advanced options
220 // Cached gnu_debuglink's target CRC
222 std::optional<StringRef> ExtractPartition;
230
231 // Repeated options
236
237 // Section matchers
241
242 // Symbol matchers
251
252 // Map options
258
259 // Symbol info specified by --add-symbol option.
261
262 // Integer options
264
265 // Boolean options
267 bool ExtractDWO = false;
269 bool OnlyKeepDebug = false;
270 bool PreserveDates = false;
271 bool StripAll = false;
272 bool StripAllGNU = false;
273 bool StripDWO = false;
274 bool StripDebug = false;
275 bool StripNonAlloc = false;
276 bool StripSections = false;
277 bool StripUnneeded = false;
278 bool Weaken = false;
280
282
285
286 // ErrorCallback is used to handle recoverable errors. An Error returned
287 // by the callback aborts the execution and is then returned to the caller.
288 // If the callback is not set, the errors are not issued.
289 std::function<Error(Error)> ErrorCallback;
290};
291
292} // namespace objcopy
293} // namespace llvm
294
295#endif // LLVM_OBJCOPY_COMMONCONFIG_H
This file defines the StringMap class.
This file defines CachedHashString and CachedHashStringRef.
#define LLVM_ABI
Definition: Compiler.h:213
This file defines the DenseSet and SmallDenseSet classes.
std::string Name
#define G(x, y, z)
Definition: MD5.cpp:56
This file defines the SmallVector class.
A container which contains a StringRef plus a precomputed hash.
Implements a dense probed hash-table based set.
Definition: DenseSet.h:263
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
bool empty() const
Definition: SmallVector.h:82
void push_back(const T &Elt)
Definition: SmallVector.h:414
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1197
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition: StringMap.h:133
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
LLVM Value Representation.
Definition: Value.h:75
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.
bool matches(StringRef S) const
Definition: CommonConfig.h:145
Error addMatcher(Expected< NameOrPattern > Matcher)
Definition: CommonConfig.h:132
std::optional< StringRef > getName() const
Definition: CommonConfig.h:113
bool operator!=(StringRef S) const
Definition: CommonConfig.h:121
static LLVM_ABI Expected< NameOrPattern > create(StringRef Pattern, MatchStyle MS, llvm::function_ref< Error(Error)> ErrorCallback)
bool operator==(StringRef S) const
Definition: CommonConfig.h:118
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
DebugCompressionType
Definition: Compression.h:28
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1886
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition: STLExtras.h:1916
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:856
#define N
SmallVector< SectionPatternAddressUpdate, 0 > ChangeSectionAddress
Definition: CommonConfig.h:235
SmallVector< NewSectionInfo, 0 > UpdateSection
Definition: CommonConfig.h:234
StringMap< SectionRename > SectionsToRename
Definition: CommonConfig.h:253
std::optional< MachineInfo > OutputArch
Definition: CommonConfig.h:216
SmallVector< std::pair< NameMatcher, llvm::DebugCompressionType >, 0 > compressSections
Definition: CommonConfig.h:284
SmallVector< StringRef, 0 > DumpSection
Definition: CommonConfig.h:233
SmallVector< NewSymbolInfo, 0 > SymbolsToAdd
Definition: CommonConfig.h:260
StringMap< uint64_t > SetSectionAlignment
Definition: CommonConfig.h:254
DebugCompressionType CompressionType
Definition: CommonConfig.h:281
std::function< Error(Error)> ErrorCallback
Definition: CommonConfig.h:289
StringMap< SectionFlagsUpdate > SetSectionFlags
Definition: CommonConfig.h:255
SmallVector< NewSectionInfo, 0 > AddSection
Definition: CommonConfig.h:232
NameMatcher UnneededSymbolsToRemove
Definition: CommonConfig.h:247
std::optional< StringRef > ExtractPartition
Definition: CommonConfig.h:222
StringMap< StringRef > SymbolsToRename
Definition: CommonConfig.h:257
StringMap< uint64_t > SetSectionType
Definition: CommonConfig.h:256
MachineInfo(uint16_t EM, bool Is64, bool IsLittle)
Definition: CommonConfig.h:38
MachineInfo(uint16_t EM, uint8_t ABI, bool Is64, bool IsLittle)
Definition: CommonConfig.h:35
std::shared_ptr< MemoryBuffer > SectionData
Definition: CommonConfig.h:204
NewSectionInfo(StringRef Name, std::unique_ptr< MemoryBuffer > &&Buffer)
Definition: CommonConfig.h:200
SmallVector< StringRef, 0 > BeforeSyms
Definition: CommonConfig.h:194
SmallVector< SymbolFlag, 0 > Flags
Definition: CommonConfig.h:193
std::optional< SectionFlag > NewFlags
Definition: CommonConfig.h:73