blob: 045b34f771500d2d2f985ff090e3645f4fd9bfdc [file] [log] [blame]
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +00001//===- ELFObjcopy.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 "ELFObjcopy.h"
11#include "Buffer.h"
12#include "CopyConfig.h"
13#include "llvm-objcopy.h"
14#include "Object.h"
15
16#include "llvm/ADT/BitmaskEnum.h"
17#include "llvm/ADT/Optional.h"
18#include "llvm/ADT/STLExtras.h"
19#include "llvm/ADT/SmallVector.h"
20#include "llvm/ADT/StringRef.h"
21#include "llvm/ADT/Twine.h"
22#include "llvm/BinaryFormat/ELF.h"
23#include "llvm/MC/MCTargetOptions.h"
24#include "llvm/Object/Binary.h"
25#include "llvm/Object/ELFObjectFile.h"
26#include "llvm/Object/ELFTypes.h"
27#include "llvm/Object/Error.h"
28#include "llvm/Option/Option.h"
29#include "llvm/Support/Casting.h"
30#include "llvm/Support/Compression.h"
31#include "llvm/Support/Error.h"
32#include "llvm/Support/ErrorHandling.h"
33#include "llvm/Support/ErrorOr.h"
34#include "llvm/Support/Memory.h"
35#include "llvm/Support/Path.h"
36#include "llvm/Support/raw_ostream.h"
37#include <algorithm>
38#include <cassert>
39#include <cstdlib>
40#include <functional>
41#include <iterator>
42#include <memory>
43#include <string>
44#include <system_error>
45#include <utility>
46
47namespace llvm {
48namespace objcopy {
49namespace elf {
50
51using namespace object;
52using namespace ELF;
53using SectionPred = std::function<bool(const SectionBase &Sec)>;
54
55static bool isDebugSection(const SectionBase &Sec) {
56 return StringRef(Sec.Name).startswith(".debug") ||
57 StringRef(Sec.Name).startswith(".zdebug") || Sec.Name == ".gdb_index";
58}
59
60static bool isDWOSection(const SectionBase &Sec) {
61 return StringRef(Sec.Name).endswith(".dwo");
62}
63
64static bool onlyKeepDWOPred(const Object &Obj, const SectionBase &Sec) {
65 // We can't remove the section header string table.
66 if (&Sec == Obj.SectionNames)
67 return false;
68 // Short of keeping the string table we want to keep everything that is a DWO
69 // section and remove everything else.
70 return !isDWOSection(Sec);
71}
72
73static ElfType getOutputElfType(const Binary &Bin) {
74 // Infer output ELF type from the input ELF object
75 if (isa<ELFObjectFile<ELF32LE>>(Bin))
76 return ELFT_ELF32LE;
77 if (isa<ELFObjectFile<ELF64LE>>(Bin))
78 return ELFT_ELF64LE;
79 if (isa<ELFObjectFile<ELF32BE>>(Bin))
80 return ELFT_ELF32BE;
81 if (isa<ELFObjectFile<ELF64BE>>(Bin))
82 return ELFT_ELF64BE;
83 llvm_unreachable("Invalid ELFType");
84}
85
86static ElfType getOutputElfType(const MachineInfo &MI) {
87 // Infer output ELF type from the binary arch specified
88 if (MI.Is64Bit)
89 return MI.IsLittleEndian ? ELFT_ELF64LE : ELFT_ELF64BE;
90 else
91 return MI.IsLittleEndian ? ELFT_ELF32LE : ELFT_ELF32BE;
92}
93
94static std::unique_ptr<Writer> createWriter(const CopyConfig &Config,
95 Object &Obj, Buffer &Buf,
96 ElfType OutputElfType) {
97 if (Config.OutputFormat == "binary") {
98 return llvm::make_unique<BinaryWriter>(Obj, Buf);
99 }
100 // Depending on the initial ELFT and OutputFormat we need a different Writer.
101 switch (OutputElfType) {
102 case ELFT_ELF32LE:
103 return llvm::make_unique<ELFWriter<ELF32LE>>(Obj, Buf,
104 !Config.StripSections);
105 case ELFT_ELF64LE:
106 return llvm::make_unique<ELFWriter<ELF64LE>>(Obj, Buf,
107 !Config.StripSections);
108 case ELFT_ELF32BE:
109 return llvm::make_unique<ELFWriter<ELF32BE>>(Obj, Buf,
110 !Config.StripSections);
111 case ELFT_ELF64BE:
112 return llvm::make_unique<ELFWriter<ELF64BE>>(Obj, Buf,
113 !Config.StripSections);
114 }
115 llvm_unreachable("Invalid output format");
116}
117
118static void splitDWOToFile(const CopyConfig &Config, const Reader &Reader,
119 StringRef File, ElfType OutputElfType) {
120 auto DWOFile = Reader.create();
121 DWOFile->removeSections(
122 [&](const SectionBase &Sec) { return onlyKeepDWOPred(*DWOFile, Sec); });
123 FileBuffer FB(File);
124 auto Writer = createWriter(Config, *DWOFile, FB, OutputElfType);
125 Writer->finalize();
126 Writer->write();
127}
128
129static Error dumpSectionToFile(StringRef SecName, StringRef Filename,
130 Object &Obj) {
131 for (auto &Sec : Obj.sections()) {
132 if (Sec.Name == SecName) {
133 if (Sec.OriginalData.size() == 0)
134 return make_error<StringError>("Can't dump section \"" + SecName +
135 "\": it has no contents",
136 object_error::parse_failed);
137 Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
138 FileOutputBuffer::create(Filename, Sec.OriginalData.size());
139 if (!BufferOrErr)
140 return BufferOrErr.takeError();
141 std::unique_ptr<FileOutputBuffer> Buf = std::move(*BufferOrErr);
142 std::copy(Sec.OriginalData.begin(), Sec.OriginalData.end(),
143 Buf->getBufferStart());
144 if (Error E = Buf->commit())
145 return E;
146 return Error::success();
147 }
148 }
149 return make_error<StringError>("Section not found",
150 object_error::parse_failed);
151}
152
153static bool isCompressed(const SectionBase &Section) {
154 const char *Magic = "ZLIB";
155 return StringRef(Section.Name).startswith(".zdebug") ||
156 (Section.OriginalData.size() > strlen(Magic) &&
157 !strncmp(reinterpret_cast<const char *>(Section.OriginalData.data()),
158 Magic, strlen(Magic))) ||
159 (Section.Flags & ELF::SHF_COMPRESSED);
160}
161
162static bool isCompressable(const SectionBase &Section) {
163 return !isCompressed(Section) && isDebugSection(Section) &&
164 Section.Name != ".gdb_index";
165}
166
167static void replaceDebugSections(
168 const CopyConfig &Config, Object &Obj, SectionPred &RemovePred,
169 function_ref<bool(const SectionBase &)> shouldReplace,
170 function_ref<SectionBase *(const SectionBase *)> addSection) {
171 SmallVector<SectionBase *, 13> ToReplace;
172 SmallVector<RelocationSection *, 13> RelocationSections;
173 for (auto &Sec : Obj.sections()) {
174 if (RelocationSection *R = dyn_cast<RelocationSection>(&Sec)) {
175 if (shouldReplace(*R->getSection()))
176 RelocationSections.push_back(R);
177 continue;
178 }
179
180 if (shouldReplace(Sec))
181 ToReplace.push_back(&Sec);
182 }
183
184 for (SectionBase *S : ToReplace) {
185 SectionBase *NewSection = addSection(S);
186
187 for (RelocationSection *RS : RelocationSections) {
188 if (RS->getSection() == S)
189 RS->setSection(NewSection);
190 }
191 }
192
193 RemovePred = [shouldReplace, RemovePred](const SectionBase &Sec) {
194 return shouldReplace(Sec) || RemovePred(Sec);
195 };
196}
197
198// This function handles the high level operations of GNU objcopy including
199// handling command line options. It's important to outline certain properties
200// we expect to hold of the command line operations. Any operation that "keeps"
201// should keep regardless of a remove. Additionally any removal should respect
202// any previous removals. Lastly whether or not something is removed shouldn't
203// depend a) on the order the options occur in or b) on some opaque priority
204// system. The only priority is that keeps/copies overrule removes.
205static void handleArgs(const CopyConfig &Config, Object &Obj,
206 const Reader &Reader, ElfType OutputElfType) {
207
208 if (!Config.SplitDWO.empty()) {
209 splitDWOToFile(Config, Reader, Config.SplitDWO, OutputElfType);
210 }
211
212 // TODO: update or remove symbols only if there is an option that affects
213 // them.
214 if (Obj.SymbolTable) {
215 Obj.SymbolTable->updateSymbols([&](Symbol &Sym) {
Jordan Rupprechtb47475c2018-11-01 17:26:36 +0000216 if (!Sym.isCommon() &&
217 ((Config.LocalizeHidden &&
218 (Sym.Visibility == STV_HIDDEN || Sym.Visibility == STV_INTERNAL)) ||
Fangrui Songe4ee0662018-11-29 17:32:51 +0000219 is_contained(Config.SymbolsToLocalize, Sym.Name)))
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000220 Sym.Binding = STB_LOCAL;
221
222 // Note: these two globalize flags have very similar names but different
223 // meanings:
224 //
225 // --globalize-symbol: promote a symbol to global
226 // --keep-global-symbol: all symbols except for these should be made local
227 //
228 // If --globalize-symbol is specified for a given symbol, it will be
229 // global in the output file even if it is not included via
230 // --keep-global-symbol. Because of that, make sure to check
231 // --globalize-symbol second.
232 if (!Config.SymbolsToKeepGlobal.empty() &&
Jordan Rupprecht634820d2018-10-30 16:23:38 +0000233 !is_contained(Config.SymbolsToKeepGlobal, Sym.Name) &&
234 Sym.getShndx() != SHN_UNDEF)
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000235 Sym.Binding = STB_LOCAL;
236
Fangrui Songe4ee0662018-11-29 17:32:51 +0000237 if (is_contained(Config.SymbolsToGlobalize, Sym.Name) &&
Jordan Rupprecht634820d2018-10-30 16:23:38 +0000238 Sym.getShndx() != SHN_UNDEF)
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000239 Sym.Binding = STB_GLOBAL;
240
Fangrui Songe4ee0662018-11-29 17:32:51 +0000241 if (is_contained(Config.SymbolsToWeaken, Sym.Name) &&
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000242 Sym.Binding == STB_GLOBAL)
243 Sym.Binding = STB_WEAK;
244
245 if (Config.Weaken && Sym.Binding == STB_GLOBAL &&
246 Sym.getShndx() != SHN_UNDEF)
247 Sym.Binding = STB_WEAK;
248
249 const auto I = Config.SymbolsToRename.find(Sym.Name);
250 if (I != Config.SymbolsToRename.end())
251 Sym.Name = I->getValue();
252
253 if (!Config.SymbolsPrefix.empty() && Sym.Type != STT_SECTION)
254 Sym.Name = (Config.SymbolsPrefix + Sym.Name).str();
255 });
256
257 // The purpose of this loop is to mark symbols referenced by sections
258 // (like GroupSection or RelocationSection). This way, we know which
259 // symbols are still 'needed' and which are not.
260 if (Config.StripUnneeded) {
261 for (auto &Section : Obj.sections())
262 Section.markSymbols();
263 }
264
265 Obj.removeSymbols([&](const Symbol &Sym) {
Fangrui Songe4ee0662018-11-29 17:32:51 +0000266 if (is_contained(Config.SymbolsToKeep, Sym.Name) ||
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000267 (Config.KeepFileSymbols && Sym.Type == STT_FILE))
268 return false;
269
270 if (Config.DiscardAll && Sym.Binding == STB_LOCAL &&
271 Sym.getShndx() != SHN_UNDEF && Sym.Type != STT_FILE &&
272 Sym.Type != STT_SECTION)
273 return true;
274
275 if (Config.StripAll || Config.StripAllGNU)
276 return true;
277
Fangrui Songe4ee0662018-11-29 17:32:51 +0000278 if (is_contained(Config.SymbolsToRemove, Sym.Name))
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000279 return true;
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000280
281 if (Config.StripUnneeded && !Sym.Referenced &&
282 (Sym.Binding == STB_LOCAL || Sym.getShndx() == SHN_UNDEF) &&
283 Sym.Type != STT_FILE && Sym.Type != STT_SECTION)
284 return true;
285
286 return false;
287 });
288 }
289
290 SectionPred RemovePred = [](const SectionBase &) { return false; };
291
292 // Removes:
293 if (!Config.ToRemove.empty()) {
294 RemovePred = [&Config](const SectionBase &Sec) {
295 return is_contained(Config.ToRemove, Sec.Name);
296 };
297 }
298
299 if (Config.StripDWO || !Config.SplitDWO.empty())
300 RemovePred = [RemovePred](const SectionBase &Sec) {
301 return isDWOSection(Sec) || RemovePred(Sec);
302 };
303
304 if (Config.ExtractDWO)
305 RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
306 return onlyKeepDWOPred(Obj, Sec) || RemovePred(Sec);
307 };
308
309 if (Config.StripAllGNU)
310 RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
311 if (RemovePred(Sec))
312 return true;
313 if ((Sec.Flags & SHF_ALLOC) != 0)
314 return false;
315 if (&Sec == Obj.SectionNames)
316 return false;
317 switch (Sec.Type) {
318 case SHT_SYMTAB:
319 case SHT_REL:
320 case SHT_RELA:
321 case SHT_STRTAB:
322 return true;
323 }
324 return isDebugSection(Sec);
325 };
326
327 if (Config.StripSections) {
328 RemovePred = [RemovePred](const SectionBase &Sec) {
329 return RemovePred(Sec) || (Sec.Flags & SHF_ALLOC) == 0;
330 };
331 }
332
333 if (Config.StripDebug) {
334 RemovePred = [RemovePred](const SectionBase &Sec) {
335 return RemovePred(Sec) || isDebugSection(Sec);
336 };
337 }
338
339 if (Config.StripNonAlloc)
340 RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
341 if (RemovePred(Sec))
342 return true;
343 if (&Sec == Obj.SectionNames)
344 return false;
345 return (Sec.Flags & SHF_ALLOC) == 0;
346 };
347
348 if (Config.StripAll)
349 RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
350 if (RemovePred(Sec))
351 return true;
352 if (&Sec == Obj.SectionNames)
353 return false;
354 if (StringRef(Sec.Name).startswith(".gnu.warning"))
355 return false;
356 return (Sec.Flags & SHF_ALLOC) == 0;
357 };
358
359 // Explicit copies:
360 if (!Config.OnlyKeep.empty()) {
361 RemovePred = [&Config, RemovePred, &Obj](const SectionBase &Sec) {
362 // Explicitly keep these sections regardless of previous removes.
363 if (is_contained(Config.OnlyKeep, Sec.Name))
364 return false;
365
366 // Allow all implicit removes.
367 if (RemovePred(Sec))
368 return true;
369
370 // Keep special sections.
371 if (Obj.SectionNames == &Sec)
372 return false;
373 if (Obj.SymbolTable == &Sec ||
374 (Obj.SymbolTable && Obj.SymbolTable->getStrTab() == &Sec))
375 return false;
376
377 // Remove everything else.
378 return true;
379 };
380 }
381
Jordan Rupprechtc5bae782018-11-13 19:32:27 +0000382 if (!Config.KeepSection.empty()) {
Fangrui Songe9f34b02018-11-12 23:46:22 +0000383 RemovePred = [&Config, RemovePred](const SectionBase &Sec) {
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000384 // Explicitly keep these sections regardless of previous removes.
Jordan Rupprechtc5bae782018-11-13 19:32:27 +0000385 if (is_contained(Config.KeepSection, Sec.Name))
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000386 return false;
387 // Otherwise defer to RemovePred.
388 return RemovePred(Sec);
389 };
390 }
391
392 // This has to be the last predicate assignment.
393 // If the option --keep-symbol has been specified
394 // and at least one of those symbols is present
395 // (equivalently, the updated symbol table is not empty)
396 // the symbol table and the string table should not be removed.
397 if ((!Config.SymbolsToKeep.empty() || Config.KeepFileSymbols) &&
398 Obj.SymbolTable && !Obj.SymbolTable->empty()) {
399 RemovePred = [&Obj, RemovePred](const SectionBase &Sec) {
400 if (&Sec == Obj.SymbolTable || &Sec == Obj.SymbolTable->getStrTab())
401 return false;
402 return RemovePred(Sec);
403 };
404 }
405
406 if (Config.CompressionType != DebugCompressionType::None)
407 replaceDebugSections(Config, Obj, RemovePred, isCompressable,
408 [&Config, &Obj](const SectionBase *S) {
409 return &Obj.addSection<CompressedSection>(
410 *S, Config.CompressionType);
411 });
412 else if (Config.DecompressDebugSections)
413 replaceDebugSections(
414 Config, Obj, RemovePred,
415 [](const SectionBase &S) { return isa<CompressedSection>(&S); },
416 [&Obj](const SectionBase *S) {
417 auto CS = cast<CompressedSection>(S);
418 return &Obj.addSection<DecompressedSection>(*CS);
419 });
420
421 Obj.removeSections(RemovePred);
422
423 if (!Config.SectionsToRename.empty()) {
424 for (auto &Sec : Obj.sections()) {
425 const auto Iter = Config.SectionsToRename.find(Sec.Name);
426 if (Iter != Config.SectionsToRename.end()) {
427 const SectionRename &SR = Iter->second;
428 Sec.Name = SR.NewName;
429 if (SR.NewFlags.hasValue()) {
430 // Preserve some flags which should not be dropped when setting flags.
431 // Also, preserve anything OS/processor dependant.
432 const uint64_t PreserveMask = ELF::SHF_COMPRESSED | ELF::SHF_EXCLUDE |
433 ELF::SHF_GROUP | ELF::SHF_LINK_ORDER |
434 ELF::SHF_MASKOS | ELF::SHF_MASKPROC |
435 ELF::SHF_TLS | ELF::SHF_INFO_LINK;
436 Sec.Flags = (Sec.Flags & PreserveMask) |
437 (SR.NewFlags.getValue() & ~PreserveMask);
438 }
439 }
440 }
441 }
442
443 if (!Config.AddSection.empty()) {
444 for (const auto &Flag : Config.AddSection) {
445 auto SecPair = Flag.split("=");
446 auto SecName = SecPair.first;
447 auto File = SecPair.second;
448 auto BufOrErr = MemoryBuffer::getFile(File);
449 if (!BufOrErr)
450 reportError(File, BufOrErr.getError());
451 auto Buf = std::move(*BufOrErr);
452 auto BufPtr = reinterpret_cast<const uint8_t *>(Buf->getBufferStart());
453 auto BufSize = Buf->getBufferSize();
454 Obj.addSection<OwnedDataSection>(SecName,
455 ArrayRef<uint8_t>(BufPtr, BufSize));
456 }
457 }
458
459 if (!Config.DumpSection.empty()) {
460 for (const auto &Flag : Config.DumpSection) {
461 std::pair<StringRef, StringRef> SecPair = Flag.split("=");
462 StringRef SecName = SecPair.first;
463 StringRef File = SecPair.second;
464 if (Error E = dumpSectionToFile(SecName, File, Obj))
465 reportError(Config.InputFilename, std::move(E));
466 }
467 }
468
469 if (!Config.AddGnuDebugLink.empty())
470 Obj.addSection<GnuDebugLinkSection>(Config.AddGnuDebugLink);
471}
472
473void executeObjcopyOnRawBinary(const CopyConfig &Config, MemoryBuffer &In,
474 Buffer &Out) {
475 BinaryReader Reader(Config.BinaryArch, &In);
476 std::unique_ptr<Object> Obj = Reader.create();
477
478 const ElfType OutputElfType = getOutputElfType(Config.BinaryArch);
479 handleArgs(Config, *Obj, Reader, OutputElfType);
480 std::unique_ptr<Writer> Writer =
481 createWriter(Config, *Obj, Out, OutputElfType);
482 Writer->finalize();
483 Writer->write();
484}
485
486void executeObjcopyOnBinary(const CopyConfig &Config,
487 object::ELFObjectFileBase &In, Buffer &Out) {
488 ELFReader Reader(&In);
489 std::unique_ptr<Object> Obj = Reader.create();
490 const ElfType OutputElfType = getOutputElfType(In);
491 handleArgs(Config, *Obj, Reader, OutputElfType);
492 std::unique_ptr<Writer> Writer =
493 createWriter(Config, *Obj, Out, OutputElfType);
494 Writer->finalize();
495 Writer->write();
496}
497
498} // end namespace elf
499} // end namespace objcopy
500} // end namespace llvm