Search code examples
cgccassemblypowerpc

Compiling PowerPC binary with gcc and restrict useable registers


I have a PowerPC device running a software and I'd like to modify this software by inserting some own code parts.

I can easily write my own assembler code, put it somewhere in an unused region in RAM, replace any instruction in the "official" code by b 0x80001234 where 0x80001234 is the RAM address where my own code extension is loaded.

However, when I compile a C code with powerpc-eabi-gcc, gcc assumes it compiles a complete program and not only "code parts" to be inserted into a running program.

This leads to a problem: The main program uses some of the CPUs registers to store data, and when I just copy my extension into it, it will mess with the previous contents.

For example, if the main program I want to insert code into uses register 5 and register 8 in that code block, the program will crash if my own code writes to r5 or r8. Then I need to convert the compiled binary back to assembler code, edit the appropriate registers to use registers other than r5 and r8 and then compile that ASM source again.

Waht I'm now searching for is an option to the ppc-gcc which tells it "never ever use the PPC registers r5 and r8 while creating the bytecode".

Is this possible or do I need to continue crawling through the ASM code on my own replacing all the "used" registers with other registers?


Solution

  • You should think of another approach to solve this problem.

    There is a gcc extension to reserve a register as a global variable:

    register int *foo asm ("r12");
    

    Please note that if you use this extension, your program does no longer confirm to the ABI of the operating system you are working on. This means that you cannot call any library functions without risking program crashes, overwritten variables, or crashes.