How to use relative position in c / assembly?

He said that the Independent Position Code uses only a relative position instead of absolute positions, how is this implemented in c and assembly, respectively?

Take, char test[] = "string";for example, how to refer to it at a relative address?

+5
source share
3 answers

In C, position-independent code is a part of a compiler implementation. See the compiler guide to determine if it is supported and how.

. , , ( ), , .

, . , .

+2

x86 :

        call 1f
1:      popl %ebx

ebx , popl.

, PIC thunk:

load_ebx:
        movl 4(%esp),%ebx
        addl $some_offset,%ebx
        ret

, thunk ebx / ( ), ebx - ​​ .

, . pc ip .

+2

:

lea str1(pc), r0 ; load address of string relative to the pc (assuming constant strings, maybe)
st r0, test  ; save the address in test (test could also be PIC, in which case it could be relative
             ; to some register)

, . - (-PIC -S gcc) , .

0

All Articles