blob: 3b9f62e9b5fe9ba654dbe63d23af5149094f0e2c [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();
126 if (Err)
127 llvm_unreachable("Error::success() was an error.");
128 for (const auto &Note : In.notes(Phdr, Err)) {
129 if (Err)
130 return std::move(Err);
131 if (Note.getType() == NT_GNU_BUILD_ID && Note.getName() == ELF_NOTE_GNU)
132 return Note.getDesc();
133 }
134 if (Err)
135 return std::move(Err);
136 }
137 return createStringError(llvm::errc::invalid_argument,
138 "Could not find build ID.");
139}
140
141static Expected<ArrayRef<uint8_t>>
142findBuildID(const object::ELFObjectFileBase &In) {
143 if (auto *O = dyn_cast<ELFObjectFile<ELF32LE>>(&In))
144 return findBuildID(*O->getELFFile());
145 else if (auto *O = dyn_cast<ELFObjectFile<ELF64LE>>(&In))
146 return findBuildID(*O->getELFFile());
147 else if (auto *O = dyn_cast<ELFObjectFile<ELF32BE>>(&In))
148 return findBuildID(*O->getELFFile());
149 else if (auto *O = dyn_cast<ELFObjectFile<ELF64BE>>(&In))
150 return findBuildID(*O->getELFFile());
151
152 llvm_unreachable("Bad file format");
153}
154
155static void linkToBuildIdDir(const CopyConfig &Config, StringRef ToLink,
156 StringRef Suffix, ArrayRef<uint8_t> BuildIdBytes) {
157 SmallString<128> Path = Config.BuildIdLinkDir;
158 sys::path::append(Path, llvm::toHex(BuildIdBytes[0], /*LowerCase*/ true));
159 if (auto EC = sys::fs::create_directories(Path))
160 error("cannot create build ID link directory " + Path + ": " +
161 EC.message());
162
163 sys::path::append(Path,
164 llvm::toHex(BuildIdBytes.slice(1), /*LowerCase*/ true));
165 Path += Suffix;
166 if (auto EC = sys::fs::create_hard_link(ToLink, Path)) {
167 // Hard linking failed, try to remove the file first if it exists.
168 if (sys::fs::exists(Path))
169 sys::fs::remove(Path);
170 EC = sys::fs::create_hard_link(ToLink, Path);
171 if (EC)
172 error("cannot link " + ToLink + " to " + Path + ": " + EC.message());
173 }
174}
175
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000176static void splitDWOToFile(const CopyConfig &Config, const Reader &Reader,
177 StringRef File, ElfType OutputElfType) {
178 auto DWOFile = Reader.create();
179 DWOFile->removeSections(
180 [&](const SectionBase &Sec) { return onlyKeepDWOPred(*DWOFile, Sec); });
181 FileBuffer FB(File);
182 auto Writer = createWriter(Config, *DWOFile, FB, OutputElfType);
183 Writer->finalize();
184 Writer->write();
185}
186
187static Error dumpSectionToFile(StringRef SecName, StringRef Filename,
188 Object &Obj) {
189 for (auto &Sec : Obj.sections()) {
190 if (Sec.Name == SecName) {
191 if (Sec.OriginalData.size() == 0)
192 return make_error<StringError>("Can't dump section \"" + SecName +
193 "\": it has no contents",
194 object_error::parse_failed);
195 Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
196 FileOutputBuffer::create(Filename, Sec.OriginalData.size());
197 if (!BufferOrErr)
198 return BufferOrErr.takeError();
199 std::unique_ptr<FileOutputBuffer> Buf = std::move(*BufferOrErr);
200 std::copy(Sec.OriginalData.begin(), Sec.OriginalData.end(),
201 Buf->getBufferStart());
202 if (Error E = Buf->commit())
203 return E;
204 return Error::success();
205 }
206 }
207 return make_error<StringError>("Section not found",
208 object_error::parse_failed);
209}
210
211static bool isCompressed(const SectionBase &Section) {
212 const char *Magic = "ZLIB";
213 return StringRef(Section.Name).startswith(".zdebug") ||
214 (Section.OriginalData.size() > strlen(Magic) &&
215 !strncmp(reinterpret_cast<const char *>(Section.OriginalData.data()),
216 Magic, strlen(Magic))) ||
217 (Section.Flags & ELF::SHF_COMPRESSED);
218}
219
220static bool isCompressable(const SectionBase &Section) {
221 return !isCompressed(Section) && isDebugSection(Section) &&
222 Section.Name != ".gdb_index";
223}
224
225static void replaceDebugSections(
226 const CopyConfig &Config, Object &Obj, SectionPred &RemovePred,
227 function_ref<bool(const SectionBase &)> shouldReplace,
228 function_ref<SectionBase *(const SectionBase *)> addSection) {
229 SmallVector<SectionBase *, 13> ToReplace;
230 SmallVector<RelocationSection *, 13> RelocationSections;
231 for (auto &Sec : Obj.sections()) {
232 if (RelocationSection *R = dyn_cast<RelocationSection>(&Sec)) {
233 if (shouldReplace(*R->getSection()))
234 RelocationSections.push_back(R);
235 continue;
236 }
237
238 if (shouldReplace(Sec))
239 ToReplace.push_back(&Sec);
240 }
241
242 for (SectionBase *S : ToReplace) {
243 SectionBase *NewSection = addSection(S);
244
245 for (RelocationSection *RS : RelocationSections) {
246 if (RS->getSection() == S)
247 RS->setSection(NewSection);
248 }
249 }
250
251 RemovePred = [shouldReplace, RemovePred](const SectionBase &Sec) {
252 return shouldReplace(Sec) || RemovePred(Sec);
253 };
254}
255
256// This function handles the high level operations of GNU objcopy including
257// handling command line options. It's important to outline certain properties
258// we expect to hold of the command line operations. Any operation that "keeps"
259// should keep regardless of a remove. Additionally any removal should respect
260// any previous removals. Lastly whether or not something is removed shouldn't
261// depend a) on the order the options occur in or b) on some opaque priority
262// system. The only priority is that keeps/copies overrule removes.
263static void handleArgs(const CopyConfig &Config, Object &Obj,
264 const Reader &Reader, ElfType OutputElfType) {
265
266 if (!Config.SplitDWO.empty()) {
267 splitDWOToFile(Config, Reader, Config.SplitDWO, OutputElfType);
268 }
269
270 // TODO: update or remove symbols only if there is an option that affects
271 // them.
272 if (Obj.SymbolTable) {
273 Obj.SymbolTable->updateSymbols([&](Symbol &Sym) {
Jordan Rupprechtb47475c2018-11-01 17:26:36 +0000274 if (!Sym.isCommon() &&
275 ((Config.LocalizeHidden &&
276 (Sym.Visibility == STV_HIDDEN || Sym.Visibility == STV_INTERNAL)) ||
Fangrui Songe4ee0662018-11-29 17:32:51 +0000277 is_contained(Config.SymbolsToLocalize, Sym.Name)))
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000278 Sym.Binding = STB_LOCAL;
279
280 // Note: these two globalize flags have very similar names but different
281 // meanings:
282 //
283 // --globalize-symbol: promote a symbol to global
284 // --keep-global-symbol: all symbols except for these should be made local
285 //
286 // If --globalize-symbol is specified for a given symbol, it will be
287 // global in the output file even if it is not included via
288 // --keep-global-symbol. Because of that, make sure to check
289 // --globalize-symbol second.
290 if (!Config.SymbolsToKeepGlobal.empty() &&
Jordan Rupprecht634820d2018-10-30 16:23:38 +0000291 !is_contained(Config.SymbolsToKeepGlobal, Sym.Name) &&
292 Sym.getShndx() != SHN_UNDEF)
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000293 Sym.Binding = STB_LOCAL;
294
Fangrui Songe4ee0662018-11-29 17:32:51 +0000295 if (is_contained(Config.SymbolsToGlobalize, Sym.Name) &&
Jordan Rupprecht634820d2018-10-30 16:23:38 +0000296 Sym.getShndx() != SHN_UNDEF)
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000297 Sym.Binding = STB_GLOBAL;
298
Fangrui Songe4ee0662018-11-29 17:32:51 +0000299 if (is_contained(Config.SymbolsToWeaken, Sym.Name) &&
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000300 Sym.Binding == STB_GLOBAL)
301 Sym.Binding = STB_WEAK;
302
303 if (Config.Weaken && Sym.Binding == STB_GLOBAL &&
304 Sym.getShndx() != SHN_UNDEF)
305 Sym.Binding = STB_WEAK;
306
307 const auto I = Config.SymbolsToRename.find(Sym.Name);
308 if (I != Config.SymbolsToRename.end())
309 Sym.Name = I->getValue();
310
311 if (!Config.SymbolsPrefix.empty() && Sym.Type != STT_SECTION)
312 Sym.Name = (Config.SymbolsPrefix + Sym.Name).str();
313 });
314
315 // The purpose of this loop is to mark symbols referenced by sections
316 // (like GroupSection or RelocationSection). This way, we know which
317 // symbols are still 'needed' and which are not.
318 if (Config.StripUnneeded) {
319 for (auto &Section : Obj.sections())
320 Section.markSymbols();
321 }
322
323 Obj.removeSymbols([&](const Symbol &Sym) {
Fangrui Songe4ee0662018-11-29 17:32:51 +0000324 if (is_contained(Config.SymbolsToKeep, Sym.Name) ||
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000325 (Config.KeepFileSymbols && Sym.Type == STT_FILE))
326 return false;
327
328 if (Config.DiscardAll && Sym.Binding == STB_LOCAL &&
329 Sym.getShndx() != SHN_UNDEF && Sym.Type != STT_FILE &&
330 Sym.Type != STT_SECTION)
331 return true;
332
333 if (Config.StripAll || Config.StripAllGNU)
334 return true;
335
Fangrui Songe4ee0662018-11-29 17:32:51 +0000336 if (is_contained(Config.SymbolsToRemove, Sym.Name))
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000337 return true;
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000338
339 if (Config.StripUnneeded && !Sym.Referenced &&
340 (Sym.Binding == STB_LOCAL || Sym.getShndx() == SHN_UNDEF) &&
341 Sym.Type != STT_FILE && Sym.Type != STT_SECTION)
342 return true;
343
344 return false;
345 });
346 }
347
348 SectionPred RemovePred = [](const SectionBase &) { return false; };
349
350 // Removes:
351 if (!Config.ToRemove.empty()) {
352 RemovePred = [&Config](const SectionBase &Sec) {
353 return is_contained(Config.ToRemove, Sec.Name);
354 };
355 }
356
357 if (Config.StripDWO || !Config.SplitDWO.empty())
358 RemovePred = [RemovePred](const SectionBase &Sec) {
359 return isDWOSection(Sec) || RemovePred(Sec);
360 };
361
362 if (Config.ExtractDWO)
363 RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
364 return onlyKeepDWOPred(Obj, Sec) || RemovePred(Sec);
365 };
366
367 if (Config.StripAllGNU)
368 RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
369 if (RemovePred(Sec))
370 return true;
371 if ((Sec.Flags & SHF_ALLOC) != 0)
372 return false;
373 if (&Sec == Obj.SectionNames)
374 return false;
375 switch (Sec.Type) {
376 case SHT_SYMTAB:
377 case SHT_REL:
378 case SHT_RELA:
379 case SHT_STRTAB:
380 return true;
381 }
382 return isDebugSection(Sec);
383 };
384
385 if (Config.StripSections) {
386 RemovePred = [RemovePred](const SectionBase &Sec) {
387 return RemovePred(Sec) || (Sec.Flags & SHF_ALLOC) == 0;
388 };
389 }
390
391 if (Config.StripDebug) {
392 RemovePred = [RemovePred](const SectionBase &Sec) {
393 return RemovePred(Sec) || isDebugSection(Sec);
394 };
395 }
396
397 if (Config.StripNonAlloc)
398 RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
399 if (RemovePred(Sec))
400 return true;
401 if (&Sec == Obj.SectionNames)
402 return false;
403 return (Sec.Flags & SHF_ALLOC) == 0;
404 };
405
406 if (Config.StripAll)
407 RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
408 if (RemovePred(Sec))
409 return true;
410 if (&Sec == Obj.SectionNames)
411 return false;
412 if (StringRef(Sec.Name).startswith(".gnu.warning"))
413 return false;
414 return (Sec.Flags & SHF_ALLOC) == 0;
415 };
416
417 // Explicit copies:
Jake Ehrlich85985ed2018-12-06 02:03:53 +0000418 if (!Config.OnlySection.empty()) {
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000419 RemovePred = [&Config, RemovePred, &Obj](const SectionBase &Sec) {
420 // Explicitly keep these sections regardless of previous removes.
Jake Ehrlich85985ed2018-12-06 02:03:53 +0000421 if (is_contained(Config.OnlySection, Sec.Name))
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000422 return false;
423
424 // Allow all implicit removes.
425 if (RemovePred(Sec))
426 return true;
427
428 // Keep special sections.
429 if (Obj.SectionNames == &Sec)
430 return false;
431 if (Obj.SymbolTable == &Sec ||
432 (Obj.SymbolTable && Obj.SymbolTable->getStrTab() == &Sec))
433 return false;
434
435 // Remove everything else.
436 return true;
437 };
438 }
439
Jordan Rupprechtc5bae782018-11-13 19:32:27 +0000440 if (!Config.KeepSection.empty()) {
Fangrui Songe9f34b02018-11-12 23:46:22 +0000441 RemovePred = [&Config, RemovePred](const SectionBase &Sec) {
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000442 // Explicitly keep these sections regardless of previous removes.
Jordan Rupprechtc5bae782018-11-13 19:32:27 +0000443 if (is_contained(Config.KeepSection, Sec.Name))
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000444 return false;
445 // Otherwise defer to RemovePred.
446 return RemovePred(Sec);
447 };
448 }
449
450 // This has to be the last predicate assignment.
451 // If the option --keep-symbol has been specified
452 // and at least one of those symbols is present
453 // (equivalently, the updated symbol table is not empty)
454 // the symbol table and the string table should not be removed.
455 if ((!Config.SymbolsToKeep.empty() || Config.KeepFileSymbols) &&
456 Obj.SymbolTable && !Obj.SymbolTable->empty()) {
457 RemovePred = [&Obj, RemovePred](const SectionBase &Sec) {
458 if (&Sec == Obj.SymbolTable || &Sec == Obj.SymbolTable->getStrTab())
459 return false;
460 return RemovePred(Sec);
461 };
462 }
463
464 if (Config.CompressionType != DebugCompressionType::None)
465 replaceDebugSections(Config, Obj, RemovePred, isCompressable,
466 [&Config, &Obj](const SectionBase *S) {
467 return &Obj.addSection<CompressedSection>(
468 *S, Config.CompressionType);
469 });
470 else if (Config.DecompressDebugSections)
471 replaceDebugSections(
472 Config, Obj, RemovePred,
473 [](const SectionBase &S) { return isa<CompressedSection>(&S); },
474 [&Obj](const SectionBase *S) {
475 auto CS = cast<CompressedSection>(S);
476 return &Obj.addSection<DecompressedSection>(*CS);
477 });
478
479 Obj.removeSections(RemovePred);
480
481 if (!Config.SectionsToRename.empty()) {
482 for (auto &Sec : Obj.sections()) {
483 const auto Iter = Config.SectionsToRename.find(Sec.Name);
484 if (Iter != Config.SectionsToRename.end()) {
485 const SectionRename &SR = Iter->second;
486 Sec.Name = SR.NewName;
487 if (SR.NewFlags.hasValue()) {
488 // Preserve some flags which should not be dropped when setting flags.
489 // Also, preserve anything OS/processor dependant.
490 const uint64_t PreserveMask = ELF::SHF_COMPRESSED | ELF::SHF_EXCLUDE |
491 ELF::SHF_GROUP | ELF::SHF_LINK_ORDER |
492 ELF::SHF_MASKOS | ELF::SHF_MASKPROC |
493 ELF::SHF_TLS | ELF::SHF_INFO_LINK;
494 Sec.Flags = (Sec.Flags & PreserveMask) |
495 (SR.NewFlags.getValue() & ~PreserveMask);
496 }
497 }
498 }
499 }
500
501 if (!Config.AddSection.empty()) {
502 for (const auto &Flag : Config.AddSection) {
503 auto SecPair = Flag.split("=");
504 auto SecName = SecPair.first;
505 auto File = SecPair.second;
506 auto BufOrErr = MemoryBuffer::getFile(File);
507 if (!BufOrErr)
508 reportError(File, BufOrErr.getError());
509 auto Buf = std::move(*BufOrErr);
510 auto BufPtr = reinterpret_cast<const uint8_t *>(Buf->getBufferStart());
511 auto BufSize = Buf->getBufferSize();
512 Obj.addSection<OwnedDataSection>(SecName,
513 ArrayRef<uint8_t>(BufPtr, BufSize));
514 }
515 }
516
517 if (!Config.DumpSection.empty()) {
518 for (const auto &Flag : Config.DumpSection) {
519 std::pair<StringRef, StringRef> SecPair = Flag.split("=");
520 StringRef SecName = SecPair.first;
521 StringRef File = SecPair.second;
522 if (Error E = dumpSectionToFile(SecName, File, Obj))
523 reportError(Config.InputFilename, std::move(E));
524 }
525 }
526
527 if (!Config.AddGnuDebugLink.empty())
528 Obj.addSection<GnuDebugLinkSection>(Config.AddGnuDebugLink);
529}
530
531void executeObjcopyOnRawBinary(const CopyConfig &Config, MemoryBuffer &In,
532 Buffer &Out) {
533 BinaryReader Reader(Config.BinaryArch, &In);
534 std::unique_ptr<Object> Obj = Reader.create();
535
536 const ElfType OutputElfType = getOutputElfType(Config.BinaryArch);
537 handleArgs(Config, *Obj, Reader, OutputElfType);
538 std::unique_ptr<Writer> Writer =
539 createWriter(Config, *Obj, Out, OutputElfType);
540 Writer->finalize();
541 Writer->write();
542}
543
544void executeObjcopyOnBinary(const CopyConfig &Config,
545 object::ELFObjectFileBase &In, Buffer &Out) {
546 ELFReader Reader(&In);
547 std::unique_ptr<Object> Obj = Reader.create();
548 const ElfType OutputElfType = getOutputElfType(In);
Jake Ehrlich8ad77792018-12-03 19:49:23 +0000549 ArrayRef<uint8_t> BuildIdBytes;
550
551 if (!Config.BuildIdLinkDir.empty()) {
552 BuildIdBytes = unwrapOrError(findBuildID(In));
553 if (BuildIdBytes.size() < 2)
554 error("build ID in file '" + Config.InputFilename +
555 "' is smaller than two bytes");
556 }
557
558 if (!Config.BuildIdLinkDir.empty() && Config.BuildIdLinkInput) {
559 linkToBuildIdDir(Config, Config.InputFilename,
560 Config.BuildIdLinkInput.getValue(), BuildIdBytes);
561 }
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000562 handleArgs(Config, *Obj, Reader, OutputElfType);
563 std::unique_ptr<Writer> Writer =
564 createWriter(Config, *Obj, Out, OutputElfType);
565 Writer->finalize();
566 Writer->write();
Jake Ehrlich8ad77792018-12-03 19:49:23 +0000567 if (!Config.BuildIdLinkDir.empty() && Config.BuildIdLinkOutput) {
568 linkToBuildIdDir(Config, Config.OutputFilename,
569 Config.BuildIdLinkOutput.getValue(), BuildIdBytes);
570 }
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000571}
572
573} // end namespace elf
574} // end namespace objcopy
575} // end namespace llvm