I have the following short application (The original is taken from: https://www.aldeid.com/wiki/X86-assembly/Instructions/str) I just modified it (tried to...) to make it compilable on linux too.
#include <stdio.h>
unsigned char mema[4] = {0, 0, 0, 0};
void test4 (void)
{
asm (
"str mema\n"
);
printf ("\n[+] Test 4: STR\n");
printf ("STR base: 0x%02x%02x%02x%02x\n", mema[0], mema[1], mema[2], mema[3]);
if ((mema[0] == 0x00) && (mema[1] == 0x40))
printf ("Result : VMware detected\n\n");
else
printf ("Result : Native OS\n\n");
}
int main () {
test4();
}
and when I try to compile it:
$ gcc -ggdb -O0 -fPIC -x c ./pill.c -o pill
it gives me the wird error:
/usr/bin/ld: /tmp/ccDhGo05.o: relocation R_X86_64_32S against symbol `mema' 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
However the same thing works on an online compiler: https://gcc.godbolt.org/z/RYWjy8
... except if I move the variable mema
to be a local variable in the function test4
when it complains both places about
undefined symbol `mema'
And another weirdity: same stuff works when compiled with clang.
Any idea why this happens? (Not considering the required privilege level for STR
, and other thingies, I am just interested in the linking error and why does it work on one system / compiler and not on another.