So given this code:
Code: Select all
int var=0;
void _init()
{
var++;
}
// gcc -shared
00000000000000e9 <_init>:
e9: 55 push %rbp
ea: 48 89 e5 mov %rsp,%rbp
ed: 48 8b 05 1c 10 00 00 mov 0x101c(%rip),%rax # 1110 <_DYNAMIC+0x100>
f4: 8b 00 mov (%rax),%eax
f6: 8d 50 01 lea 0x1(%rax),%edx
f9: 48 8b 05 10 10 00 00 mov 0x1010(%rip),%rax # 1110 <_DYNAMIC+0x100>
100: 89 10 mov %edx,(%rax)
102: 90 nop
103: 5d pop %rbp
104: c3 retq
nm -D test.so
0000000000002000 A _edata
0000000000001130 D _end
00000000000000e9 T _init
0000000000001000 D var
Code: Select all
.section .text
asmfunc:
movq var, %rax
/usr/bin/ld: aaa.o: relocation R_X86_64_32S against symbol `var' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: Nonrepresentable section on output
Code: Select all
int var=0;
void _init()
{
var++;
}
// gcc -shared -fvisibility=hidden
00000000000000e9 <_init>:
e9: 55 push %rbp
ea: 48 89 e5 mov %rsp,%rbp
ed: 8b 05 0d 0f 00 00 mov 0xf0d(%rip),%eax # 1000 <var>
f3: 83 c0 01 add $0x1,%eax
f6: 89 05 04 0f 00 00 mov %eax,0xf04(%rip) # 1000 <var>
fc: 90 nop
fd: 5d pop %rbp
fe: c3 retq
nm -D test.so
0000000000002000 A _edata
00000000000010e0 D _end
Code: Select all
int var=0;
void __attribute__ ((__visibility__("default"))) _init()
{
var++;
}
// gcc -shared -fvisibility=hidden
00000000000000e9 <_init>:
e9: 55 push %rbp
ea: 48 89 e5 mov %rsp,%rbp
ed: 8b 05 0d 0f 00 00 mov 0xf0d(%rip),%eax # 1000 <var>
f3: 83 c0 01 add $0x1,%eax
f6: 89 05 04 0f 00 00 mov %eax,0xf04(%rip) # 1000 <var>
fc: 90 nop
fd: 5d pop %rbp
fe: c3 retq
nm -D test.so
0000000000002000 A _edata
00000000000010e0 D _end
00000000000000e9 T _init
Code: Select all
.section .text
asmfunc:
movq var, %rax
/usr/bin/ld: aaa.o: relocation R_X86_64_32S against hidden symbol `var' can not be used when making a shared object
/usr/bin/ld: final link failed: Nonrepresentable section on output
I've tried all of the directives ".hidden", ".internal" etc., neither helped. I've also tried to compile without -fvisibility=hidden and adding attributes "hidden", "internal" to "var" without luck.
My question is, given a C internal variable (local, hidden whatever you call it), which is rip-relatively referenced from C, how to access that from asm?