1 //===- CopyConfig.h -------------------------------------------------------===//
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
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"
29 // This type keeps track of the machine info for various architectures. This
30 // lets us map architecture names to ELF types and the e_machine value of the
33 MachineInfo(uint16_t EM, uint8_t ABI, bool Is64, bool IsLittle)
34 : EMachine(EM), OSABI(ABI), Is64Bit(Is64), IsLittleEndian(IsLittle) {}
35 // Alternative constructor that defaults to NONE for OSABI.
36 MachineInfo(uint16_t EM, bool Is64, bool IsLittle)
37 : MachineInfo(EM, ELF::ELFOSABI_NONE, Is64, IsLittle) {}
38 // Default constructor for unset fields.
39 MachineInfo() : MachineInfo(0, 0, false, false) {}
46 // Flags set by --set-section-flags or --rename-section. Interpretation of these
47 // is format-specific and not all flags are meaningful for all object file
48 // formats. This is a bitmask; many section flags may be set.
61 SecContents = 1 << 10,
63 LLVM_MARK_AS_BITMASK_ENUM(/* LargestValue = */ SecShare)
66 struct SectionRename {
67 StringRef OriginalName;
69 Optional<SectionFlag> NewFlags;
72 struct SectionFlagsUpdate {
77 enum class DiscardType {
79 All, // --discard-all (-x)
80 Locals, // --discard-locals (-X)
85 // Regex is shared between multiple CopyConfig instances.
86 std::shared_ptr<Regex> R;
89 NameOrRegex(StringRef Pattern, bool IsRegex);
90 bool operator==(StringRef S) const { return R ? R->match(S) : Name == S; }
91 bool operator!=(StringRef S) const { return !operator==(S); }
94 struct NewSymbolInfo {
96 StringRef SectionName;
98 uint8_t Type = ELF::STT_NOTYPE;
99 uint8_t Bind = ELF::STB_GLOBAL;
100 uint8_t Visibility = ELF::STV_DEFAULT;
103 // Configuration for copying/stripping a single file.
105 // Main input/output options
106 StringRef InputFilename;
107 StringRef InputFormat;
108 StringRef OutputFilename;
109 StringRef OutputFormat;
111 // Only applicable for --input-format=binary
112 MachineInfo BinaryArch;
113 // Only applicable when --output-format!=binary (e.g. elf64-x86-64).
114 Optional<MachineInfo> OutputArch;
117 StringRef AddGnuDebugLink;
118 // Cached gnu_debuglink's target CRC
119 uint32_t GnuDebugLinkCRC32;
120 StringRef BuildIdLinkDir;
121 Optional<StringRef> BuildIdLinkInput;
122 Optional<StringRef> BuildIdLinkOutput;
123 Optional<StringRef> ExtractPartition;
125 StringRef SymbolsPrefix;
126 StringRef AllocSectionsPrefix;
127 DiscardType DiscardMode = DiscardType::None;
130 std::vector<StringRef> AddSection;
131 std::vector<StringRef> DumpSection;
132 std::vector<NewSymbolInfo> SymbolsToAdd;
133 std::vector<NameOrRegex> KeepSection;
134 std::vector<NameOrRegex> OnlySection;
135 std::vector<NameOrRegex> SymbolsToGlobalize;
136 std::vector<NameOrRegex> SymbolsToKeep;
137 std::vector<NameOrRegex> SymbolsToLocalize;
138 std::vector<NameOrRegex> SymbolsToRemove;
139 std::vector<NameOrRegex> UnneededSymbolsToRemove;
140 std::vector<NameOrRegex> SymbolsToWeaken;
141 std::vector<NameOrRegex> ToRemove;
142 std::vector<NameOrRegex> SymbolsToKeepGlobal;
145 StringMap<SectionRename> SectionsToRename;
146 StringMap<SectionFlagsUpdate> SetSectionFlags;
147 StringMap<StringRef> SymbolsToRename;
149 // ELF entry point address expression. The input parameter is an entry point
150 // address in the input ELF file. The entry address in the output file is
151 // calculated with EntryExpr(input_address), when either --set-start or
152 // --change-start is used.
153 std::function<uint64_t(uint64_t)> EntryExpr;
156 bool AllowBrokenLinks = false;
157 bool DeterministicArchives = true;
158 bool ExtractDWO = false;
159 bool ExtractMainPartition = false;
160 bool KeepFileSymbols = false;
161 bool LocalizeHidden = false;
162 bool OnlyKeepDebug = false;
163 bool PreserveDates = false;
164 bool StripAll = false;
165 bool StripAllGNU = false;
166 bool StripDWO = false;
167 bool StripDebug = false;
168 bool StripNonAlloc = false;
169 bool StripSections = false;
170 bool StripUnneeded = false;
172 bool DecompressDebugSections = false;
173 DebugCompressionType CompressionType = DebugCompressionType::None;
176 // Configuration for the overall invocation of this tool. When invoked as
177 // objcopy, will always contain exactly one CopyConfig. When invoked as strip,
178 // will contain one or more CopyConfigs.
179 struct DriverConfig {
180 SmallVector<CopyConfig, 1> CopyConfigs;
181 BumpPtrAllocator Alloc;
184 // ParseObjcopyOptions returns the config and sets the input arguments. If a
185 // help flag is set then ParseObjcopyOptions will print the help messege and
187 Expected<DriverConfig> parseObjcopyOptions(ArrayRef<const char *> ArgsArr);
189 // ParseStripOptions returns the config and sets the input arguments. If a
190 // help flag is set then ParseStripOptions will print the help messege and
191 // exit. ErrorCallback is used to handle recoverable errors. An Error returned
192 // by the callback aborts the parsing and is then returned by this function.
193 Expected<DriverConfig>
194 parseStripOptions(ArrayRef<const char *> ArgsArr,
195 std::function<Error(Error)> ErrorCallback);
197 } // namespace objcopy