43#include <system_error>
74 if (AllFlags & SectionFlag::SecAlloc)
76 if (!(AllFlags & SectionFlag::SecReadonly))
78 if (AllFlags & SectionFlag::SecCode)
80 if (AllFlags & SectionFlag::SecMerge)
82 if (AllFlags & SectionFlag::SecStrings)
84 if (AllFlags & SectionFlag::SecExclude)
86 if (AllFlags & SectionFlag::SecLarge) {
89 "section flag SHF_X86_64_LARGE can only be used "
90 "with x86_64 architecture");
107 return (OldFlags & PreserveMask) | (NewFlags & ~PreserveMask);
130 Flags & (SectionFlag::SecContents | SectionFlag::SecLoad)))
161 switch (OutputElfType) {
163 return std::make_unique<ELFWriter<ELF32LE>>(Obj, Out, !
Config.StripSections,
166 return std::make_unique<ELFWriter<ELF64LE>>(Obj, Out, !
Config.StripSections,
169 return std::make_unique<ELFWriter<ELF32BE>>(Obj, Out, !
Config.StripSections,
172 return std::make_unique<ELFWriter<ELF64BE>>(Obj, Out, !
Config.StripSections,
181 switch (
Config.OutputFormat) {
182 case FileFormat::Binary:
183 return std::make_unique<BinaryWriter>(Obj, Out);
184 case FileFormat::IHex:
185 return std::make_unique<IHexWriter>(Obj, Out);
194 if (Sec.Name == SecName) {
197 "cannot dump section '%s': it has no contents",
198 SecName.
str().c_str());
203 std::unique_ptr<FileOutputBuffer> Buf = std::move(*BufferOrErr);
204 std::copy(Sec.OriginalData.begin(), Sec.OriginalData.end(),
205 Buf->getBufferStart());
206 if (
Error E = Buf->commit())
212 SecName.
str().c_str());
228 if (ShouldReplace(Sec))
238 FromTo[S] = *NewSection;
249 if (!
Name.consume_front(
"$x") && !
Name.consume_front(
"$d"))
251 return Name.empty() ||
Name.startswith(
".");
259 if (!
Name.consume_front(
"$a") && !
Name.consume_front(
"$d") &&
260 !
Name.consume_front(
"$t"))
262 return Name.empty() ||
Name.startswith(
".");
280 return !
Sym.Referenced &&
298 Config.SymbolsToLocalize.matches(
Sym.Name)))
311 if (!
Config.SymbolsToKeepGlobal.empty() &&
312 !
Config.SymbolsToKeepGlobal.matches(
Sym.Name) &&
328 const auto I =
Config.SymbolsToRename.find(
Sym.Name);
330 Sym.Name = std::string(
I->getValue());
339 if (
Config.StripUnneeded || !
Config.UnneededSymbolsToRemove.empty() ||
340 !
Config.OnlySection.empty()) {
345 auto RemoveSymbolsPred = [&](
const Symbol &
Sym) {
346 if (
Config.SymbolsToKeep.matches(
Sym.Name) ||
350 if (
Config.SymbolsToRemove.matches(
Sym.Name))
362 if ((
Config.DiscardMode == DiscardType::All ||
363 (
Config.DiscardMode == DiscardType::Locals &&
369 if ((
Config.StripUnneeded ||
370 Config.UnneededSymbolsToRemove.matches(
Sym.Name)) &&
375 if (!
Config.OnlySection.empty() && !
Sym.Referenced &&
390 if (!
Config.ToRemove.empty()) {
392 return Config.ToRemove.matches(Sec.Name);
397 RemovePred = [RemovePred](
const SectionBase &Sec) {
402 RemovePred = [RemovePred, &Obj](
const SectionBase &Sec) {
407 RemovePred = [RemovePred, &Obj](
const SectionBase &Sec) {
424 if (
Config.StripSections) {
425 RemovePred = [RemovePred](
const SectionBase &Sec) {
426 return RemovePred(Sec) || Sec.ParentSegment ==
nullptr;
431 RemovePred = [RemovePred](
const SectionBase &Sec) {
437 RemovePred = [RemovePred, &Obj](
const SectionBase &Sec) {
442 return (Sec.Flags &
SHF_ALLOC) == 0 && Sec.ParentSegment ==
nullptr;
446 RemovePred = [RemovePred, &Obj](
const SectionBase &Sec) {
459 if (Sec.ParentSegment !=
nullptr)
464 if (
Config.ExtractPartition ||
Config.ExtractMainPartition) {
465 RemovePred = [RemovePred](
const SectionBase &Sec) {
470 return (Sec.Flags &
SHF_ALLOC) != 0 && !Sec.ParentSegment;
475 if (!
Config.OnlySection.empty()) {
478 if (
Config.OnlySection.matches(Sec.Name))
497 if (!
Config.KeepSection.empty()) {
500 if (
Config.KeepSection.matches(Sec.Name))
503 return RemovePred(Sec);
514 RemovePred = [&Obj, RemovePred](
const SectionBase &Sec) {
517 return RemovePred(Sec);
524 if (
Config.CompressionType != DebugCompressionType::None) {
532 }
else if (
Config.DecompressDebugSections) {
535 [](
const SectionBase &S) {
return isa<CompressedSection>(&S); },
548 uint8_t DefaultVisibility) {
554 uint8_t Visibility = DefaultVisibility;
558 case SymbolFlag::Global:
561 case SymbolFlag::Local:
564 case SymbolFlag::Weak:
567 case SymbolFlag::Default:
570 case SymbolFlag::Hidden:
573 case SymbolFlag::Protected:
576 case SymbolFlag::File:
579 case SymbolFlag::Section:
582 case SymbolFlag::Object:
585 case SymbolFlag::Function:
588 case SymbolFlag::IndirectFunction:
626 [&Obj](
const SectionBase &Sec) { return onlyKeepDWOPred(Obj, Sec); });
648 if (!
Config.SetSectionAlignment.empty()) {
650 auto I =
Config.SetSectionAlignment.find(Sec.Name);
651 if (
I !=
Config.SetSectionAlignment.end())
652 Sec.Align =
I->second;
665 if (
Name.startswith(
".note") &&
Name !=
".note.GNU-stack")
681 if (!
Config.AddGnuDebugLink.empty())
683 Config.GnuDebugLinkCRC32);
695 if (!
Config.SetSectionFlags.empty() || !
Config.SetSectionType.empty()) {
697 const auto Iter =
Config.SetSectionFlags.find(Sec.Name);
698 if (Iter !=
Config.SetSectionFlags.end()) {
703 auto It2 =
Config.SetSectionType.find(Sec.Name);
704 if (It2 !=
Config.SetSectionType.end())
709 if (!
Config.SectionsToRename.empty()) {
710 std::vector<RelocationSectionBase *> RelocSections;
713 auto *RelocSec = dyn_cast<RelocationSectionBase>(&Sec);
714 const auto Iter =
Config.SectionsToRename.find(Sec.Name);
715 if (Iter !=
Config.SectionsToRename.end()) {
717 Sec.Name = std::string(SR.
NewName);
722 RenamedSections.
insert(&Sec);
723 }
else if (RelocSec && !(Sec.Flags &
SHF_ALLOC))
730 RelocSections.push_back(RelocSec);
735 auto Iter = RenamedSections.
find(RelocSec->getSection());
736 if (Iter != RenamedSections.
end())
737 RelocSec->Name = (RelocSec->getNamePrefix() + (*Iter)->Name).str();
744 if (!
Config.AllocSectionsPrefix.empty()) {
748 Sec.Name = (
Config.AllocSectionsPrefix + Sec.Name).str();
749 PrefixedSections.
insert(&Sec);
750 }
else if (
auto *RelocSec = dyn_cast<RelocationSectionBase>(&Sec)) {
758 const SectionBase *TargetSec = RelocSec->getSection();
764 if (PrefixedSections.
count(TargetSec))
765 Sec.
Name = (RelocSec->getNamePrefix() + TargetSec->
Name).str();
767 Sec.Name = (RelocSec->getNamePrefix() +
Config.AllocSectionsPrefix +
782 std::unique_ptr<Writer>
Writer =
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file defines the DenseSet and SmallDenseSet classes.
static Error replaceAndRemoveSections(const CommonConfig &Config, const ELFConfig &ELFConfig, Object &Obj)
static bool isArmMappingSymbol(const Symbol &Sym)
static Error handleUserSection(const NewSectionInfo &NewSection, function_ref< Error(StringRef, ArrayRef< uint8_t >)> F)
static void addSymbol(Object &Obj, const NewSymbolInfo &SymInfo, uint8_t DefaultVisibility)
static Error updateAndRemoveSymbols(const CommonConfig &Config, const ELFConfig &ELFConfig, Object &Obj)
std::function< bool(const SectionBase &Sec)> SectionPred
static bool isDWOSection(const SectionBase &Sec)
static bool isCompressable(const SectionBase &Sec)
static bool isRequiredByABISymbol(const Object &Obj, const Symbol &Sym)
static Error replaceDebugSections(Object &Obj, function_ref< bool(const SectionBase &)> ShouldReplace, function_ref< Expected< SectionBase * >(const SectionBase *)> AddSection)
static bool isDebugSection(const SectionBase &Sec)
static Error dumpSectionToFile(StringRef SecName, StringRef Filename, Object &Obj)
static Error writeOutput(const CommonConfig &Config, Object &Obj, raw_ostream &Out, ElfType OutputElfType)
static bool isUnneededSymbol(const Symbol &Sym)
static Error handleArgs(const CommonConfig &Config, const ELFConfig &ELFConfig, Object &Obj)
static bool isAArch64MappingSymbol(const Symbol &Sym)
static bool onlyKeepDWOPred(const Object &Obj, const SectionBase &Sec)
static void setSectionType(SectionBase &Sec, uint64_t Type)
static Expected< uint64_t > getNewShfFlags(SectionFlag AllFlags, uint16_t EMachine)
static Error setSectionFlagsAndType(SectionBase &Sec, SectionFlag Flags, uint16_t EMachine)
static std::unique_ptr< Writer > createELFWriter(const CommonConfig &Config, Object &Obj, raw_ostream &Out, ElfType OutputElfType)
static uint64_t getSectionFlagsPreserveMask(uint64_t OldFlags, uint64_t NewFlags, uint16_t EMachine)
static std::unique_ptr< Writer > createWriter(const CommonConfig &Config, Object &Obj, raw_ostream &Out, ElfType OutputElfType)
static ElfType getOutputElfType(const Binary &Bin)
Provides ErrorOr<T> smart pointer.
This file defines the SmallVector class.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Implements a dense probed hash-table based set.
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
Tagged union holding either a T or a Error.
Error takeError()
Take ownership of the stored error.
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...
This interface provides simple read-only access to a block of memory, and provides simple methods for...
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
std::string str() const
str - Get the contents as an std::string.
bool startswith(StringRef Prefix) const
bool endswith(StringRef Suffix) const
The instances of the Type class are immutable: once they are created, they are never changed.
LLVM Value Representation.
std::pair< iterator, bool > insert(const ValueT &V)
iterator find(const_arg_type_t< ValueT > V)
size_type count(const_arg_type_t< ValueT > V) const
Return 1 if the specified key is in the set, 0 otherwise.
An efficient, type-erasing, non-owning reference to a callable.
SectionTableRef sections() const
StringTableSection * SectionNames
bool isRelocatable() const
Error updateSection(StringRef Name, ArrayRef< uint8_t > Data)
Error removeSymbols(function_ref< bool(const Symbol &)> ToRemove)
T & addSection(Ts &&...Args)
Error removeSections(bool AllowBrokenLinks, std::function< bool(const SectionBase &)> ToRemove)
SymbolTableSection * SymbolTable
SectionBase * findSection(StringRef Name)
Error addNewSymbolTable()
Error replaceSections(const DenseMap< SectionBase *, SectionBase * > &FromTo)
virtual Expected< std::unique_ptr< Object > > create(bool EnsureSymtab) const =0
const SectionBase * getStrTab() const
void addSymbol(Twine Name, uint8_t Bind, uint8_t Type, SectionBase *DefinedIn, uint64_t Value, uint8_t Visibility, uint16_t Shndx, uint64_t SymbolSize)
void updateSymbols(function_ref< void(Symbol &)> Callable)
virtual Error finalize()=0
This class implements an extremely fast bulk output stream that can only output to a stream.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
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,...
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.
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...
This is an optimization pass for GlobalISel generic memory operations.
Error createFileError(const Twine &F, Error E)
Concatenate a source file path and/or name with an Error.
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
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...
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
SymInfo contains information about symbol: it's address and section index which is -1LL for absolute ...
uint8_t NewSymbolVisibility
std::function< uint64_t(uint64_t)> EntryExpr
std::shared_ptr< MemoryBuffer > SectionData
std::optional< SectionFlag > NewFlags