Skip to content

Support gp relocations for Psy-Q AS 2.5x #1888

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 12, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 41 additions & 5 deletions tools/psyq-obj-parser/psyq-obj-parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@

enum class PsyqRelocType : uint8_t {
REL32_BE = 8,
GPREL16_BE = 12,
REL32 = 16,
GPREL16_LE = 30,
REL26 = 74,
HI16 = 82,
LO16 = 84,
Expand Down Expand Up @@ -339,6 +341,14 @@
vprint("(GPREL16), ");
break;
}
case (uint8_t)PsyqRelocType::GPREL16_LE: {
vprint("(GPREL16 LE), ");
break;
}
case (uint8_t)PsyqRelocType::GPREL16_BE: {
vprint("(GPREL16 BE), ");
break;
}

Check warning on line 351 in tools/psyq-obj-parser/psyq-obj-parser.cc

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

❌ Getting worse: Complex Method

PsyqLnkFile::parse increases in cyclomatic complexity from 55 to 57, threshold = 9. This function has many conditional statements (e.g. if, for, while), leading to lower code health. Avoid adding more conditionals and code to it without refactoring.
case (uint8_t)PsyqRelocType::HI16_BE: {
vprint("(HI16 BE), ");
break;
Expand Down Expand Up @@ -764,10 +774,11 @@

void PsyqLnkFile::Relocation::display(PsyqLnkFile* lnk, PsyqLnkFile::Section* sec) {
static const std::map<PsyqRelocType, std::string> typeStr = {
{PsyqRelocType::REL32, "REL32"}, {PsyqRelocType::REL26, "REL26"},
{PsyqRelocType::HI16, "HI16"}, {PsyqRelocType::LO16, "LO16"},
{PsyqRelocType::GPREL16, "GPREL16"}, {PsyqRelocType::REL32_BE, "REL32 BE"},
{PsyqRelocType::REL26_BE, "REL26 BE"}, {PsyqRelocType::HI16_BE, "HI16 BE"},
{PsyqRelocType::REL32, "REL32"}, {PsyqRelocType::REL26, "REL26"},
{PsyqRelocType::HI16, "HI16"}, {PsyqRelocType::LO16, "LO16"},
{PsyqRelocType::GPREL16, "GPREL16"}, {PsyqRelocType::GPREL16_LE, "GPREL16 LE"},
{PsyqRelocType::GPREL16_BE, "GPREL16 BE"}, {PsyqRelocType::REL32_BE, "REL32 BE"},
{PsyqRelocType::REL26_BE, "REL26 BE"}, {PsyqRelocType::HI16_BE, "HI16 BE"},
{PsyqRelocType::LO16_BE, "LO16 BE"},
};
fmt::print(" {:8} {:>12}::{:08x} ", typeStr.find(type)->second, sec->name, offset);
Expand Down Expand Up @@ -994,6 +1005,8 @@
{PsyqRelocType::HI16, elf_mips_reloc_type::R_MIPS_HI16},
{PsyqRelocType::LO16, elf_mips_reloc_type::R_MIPS_LO16},
{PsyqRelocType::GPREL16, elf_mips_reloc_type::R_MIPS_GPREL16},
{PsyqRelocType::GPREL16_LE, elf_mips_reloc_type::R_MIPS_GPREL16},
{PsyqRelocType::GPREL16_BE, elf_mips_reloc_type::R_MIPS_GPREL16},
{PsyqRelocType::REL26_BE, elf_mips_reloc_type::R_MIPS_26},
{PsyqRelocType::HI16_BE, elf_mips_reloc_type::R_MIPS_HI16},
{PsyqRelocType::LO16_BE, elf_mips_reloc_type::R_MIPS_LO16},
Expand Down Expand Up @@ -1182,7 +1195,7 @@
}
elfSym = symbol->elfSym;
}
if (type == PsyqRelocType::HI16_BE || type == PsyqRelocType::LO16_BE) {
if (type == PsyqRelocType::HI16_BE || type == PsyqRelocType::LO16_BE || type == PsyqRelocType::GPREL16_BE) {

Check warning on line 1198 in tools/psyq-obj-parser/psyq-obj-parser.cc

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

❌ New issue: Complex Conditional

PsyqLnkFile::Relocation::generateElf has 1 complex conditionals with 2 branches, threshold = 2. A complex conditional is an expression inside a branch (e.g. if, for, while) which consists of multiple, logical operators such as AND/OR. The more logical operators in an expression, the more severe the code smell.
offset -= 0x2;
}
auto elfType = typeMap.find(type);
Expand Down Expand Up @@ -1230,6 +1243,18 @@
sectionData[offset + 1] = 0;
break;
}
case PsyqRelocType::GPREL16_LE: {
uint16_t lo = symbolOffset & 0xFFFF;
sectionData[offset + 0] = (uint8_t)(lo >> 0);
sectionData[offset + 1] = (uint8_t)(lo >> 8);
break;
}
case PsyqRelocType::GPREL16_BE: {
uint16_t lo = symbolOffset & 0xFFFF;
sectionData[offset + 3] = (uint8_t)(lo >> 0);
sectionData[offset + 2] = (uint8_t)(lo >> 8);
break;
}
case PsyqRelocType::REL32_BE: {
sectionData[offset + 3] = (uint8_t)(symbolOffset >> 0x00);
sectionData[offset + 2] = (uint8_t)(symbolOffset >> 0x08);
Expand Down Expand Up @@ -1300,6 +1325,7 @@
case PsyqExprOpcode::SECTION_BASE: {
return localSymbolReloc(expr->sectionIndex, 0);
}
case PsyqExprOpcode::SECTION_START:
case PsyqExprOpcode::SYMBOL: {
if (pass == ElfRelocationPass::PASS1) {
skipped.skipped = true;
Expand Down Expand Up @@ -1425,6 +1451,16 @@
} else {
return check(expression->left.get(), -((int32_t)expression->right->value));
}
} else if (expression->right->type == PsyqExprOpcode::SECTION_START) {
// Why
if (expression->left->type == PsyqExprOpcode::ADD) {
if (expression->left->left->type == PsyqExprOpcode::VALUE) {
return check(expression->left->right.get(),
expression->left->left->value - expression->right->value);
}
} else {
return checkZero(expression->left.get());
}

Check warning on line 1463 in tools/psyq-obj-parser/psyq-obj-parser.cc

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

❌ Getting worse: Complex Method

PsyqLnkFile::Relocation::generateElf increases in cyclomatic complexity from 55 to 62, threshold = 9. This function has many conditional statements (e.g. if, for, while), leading to lower code health. Avoid adding more conditionals and code to it without refactoring.
} else {
psyq->setElfConversionError("Unsupported SUB operation in relocation");
return false;
Expand Down
Loading