2020-04-23 01:05:07 +02:00
|
|
|
//===- Object.cpp - Mach-O object file model --------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-06-21 02:21:50 +02:00
|
|
|
#include "Object.h"
|
2020-04-22 23:26:28 +02:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
|
|
|
#include <unordered_set>
|
2019-06-21 02:21:50 +02:00
|
|
|
|
2021-06-23 09:31:52 +02:00
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::objcopy::macho;
|
2019-06-21 02:21:50 +02:00
|
|
|
|
|
|
|
const SymbolEntry *SymbolTable::getSymbolByIndex(uint32_t Index) const {
|
|
|
|
assert(Index < Symbols.size() && "invalid symbol index");
|
|
|
|
return Symbols[Index].get();
|
|
|
|
}
|
|
|
|
|
2019-10-31 05:51:11 +01:00
|
|
|
SymbolEntry *SymbolTable::getSymbolByIndex(uint32_t Index) {
|
|
|
|
return const_cast<SymbolEntry *>(
|
|
|
|
static_cast<const SymbolTable *>(this)->getSymbolByIndex(Index));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SymbolTable::removeSymbols(
|
|
|
|
function_ref<bool(const std::unique_ptr<SymbolEntry> &)> ToRemove) {
|
2020-12-25 19:13:18 +01:00
|
|
|
llvm::erase_if(Symbols, ToRemove);
|
2019-10-31 05:51:11 +01:00
|
|
|
}
|
|
|
|
|
2020-06-23 01:49:14 +02:00
|
|
|
void Object::updateLoadCommandIndexes() {
|
|
|
|
// Update indices of special load commands
|
|
|
|
for (size_t Index = 0, Size = LoadCommands.size(); Index < Size; ++Index) {
|
|
|
|
LoadCommand &LC = LoadCommands[Index];
|
|
|
|
switch (LC.MachOLoadCommand.load_command_data.cmd) {
|
|
|
|
case MachO::LC_SYMTAB:
|
|
|
|
SymTabCommandIndex = Index;
|
|
|
|
break;
|
|
|
|
case MachO::LC_DYSYMTAB:
|
|
|
|
DySymTabCommandIndex = Index;
|
|
|
|
break;
|
|
|
|
case MachO::LC_DYLD_INFO:
|
|
|
|
case MachO::LC_DYLD_INFO_ONLY:
|
|
|
|
DyLdInfoCommandIndex = Index;
|
|
|
|
break;
|
|
|
|
case MachO::LC_DATA_IN_CODE:
|
|
|
|
DataInCodeCommandIndex = Index;
|
|
|
|
break;
|
2021-06-30 03:47:55 +02:00
|
|
|
case MachO::LC_LINKER_OPTIMIZATION_HINT:
|
|
|
|
LinkerOptimizationHintCommandIndex = Index;
|
|
|
|
break;
|
2020-06-23 01:49:14 +02:00
|
|
|
case MachO::LC_FUNCTION_STARTS:
|
|
|
|
FunctionStartsCommandIndex = Index;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Error Object::removeLoadCommands(
|
|
|
|
function_ref<bool(const LoadCommand &)> ToRemove) {
|
|
|
|
auto It = std::stable_partition(
|
|
|
|
LoadCommands.begin(), LoadCommands.end(),
|
|
|
|
[&](const LoadCommand &LC) { return !ToRemove(LC); });
|
|
|
|
LoadCommands.erase(It, LoadCommands.end());
|
|
|
|
|
|
|
|
updateLoadCommandIndexes();
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
2020-04-22 23:26:28 +02:00
|
|
|
Error Object::removeSections(
|
|
|
|
function_ref<bool(const std::unique_ptr<Section> &)> ToRemove) {
|
2020-04-23 00:37:17 +02:00
|
|
|
DenseMap<uint32_t, const Section *> OldIndexToSection;
|
|
|
|
uint32_t NextSectionIndex = 1;
|
2020-04-22 23:26:28 +02:00
|
|
|
for (LoadCommand &LC : LoadCommands) {
|
|
|
|
auto It = std::stable_partition(
|
|
|
|
std::begin(LC.Sections), std::end(LC.Sections),
|
|
|
|
[&](const std::unique_ptr<Section> &Sec) { return !ToRemove(Sec); });
|
2020-04-23 00:37:17 +02:00
|
|
|
for (auto I = LC.Sections.begin(), End = It; I != End; ++I) {
|
|
|
|
OldIndexToSection[(*I)->Index] = I->get();
|
|
|
|
(*I)->Index = NextSectionIndex++;
|
|
|
|
}
|
2020-04-22 23:26:28 +02:00
|
|
|
LC.Sections.erase(It, LC.Sections.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
auto IsDead = [&](const std::unique_ptr<SymbolEntry> &S) -> bool {
|
|
|
|
Optional<uint32_t> Section = S->section();
|
2020-04-23 00:37:17 +02:00
|
|
|
return (Section && !OldIndexToSection.count(*Section));
|
2020-04-22 23:26:28 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
SmallPtrSet<const SymbolEntry *, 2> DeadSymbols;
|
|
|
|
for (const std::unique_ptr<SymbolEntry> &Sym : SymTable.Symbols)
|
|
|
|
if (IsDead(Sym))
|
|
|
|
DeadSymbols.insert(Sym.get());
|
|
|
|
|
|
|
|
for (const LoadCommand &LC : LoadCommands)
|
|
|
|
for (const std::unique_ptr<Section> &Sec : LC.Sections)
|
|
|
|
for (const RelocationInfo &R : Sec->Relocations)
|
2020-04-27 07:39:50 +02:00
|
|
|
if (R.Symbol && *R.Symbol && DeadSymbols.count(*R.Symbol))
|
2020-04-22 23:26:28 +02:00
|
|
|
return createStringError(std::errc::invalid_argument,
|
|
|
|
"symbol '%s' defined in section with index "
|
|
|
|
"'%u' cannot be removed because it is "
|
|
|
|
"referenced by a relocation in section '%s'",
|
2020-04-27 07:39:50 +02:00
|
|
|
(*R.Symbol)->Name.c_str(),
|
|
|
|
*((*R.Symbol)->section()),
|
2020-04-22 23:26:28 +02:00
|
|
|
Sec->CanonicalName.c_str());
|
|
|
|
SymTable.removeSymbols(IsDead);
|
2020-04-23 00:37:17 +02:00
|
|
|
for (std::unique_ptr<SymbolEntry> &S : SymTable.Symbols)
|
|
|
|
if (S->section())
|
|
|
|
S->n_sect = OldIndexToSection[S->n_sect]->Index;
|
2020-04-22 23:26:28 +02:00
|
|
|
return Error::success();
|
2019-10-28 07:40:37 +01:00
|
|
|
}
|
|
|
|
|
2020-09-24 10:48:21 +02:00
|
|
|
uint64_t Object::nextAvailableSegmentAddress() const {
|
|
|
|
uint64_t HeaderSize =
|
|
|
|
is64Bit() ? sizeof(MachO::mach_header_64) : sizeof(MachO::mach_header);
|
|
|
|
uint64_t Addr = HeaderSize + Header.SizeOfCmds;
|
|
|
|
for (const LoadCommand &LC : LoadCommands) {
|
|
|
|
const MachO::macho_load_command &MLC = LC.MachOLoadCommand;
|
|
|
|
switch (MLC.load_command_data.cmd) {
|
|
|
|
case MachO::LC_SEGMENT:
|
|
|
|
Addr = std::max(Addr,
|
|
|
|
static_cast<uint64_t>(MLC.segment_command_data.vmaddr) +
|
|
|
|
MLC.segment_command_data.vmsize);
|
|
|
|
break;
|
|
|
|
case MachO::LC_SEGMENT_64:
|
|
|
|
Addr = std::max(Addr, MLC.segment_command_64_data.vmaddr +
|
|
|
|
MLC.segment_command_64_data.vmsize);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Addr;
|
|
|
|
}
|
|
|
|
|
2019-12-16 06:05:06 +01:00
|
|
|
template <typename SegmentType>
|
2020-11-08 03:12:51 +01:00
|
|
|
static void
|
|
|
|
constructSegment(SegmentType &Seg, llvm::MachO::LoadCommandType CmdType,
|
|
|
|
StringRef SegName, uint64_t SegVMAddr, uint64_t SegVMSize) {
|
2019-12-16 06:05:06 +01:00
|
|
|
assert(SegName.size() <= sizeof(Seg.segname) && "too long segment name");
|
|
|
|
memset(&Seg, 0, sizeof(SegmentType));
|
|
|
|
Seg.cmd = CmdType;
|
|
|
|
strncpy(Seg.segname, SegName.data(), SegName.size());
|
2020-09-24 10:48:21 +02:00
|
|
|
Seg.maxprot |=
|
|
|
|
(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE | MachO::VM_PROT_EXECUTE);
|
|
|
|
Seg.initprot |=
|
|
|
|
(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE | MachO::VM_PROT_EXECUTE);
|
|
|
|
Seg.vmaddr = SegVMAddr;
|
2020-11-08 03:12:51 +01:00
|
|
|
Seg.vmsize = SegVMSize;
|
2019-12-16 06:05:06 +01:00
|
|
|
}
|
|
|
|
|
2020-11-08 03:12:51 +01:00
|
|
|
LoadCommand &Object::addSegment(StringRef SegName, uint64_t SegVMSize) {
|
2019-12-16 06:05:06 +01:00
|
|
|
LoadCommand LC;
|
2020-09-24 10:48:21 +02:00
|
|
|
const uint64_t SegVMAddr = nextAvailableSegmentAddress();
|
2019-12-16 06:05:06 +01:00
|
|
|
if (is64Bit())
|
|
|
|
constructSegment(LC.MachOLoadCommand.segment_command_64_data,
|
2020-11-08 03:12:51 +01:00
|
|
|
MachO::LC_SEGMENT_64, SegName, SegVMAddr, SegVMSize);
|
2019-12-16 06:05:06 +01:00
|
|
|
else
|
|
|
|
constructSegment(LC.MachOLoadCommand.segment_command_data,
|
2020-11-08 03:12:51 +01:00
|
|
|
MachO::LC_SEGMENT, SegName, SegVMAddr, SegVMSize);
|
2019-12-16 06:05:06 +01:00
|
|
|
|
2020-02-21 22:18:36 +01:00
|
|
|
LoadCommands.push_back(std::move(LC));
|
2019-12-16 06:05:06 +01:00
|
|
|
return LoadCommands.back();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Extracts a segment name from a string which is possibly non-null-terminated.
|
|
|
|
static StringRef extractSegmentName(const char *SegName) {
|
|
|
|
return StringRef(SegName,
|
|
|
|
strnlen(SegName, sizeof(MachO::segment_command::segname)));
|
|
|
|
}
|
|
|
|
|
|
|
|
Optional<StringRef> LoadCommand::getSegmentName() const {
|
|
|
|
const MachO::macho_load_command &MLC = MachOLoadCommand;
|
|
|
|
switch (MLC.load_command_data.cmd) {
|
|
|
|
case MachO::LC_SEGMENT:
|
|
|
|
return extractSegmentName(MLC.segment_command_data.segname);
|
|
|
|
case MachO::LC_SEGMENT_64:
|
|
|
|
return extractSegmentName(MLC.segment_command_64_data.segname);
|
|
|
|
default:
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-24 10:48:21 +02:00
|
|
|
Optional<uint64_t> LoadCommand::getSegmentVMAddr() const {
|
|
|
|
const MachO::macho_load_command &MLC = MachOLoadCommand;
|
|
|
|
switch (MLC.load_command_data.cmd) {
|
|
|
|
case MachO::LC_SEGMENT:
|
|
|
|
return MLC.segment_command_data.vmaddr;
|
|
|
|
case MachO::LC_SEGMENT_64:
|
|
|
|
return MLC.segment_command_64_data.vmaddr;
|
|
|
|
default:
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
}
|