Minimal reproducible example:
foo.s:
global foo
section .data
foo: db "bar", 0
main.c:
#include <stdio.h>
extern char *foo;
int main() {
puts(foo);
}
Where I’m running these commands on a 64-bit Ubuntu computer:
nasm -f elf64 foo.s
gcc main.c foo.o
./a.out
But this prints “Segmentation fault (core dumped)”.
I’ve tried a mix of -fPIC
and -no-pie
, but haven’t been able to get rid of the runtime segmentation fault with the combinations I’ve tried.
Running nm foo.o
prints 0000000000000000 D foo
, which seems correct, as the capitalized D means global data according to nm’s man page.
By compiling with -g
I can step into main() and run p foo
to see that gdb states that foo
its address is just wrong:
$1 = 0x726162 <error: Cannot access memory at address 0x726162>