Skip to content

Commit 006eed5

Browse files
authored
Merge pull request #731 from paulsapps/main
Fix R_MIPS_GPREL16 by setting the correction section attributes.
2 parents 582c9e2 + 7d5c33c commit 006eed5

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

tools/psyq-obj-parser/psyq-obj-parser.cc

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
#include "support/slice.h"
3030
#include "support/windowswrapper.h"
3131

32+
#define SHF_MIPS_GPREL 0x10000000
33+
3234
#define vprint(...) \
3335
if (verbose) fmt::print(__VA_ARGS__)
3436

@@ -400,22 +402,33 @@ std::unique_ptr<PsyqLnkFile> PsyqLnkFile::parse(PCSX::File* file, bool verbose)
400402
uint16_t sectionIndex = file->read<uint16_t>();
401403
uint32_t size = file->read<uint32_t>();
402404
std::string name = readPsyqString(file);
403-
vprint("Uninitialized: id {}, section {}, size {:08x}, name {}\n", symbolIndex, sectionIndex, size,
404-
name);
405+
405406
Symbol* symbol = new Symbol();
406407
symbol->symbolType = Symbol::Type::UNINITIALIZED;
407408
symbol->sectionIndex = sectionIndex;
408409
symbol->size = size;
409410
symbol->name = name;
410411
auto section = ret->sections.find(sectionIndex);
411412
if (section == ret->sections.end()) {
412-
fmt::print("Section {} not found.\n", sectionIndex);
413+
fmt::print("Section {} not found for {}.\n", sectionIndex, name);
413414
return nullptr;
414415
}
415-
auto align = section->alignment - 1;
416+
417+
// Each entry is aligned to the size of the type after testing the output of mixed .bss and sbss with psyq GCC 2.7.2
418+
// this works the same way as modern GCC.
419+
auto sizeToUse = symbol->size;
420+
if (sizeToUse > section->alignment) {
421+
sizeToUse = section->alignment;
422+
}
423+
auto align = sizeToUse - 1;
416424
section->uninitializedOffset += align;
417425
section->uninitializedOffset &= ~align;
418426
symbol->offset = section->uninitializedOffset;
427+
428+
vprint("Uninitialized: id {}, section {}, offset {:08x}, size {:08x}, name {}\n", symbolIndex,
429+
sectionIndex, symbol->offset, size,
430+
name);
431+
419432
section->uninitializedOffset += size;
420433
ret->symbols.insert(symbolIndex, symbol);
421434
break;
@@ -829,8 +842,9 @@ bool PsyqLnkFile::Section::generateElfSection(PsyqLnkFile* psyq, ELFIO::elfio& w
829842
if (getFullSize() == 0) return true;
830843
fmt::print(" :: Generating section {}\n", name);
831844
static const std::map<std::string, ELFIO::Elf_Xword> flagsMap = {
832-
{".text", SHF_ALLOC | SHF_EXECINSTR}, {".rdata", SHF_ALLOC}, {".data", SHF_ALLOC | SHF_WRITE},
833-
{".sdata", SHF_ALLOC | SHF_WRITE}, {".bss", SHF_ALLOC | SHF_WRITE}, {".sbss", SHF_ALLOC | SHF_WRITE},
845+
{".text", SHF_ALLOC | SHF_EXECINSTR}, {".rdata", SHF_ALLOC},
846+
{".data", SHF_ALLOC | SHF_WRITE}, {".sdata", SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL},
847+
{".bss", SHF_ALLOC | SHF_WRITE}, {".sbss", SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL},
834848
};
835849
auto flags = flagsMap.find(name);
836850
if (flags == flagsMap.end()) {

0 commit comments

Comments
 (0)