[lit] Add argument check: --timeout must be non-negative integer
[llvm-core.git] / tools / llvm-objcopy / CopyConfig.h
blob745af0ce480f04c21b80c83d62aea9a389ec001a
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/Regex.h"
23 // Necessary for llvm::DebugCompressionType::None
24 #include "llvm/Target/TargetOptions.h"
25 #include <vector>
27 namespace llvm {
28 namespace objcopy {
30 enum class FileFormat {
31 Unspecified,
32 ELF,
33 Binary,
34 IHex,
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.
40 struct MachineInfo {
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.
47 MachineInfo() : MachineInfo(0, 0, false, false) {}
48 uint16_t EMachine;
49 uint8_t OSABI;
50 bool Is64Bit;
51 bool IsLittleEndian;
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.
57 enum SectionFlag {
58 SecNone = 0,
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 LLVM_MARK_AS_BITMASK_ENUM(/* LargestValue = */ SecShare)
74 struct SectionRename {
75 StringRef OriginalName;
76 StringRef NewName;
77 Optional<SectionFlag> NewFlags;
80 struct SectionFlagsUpdate {
81 StringRef Name;
82 SectionFlag NewFlags;
85 enum class DiscardType {
86 None, // Default
87 All, // --discard-all (-x)
88 Locals, // --discard-locals (-X)
91 class NameOrRegex {
92 StringRef Name;
93 // Regex is shared between multiple CopyConfig instances.
94 std::shared_ptr<Regex> R;
96 public:
97 NameOrRegex(StringRef Pattern, bool IsRegex);
98 bool operator==(StringRef S) const { return R ? R->match(S) : Name == S; }
99 bool operator!=(StringRef S) const { return !operator==(S); }
102 // Matcher that checks symbol or section names against the command line flags
103 // provided for that option.
104 class NameMatcher {
105 std::vector<NameOrRegex> Matchers;
107 public:
108 void addMatcher(NameOrRegex Matcher) {
109 Matchers.push_back(std::move(Matcher));
111 bool matches(StringRef S) const { return is_contained(Matchers, S); }
112 bool empty() const { return Matchers.empty(); }
115 // Configuration for copying/stripping a single file.
116 struct CopyConfig {
117 // Format-specific options to be initialized lazily when needed.
118 Optional<elf::ELFCopyConfig> ELF;
120 // Main input/output options
121 StringRef InputFilename;
122 FileFormat InputFormat;
123 StringRef OutputFilename;
124 FileFormat OutputFormat;
126 // Only applicable when --output-format!=binary (e.g. elf64-x86-64).
127 Optional<MachineInfo> OutputArch;
129 // Advanced options
130 StringRef AddGnuDebugLink;
131 // Cached gnu_debuglink's target CRC
132 uint32_t GnuDebugLinkCRC32;
133 StringRef BuildIdLinkDir;
134 Optional<StringRef> BuildIdLinkInput;
135 Optional<StringRef> BuildIdLinkOutput;
136 Optional<StringRef> ExtractPartition;
137 StringRef SplitDWO;
138 StringRef SymbolsPrefix;
139 StringRef AllocSectionsPrefix;
140 DiscardType DiscardMode = DiscardType::None;
141 Optional<StringRef> NewSymbolVisibility;
143 // Repeated options
144 std::vector<StringRef> AddSection;
145 std::vector<StringRef> DumpSection;
146 std::vector<StringRef> SymbolsToAdd;
148 // Section matchers
149 NameMatcher KeepSection;
150 NameMatcher OnlySection;
151 NameMatcher ToRemove;
153 // Symbol matchers
154 NameMatcher SymbolsToGlobalize;
155 NameMatcher SymbolsToKeep;
156 NameMatcher SymbolsToLocalize;
157 NameMatcher SymbolsToRemove;
158 NameMatcher UnneededSymbolsToRemove;
159 NameMatcher SymbolsToWeaken;
160 NameMatcher SymbolsToKeepGlobal;
162 // Map options
163 StringMap<SectionRename> SectionsToRename;
164 StringMap<uint64_t> SetSectionAlignment;
165 StringMap<SectionFlagsUpdate> SetSectionFlags;
166 StringMap<StringRef> SymbolsToRename;
168 // ELF entry point address expression. The input parameter is an entry point
169 // address in the input ELF file. The entry address in the output file is
170 // calculated with EntryExpr(input_address), when either --set-start or
171 // --change-start is used.
172 std::function<uint64_t(uint64_t)> EntryExpr;
174 // Boolean options
175 bool AllowBrokenLinks = false;
176 bool DeterministicArchives = true;
177 bool ExtractDWO = false;
178 bool ExtractMainPartition = false;
179 bool KeepFileSymbols = false;
180 bool LocalizeHidden = false;
181 bool OnlyKeepDebug = false;
182 bool PreserveDates = false;
183 bool StripAll = false;
184 bool StripAllGNU = false;
185 bool StripDWO = false;
186 bool StripDebug = false;
187 bool StripNonAlloc = false;
188 bool StripSections = false;
189 bool StripUnneeded = false;
190 bool Weaken = false;
191 bool DecompressDebugSections = false;
192 DebugCompressionType CompressionType = DebugCompressionType::None;
194 // parseELFConfig performs ELF-specific command-line parsing. Fills `ELF` on
195 // success or returns an Error otherwise.
196 Error parseELFConfig() {
197 if (!ELF) {
198 Expected<elf::ELFCopyConfig> ELFConfig = elf::parseConfig(*this);
199 if (!ELFConfig)
200 return ELFConfig.takeError();
201 ELF = *ELFConfig;
203 return Error::success();
207 // Configuration for the overall invocation of this tool. When invoked as
208 // objcopy, will always contain exactly one CopyConfig. When invoked as strip,
209 // will contain one or more CopyConfigs.
210 struct DriverConfig {
211 SmallVector<CopyConfig, 1> CopyConfigs;
212 BumpPtrAllocator Alloc;
215 // ParseObjcopyOptions returns the config and sets the input arguments. If a
216 // help flag is set then ParseObjcopyOptions will print the help messege and
217 // exit.
218 Expected<DriverConfig> parseObjcopyOptions(ArrayRef<const char *> ArgsArr);
220 // ParseStripOptions returns the config and sets the input arguments. If a
221 // help flag is set then ParseStripOptions will print the help messege and
222 // exit. ErrorCallback is used to handle recoverable errors. An Error returned
223 // by the callback aborts the parsing and is then returned by this function.
224 Expected<DriverConfig>
225 parseStripOptions(ArrayRef<const char *> ArgsArr,
226 std::function<Error(Error)> ErrorCallback);
228 } // namespace objcopy
229 } // namespace llvm
231 #endif