<div dir="ltr"><div><div>Hello,<br></div>I've been working on powerpc and I'm having some trouble since there's no PC register. What I would like to do is to be able to set PC to an arbitrary address that I want. I know that the only way to do this is to branch.<br><br></div>I've written the following code to be able to jump to the beginning of foo. I need to be able to use both immediate values and a register for introducing the address. I compiled on power8 with<br>clang -O3 power_pc_write.c -o power_pc_write<br><br>The code is working.<br clear="all"><div><div><div><br>#include <stdio.h><br>#include <stdint.h><br><br>void* foo(){<br><br> void* addr =  __builtin_return_address(0);<br><br>  printf("in foo addr: %p\n", addr);<br><br>  return addr;<br>}<br><br>int main(){<br><br>  void* addr = foo();<br>  void* foo_addr = foo;<br>  printf("foo addr: %p\n", foo_addr);<br><br>  // This is for setting PC_IMM<br>  asm volatile("bl %0" : : "i" (foo) );<br>  asm volatile("bcl 20,31,%0" : : "i" (foo) );<br><br>  // This is for setting PC_REG<br>  asm volatile("mflr 4; mtlr %0 ; bclrl 20,31"  : : "r" (foo_addr): "r4" );<br><br>  return 0;<br>}<br><br></div><div>My concerns are as follows:<br></div><div>I'm not sure if there's a specific way of setting PC to an arbitrary address other than branching. Am I doing this the right way?<br></div><div>I realized that bl instruction jumps to foo+8. But bcl does not. I couldn't figure out why.<br></div><div>The ISA specification says that for gettin the the next instruction we can use this:<br>  bcl 20,31,$+4<br></div><div>  So I tried bcl first. But this is a conditional branch instruction. It's not really clear to me why I need to use a conditional branch here.<br></div><div>  Also I assumed that $ give me some sort of address. But I wasn't able to use "$" in another way. Could you provide me hints on this?<br></div><div><br></div><div>I appreciate if you can help me clear these in my mind.<br><br></div><div><b>P.S. I'm using llvm 3.7. The ISA specification I'm currently workign with is PowerISA Version 3.0 B.</b><br></div><div><br>Thanks!<br></div><div>-- <br><div class="gmail_signature"><div dir="ltr">Buse</div></div>
</div></div></div></div>