The environment is Solaris on 32bit SPARC, but I think this is a more general issue with dynamic linking and/or position independent code.
I have an assembly program that I compile as position independent code and dynamically link to it from a C program. It works fine, except that I can't refer to any memory reserved by the assembly program from the assembly program. Jumping within the assembly program works fine.
I just want to read and write to memory within the assembly program, but any time I try I get segmentation fault.
I wrote this test program to debug this issue
.section ".data"
.global foo
foo: .word 1
.section ".text"
.global testprog
testprog:
save %sp, -(92+4), %sp
sethi %hi(foo), %o0 ! set foo, %o0
or %o0, %lo(foo), %o0
call print_int
nop
ret
restore
I compile this to with
as -K PIC -b
and dlopen the resulting .so in C
dlhandle = dlopen(obj_file, RTLD_NOW)
dl_testprog = dlsym(dlhandle, "testprog")
when I call dl_testprog()
, it prints "4". It also prints "4" if I try to print the address of testprog or print_int. Jumping to a label and everything else works just fine. Looking at the disassembly, foo is replaced with 0x0, like it should.
Do I have to go thru _GLOBAL_OFFSET_TABLE_
or something, to be able to write to my own memory within the assembly program? If so, how do I do this? Everything I tried resulted in a segfault, and I havn't been able to find a very good guide how to do this (which leads me to believe that you are not supposed to do it. Isn't this the linkers problem anyway?).