blob: e2d3d4575c1dbaacb266d07a1cbc64fa42b0003b [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{
James Hendersonc040d5d2019-03-22 10:21:09 +0000256 // Name, {EMachine, OS/ABI, 64bit, LittleEndian}
257 {"aarch64", {ELF::EM_AARCH64, ELF::ELFOSABI_NONE, true, true}},
258 {"arm", {ELF::EM_ARM, ELF::ELFOSABI_NONE, false, true}},
259 {"i386", {ELF::EM_386, ELF::ELFOSABI_NONE, false, true}},
260 {"i386:x86-64", {ELF::EM_X86_64, ELF::ELFOSABI_NONE, true, true}},
261 {"powerpc:common64", {ELF::EM_PPC64, ELF::ELFOSABI_NONE, true, true}},
262 {"sparc", {ELF::EM_SPARC, ELF::ELFOSABI_NONE, false, true}},
263 {"x86-64", {ELF::EM_X86_64, ELF::ELFOSABI_NONE, 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 Rupprecht70038e02019-01-07 16:59:12 +0000274static const StringMap<MachineInfo> OutputFormatMap{
James Hendersonc040d5d2019-03-22 10:21:09 +0000275 // Name, {EMachine, OSABI, 64bit, LittleEndian}
276 {"elf32-i386", {ELF::EM_386, ELF::ELFOSABI_NONE, false, true}},
277 {"elf32-i386-freebsd", {ELF::EM_386, ELF::ELFOSABI_FREEBSD, false, true}},
278 {"elf32-powerpcle", {ELF::EM_PPC, ELF::ELFOSABI_NONE, false, true}},
279 {"elf32-x86-64", {ELF::EM_X86_64, ELF::ELFOSABI_NONE, false, true}},
280 {"elf64-powerpcle", {ELF::EM_PPC64, ELF::ELFOSABI_NONE, true, true}},
281 {"elf64-x86-64", {ELF::EM_X86_64, ELF::ELFOSABI_NONE, true, true}},
282 {"elf64-x86-64-freebsd",
283 {ELF::EM_X86_64, ELF::ELFOSABI_FREEBSD, true, true}},
Jordan Rupprecht70038e02019-01-07 16:59:12 +0000284};
285
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000286static Expected<const MachineInfo &>
287getOutputFormatMachineInfo(StringRef Format) {
Jordan Rupprecht70038e02019-01-07 16:59:12 +0000288 auto Iter = OutputFormatMap.find(Format);
289 if (Iter == std::end(OutputFormatMap))
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000290 return createStringError(errc::invalid_argument,
291 "Invalid output format: '%s'",
292 Format.str().c_str());
Jordan Rupprecht70038e02019-01-07 16:59:12 +0000293 return Iter->getValue();
294}
295
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000296static Error addSymbolsFromFile(std::vector<NameOrRegex> &Symbols,
297 BumpPtrAllocator &Alloc, StringRef Filename,
298 bool UseRegex) {
Jordan Rupprecht5745c5f2019-02-04 18:38:00 +0000299 StringSaver Saver(Alloc);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000300 SmallVector<StringRef, 16> Lines;
301 auto BufOrErr = MemoryBuffer::getFile(Filename);
302 if (!BufOrErr)
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000303 return createFileError(Filename, BufOrErr.getError());
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000304
305 BufOrErr.get()->getBuffer().split(Lines, '\n');
306 for (StringRef Line : Lines) {
307 // Ignore everything after '#', trim whitespace, and only add the symbol if
308 // it's not empty.
309 auto TrimmedLine = Line.split('#').first.trim();
310 if (!TrimmedLine.empty())
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000311 Symbols.emplace_back(Saver.save(TrimmedLine), UseRegex);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000312 }
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000313
314 return Error::success();
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000315}
316
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000317NameOrRegex::NameOrRegex(StringRef Pattern, bool IsRegex) {
318 if (!IsRegex) {
319 Name = Pattern;
320 return;
321 }
322
323 SmallVector<char, 32> Data;
324 R = std::make_shared<Regex>(
325 ("^" + Pattern.ltrim('^').rtrim('$') + "$").toStringRef(Data));
326}
327
Eugene Leviant340cb872019-02-08 10:33:16 +0000328static Error addSymbolsToRenameFromFile(StringMap<StringRef> &SymbolsToRename,
329 BumpPtrAllocator &Alloc,
330 StringRef Filename) {
331 StringSaver Saver(Alloc);
332 SmallVector<StringRef, 16> Lines;
333 auto BufOrErr = MemoryBuffer::getFile(Filename);
334 if (!BufOrErr)
Eugene Leviant317f9e72019-02-11 09:49:37 +0000335 return createFileError(Filename, BufOrErr.getError());
Eugene Leviant340cb872019-02-08 10:33:16 +0000336
337 BufOrErr.get()->getBuffer().split(Lines, '\n');
338 size_t NumLines = Lines.size();
339 for (size_t LineNo = 0; LineNo < NumLines; ++LineNo) {
340 StringRef TrimmedLine = Lines[LineNo].split('#').first.trim();
341 if (TrimmedLine.empty())
342 continue;
343
344 std::pair<StringRef, StringRef> Pair = Saver.save(TrimmedLine).split(' ');
345 StringRef NewName = Pair.second.trim();
346 if (NewName.empty())
347 return createStringError(errc::invalid_argument,
348 "%s:%zu: missing new symbol name",
349 Filename.str().c_str(), LineNo + 1);
350 SymbolsToRename.insert({Pair.first, NewName});
351 }
352 return Error::success();
353}
Eugene Leviant53350d02019-02-26 09:24:22 +0000354
355template <class T> static ErrorOr<T> getAsInteger(StringRef Val) {
356 T Result;
357 if (Val.getAsInteger(0, Result))
358 return errc::invalid_argument;
359 return Result;
360}
361
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000362// ParseObjcopyOptions returns the config and sets the input arguments. If a
363// help flag is set then ParseObjcopyOptions will print the help messege and
364// exit.
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000365Expected<DriverConfig> parseObjcopyOptions(ArrayRef<const char *> ArgsArr) {
Jordan Rupprecht5745c5f2019-02-04 18:38:00 +0000366 DriverConfig DC;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000367 ObjcopyOptTable T;
368 unsigned MissingArgumentIndex, MissingArgumentCount;
369 llvm::opt::InputArgList InputArgs =
370 T.ParseArgs(ArgsArr, MissingArgumentIndex, MissingArgumentCount);
371
372 if (InputArgs.size() == 0) {
373 T.PrintHelp(errs(), "llvm-objcopy input [output]", "objcopy tool");
374 exit(1);
375 }
376
377 if (InputArgs.hasArg(OBJCOPY_help)) {
378 T.PrintHelp(outs(), "llvm-objcopy input [output]", "objcopy tool");
379 exit(0);
380 }
381
382 if (InputArgs.hasArg(OBJCOPY_version)) {
Martin Storsjoe9af7152018-11-28 06:51:50 +0000383 outs() << "llvm-objcopy, compatible with GNU objcopy\n";
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000384 cl::PrintVersionMessage();
385 exit(0);
386 }
387
388 SmallVector<const char *, 2> Positional;
389
390 for (auto Arg : InputArgs.filtered(OBJCOPY_UNKNOWN))
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000391 return createStringError(errc::invalid_argument, "unknown argument '%s'",
392 Arg->getAsString(InputArgs).c_str());
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000393
394 for (auto Arg : InputArgs.filtered(OBJCOPY_INPUT))
395 Positional.push_back(Arg->getValue());
396
397 if (Positional.empty())
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000398 return createStringError(errc::invalid_argument, "No input file specified");
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000399
400 if (Positional.size() > 2)
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000401 return createStringError(errc::invalid_argument,
402 "Too many positional arguments");
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000403
404 CopyConfig Config;
405 Config.InputFilename = Positional[0];
406 Config.OutputFilename = Positional[Positional.size() == 1 ? 0 : 1];
Jordan Rupprechtbb4588e2018-10-12 00:36:01 +0000407 if (InputArgs.hasArg(OBJCOPY_target) &&
408 (InputArgs.hasArg(OBJCOPY_input_target) ||
409 InputArgs.hasArg(OBJCOPY_output_target)))
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000410 return createStringError(
411 errc::invalid_argument,
412 "--target cannot be used with --input-target or --output-target");
Jordan Rupprechtbb4588e2018-10-12 00:36:01 +0000413
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000414 bool UseRegex = InputArgs.hasArg(OBJCOPY_regex);
Jordan Rupprechtbb4588e2018-10-12 00:36:01 +0000415 if (InputArgs.hasArg(OBJCOPY_target)) {
416 Config.InputFormat = InputArgs.getLastArgValue(OBJCOPY_target);
417 Config.OutputFormat = InputArgs.getLastArgValue(OBJCOPY_target);
418 } else {
419 Config.InputFormat = InputArgs.getLastArgValue(OBJCOPY_input_target);
420 Config.OutputFormat = InputArgs.getLastArgValue(OBJCOPY_output_target);
421 }
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000422 if (Config.InputFormat == "binary") {
423 auto BinaryArch = InputArgs.getLastArgValue(OBJCOPY_binary_architecture);
424 if (BinaryArch.empty())
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000425 return createStringError(
426 errc::invalid_argument,
427 "Specified binary input without specifiying an architecture");
428 Expected<const MachineInfo &> MI = getMachineInfo(BinaryArch);
429 if (!MI)
430 return MI.takeError();
431 Config.BinaryArch = *MI;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000432 }
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000433 if (!Config.OutputFormat.empty() && Config.OutputFormat != "binary") {
434 Expected<const MachineInfo &> MI =
435 getOutputFormatMachineInfo(Config.OutputFormat);
436 if (!MI)
437 return MI.takeError();
438 Config.OutputArch = *MI;
439 }
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000440
441 if (auto Arg = InputArgs.getLastArg(OBJCOPY_compress_debug_sections,
442 OBJCOPY_compress_debug_sections_eq)) {
443 Config.CompressionType = DebugCompressionType::Z;
444
445 if (Arg->getOption().getID() == OBJCOPY_compress_debug_sections_eq) {
446 Config.CompressionType =
447 StringSwitch<DebugCompressionType>(
448 InputArgs.getLastArgValue(OBJCOPY_compress_debug_sections_eq))
449 .Case("zlib-gnu", DebugCompressionType::GNU)
450 .Case("zlib", DebugCompressionType::Z)
451 .Default(DebugCompressionType::None);
452 if (Config.CompressionType == DebugCompressionType::None)
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000453 return createStringError(
454 errc::invalid_argument,
455 "Invalid or unsupported --compress-debug-sections format: %s",
456 InputArgs.getLastArgValue(OBJCOPY_compress_debug_sections_eq)
457 .str()
458 .c_str());
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000459 }
George Rimar1e930802019-03-05 11:32:14 +0000460 if (!zlib::isAvailable())
461 return createStringError(
462 errc::invalid_argument,
463 "LLVM was not compiled with LLVM_ENABLE_ZLIB: can not compress");
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000464 }
465
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000466 Config.AddGnuDebugLink = InputArgs.getLastArgValue(OBJCOPY_add_gnu_debuglink);
Jake Ehrlich8ad77792018-12-03 19:49:23 +0000467 Config.BuildIdLinkDir = InputArgs.getLastArgValue(OBJCOPY_build_id_link_dir);
468 if (InputArgs.hasArg(OBJCOPY_build_id_link_input))
469 Config.BuildIdLinkInput =
470 InputArgs.getLastArgValue(OBJCOPY_build_id_link_input);
471 if (InputArgs.hasArg(OBJCOPY_build_id_link_output))
472 Config.BuildIdLinkOutput =
473 InputArgs.getLastArgValue(OBJCOPY_build_id_link_output);
474 Config.SplitDWO = InputArgs.getLastArgValue(OBJCOPY_split_dwo);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000475 Config.SymbolsPrefix = InputArgs.getLastArgValue(OBJCOPY_prefix_symbols);
476
477 for (auto Arg : InputArgs.filtered(OBJCOPY_redefine_symbol)) {
478 if (!StringRef(Arg->getValue()).contains('='))
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000479 return createStringError(errc::invalid_argument,
480 "Bad format for --redefine-sym");
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000481 auto Old2New = StringRef(Arg->getValue()).split('=');
482 if (!Config.SymbolsToRename.insert(Old2New).second)
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000483 return createStringError(errc::invalid_argument,
484 "Multiple redefinition of symbol %s",
485 Old2New.first.str().c_str());
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000486 }
487
Eugene Leviant340cb872019-02-08 10:33:16 +0000488 for (auto Arg : InputArgs.filtered(OBJCOPY_redefine_symbols))
489 if (Error E = addSymbolsToRenameFromFile(Config.SymbolsToRename, DC.Alloc,
490 Arg->getValue()))
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000491 return std::move(E);
Eugene Leviant340cb872019-02-08 10:33:16 +0000492
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000493 for (auto Arg : InputArgs.filtered(OBJCOPY_rename_section)) {
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000494 Expected<SectionRename> SR =
495 parseRenameSectionValue(StringRef(Arg->getValue()));
496 if (!SR)
497 return SR.takeError();
498 if (!Config.SectionsToRename.try_emplace(SR->OriginalName, *SR).second)
499 return createStringError(errc::invalid_argument,
500 "Multiple renames of section %s",
501 SR->OriginalName.str().c_str());
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000502 }
Jordan Rupprechtc8927412019-01-29 15:05:38 +0000503 for (auto Arg : InputArgs.filtered(OBJCOPY_set_section_flags)) {
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000504 Expected<SectionFlagsUpdate> SFU =
505 parseSetSectionFlagValue(Arg->getValue());
506 if (!SFU)
507 return SFU.takeError();
508 if (!Config.SetSectionFlags.try_emplace(SFU->Name, *SFU).second)
509 return createStringError(
510 errc::invalid_argument,
511 "--set-section-flags set multiple times for section %s",
512 SFU->Name.str().c_str());
Jordan Rupprechtc8927412019-01-29 15:05:38 +0000513 }
514 // Prohibit combinations of --set-section-flags when the section name is used
515 // by --rename-section, either as a source or a destination.
516 for (const auto &E : Config.SectionsToRename) {
517 const SectionRename &SR = E.second;
518 if (Config.SetSectionFlags.count(SR.OriginalName))
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000519 return createStringError(
520 errc::invalid_argument,
521 "--set-section-flags=%s conflicts with --rename-section=%s=%s",
522 SR.OriginalName.str().c_str(), SR.OriginalName.str().c_str(),
523 SR.NewName.str().c_str());
Jordan Rupprechtc8927412019-01-29 15:05:38 +0000524 if (Config.SetSectionFlags.count(SR.NewName))
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000525 return createStringError(
526 errc::invalid_argument,
527 "--set-section-flags=%s conflicts with --rename-section=%s=%s",
528 SR.NewName.str().c_str(), SR.OriginalName.str().c_str(),
529 SR.NewName.str().c_str());
Jordan Rupprechtc8927412019-01-29 15:05:38 +0000530 }
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000531
532 for (auto Arg : InputArgs.filtered(OBJCOPY_remove_section))
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000533 Config.ToRemove.emplace_back(Arg->getValue(), UseRegex);
Jordan Rupprechtc5bae782018-11-13 19:32:27 +0000534 for (auto Arg : InputArgs.filtered(OBJCOPY_keep_section))
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000535 Config.KeepSection.emplace_back(Arg->getValue(), UseRegex);
Jake Ehrlich85985ed2018-12-06 02:03:53 +0000536 for (auto Arg : InputArgs.filtered(OBJCOPY_only_section))
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000537 Config.OnlySection.emplace_back(Arg->getValue(), UseRegex);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000538 for (auto Arg : InputArgs.filtered(OBJCOPY_add_section))
539 Config.AddSection.push_back(Arg->getValue());
540 for (auto Arg : InputArgs.filtered(OBJCOPY_dump_section))
541 Config.DumpSection.push_back(Arg->getValue());
542 Config.StripAll = InputArgs.hasArg(OBJCOPY_strip_all);
543 Config.StripAllGNU = InputArgs.hasArg(OBJCOPY_strip_all_gnu);
544 Config.StripDebug = InputArgs.hasArg(OBJCOPY_strip_debug);
545 Config.StripDWO = InputArgs.hasArg(OBJCOPY_strip_dwo);
546 Config.StripSections = InputArgs.hasArg(OBJCOPY_strip_sections);
547 Config.StripNonAlloc = InputArgs.hasArg(OBJCOPY_strip_non_alloc);
548 Config.StripUnneeded = InputArgs.hasArg(OBJCOPY_strip_unneeded);
549 Config.ExtractDWO = InputArgs.hasArg(OBJCOPY_extract_dwo);
550 Config.LocalizeHidden = InputArgs.hasArg(OBJCOPY_localize_hidden);
551 Config.Weaken = InputArgs.hasArg(OBJCOPY_weaken);
Jordan Rupprechtd0f7bcf2019-01-30 14:58:13 +0000552 if (InputArgs.hasArg(OBJCOPY_discard_all, OBJCOPY_discard_locals))
553 Config.DiscardMode =
554 InputArgs.hasFlag(OBJCOPY_discard_all, OBJCOPY_discard_locals)
555 ? DiscardType::All
556 : DiscardType::Locals;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000557 Config.OnlyKeepDebug = InputArgs.hasArg(OBJCOPY_only_keep_debug);
558 Config.KeepFileSymbols = InputArgs.hasArg(OBJCOPY_keep_file_symbols);
559 Config.DecompressDebugSections =
560 InputArgs.hasArg(OBJCOPY_decompress_debug_sections);
561 for (auto Arg : InputArgs.filtered(OBJCOPY_localize_symbol))
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000562 Config.SymbolsToLocalize.emplace_back(Arg->getValue(), UseRegex);
Eugene Leviante08fe352019-02-08 14:37:54 +0000563 for (auto Arg : InputArgs.filtered(OBJCOPY_localize_symbols))
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000564 if (Error E = addSymbolsFromFile(Config.SymbolsToLocalize, DC.Alloc,
565 Arg->getValue(), UseRegex))
566 return std::move(E);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000567 for (auto Arg : InputArgs.filtered(OBJCOPY_keep_global_symbol))
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000568 Config.SymbolsToKeepGlobal.emplace_back(Arg->getValue(), UseRegex);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000569 for (auto Arg : InputArgs.filtered(OBJCOPY_keep_global_symbols))
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000570 if (Error E = addSymbolsFromFile(Config.SymbolsToKeepGlobal, DC.Alloc,
571 Arg->getValue(), UseRegex))
572 return std::move(E);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000573 for (auto Arg : InputArgs.filtered(OBJCOPY_globalize_symbol))
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000574 Config.SymbolsToGlobalize.emplace_back(Arg->getValue(), UseRegex);
Eugene Leviante08fe352019-02-08 14:37:54 +0000575 for (auto Arg : InputArgs.filtered(OBJCOPY_globalize_symbols))
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000576 if (Error E = addSymbolsFromFile(Config.SymbolsToGlobalize, DC.Alloc,
577 Arg->getValue(), UseRegex))
578 return std::move(E);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000579 for (auto Arg : InputArgs.filtered(OBJCOPY_weaken_symbol))
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000580 Config.SymbolsToWeaken.emplace_back(Arg->getValue(), UseRegex);
Eugene Leviante08fe352019-02-08 14:37:54 +0000581 for (auto Arg : InputArgs.filtered(OBJCOPY_weaken_symbols))
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000582 if (Error E = addSymbolsFromFile(Config.SymbolsToWeaken, DC.Alloc,
583 Arg->getValue(), UseRegex))
584 return std::move(E);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000585 for (auto Arg : InputArgs.filtered(OBJCOPY_strip_symbol))
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000586 Config.SymbolsToRemove.emplace_back(Arg->getValue(), UseRegex);
Eugene Leviante08fe352019-02-08 14:37:54 +0000587 for (auto Arg : InputArgs.filtered(OBJCOPY_strip_symbols))
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000588 if (Error E = addSymbolsFromFile(Config.SymbolsToRemove, DC.Alloc,
589 Arg->getValue(), UseRegex))
590 return std::move(E);
Eugene Leviant2db10622019-02-13 07:34:54 +0000591 for (auto Arg : InputArgs.filtered(OBJCOPY_strip_unneeded_symbol))
592 Config.UnneededSymbolsToRemove.emplace_back(Arg->getValue(), UseRegex);
593 for (auto Arg : InputArgs.filtered(OBJCOPY_strip_unneeded_symbols))
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000594 if (Error E = addSymbolsFromFile(Config.UnneededSymbolsToRemove, DC.Alloc,
595 Arg->getValue(), UseRegex))
596 return std::move(E);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000597 for (auto Arg : InputArgs.filtered(OBJCOPY_keep_symbol))
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000598 Config.SymbolsToKeep.emplace_back(Arg->getValue(), UseRegex);
Yi Kongf2baddb2019-04-01 18:12:43 +0000599 for (auto Arg : InputArgs.filtered(OBJCOPY_keep_symbols))
600 if (Error E = addSymbolsFromFile(Config.SymbolsToKeep, DC.Alloc,
601 Arg->getValue(), UseRegex))
602 return std::move(E);
Jordan Rupprecht42bc1e22019-03-13 22:26:01 +0000603 for (auto Arg : InputArgs.filtered(OBJCOPY_add_symbol)) {
604 Expected<NewSymbolInfo> NSI = parseNewSymbolInfo(Arg->getValue());
605 if (!NSI)
606 return NSI.takeError();
607 Config.SymbolsToAdd.push_back(*NSI);
608 }
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000609
Jordan Rupprechtfc780bb2018-11-01 17:36:37 +0000610 Config.DeterministicArchives = InputArgs.hasFlag(
611 OBJCOPY_enable_deterministic_archives,
612 OBJCOPY_disable_deterministic_archives, /*default=*/true);
613
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000614 Config.PreserveDates = InputArgs.hasArg(OBJCOPY_preserve_dates);
615
Eugene Leviant53350d02019-02-26 09:24:22 +0000616 for (auto Arg : InputArgs)
617 if (Arg->getOption().matches(OBJCOPY_set_start)) {
618 auto EAddr = getAsInteger<uint64_t>(Arg->getValue());
619 if (!EAddr)
620 return createStringError(
621 EAddr.getError(), "bad entry point address: '%s'", Arg->getValue());
622
623 Config.EntryExpr = [EAddr](uint64_t) { return *EAddr; };
624 } else if (Arg->getOption().matches(OBJCOPY_change_start)) {
625 auto EIncr = getAsInteger<int64_t>(Arg->getValue());
626 if (!EIncr)
627 return createStringError(EIncr.getError(),
628 "bad entry point increment: '%s'",
629 Arg->getValue());
630 auto Expr = Config.EntryExpr ? std::move(Config.EntryExpr)
631 : [](uint64_t A) { return A; };
632 Config.EntryExpr = [Expr, EIncr](uint64_t EAddr) {
633 return Expr(EAddr) + *EIncr;
634 };
635 }
636
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000637 if (Config.DecompressDebugSections &&
638 Config.CompressionType != DebugCompressionType::None) {
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000639 return createStringError(
640 errc::invalid_argument,
641 "Cannot specify --compress-debug-sections at the same time as "
642 "--decompress-debug-sections at the same time");
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000643 }
644
645 if (Config.DecompressDebugSections && !zlib::isAvailable())
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000646 return createStringError(
647 errc::invalid_argument,
648 "LLVM was not compiled with LLVM_ENABLE_ZLIB: cannot decompress");
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000649
Jordan Rupprechtab9f6622018-10-23 20:54:51 +0000650 DC.CopyConfigs.push_back(std::move(Config));
Jordan Rupprecht93ad8b32019-02-21 17:24:55 +0000651 return std::move(DC);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000652}
653
654// ParseStripOptions returns the config and sets the input arguments. If a
655// help flag is set then ParseStripOptions will print the help messege and
656// exit.
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000657Expected<DriverConfig> parseStripOptions(ArrayRef<const char *> ArgsArr) {
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000658 StripOptTable T;
659 unsigned MissingArgumentIndex, MissingArgumentCount;
660 llvm::opt::InputArgList InputArgs =
661 T.ParseArgs(ArgsArr, MissingArgumentIndex, MissingArgumentCount);
662
663 if (InputArgs.size() == 0) {
664 T.PrintHelp(errs(), "llvm-strip [options] file...", "strip tool");
665 exit(1);
666 }
667
668 if (InputArgs.hasArg(STRIP_help)) {
669 T.PrintHelp(outs(), "llvm-strip [options] file...", "strip tool");
670 exit(0);
671 }
672
673 if (InputArgs.hasArg(STRIP_version)) {
Martin Storsjoe9af7152018-11-28 06:51:50 +0000674 outs() << "llvm-strip, compatible with GNU strip\n";
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000675 cl::PrintVersionMessage();
676 exit(0);
677 }
678
679 SmallVector<const char *, 2> Positional;
680 for (auto Arg : InputArgs.filtered(STRIP_UNKNOWN))
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000681 return createStringError(errc::invalid_argument, "unknown argument '%s'",
682 Arg->getAsString(InputArgs).c_str());
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000683 for (auto Arg : InputArgs.filtered(STRIP_INPUT))
684 Positional.push_back(Arg->getValue());
685
686 if (Positional.empty())
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000687 return createStringError(errc::invalid_argument, "No input file specified");
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000688
689 if (Positional.size() > 1 && InputArgs.hasArg(STRIP_output))
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000690 return createStringError(
691 errc::invalid_argument,
692 "Multiple input files cannot be used in combination with -o");
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000693
694 CopyConfig Config;
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000695 bool UseRegexp = InputArgs.hasArg(STRIP_regex);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000696 Config.StripDebug = InputArgs.hasArg(STRIP_strip_debug);
697
Jordan Rupprechtd0f7bcf2019-01-30 14:58:13 +0000698 if (InputArgs.hasArg(STRIP_discard_all, STRIP_discard_locals))
699 Config.DiscardMode =
700 InputArgs.hasFlag(STRIP_discard_all, STRIP_discard_locals)
701 ? DiscardType::All
702 : DiscardType::Locals;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000703 Config.StripUnneeded = InputArgs.hasArg(STRIP_strip_unneeded);
704 Config.StripAll = InputArgs.hasArg(STRIP_strip_all);
Jordan Rupprecht30d1b192018-11-01 17:48:46 +0000705 Config.StripAllGNU = InputArgs.hasArg(STRIP_strip_all_gnu);
Jordan Rupprecht12ed01d2019-03-14 21:51:42 +0000706 Config.OnlyKeepDebug = InputArgs.hasArg(STRIP_only_keep_debug);
Eugene Leviant05a3f992019-02-01 15:25:15 +0000707 Config.KeepFileSymbols = InputArgs.hasArg(STRIP_keep_file_symbols);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000708
Jordan Rupprechtc5bae782018-11-13 19:32:27 +0000709 for (auto Arg : InputArgs.filtered(STRIP_keep_section))
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000710 Config.KeepSection.emplace_back(Arg->getValue(), UseRegexp);
Jordan Rupprecht30d1b192018-11-01 17:48:46 +0000711
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000712 for (auto Arg : InputArgs.filtered(STRIP_remove_section))
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000713 Config.ToRemove.emplace_back(Arg->getValue(), UseRegexp);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000714
Eugene Leviant2267c582019-01-31 12:16:20 +0000715 for (auto Arg : InputArgs.filtered(STRIP_strip_symbol))
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000716 Config.SymbolsToRemove.emplace_back(Arg->getValue(), UseRegexp);
Eugene Leviant2267c582019-01-31 12:16:20 +0000717
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000718 for (auto Arg : InputArgs.filtered(STRIP_keep_symbol))
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000719 Config.SymbolsToKeep.emplace_back(Arg->getValue(), UseRegexp);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000720
Eugene Leviant2267c582019-01-31 12:16:20 +0000721 if (!Config.StripDebug && !Config.StripUnneeded &&
722 Config.DiscardMode == DiscardType::None && !Config.StripAllGNU && Config.SymbolsToRemove.empty())
723 Config.StripAll = true;
724
Jordan Rupprechtfc780bb2018-11-01 17:36:37 +0000725 Config.DeterministicArchives =
726 InputArgs.hasFlag(STRIP_enable_deterministic_archives,
727 STRIP_disable_deterministic_archives, /*default=*/true);
728
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000729 Config.PreserveDates = InputArgs.hasArg(STRIP_preserve_dates);
730
731 DriverConfig DC;
732 if (Positional.size() == 1) {
733 Config.InputFilename = Positional[0];
734 Config.OutputFilename =
735 InputArgs.getLastArgValue(STRIP_output, Positional[0]);
736 DC.CopyConfigs.push_back(std::move(Config));
737 } else {
738 for (const char *Filename : Positional) {
739 Config.InputFilename = Filename;
740 Config.OutputFilename = Filename;
741 DC.CopyConfigs.push_back(Config);
742 }
743 }
744
Jordan Rupprecht93ad8b32019-02-21 17:24:55 +0000745 return std::move(DC);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000746}
747
748} // namespace objcopy
749} // namespace llvm