43#include <system_error>
73 if (AllFlags & SectionFlag::SecAlloc)
75 if (!(AllFlags & SectionFlag::SecReadonly))
77 if (AllFlags & SectionFlag::SecCode)
79 if (AllFlags & SectionFlag::SecMerge)
81 if (AllFlags & SectionFlag::SecStrings)
83 if (AllFlags & SectionFlag::SecExclude)
97 return (OldFlags & PreserveMask) | (NewFlags & ~PreserveMask);
108 Flags & (SectionFlag::SecContents | SectionFlag::SecLoad)))
137 switch (OutputElfType) {
139 return std::make_unique<ELFWriter<ELF32LE>>(Obj, Out, !Config.
StripSections,
142 return std::make_unique<ELFWriter<ELF64LE>>(Obj, Out, !Config.
StripSections,
145 return std::make_unique<ELFWriter<ELF32BE>>(Obj, Out, !Config.
StripSections,
148 return std::make_unique<ELFWriter<ELF64BE>>(Obj, Out, !Config.
StripSections,
158 case FileFormat::Binary:
159 return std::make_unique<BinaryWriter>(Obj, Out);
160 case FileFormat::IHex:
161 return std::make_unique<IHexWriter>(Obj, Out);
167template <
class... Ts>
170 std::string FullMsg = (EC.message() +
": " + Msg).str();
177 if (Sec.Name == SecName) {
180 "cannot dump section '%s': it has no contents",
181 SecName.
str().c_str());
186 std::unique_ptr<FileOutputBuffer> Buf = std::move(*BufferOrErr);
187 std::copy(Sec.OriginalData.begin(), Sec.OriginalData.end(),
188 Buf->getBufferStart());
189 if (
Error E = Buf->commit())
195 SecName.
str().c_str());
211 if (ShouldReplace(Sec))
221 FromTo[S] = *NewSection;
232 if (!
Name.consume_front(
"$x") && !
Name.consume_front(
"$d"))
234 return Name.empty() ||
Name.startswith(
".");
242 if (!
Name.consume_front(
"$a") && !
Name.consume_front(
"$d") &&
243 !
Name.consume_front(
"$t"))
245 return Name.empty() ||
Name.startswith(
".");
313 Sym.
Name = std::string(
I->getValue());
328 auto RemoveSymbolsPred = [&](
const Symbol &Sym) {
380 RemovePred = [RemovePred](
const SectionBase &Sec) {
385 RemovePred = [RemovePred, &Obj](
const SectionBase &Sec) {
390 RemovePred = [RemovePred, &Obj](
const SectionBase &Sec) {
408 RemovePred = [RemovePred](
const SectionBase &Sec) {
409 return RemovePred(Sec) || Sec.ParentSegment ==
nullptr;
414 RemovePred = [RemovePred](
const SectionBase &Sec) {
420 RemovePred = [RemovePred, &Obj](
const SectionBase &Sec) {
425 return (Sec.Flags &
SHF_ALLOC) == 0 && Sec.ParentSegment ==
nullptr;
429 RemovePred = [RemovePred, &Obj](
const SectionBase &Sec) {
442 if (Sec.ParentSegment !=
nullptr)
448 RemovePred = [RemovePred](
const SectionBase &Sec) {
453 return (Sec.Flags &
SHF_ALLOC) != 0 && !Sec.ParentSegment;
459 RemovePred = [&Config, RemovePred, &Obj](
const SectionBase &Sec) {
481 RemovePred = [&Config, RemovePred](
const SectionBase &Sec) {
486 return RemovePred(Sec);
497 RemovePred = [&Obj, RemovePred](
const SectionBase &Sec) {
500 return RemovePred(Sec);
518 [](
const SectionBase &S) {
return isa<CompressedSection>(&S); },
531 uint8_t DefaultVisibility) {
537 uint8_t Visibility = DefaultVisibility;
541 case SymbolFlag::Global:
544 case SymbolFlag::Local:
547 case SymbolFlag::Weak:
550 case SymbolFlag::Default:
553 case SymbolFlag::Hidden:
556 case SymbolFlag::Protected:
559 case SymbolFlag::File:
562 case SymbolFlag::Section:
565 case SymbolFlag::Object:
568 case SymbolFlag::Function:
571 case SymbolFlag::IndirectFunction:
609 [&Obj](
const SectionBase &Sec) { return onlyKeepDWOPred(Obj, Sec); });
635 Sec.Align =
I->second;
648 if (
Name.startswith(
".note") &&
Name !=
".note.GNU-stack")
687 Sec.Type = It2->second;
692 std::vector<RelocationSectionBase *> RelocSections;
695 auto *RelocSec = dyn_cast<RelocationSectionBase>(&Sec);
699 Sec.Name = std::string(SR.
NewName);
702 RenamedSections.
insert(&Sec);
703 }
else if (RelocSec && !(Sec.Flags &
SHF_ALLOC))
710 RelocSections.push_back(RelocSec);
715 auto Iter = RenamedSections.
find(RelocSec->getSection());
716 if (Iter != RenamedSections.
end())
717 RelocSec->Name = (RelocSec->getNamePrefix() + (*Iter)->Name).str();
729 PrefixedSections.
insert(&Sec);
730 }
else if (
auto *RelocSec = dyn_cast<RelocationSectionBase>(&Sec)) {
738 const SectionBase *TargetSec = RelocSec->getSection();
744 if (PrefixedSections.
count(TargetSec))
745 Sec.
Name = (RelocSec->getNamePrefix() + TargetSec->
Name).str();
762 std::unique_ptr<Writer>
Writer =
781 return writeOutput(Config, **Obj, Out, OutputElfType);
799 return writeOutput(Config, **Obj, Out, OutputElfType);
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 uint64_t getNewShfFlags(SectionFlag AllFlags)
static void setSectionFlagsAndType(SectionBase &Sec, SectionFlag Flags)
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 Error makeStringError(std::error_code EC, const Twine &Msg, Ts &&...Args)
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 uint64_t getSectionFlagsPreserveMask(uint64_t OldFlags, uint64_t NewFlags)
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 std::unique_ptr< Writer > createELFWriter(const CommonConfig &Config, Object &Obj, raw_ostream &Out, ElfType OutputElfType)
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.
iterator find(StringRef Key)
StringRef - Represent a constant reference to a string, i.e.
std::string str() const
str - Get the contents as an std::string.
constexpr bool empty() const
empty - Check if the string is empty.
bool startswith(StringRef Prefix) const
bool endswith(StringRef Suffix) const
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
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.
bool matches(StringRef S) const
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...
SymInfo contains information about symbol: it's address and section index which is -1LL for absolute ...
NameMatcher SymbolsToGlobalize
StringMap< SectionRename > SectionsToRename
NameMatcher SymbolsToRemove
std::optional< MachineInfo > OutputArch
std::vector< StringRef > DumpSection
std::vector< NewSectionInfo > UpdateSection
bool ExtractMainPartition
bool DecompressDebugSections
NameMatcher SymbolsToKeep
StringRef AddGnuDebugLink
StringMap< uint64_t > SetSectionAlignment
NameMatcher SymbolsToWeaken
uint32_t GnuDebugLinkCRC32
NameMatcher SymbolsToKeepGlobal
std::vector< NewSymbolInfo > SymbolsToAdd
DebugCompressionType CompressionType
StringMap< SectionFlagsUpdate > SetSectionFlags
NameMatcher UnneededSymbolsToRemove
std::optional< StringRef > ExtractPartition
std::vector< NewSectionInfo > AddSection
StringRef AllocSectionsPrefix
StringMap< StringRef > SymbolsToRename
NameMatcher SymbolsToLocalize
StringMap< uint64_t > SetSectionType
uint8_t NewSymbolVisibility
std::function< uint64_t(uint64_t)> EntryExpr
std::shared_ptr< MemoryBuffer > SectionData
std::optional< SectionFlag > NewFlags
uint16_t getShndx() const