blob: c262934b4a419168d016588c4a2aac31b910fa6f [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 Shaposhnikov8d0b74c2018-10-11 22:33:50 +000015#include "llvm/ADT/Optional.h"
16#include "llvm/ADT/SmallVector.h"
17#include "llvm/ADT/StringMap.h"
18#include "llvm/ADT/StringRef.h"
Eugene Leviant51c1f642019-02-25 14:12:41 +000019#include "llvm/Object/ELFTypes.h"
Jordan Rupprecht5745c5f2019-02-04 18:38:00 +000020#include "llvm/Support/Allocator.h"
Jordan Rupprechtad29d292019-02-21 17:05:19 +000021#include "llvm/Support/Error.h"
Jordan Rupprechtedeebad2019-10-17 20:51:00 +000022#include "llvm/Support/GlobPattern.h"
Eugene Leviantf324f6d2019-02-06 11:00:07 +000023#include "llvm/Support/Regex.h"
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +000024// Necessary for llvm::DebugCompressionType::None
25#include "llvm/Target/TargetOptions.h"
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +000026#include <vector>
27
28namespace llvm {
29namespace objcopy {
30
Seiya Nutaecb60b72019-07-05 05:28:38 +000031enum class FileFormat {
32 Unspecified,
33 ELF,
34 Binary,
35 IHex,
36};
37
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +000038// 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
40// ELF file.
41struct MachineInfo {
Jordan Rupprechtb0b65ca2019-04-17 07:42:31 +000042 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) {}
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +000049 uint16_t EMachine;
James Hendersonc040d5d2019-03-22 10:21:09 +000050 uint8_t OSABI;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +000051 bool Is64Bit;
52 bool IsLittleEndian;
53};
54
Jordan Rupprechtbd95a9f2019-03-28 18:27:00 +000055// 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.
58enum SectionFlag {
59 SecNone = 0,
60 SecAlloc = 1 << 0,
61 SecLoad = 1 << 1,
62 SecNoload = 1 << 2,
63 SecReadonly = 1 << 3,
64 SecDebug = 1 << 4,
65 SecCode = 1 << 5,
66 SecData = 1 << 6,
67 SecRom = 1 << 7,
68 SecMerge = 1 << 8,
69 SecStrings = 1 << 9,
70 SecContents = 1 << 10,
71 SecShare = 1 << 11,
72 LLVM_MARK_AS_BITMASK_ENUM(/* LargestValue = */ SecShare)
73};
74
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +000075struct SectionRename {
76 StringRef OriginalName;
77 StringRef NewName;
Jordan Rupprechtbd95a9f2019-03-28 18:27:00 +000078 Optional<SectionFlag> NewFlags;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +000079};
80
Jordan Rupprechtc8927412019-01-29 15:05:38 +000081struct SectionFlagsUpdate {
82 StringRef Name;
Jordan Rupprechtbd95a9f2019-03-28 18:27:00 +000083 SectionFlag NewFlags;
Jordan Rupprechtc8927412019-01-29 15:05:38 +000084};
85
Jordan Rupprechtd0f7bcf2019-01-30 14:58:13 +000086enum class DiscardType {
87 None, // Default
88 All, // --discard-all (-x)
89 Locals, // --discard-locals (-X)
90};
91
Jordan Rupprechtedeebad2019-10-17 20:51:00 +000092enum class MatchStyle {
93 Literal, // Default for symbols.
94 Wildcard, // Default for sections, or enabled with --wildcard (-w).
95 Regex, // Enabled with --regex.
96};
97
98class NameOrPattern {
Eugene Leviantf324f6d2019-02-06 11:00:07 +000099 StringRef Name;
100 // Regex is shared between multiple CopyConfig instances.
101 std::shared_ptr<Regex> R;
Jordan Rupprechtedeebad2019-10-17 20:51:00 +0000102 std::shared_ptr<GlobPattern> G;
103 bool IsPositiveMatch = true;
104
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) {}
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000109
110public:
Jordan Rupprechtedeebad2019-10-17 20:51:00 +0000111 // 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);
116
117 bool isPositiveMatch() const { return IsPositiveMatch; }
118 bool operator==(StringRef S) const {
119 return R ? R->match(S) : G ? G->match(S) : Name == S;
120 }
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000121 bool operator!=(StringRef S) const { return !operator==(S); }
122};
123
Jordan Rupprecht6c6dd6a2019-08-22 19:17:50 +0000124// Matcher that checks symbol or section names against the command line flags
125// provided for that option.
126class NameMatcher {
Jordan Rupprechtedeebad2019-10-17 20:51:00 +0000127 std::vector<NameOrPattern> PosMatchers;
128 std::vector<NameOrPattern> NegMatchers;
Jordan Rupprecht6c6dd6a2019-08-22 19:17:50 +0000129
130public:
Jordan Rupprechtedeebad2019-10-17 20:51:00 +0000131 Error addMatcher(Expected<NameOrPattern> Matcher) {
132 if (!Matcher)
133 return Matcher.takeError();
134 if (Matcher->isPositiveMatch())
135 PosMatchers.push_back(std::move(*Matcher));
136 else
137 NegMatchers.push_back(std::move(*Matcher));
138 return Error::success();
Jordan Rupprecht6c6dd6a2019-08-22 19:17:50 +0000139 }
Jordan Rupprechtedeebad2019-10-17 20:51:00 +0000140 bool matches(StringRef S) const {
141 return is_contained(PosMatchers, S) && !is_contained(NegMatchers, S);
142 }
143 bool empty() const { return PosMatchers.empty() && NegMatchers.empty(); }
Jordan Rupprecht6c6dd6a2019-08-22 19:17:50 +0000144};
145
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000146// Configuration for copying/stripping a single file.
147struct CopyConfig {
Seiya Nutac83eefc2019-09-24 09:38:23 +0000148 // Format-specific options to be initialized lazily when needed.
149 Optional<elf::ELFCopyConfig> ELF;
150
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000151 // Main input/output options
152 StringRef InputFilename;
Alexander Shaposhnikov88f00ae2019-11-18 15:44:58 -0800153 FileFormat InputFormat = FileFormat::Unspecified;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000154 StringRef OutputFilename;
Alexander Shaposhnikov88f00ae2019-11-18 15:44:58 -0800155 FileFormat OutputFormat = FileFormat::Unspecified;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000156
Jordan Rupprecht70038e02019-01-07 16:59:12 +0000157 // Only applicable when --output-format!=binary (e.g. elf64-x86-64).
158 Optional<MachineInfo> OutputArch;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000159
160 // Advanced options
161 StringRef AddGnuDebugLink;
James Henderson9df38832019-05-14 10:59:04 +0000162 // Cached gnu_debuglink's target CRC
163 uint32_t GnuDebugLinkCRC32;
Jake Ehrlich8ad77792018-12-03 19:49:23 +0000164 StringRef BuildIdLinkDir;
165 Optional<StringRef> BuildIdLinkInput;
166 Optional<StringRef> BuildIdLinkOutput;
Peter Collingbourne8d58a982019-06-07 17:57:48 +0000167 Optional<StringRef> ExtractPartition;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000168 StringRef SplitDWO;
169 StringRef SymbolsPrefix;
James Hendersonfa11fb32019-05-08 09:49:35 +0000170 StringRef AllocSectionsPrefix;
Jordan Rupprechtd0f7bcf2019-01-30 14:58:13 +0000171 DiscardType DiscardMode = DiscardType::None;
Seiya Nutac83eefc2019-09-24 09:38:23 +0000172 Optional<StringRef> NewSymbolVisibility;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000173
174 // Repeated options
175 std::vector<StringRef> AddSection;
176 std::vector<StringRef> DumpSection;
Seiya Nutac83eefc2019-09-24 09:38:23 +0000177 std::vector<StringRef> SymbolsToAdd;
Alexander Shaposhnikov88f00ae2019-11-18 15:44:58 -0800178 std::vector<StringRef> RPathToAdd;
Jordan Rupprecht6c6dd6a2019-08-22 19:17:50 +0000179
180 // Section matchers
181 NameMatcher KeepSection;
182 NameMatcher OnlySection;
183 NameMatcher ToRemove;
184
185 // Symbol matchers
186 NameMatcher SymbolsToGlobalize;
187 NameMatcher SymbolsToKeep;
188 NameMatcher SymbolsToLocalize;
189 NameMatcher SymbolsToRemove;
190 NameMatcher UnneededSymbolsToRemove;
191 NameMatcher SymbolsToWeaken;
192 NameMatcher SymbolsToKeepGlobal;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000193
194 // Map options
195 StringMap<SectionRename> SectionsToRename;
Fangrui Song671fb342019-10-02 12:41:25 +0000196 StringMap<uint64_t> SetSectionAlignment;
Jordan Rupprechtc8927412019-01-29 15:05:38 +0000197 StringMap<SectionFlagsUpdate> SetSectionFlags;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000198 StringMap<StringRef> SymbolsToRename;
199
Eugene Leviant53350d02019-02-26 09:24:22 +0000200 // 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;
205
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000206 // Boolean options
James Henderson66a9d0f2019-04-18 09:13:30 +0000207 bool AllowBrokenLinks = false;
Jordan Rupprechtfc780bb2018-11-01 17:36:37 +0000208 bool DeterministicArchives = true;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000209 bool ExtractDWO = false;
Peter Collingbourne8d58a982019-06-07 17:57:48 +0000210 bool ExtractMainPartition = false;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000211 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;
222 bool Weaken = false;
223 bool DecompressDebugSections = false;
224 DebugCompressionType CompressionType = DebugCompressionType::None;
Seiya Nutac83eefc2019-09-24 09:38:23 +0000225
226 // parseELFConfig performs ELF-specific command-line parsing. Fills `ELF` on
227 // success or returns an Error otherwise.
228 Error parseELFConfig() {
229 if (!ELF) {
230 Expected<elf::ELFCopyConfig> ELFConfig = elf::parseConfig(*this);
231 if (!ELFConfig)
232 return ELFConfig.takeError();
233 ELF = *ELFConfig;
234 }
235 return Error::success();
236 }
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000237};
238
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.
242struct DriverConfig {
243 SmallVector<CopyConfig, 1> CopyConfigs;
Jordan Rupprecht5745c5f2019-02-04 18:38:00 +0000244 BumpPtrAllocator Alloc;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000245};
246
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
Jordan Rupprechtedeebad2019-10-17 20:51:00 +0000249// 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.
251Expected<DriverConfig>
252parseObjcopyOptions(ArrayRef<const char *> ArgsArr,
253 llvm::function_ref<Error(Error)> ErrorCallback);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000254
Alexander Shaposhnikov88f00ae2019-11-18 15:44:58 -0800255// ParseInstallNameToolOptions returns the config and sets the input arguments.
256// If a help flag is set then ParseInstallNameToolOptions will print the help
257// messege and exit.
258Expected<DriverConfig>
259parseInstallNameToolOptions(ArrayRef<const char *> ArgsArr);
260
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000261// ParseStripOptions returns the config and sets the input arguments. If a
262// help flag is set then ParseStripOptions will print the help messege and
Alex Brachet77477002019-06-18 00:39:10 +0000263// 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.
265Expected<DriverConfig>
266parseStripOptions(ArrayRef<const char *> ArgsArr,
Jordan Rupprechtedeebad2019-10-17 20:51:00 +0000267 llvm::function_ref<Error(Error)> ErrorCallback);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000268} // namespace objcopy
269} // namespace llvm
270
271#endif