Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does 'BL' arm instruction disassembly work?

'bl' or branch with link instruction is almost always becomes 0xebfffffe

However, the processor and GNU binutils objdump somehow know where to branch:

00000000 <init_module>:
   0:   e1a0c00d        mov     ip, sp
   4:   e92ddff0        push    {r4, r5, r6, r7, r8, r9, sl, fp, ip, lr, pc}
   8:   e24cb004        sub     fp, ip, #4
   c:   e24dd038        sub     sp, sp, #56     ; 0x38
  10:   ebfffffe        bl      0 <init_module>
  14:   e59f0640        ldr     r0, [pc, #1600] ; 65c <init_module+0x65c>
  18:   ebfffffe        bl      74 <init_module+0x74>

How do they know?

like image 435
yanychar Avatar asked Feb 21 '13 13:02

yanychar


People also ask

What does the BL instruction do?

The BL instruction causes a branch to label , and copies the address of the next instruction into LR ( R14 , the link register).

How does bl work in ARM?

The BL instruction copies the address of the next instruction into r14 (lr, the link register), and causes a branch to label . Machine-level B and BL instructions have a range of ±32Mb from the address of the current instruction. However, you can use these instructions even if label is out of range.

What does bl mean in assembly?

Branch Link (BL) performs a similar operation, but it copies the address of the next instruction into R14, the link register (LR). This works great when performing subroutine/procedure calls, because as soon as the section of code at the label is finished we can use the LR to get back to where we branched.


1 Answers

The issue is caused by the fact that you're looking at the disassembly of an object file, not final executable or shared object.

When assembler is producing the object file, the final address of the bl target is not fixed yet (it depends on the other object files that will be linked with it). So the assembler sets the address to 0 but also adds a relocation that tells the linker where this bl is supposed to go in the final file. (You can see the relocation info in objdump by adding the -r switch.)

When linking, the linker processes the relocation, calculates the final address of the target function and patches the instruction so that the target address lines up. If you disassemble the final, linked executable, you will see a different opcode.

like image 125
Igor Skochinsky Avatar answered Oct 14 '22 04:10

Igor Skochinsky