blob: 8bf7ac77f16658164f72777ff70f0cc3610f5be2 [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;
Paul Semel2c0510f2018-05-02 20:14:49 +0000133 bool Weaken;
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000134};
Jake Ehrlichf03384d2017-10-11 18:09:18 +0000135
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000136using SectionPred = std::function<bool(const SectionBase &Sec)>;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000137
Jake Ehrlich777fb002017-12-15 20:17:55 +0000138bool IsDWOSection(const SectionBase &Sec) { return Sec.Name.endswith(".dwo"); }
Jake Ehrlich5de70d92017-11-03 18:58:41 +0000139
Jake Ehrlich76e91102018-01-25 22:46:17 +0000140bool OnlyKeepDWOPred(const Object &Obj, const SectionBase &Sec) {
Jake Ehrlich5de70d92017-11-03 18:58:41 +0000141 // We can't remove the section header string table.
Jake Ehrlich76e91102018-01-25 22:46:17 +0000142 if (&Sec == Obj.SectionNames)
Jake Ehrlich5de70d92017-11-03 18:58:41 +0000143 return false;
144 // Short of keeping the string table we want to keep everything that is a DWO
145 // section and remove everything else.
146 return !IsDWOSection(Sec);
147}
148
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000149std::unique_ptr<Writer> CreateWriter(const CopyConfig &Config, Object &Obj,
150 StringRef File, ElfType OutputElfType) {
151 if (Config.OutputFormat == "binary") {
152 return llvm::make_unique<BinaryWriter>(File, Obj);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000153 }
154 // Depending on the initial ELFT and OutputFormat we need a different Writer.
155 switch (OutputElfType) {
156 case ELFT_ELF32LE:
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000157 return llvm::make_unique<ELFWriter<ELF32LE>>(File, Obj,
158 !Config.StripSections);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000159 case ELFT_ELF64LE:
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000160 return llvm::make_unique<ELFWriter<ELF64LE>>(File, Obj,
161 !Config.StripSections);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000162 case ELFT_ELF32BE:
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000163 return llvm::make_unique<ELFWriter<ELF32BE>>(File, Obj,
164 !Config.StripSections);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000165 case ELFT_ELF64BE:
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000166 return llvm::make_unique<ELFWriter<ELF64BE>>(File, Obj,
167 !Config.StripSections);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000168 }
169 llvm_unreachable("Invalid output format");
Jake Ehrlich5de70d92017-11-03 18:58:41 +0000170}
171
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000172void SplitDWOToFile(const CopyConfig &Config, const Reader &Reader,
173 StringRef File, ElfType OutputElfType) {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000174 auto DWOFile = Reader.create();
175 DWOFile->removeSections(
176 [&](const SectionBase &Sec) { return OnlyKeepDWOPred(*DWOFile, Sec); });
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000177 auto Writer = CreateWriter(Config, *DWOFile, File, OutputElfType);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000178 Writer->finalize();
179 Writer->write();
Jake Ehrlich5de70d92017-11-03 18:58:41 +0000180}
181
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000182// This function handles the high level operations of GNU objcopy including
183// handling command line options. It's important to outline certain properties
184// we expect to hold of the command line operations. Any operation that "keeps"
185// should keep regardless of a remove. Additionally any removal should respect
186// any previous removals. Lastly whether or not something is removed shouldn't
187// depend a) on the order the options occur in or b) on some opaque priority
188// system. The only priority is that keeps/copies overrule removes.
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000189void HandleArgs(const CopyConfig &Config, Object &Obj, const Reader &Reader,
190 ElfType OutputElfType) {
Jake Ehrlich5de70d92017-11-03 18:58:41 +0000191
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000192 if (!Config.SplitDWO.empty()) {
193 SplitDWOToFile(Config, Reader, Config.SplitDWO, OutputElfType);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000194 }
Jake Ehrlich5de70d92017-11-03 18:58:41 +0000195
Jake Ehrlichf03384d2017-10-11 18:09:18 +0000196 SectionPred RemovePred = [](const SectionBase &) { return false; };
197
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000198 // Removes:
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000199 if (!Config.ToRemove.empty()) {
200 RemovePred = [&Config](const SectionBase &Sec) {
201 return std::find(std::begin(Config.ToRemove), std::end(Config.ToRemove),
202 Sec.Name) != std::end(Config.ToRemove);
Jake Ehrlichf03384d2017-10-11 18:09:18 +0000203 };
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000204 }
Jake Ehrlichf03384d2017-10-11 18:09:18 +0000205
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000206 if (Config.StripDWO || !Config.SplitDWO.empty())
David Blaikie998ff812017-11-03 20:57:09 +0000207 RemovePred = [RemovePred](const SectionBase &Sec) {
Jake Ehrlich5de70d92017-11-03 18:58:41 +0000208 return IsDWOSection(Sec) || RemovePred(Sec);
209 };
210
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000211 if (Config.ExtractDWO)
Jake Ehrlich5de70d92017-11-03 18:58:41 +0000212 RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000213 return OnlyKeepDWOPred(Obj, Sec) || RemovePred(Sec);
Jake Ehrlich5de70d92017-11-03 18:58:41 +0000214 };
215
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000216 if (Config.StripAllGNU)
Jake Ehrlichfabddf12017-11-13 22:02:07 +0000217 RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
218 if (RemovePred(Sec))
219 return true;
220 if ((Sec.Flags & SHF_ALLOC) != 0)
221 return false;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000222 if (&Sec == Obj.SectionNames)
Jake Ehrlichfabddf12017-11-13 22:02:07 +0000223 return false;
Jake Ehrlich777fb002017-12-15 20:17:55 +0000224 switch (Sec.Type) {
Jake Ehrlichfabddf12017-11-13 22:02:07 +0000225 case SHT_SYMTAB:
226 case SHT_REL:
227 case SHT_RELA:
228 case SHT_STRTAB:
229 return true;
230 }
231 return Sec.Name.startswith(".debug");
232 };
233
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000234 if (Config.StripSections) {
Jake Ehrlichf03384d2017-10-11 18:09:18 +0000235 RemovePred = [RemovePred](const SectionBase &Sec) {
236 return RemovePred(Sec) || (Sec.Flags & SHF_ALLOC) == 0;
237 };
Jake Ehrlichf03384d2017-10-11 18:09:18 +0000238 }
239
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000240 if (Config.StripDebug) {
Jake Ehrlich1bfefc12017-11-13 22:13:08 +0000241 RemovePred = [RemovePred](const SectionBase &Sec) {
242 return RemovePred(Sec) || Sec.Name.startswith(".debug");
243 };
244 }
245
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000246 if (Config.StripNonAlloc)
Jake Ehrlichd56725a2017-11-14 18:50:24 +0000247 RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
248 if (RemovePred(Sec))
249 return true;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000250 if (&Sec == Obj.SectionNames)
Jake Ehrlichd56725a2017-11-14 18:50:24 +0000251 return false;
252 return (Sec.Flags & SHF_ALLOC) == 0;
253 };
254
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000255 if (Config.StripAll)
Jake Ehrlich6ad72d02017-11-27 18:56:01 +0000256 RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
257 if (RemovePred(Sec))
258 return true;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000259 if (&Sec == Obj.SectionNames)
Jake Ehrlich6ad72d02017-11-27 18:56:01 +0000260 return false;
261 if (Sec.Name.startswith(".gnu.warning"))
262 return false;
263 return (Sec.Flags & SHF_ALLOC) == 0;
264 };
265
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000266 // Explicit copies:
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000267 if (!Config.OnlyKeep.empty()) {
268 RemovePred = [&Config, RemovePred, &Obj](const SectionBase &Sec) {
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000269 // Explicitly keep these sections regardless of previous removes.
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000270 if (std::find(std::begin(Config.OnlyKeep), std::end(Config.OnlyKeep),
271 Sec.Name) != std::end(Config.OnlyKeep))
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000272 return false;
273
274 // Allow all implicit removes.
Jake Ehrlich76e91102018-01-25 22:46:17 +0000275 if (RemovePred(Sec))
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000276 return true;
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000277
278 // Keep special sections.
Jake Ehrlich76e91102018-01-25 22:46:17 +0000279 if (Obj.SectionNames == &Sec)
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000280 return false;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000281 if (Obj.SymbolTable == &Sec || Obj.SymbolTable->getStrTab() == &Sec)
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000282 return false;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000283
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000284 // Remove everything else.
285 return true;
286 };
287 }
288
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000289 if (!Config.Keep.empty()) {
290 RemovePred = [Config, RemovePred](const SectionBase &Sec) {
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000291 // Explicitly keep these sections regardless of previous removes.
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000292 if (std::find(std::begin(Config.Keep), std::end(Config.Keep), Sec.Name) !=
293 std::end(Config.Keep))
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000294 return false;
295 // Otherwise defer to RemovePred.
296 return RemovePred(Sec);
297 };
298 }
299
Jake Ehrlich76e91102018-01-25 22:46:17 +0000300 Obj.removeSections(RemovePred);
Jake Ehrliche8437de2017-12-19 00:47:30 +0000301
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000302 if (!Config.AddSection.empty()) {
303 for (const auto &Flag : Config.AddSection) {
304 auto SecPair = Flag.split("=");
Jake Ehrliche8437de2017-12-19 00:47:30 +0000305 auto SecName = SecPair.first;
306 auto File = SecPair.second;
307 auto BufOrErr = MemoryBuffer::getFile(File);
308 if (!BufOrErr)
309 reportError(File, BufOrErr.getError());
310 auto Buf = std::move(*BufOrErr);
311 auto BufPtr = reinterpret_cast<const uint8_t *>(Buf->getBufferStart());
312 auto BufSize = Buf->getBufferSize();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000313 Obj.addSection<OwnedDataSection>(SecName,
314 ArrayRef<uint8_t>(BufPtr, BufSize));
Jake Ehrliche8437de2017-12-19 00:47:30 +0000315 }
316 }
317
Alexander Shaposhnikov40e9bdf2018-04-26 18:28:17 +0000318 if (!Config.AddGnuDebugLink.empty())
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000319 Obj.addSection<GnuDebugLinkSection>(Config.AddGnuDebugLink);
Alexander Shaposhnikov40e9bdf2018-04-26 18:28:17 +0000320
321 if (Obj.SymbolTable) {
322 Obj.SymbolTable->updateSymbols([&](Symbol &Sym) {
323 if ((Config.LocalizeHidden &&
324 (Sym.Visibility == STV_HIDDEN || Sym.Visibility == STV_INTERNAL)) ||
325 (!Config.SymbolsToLocalize.empty() &&
Paul Semelee5be792018-04-27 19:09:44 +0000326 is_contained(Config.SymbolsToLocalize, Sym.Name)))
Alexander Shaposhnikov40e9bdf2018-04-26 18:28:17 +0000327 Sym.Binding = STB_LOCAL;
328
Paul Semelee5be792018-04-27 19:09:44 +0000329 if (!Config.SymbolsToGlobalize.empty() &&
330 is_contained(Config.SymbolsToGlobalize, Sym.Name))
331 Sym.Binding = STB_GLOBAL;
332
Paul Semel3a8a56b2018-04-27 19:16:27 +0000333 if (!Config.SymbolsToWeaken.empty() &&
334 is_contained(Config.SymbolsToWeaken, Sym.Name) &&
335 Sym.Binding == STB_GLOBAL)
336 Sym.Binding = STB_WEAK;
337
Paul Semel2c0510f2018-05-02 20:14:49 +0000338 if (Config.Weaken && Sym.Binding == STB_GLOBAL &&
339 Sym.getShndx() != SHN_UNDEF)
340 Sym.Binding = STB_WEAK;
341
Alexander Shaposhnikov40e9bdf2018-04-26 18:28:17 +0000342 const auto I = Config.SymbolsToRename.find(Sym.Name);
343 if (I != Config.SymbolsToRename.end())
344 Sym.Name = I->getValue();
345 });
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000346 }
Jake Ehrlich76e91102018-01-25 22:46:17 +0000347}
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000348
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000349std::unique_ptr<Reader> CreateReader(StringRef InputFilename,
350 ElfType &OutputElfType) {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000351 // Right now we can only read ELF files so there's only one reader;
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000352 auto Out = llvm::make_unique<ELFReader>(InputFilename);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000353 // We need to set the default ElfType for output.
354 OutputElfType = Out->getElfType();
355 return std::move(Out);
Petr Hosek05a04cb2017-08-01 00:33:58 +0000356}
357
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000358void ExecuteElfObjcopy(const CopyConfig &Config) {
359 ElfType OutputElfType;
360 auto Reader = CreateReader(Config.InputFilename, OutputElfType);
Jake Ehrlicha8c689e2018-04-12 00:40:50 +0000361 auto Obj = Reader->create();
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000362 auto Writer =
363 CreateWriter(Config, *Obj, Config.OutputFilename, OutputElfType);
364 HandleArgs(Config, *Obj, *Reader, OutputElfType);
Jake Ehrlicha8c689e2018-04-12 00:40:50 +0000365 Writer->finalize();
366 Writer->write();
Petr Hosek05a04cb2017-08-01 00:33:58 +0000367}
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000368
369// ParseObjcopyOptions returns the config and sets the input arguments. If a
370// help flag is set then ParseObjcopyOptions will print the help messege and
371// exit.
372CopyConfig ParseObjcopyOptions(ArrayRef<const char *> ArgsArr) {
373 ObjcopyOptTable T;
374 unsigned MissingArgumentIndex, MissingArgumentCount;
375 llvm::opt::InputArgList InputArgs =
376 T.ParseArgs(ArgsArr, MissingArgumentIndex, MissingArgumentCount);
377
378 if (InputArgs.size() == 0 || InputArgs.hasArg(OBJCOPY_help)) {
379 T.PrintHelp(outs(), "llvm-objcopy <input> [ <output> ]", "objcopy tool");
380 exit(0);
381 }
382
383 SmallVector<const char *, 2> Positional;
384
385 for (auto Arg : InputArgs.filtered(OBJCOPY_UNKNOWN))
386 error("unknown argument '" + Arg->getAsString(InputArgs) + "'");
387
388 for (auto Arg : InputArgs.filtered(OBJCOPY_INPUT))
389 Positional.push_back(Arg->getValue());
390
391 if (Positional.empty())
392 error("No input file specified");
393
394 if (Positional.size() > 2)
395 error("Too many positional arguments");
396
397 CopyConfig Config;
398 Config.InputFilename = Positional[0];
399 Config.OutputFilename = Positional[Positional.size() == 1 ? 0 : 1];
400 Config.InputFormat = InputArgs.getLastArgValue(OBJCOPY_input_target);
401 Config.OutputFormat = InputArgs.getLastArgValue(OBJCOPY_output_target);
402 Config.BinaryArch = InputArgs.getLastArgValue(OBJCOPY_binary_architecture);
403
404 Config.SplitDWO = InputArgs.getLastArgValue(OBJCOPY_split_dwo);
405 Config.AddGnuDebugLink = InputArgs.getLastArgValue(OBJCOPY_add_gnu_debuglink);
Alexander Shaposhnikov40e9bdf2018-04-26 18:28:17 +0000406
407 for (auto Arg : InputArgs.filtered(OBJCOPY_redefine_symbol)) {
408 if (!StringRef(Arg->getValue()).contains('='))
409 error("Bad format for --redefine-sym");
410 auto Old2New = StringRef(Arg->getValue()).split('=');
411 if (!Config.SymbolsToRename.insert(Old2New).second)
412 error("Multiple redefinition of symbol " + Old2New.first);
413 }
414
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000415 for (auto Arg : InputArgs.filtered(OBJCOPY_remove_section))
416 Config.ToRemove.push_back(Arg->getValue());
417 for (auto Arg : InputArgs.filtered(OBJCOPY_keep))
418 Config.Keep.push_back(Arg->getValue());
419 for (auto Arg : InputArgs.filtered(OBJCOPY_only_keep))
420 Config.OnlyKeep.push_back(Arg->getValue());
421 for (auto Arg : InputArgs.filtered(OBJCOPY_add_section))
422 Config.AddSection.push_back(Arg->getValue());
423 Config.StripAll = InputArgs.hasArg(OBJCOPY_strip_all);
424 Config.StripAllGNU = InputArgs.hasArg(OBJCOPY_strip_all_gnu);
425 Config.StripDebug = InputArgs.hasArg(OBJCOPY_strip_debug);
426 Config.StripDWO = InputArgs.hasArg(OBJCOPY_strip_dwo);
427 Config.StripSections = InputArgs.hasArg(OBJCOPY_strip_sections);
428 Config.StripNonAlloc = InputArgs.hasArg(OBJCOPY_strip_non_alloc);
429 Config.ExtractDWO = InputArgs.hasArg(OBJCOPY_extract_dwo);
430 Config.LocalizeHidden = InputArgs.hasArg(OBJCOPY_localize_hidden);
Paul Semel2c0510f2018-05-02 20:14:49 +0000431 Config.Weaken = InputArgs.hasArg(OBJCOPY_weaken);
Paul Semelb4924942018-04-26 17:44:43 +0000432 for (auto Arg : InputArgs.filtered(OBJCOPY_localize_symbol))
Alexander Shaposhnikov40e9bdf2018-04-26 18:28:17 +0000433 Config.SymbolsToLocalize.push_back(Arg->getValue());
Paul Semelee5be792018-04-27 19:09:44 +0000434 for (auto Arg : InputArgs.filtered(OBJCOPY_globalize_symbol))
435 Config.SymbolsToGlobalize.push_back(Arg->getValue());
Paul Semel3a8a56b2018-04-27 19:16:27 +0000436 for (auto Arg : InputArgs.filtered(OBJCOPY_weaken_symbol))
437 Config.SymbolsToWeaken.push_back(Arg->getValue());
Alexander Shaposhnikovd6884792018-04-24 05:43:32 +0000438
439 return Config;
440}
441
442int main(int argc, char **argv) {
443 InitLLVM X(argc, argv);
444 ToolName = argv[0];
445
446 CopyConfig Config = ParseObjcopyOptions(makeArrayRef(argv + 1, argc));
447 ExecuteElfObjcopy(Config);
448}