Skip to content
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

MIPS relocations are not paired #122

Open
Mr-Wiseguy opened this issue Oct 11, 2024 · 0 comments
Open

MIPS relocations are not paired #122

Mr-Wiseguy opened this issue Oct 11, 2024 · 0 comments

Comments

@Mr-Wiseguy
Copy link

Mr-Wiseguy commented Oct 11, 2024

According to the MIPS System V ABI (https://refspecs.linuxfoundation.org/elf/mipsabi.pdf, pages 4-17 and 4-18), HI16 and LO16 relocations must be paired to determine the full addend for a given relocation of either type. This is important to show the correct addend on a HI16 reloc, and is also needed to show the correct addend on the LO16 when the HI16's addend is nonzero. It's also necessary for correctly identifying symbols for compilers that emit section-relative relocations instead of references to symbols. Here's an excerpt of a relocation pairing algorithm, where relocations is a list of relocations in the exact original order they show up in the elf's reloc section:

bool prev_hi = false;
int32_t hi_immediate = 0;
for (size_t i = 0; i < relocations.size(); i++) {
    Relocation& reloc = relocations[i];
    
    // Calculate addends for LO16 relocs.
    if (reloc.type == R_MIPS_LO16) {
        int32_t cur_immediate = (int16_t)(reloc.word & 0xFFFF);
        int32_t full_addend = hi_immediate + cur_immediate;
        if (prev_hi) {
            // Paired LO16 relocs (use the paired HI16's immediate and update the HI16's addend)
            reloc.addend = full_addend;
            relocations[i - 1].addend = full_addend;            
        } else {
            // Orphaned LO16 relocs (use the most recent HI16's immediate)
            reloc.addend = full_addend;
        }
    } else {
        if (prev_hi) {
            // Invalid elf, HI16 followed by a relocation that isn't a LO16    
        }
    }
    
    // Track HI16 relocs.
    if (reloc.type == R_MIPS_HI16) {
        prev_hi = true;
        hi_immediate = (reloc.word & 0xFFFF) << 16;
    } else {
        prev_hi = false;
    }
}
@Mr-Wiseguy Mr-Wiseguy changed the title MIPS Relocations are not paired MIPS relocations are not paired Oct 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant