What does this assembly statement mean?

2019-02-27 09:34发布

问题:

I am looking at the assembly code generated by gcc by using the -s flag. Some statements look like the following.

movl    is_leader(%rip), destination

Here, is_leader is a globally defined variable of type int in the C code. What I don't understand is the term is_leader(%rip) here. Isn't rip the instruction pointer? I need to know how is this statement used to access is_leader.

回答1:

It asks the assembler to generate code that adds or subtracts the difference between the address of the current instruction and the address of the object to the instruction pointer.

This gives the address of the object without generating an absolute address (and generally, the offset fits into 16 or 32 bits, so the resulting code is also shorter and thus faster).

This adds the constraint that the distance between both items remains constant, so this can be used only for data in the same loadable object; the linker will flag an error if that condition is not met.



回答2:

Perhaps related to position independent code, inside a *.so executable.