Go to the documentation of this file.
11 #include <unordered_set>
32 static constexpr
char TextSegmentName[] =
"__TEXT";
37 case MachO::LC_CODE_SIGNATURE:
40 case MachO::LC_SEGMENT:
45 case MachO::LC_SEGMENT_64:
50 case MachO::LC_SYMTAB:
53 case MachO::LC_DYSYMTAB:
56 case MachO::LC_DYLD_INFO:
57 case MachO::LC_DYLD_INFO_ONLY:
60 case MachO::LC_DATA_IN_CODE:
63 case MachO::LC_LINKER_OPTIMIZATION_HINT:
66 case MachO::LC_FUNCTION_STARTS:
69 case MachO::LC_DYLD_CHAINED_FIXUPS:
72 case MachO::LC_DYLD_EXPORTS_TRIE:
81 auto It = std::stable_partition(
83 [&](
const LoadCommand &LC) { return !ToRemove(LC); });
95 auto It = std::stable_partition(
97 [&](
const std::unique_ptr<Section> &Sec) { return !ToRemove(Sec); });
98 for (
auto I = LC.Sections.begin(), End = It;
I != End; ++
I) {
99 OldIndexToSection[(*I)->Index] =
I->get();
100 (*I)->Index = NextSectionIndex++;
102 LC.Sections.erase(It, LC.Sections.end());
105 auto IsDead = [&](
const std::unique_ptr<SymbolEntry> &
S) ->
bool {
113 DeadSymbols.
insert(Sym.get());
116 for (
const std::unique_ptr<Section> &Sec : LC.Sections)
118 if (R.Symbol && *R.Symbol && DeadSymbols.
count(*R.Symbol))
120 "symbol '%s' defined in section with index "
121 "'%u' cannot be removed because it is "
122 "referenced by a relocation in section '%s'",
123 (*R.Symbol)->Name.c_str(),
124 *((*R.Symbol)->section()),
125 Sec->CanonicalName.c_str());
129 S->n_sect = OldIndexToSection[
S->n_sect]->Index;
139 switch (MLC.load_command_data.cmd) {
140 case MachO::LC_SEGMENT:
142 static_cast<uint64_t>(MLC.segment_command_data.vmaddr) +
143 MLC.segment_command_data.vmsize);
145 case MachO::LC_SEGMENT_64:
147 MLC.segment_command_64_data.vmsize);
156 template <
typename SegmentType>
160 assert(SegName.
size() <=
sizeof(Seg.segname) &&
"too long segment name");
161 memset(&Seg, 0,
sizeof(SegmentType));
163 strncpy(Seg.segname, SegName.
data(), SegName.
size());
168 Seg.vmaddr = SegVMAddr;
169 Seg.vmsize = SegVMSize;
177 MachO::LC_SEGMENT_64, SegName, SegVMAddr, SegVMSize);
180 MachO::LC_SEGMENT, SegName, SegVMAddr, SegVMSize);
194 switch (MLC.load_command_data.cmd) {
195 case MachO::LC_SEGMENT:
197 case MachO::LC_SEGMENT_64:
206 switch (MLC.load_command_data.cmd) {
207 case MachO::LC_SEGMENT:
208 return MLC.segment_command_data.vmaddr;
209 case MachO::LC_SEGMENT_64:
210 return MLC.segment_command_64_data.vmaddr;
Optional< size_t > SymTabCommandIndex
The index of LC_SYMTAB load command if present.
This is an optimization pass for GlobalISel generic memory operations.
Optional< size_t > LinkerOptimizationHintCommandIndex
The index of LC_LINKER_OPTIMIZATIN_HINT load command if present.
static void constructSegment(SegmentType &Seg, llvm::MachO::LoadCommandType CmdType, StringRef SegName, uint64_t SegVMAddr, uint64_t SegVMSize)
The location of the symbol table inside the binary is described by LC_SYMTAB load command.
ReachingDefAnalysis InstSet & ToRemove
static ErrorSuccess success()
Create a success value.
void erase_if(Container &C, UnaryPredicate P)
Provide a container algorithm similar to C++ Library Fundamentals v2's erase_if which is equivalent t...
void removeSymbols(function_ref< bool(const std::unique_ptr< SymbolEntry > &)> ToRemove)
const_iterator end(StringRef path)
Get end iterator over path.
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
Optional< size_t > DataInCodeCommandIndex
The index LC_DATA_IN_CODE load command if present.
Optional< size_t > FunctionStartsCommandIndex
The index LC_FUNCTION_STARTS load command if present.
Optional< size_t > ChainedFixupsCommandIndex
The index LC_DYLD_CHAINED_FIXUPS load command if present.
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Expected< ExpressionValue > max(const ExpressionValue &Lhs, const ExpressionValue &Rhs)
Optional< StringRef > getSegmentName() const
uint64_t nextAvailableSegmentAddress() const
Error removeLoadCommands(function_ref< bool(const LoadCommand &)> ToRemove)
void updateLoadCommandIndexes()
Optional< uint64_t > getSegmentVMAddr() const
An efficient, type-erasing, non-owning reference to a callable.
Error removeSections(function_ref< bool(const std::unique_ptr< Section > &)> ToRemove)
std::vector< LoadCommand > LoadCommands
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
MachO::macho_load_command MachOLoadCommand
LoadCommand & addSegment(StringRef SegName, uint64_t SegVMSize)
Creates a new segment load command in the object and returns a reference to the newly created load co...
Optional< size_t > DyLdInfoCommandIndex
The index of LC_DYLD_INFO or LC_DYLD_INFO_ONLY load command if present.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
const SymbolEntry * getSymbolByIndex(uint32_t Index) const
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
StringRef - Represent a constant reference to a string, i.e.
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Optional< size_t > DySymTabCommandIndex
The index LC_DYSYMTAB load command if present.
Optional< size_t > TextSegmentCommandIndex
The index of the LC_SEGMENT or LC_SEGMENT_64 load command corresponding to the __TEXT segment.
static StringRef extractSegmentName(const char *SegName)
Extracts a segment name from a string which is possibly non-null-terminated.
constexpr LLVM_NODISCARD size_t size() const
size - Get the string size.
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Lightweight error class with error context and mandatory checking.
std::vector< std::unique_ptr< SymbolEntry > > Symbols
Optional< size_t > ExportsTrieCommandIndex
The index LC_DYLD_EXPORTS_TRIE load command if present.
const LLVM_NODISCARD char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Optional< size_t > CodeSignatureCommandIndex
The index of LC_CODE_SIGNATURE load command if present.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.