xref: /llvm-project/llvm/tools/llvm-objcopy/llvm-objcopy.cpp (revision 3a8a56b8b76a39c24110fc92fc24bbd0549f737e)
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 };
134 
135 using SectionPred = std::function<bool(const SectionBase &Sec)>;
136 
137 bool IsDWOSection(const SectionBase &Sec) { return Sec.Name.endswith(".dwo"); }
138 
139 bool OnlyKeepDWOPred(const Object &Obj, const SectionBase &Sec) {
140   // We can't remove the section header string table.
141   if (&Sec == Obj.SectionNames)
142     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 
148 std::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);
152   }
153   // Depending on the initial ELFT and OutputFormat we need a different Writer.
154   switch (OutputElfType) {
155   case ELFT_ELF32LE:
156     return llvm::make_unique<ELFWriter<ELF32LE>>(File, Obj,
157                                                  !Config.StripSections);
158   case ELFT_ELF64LE:
159     return llvm::make_unique<ELFWriter<ELF64LE>>(File, Obj,
160                                                  !Config.StripSections);
161   case ELFT_ELF32BE:
162     return llvm::make_unique<ELFWriter<ELF32BE>>(File, Obj,
163                                                  !Config.StripSections);
164   case ELFT_ELF64BE:
165     return llvm::make_unique<ELFWriter<ELF64BE>>(File, Obj,
166                                                  !Config.StripSections);
167   }
168   llvm_unreachable("Invalid output format");
169 }
170 
171 void SplitDWOToFile(const CopyConfig &Config, const Reader &Reader,
172                     StringRef File, ElfType OutputElfType) {
173   auto DWOFile = Reader.create();
174   DWOFile->removeSections(
175       [&](const SectionBase &Sec) { return OnlyKeepDWOPred(*DWOFile, Sec); });
176   auto Writer = CreateWriter(Config, *DWOFile, File, OutputElfType);
177   Writer->finalize();
178   Writer->write();
179 }
180 
181 // 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.
188 void HandleArgs(const CopyConfig &Config, Object &Obj, const Reader &Reader,
189                 ElfType OutputElfType) {
190 
191   if (!Config.SplitDWO.empty()) {
192     SplitDWOToFile(Config, Reader, Config.SplitDWO, OutputElfType);
193   }
194 
195   SectionPred RemovePred = [](const SectionBase &) { return false; };
196 
197   // Removes:
198   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);
202     };
203   }
204 
205   if (Config.StripDWO || !Config.SplitDWO.empty())
206     RemovePred = [RemovePred](const SectionBase &Sec) {
207       return IsDWOSection(Sec) || RemovePred(Sec);
208     };
209 
210   if (Config.ExtractDWO)
211     RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
212       return OnlyKeepDWOPred(Obj, Sec) || RemovePred(Sec);
213     };
214 
215   if (Config.StripAllGNU)
216     RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
217       if (RemovePred(Sec))
218         return true;
219       if ((Sec.Flags & SHF_ALLOC) != 0)
220         return false;
221       if (&Sec == Obj.SectionNames)
222         return false;
223       switch (Sec.Type) {
224       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 
233   if (Config.StripSections) {
234     RemovePred = [RemovePred](const SectionBase &Sec) {
235       return RemovePred(Sec) || (Sec.Flags & SHF_ALLOC) == 0;
236     };
237   }
238 
239   if (Config.StripDebug) {
240     RemovePred = [RemovePred](const SectionBase &Sec) {
241       return RemovePred(Sec) || Sec.Name.startswith(".debug");
242     };
243   }
244 
245   if (Config.StripNonAlloc)
246     RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
247       if (RemovePred(Sec))
248         return true;
249       if (&Sec == Obj.SectionNames)
250         return false;
251       return (Sec.Flags & SHF_ALLOC) == 0;
252     };
253 
254   if (Config.StripAll)
255     RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
256       if (RemovePred(Sec))
257         return true;
258       if (&Sec == Obj.SectionNames)
259         return false;
260       if (Sec.Name.startswith(".gnu.warning"))
261         return false;
262       return (Sec.Flags & SHF_ALLOC) == 0;
263     };
264 
265   // Explicit copies:
266   if (!Config.OnlyKeep.empty()) {
267     RemovePred = [&Config, RemovePred, &Obj](const SectionBase &Sec) {
268       // Explicitly keep these sections regardless of previous removes.
269       if (std::find(std::begin(Config.OnlyKeep), std::end(Config.OnlyKeep),
270                     Sec.Name) != std::end(Config.OnlyKeep))
271         return false;
272 
273       // Allow all implicit removes.
274       if (RemovePred(Sec))
275         return true;
276 
277       // Keep special sections.
278       if (Obj.SectionNames == &Sec)
279         return false;
280       if (Obj.SymbolTable == &Sec || Obj.SymbolTable->getStrTab() == &Sec)
281         return false;
282 
283       // Remove everything else.
284       return true;
285     };
286   }
287 
288   if (!Config.Keep.empty()) {
289     RemovePred = [Config, RemovePred](const SectionBase &Sec) {
290       // Explicitly keep these sections regardless of previous removes.
291       if (std::find(std::begin(Config.Keep), std::end(Config.Keep), Sec.Name) !=
292           std::end(Config.Keep))
293         return false;
294       // Otherwise defer to RemovePred.
295       return RemovePred(Sec);
296     };
297   }
298 
299   Obj.removeSections(RemovePred);
300 
301   if (!Config.AddSection.empty()) {
302     for (const auto &Flag : Config.AddSection) {
303       auto SecPair = Flag.split("=");
304       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();
312       Obj.addSection<OwnedDataSection>(SecName,
313                                        ArrayRef<uint8_t>(BufPtr, BufSize));
314     }
315   }
316 
317   if (!Config.AddGnuDebugLink.empty())
318     Obj.addSection<GnuDebugLinkSection>(Config.AddGnuDebugLink);
319 
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() &&
325            is_contained(Config.SymbolsToLocalize, Sym.Name)))
326         Sym.Binding = STB_LOCAL;
327 
328       if (!Config.SymbolsToGlobalize.empty() &&
329           is_contained(Config.SymbolsToGlobalize, Sym.Name))
330         Sym.Binding = STB_GLOBAL;
331 
332       if (!Config.SymbolsToWeaken.empty() &&
333           is_contained(Config.SymbolsToWeaken, Sym.Name) &&
334           Sym.Binding == STB_GLOBAL)
335         Sym.Binding = STB_WEAK;
336 
337       const auto I = Config.SymbolsToRename.find(Sym.Name);
338       if (I != Config.SymbolsToRename.end())
339         Sym.Name = I->getValue();
340     });
341   }
342 }
343 
344 std::unique_ptr<Reader> CreateReader(StringRef InputFilename,
345                                      ElfType &OutputElfType) {
346   // Right now we can only read ELF files so there's only one reader;
347   auto Out = llvm::make_unique<ELFReader>(InputFilename);
348   // We need to set the default ElfType for output.
349   OutputElfType = Out->getElfType();
350   return std::move(Out);
351 }
352 
353 void ExecuteElfObjcopy(const CopyConfig &Config) {
354   ElfType OutputElfType;
355   auto Reader = CreateReader(Config.InputFilename, OutputElfType);
356   auto Obj = Reader->create();
357   auto Writer =
358       CreateWriter(Config, *Obj, Config.OutputFilename, OutputElfType);
359   HandleArgs(Config, *Obj, *Reader, OutputElfType);
360   Writer->finalize();
361   Writer->write();
362 }
363 
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.
367 CopyConfig 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);
401 
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 
410   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);
426   for (auto Arg : InputArgs.filtered(OBJCOPY_localize_symbol))
427     Config.SymbolsToLocalize.push_back(Arg->getValue());
428   for (auto Arg : InputArgs.filtered(OBJCOPY_globalize_symbol))
429     Config.SymbolsToGlobalize.push_back(Arg->getValue());
430   for (auto Arg : InputArgs.filtered(OBJCOPY_weaken_symbol))
431     Config.SymbolsToWeaken.push_back(Arg->getValue());
432 
433   return Config;
434 }
435 
436 int 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 }
443