I'm new to x86 assembly and using NASM in Ubuntu 18.04(64-bit). I tried to access C++ global (count in below code) variable from NASM as below.
C++ code: (stack.cpp)
#include <iostream>
extern "C" {
int sample_(int a, int b);
unsigned int count = 200;
}
int main()
{
int a = 2, b = 3;
int ret = sample_(a, b);
std::cout << ret << std::endl;
return 0;
}
NASM code: (test.asm)
extern count
global sample_
section .text
sample_:
mov eax,ecx
add eax,edx
add eax,[count]
ret
and I'm compiling it as below.
nasm -felf64 test.asm && g++ stack.cpp test.o && ./a.out
But it doesn't compile and throw an error.
/usr/bin/ld: test.o: relocation R_X86_64_32S against symbol `count'
can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: final link failed: Nonrepresentable section on output
collect2: error: ld returned 1 exit status