blob: 8a136de24a87bce94a80e5493e13e82cace4e30b [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"
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +000013#include "Object.h"
Jake Ehrlich8ad77792018-12-03 19:49:23 +000014#include "llvm-objcopy.h"
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +000015
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"
Jake Ehrlich8ad77792018-12-03 19:49:23 +000031#include "llvm/Support/Errc.h"
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +000032#include "llvm/Support/Error.h"
33#include "llvm/Support/ErrorHandling.h"
34#include "llvm/Support/ErrorOr.h"
35#include "llvm/Support/Memory.h"
36#include "llvm/Support/Path.h"
37#include "llvm/Support/raw_ostream.h"
38#include <algorithm>
39#include <cassert>
40#include <cstdlib>
41#include <functional>
42#include <iterator>
43#include <memory>
44#include <string>
45#include <system_error>
46#include <utility>
47
48namespace llvm {
49namespace objcopy {
50namespace elf {
51
52using namespace object;
53using namespace ELF;
54using SectionPred = std::function<bool(const SectionBase &Sec)>;
55
56static bool isDebugSection(const SectionBase &Sec) {
57 return StringRef(Sec.Name).startswith(".debug") ||
58 StringRef(Sec.Name).startswith(".zdebug") || Sec.Name == ".gdb_index";
59}
60
61static bool isDWOSection(const SectionBase &Sec) {
62 return StringRef(Sec.Name).endswith(".dwo");
63}
64
65static bool onlyKeepDWOPred(const Object &Obj, const SectionBase &Sec) {
66 // We can't remove the section header string table.
67 if (&Sec == Obj.SectionNames)
68 return false;
69 // Short of keeping the string table we want to keep everything that is a DWO
70 // section and remove everything else.
71 return !isDWOSection(Sec);
72}
73
74static ElfType getOutputElfType(const Binary &Bin) {
75 // Infer output ELF type from the input ELF object
76 if (isa<ELFObjectFile<ELF32LE>>(Bin))
77 return ELFT_ELF32LE;
78 if (isa<ELFObjectFile<ELF64LE>>(Bin))
79 return ELFT_ELF64LE;
80 if (isa<ELFObjectFile<ELF32BE>>(Bin))
81 return ELFT_ELF32BE;
82 if (isa<ELFObjectFile<ELF64BE>>(Bin))
83 return ELFT_ELF64BE;
84 llvm_unreachable("Invalid ELFType");
85}
86
87static ElfType getOutputElfType(const MachineInfo &MI) {
88 // Infer output ELF type from the binary arch specified
89 if (MI.Is64Bit)
90 return MI.IsLittleEndian ? ELFT_ELF64LE : ELFT_ELF64BE;
91 else
92 return MI.IsLittleEndian ? ELFT_ELF32LE : ELFT_ELF32BE;
93}
94
95static std::unique_ptr<Writer> createWriter(const CopyConfig &Config,
96 Object &Obj, Buffer &Buf,
97 ElfType OutputElfType) {
98 if (Config.OutputFormat == "binary") {
99 return llvm::make_unique<BinaryWriter>(Obj, Buf);
100 }
101 // Depending on the initial ELFT and OutputFormat we need a different Writer.
102 switch (OutputElfType) {
103 case ELFT_ELF32LE:
104 return llvm::make_unique<ELFWriter<ELF32LE>>(Obj, Buf,
105 !Config.StripSections);
106 case ELFT_ELF64LE:
107 return llvm::make_unique<ELFWriter<ELF64LE>>(Obj, Buf,
108 !Config.StripSections);
109 case ELFT_ELF32BE:
110 return llvm::make_unique<ELFWriter<ELF32BE>>(Obj, Buf,
111 !Config.StripSections);
112 case ELFT_ELF64BE:
113 return llvm::make_unique<ELFWriter<ELF64BE>>(Obj, Buf,
114 !Config.StripSections);
115 }
116 llvm_unreachable("Invalid output format");
117}
118
Jake Ehrlich8ad77792018-12-03 19:49:23 +0000119template <class ELFT>
120static Expected<ArrayRef<uint8_t>>
121findBuildID(const object::ELFFile<ELFT> &In) {
122 for (const auto &Phdr : unwrapOrError(In.program_headers())) {
123 if (Phdr.p_type != PT_NOTE)
124 continue;
125 Error Err = Error::success();
David Blaikieba005aa2018-12-11 00:09:06 +0000126 for (const auto &Note : In.notes(Phdr, Err))
Jake Ehrlich8ad77792018-12-03 19:49:23 +0000127 if (Note.getType() == NT_GNU_BUILD_ID && Note.getName() == ELF_NOTE_GNU)
128 return Note.getDesc();
Jake Ehrlich8ad77792018-12-03 19:49:23 +0000129 if (Err)
130 return std::move(Err);
131 }
132 return createStringError(llvm::errc::invalid_argument,
133 "Could not find build ID.");
134}
135
136static Expected<ArrayRef<uint8_t>>
137findBuildID(const object::ELFObjectFileBase &In) {
138 if (auto *O = dyn_cast<ELFObjectFile<ELF32LE>>(&In))
139 return findBuildID(*O->getELFFile());
140 else if (auto *O = dyn_cast<ELFObjectFile<ELF64LE>>(&In))
141 return findBuildID(*O->getELFFile());
142 else if (auto *O = dyn_cast<ELFObjectFile<ELF32BE>>(&In))
143 return findBuildID(*O->getELFFile());
144 else if (auto *O = dyn_cast<ELFObjectFile<ELF64BE>>(&In))
145 return findBuildID(*O->getELFFile());
146
147 llvm_unreachable("Bad file format");
148}
149
150static void linkToBuildIdDir(const CopyConfig &Config, StringRef ToLink,
151 StringRef Suffix, ArrayRef<uint8_t> BuildIdBytes) {
152 SmallString<128> Path = Config.BuildIdLinkDir;
153 sys::path::append(Path, llvm::toHex(BuildIdBytes[0], /*LowerCase*/ true));
154 if (auto EC = sys::fs::create_directories(Path))
155 error("cannot create build ID link directory " + Path + ": " +
156 EC.message());
157
158 sys::path::append(Path,
159 llvm::toHex(BuildIdBytes.slice(1), /*LowerCase*/ true));
160 Path += Suffix;
161 if (auto EC = sys::fs::create_hard_link(ToLink, Path)) {
162 // Hard linking failed, try to remove the file first if it exists.
163 if (sys::fs::exists(Path))
164 sys::fs::remove(Path);
165 EC = sys::fs::create_hard_link(ToLink, Path);
166 if (EC)
167 error("cannot link " + ToLink + " to " + Path + ": " + EC.message());
168 }
169}
170
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000171static 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 FileBuffer FB(File);
177 auto Writer = createWriter(Config, *DWOFile, FB, OutputElfType);
178 Writer->finalize();
179 Writer->write();
180}
181
182static Error dumpSectionToFile(StringRef SecName, StringRef Filename,
183 Object &Obj) {
184 for (auto &Sec : Obj.sections()) {
185 if (Sec.Name == SecName) {
186 if (Sec.OriginalData.size() == 0)
187 return make_error<StringError>("Can't dump section \"" + SecName +
188 "\": it has no contents",
189 object_error::parse_failed);
190 Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
191 FileOutputBuffer::create(Filename, Sec.OriginalData.size());
192 if (!BufferOrErr)
193 return BufferOrErr.takeError();
194 std::unique_ptr<FileOutputBuffer> Buf = std::move(*BufferOrErr);
195 std::copy(Sec.OriginalData.begin(), Sec.OriginalData.end(),
196 Buf->getBufferStart());
197 if (Error E = Buf->commit())
198 return E;
199 return Error::success();
200 }
201 }
202 return make_error<StringError>("Section not found",
203 object_error::parse_failed);
204}
205
206static bool isCompressed(const SectionBase &Section) {
207 const char *Magic = "ZLIB";
208 return StringRef(Section.Name).startswith(".zdebug") ||
209 (Section.OriginalData.size() > strlen(Magic) &&
210 !strncmp(reinterpret_cast<const char *>(Section.OriginalData.data()),
211 Magic, strlen(Magic))) ||
212 (Section.Flags & ELF::SHF_COMPRESSED);
213}
214
215static bool isCompressable(const SectionBase &Section) {
216 return !isCompressed(Section) && isDebugSection(Section) &&
217 Section.Name != ".gdb_index";
218}
219
220static void replaceDebugSections(
221 const CopyConfig &Config, Object &Obj, SectionPred &RemovePred,
222 function_ref<bool(const SectionBase &)> shouldReplace,
223 function_ref<SectionBase *(const SectionBase *)> addSection) {
224 SmallVector<SectionBase *, 13> ToReplace;
225 SmallVector<RelocationSection *, 13> RelocationSections;
226 for (auto &Sec : Obj.sections()) {
227 if (RelocationSection *R = dyn_cast<RelocationSection>(&Sec)) {
228 if (shouldReplace(*R->getSection()))
229 RelocationSections.push_back(R);
230 continue;
231 }
232
233 if (shouldReplace(Sec))
234 ToReplace.push_back(&Sec);
235 }
236
237 for (SectionBase *S : ToReplace) {
238 SectionBase *NewSection = addSection(S);
239
240 for (RelocationSection *RS : RelocationSections) {
241 if (RS->getSection() == S)
242 RS->setSection(NewSection);
243 }
244 }
245
246 RemovePred = [shouldReplace, RemovePred](const SectionBase &Sec) {
247 return shouldReplace(Sec) || RemovePred(Sec);
248 };
249}
250
251// This function handles the high level operations of GNU objcopy including
252// handling command line options. It's important to outline certain properties
253// we expect to hold of the command line operations. Any operation that "keeps"
254// should keep regardless of a remove. Additionally any removal should respect
255// any previous removals. Lastly whether or not something is removed shouldn't
256// depend a) on the order the options occur in or b) on some opaque priority
257// system. The only priority is that keeps/copies overrule removes.
258static void handleArgs(const CopyConfig &Config, Object &Obj,
259 const Reader &Reader, ElfType OutputElfType) {
260
261 if (!Config.SplitDWO.empty()) {
262 splitDWOToFile(Config, Reader, Config.SplitDWO, OutputElfType);
263 }
264
265 // TODO: update or remove symbols only if there is an option that affects
266 // them.
267 if (Obj.SymbolTable) {
268 Obj.SymbolTable->updateSymbols([&](Symbol &Sym) {
Jordan Rupprechtb47475c2018-11-01 17:26:36 +0000269 if (!Sym.isCommon() &&
270 ((Config.LocalizeHidden &&
271 (Sym.Visibility == STV_HIDDEN || Sym.Visibility == STV_INTERNAL)) ||
Fangrui Songe4ee0662018-11-29 17:32:51 +0000272 is_contained(Config.SymbolsToLocalize, Sym.Name)))
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000273 Sym.Binding = STB_LOCAL;
274
275 // Note: these two globalize flags have very similar names but different
276 // meanings:
277 //
278 // --globalize-symbol: promote a symbol to global
279 // --keep-global-symbol: all symbols except for these should be made local
280 //
281 // If --globalize-symbol is specified for a given symbol, it will be
282 // global in the output file even if it is not included via
283 // --keep-global-symbol. Because of that, make sure to check
284 // --globalize-symbol second.
285 if (!Config.SymbolsToKeepGlobal.empty() &&
Jordan Rupprecht634820d2018-10-30 16:23:38 +0000286 !is_contained(Config.SymbolsToKeepGlobal, Sym.Name) &&
287 Sym.getShndx() != SHN_UNDEF)
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000288 Sym.Binding = STB_LOCAL;
289
Fangrui Songe4ee0662018-11-29 17:32:51 +0000290 if (is_contained(Config.SymbolsToGlobalize, Sym.Name) &&
Jordan Rupprecht634820d2018-10-30 16:23:38 +0000291 Sym.getShndx() != SHN_UNDEF)
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000292 Sym.Binding = STB_GLOBAL;
293
Fangrui Songe4ee0662018-11-29 17:32:51 +0000294 if (is_contained(Config.SymbolsToWeaken, Sym.Name) &&
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000295 Sym.Binding == STB_GLOBAL)
296 Sym.Binding = STB_WEAK;
297
298 if (Config.Weaken && Sym.Binding == STB_GLOBAL &&
299 Sym.getShndx() != SHN_UNDEF)
300 Sym.Binding = STB_WEAK;
301
302 const auto I = Config.SymbolsToRename.find(Sym.Name);
303 if (I != Config.SymbolsToRename.end())
304 Sym.Name = I->getValue();
305
306 if (!Config.SymbolsPrefix.empty() && Sym.Type != STT_SECTION)
307 Sym.Name = (Config.SymbolsPrefix + Sym.Name).str();
308 });
309
310 // The purpose of this loop is to mark symbols referenced by sections
311 // (like GroupSection or RelocationSection). This way, we know which
312 // symbols are still 'needed' and which are not.
313 if (Config.StripUnneeded) {
314 for (auto &Section : Obj.sections())
315 Section.markSymbols();
316 }
317
318 Obj.removeSymbols([&](const Symbol &Sym) {
Fangrui Songe4ee0662018-11-29 17:32:51 +0000319 if (is_contained(Config.SymbolsToKeep, Sym.Name) ||
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000320 (Config.KeepFileSymbols && Sym.Type == STT_FILE))
321 return false;
322
323 if (Config.DiscardAll && Sym.Binding == STB_LOCAL &&
324 Sym.getShndx() != SHN_UNDEF && Sym.Type != STT_FILE &&
325 Sym.Type != STT_SECTION)
326 return true;
327
328 if (Config.StripAll || Config.StripAllGNU)
329 return true;
330
Fangrui Songe4ee0662018-11-29 17:32:51 +0000331 if (is_contained(Config.SymbolsToRemove, Sym.Name))
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000332 return true;
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000333
334 if (Config.StripUnneeded && !Sym.Referenced &&
335 (Sym.Binding == STB_LOCAL || Sym.getShndx() == SHN_UNDEF) &&
336 Sym.Type != STT_FILE && Sym.Type != STT_SECTION)
337 return true;
338
339 return false;
340 });
341 }
342
343 SectionPred RemovePred = [](const SectionBase &) { return false; };
344
345 // Removes:
346 if (!Config.ToRemove.empty()) {
347 RemovePred = [&Config](const SectionBase &Sec) {
348 return is_contained(Config.ToRemove, Sec.Name);
349 };
350 }
351
352 if (Config.StripDWO || !Config.SplitDWO.empty())
353 RemovePred = [RemovePred](const SectionBase &Sec) {
354 return isDWOSection(Sec) || RemovePred(Sec);
355 };
356
357 if (Config.ExtractDWO)
358 RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
359 return onlyKeepDWOPred(Obj, Sec) || RemovePred(Sec);
360 };
361
362 if (Config.StripAllGNU)
363 RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
364 if (RemovePred(Sec))
365 return true;
366 if ((Sec.Flags & SHF_ALLOC) != 0)
367 return false;
368 if (&Sec == Obj.SectionNames)
369 return false;
370 switch (Sec.Type) {
371 case SHT_SYMTAB:
372 case SHT_REL:
373 case SHT_RELA:
374 case SHT_STRTAB:
375 return true;
376 }
377 return isDebugSection(Sec);
378 };
379
380 if (Config.StripSections) {
381 RemovePred = [RemovePred](const SectionBase &Sec) {
382 return RemovePred(Sec) || (Sec.Flags & SHF_ALLOC) == 0;
383 };
384 }
385
386 if (Config.StripDebug) {
387 RemovePred = [RemovePred](const SectionBase &Sec) {
388 return RemovePred(Sec) || isDebugSection(Sec);
389 };
390 }
391
392 if (Config.StripNonAlloc)
393 RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
394 if (RemovePred(Sec))
395 return true;
396 if (&Sec == Obj.SectionNames)
397 return false;
398 return (Sec.Flags & SHF_ALLOC) == 0;
399 };
400
401 if (Config.StripAll)
402 RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
403 if (RemovePred(Sec))
404 return true;
405 if (&Sec == Obj.SectionNames)
406 return false;
407 if (StringRef(Sec.Name).startswith(".gnu.warning"))
408 return false;
409 return (Sec.Flags & SHF_ALLOC) == 0;
410 };
411
412 // Explicit copies:
Jake Ehrlich85985ed2018-12-06 02:03:53 +0000413 if (!Config.OnlySection.empty()) {
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000414 RemovePred = [&Config, RemovePred, &Obj](const SectionBase &Sec) {
415 // Explicitly keep these sections regardless of previous removes.
Jake Ehrlich85985ed2018-12-06 02:03:53 +0000416 if (is_contained(Config.OnlySection, Sec.Name))
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000417 return false;
418
419 // Allow all implicit removes.
420 if (RemovePred(Sec))
421 return true;
422
423 // Keep special sections.
424 if (Obj.SectionNames == &Sec)
425 return false;
426 if (Obj.SymbolTable == &Sec ||
427 (Obj.SymbolTable && Obj.SymbolTable->getStrTab() == &Sec))
428 return false;
429
430 // Remove everything else.
431 return true;
432 };
433 }
434
Jordan Rupprechtc5bae782018-11-13 19:32:27 +0000435 if (!Config.KeepSection.empty()) {
Fangrui Songe9f34b02018-11-12 23:46:22 +0000436 RemovePred = [&Config, RemovePred](const SectionBase &Sec) {
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000437 // Explicitly keep these sections regardless of previous removes.
Jordan Rupprechtc5bae782018-11-13 19:32:27 +0000438 if (is_contained(Config.KeepSection, Sec.Name))
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000439 return false;
440 // Otherwise defer to RemovePred.
441 return RemovePred(Sec);
442 };
443 }
444
445 // This has to be the last predicate assignment.
446 // If the option --keep-symbol has been specified
447 // and at least one of those symbols is present
448 // (equivalently, the updated symbol table is not empty)
449 // the symbol table and the string table should not be removed.
450 if ((!Config.SymbolsToKeep.empty() || Config.KeepFileSymbols) &&
451 Obj.SymbolTable && !Obj.SymbolTable->empty()) {
452 RemovePred = [&Obj, RemovePred](const SectionBase &Sec) {
453 if (&Sec == Obj.SymbolTable || &Sec == Obj.SymbolTable->getStrTab())
454 return false;
455 return RemovePred(Sec);
456 };
457 }
458
459 if (Config.CompressionType != DebugCompressionType::None)
460 replaceDebugSections(Config, Obj, RemovePred, isCompressable,
461 [&Config, &Obj](const SectionBase *S) {
462 return &Obj.addSection<CompressedSection>(
463 *S, Config.CompressionType);
464 });
465 else if (Config.DecompressDebugSections)
466 replaceDebugSections(
467 Config, Obj, RemovePred,
468 [](const SectionBase &S) { return isa<CompressedSection>(&S); },
469 [&Obj](const SectionBase *S) {
470 auto CS = cast<CompressedSection>(S);
471 return &Obj.addSection<DecompressedSection>(*CS);
472 });
473
474 Obj.removeSections(RemovePred);
475
476 if (!Config.SectionsToRename.empty()) {
477 for (auto &Sec : Obj.sections()) {
478 const auto Iter = Config.SectionsToRename.find(Sec.Name);
479 if (Iter != Config.SectionsToRename.end()) {
480 const SectionRename &SR = Iter->second;
481 Sec.Name = SR.NewName;
482 if (SR.NewFlags.hasValue()) {
483 // Preserve some flags which should not be dropped when setting flags.
484 // Also, preserve anything OS/processor dependant.
485 const uint64_t PreserveMask = ELF::SHF_COMPRESSED | ELF::SHF_EXCLUDE |
486 ELF::SHF_GROUP | ELF::SHF_LINK_ORDER |
487 ELF::SHF_MASKOS | ELF::SHF_MASKPROC |
488 ELF::SHF_TLS | ELF::SHF_INFO_LINK;
489 Sec.Flags = (Sec.Flags & PreserveMask) |
490 (SR.NewFlags.getValue() & ~PreserveMask);
491 }
492 }
493 }
494 }
495
496 if (!Config.AddSection.empty()) {
497 for (const auto &Flag : Config.AddSection) {
498 auto SecPair = Flag.split("=");
499 auto SecName = SecPair.first;
500 auto File = SecPair.second;
501 auto BufOrErr = MemoryBuffer::getFile(File);
502 if (!BufOrErr)
503 reportError(File, BufOrErr.getError());
504 auto Buf = std::move(*BufOrErr);
505 auto BufPtr = reinterpret_cast<const uint8_t *>(Buf->getBufferStart());
506 auto BufSize = Buf->getBufferSize();
507 Obj.addSection<OwnedDataSection>(SecName,
508 ArrayRef<uint8_t>(BufPtr, BufSize));
509 }
510 }
511
512 if (!Config.DumpSection.empty()) {
513 for (const auto &Flag : Config.DumpSection) {
514 std::pair<StringRef, StringRef> SecPair = Flag.split("=");
515 StringRef SecName = SecPair.first;
516 StringRef File = SecPair.second;
517 if (Error E = dumpSectionToFile(SecName, File, Obj))
518 reportError(Config.InputFilename, std::move(E));
519 }
520 }
521
522 if (!Config.AddGnuDebugLink.empty())
523 Obj.addSection<GnuDebugLinkSection>(Config.AddGnuDebugLink);
524}
525
526void executeObjcopyOnRawBinary(const CopyConfig &Config, MemoryBuffer &In,
527 Buffer &Out) {
528 BinaryReader Reader(Config.BinaryArch, &In);
529 std::unique_ptr<Object> Obj = Reader.create();
530
531 const ElfType OutputElfType = getOutputElfType(Config.BinaryArch);
532 handleArgs(Config, *Obj, Reader, OutputElfType);
533 std::unique_ptr<Writer> Writer =
534 createWriter(Config, *Obj, Out, OutputElfType);
535 Writer->finalize();
536 Writer->write();
537}
538
539void executeObjcopyOnBinary(const CopyConfig &Config,
540 object::ELFObjectFileBase &In, Buffer &Out) {
541 ELFReader Reader(&In);
542 std::unique_ptr<Object> Obj = Reader.create();
543 const ElfType OutputElfType = getOutputElfType(In);
Jake Ehrlich8ad77792018-12-03 19:49:23 +0000544 ArrayRef<uint8_t> BuildIdBytes;
545
546 if (!Config.BuildIdLinkDir.empty()) {
547 BuildIdBytes = unwrapOrError(findBuildID(In));
548 if (BuildIdBytes.size() < 2)
549 error("build ID in file '" + Config.InputFilename +
550 "' is smaller than two bytes");
551 }
552
553 if (!Config.BuildIdLinkDir.empty() && Config.BuildIdLinkInput) {
554 linkToBuildIdDir(Config, Config.InputFilename,
555 Config.BuildIdLinkInput.getValue(), BuildIdBytes);
556 }
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000557 handleArgs(Config, *Obj, Reader, OutputElfType);
558 std::unique_ptr<Writer> Writer =
559 createWriter(Config, *Obj, Out, OutputElfType);
560 Writer->finalize();
561 Writer->write();
Jake Ehrlich8ad77792018-12-03 19:49:23 +0000562 if (!Config.BuildIdLinkDir.empty() && Config.BuildIdLinkOutput) {
563 linkToBuildIdDir(Config, Config.OutputFilename,
564 Config.BuildIdLinkOutput.getValue(), BuildIdBytes);
565 }
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000566}
567
568} // end namespace elf
569} // end namespace objcopy
570} // end namespace llvm