When working on LLVM, I found this problem https://github.com/llvm/llvm-project/issues/64974. Maybe it's time for us to reconsider the way of getting GOT address for PIC code.
1.Background[1]: All of the accessing of global variables and normal function calls needs help from GOT. So normally, the first 3 instructions of a function are to compute the address of GOT. Normally like: lui $gp,%hi(_gp_disp) addiu $gp,$gp,%lo(_gp_disp) addu $gp,$gp,$t9 These 3 instructions load the value of _gp_disp, which is a link-time constant value, and add them with $t9, which normally contains the address of the current function. So, why $t9? The reason is that the pre-R6 MIPS lacks instructions to get the PC value. Thus, the ABI defines that, the caller should call functions with jr/jarl $t9. So the callee can get the address of itself by $t9. 2. What's my proposal? 2.1 For MIPSr6, its has instructions to get the current PC value, so we can use them, which can gain performance improvement. 2.2 For pre-R6, in fact, it can get the value of PC with NAL instruction [2], while the performance will be some worse. For the worst case, the decreasing of performance will be 20%-40% My plan for pre-R6, is to add a non-default option to use NAL to get the address of GOT. Some software, like Linux kernel (for Kalsr support) will need it. Any suggestions? [1] https://refspecs.linuxfoundation.org/elf/mipsabi.pdf [2] https://s3-eu-west-1.amazonaws.com/downloads-mips/documents/MD00087-2B-MIPS64BIS-AFP-6.06.pdf Page 404 -- YunQiang Su