38#include <system_error>
84 "section flag SHF_X86_64_LARGE can only be used "
85 "with x86_64 architecture");
102 return (OldFlags & PreserveMask) | (NewFlags & ~PreserveMask);
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,
178 return std::make_unique<BinaryWriter>(Obj, Out, Config);
180 return std::make_unique<IHexWriter>(Obj, Out, Config.
OutputFilename);
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())
210 "section '%s' not found", SecName.
str().c_str());
220 std::optional<DebugCompressionType> CType;
222 if (Matcher.matches(Sec.Name))
235 if (Sec.ParentSegment)
238 "section '" + Sec.Name +
239 "' within a segment cannot be (de)compressed");
243 ToReplace.emplace_back(
246 ToReplace.emplace_back(&Sec, [=, S = &Sec] {
254 for (
auto [S, Func] : ToReplace)
264 if (!Name.consume_front(
"$x") && !Name.consume_front(
"$d"))
266 return Name.empty() || Name.starts_with(
".");
274 if (!Name.consume_front(
"$a") && !Name.consume_front(
"$d") &&
275 !Name.consume_front(
"$t"))
277 return Name.empty() || Name.starts_with(
".");
295 return !Sym.Referenced &&
313 if (!Sym.isCommon() && Sym.getShndx() !=
SHN_UNDEF &&
320 if (Matcher.matches(Sym.Name))
321 Sym.Visibility = Visibility;
352 Sym.Name = std::string(
I->getValue());
371 auto RemoveSymbolsPred = [&](
const Symbol &Sym) {
393 if (!Sym.Referenced) {
424 RemovePred = [RemovePred](
const SectionBase &Sec) {
429 RemovePred = [RemovePred, &Obj](
const SectionBase &Sec) {
434 RemovePred = [RemovePred, &Obj](
const SectionBase &Sec) {
452 RemovePred = [RemovePred](
const SectionBase &Sec) {
453 return RemovePred(Sec) || Sec.ParentSegment ==
nullptr;
458 RemovePred = [RemovePred](
const SectionBase &Sec) {
464 RemovePred = [RemovePred, &Obj](
const SectionBase &Sec) {
469 return (Sec.Flags &
SHF_ALLOC) == 0 && Sec.ParentSegment ==
nullptr;
473 RemovePred = [RemovePred, &Obj](
const SectionBase &Sec) {
488 if (Sec.ParentSegment !=
nullptr)
494 RemovePred = [RemovePred](
const SectionBase &Sec) {
499 return (Sec.Flags &
SHF_ALLOC) != 0 && !Sec.ParentSegment;
505 RemovePred = [&Config, RemovePred, &Obj](
const SectionBase &Sec) {
527 RemovePred = [&Config, RemovePred](
const SectionBase &Sec) {
532 return RemovePred(Sec);
543 RemovePred = [&Obj, RemovePred](
const SectionBase &Sec) {
546 return RemovePred(Sec);
567 uint8_t Visibility = DefaultVisibility;
614struct RemoveNoteDetail {
615 struct DeletedRange {
620 template <
class ELFT>
621 static std::vector<DeletedRange>
622 findNotesToRemove(ArrayRef<uint8_t>
Data,
size_t Align,
624 static std::vector<uint8_t> updateData(ArrayRef<uint8_t> OldData,
630std::vector<RemoveNoteDetail::DeletedRange>
631RemoveNoteDetail::findNotesToRemove(ArrayRef<uint8_t>
Data,
size_t Align,
633 using Elf_Nhdr =
typename ELFT::Nhdr;
634 using Elf_Note =
typename ELFT::Note;
637 while (CurPos +
sizeof(Elf_Nhdr) <=
Data.size()) {
638 auto Nhdr =
reinterpret_cast<const Elf_Nhdr *
>(
Data.data() + CurPos);
639 size_t FullSize = Nhdr->getSize(Align);
640 if (CurPos + FullSize >
Data.size())
642 Elf_Note
Note(*Nhdr);
649 ToRemove.push_back({CurPos, CurPos + FullSize});
656RemoveNoteDetail::updateData(ArrayRef<uint8_t> OldData,
658 std::vector<uint8_t> NewData;
659 NewData.reserve(OldData.
size());
661 for (
const DeletedRange &RemRange :
ToRemove) {
662 if (CurPos < RemRange.OldFrom) {
663 auto Slice = OldData.
slice(CurPos, RemRange.OldFrom - CurPos);
666 CurPos = RemRange.OldTo;
668 if (CurPos < OldData.
size()) {
669 auto Slice = OldData.
slice(CurPos);
690 if (Sec.Type !=
SHT_NOTE || !Sec.hasContents())
693 if (Sec.ParentSegment) {
697 "cannot remove note(s) from " + Sec.Name +
698 ": sections in segments are not supported")))
703 size_t Align = std::max<size_t>(4, Sec.Align);
707 ? RemoveNoteDetail::findNotesToRemove<ELF64LE>(
708 OldData,
Align, NotesToRemove)
709 : RemoveNoteDetail::findNotesToRemove<ELF64BE>(
710 OldData,
Align, NotesToRemove);
712 if (
Error E = Obj.updateSectionData(
713 Sec, RemoveNoteDetail::updateData(OldData,
ToRemove)))
741 if (
Data.size() < 12) {
744 << Name <<
" data must be either empty or at least 12 bytes long";
747 if (
Data.size() % 4 != 0) {
750 << Name <<
" data size must be a multiple of 4 bytes";
764 if (ActualDataSize != ExpectedDataSize) {
768 <<
" data size is incompatible with the content of "
769 "the name and description size fields:"
770 <<
" expecting " << ExpectedDataSize <<
", found " << ActualDataSize;
794 [&Obj](
const SectionBase &Sec) { return onlyKeepDWOPred(Obj, Sec); });
821 Sec.Align =
I->second;
827 if (Seg.MemSize > 0) {
829 Seg.PAddr > std::numeric_limits<uint64_t>::max() -
834 " cannot be increased by 0x" +
836 ". The result would overflow");
838 Seg.PAddr < std::numeric_limits<uint64_t>::min() -
843 " cannot be decreased by 0x" +
845 ". The result would underflow");
856 "cannot change section address in a non-relocatable file");
861 if (PatternUpdate.SectionPattern.matches(Sec.Name) &&
862 SectionsToUpdateAddress.
try_emplace(Sec.Name, PatternUpdate.Update)
865 Sec.Addr < PatternUpdate.Update.Value) {
869 " cannot be decreased by 0x" +
871 ". The result would underflow");
874 Sec.Addr > std::numeric_limits<uint64_t>::max() -
875 PatternUpdate.Update.Value) {
879 " cannot be increased by 0x" +
881 ". The result would overflow");
884 switch (PatternUpdate.Update.Kind) {
886 Sec.Addr = PatternUpdate.Update.Value;
889 Sec.Addr -= PatternUpdate.Update.Value;
892 Sec.Addr += PatternUpdate.Update.Value;
919 if (Name.starts_with(
".note") && Name !=
".note.GNU-stack") {
967 std::vector<RelocationSectionBase *> RelocSections;
974 Sec.Name = std::string(SR.
NewName);
979 RenamedSections.
insert(&Sec);
980 }
else if (RelocSec && !(Sec.Flags &
SHF_ALLOC))
987 RelocSections.push_back(RelocSec);
992 auto Iter = RenamedSections.
find(RelocSec->getSection());
993 if (Iter != RenamedSections.
end())
994 RelocSec->Name = (RelocSec->getNamePrefix() + (*Iter)->Name).str();
1006 PrefixedSections.
insert(&Sec);
1015 const SectionBase *TargetSec = RelocSec->getSection();
1021 if (PrefixedSections.
count(TargetSec))
1022 Sec.Name = (RelocSec->getNamePrefix() + TargetSec->
Name).str();
1039 std::unique_ptr<Writer>
Writer =
1058 return writeOutput(Config, **Obj, Out, OutputElfType);
1076 return writeOutput(Config, **Obj, Out, OutputElfType);
ReachingDefAnalysis InstSet & ToRemove
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file defines the DenseSet and SmallDenseSet classes.
static Error removeNotes(Object &Obj, endianness Endianness, ArrayRef< RemoveNoteInfo > NotesToRemove, function_ref< Error(Error)> ErrorCallback)
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, StringRef InputFilename, 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)
static cl::opt< std::string > InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"))
This file defines the SmallVector class.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
size_t size() const
size - Get the array size.
ArrayRef< T > slice(size_t N, size_t M) const
slice(n, m) - Chop off the first N elements of the array, and keep M elements in the array.
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 LLVM_ABI 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",...
iterator find(StringRef Key)
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.
constexpr bool empty() const
empty - Check if the string is empty.
constexpr size_t size() const
size - Get the string size.
bool ends_with(StringRef Suffix) const
Check if this string ends with the given Suffix.
static Twine utohexstr(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.
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)
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
LLVM_ABI 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,...
LLVM_ABI 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.
LLVM_ABI 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.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
auto reverse(ContainerTy &&C)
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...
FunctionAddr VTableAddr uintptr_t uintptr_t Data
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
ArrayRef(const T &OneElt) -> ArrayRef< T >
SymInfo contains information about symbol: it's address and section index which is -1LL for absolute ...
This struct is a compact representation of a valid (non-zero power of two) alignment.
SmallVector< SectionPatternAddressUpdate, 0 > ChangeSectionAddress
SmallVector< NewSectionInfo, 0 > UpdateSection
NameMatcher SymbolsToGlobalize
int64_t ChangeSectionLMAValAll
StringMap< SectionRename > SectionsToRename
NameMatcher SymbolsToRemove
std::optional< MachineInfo > OutputArch
SmallVector< std::pair< NameMatcher, llvm::DebugCompressionType >, 0 > compressSections
bool ExtractMainPartition
bool DecompressDebugSections
SmallVector< StringRef, 0 > DumpSection
SmallVector< NewSymbolInfo, 0 > SymbolsToAdd
NameMatcher SymbolsToKeep
StringRef AddGnuDebugLink
StringMap< uint64_t > SetSectionAlignment
NameMatcher SymbolsToWeaken
uint32_t GnuDebugLinkCRC32
NameMatcher SymbolsToKeepGlobal
DebugCompressionType CompressionType
std::function< Error(Error)> ErrorCallback
StringMap< SectionFlagsUpdate > SetSectionFlags
NameMatcher SymbolsToSkip
SmallVector< NewSectionInfo, 0 > AddSection
NameMatcher UnneededSymbolsToRemove
StringRef SymbolsPrefixRemove
std::optional< StringRef > ExtractPartition
StringRef AllocSectionsPrefix
StringMap< StringRef > SymbolsToRename
NameMatcher SymbolsToLocalize
StringMap< uint64_t > SetSectionType
std::vector< std::pair< NameMatcher, uint8_t > > SymbolsToSetVisibility
uint8_t NewSymbolVisibility
SmallVector< RemoveNoteInfo, 0 > NotesToRemove
std::function< uint64_t(uint64_t)> EntryExpr
std::shared_ptr< MemoryBuffer > SectionData
std::optional< SectionFlag > NewFlags