LLVM 22.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"
23#include "llvm/Support/Regex.h"
24#include <optional>
25
26namespace llvm {
27namespace objcopy {
28
30
31// This type keeps track of the machine info for various architectures. This
32// lets us map architecture names to ELF types and the e_machine value of the
33// ELF file.
35 MachineInfo(uint16_t EM, uint8_t ABI, bool Is64, bool IsLittle)
36 : EMachine(EM), OSABI(ABI), Is64Bit(Is64), IsLittleEndian(IsLittle) {}
37 // Alternative constructor that defaults to NONE for OSABI.
38 MachineInfo(uint16_t EM, bool Is64, bool IsLittle)
39 : MachineInfo(EM, ELF::ELFOSABI_NONE, Is64, IsLittle) {}
40 // Default constructor for unset fields.
44 bool Is64Bit;
46};
47
48// Flags set by --set-section-flags or --rename-section. Interpretation of these
49// is format-specific and not all flags are meaningful for all object file
50// formats. This is a bitmask; many section flags may be set.
53 SecAlloc = 1 << 0,
54 SecLoad = 1 << 1,
55 SecNoload = 1 << 2,
56 SecReadonly = 1 << 3,
57 SecDebug = 1 << 4,
58 SecCode = 1 << 5,
59 SecData = 1 << 6,
60 SecRom = 1 << 7,
61 SecMerge = 1 << 8,
62 SecStrings = 1 << 9,
63 SecContents = 1 << 10,
64 SecShare = 1 << 11,
65 SecExclude = 1 << 12,
66 SecLarge = 1 << 13,
67 LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue=*/SecLarge)
68};
69
73 std::optional<SectionFlag> NewFlags;
74};
75
80
81enum class DiscardType {
82 None, // Default
83 All, // --discard-all (-x)
84 Locals, // --discard-locals (-X)
85};
86
87enum class MatchStyle {
88 Literal, // Default for symbols.
89 Wildcard, // Default for sections, or enabled with --wildcard (-w).
90 Regex, // Enabled with --regex.
91};
92
93class NameOrPattern {
94 StringRef Name;
95 // Regex is shared between multiple CommonConfig instances.
96 std::shared_ptr<Regex> R;
97 std::shared_ptr<GlobPattern> G;
98 bool IsPositiveMatch = true;
99
100 NameOrPattern(StringRef N) : Name(N) {}
101 NameOrPattern(std::shared_ptr<Regex> R) : R(R) {}
102 NameOrPattern(std::shared_ptr<GlobPattern> G, bool IsPositiveMatch)
103 : G(G), IsPositiveMatch(IsPositiveMatch) {}
104
105public:
106 // ErrorCallback is used to handle recoverable errors. An Error returned
107 // by the callback aborts the parsing and is then returned by this function.
110 llvm::function_ref<Error(Error)> ErrorCallback);
111
112 bool isPositiveMatch() const { return IsPositiveMatch; }
113 std::optional<StringRef> getName() const {
114 if (!R && !G)
115 return Name;
116 return std::nullopt;
117 }
118 bool operator==(StringRef S) const {
119 return R ? R->match(S) : G ? G->match(S) : Name == S;
120 }
121 bool operator!=(StringRef S) const { return !operator==(S); }
122};
123
124// Matcher that checks symbol or section names against the command line flags
125// provided for that option.
130
131public:
133 if (!Matcher)
134 return Matcher.takeError();
135 if (Matcher->isPositiveMatch()) {
136 if (std::optional<StringRef> MaybeName = Matcher->getName())
137 PosNames.insert(CachedHashStringRef(*MaybeName));
138 else
139 PosPatterns.push_back(std::move(*Matcher));
140 } else {
141 NegMatchers.push_back(std::move(*Matcher));
142 }
143 return Error::success();
144 }
145 bool matches(StringRef S) const {
146 return (PosNames.contains(CachedHashStringRef(S)) ||
147 is_contained(PosPatterns, S)) &&
148 !is_contained(NegMatchers, S);
149 }
150 bool empty() const {
151 return PosNames.empty() && PosPatterns.empty() && NegMatchers.empty();
152 }
153};
154
155enum class AdjustKind { Set, Add, Subtract };
156
161
166
186
187// Symbol info specified by --add-symbol option. Symbol flags not supported
188// by a concrete format should be ignored.
196
197// Specify section name and section body for newly added or updated section.
199 NewSectionInfo() = default;
200 NewSectionInfo(StringRef Name, std::unique_ptr<MemoryBuffer> &&Buffer)
201 : SectionName(Name), SectionData(std::move(Buffer)) {}
202
204 std::shared_ptr<MemoryBuffer> SectionData;
205};
206
207// Configuration for copying/stripping a single file.
209 // Main input/output options
214
215 // Only applicable when --output-format!=binary (e.g. elf64-x86-64).
216 std::optional<MachineInfo> OutputArch;
217
218 // Advanced options
220 // Cached gnu_debuglink's target CRC
222 std::optional<StringRef> ExtractPartition;
230
231 // Repeated options
237
238 // Section matchers
242
243 // Symbol matchers
252
253 // Map options
259
260 // Symbol info specified by --add-symbol option.
262
263 // Integer options
265
266 // Boolean options
268 bool ExtractDWO = false;
270 bool OnlyKeepDebug = false;
271 bool PreserveDates = false;
272 bool StripAll = false;
273 bool StripAllGNU = false;
274 bool StripDWO = false;
275 bool StripDebug = false;
276 bool StripNonAlloc = false;
277 bool StripSections = false;
278 bool StripUnneeded = false;
279 bool Weaken = false;
281
283
286
287 // ErrorCallback is used to handle recoverable errors. An Error returned
288 // by the callback aborts the execution and is then returned to the caller.
289 // If the callback is not set, the errors are not issued.
290 std::function<Error(Error)> ErrorCallback;
291};
292
293} // namespace objcopy
294} // namespace llvm
295
296#endif // LLVM_OBJCOPY_COMMONCONFIG_H
This file defines the StringMap class.
This file defines CachedHashString and CachedHashStringRef.
#define LLVM_ABI
Definition Compiler.h:213
This file defines the DenseSet and SmallDenseSet classes.
#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:261
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
Tagged union holding either a T or a Error.
Definition Error.h:485
Error takeError()
Take ownership of the stored error.
Definition Error.h:612
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition StringMap.h:133
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
An efficient, type-erasing, non-owning reference to a callable.
bool matches(StringRef S) const
Error addMatcher(Expected< NameOrPattern > Matcher)
std::optional< StringRef > getName() const
bool operator!=(StringRef S) const
static LLVM_ABI Expected< NameOrPattern > create(StringRef Pattern, MatchStyle MS, llvm::function_ref< Error(Error)> ErrorCallback)
bool operator==(StringRef S) const
This is an optimization pass for GlobalISel generic memory operations.
@ LLVM_MARK_AS_BITMASK_ENUM
Definition ModRef.h:37
DebugCompressionType
Definition Compression.h:28
@ None
No compression.
Definition Compression.h:29
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:1869
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1899
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:851
#define N
SmallVector< SectionPatternAddressUpdate, 0 > ChangeSectionAddress
SmallVector< NewSectionInfo, 0 > UpdateSection
StringMap< SectionRename > SectionsToRename
std::optional< MachineInfo > OutputArch
SmallVector< StringRef, 0 > ExtractSection
SmallVector< std::pair< NameMatcher, llvm::DebugCompressionType >, 0 > compressSections
SmallVector< StringRef, 0 > DumpSection
SmallVector< NewSymbolInfo, 0 > SymbolsToAdd
StringMap< uint64_t > SetSectionAlignment
DebugCompressionType CompressionType
std::function< Error(Error)> ErrorCallback
StringMap< SectionFlagsUpdate > SetSectionFlags
SmallVector< NewSectionInfo, 0 > AddSection
std::optional< StringRef > ExtractPartition
StringMap< StringRef > SymbolsToRename
StringMap< uint64_t > SetSectionType
MachineInfo(uint16_t EM, bool Is64, bool IsLittle)
MachineInfo(uint16_t EM, uint8_t ABI, bool Is64, bool IsLittle)
std::shared_ptr< MemoryBuffer > SectionData
NewSectionInfo(StringRef Name, std::unique_ptr< MemoryBuffer > &&Buffer)
SmallVector< StringRef, 0 > BeforeSyms
SmallVector< SymbolFlag, 0 > Flags
std::optional< SectionFlag > NewFlags