blob: 4dd8d36ee0882681c76f542f627165f19269dbf6 [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;
Paul Semel3a8a56b2018-04-27 19:16:27 +0000123 std::vector<StringRef> SymbolsToWeaken;
Alexander Shaposhnikov40e9bdf2018-04-26 18:28:17 +0000124 StringMap<StringRef> SymbolsToRename;
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000125 bool StripAll;
126 bool StripAllGNU;
127 bool StripDebug;
128 bool StripSections;
129 bool StripNonAlloc;
130 bool StripDWO;
131 bool ExtractDWO;
132 bool LocalizeHidden;
133};
Jake Ehrlichf03384d2017-10-11 18:09:18 +0000134
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000135using SectionPred = std::function<bool(const SectionBase &Sec)>;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000136
Jake Ehrlich777fb002017-12-15 20:17:55 +0000137bool IsDWOSection(const SectionBase &Sec) { return Sec.Name.endswith(".dwo"); }
Jake Ehrlich5de70d92017-11-03 18:58:41 +0000138
Jake Ehrlich76e91102018-01-25 22:46:17 +0000139bool OnlyKeepDWOPred(const Object &Obj, const SectionBase &Sec) {
Jake Ehrlich5de70d92017-11-03 18:58:41 +0000140 // We can't remove the section header string table.
Jake Ehrlich76e91102018-01-25 22:46:17 +0000141 if (&Sec == Obj.SectionNames)
Jake Ehrlich5de70d92017-11-03 18:58:41 +0000142 return false;
143 // Short of keeping the string table we want to keep everything that is a DWO
144 // section and remove everything else.
145 return !IsDWOSection(Sec);
146}
147
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000148std::unique_ptr<Writer> CreateWriter(const CopyConfig &Config, Object &Obj,
149 StringRef File, ElfType OutputElfType) {
150 if (Config.OutputFormat == "binary") {
151 return llvm::make_unique<BinaryWriter>(File, Obj);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000152 }
153 // Depending on the initial ELFT and OutputFormat we need a different Writer.
154 switch (OutputElfType) {
155 case ELFT_ELF32LE:
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000156 return llvm::make_unique<ELFWriter<ELF32LE>>(File, Obj,
157 !Config.StripSections);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000158 case ELFT_ELF64LE:
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000159 return llvm::make_unique<ELFWriter<ELF64LE>>(File, Obj,
160 !Config.StripSections);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000161 case ELFT_ELF32BE:
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000162 return llvm::make_unique<ELFWriter<ELF32BE>>(File, Obj,
163 !Config.StripSections);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000164 case ELFT_ELF64BE:
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000165 return llvm::make_unique<ELFWriter<ELF64BE>>(File, Obj,
166 !Config.StripSections);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000167 }
168 llvm_unreachable("Invalid output format");
Jake Ehrlich5de70d92017-11-03 18:58:41 +0000169}
170
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000171void SplitDWOToFile(const CopyConfig &Config, const Reader &Reader,
172 StringRef File, ElfType OutputElfType) {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000173 auto DWOFile = Reader.create();
174 DWOFile->removeSections(
175 [&](const SectionBase &Sec) { return OnlyKeepDWOPred(*DWOFile, Sec); });
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000176 auto Writer = CreateWriter(Config, *DWOFile, File, OutputElfType);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000177 Writer->finalize();
178 Writer->write();
Jake Ehrlich5de70d92017-11-03 18:58:41 +0000179}
180
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000181// This function handles the high level operations of GNU objcopy including
182// handling command line options. It's important to outline certain properties
183// we expect to hold of the command line operations. Any operation that "keeps"
184// should keep regardless of a remove. Additionally any removal should respect
185// any previous removals. Lastly whether or not something is removed shouldn't
186// depend a) on the order the options occur in or b) on some opaque priority
187// system. The only priority is that keeps/copies overrule removes.
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000188void HandleArgs(const CopyConfig &Config, Object &Obj, const Reader &Reader,
189 ElfType OutputElfType) {
Jake Ehrlich5de70d92017-11-03 18:58:41 +0000190
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000191 if (!Config.SplitDWO.empty()) {
192 SplitDWOToFile(Config, Reader, Config.SplitDWO, OutputElfType);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000193 }
Jake Ehrlich5de70d92017-11-03 18:58:41 +0000194
Jake Ehrlichf03384d2017-10-11 18:09:18 +0000195 SectionPred RemovePred = [](const SectionBase &) { return false; };
196
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000197 // Removes:
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000198 if (!Config.ToRemove.empty()) {
199 RemovePred = [&Config](const SectionBase &Sec) {
200 return std::find(std::begin(Config.ToRemove), std::end(Config.ToRemove),
201 Sec.Name) != std::end(Config.ToRemove);
Jake Ehrlichf03384d2017-10-11 18:09:18 +0000202 };
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000203 }
Jake Ehrlichf03384d2017-10-11 18:09:18 +0000204
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000205 if (Config.StripDWO || !Config.SplitDWO.empty())
David Blaikie998ff812017-11-03 20:57:09 +0000206 RemovePred = [RemovePred](const SectionBase &Sec) {
Jake Ehrlich5de70d92017-11-03 18:58:41 +0000207 return IsDWOSection(Sec) || RemovePred(Sec);
208 };
209
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000210 if (Config.ExtractDWO)
Jake Ehrlich5de70d92017-11-03 18:58:41 +0000211 RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000212 return OnlyKeepDWOPred(Obj, Sec) || RemovePred(Sec);
Jake Ehrlich5de70d92017-11-03 18:58:41 +0000213 };
214
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000215 if (Config.StripAllGNU)
Jake Ehrlichfabddf12017-11-13 22:02:07 +0000216 RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
217 if (RemovePred(Sec))
218 return true;
219 if ((Sec.Flags & SHF_ALLOC) != 0)
220 return false;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000221 if (&Sec == Obj.SectionNames)
Jake Ehrlichfabddf12017-11-13 22:02:07 +0000222 return false;
Jake Ehrlich777fb002017-12-15 20:17:55 +0000223 switch (Sec.Type) {
Jake Ehrlichfabddf12017-11-13 22:02:07 +0000224 case SHT_SYMTAB:
225 case SHT_REL:
226 case SHT_RELA:
227 case SHT_STRTAB:
228 return true;
229 }
230 return Sec.Name.startswith(".debug");
231 };
232
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000233 if (Config.StripSections) {
Jake Ehrlichf03384d2017-10-11 18:09:18 +0000234 RemovePred = [RemovePred](const SectionBase &Sec) {
235 return RemovePred(Sec) || (Sec.Flags & SHF_ALLOC) == 0;
236 };
Jake Ehrlichf03384d2017-10-11 18:09:18 +0000237 }
238
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000239 if (Config.StripDebug) {
Jake Ehrlich1bfefc12017-11-13 22:13:08 +0000240 RemovePred = [RemovePred](const SectionBase &Sec) {
241 return RemovePred(Sec) || Sec.Name.startswith(".debug");
242 };
243 }
244
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000245 if (Config.StripNonAlloc)
Jake Ehrlichd56725a2017-11-14 18:50:24 +0000246 RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
247 if (RemovePred(Sec))
248 return true;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000249 if (&Sec == Obj.SectionNames)
Jake Ehrlichd56725a2017-11-14 18:50:24 +0000250 return false;
251 return (Sec.Flags & SHF_ALLOC) == 0;
252 };
253
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000254 if (Config.StripAll)
Jake Ehrlich6ad72d02017-11-27 18:56:01 +0000255 RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
256 if (RemovePred(Sec))
257 return true;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000258 if (&Sec == Obj.SectionNames)
Jake Ehrlich6ad72d02017-11-27 18:56:01 +0000259 return false;
260 if (Sec.Name.startswith(".gnu.warning"))
261 return false;
262 return (Sec.Flags & SHF_ALLOC) == 0;
263 };
264
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000265 // Explicit copies:
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000266 if (!Config.OnlyKeep.empty()) {
267 RemovePred = [&Config, RemovePred, &Obj](const SectionBase &Sec) {
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000268 // Explicitly keep these sections regardless of previous removes.
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000269 if (std::find(std::begin(Config.OnlyKeep), std::end(Config.OnlyKeep),
270 Sec.Name) != std::end(Config.OnlyKeep))
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000271 return false;
272
273 // Allow all implicit removes.
Jake Ehrlich76e91102018-01-25 22:46:17 +0000274 if (RemovePred(Sec))
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000275 return true;
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000276
277 // Keep special sections.
Jake Ehrlich76e91102018-01-25 22:46:17 +0000278 if (Obj.SectionNames == &Sec)
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000279 return false;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000280 if (Obj.SymbolTable == &Sec || Obj.SymbolTable->getStrTab() == &Sec)
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000281 return false;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000282
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000283 // Remove everything else.
284 return true;
285 };
286 }
287
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000288 if (!Config.Keep.empty()) {
289 RemovePred = [Config, RemovePred](const SectionBase &Sec) {
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000290 // Explicitly keep these sections regardless of previous removes.
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000291 if (std::find(std::begin(Config.Keep), std::end(Config.Keep), Sec.Name) !=
292 std::end(Config.Keep))
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000293 return false;
294 // Otherwise defer to RemovePred.
295 return RemovePred(Sec);
296 };
297 }
298
Jake Ehrlich76e91102018-01-25 22:46:17 +0000299 Obj.removeSections(RemovePred);
Jake Ehrliche8437de2017-12-19 00:47:30 +0000300
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000301 if (!Config.AddSection.empty()) {
302 for (const auto &Flag : Config.AddSection) {
303 auto SecPair = Flag.split("=");
Jake Ehrliche8437de2017-12-19 00:47:30 +0000304 auto SecName = SecPair.first;
305 auto File = SecPair.second;
306 auto BufOrErr = MemoryBuffer::getFile(File);
307 if (!BufOrErr)
308 reportError(File, BufOrErr.getError());
309 auto Buf = std::move(*BufOrErr);
310 auto BufPtr = reinterpret_cast<const uint8_t *>(Buf->getBufferStart());
311 auto BufSize = Buf->getBufferSize();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000312 Obj.addSection<OwnedDataSection>(SecName,
313 ArrayRef<uint8_t>(BufPtr, BufSize));
Jake Ehrliche8437de2017-12-19 00:47:30 +0000314 }
315 }
316
Alexander Shaposhnikov40e9bdf2018-04-26 18:28:17 +0000317 if (!Config.AddGnuDebugLink.empty())
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000318 Obj.addSection<GnuDebugLinkSection>(Config.AddGnuDebugLink);
Alexander Shaposhnikov40e9bdf2018-04-26 18:28:17 +0000319
320 if (Obj.SymbolTable) {
321 Obj.SymbolTable->updateSymbols([&](Symbol &Sym) {
322 if ((Config.LocalizeHidden &&
323 (Sym.Visibility == STV_HIDDEN || Sym.Visibility == STV_INTERNAL)) ||
324 (!Config.SymbolsToLocalize.empty() &&
Paul Semelee5be792018-04-27 19:09:44 +0000325 is_contained(Config.SymbolsToLocalize, Sym.Name)))
Alexander Shaposhnikov40e9bdf2018-04-26 18:28:17 +0000326 Sym.Binding = STB_LOCAL;
327
Paul Semelee5be792018-04-27 19:09:44 +0000328 if (!Config.SymbolsToGlobalize.empty() &&
329 is_contained(Config.SymbolsToGlobalize, Sym.Name))
330 Sym.Binding = STB_GLOBAL;
331
Paul Semel3a8a56b2018-04-27 19:16:27 +0000332 if (!Config.SymbolsToWeaken.empty() &&
333 is_contained(Config.SymbolsToWeaken, Sym.Name) &&
334 Sym.Binding == STB_GLOBAL)
335 Sym.Binding = STB_WEAK;
336
Alexander Shaposhnikov40e9bdf2018-04-26 18:28:17 +0000337 const auto I = Config.SymbolsToRename.find(Sym.Name);
338 if (I != Config.SymbolsToRename.end())
339 Sym.Name = I->getValue();
340 });
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000341 }
Jake Ehrlich76e91102018-01-25 22:46:17 +0000342}
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000343
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000344std::unique_ptr<Reader> CreateReader(StringRef InputFilename,
345 ElfType &OutputElfType) {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000346 // Right now we can only read ELF files so there's only one reader;
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000347 auto Out = llvm::make_unique<ELFReader>(InputFilename);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000348 // We need to set the default ElfType for output.
349 OutputElfType = Out->getElfType();
350 return std::move(Out);
Petr Hosek05a04cb2017-08-01 00:33:58 +0000351}
352
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000353void ExecuteElfObjcopy(const CopyConfig &Config) {
354 ElfType OutputElfType;
355 auto Reader = CreateReader(Config.InputFilename, OutputElfType);
Jake Ehrlicha8c689e2018-04-12 00:40:50 +0000356 auto Obj = Reader->create();
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000357 auto Writer =
358 CreateWriter(Config, *Obj, Config.OutputFilename, OutputElfType);
359 HandleArgs(Config, *Obj, *Reader, OutputElfType);
Jake Ehrlicha8c689e2018-04-12 00:40:50 +0000360 Writer->finalize();
361 Writer->write();
Petr Hosek05a04cb2017-08-01 00:33:58 +0000362}
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000363
364// ParseObjcopyOptions returns the config and sets the input arguments. If a
365// help flag is set then ParseObjcopyOptions will print the help messege and
366// exit.
367CopyConfig ParseObjcopyOptions(ArrayRef<const char *> ArgsArr) {
368 ObjcopyOptTable T;
369 unsigned MissingArgumentIndex, MissingArgumentCount;
370 llvm::opt::InputArgList InputArgs =
371 T.ParseArgs(ArgsArr, MissingArgumentIndex, MissingArgumentCount);
372
373 if (InputArgs.size() == 0 || InputArgs.hasArg(OBJCOPY_help)) {
374 T.PrintHelp(outs(), "llvm-objcopy <input> [ <output> ]", "objcopy tool");
375 exit(0);
376 }
377
378 SmallVector<const char *, 2> Positional;
379
380 for (auto Arg : InputArgs.filtered(OBJCOPY_UNKNOWN))
381 error("unknown argument '" + Arg->getAsString(InputArgs) + "'");
382
383 for (auto Arg : InputArgs.filtered(OBJCOPY_INPUT))
384 Positional.push_back(Arg->getValue());
385
386 if (Positional.empty())
387 error("No input file specified");
388
389 if (Positional.size() > 2)
390 error("Too many positional arguments");
391
392 CopyConfig Config;
393 Config.InputFilename = Positional[0];
394 Config.OutputFilename = Positional[Positional.size() == 1 ? 0 : 1];
395 Config.InputFormat = InputArgs.getLastArgValue(OBJCOPY_input_target);
396 Config.OutputFormat = InputArgs.getLastArgValue(OBJCOPY_output_target);
397 Config.BinaryArch = InputArgs.getLastArgValue(OBJCOPY_binary_architecture);
398
399 Config.SplitDWO = InputArgs.getLastArgValue(OBJCOPY_split_dwo);
400 Config.AddGnuDebugLink = InputArgs.getLastArgValue(OBJCOPY_add_gnu_debuglink);
Alexander Shaposhnikov40e9bdf2018-04-26 18:28:17 +0000401
402 for (auto Arg : InputArgs.filtered(OBJCOPY_redefine_symbol)) {
403 if (!StringRef(Arg->getValue()).contains('='))
404 error("Bad format for --redefine-sym");
405 auto Old2New = StringRef(Arg->getValue()).split('=');
406 if (!Config.SymbolsToRename.insert(Old2New).second)
407 error("Multiple redefinition of symbol " + Old2New.first);
408 }
409
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000410 for (auto Arg : InputArgs.filtered(OBJCOPY_remove_section))
411 Config.ToRemove.push_back(Arg->getValue());
412 for (auto Arg : InputArgs.filtered(OBJCOPY_keep))
413 Config.Keep.push_back(Arg->getValue());
414 for (auto Arg : InputArgs.filtered(OBJCOPY_only_keep))
415 Config.OnlyKeep.push_back(Arg->getValue());
416 for (auto Arg : InputArgs.filtered(OBJCOPY_add_section))
417 Config.AddSection.push_back(Arg->getValue());
418 Config.StripAll = InputArgs.hasArg(OBJCOPY_strip_all);
419 Config.StripAllGNU = InputArgs.hasArg(OBJCOPY_strip_all_gnu);
420 Config.StripDebug = InputArgs.hasArg(OBJCOPY_strip_debug);
421 Config.StripDWO = InputArgs.hasArg(OBJCOPY_strip_dwo);
422 Config.StripSections = InputArgs.hasArg(OBJCOPY_strip_sections);
423 Config.StripNonAlloc = InputArgs.hasArg(OBJCOPY_strip_non_alloc);
424 Config.ExtractDWO = InputArgs.hasArg(OBJCOPY_extract_dwo);
425 Config.LocalizeHidden = InputArgs.hasArg(OBJCOPY_localize_hidden);
Paul Semelb4924942018-04-26 17:44:43 +0000426 for (auto Arg : InputArgs.filtered(OBJCOPY_localize_symbol))
Alexander Shaposhnikov40e9bdf2018-04-26 18:28:17 +0000427 Config.SymbolsToLocalize.push_back(Arg->getValue());
Paul Semelee5be792018-04-27 19:09:44 +0000428 for (auto Arg : InputArgs.filtered(OBJCOPY_globalize_symbol))
429 Config.SymbolsToGlobalize.push_back(Arg->getValue());
Paul Semel3a8a56b2018-04-27 19:16:27 +0000430 for (auto Arg : InputArgs.filtered(OBJCOPY_weaken_symbol))
431 Config.SymbolsToWeaken.push_back(Arg->getValue());
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000432
433 return Config;
434}
435
436int main(int argc, char **argv) {
437 InitLLVM X(argc, argv);
438 ToolName = argv[0];
439
440 CopyConfig Config = ParseObjcopyOptions(makeArrayRef(argv + 1, argc));
441 ExecuteElfObjcopy(Config);
442}