0

Though some posts exist about the -fPIC effect (see this SO post for example) most of them are unclear. I wrote an extremely simple example and I still can't figure out what happens w/o -fPIC regarding the location of global variables. Here is my C file:

$ cat main.c
#include <stdio.h>
int var1 = 94;
int var2 = 76;

int main(int argc, char **argv)
{
    int var1Loc = (int) &var1;
    int var2Loc = (int) &var2;
    printf("var1 address is: %d\n",var1Loc);
    printf("var2 address is: %d\n",var2Loc);
    printf("diff         is: %d\n",var2Loc-var1Loc);

    return var1+var2;
}

Then I compile it with -fPIC and run it twice:

$ gcc -Wno-pointer-to-int-cast -O0 -g -o main main.c
$ ./main
var1 address is: -2019672048
var2 address is: -2019672044
diff         is: 4
$ ./main
var1 address is: 1441697808
var2 address is: 1441697812
diff         is: 4

When I do the exact same thing without -fPIC I get similar results. I thought that without the -fPIC the addresses should be identical across runs no?

OrenIshShalom
  • 5,974
  • 9
  • 37
  • 87
  • 1
    ASLR — Address Space Layout Randomization – Jonathan Leffler Feb 05 '19 at 17:17
  • I thought ASLR is only for the stack memory – OrenIshShalom Feb 05 '19 at 17:19
  • OK — maybe it isn't ASLR. Not that I've heard that ASLR only applies to stack memory — if you'd said "only shared library addresses" I'd be less surprised. You'll have to wait for someone else with an alternative idea. Sorry I can't help more. – Jonathan Leffler Feb 05 '19 at 17:21
  • 2
    Wikipedia on [ASLR](https://en.wikipedia.org/wiki/Address_space_layout_randomization) — mentions _"ASLR randomly arranges the address space positions of key data areas of a process, including the base of the executable and the positions of the stack, heap and libraries."_ – Jonathan Leffler Feb 05 '19 at 17:28
  • 1
    I used `echo 0 | sudo tee /proc/sys/kernel/randomize_va_space` to turn ASLR off and indeed the addresses are the same across runs. Either with or without `-fPIC`. – OrenIshShalom Feb 05 '19 at 17:34

1 Answers1

2

In an acronym (or initialism): ASLR — Address Space Layout Randomization.

Wikipedia on ASLR says:

ASLR randomly arranges the address space positions of key data areas of a process, including the base of the executable and the positions of the stack, heap and libraries.

This describes what you're seeing. You subsequently found that using:

 echo 0 | sudo tee /proc/sys/kernel/randomize_va_space

to turn ASLR off meant that the addresses are the same across runs, either with or without -fPIC, thus confirming that ASLR was the cause of the address changes.

Note that your address printing should probably use the %p format and void * arguments (though you have a potentially truncating data conversion when you apply the (int) cast to the addresses — but the format is correct to use %d). The type modifier t is for ptrdiff_t, the difference between two pointers.

#include <stdio.h>

int var1 = 94;
int var2 = 76;

int main(void)
{
    void *var1Loc = &var1;
    void *var2Loc = &var2;
    printf("var1 address is: %p\n", var1Loc);
    printf("var2 address is: %p\n", var2Loc);
    printf("diff         is: %td\n", &var2 - &var1);

    return 0;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278