mysql_go
mysql_go

Reputation: 2427

How to use relative position in c/assembly?

It's said Position Independent Code only uses relative position instead of absolute positions, how's this implemented in c and assembly respectively?

Let's take char test[] = "string"; as an example, how to reference it by relative address?

Upvotes: 5

Views: 2462

Answers (3)

R.. GitHub STOP HELPING ICE
R.. GitHub STOP HELPING ICE

Reputation: 215447

On x86, position-independent code in principle looks like this:

        call 1f
1:      popl %ebx

followed by use of ebx as a base pointer with a displacement equal to the distance between the data to be accessed and the address of the popl instruction.

In reality it's often more complicated, and typically a tiny thunk function might be used to load the PIC register like this:

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

where the offset is chosen such that, when the thunk returns, ebx contains a pointer to a designated special point in the program/library (usually the start of the global offset table), and then all subsequent ebx-relative accesses can simply use the distance between the desired data and the designated special point as the offset.

On other archs everything is similar in principle, but there may be easier ways to load the program counter. Many simply let you use the pc or ip register as an ordinary register in relative addressing modes.

Upvotes: 2

Potatoswatter
Potatoswatter

Reputation: 137890

In C, position-independent code is a detail of the compiler's implementation. See your compiler manual to determine whether it is supported and how.

In assembly, position-independent code is a detail of the instruction set architecture. See your CPU manual to find out how to read the PC (program counter) register, how efficient that is, and what the recommended best practices are in translating a code address to a data address.

Position-relative data is less popular now that code and data are separated into different pages on most modern operating systems. It is a good way to implement self-contained executable modules, but the most common such things nowadays are viruses.

Upvotes: 2

Richard Pennington
Richard Pennington

Reputation: 19975

In pseudo code it could look like:

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)

A lot depends on your compiler and CPU architecture, as the previous answer stated. One way to find out would be to compile with the appropriate flags (-PIC -S for gcc) and look at the assembly language you get.

Upvotes: 0

Related Questions