Questions tagged [position-independent-code]
99 questions
138
votes
2 answers
What is the -fPIE option for position-independent executables in gcc and ld?
How will it change the code, e.g. function calls?

osgx
- 90,338
- 53
- 357
- 513
51
votes
2 answers
Why does GCC create a shared object instead of an executable binary according to file?
I have a library I am building. All of my objects compile and link successively when I run either one of:
ar rcs lib/libryftts.a $^
gcc -shared $^ -o lib/libryftts.so
in my Makefile. I also am able to successfully install them into…

Luke Smith
- 781
- 3
- 7
- 15
21
votes
1 answer
What are the semantics of ADRP and ADRL instructions in ARM assembly?
ADRP
Address of 4KB page at a PC-relative offset.
ADRL
Load a PC-relative address into a register. It is similar to the ADR
instruction. ADRL can load a wider range of addresses than ADR because
it generates two data processing…

sherlock
- 2,397
- 3
- 27
- 44
9
votes
1 answer
STM32, Position independent code - function pointers not in GOT?
I need a position independent code (PIC) working on STM32F401. But i have problem with pointers to functions used e.g. in struct.
Short example:
struct process {
struct process *next;
const char *name;
PT_THREAD((* thread)(struct pt *,…

Robert Sedláček
- 99
- 5
8
votes
2 answers
How can two processes share the same Shared Library?
I've been trying to get a better grasp of how shared libraries work but I just can't rap my head around two things.
1- Each process has its own virtual memory space and page table, so If a shared library gets loaded into one process virtual memory…

GamefanA
- 1,555
- 2
- 16
- 23
8
votes
2 answers
How do I force gcc to call a function directly in PIC code?
Consider the following function:
extern void test1(void);
extern void test2(void) {
test1();
}
This is the code gcc generates without -fpic on amd64 Linux:
test2:
jmp test1
When I compile with -fpic , gcc explicitly calls through the PLT…

fuz
- 88,405
- 25
- 200
- 352
7
votes
1 answer
Why I cannot compile with -fPIE but can with -fPIC?
I have one interesting compilation problem.
At first, please see code to be compiled.
$ ls
Makefile main.c sub.c sub.h
$ gcc -v
...
gcc version 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC)
## Makefile
%.o: CFLAGS+=-fPIE #[2]
main.so: main.o sub.o
…

nutsman
- 331
- 2
- 10
6
votes
2 answers
How to configure gcc to use -no-pie by default?
I want to compile the following program on Linux:
.global _start
.text
_start:
mov $1, %rax
mov $1, %rdi
mov $msg, %rsi
mov $13, %rdx
syscall
mov $60, %rax
xor %rdi, %rdi
syscall
msg:
.ascii "Hello…

Aadit M Shah
- 72,912
- 30
- 168
- 299
6
votes
3 answers
How can I tell if jump is absolute or relative?
I'm studying for a test in assembly and in the subject of "Position-Independent-Code" I find the difference between a relative jump and an absolute jump confusing.
How can I tell what kind of jump it is?
I understand what a relative jump is (the…

lolu
- 370
- 4
- 20
5
votes
2 answers
How to find load relocation for a PIE binary?
I need to get base address of stack inside my running process. This would enable me to print raw stacktraces that will be understood by addr2line (running binary is stripped, but addr2line has access to symbols).
I managed to do this by examining…

MateuszL
- 2,751
- 25
- 38
5
votes
1 answer
How is the address of the text section of a PIE executable determined in Linux?
First I tried to reverse engineer it a bit:
printf '
#include
int main() {
puts("hello world");
}
' > main.c
gcc -std=c99 -pie -fpie -ggdb3 -o pie main.c
echo 2 | sudo tee /proc/sys/kernel/randomize_va_space
readelf -s ./pie | grep -E…

Ciro Santilli OurBigBook.com
- 347,512
- 102
- 1,199
- 985
5
votes
1 answer
Why does a Program Compiled with -fpic and -pie Have Relocation Table?
If a trivial program is compiled with the following command:
arm-none-eabi-gcc -shared -fpic -pie --specs=nosys.specs simple.c -o simple.exe
and the relocation entries are printed with the command:
arm-none-eabi-readelf simple.exe -r
There are a…

PeterM
- 2,372
- 1
- 26
- 35
4
votes
0 answers
How can a shared object contain static thread local storage?
Background
I've encountered a problem that violates my conceptual model of position independent code and thread local storage. The problem that prompted this can be found in this StackOverflow post; I have a binary, which in turn dlopen's a shared…

OMGtechy
- 7,935
- 8
- 48
- 83
4
votes
1 answer
RISCV - How are jump instructions PC-relative?
In the RISC-V Unpriviliged spec V20191213, the following is stated, (page 21)
The unconditional jump instructions all use PC-relative addressing to help support position-independent
code.
Looking at the definition of the JALR instruction,
The…

zeke
- 155
- 3
- 8
4
votes
0 answers
Disable PIC for link in CMake
I have a C++ project in CMake (32-bit shared library) and gcc compiler. I set POSITION_INDEPENDENT_CODE property to OFF for my project, so it removes -fPIC only for source compiling, but not for link stage too. How can I disable it for link?
I know…

Artem
- 91
- 8