gcccompilationmipscode-generationaddressing

Compiling MIPS to use branch instead of jump


Having the following very simple c program:

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

int main()
{
        char *buffer = (char*)malloc(20);
}

And Compiling it with mips-linux-gnu-gcc, it looks like the call is compiled to the following instructions:

.text:004007EC 24 04 00 14             li      $a0, 0x14
.text:004007F0 8F 82 80 50             la      $v0, malloc      # Load Address
.text:004007F4 00 40 C8 25             move    $t9, $v0
.text:004007F8 03 20 F8 09             jalr    $t9 ; malloc     # Jump And Link Register
.text:004007FC 00 00 00 00             nop

The full command line of the compilation is:

mips-linux-gnu-gcc my_malloc.c -o my_malloc.so

However, I would like the function calls to compile to just normal branch instructions:

jal     malloc
li      $a0, 0x14

Does someone know how to achieve this result?


Solution

  • You need to tell the compiler to use the PLT for call, using the -mplt option. This requires support for the PLT in the rest of the toolchain.