LLVM  16.0.0git
ELFObjcopy.cpp
Go to the documentation of this file.
1 //===- ELFObjcopy.cpp -----------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
10 #include "ELFObject.h"
11 #include "llvm/ADT/BitmaskEnum.h"
12 #include "llvm/ADT/DenseSet.h"
13 #include "llvm/ADT/Optional.h"
14 #include "llvm/ADT/STLExtras.h"
15 #include "llvm/ADT/SmallVector.h"
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/ADT/Twine.h"
18 #include "llvm/BinaryFormat/ELF.h"
22 #include "llvm/Object/Binary.h"
24 #include "llvm/Object/ELFTypes.h"
25 #include "llvm/Object/Error.h"
26 #include "llvm/Option/Option.h"
27 #include "llvm/Support/Casting.h"
29 #include "llvm/Support/Errc.h"
30 #include "llvm/Support/Error.h"
32 #include "llvm/Support/ErrorOr.h"
34 #include "llvm/Support/Memory.h"
35 #include "llvm/Support/Path.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 
47 using namespace llvm;
48 using namespace llvm::ELF;
49 using namespace llvm::objcopy;
50 using namespace llvm::objcopy::elf;
51 using namespace llvm::object;
52 
53 using SectionPred = std::function<bool(const SectionBase &Sec)>;
54 
55 static bool isDebugSection(const SectionBase &Sec) {
56  return StringRef(Sec.Name).startswith(".debug") || Sec.Name == ".gdb_index";
57 }
58 
59 static bool isDWOSection(const SectionBase &Sec) {
60  return StringRef(Sec.Name).endswith(".dwo");
61 }
62 
63 static bool onlyKeepDWOPred(const Object &Obj, const SectionBase &Sec) {
64  // We can't remove the section header string table.
65  if (&Sec == Obj.SectionNames)
66  return false;
67  // Short of keeping the string table we want to keep everything that is a DWO
68  // section and remove everything else.
69  return !isDWOSection(Sec);
70 }
71 
73  uint64_t NewFlags = 0;
74  if (AllFlags & SectionFlag::SecAlloc)
75  NewFlags |= ELF::SHF_ALLOC;
76  if (!(AllFlags & SectionFlag::SecReadonly))
77  NewFlags |= ELF::SHF_WRITE;
78  if (AllFlags & SectionFlag::SecCode)
79  NewFlags |= ELF::SHF_EXECINSTR;
80  if (AllFlags & SectionFlag::SecMerge)
81  NewFlags |= ELF::SHF_MERGE;
82  if (AllFlags & SectionFlag::SecStrings)
83  NewFlags |= ELF::SHF_STRINGS;
84  if (AllFlags & SectionFlag::SecExclude)
85  NewFlags |= ELF::SHF_EXCLUDE;
86  return NewFlags;
87 }
88 
90  uint64_t NewFlags) {
91  // Preserve some flags which should not be dropped when setting flags.
92  // Also, preserve anything OS/processor dependant.
93  const uint64_t PreserveMask =
98  return (OldFlags & PreserveMask) | (NewFlags & ~PreserveMask);
99 }
100 
103 
104  // In GNU objcopy, certain flags promote SHT_NOBITS to SHT_PROGBITS. This rule
105  // may promote more non-ALLOC sections than GNU objcopy, but it is fine as
106  // non-ALLOC SHT_NOBITS sections do not make much sense.
107  if (Sec.Type == SHT_NOBITS &&
108  (!(Sec.Flags & ELF::SHF_ALLOC) ||
110  Sec.Type = SHT_PROGBITS;
111 }
112 
113 static ElfType getOutputElfType(const Binary &Bin) {
114  // Infer output ELF type from the input ELF object
116  return ELFT_ELF32LE;
118  return ELFT_ELF64LE;
120  return ELFT_ELF32BE;
122  return ELFT_ELF64BE;
123  llvm_unreachable("Invalid ELFType");
124 }
125 
127  // Infer output ELF type from the binary arch specified
128  if (MI.Is64Bit)
129  return MI.IsLittleEndian ? ELFT_ELF64LE : ELFT_ELF64BE;
130  else
131  return MI.IsLittleEndian ? ELFT_ELF32LE : ELFT_ELF32BE;
132 }
133 
134 static std::unique_ptr<Writer> createELFWriter(const CommonConfig &Config,
135  Object &Obj, raw_ostream &Out,
136  ElfType OutputElfType) {
137  // Depending on the initial ELFT and OutputFormat we need a different Writer.
138  switch (OutputElfType) {
139  case ELFT_ELF32LE:
140  return std::make_unique<ELFWriter<ELF32LE>>(Obj, Out, !Config.StripSections,
141  Config.OnlyKeepDebug);
142  case ELFT_ELF64LE:
143  return std::make_unique<ELFWriter<ELF64LE>>(Obj, Out, !Config.StripSections,
144  Config.OnlyKeepDebug);
145  case ELFT_ELF32BE:
146  return std::make_unique<ELFWriter<ELF32BE>>(Obj, Out, !Config.StripSections,
147  Config.OnlyKeepDebug);
148  case ELFT_ELF64BE:
149  return std::make_unique<ELFWriter<ELF64BE>>(Obj, Out, !Config.StripSections,
150  Config.OnlyKeepDebug);
151  }
152  llvm_unreachable("Invalid output format");
153 }
154 
155 static std::unique_ptr<Writer> createWriter(const CommonConfig &Config,
156  Object &Obj, raw_ostream &Out,
157  ElfType OutputElfType) {
158  switch (Config.OutputFormat) {
159  case FileFormat::Binary:
160  return std::make_unique<BinaryWriter>(Obj, Out);
161  case FileFormat::IHex:
162  return std::make_unique<IHexWriter>(Obj, Out);
163  default:
164  return createELFWriter(Config, Obj, Out, OutputElfType);
165  }
166 }
167 
168 template <class... Ts>
169 static Error makeStringError(std::error_code EC, const Twine &Msg,
170  Ts &&...Args) {
171  std::string FullMsg = (EC.message() + ": " + Msg).str();
172  return createStringError(EC, FullMsg.c_str(), std::forward<Ts>(Args)...);
173 }
174 
175 static Error dumpSectionToFile(StringRef SecName, StringRef Filename,
176  Object &Obj) {
177  for (auto &Sec : Obj.sections()) {
178  if (Sec.Name == SecName) {
179  if (Sec.Type == SHT_NOBITS)
180  return createStringError(object_error::parse_failed,
181  "cannot dump section '%s': it has no contents",
182  SecName.str().c_str());
184  FileOutputBuffer::create(Filename, Sec.OriginalData.size());
185  if (!BufferOrErr)
186  return BufferOrErr.takeError();
187  std::unique_ptr<FileOutputBuffer> Buf = std::move(*BufferOrErr);
188  std::copy(Sec.OriginalData.begin(), Sec.OriginalData.end(),
189  Buf->getBufferStart());
190  if (Error E = Buf->commit())
191  return E;
192  return Error::success();
193  }
194  }
195  return createStringError(object_error::parse_failed, "section '%s' not found",
196  SecName.str().c_str());
197 }
198 
199 static bool isCompressable(const SectionBase &Sec) {
200  return !(Sec.Flags & ELF::SHF_COMPRESSED) &&
201  StringRef(Sec.Name).startswith(".debug");
202 }
203 
205  Object &Obj, function_ref<bool(const SectionBase &)> ShouldReplace,
206  function_ref<Expected<SectionBase *>(const SectionBase *)> AddSection) {
207  // Build a list of the debug sections we are going to replace.
208  // We can't call `AddSection` while iterating over sections,
209  // because it would mutate the sections array.
211  for (auto &Sec : Obj.sections())
212  if (ShouldReplace(Sec))
213  ToReplace.push_back(&Sec);
214 
215  // Build a mapping from original section to a new one.
217  for (SectionBase *S : ToReplace) {
218  Expected<SectionBase *> NewSection = AddSection(S);
219  if (!NewSection)
220  return NewSection.takeError();
221 
222  FromTo[S] = *NewSection;
223  }
224 
225  return Obj.replaceSections(FromTo);
226 }
227 
228 static bool isAArch64MappingSymbol(const Symbol &Sym) {
229  if (Sym.Binding != STB_LOCAL || Sym.Type != STT_NOTYPE ||
230  Sym.getShndx() == SHN_UNDEF)
231  return false;
232  StringRef Name = Sym.Name;
233  if (!Name.consume_front("$x") && !Name.consume_front("$d"))
234  return false;
235  return Name.empty() || Name.startswith(".");
236 }
237 
238 static bool isArmMappingSymbol(const Symbol &Sym) {
239  if (Sym.Binding != STB_LOCAL || Sym.Type != STT_NOTYPE ||
240  Sym.getShndx() == SHN_UNDEF)
241  return false;
242  StringRef Name = Sym.Name;
243  if (!Name.consume_front("$a") && !Name.consume_front("$d") &&
244  !Name.consume_front("$t"))
245  return false;
246  return Name.empty() || Name.startswith(".");
247 }
248 
249 // Check if the symbol should be preserved because it is required by ABI.
250 static bool isRequiredByABISymbol(const Object &Obj, const Symbol &Sym) {
251  switch (Obj.Machine) {
252  case EM_AARCH64:
253  // Mapping symbols should be preserved for a relocatable object file.
254  return Obj.isRelocatable() && isAArch64MappingSymbol(Sym);
255  case EM_ARM:
256  // Mapping symbols should be preserved for a relocatable object file.
257  return Obj.isRelocatable() && isArmMappingSymbol(Sym);
258  default:
259  return false;
260  }
261 }
262 
263 static bool isUnneededSymbol(const Symbol &Sym) {
264  return !Sym.Referenced &&
265  (Sym.Binding == STB_LOCAL || Sym.getShndx() == SHN_UNDEF) &&
266  Sym.Type != STT_SECTION;
267 }
268 
270  const ELFConfig &ELFConfig, Object &Obj) {
271  // TODO: update or remove symbols only if there is an option that affects
272  // them.
273  if (!Obj.SymbolTable)
274  return Error::success();
275 
276  Obj.SymbolTable->updateSymbols([&](Symbol &Sym) {
277  // Common and undefined symbols don't make sense as local symbols, and can
278  // even cause crashes if we localize those, so skip them.
279  if (!Sym.isCommon() && Sym.getShndx() != SHN_UNDEF &&
281  (Sym.Visibility == STV_HIDDEN || Sym.Visibility == STV_INTERNAL)) ||
282  Config.SymbolsToLocalize.matches(Sym.Name)))
283  Sym.Binding = STB_LOCAL;
284 
285  // Note: these two globalize flags have very similar names but different
286  // meanings:
287  //
288  // --globalize-symbol: promote a symbol to global
289  // --keep-global-symbol: all symbols except for these should be made local
290  //
291  // If --globalize-symbol is specified for a given symbol, it will be
292  // global in the output file even if it is not included via
293  // --keep-global-symbol. Because of that, make sure to check
294  // --globalize-symbol second.
295  if (!Config.SymbolsToKeepGlobal.empty() &&
296  !Config.SymbolsToKeepGlobal.matches(Sym.Name) &&
297  Sym.getShndx() != SHN_UNDEF)
298  Sym.Binding = STB_LOCAL;
299 
300  if (Config.SymbolsToGlobalize.matches(Sym.Name) &&
301  Sym.getShndx() != SHN_UNDEF)
302  Sym.Binding = STB_GLOBAL;
303 
304  // SymbolsToWeaken applies to both STB_GLOBAL and STB_GNU_UNIQUE.
305  if (Config.SymbolsToWeaken.matches(Sym.Name) && Sym.Binding != STB_LOCAL)
306  Sym.Binding = STB_WEAK;
307 
308  if (Config.Weaken && Sym.Binding != STB_LOCAL &&
309  Sym.getShndx() != SHN_UNDEF)
310  Sym.Binding = STB_WEAK;
311 
312  const auto I = Config.SymbolsToRename.find(Sym.Name);
313  if (I != Config.SymbolsToRename.end())
314  Sym.Name = std::string(I->getValue());
315 
316  if (!Config.SymbolsPrefix.empty() && Sym.Type != STT_SECTION)
317  Sym.Name = (Config.SymbolsPrefix + Sym.Name).str();
318  });
319 
320  // The purpose of this loop is to mark symbols referenced by sections
321  // (like GroupSection or RelocationSection). This way, we know which
322  // symbols are still 'needed' and which are not.
323  if (Config.StripUnneeded || !Config.UnneededSymbolsToRemove.empty() ||
324  !Config.OnlySection.empty()) {
325  for (SectionBase &Sec : Obj.sections())
326  Sec.markSymbols();
327  }
328 
329  auto RemoveSymbolsPred = [&](const Symbol &Sym) {
330  if (Config.SymbolsToKeep.matches(Sym.Name) ||
332  return false;
333 
334  if (Config.SymbolsToRemove.matches(Sym.Name))
335  return true;
336 
337  if (Config.StripAll || Config.StripAllGNU)
338  return true;
339 
340  if (isRequiredByABISymbol(Obj, Sym))
341  return false;
342 
343  if (Config.StripDebug && Sym.Type == STT_FILE)
344  return true;
345 
346  if ((Config.DiscardMode == DiscardType::All ||
347  (Config.DiscardMode == DiscardType::Locals &&
348  StringRef(Sym.Name).startswith(".L"))) &&
349  Sym.Binding == STB_LOCAL && Sym.getShndx() != SHN_UNDEF &&
350  Sym.Type != STT_FILE && Sym.Type != STT_SECTION)
351  return true;
352 
353  if ((Config.StripUnneeded ||
354  Config.UnneededSymbolsToRemove.matches(Sym.Name)) &&
355  (!Obj.isRelocatable() || isUnneededSymbol(Sym)))
356  return true;
357 
358  // We want to remove undefined symbols if all references have been stripped.
359  if (!Config.OnlySection.empty() && !Sym.Referenced &&
360  Sym.getShndx() == SHN_UNDEF)
361  return true;
362 
363  return false;
364  };
365 
366  return Obj.removeSymbols(RemoveSymbolsPred);
367 }
368 
370  const ELFConfig &ELFConfig, Object &Obj) {
371  SectionPred RemovePred = [](const SectionBase &) { return false; };
372 
373  // Removes:
374  if (!Config.ToRemove.empty()) {
375  RemovePred = [&Config](const SectionBase &Sec) {
376  return Config.ToRemove.matches(Sec.Name);
377  };
378  }
379 
380  if (Config.StripDWO)
381  RemovePred = [RemovePred](const SectionBase &Sec) {
382  return isDWOSection(Sec) || RemovePred(Sec);
383  };
384 
385  if (Config.ExtractDWO)
386  RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
387  return onlyKeepDWOPred(Obj, Sec) || RemovePred(Sec);
388  };
389 
390  if (Config.StripAllGNU)
391  RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
392  if (RemovePred(Sec))
393  return true;
394  if ((Sec.Flags & SHF_ALLOC) != 0)
395  return false;
396  if (&Sec == Obj.SectionNames)
397  return false;
398  switch (Sec.Type) {
399  case SHT_SYMTAB:
400  case SHT_REL:
401  case SHT_RELA:
402  case SHT_STRTAB:
403  return true;
404  }
405  return isDebugSection(Sec);
406  };
407 
408  if (Config.StripSections) {
409  RemovePred = [RemovePred](const SectionBase &Sec) {
410  return RemovePred(Sec) || Sec.ParentSegment == nullptr;
411  };
412  }
413 
414  if (Config.StripDebug || Config.StripUnneeded) {
415  RemovePred = [RemovePred](const SectionBase &Sec) {
416  return RemovePred(Sec) || isDebugSection(Sec);
417  };
418  }
419 
420  if (Config.StripNonAlloc)
421  RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
422  if (RemovePred(Sec))
423  return true;
424  if (&Sec == Obj.SectionNames)
425  return false;
426  return (Sec.Flags & SHF_ALLOC) == 0 && Sec.ParentSegment == nullptr;
427  };
428 
429  if (Config.StripAll)
430  RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
431  if (RemovePred(Sec))
432  return true;
433  if (&Sec == Obj.SectionNames)
434  return false;
435  if (StringRef(Sec.Name).startswith(".gnu.warning"))
436  return false;
437  // We keep the .ARM.attribute section to maintain compatibility
438  // with Debian derived distributions. This is a bug in their
439  // patchset as documented here:
440  // https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=943798
441  if (Sec.Type == SHT_ARM_ATTRIBUTES)
442  return false;
443  if (Sec.ParentSegment != nullptr)
444  return false;
445  return (Sec.Flags & SHF_ALLOC) == 0;
446  };
447 
448  if (Config.ExtractPartition || Config.ExtractMainPartition) {
449  RemovePred = [RemovePred](const SectionBase &Sec) {
450  if (RemovePred(Sec))
451  return true;
452  if (Sec.Type == SHT_LLVM_PART_EHDR || Sec.Type == SHT_LLVM_PART_PHDR)
453  return true;
454  return (Sec.Flags & SHF_ALLOC) != 0 && !Sec.ParentSegment;
455  };
456  }
457 
458  // Explicit copies:
459  if (!Config.OnlySection.empty()) {
460  RemovePred = [&Config, RemovePred, &Obj](const SectionBase &Sec) {
461  // Explicitly keep these sections regardless of previous removes.
462  if (Config.OnlySection.matches(Sec.Name))
463  return false;
464 
465  // Allow all implicit removes.
466  if (RemovePred(Sec))
467  return true;
468 
469  // Keep special sections.
470  if (Obj.SectionNames == &Sec)
471  return false;
472  if (Obj.SymbolTable == &Sec ||
473  (Obj.SymbolTable && Obj.SymbolTable->getStrTab() == &Sec))
474  return false;
475 
476  // Remove everything else.
477  return true;
478  };
479  }
480 
481  if (!Config.KeepSection.empty()) {
482  RemovePred = [&Config, RemovePred](const SectionBase &Sec) {
483  // Explicitly keep these sections regardless of previous removes.
484  if (Config.KeepSection.matches(Sec.Name))
485  return false;
486  // Otherwise defer to RemovePred.
487  return RemovePred(Sec);
488  };
489  }
490 
491  // This has to be the last predicate assignment.
492  // If the option --keep-symbol has been specified
493  // and at least one of those symbols is present
494  // (equivalently, the updated symbol table is not empty)
495  // the symbol table and the string table should not be removed.
496  if ((!Config.SymbolsToKeep.empty() || ELFConfig.KeepFileSymbols) &&
497  Obj.SymbolTable && !Obj.SymbolTable->empty()) {
498  RemovePred = [&Obj, RemovePred](const SectionBase &Sec) {
499  if (&Sec == Obj.SymbolTable || &Sec == Obj.SymbolTable->getStrTab())
500  return false;
501  return RemovePred(Sec);
502  };
503  }
504 
505  if (Error E = Obj.removeSections(ELFConfig.AllowBrokenLinks, RemovePred))
506  return E;
507 
509  if (Error Err = replaceDebugSections(
510  Obj, isCompressable,
511  [&Config, &Obj](const SectionBase *S) -> Expected<SectionBase *> {
512  return &Obj.addSection<CompressedSection>(
514  }))
515  return Err;
516  } else if (Config.DecompressDebugSections) {
517  if (Error Err = replaceDebugSections(
518  Obj,
519  [](const SectionBase &S) { return isa<CompressedSection>(&S); },
520  [&Obj](const SectionBase *S) {
521  const CompressedSection *CS = cast<CompressedSection>(S);
522  return &Obj.addSection<DecompressedSection>(*CS);
523  }))
524  return Err;
525  }
526 
527  return Error::success();
528 }
529 
530 // Add symbol to the Object symbol table with the specified properties.
531 static void addSymbol(Object &Obj, const NewSymbolInfo &SymInfo,
532  uint8_t DefaultVisibility) {
533  SectionBase *Sec = Obj.findSection(SymInfo.SectionName);
534  uint64_t Value = Sec ? Sec->Addr + SymInfo.Value : SymInfo.Value;
535 
536  uint8_t Bind = ELF::STB_GLOBAL;
537  uint8_t Type = ELF::STT_NOTYPE;
538  uint8_t Visibility = DefaultVisibility;
539 
540  for (SymbolFlag FlagValue : SymInfo.Flags)
541  switch (FlagValue) {
542  case SymbolFlag::Global:
543  Bind = ELF::STB_GLOBAL;
544  break;
545  case SymbolFlag::Local:
546  Bind = ELF::STB_LOCAL;
547  break;
548  case SymbolFlag::Weak:
549  Bind = ELF::STB_WEAK;
550  break;
551  case SymbolFlag::Default:
552  Visibility = ELF::STV_DEFAULT;
553  break;
554  case SymbolFlag::Hidden:
555  Visibility = ELF::STV_HIDDEN;
556  break;
557  case SymbolFlag::Protected:
558  Visibility = ELF::STV_PROTECTED;
559  break;
560  case SymbolFlag::File:
562  break;
563  case SymbolFlag::Section:
565  break;
566  case SymbolFlag::Object:
568  break;
569  case SymbolFlag::Function:
571  break;
572  case SymbolFlag::IndirectFunction:
574  break;
575  default: /* Other flag values are ignored for ELF. */
576  break;
577  };
578 
579  Obj.SymbolTable->addSymbol(
580  SymInfo.SymbolName, Bind, Type, Sec, Value, Visibility,
582 }
583 
584 static Error
587  ArrayRef<uint8_t> Data(reinterpret_cast<const uint8_t *>(
588  NewSection.SectionData->getBufferStart()),
589  NewSection.SectionData->getBufferSize());
590  return F(NewSection.SectionName, Data);
591 }
592 
593 // This function handles the high level operations of GNU objcopy including
594 // handling command line options. It's important to outline certain properties
595 // we expect to hold of the command line operations. Any operation that "keeps"
596 // should keep regardless of a remove. Additionally any removal should respect
597 // any previous removals. Lastly whether or not something is removed shouldn't
598 // depend a) on the order the options occur in or b) on some opaque priority
599 // system. The only priority is that keeps/copies overrule removes.
600 static Error handleArgs(const CommonConfig &Config, const ELFConfig &ELFConfig,
601  Object &Obj) {
602  if (Config.OutputArch) {
603  Obj.Machine = Config.OutputArch.value().EMachine;
604  Obj.OSABI = Config.OutputArch.value().OSABI;
605  }
606 
607  if (!Config.SplitDWO.empty() && Config.ExtractDWO) {
608  return Obj.removeSections(
610  [&Obj](const SectionBase &Sec) { return onlyKeepDWOPred(Obj, Sec); });
611  }
612 
613  // Dump sections before add/remove for compatibility with GNU objcopy.
614  for (StringRef Flag : Config.DumpSection) {
616  StringRef FileName;
617  std::tie(SectionName, FileName) = Flag.split('=');
618  if (Error E = dumpSectionToFile(SectionName, FileName, Obj))
619  return E;
620  }
621 
622  // It is important to remove the sections first. For example, we want to
623  // remove the relocation sections before removing the symbols. That allows
624  // us to avoid reporting the inappropriate errors about removing symbols
625  // named in relocations.
626  if (Error E = replaceAndRemoveSections(Config, ELFConfig, Obj))
627  return E;
628 
629  if (Error E = updateAndRemoveSymbols(Config, ELFConfig, Obj))
630  return E;
631 
632  if (!Config.SetSectionAlignment.empty()) {
633  for (SectionBase &Sec : Obj.sections()) {
634  auto I = Config.SetSectionAlignment.find(Sec.Name);
635  if (I != Config.SetSectionAlignment.end())
636  Sec.Align = I->second;
637  }
638  }
639 
640  if (Config.OnlyKeepDebug)
641  for (auto &Sec : Obj.sections())
642  if (Sec.Flags & SHF_ALLOC && Sec.Type != SHT_NOTE)
643  Sec.Type = SHT_NOBITS;
644 
645  for (const NewSectionInfo &AddedSection : Config.AddSection) {
646  auto AddSection = [&](StringRef Name, ArrayRef<uint8_t> Data) {
647  OwnedDataSection &NewSection =
648  Obj.addSection<OwnedDataSection>(Name, Data);
649  if (Name.startswith(".note") && Name != ".note.GNU-stack")
650  NewSection.Type = SHT_NOTE;
651  return Error::success();
652  };
653  if (Error E = handleUserSection(AddedSection, AddSection))
654  return E;
655  }
656 
657  for (const NewSectionInfo &NewSection : Config.UpdateSection) {
658  auto UpdateSection = [&](StringRef Name, ArrayRef<uint8_t> Data) {
659  return Obj.updateSection(Name, Data);
660  };
661  if (Error E = handleUserSection(NewSection, UpdateSection))
662  return E;
663  }
664 
665  if (!Config.AddGnuDebugLink.empty())
667  Config.GnuDebugLinkCRC32);
668 
669  // If the symbol table was previously removed, we need to create a new one
670  // before adding new symbols.
671  if (!Obj.SymbolTable && !Config.SymbolsToAdd.empty())
672  if (Error E = Obj.addNewSymbolTable())
673  return E;
674 
675  for (const NewSymbolInfo &SI : Config.SymbolsToAdd)
677 
678  // --set-section-{flags,type} work with sections added by --add-section.
679  if (!Config.SetSectionFlags.empty() || !Config.SetSectionType.empty()) {
680  for (auto &Sec : Obj.sections()) {
681  const auto Iter = Config.SetSectionFlags.find(Sec.Name);
682  if (Iter != Config.SetSectionFlags.end()) {
683  const SectionFlagsUpdate &SFU = Iter->second;
685  }
686  auto It2 = Config.SetSectionType.find(Sec.Name);
687  if (It2 != Config.SetSectionType.end())
688  Sec.Type = It2->second;
689  }
690  }
691 
692  if (!Config.SectionsToRename.empty()) {
693  std::vector<RelocationSectionBase *> RelocSections;
694  DenseSet<SectionBase *> RenamedSections;
695  for (SectionBase &Sec : Obj.sections()) {
696  auto *RelocSec = dyn_cast<RelocationSectionBase>(&Sec);
697  const auto Iter = Config.SectionsToRename.find(Sec.Name);
698  if (Iter != Config.SectionsToRename.end()) {
699  const SectionRename &SR = Iter->second;
700  Sec.Name = std::string(SR.NewName);
701  if (SR.NewFlags)
703  RenamedSections.insert(&Sec);
704  } else if (RelocSec && !(Sec.Flags & SHF_ALLOC))
705  // Postpone processing relocation sections which are not specified in
706  // their explicit '--rename-section' commands until after their target
707  // sections are renamed.
708  // Dynamic relocation sections (i.e. ones with SHF_ALLOC) should be
709  // renamed only explicitly. Otherwise, renaming, for example, '.got.plt'
710  // would affect '.rela.plt', which is not desirable.
711  RelocSections.push_back(RelocSec);
712  }
713 
714  // Rename relocation sections according to their target sections.
715  for (RelocationSectionBase *RelocSec : RelocSections) {
716  auto Iter = RenamedSections.find(RelocSec->getSection());
717  if (Iter != RenamedSections.end())
718  RelocSec->Name = (RelocSec->getNamePrefix() + (*Iter)->Name).str();
719  }
720  }
721 
722  // Add a prefix to allocated sections and their relocation sections. This
723  // should be done after renaming the section by Config.SectionToRename to
724  // imitate the GNU objcopy behavior.
725  if (!Config.AllocSectionsPrefix.empty()) {
726  DenseSet<SectionBase *> PrefixedSections;
727  for (SectionBase &Sec : Obj.sections()) {
728  if (Sec.Flags & SHF_ALLOC) {
729  Sec.Name = (Config.AllocSectionsPrefix + Sec.Name).str();
730  PrefixedSections.insert(&Sec);
731  } else if (auto *RelocSec = dyn_cast<RelocationSectionBase>(&Sec)) {
732  // Rename relocation sections associated to the allocated sections.
733  // For example, if we rename .text to .prefix.text, we also rename
734  // .rel.text to .rel.prefix.text.
735  //
736  // Dynamic relocation sections (SHT_REL[A] with SHF_ALLOC) are handled
737  // above, e.g., .rela.plt is renamed to .prefix.rela.plt, not
738  // .rela.prefix.plt since GNU objcopy does so.
739  const SectionBase *TargetSec = RelocSec->getSection();
740  if (TargetSec && (TargetSec->Flags & SHF_ALLOC)) {
741  // If the relocation section comes *after* the target section, we
742  // don't add Config.AllocSectionsPrefix because we've already added
743  // the prefix to TargetSec->Name. Otherwise, if the relocation
744  // section comes *before* the target section, we add the prefix.
745  if (PrefixedSections.count(TargetSec))
746  Sec.Name = (RelocSec->getNamePrefix() + TargetSec->Name).str();
747  else
748  Sec.Name = (RelocSec->getNamePrefix() + Config.AllocSectionsPrefix +
749  TargetSec->Name)
750  .str();
751  }
752  }
753  }
754  }
755 
756  if (ELFConfig.EntryExpr)
757  Obj.Entry = ELFConfig.EntryExpr(Obj.Entry);
758  return Error::success();
759 }
760 
761 static Error writeOutput(const CommonConfig &Config, Object &Obj,
762  raw_ostream &Out, ElfType OutputElfType) {
763  std::unique_ptr<Writer> Writer =
764  createWriter(Config, Obj, Out, OutputElfType);
765  if (Error E = Writer->finalize())
766  return E;
767  return Writer->write();
768 }
769 
771  const ELFConfig &ELFConfig,
772  MemoryBuffer &In, raw_ostream &Out) {
773  IHexReader Reader(&In);
775  if (!Obj)
776  return Obj.takeError();
777 
778  const ElfType OutputElfType =
779  getOutputElfType(Config.OutputArch.value_or(MachineInfo()));
780  if (Error E = handleArgs(Config, ELFConfig, **Obj))
781  return E;
782  return writeOutput(Config, **Obj, Out, OutputElfType);
783 }
784 
786  const ELFConfig &ELFConfig,
787  MemoryBuffer &In,
788  raw_ostream &Out) {
791  if (!Obj)
792  return Obj.takeError();
793 
794  // Prefer OutputArch (-O<format>) if set, otherwise fallback to BinaryArch
795  // (-B<arch>).
796  const ElfType OutputElfType =
797  getOutputElfType(Config.OutputArch.value_or(MachineInfo()));
798  if (Error E = handleArgs(Config, ELFConfig, **Obj))
799  return E;
800  return writeOutput(Config, **Obj, Out, OutputElfType);
801 }
802 
804  const ELFConfig &ELFConfig,
806  raw_ostream &Out) {
809  Reader.create(!Config.SymbolsToAdd.empty());
810  if (!Obj)
811  return Obj.takeError();
812  // Prefer OutputArch (-O<format>) if set, otherwise infer it from the input.
813  const ElfType OutputElfType =
814  Config.OutputArch ? getOutputElfType(Config.OutputArch.value())
815  : getOutputElfType(In);
816 
817  if (Error E = handleArgs(Config, ELFConfig, **Obj))
818  return createFileError(Config.InputFilename, std::move(E));
819 
820  if (Error E = writeOutput(Config, **Obj, Out, OutputElfType))
821  return createFileError(Config.InputFilename, std::move(E));
822 
823  return Error::success();
824 }
llvm::objcopy::elf::Reader::create
virtual Expected< std::unique_ptr< Object > > create(bool EnsureSymtab) const =0
Option.h
Compression.h
MI
IRTranslator LLVM IR MI
Definition: IRTranslator.cpp:108
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::ELF::STB_GLOBAL
@ STB_GLOBAL
Definition: ELF.h:1218
llvm::objcopy::elf::Symbol::getShndx
uint16_t getShndx() const
Definition: ELFObject.cpp:660
llvm::objcopy::CommonConfig::InputFilename
StringRef InputFilename
Definition: CommonConfig.h:203
llvm::ELF::SHF_EXECINSTR
@ SHF_EXECINSTR
Definition: ELF.h:1067
llvm::objcopy::elf::Object::updateSection
Error updateSection(StringRef Name, ArrayRef< uint8_t > Data)
Definition: ELFObject.cpp:2101
handleArgs
static Error handleArgs(const CommonConfig &Config, const ELFConfig &ELFConfig, Object &Obj)
Definition: ELFObjcopy.cpp:600
Optional.h
FileSystem.h
MCTargetOptions.h
llvm::objcopy::elf::ELFT_ELF64LE
@ ELFT_ELF64LE
Definition: ELFObject.h:71
llvm::isa
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition: Casting.h:548
StringRef.h
llvm::objcopy::NewSectionInfo::SectionName
StringRef SectionName
Definition: CommonConfig.h:196
llvm::objcopy::CommonConfig::SymbolsToWeaken
NameMatcher SymbolsToWeaken
Definition: CommonConfig.h:237
SectionPred
std::function< bool(const SectionBase &Sec)> SectionPred
Definition: ELFObjcopy.cpp:53
llvm::objcopy::elf::Object::Entry
uint64_t Entry
Definition: ELFObject.h:1051
llvm::ELF::SHT_LLVM_PART_PHDR
@ SHT_LLVM_PART_PHDR
Definition: ELF.h:1012
llvm::ELF::SHF_MASKPROC
@ SHF_MASKPROC
Definition: ELF.h:1108
llvm::objcopy::NameMatcher::matches
bool matches(StringRef S) const
Definition: CommonConfig.h:150
ELFObject.h
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1199
Path.h
llvm::objcopy::elf::DecompressedSection
Definition: ELFObject.h:563
llvm::detail::DenseSetImpl< ValueT, DenseMap< ValueT, detail::DenseSetEmpty, DenseMapInfo< ValueT >, detail::DenseSetPair< ValueT > >, DenseMapInfo< ValueT > >::find
iterator find(const_arg_type_t< ValueT > V)
Definition: DenseSet.h:179
llvm::objcopy::CommonConfig::OutputArch
Optional< MachineInfo > OutputArch
Definition: CommonConfig.h:209
llvm::objcopy::SecCode
@ SecCode
Definition: CommonConfig.h:64
ErrorHandling.h
isCompressable
static bool isCompressable(const SectionBase &Sec)
Definition: ELFObjcopy.cpp:199
llvm::Error::success
static ErrorSuccess success()
Create a success value.
Definition: Error.h:329
handleUserSection
static Error handleUserSection(const NewSectionInfo &NewSection, function_ref< Error(StringRef, ArrayRef< uint8_t >)> F)
Definition: ELFObjcopy.cpp:585
Error.h
llvm::cl::Hidden
@ Hidden
Definition: CommandLine.h:140
llvm::objcopy::elf::Object::isRelocatable
bool isRelocatable() const
Definition: ELFObject.h:1103
llvm::objcopy::elf::Object::removeSymbols
Error removeSymbols(function_ref< bool(const Symbol &)> ToRemove)
Definition: ELFObject.cpp:2208
llvm::objcopy::CommonConfig::SymbolsToRename
StringMap< StringRef > SymbolsToRename
Definition: CommonConfig.h:245
llvm::ELF::SHT_NOBITS
@ SHT_NOBITS
Definition: ELF.h:987
llvm::ELF::STT_NOTYPE
@ STT_NOTYPE
Definition: ELF.h:1229
llvm::objcopy::CommonConfig::DiscardMode
DiscardType DiscardMode
Definition: CommonConfig.h:219
Errc.h
llvm::objcopy::CommonConfig::StripSections
bool StripSections
Definition: CommonConfig.h:261
llvm::StringMap::end
iterator end()
Definition: StringMap.h:204
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
llvm::ELF::SHT_SYMTAB
@ SHT_SYMTAB
Definition: ELF.h:981
llvm::ELF::SHT_ARM_ATTRIBUTES
@ SHT_ARM_ATTRIBUTES
Definition: ELF.h:1035
llvm::objcopy::SectionRename::NewName
StringRef NewName
Definition: CommonConfig.h:77
llvm::ELF::SHF_MERGE
@ SHF_MERGE
Definition: ELF.h:1070
llvm::objcopy::CommonConfig::StripNonAlloc
bool StripNonAlloc
Definition: CommonConfig.h:260
llvm::ELF::SHT_LLVM_PART_EHDR
@ SHT_LLVM_PART_EHDR
Definition: ELF.h:1011
llvm::ELF::SHT_PROGBITS
@ SHT_PROGBITS
Definition: ELF.h:980
llvm::objcopy::elf::SectionBase::Name
std::string Name
Definition: ELFObject.h:392
llvm::ELF::SHF_TLS
@ SHF_TLS
Definition: ELF.h:1089
llvm::objcopy::elf::SectionBase::Addr
uint64_t Addr
Definition: ELFObject.h:402
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:41
llvm::ELF::SHT_STRTAB
@ SHT_STRTAB
Definition: ELF.h:982
STLExtras.h
llvm::StringMap::find
iterator find(StringRef Key)
Definition: StringMap.h:217
llvm::ELF::SHF_INFO_LINK
@ SHF_INFO_LINK
Definition: ELF.h:1076
llvm::ELF::SHF_COMPRESSED
@ SHF_COMPRESSED
Definition: ELF.h:1092
llvm::detail::DenseSetImpl< ValueT, DenseMap< ValueT, detail::DenseSetEmpty, DenseMapInfo< ValueT >, detail::DenseSetPair< ValueT > >, DenseMapInfo< ValueT > >::insert
std::pair< iterator, bool > insert(const ValueT &V)
Definition: DenseSet.h:206
llvm::detail::DenseSetImpl< ValueT, DenseMap< ValueT, detail::DenseSetEmpty, DenseMapInfo< ValueT >, detail::DenseSetPair< ValueT > >, DenseMapInfo< ValueT > >::count
size_type count(const_arg_type_t< ValueT > V) const
Return 1 if the specified key is in the set, 0 otherwise.
Definition: DenseSet.h:97
llvm::objcopy::elf::Writer
Definition: ELFObject.h:307
llvm::Data
@ Data
Definition: SIMachineScheduler.h:55
llvm::ELF
Definition: ELF.h:27
llvm::objcopy::elf::Object::Is64Bits
bool Is64Bits
Definition: ELFObject.h:1048
llvm::objcopy::SecAlloc
@ SecAlloc
Definition: CommonConfig.h:59
llvm::objcopy::elf::Object::replaceSections
Error replaceSections(const DenseMap< SectionBase *, SectionBase * > &FromTo)
Definition: ELFObject.cpp:2184
llvm::MemoryBuffer
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:51
llvm::objcopy::elf::Object::addSection
T & addSection(Ts &&...Args)
Definition: ELFObject.h:1090
llvm::ELF::STV_HIDDEN
@ STV_HIDDEN
Definition: ELF.h:1249
F
#define F(x, y, z)
Definition: MD5.cpp:55
llvm::ELF::STT_FUNC
@ STT_FUNC
Definition: ELF.h:1231
llvm::SubDirectoryType::Bin
@ Bin
llvm::object::ELFObjectFileBase
Definition: ELFObjectFile.h:51
llvm::ARMBuildAttrs::Section
@ Section
Legacy Tags.
Definition: ARMBuildAttributes.h:82
llvm::detail::DenseSetImpl< ValueT, DenseMap< ValueT, detail::DenseSetEmpty, DenseMapInfo< ValueT >, detail::DenseSetPair< ValueT > >, DenseMapInfo< ValueT > >::end
iterator end()
Definition: DenseSet.h:174
llvm::objcopy::elf::SectionBase::Flags
uint64_t Flags
Definition: ELFObject.h:405
llvm::objcopy::ELFConfig
Definition: ELFConfig.h:21
llvm::objcopy::elf::Object::Machine
uint32_t Machine
Definition: ELFObject.h:1054
llvm::ELF::STV_PROTECTED
@ STV_PROTECTED
Definition: ELF.h:1250
llvm::objcopy::elf::Object
Definition: ELFObject.h:1020
llvm::object
Definition: DWARFDebugLoc.h:25
llvm::objcopy::elf::Object::findSection
SectionBase * findSection(StringRef Name)
Definition: ELFObject.h:1077
llvm::objcopy::elf::Symbol::Type
uint8_t Type
Definition: ELFObject.h:643
Error.h
llvm::objcopy::elf::Object::addNewSymbolTable
Error addNewSymbolTable()
Definition: ELFObject.cpp:2216
ELF.h
llvm::ELF::EM_ARM
@ EM_ARM
Definition: ELF.h:156
llvm::objcopy::elf::SymbolTableSection::addSymbol
void addSymbol(Twine Name, uint8_t Bind, uint8_t Type, SectionBase *DefinedIn, uint64_t Value, uint8_t Visibility, uint16_t Shndx, uint64_t SymbolSize)
Definition: ELFObject.cpp:687
llvm::StringRef::startswith
bool startswith(StringRef Prefix) const
Definition: StringRef.h:260
llvm::DebugCompressionType::None
@ None
No compression.
llvm::objcopy::CommonConfig::OnlySection
NameMatcher OnlySection
Definition: CommonConfig.h:228
llvm::createFileError
Error createFileError(const Twine &F, Error E)
Concatenate a source file path and/or name with an Error.
Definition: Error.h:1319
llvm::objcopy::elf::Reader
Definition: ELFObject.h:903
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::objcopy::CommonConfig::CompressionType
DebugCompressionType CompressionType
Definition: CommonConfig.h:266
llvm::objcopy::NewSectionInfo
Definition: CommonConfig.h:191
Twine.h
llvm::objcopy::elf::IHexReader
Definition: ELFObject.h:988
llvm::objcopy::CommonConfig::SymbolsToKeep
NameMatcher SymbolsToKeep
Definition: CommonConfig.h:233
llvm::objcopy::CommonConfig::SetSectionType
StringMap< uint64_t > SetSectionType
Definition: CommonConfig.h:244
llvm::objcopy::elf::Writer::write
virtual Error write()=0
llvm::objcopy::elf::Object::SectionNames
StringTableSection * SectionNames
Definition: ELFObject.h:1060
llvm::objcopy::CommonConfig::AddSection
std::vector< NewSectionInfo > AddSection
Definition: CommonConfig.h:222
SI
@ SI
Definition: SIInstrInfo.cpp:7882
writeOutput
static Error writeOutput(const CommonConfig &Config, Object &Obj, raw_ostream &Out, ElfType OutputElfType)
Definition: ELFObjcopy.cpp:761
DenseSet.h
llvm::objcopy::CommonConfig::KeepSection
NameMatcher KeepSection
Definition: CommonConfig.h:227
llvm::objcopy::CommonConfig::DumpSection
std::vector< StringRef > DumpSection
Definition: CommonConfig.h:223
llvm::objcopy::SecStrings
@ SecStrings
Definition: CommonConfig.h:68
setSectionFlagsAndType
static void setSectionFlagsAndType(SectionBase &Sec, SectionFlag Flags)
Definition: ELFObjcopy.cpp:101
llvm::objcopy::elf::executeObjcopyOnBinary
Error executeObjcopyOnBinary(const CommonConfig &Config, const ELFConfig &ELFConfig, object::ELFObjectFileBase &In, raw_ostream &Out)
Apply the transformations described by Config and ELFConfig to In and writes the result into Out.
Definition: ELFObjcopy.cpp:803
llvm::MCID::Flag
Flag
These should be considered private to the implementation of the MCInstrDesc class.
Definition: MCInstrDesc.h:147
llvm::objcopy::CommonConfig::SymbolsToGlobalize
NameMatcher SymbolsToGlobalize
Definition: CommonConfig.h:232
llvm::objcopy::elf::SectionBase::Type
uint64_t Type
Definition: ELFObject.h:411
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
llvm::AMDGPU::ElfNote::SectionName
const char SectionName[]
Definition: AMDGPUPTNote.h:24
llvm::objcopy::elf::OwnedDataSection
Definition: ELFObject.h:502
llvm::objcopy::elf::SymbolTableSection::getStrTab
const SectionBase * getStrTab() const
Definition: ELFObject.h:711
CommonConfig.h
llvm::ELF::SHF_EXCLUDE
@ SHF_EXCLUDE
Definition: ELF.h:1098
llvm::objcopy::elf::ELFReader
Definition: ELFObject.h:1010
isArmMappingSymbol
static bool isArmMappingSymbol(const Symbol &Sym)
Definition: ELFObjcopy.cpp:238
llvm::objcopy::NewSectionInfo::SectionData
std::shared_ptr< MemoryBuffer > SectionData
Definition: CommonConfig.h:197
llvm::objcopy::elf::SYMBOL_SIMPLE_INDEX
@ SYMBOL_SIMPLE_INDEX
Definition: ELFObject.h:615
llvm::objcopy::elf::BinaryReader
Definition: ELFObject.h:978
replaceDebugSections
static Error replaceDebugSections(Object &Obj, function_ref< bool(const SectionBase &)> ShouldReplace, function_ref< Expected< SectionBase * >(const SectionBase *)> AddSection)
Definition: ELFObjcopy.cpp:204
llvm::ELF::SHF_MASKOS
@ SHF_MASKOS
Definition: ELF.h:1102
llvm::objcopy::NameMatcher::empty
bool empty() const
Definition: CommonConfig.h:155
llvm::objcopy::elf::Symbol
Definition: ELFObject.h:635
llvm::function_ref
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLFunctionalExtras.h:36
llvm::objcopy::elf::ELFT_ELF32BE
@ ELFT_ELF32BE
Definition: ELFObject.h:71
llvm::StringRef::empty
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:134
llvm::objcopy::SecMerge
@ SecMerge
Definition: CommonConfig.h:67
llvm::objcopy::CommonConfig::StripUnneeded
bool StripUnneeded
Definition: CommonConfig.h:262
llvm::DenseSet
Implements a dense probed hash-table based set.
Definition: DenseSet.h:268
llvm::tgtok::In
@ In
Definition: TGLexer.h:51
llvm::ELF::STT_GNU_IFUNC
@ STT_GNU_IFUNC
Definition: ELF.h:1236
llvm::ELF::SHF_LINK_ORDER
@ SHF_LINK_ORDER
Definition: ELF.h:1079
llvm::ELF::STT_SECTION
@ STT_SECTION
Definition: ELF.h:1232
llvm::objcopy::CommonConfig::SplitDWO
StringRef SplitDWO
Definition: CommonConfig.h:216
llvm::objcopy::CommonConfig::StripAllGNU
bool StripAllGNU
Definition: CommonConfig.h:257
llvm::objcopy::CommonConfig::SymbolsToLocalize
NameMatcher SymbolsToLocalize
Definition: CommonConfig.h:234
llvm::objcopy::CommonConfig::SymbolsToKeepGlobal
NameMatcher SymbolsToKeepGlobal
Definition: CommonConfig.h:238
uint64_t
llvm::objcopy::CommonConfig::UnneededSymbolsToRemove
NameMatcher UnneededSymbolsToRemove
Definition: CommonConfig.h:236
llvm::objcopy::MachineInfo
Definition: CommonConfig.h:40
createWriter
static std::unique_ptr< Writer > createWriter(const CommonConfig &Config, Object &Obj, raw_ostream &Out, ElfType OutputElfType)
Definition: ELFObjcopy.cpp:155
updateAndRemoveSymbols
static Error updateAndRemoveSymbols(const CommonConfig &Config, const ELFConfig &ELFConfig, Object &Obj)
Definition: ELFObjcopy.cpp:269
llvm::ELF::SHF_STRINGS
@ SHF_STRINGS
Definition: ELF.h:1073
isRequiredByABISymbol
static bool isRequiredByABISymbol(const Object &Obj, const Symbol &Sym)
Definition: ELFObjcopy.cpp:250
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
createELFWriter
static std::unique_ptr< Writer > createELFWriter(const CommonConfig &Config, Object &Obj, raw_ostream &Out, ElfType OutputElfType)
Definition: ELFObjcopy.cpp:134
llvm::objcopy::SecContents
@ SecContents
Definition: CommonConfig.h:69
llvm::DenseMap
Definition: DenseMap.h:714
isAArch64MappingSymbol
static bool isAArch64MappingSymbol(const Symbol &Sym)
Definition: ELFObjcopy.cpp:228
ErrorOr.h
llvm::objcopy::ELFConfig::AllowBrokenLinks
bool AllowBrokenLinks
Definition: ELFConfig.h:30
I
#define I(x, y, z)
Definition: MD5.cpp:58
llvm::objcopy::SecLoad
@ SecLoad
Definition: CommonConfig.h:60
onlyKeepDWOPred
static bool onlyKeepDWOPred(const Object &Obj, const SectionBase &Sec)
Definition: ELFObjcopy.cpp:63
getOutputElfType
static ElfType getOutputElfType(const Binary &Bin)
Definition: ELFObjcopy.cpp:113
llvm::objcopy::SectionRename::NewFlags
Optional< SectionFlag > NewFlags
Definition: CommonConfig.h:78
llvm::ELF::SHF_ALLOC
@ SHF_ALLOC
Definition: ELF.h:1064
llvm::ELF::SHT_NOTE
@ SHT_NOTE
Definition: ELF.h:986
llvm::objcopy::elf::ELFT_ELF64BE
@ ELFT_ELF64BE
Definition: ELFObject.h:71
llvm::objcopy::elf::Symbol::Visibility
uint8_t Visibility
Definition: ELFObject.h:645
llvm::objcopy::CommonConfig::GnuDebugLinkCRC32
uint32_t GnuDebugLinkCRC32
Definition: CommonConfig.h:214
llvm::objcopy::CommonConfig::StripDebug
bool StripDebug
Definition: CommonConfig.h:259
ELFTypes.h
makeStringError
static Error makeStringError(std::error_code EC, const Twine &Msg, Ts &&...Args)
Definition: ELFObjcopy.cpp:169
function
print Print MemDeps of function
Definition: MemDepPrinter.cpp:82
llvm::objcopy::elf::ELFT_ELF32LE
@ ELFT_ELF32LE
Definition: ELFObject.h:71
llvm::objcopy::CommonConfig::SymbolsToAdd
std::vector< NewSymbolInfo > SymbolsToAdd
Definition: CommonConfig.h:248
llvm::objcopy::ELFConfig::LocalizeHidden
bool LocalizeHidden
Definition: ELFConfig.h:32
llvm::ArrayRef< uint8_t >
llvm::objcopy::elf::SymbolTableSection::empty
bool empty() const
Definition: ELFObject.h:705
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
llvm::object::Binary
Definition: Binary.h:32
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:143
if
if(llvm_vc STREQUAL "") set(fake_version_inc "$
Definition: CMakeLists.txt:14
BitmaskEnum.h
llvm::objcopy::elf::Symbol::Binding
uint8_t Binding
Definition: ELFObject.h:636
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
Memory.h
llvm::FileOutputBuffer::create
static Expected< std::unique_ptr< FileOutputBuffer > > create(StringRef FilePath, size_t Size, unsigned Flags=0)
Factory method to create an OutputBuffer object which manages a read/write buffer of the specified si...
Definition: FileOutputBuffer.cpp:156
llvm::objcopy::CommonConfig::SymbolsPrefix
StringRef SymbolsPrefix
Definition: CommonConfig.h:217
ELFConfig.h
llvm::objcopy::SecExclude
@ SecExclude
Definition: CommonConfig.h:71
llvm::objcopy::ELFConfig::NewSymbolVisibility
uint8_t NewSymbolVisibility
Definition: ELFConfig.h:22
llvm::objcopy::CommonConfig::StripDWO
bool StripDWO
Definition: CommonConfig.h:258
ELFObjectFile.h
llvm::objcopy::CommonConfig::SetSectionAlignment
StringMap< uint64_t > SetSectionAlignment
Definition: CommonConfig.h:242
llvm::AMDGPU::SendMsg::Msg
const CustomOperand< const MCSubtargetInfo & > Msg[]
Definition: AMDGPUAsmUtils.cpp:39
llvm::objcopy::elf::Symbol::Name
std::string Name
Definition: ELFObject.h:640
llvm::objcopy::SectionRename
Definition: CommonConfig.h:75
llvm::objcopy::elf::SymbolTableSection::updateSymbols
void updateSymbols(function_ref< void(Symbol &)> Callable)
Definition: ELFObject.cpp:729
llvm::objcopy::elf
Definition: ELFObjcopy.h:25
llvm::objcopy::SectionFlagsUpdate::NewFlags
SectionFlag NewFlags
Definition: CommonConfig.h:83
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
llvm::objcopy::elf::Object::sections
SectionTableRef sections() const
Definition: ELFObject.h:1066
llvm::createStringError
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition: Error.h:1238
uint16_t
getSectionFlagsPreserveMask
static uint64_t getSectionFlagsPreserveMask(uint64_t OldFlags, uint64_t NewFlags)
Definition: ELFObjcopy.cpp:89
llvm::objcopy::elf::CompressedSection
Definition: ELFObject.h:536
llvm::objcopy::elf::Object::removeSections
Error removeSections(bool AllowBrokenLinks, std::function< bool(const SectionBase &)> ToRemove)
Definition: ELFObject.cpp:2132
llvm::SectionName
Definition: DWARFSection.h:21
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:155
llvm::StringRef::endswith
bool endswith(StringRef Suffix) const
Definition: StringRef.h:276
llvm::objcopy::CommonConfig::UpdateSection
std::vector< NewSectionInfo > UpdateSection
Definition: CommonConfig.h:224
Casting.h
llvm::objcopy::elf::Writer::finalize
virtual Error finalize()=0
llvm::objcopy::SectionFlagsUpdate
Definition: CommonConfig.h:81
llvm::ELF::SHF_WRITE
@ SHF_WRITE
Definition: ELF.h:1061
llvm::TargetStackID::Default
@ Default
Definition: TargetFrameLowering.h:28
llvm::objcopy::elf::executeObjcopyOnRawBinary
Error executeObjcopyOnRawBinary(const CommonConfig &Config, const ELFConfig &ELFConfig, MemoryBuffer &In, raw_ostream &Out)
Apply the transformations described by Config and ELFConfig to In, which is treated as a raw binary i...
Definition: ELFObjcopy.cpp:785
llvm::objcopy::CommonConfig::StripAll
bool StripAll
Definition: CommonConfig.h:256
isDebugSection
static bool isDebugSection(const SectionBase &Sec)
Definition: ELFObjcopy.cpp:55
llvm::objcopy::elf::ElfType
ElfType
Definition: ELFObject.h:71
llvm::objcopy::CommonConfig::ExtractPartition
Optional< StringRef > ExtractPartition
Definition: CommonConfig.h:215
isDWOSection
static bool isDWOSection(const SectionBase &Sec)
Definition: ELFObjcopy.cpp:59
llvm::Expected::takeError
Error takeError()
Take ownership of the stored error.
Definition: Error.h:596
Binary.h
llvm::objcopy::CommonConfig::SectionsToRename
StringMap< SectionRename > SectionsToRename
Definition: CommonConfig.h:241
llvm::ELF::STV_INTERNAL
@ STV_INTERNAL
Definition: ELF.h:1248
getNewShfFlags
static uint64_t getNewShfFlags(SectionFlag AllFlags)
Definition: ELFObjcopy.cpp:72
isUnneededSymbol
static bool isUnneededSymbol(const Symbol &Sym)
Definition: ELFObjcopy.cpp:263
replaceAndRemoveSections
static Error replaceAndRemoveSections(const CommonConfig &Config, const ELFConfig &ELFConfig, Object &Obj)
Definition: ELFObjcopy.cpp:369
llvm::objcopy::SectionFlag
SectionFlag
Definition: CommonConfig.h:57
llvm::StringMapImpl::empty
bool empty() const
Definition: StringMap.h:94
SmallVector.h
llvm::objcopy::elf::SectionBase
Definition: ELFObject.h:390
llvm::objcopy::elf::Object::OSABI
uint8_t OSABI
Definition: ELFObject.h:1049
llvm::objcopy::CommonConfig::SymbolsToRemove
NameMatcher SymbolsToRemove
Definition: CommonConfig.h:235
llvm::ELF::SHT_RELA
@ SHT_RELA
Definition: ELF.h:983
llvm::objcopy::CommonConfig::OutputFormat
FileFormat OutputFormat
Definition: CommonConfig.h:206
llvm::ELF::STB_WEAK
@ STB_WEAK
Definition: ELF.h:1219
llvm::objcopy::CommonConfig::DecompressDebugSections
bool DecompressDebugSections
Definition: CommonConfig.h:264
llvm::objcopy::CommonConfig::ExtractMainPartition
bool ExtractMainPartition
Definition: CommonConfig.h:253
llvm::ELF::STB_LOCAL
@ STB_LOCAL
Definition: ELF.h:1217
llvm::ELF::STV_DEFAULT
@ STV_DEFAULT
Definition: ELF.h:1247
addSymbol
static void addSymbol(Object &Obj, const NewSymbolInfo &SymInfo, uint8_t DefaultVisibility)
Definition: ELFObjcopy.cpp:531
llvm::ELF::SHF_GROUP
@ SHF_GROUP
Definition: ELF.h:1086
llvm::ELF::SHN_ABS
@ SHN_ABS
Definition: ELF.h:971
SymInfo
SymInfo contains information about symbol: it's address and section index which is -1LL for absolute ...
Definition: DWARFContext.cpp:1458
llvm::Optional::value
constexpr const T & value() const &
Definition: Optional.h:281
llvm::objcopy::ELFConfig::KeepFileSymbols
bool KeepFileSymbols
Definition: ELFConfig.h:31
llvm::ELF::SHN_UNDEF
@ SHN_UNDEF
Definition: ELF.h:965
dumpSectionToFile
static Error dumpSectionToFile(StringRef SecName, StringRef Filename, Object &Obj)
Definition: ELFObjcopy.cpp:175
llvm::objcopy::NewSymbolInfo
Definition: CommonConfig.h:182
llvm::objcopy::CommonConfig::ExtractDWO
bool ExtractDWO
Definition: CommonConfig.h:252
llvm::objcopy::CommonConfig::ToRemove
NameMatcher ToRemove
Definition: CommonConfig.h:229
llvm::StringRef::str
std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:221
llvm::objcopy::ELFConfig::EntryExpr
std::function< uint64_t(uint64_t)> EntryExpr
Definition: ELFConfig.h:28
llvm::objcopy::elf::Object::SymbolTable
SymbolTableSection * SymbolTable
Definition: ELFObject.h:1061
llvm::objcopy::CommonConfig::SetSectionFlags
StringMap< SectionFlagsUpdate > SetSectionFlags
Definition: CommonConfig.h:243
llvm::objcopy::CommonConfig
Definition: CommonConfig.h:201
llvm::objcopy::elf::RelocationSectionBase
Definition: ELFObject.h:748
llvm::AMDGPU::HSAMD::Kernel::Key::Args
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
Definition: AMDGPUMetadata.h:394
llvm::objcopy::SymbolFlag
SymbolFlag
Definition: CommonConfig.h:160
llvm::ELF::EM_AARCH64
@ EM_AARCH64
Definition: ELF.h:280
llvm::ARMBuildAttrs::File
@ File
Definition: ARMBuildAttributes.h:36
raw_ostream.h
llvm::objcopy::CommonConfig::Weaken
bool Weaken
Definition: CommonConfig.h:263
ELFObjcopy.h
copy
we should consider alternate ways to model stack dependencies Lots of things could be done in WebAssemblyTargetTransformInfo cpp there are numerous optimization related hooks that can be overridden in WebAssemblyTargetLowering Instead of the OptimizeReturned which should consider preserving the returned attribute through to MachineInstrs and extending the MemIntrinsicResults pass to do this optimization on calls too That would also let the WebAssemblyPeephole pass clean up dead defs for such as it does for stores Consider implementing and or getMachineCombinerPatterns Find a clean way to fix the problem which leads to the Shrink Wrapping pass being run after the WebAssembly PEI pass When setting multiple variables to the same we currently get code like const It could be done with a smaller encoding like local tee $pop5 local copy
Definition: README.txt:101
llvm::object::ELFObjectFile
Definition: ELFObjectFile.h:240
llvm::ELF::STT_FILE
@ STT_FILE
Definition: ELF.h:1233
llvm::ELF::STT_OBJECT
@ STT_OBJECT
Definition: ELF.h:1230
llvm::ELF::SHT_REL
@ SHT_REL
Definition: ELF.h:988
llvm::objcopy
Definition: COFFConfig.h:15
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
llvm::objcopy::CommonConfig::AddGnuDebugLink
StringRef AddGnuDebugLink
Definition: CommonConfig.h:212
llvm::objcopy::CommonConfig::AllocSectionsPrefix
StringRef AllocSectionsPrefix
Definition: CommonConfig.h:218
llvm::objcopy::CommonConfig::OnlyKeepDebug
bool OnlyKeepDebug
Definition: CommonConfig.h:254
llvm::objcopy::elf::Symbol::isCommon
bool isCommon() const
Definition: ELFObject.cpp:679
llvm::objcopy::elf::executeObjcopyOnIHex
Error executeObjcopyOnIHex(const CommonConfig &Config, const ELFConfig &ELFConfig, MemoryBuffer &In, raw_ostream &Out)
Apply the transformations described by Config and ELFConfig to In, which must represent an IHex file,...
Definition: ELFObjcopy.cpp:770
llvm::objcopy::SecReadonly
@ SecReadonly
Definition: CommonConfig.h:62
llvm::objcopy::elf::Symbol::Referenced
bool Referenced
Definition: ELFObject.h:646