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