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 "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"
31 enum class FileFormat {
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
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) {}
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.
70 SecContents = 1 << 10,
72 LLVM_MARK_AS_BITMASK_ENUM(/* LargestValue = */ SecShare)
75 struct SectionRename {
76 StringRef OriginalName;
78 Optional<SectionFlag> NewFlags;
81 struct SectionFlagsUpdate {
86 enum class DiscardType {
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.
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) {}
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.
127 std::vector<NameOrPattern> PosMatchers;
128 std::vector<NameOrPattern> NegMatchers;
131 Error addMatcher(Expected<NameOrPattern> Matcher) {
133 return Matcher.takeError();
134 if (Matcher->isPositiveMatch())
135 PosMatchers.push_back(std::move(*Matcher));
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.
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 = FileFormat::Unspecified;
154 StringRef OutputFilename;
155 FileFormat OutputFormat = FileFormat::Unspecified;
157 // Only applicable when --output-format!=binary (e.g. elf64-x86-64).
158 Optional<MachineInfo> OutputArch;
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;
169 StringRef SymbolsPrefix;
170 StringRef AllocSectionsPrefix;
171 DiscardType DiscardMode = DiscardType::None;
172 Optional<StringRef> NewSymbolVisibility;
175 std::vector<StringRef> AddSection;
176 std::vector<StringRef> DumpSection;
177 std::vector<StringRef> SymbolsToAdd;
178 std::vector<StringRef> RPathToAdd;
181 NameMatcher KeepSection;
182 NameMatcher OnlySection;
183 NameMatcher ToRemove;
186 NameMatcher SymbolsToGlobalize;
187 NameMatcher SymbolsToKeep;
188 NameMatcher SymbolsToLocalize;
189 NameMatcher SymbolsToRemove;
190 NameMatcher UnneededSymbolsToRemove;
191 NameMatcher SymbolsToWeaken;
192 NameMatcher SymbolsToKeepGlobal;
195 StringMap<SectionRename> SectionsToRename;
196 StringMap<uint64_t> SetSectionAlignment;
197 StringMap<SectionFlagsUpdate> SetSectionFlags;
198 StringMap<StringRef> SymbolsToRename;
200 // ELF entry point address expression. The input parameter is an entry point
201 // address in the input ELF file. The entry address in the output file is
202 // calculated with EntryExpr(input_address), when either --set-start or
203 // --change-start is used.
204 std::function<uint64_t(uint64_t)> EntryExpr;
207 bool AllowBrokenLinks = false;
208 bool DeterministicArchives = true;
209 bool ExtractDWO = false;
210 bool ExtractMainPartition = false;
211 bool KeepFileSymbols = false;
212 bool LocalizeHidden = false;
213 bool OnlyKeepDebug = false;
214 bool PreserveDates = false;
215 bool StripAll = false;
216 bool StripAllGNU = false;
217 bool StripDWO = false;
218 bool StripDebug = false;
219 bool StripNonAlloc = false;
220 bool StripSections = false;
221 bool StripUnneeded = false;
223 bool DecompressDebugSections = false;
224 DebugCompressionType CompressionType = DebugCompressionType::None;
226 // parseELFConfig performs ELF-specific command-line parsing. Fills `ELF` on
227 // success or returns an Error otherwise.
228 Error parseELFConfig() {
230 Expected<elf::ELFCopyConfig> ELFConfig = elf::parseConfig(*this);
232 return ELFConfig.takeError();
235 return Error::success();
239 // Configuration for the overall invocation of this tool. When invoked as
240 // objcopy, will always contain exactly one CopyConfig. When invoked as strip,
241 // will contain one or more CopyConfigs.
242 struct DriverConfig {
243 SmallVector<CopyConfig, 1> CopyConfigs;
244 BumpPtrAllocator Alloc;
247 // ParseObjcopyOptions returns the config and sets the input arguments. If a
248 // help flag is set then ParseObjcopyOptions will print the help messege and
249 // exit. ErrorCallback is used to handle recoverable errors. An Error returned
250 // by the callback aborts the parsing and is then returned by this function.
251 Expected<DriverConfig>
252 parseObjcopyOptions(ArrayRef<const char *> ArgsArr,
253 llvm::function_ref<Error(Error)> ErrorCallback);
255 // ParseInstallNameToolOptions returns the config and sets the input arguments.
256 // If a help flag is set then ParseInstallNameToolOptions will print the help
258 Expected<DriverConfig>
259 parseInstallNameToolOptions(ArrayRef<const char *> ArgsArr);
261 // ParseStripOptions returns the config and sets the input arguments. If a
262 // help flag is set then ParseStripOptions will print the help messege and
263 // exit. ErrorCallback is used to handle recoverable errors. An Error returned
264 // by the callback aborts the parsing and is then returned by this function.
265 Expected<DriverConfig>
266 parseStripOptions(ArrayRef<const char *> ArgsArr,
267 llvm::function_ref<Error(Error)> ErrorCallback);
268 } // namespace objcopy