position-independent-code

How to find load relocation for a PIE binary?

六月ゝ 毕业季﹏ 提交于 2021-02-05 05:00:04
问题 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 elf header of argv[0] : I read entry point and substract it from &_start : #include <stdio.h> #include <execinfo.h> #include <unistd.h> #include <elf.h> #include <stdio.h> #include <string.h> void* entry_point = NULL; void* base_addr = NULL; extern

How to find load relocation for a PIE binary?

时间秒杀一切 提交于 2021-02-05 04:59:00
问题 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 elf header of argv[0] : I read entry point and substract it from &_start : #include <stdio.h> #include <execinfo.h> #include <unistd.h> #include <elf.h> #include <stdio.h> #include <string.h> void* entry_point = NULL; void* base_addr = NULL; extern

Why does gcc generates strange code without flag -fno-pie?

眉间皱痕 提交于 2020-05-09 06:35:09
问题 I am trying to compile dummy function in gcc with flag -fno-pie and without. void dummy_test_entrypoint() { } When i compile without the flag. gcc -m32 -ffreestanding -c test.c -o test.o I get the following disassembled code. 00000000 <dummy_test_entrypoint>: 0: 55 push ebp 1: 89 e5 mov ebp,esp 3: e8 fc ff ff ff call 4 <dummy_test_entrypoint+0x4> 8: 05 01 00 00 00 add eax,0x1 d: 90 nop e: 5d pop ebp f: c3 ret When i compile with the flag. 00000000 <dummy_test_entrypoint>: 0: 55 push ebp 1: 89

Why does gcc generates strange code without flag -fno-pie?

蹲街弑〆低调 提交于 2020-05-09 06:35:01
问题 I am trying to compile dummy function in gcc with flag -fno-pie and without. void dummy_test_entrypoint() { } When i compile without the flag. gcc -m32 -ffreestanding -c test.c -o test.o I get the following disassembled code. 00000000 <dummy_test_entrypoint>: 0: 55 push ebp 1: 89 e5 mov ebp,esp 3: e8 fc ff ff ff call 4 <dummy_test_entrypoint+0x4> 8: 05 01 00 00 00 add eax,0x1 d: 90 nop e: 5d pop ebp f: c3 ret When i compile with the flag. 00000000 <dummy_test_entrypoint>: 0: 55 push ebp 1: 89

How to configure gcc to use -no-pie by default?

落花浮王杯 提交于 2019-12-23 19:40:35
问题 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 World!\n" However, it gives me the following linker error: $ gcc -nostdlib hello.s /usr/bin/ld: /tmp/ccMNQrOF.o: relocation R_X86_64_32S against `.text' can not be used when making a shared object; recompile with -fPIC /usr/bin/ld: final link failed: Nonrepresentable section on output collect2:

Compile a kernel module as a position independant executable

爱⌒轻易说出口 提交于 2019-12-11 17:18:32
问题 For a PoC (context), I’m trying to build a kernel module as a position independent executable. Currently, I compile my module using mcmodel=small -fpie -mno-red-zone -mnosse to my Makefile ( /lib/modules/$(uname -r)fixed/build/Makefile ) and then I resolve my symbols by parsing /proc/kallsyms and patching my binary using ld ’s option --defsym symbol=address But this is not satisfactory. I get a rip-relative addressing but no got/plt. Below an example of function in the generated module before

Tell if a shared library was compiled with position independent code

萝らか妹 提交于 2019-12-11 10:23:20
问题 Is there a way to tell whether an OSX shared library ( .dylib ) was compiled as position independent code/executable? 回答1: According to this answer you can run otool -hv <file_path> and look for a PIE flag. Edit: I tested this and it does not work. Edit 2: Actually it does work on executables. I'm not sure it works on shared libraries. 来源: https://stackoverflow.com/questions/48622448/tell-if-a-shared-library-was-compiled-with-position-independent-code

How do I force gcc to call a function directly in PIC code?

£可爱£侵袭症+ 提交于 2019-12-04 22:15:03
问题 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 to enable symbol interposition: test2: jmp test1@PLT This however is not strictly needed for position independent code and could be left out if I don't want to support. If necessary, the linker rewrites the jump target to the PLT symbol anyway. How can I,

How do I force gcc to call a function directly in PIC code?

岁酱吖の 提交于 2019-12-03 16:11:27
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 to enable symbol interposition: test2: jmp test1@PLT This however is not strictly needed for position independent code and could be left out if I don't want to support. If necessary, the linker rewrites the jump target to the PLT symbol anyway. How can I, without changing the source code and without making the compiled code unsuitable for a shared library,