blob: bc2b384f73c125e4b78b20bd951e4063d476bced [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{
James Hendersonc040d5d2019-03-22 10:21:09 +0000285 // Name, {EMachine, OS/ABI, 64bit, LittleEndian}
286 {"aarch64", {ELF::EM_AARCH64, ELF::ELFOSABI_NONE, true, true}},
287 {"arm", {ELF::EM_ARM, ELF::ELFOSABI_NONE, false, true}},
288 {"i386", {ELF::EM_386, ELF::ELFOSABI_NONE, false, true}},
289 {"i386:x86-64", {ELF::EM_X86_64, ELF::ELFOSABI_NONE, true, true}},
290 {"powerpc:common64", {ELF::EM_PPC64, ELF::ELFOSABI_NONE, true, true}},
291 {"sparc", {ELF::EM_SPARC, ELF::ELFOSABI_NONE, false, true}},
292 {"x86-64", {ELF::EM_X86_64, ELF::ELFOSABI_NONE, true, true}},
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000293};
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{
James Hendersonc040d5d2019-03-22 10:21:09 +0000304 // Name, {EMachine, OSABI, 64bit, LittleEndian}
305 {"elf32-i386", {ELF::EM_386, ELF::ELFOSABI_NONE, false, true}},
306 {"elf32-i386-freebsd", {ELF::EM_386, ELF::ELFOSABI_FREEBSD, false, true}},
307 {"elf32-powerpcle", {ELF::EM_PPC, ELF::ELFOSABI_NONE, false, true}},
308 {"elf32-x86-64", {ELF::EM_X86_64, ELF::ELFOSABI_NONE, false, true}},
309 {"elf64-powerpcle", {ELF::EM_PPC64, ELF::ELFOSABI_NONE, true, true}},
310 {"elf64-x86-64", {ELF::EM_X86_64, ELF::ELFOSABI_NONE, true, true}},
311 {"elf64-x86-64-freebsd",
312 {ELF::EM_X86_64, ELF::ELFOSABI_FREEBSD, true, true}},
Jordan Rupprecht70038e02019-01-07 16:59:12 +0000313};
314
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000315static Expected<const MachineInfo &>
316getOutputFormatMachineInfo(StringRef Format) {
Jordan Rupprecht70038e02019-01-07 16:59:12 +0000317 auto Iter = OutputFormatMap.find(Format);
318 if (Iter == std::end(OutputFormatMap))
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000319 return createStringError(errc::invalid_argument,
320 "Invalid output format: '%s'",
321 Format.str().c_str());
Jordan Rupprecht70038e02019-01-07 16:59:12 +0000322 return Iter->getValue();
323}
324
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000325static Error addSymbolsFromFile(std::vector<NameOrRegex> &Symbols,
326 BumpPtrAllocator &Alloc, StringRef Filename,
327 bool UseRegex) {
Jordan Rupprecht5745c5f2019-02-04 18:38:00 +0000328 StringSaver Saver(Alloc);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000329 SmallVector<StringRef, 16> Lines;
330 auto BufOrErr = MemoryBuffer::getFile(Filename);
331 if (!BufOrErr)
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000332 return createFileError(Filename, BufOrErr.getError());
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000333
334 BufOrErr.get()->getBuffer().split(Lines, '\n');
335 for (StringRef Line : Lines) {
336 // Ignore everything after '#', trim whitespace, and only add the symbol if
337 // it's not empty.
338 auto TrimmedLine = Line.split('#').first.trim();
339 if (!TrimmedLine.empty())
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000340 Symbols.emplace_back(Saver.save(TrimmedLine), UseRegex);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000341 }
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000342
343 return Error::success();
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000344}
345
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000346NameOrRegex::NameOrRegex(StringRef Pattern, bool IsRegex) {
347 if (!IsRegex) {
348 Name = Pattern;
349 return;
350 }
351
352 SmallVector<char, 32> Data;
353 R = std::make_shared<Regex>(
354 ("^" + Pattern.ltrim('^').rtrim('$') + "$").toStringRef(Data));
355}
356
Eugene Leviant340cb872019-02-08 10:33:16 +0000357static Error addSymbolsToRenameFromFile(StringMap<StringRef> &SymbolsToRename,
358 BumpPtrAllocator &Alloc,
359 StringRef Filename) {
360 StringSaver Saver(Alloc);
361 SmallVector<StringRef, 16> Lines;
362 auto BufOrErr = MemoryBuffer::getFile(Filename);
363 if (!BufOrErr)
Eugene Leviant317f9e72019-02-11 09:49:37 +0000364 return createFileError(Filename, BufOrErr.getError());
Eugene Leviant340cb872019-02-08 10:33:16 +0000365
366 BufOrErr.get()->getBuffer().split(Lines, '\n');
367 size_t NumLines = Lines.size();
368 for (size_t LineNo = 0; LineNo < NumLines; ++LineNo) {
369 StringRef TrimmedLine = Lines[LineNo].split('#').first.trim();
370 if (TrimmedLine.empty())
371 continue;
372
373 std::pair<StringRef, StringRef> Pair = Saver.save(TrimmedLine).split(' ');
374 StringRef NewName = Pair.second.trim();
375 if (NewName.empty())
376 return createStringError(errc::invalid_argument,
377 "%s:%zu: missing new symbol name",
378 Filename.str().c_str(), LineNo + 1);
379 SymbolsToRename.insert({Pair.first, NewName});
380 }
381 return Error::success();
382}
Eugene Leviant53350d02019-02-26 09:24:22 +0000383
384template <class T> static ErrorOr<T> getAsInteger(StringRef Val) {
385 T Result;
386 if (Val.getAsInteger(0, Result))
387 return errc::invalid_argument;
388 return Result;
389}
390
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000391// ParseObjcopyOptions returns the config and sets the input arguments. If a
392// help flag is set then ParseObjcopyOptions will print the help messege and
393// exit.
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000394Expected<DriverConfig> parseObjcopyOptions(ArrayRef<const char *> ArgsArr) {
Jordan Rupprecht5745c5f2019-02-04 18:38:00 +0000395 DriverConfig DC;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000396 ObjcopyOptTable T;
397 unsigned MissingArgumentIndex, MissingArgumentCount;
398 llvm::opt::InputArgList InputArgs =
399 T.ParseArgs(ArgsArr, MissingArgumentIndex, MissingArgumentCount);
400
401 if (InputArgs.size() == 0) {
402 T.PrintHelp(errs(), "llvm-objcopy input [output]", "objcopy tool");
403 exit(1);
404 }
405
406 if (InputArgs.hasArg(OBJCOPY_help)) {
407 T.PrintHelp(outs(), "llvm-objcopy input [output]", "objcopy tool");
408 exit(0);
409 }
410
411 if (InputArgs.hasArg(OBJCOPY_version)) {
Martin Storsjoe9af7152018-11-28 06:51:50 +0000412 outs() << "llvm-objcopy, compatible with GNU objcopy\n";
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000413 cl::PrintVersionMessage();
414 exit(0);
415 }
416
417 SmallVector<const char *, 2> Positional;
418
419 for (auto Arg : InputArgs.filtered(OBJCOPY_UNKNOWN))
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000420 return createStringError(errc::invalid_argument, "unknown argument '%s'",
421 Arg->getAsString(InputArgs).c_str());
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000422
423 for (auto Arg : InputArgs.filtered(OBJCOPY_INPUT))
424 Positional.push_back(Arg->getValue());
425
426 if (Positional.empty())
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000427 return createStringError(errc::invalid_argument, "No input file specified");
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000428
429 if (Positional.size() > 2)
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000430 return createStringError(errc::invalid_argument,
431 "Too many positional arguments");
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000432
433 CopyConfig Config;
434 Config.InputFilename = Positional[0];
435 Config.OutputFilename = Positional[Positional.size() == 1 ? 0 : 1];
Jordan Rupprechtbb4588e2018-10-12 00:36:01 +0000436 if (InputArgs.hasArg(OBJCOPY_target) &&
437 (InputArgs.hasArg(OBJCOPY_input_target) ||
438 InputArgs.hasArg(OBJCOPY_output_target)))
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000439 return createStringError(
440 errc::invalid_argument,
441 "--target cannot be used with --input-target or --output-target");
Jordan Rupprechtbb4588e2018-10-12 00:36:01 +0000442
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000443 bool UseRegex = InputArgs.hasArg(OBJCOPY_regex);
Jordan Rupprechtbb4588e2018-10-12 00:36:01 +0000444 if (InputArgs.hasArg(OBJCOPY_target)) {
445 Config.InputFormat = InputArgs.getLastArgValue(OBJCOPY_target);
446 Config.OutputFormat = InputArgs.getLastArgValue(OBJCOPY_target);
447 } else {
448 Config.InputFormat = InputArgs.getLastArgValue(OBJCOPY_input_target);
449 Config.OutputFormat = InputArgs.getLastArgValue(OBJCOPY_output_target);
450 }
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000451 if (Config.InputFormat == "binary") {
452 auto BinaryArch = InputArgs.getLastArgValue(OBJCOPY_binary_architecture);
453 if (BinaryArch.empty())
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000454 return createStringError(
455 errc::invalid_argument,
456 "Specified binary input without specifiying an architecture");
457 Expected<const MachineInfo &> MI = getMachineInfo(BinaryArch);
458 if (!MI)
459 return MI.takeError();
460 Config.BinaryArch = *MI;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000461 }
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000462 if (!Config.OutputFormat.empty() && Config.OutputFormat != "binary") {
463 Expected<const MachineInfo &> MI =
464 getOutputFormatMachineInfo(Config.OutputFormat);
465 if (!MI)
466 return MI.takeError();
467 Config.OutputArch = *MI;
468 }
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000469
470 if (auto Arg = InputArgs.getLastArg(OBJCOPY_compress_debug_sections,
471 OBJCOPY_compress_debug_sections_eq)) {
472 Config.CompressionType = DebugCompressionType::Z;
473
474 if (Arg->getOption().getID() == OBJCOPY_compress_debug_sections_eq) {
475 Config.CompressionType =
476 StringSwitch<DebugCompressionType>(
477 InputArgs.getLastArgValue(OBJCOPY_compress_debug_sections_eq))
478 .Case("zlib-gnu", DebugCompressionType::GNU)
479 .Case("zlib", DebugCompressionType::Z)
480 .Default(DebugCompressionType::None);
481 if (Config.CompressionType == DebugCompressionType::None)
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000482 return createStringError(
483 errc::invalid_argument,
484 "Invalid or unsupported --compress-debug-sections format: %s",
485 InputArgs.getLastArgValue(OBJCOPY_compress_debug_sections_eq)
486 .str()
487 .c_str());
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000488 }
George Rimar1e930802019-03-05 11:32:14 +0000489 if (!zlib::isAvailable())
490 return createStringError(
491 errc::invalid_argument,
492 "LLVM was not compiled with LLVM_ENABLE_ZLIB: can not compress");
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000493 }
494
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000495 Config.AddGnuDebugLink = InputArgs.getLastArgValue(OBJCOPY_add_gnu_debuglink);
Jake Ehrlich8ad77792018-12-03 19:49:23 +0000496 Config.BuildIdLinkDir = InputArgs.getLastArgValue(OBJCOPY_build_id_link_dir);
497 if (InputArgs.hasArg(OBJCOPY_build_id_link_input))
498 Config.BuildIdLinkInput =
499 InputArgs.getLastArgValue(OBJCOPY_build_id_link_input);
500 if (InputArgs.hasArg(OBJCOPY_build_id_link_output))
501 Config.BuildIdLinkOutput =
502 InputArgs.getLastArgValue(OBJCOPY_build_id_link_output);
503 Config.SplitDWO = InputArgs.getLastArgValue(OBJCOPY_split_dwo);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000504 Config.SymbolsPrefix = InputArgs.getLastArgValue(OBJCOPY_prefix_symbols);
505
506 for (auto Arg : InputArgs.filtered(OBJCOPY_redefine_symbol)) {
507 if (!StringRef(Arg->getValue()).contains('='))
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000508 return createStringError(errc::invalid_argument,
509 "Bad format for --redefine-sym");
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000510 auto Old2New = StringRef(Arg->getValue()).split('=');
511 if (!Config.SymbolsToRename.insert(Old2New).second)
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000512 return createStringError(errc::invalid_argument,
513 "Multiple redefinition of symbol %s",
514 Old2New.first.str().c_str());
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000515 }
516
Eugene Leviant340cb872019-02-08 10:33:16 +0000517 for (auto Arg : InputArgs.filtered(OBJCOPY_redefine_symbols))
518 if (Error E = addSymbolsToRenameFromFile(Config.SymbolsToRename, DC.Alloc,
519 Arg->getValue()))
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000520 return std::move(E);
Eugene Leviant340cb872019-02-08 10:33:16 +0000521
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000522 for (auto Arg : InputArgs.filtered(OBJCOPY_rename_section)) {
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000523 Expected<SectionRename> SR =
524 parseRenameSectionValue(StringRef(Arg->getValue()));
525 if (!SR)
526 return SR.takeError();
527 if (!Config.SectionsToRename.try_emplace(SR->OriginalName, *SR).second)
528 return createStringError(errc::invalid_argument,
529 "Multiple renames of section %s",
530 SR->OriginalName.str().c_str());
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000531 }
Jordan Rupprechtc8927412019-01-29 15:05:38 +0000532 for (auto Arg : InputArgs.filtered(OBJCOPY_set_section_flags)) {
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000533 Expected<SectionFlagsUpdate> SFU =
534 parseSetSectionFlagValue(Arg->getValue());
535 if (!SFU)
536 return SFU.takeError();
537 if (!Config.SetSectionFlags.try_emplace(SFU->Name, *SFU).second)
538 return createStringError(
539 errc::invalid_argument,
540 "--set-section-flags set multiple times for section %s",
541 SFU->Name.str().c_str());
Jordan Rupprechtc8927412019-01-29 15:05:38 +0000542 }
543 // Prohibit combinations of --set-section-flags when the section name is used
544 // by --rename-section, either as a source or a destination.
545 for (const auto &E : Config.SectionsToRename) {
546 const SectionRename &SR = E.second;
547 if (Config.SetSectionFlags.count(SR.OriginalName))
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000548 return createStringError(
549 errc::invalid_argument,
550 "--set-section-flags=%s conflicts with --rename-section=%s=%s",
551 SR.OriginalName.str().c_str(), SR.OriginalName.str().c_str(),
552 SR.NewName.str().c_str());
Jordan Rupprechtc8927412019-01-29 15:05:38 +0000553 if (Config.SetSectionFlags.count(SR.NewName))
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000554 return createStringError(
555 errc::invalid_argument,
556 "--set-section-flags=%s conflicts with --rename-section=%s=%s",
557 SR.NewName.str().c_str(), SR.OriginalName.str().c_str(),
558 SR.NewName.str().c_str());
Jordan Rupprechtc8927412019-01-29 15:05:38 +0000559 }
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000560
561 for (auto Arg : InputArgs.filtered(OBJCOPY_remove_section))
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000562 Config.ToRemove.emplace_back(Arg->getValue(), UseRegex);
Jordan Rupprechtc5bae782018-11-13 19:32:27 +0000563 for (auto Arg : InputArgs.filtered(OBJCOPY_keep_section))
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000564 Config.KeepSection.emplace_back(Arg->getValue(), UseRegex);
Jake Ehrlich85985ed2018-12-06 02:03:53 +0000565 for (auto Arg : InputArgs.filtered(OBJCOPY_only_section))
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000566 Config.OnlySection.emplace_back(Arg->getValue(), UseRegex);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000567 for (auto Arg : InputArgs.filtered(OBJCOPY_add_section))
568 Config.AddSection.push_back(Arg->getValue());
569 for (auto Arg : InputArgs.filtered(OBJCOPY_dump_section))
570 Config.DumpSection.push_back(Arg->getValue());
571 Config.StripAll = InputArgs.hasArg(OBJCOPY_strip_all);
572 Config.StripAllGNU = InputArgs.hasArg(OBJCOPY_strip_all_gnu);
573 Config.StripDebug = InputArgs.hasArg(OBJCOPY_strip_debug);
574 Config.StripDWO = InputArgs.hasArg(OBJCOPY_strip_dwo);
575 Config.StripSections = InputArgs.hasArg(OBJCOPY_strip_sections);
576 Config.StripNonAlloc = InputArgs.hasArg(OBJCOPY_strip_non_alloc);
577 Config.StripUnneeded = InputArgs.hasArg(OBJCOPY_strip_unneeded);
578 Config.ExtractDWO = InputArgs.hasArg(OBJCOPY_extract_dwo);
579 Config.LocalizeHidden = InputArgs.hasArg(OBJCOPY_localize_hidden);
580 Config.Weaken = InputArgs.hasArg(OBJCOPY_weaken);
Jordan Rupprechtd0f7bcf2019-01-30 14:58:13 +0000581 if (InputArgs.hasArg(OBJCOPY_discard_all, OBJCOPY_discard_locals))
582 Config.DiscardMode =
583 InputArgs.hasFlag(OBJCOPY_discard_all, OBJCOPY_discard_locals)
584 ? DiscardType::All
585 : DiscardType::Locals;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000586 Config.OnlyKeepDebug = InputArgs.hasArg(OBJCOPY_only_keep_debug);
587 Config.KeepFileSymbols = InputArgs.hasArg(OBJCOPY_keep_file_symbols);
588 Config.DecompressDebugSections =
589 InputArgs.hasArg(OBJCOPY_decompress_debug_sections);
590 for (auto Arg : InputArgs.filtered(OBJCOPY_localize_symbol))
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000591 Config.SymbolsToLocalize.emplace_back(Arg->getValue(), UseRegex);
Eugene Leviante08fe352019-02-08 14:37:54 +0000592 for (auto Arg : InputArgs.filtered(OBJCOPY_localize_symbols))
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000593 if (Error E = addSymbolsFromFile(Config.SymbolsToLocalize, DC.Alloc,
594 Arg->getValue(), UseRegex))
595 return std::move(E);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000596 for (auto Arg : InputArgs.filtered(OBJCOPY_keep_global_symbol))
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000597 Config.SymbolsToKeepGlobal.emplace_back(Arg->getValue(), UseRegex);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000598 for (auto Arg : InputArgs.filtered(OBJCOPY_keep_global_symbols))
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000599 if (Error E = addSymbolsFromFile(Config.SymbolsToKeepGlobal, DC.Alloc,
600 Arg->getValue(), UseRegex))
601 return std::move(E);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000602 for (auto Arg : InputArgs.filtered(OBJCOPY_globalize_symbol))
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000603 Config.SymbolsToGlobalize.emplace_back(Arg->getValue(), UseRegex);
Eugene Leviante08fe352019-02-08 14:37:54 +0000604 for (auto Arg : InputArgs.filtered(OBJCOPY_globalize_symbols))
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000605 if (Error E = addSymbolsFromFile(Config.SymbolsToGlobalize, DC.Alloc,
606 Arg->getValue(), UseRegex))
607 return std::move(E);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000608 for (auto Arg : InputArgs.filtered(OBJCOPY_weaken_symbol))
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000609 Config.SymbolsToWeaken.emplace_back(Arg->getValue(), UseRegex);
Eugene Leviante08fe352019-02-08 14:37:54 +0000610 for (auto Arg : InputArgs.filtered(OBJCOPY_weaken_symbols))
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000611 if (Error E = addSymbolsFromFile(Config.SymbolsToWeaken, DC.Alloc,
612 Arg->getValue(), UseRegex))
613 return std::move(E);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000614 for (auto Arg : InputArgs.filtered(OBJCOPY_strip_symbol))
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000615 Config.SymbolsToRemove.emplace_back(Arg->getValue(), UseRegex);
Eugene Leviante08fe352019-02-08 14:37:54 +0000616 for (auto Arg : InputArgs.filtered(OBJCOPY_strip_symbols))
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000617 if (Error E = addSymbolsFromFile(Config.SymbolsToRemove, DC.Alloc,
618 Arg->getValue(), UseRegex))
619 return std::move(E);
Eugene Leviant2db10622019-02-13 07:34:54 +0000620 for (auto Arg : InputArgs.filtered(OBJCOPY_strip_unneeded_symbol))
621 Config.UnneededSymbolsToRemove.emplace_back(Arg->getValue(), UseRegex);
622 for (auto Arg : InputArgs.filtered(OBJCOPY_strip_unneeded_symbols))
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000623 if (Error E = addSymbolsFromFile(Config.UnneededSymbolsToRemove, DC.Alloc,
624 Arg->getValue(), UseRegex))
625 return std::move(E);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000626 for (auto Arg : InputArgs.filtered(OBJCOPY_keep_symbol))
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000627 Config.SymbolsToKeep.emplace_back(Arg->getValue(), UseRegex);
Jordan Rupprecht42bc1e22019-03-13 22:26:01 +0000628 for (auto Arg : InputArgs.filtered(OBJCOPY_add_symbol)) {
629 Expected<NewSymbolInfo> NSI = parseNewSymbolInfo(Arg->getValue());
630 if (!NSI)
631 return NSI.takeError();
632 Config.SymbolsToAdd.push_back(*NSI);
633 }
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000634
Jordan Rupprechtfc780bb2018-11-01 17:36:37 +0000635 Config.DeterministicArchives = InputArgs.hasFlag(
636 OBJCOPY_enable_deterministic_archives,
637 OBJCOPY_disable_deterministic_archives, /*default=*/true);
638
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000639 Config.PreserveDates = InputArgs.hasArg(OBJCOPY_preserve_dates);
640
Eugene Leviant53350d02019-02-26 09:24:22 +0000641 for (auto Arg : InputArgs)
642 if (Arg->getOption().matches(OBJCOPY_set_start)) {
643 auto EAddr = getAsInteger<uint64_t>(Arg->getValue());
644 if (!EAddr)
645 return createStringError(
646 EAddr.getError(), "bad entry point address: '%s'", Arg->getValue());
647
648 Config.EntryExpr = [EAddr](uint64_t) { return *EAddr; };
649 } else if (Arg->getOption().matches(OBJCOPY_change_start)) {
650 auto EIncr = getAsInteger<int64_t>(Arg->getValue());
651 if (!EIncr)
652 return createStringError(EIncr.getError(),
653 "bad entry point increment: '%s'",
654 Arg->getValue());
655 auto Expr = Config.EntryExpr ? std::move(Config.EntryExpr)
656 : [](uint64_t A) { return A; };
657 Config.EntryExpr = [Expr, EIncr](uint64_t EAddr) {
658 return Expr(EAddr) + *EIncr;
659 };
660 }
661
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000662 if (Config.DecompressDebugSections &&
663 Config.CompressionType != DebugCompressionType::None) {
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000664 return createStringError(
665 errc::invalid_argument,
666 "Cannot specify --compress-debug-sections at the same time as "
667 "--decompress-debug-sections at the same time");
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000668 }
669
670 if (Config.DecompressDebugSections && !zlib::isAvailable())
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000671 return createStringError(
672 errc::invalid_argument,
673 "LLVM was not compiled with LLVM_ENABLE_ZLIB: cannot decompress");
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000674
Jordan Rupprechtab9f6622018-10-23 20:54:51 +0000675 DC.CopyConfigs.push_back(std::move(Config));
Jordan Rupprecht93ad8b32019-02-21 17:24:55 +0000676 return std::move(DC);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000677}
678
679// ParseStripOptions returns the config and sets the input arguments. If a
680// help flag is set then ParseStripOptions will print the help messege and
681// exit.
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000682Expected<DriverConfig> parseStripOptions(ArrayRef<const char *> ArgsArr) {
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000683 StripOptTable T;
684 unsigned MissingArgumentIndex, MissingArgumentCount;
685 llvm::opt::InputArgList InputArgs =
686 T.ParseArgs(ArgsArr, MissingArgumentIndex, MissingArgumentCount);
687
688 if (InputArgs.size() == 0) {
689 T.PrintHelp(errs(), "llvm-strip [options] file...", "strip tool");
690 exit(1);
691 }
692
693 if (InputArgs.hasArg(STRIP_help)) {
694 T.PrintHelp(outs(), "llvm-strip [options] file...", "strip tool");
695 exit(0);
696 }
697
698 if (InputArgs.hasArg(STRIP_version)) {
Martin Storsjoe9af7152018-11-28 06:51:50 +0000699 outs() << "llvm-strip, compatible with GNU strip\n";
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000700 cl::PrintVersionMessage();
701 exit(0);
702 }
703
704 SmallVector<const char *, 2> Positional;
705 for (auto Arg : InputArgs.filtered(STRIP_UNKNOWN))
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000706 return createStringError(errc::invalid_argument, "unknown argument '%s'",
707 Arg->getAsString(InputArgs).c_str());
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000708 for (auto Arg : InputArgs.filtered(STRIP_INPUT))
709 Positional.push_back(Arg->getValue());
710
711 if (Positional.empty())
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000712 return createStringError(errc::invalid_argument, "No input file specified");
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000713
714 if (Positional.size() > 1 && InputArgs.hasArg(STRIP_output))
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000715 return createStringError(
716 errc::invalid_argument,
717 "Multiple input files cannot be used in combination with -o");
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000718
719 CopyConfig Config;
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000720 bool UseRegexp = InputArgs.hasArg(STRIP_regex);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000721 Config.StripDebug = InputArgs.hasArg(STRIP_strip_debug);
722
Jordan Rupprechtd0f7bcf2019-01-30 14:58:13 +0000723 if (InputArgs.hasArg(STRIP_discard_all, STRIP_discard_locals))
724 Config.DiscardMode =
725 InputArgs.hasFlag(STRIP_discard_all, STRIP_discard_locals)
726 ? DiscardType::All
727 : DiscardType::Locals;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000728 Config.StripUnneeded = InputArgs.hasArg(STRIP_strip_unneeded);
729 Config.StripAll = InputArgs.hasArg(STRIP_strip_all);
Jordan Rupprecht30d1b192018-11-01 17:48:46 +0000730 Config.StripAllGNU = InputArgs.hasArg(STRIP_strip_all_gnu);
Jordan Rupprecht12ed01d2019-03-14 21:51:42 +0000731 Config.OnlyKeepDebug = InputArgs.hasArg(STRIP_only_keep_debug);
Eugene Leviant05a3f992019-02-01 15:25:15 +0000732 Config.KeepFileSymbols = InputArgs.hasArg(STRIP_keep_file_symbols);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000733
Jordan Rupprechtc5bae782018-11-13 19:32:27 +0000734 for (auto Arg : InputArgs.filtered(STRIP_keep_section))
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000735 Config.KeepSection.emplace_back(Arg->getValue(), UseRegexp);
Jordan Rupprecht30d1b192018-11-01 17:48:46 +0000736
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000737 for (auto Arg : InputArgs.filtered(STRIP_remove_section))
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000738 Config.ToRemove.emplace_back(Arg->getValue(), UseRegexp);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000739
Eugene Leviant2267c582019-01-31 12:16:20 +0000740 for (auto Arg : InputArgs.filtered(STRIP_strip_symbol))
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000741 Config.SymbolsToRemove.emplace_back(Arg->getValue(), UseRegexp);
Eugene Leviant2267c582019-01-31 12:16:20 +0000742
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000743 for (auto Arg : InputArgs.filtered(STRIP_keep_symbol))
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000744 Config.SymbolsToKeep.emplace_back(Arg->getValue(), UseRegexp);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000745
Eugene Leviant2267c582019-01-31 12:16:20 +0000746 if (!Config.StripDebug && !Config.StripUnneeded &&
747 Config.DiscardMode == DiscardType::None && !Config.StripAllGNU && Config.SymbolsToRemove.empty())
748 Config.StripAll = true;
749
Jordan Rupprechtfc780bb2018-11-01 17:36:37 +0000750 Config.DeterministicArchives =
751 InputArgs.hasFlag(STRIP_enable_deterministic_archives,
752 STRIP_disable_deterministic_archives, /*default=*/true);
753
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000754 Config.PreserveDates = InputArgs.hasArg(STRIP_preserve_dates);
755
756 DriverConfig DC;
757 if (Positional.size() == 1) {
758 Config.InputFilename = Positional[0];
759 Config.OutputFilename =
760 InputArgs.getLastArgValue(STRIP_output, Positional[0]);
761 DC.CopyConfigs.push_back(std::move(Config));
762 } else {
763 for (const char *Filename : Positional) {
764 Config.InputFilename = Filename;
765 Config.OutputFilename = Filename;
766 DC.CopyConfigs.push_back(Config);
767 }
768 }
769
Jordan Rupprecht93ad8b32019-02-21 17:24:55 +0000770 return std::move(DC);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000771}
772
773} // namespace objcopy
774} // namespace llvm