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"
21#include "llvm/Support/Regex.h"
22// Necessary for llvm::DebugCompressionType::None
24#include <optional>
25#include <vector>
26
27namespace llvm {
28namespace objcopy {
29
31
32// This type keeps track of the machine info for various architectures. This
33// lets us map architecture names to ELF types and the e_machine value of the
34// ELF file.
36 MachineInfo(uint16_t EM, uint8_t ABI, bool Is64, bool IsLittle)
37 : EMachine(EM), OSABI(ABI), Is64Bit(Is64), IsLittleEndian(IsLittle) {}
38 // Alternative constructor that defaults to NONE for OSABI.
39 MachineInfo(uint16_t EM, bool Is64, bool IsLittle)
40 : MachineInfo(EM, ELF::ELFOSABI_NONE, Is64, IsLittle) {}
41 // Default constructor for unset fields.
44 uint8_t OSABI;
45 bool Is64Bit;
47};
48
49// Flags set by --set-section-flags or --rename-section. Interpretation of these
50// is format-specific and not all flags are meaningful for all object file
51// formats. This is a bitmask; many section flags may be set.
54 SecAlloc = 1 << 0,
55 SecLoad = 1 << 1,
56 SecNoload = 1 << 2,
57 SecReadonly = 1 << 3,
58 SecDebug = 1 << 4,
59 SecCode = 1 << 5,
60 SecData = 1 << 6,
61 SecRom = 1 << 7,
62 SecMerge = 1 << 8,
63 SecStrings = 1 << 9,
64 SecContents = 1 << 10,
65 SecShare = 1 << 11,
66 SecExclude = 1 << 12,
67 SecLarge = 1 << 13,
68 LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue=*/SecLarge)
69};
70
74 std::optional<SectionFlag> NewFlags;
75};
76
80};
81
82enum class DiscardType {
83 None, // Default
84 All, // --discard-all (-x)
85 Locals, // --discard-locals (-X)
86};
87
88enum class MatchStyle {
89 Literal, // Default for symbols.
90 Wildcard, // Default for sections, or enabled with --wildcard (-w).
91 Regex, // Enabled with --regex.
92};
93
95 StringRef Name;
96 // Regex is shared between multiple CommonConfig instances.
97 std::shared_ptr<Regex> R;
98 std::shared_ptr<GlobPattern> G;
99 bool IsPositiveMatch = true;
100
101 NameOrPattern(StringRef N) : Name(N) {}
102 NameOrPattern(std::shared_ptr<Regex> R) : R(R) {}
103 NameOrPattern(std::shared_ptr<GlobPattern> G, bool IsPositiveMatch)
104 : G(G), IsPositiveMatch(IsPositiveMatch) {}
105
106public:
107 // ErrorCallback is used to handle recoverable errors. An Error returned
108 // by the callback aborts the parsing and is then returned by this function.
111 llvm::function_ref<Error(Error)> ErrorCallback);
112
113 bool isPositiveMatch() const { return IsPositiveMatch; }
114 std::optional<StringRef> getName() const {
115 if (!R && !G)
116 return Name;
117 return std::nullopt;
118 }
119 bool operator==(StringRef S) const {
120 return R ? R->match(S) : G ? G->match(S) : Name == S;
121 }
122 bool operator!=(StringRef S) const { return !operator==(S); }
123};
124
125// Matcher that checks symbol or section names against the command line flags
126// provided for that option.
129 std::vector<NameOrPattern> PosPatterns;
130 std::vector<NameOrPattern> NegMatchers;
131
132public:
134 if (!Matcher)
135 return Matcher.takeError();
136 if (Matcher->isPositiveMatch()) {
137 if (std::optional<StringRef> MaybeName = Matcher->getName())
138 PosNames.insert(CachedHashStringRef(*MaybeName));
139 else
140 PosPatterns.push_back(std::move(*Matcher));
141 } else {
142 NegMatchers.push_back(std::move(*Matcher));
143 }
144 return Error::success();
145 }
146 bool matches(StringRef S) const {
147 return (PosNames.contains(CachedHashStringRef(S)) ||
148 is_contained(PosPatterns, S)) &&
149 !is_contained(NegMatchers, S);
150 }
151 bool empty() const {
152 return PosNames.empty() && PosPatterns.empty() && NegMatchers.empty();
153 }
154};
155
156enum class SymbolFlag {
157 Global,
158 Local,
159 Weak,
160 Default,
161 Hidden,
162 Protected,
163 File,
164 Section,
165 Object,
166 Function,
168 Debug,
170 Warning,
171 Indirect,
172 Synthetic,
174};
175
176// Symbol info specified by --add-symbol option. Symbol flags not supported
177// by a concrete format should be ignored.
182 std::vector<SymbolFlag> Flags;
183 std::vector<StringRef> BeforeSyms;
184};
185
186// Specify section name and section body for newly added or updated section.
188 NewSectionInfo() = default;
189 NewSectionInfo(StringRef Name, std::unique_ptr<MemoryBuffer> &&Buffer)
190 : SectionName(Name), SectionData(std::move(Buffer)) {}
191
193 std::shared_ptr<MemoryBuffer> SectionData;
194};
195
196// Configuration for copying/stripping a single file.
198 // Main input/output options
203
204 // Only applicable when --output-format!=binary (e.g. elf64-x86-64).
205 std::optional<MachineInfo> OutputArch;
206
207 // Advanced options
209 // Cached gnu_debuglink's target CRC
211 std::optional<StringRef> ExtractPartition;
212 uint8_t GapFill = 0;
219
220 // Repeated options
221 std::vector<NewSectionInfo> AddSection;
222 std::vector<StringRef> DumpSection;
223 std::vector<NewSectionInfo> UpdateSection;
224
225 // Section matchers
229
230 // Symbol matchers
238
239 // Map options
245
246 // Symbol info specified by --add-symbol option.
247 std::vector<NewSymbolInfo> SymbolsToAdd;
248
249 // Boolean options
251 bool ExtractDWO = false;
253 bool OnlyKeepDebug = false;
254 bool PreserveDates = false;
255 bool StripAll = false;
256 bool StripAllGNU = false;
257 bool StripDWO = false;
258 bool StripDebug = false;
259 bool StripNonAlloc = false;
260 bool StripSections = false;
261 bool StripUnneeded = false;
262 bool Weaken = false;
264
266};
267
268} // namespace objcopy
269} // namespace llvm
270
271#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
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:146
Error addMatcher(Expected< NameOrPattern > Matcher)
Definition: CommonConfig.h:133
std::optional< StringRef > getName() const
Definition: CommonConfig.h:114
bool operator!=(StringRef S) const
Definition: CommonConfig.h:122
bool operator==(StringRef S) const
Definition: CommonConfig.h:119
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:1858
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition: STLExtras.h:1888
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858
#define N
StringMap< SectionRename > SectionsToRename
Definition: CommonConfig.h:240
std::optional< MachineInfo > OutputArch
Definition: CommonConfig.h:205
std::vector< StringRef > DumpSection
Definition: CommonConfig.h:222
std::vector< NewSectionInfo > UpdateSection
Definition: CommonConfig.h:223
StringMap< uint64_t > SetSectionAlignment
Definition: CommonConfig.h:241
std::vector< NewSymbolInfo > SymbolsToAdd
Definition: CommonConfig.h:247
DebugCompressionType CompressionType
Definition: CommonConfig.h:265
StringMap< SectionFlagsUpdate > SetSectionFlags
Definition: CommonConfig.h:242
NameMatcher UnneededSymbolsToRemove
Definition: CommonConfig.h:235
std::optional< StringRef > ExtractPartition
Definition: CommonConfig.h:211
std::vector< NewSectionInfo > AddSection
Definition: CommonConfig.h:221
StringMap< StringRef > SymbolsToRename
Definition: CommonConfig.h:244
StringMap< uint64_t > SetSectionType
Definition: CommonConfig.h:243
MachineInfo(uint16_t EM, bool Is64, bool IsLittle)
Definition: CommonConfig.h:39
MachineInfo(uint16_t EM, uint8_t ABI, bool Is64, bool IsLittle)
Definition: CommonConfig.h:36
std::shared_ptr< MemoryBuffer > SectionData
Definition: CommonConfig.h:193
NewSectionInfo(StringRef Name, std::unique_ptr< MemoryBuffer > &&Buffer)
Definition: CommonConfig.h:189
std::vector< StringRef > BeforeSyms
Definition: CommonConfig.h:183
std::vector< SymbolFlag > Flags
Definition: CommonConfig.h:182
std::optional< SectionFlag > NewFlags
Definition: CommonConfig.h:74