mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
refactor(patcher): folders structure for elfpatcher + byte writing di
This commit is contained in:
@@ -1,6 +1,11 @@
|
||||
add_executable(relinker
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/main.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/elfpatcher/src/ElfPatcher.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/elfpatcher/src/filtering/SegmentFilter.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/elfpatcher/src/stub/EntryStubBuilder.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/elfpatcher/src/layout/ProgramHeaderLayoutBuilder.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/elfpatcher/src/sections/SectionHeaderTableBuilder.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/io/src/ByteWriter.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/io/src/FileReader.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/io/src/FileWriter.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/relinker/src/parsing/ElfReader.cpp
|
||||
|
||||
@@ -2,11 +2,23 @@
|
||||
#define ELFPATCHER_ELFPATCHER_HPP
|
||||
|
||||
#include <elfpatcher/IElfPatcher.hpp>
|
||||
#include <elfpatcher/domain/IEntryStubBuilder.hpp>
|
||||
#include <elfpatcher/domain/IProgramHeaderLayoutBuilder.hpp>
|
||||
#include <elfpatcher/domain/ISectionHeaderTableBuilder.hpp>
|
||||
#include <io/IByteWriter.hpp>
|
||||
#include <memory>
|
||||
|
||||
namespace Elfpatcher {
|
||||
|
||||
class ElfPatcher : public IElfPatcher {
|
||||
public:
|
||||
ElfPatcher(
|
||||
std::shared_ptr<IEntryStubBuilder> entryStubBuilder,
|
||||
std::shared_ptr<IProgramHeaderLayoutBuilder> programHeaderLayoutBuilder,
|
||||
std::shared_ptr<ISectionHeaderTableBuilder> sectionHeaderTableBuilder,
|
||||
std::shared_ptr<Io::IByteWriter> byteWriter
|
||||
);
|
||||
|
||||
std::vector<std::uint8_t> Patch(
|
||||
const std::vector<std::uint8_t>& sourceElf,
|
||||
const std::vector<Domain::ProgramHeader>& originalHeaders,
|
||||
@@ -14,70 +26,12 @@ public:
|
||||
) override;
|
||||
|
||||
private:
|
||||
static constexpr std::uint32_t PT_LOAD = 1;
|
||||
static constexpr std::uint32_t PT_DYNAMIC = 2;
|
||||
static constexpr std::uint32_t PT_INTERP = 3;
|
||||
static constexpr std::uint32_t PT_NOTE = 4;
|
||||
static constexpr std::uint32_t PT_PHDR = 6;
|
||||
static constexpr std::uint32_t PT_GNU_RELRO = 0x6474e552;
|
||||
static constexpr std::uint32_t PF_X = 0x1;
|
||||
static constexpr std::uint32_t PF_W = 0x2;
|
||||
static constexpr std::uint32_t PF_R = 0x4;
|
||||
static constexpr std::uint32_t ET_DYN = 3;
|
||||
static constexpr std::uint32_t PT_SCE_DYNLIBDATA = 0x61000000;
|
||||
static constexpr std::uint32_t PT_OS_PROCPARAM = 0x61000001;
|
||||
static constexpr std::uint32_t PT_OS_RELRO = 0x61000010;
|
||||
std::shared_ptr<IEntryStubBuilder> _entryStubBuilder;
|
||||
std::shared_ptr<IProgramHeaderLayoutBuilder> _programHeaderLayoutBuilder;
|
||||
std::shared_ptr<ISectionHeaderTableBuilder> _sectionHeaderTableBuilder;
|
||||
std::shared_ptr<Io::IByteWriter> _byteWriter;
|
||||
|
||||
static constexpr std::int64_t DT_NULL = 0;
|
||||
static constexpr std::int64_t DT_NEEDED = 1;
|
||||
static constexpr std::int64_t DT_STRTAB = 5;
|
||||
static constexpr std::int64_t DT_SYMTAB = 6;
|
||||
static constexpr std::int64_t DT_RELA = 7;
|
||||
static constexpr std::int64_t DT_RELASZ = 8;
|
||||
static constexpr std::int64_t DT_RELAENT = 9;
|
||||
static constexpr std::int64_t DT_STRSZ = 10;
|
||||
static constexpr std::int64_t DT_SYMENT = 11;
|
||||
static constexpr std::int64_t DT_JMPREL = 23;
|
||||
static constexpr std::int64_t DT_PLTRELSZ = 2;
|
||||
static constexpr std::int64_t DT_PLTREL = 20;
|
||||
static constexpr std::uint32_t SHT_NULL = 0;
|
||||
static constexpr std::uint32_t SHT_PROGBITS = 1;
|
||||
static constexpr std::uint32_t SHT_DYNSYM = 11;
|
||||
static constexpr std::uint32_t SHT_STRTAB = 3;
|
||||
static constexpr std::uint32_t SHT_DYNAMIC = 6;
|
||||
static constexpr std::uint32_t SHF_ALLOC = 0x2;
|
||||
static constexpr std::uint32_t SHF_WRITE = 0x1;
|
||||
static constexpr std::uint32_t SHF_EXECINSTR = 0x4;
|
||||
|
||||
void _writeSectionHeader(
|
||||
std::vector<std::uint8_t>& buf,
|
||||
std::size_t offset,
|
||||
std::uint32_t nameOff,
|
||||
std::uint32_t type,
|
||||
std::uint64_t flags,
|
||||
std::uint64_t addr,
|
||||
std::uint64_t fileOffset,
|
||||
std::uint64_t size,
|
||||
std::uint32_t link,
|
||||
std::uint32_t info,
|
||||
std::uint64_t align,
|
||||
std::uint64_t entSize
|
||||
) const;
|
||||
[[nodiscard]] bool _isSceSpecificSegment(std::uint32_t type) const;
|
||||
[[nodiscard]] bool _isNullPageLoad(const Domain::ProgramHeader& ph) const;
|
||||
[[nodiscard]] bool _shouldSkip(const Domain::ProgramHeader& ph) const;
|
||||
[[nodiscard]] Domain::ProgramHeader _makeLoadHeader(std::uint64_t offset, std::uint64_t size) const;
|
||||
[[nodiscard]] Domain::ProgramHeader _makeHeaderBlockLoad(std::uint64_t vaddr, std::uint64_t size, std::uint64_t align) const;
|
||||
[[nodiscard]] Domain::ProgramHeader _makePhdrHeader(std::uint64_t offset, std::uint64_t vaddr, std::uint64_t size) const;
|
||||
[[nodiscard]] std::vector<std::uint8_t> _buildEntryStub(std::uint64_t stubVaddr, std::uint64_t realEntryVaddr) const;
|
||||
void _writeU16(std::vector<std::uint8_t>& buf, std::size_t offset, std::uint16_t v) const;
|
||||
void _writeU32(std::vector<std::uint8_t>& buf, std::size_t offset, std::uint32_t v) const;
|
||||
void _writeU64(std::vector<std::uint8_t>& buf, std::size_t offset, std::uint64_t v) const;
|
||||
void _appendU64(std::vector<std::uint8_t>& buf, std::uint64_t v) const;
|
||||
void _appendI64(std::vector<std::uint8_t>& buf, std::int64_t v) const;
|
||||
void _appendDynEntry(std::vector<std::uint8_t>& buf, std::int64_t tag, std::uint64_t val) const;
|
||||
void _writeProgramHeader(std::vector<std::uint8_t>& buf, std::size_t offset, const Domain::ProgramHeader& ph) const;
|
||||
[[nodiscard]] std::uint32_t _fixLoadFlags(std::uint32_t originalFlags) const;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
#ifndef ELFPATCHER_DOMAIN_ELFCONSTANTS_HPP
|
||||
#define ELFPATCHER_DOMAIN_ELFCONSTANTS_HPP
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace Elfpatcher {
|
||||
|
||||
inline constexpr std::uint32_t PT_LOAD = 1;
|
||||
inline constexpr std::uint32_t PT_DYNAMIC = 2;
|
||||
inline constexpr std::uint32_t PT_INTERP = 3;
|
||||
inline constexpr std::uint32_t PT_NOTE = 4;
|
||||
inline constexpr std::uint32_t PT_PHDR = 6;
|
||||
inline constexpr std::uint32_t PT_GNU_RELRO = 0x6474e552;
|
||||
inline constexpr std::uint32_t PF_X = 0x1;
|
||||
inline constexpr std::uint32_t PF_W = 0x2;
|
||||
inline constexpr std::uint32_t PF_R = 0x4;
|
||||
inline constexpr std::uint32_t ET_DYN = 3;
|
||||
inline constexpr std::uint32_t PT_SCE_DYNLIBDATA = 0x61000000;
|
||||
inline constexpr std::uint32_t PT_OS_PROCPARAM = 0x61000001;
|
||||
inline constexpr std::uint32_t PT_OS_RELRO = 0x61000010;
|
||||
|
||||
inline constexpr std::int64_t DT_NULL = 0;
|
||||
inline constexpr std::int64_t DT_NEEDED = 1;
|
||||
inline constexpr std::int64_t DT_PLTRELSZ = 2;
|
||||
inline constexpr std::int64_t DT_STRTAB = 5;
|
||||
inline constexpr std::int64_t DT_SYMTAB = 6;
|
||||
inline constexpr std::int64_t DT_RELA = 7;
|
||||
inline constexpr std::int64_t DT_RELASZ = 8;
|
||||
inline constexpr std::int64_t DT_RELAENT = 9;
|
||||
inline constexpr std::int64_t DT_STRSZ = 10;
|
||||
inline constexpr std::int64_t DT_SYMENT = 11;
|
||||
inline constexpr std::int64_t DT_PLTREL = 20;
|
||||
inline constexpr std::int64_t DT_JMPREL = 23;
|
||||
|
||||
inline constexpr std::uint32_t SHT_NULL = 0;
|
||||
inline constexpr std::uint32_t SHT_PROGBITS = 1;
|
||||
inline constexpr std::uint32_t SHT_STRTAB = 3;
|
||||
inline constexpr std::uint32_t SHT_DYNAMIC = 6;
|
||||
inline constexpr std::uint32_t SHT_DYNSYM = 11;
|
||||
inline constexpr std::uint32_t SHF_WRITE = 0x1;
|
||||
inline constexpr std::uint32_t SHF_ALLOC = 0x2;
|
||||
inline constexpr std::uint32_t SHF_EXECINSTR = 0x4;
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,21 @@
|
||||
#ifndef ELFPATCHER_DOMAIN_IENTRYSTUBBUILDER_HPP
|
||||
#define ELFPATCHER_DOMAIN_IENTRYSTUBBUILDER_HPP
|
||||
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
|
||||
namespace Elfpatcher {
|
||||
|
||||
class IEntryStubBuilder {
|
||||
public:
|
||||
virtual ~IEntryStubBuilder() = default;
|
||||
|
||||
virtual std::vector<std::uint8_t> BuildEntryStub(
|
||||
std::uint64_t stubVaddr,
|
||||
std::uint64_t realEntryVaddr
|
||||
) const = 0;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,22 @@
|
||||
#ifndef ELFPATCHER_DOMAIN_IPROGRAMHEADERLAYOUTBUILDER_HPP
|
||||
#define ELFPATCHER_DOMAIN_IPROGRAMHEADERLAYOUTBUILDER_HPP
|
||||
|
||||
#include <elfpatcher/domain/ProgramHeaderLayoutRequest.hpp>
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
|
||||
namespace Elfpatcher {
|
||||
|
||||
class IProgramHeaderLayoutBuilder {
|
||||
public:
|
||||
virtual ~IProgramHeaderLayoutBuilder() = default;
|
||||
|
||||
virtual std::uint16_t WriteLayout(
|
||||
std::vector<std::uint8_t>& buf,
|
||||
const ProgramHeaderLayoutRequest& request
|
||||
) const = 0;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,22 @@
|
||||
#ifndef ELFPATCHER_DOMAIN_ISECTIONHEADERTABLEBUILDER_HPP
|
||||
#define ELFPATCHER_DOMAIN_ISECTIONHEADERTABLEBUILDER_HPP
|
||||
|
||||
#include <elfpatcher/domain/SectionHeaderTableRequest.hpp>
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
|
||||
namespace Elfpatcher {
|
||||
|
||||
class ISectionHeaderTableBuilder {
|
||||
public:
|
||||
virtual ~ISectionHeaderTableBuilder() = default;
|
||||
|
||||
virtual void WriteTable(
|
||||
std::vector<std::uint8_t>& buf,
|
||||
const SectionHeaderTableRequest& request
|
||||
) const = 0;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,17 @@
|
||||
#ifndef ELFPATCHER_DOMAIN_ISEGMENTFILTER_HPP
|
||||
#define ELFPATCHER_DOMAIN_ISEGMENTFILTER_HPP
|
||||
|
||||
#include <domain/Types.hpp>
|
||||
|
||||
namespace Elfpatcher {
|
||||
|
||||
class ISegmentFilter {
|
||||
public:
|
||||
virtual ~ISegmentFilter() = default;
|
||||
|
||||
virtual bool ShouldSkip(const Domain::ProgramHeader& ph) const = 0;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,25 @@
|
||||
#ifndef ELFPATCHER_DOMAIN_PROGRAMHEADERLAYOUTREQUEST_HPP
|
||||
#define ELFPATCHER_DOMAIN_PROGRAMHEADERLAYOUTREQUEST_HPP
|
||||
|
||||
#include <domain/Types.hpp>
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
|
||||
namespace Elfpatcher {
|
||||
|
||||
struct ProgramHeaderLayoutRequest {
|
||||
std::uint64_t PhOff;
|
||||
std::uint16_t PhEntSize;
|
||||
std::uint16_t PhNum;
|
||||
std::vector<Domain::ProgramHeader> OriginalHeaders;
|
||||
std::uint64_t ExtraBlockOffset;
|
||||
std::uint64_t ExtraBlockSize;
|
||||
std::uint64_t DynamicSegmentOffset;
|
||||
std::uint64_t DynamicSegmentSize;
|
||||
std::uint64_t InterpOffset;
|
||||
std::uint64_t InterpSize;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,21 @@
|
||||
#ifndef ELFPATCHER_DOMAIN_SECTIONHEADERTABLEREQUEST_HPP
|
||||
#define ELFPATCHER_DOMAIN_SECTIONHEADERTABLEREQUEST_HPP
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace Elfpatcher {
|
||||
|
||||
struct SectionHeaderTableRequest {
|
||||
std::uint64_t DynStrOffset;
|
||||
std::uint64_t DynStrSize;
|
||||
std::uint64_t DynSymOffset;
|
||||
std::uint64_t DynSymSize;
|
||||
std::uint64_t DynamicSegmentOffset;
|
||||
std::uint64_t DynamicSegmentSize;
|
||||
std::uint64_t StubOffset;
|
||||
std::uint64_t StubSize;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,19 @@
|
||||
#ifndef ELFPATCHER_FILTERING_SEGMENTFILTER_HPP
|
||||
#define ELFPATCHER_FILTERING_SEGMENTFILTER_HPP
|
||||
|
||||
#include <elfpatcher/domain/ISegmentFilter.hpp>
|
||||
|
||||
namespace Elfpatcher {
|
||||
|
||||
class SegmentFilter : public ISegmentFilter {
|
||||
public:
|
||||
bool ShouldSkip(const Domain::ProgramHeader& ph) const override;
|
||||
|
||||
private:
|
||||
[[nodiscard]] bool _isSceSpecificSegment(std::uint32_t type) const;
|
||||
[[nodiscard]] bool _isNullPageLoad(const Domain::ProgramHeader& ph) const;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,38 @@
|
||||
#ifndef ELFPATCHER_LAYOUT_PROGRAMHEADERLAYOUTBUILDER_HPP
|
||||
#define ELFPATCHER_LAYOUT_PROGRAMHEADERLAYOUTBUILDER_HPP
|
||||
|
||||
#include <elfpatcher/domain/IProgramHeaderLayoutBuilder.hpp>
|
||||
#include <elfpatcher/domain/ISegmentFilter.hpp>
|
||||
#include <io/IByteWriter.hpp>
|
||||
#include <memory>
|
||||
|
||||
namespace Elfpatcher {
|
||||
|
||||
class ProgramHeaderLayoutBuilder : public IProgramHeaderLayoutBuilder {
|
||||
public:
|
||||
ProgramHeaderLayoutBuilder(
|
||||
std::shared_ptr<ISegmentFilter> segmentFilter,
|
||||
std::shared_ptr<Io::IByteWriter> byteWriter
|
||||
);
|
||||
|
||||
std::uint16_t WriteLayout(
|
||||
std::vector<std::uint8_t>& buf,
|
||||
const ProgramHeaderLayoutRequest& request
|
||||
) const override;
|
||||
|
||||
private:
|
||||
std::shared_ptr<ISegmentFilter> _segmentFilter;
|
||||
std::shared_ptr<Io::IByteWriter> _byteWriter;
|
||||
|
||||
[[nodiscard]] Domain::ProgramHeader _makeLoadHeader(std::uint64_t offset, std::uint64_t size) const;
|
||||
[[nodiscard]] Domain::ProgramHeader _makeHeaderBlockLoad(std::uint64_t vaddr, std::uint64_t size, std::uint64_t align) const;
|
||||
[[nodiscard]] Domain::ProgramHeader _makePhdrHeader(std::uint64_t offset, std::uint64_t vaddr, std::uint64_t size) const;
|
||||
[[nodiscard]] Domain::ProgramHeader _makeDynamicHeader(std::uint64_t offset, std::uint64_t size) const;
|
||||
[[nodiscard]] Domain::ProgramHeader _makeInterpHeader(std::uint64_t offset, std::uint64_t size) const;
|
||||
[[nodiscard]] std::uint32_t _fixLoadFlags(std::uint32_t originalFlags) const;
|
||||
void _writeProgramHeader(std::vector<std::uint8_t>& buf, std::size_t offset, const Domain::ProgramHeader& ph) const;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,40 @@
|
||||
#ifndef ELFPATCHER_SECTIONS_SECTIONHEADERTABLEBUILDER_HPP
|
||||
#define ELFPATCHER_SECTIONS_SECTIONHEADERTABLEBUILDER_HPP
|
||||
|
||||
#include <elfpatcher/domain/ISectionHeaderTableBuilder.hpp>
|
||||
#include <io/IByteWriter.hpp>
|
||||
#include <memory>
|
||||
|
||||
namespace Elfpatcher {
|
||||
|
||||
class SectionHeaderTableBuilder : public ISectionHeaderTableBuilder {
|
||||
public:
|
||||
explicit SectionHeaderTableBuilder(std::shared_ptr<Io::IByteWriter> byteWriter);
|
||||
|
||||
void WriteTable(
|
||||
std::vector<std::uint8_t>& buf,
|
||||
const SectionHeaderTableRequest& request
|
||||
) const override;
|
||||
|
||||
private:
|
||||
std::shared_ptr<Io::IByteWriter> _byteWriter;
|
||||
|
||||
void _writeSectionHeader(
|
||||
std::vector<std::uint8_t>& buf,
|
||||
std::size_t offset,
|
||||
std::uint32_t nameOff,
|
||||
std::uint32_t type,
|
||||
std::uint64_t flags,
|
||||
std::uint64_t addr,
|
||||
std::uint64_t fileOffset,
|
||||
std::uint64_t size,
|
||||
std::uint32_t link,
|
||||
std::uint32_t info,
|
||||
std::uint64_t align,
|
||||
std::uint64_t entSize
|
||||
) const;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,18 @@
|
||||
#ifndef ELFPATCHER_STUB_ENTRYSTUBBUILDER_HPP
|
||||
#define ELFPATCHER_STUB_ENTRYSTUBBUILDER_HPP
|
||||
|
||||
#include <elfpatcher/domain/IEntryStubBuilder.hpp>
|
||||
|
||||
namespace Elfpatcher {
|
||||
|
||||
class EntryStubBuilder : public IEntryStubBuilder {
|
||||
public:
|
||||
std::vector<std::uint8_t> BuildEntryStub(
|
||||
std::uint64_t stubVaddr,
|
||||
std::uint64_t realEntryVaddr
|
||||
) const override;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -1,153 +1,26 @@
|
||||
#include <elfpatcher/ElfPatcher.hpp>
|
||||
#include <cstring>
|
||||
#include <elfpatcher/domain/ElfConstants.hpp>
|
||||
#include <elfpatcher/domain/ProgramHeaderLayoutRequest.hpp>
|
||||
#include <elfpatcher/domain/SectionHeaderTableRequest.hpp>
|
||||
|
||||
namespace Elfpatcher {
|
||||
|
||||
bool ElfPatcher::_isSceSpecificSegment(std::uint32_t type) const {
|
||||
return type == PT_SCE_DYNLIBDATA
|
||||
|| type == PT_OS_PROCPARAM
|
||||
|| type == PT_OS_RELRO
|
||||
|| (type >= 0x61000000 && type <= 0x6fffffff);
|
||||
}
|
||||
|
||||
bool ElfPatcher::_isNullPageLoad(const Domain::ProgramHeader& ph) const {
|
||||
return ph.Type == PT_LOAD && ph.MappedAddress == 0;
|
||||
}
|
||||
|
||||
void ElfPatcher::_writeU16(std::vector<std::uint8_t>& buf, std::size_t offset, std::uint16_t v) const {
|
||||
std::memcpy(buf.data() + offset, &v, 2);
|
||||
}
|
||||
|
||||
void ElfPatcher::_writeU32(std::vector<std::uint8_t>& buf, const std::size_t offset, std::uint32_t v) const {
|
||||
std::memcpy(buf.data() + offset, &v, 4);
|
||||
}
|
||||
|
||||
void ElfPatcher::_writeU64(std::vector<std::uint8_t>& buf, const std::size_t offset, std::uint64_t v) const {
|
||||
std::memcpy(buf.data() + offset, &v, 8);
|
||||
}
|
||||
|
||||
void ElfPatcher::_appendU64(std::vector<std::uint8_t>& buf, std::uint64_t v) const {
|
||||
std::size_t pos = buf.size();
|
||||
buf.resize(pos + 8);
|
||||
std::memcpy(buf.data() + pos, &v, 8);
|
||||
}
|
||||
|
||||
void ElfPatcher::_appendI64(std::vector<std::uint8_t>& buf, std::int64_t v) const {
|
||||
_appendU64(buf, static_cast<std::uint64_t>(v));
|
||||
ElfPatcher::ElfPatcher(
|
||||
std::shared_ptr<IEntryStubBuilder> entryStubBuilder,
|
||||
std::shared_ptr<IProgramHeaderLayoutBuilder> programHeaderLayoutBuilder,
|
||||
std::shared_ptr<ISectionHeaderTableBuilder> sectionHeaderTableBuilder,
|
||||
std::shared_ptr<Io::IByteWriter> byteWriter
|
||||
)
|
||||
: _entryStubBuilder(std::move(entryStubBuilder))
|
||||
, _programHeaderLayoutBuilder(std::move(programHeaderLayoutBuilder))
|
||||
, _sectionHeaderTableBuilder(std::move(sectionHeaderTableBuilder))
|
||||
, _byteWriter(std::move(byteWriter))
|
||||
{
|
||||
}
|
||||
|
||||
void ElfPatcher::_appendDynEntry(std::vector<std::uint8_t>& buf, std::int64_t tag, std::uint64_t val) const {
|
||||
_appendI64(buf, tag);
|
||||
_appendU64(buf, val);
|
||||
}
|
||||
|
||||
void ElfPatcher::_writeProgramHeader(std::vector<std::uint8_t>& buf, std::size_t offset, const Domain::ProgramHeader& ph) const {
|
||||
_writeU32(buf, offset + 0, ph.Type);
|
||||
_writeU32(buf, offset + 4, ph.Flags);
|
||||
_writeU64(buf, offset + 8, ph.Offset);
|
||||
_writeU64(buf, offset + 16, ph.MappedAddress);
|
||||
_writeU64(buf, offset + 24, ph.PhysicalAddress);
|
||||
_writeU64(buf, offset + 32, ph.FileSize);
|
||||
_writeU64(buf, offset + 40, ph.MemorySize);
|
||||
_writeU64(buf, offset + 48, ph.Alignment);
|
||||
}
|
||||
|
||||
Domain::ProgramHeader ElfPatcher::_makeLoadHeader(std::uint64_t offset, std::uint64_t size) const {
|
||||
Domain::ProgramHeader ph{};
|
||||
ph.Type = PT_LOAD;
|
||||
ph.Flags = PF_R | PF_W;
|
||||
ph.Offset = offset;
|
||||
ph.MappedAddress = offset;
|
||||
ph.PhysicalAddress = offset;
|
||||
ph.FileSize = size;
|
||||
ph.MemorySize = size;
|
||||
ph.Alignment = 0x1000;
|
||||
return ph;
|
||||
}
|
||||
|
||||
Domain::ProgramHeader ElfPatcher::_makeHeaderBlockLoad(std::uint64_t vaddr, std::uint64_t size, std::uint64_t align) const {
|
||||
Domain::ProgramHeader ph{};
|
||||
ph.Type = PT_LOAD;
|
||||
ph.Flags = PF_R;
|
||||
ph.Offset = 0;
|
||||
ph.MappedAddress = vaddr;
|
||||
ph.PhysicalAddress = vaddr;
|
||||
ph.FileSize = size;
|
||||
ph.MemorySize = size;
|
||||
ph.Alignment = align;
|
||||
return ph;
|
||||
}
|
||||
|
||||
Domain::ProgramHeader ElfPatcher::_makePhdrHeader(std::uint64_t offset, std::uint64_t vaddr, std::uint64_t size) const {
|
||||
Domain::ProgramHeader ph{};
|
||||
ph.Type = PT_PHDR;
|
||||
ph.Flags = PF_R;
|
||||
ph.Offset = offset;
|
||||
ph.MappedAddress = vaddr;
|
||||
ph.PhysicalAddress = vaddr;
|
||||
ph.FileSize = size;
|
||||
ph.MemorySize = size;
|
||||
ph.Alignment = 8;
|
||||
return ph;
|
||||
}
|
||||
|
||||
std::vector<std::uint8_t> ElfPatcher::_buildEntryStub(const std::uint64_t stubVaddr, const std::uint64_t realEntryVaddr) const {
|
||||
std::vector<std::uint8_t> s;
|
||||
s.push_back(0x58);
|
||||
s.push_back(0x48); s.push_back(0x89); s.push_back(0xe3);
|
||||
s.push_back(0x48); s.push_back(0x83); s.push_back(0xec); s.push_back(0x30);
|
||||
s.push_back(0x48); s.push_back(0x83); s.push_back(0xe4); s.push_back(0xf0);
|
||||
s.push_back(0x89); s.push_back(0x04); s.push_back(0x24);
|
||||
s.push_back(0x48); s.push_back(0x89); s.push_back(0x5c); s.push_back(0x24); s.push_back(0x08);
|
||||
s.push_back(0x48); s.push_back(0x89); s.push_back(0xe7);
|
||||
s.push_back(0x48); s.push_back(0x31); s.push_back(0xf6);
|
||||
const std::uint64_t jmpInsnVaddr = stubVaddr + s.size();
|
||||
const std::uint64_t jmpNextVaddr = jmpInsnVaddr + 5;
|
||||
const std::int32_t rel32 = static_cast<std::int32_t>(realEntryVaddr - jmpNextVaddr);
|
||||
s.push_back(0xe9);
|
||||
s.push_back(static_cast<std::uint8_t>(rel32 & 0xff));
|
||||
s.push_back(static_cast<std::uint8_t>((rel32 >> 8) & 0xff));
|
||||
s.push_back(static_cast<std::uint8_t>((rel32 >> 16) & 0xff));
|
||||
s.push_back(static_cast<std::uint8_t>((rel32 >> 24) & 0xff));
|
||||
return s;
|
||||
}
|
||||
|
||||
std::uint32_t ElfPatcher::_fixLoadFlags(std::uint32_t originalFlags) const {
|
||||
return originalFlags | PF_R;
|
||||
}
|
||||
|
||||
void ElfPatcher::_writeSectionHeader(
|
||||
std::vector<std::uint8_t>& buf,
|
||||
const std::size_t offset,
|
||||
const std::uint32_t nameOff,
|
||||
const std::uint32_t type,
|
||||
const std::uint64_t flags,
|
||||
const std::uint64_t addr,
|
||||
const std::uint64_t fileOffset,
|
||||
const std::uint64_t size,
|
||||
const std::uint32_t link,
|
||||
const std::uint32_t info,
|
||||
const std::uint64_t align,
|
||||
const std::uint64_t entSize
|
||||
) const {
|
||||
_writeU32(buf, offset + 0, nameOff);
|
||||
_writeU32(buf, offset + 4, type);
|
||||
_writeU64(buf, offset + 8, flags);
|
||||
_writeU64(buf, offset + 16, addr);
|
||||
_writeU64(buf, offset + 24, fileOffset);
|
||||
_writeU64(buf, offset + 32, size);
|
||||
_writeU32(buf, offset + 40, link);
|
||||
_writeU32(buf, offset + 44, info);
|
||||
_writeU64(buf, offset + 48, align);
|
||||
_writeU64(buf, offset + 56, entSize);
|
||||
}
|
||||
|
||||
bool ElfPatcher::_shouldSkip(const Domain::ProgramHeader& ph) const {
|
||||
if (_isSceSpecificSegment(ph.Type)) return true;
|
||||
if (ph.Type == PT_DYNAMIC) return true;
|
||||
if (_isNullPageLoad(ph)) return true;
|
||||
if (ph.Type == PT_NOTE && ph.MappedAddress == 0 && ph.FileSize > 0) return true;
|
||||
return false;
|
||||
_byteWriter->AppendI64(buf, tag);
|
||||
_byteWriter->AppendU64(buf, val);
|
||||
}
|
||||
|
||||
std::vector<std::uint8_t> ElfPatcher::Patch(
|
||||
@@ -161,9 +34,9 @@ std::vector<std::uint8_t> ElfPatcher::Patch(
|
||||
buf[8] = 0;
|
||||
buf[0x10] = static_cast<std::uint8_t>(ET_DYN & 0xFF);
|
||||
buf[0x11] = static_cast<std::uint8_t>((ET_DYN >> 8) & 0xFF);
|
||||
_writeU64(buf, 40, 0);
|
||||
_writeU16(buf, 60, 0);
|
||||
_writeU16(buf, 62, 0);
|
||||
_byteWriter->WriteU64(buf, 40, 0);
|
||||
_byteWriter->WriteU16(buf, 60, 0);
|
||||
_byteWriter->WriteU16(buf, 62, 0);
|
||||
|
||||
const std::uint64_t phOff = *reinterpret_cast<const std::uint64_t*>(buf.data() + 32);
|
||||
const std::uint16_t phEntSize = *reinterpret_cast<const std::uint16_t*>(buf.data() + 54);
|
||||
@@ -174,22 +47,22 @@ std::vector<std::uint8_t> ElfPatcher::Patch(
|
||||
b.push_back(0);
|
||||
};
|
||||
|
||||
const std::uint64_t dynStrOff = static_cast<std::uint64_t>(buf.size());
|
||||
const auto dynStrOff = static_cast<std::uint64_t>(buf.size());
|
||||
for (std::uint8_t b : dynSection.DynStrData)
|
||||
buf.push_back(b);
|
||||
alignBuf(buf, 24);
|
||||
|
||||
const std::uint64_t dynSymOff = static_cast<std::uint64_t>(buf.size());
|
||||
const auto dynSymOff = static_cast<std::uint64_t>(buf.size());
|
||||
for (std::uint8_t b : dynSection.DynSymData)
|
||||
buf.push_back(b);
|
||||
alignBuf(buf, 24);
|
||||
|
||||
const std::uint64_t relaOff = static_cast<std::uint64_t>(buf.size());
|
||||
const auto relaOff = static_cast<std::uint64_t>(buf.size());
|
||||
for (std::uint8_t b : dynSection.RelaData)
|
||||
buf.push_back(b);
|
||||
alignBuf(buf, 24);
|
||||
|
||||
const std::uint64_t relaPltOff = static_cast<std::uint64_t>(buf.size());
|
||||
const auto relaPltOff = static_cast<std::uint64_t>(buf.size());
|
||||
for (std::uint8_t b : dynSection.RelaPltData)
|
||||
buf.push_back(b);
|
||||
alignBuf(buf, 8);
|
||||
@@ -213,148 +86,52 @@ std::vector<std::uint8_t> ElfPatcher::Patch(
|
||||
}
|
||||
_appendDynEntry(dynSegBuf, DT_NULL, 0);
|
||||
|
||||
const std::uint64_t dynSegOff = static_cast<std::uint64_t>(buf.size());
|
||||
const auto dynSegOff = static_cast<std::uint64_t>(buf.size());
|
||||
for (std::uint8_t b : dynSegBuf)
|
||||
buf.push_back(b);
|
||||
|
||||
const std::uint64_t realEntryVaddr = *reinterpret_cast<const std::uint64_t*>(buf.data() + 24);
|
||||
const std::uint64_t stubOff = static_cast<std::uint64_t>(buf.size());
|
||||
const auto stubBytes = _buildEntryStub(stubOff, realEntryVaddr);
|
||||
const auto stubOff = static_cast<std::uint64_t>(buf.size());
|
||||
const auto stubBytes = _entryStubBuilder->BuildEntryStub(stubOff, realEntryVaddr);
|
||||
for (std::uint8_t b : stubBytes)
|
||||
buf.push_back(b);
|
||||
_writeU64(buf, 24, stubOff);
|
||||
_byteWriter->WriteU64(buf, 24, stubOff);
|
||||
|
||||
static constexpr char kInterp[] = "/lib64/ld-linux-x86-64.so.2";
|
||||
const std::uint64_t interpOff = static_cast<std::uint64_t>(buf.size());
|
||||
const auto interpOff = static_cast<std::uint64_t>(buf.size());
|
||||
constexpr std::uint64_t interpSize = sizeof(kInterp);
|
||||
for (std::size_t i = 0; i < interpSize; i++)
|
||||
buf.push_back(static_cast<std::uint8_t>(kInterp[i]));
|
||||
|
||||
std::uint16_t keptCount = 0;
|
||||
std::uint64_t keptMinVaddr = UINT64_MAX;
|
||||
std::uint64_t keptMinVaddrAlign = 0x1000;
|
||||
for (const auto& ph : originalHeaders) {
|
||||
if (_shouldSkip(ph))
|
||||
continue;
|
||||
keptCount++;
|
||||
if (ph.Type == PT_LOAD && ph.MappedAddress < keptMinVaddr) {
|
||||
keptMinVaddr = ph.MappedAddress;
|
||||
keptMinVaddrAlign = ph.Alignment;
|
||||
}
|
||||
}
|
||||
if (keptMinVaddr == UINT64_MAX)
|
||||
throw Domain::RelinkerException("No kept PT_LOAD segment to anchor the header block against");
|
||||
if (keptMinVaddrAlign == 0)
|
||||
throw Domain::RelinkerException("Lowest kept PT_LOAD has zero alignment");
|
||||
|
||||
const std::uint16_t neededPh = keptCount + 4;
|
||||
if (neededPh > phNum)
|
||||
throw Domain::RelinkerException(
|
||||
"Not enough program header slots: need " + std::to_string(neededPh) +
|
||||
", available " + std::to_string(phNum));
|
||||
|
||||
const std::uint64_t headerBlockSize = phOff + static_cast<std::uint64_t>(neededPh) * phEntSize;
|
||||
if (headerBlockSize >= keptMinVaddr)
|
||||
throw Domain::RelinkerException(
|
||||
"Header block does not fit below the lowest kept PT_LOAD: need " +
|
||||
std::to_string(headerBlockSize) + " bytes, only " + std::to_string(keptMinVaddr) + " available");
|
||||
|
||||
const std::uint64_t headerBlockVaddr = (keptMinVaddr - headerBlockSize) & ~(keptMinVaddrAlign - 1);
|
||||
if (headerBlockVaddr + headerBlockSize > keptMinVaddr)
|
||||
throw Domain::RelinkerException("Header block would overlap the lowest kept PT_LOAD after alignment");
|
||||
for (char i : kInterp)
|
||||
buf.push_back(static_cast<std::uint8_t>(i));
|
||||
|
||||
const std::uint64_t extraBlockOff = dynStrOff;
|
||||
const std::uint64_t extraBlockSize = static_cast<std::uint64_t>(buf.size()) - extraBlockOff;
|
||||
|
||||
std::uint16_t writtenPh = 0;
|
||||
ProgramHeaderLayoutRequest layoutRequest{};
|
||||
layoutRequest.PhOff = phOff;
|
||||
layoutRequest.PhEntSize = phEntSize;
|
||||
layoutRequest.PhNum = phNum;
|
||||
layoutRequest.OriginalHeaders = originalHeaders;
|
||||
layoutRequest.ExtraBlockOffset = extraBlockOff;
|
||||
layoutRequest.ExtraBlockSize = extraBlockSize;
|
||||
layoutRequest.DynamicSegmentOffset = dynSegOff;
|
||||
layoutRequest.DynamicSegmentSize = dynSegBuf.size();
|
||||
layoutRequest.InterpOffset = interpOff;
|
||||
layoutRequest.InterpSize = interpSize;
|
||||
|
||||
const std::size_t phdrEntOff = static_cast<std::size_t>(phOff) + writtenPh * phEntSize;
|
||||
_writeProgramHeader(buf, phdrEntOff, _makePhdrHeader(phOff, headerBlockVaddr + phOff, static_cast<std::uint64_t>(neededPh) * phEntSize));
|
||||
writtenPh++;
|
||||
const std::uint16_t writtenPh = _programHeaderLayoutBuilder->WriteLayout(buf, layoutRequest);
|
||||
_byteWriter->WriteU16(buf, 56, writtenPh);
|
||||
|
||||
const std::size_t headerLoadEntOff = static_cast<std::size_t>(phOff) + writtenPh * phEntSize;
|
||||
_writeProgramHeader(buf, headerLoadEntOff, _makeHeaderBlockLoad(headerBlockVaddr, headerBlockSize, keptMinVaddrAlign));
|
||||
writtenPh++;
|
||||
SectionHeaderTableRequest sectionRequest{};
|
||||
sectionRequest.DynStrOffset = dynStrOff;
|
||||
sectionRequest.DynStrSize = dynSection.DynStrData.size();
|
||||
sectionRequest.DynSymOffset = dynSymOff;
|
||||
sectionRequest.DynSymSize = dynSection.DynSymData.size();
|
||||
sectionRequest.DynamicSegmentOffset = dynSegOff;
|
||||
sectionRequest.DynamicSegmentSize = dynSegBuf.size();
|
||||
sectionRequest.StubOffset = stubOff;
|
||||
sectionRequest.StubSize = stubBytes.size();
|
||||
|
||||
for (const auto& ph : originalHeaders) {
|
||||
if (_shouldSkip(ph))
|
||||
continue;
|
||||
const std::size_t phEntOff = static_cast<std::size_t>(phOff) + writtenPh * phEntSize;
|
||||
if (ph.Type == PT_LOAD) {
|
||||
Domain::ProgramHeader fixed = ph;
|
||||
fixed.Flags = _fixLoadFlags(ph.Flags);
|
||||
_writeProgramHeader(buf, phEntOff, fixed);
|
||||
} else {
|
||||
_writeProgramHeader(buf, phEntOff, ph);
|
||||
}
|
||||
writtenPh++;
|
||||
}
|
||||
|
||||
{
|
||||
const std::size_t phEntOff = static_cast<std::size_t>(phOff) + writtenPh * phEntSize;
|
||||
_writeProgramHeader(buf, phEntOff, _makeLoadHeader(extraBlockOff, extraBlockSize));
|
||||
writtenPh++;
|
||||
}
|
||||
|
||||
{
|
||||
const std::size_t phEntOff = static_cast<std::size_t>(phOff) + writtenPh * phEntSize;
|
||||
Domain::ProgramHeader dynPh{};
|
||||
dynPh.Type = PT_DYNAMIC;
|
||||
dynPh.Flags = PF_R | PF_W;
|
||||
dynPh.Offset = dynSegOff;
|
||||
dynPh.MappedAddress = dynSegOff;
|
||||
dynPh.PhysicalAddress = dynSegOff;
|
||||
dynPh.FileSize = dynSegBuf.size();
|
||||
dynPh.MemorySize = dynSegBuf.size();
|
||||
dynPh.Alignment = 8;
|
||||
_writeProgramHeader(buf, phEntOff, dynPh);
|
||||
writtenPh++;
|
||||
}
|
||||
|
||||
{
|
||||
const std::size_t phEntOff = static_cast<std::size_t>(phOff) + writtenPh * phEntSize;
|
||||
Domain::ProgramHeader interpPh{};
|
||||
interpPh.Type = PT_INTERP;
|
||||
interpPh.Flags = PF_R;
|
||||
interpPh.Offset = interpOff;
|
||||
interpPh.MappedAddress = interpOff;
|
||||
interpPh.PhysicalAddress = interpOff;
|
||||
interpPh.FileSize = interpSize;
|
||||
interpPh.MemorySize = interpSize;
|
||||
interpPh.Alignment = 1;
|
||||
_writeProgramHeader(buf, phEntOff, interpPh);
|
||||
writtenPh++;
|
||||
}
|
||||
|
||||
_writeU16(buf, 56, writtenPh);
|
||||
|
||||
static constexpr char kShStrTab[] = "\0.shstrtab\0.dynstr\0.dynsym\0.dynamic\0.text";
|
||||
constexpr std::uint32_t nameShStrTab = 1;
|
||||
constexpr std::uint32_t nameDynStr = 11;
|
||||
constexpr std::uint32_t nameDynSym = 19;
|
||||
constexpr std::uint32_t nameDynamic = 27;
|
||||
constexpr std::uint32_t nameText = 36;
|
||||
const std::uint64_t shStrTabOff = static_cast<std::uint64_t>(buf.size());
|
||||
constexpr std::uint64_t shStrTabSize = sizeof(kShStrTab);
|
||||
for (std::size_t i = 0; i < shStrTabSize; i++)
|
||||
buf.push_back(static_cast<std::uint8_t>(kShStrTab[i]));
|
||||
|
||||
const std::uint64_t shOff = static_cast<std::uint64_t>(buf.size());
|
||||
constexpr std::size_t shEntSize = 64;
|
||||
constexpr std::size_t shNum = 6;
|
||||
buf.resize(buf.size() + shEntSize * shNum, 0);
|
||||
|
||||
_writeSectionHeader(buf, shOff + shEntSize * 0, 0, SHT_NULL, 0, 0, 0, 0, 0, 0, 0, 0);
|
||||
_writeSectionHeader(buf, shOff + shEntSize * 1, nameShStrTab, SHT_STRTAB, 0, shStrTabOff, shStrTabOff, shStrTabSize, 0, 0, 1, 0);
|
||||
_writeSectionHeader(buf, shOff + shEntSize * 2, nameDynStr, SHT_STRTAB, SHF_ALLOC, dynStrOff, dynStrOff, dynSection.DynStrData.size(), 0, 0, 1, 0);
|
||||
_writeSectionHeader(buf, shOff + shEntSize * 3, nameDynSym, SHT_DYNSYM, SHF_ALLOC, dynSymOff, dynSymOff, dynSection.DynSymData.size(), 2, 1, 8, 24);
|
||||
_writeSectionHeader(buf, shOff + shEntSize * 4, nameDynamic, SHT_DYNAMIC, SHF_ALLOC | SHF_WRITE, dynSegOff, dynSegOff, dynSegBuf.size(), 2, 0, 8, 16);
|
||||
_writeSectionHeader(buf, shOff + shEntSize * 5, nameText, SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR, stubOff, stubOff, stubBytes.size(), 0, 0, 1, 0);
|
||||
|
||||
_writeU64(buf, 40, shOff);
|
||||
_writeU16(buf, 58, static_cast<std::uint16_t>(shEntSize));
|
||||
_writeU16(buf, 60, static_cast<std::uint16_t>(shNum));
|
||||
_writeU16(buf, 62, 1);
|
||||
_sectionHeaderTableBuilder->WriteTable(buf, sectionRequest);
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
#include <elfpatcher/filtering/SegmentFilter.hpp>
|
||||
#include <elfpatcher/domain/ElfConstants.hpp>
|
||||
|
||||
namespace Elfpatcher {
|
||||
|
||||
bool SegmentFilter::_isSceSpecificSegment(const std::uint32_t type) const {
|
||||
return type == PT_SCE_DYNLIBDATA
|
||||
|| type == PT_OS_PROCPARAM
|
||||
|| type == PT_OS_RELRO
|
||||
|| (type >= 0x61000000 && type <= 0x6fffffff);
|
||||
}
|
||||
|
||||
bool SegmentFilter::_isNullPageLoad(const Domain::ProgramHeader& ph) const {
|
||||
return ph.Type == PT_LOAD && ph.MappedAddress == 0;
|
||||
}
|
||||
|
||||
bool SegmentFilter::ShouldSkip(const Domain::ProgramHeader& ph) const {
|
||||
if (_isSceSpecificSegment(ph.Type)) return true;
|
||||
if (ph.Type == PT_DYNAMIC) return true;
|
||||
if (_isNullPageLoad(ph)) return true;
|
||||
if (ph.Type == PT_NOTE && ph.MappedAddress == 0 && ph.FileSize > 0) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,180 @@
|
||||
#include <elfpatcher/layout/ProgramHeaderLayoutBuilder.hpp>
|
||||
#include <elfpatcher/domain/ElfConstants.hpp>
|
||||
#include <domain/Types.hpp>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
|
||||
namespace Elfpatcher {
|
||||
|
||||
ProgramHeaderLayoutBuilder::ProgramHeaderLayoutBuilder(
|
||||
std::shared_ptr<ISegmentFilter> segmentFilter,
|
||||
std::shared_ptr<Io::IByteWriter> byteWriter
|
||||
)
|
||||
: _segmentFilter(std::move(segmentFilter))
|
||||
, _byteWriter(std::move(byteWriter))
|
||||
{
|
||||
}
|
||||
|
||||
void ProgramHeaderLayoutBuilder::_writeProgramHeader(std::vector<std::uint8_t>& buf, std::size_t offset, const Domain::ProgramHeader& ph) const {
|
||||
_byteWriter->WriteU32(buf, offset + 0, ph.Type);
|
||||
_byteWriter->WriteU32(buf, offset + 4, ph.Flags);
|
||||
_byteWriter->WriteU64(buf, offset + 8, ph.Offset);
|
||||
_byteWriter->WriteU64(buf, offset + 16, ph.MappedAddress);
|
||||
_byteWriter->WriteU64(buf, offset + 24, ph.PhysicalAddress);
|
||||
_byteWriter->WriteU64(buf, offset + 32, ph.FileSize);
|
||||
_byteWriter->WriteU64(buf, offset + 40, ph.MemorySize);
|
||||
_byteWriter->WriteU64(buf, offset + 48, ph.Alignment);
|
||||
}
|
||||
|
||||
Domain::ProgramHeader ProgramHeaderLayoutBuilder::_makeLoadHeader(std::uint64_t offset, std::uint64_t size) const {
|
||||
Domain::ProgramHeader ph{};
|
||||
ph.Type = PT_LOAD;
|
||||
ph.Flags = PF_R | PF_W;
|
||||
ph.Offset = offset;
|
||||
ph.MappedAddress = offset;
|
||||
ph.PhysicalAddress = offset;
|
||||
ph.FileSize = size;
|
||||
ph.MemorySize = size;
|
||||
ph.Alignment = 0x1000;
|
||||
return ph;
|
||||
}
|
||||
|
||||
Domain::ProgramHeader ProgramHeaderLayoutBuilder::_makeHeaderBlockLoad(std::uint64_t vaddr, std::uint64_t size, std::uint64_t align) const {
|
||||
Domain::ProgramHeader ph{};
|
||||
ph.Type = PT_LOAD;
|
||||
ph.Flags = PF_R;
|
||||
ph.Offset = 0;
|
||||
ph.MappedAddress = vaddr;
|
||||
ph.PhysicalAddress = vaddr;
|
||||
ph.FileSize = size;
|
||||
ph.MemorySize = size;
|
||||
ph.Alignment = align;
|
||||
return ph;
|
||||
}
|
||||
|
||||
Domain::ProgramHeader ProgramHeaderLayoutBuilder::_makePhdrHeader(std::uint64_t offset, std::uint64_t vaddr, std::uint64_t size) const {
|
||||
Domain::ProgramHeader ph{};
|
||||
ph.Type = PT_PHDR;
|
||||
ph.Flags = PF_R;
|
||||
ph.Offset = offset;
|
||||
ph.MappedAddress = vaddr;
|
||||
ph.PhysicalAddress = vaddr;
|
||||
ph.FileSize = size;
|
||||
ph.MemorySize = size;
|
||||
ph.Alignment = 8;
|
||||
return ph;
|
||||
}
|
||||
|
||||
Domain::ProgramHeader ProgramHeaderLayoutBuilder::_makeDynamicHeader(std::uint64_t offset, std::uint64_t size) const {
|
||||
Domain::ProgramHeader ph{};
|
||||
ph.Type = PT_DYNAMIC;
|
||||
ph.Flags = PF_R | PF_W;
|
||||
ph.Offset = offset;
|
||||
ph.MappedAddress = offset;
|
||||
ph.PhysicalAddress = offset;
|
||||
ph.FileSize = size;
|
||||
ph.MemorySize = size;
|
||||
ph.Alignment = 8;
|
||||
return ph;
|
||||
}
|
||||
|
||||
Domain::ProgramHeader ProgramHeaderLayoutBuilder::_makeInterpHeader(std::uint64_t offset, std::uint64_t size) const {
|
||||
Domain::ProgramHeader ph{};
|
||||
ph.Type = PT_INTERP;
|
||||
ph.Flags = PF_R;
|
||||
ph.Offset = offset;
|
||||
ph.MappedAddress = offset;
|
||||
ph.PhysicalAddress = offset;
|
||||
ph.FileSize = size;
|
||||
ph.MemorySize = size;
|
||||
ph.Alignment = 1;
|
||||
return ph;
|
||||
}
|
||||
|
||||
std::uint32_t ProgramHeaderLayoutBuilder::_fixLoadFlags(std::uint32_t originalFlags) const {
|
||||
return originalFlags | PF_R;
|
||||
}
|
||||
|
||||
std::uint16_t ProgramHeaderLayoutBuilder::WriteLayout(
|
||||
std::vector<std::uint8_t>& buf,
|
||||
const ProgramHeaderLayoutRequest& request
|
||||
) const {
|
||||
std::uint16_t keptCount = 0;
|
||||
std::uint64_t keptMinVaddr = UINT64_MAX;
|
||||
std::uint64_t keptMinVaddrAlign = 0x1000;
|
||||
for (const auto& ph : request.OriginalHeaders) {
|
||||
if (_segmentFilter->ShouldSkip(ph))
|
||||
continue;
|
||||
keptCount++;
|
||||
if (ph.Type == PT_LOAD && ph.MappedAddress < keptMinVaddr) {
|
||||
keptMinVaddr = ph.MappedAddress;
|
||||
keptMinVaddrAlign = ph.Alignment;
|
||||
}
|
||||
}
|
||||
if (keptMinVaddr == UINT64_MAX)
|
||||
throw Domain::RelinkerException("No kept PT_LOAD segment to anchor the header block against");
|
||||
if (keptMinVaddrAlign == 0)
|
||||
throw Domain::RelinkerException("Lowest kept PT_LOAD has zero alignment");
|
||||
|
||||
const std::uint16_t neededPh = keptCount + 4;
|
||||
if (neededPh > request.PhNum)
|
||||
throw Domain::RelinkerException(
|
||||
"Not enough program header slots: need " + std::to_string(neededPh) +
|
||||
", available " + std::to_string(request.PhNum));
|
||||
|
||||
const std::uint64_t headerBlockSize = request.PhOff + static_cast<std::uint64_t>(neededPh) * request.PhEntSize;
|
||||
if (headerBlockSize >= keptMinVaddr)
|
||||
throw Domain::RelinkerException(
|
||||
"Header block does not fit below the lowest kept PT_LOAD: need " +
|
||||
std::to_string(headerBlockSize) + " bytes, only " + std::to_string(keptMinVaddr) + " available");
|
||||
|
||||
const std::uint64_t headerBlockVaddr = (keptMinVaddr - headerBlockSize) & ~(keptMinVaddrAlign - 1);
|
||||
if (headerBlockVaddr + headerBlockSize > keptMinVaddr)
|
||||
throw Domain::RelinkerException("Header block would overlap the lowest kept PT_LOAD after alignment");
|
||||
|
||||
std::uint16_t writtenPh = 0;
|
||||
|
||||
const std::size_t phdrEntOff = static_cast<std::size_t>(request.PhOff) + writtenPh * request.PhEntSize;
|
||||
_writeProgramHeader(buf, phdrEntOff, _makePhdrHeader(request.PhOff, headerBlockVaddr + request.PhOff, static_cast<std::uint64_t>(neededPh) * request.PhEntSize));
|
||||
writtenPh++;
|
||||
|
||||
const std::size_t headerLoadEntOff = static_cast<std::size_t>(request.PhOff) + writtenPh * request.PhEntSize;
|
||||
_writeProgramHeader(buf, headerLoadEntOff, _makeHeaderBlockLoad(headerBlockVaddr, headerBlockSize, keptMinVaddrAlign));
|
||||
writtenPh++;
|
||||
|
||||
for (const auto& ph : request.OriginalHeaders) {
|
||||
if (_segmentFilter->ShouldSkip(ph))
|
||||
continue;
|
||||
const std::size_t phEntOff = static_cast<std::size_t>(request.PhOff) + writtenPh * request.PhEntSize;
|
||||
if (ph.Type == PT_LOAD) {
|
||||
Domain::ProgramHeader fixed = ph;
|
||||
fixed.Flags = _fixLoadFlags(ph.Flags);
|
||||
_writeProgramHeader(buf, phEntOff, fixed);
|
||||
} else {
|
||||
_writeProgramHeader(buf, phEntOff, ph);
|
||||
}
|
||||
writtenPh++;
|
||||
}
|
||||
|
||||
{
|
||||
const std::size_t phEntOff = static_cast<std::size_t>(request.PhOff) + writtenPh * request.PhEntSize;
|
||||
_writeProgramHeader(buf, phEntOff, _makeLoadHeader(request.ExtraBlockOffset, request.ExtraBlockSize));
|
||||
writtenPh++;
|
||||
}
|
||||
|
||||
{
|
||||
const std::size_t phEntOff = static_cast<std::size_t>(request.PhOff) + writtenPh * request.PhEntSize;
|
||||
_writeProgramHeader(buf, phEntOff, _makeDynamicHeader(request.DynamicSegmentOffset, request.DynamicSegmentSize));
|
||||
writtenPh++;
|
||||
}
|
||||
|
||||
{
|
||||
const std::size_t phEntOff = static_cast<std::size_t>(request.PhOff) + writtenPh * request.PhEntSize;
|
||||
_writeProgramHeader(buf, phEntOff, _makeInterpHeader(request.InterpOffset, request.InterpSize));
|
||||
writtenPh++;
|
||||
}
|
||||
|
||||
return writtenPh;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
#include <elfpatcher/sections/SectionHeaderTableBuilder.hpp>
|
||||
#include <elfpatcher/domain/ElfConstants.hpp>
|
||||
|
||||
namespace Elfpatcher {
|
||||
|
||||
SectionHeaderTableBuilder::SectionHeaderTableBuilder(std::shared_ptr<Io::IByteWriter> byteWriter)
|
||||
: _byteWriter(std::move(byteWriter))
|
||||
{
|
||||
}
|
||||
|
||||
void SectionHeaderTableBuilder::_writeSectionHeader(
|
||||
std::vector<std::uint8_t>& buf,
|
||||
const std::size_t offset,
|
||||
const std::uint32_t nameOff,
|
||||
const std::uint32_t type,
|
||||
const std::uint64_t flags,
|
||||
const std::uint64_t addr,
|
||||
const std::uint64_t fileOffset,
|
||||
const std::uint64_t size,
|
||||
const std::uint32_t link,
|
||||
const std::uint32_t info,
|
||||
const std::uint64_t align,
|
||||
const std::uint64_t entSize
|
||||
) const {
|
||||
_byteWriter->WriteU32(buf, offset + 0, nameOff);
|
||||
_byteWriter->WriteU32(buf, offset + 4, type);
|
||||
_byteWriter->WriteU64(buf, offset + 8, flags);
|
||||
_byteWriter->WriteU64(buf, offset + 16, addr);
|
||||
_byteWriter->WriteU64(buf, offset + 24, fileOffset);
|
||||
_byteWriter->WriteU64(buf, offset + 32, size);
|
||||
_byteWriter->WriteU32(buf, offset + 40, link);
|
||||
_byteWriter->WriteU32(buf, offset + 44, info);
|
||||
_byteWriter->WriteU64(buf, offset + 48, align);
|
||||
_byteWriter->WriteU64(buf, offset + 56, entSize);
|
||||
}
|
||||
|
||||
void SectionHeaderTableBuilder::WriteTable(
|
||||
std::vector<std::uint8_t>& buf,
|
||||
const SectionHeaderTableRequest& request
|
||||
) const {
|
||||
static constexpr char kShStrTab[] = "\0.shstrtab\0.dynstr\0.dynsym\0.dynamic\0.text";
|
||||
constexpr std::uint32_t nameShStrTab = 1;
|
||||
constexpr std::uint32_t nameDynStr = 11;
|
||||
constexpr std::uint32_t nameDynSym = 19;
|
||||
constexpr std::uint32_t nameDynamic = 27;
|
||||
constexpr std::uint32_t nameText = 36;
|
||||
const auto shStrTabOff = static_cast<std::uint64_t>(buf.size());
|
||||
constexpr std::uint64_t shStrTabSize = sizeof(kShStrTab);
|
||||
for (const char i : kShStrTab)
|
||||
buf.push_back(static_cast<std::uint8_t>(i));
|
||||
|
||||
const auto shOff = static_cast<std::uint64_t>(buf.size());
|
||||
constexpr std::size_t shEntSize = 64;
|
||||
constexpr std::size_t shNum = 6;
|
||||
buf.resize(buf.size() + shEntSize * shNum, 0);
|
||||
|
||||
_writeSectionHeader(buf, shOff + shEntSize * 0, 0, SHT_NULL, 0, 0, 0, 0, 0, 0, 0, 0);
|
||||
_writeSectionHeader(buf, shOff + shEntSize * 1, nameShStrTab, SHT_STRTAB, 0, shStrTabOff, shStrTabOff, shStrTabSize, 0, 0, 1, 0);
|
||||
_writeSectionHeader(buf, shOff + shEntSize * 2, nameDynStr, SHT_STRTAB, SHF_ALLOC, request.DynStrOffset, request.DynStrOffset, request.DynStrSize, 0, 0, 1, 0);
|
||||
_writeSectionHeader(buf, shOff + shEntSize * 3, nameDynSym, SHT_DYNSYM, SHF_ALLOC, request.DynSymOffset, request.DynSymOffset, request.DynSymSize, 2, 1, 8, 24);
|
||||
_writeSectionHeader(buf, shOff + shEntSize * 4, nameDynamic, SHT_DYNAMIC, SHF_ALLOC | SHF_WRITE, request.DynamicSegmentOffset, request.DynamicSegmentOffset, request.DynamicSegmentSize, 2, 0, 8, 16);
|
||||
_writeSectionHeader(buf, shOff + shEntSize * 5, nameText, SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR, request.StubOffset, request.StubOffset, request.StubSize, 0, 0, 1, 0);
|
||||
|
||||
_byteWriter->WriteU64(buf, 40, shOff);
|
||||
_byteWriter->WriteU16(buf, 58, static_cast<std::uint16_t>(shEntSize));
|
||||
_byteWriter->WriteU16(buf, 60, static_cast<std::uint16_t>(shNum));
|
||||
_byteWriter->WriteU16(buf, 62, 1);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
#include <elfpatcher/stub/EntryStubBuilder.hpp>
|
||||
|
||||
namespace Elfpatcher {
|
||||
|
||||
std::vector<std::uint8_t> EntryStubBuilder::BuildEntryStub(
|
||||
std::uint64_t stubVaddr,
|
||||
std::uint64_t realEntryVaddr
|
||||
) const {
|
||||
std::vector<std::uint8_t> s;
|
||||
s.push_back(0x58);
|
||||
s.push_back(0x48); s.push_back(0x89); s.push_back(0xe3);
|
||||
s.push_back(0x48); s.push_back(0x83); s.push_back(0xec); s.push_back(0x30);
|
||||
s.push_back(0x48); s.push_back(0x83); s.push_back(0xe4); s.push_back(0xf0);
|
||||
s.push_back(0x89); s.push_back(0x04); s.push_back(0x24);
|
||||
s.push_back(0x48); s.push_back(0x89); s.push_back(0x5c); s.push_back(0x24); s.push_back(0x08);
|
||||
s.push_back(0x48); s.push_back(0x89); s.push_back(0xe7);
|
||||
s.push_back(0x48); s.push_back(0x31); s.push_back(0xf6);
|
||||
const std::uint64_t jmpInsnVaddr = stubVaddr + s.size();
|
||||
const std::uint64_t jmpNextVaddr = jmpInsnVaddr + 5;
|
||||
const std::int32_t rel32 = static_cast<std::int32_t>(realEntryVaddr - jmpNextVaddr);
|
||||
s.push_back(0xe9);
|
||||
s.push_back(static_cast<std::uint8_t>(rel32 & 0xff));
|
||||
s.push_back(static_cast<std::uint8_t>((rel32 >> 8) & 0xff));
|
||||
s.push_back(static_cast<std::uint8_t>((rel32 >> 16) & 0xff));
|
||||
s.push_back(static_cast<std::uint8_t>((rel32 >> 24) & 0xff));
|
||||
return s;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
#ifndef IO_BYTEWRITER_HPP
|
||||
#define IO_BYTEWRITER_HPP
|
||||
|
||||
#include <io/IByteWriter.hpp>
|
||||
|
||||
namespace Io {
|
||||
|
||||
class ByteWriter : public IByteWriter {
|
||||
public:
|
||||
void WriteU16(std::vector<std::uint8_t>& buf, std::size_t offset, std::uint16_t v) const override;
|
||||
void WriteU32(std::vector<std::uint8_t>& buf, std::size_t offset, std::uint32_t v) const override;
|
||||
void WriteU64(std::vector<std::uint8_t>& buf, std::size_t offset, std::uint64_t v) const override;
|
||||
void AppendU64(std::vector<std::uint8_t>& buf, std::uint64_t v) const override;
|
||||
void AppendI64(std::vector<std::uint8_t>& buf, std::int64_t v) const override;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,22 @@
|
||||
#ifndef IO_IBYTEWRITER_HPP
|
||||
#define IO_IBYTEWRITER_HPP
|
||||
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
|
||||
namespace Io {
|
||||
|
||||
class IByteWriter {
|
||||
public:
|
||||
virtual ~IByteWriter() = default;
|
||||
|
||||
virtual void WriteU16(std::vector<std::uint8_t>& buf, std::size_t offset, std::uint16_t v) const = 0;
|
||||
virtual void WriteU32(std::vector<std::uint8_t>& buf, std::size_t offset, std::uint32_t v) const = 0;
|
||||
virtual void WriteU64(std::vector<std::uint8_t>& buf, std::size_t offset, std::uint64_t v) const = 0;
|
||||
virtual void AppendU64(std::vector<std::uint8_t>& buf, std::uint64_t v) const = 0;
|
||||
virtual void AppendI64(std::vector<std::uint8_t>& buf, std::int64_t v) const = 0;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,28 @@
|
||||
#include <io/ByteWriter.hpp>
|
||||
#include <cstring>
|
||||
|
||||
namespace Io {
|
||||
|
||||
void ByteWriter::WriteU16(std::vector<std::uint8_t>& buf, std::size_t offset, std::uint16_t v) const {
|
||||
std::memcpy(buf.data() + offset, &v, 2);
|
||||
}
|
||||
|
||||
void ByteWriter::WriteU32(std::vector<std::uint8_t>& buf, std::size_t offset, std::uint32_t v) const {
|
||||
std::memcpy(buf.data() + offset, &v, 4);
|
||||
}
|
||||
|
||||
void ByteWriter::WriteU64(std::vector<std::uint8_t>& buf, std::size_t offset, std::uint64_t v) const {
|
||||
std::memcpy(buf.data() + offset, &v, 8);
|
||||
}
|
||||
|
||||
void ByteWriter::AppendU64(std::vector<std::uint8_t>& buf, std::uint64_t v) const {
|
||||
const std::size_t pos = buf.size();
|
||||
buf.resize(pos + 8);
|
||||
std::memcpy(buf.data() + pos, &v, 8);
|
||||
}
|
||||
|
||||
void ByteWriter::AppendI64(std::vector<std::uint8_t>& buf, std::int64_t v) const {
|
||||
AppendU64(buf, static_cast<std::uint64_t>(v));
|
||||
}
|
||||
|
||||
}
|
||||
+15
-1
@@ -2,6 +2,11 @@
|
||||
#include <io/FileReader.hpp>
|
||||
#include <io/FileWriter.hpp>
|
||||
#include <elfpatcher/ElfPatcher.hpp>
|
||||
#include <elfpatcher/filtering/SegmentFilter.hpp>
|
||||
#include <elfpatcher/stub/EntryStubBuilder.hpp>
|
||||
#include <elfpatcher/layout/ProgramHeaderLayoutBuilder.hpp>
|
||||
#include <elfpatcher/sections/SectionHeaderTableBuilder.hpp>
|
||||
#include <io/ByteWriter.hpp>
|
||||
#include <relinker/parsing/ElfReader.hpp>
|
||||
#include <relinker/parsing/SceDynlibParser.hpp>
|
||||
#include <relinker/parsing/SdkRevisionProfile.hpp>
|
||||
@@ -76,7 +81,16 @@ int main(const int argc, char* argv[]) {
|
||||
fileWriter.Write(*registryPath, callRegistryWriter->WriteCallRegistry(result.RegistryEntries));
|
||||
}
|
||||
|
||||
Elfpatcher::ElfPatcher elfPatcher;
|
||||
auto byteWriter = std::make_shared<Io::ByteWriter>();
|
||||
Elfpatcher::ElfPatcher elfPatcher(
|
||||
std::make_shared<Elfpatcher::EntryStubBuilder>(),
|
||||
std::make_shared<Elfpatcher::ProgramHeaderLayoutBuilder>(
|
||||
std::make_shared<Elfpatcher::SegmentFilter>(),
|
||||
byteWriter
|
||||
),
|
||||
std::make_shared<Elfpatcher::SectionHeaderTableBuilder>(byteWriter),
|
||||
byteWriter
|
||||
);
|
||||
auto patchedElf = elfPatcher.Patch(sourceBytes, result.OriginalHeaders, result.DynamicSection);
|
||||
fileWriter.Write(outputElfPath, patchedElf);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user