Refactoring, fix external function pointers.

This commit is contained in:
Christian Speckner 2024-07-10 08:35:47 +02:00
parent 5a7a72c745
commit a606d294a4
3 changed files with 14 additions and 13 deletions

View File

@ -71,7 +71,8 @@ namespace {
constexpr uInt32 ADDR_DATA_ODR = 0xf0000008;
constexpr uInt32 ADDR_DATA_MODER = 0xf0000010;
constexpr uInt32 ADDR_VCS_JSR6 = 0x1000;
constexpr uInt32 VCSLIB_BASE = 0x1001;
constexpr uInt32 ADDR_VCS_JSR6 = VCSLIB_BASE;
const vector<ElfLinker::ExternalSymbol> EXTERNAL_SYMBOLS = {
{"ADDR_IDR", ADDR_ADDR_IDR},
@ -127,7 +128,7 @@ namespace {
cout
<< sections[i].name
<< " @ 0x"<< std::setw(8) << (relocatedSections[i]->offset +
(relocatedSections[i]->type == ElfLinker::SectionType::text ? ADDR_TEXT_BASE : ADDR_DATA_BASE)
(relocatedSections[i]->segment == ElfLinker::SegmentType::text ? ADDR_TEXT_BASE : ADDR_DATA_BASE)
)
<< " size 0x" << std::setw(8) << sections[i].size << std::endl;
}
@ -144,8 +145,8 @@ namespace {
<< symbols[i].name
<< " = 0x" << std::setw(8) << relocatedSymbols[i]->value;
if (relocatedSymbols[i]->section) {
cout << (*relocatedSymbols[i]->section == ElfLinker::SectionType::text ? " (text)" : " (data)");
if (relocatedSymbols[i]->segment) {
cout << (*relocatedSymbols[i]->segment == ElfLinker::SegmentType::text ? " (text)" : " (data)");
} else {
cout << " (abs)";
}

View File

@ -47,7 +47,7 @@ void ElfLinker::link(const vector<ExternalSymbol>& externalSymbols)
if (segmentSize % section.align)
segmentSize = (segmentSize / section.align + 1) * section.align;
myRelocatedSections[i] = {isText ? SectionType::text : SectionType::data, segmentSize};
myRelocatedSections[i] = {isText ? SegmentType::text : SegmentType::data, segmentSize};
segmentSize += section.size;
}
}
@ -60,7 +60,7 @@ void ElfLinker::link(const vector<ExternalSymbol>& externalSymbols)
if (myDataSize % section.align)
myDataSize = (myDataSize / section.align + 1) * section.align;
myRelocatedSections[i] = {SectionType::data, myDataSize};
myRelocatedSections[i] = {SegmentType::data, myDataSize};
myDataSize += section.size;
}
}
@ -119,11 +119,11 @@ void ElfLinker::link(const vector<ExternalSymbol>& externalSymbols)
const auto& relocatedSection = myRelocatedSections[symbol.section];
if (!relocatedSection) continue;
uInt32 value = relocatedSection->type == SectionType::text ? myTextBase : myDataBase;
uInt32 value = relocatedSection->segment == SegmentType::text ? myTextBase : myDataBase;
value += relocatedSection->offset;
if (symbol.type != ElfParser::STT_SECTION) value += symbol.value;
myRelocatedSymbols[i] = {relocatedSection->type, value};
myRelocatedSymbols[i] = {relocatedSection->segment, value};
}
// apply relocations
@ -220,7 +220,7 @@ void ElfLinker::applyRelocation(const ElfParser::Relocation& relocation, size_t
);
uInt8* target =
(targetSectionRelocated.type == SectionType::text ? myTextData : myDataData).get() +
(targetSectionRelocated.segment == SegmentType::text ? myTextData : myDataData).get() +
targetSectionRelocated.offset + relocation.offset;
switch (relocation.type) {
@ -239,7 +239,7 @@ void ElfLinker::applyRelocation(const ElfParser::Relocation& relocation, size_t
const uInt32 op = read32(target);
Int32 offset = relocatedSymbol->value + relocation.addend.value_or(elfUtil::decode_B_BL(op)) -
(targetSectionRelocated.type == SectionType::text ? myTextBase : myDataBase) -
(targetSectionRelocated.segment == SegmentType::text ? myTextBase : myDataBase) -
targetSectionRelocated.offset - relocation.offset - 4;
if ((offset >> 24) != -1 && (offset >> 24) != 0)

View File

@ -57,15 +57,15 @@ class ElfLinker {
const string myReason;
};
enum class SectionType: uInt8 { text, data };
enum class SegmentType: uInt8 { text, data };
struct RelocatedSection {
SectionType type;
SegmentType segment;
uInt32 offset;
};
struct RelocatedSymbol {
optional<SectionType> section;
optional<SegmentType> segment;
uInt32 value;
};