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 ELF relocation section .rel.dyn not parsed correctly and resulting in invalid symbols #795

Open
martonilles opened this issue Sep 17, 2022 · 1 comment
Assignees

Comments

@martonilles
Copy link

Describe the bug
I have the following test ELF file:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

const int NUMBER = 42;
const char VERSION[] = "11.22";
const char *VERSION2 = "22.33";


void function1 (const char* str) {
   char buffer[16];
   strcpy(buffer, str);
}

int main(int argc, char *argv[]) {
   printf("Hello %s %s\n", VERSION, VERSION2);

   exit(0);
}

Which is compiled to MIPS platform:

test-mips-64bit-le-lib: test.c
        mips64el-linux-gnuabi64-gcc-10 -o $@ -shared $^

test-mips-64bit-le-lib.zip

This has the following relocation section:

Relocation section '.rel.dyn' at offset 0x818 contains 3 entries:
    Offset             Info             Type               Symbol's Value  Symbol's Name
0000000000000000  0000000000000000 R_MIPS_NONE           
                    Type2: R_MIPS_NONE      
                    Type3: R_MIPS_NONE      
0000000000011000  0000000000001203 R_MIPS_REL32          
                    Type2: R_MIPS_64        
                    Type3: R_MIPS_NONE      
00000000000110d0  0000000000001203 R_MIPS_REL32          
                    Type2: R_MIPS_64        
                    Type3: R_MIPS_NONE      

However when parsing these relocations in max_relocation_index: idx = std::max(idx, static_cast<uint32_t>(reloc_entry->r_info >> shift));

This results in an idx of 51511296

And therefore:

    - Number of symbols counted: 51511297
    - Table Offset:              0x530
    - String Table Offset:       0x6b0

So it try to read 51511297 symbols, which is incorrect and results in a parsing error till EOF is reached. (Also many Symbol's name #16 is empty! like logs).

There are also relocation parsing error logs: Unable to find the symbol associated with the relocation (idx: 51511296) 11000 MIPS_NONE 0 0 3120000 DYNAMIC

Not sure exactly how to fix this, elf-utils also has hard time parsing it:

Relocation section [10] '.rel.dyn' at offset 0x818 contains 3 entries:
  Offset              Type                 Value               Name
  000000000000000000  <INVALID RELOC>      000000000000000000  
  0x0000000000011000  <INVALID RELOC>      <INVALID SYMBOL 51511296>
  0x00000000000110d0  <INVALID RELOC>      <INVALID SYMBOL 51511296>

I think when parsing reloc_entry->r_info we should consider R_MIPS_REL32 types in a special way.

@romainthomas
Copy link
Member

Yes, need to fix/improve the MIPS support

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants