LLVM 18.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
30enum class FileFormat {
32 ELF,
33 Binary,
34 IHex,
35};
36
37// This type keeps track of the machine info for various architectures. This
38// lets us map architecture names to ELF types and the e_machine value of the
39// ELF file.
41 MachineInfo(uint16_t EM, uint8_t ABI, bool Is64, bool IsLittle)
42 : EMachine(EM), OSABI(ABI), Is64Bit(Is64), IsLittleEndian(IsLittle) {}
43 // Alternative constructor that defaults to NONE for OSABI.
44 MachineInfo(uint16_t EM, bool Is64, bool IsLittle)
45 : MachineInfo(EM, ELF::ELFOSABI_NONE, Is64, IsLittle) {}
46 // Default constructor for unset fields.
49 uint8_t OSABI;
50 bool Is64Bit;
52};
53
54// Flags set by --set-section-flags or --rename-section. Interpretation of these
55// is format-specific and not all flags are meaningful for all object file
56// formats. This is a bitmask; many section flags may be set.
59 SecAlloc = 1 << 0,
60 SecLoad = 1 << 1,
61 SecNoload = 1 << 2,
62 SecReadonly = 1 << 3,
63 SecDebug = 1 << 4,
64 SecCode = 1 << 5,
65 SecData = 1 << 6,
66 SecRom = 1 << 7,
67 SecMerge = 1 << 8,
68 SecStrings = 1 << 9,
69 SecContents = 1 << 10,
70 SecShare = 1 << 11,
71 SecExclude = 1 << 12,
72 SecLarge = 1 << 13,
73 LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue=*/SecLarge)
74};
75
79 std::optional<SectionFlag> NewFlags;
80};
81
85};
86
87enum class DiscardType {
88 None, // Default
89 All, // --discard-all (-x)
90 Locals, // --discard-locals (-X)
91};
92
93enum class MatchStyle {
94 Literal, // Default for symbols.
95 Wildcard, // Default for sections, or enabled with --wildcard (-w).
96 Regex, // Enabled with --regex.
97};
98
100 StringRef Name;
101 // Regex is shared between multiple CommonConfig instances.
102 std::shared_ptr<Regex> R;
103 std::shared_ptr<GlobPattern> G;
104 bool IsPositiveMatch = true;
105
106 NameOrPattern(StringRef N) : Name(N) {}
107 NameOrPattern(std::shared_ptr<Regex> R) : R(R) {}
108 NameOrPattern(std::shared_ptr<GlobPattern> G, bool IsPositiveMatch)
109 : G(G), IsPositiveMatch(IsPositiveMatch) {}
110
111public:
112 // ErrorCallback is used to handle recoverable errors. An Error returned
113 // by the callback aborts the parsing and is then returned by this function.
116 llvm::function_ref<Error(Error)> ErrorCallback);
117
118 bool isPositiveMatch() const { return IsPositiveMatch; }
119 std::optional<StringRef> getName() const {
120 if (!R && !G)
121 return Name;
122 return std::nullopt;
123 }
124 bool operator==(StringRef S) const {
125 return R ? R->match(S) : G ? G->match(S) : Name == S;
126 }
127 bool operator!=(StringRef S) const { return !operator==(S); }
128};
129
130// Matcher that checks symbol or section names against the command line flags
131// provided for that option.
134 std::vector<NameOrPattern> PosPatterns;
135 std::vector<NameOrPattern> NegMatchers;
136
137public:
139 if (!Matcher)
140 return Matcher.takeError();
141 if (Matcher->isPositiveMatch()) {
142 if (std::optional<StringRef> MaybeName = Matcher->getName())
143 PosNames.insert(CachedHashStringRef(*MaybeName));
144 else
145 PosPatterns.push_back(std::move(*Matcher));
146 } else {
147 NegMatchers.push_back(std::move(*Matcher));
148 }
149 return Error::success();
150 }
151 bool matches(StringRef S) const {
152 return (PosNames.contains(CachedHashStringRef(S)) ||
153 is_contained(PosPatterns, S)) &&
154 !is_contained(NegMatchers, S);
155 }
156 bool empty() const {
157 return PosNames.empty() && PosPatterns.empty() && NegMatchers.empty();
158 }
159};
160
161enum class SymbolFlag {
162 Global,
163 Local,
164 Weak,
165 Default,
166 Hidden,
167 Protected,
168 File,
169 Section,
170 Object,
171 Function,
173 Debug,
175 Warning,
176 Indirect,
177 Synthetic,
179};
180
181// Symbol info specified by --add-symbol option. Symbol flags not supported
182// by a concrete format should be ignored.
187 std::vector<SymbolFlag> Flags;
188 std::vector<StringRef> BeforeSyms;
189};
190
191// Specify section name and section body for newly added or updated section.
193 NewSectionInfo() = default;
194 NewSectionInfo(StringRef Name, std::unique_ptr<MemoryBuffer> &&Buffer)
195 : SectionName(Name), SectionData(std::move(Buffer)) {}
196
198 std::shared_ptr<MemoryBuffer> SectionData;
199};
200
201// Configuration for copying/stripping a single file.
203 // Main input/output options
208
209 // Only applicable when --output-format!=binary (e.g. elf64-x86-64).
210 std::optional<MachineInfo> OutputArch;
211
212 // Advanced options
214 // Cached gnu_debuglink's target CRC
216 std::optional<StringRef> ExtractPartition;
221
222 // Repeated options
223 std::vector<NewSectionInfo> AddSection;
224 std::vector<StringRef> DumpSection;
225 std::vector<NewSectionInfo> UpdateSection;
226
227 // Section matchers
231
232 // Symbol matchers
240
241 // Map options
247
248 // Symbol info specified by --add-symbol option.
249 std::vector<NewSymbolInfo> SymbolsToAdd;
250
251 // Boolean options
253 bool ExtractDWO = false;
255 bool OnlyKeepDebug = false;
256 bool PreserveDates = false;
257 bool StripAll = false;
258 bool StripAllGNU = false;
259 bool StripDWO = false;
260 bool StripDebug = false;
261 bool StripNonAlloc = false;
262 bool StripSections = false;
263 bool StripUnneeded = false;
264 bool Weaken = false;
266
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
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition: StringMap.h:112
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:151
Error addMatcher(Expected< NameOrPattern > Matcher)
Definition: CommonConfig.h:138
std::optional< StringRef > getName() const
Definition: CommonConfig.h:119
bool operator!=(StringRef S) const
Definition: CommonConfig.h:127
bool operator==(StringRef S) const
Definition: CommonConfig.h:124
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:1854
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition: STLExtras.h:1884
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:242
std::optional< MachineInfo > OutputArch
Definition: CommonConfig.h:210
std::vector< StringRef > DumpSection
Definition: CommonConfig.h:224
std::vector< NewSectionInfo > UpdateSection
Definition: CommonConfig.h:225
StringMap< uint64_t > SetSectionAlignment
Definition: CommonConfig.h:243
std::vector< NewSymbolInfo > SymbolsToAdd
Definition: CommonConfig.h:249
DebugCompressionType CompressionType
Definition: CommonConfig.h:267
StringMap< SectionFlagsUpdate > SetSectionFlags
Definition: CommonConfig.h:244
NameMatcher UnneededSymbolsToRemove
Definition: CommonConfig.h:237
std::optional< StringRef > ExtractPartition
Definition: CommonConfig.h:216
std::vector< NewSectionInfo > AddSection
Definition: CommonConfig.h:223
StringMap< StringRef > SymbolsToRename
Definition: CommonConfig.h:246
StringMap< uint64_t > SetSectionType
Definition: CommonConfig.h:245
MachineInfo(uint16_t EM, bool Is64, bool IsLittle)
Definition: CommonConfig.h:44
MachineInfo(uint16_t EM, uint8_t ABI, bool Is64, bool IsLittle)
Definition: CommonConfig.h:41
std::shared_ptr< MemoryBuffer > SectionData
Definition: CommonConfig.h:198
NewSectionInfo(StringRef Name, std::unique_ptr< MemoryBuffer > &&Buffer)
Definition: CommonConfig.h:194
std::vector< StringRef > BeforeSyms
Definition: CommonConfig.h:188
std::vector< SymbolFlag > Flags
Definition: CommonConfig.h:187
std::optional< SectionFlag > NewFlags
Definition: CommonConfig.h:79