blob: 47867826ec2038e5466010541c13fea4f0a9c985 [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
11#include "llvm/ADT/BitmaskEnum.h"
12#include "llvm/ADT/Optional.h"
13#include "llvm/ADT/SmallVector.h"
14#include "llvm/ADT/StringRef.h"
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +000015#include "llvm/Option/Arg.h"
16#include "llvm/Option/ArgList.h"
17#include "llvm/Support/CommandLine.h"
18#include "llvm/Support/Compression.h"
Eugene Leviant340cb872019-02-08 10:33:16 +000019#include "llvm/Support/Errc.h"
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +000020#include "llvm/Support/MemoryBuffer.h"
Jordan Rupprecht5745c5f2019-02-04 18:38:00 +000021#include "llvm/Support/StringSaver.h"
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +000022#include <memory>
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +000023
24namespace llvm {
25namespace objcopy {
26
27namespace {
28enum ObjcopyID {
29 OBJCOPY_INVALID = 0, // This is not an option ID.
30#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
31 HELPTEXT, METAVAR, VALUES) \
32 OBJCOPY_##ID,
33#include "ObjcopyOpts.inc"
34#undef OPTION
35};
36
37#define PREFIX(NAME, VALUE) const char *const OBJCOPY_##NAME[] = VALUE;
38#include "ObjcopyOpts.inc"
39#undef PREFIX
40
41static const opt::OptTable::Info ObjcopyInfoTable[] = {
42#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
43 HELPTEXT, METAVAR, VALUES) \
44 {OBJCOPY_##PREFIX, \
45 NAME, \
46 HELPTEXT, \
47 METAVAR, \
48 OBJCOPY_##ID, \
49 opt::Option::KIND##Class, \
50 PARAM, \
51 FLAGS, \
52 OBJCOPY_##GROUP, \
53 OBJCOPY_##ALIAS, \
54 ALIASARGS, \
55 VALUES},
56#include "ObjcopyOpts.inc"
57#undef OPTION
58};
59
60class ObjcopyOptTable : public opt::OptTable {
61public:
Jordan Rupprechtaaeaa0a2018-10-23 18:46:33 +000062 ObjcopyOptTable() : OptTable(ObjcopyInfoTable) {}
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +000063};
64
65enum StripID {
66 STRIP_INVALID = 0, // This is not an option ID.
67#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
68 HELPTEXT, METAVAR, VALUES) \
69 STRIP_##ID,
70#include "StripOpts.inc"
71#undef OPTION
72};
73
74#define PREFIX(NAME, VALUE) const char *const STRIP_##NAME[] = VALUE;
75#include "StripOpts.inc"
76#undef PREFIX
77
78static const opt::OptTable::Info StripInfoTable[] = {
79#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
80 HELPTEXT, METAVAR, VALUES) \
81 {STRIP_##PREFIX, NAME, HELPTEXT, \
82 METAVAR, STRIP_##ID, opt::Option::KIND##Class, \
83 PARAM, FLAGS, STRIP_##GROUP, \
84 STRIP_##ALIAS, ALIASARGS, VALUES},
85#include "StripOpts.inc"
86#undef OPTION
87};
88
89class StripOptTable : public opt::OptTable {
90public:
Jordan Rupprechtaaeaa0a2018-10-23 18:46:33 +000091 StripOptTable() : OptTable(StripInfoTable) {}
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +000092};
93
94enum SectionFlag {
95 SecNone = 0,
96 SecAlloc = 1 << 0,
97 SecLoad = 1 << 1,
98 SecNoload = 1 << 2,
99 SecReadonly = 1 << 3,
100 SecDebug = 1 << 4,
101 SecCode = 1 << 5,
102 SecData = 1 << 6,
103 SecRom = 1 << 7,
104 SecMerge = 1 << 8,
105 SecStrings = 1 << 9,
106 SecContents = 1 << 10,
107 SecShare = 1 << 11,
108 LLVM_MARK_AS_BITMASK_ENUM(/* LargestValue = */ SecShare)
109};
110
111} // namespace
112
113static SectionFlag parseSectionRenameFlag(StringRef SectionName) {
114 return llvm::StringSwitch<SectionFlag>(SectionName)
115 .Case("alloc", SectionFlag::SecAlloc)
116 .Case("load", SectionFlag::SecLoad)
117 .Case("noload", SectionFlag::SecNoload)
118 .Case("readonly", SectionFlag::SecReadonly)
119 .Case("debug", SectionFlag::SecDebug)
120 .Case("code", SectionFlag::SecCode)
121 .Case("data", SectionFlag::SecData)
122 .Case("rom", SectionFlag::SecRom)
123 .Case("merge", SectionFlag::SecMerge)
124 .Case("strings", SectionFlag::SecStrings)
125 .Case("contents", SectionFlag::SecContents)
126 .Case("share", SectionFlag::SecShare)
127 .Default(SectionFlag::SecNone);
128}
129
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000130static Expected<uint64_t>
131parseSectionFlagSet(ArrayRef<StringRef> SectionFlags) {
Jordan Rupprechtc8927412019-01-29 15:05:38 +0000132 SectionFlag ParsedFlags = SectionFlag::SecNone;
133 for (StringRef Flag : SectionFlags) {
134 SectionFlag ParsedFlag = parseSectionRenameFlag(Flag);
135 if (ParsedFlag == SectionFlag::SecNone)
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000136 return createStringError(
137 errc::invalid_argument,
138 "Unrecognized section flag '%s'. Flags supported for GNU "
139 "compatibility: alloc, load, noload, readonly, debug, code, data, "
140 "rom, share, contents, merge, strings",
141 Flag.str().c_str());
Jordan Rupprechtc8927412019-01-29 15:05:38 +0000142 ParsedFlags |= ParsedFlag;
143 }
144
145 uint64_t NewFlags = 0;
146 if (ParsedFlags & SectionFlag::SecAlloc)
147 NewFlags |= ELF::SHF_ALLOC;
148 if (!(ParsedFlags & SectionFlag::SecReadonly))
149 NewFlags |= ELF::SHF_WRITE;
150 if (ParsedFlags & SectionFlag::SecCode)
151 NewFlags |= ELF::SHF_EXECINSTR;
152 if (ParsedFlags & SectionFlag::SecMerge)
153 NewFlags |= ELF::SHF_MERGE;
154 if (ParsedFlags & SectionFlag::SecStrings)
155 NewFlags |= ELF::SHF_STRINGS;
156 return NewFlags;
157}
158
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000159static Expected<SectionRename> parseRenameSectionValue(StringRef FlagValue) {
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000160 if (!FlagValue.contains('='))
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000161 return createStringError(errc::invalid_argument,
162 "Bad format for --rename-section: missing '='");
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000163
164 // Initial split: ".foo" = ".bar,f1,f2,..."
165 auto Old2New = FlagValue.split('=');
166 SectionRename SR;
167 SR.OriginalName = Old2New.first;
168
169 // Flags split: ".bar" "f1" "f2" ...
170 SmallVector<StringRef, 6> NameAndFlags;
171 Old2New.second.split(NameAndFlags, ',');
172 SR.NewName = NameAndFlags[0];
173
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000174 if (NameAndFlags.size() > 1) {
175 Expected<uint64_t> ParsedFlagSet =
176 parseSectionFlagSet(makeArrayRef(NameAndFlags).drop_front());
177 if (!ParsedFlagSet)
178 return ParsedFlagSet.takeError();
179 SR.NewFlags = *ParsedFlagSet;
180 }
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000181
182 return SR;
183}
184
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000185static Expected<SectionFlagsUpdate>
186parseSetSectionFlagValue(StringRef FlagValue) {
Jordan Rupprechtc8927412019-01-29 15:05:38 +0000187 if (!StringRef(FlagValue).contains('='))
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000188 return createStringError(errc::invalid_argument,
189 "Bad format for --set-section-flags: missing '='");
Jordan Rupprechtc8927412019-01-29 15:05:38 +0000190
191 // Initial split: ".foo" = "f1,f2,..."
192 auto Section2Flags = StringRef(FlagValue).split('=');
193 SectionFlagsUpdate SFU;
194 SFU.Name = Section2Flags.first;
195
196 // Flags split: "f1" "f2" ...
197 SmallVector<StringRef, 6> SectionFlags;
198 Section2Flags.second.split(SectionFlags, ',');
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000199 Expected<uint64_t> ParsedFlagSet = parseSectionFlagSet(SectionFlags);
200 if (!ParsedFlagSet)
201 return ParsedFlagSet.takeError();
202 SFU.NewFlags = *ParsedFlagSet;
Jordan Rupprechtc8927412019-01-29 15:05:38 +0000203
204 return SFU;
205}
206
Jordan Rupprecht42bc1e22019-03-13 22:26:01 +0000207static Expected<NewSymbolInfo> parseNewSymbolInfo(StringRef FlagValue) {
Eugene Leviant51c1f642019-02-25 14:12:41 +0000208 // Parse value given with --add-symbol option and create the
209 // new symbol if possible. The value format for --add-symbol is:
210 //
211 // <name>=[<section>:]<value>[,<flags>]
212 //
213 // where:
214 // <name> - symbol name, can be empty string
215 // <section> - optional section name. If not given ABS symbol is created
216 // <value> - symbol value, can be decimal or hexadecimal number prefixed
217 // with 0x.
218 // <flags> - optional flags affecting symbol type, binding or visibility:
219 // The following are currently supported:
220 //
221 // global, local, weak, default, hidden, file, section, object,
222 // indirect-function.
223 //
224 // The following flags are ignored and provided for GNU
225 // compatibility only:
226 //
227 // warning, debug, constructor, indirect, synthetic,
228 // unique-object, before=<symbol>.
229 NewSymbolInfo SI;
230 StringRef Value;
231 std::tie(SI.SymbolName, Value) = FlagValue.split('=');
232 if (Value.empty())
Jordan Rupprecht42bc1e22019-03-13 22:26:01 +0000233 return createStringError(
234 errc::invalid_argument,
235 "bad format for --add-symbol, missing '=' after '%s'",
236 SI.SymbolName.str().c_str());
Eugene Leviant51c1f642019-02-25 14:12:41 +0000237
238 if (Value.contains(':')) {
239 std::tie(SI.SectionName, Value) = Value.split(':');
240 if (SI.SectionName.empty() || Value.empty())
Jordan Rupprecht42bc1e22019-03-13 22:26:01 +0000241 return createStringError(
242 errc::invalid_argument,
Eugene Leviant51c1f642019-02-25 14:12:41 +0000243 "bad format for --add-symbol, missing section name or symbol value");
244 }
245
246 SmallVector<StringRef, 6> Flags;
247 Value.split(Flags, ',');
248 if (Flags[0].getAsInteger(0, SI.Value))
Jordan Rupprecht42bc1e22019-03-13 22:26:01 +0000249 return createStringError(errc::invalid_argument, "bad symbol value: '%s'",
250 Flags[0].str().c_str());
Eugene Leviant51c1f642019-02-25 14:12:41 +0000251
Jordan Rupprecht42bc1e22019-03-13 22:26:01 +0000252 using Functor = std::function<void(void)>;
253 SmallVector<StringRef, 6> UnsupportedFlags;
254 for (size_t I = 1, NumFlags = Flags.size(); I < NumFlags; ++I)
Eugene Leviant51c1f642019-02-25 14:12:41 +0000255 static_cast<Functor>(
256 StringSwitch<Functor>(Flags[I])
257 .CaseLower("global", [&SI] { SI.Bind = ELF::STB_GLOBAL; })
258 .CaseLower("local", [&SI] { SI.Bind = ELF::STB_LOCAL; })
259 .CaseLower("weak", [&SI] { SI.Bind = ELF::STB_WEAK; })
260 .CaseLower("default", [&SI] { SI.Visibility = ELF::STV_DEFAULT; })
261 .CaseLower("hidden", [&SI] { SI.Visibility = ELF::STV_HIDDEN; })
262 .CaseLower("file", [&SI] { SI.Type = ELF::STT_FILE; })
263 .CaseLower("section", [&SI] { SI.Type = ELF::STT_SECTION; })
264 .CaseLower("object", [&SI] { SI.Type = ELF::STT_OBJECT; })
265 .CaseLower("function", [&SI] { SI.Type = ELF::STT_FUNC; })
266 .CaseLower("indirect-function",
267 [&SI] { SI.Type = ELF::STT_GNU_IFUNC; })
268 .CaseLower("debug", [] {})
269 .CaseLower("constructor", [] {})
270 .CaseLower("warning", [] {})
271 .CaseLower("indirect", [] {})
272 .CaseLower("synthetic", [] {})
273 .CaseLower("unique-object", [] {})
274 .StartsWithLower("before", [] {})
Jordan Rupprecht42bc1e22019-03-13 22:26:01 +0000275 .Default([&] { UnsupportedFlags.push_back(Flags[I]); }))();
276 if (!UnsupportedFlags.empty())
277 return createStringError(errc::invalid_argument,
278 "unsupported flag%s for --add-symbol: '%s'",
279 UnsupportedFlags.size() > 1 ? "s" : "",
280 join(UnsupportedFlags, "', '").c_str());
Eugene Leviant51c1f642019-02-25 14:12:41 +0000281 return SI;
282}
283
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000284static const StringMap<MachineInfo> ArchMap{
285 // Name, {EMachine, 64bit, LittleEndian}
286 {"aarch64", {ELF::EM_AARCH64, true, true}},
287 {"arm", {ELF::EM_ARM, false, true}},
288 {"i386", {ELF::EM_386, false, true}},
289 {"i386:x86-64", {ELF::EM_X86_64, true, true}},
290 {"powerpc:common64", {ELF::EM_PPC64, true, true}},
291 {"sparc", {ELF::EM_SPARC, false, true}},
292 {"x86-64", {ELF::EM_X86_64, true, true}},
293};
294
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000295static Expected<const MachineInfo &> getMachineInfo(StringRef Arch) {
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000296 auto Iter = ArchMap.find(Arch);
297 if (Iter == std::end(ArchMap))
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000298 return createStringError(errc::invalid_argument,
299 "Invalid architecture: '%s'", Arch.str().c_str());
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000300 return Iter->getValue();
301}
302
Jordan Rupprecht70038e02019-01-07 16:59:12 +0000303static const StringMap<MachineInfo> OutputFormatMap{
304 // Name, {EMachine, 64bit, LittleEndian}
305 {"elf32-i386", {ELF::EM_386, false, true}},
306 {"elf32-powerpcle", {ELF::EM_PPC, false, true}},
307 {"elf32-x86-64", {ELF::EM_X86_64, false, true}},
308 {"elf64-powerpcle", {ELF::EM_PPC64, true, true}},
309 {"elf64-x86-64", {ELF::EM_X86_64, true, true}},
310};
311
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000312static Expected<const MachineInfo &>
313getOutputFormatMachineInfo(StringRef Format) {
Jordan Rupprecht70038e02019-01-07 16:59:12 +0000314 auto Iter = OutputFormatMap.find(Format);
315 if (Iter == std::end(OutputFormatMap))
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000316 return createStringError(errc::invalid_argument,
317 "Invalid output format: '%s'",
318 Format.str().c_str());
Jordan Rupprecht70038e02019-01-07 16:59:12 +0000319 return Iter->getValue();
320}
321
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000322static Error addSymbolsFromFile(std::vector<NameOrRegex> &Symbols,
323 BumpPtrAllocator &Alloc, StringRef Filename,
324 bool UseRegex) {
Jordan Rupprecht5745c5f2019-02-04 18:38:00 +0000325 StringSaver Saver(Alloc);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000326 SmallVector<StringRef, 16> Lines;
327 auto BufOrErr = MemoryBuffer::getFile(Filename);
328 if (!BufOrErr)
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000329 return createFileError(Filename, BufOrErr.getError());
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000330
331 BufOrErr.get()->getBuffer().split(Lines, '\n');
332 for (StringRef Line : Lines) {
333 // Ignore everything after '#', trim whitespace, and only add the symbol if
334 // it's not empty.
335 auto TrimmedLine = Line.split('#').first.trim();
336 if (!TrimmedLine.empty())
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000337 Symbols.emplace_back(Saver.save(TrimmedLine), UseRegex);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000338 }
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000339
340 return Error::success();
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000341}
342
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000343NameOrRegex::NameOrRegex(StringRef Pattern, bool IsRegex) {
344 if (!IsRegex) {
345 Name = Pattern;
346 return;
347 }
348
349 SmallVector<char, 32> Data;
350 R = std::make_shared<Regex>(
351 ("^" + Pattern.ltrim('^').rtrim('$') + "$").toStringRef(Data));
352}
353
Eugene Leviant340cb872019-02-08 10:33:16 +0000354static Error addSymbolsToRenameFromFile(StringMap<StringRef> &SymbolsToRename,
355 BumpPtrAllocator &Alloc,
356 StringRef Filename) {
357 StringSaver Saver(Alloc);
358 SmallVector<StringRef, 16> Lines;
359 auto BufOrErr = MemoryBuffer::getFile(Filename);
360 if (!BufOrErr)
Eugene Leviant317f9e72019-02-11 09:49:37 +0000361 return createFileError(Filename, BufOrErr.getError());
Eugene Leviant340cb872019-02-08 10:33:16 +0000362
363 BufOrErr.get()->getBuffer().split(Lines, '\n');
364 size_t NumLines = Lines.size();
365 for (size_t LineNo = 0; LineNo < NumLines; ++LineNo) {
366 StringRef TrimmedLine = Lines[LineNo].split('#').first.trim();
367 if (TrimmedLine.empty())
368 continue;
369
370 std::pair<StringRef, StringRef> Pair = Saver.save(TrimmedLine).split(' ');
371 StringRef NewName = Pair.second.trim();
372 if (NewName.empty())
373 return createStringError(errc::invalid_argument,
374 "%s:%zu: missing new symbol name",
375 Filename.str().c_str(), LineNo + 1);
376 SymbolsToRename.insert({Pair.first, NewName});
377 }
378 return Error::success();
379}
Eugene Leviant53350d02019-02-26 09:24:22 +0000380
381template <class T> static ErrorOr<T> getAsInteger(StringRef Val) {
382 T Result;
383 if (Val.getAsInteger(0, Result))
384 return errc::invalid_argument;
385 return Result;
386}
387
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000388// ParseObjcopyOptions returns the config and sets the input arguments. If a
389// help flag is set then ParseObjcopyOptions will print the help messege and
390// exit.
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000391Expected<DriverConfig> parseObjcopyOptions(ArrayRef<const char *> ArgsArr) {
Jordan Rupprecht5745c5f2019-02-04 18:38:00 +0000392 DriverConfig DC;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000393 ObjcopyOptTable T;
394 unsigned MissingArgumentIndex, MissingArgumentCount;
395 llvm::opt::InputArgList InputArgs =
396 T.ParseArgs(ArgsArr, MissingArgumentIndex, MissingArgumentCount);
397
398 if (InputArgs.size() == 0) {
399 T.PrintHelp(errs(), "llvm-objcopy input [output]", "objcopy tool");
400 exit(1);
401 }
402
403 if (InputArgs.hasArg(OBJCOPY_help)) {
404 T.PrintHelp(outs(), "llvm-objcopy input [output]", "objcopy tool");
405 exit(0);
406 }
407
408 if (InputArgs.hasArg(OBJCOPY_version)) {
Martin Storsjoe9af7152018-11-28 06:51:50 +0000409 outs() << "llvm-objcopy, compatible with GNU objcopy\n";
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000410 cl::PrintVersionMessage();
411 exit(0);
412 }
413
414 SmallVector<const char *, 2> Positional;
415
416 for (auto Arg : InputArgs.filtered(OBJCOPY_UNKNOWN))
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000417 return createStringError(errc::invalid_argument, "unknown argument '%s'",
418 Arg->getAsString(InputArgs).c_str());
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000419
420 for (auto Arg : InputArgs.filtered(OBJCOPY_INPUT))
421 Positional.push_back(Arg->getValue());
422
423 if (Positional.empty())
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000424 return createStringError(errc::invalid_argument, "No input file specified");
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000425
426 if (Positional.size() > 2)
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000427 return createStringError(errc::invalid_argument,
428 "Too many positional arguments");
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000429
430 CopyConfig Config;
431 Config.InputFilename = Positional[0];
432 Config.OutputFilename = Positional[Positional.size() == 1 ? 0 : 1];
Jordan Rupprechtbb4588e2018-10-12 00:36:01 +0000433 if (InputArgs.hasArg(OBJCOPY_target) &&
434 (InputArgs.hasArg(OBJCOPY_input_target) ||
435 InputArgs.hasArg(OBJCOPY_output_target)))
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000436 return createStringError(
437 errc::invalid_argument,
438 "--target cannot be used with --input-target or --output-target");
Jordan Rupprechtbb4588e2018-10-12 00:36:01 +0000439
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000440 bool UseRegex = InputArgs.hasArg(OBJCOPY_regex);
Jordan Rupprechtbb4588e2018-10-12 00:36:01 +0000441 if (InputArgs.hasArg(OBJCOPY_target)) {
442 Config.InputFormat = InputArgs.getLastArgValue(OBJCOPY_target);
443 Config.OutputFormat = InputArgs.getLastArgValue(OBJCOPY_target);
444 } else {
445 Config.InputFormat = InputArgs.getLastArgValue(OBJCOPY_input_target);
446 Config.OutputFormat = InputArgs.getLastArgValue(OBJCOPY_output_target);
447 }
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000448 if (Config.InputFormat == "binary") {
449 auto BinaryArch = InputArgs.getLastArgValue(OBJCOPY_binary_architecture);
450 if (BinaryArch.empty())
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000451 return createStringError(
452 errc::invalid_argument,
453 "Specified binary input without specifiying an architecture");
454 Expected<const MachineInfo &> MI = getMachineInfo(BinaryArch);
455 if (!MI)
456 return MI.takeError();
457 Config.BinaryArch = *MI;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000458 }
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000459 if (!Config.OutputFormat.empty() && Config.OutputFormat != "binary") {
460 Expected<const MachineInfo &> MI =
461 getOutputFormatMachineInfo(Config.OutputFormat);
462 if (!MI)
463 return MI.takeError();
464 Config.OutputArch = *MI;
465 }
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000466
467 if (auto Arg = InputArgs.getLastArg(OBJCOPY_compress_debug_sections,
468 OBJCOPY_compress_debug_sections_eq)) {
469 Config.CompressionType = DebugCompressionType::Z;
470
471 if (Arg->getOption().getID() == OBJCOPY_compress_debug_sections_eq) {
472 Config.CompressionType =
473 StringSwitch<DebugCompressionType>(
474 InputArgs.getLastArgValue(OBJCOPY_compress_debug_sections_eq))
475 .Case("zlib-gnu", DebugCompressionType::GNU)
476 .Case("zlib", DebugCompressionType::Z)
477 .Default(DebugCompressionType::None);
478 if (Config.CompressionType == DebugCompressionType::None)
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000479 return createStringError(
480 errc::invalid_argument,
481 "Invalid or unsupported --compress-debug-sections format: %s",
482 InputArgs.getLastArgValue(OBJCOPY_compress_debug_sections_eq)
483 .str()
484 .c_str());
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000485 }
George Rimar1e930802019-03-05 11:32:14 +0000486 if (!zlib::isAvailable())
487 return createStringError(
488 errc::invalid_argument,
489 "LLVM was not compiled with LLVM_ENABLE_ZLIB: can not compress");
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000490 }
491
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000492 Config.AddGnuDebugLink = InputArgs.getLastArgValue(OBJCOPY_add_gnu_debuglink);
Jake Ehrlich8ad77792018-12-03 19:49:23 +0000493 Config.BuildIdLinkDir = InputArgs.getLastArgValue(OBJCOPY_build_id_link_dir);
494 if (InputArgs.hasArg(OBJCOPY_build_id_link_input))
495 Config.BuildIdLinkInput =
496 InputArgs.getLastArgValue(OBJCOPY_build_id_link_input);
497 if (InputArgs.hasArg(OBJCOPY_build_id_link_output))
498 Config.BuildIdLinkOutput =
499 InputArgs.getLastArgValue(OBJCOPY_build_id_link_output);
500 Config.SplitDWO = InputArgs.getLastArgValue(OBJCOPY_split_dwo);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000501 Config.SymbolsPrefix = InputArgs.getLastArgValue(OBJCOPY_prefix_symbols);
502
503 for (auto Arg : InputArgs.filtered(OBJCOPY_redefine_symbol)) {
504 if (!StringRef(Arg->getValue()).contains('='))
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000505 return createStringError(errc::invalid_argument,
506 "Bad format for --redefine-sym");
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000507 auto Old2New = StringRef(Arg->getValue()).split('=');
508 if (!Config.SymbolsToRename.insert(Old2New).second)
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000509 return createStringError(errc::invalid_argument,
510 "Multiple redefinition of symbol %s",
511 Old2New.first.str().c_str());
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000512 }
513
Eugene Leviant340cb872019-02-08 10:33:16 +0000514 for (auto Arg : InputArgs.filtered(OBJCOPY_redefine_symbols))
515 if (Error E = addSymbolsToRenameFromFile(Config.SymbolsToRename, DC.Alloc,
516 Arg->getValue()))
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000517 return std::move(E);
Eugene Leviant340cb872019-02-08 10:33:16 +0000518
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000519 for (auto Arg : InputArgs.filtered(OBJCOPY_rename_section)) {
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000520 Expected<SectionRename> SR =
521 parseRenameSectionValue(StringRef(Arg->getValue()));
522 if (!SR)
523 return SR.takeError();
524 if (!Config.SectionsToRename.try_emplace(SR->OriginalName, *SR).second)
525 return createStringError(errc::invalid_argument,
526 "Multiple renames of section %s",
527 SR->OriginalName.str().c_str());
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000528 }
Jordan Rupprechtc8927412019-01-29 15:05:38 +0000529 for (auto Arg : InputArgs.filtered(OBJCOPY_set_section_flags)) {
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000530 Expected<SectionFlagsUpdate> SFU =
531 parseSetSectionFlagValue(Arg->getValue());
532 if (!SFU)
533 return SFU.takeError();
534 if (!Config.SetSectionFlags.try_emplace(SFU->Name, *SFU).second)
535 return createStringError(
536 errc::invalid_argument,
537 "--set-section-flags set multiple times for section %s",
538 SFU->Name.str().c_str());
Jordan Rupprechtc8927412019-01-29 15:05:38 +0000539 }
540 // Prohibit combinations of --set-section-flags when the section name is used
541 // by --rename-section, either as a source or a destination.
542 for (const auto &E : Config.SectionsToRename) {
543 const SectionRename &SR = E.second;
544 if (Config.SetSectionFlags.count(SR.OriginalName))
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000545 return createStringError(
546 errc::invalid_argument,
547 "--set-section-flags=%s conflicts with --rename-section=%s=%s",
548 SR.OriginalName.str().c_str(), SR.OriginalName.str().c_str(),
549 SR.NewName.str().c_str());
Jordan Rupprechtc8927412019-01-29 15:05:38 +0000550 if (Config.SetSectionFlags.count(SR.NewName))
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000551 return createStringError(
552 errc::invalid_argument,
553 "--set-section-flags=%s conflicts with --rename-section=%s=%s",
554 SR.NewName.str().c_str(), SR.OriginalName.str().c_str(),
555 SR.NewName.str().c_str());
Jordan Rupprechtc8927412019-01-29 15:05:38 +0000556 }
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000557
558 for (auto Arg : InputArgs.filtered(OBJCOPY_remove_section))
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000559 Config.ToRemove.emplace_back(Arg->getValue(), UseRegex);
Jordan Rupprechtc5bae782018-11-13 19:32:27 +0000560 for (auto Arg : InputArgs.filtered(OBJCOPY_keep_section))
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000561 Config.KeepSection.emplace_back(Arg->getValue(), UseRegex);
Jake Ehrlich85985ed2018-12-06 02:03:53 +0000562 for (auto Arg : InputArgs.filtered(OBJCOPY_only_section))
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000563 Config.OnlySection.emplace_back(Arg->getValue(), UseRegex);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000564 for (auto Arg : InputArgs.filtered(OBJCOPY_add_section))
565 Config.AddSection.push_back(Arg->getValue());
566 for (auto Arg : InputArgs.filtered(OBJCOPY_dump_section))
567 Config.DumpSection.push_back(Arg->getValue());
568 Config.StripAll = InputArgs.hasArg(OBJCOPY_strip_all);
569 Config.StripAllGNU = InputArgs.hasArg(OBJCOPY_strip_all_gnu);
570 Config.StripDebug = InputArgs.hasArg(OBJCOPY_strip_debug);
571 Config.StripDWO = InputArgs.hasArg(OBJCOPY_strip_dwo);
572 Config.StripSections = InputArgs.hasArg(OBJCOPY_strip_sections);
573 Config.StripNonAlloc = InputArgs.hasArg(OBJCOPY_strip_non_alloc);
574 Config.StripUnneeded = InputArgs.hasArg(OBJCOPY_strip_unneeded);
575 Config.ExtractDWO = InputArgs.hasArg(OBJCOPY_extract_dwo);
576 Config.LocalizeHidden = InputArgs.hasArg(OBJCOPY_localize_hidden);
577 Config.Weaken = InputArgs.hasArg(OBJCOPY_weaken);
Jordan Rupprechtd0f7bcf2019-01-30 14:58:13 +0000578 if (InputArgs.hasArg(OBJCOPY_discard_all, OBJCOPY_discard_locals))
579 Config.DiscardMode =
580 InputArgs.hasFlag(OBJCOPY_discard_all, OBJCOPY_discard_locals)
581 ? DiscardType::All
582 : DiscardType::Locals;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000583 Config.OnlyKeepDebug = InputArgs.hasArg(OBJCOPY_only_keep_debug);
584 Config.KeepFileSymbols = InputArgs.hasArg(OBJCOPY_keep_file_symbols);
585 Config.DecompressDebugSections =
586 InputArgs.hasArg(OBJCOPY_decompress_debug_sections);
587 for (auto Arg : InputArgs.filtered(OBJCOPY_localize_symbol))
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000588 Config.SymbolsToLocalize.emplace_back(Arg->getValue(), UseRegex);
Eugene Leviante08fe352019-02-08 14:37:54 +0000589 for (auto Arg : InputArgs.filtered(OBJCOPY_localize_symbols))
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000590 if (Error E = addSymbolsFromFile(Config.SymbolsToLocalize, 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_keep_global_symbol))
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000594 Config.SymbolsToKeepGlobal.emplace_back(Arg->getValue(), UseRegex);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000595 for (auto Arg : InputArgs.filtered(OBJCOPY_keep_global_symbols))
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000596 if (Error E = addSymbolsFromFile(Config.SymbolsToKeepGlobal, 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_globalize_symbol))
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000600 Config.SymbolsToGlobalize.emplace_back(Arg->getValue(), UseRegex);
Eugene Leviante08fe352019-02-08 14:37:54 +0000601 for (auto Arg : InputArgs.filtered(OBJCOPY_globalize_symbols))
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000602 if (Error E = addSymbolsFromFile(Config.SymbolsToGlobalize, DC.Alloc,
603 Arg->getValue(), UseRegex))
604 return std::move(E);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000605 for (auto Arg : InputArgs.filtered(OBJCOPY_weaken_symbol))
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000606 Config.SymbolsToWeaken.emplace_back(Arg->getValue(), UseRegex);
Eugene Leviante08fe352019-02-08 14:37:54 +0000607 for (auto Arg : InputArgs.filtered(OBJCOPY_weaken_symbols))
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000608 if (Error E = addSymbolsFromFile(Config.SymbolsToWeaken, 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_strip_symbol))
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000612 Config.SymbolsToRemove.emplace_back(Arg->getValue(), UseRegex);
Eugene Leviante08fe352019-02-08 14:37:54 +0000613 for (auto Arg : InputArgs.filtered(OBJCOPY_strip_symbols))
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000614 if (Error E = addSymbolsFromFile(Config.SymbolsToRemove, DC.Alloc,
615 Arg->getValue(), UseRegex))
616 return std::move(E);
Eugene Leviant2db10622019-02-13 07:34:54 +0000617 for (auto Arg : InputArgs.filtered(OBJCOPY_strip_unneeded_symbol))
618 Config.UnneededSymbolsToRemove.emplace_back(Arg->getValue(), UseRegex);
619 for (auto Arg : InputArgs.filtered(OBJCOPY_strip_unneeded_symbols))
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000620 if (Error E = addSymbolsFromFile(Config.UnneededSymbolsToRemove, DC.Alloc,
621 Arg->getValue(), UseRegex))
622 return std::move(E);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000623 for (auto Arg : InputArgs.filtered(OBJCOPY_keep_symbol))
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000624 Config.SymbolsToKeep.emplace_back(Arg->getValue(), UseRegex);
Jordan Rupprecht42bc1e22019-03-13 22:26:01 +0000625 for (auto Arg : InputArgs.filtered(OBJCOPY_add_symbol)) {
626 Expected<NewSymbolInfo> NSI = parseNewSymbolInfo(Arg->getValue());
627 if (!NSI)
628 return NSI.takeError();
629 Config.SymbolsToAdd.push_back(*NSI);
630 }
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000631
Jordan Rupprechtfc780bb2018-11-01 17:36:37 +0000632 Config.DeterministicArchives = InputArgs.hasFlag(
633 OBJCOPY_enable_deterministic_archives,
634 OBJCOPY_disable_deterministic_archives, /*default=*/true);
635
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000636 Config.PreserveDates = InputArgs.hasArg(OBJCOPY_preserve_dates);
637
Eugene Leviant53350d02019-02-26 09:24:22 +0000638 for (auto Arg : InputArgs)
639 if (Arg->getOption().matches(OBJCOPY_set_start)) {
640 auto EAddr = getAsInteger<uint64_t>(Arg->getValue());
641 if (!EAddr)
642 return createStringError(
643 EAddr.getError(), "bad entry point address: '%s'", Arg->getValue());
644
645 Config.EntryExpr = [EAddr](uint64_t) { return *EAddr; };
646 } else if (Arg->getOption().matches(OBJCOPY_change_start)) {
647 auto EIncr = getAsInteger<int64_t>(Arg->getValue());
648 if (!EIncr)
649 return createStringError(EIncr.getError(),
650 "bad entry point increment: '%s'",
651 Arg->getValue());
652 auto Expr = Config.EntryExpr ? std::move(Config.EntryExpr)
653 : [](uint64_t A) { return A; };
654 Config.EntryExpr = [Expr, EIncr](uint64_t EAddr) {
655 return Expr(EAddr) + *EIncr;
656 };
657 }
658
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000659 if (Config.DecompressDebugSections &&
660 Config.CompressionType != DebugCompressionType::None) {
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000661 return createStringError(
662 errc::invalid_argument,
663 "Cannot specify --compress-debug-sections at the same time as "
664 "--decompress-debug-sections at the same time");
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000665 }
666
667 if (Config.DecompressDebugSections && !zlib::isAvailable())
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000668 return createStringError(
669 errc::invalid_argument,
670 "LLVM was not compiled with LLVM_ENABLE_ZLIB: cannot decompress");
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000671
Jordan Rupprechtab9f6622018-10-23 20:54:51 +0000672 DC.CopyConfigs.push_back(std::move(Config));
Jordan Rupprecht93ad8b32019-02-21 17:24:55 +0000673 return std::move(DC);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000674}
675
676// ParseStripOptions returns the config and sets the input arguments. If a
677// help flag is set then ParseStripOptions will print the help messege and
678// exit.
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000679Expected<DriverConfig> parseStripOptions(ArrayRef<const char *> ArgsArr) {
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000680 StripOptTable T;
681 unsigned MissingArgumentIndex, MissingArgumentCount;
682 llvm::opt::InputArgList InputArgs =
683 T.ParseArgs(ArgsArr, MissingArgumentIndex, MissingArgumentCount);
684
685 if (InputArgs.size() == 0) {
686 T.PrintHelp(errs(), "llvm-strip [options] file...", "strip tool");
687 exit(1);
688 }
689
690 if (InputArgs.hasArg(STRIP_help)) {
691 T.PrintHelp(outs(), "llvm-strip [options] file...", "strip tool");
692 exit(0);
693 }
694
695 if (InputArgs.hasArg(STRIP_version)) {
Martin Storsjoe9af7152018-11-28 06:51:50 +0000696 outs() << "llvm-strip, compatible with GNU strip\n";
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000697 cl::PrintVersionMessage();
698 exit(0);
699 }
700
701 SmallVector<const char *, 2> Positional;
702 for (auto Arg : InputArgs.filtered(STRIP_UNKNOWN))
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000703 return createStringError(errc::invalid_argument, "unknown argument '%s'",
704 Arg->getAsString(InputArgs).c_str());
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000705 for (auto Arg : InputArgs.filtered(STRIP_INPUT))
706 Positional.push_back(Arg->getValue());
707
708 if (Positional.empty())
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000709 return createStringError(errc::invalid_argument, "No input file specified");
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000710
711 if (Positional.size() > 1 && InputArgs.hasArg(STRIP_output))
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000712 return createStringError(
713 errc::invalid_argument,
714 "Multiple input files cannot be used in combination with -o");
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000715
716 CopyConfig Config;
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000717 bool UseRegexp = InputArgs.hasArg(STRIP_regex);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000718 Config.StripDebug = InputArgs.hasArg(STRIP_strip_debug);
719
Jordan Rupprechtd0f7bcf2019-01-30 14:58:13 +0000720 if (InputArgs.hasArg(STRIP_discard_all, STRIP_discard_locals))
721 Config.DiscardMode =
722 InputArgs.hasFlag(STRIP_discard_all, STRIP_discard_locals)
723 ? DiscardType::All
724 : DiscardType::Locals;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000725 Config.StripUnneeded = InputArgs.hasArg(STRIP_strip_unneeded);
726 Config.StripAll = InputArgs.hasArg(STRIP_strip_all);
Jordan Rupprecht30d1b192018-11-01 17:48:46 +0000727 Config.StripAllGNU = InputArgs.hasArg(STRIP_strip_all_gnu);
Eugene Leviant05a3f992019-02-01 15:25:15 +0000728 Config.KeepFileSymbols = InputArgs.hasArg(STRIP_keep_file_symbols);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000729
Jordan Rupprechtc5bae782018-11-13 19:32:27 +0000730 for (auto Arg : InputArgs.filtered(STRIP_keep_section))
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000731 Config.KeepSection.emplace_back(Arg->getValue(), UseRegexp);
Jordan Rupprecht30d1b192018-11-01 17:48:46 +0000732
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000733 for (auto Arg : InputArgs.filtered(STRIP_remove_section))
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000734 Config.ToRemove.emplace_back(Arg->getValue(), UseRegexp);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000735
Eugene Leviant2267c582019-01-31 12:16:20 +0000736 for (auto Arg : InputArgs.filtered(STRIP_strip_symbol))
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000737 Config.SymbolsToRemove.emplace_back(Arg->getValue(), UseRegexp);
Eugene Leviant2267c582019-01-31 12:16:20 +0000738
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000739 for (auto Arg : InputArgs.filtered(STRIP_keep_symbol))
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000740 Config.SymbolsToKeep.emplace_back(Arg->getValue(), UseRegexp);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000741
Eugene Leviant2267c582019-01-31 12:16:20 +0000742 if (!Config.StripDebug && !Config.StripUnneeded &&
743 Config.DiscardMode == DiscardType::None && !Config.StripAllGNU && Config.SymbolsToRemove.empty())
744 Config.StripAll = true;
745
Jordan Rupprechtfc780bb2018-11-01 17:36:37 +0000746 Config.DeterministicArchives =
747 InputArgs.hasFlag(STRIP_enable_deterministic_archives,
748 STRIP_disable_deterministic_archives, /*default=*/true);
749
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000750 Config.PreserveDates = InputArgs.hasArg(STRIP_preserve_dates);
751
752 DriverConfig DC;
753 if (Positional.size() == 1) {
754 Config.InputFilename = Positional[0];
755 Config.OutputFilename =
756 InputArgs.getLastArgValue(STRIP_output, Positional[0]);
757 DC.CopyConfigs.push_back(std::move(Config));
758 } else {
759 for (const char *Filename : Positional) {
760 Config.InputFilename = Filename;
761 Config.OutputFilename = Filename;
762 DC.CopyConfigs.push_back(Config);
763 }
764 }
765
Jordan Rupprecht93ad8b32019-02-21 17:24:55 +0000766 return std::move(DC);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000767}
768
769} // namespace objcopy
770} // namespace llvm