[llvm-objcopy] - Remove python invocations from 2 test cases.
[llvm-complete.git] / tools / llvm-objcopy / CopyConfig.h
blob0b68d23a1b7577ab0d66939d26cdfc688d9ac455
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 "llvm/ADT/ArrayRef.h"
13 #include "llvm/ADT/BitmaskEnum.h"
14 #include "llvm/ADT/Optional.h"
15 #include "llvm/ADT/SmallVector.h"
16 #include "llvm/ADT/StringMap.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/Object/ELFTypes.h"
19 #include "llvm/Support/Allocator.h"
20 #include "llvm/Support/Error.h"
21 #include "llvm/Support/Regex.h"
22 // Necessary for llvm::DebugCompressionType::None
23 #include "llvm/Target/TargetOptions.h"
24 #include <vector>
26 namespace llvm {
27 namespace objcopy {
29 enum class FileFormat {
30 Unspecified,
31 ELF,
32 Binary,
33 IHex,
36 // This type keeps track of the machine info for various architectures. This
37 // lets us map architecture names to ELF types and the e_machine value of the
38 // ELF file.
39 struct MachineInfo {
40 MachineInfo(uint16_t EM, uint8_t ABI, bool Is64, bool IsLittle)
41 : EMachine(EM), OSABI(ABI), Is64Bit(Is64), IsLittleEndian(IsLittle) {}
42 // Alternative constructor that defaults to NONE for OSABI.
43 MachineInfo(uint16_t EM, bool Is64, bool IsLittle)
44 : MachineInfo(EM, ELF::ELFOSABI_NONE, Is64, IsLittle) {}
45 // Default constructor for unset fields.
46 MachineInfo() : MachineInfo(0, 0, false, false) {}
47 uint16_t EMachine;
48 uint8_t OSABI;
49 bool Is64Bit;
50 bool IsLittleEndian;
53 // Flags set by --set-section-flags or --rename-section. Interpretation of these
54 // is format-specific and not all flags are meaningful for all object file
55 // formats. This is a bitmask; many section flags may be set.
56 enum SectionFlag {
57 SecNone = 0,
58 SecAlloc = 1 << 0,
59 SecLoad = 1 << 1,
60 SecNoload = 1 << 2,
61 SecReadonly = 1 << 3,
62 SecDebug = 1 << 4,
63 SecCode = 1 << 5,
64 SecData = 1 << 6,
65 SecRom = 1 << 7,
66 SecMerge = 1 << 8,
67 SecStrings = 1 << 9,
68 SecContents = 1 << 10,
69 SecShare = 1 << 11,
70 LLVM_MARK_AS_BITMASK_ENUM(/* LargestValue = */ SecShare)
73 struct SectionRename {
74 StringRef OriginalName;
75 StringRef NewName;
76 Optional<SectionFlag> NewFlags;
79 struct SectionFlagsUpdate {
80 StringRef Name;
81 SectionFlag NewFlags;
84 enum class DiscardType {
85 None, // Default
86 All, // --discard-all (-x)
87 Locals, // --discard-locals (-X)
90 class NameOrRegex {
91 StringRef Name;
92 // Regex is shared between multiple CopyConfig instances.
93 std::shared_ptr<Regex> R;
95 public:
96 NameOrRegex(StringRef Pattern, bool IsRegex);
97 bool operator==(StringRef S) const { return R ? R->match(S) : Name == S; }
98 bool operator!=(StringRef S) const { return !operator==(S); }
101 // Matcher that checks symbol or section names against the command line flags
102 // provided for that option.
103 class NameMatcher {
104 std::vector<NameOrRegex> Matchers;
106 public:
107 void addMatcher(NameOrRegex Matcher) {
108 Matchers.push_back(std::move(Matcher));
110 bool matches(StringRef S) const { return is_contained(Matchers, S); }
111 bool empty() const { return Matchers.empty(); }
114 struct NewSymbolInfo {
115 StringRef SymbolName;
116 StringRef SectionName;
117 uint64_t Value = 0;
118 uint8_t Type = ELF::STT_NOTYPE;
119 uint8_t Bind = ELF::STB_GLOBAL;
120 uint8_t Visibility = ELF::STV_DEFAULT;
123 // Configuration for copying/stripping a single file.
124 struct CopyConfig {
125 // Main input/output options
126 StringRef InputFilename;
127 FileFormat InputFormat;
128 StringRef OutputFilename;
129 FileFormat OutputFormat;
131 // Only applicable when --output-format!=binary (e.g. elf64-x86-64).
132 Optional<MachineInfo> OutputArch;
134 // Advanced options
135 StringRef AddGnuDebugLink;
136 // Cached gnu_debuglink's target CRC
137 uint32_t GnuDebugLinkCRC32;
138 StringRef BuildIdLinkDir;
139 Optional<StringRef> BuildIdLinkInput;
140 Optional<StringRef> BuildIdLinkOutput;
141 Optional<StringRef> ExtractPartition;
142 StringRef SplitDWO;
143 StringRef SymbolsPrefix;
144 StringRef AllocSectionsPrefix;
145 DiscardType DiscardMode = DiscardType::None;
146 Optional<uint8_t> NewSymbolVisibility;
148 // Repeated options
149 std::vector<StringRef> AddSection;
150 std::vector<StringRef> DumpSection;
151 std::vector<NewSymbolInfo> SymbolsToAdd;
153 // Section matchers
154 NameMatcher KeepSection;
155 NameMatcher OnlySection;
156 NameMatcher ToRemove;
158 // Symbol matchers
159 NameMatcher SymbolsToGlobalize;
160 NameMatcher SymbolsToKeep;
161 NameMatcher SymbolsToLocalize;
162 NameMatcher SymbolsToRemove;
163 NameMatcher UnneededSymbolsToRemove;
164 NameMatcher SymbolsToWeaken;
165 NameMatcher SymbolsToKeepGlobal;
167 // Map options
168 StringMap<SectionRename> SectionsToRename;
169 StringMap<SectionFlagsUpdate> SetSectionFlags;
170 StringMap<StringRef> SymbolsToRename;
172 // ELF entry point address expression. The input parameter is an entry point
173 // address in the input ELF file. The entry address in the output file is
174 // calculated with EntryExpr(input_address), when either --set-start or
175 // --change-start is used.
176 std::function<uint64_t(uint64_t)> EntryExpr;
178 // Boolean options
179 bool AllowBrokenLinks = false;
180 bool DeterministicArchives = true;
181 bool ExtractDWO = false;
182 bool ExtractMainPartition = false;
183 bool KeepFileSymbols = false;
184 bool LocalizeHidden = false;
185 bool OnlyKeepDebug = false;
186 bool PreserveDates = false;
187 bool StripAll = false;
188 bool StripAllGNU = false;
189 bool StripDWO = false;
190 bool StripDebug = false;
191 bool StripNonAlloc = false;
192 bool StripSections = false;
193 bool StripUnneeded = false;
194 bool Weaken = false;
195 bool DecompressDebugSections = false;
196 DebugCompressionType CompressionType = DebugCompressionType::None;
199 // Configuration for the overall invocation of this tool. When invoked as
200 // objcopy, will always contain exactly one CopyConfig. When invoked as strip,
201 // will contain one or more CopyConfigs.
202 struct DriverConfig {
203 SmallVector<CopyConfig, 1> CopyConfigs;
204 BumpPtrAllocator Alloc;
207 // ParseObjcopyOptions returns the config and sets the input arguments. If a
208 // help flag is set then ParseObjcopyOptions will print the help messege and
209 // exit.
210 Expected<DriverConfig> parseObjcopyOptions(ArrayRef<const char *> ArgsArr);
212 // ParseStripOptions returns the config and sets the input arguments. If a
213 // help flag is set then ParseStripOptions will print the help messege and
214 // exit. ErrorCallback is used to handle recoverable errors. An Error returned
215 // by the callback aborts the parsing and is then returned by this function.
216 Expected<DriverConfig>
217 parseStripOptions(ArrayRef<const char *> ArgsArr,
218 std::function<Error(Error)> ErrorCallback);
220 } // namespace objcopy
221 } // namespace llvm
223 #endif