blob: a4b38cfaf2e35dbce65a9941ffa74dc957d320ad [file] [log] [blame]
Eugene Zelenko0ad18f82017-11-01 21:16:06 +00001//===- llvm-objcopy.cpp ---------------------------------------------------===//
Petr Hosek05a04cb2017-08-01 00:33:58 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Eugene Zelenko0ad18f82017-11-01 21:16:06 +00009
Petr Hosek05a04cb2017-08-01 00:33:58 +000010#include "llvm-objcopy.h"
11#include "Object.h"
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000012#include "llvm/ADT/STLExtras.h"
13#include "llvm/ADT/StringRef.h"
14#include "llvm/ADT/Twine.h"
15#include "llvm/BinaryFormat/ELF.h"
16#include "llvm/Object/Binary.h"
17#include "llvm/Object/ELFObjectFile.h"
18#include "llvm/Object/ELFTypes.h"
19#include "llvm/Object/Error.h"
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +000020#include "llvm/Option/Arg.h"
21#include "llvm/Option/ArgList.h"
22#include "llvm/Option/Option.h"
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000023#include "llvm/Support/Casting.h"
Petr Hosek05a04cb2017-08-01 00:33:58 +000024#include "llvm/Support/CommandLine.h"
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000025#include "llvm/Support/Compiler.h"
26#include "llvm/Support/Error.h"
27#include "llvm/Support/ErrorHandling.h"
28#include "llvm/Support/ErrorOr.h"
Petr Hosek05a04cb2017-08-01 00:33:58 +000029#include "llvm/Support/FileOutputBuffer.h"
Rui Ueyama197194b2018-04-13 18:26:06 +000030#include "llvm/Support/InitLLVM.h"
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000031#include "llvm/Support/raw_ostream.h"
32#include <algorithm>
33#include <cassert>
34#include <cstdlib>
35#include <functional>
36#include <iterator>
Petr Hosek05a04cb2017-08-01 00:33:58 +000037#include <memory>
38#include <string>
39#include <system_error>
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000040#include <utility>
Petr Hosek05a04cb2017-08-01 00:33:58 +000041
42using namespace llvm;
43using namespace object;
44using namespace ELF;
45
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +000046namespace {
47
48enum ID {
49 OBJCOPY_INVALID = 0, // This is not an option ID.
50#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
51 HELPTEXT, METAVAR, VALUES) \
52 OBJCOPY_##ID,
53#include "Opts.inc"
54#undef OPTION
55};
56
57#define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
58#include "Opts.inc"
59#undef PREFIX
60
Alexander Shaposhnikov3326e782018-04-24 06:23:22 +000061static const opt::OptTable::Info ObjcopyInfoTable[] = {
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +000062#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
63 HELPTEXT, METAVAR, VALUES) \
64 {PREFIX, NAME, HELPTEXT, \
65 METAVAR, OBJCOPY_##ID, opt::Option::KIND##Class, \
66 PARAM, FLAGS, OBJCOPY_##GROUP, \
67 OBJCOPY_##ALIAS, ALIASARGS, VALUES},
68#include "Opts.inc"
69#undef OPTION
70};
71
72class ObjcopyOptTable : public opt::OptTable {
73public:
74 ObjcopyOptTable() : OptTable(ObjcopyInfoTable, true) {}
75};
76
77} // namespace
78
Petr Hosek05a04cb2017-08-01 00:33:58 +000079// The name this program was invoked as.
80static StringRef ToolName;
81
82namespace llvm {
83
Zachary Turner41a9ee92017-10-11 23:54:34 +000084LLVM_ATTRIBUTE_NORETURN void error(Twine Message) {
Petr Hosek05a04cb2017-08-01 00:33:58 +000085 errs() << ToolName << ": " << Message << ".\n";
86 errs().flush();
87 exit(1);
88}
89
90LLVM_ATTRIBUTE_NORETURN void reportError(StringRef File, std::error_code EC) {
91 assert(EC);
92 errs() << ToolName << ": '" << File << "': " << EC.message() << ".\n";
93 exit(1);
94}
95
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000096LLVM_ATTRIBUTE_NORETURN void reportError(StringRef File, Error E) {
Petr Hosek05a04cb2017-08-01 00:33:58 +000097 assert(E);
98 std::string Buf;
99 raw_string_ostream OS(Buf);
100 logAllUnhandledErrors(std::move(E), OS, "");
101 OS.flush();
102 errs() << ToolName << ": '" << File << "': " << Buf;
103 exit(1);
104}
Petr Hosek05a04cb2017-08-01 00:33:58 +0000105
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000106} // end namespace llvm
107
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000108struct CopyConfig {
109 StringRef OutputFilename;
110 StringRef InputFilename;
111 StringRef OutputFormat;
112 StringRef InputFormat;
113 StringRef BinaryArch;
Alexander Shaposhnikovfedb0162018-02-09 23:33:31 +0000114
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000115 StringRef SplitDWO;
116 StringRef AddGnuDebugLink;
117 std::vector<StringRef> ToRemove;
118 std::vector<StringRef> Keep;
119 std::vector<StringRef> OnlyKeep;
120 std::vector<StringRef> AddSection;
Alexander Shaposhnikov40e9bdf2018-04-26 18:28:17 +0000121 std::vector<StringRef> SymbolsToLocalize;
122 StringMap<StringRef> SymbolsToRename;
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000123 bool StripAll;
124 bool StripAllGNU;
125 bool StripDebug;
126 bool StripSections;
127 bool StripNonAlloc;
128 bool StripDWO;
129 bool ExtractDWO;
130 bool LocalizeHidden;
131};
Jake Ehrlichf03384d2017-10-11 18:09:18 +0000132
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000133using SectionPred = std::function<bool(const SectionBase &Sec)>;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000134
Jake Ehrlich777fb002017-12-15 20:17:55 +0000135bool IsDWOSection(const SectionBase &Sec) { return Sec.Name.endswith(".dwo"); }
Jake Ehrlich5de70d92017-11-03 18:58:41 +0000136
Jake Ehrlich76e91102018-01-25 22:46:17 +0000137bool OnlyKeepDWOPred(const Object &Obj, const SectionBase &Sec) {
Jake Ehrlich5de70d92017-11-03 18:58:41 +0000138 // We can't remove the section header string table.
Jake Ehrlich76e91102018-01-25 22:46:17 +0000139 if (&Sec == Obj.SectionNames)
Jake Ehrlich5de70d92017-11-03 18:58:41 +0000140 return false;
141 // Short of keeping the string table we want to keep everything that is a DWO
142 // section and remove everything else.
143 return !IsDWOSection(Sec);
144}
145
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000146std::unique_ptr<Writer> CreateWriter(const CopyConfig &Config, Object &Obj,
147 StringRef File, ElfType OutputElfType) {
148 if (Config.OutputFormat == "binary") {
149 return llvm::make_unique<BinaryWriter>(File, Obj);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000150 }
151 // Depending on the initial ELFT and OutputFormat we need a different Writer.
152 switch (OutputElfType) {
153 case ELFT_ELF32LE:
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000154 return llvm::make_unique<ELFWriter<ELF32LE>>(File, Obj,
155 !Config.StripSections);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000156 case ELFT_ELF64LE:
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000157 return llvm::make_unique<ELFWriter<ELF64LE>>(File, Obj,
158 !Config.StripSections);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000159 case ELFT_ELF32BE:
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000160 return llvm::make_unique<ELFWriter<ELF32BE>>(File, Obj,
161 !Config.StripSections);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000162 case ELFT_ELF64BE:
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000163 return llvm::make_unique<ELFWriter<ELF64BE>>(File, Obj,
164 !Config.StripSections);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000165 }
166 llvm_unreachable("Invalid output format");
Jake Ehrlich5de70d92017-11-03 18:58:41 +0000167}
168
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000169void SplitDWOToFile(const CopyConfig &Config, const Reader &Reader,
170 StringRef File, ElfType OutputElfType) {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000171 auto DWOFile = Reader.create();
172 DWOFile->removeSections(
173 [&](const SectionBase &Sec) { return OnlyKeepDWOPred(*DWOFile, Sec); });
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000174 auto Writer = CreateWriter(Config, *DWOFile, File, OutputElfType);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000175 Writer->finalize();
176 Writer->write();
Jake Ehrlich5de70d92017-11-03 18:58:41 +0000177}
178
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000179// This function handles the high level operations of GNU objcopy including
180// handling command line options. It's important to outline certain properties
181// we expect to hold of the command line operations. Any operation that "keeps"
182// should keep regardless of a remove. Additionally any removal should respect
183// any previous removals. Lastly whether or not something is removed shouldn't
184// depend a) on the order the options occur in or b) on some opaque priority
185// system. The only priority is that keeps/copies overrule removes.
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000186void HandleArgs(const CopyConfig &Config, Object &Obj, const Reader &Reader,
187 ElfType OutputElfType) {
Jake Ehrlich5de70d92017-11-03 18:58:41 +0000188
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000189 if (!Config.SplitDWO.empty()) {
190 SplitDWOToFile(Config, Reader, Config.SplitDWO, OutputElfType);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000191 }
Jake Ehrlich5de70d92017-11-03 18:58:41 +0000192
Jake Ehrlichf03384d2017-10-11 18:09:18 +0000193 SectionPred RemovePred = [](const SectionBase &) { return false; };
194
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000195 // Removes:
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000196 if (!Config.ToRemove.empty()) {
197 RemovePred = [&Config](const SectionBase &Sec) {
198 return std::find(std::begin(Config.ToRemove), std::end(Config.ToRemove),
199 Sec.Name) != std::end(Config.ToRemove);
Jake Ehrlichf03384d2017-10-11 18:09:18 +0000200 };
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000201 }
Jake Ehrlichf03384d2017-10-11 18:09:18 +0000202
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000203 if (Config.StripDWO || !Config.SplitDWO.empty())
David Blaikie998ff812017-11-03 20:57:09 +0000204 RemovePred = [RemovePred](const SectionBase &Sec) {
Jake Ehrlich5de70d92017-11-03 18:58:41 +0000205 return IsDWOSection(Sec) || RemovePred(Sec);
206 };
207
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000208 if (Config.ExtractDWO)
Jake Ehrlich5de70d92017-11-03 18:58:41 +0000209 RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000210 return OnlyKeepDWOPred(Obj, Sec) || RemovePred(Sec);
Jake Ehrlich5de70d92017-11-03 18:58:41 +0000211 };
212
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000213 if (Config.StripAllGNU)
Jake Ehrlichfabddf12017-11-13 22:02:07 +0000214 RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
215 if (RemovePred(Sec))
216 return true;
217 if ((Sec.Flags & SHF_ALLOC) != 0)
218 return false;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000219 if (&Sec == Obj.SectionNames)
Jake Ehrlichfabddf12017-11-13 22:02:07 +0000220 return false;
Jake Ehrlich777fb002017-12-15 20:17:55 +0000221 switch (Sec.Type) {
Jake Ehrlichfabddf12017-11-13 22:02:07 +0000222 case SHT_SYMTAB:
223 case SHT_REL:
224 case SHT_RELA:
225 case SHT_STRTAB:
226 return true;
227 }
228 return Sec.Name.startswith(".debug");
229 };
230
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000231 if (Config.StripSections) {
Jake Ehrlichf03384d2017-10-11 18:09:18 +0000232 RemovePred = [RemovePred](const SectionBase &Sec) {
233 return RemovePred(Sec) || (Sec.Flags & SHF_ALLOC) == 0;
234 };
Jake Ehrlichf03384d2017-10-11 18:09:18 +0000235 }
236
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000237 if (Config.StripDebug) {
Jake Ehrlich1bfefc12017-11-13 22:13:08 +0000238 RemovePred = [RemovePred](const SectionBase &Sec) {
239 return RemovePred(Sec) || Sec.Name.startswith(".debug");
240 };
241 }
242
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000243 if (Config.StripNonAlloc)
Jake Ehrlichd56725a2017-11-14 18:50:24 +0000244 RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
245 if (RemovePred(Sec))
246 return true;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000247 if (&Sec == Obj.SectionNames)
Jake Ehrlichd56725a2017-11-14 18:50:24 +0000248 return false;
249 return (Sec.Flags & SHF_ALLOC) == 0;
250 };
251
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000252 if (Config.StripAll)
Jake Ehrlich6ad72d02017-11-27 18:56:01 +0000253 RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
254 if (RemovePred(Sec))
255 return true;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000256 if (&Sec == Obj.SectionNames)
Jake Ehrlich6ad72d02017-11-27 18:56:01 +0000257 return false;
258 if (Sec.Name.startswith(".gnu.warning"))
259 return false;
260 return (Sec.Flags & SHF_ALLOC) == 0;
261 };
262
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000263 // Explicit copies:
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000264 if (!Config.OnlyKeep.empty()) {
265 RemovePred = [&Config, RemovePred, &Obj](const SectionBase &Sec) {
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000266 // Explicitly keep these sections regardless of previous removes.
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000267 if (std::find(std::begin(Config.OnlyKeep), std::end(Config.OnlyKeep),
268 Sec.Name) != std::end(Config.OnlyKeep))
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000269 return false;
270
271 // Allow all implicit removes.
Jake Ehrlich76e91102018-01-25 22:46:17 +0000272 if (RemovePred(Sec))
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000273 return true;
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000274
275 // Keep special sections.
Jake Ehrlich76e91102018-01-25 22:46:17 +0000276 if (Obj.SectionNames == &Sec)
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000277 return false;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000278 if (Obj.SymbolTable == &Sec || Obj.SymbolTable->getStrTab() == &Sec)
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000279 return false;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000280
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000281 // Remove everything else.
282 return true;
283 };
284 }
285
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000286 if (!Config.Keep.empty()) {
287 RemovePred = [Config, RemovePred](const SectionBase &Sec) {
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000288 // Explicitly keep these sections regardless of previous removes.
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000289 if (std::find(std::begin(Config.Keep), std::end(Config.Keep), Sec.Name) !=
290 std::end(Config.Keep))
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000291 return false;
292 // Otherwise defer to RemovePred.
293 return RemovePred(Sec);
294 };
295 }
296
Jake Ehrlich76e91102018-01-25 22:46:17 +0000297 Obj.removeSections(RemovePred);
Jake Ehrliche8437de2017-12-19 00:47:30 +0000298
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000299 if (!Config.AddSection.empty()) {
300 for (const auto &Flag : Config.AddSection) {
301 auto SecPair = Flag.split("=");
Jake Ehrliche8437de2017-12-19 00:47:30 +0000302 auto SecName = SecPair.first;
303 auto File = SecPair.second;
304 auto BufOrErr = MemoryBuffer::getFile(File);
305 if (!BufOrErr)
306 reportError(File, BufOrErr.getError());
307 auto Buf = std::move(*BufOrErr);
308 auto BufPtr = reinterpret_cast<const uint8_t *>(Buf->getBufferStart());
309 auto BufSize = Buf->getBufferSize();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000310 Obj.addSection<OwnedDataSection>(SecName,
311 ArrayRef<uint8_t>(BufPtr, BufSize));
Jake Ehrliche8437de2017-12-19 00:47:30 +0000312 }
313 }
314
Alexander Shaposhnikov40e9bdf2018-04-26 18:28:17 +0000315 if (!Config.AddGnuDebugLink.empty())
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000316 Obj.addSection<GnuDebugLinkSection>(Config.AddGnuDebugLink);
Alexander Shaposhnikov40e9bdf2018-04-26 18:28:17 +0000317
318 if (Obj.SymbolTable) {
319 Obj.SymbolTable->updateSymbols([&](Symbol &Sym) {
320 if ((Config.LocalizeHidden &&
321 (Sym.Visibility == STV_HIDDEN || Sym.Visibility == STV_INTERNAL)) ||
322 (!Config.SymbolsToLocalize.empty() &&
323 std::find(std::begin(Config.SymbolsToLocalize),
324 std::end(Config.SymbolsToLocalize),
325 Sym.Name) != std::end(Config.SymbolsToLocalize)))
326 Sym.Binding = STB_LOCAL;
327
328 const auto I = Config.SymbolsToRename.find(Sym.Name);
329 if (I != Config.SymbolsToRename.end())
330 Sym.Name = I->getValue();
331 });
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000332 }
Jake Ehrlich76e91102018-01-25 22:46:17 +0000333}
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000334
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000335std::unique_ptr<Reader> CreateReader(StringRef InputFilename,
336 ElfType &OutputElfType) {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000337 // Right now we can only read ELF files so there's only one reader;
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000338 auto Out = llvm::make_unique<ELFReader>(InputFilename);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000339 // We need to set the default ElfType for output.
340 OutputElfType = Out->getElfType();
341 return std::move(Out);
Petr Hosek05a04cb2017-08-01 00:33:58 +0000342}
343
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000344void ExecuteElfObjcopy(const CopyConfig &Config) {
345 ElfType OutputElfType;
346 auto Reader = CreateReader(Config.InputFilename, OutputElfType);
Jake Ehrlicha8c689e2018-04-12 00:40:50 +0000347 auto Obj = Reader->create();
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000348 auto Writer =
349 CreateWriter(Config, *Obj, Config.OutputFilename, OutputElfType);
350 HandleArgs(Config, *Obj, *Reader, OutputElfType);
Jake Ehrlicha8c689e2018-04-12 00:40:50 +0000351 Writer->finalize();
352 Writer->write();
Petr Hosek05a04cb2017-08-01 00:33:58 +0000353}
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000354
355// ParseObjcopyOptions returns the config and sets the input arguments. If a
356// help flag is set then ParseObjcopyOptions will print the help messege and
357// exit.
358CopyConfig ParseObjcopyOptions(ArrayRef<const char *> ArgsArr) {
359 ObjcopyOptTable T;
360 unsigned MissingArgumentIndex, MissingArgumentCount;
361 llvm::opt::InputArgList InputArgs =
362 T.ParseArgs(ArgsArr, MissingArgumentIndex, MissingArgumentCount);
363
364 if (InputArgs.size() == 0 || InputArgs.hasArg(OBJCOPY_help)) {
365 T.PrintHelp(outs(), "llvm-objcopy <input> [ <output> ]", "objcopy tool");
366 exit(0);
367 }
368
369 SmallVector<const char *, 2> Positional;
370
371 for (auto Arg : InputArgs.filtered(OBJCOPY_UNKNOWN))
372 error("unknown argument '" + Arg->getAsString(InputArgs) + "'");
373
374 for (auto Arg : InputArgs.filtered(OBJCOPY_INPUT))
375 Positional.push_back(Arg->getValue());
376
377 if (Positional.empty())
378 error("No input file specified");
379
380 if (Positional.size() > 2)
381 error("Too many positional arguments");
382
383 CopyConfig Config;
384 Config.InputFilename = Positional[0];
385 Config.OutputFilename = Positional[Positional.size() == 1 ? 0 : 1];
386 Config.InputFormat = InputArgs.getLastArgValue(OBJCOPY_input_target);
387 Config.OutputFormat = InputArgs.getLastArgValue(OBJCOPY_output_target);
388 Config.BinaryArch = InputArgs.getLastArgValue(OBJCOPY_binary_architecture);
389
390 Config.SplitDWO = InputArgs.getLastArgValue(OBJCOPY_split_dwo);
391 Config.AddGnuDebugLink = InputArgs.getLastArgValue(OBJCOPY_add_gnu_debuglink);
Alexander Shaposhnikov40e9bdf2018-04-26 18:28:17 +0000392
393 for (auto Arg : InputArgs.filtered(OBJCOPY_redefine_symbol)) {
394 if (!StringRef(Arg->getValue()).contains('='))
395 error("Bad format for --redefine-sym");
396 auto Old2New = StringRef(Arg->getValue()).split('=');
397 if (!Config.SymbolsToRename.insert(Old2New).second)
398 error("Multiple redefinition of symbol " + Old2New.first);
399 }
400
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000401 for (auto Arg : InputArgs.filtered(OBJCOPY_remove_section))
402 Config.ToRemove.push_back(Arg->getValue());
403 for (auto Arg : InputArgs.filtered(OBJCOPY_keep))
404 Config.Keep.push_back(Arg->getValue());
405 for (auto Arg : InputArgs.filtered(OBJCOPY_only_keep))
406 Config.OnlyKeep.push_back(Arg->getValue());
407 for (auto Arg : InputArgs.filtered(OBJCOPY_add_section))
408 Config.AddSection.push_back(Arg->getValue());
409 Config.StripAll = InputArgs.hasArg(OBJCOPY_strip_all);
410 Config.StripAllGNU = InputArgs.hasArg(OBJCOPY_strip_all_gnu);
411 Config.StripDebug = InputArgs.hasArg(OBJCOPY_strip_debug);
412 Config.StripDWO = InputArgs.hasArg(OBJCOPY_strip_dwo);
413 Config.StripSections = InputArgs.hasArg(OBJCOPY_strip_sections);
414 Config.StripNonAlloc = InputArgs.hasArg(OBJCOPY_strip_non_alloc);
415 Config.ExtractDWO = InputArgs.hasArg(OBJCOPY_extract_dwo);
416 Config.LocalizeHidden = InputArgs.hasArg(OBJCOPY_localize_hidden);
Paul Semelb4924942018-04-26 17:44:43 +0000417 for (auto Arg : InputArgs.filtered(OBJCOPY_localize_symbol))
Alexander Shaposhnikov40e9bdf2018-04-26 18:28:17 +0000418 Config.SymbolsToLocalize.push_back(Arg->getValue());
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000419
420 return Config;
421}
422
423int main(int argc, char **argv) {
424 InitLLVM X(argc, argv);
425 ToolName = argv[0];
426
427 CopyConfig Config = ParseObjcopyOptions(makeArrayRef(argv + 1, argc));
428 ExecuteElfObjcopy(Config);
429}