[yaml2obj][obj2yaml] - Do not create a symbol table by default.
[llvm-complete.git] / tools / llvm-objcopy / CopyConfig.h
blob55a55d3a2bc2733f7baf0e1328c617c7d922ed59
1 //===- CopyConfig.h -------------------------------------------------------===//
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 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_TOOLS_LLVM_OBJCOPY_COPY_CONFIG_H
10 #define LLVM_TOOLS_LLVM_OBJCOPY_COPY_CONFIG_H
12 #include "ELF/ELFConfig.h"
13 #include "llvm/ADT/ArrayRef.h"
14 #include "llvm/ADT/BitmaskEnum.h"
15 #include "llvm/ADT/Optional.h"
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/ADT/StringMap.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/Object/ELFTypes.h"
20 #include "llvm/Support/Allocator.h"
21 #include "llvm/Support/Error.h"
22 #include "llvm/Support/GlobPattern.h"
23 #include "llvm/Support/Regex.h"
24 // Necessary for llvm::DebugCompressionType::None
25 #include "llvm/Target/TargetOptions.h"
26 #include <vector>
28 namespace llvm {
29 namespace objcopy {
31 enum class FileFormat {
32 Unspecified,
33 ELF,
34 Binary,
35 IHex,
38 // This type keeps track of the machine info for various architectures. This
39 // lets us map architecture names to ELF types and the e_machine value of the
40 // ELF file.
41 struct MachineInfo {
42 MachineInfo(uint16_t EM, uint8_t ABI, bool Is64, bool IsLittle)
43 : EMachine(EM), OSABI(ABI), Is64Bit(Is64), IsLittleEndian(IsLittle) {}
44 // Alternative constructor that defaults to NONE for OSABI.
45 MachineInfo(uint16_t EM, bool Is64, bool IsLittle)
46 : MachineInfo(EM, ELF::ELFOSABI_NONE, Is64, IsLittle) {}
47 // Default constructor for unset fields.
48 MachineInfo() : MachineInfo(0, 0, false, false) {}
49 uint16_t EMachine;
50 uint8_t OSABI;
51 bool Is64Bit;
52 bool IsLittleEndian;
55 // Flags set by --set-section-flags or --rename-section. Interpretation of these
56 // is format-specific and not all flags are meaningful for all object file
57 // formats. This is a bitmask; many section flags may be set.
58 enum SectionFlag {
59 SecNone = 0,
60 SecAlloc = 1 << 0,
61 SecLoad = 1 << 1,
62 SecNoload = 1 << 2,
63 SecReadonly = 1 << 3,
64 SecDebug = 1 << 4,
65 SecCode = 1 << 5,
66 SecData = 1 << 6,
67 SecRom = 1 << 7,
68 SecMerge = 1 << 8,
69 SecStrings = 1 << 9,
70 SecContents = 1 << 10,
71 SecShare = 1 << 11,
72 LLVM_MARK_AS_BITMASK_ENUM(/* LargestValue = */ SecShare)
75 struct SectionRename {
76 StringRef OriginalName;
77 StringRef NewName;
78 Optional<SectionFlag> NewFlags;
81 struct SectionFlagsUpdate {
82 StringRef Name;
83 SectionFlag NewFlags;
86 enum class DiscardType {
87 None, // Default
88 All, // --discard-all (-x)
89 Locals, // --discard-locals (-X)
92 enum class MatchStyle {
93 Literal, // Default for symbols.
94 Wildcard, // Default for sections, or enabled with --wildcard (-w).
95 Regex, // Enabled with --regex.
98 class NameOrPattern {
99 StringRef Name;
100 // Regex is shared between multiple CopyConfig instances.
101 std::shared_ptr<Regex> R;
102 std::shared_ptr<GlobPattern> G;
103 bool IsPositiveMatch = true;
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) {}
110 public:
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.
113 static Expected<NameOrPattern>
114 create(StringRef Pattern, MatchStyle MS,
115 llvm::function_ref<Error(Error)> ErrorCallback);
117 bool isPositiveMatch() const { return IsPositiveMatch; }
118 bool operator==(StringRef S) const {
119 return R ? R->match(S) : G ? G->match(S) : Name == S;
121 bool operator!=(StringRef S) const { return !operator==(S); }
124 // Matcher that checks symbol or section names against the command line flags
125 // provided for that option.
126 class NameMatcher {
127 std::vector<NameOrPattern> PosMatchers;
128 std::vector<NameOrPattern> NegMatchers;
130 public:
131 Error addMatcher(Expected<NameOrPattern> Matcher) {
132 if (!Matcher)
133 return Matcher.takeError();
134 if (Matcher->isPositiveMatch())
135 PosMatchers.push_back(std::move(*Matcher));
136 else
137 NegMatchers.push_back(std::move(*Matcher));
138 return Error::success();
140 bool matches(StringRef S) const {
141 return is_contained(PosMatchers, S) && !is_contained(NegMatchers, S);
143 bool empty() const { return PosMatchers.empty() && NegMatchers.empty(); }
146 // Configuration for copying/stripping a single file.
147 struct CopyConfig {
148 // Format-specific options to be initialized lazily when needed.
149 Optional<elf::ELFCopyConfig> ELF;
151 // Main input/output options
152 StringRef InputFilename;
153 FileFormat InputFormat;
154 StringRef OutputFilename;
155 FileFormat OutputFormat;
157 // Only applicable when --output-format!=binary (e.g. elf64-x86-64).
158 Optional<MachineInfo> OutputArch;
160 // Advanced options
161 StringRef AddGnuDebugLink;
162 // Cached gnu_debuglink's target CRC
163 uint32_t GnuDebugLinkCRC32;
164 StringRef BuildIdLinkDir;
165 Optional<StringRef> BuildIdLinkInput;
166 Optional<StringRef> BuildIdLinkOutput;
167 Optional<StringRef> ExtractPartition;
168 StringRef SplitDWO;
169 StringRef SymbolsPrefix;
170 StringRef AllocSectionsPrefix;
171 DiscardType DiscardMode = DiscardType::None;
172 Optional<StringRef> NewSymbolVisibility;
174 // Repeated options
175 std::vector<StringRef> AddSection;
176 std::vector<StringRef> DumpSection;
177 std::vector<StringRef> SymbolsToAdd;
179 // Section matchers
180 NameMatcher KeepSection;
181 NameMatcher OnlySection;
182 NameMatcher ToRemove;
184 // Symbol matchers
185 NameMatcher SymbolsToGlobalize;
186 NameMatcher SymbolsToKeep;
187 NameMatcher SymbolsToLocalize;
188 NameMatcher SymbolsToRemove;
189 NameMatcher UnneededSymbolsToRemove;
190 NameMatcher SymbolsToWeaken;
191 NameMatcher SymbolsToKeepGlobal;
193 // Map options
194 StringMap<SectionRename> SectionsToRename;
195 StringMap<uint64_t> SetSectionAlignment;
196 StringMap<SectionFlagsUpdate> SetSectionFlags;
197 StringMap<StringRef> SymbolsToRename;
199 // ELF entry point address expression. The input parameter is an entry point
200 // address in the input ELF file. The entry address in the output file is
201 // calculated with EntryExpr(input_address), when either --set-start or
202 // --change-start is used.
203 std::function<uint64_t(uint64_t)> EntryExpr;
205 // Boolean options
206 bool AllowBrokenLinks = false;
207 bool DeterministicArchives = true;
208 bool ExtractDWO = false;
209 bool ExtractMainPartition = false;
210 bool KeepFileSymbols = false;
211 bool LocalizeHidden = false;
212 bool OnlyKeepDebug = false;
213 bool PreserveDates = false;
214 bool StripAll = false;
215 bool StripAllGNU = false;
216 bool StripDWO = false;
217 bool StripDebug = false;
218 bool StripNonAlloc = false;
219 bool StripSections = false;
220 bool StripUnneeded = false;
221 bool Weaken = false;
222 bool DecompressDebugSections = false;
223 DebugCompressionType CompressionType = DebugCompressionType::None;
225 // parseELFConfig performs ELF-specific command-line parsing. Fills `ELF` on
226 // success or returns an Error otherwise.
227 Error parseELFConfig() {
228 if (!ELF) {
229 Expected<elf::ELFCopyConfig> ELFConfig = elf::parseConfig(*this);
230 if (!ELFConfig)
231 return ELFConfig.takeError();
232 ELF = *ELFConfig;
234 return Error::success();
238 // Configuration for the overall invocation of this tool. When invoked as
239 // objcopy, will always contain exactly one CopyConfig. When invoked as strip,
240 // will contain one or more CopyConfigs.
241 struct DriverConfig {
242 SmallVector<CopyConfig, 1> CopyConfigs;
243 BumpPtrAllocator Alloc;
246 // ParseObjcopyOptions returns the config and sets the input arguments. If a
247 // help flag is set then ParseObjcopyOptions will print the help messege and
248 // exit. ErrorCallback is used to handle recoverable errors. An Error returned
249 // by the callback aborts the parsing and is then returned by this function.
250 Expected<DriverConfig>
251 parseObjcopyOptions(ArrayRef<const char *> ArgsArr,
252 llvm::function_ref<Error(Error)> ErrorCallback);
254 // ParseStripOptions returns the config and sets the input arguments. If a
255 // help flag is set then ParseStripOptions will print the help messege and
256 // exit. ErrorCallback is used to handle recoverable errors. An Error returned
257 // by the callback aborts the parsing and is then returned by this function.
258 Expected<DriverConfig>
259 parseStripOptions(ArrayRef<const char *> ArgsArr,
260 llvm::function_ref<Error(Error)> ErrorCallback);
262 } // namespace objcopy
263 } // namespace llvm
265 #endif