15.2.13 Function_replacement_prologue Function Attribute

The function_replacement_prologue attribute allows the application to redirect one function to another implementation at runtime without replacing the existing function. This is achieved by changing the method of invoking functions through a function replacement table instead of from a linker-resolved address. Initially the function address in the table points to the location of the entry point of the original function's prologue. The application can then replace the table entry with the new function address. Now, during the program execution, the control will pass to the new function address and returns to the caller function. This feature adds a new function_replacement_prologue function attribute. To redirect the function, modify the corresponding Function Replacement Table entry at runtime.

Example C code:

int a, b, c, d;
int __attribute__((function_replacement_prologue)) foo (void)
{
   a = b + c;
   return (a);
}
int main()
{
   d = foo();
   return 0;
}

Example generated assembly code:

# Function Replacement Table entries, located in data memory
   .section .fixtable, data
fixtable.foo:
   .word     cont.foo         # By default, populate the table with the address
                              # of the original implementation. Redirect to
                              # another implementation by overwriting this
                              # location with the address of the new implementation.

   .section .text, code
   .globl foo
   .ent foo
   .type foo, @function
foo:
   # Begin Function Replacement Table Prologue
   lui $25,%hi(fixtable.foo)        # Load address from .fixtable above
   lw $25,%lo(fixtable.foo)($25)
   j $25                            # Jump to address loaded from table
   nop
cont.foo:
   # End Function Replacement Table Prologue
   addiu $sp,$sp,-8
   sw $fp,4($sp)
   move $fp,$sp
   lw $3,%gp_rel(b)($28)
   ...........
   j $31
   nop