blob: 59b07828cbd7f5878c34fdc8e9fdd8892edf6899 [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;
Paul Semelee5be792018-04-27 19:09:44 +0000122 std::vector<StringRef> SymbolsToGlobalize;
Alexander Shaposhnikov40e9bdf2018-04-26 18:28:17 +0000123 StringMap<StringRef> SymbolsToRename;
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000124 bool StripAll;
125 bool StripAllGNU;
126 bool StripDebug;
127 bool StripSections;
128 bool StripNonAlloc;
129 bool StripDWO;
130 bool ExtractDWO;
131 bool LocalizeHidden;
132};
Jake Ehrlichf03384d2017-10-11 18:09:18 +0000133
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000134using SectionPred = std::function<bool(const SectionBase &Sec)>;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000135
Jake Ehrlich777fb002017-12-15 20:17:55 +0000136bool IsDWOSection(const SectionBase &Sec) { return Sec.Name.endswith(".dwo"); }
Jake Ehrlich5de70d92017-11-03 18:58:41 +0000137
Jake Ehrlich76e91102018-01-25 22:46:17 +0000138bool OnlyKeepDWOPred(const Object &Obj, const SectionBase &Sec) {
Jake Ehrlich5de70d92017-11-03 18:58:41 +0000139 // We can't remove the section header string table.
Jake Ehrlich76e91102018-01-25 22:46:17 +0000140 if (&Sec == Obj.SectionNames)
Jake Ehrlich5de70d92017-11-03 18:58:41 +0000141 return false;
142 // Short of keeping the string table we want to keep everything that is a DWO
143 // section and remove everything else.
144 return !IsDWOSection(Sec);
145}
146
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000147std::unique_ptr<Writer> CreateWriter(const CopyConfig &Config, Object &Obj,
148 StringRef File, ElfType OutputElfType) {
149 if (Config.OutputFormat == "binary") {
150 return llvm::make_unique<BinaryWriter>(File, Obj);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000151 }
152 // Depending on the initial ELFT and OutputFormat we need a different Writer.
153 switch (OutputElfType) {
154 case ELFT_ELF32LE:
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000155 return llvm::make_unique<ELFWriter<ELF32LE>>(File, Obj,
156 !Config.StripSections);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000157 case ELFT_ELF64LE:
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000158 return llvm::make_unique<ELFWriter<ELF64LE>>(File, Obj,
159 !Config.StripSections);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000160 case ELFT_ELF32BE:
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000161 return llvm::make_unique<ELFWriter<ELF32BE>>(File, Obj,
162 !Config.StripSections);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000163 case ELFT_ELF64BE:
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000164 return llvm::make_unique<ELFWriter<ELF64BE>>(File, Obj,
165 !Config.StripSections);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000166 }
167 llvm_unreachable("Invalid output format");
Jake Ehrlich5de70d92017-11-03 18:58:41 +0000168}
169
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000170void SplitDWOToFile(const CopyConfig &Config, const Reader &Reader,
171 StringRef File, ElfType OutputElfType) {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000172 auto DWOFile = Reader.create();
173 DWOFile->removeSections(
174 [&](const SectionBase &Sec) { return OnlyKeepDWOPred(*DWOFile, Sec); });
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000175 auto Writer = CreateWriter(Config, *DWOFile, File, OutputElfType);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000176 Writer->finalize();
177 Writer->write();
Jake Ehrlich5de70d92017-11-03 18:58:41 +0000178}
179
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000180// This function handles the high level operations of GNU objcopy including
181// handling command line options. It's important to outline certain properties
182// we expect to hold of the command line operations. Any operation that "keeps"
183// should keep regardless of a remove. Additionally any removal should respect
184// any previous removals. Lastly whether or not something is removed shouldn't
185// depend a) on the order the options occur in or b) on some opaque priority
186// system. The only priority is that keeps/copies overrule removes.
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000187void HandleArgs(const CopyConfig &Config, Object &Obj, const Reader &Reader,
188 ElfType OutputElfType) {
Jake Ehrlich5de70d92017-11-03 18:58:41 +0000189
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000190 if (!Config.SplitDWO.empty()) {
191 SplitDWOToFile(Config, Reader, Config.SplitDWO, OutputElfType);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000192 }
Jake Ehrlich5de70d92017-11-03 18:58:41 +0000193
Jake Ehrlichf03384d2017-10-11 18:09:18 +0000194 SectionPred RemovePred = [](const SectionBase &) { return false; };
195
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000196 // Removes:
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000197 if (!Config.ToRemove.empty()) {
198 RemovePred = [&Config](const SectionBase &Sec) {
199 return std::find(std::begin(Config.ToRemove), std::end(Config.ToRemove),
200 Sec.Name) != std::end(Config.ToRemove);
Jake Ehrlichf03384d2017-10-11 18:09:18 +0000201 };
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000202 }
Jake Ehrlichf03384d2017-10-11 18:09:18 +0000203
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000204 if (Config.StripDWO || !Config.SplitDWO.empty())
David Blaikie998ff812017-11-03 20:57:09 +0000205 RemovePred = [RemovePred](const SectionBase &Sec) {
Jake Ehrlich5de70d92017-11-03 18:58:41 +0000206 return IsDWOSection(Sec) || RemovePred(Sec);
207 };
208
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000209 if (Config.ExtractDWO)
Jake Ehrlich5de70d92017-11-03 18:58:41 +0000210 RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000211 return OnlyKeepDWOPred(Obj, Sec) || RemovePred(Sec);
Jake Ehrlich5de70d92017-11-03 18:58:41 +0000212 };
213
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000214 if (Config.StripAllGNU)
Jake Ehrlichfabddf12017-11-13 22:02:07 +0000215 RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
216 if (RemovePred(Sec))
217 return true;
218 if ((Sec.Flags & SHF_ALLOC) != 0)
219 return false;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000220 if (&Sec == Obj.SectionNames)
Jake Ehrlichfabddf12017-11-13 22:02:07 +0000221 return false;
Jake Ehrlich777fb002017-12-15 20:17:55 +0000222 switch (Sec.Type) {
Jake Ehrlichfabddf12017-11-13 22:02:07 +0000223 case SHT_SYMTAB:
224 case SHT_REL:
225 case SHT_RELA:
226 case SHT_STRTAB:
227 return true;
228 }
229 return Sec.Name.startswith(".debug");
230 };
231
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000232 if (Config.StripSections) {
Jake Ehrlichf03384d2017-10-11 18:09:18 +0000233 RemovePred = [RemovePred](const SectionBase &Sec) {
234 return RemovePred(Sec) || (Sec.Flags & SHF_ALLOC) == 0;
235 };
Jake Ehrlichf03384d2017-10-11 18:09:18 +0000236 }
237
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000238 if (Config.StripDebug) {
Jake Ehrlich1bfefc12017-11-13 22:13:08 +0000239 RemovePred = [RemovePred](const SectionBase &Sec) {
240 return RemovePred(Sec) || Sec.Name.startswith(".debug");
241 };
242 }
243
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000244 if (Config.StripNonAlloc)
Jake Ehrlichd56725a2017-11-14 18:50:24 +0000245 RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
246 if (RemovePred(Sec))
247 return true;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000248 if (&Sec == Obj.SectionNames)
Jake Ehrlichd56725a2017-11-14 18:50:24 +0000249 return false;
250 return (Sec.Flags & SHF_ALLOC) == 0;
251 };
252
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000253 if (Config.StripAll)
Jake Ehrlich6ad72d02017-11-27 18:56:01 +0000254 RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
255 if (RemovePred(Sec))
256 return true;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000257 if (&Sec == Obj.SectionNames)
Jake Ehrlich6ad72d02017-11-27 18:56:01 +0000258 return false;
259 if (Sec.Name.startswith(".gnu.warning"))
260 return false;
261 return (Sec.Flags & SHF_ALLOC) == 0;
262 };
263
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000264 // Explicit copies:
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000265 if (!Config.OnlyKeep.empty()) {
266 RemovePred = [&Config, RemovePred, &Obj](const SectionBase &Sec) {
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000267 // Explicitly keep these sections regardless of previous removes.
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000268 if (std::find(std::begin(Config.OnlyKeep), std::end(Config.OnlyKeep),
269 Sec.Name) != std::end(Config.OnlyKeep))
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000270 return false;
271
272 // Allow all implicit removes.
Jake Ehrlich76e91102018-01-25 22:46:17 +0000273 if (RemovePred(Sec))
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000274 return true;
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000275
276 // Keep special sections.
Jake Ehrlich76e91102018-01-25 22:46:17 +0000277 if (Obj.SectionNames == &Sec)
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000278 return false;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000279 if (Obj.SymbolTable == &Sec || Obj.SymbolTable->getStrTab() == &Sec)
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000280 return false;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000281
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000282 // Remove everything else.
283 return true;
284 };
285 }
286
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000287 if (!Config.Keep.empty()) {
288 RemovePred = [Config, RemovePred](const SectionBase &Sec) {
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000289 // Explicitly keep these sections regardless of previous removes.
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000290 if (std::find(std::begin(Config.Keep), std::end(Config.Keep), Sec.Name) !=
291 std::end(Config.Keep))
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000292 return false;
293 // Otherwise defer to RemovePred.
294 return RemovePred(Sec);
295 };
296 }
297
Jake Ehrlich76e91102018-01-25 22:46:17 +0000298 Obj.removeSections(RemovePred);
Jake Ehrliche8437de2017-12-19 00:47:30 +0000299
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000300 if (!Config.AddSection.empty()) {
301 for (const auto &Flag : Config.AddSection) {
302 auto SecPair = Flag.split("=");
Jake Ehrliche8437de2017-12-19 00:47:30 +0000303 auto SecName = SecPair.first;
304 auto File = SecPair.second;
305 auto BufOrErr = MemoryBuffer::getFile(File);
306 if (!BufOrErr)
307 reportError(File, BufOrErr.getError());
308 auto Buf = std::move(*BufOrErr);
309 auto BufPtr = reinterpret_cast<const uint8_t *>(Buf->getBufferStart());
310 auto BufSize = Buf->getBufferSize();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000311 Obj.addSection<OwnedDataSection>(SecName,
312 ArrayRef<uint8_t>(BufPtr, BufSize));
Jake Ehrliche8437de2017-12-19 00:47:30 +0000313 }
314 }
315
Alexander Shaposhnikov40e9bdf2018-04-26 18:28:17 +0000316 if (!Config.AddGnuDebugLink.empty())
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000317 Obj.addSection<GnuDebugLinkSection>(Config.AddGnuDebugLink);
Alexander Shaposhnikov40e9bdf2018-04-26 18:28:17 +0000318
319 if (Obj.SymbolTable) {
320 Obj.SymbolTable->updateSymbols([&](Symbol &Sym) {
321 if ((Config.LocalizeHidden &&
322 (Sym.Visibility == STV_HIDDEN || Sym.Visibility == STV_INTERNAL)) ||
323 (!Config.SymbolsToLocalize.empty() &&
Paul Semelee5be792018-04-27 19:09:44 +0000324 is_contained(Config.SymbolsToLocalize, Sym.Name)))
Alexander Shaposhnikov40e9bdf2018-04-26 18:28:17 +0000325 Sym.Binding = STB_LOCAL;
326
Paul Semelee5be792018-04-27 19:09:44 +0000327 if (!Config.SymbolsToGlobalize.empty() &&
328 is_contained(Config.SymbolsToGlobalize, Sym.Name))
329 Sym.Binding = STB_GLOBAL;
330
Alexander Shaposhnikov40e9bdf2018-04-26 18:28:17 +0000331 const auto I = Config.SymbolsToRename.find(Sym.Name);
332 if (I != Config.SymbolsToRename.end())
333 Sym.Name = I->getValue();
334 });
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000335 }
Jake Ehrlich76e91102018-01-25 22:46:17 +0000336}
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000337
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000338std::unique_ptr<Reader> CreateReader(StringRef InputFilename,
339 ElfType &OutputElfType) {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000340 // Right now we can only read ELF files so there's only one reader;
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000341 auto Out = llvm::make_unique<ELFReader>(InputFilename);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000342 // We need to set the default ElfType for output.
343 OutputElfType = Out->getElfType();
344 return std::move(Out);
Petr Hosek05a04cb2017-08-01 00:33:58 +0000345}
346
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000347void ExecuteElfObjcopy(const CopyConfig &Config) {
348 ElfType OutputElfType;
349 auto Reader = CreateReader(Config.InputFilename, OutputElfType);
Jake Ehrlicha8c689e2018-04-12 00:40:50 +0000350 auto Obj = Reader->create();
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000351 auto Writer =
352 CreateWriter(Config, *Obj, Config.OutputFilename, OutputElfType);
353 HandleArgs(Config, *Obj, *Reader, OutputElfType);
Jake Ehrlicha8c689e2018-04-12 00:40:50 +0000354 Writer->finalize();
355 Writer->write();
Petr Hosek05a04cb2017-08-01 00:33:58 +0000356}
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000357
358// ParseObjcopyOptions returns the config and sets the input arguments. If a
359// help flag is set then ParseObjcopyOptions will print the help messege and
360// exit.
361CopyConfig ParseObjcopyOptions(ArrayRef<const char *> ArgsArr) {
362 ObjcopyOptTable T;
363 unsigned MissingArgumentIndex, MissingArgumentCount;
364 llvm::opt::InputArgList InputArgs =
365 T.ParseArgs(ArgsArr, MissingArgumentIndex, MissingArgumentCount);
366
367 if (InputArgs.size() == 0 || InputArgs.hasArg(OBJCOPY_help)) {
368 T.PrintHelp(outs(), "llvm-objcopy <input> [ <output> ]", "objcopy tool");
369 exit(0);
370 }
371
372 SmallVector<const char *, 2> Positional;
373
374 for (auto Arg : InputArgs.filtered(OBJCOPY_UNKNOWN))
375 error("unknown argument '" + Arg->getAsString(InputArgs) + "'");
376
377 for (auto Arg : InputArgs.filtered(OBJCOPY_INPUT))
378 Positional.push_back(Arg->getValue());
379
380 if (Positional.empty())
381 error("No input file specified");
382
383 if (Positional.size() > 2)
384 error("Too many positional arguments");
385
386 CopyConfig Config;
387 Config.InputFilename = Positional[0];
388 Config.OutputFilename = Positional[Positional.size() == 1 ? 0 : 1];
389 Config.InputFormat = InputArgs.getLastArgValue(OBJCOPY_input_target);
390 Config.OutputFormat = InputArgs.getLastArgValue(OBJCOPY_output_target);
391 Config.BinaryArch = InputArgs.getLastArgValue(OBJCOPY_binary_architecture);
392
393 Config.SplitDWO = InputArgs.getLastArgValue(OBJCOPY_split_dwo);
394 Config.AddGnuDebugLink = InputArgs.getLastArgValue(OBJCOPY_add_gnu_debuglink);
Alexander Shaposhnikov40e9bdf2018-04-26 18:28:17 +0000395
396 for (auto Arg : InputArgs.filtered(OBJCOPY_redefine_symbol)) {
397 if (!StringRef(Arg->getValue()).contains('='))
398 error("Bad format for --redefine-sym");
399 auto Old2New = StringRef(Arg->getValue()).split('=');
400 if (!Config.SymbolsToRename.insert(Old2New).second)
401 error("Multiple redefinition of symbol " + Old2New.first);
402 }
403
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000404 for (auto Arg : InputArgs.filtered(OBJCOPY_remove_section))
405 Config.ToRemove.push_back(Arg->getValue());
406 for (auto Arg : InputArgs.filtered(OBJCOPY_keep))
407 Config.Keep.push_back(Arg->getValue());
408 for (auto Arg : InputArgs.filtered(OBJCOPY_only_keep))
409 Config.OnlyKeep.push_back(Arg->getValue());
410 for (auto Arg : InputArgs.filtered(OBJCOPY_add_section))
411 Config.AddSection.push_back(Arg->getValue());
412 Config.StripAll = InputArgs.hasArg(OBJCOPY_strip_all);
413 Config.StripAllGNU = InputArgs.hasArg(OBJCOPY_strip_all_gnu);
414 Config.StripDebug = InputArgs.hasArg(OBJCOPY_strip_debug);
415 Config.StripDWO = InputArgs.hasArg(OBJCOPY_strip_dwo);
416 Config.StripSections = InputArgs.hasArg(OBJCOPY_strip_sections);
417 Config.StripNonAlloc = InputArgs.hasArg(OBJCOPY_strip_non_alloc);
418 Config.ExtractDWO = InputArgs.hasArg(OBJCOPY_extract_dwo);
419 Config.LocalizeHidden = InputArgs.hasArg(OBJCOPY_localize_hidden);
Paul Semelb4924942018-04-26 17:44:43 +0000420 for (auto Arg : InputArgs.filtered(OBJCOPY_localize_symbol))
Alexander Shaposhnikov40e9bdf2018-04-26 18:28:17 +0000421 Config.SymbolsToLocalize.push_back(Arg->getValue());
Paul Semelee5be792018-04-27 19:09:44 +0000422 for (auto Arg : InputArgs.filtered(OBJCOPY_globalize_symbol))
423 Config.SymbolsToGlobalize.push_back(Arg->getValue());
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000424
425 return Config;
426}
427
428int main(int argc, char **argv) {
429 InitLLVM X(argc, argv);
430 ToolName = argv[0];
431
432 CopyConfig Config = ParseObjcopyOptions(makeArrayRef(argv + 1, argc));
433 ExecuteElfObjcopy(Config);
434}