Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use relative position in c/assembly?

Tags:

c

assembly

fpic

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?

like image 209
mysql_go Avatar asked Oct 12 '22 09:10

mysql_go


2 Answers

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.

like image 158
Potatoswatter Avatar answered Oct 15 '22 10:10

Potatoswatter


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.

like image 22
R.. GitHub STOP HELPING ICE Avatar answered Oct 15 '22 10:10

R.. GitHub STOP HELPING ICE