blob: 07eac9d2bb1b54307298a6fc8bc1a41611132a20 [file] [log] [blame]
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +00001//===- CopyConfig.h -------------------------------------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +00006//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_TOOLS_LLVM_OBJCOPY_COPY_CONFIG_H
10#define LLVM_TOOLS_LLVM_OBJCOPY_COPY_CONFIG_H
11
Seiya Nutac83eefc2019-09-24 09:38:23 +000012#include "ELF/ELFConfig.h"
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +000013#include "llvm/ADT/ArrayRef.h"
Jordan Rupprechtbd95a9f2019-03-28 18:27:00 +000014#include "llvm/ADT/BitmaskEnum.h"
Alexander Shaposhnikovd332ec92020-06-22 16:49:14 -070015#include "llvm/ADT/DenseSet.h"
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +000016#include "llvm/ADT/Optional.h"
17#include "llvm/ADT/SmallVector.h"
18#include "llvm/ADT/StringMap.h"
19#include "llvm/ADT/StringRef.h"
Eugene Leviant51c1f642019-02-25 14:12:41 +000020#include "llvm/Object/ELFTypes.h"
Jordan Rupprecht5745c5f2019-02-04 18:38:00 +000021#include "llvm/Support/Allocator.h"
Jordan Rupprechtad29d292019-02-21 17:05:19 +000022#include "llvm/Support/Error.h"
Jordan Rupprechtedeebad2019-10-17 20:51:00 +000023#include "llvm/Support/GlobPattern.h"
Eugene Leviantf324f6d2019-02-06 11:00:07 +000024#include "llvm/Support/Regex.h"
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +000025// Necessary for llvm::DebugCompressionType::None
26#include "llvm/Target/TargetOptions.h"
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +000027#include <vector>
28
29namespace llvm {
30namespace objcopy {
31
Seiya Nutaecb60b72019-07-05 05:28:38 +000032enum class FileFormat {
33 Unspecified,
34 ELF,
35 Binary,
36 IHex,
37};
38
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +000039// This type keeps track of the machine info for various architectures. This
40// lets us map architecture names to ELF types and the e_machine value of the
41// ELF file.
42struct MachineInfo {
Jordan Rupprechtb0b65ca2019-04-17 07:42:31 +000043 MachineInfo(uint16_t EM, uint8_t ABI, bool Is64, bool IsLittle)
44 : EMachine(EM), OSABI(ABI), Is64Bit(Is64), IsLittleEndian(IsLittle) {}
45 // Alternative constructor that defaults to NONE for OSABI.
46 MachineInfo(uint16_t EM, bool Is64, bool IsLittle)
47 : MachineInfo(EM, ELF::ELFOSABI_NONE, Is64, IsLittle) {}
48 // Default constructor for unset fields.
49 MachineInfo() : MachineInfo(0, 0, false, false) {}
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +000050 uint16_t EMachine;
James Hendersonc040d5d2019-03-22 10:21:09 +000051 uint8_t OSABI;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +000052 bool Is64Bit;
53 bool IsLittleEndian;
54};
55
Jordan Rupprechtbd95a9f2019-03-28 18:27:00 +000056// Flags set by --set-section-flags or --rename-section. Interpretation of these
57// is format-specific and not all flags are meaningful for all object file
58// formats. This is a bitmask; many section flags may be set.
59enum SectionFlag {
60 SecNone = 0,
61 SecAlloc = 1 << 0,
62 SecLoad = 1 << 1,
63 SecNoload = 1 << 2,
64 SecReadonly = 1 << 3,
65 SecDebug = 1 << 4,
66 SecCode = 1 << 5,
67 SecData = 1 << 6,
68 SecRom = 1 << 7,
69 SecMerge = 1 << 8,
70 SecStrings = 1 << 9,
71 SecContents = 1 << 10,
72 SecShare = 1 << 11,
Sergey Dmitrieve4463222020-01-20 17:06:03 -080073 SecExclude = 1 << 12,
74 LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue=*/SecExclude)
Jordan Rupprechtbd95a9f2019-03-28 18:27:00 +000075};
76
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +000077struct SectionRename {
78 StringRef OriginalName;
79 StringRef NewName;
Jordan Rupprechtbd95a9f2019-03-28 18:27:00 +000080 Optional<SectionFlag> NewFlags;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +000081};
82
Jordan Rupprechtc8927412019-01-29 15:05:38 +000083struct SectionFlagsUpdate {
84 StringRef Name;
Jordan Rupprechtbd95a9f2019-03-28 18:27:00 +000085 SectionFlag NewFlags;
Jordan Rupprechtc8927412019-01-29 15:05:38 +000086};
87
Jordan Rupprechtd0f7bcf2019-01-30 14:58:13 +000088enum class DiscardType {
89 None, // Default
90 All, // --discard-all (-x)
91 Locals, // --discard-locals (-X)
92};
93
Jordan Rupprechtedeebad2019-10-17 20:51:00 +000094enum class MatchStyle {
95 Literal, // Default for symbols.
96 Wildcard, // Default for sections, or enabled with --wildcard (-w).
97 Regex, // Enabled with --regex.
98};
99
100class NameOrPattern {
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000101 StringRef Name;
102 // Regex is shared between multiple CopyConfig instances.
103 std::shared_ptr<Regex> R;
Jordan Rupprechtedeebad2019-10-17 20:51:00 +0000104 std::shared_ptr<GlobPattern> G;
105 bool IsPositiveMatch = true;
106
107 NameOrPattern(StringRef N) : Name(N) {}
108 NameOrPattern(std::shared_ptr<Regex> R) : R(R) {}
109 NameOrPattern(std::shared_ptr<GlobPattern> G, bool IsPositiveMatch)
110 : G(G), IsPositiveMatch(IsPositiveMatch) {}
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000111
112public:
Jordan Rupprechtedeebad2019-10-17 20:51:00 +0000113 // ErrorCallback is used to handle recoverable errors. An Error returned
114 // by the callback aborts the parsing and is then returned by this function.
115 static Expected<NameOrPattern>
116 create(StringRef Pattern, MatchStyle MS,
117 llvm::function_ref<Error(Error)> ErrorCallback);
118
119 bool isPositiveMatch() const { return IsPositiveMatch; }
120 bool operator==(StringRef S) const {
121 return R ? R->match(S) : G ? G->match(S) : Name == S;
122 }
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000123 bool operator!=(StringRef S) const { return !operator==(S); }
124};
125
Jordan Rupprecht6c6dd6a2019-08-22 19:17:50 +0000126// Matcher that checks symbol or section names against the command line flags
127// provided for that option.
128class NameMatcher {
Jordan Rupprechtedeebad2019-10-17 20:51:00 +0000129 std::vector<NameOrPattern> PosMatchers;
130 std::vector<NameOrPattern> NegMatchers;
Jordan Rupprecht6c6dd6a2019-08-22 19:17:50 +0000131
132public:
Jordan Rupprechtedeebad2019-10-17 20:51:00 +0000133 Error addMatcher(Expected<NameOrPattern> Matcher) {
134 if (!Matcher)
135 return Matcher.takeError();
136 if (Matcher->isPositiveMatch())
137 PosMatchers.push_back(std::move(*Matcher));
138 else
139 NegMatchers.push_back(std::move(*Matcher));
140 return Error::success();
Jordan Rupprecht6c6dd6a2019-08-22 19:17:50 +0000141 }
Jordan Rupprechtedeebad2019-10-17 20:51:00 +0000142 bool matches(StringRef S) const {
143 return is_contained(PosMatchers, S) && !is_contained(NegMatchers, S);
144 }
145 bool empty() const { return PosMatchers.empty() && NegMatchers.empty(); }
Jordan Rupprecht6c6dd6a2019-08-22 19:17:50 +0000146};
147
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000148// Configuration for copying/stripping a single file.
149struct CopyConfig {
Seiya Nutac83eefc2019-09-24 09:38:23 +0000150 // Format-specific options to be initialized lazily when needed.
151 Optional<elf::ELFCopyConfig> ELF;
152
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000153 // Main input/output options
154 StringRef InputFilename;
Alexander Shaposhnikovc54959c2019-11-19 23:30:52 -0800155 FileFormat InputFormat = FileFormat::Unspecified;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000156 StringRef OutputFilename;
Alexander Shaposhnikovc54959c2019-11-19 23:30:52 -0800157 FileFormat OutputFormat = FileFormat::Unspecified;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000158
Jordan Rupprecht70038e02019-01-07 16:59:12 +0000159 // Only applicable when --output-format!=binary (e.g. elf64-x86-64).
160 Optional<MachineInfo> OutputArch;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000161
162 // Advanced options
163 StringRef AddGnuDebugLink;
James Henderson9df38832019-05-14 10:59:04 +0000164 // Cached gnu_debuglink's target CRC
165 uint32_t GnuDebugLinkCRC32;
Jake Ehrlich8ad77792018-12-03 19:49:23 +0000166 StringRef BuildIdLinkDir;
167 Optional<StringRef> BuildIdLinkInput;
168 Optional<StringRef> BuildIdLinkOutput;
Peter Collingbourne8d58a982019-06-07 17:57:48 +0000169 Optional<StringRef> ExtractPartition;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000170 StringRef SplitDWO;
171 StringRef SymbolsPrefix;
James Hendersonfa11fb32019-05-08 09:49:35 +0000172 StringRef AllocSectionsPrefix;
Jordan Rupprechtd0f7bcf2019-01-30 14:58:13 +0000173 DiscardType DiscardMode = DiscardType::None;
Seiya Nutac83eefc2019-09-24 09:38:23 +0000174 Optional<StringRef> NewSymbolVisibility;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000175
176 // Repeated options
177 std::vector<StringRef> AddSection;
178 std::vector<StringRef> DumpSection;
Seiya Nutac83eefc2019-09-24 09:38:23 +0000179 std::vector<StringRef> SymbolsToAdd;
Alexander Shaposhnikovc54959c2019-11-19 23:30:52 -0800180 std::vector<StringRef> RPathToAdd;
Keith Smiley77cbf252020-10-23 15:00:25 -0700181 std::vector<StringRef> RPathToPrepend;
Sameer Arora3b5db7f2020-07-06 14:53:24 -0700182 DenseMap<StringRef, StringRef> RPathsToUpdate;
183 DenseMap<StringRef, StringRef> InstallNamesToUpdate;
Alexander Shaposhnikovd332ec92020-06-22 16:49:14 -0700184 DenseSet<StringRef> RPathsToRemove;
Jordan Rupprecht6c6dd6a2019-08-22 19:17:50 +0000185
Sameer Aroraca518c42020-06-30 11:01:45 -0700186 // install-name-tool's id option
187 Optional<StringRef> SharedLibId;
188
Jordan Rupprecht6c6dd6a2019-08-22 19:17:50 +0000189 // Section matchers
190 NameMatcher KeepSection;
191 NameMatcher OnlySection;
192 NameMatcher ToRemove;
193
194 // Symbol matchers
195 NameMatcher SymbolsToGlobalize;
196 NameMatcher SymbolsToKeep;
197 NameMatcher SymbolsToLocalize;
198 NameMatcher SymbolsToRemove;
199 NameMatcher UnneededSymbolsToRemove;
200 NameMatcher SymbolsToWeaken;
201 NameMatcher SymbolsToKeepGlobal;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000202
203 // Map options
204 StringMap<SectionRename> SectionsToRename;
Fangrui Song671fb342019-10-02 12:41:25 +0000205 StringMap<uint64_t> SetSectionAlignment;
Jordan Rupprechtc8927412019-01-29 15:05:38 +0000206 StringMap<SectionFlagsUpdate> SetSectionFlags;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000207 StringMap<StringRef> SymbolsToRename;
208
Eugene Leviant53350d02019-02-26 09:24:22 +0000209 // ELF entry point address expression. The input parameter is an entry point
210 // address in the input ELF file. The entry address in the output file is
211 // calculated with EntryExpr(input_address), when either --set-start or
212 // --change-start is used.
213 std::function<uint64_t(uint64_t)> EntryExpr;
214
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000215 // Boolean options
James Henderson66a9d0f2019-04-18 09:13:30 +0000216 bool AllowBrokenLinks = false;
Jordan Rupprechtfc780bb2018-11-01 17:36:37 +0000217 bool DeterministicArchives = true;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000218 bool ExtractDWO = false;
Peter Collingbourne8d58a982019-06-07 17:57:48 +0000219 bool ExtractMainPartition = false;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000220 bool KeepFileSymbols = false;
221 bool LocalizeHidden = false;
222 bool OnlyKeepDebug = false;
223 bool PreserveDates = false;
224 bool StripAll = false;
225 bool StripAllGNU = false;
226 bool StripDWO = false;
227 bool StripDebug = false;
228 bool StripNonAlloc = false;
229 bool StripSections = false;
Alexander Shaposhnikov842a8cc2020-05-26 16:49:56 -0700230 bool StripSwiftSymbols = false;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000231 bool StripUnneeded = false;
232 bool Weaken = false;
233 bool DecompressDebugSections = false;
Tobias Hieta61133e02020-10-13 00:45:14 -0700234 // install-name-tool's --delete_all_rpaths
235 bool RemoveAllRpaths = false;
236
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000237 DebugCompressionType CompressionType = DebugCompressionType::None;
Seiya Nutac83eefc2019-09-24 09:38:23 +0000238
239 // parseELFConfig performs ELF-specific command-line parsing. Fills `ELF` on
240 // success or returns an Error otherwise.
241 Error parseELFConfig() {
242 if (!ELF) {
243 Expected<elf::ELFCopyConfig> ELFConfig = elf::parseConfig(*this);
244 if (!ELFConfig)
245 return ELFConfig.takeError();
246 ELF = *ELFConfig;
247 }
248 return Error::success();
249 }
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000250};
251
252// Configuration for the overall invocation of this tool. When invoked as
253// objcopy, will always contain exactly one CopyConfig. When invoked as strip,
254// will contain one or more CopyConfigs.
255struct DriverConfig {
256 SmallVector<CopyConfig, 1> CopyConfigs;
Jordan Rupprecht5745c5f2019-02-04 18:38:00 +0000257 BumpPtrAllocator Alloc;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000258};
259
260// ParseObjcopyOptions returns the config and sets the input arguments. If a
261// help flag is set then ParseObjcopyOptions will print the help messege and
Jordan Rupprechtedeebad2019-10-17 20:51:00 +0000262// exit. ErrorCallback is used to handle recoverable errors. An Error returned
263// by the callback aborts the parsing and is then returned by this function.
264Expected<DriverConfig>
265parseObjcopyOptions(ArrayRef<const char *> ArgsArr,
266 llvm::function_ref<Error(Error)> ErrorCallback);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000267
Alexander Shaposhnikovc54959c2019-11-19 23:30:52 -0800268// ParseInstallNameToolOptions returns the config and sets the input arguments.
269// If a help flag is set then ParseInstallNameToolOptions will print the help
270// messege and exit.
271Expected<DriverConfig>
272parseInstallNameToolOptions(ArrayRef<const char *> ArgsArr);
273
Alexander Shaposhnikov5495b692020-09-18 18:11:22 -0700274// ParseBitcodeStripOptions returns the config and sets the input arguments.
275// If a help flag is set then ParseBitcodeStripOptions will print the help
276// messege and exit.
277Expected<DriverConfig> parseBitcodeStripOptions(ArrayRef<const char *> ArgsArr);
278
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000279// ParseStripOptions returns the config and sets the input arguments. If a
280// help flag is set then ParseStripOptions will print the help messege and
Alex Brachet77477002019-06-18 00:39:10 +0000281// exit. ErrorCallback is used to handle recoverable errors. An Error returned
282// by the callback aborts the parsing and is then returned by this function.
283Expected<DriverConfig>
284parseStripOptions(ArrayRef<const char *> ArgsArr,
Jordan Rupprechtedeebad2019-10-17 20:51:00 +0000285 llvm::function_ref<Error(Error)> ErrorCallback);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000286} // namespace objcopy
287} // namespace llvm
288
289#endif