xref: /llvm-project/llvm/tools/llvm-objcopy/llvm-objcopy.cpp (revision 41695f8e7397dfa1bc1691a7f3198dbf65adb3f1)
1 //===- llvm-objcopy.cpp ---------------------------------------------------===//
2 //
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 //===----------------------------------------------------------------------===//
9 
10 #include "llvm-objcopy.h"
11 #include "Object.h"
12 #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"
20 #include "llvm/Option/Arg.h"
21 #include "llvm/Option/ArgList.h"
22 #include "llvm/Option/Option.h"
23 #include "llvm/Support/Casting.h"
24 #include "llvm/Support/CommandLine.h"
25 #include "llvm/Support/Compiler.h"
26 #include "llvm/Support/Error.h"
27 #include "llvm/Support/ErrorHandling.h"
28 #include "llvm/Support/ErrorOr.h"
29 #include "llvm/Support/FileOutputBuffer.h"
30 #include "llvm/Support/InitLLVM.h"
31 #include "llvm/Support/raw_ostream.h"
32 #include <algorithm>
33 #include <cassert>
34 #include <cstdlib>
35 #include <functional>
36 #include <iterator>
37 #include <memory>
38 #include <string>
39 #include <system_error>
40 #include <utility>
41 
42 using namespace llvm;
43 using namespace object;
44 using namespace ELF;
45 
46 namespace {
47 
48 enum 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 
61 static const opt::OptTable::Info ObjcopyInfoTable[] = {
62 #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 
72 class ObjcopyOptTable : public opt::OptTable {
73 public:
74   ObjcopyOptTable() : OptTable(ObjcopyInfoTable, true) {}
75 };
76 
77 } // namespace
78 
79 // The name this program was invoked as.
80 static StringRef ToolName;
81 
82 namespace llvm {
83 
84 LLVM_ATTRIBUTE_NORETURN void error(Twine Message) {
85   errs() << ToolName << ": " << Message << ".\n";
86   errs().flush();
87   exit(1);
88 }
89 
90 LLVM_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 
96 LLVM_ATTRIBUTE_NORETURN void reportError(StringRef File, Error E) {
97   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 }
105 
106 } // end namespace llvm
107 
108 struct CopyConfig {
109   StringRef OutputFilename;
110   StringRef InputFilename;
111   StringRef OutputFormat;
112   StringRef InputFormat;
113   StringRef BinaryArch;
114 
115   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;
121   std::vector<StringRef> SymbolsToLocalize;
122   std::vector<StringRef> SymbolsToGlobalize;
123   std::vector<StringRef> SymbolsToWeaken;
124   StringMap<StringRef> SymbolsToRename;
125   bool StripAll;
126   bool StripAllGNU;
127   bool StripDebug;
128   bool StripSections;
129   bool StripNonAlloc;
130   bool StripDWO;
131   bool ExtractDWO;
132   bool LocalizeHidden;
133   bool Weaken;
134   bool DiscardAll;
135 };
136 
137 using SectionPred = std::function<bool(const SectionBase &Sec)>;
138 
139 bool IsDWOSection(const SectionBase &Sec) { return Sec.Name.endswith(".dwo"); }
140 
141 bool OnlyKeepDWOPred(const Object &Obj, const SectionBase &Sec) {
142   // We can't remove the section header string table.
143   if (&Sec == Obj.SectionNames)
144     return false;
145   // Short of keeping the string table we want to keep everything that is a DWO
146   // section and remove everything else.
147   return !IsDWOSection(Sec);
148 }
149 
150 std::unique_ptr<Writer> CreateWriter(const CopyConfig &Config, Object &Obj,
151                                      StringRef File, ElfType OutputElfType) {
152   if (Config.OutputFormat == "binary") {
153     return llvm::make_unique<BinaryWriter>(File, Obj);
154   }
155   // Depending on the initial ELFT and OutputFormat we need a different Writer.
156   switch (OutputElfType) {
157   case ELFT_ELF32LE:
158     return llvm::make_unique<ELFWriter<ELF32LE>>(File, Obj,
159                                                  !Config.StripSections);
160   case ELFT_ELF64LE:
161     return llvm::make_unique<ELFWriter<ELF64LE>>(File, Obj,
162                                                  !Config.StripSections);
163   case ELFT_ELF32BE:
164     return llvm::make_unique<ELFWriter<ELF32BE>>(File, Obj,
165                                                  !Config.StripSections);
166   case ELFT_ELF64BE:
167     return llvm::make_unique<ELFWriter<ELF64BE>>(File, Obj,
168                                                  !Config.StripSections);
169   }
170   llvm_unreachable("Invalid output format");
171 }
172 
173 void SplitDWOToFile(const CopyConfig &Config, const Reader &Reader,
174                     StringRef File, ElfType OutputElfType) {
175   auto DWOFile = Reader.create();
176   DWOFile->removeSections(
177       [&](const SectionBase &Sec) { return OnlyKeepDWOPred(*DWOFile, Sec); });
178   auto Writer = CreateWriter(Config, *DWOFile, File, OutputElfType);
179   Writer->finalize();
180   Writer->write();
181 }
182 
183 // This function handles the high level operations of GNU objcopy including
184 // handling command line options. It's important to outline certain properties
185 // we expect to hold of the command line operations. Any operation that "keeps"
186 // should keep regardless of a remove. Additionally any removal should respect
187 // any previous removals. Lastly whether or not something is removed shouldn't
188 // depend a) on the order the options occur in or b) on some opaque priority
189 // system. The only priority is that keeps/copies overrule removes.
190 void HandleArgs(const CopyConfig &Config, Object &Obj, const Reader &Reader,
191                 ElfType OutputElfType) {
192 
193   if (!Config.SplitDWO.empty()) {
194     SplitDWOToFile(Config, Reader, Config.SplitDWO, OutputElfType);
195   }
196 
197   SectionPred RemovePred = [](const SectionBase &) { return false; };
198 
199   // Removes:
200   if (!Config.ToRemove.empty()) {
201     RemovePred = [&Config](const SectionBase &Sec) {
202       return std::find(std::begin(Config.ToRemove), std::end(Config.ToRemove),
203                        Sec.Name) != std::end(Config.ToRemove);
204     };
205   }
206 
207   if (Config.StripDWO || !Config.SplitDWO.empty())
208     RemovePred = [RemovePred](const SectionBase &Sec) {
209       return IsDWOSection(Sec) || RemovePred(Sec);
210     };
211 
212   if (Config.ExtractDWO)
213     RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
214       return OnlyKeepDWOPred(Obj, Sec) || RemovePred(Sec);
215     };
216 
217   if (Config.StripAllGNU)
218     RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
219       if (RemovePred(Sec))
220         return true;
221       if ((Sec.Flags & SHF_ALLOC) != 0)
222         return false;
223       if (&Sec == Obj.SectionNames)
224         return false;
225       switch (Sec.Type) {
226       case SHT_SYMTAB:
227       case SHT_REL:
228       case SHT_RELA:
229       case SHT_STRTAB:
230         return true;
231       }
232       return Sec.Name.startswith(".debug");
233     };
234 
235   if (Config.StripSections) {
236     RemovePred = [RemovePred](const SectionBase &Sec) {
237       return RemovePred(Sec) || (Sec.Flags & SHF_ALLOC) == 0;
238     };
239   }
240 
241   if (Config.StripDebug) {
242     RemovePred = [RemovePred](const SectionBase &Sec) {
243       return RemovePred(Sec) || Sec.Name.startswith(".debug");
244     };
245   }
246 
247   if (Config.StripNonAlloc)
248     RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
249       if (RemovePred(Sec))
250         return true;
251       if (&Sec == Obj.SectionNames)
252         return false;
253       return (Sec.Flags & SHF_ALLOC) == 0;
254     };
255 
256   if (Config.StripAll)
257     RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
258       if (RemovePred(Sec))
259         return true;
260       if (&Sec == Obj.SectionNames)
261         return false;
262       if (Sec.Name.startswith(".gnu.warning"))
263         return false;
264       return (Sec.Flags & SHF_ALLOC) == 0;
265     };
266 
267   // Explicit copies:
268   if (!Config.OnlyKeep.empty()) {
269     RemovePred = [&Config, RemovePred, &Obj](const SectionBase &Sec) {
270       // Explicitly keep these sections regardless of previous removes.
271       if (std::find(std::begin(Config.OnlyKeep), std::end(Config.OnlyKeep),
272                     Sec.Name) != std::end(Config.OnlyKeep))
273         return false;
274 
275       // Allow all implicit removes.
276       if (RemovePred(Sec))
277         return true;
278 
279       // Keep special sections.
280       if (Obj.SectionNames == &Sec)
281         return false;
282       if (Obj.SymbolTable == &Sec || Obj.SymbolTable->getStrTab() == &Sec)
283         return false;
284 
285       // Remove everything else.
286       return true;
287     };
288   }
289 
290   if (!Config.Keep.empty()) {
291     RemovePred = [Config, RemovePred](const SectionBase &Sec) {
292       // Explicitly keep these sections regardless of previous removes.
293       if (std::find(std::begin(Config.Keep), std::end(Config.Keep), Sec.Name) !=
294           std::end(Config.Keep))
295         return false;
296       // Otherwise defer to RemovePred.
297       return RemovePred(Sec);
298     };
299   }
300 
301   Obj.removeSections(RemovePred);
302 
303   if (!Config.AddSection.empty()) {
304     for (const auto &Flag : Config.AddSection) {
305       auto SecPair = Flag.split("=");
306       auto SecName = SecPair.first;
307       auto File = SecPair.second;
308       auto BufOrErr = MemoryBuffer::getFile(File);
309       if (!BufOrErr)
310         reportError(File, BufOrErr.getError());
311       auto Buf = std::move(*BufOrErr);
312       auto BufPtr = reinterpret_cast<const uint8_t *>(Buf->getBufferStart());
313       auto BufSize = Buf->getBufferSize();
314       Obj.addSection<OwnedDataSection>(SecName,
315                                        ArrayRef<uint8_t>(BufPtr, BufSize));
316     }
317   }
318 
319   if (!Config.AddGnuDebugLink.empty())
320     Obj.addSection<GnuDebugLinkSection>(Config.AddGnuDebugLink);
321 
322   if (Obj.SymbolTable) {
323     Obj.SymbolTable->updateSymbols([&](Symbol &Sym) {
324       if ((Config.LocalizeHidden &&
325            (Sym.Visibility == STV_HIDDEN || Sym.Visibility == STV_INTERNAL)) ||
326           (!Config.SymbolsToLocalize.empty() &&
327            is_contained(Config.SymbolsToLocalize, Sym.Name)))
328         Sym.Binding = STB_LOCAL;
329 
330       if (!Config.SymbolsToGlobalize.empty() &&
331           is_contained(Config.SymbolsToGlobalize, Sym.Name))
332         Sym.Binding = STB_GLOBAL;
333 
334       if (!Config.SymbolsToWeaken.empty() &&
335           is_contained(Config.SymbolsToWeaken, Sym.Name) &&
336           Sym.Binding == STB_GLOBAL)
337         Sym.Binding = STB_WEAK;
338 
339       if (Config.Weaken && Sym.Binding == STB_GLOBAL &&
340           Sym.getShndx() != SHN_UNDEF)
341         Sym.Binding = STB_WEAK;
342 
343       const auto I = Config.SymbolsToRename.find(Sym.Name);
344       if (I != Config.SymbolsToRename.end())
345         Sym.Name = I->getValue();
346     });
347 
348     Obj.SymbolTable->removeSymbols([&](const Symbol &Sym) {
349       if (Config.DiscardAll && Sym.Binding == STB_LOCAL &&
350           Sym.getShndx() != SHN_UNDEF && Sym.Type != STT_FILE &&
351           Sym.Type != STT_SECTION)
352         return true;
353       return false;
354     });
355   }
356 }
357 
358 std::unique_ptr<Reader> CreateReader(StringRef InputFilename,
359                                      ElfType &OutputElfType) {
360   // Right now we can only read ELF files so there's only one reader;
361   auto Out = llvm::make_unique<ELFReader>(InputFilename);
362   // We need to set the default ElfType for output.
363   OutputElfType = Out->getElfType();
364   return std::move(Out);
365 }
366 
367 void ExecuteElfObjcopy(const CopyConfig &Config) {
368   ElfType OutputElfType;
369   auto Reader = CreateReader(Config.InputFilename, OutputElfType);
370   auto Obj = Reader->create();
371   auto Writer =
372       CreateWriter(Config, *Obj, Config.OutputFilename, OutputElfType);
373   HandleArgs(Config, *Obj, *Reader, OutputElfType);
374   Writer->finalize();
375   Writer->write();
376 }
377 
378 // ParseObjcopyOptions returns the config and sets the input arguments. If a
379 // help flag is set then ParseObjcopyOptions will print the help messege and
380 // exit.
381 CopyConfig ParseObjcopyOptions(ArrayRef<const char *> ArgsArr) {
382   ObjcopyOptTable T;
383   unsigned MissingArgumentIndex, MissingArgumentCount;
384   llvm::opt::InputArgList InputArgs =
385       T.ParseArgs(ArgsArr, MissingArgumentIndex, MissingArgumentCount);
386 
387   if (InputArgs.size() == 0 || InputArgs.hasArg(OBJCOPY_help)) {
388     T.PrintHelp(outs(), "llvm-objcopy <input> [ <output> ]", "objcopy tool");
389     exit(0);
390   }
391 
392   SmallVector<const char *, 2> Positional;
393 
394   for (auto Arg : InputArgs.filtered(OBJCOPY_UNKNOWN))
395     error("unknown argument '" + Arg->getAsString(InputArgs) + "'");
396 
397   for (auto Arg : InputArgs.filtered(OBJCOPY_INPUT))
398     Positional.push_back(Arg->getValue());
399 
400   if (Positional.empty())
401     error("No input file specified");
402 
403   if (Positional.size() > 2)
404     error("Too many positional arguments");
405 
406   CopyConfig Config;
407   Config.InputFilename = Positional[0];
408   Config.OutputFilename = Positional[Positional.size() == 1 ? 0 : 1];
409   Config.InputFormat = InputArgs.getLastArgValue(OBJCOPY_input_target);
410   Config.OutputFormat = InputArgs.getLastArgValue(OBJCOPY_output_target);
411   Config.BinaryArch = InputArgs.getLastArgValue(OBJCOPY_binary_architecture);
412 
413   Config.SplitDWO = InputArgs.getLastArgValue(OBJCOPY_split_dwo);
414   Config.AddGnuDebugLink = InputArgs.getLastArgValue(OBJCOPY_add_gnu_debuglink);
415 
416   for (auto Arg : InputArgs.filtered(OBJCOPY_redefine_symbol)) {
417     if (!StringRef(Arg->getValue()).contains('='))
418       error("Bad format for --redefine-sym");
419     auto Old2New = StringRef(Arg->getValue()).split('=');
420     if (!Config.SymbolsToRename.insert(Old2New).second)
421       error("Multiple redefinition of symbol " + Old2New.first);
422   }
423 
424   for (auto Arg : InputArgs.filtered(OBJCOPY_remove_section))
425     Config.ToRemove.push_back(Arg->getValue());
426   for (auto Arg : InputArgs.filtered(OBJCOPY_keep))
427     Config.Keep.push_back(Arg->getValue());
428   for (auto Arg : InputArgs.filtered(OBJCOPY_only_keep))
429     Config.OnlyKeep.push_back(Arg->getValue());
430   for (auto Arg : InputArgs.filtered(OBJCOPY_add_section))
431     Config.AddSection.push_back(Arg->getValue());
432   Config.StripAll = InputArgs.hasArg(OBJCOPY_strip_all);
433   Config.StripAllGNU = InputArgs.hasArg(OBJCOPY_strip_all_gnu);
434   Config.StripDebug = InputArgs.hasArg(OBJCOPY_strip_debug);
435   Config.StripDWO = InputArgs.hasArg(OBJCOPY_strip_dwo);
436   Config.StripSections = InputArgs.hasArg(OBJCOPY_strip_sections);
437   Config.StripNonAlloc = InputArgs.hasArg(OBJCOPY_strip_non_alloc);
438   Config.ExtractDWO = InputArgs.hasArg(OBJCOPY_extract_dwo);
439   Config.LocalizeHidden = InputArgs.hasArg(OBJCOPY_localize_hidden);
440   Config.Weaken = InputArgs.hasArg(OBJCOPY_weaken);
441   Config.DiscardAll = InputArgs.hasArg(OBJCOPY_discard_all);
442   for (auto Arg : InputArgs.filtered(OBJCOPY_localize_symbol))
443     Config.SymbolsToLocalize.push_back(Arg->getValue());
444   for (auto Arg : InputArgs.filtered(OBJCOPY_globalize_symbol))
445     Config.SymbolsToGlobalize.push_back(Arg->getValue());
446   for (auto Arg : InputArgs.filtered(OBJCOPY_weaken_symbol))
447     Config.SymbolsToWeaken.push_back(Arg->getValue());
448 
449   return Config;
450 }
451 
452 int main(int argc, char **argv) {
453   InitLLVM X(argc, argv);
454   ToolName = argv[0];
455 
456   CopyConfig Config = ParseObjcopyOptions(makeArrayRef(argv + 1, argc));
457   ExecuteElfObjcopy(Config);
458 }
459