I'm using -nostartfiles
flag (GCC) so my main function is :
void _start() {}
Now I want to use dlopen()
function to open a .so
lib ... So I decide to link the libdl.so
too (to access dlopen()
and ...) ... In fact, my CMake is like this:
cmake_minimum_required(VERSION 3.0)
project(myproject C)
set(CMAKE_C_STANDARD 11)
add_executable(myproject start.c)
set(CMAKE_C_FLAGS "-O3 -mavx2 -nostartfiles")
target_link_libraries(myproject
"libdl.so"
"libc.so")
and this is my start.c
void * dlopen(const char * filename, int flag);
void _start() {
void * lib = dlopen("mylib.so", RTLD_LAZY);
if(!lib) {}
// sys_exit(0)
asm volatile("syscall" :: "a" (60), "D" (0) : "rcx", "r11");
}
As you see, I just used dlopen()
function and when I run this program, I get 139 as exit-code and there is a problem with dlopen()
function … There is a problem inside _dl_relocate_object
function on movaps %xmm0,-0x70(%rbp)
... segmentation fault
But when I remove -nostartfiles
and I change void _start() {}
to int main() {}
, everything is just fine ... How to fix this problem with -nostartfiles
? What is the problem !!?
Attention, the "mylib.so"
library MUST exist otherwise you get no error on void _start() {}
UPDATE I test it in Assembly (FASM) and worked successfully without requiring any initialization !!!!!
format ELF64 EXECUTABLE 3
entry start
include 'import64.inc'
interpreter '/lib64/ld-linux-x86-64.so.2'
needed 'libdl.so'
import dlopen,dlsym
segment readable executable
start:
mov rdi, dl_filename
mov esi, 1
call [dlopen]
mov rdi, rax
mov rsi, dl_function_name
call [dlsym]
call rax
mov eax, 60
xor edi, edi
syscall
hlt
segment readable
dl_filename db 'libuntitled.so', 0x00
dl_function_name db 'hello', 0x00