blob: 1e6aa40b5c0488d4af4fb39c5568dabf8ccfc9d3 [file] [log] [blame]
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +00001//===- CopyConfig.cpp -----------------------------------------------------===//
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#include "CopyConfig.h"
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +000010
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +000011#include "llvm/ADT/Optional.h"
12#include "llvm/ADT/SmallVector.h"
13#include "llvm/ADT/StringRef.h"
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +000014#include "llvm/Option/Arg.h"
15#include "llvm/Option/ArgList.h"
16#include "llvm/Support/CommandLine.h"
17#include "llvm/Support/Compression.h"
Eugene Leviant340cb872019-02-08 10:33:16 +000018#include "llvm/Support/Errc.h"
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +000019#include "llvm/Support/MemoryBuffer.h"
Jordan Rupprecht5745c5f2019-02-04 18:38:00 +000020#include "llvm/Support/StringSaver.h"
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +000021#include <memory>
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +000022
23namespace llvm {
24namespace objcopy {
25
26namespace {
27enum ObjcopyID {
28 OBJCOPY_INVALID = 0, // This is not an option ID.
29#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
30 HELPTEXT, METAVAR, VALUES) \
31 OBJCOPY_##ID,
32#include "ObjcopyOpts.inc"
33#undef OPTION
34};
35
36#define PREFIX(NAME, VALUE) const char *const OBJCOPY_##NAME[] = VALUE;
37#include "ObjcopyOpts.inc"
38#undef PREFIX
39
40static const opt::OptTable::Info ObjcopyInfoTable[] = {
41#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
42 HELPTEXT, METAVAR, VALUES) \
43 {OBJCOPY_##PREFIX, \
44 NAME, \
45 HELPTEXT, \
46 METAVAR, \
47 OBJCOPY_##ID, \
48 opt::Option::KIND##Class, \
49 PARAM, \
50 FLAGS, \
51 OBJCOPY_##GROUP, \
52 OBJCOPY_##ALIAS, \
53 ALIASARGS, \
54 VALUES},
55#include "ObjcopyOpts.inc"
56#undef OPTION
57};
58
59class ObjcopyOptTable : public opt::OptTable {
60public:
Jordan Rupprechtaaeaa0a2018-10-23 18:46:33 +000061 ObjcopyOptTable() : OptTable(ObjcopyInfoTable) {}
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +000062};
63
64enum StripID {
65 STRIP_INVALID = 0, // This is not an option ID.
66#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
67 HELPTEXT, METAVAR, VALUES) \
68 STRIP_##ID,
69#include "StripOpts.inc"
70#undef OPTION
71};
72
73#define PREFIX(NAME, VALUE) const char *const STRIP_##NAME[] = VALUE;
74#include "StripOpts.inc"
75#undef PREFIX
76
77static const opt::OptTable::Info StripInfoTable[] = {
78#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
79 HELPTEXT, METAVAR, VALUES) \
80 {STRIP_##PREFIX, NAME, HELPTEXT, \
81 METAVAR, STRIP_##ID, opt::Option::KIND##Class, \
82 PARAM, FLAGS, STRIP_##GROUP, \
83 STRIP_##ALIAS, ALIASARGS, VALUES},
84#include "StripOpts.inc"
85#undef OPTION
86};
87
88class StripOptTable : public opt::OptTable {
89public:
Jordan Rupprechtaaeaa0a2018-10-23 18:46:33 +000090 StripOptTable() : OptTable(StripInfoTable) {}
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +000091};
92
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +000093} // namespace
94
95static SectionFlag parseSectionRenameFlag(StringRef SectionName) {
96 return llvm::StringSwitch<SectionFlag>(SectionName)
James Hendersond931cf32019-04-03 14:40:27 +000097 .CaseLower("alloc", SectionFlag::SecAlloc)
98 .CaseLower("load", SectionFlag::SecLoad)
99 .CaseLower("noload", SectionFlag::SecNoload)
100 .CaseLower("readonly", SectionFlag::SecReadonly)
101 .CaseLower("debug", SectionFlag::SecDebug)
102 .CaseLower("code", SectionFlag::SecCode)
103 .CaseLower("data", SectionFlag::SecData)
104 .CaseLower("rom", SectionFlag::SecRom)
105 .CaseLower("merge", SectionFlag::SecMerge)
106 .CaseLower("strings", SectionFlag::SecStrings)
107 .CaseLower("contents", SectionFlag::SecContents)
108 .CaseLower("share", SectionFlag::SecShare)
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000109 .Default(SectionFlag::SecNone);
110}
111
Jordan Rupprechtbd95a9f2019-03-28 18:27:00 +0000112static Expected<SectionFlag>
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000113parseSectionFlagSet(ArrayRef<StringRef> SectionFlags) {
Jordan Rupprechtc8927412019-01-29 15:05:38 +0000114 SectionFlag ParsedFlags = SectionFlag::SecNone;
115 for (StringRef Flag : SectionFlags) {
116 SectionFlag ParsedFlag = parseSectionRenameFlag(Flag);
117 if (ParsedFlag == SectionFlag::SecNone)
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000118 return createStringError(
119 errc::invalid_argument,
120 "Unrecognized section flag '%s'. Flags supported for GNU "
121 "compatibility: alloc, load, noload, readonly, debug, code, data, "
122 "rom, share, contents, merge, strings",
123 Flag.str().c_str());
Jordan Rupprechtc8927412019-01-29 15:05:38 +0000124 ParsedFlags |= ParsedFlag;
125 }
126
Jordan Rupprechtbd95a9f2019-03-28 18:27:00 +0000127 return ParsedFlags;
Jordan Rupprechtc8927412019-01-29 15:05:38 +0000128}
129
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000130static Expected<SectionRename> parseRenameSectionValue(StringRef FlagValue) {
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000131 if (!FlagValue.contains('='))
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000132 return createStringError(errc::invalid_argument,
133 "Bad format for --rename-section: missing '='");
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000134
135 // Initial split: ".foo" = ".bar,f1,f2,..."
136 auto Old2New = FlagValue.split('=');
137 SectionRename SR;
138 SR.OriginalName = Old2New.first;
139
140 // Flags split: ".bar" "f1" "f2" ...
141 SmallVector<StringRef, 6> NameAndFlags;
142 Old2New.second.split(NameAndFlags, ',');
143 SR.NewName = NameAndFlags[0];
144
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000145 if (NameAndFlags.size() > 1) {
Jordan Rupprechtbd95a9f2019-03-28 18:27:00 +0000146 Expected<SectionFlag> ParsedFlagSet =
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000147 parseSectionFlagSet(makeArrayRef(NameAndFlags).drop_front());
148 if (!ParsedFlagSet)
149 return ParsedFlagSet.takeError();
150 SR.NewFlags = *ParsedFlagSet;
151 }
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000152
153 return SR;
154}
155
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000156static Expected<SectionFlagsUpdate>
157parseSetSectionFlagValue(StringRef FlagValue) {
Jordan Rupprechtc8927412019-01-29 15:05:38 +0000158 if (!StringRef(FlagValue).contains('='))
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000159 return createStringError(errc::invalid_argument,
160 "Bad format for --set-section-flags: missing '='");
Jordan Rupprechtc8927412019-01-29 15:05:38 +0000161
162 // Initial split: ".foo" = "f1,f2,..."
163 auto Section2Flags = StringRef(FlagValue).split('=');
164 SectionFlagsUpdate SFU;
165 SFU.Name = Section2Flags.first;
166
167 // Flags split: "f1" "f2" ...
168 SmallVector<StringRef, 6> SectionFlags;
169 Section2Flags.second.split(SectionFlags, ',');
Jordan Rupprechtbd95a9f2019-03-28 18:27:00 +0000170 Expected<SectionFlag> ParsedFlagSet = parseSectionFlagSet(SectionFlags);
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000171 if (!ParsedFlagSet)
172 return ParsedFlagSet.takeError();
173 SFU.NewFlags = *ParsedFlagSet;
Jordan Rupprechtc8927412019-01-29 15:05:38 +0000174
175 return SFU;
176}
177
Jordan Rupprecht42bc1e22019-03-13 22:26:01 +0000178static Expected<NewSymbolInfo> parseNewSymbolInfo(StringRef FlagValue) {
Eugene Leviant51c1f642019-02-25 14:12:41 +0000179 // Parse value given with --add-symbol option and create the
180 // new symbol if possible. The value format for --add-symbol is:
181 //
182 // <name>=[<section>:]<value>[,<flags>]
183 //
184 // where:
185 // <name> - symbol name, can be empty string
186 // <section> - optional section name. If not given ABS symbol is created
187 // <value> - symbol value, can be decimal or hexadecimal number prefixed
188 // with 0x.
189 // <flags> - optional flags affecting symbol type, binding or visibility:
190 // The following are currently supported:
191 //
192 // global, local, weak, default, hidden, file, section, object,
193 // indirect-function.
194 //
195 // The following flags are ignored and provided for GNU
196 // compatibility only:
197 //
198 // warning, debug, constructor, indirect, synthetic,
199 // unique-object, before=<symbol>.
200 NewSymbolInfo SI;
201 StringRef Value;
202 std::tie(SI.SymbolName, Value) = FlagValue.split('=');
203 if (Value.empty())
Jordan Rupprecht42bc1e22019-03-13 22:26:01 +0000204 return createStringError(
205 errc::invalid_argument,
206 "bad format for --add-symbol, missing '=' after '%s'",
207 SI.SymbolName.str().c_str());
Eugene Leviant51c1f642019-02-25 14:12:41 +0000208
209 if (Value.contains(':')) {
210 std::tie(SI.SectionName, Value) = Value.split(':');
211 if (SI.SectionName.empty() || Value.empty())
Jordan Rupprecht42bc1e22019-03-13 22:26:01 +0000212 return createStringError(
213 errc::invalid_argument,
Eugene Leviant51c1f642019-02-25 14:12:41 +0000214 "bad format for --add-symbol, missing section name or symbol value");
215 }
216
217 SmallVector<StringRef, 6> Flags;
218 Value.split(Flags, ',');
219 if (Flags[0].getAsInteger(0, SI.Value))
Jordan Rupprecht42bc1e22019-03-13 22:26:01 +0000220 return createStringError(errc::invalid_argument, "bad symbol value: '%s'",
221 Flags[0].str().c_str());
Eugene Leviant51c1f642019-02-25 14:12:41 +0000222
Jordan Rupprecht42bc1e22019-03-13 22:26:01 +0000223 using Functor = std::function<void(void)>;
224 SmallVector<StringRef, 6> UnsupportedFlags;
225 for (size_t I = 1, NumFlags = Flags.size(); I < NumFlags; ++I)
Eugene Leviant51c1f642019-02-25 14:12:41 +0000226 static_cast<Functor>(
227 StringSwitch<Functor>(Flags[I])
228 .CaseLower("global", [&SI] { SI.Bind = ELF::STB_GLOBAL; })
229 .CaseLower("local", [&SI] { SI.Bind = ELF::STB_LOCAL; })
230 .CaseLower("weak", [&SI] { SI.Bind = ELF::STB_WEAK; })
231 .CaseLower("default", [&SI] { SI.Visibility = ELF::STV_DEFAULT; })
232 .CaseLower("hidden", [&SI] { SI.Visibility = ELF::STV_HIDDEN; })
233 .CaseLower("file", [&SI] { SI.Type = ELF::STT_FILE; })
234 .CaseLower("section", [&SI] { SI.Type = ELF::STT_SECTION; })
235 .CaseLower("object", [&SI] { SI.Type = ELF::STT_OBJECT; })
236 .CaseLower("function", [&SI] { SI.Type = ELF::STT_FUNC; })
237 .CaseLower("indirect-function",
238 [&SI] { SI.Type = ELF::STT_GNU_IFUNC; })
239 .CaseLower("debug", [] {})
240 .CaseLower("constructor", [] {})
241 .CaseLower("warning", [] {})
242 .CaseLower("indirect", [] {})
243 .CaseLower("synthetic", [] {})
244 .CaseLower("unique-object", [] {})
245 .StartsWithLower("before", [] {})
Jordan Rupprecht42bc1e22019-03-13 22:26:01 +0000246 .Default([&] { UnsupportedFlags.push_back(Flags[I]); }))();
247 if (!UnsupportedFlags.empty())
248 return createStringError(errc::invalid_argument,
249 "unsupported flag%s for --add-symbol: '%s'",
250 UnsupportedFlags.size() > 1 ? "s" : "",
251 join(UnsupportedFlags, "', '").c_str());
Eugene Leviant51c1f642019-02-25 14:12:41 +0000252 return SI;
253}
254
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000255static const StringMap<MachineInfo> ArchMap{
Jordan Rupprechtb0b65ca2019-04-17 07:42:31 +0000256 // Name, {EMachine, 64bit, LittleEndian}
257 {"aarch64", {ELF::EM_AARCH64, true, true}},
258 {"arm", {ELF::EM_ARM, false, true}},
259 {"i386", {ELF::EM_386, false, true}},
260 {"i386:x86-64", {ELF::EM_X86_64, true, true}},
261 {"powerpc:common64", {ELF::EM_PPC64, true, true}},
262 {"sparc", {ELF::EM_SPARC, false, true}},
263 {"x86-64", {ELF::EM_X86_64, true, true}},
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000264};
265
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000266static Expected<const MachineInfo &> getMachineInfo(StringRef Arch) {
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000267 auto Iter = ArchMap.find(Arch);
268 if (Iter == std::end(ArchMap))
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000269 return createStringError(errc::invalid_argument,
270 "Invalid architecture: '%s'", Arch.str().c_str());
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000271 return Iter->getValue();
272}
273
Jordan Rupprechtb0b65ca2019-04-17 07:42:31 +0000274// FIXME: consolidate with the bfd parsing used by lld.
Jordan Rupprecht70038e02019-01-07 16:59:12 +0000275static const StringMap<MachineInfo> OutputFormatMap{
Jordan Rupprechtb0b65ca2019-04-17 07:42:31 +0000276 // Name, {EMachine, 64bit, LittleEndian}
277 {"elf32-i386", {ELF::EM_386, false, true}},
278 {"elf32-iamcu", {ELF::EM_IAMCU, false, true}},
279 {"elf32-littlearm", {ELF::EM_ARM, false, true}},
280 {"elf32-x86-64", {ELF::EM_X86_64, false, true}},
281 {"elf64-aarch64", {ELF::EM_AARCH64, true, true}},
282 {"elf64-littleaarch64", {ELF::EM_AARCH64, true, true}},
283 {"elf32-powerpc", {ELF::EM_PPC, false, false}},
284 {"elf32-powerpcle", {ELF::EM_PPC, false, true}},
285 {"elf64-powerpc", {ELF::EM_PPC64, true, false}},
286 {"elf64-powerpcle", {ELF::EM_PPC64, true, true}},
287 {"elf64-x86-64", {ELF::EM_X86_64, true, true}},
288 {"elf32-tradbigmips", {ELF::EM_MIPS, false, false}},
289 {"elf32-bigmips", {ELF::EM_MIPS, false, false}},
290 {"elf32-ntradbigmips", {ELF::EM_MIPS, false, false}},
291 {"elf32-tradlittlemips", {ELF::EM_MIPS, false, true}},
292 {"elf32-ntradlittlemips", {ELF::EM_MIPS, false, true}},
293 {"elf64-tradbigmips", {ELF::EM_MIPS, true, false}},
294 {"elf64-tradlittlemips", {ELF::EM_MIPS, true, true}},
Jordan Rupprecht70038e02019-01-07 16:59:12 +0000295};
296
Jordan Rupprechtb0b65ca2019-04-17 07:42:31 +0000297static Expected<MachineInfo> getOutputFormatMachineInfo(StringRef Format) {
298 StringRef OriginalFormat = Format;
299 bool IsFreeBSD = Format.consume_back("-freebsd");
Jordan Rupprecht70038e02019-01-07 16:59:12 +0000300 auto Iter = OutputFormatMap.find(Format);
301 if (Iter == std::end(OutputFormatMap))
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000302 return createStringError(errc::invalid_argument,
303 "Invalid output format: '%s'",
Jordan Rupprechtb0b65ca2019-04-17 07:42:31 +0000304 OriginalFormat.str().c_str());
305 MachineInfo MI = Iter->getValue();
306 if (IsFreeBSD)
307 MI.OSABI = ELF::ELFOSABI_FREEBSD;
308 return {MI};
Jordan Rupprecht70038e02019-01-07 16:59:12 +0000309}
310
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000311static Error addSymbolsFromFile(std::vector<NameOrRegex> &Symbols,
312 BumpPtrAllocator &Alloc, StringRef Filename,
313 bool UseRegex) {
Jordan Rupprecht5745c5f2019-02-04 18:38:00 +0000314 StringSaver Saver(Alloc);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000315 SmallVector<StringRef, 16> Lines;
316 auto BufOrErr = MemoryBuffer::getFile(Filename);
317 if (!BufOrErr)
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000318 return createFileError(Filename, BufOrErr.getError());
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000319
320 BufOrErr.get()->getBuffer().split(Lines, '\n');
321 for (StringRef Line : Lines) {
322 // Ignore everything after '#', trim whitespace, and only add the symbol if
323 // it's not empty.
324 auto TrimmedLine = Line.split('#').first.trim();
325 if (!TrimmedLine.empty())
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000326 Symbols.emplace_back(Saver.save(TrimmedLine), UseRegex);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000327 }
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000328
329 return Error::success();
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000330}
331
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000332NameOrRegex::NameOrRegex(StringRef Pattern, bool IsRegex) {
333 if (!IsRegex) {
334 Name = Pattern;
335 return;
336 }
337
338 SmallVector<char, 32> Data;
339 R = std::make_shared<Regex>(
340 ("^" + Pattern.ltrim('^').rtrim('$') + "$").toStringRef(Data));
341}
342
Eugene Leviant340cb872019-02-08 10:33:16 +0000343static Error addSymbolsToRenameFromFile(StringMap<StringRef> &SymbolsToRename,
344 BumpPtrAllocator &Alloc,
345 StringRef Filename) {
346 StringSaver Saver(Alloc);
347 SmallVector<StringRef, 16> Lines;
348 auto BufOrErr = MemoryBuffer::getFile(Filename);
349 if (!BufOrErr)
Eugene Leviant317f9e72019-02-11 09:49:37 +0000350 return createFileError(Filename, BufOrErr.getError());
Eugene Leviant340cb872019-02-08 10:33:16 +0000351
352 BufOrErr.get()->getBuffer().split(Lines, '\n');
353 size_t NumLines = Lines.size();
354 for (size_t LineNo = 0; LineNo < NumLines; ++LineNo) {
355 StringRef TrimmedLine = Lines[LineNo].split('#').first.trim();
356 if (TrimmedLine.empty())
357 continue;
358
359 std::pair<StringRef, StringRef> Pair = Saver.save(TrimmedLine).split(' ');
360 StringRef NewName = Pair.second.trim();
361 if (NewName.empty())
362 return createStringError(errc::invalid_argument,
363 "%s:%zu: missing new symbol name",
364 Filename.str().c_str(), LineNo + 1);
365 SymbolsToRename.insert({Pair.first, NewName});
366 }
367 return Error::success();
368}
Eugene Leviant53350d02019-02-26 09:24:22 +0000369
370template <class T> static ErrorOr<T> getAsInteger(StringRef Val) {
371 T Result;
372 if (Val.getAsInteger(0, Result))
373 return errc::invalid_argument;
374 return Result;
375}
376
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000377// ParseObjcopyOptions returns the config and sets the input arguments. If a
378// help flag is set then ParseObjcopyOptions will print the help messege and
379// exit.
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000380Expected<DriverConfig> parseObjcopyOptions(ArrayRef<const char *> ArgsArr) {
Jordan Rupprecht5745c5f2019-02-04 18:38:00 +0000381 DriverConfig DC;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000382 ObjcopyOptTable T;
383 unsigned MissingArgumentIndex, MissingArgumentCount;
384 llvm::opt::InputArgList InputArgs =
385 T.ParseArgs(ArgsArr, MissingArgumentIndex, MissingArgumentCount);
386
387 if (InputArgs.size() == 0) {
388 T.PrintHelp(errs(), "llvm-objcopy input [output]", "objcopy tool");
389 exit(1);
390 }
391
392 if (InputArgs.hasArg(OBJCOPY_help)) {
393 T.PrintHelp(outs(), "llvm-objcopy input [output]", "objcopy tool");
394 exit(0);
395 }
396
397 if (InputArgs.hasArg(OBJCOPY_version)) {
Martin Storsjoe9af7152018-11-28 06:51:50 +0000398 outs() << "llvm-objcopy, compatible with GNU objcopy\n";
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000399 cl::PrintVersionMessage();
400 exit(0);
401 }
402
403 SmallVector<const char *, 2> Positional;
404
405 for (auto Arg : InputArgs.filtered(OBJCOPY_UNKNOWN))
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000406 return createStringError(errc::invalid_argument, "unknown argument '%s'",
407 Arg->getAsString(InputArgs).c_str());
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000408
409 for (auto Arg : InputArgs.filtered(OBJCOPY_INPUT))
410 Positional.push_back(Arg->getValue());
411
412 if (Positional.empty())
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000413 return createStringError(errc::invalid_argument, "No input file specified");
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000414
415 if (Positional.size() > 2)
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000416 return createStringError(errc::invalid_argument,
417 "Too many positional arguments");
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000418
419 CopyConfig Config;
420 Config.InputFilename = Positional[0];
421 Config.OutputFilename = Positional[Positional.size() == 1 ? 0 : 1];
Jordan Rupprechtbb4588e2018-10-12 00:36:01 +0000422 if (InputArgs.hasArg(OBJCOPY_target) &&
423 (InputArgs.hasArg(OBJCOPY_input_target) ||
424 InputArgs.hasArg(OBJCOPY_output_target)))
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000425 return createStringError(
426 errc::invalid_argument,
427 "--target cannot be used with --input-target or --output-target");
Jordan Rupprechtbb4588e2018-10-12 00:36:01 +0000428
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000429 bool UseRegex = InputArgs.hasArg(OBJCOPY_regex);
Jordan Rupprechtbb4588e2018-10-12 00:36:01 +0000430 if (InputArgs.hasArg(OBJCOPY_target)) {
431 Config.InputFormat = InputArgs.getLastArgValue(OBJCOPY_target);
432 Config.OutputFormat = InputArgs.getLastArgValue(OBJCOPY_target);
433 } else {
434 Config.InputFormat = InputArgs.getLastArgValue(OBJCOPY_input_target);
435 Config.OutputFormat = InputArgs.getLastArgValue(OBJCOPY_output_target);
436 }
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000437 if (Config.InputFormat == "binary") {
438 auto BinaryArch = InputArgs.getLastArgValue(OBJCOPY_binary_architecture);
439 if (BinaryArch.empty())
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000440 return createStringError(
441 errc::invalid_argument,
442 "Specified binary input without specifiying an architecture");
443 Expected<const MachineInfo &> MI = getMachineInfo(BinaryArch);
444 if (!MI)
445 return MI.takeError();
446 Config.BinaryArch = *MI;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000447 }
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000448 if (!Config.OutputFormat.empty() && Config.OutputFormat != "binary") {
Jordan Rupprechtb0b65ca2019-04-17 07:42:31 +0000449 Expected<MachineInfo> MI = getOutputFormatMachineInfo(Config.OutputFormat);
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000450 if (!MI)
451 return MI.takeError();
452 Config.OutputArch = *MI;
453 }
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000454
455 if (auto Arg = InputArgs.getLastArg(OBJCOPY_compress_debug_sections,
456 OBJCOPY_compress_debug_sections_eq)) {
457 Config.CompressionType = DebugCompressionType::Z;
458
459 if (Arg->getOption().getID() == OBJCOPY_compress_debug_sections_eq) {
460 Config.CompressionType =
461 StringSwitch<DebugCompressionType>(
462 InputArgs.getLastArgValue(OBJCOPY_compress_debug_sections_eq))
463 .Case("zlib-gnu", DebugCompressionType::GNU)
464 .Case("zlib", DebugCompressionType::Z)
465 .Default(DebugCompressionType::None);
466 if (Config.CompressionType == DebugCompressionType::None)
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000467 return createStringError(
468 errc::invalid_argument,
469 "Invalid or unsupported --compress-debug-sections format: %s",
470 InputArgs.getLastArgValue(OBJCOPY_compress_debug_sections_eq)
471 .str()
472 .c_str());
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000473 }
George Rimar1e930802019-03-05 11:32:14 +0000474 if (!zlib::isAvailable())
475 return createStringError(
476 errc::invalid_argument,
477 "LLVM was not compiled with LLVM_ENABLE_ZLIB: can not compress");
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000478 }
479
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000480 Config.AddGnuDebugLink = InputArgs.getLastArgValue(OBJCOPY_add_gnu_debuglink);
Jake Ehrlich8ad77792018-12-03 19:49:23 +0000481 Config.BuildIdLinkDir = InputArgs.getLastArgValue(OBJCOPY_build_id_link_dir);
482 if (InputArgs.hasArg(OBJCOPY_build_id_link_input))
483 Config.BuildIdLinkInput =
484 InputArgs.getLastArgValue(OBJCOPY_build_id_link_input);
485 if (InputArgs.hasArg(OBJCOPY_build_id_link_output))
486 Config.BuildIdLinkOutput =
487 InputArgs.getLastArgValue(OBJCOPY_build_id_link_output);
488 Config.SplitDWO = InputArgs.getLastArgValue(OBJCOPY_split_dwo);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000489 Config.SymbolsPrefix = InputArgs.getLastArgValue(OBJCOPY_prefix_symbols);
490
491 for (auto Arg : InputArgs.filtered(OBJCOPY_redefine_symbol)) {
492 if (!StringRef(Arg->getValue()).contains('='))
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000493 return createStringError(errc::invalid_argument,
494 "Bad format for --redefine-sym");
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000495 auto Old2New = StringRef(Arg->getValue()).split('=');
496 if (!Config.SymbolsToRename.insert(Old2New).second)
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000497 return createStringError(errc::invalid_argument,
498 "Multiple redefinition of symbol %s",
499 Old2New.first.str().c_str());
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000500 }
501
Eugene Leviant340cb872019-02-08 10:33:16 +0000502 for (auto Arg : InputArgs.filtered(OBJCOPY_redefine_symbols))
503 if (Error E = addSymbolsToRenameFromFile(Config.SymbolsToRename, DC.Alloc,
504 Arg->getValue()))
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000505 return std::move(E);
Eugene Leviant340cb872019-02-08 10:33:16 +0000506
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000507 for (auto Arg : InputArgs.filtered(OBJCOPY_rename_section)) {
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000508 Expected<SectionRename> SR =
509 parseRenameSectionValue(StringRef(Arg->getValue()));
510 if (!SR)
511 return SR.takeError();
512 if (!Config.SectionsToRename.try_emplace(SR->OriginalName, *SR).second)
513 return createStringError(errc::invalid_argument,
514 "Multiple renames of section %s",
515 SR->OriginalName.str().c_str());
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000516 }
Jordan Rupprechtc8927412019-01-29 15:05:38 +0000517 for (auto Arg : InputArgs.filtered(OBJCOPY_set_section_flags)) {
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000518 Expected<SectionFlagsUpdate> SFU =
519 parseSetSectionFlagValue(Arg->getValue());
520 if (!SFU)
521 return SFU.takeError();
522 if (!Config.SetSectionFlags.try_emplace(SFU->Name, *SFU).second)
523 return createStringError(
524 errc::invalid_argument,
525 "--set-section-flags set multiple times for section %s",
526 SFU->Name.str().c_str());
Jordan Rupprechtc8927412019-01-29 15:05:38 +0000527 }
528 // Prohibit combinations of --set-section-flags when the section name is used
529 // by --rename-section, either as a source or a destination.
530 for (const auto &E : Config.SectionsToRename) {
531 const SectionRename &SR = E.second;
532 if (Config.SetSectionFlags.count(SR.OriginalName))
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000533 return createStringError(
534 errc::invalid_argument,
535 "--set-section-flags=%s conflicts with --rename-section=%s=%s",
536 SR.OriginalName.str().c_str(), SR.OriginalName.str().c_str(),
537 SR.NewName.str().c_str());
Jordan Rupprechtc8927412019-01-29 15:05:38 +0000538 if (Config.SetSectionFlags.count(SR.NewName))
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000539 return createStringError(
540 errc::invalid_argument,
541 "--set-section-flags=%s conflicts with --rename-section=%s=%s",
542 SR.NewName.str().c_str(), SR.OriginalName.str().c_str(),
543 SR.NewName.str().c_str());
Jordan Rupprechtc8927412019-01-29 15:05:38 +0000544 }
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000545
546 for (auto Arg : InputArgs.filtered(OBJCOPY_remove_section))
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000547 Config.ToRemove.emplace_back(Arg->getValue(), UseRegex);
Jordan Rupprechtc5bae782018-11-13 19:32:27 +0000548 for (auto Arg : InputArgs.filtered(OBJCOPY_keep_section))
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000549 Config.KeepSection.emplace_back(Arg->getValue(), UseRegex);
Jake Ehrlich85985ed2018-12-06 02:03:53 +0000550 for (auto Arg : InputArgs.filtered(OBJCOPY_only_section))
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000551 Config.OnlySection.emplace_back(Arg->getValue(), UseRegex);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000552 for (auto Arg : InputArgs.filtered(OBJCOPY_add_section))
553 Config.AddSection.push_back(Arg->getValue());
554 for (auto Arg : InputArgs.filtered(OBJCOPY_dump_section))
555 Config.DumpSection.push_back(Arg->getValue());
556 Config.StripAll = InputArgs.hasArg(OBJCOPY_strip_all);
557 Config.StripAllGNU = InputArgs.hasArg(OBJCOPY_strip_all_gnu);
558 Config.StripDebug = InputArgs.hasArg(OBJCOPY_strip_debug);
559 Config.StripDWO = InputArgs.hasArg(OBJCOPY_strip_dwo);
560 Config.StripSections = InputArgs.hasArg(OBJCOPY_strip_sections);
561 Config.StripNonAlloc = InputArgs.hasArg(OBJCOPY_strip_non_alloc);
562 Config.StripUnneeded = InputArgs.hasArg(OBJCOPY_strip_unneeded);
563 Config.ExtractDWO = InputArgs.hasArg(OBJCOPY_extract_dwo);
564 Config.LocalizeHidden = InputArgs.hasArg(OBJCOPY_localize_hidden);
565 Config.Weaken = InputArgs.hasArg(OBJCOPY_weaken);
Jordan Rupprechtd0f7bcf2019-01-30 14:58:13 +0000566 if (InputArgs.hasArg(OBJCOPY_discard_all, OBJCOPY_discard_locals))
567 Config.DiscardMode =
568 InputArgs.hasFlag(OBJCOPY_discard_all, OBJCOPY_discard_locals)
569 ? DiscardType::All
570 : DiscardType::Locals;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000571 Config.OnlyKeepDebug = InputArgs.hasArg(OBJCOPY_only_keep_debug);
572 Config.KeepFileSymbols = InputArgs.hasArg(OBJCOPY_keep_file_symbols);
573 Config.DecompressDebugSections =
574 InputArgs.hasArg(OBJCOPY_decompress_debug_sections);
575 for (auto Arg : InputArgs.filtered(OBJCOPY_localize_symbol))
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000576 Config.SymbolsToLocalize.emplace_back(Arg->getValue(), UseRegex);
Eugene Leviante08fe352019-02-08 14:37:54 +0000577 for (auto Arg : InputArgs.filtered(OBJCOPY_localize_symbols))
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000578 if (Error E = addSymbolsFromFile(Config.SymbolsToLocalize, DC.Alloc,
579 Arg->getValue(), UseRegex))
580 return std::move(E);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000581 for (auto Arg : InputArgs.filtered(OBJCOPY_keep_global_symbol))
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000582 Config.SymbolsToKeepGlobal.emplace_back(Arg->getValue(), UseRegex);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000583 for (auto Arg : InputArgs.filtered(OBJCOPY_keep_global_symbols))
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000584 if (Error E = addSymbolsFromFile(Config.SymbolsToKeepGlobal, DC.Alloc,
585 Arg->getValue(), UseRegex))
586 return std::move(E);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000587 for (auto Arg : InputArgs.filtered(OBJCOPY_globalize_symbol))
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000588 Config.SymbolsToGlobalize.emplace_back(Arg->getValue(), UseRegex);
Eugene Leviante08fe352019-02-08 14:37:54 +0000589 for (auto Arg : InputArgs.filtered(OBJCOPY_globalize_symbols))
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000590 if (Error E = addSymbolsFromFile(Config.SymbolsToGlobalize, DC.Alloc,
591 Arg->getValue(), UseRegex))
592 return std::move(E);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000593 for (auto Arg : InputArgs.filtered(OBJCOPY_weaken_symbol))
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000594 Config.SymbolsToWeaken.emplace_back(Arg->getValue(), UseRegex);
Eugene Leviante08fe352019-02-08 14:37:54 +0000595 for (auto Arg : InputArgs.filtered(OBJCOPY_weaken_symbols))
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000596 if (Error E = addSymbolsFromFile(Config.SymbolsToWeaken, DC.Alloc,
597 Arg->getValue(), UseRegex))
598 return std::move(E);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000599 for (auto Arg : InputArgs.filtered(OBJCOPY_strip_symbol))
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000600 Config.SymbolsToRemove.emplace_back(Arg->getValue(), UseRegex);
Eugene Leviante08fe352019-02-08 14:37:54 +0000601 for (auto Arg : InputArgs.filtered(OBJCOPY_strip_symbols))
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000602 if (Error E = addSymbolsFromFile(Config.SymbolsToRemove, DC.Alloc,
603 Arg->getValue(), UseRegex))
604 return std::move(E);
Eugene Leviant2db10622019-02-13 07:34:54 +0000605 for (auto Arg : InputArgs.filtered(OBJCOPY_strip_unneeded_symbol))
606 Config.UnneededSymbolsToRemove.emplace_back(Arg->getValue(), UseRegex);
607 for (auto Arg : InputArgs.filtered(OBJCOPY_strip_unneeded_symbols))
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000608 if (Error E = addSymbolsFromFile(Config.UnneededSymbolsToRemove, DC.Alloc,
609 Arg->getValue(), UseRegex))
610 return std::move(E);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000611 for (auto Arg : InputArgs.filtered(OBJCOPY_keep_symbol))
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000612 Config.SymbolsToKeep.emplace_back(Arg->getValue(), UseRegex);
Yi Kongf2baddb2019-04-01 18:12:43 +0000613 for (auto Arg : InputArgs.filtered(OBJCOPY_keep_symbols))
614 if (Error E = addSymbolsFromFile(Config.SymbolsToKeep, DC.Alloc,
615 Arg->getValue(), UseRegex))
616 return std::move(E);
Jordan Rupprecht42bc1e22019-03-13 22:26:01 +0000617 for (auto Arg : InputArgs.filtered(OBJCOPY_add_symbol)) {
618 Expected<NewSymbolInfo> NSI = parseNewSymbolInfo(Arg->getValue());
619 if (!NSI)
620 return NSI.takeError();
621 Config.SymbolsToAdd.push_back(*NSI);
622 }
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000623
Jordan Rupprechtfc780bb2018-11-01 17:36:37 +0000624 Config.DeterministicArchives = InputArgs.hasFlag(
625 OBJCOPY_enable_deterministic_archives,
626 OBJCOPY_disable_deterministic_archives, /*default=*/true);
627
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000628 Config.PreserveDates = InputArgs.hasArg(OBJCOPY_preserve_dates);
629
Eugene Leviant53350d02019-02-26 09:24:22 +0000630 for (auto Arg : InputArgs)
631 if (Arg->getOption().matches(OBJCOPY_set_start)) {
632 auto EAddr = getAsInteger<uint64_t>(Arg->getValue());
633 if (!EAddr)
634 return createStringError(
635 EAddr.getError(), "bad entry point address: '%s'", Arg->getValue());
636
637 Config.EntryExpr = [EAddr](uint64_t) { return *EAddr; };
638 } else if (Arg->getOption().matches(OBJCOPY_change_start)) {
639 auto EIncr = getAsInteger<int64_t>(Arg->getValue());
640 if (!EIncr)
641 return createStringError(EIncr.getError(),
642 "bad entry point increment: '%s'",
643 Arg->getValue());
644 auto Expr = Config.EntryExpr ? std::move(Config.EntryExpr)
645 : [](uint64_t A) { return A; };
646 Config.EntryExpr = [Expr, EIncr](uint64_t EAddr) {
647 return Expr(EAddr) + *EIncr;
648 };
649 }
650
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000651 if (Config.DecompressDebugSections &&
652 Config.CompressionType != DebugCompressionType::None) {
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000653 return createStringError(
654 errc::invalid_argument,
655 "Cannot specify --compress-debug-sections at the same time as "
656 "--decompress-debug-sections at the same time");
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000657 }
658
659 if (Config.DecompressDebugSections && !zlib::isAvailable())
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000660 return createStringError(
661 errc::invalid_argument,
662 "LLVM was not compiled with LLVM_ENABLE_ZLIB: cannot decompress");
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000663
Jordan Rupprechtab9f6622018-10-23 20:54:51 +0000664 DC.CopyConfigs.push_back(std::move(Config));
Jordan Rupprecht93ad8b32019-02-21 17:24:55 +0000665 return std::move(DC);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000666}
667
668// ParseStripOptions returns the config and sets the input arguments. If a
669// help flag is set then ParseStripOptions will print the help messege and
670// exit.
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000671Expected<DriverConfig> parseStripOptions(ArrayRef<const char *> ArgsArr) {
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000672 StripOptTable T;
673 unsigned MissingArgumentIndex, MissingArgumentCount;
674 llvm::opt::InputArgList InputArgs =
675 T.ParseArgs(ArgsArr, MissingArgumentIndex, MissingArgumentCount);
676
677 if (InputArgs.size() == 0) {
678 T.PrintHelp(errs(), "llvm-strip [options] file...", "strip tool");
679 exit(1);
680 }
681
682 if (InputArgs.hasArg(STRIP_help)) {
683 T.PrintHelp(outs(), "llvm-strip [options] file...", "strip tool");
684 exit(0);
685 }
686
687 if (InputArgs.hasArg(STRIP_version)) {
Martin Storsjoe9af7152018-11-28 06:51:50 +0000688 outs() << "llvm-strip, compatible with GNU strip\n";
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000689 cl::PrintVersionMessage();
690 exit(0);
691 }
692
693 SmallVector<const char *, 2> Positional;
694 for (auto Arg : InputArgs.filtered(STRIP_UNKNOWN))
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000695 return createStringError(errc::invalid_argument, "unknown argument '%s'",
696 Arg->getAsString(InputArgs).c_str());
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000697 for (auto Arg : InputArgs.filtered(STRIP_INPUT))
698 Positional.push_back(Arg->getValue());
699
700 if (Positional.empty())
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000701 return createStringError(errc::invalid_argument, "No input file specified");
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000702
703 if (Positional.size() > 1 && InputArgs.hasArg(STRIP_output))
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000704 return createStringError(
705 errc::invalid_argument,
706 "Multiple input files cannot be used in combination with -o");
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000707
708 CopyConfig Config;
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000709 bool UseRegexp = InputArgs.hasArg(STRIP_regex);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000710 Config.StripDebug = InputArgs.hasArg(STRIP_strip_debug);
711
Jordan Rupprechtd0f7bcf2019-01-30 14:58:13 +0000712 if (InputArgs.hasArg(STRIP_discard_all, STRIP_discard_locals))
713 Config.DiscardMode =
714 InputArgs.hasFlag(STRIP_discard_all, STRIP_discard_locals)
715 ? DiscardType::All
716 : DiscardType::Locals;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000717 Config.StripUnneeded = InputArgs.hasArg(STRIP_strip_unneeded);
718 Config.StripAll = InputArgs.hasArg(STRIP_strip_all);
Jordan Rupprecht30d1b192018-11-01 17:48:46 +0000719 Config.StripAllGNU = InputArgs.hasArg(STRIP_strip_all_gnu);
Jordan Rupprecht12ed01d2019-03-14 21:51:42 +0000720 Config.OnlyKeepDebug = InputArgs.hasArg(STRIP_only_keep_debug);
Eugene Leviant05a3f992019-02-01 15:25:15 +0000721 Config.KeepFileSymbols = InputArgs.hasArg(STRIP_keep_file_symbols);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000722
Jordan Rupprechtc5bae782018-11-13 19:32:27 +0000723 for (auto Arg : InputArgs.filtered(STRIP_keep_section))
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000724 Config.KeepSection.emplace_back(Arg->getValue(), UseRegexp);
Jordan Rupprecht30d1b192018-11-01 17:48:46 +0000725
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000726 for (auto Arg : InputArgs.filtered(STRIP_remove_section))
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000727 Config.ToRemove.emplace_back(Arg->getValue(), UseRegexp);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000728
Eugene Leviant2267c582019-01-31 12:16:20 +0000729 for (auto Arg : InputArgs.filtered(STRIP_strip_symbol))
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000730 Config.SymbolsToRemove.emplace_back(Arg->getValue(), UseRegexp);
Eugene Leviant2267c582019-01-31 12:16:20 +0000731
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000732 for (auto Arg : InputArgs.filtered(STRIP_keep_symbol))
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000733 Config.SymbolsToKeep.emplace_back(Arg->getValue(), UseRegexp);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000734
Eugene Leviant2267c582019-01-31 12:16:20 +0000735 if (!Config.StripDebug && !Config.StripUnneeded &&
736 Config.DiscardMode == DiscardType::None && !Config.StripAllGNU && Config.SymbolsToRemove.empty())
737 Config.StripAll = true;
738
Jordan Rupprechtfc780bb2018-11-01 17:36:37 +0000739 Config.DeterministicArchives =
740 InputArgs.hasFlag(STRIP_enable_deterministic_archives,
741 STRIP_disable_deterministic_archives, /*default=*/true);
742
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000743 Config.PreserveDates = InputArgs.hasArg(STRIP_preserve_dates);
744
745 DriverConfig DC;
746 if (Positional.size() == 1) {
747 Config.InputFilename = Positional[0];
748 Config.OutputFilename =
749 InputArgs.getLastArgValue(STRIP_output, Positional[0]);
750 DC.CopyConfigs.push_back(std::move(Config));
751 } else {
752 for (const char *Filename : Positional) {
753 Config.InputFilename = Filename;
754 Config.OutputFilename = Filename;
755 DC.CopyConfigs.push_back(Config);
756 }
757 }
758
Jordan Rupprecht93ad8b32019-02-21 17:24:55 +0000759 return std::move(DC);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000760}
761
762} // namespace objcopy
763} // namespace llvm