LLVM 17.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,
73};
74
78 std::optional<SectionFlag> NewFlags;
79};
80
84};
85
86enum class DiscardType {
87 None, // Default
88 All, // --discard-all (-x)
89 Locals, // --discard-locals (-X)
90};
91
92enum class MatchStyle {
93 Literal, // Default for symbols.
94 Wildcard, // Default for sections, or enabled with --wildcard (-w).
95 Regex, // Enabled with --regex.
96};
97
99 StringRef Name;
100 // Regex is shared between multiple CommonConfig instances.
101 std::shared_ptr<Regex> R;
102 std::shared_ptr<GlobPattern> G;
103 bool IsPositiveMatch = true;
104
105 NameOrPattern(StringRef N) : Name(N) {}
106 NameOrPattern(std::shared_ptr<Regex> R) : R(R) {}
107 NameOrPattern(std::shared_ptr<GlobPattern> G, bool IsPositiveMatch)
108 : G(G), IsPositiveMatch(IsPositiveMatch) {}
109
110public:
111 // ErrorCallback is used to handle recoverable errors. An Error returned
112 // by the callback aborts the parsing and is then returned by this function.
115 llvm::function_ref<Error(Error)> ErrorCallback);
116
117 bool isPositiveMatch() const { return IsPositiveMatch; }
118 std::optional<StringRef> getName() const {
119 if (!R && !G)
120 return Name;
121 return std::nullopt;
122 }
123 bool operator==(StringRef S) const {
124 return R ? R->match(S) : G ? G->match(S) : Name == S;
125 }
126 bool operator!=(StringRef S) const { return !operator==(S); }
127};
128
129// Matcher that checks symbol or section names against the command line flags
130// provided for that option.
133 std::vector<NameOrPattern> PosPatterns;
134 std::vector<NameOrPattern> NegMatchers;
135
136public:
138 if (!Matcher)
139 return Matcher.takeError();
140 if (Matcher->isPositiveMatch()) {
141 if (std::optional<StringRef> MaybeName = Matcher->getName())
142 PosNames.insert(CachedHashStringRef(*MaybeName));
143 else
144 PosPatterns.push_back(std::move(*Matcher));
145 } else {
146 NegMatchers.push_back(std::move(*Matcher));
147 }
148 return Error::success();
149 }
150 bool matches(StringRef S) const {
151 return (PosNames.contains(CachedHashStringRef(S)) ||
152 is_contained(PosPatterns, S)) &&
153 !is_contained(NegMatchers, S);
154 }
155 bool empty() const {
156 return PosNames.empty() && PosPatterns.empty() && NegMatchers.empty();
157 }
158};
159
160enum class SymbolFlag {
161 Global,
162 Local,
163 Weak,
164 Default,
165 Hidden,
166 Protected,
167 File,
168 Section,
169 Object,
170 Function,
172 Debug,
174 Warning,
175 Indirect,
176 Synthetic,
178};
179
180// Symbol info specified by --add-symbol option. Symbol flags not supported
181// by a concrete format should be ignored.
186 std::vector<SymbolFlag> Flags;
187 std::vector<StringRef> BeforeSyms;
188};
189
190// Specify section name and section body for newly added or updated section.
192 NewSectionInfo() = default;
193 NewSectionInfo(StringRef Name, std::unique_ptr<MemoryBuffer> &&Buffer)
194 : SectionName(Name), SectionData(std::move(Buffer)) {}
195
197 std::shared_ptr<MemoryBuffer> SectionData;
198};
199
200// Configuration for copying/stripping a single file.
202 // Main input/output options
207
208 // Only applicable when --output-format!=binary (e.g. elf64-x86-64).
209 std::optional<MachineInfo> OutputArch;
210
211 // Advanced options
213 // Cached gnu_debuglink's target CRC
215 std::optional<StringRef> ExtractPartition;
220
221 // Repeated options
222 std::vector<NewSectionInfo> AddSection;
223 std::vector<StringRef> DumpSection;
224 std::vector<NewSectionInfo> UpdateSection;
225
226 // Section matchers
230
231 // Symbol matchers
239
240 // Map options
246
247 // Symbol info specified by --add-symbol option.
248 std::vector<NewSymbolInfo> SymbolsToAdd;
249
250 // Boolean options
252 bool ExtractDWO = false;
254 bool OnlyKeepDebug = false;
255 bool PreserveDates = false;
256 bool StripAll = false;
257 bool StripAllGNU = false;
258 bool StripDWO = false;
259 bool StripDebug = false;
260 bool StripNonAlloc = false;
261 bool StripSections = false;
262 bool StripUnneeded = false;
263 bool Weaken = false;
265
267};
268
269} // namespace objcopy
270} // namespace llvm
271
272#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:156
static ErrorSuccess success()
Create a success value.
Definition: Error.h:330
Tagged union holding either a T or a Error.
Definition: Error.h:470
Error takeError()
Take ownership of the stored error.
Definition: Error.h:597
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition: StringMap.h:111
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:150
Error addMatcher(Expected< NameOrPattern > Matcher)
Definition: CommonConfig.h:137
std::optional< StringRef > getName() const
Definition: CommonConfig.h:118
bool operator!=(StringRef S) const
Definition: CommonConfig.h:126
bool operator==(StringRef S) const
Definition: CommonConfig.h:123
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:1862
bool is_contained(R &&Range, const E &Element)
Wrapper function around std::find to detect if an element exists in a container.
Definition: STLExtras.h:1869
Definition: BitVector.h:851
#define N
StringMap< SectionRename > SectionsToRename
Definition: CommonConfig.h:241
std::optional< MachineInfo > OutputArch
Definition: CommonConfig.h:209
std::vector< StringRef > DumpSection
Definition: CommonConfig.h:223
std::vector< NewSectionInfo > UpdateSection
Definition: CommonConfig.h:224
StringMap< uint64_t > SetSectionAlignment
Definition: CommonConfig.h:242
std::vector< NewSymbolInfo > SymbolsToAdd
Definition: CommonConfig.h:248
DebugCompressionType CompressionType
Definition: CommonConfig.h:266
StringMap< SectionFlagsUpdate > SetSectionFlags
Definition: CommonConfig.h:243
NameMatcher UnneededSymbolsToRemove
Definition: CommonConfig.h:236
std::optional< StringRef > ExtractPartition
Definition: CommonConfig.h:215
std::vector< NewSectionInfo > AddSection
Definition: CommonConfig.h:222
StringMap< StringRef > SymbolsToRename
Definition: CommonConfig.h:245
StringMap< uint64_t > SetSectionType
Definition: CommonConfig.h:244
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:197
NewSectionInfo(StringRef Name, std::unique_ptr< MemoryBuffer > &&Buffer)
Definition: CommonConfig.h:193
std::vector< StringRef > BeforeSyms
Definition: CommonConfig.h:187
std::vector< SymbolFlag > Flags
Definition: CommonConfig.h:186
std::optional< SectionFlag > NewFlags
Definition: CommonConfig.h:78