38#include <system_error>
69 if (AllFlags & SectionFlag::SecAlloc)
71 if (!(AllFlags & SectionFlag::SecReadonly))
73 if (AllFlags & SectionFlag::SecCode)
75 if (AllFlags & SectionFlag::SecMerge)
77 if (AllFlags & SectionFlag::SecStrings)
79 if (AllFlags & SectionFlag::SecExclude)
81 if (AllFlags & SectionFlag::SecLarge) {
84 "section flag SHF_X86_64_LARGE can only be used "
85 "with x86_64 architecture");
102 return (OldFlags & PreserveMask) | (NewFlags & ~PreserveMask);
125 Flags & (SectionFlag::SecContents | SectionFlag::SecLoad)))
156 switch (OutputElfType) {
158 return std::make_unique<ELFWriter<ELF32LE>>(Obj, Out, !
Config.StripSections,
161 return std::make_unique<ELFWriter<ELF64LE>>(Obj, Out, !
Config.StripSections,
164 return std::make_unique<ELFWriter<ELF32BE>>(Obj, Out, !
Config.StripSections,
167 return std::make_unique<ELFWriter<ELF64BE>>(Obj, Out, !
Config.StripSections,
176 switch (
Config.OutputFormat) {
177 case FileFormat::Binary:
178 return std::make_unique<BinaryWriter>(Obj, Out,
Config);
179 case FileFormat::IHex:
180 return std::make_unique<IHexWriter>(Obj, Out,
Config.OutputFilename);
181 case FileFormat::SREC:
182 return std::make_unique<SRECWriter>(Obj, Out,
Config.OutputFilename);
191 if (Sec.Name == SecName) {
194 "cannot dump section '%s': it has no contents",
195 SecName.
str().c_str());
200 std::unique_ptr<FileOutputBuffer> Buf = std::move(*BufferOrErr);
201 std::copy(Sec.OriginalData.begin(), Sec.OriginalData.end(),
202 Buf->getBufferStart());
203 if (
Error E = Buf->commit())
209 SecName.
str().c_str());
219 std::optional<DebugCompressionType> CType;
220 for (
auto &[Matcher,
T] :
Config.compressSections)
221 if (Matcher.matches(Sec.Name))
227 CType =
Config.CompressionType;
228 else if (
Config.DecompressDebugSections)
234 if (Sec.ParentSegment)
237 "section '" + Sec.Name +
238 "' within a segment cannot be (de)compressed");
240 if (
auto *CS = dyn_cast<CompressedSection>(&Sec)) {
242 ToReplace.emplace_back(
243 &Sec, [=] {
return &addSection<DecompressedSection>(*CS); });
245 ToReplace.emplace_back(&Sec, [=, S = &Sec] {
246 return &addSection<CompressedSection>(
253 for (
auto [S, Func] : ToReplace)
263 if (!
Name.consume_front(
"$x") && !
Name.consume_front(
"$d"))
265 return Name.empty() ||
Name.starts_with(
".");
273 if (!
Name.consume_front(
"$a") && !
Name.consume_front(
"$d") &&
274 !
Name.consume_front(
"$t"))
276 return Name.empty() ||
Name.starts_with(
".");
332 if (!
Config.SymbolsToKeepGlobal.empty() &&
351 Sym.
Name = std::string(
I->getValue());
355 Config.SymbolsPrefixRemove) == 0)
365 if (
Config.StripUnneeded || !
Config.UnneededSymbolsToRemove.empty() ||
366 !
Config.OnlySection.empty()) {
371 auto RemoveSymbolsPred = [&](
const Symbol &
Sym) {
395 if ((
Config.StripUnneeded ||
416 if (!
Config.ToRemove.empty()) {
418 return Config.ToRemove.matches(Sec.Name);
423 RemovePred = [RemovePred](
const SectionBase &Sec) {
428 RemovePred = [RemovePred, &Obj](
const SectionBase &Sec) {
433 RemovePred = [RemovePred, &Obj](
const SectionBase &Sec) {
450 if (
Config.StripSections) {
451 RemovePred = [RemovePred](
const SectionBase &Sec) {
452 return RemovePred(Sec) || Sec.ParentSegment ==
nullptr;
457 RemovePred = [RemovePred](
const SectionBase &Sec) {
463 RemovePred = [RemovePred, &Obj](
const SectionBase &Sec) {
468 return (Sec.Flags &
SHF_ALLOC) == 0 && Sec.ParentSegment ==
nullptr;
472 RemovePred = [RemovePred, &Obj](
const SectionBase &Sec) {
487 if (Sec.ParentSegment !=
nullptr)
492 if (
Config.ExtractPartition ||
Config.ExtractMainPartition) {
493 RemovePred = [RemovePred](
const SectionBase &Sec) {
498 return (Sec.Flags &
SHF_ALLOC) != 0 && !Sec.ParentSegment;
503 if (!
Config.OnlySection.empty()) {
506 if (
Config.OnlySection.matches(Sec.Name))
525 if (!
Config.KeepSection.empty()) {
528 if (
Config.KeepSection.matches(Sec.Name))
531 return RemovePred(Sec);
542 RemovePred = [&Obj, RemovePred](
const SectionBase &Sec) {
545 return RemovePred(Sec);
566 uint8_t Visibility = DefaultVisibility;
633 if (
Data.size() < 12) {
636 <<
Name <<
" data must be either empty or at least 12 bytes long";
639 if (
Data.size() % 4 != 0) {
642 <<
Name <<
" data size must be a multiple of 4 bytes";
656 if (ActualDataSize != ExpectedDataSize) {
660 <<
" data size is incompatible with the content of "
661 "the name and description size fields:"
662 <<
" expecting " << ExpectedDataSize <<
", found " << ActualDataSize;
686 [&Obj](
const SectionBase &Sec) { return onlyKeepDWOPred(Obj, Sec); });
708 if (!
Config.SetSectionAlignment.empty()) {
710 auto I =
Config.SetSectionAlignment.find(Sec.Name);
711 if (
I !=
Config.SetSectionAlignment.end())
712 Sec.Align =
I->second;
716 if (
Config.ChangeSectionLMAValAll != 0) {
718 if (Seg.FileSize > 0) {
719 if (
Config.ChangeSectionLMAValAll > 0 &&
720 Seg.PAddr > std::numeric_limits<uint64_t>::max() -
721 Config.ChangeSectionLMAValAll) {
725 " cannot be increased by 0x" +
727 ". The result would overflow");
728 }
else if (
Config.ChangeSectionLMAValAll < 0 &&
729 Seg.PAddr < std::numeric_limits<uint64_t>::min() -
730 Config.ChangeSectionLMAValAll) {
734 " cannot be decreased by 0x" +
736 ". The result would underflow");
738 Seg.PAddr +=
Config.ChangeSectionLMAValAll;
743 if (!
Config.ChangeSectionAddress.empty()) {
746 object_error::invalid_file_type,
747 "cannot change section address in a non-relocatable file");
752 Config.ChangeSectionAddress.rend())) {
754 if (PatternUpdate.SectionPattern.matches(Sec.Name) &&
755 SectionsToUpdateAddress.
try_emplace(Sec.Name, PatternUpdate.Update)
758 Sec.Addr < PatternUpdate.Update.Value) {
762 " cannot be decreased by 0x" +
764 ". The result would underflow");
767 Sec.Addr > std::numeric_limits<uint64_t>::max() -
768 PatternUpdate.Update.Value) {
772 " cannot be increased by 0x" +
774 ". The result would overflow");
777 switch (PatternUpdate.Update.Kind) {
779 Sec.Addr = PatternUpdate.Update.Value;
782 Sec.Addr -= PatternUpdate.Update.Value;
785 Sec.Addr += PatternUpdate.Update.Value;
806 if (
Name.starts_with(
".note") &&
Name !=
".note.GNU-stack") {
825 if (!
Config.AddGnuDebugLink.empty())
827 Config.GnuDebugLinkCRC32);
839 if (!
Config.SetSectionFlags.empty() || !
Config.SetSectionType.empty()) {
841 const auto Iter =
Config.SetSectionFlags.find(Sec.Name);
842 if (Iter !=
Config.SetSectionFlags.end()) {
847 auto It2 =
Config.SetSectionType.find(Sec.Name);
848 if (It2 !=
Config.SetSectionType.end())
853 if (!
Config.SectionsToRename.empty()) {
854 std::vector<RelocationSectionBase *> RelocSections;
857 auto *RelocSec = dyn_cast<RelocationSectionBase>(&Sec);
858 const auto Iter =
Config.SectionsToRename.find(Sec.Name);
859 if (Iter !=
Config.SectionsToRename.end()) {
861 Sec.Name = std::string(SR.
NewName);
866 RenamedSections.
insert(&Sec);
867 }
else if (RelocSec && !(Sec.Flags &
SHF_ALLOC))
874 RelocSections.push_back(RelocSec);
879 auto Iter = RenamedSections.
find(RelocSec->getSection());
880 if (Iter != RenamedSections.
end())
881 RelocSec->Name = (RelocSec->getNamePrefix() + (*Iter)->Name).str();
888 if (!
Config.AllocSectionsPrefix.empty()) {
892 Sec.Name = (
Config.AllocSectionsPrefix + Sec.Name).str();
893 PrefixedSections.
insert(&Sec);
894 }
else if (
auto *RelocSec = dyn_cast<RelocationSectionBase>(&Sec)) {
902 const SectionBase *TargetSec = RelocSec->getSection();
908 if (PrefixedSections.
count(TargetSec))
909 Sec.
Name = (RelocSec->getNamePrefix() + TargetSec->
Name).str();
911 Sec.Name = (RelocSec->getNamePrefix() +
Config.AllocSectionsPrefix +
926 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 handleArgs(const CommonConfig &Config, const ELFConfig &ELFConfig, ElfType OutputElfType, Object &Obj)
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 Error verifyNoteSection(StringRef Name, endianness Endianness, ArrayRef< uint8_t > Data)
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 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)
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.
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
std::pair< iterator, bool > try_emplace(StringRef Key, ArgsTy &&...Args)
Emplace a new element for the specified key into the map if the key isn't already in the map.
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.
static Twine utohexstr(const uint64_t &Val)
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)
ConstRange< Segment > segments() const
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.
A raw_ostream that writes to an std::string.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr size_t NameSize
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...
uint32_t read32(const void *P, endianness E)
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.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
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