LLVM 19.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 SymbolFlag {
155 Global,
156 Local,
157 Weak,
158 Default,
159 Hidden,
160 Protected,
161 File,
162 Section,
163 Object,
164 Function,
166 Debug,
168 Warning,
169 Indirect,
170 Synthetic,
172};
173
174// Symbol info specified by --add-symbol option. Symbol flags not supported
175// by a concrete format should be ignored.
182};
183
184// Specify section name and section body for newly added or updated section.
186 NewSectionInfo() = default;
187 NewSectionInfo(StringRef Name, std::unique_ptr<MemoryBuffer> &&Buffer)
188 : SectionName(Name), SectionData(std::move(Buffer)) {}
189
191 std::shared_ptr<MemoryBuffer> SectionData;
192};
193
194// Configuration for copying/stripping a single file.
196 // Main input/output options
201
202 // Only applicable when --output-format!=binary (e.g. elf64-x86-64).
203 std::optional<MachineInfo> OutputArch;
204
205 // Advanced options
207 // Cached gnu_debuglink's target CRC
209 std::optional<StringRef> ExtractPartition;
210 uint8_t GapFill = 0;
217
218 // Repeated options
222
223 // Section matchers
227
228 // Symbol matchers
237
238 // Map options
244
245 // Symbol info specified by --add-symbol option.
247
248 // Boolean options
250 bool ExtractDWO = false;
252 bool OnlyKeepDebug = false;
253 bool PreserveDates = false;
254 bool StripAll = false;
255 bool StripAllGNU = false;
256 bool StripDWO = false;
257 bool StripDebug = false;
258 bool StripNonAlloc = false;
259 bool StripSections = false;
260 bool StripUnneeded = false;
261 bool Weaken = false;
263
265
268};
269
270} // namespace objcopy
271} // namespace llvm
272
273#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:334
Tagged union holding either a T or a Error.
Definition: Error.h:474
Error takeError()
Take ownership of the stored error.
Definition: Error.h:601
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:127
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:1849
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition: STLExtras.h:1879
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858
#define N
SmallVector< NewSectionInfo, 0 > UpdateSection
Definition: CommonConfig.h:221
StringMap< SectionRename > SectionsToRename
Definition: CommonConfig.h:239
std::optional< MachineInfo > OutputArch
Definition: CommonConfig.h:203
SmallVector< std::pair< NameMatcher, llvm::DebugCompressionType >, 0 > compressSections
Definition: CommonConfig.h:267
SmallVector< StringRef, 0 > DumpSection
Definition: CommonConfig.h:220
SmallVector< NewSymbolInfo, 0 > SymbolsToAdd
Definition: CommonConfig.h:246
StringMap< uint64_t > SetSectionAlignment
Definition: CommonConfig.h:240
DebugCompressionType CompressionType
Definition: CommonConfig.h:264
StringMap< SectionFlagsUpdate > SetSectionFlags
Definition: CommonConfig.h:241
SmallVector< NewSectionInfo, 0 > AddSection
Definition: CommonConfig.h:219
NameMatcher UnneededSymbolsToRemove
Definition: CommonConfig.h:233
std::optional< StringRef > ExtractPartition
Definition: CommonConfig.h:209
StringMap< StringRef > SymbolsToRename
Definition: CommonConfig.h:243
StringMap< uint64_t > SetSectionType
Definition: CommonConfig.h:242
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:191
NewSectionInfo(StringRef Name, std::unique_ptr< MemoryBuffer > &&Buffer)
Definition: CommonConfig.h:187
SmallVector< StringRef, 0 > BeforeSyms
Definition: CommonConfig.h:181
SmallVector< SymbolFlag, 0 > Flags
Definition: CommonConfig.h:180
std::optional< SectionFlag > NewFlags
Definition: CommonConfig.h:72