LLVM  15.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.getValue().EMachine;
604  Obj.OSABI = Config.OutputArch.getValue().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.SectionsToRename.empty()) {
633  std::vector<RelocationSectionBase *> RelocSections;
634  DenseSet<SectionBase *> RenamedSections;
635  for (SectionBase &Sec : Obj.sections()) {
636  auto *RelocSec = dyn_cast<RelocationSectionBase>(&Sec);
637  const auto Iter = Config.SectionsToRename.find(Sec.Name);
638  if (Iter != Config.SectionsToRename.end()) {
639  const SectionRename &SR = Iter->second;
640  Sec.Name = std::string(SR.NewName);
641  if (SR.NewFlags)
643  RenamedSections.insert(&Sec);
644  } else if (RelocSec && !(Sec.Flags & SHF_ALLOC))
645  // Postpone processing relocation sections which are not specified in
646  // their explicit '--rename-section' commands until after their target
647  // sections are renamed.
648  // Dynamic relocation sections (i.e. ones with SHF_ALLOC) should be
649  // renamed only explicitly. Otherwise, renaming, for example, '.got.plt'
650  // would affect '.rela.plt', which is not desirable.
651  RelocSections.push_back(RelocSec);
652  }
653 
654  // Rename relocation sections according to their target sections.
655  for (RelocationSectionBase *RelocSec : RelocSections) {
656  auto Iter = RenamedSections.find(RelocSec->getSection());
657  if (Iter != RenamedSections.end())
658  RelocSec->Name = (RelocSec->getNamePrefix() + (*Iter)->Name).str();
659  }
660  }
661 
662  // Add a prefix to allocated sections and their relocation sections. This
663  // should be done after renaming the section by Config.SectionToRename to
664  // imitate the GNU objcopy behavior.
665  if (!Config.AllocSectionsPrefix.empty()) {
666  DenseSet<SectionBase *> PrefixedSections;
667  for (SectionBase &Sec : Obj.sections()) {
668  if (Sec.Flags & SHF_ALLOC) {
669  Sec.Name = (Config.AllocSectionsPrefix + Sec.Name).str();
670  PrefixedSections.insert(&Sec);
671  } else if (auto *RelocSec = dyn_cast<RelocationSectionBase>(&Sec)) {
672  // Rename relocation sections associated to the allocated sections.
673  // For example, if we rename .text to .prefix.text, we also rename
674  // .rel.text to .rel.prefix.text.
675  //
676  // Dynamic relocation sections (SHT_REL[A] with SHF_ALLOC) are handled
677  // above, e.g., .rela.plt is renamed to .prefix.rela.plt, not
678  // .rela.prefix.plt since GNU objcopy does so.
679  const SectionBase *TargetSec = RelocSec->getSection();
680  if (TargetSec && (TargetSec->Flags & SHF_ALLOC)) {
681  // If the relocation section comes *after* the target section, we
682  // don't add Config.AllocSectionsPrefix because we've already added
683  // the prefix to TargetSec->Name. Otherwise, if the relocation
684  // section comes *before* the target section, we add the prefix.
685  if (PrefixedSections.count(TargetSec))
686  Sec.Name = (RelocSec->getNamePrefix() + TargetSec->Name).str();
687  else
688  Sec.Name = (RelocSec->getNamePrefix() + Config.AllocSectionsPrefix +
689  TargetSec->Name)
690  .str();
691  }
692  }
693  }
694  }
695 
696  if (!Config.SetSectionAlignment.empty()) {
697  for (SectionBase &Sec : Obj.sections()) {
698  auto I = Config.SetSectionAlignment.find(Sec.Name);
699  if (I != Config.SetSectionAlignment.end())
700  Sec.Align = I->second;
701  }
702  }
703 
704  if (Config.OnlyKeepDebug)
705  for (auto &Sec : Obj.sections())
706  if (Sec.Flags & SHF_ALLOC && Sec.Type != SHT_NOTE)
707  Sec.Type = SHT_NOBITS;
708 
709  for (const NewSectionInfo &AddedSection : Config.AddSection) {
710  auto AddSection = [&](StringRef Name, ArrayRef<uint8_t> Data) {
711  OwnedDataSection &NewSection =
713  if (Name.startswith(".note") && Name != ".note.GNU-stack")
714  NewSection.Type = SHT_NOTE;
715  return Error::success();
716  };
717  if (Error E = handleUserSection(AddedSection, AddSection))
718  return E;
719  }
720 
721  for (const NewSectionInfo &NewSection : Config.UpdateSection) {
722  auto UpdateSection = [&](StringRef Name, ArrayRef<uint8_t> Data) {
723  return Obj.updateSection(Name, Data);
724  };
725  if (Error E = handleUserSection(NewSection, UpdateSection))
726  return E;
727  }
728 
729  if (!Config.AddGnuDebugLink.empty())
731  Config.GnuDebugLinkCRC32);
732 
733  // If the symbol table was previously removed, we need to create a new one
734  // before adding new symbols.
735  if (!Obj.SymbolTable && !Config.SymbolsToAdd.empty())
736  if (Error E = Obj.addNewSymbolTable())
737  return E;
738 
739  for (const NewSymbolInfo &SI : Config.SymbolsToAdd)
741 
742  // --set-section-flags works with sections added by --add-section.
743  if (!Config.SetSectionFlags.empty()) {
744  for (auto &Sec : Obj.sections()) {
745  const auto Iter = Config.SetSectionFlags.find(Sec.Name);
746  if (Iter != Config.SetSectionFlags.end()) {
747  const SectionFlagsUpdate &SFU = Iter->second;
749  }
750  }
751  }
752 
753  if (ELFConfig.EntryExpr)
754  Obj.Entry = ELFConfig.EntryExpr(Obj.Entry);
755  return Error::success();
756 }
757 
758 static Error writeOutput(const CommonConfig &Config, Object &Obj,
759  raw_ostream &Out, ElfType OutputElfType) {
760  std::unique_ptr<Writer> Writer =
761  createWriter(Config, Obj, Out, OutputElfType);
762  if (Error E = Writer->finalize())
763  return E;
764  return Writer->write();
765 }
766 
768  const ELFConfig &ELFConfig,
769  MemoryBuffer &In, raw_ostream &Out) {
770  IHexReader Reader(&In);
772  if (!Obj)
773  return Obj.takeError();
774 
775  const ElfType OutputElfType =
776  getOutputElfType(Config.OutputArch.value_or(MachineInfo()));
777  if (Error E = handleArgs(Config, ELFConfig, **Obj))
778  return E;
779  return writeOutput(Config, **Obj, Out, OutputElfType);
780 }
781 
783  const ELFConfig &ELFConfig,
784  MemoryBuffer &In,
785  raw_ostream &Out) {
788  if (!Obj)
789  return Obj.takeError();
790 
791  // Prefer OutputArch (-O<format>) if set, otherwise fallback to BinaryArch
792  // (-B<arch>).
793  const ElfType OutputElfType =
794  getOutputElfType(Config.OutputArch.value_or(MachineInfo()));
795  if (Error E = handleArgs(Config, ELFConfig, **Obj))
796  return E;
797  return writeOutput(Config, **Obj, Out, OutputElfType);
798 }
799 
801  const ELFConfig &ELFConfig,
803  raw_ostream &Out) {
806  Reader.create(!Config.SymbolsToAdd.empty());
807  if (!Obj)
808  return Obj.takeError();
809  // Prefer OutputArch (-O<format>) if set, otherwise infer it from the input.
810  const ElfType OutputElfType =
811  Config.OutputArch ? getOutputElfType(Config.OutputArch.getValue())
812  : getOutputElfType(In);
813 
814  if (Error E = handleArgs(Config, ELFConfig, **Obj))
815  return createFileError(Config.InputFilename, std::move(E));
816 
817  if (Error E = writeOutput(Config, **Obj, Out, OutputElfType))
818  return createFileError(Config.InputFilename, std::move(E));
819 
820  return Error::success();
821 }
llvm::objcopy::elf::Reader::create
virtual Expected< std::unique_ptr< Object > > create(bool EnsureSymtab) const =0
llvm::ELF::STT_FUNC
@ STT_FUNC
Definition: ELF.h:1205
Option.h
Compression.h
llvm::StringRef::startswith
LLVM_NODISCARD bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:290
MI
IRTranslator LLVM IR MI
Definition: IRTranslator.cpp:104
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::objcopy::elf::Symbol::getShndx
uint16_t getShndx() const
Definition: ELFObject.cpp:673
llvm::objcopy::CommonConfig::InputFilename
StringRef InputFilename
Definition: CommonConfig.h:203
llvm::objcopy::elf::Object::updateSection
Error updateSection(StringRef Name, ArrayRef< uint8_t > Data)
Definition: ELFObject.cpp:2117
handleArgs
static Error handleArgs(const CommonConfig &Config, const ELFConfig &ELFConfig, Object &Obj)
Definition: ELFObjcopy.cpp:600
Optional.h
llvm::ELF::SHF_WRITE
@ SHF_WRITE
Definition: ELF.h:1035
FileSystem.h
MCTargetOptions.h
llvm::ELF::SHF_STRINGS
@ SHF_STRINGS
Definition: ELF.h:1047
llvm::objcopy::elf::ELFT_ELF64LE
@ ELFT_ELF64LE
Definition: ELFObject.h:71
llvm::StringRef::endswith
LLVM_NODISCARD bool endswith(StringRef Suffix) const
Check if this string ends with the given Suffix.
Definition: StringRef.h:301
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:1047
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:1185
Path.h
llvm::objcopy::elf::DecompressedSection
Definition: ELFObject.h:561
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
llvm::ELF::STT_GNU_IFUNC
@ STT_GNU_IFUNC
Definition: ELF.h:1210
Error.h
llvm::ELF::SHF_TLS
@ SHF_TLS
Definition: ELF.h:1063
llvm::cl::Hidden
@ Hidden
Definition: CommandLine.h:139
llvm::objcopy::elf::Object::isRelocatable
bool isRelocatable() const
Definition: ELFObject.h:1099
llvm::objcopy::elf::Object::removeSymbols
Error removeSymbols(function_ref< bool(const Symbol &)> ToRemove)
Definition: ELFObject.cpp:2224
llvm::objcopy::CommonConfig::SymbolsToRename
StringMap< StringRef > SymbolsToRename
Definition: CommonConfig.h:244
llvm::ELF::SHT_STRTAB
@ SHT_STRTAB
Definition: ELF.h:957
llvm::objcopy::CommonConfig::DiscardMode
DiscardType DiscardMode
Definition: CommonConfig.h:219
Errc.h
llvm::objcopy::CommonConfig::StripSections
bool StripSections
Definition: CommonConfig.h:260
llvm::StringMap::end
iterator end()
Definition: StringMap.h:205
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
llvm::objcopy::SectionRename::NewName
StringRef NewName
Definition: CommonConfig.h:77
llvm::objcopy::CommonConfig::StripNonAlloc
bool StripNonAlloc
Definition: CommonConfig.h:259
llvm::isa
LLVM_NODISCARD 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
llvm::ELF::SHF_ALLOC
@ SHF_ALLOC
Definition: ELF.h:1038
llvm::objcopy::elf::SectionBase::Name
std::string Name
Definition: ELFObject.h:392
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
STLExtras.h
llvm::StringMap::find
iterator find(StringRef Key)
Definition: StringMap.h:218
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::SecAlloc
@ SecAlloc
Definition: CommonConfig.h:59
llvm::objcopy::elf::Object::replaceSections
Error replaceSections(const DenseMap< SectionBase *, SectionBase * > &FromTo)
Definition: ELFObject.cpp:2200
llvm::MemoryBuffer
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:50
llvm::objcopy::elf::Object::addSection
T & addSection(Ts &&...Args)
Definition: ELFObject.h:1086
llvm::ELF::SHF_INFO_LINK
@ SHF_INFO_LINK
Definition: ELF.h:1050
F
#define F(x, y, z)
Definition: MD5.cpp:55
llvm::SubDirectoryType::Bin
@ Bin
llvm::ELF::SHF_COMPRESSED
@ SHF_COMPRESSED
Definition: ELF.h:1066
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:1050
llvm::ELF::SHT_LLVM_PART_EHDR
@ SHT_LLVM_PART_EHDR
Definition: ELF.h:986
llvm::objcopy::elf::Object
Definition: ELFObject.h:1017
llvm::object
Definition: DWARFDebugLoc.h:25
llvm::objcopy::elf::Object::findSection
SectionBase * findSection(StringRef Name)
Definition: ELFObject.h:1073
llvm::objcopy::elf::Symbol::Type
uint8_t Type
Definition: ELFObject.h:640
Error.h
llvm::objcopy::elf::Object::addNewSymbolTable
Error addNewSymbolTable()
Definition: ELFObject.cpp:2232
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:700
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:1320
llvm::objcopy::elf::Reader
Definition: ELFObject.h:900
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::ELF::STB_GLOBAL
@ STB_GLOBAL
Definition: ELF.h:1192
llvm::objcopy::CommonConfig::CompressionType
DebugCompressionType CompressionType
Definition: CommonConfig.h:265
llvm::objcopy::NewSectionInfo
Definition: CommonConfig.h:191
Twine.h
llvm::objcopy::elf::IHexReader
Definition: ELFObject.h:985
llvm::objcopy::CommonConfig::SymbolsToKeep
NameMatcher SymbolsToKeep
Definition: CommonConfig.h:233
llvm::objcopy::elf::Writer::write
virtual Error write()=0
llvm::objcopy::elf::Object::SectionNames
StringTableSection * SectionNames
Definition: ELFObject.h:1056
llvm::objcopy::CommonConfig::AddSection
std::vector< NewSectionInfo > AddSection
Definition: CommonConfig.h:222
writeOutput
static Error writeOutput(const CommonConfig &Config, Object &Obj, raw_ostream &Out, ElfType OutputElfType)
Definition: ELFObjcopy.cpp:758
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:800
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::ELF::SHN_ABS
@ SHN_ABS
Definition: ELF.h:946
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:54
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:708
llvm::ELF::STV_DEFAULT
@ STV_DEFAULT
Definition: ELF.h:1221
CommonConfig.h
llvm::ELF::STT_SECTION
@ STT_SECTION
Definition: ELF.h:1206
llvm::objcopy::elf::ELFReader
Definition: ELFObject.h:1007
llvm::StringRef::str
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:249
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:612
llvm::objcopy::elf::BinaryReader
Definition: ELFObject.h:975
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::SHT_NOBITS
@ SHT_NOBITS
Definition: ELF.h:962
llvm::objcopy::NameMatcher::empty
bool empty() const
Definition: CommonConfig.h:155
llvm::ELF::STV_HIDDEN
@ STV_HIDDEN
Definition: ELF.h:1223
llvm::objcopy::elf::Symbol
Definition: ELFObject.h:632
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::objcopy::SecMerge
@ SecMerge
Definition: CommonConfig.h:67
llvm::objcopy::CommonConfig::StripUnneeded
bool StripUnneeded
Definition: CommonConfig.h:261
llvm::ELF::SHF_GROUP
@ SHF_GROUP
Definition: ELF.h:1060
llvm::DenseSet
Implements a dense probed hash-table based set.
Definition: DenseSet.h:268
llvm::tgtok::In
@ In
Definition: TGLexer.h:51
llvm::objcopy::CommonConfig::SplitDWO
StringRef SplitDWO
Definition: CommonConfig.h:216
llvm::objcopy::CommonConfig::StripAllGNU
bool StripAllGNU
Definition: CommonConfig.h:256
llvm::objcopy::CommonConfig::SymbolsToLocalize
NameMatcher SymbolsToLocalize
Definition: CommonConfig.h:234
llvm::StringRef::empty
constexpr LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:153
llvm::objcopy::CommonConfig::SymbolsToKeepGlobal
NameMatcher SymbolsToKeepGlobal
Definition: CommonConfig.h:238
uint64_t
llvm::ELF::SHT_SYMTAB
@ SHT_SYMTAB
Definition: ELF.h:956
llvm::ELF::STT_NOTYPE
@ STT_NOTYPE
Definition: ELF.h:1203
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::STB_LOCAL
@ STB_LOCAL
Definition: ELF.h:1191
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:716
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::objcopy::elf::ELFT_ELF64BE
@ ELFT_ELF64BE
Definition: ELFObject.h:71
llvm::objcopy::elf::Symbol::Visibility
uint8_t Visibility
Definition: ELFObject.h:642
llvm::ELF::SHF_LINK_ORDER
@ SHF_LINK_ORDER
Definition: ELF.h:1053
llvm::Optional::getValue
constexpr const T & getValue() const &
Definition: Optional.h:306
SI
StandardInstrumentations SI(Debug, VerifyEach)
llvm::objcopy::CommonConfig::GnuDebugLinkCRC32
uint32_t GnuDebugLinkCRC32
Definition: CommonConfig.h:214
llvm::objcopy::CommonConfig::StripDebug
bool StripDebug
Definition: CommonConfig.h:258
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::ELF::SHT_RELA
@ SHT_RELA
Definition: ELF.h:958
llvm::objcopy::CommonConfig::SymbolsToAdd
std::vector< NewSymbolInfo > SymbolsToAdd
Definition: CommonConfig.h:247
llvm::ELF::STV_INTERNAL
@ STV_INTERNAL
Definition: ELF.h:1222
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:702
llvm::ELF::SHT_NOTE
@ SHT_NOTE
Definition: ELF.h:961
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::object::Binary
Definition: Binary.h:32
llvm::ELF::SHF_MASKPROC
@ SHF_MASKPROC
Definition: ELF.h:1082
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:633
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:257
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:637
llvm::objcopy::SectionRename
Definition: CommonConfig.h:75
llvm::objcopy::elf::SymbolTableSection::updateSymbols
void updateSymbols(function_ref< void(Symbol &)> Callable)
Definition: ELFObject.cpp:742
llvm::objcopy::elf
Definition: ELFObjcopy.h:25
llvm::objcopy::SectionFlagsUpdate::NewFlags
SectionFlag NewFlags
Definition: CommonConfig.h:83
llvm::StringRef::size
constexpr LLVM_NODISCARD size_t size() const
size - Get the string size.
Definition: StringRef.h:157
llvm::ELF::SHF_MASKOS
@ SHF_MASKOS
Definition: ELF.h:1076
llvm::ELF::STV_PROTECTED
@ STV_PROTECTED
Definition: ELF.h:1224
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:83
llvm::objcopy::elf::Object::sections
SectionTableRef sections() const
Definition: ELFObject.h:1062
llvm::createStringError
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition: Error.h:1239
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:50
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:2148
llvm::ELF::SHF_MERGE
@ SHF_MERGE
Definition: ELF.h:1044
llvm::SectionName
Definition: DWARFSection.h:21
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:155
llvm::ELF::STT_OBJECT
@ STT_OBJECT
Definition: ELF.h:1204
llvm::objcopy::CommonConfig::UpdateSection
std::vector< NewSectionInfo > UpdateSection
Definition: CommonConfig.h:224
Casting.h
llvm::objcopy::elf::Writer::finalize
virtual Error finalize()=0
llvm::ELF::SHT_REL
@ SHT_REL
Definition: ELF.h:963
llvm::objcopy::SectionFlagsUpdate
Definition: CommonConfig.h:81
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:782
llvm::objcopy::CommonConfig::StripAll
bool StripAll
Definition: CommonConfig.h:255
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:597
Binary.h
llvm::objcopy::CommonConfig::SectionsToRename
StringMap< SectionRename > SectionsToRename
Definition: CommonConfig.h:241
getNewShfFlags
static uint64_t getNewShfFlags(SectionFlag AllFlags)
Definition: ELFObjcopy.cpp:72
isUnneededSymbol
static bool isUnneededSymbol(const Symbol &Sym)
Definition: ELFObjcopy.cpp:263
llvm::ELF::SHN_UNDEF
@ SHN_UNDEF
Definition: ELF.h:940
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:1045
llvm::objcopy::CommonConfig::SymbolsToRemove
NameMatcher SymbolsToRemove
Definition: CommonConfig.h:235
llvm::ELF::SHT_PROGBITS
@ SHT_PROGBITS
Definition: ELF.h:955
llvm::objcopy::CommonConfig::OutputFormat
FileFormat OutputFormat
Definition: CommonConfig.h:206
llvm::objcopy::CommonConfig::DecompressDebugSections
bool DecompressDebugSections
Definition: CommonConfig.h:263
llvm::objcopy::CommonConfig::ExtractMainPartition
bool ExtractMainPartition
Definition: CommonConfig.h:252
addSymbol
static void addSymbol(Object &Obj, const NewSymbolInfo &SymInfo, uint8_t DefaultVisibility)
Definition: ELFObjcopy.cpp:531
SymInfo
SymInfo contains information about symbol: it's address and section index which is -1LL for absolute ...
Definition: DWARFContext.cpp:1458
llvm::ELF::SHT_LLVM_PART_PHDR
@ SHT_LLVM_PART_PHDR
Definition: ELF.h:987
llvm::ELF::STT_FILE
@ STT_FILE
Definition: ELF.h:1207
llvm::objcopy::ELFConfig::KeepFileSymbols
bool KeepFileSymbols
Definition: ELFConfig.h:31
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:251
llvm::objcopy::CommonConfig::ToRemove
NameMatcher ToRemove
Definition: CommonConfig.h:229
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:1057
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:745
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
raw_ostream.h
llvm::objcopy::CommonConfig::Weaken
bool Weaken
Definition: CommonConfig.h:262
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::objcopy
Definition: COFFConfig.h:15
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
llvm::ELF::SHF_EXECINSTR
@ SHF_EXECINSTR
Definition: ELF.h:1041
llvm::objcopy::CommonConfig::AddGnuDebugLink
StringRef AddGnuDebugLink
Definition: CommonConfig.h:212
File
Instrumentation for Order File
Definition: InstrOrderFile.cpp:198
llvm::objcopy::CommonConfig::AllocSectionsPrefix
StringRef AllocSectionsPrefix
Definition: CommonConfig.h:218
llvm::objcopy::CommonConfig::OnlyKeepDebug
bool OnlyKeepDebug
Definition: CommonConfig.h:253
llvm::ELF::STB_WEAK
@ STB_WEAK
Definition: ELF.h:1193
llvm::ELF::SHF_EXCLUDE
@ SHF_EXCLUDE
Definition: ELF.h:1072
llvm::objcopy::elf::Symbol::isCommon
bool isCommon() const
Definition: ELFObject.cpp:692
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:767
llvm::objcopy::SecReadonly
@ SecReadonly
Definition: CommonConfig.h:62
llvm::objcopy::elf::Symbol::Referenced
bool Referenced
Definition: ELFObject.h:643
llvm::ELF::SHT_ARM_ATTRIBUTES
@ SHT_ARM_ATTRIBUTES
Definition: ELF.h:1009