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,
Config);
184 case FileFormat::IHex:
185 return std::make_unique<IHexWriter>(Obj, Out,
Config.OutputFilename);
186 case FileFormat::SREC:
187 return std::make_unique<SRECWriter>(Obj, Out,
Config.OutputFilename);
196 if (Sec.Name == SecName) {
199 "cannot dump section '%s': it has no contents",
200 SecName.
str().c_str());
205 std::unique_ptr<FileOutputBuffer> Buf = std::move(*BufferOrErr);
206 std::copy(Sec.OriginalData.begin(), Sec.OriginalData.end(),
207 Buf->getBufferStart());
208 if (
Error E = Buf->commit())
214 SecName.
str().c_str());
226 if (
auto *CS = dyn_cast<CompressedSection>(&Sec)) {
227 if (
Config.DecompressDebugSections) {
228 ToReplace.emplace_back(
229 &Sec, [=] {
return &addSection<DecompressedSection>(*CS); });
232 ToReplace.emplace_back(&Sec, [&, S = &Sec] {
233 return &addSection<CompressedSection>(
240 for (
auto [S, Func] : ToReplace)
250 if (!
Name.consume_front(
"$x") && !
Name.consume_front(
"$d"))
252 return Name.empty() ||
Name.starts_with(
".");
260 if (!
Name.consume_front(
"$a") && !
Name.consume_front(
"$d") &&
261 !
Name.consume_front(
"$t"))
263 return Name.empty() ||
Name.starts_with(
".");
319 if (!
Config.SymbolsToKeepGlobal.empty() &&
338 Sym.
Name = std::string(
I->getValue());
342 Config.SymbolsPrefixRemove) == 0)
352 if (
Config.StripUnneeded || !
Config.UnneededSymbolsToRemove.empty() ||
353 !
Config.OnlySection.empty()) {
358 auto RemoveSymbolsPred = [&](
const Symbol &
Sym) {
382 if ((
Config.StripUnneeded ||
403 if (!
Config.ToRemove.empty()) {
405 return Config.ToRemove.matches(Sec.Name);
410 RemovePred = [RemovePred](
const SectionBase &Sec) {
415 RemovePred = [RemovePred, &Obj](
const SectionBase &Sec) {
420 RemovePred = [RemovePred, &Obj](
const SectionBase &Sec) {
437 if (
Config.StripSections) {
438 RemovePred = [RemovePred](
const SectionBase &Sec) {
439 return RemovePred(Sec) || Sec.ParentSegment ==
nullptr;
444 RemovePred = [RemovePred](
const SectionBase &Sec) {
450 RemovePred = [RemovePred, &Obj](
const SectionBase &Sec) {
455 return (Sec.Flags &
SHF_ALLOC) == 0 && Sec.ParentSegment ==
nullptr;
459 RemovePred = [RemovePred, &Obj](
const SectionBase &Sec) {
474 if (Sec.ParentSegment !=
nullptr)
479 if (
Config.ExtractPartition ||
Config.ExtractMainPartition) {
480 RemovePred = [RemovePred](
const SectionBase &Sec) {
485 return (Sec.Flags &
SHF_ALLOC) != 0 && !Sec.ParentSegment;
490 if (!
Config.OnlySection.empty()) {
493 if (
Config.OnlySection.matches(Sec.Name))
512 if (!
Config.KeepSection.empty()) {
515 if (
Config.KeepSection.matches(Sec.Name))
518 return RemovePred(Sec);
529 RemovePred = [&Obj, RemovePred](
const SectionBase &Sec) {
532 return RemovePred(Sec);
547 uint8_t DefaultVisibility) {
553 uint8_t Visibility = DefaultVisibility;
625 [&Obj](
const SectionBase &Sec) { return onlyKeepDWOPred(Obj, Sec); });
647 if (!
Config.SetSectionAlignment.empty()) {
649 auto I =
Config.SetSectionAlignment.find(Sec.Name);
650 if (
I !=
Config.SetSectionAlignment.end())
651 Sec.Align =
I->second;
664 if (
Name.starts_with(
".note") &&
Name !=
".note.GNU-stack")
680 if (!
Config.AddGnuDebugLink.empty())
682 Config.GnuDebugLinkCRC32);
694 if (!
Config.SetSectionFlags.empty() || !
Config.SetSectionType.empty()) {
696 const auto Iter =
Config.SetSectionFlags.find(Sec.Name);
697 if (Iter !=
Config.SetSectionFlags.end()) {
702 auto It2 =
Config.SetSectionType.find(Sec.Name);
703 if (It2 !=
Config.SetSectionType.end())
708 if (!
Config.SectionsToRename.empty()) {
709 std::vector<RelocationSectionBase *> RelocSections;
712 auto *RelocSec = dyn_cast<RelocationSectionBase>(&Sec);
713 const auto Iter =
Config.SectionsToRename.find(Sec.Name);
714 if (Iter !=
Config.SectionsToRename.end()) {
716 Sec.Name = std::string(SR.
NewName);
721 RenamedSections.
insert(&Sec);
722 }
else if (RelocSec && !(Sec.Flags &
SHF_ALLOC))
729 RelocSections.push_back(RelocSec);
734 auto Iter = RenamedSections.
find(RelocSec->getSection());
735 if (Iter != RenamedSections.
end())
736 RelocSec->Name = (RelocSec->getNamePrefix() + (*Iter)->Name).str();
743 if (!
Config.AllocSectionsPrefix.empty()) {
747 Sec.Name = (
Config.AllocSectionsPrefix + Sec.Name).str();
748 PrefixedSections.
insert(&Sec);
749 }
else if (
auto *RelocSec = dyn_cast<RelocationSectionBase>(&Sec)) {
757 const SectionBase *TargetSec = RelocSec->getSection();
763 if (PrefixedSections.
count(TargetSec))
764 Sec.
Name = (RelocSec->getNamePrefix() + TargetSec->
Name).str();
766 Sec.Name = (RelocSec->getNamePrefix() +
Config.AllocSectionsPrefix +
781 std::unique_ptr<Writer>
Writer =
for(const MachineOperand &MO :llvm::drop_begin(OldMI.operands(), Desc.getNumOperands()))
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 isRequiredByABISymbol(const Object &Obj, const Symbol &Sym)
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...
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 starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
bool ends_with(StringRef Suffix) const
Check if this string ends with the given Suffix.
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
Error compressOrDecompressSections(const CommonConfig &Config)
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 ...
std::vector< std::pair< NameMatcher, uint8_t > > SymbolsToSetVisibility
uint8_t NewSymbolVisibility
std::function< uint64_t(uint64_t)> EntryExpr
std::shared_ptr< MemoryBuffer > SectionData
std::optional< SectionFlag > NewFlags
uint16_t getShndx() const