Global variables, shared libraries and -fPIC effect

前端 未结 2 1334
被撕碎了的回忆
被撕碎了的回忆 2020-12-17 17:16

I made a piece of code which consists in a dynamic library (lib.c), and a main executable (main.c). In both files I define a global variable named:

相关标签:
2条回答
  • 2020-12-17 17:24

    When you compile with -fPIC the object in question will determine the address of global symbols using the Global Offset Table. What happens though when part of the code is -fPIC and part isn't is that one of your int globals will be using this table to determine the address whilst the other part isn't.

    If you had two shared object linked with -fPIC, but your main program not then you would still have two addresses for int global, one using the global offset table and one which was just local to the non-PIC code.

    There's a really great discussion on PIC vs pic vs non PIC if you want to read further.

    0 讨论(0)
  • 2020-12-17 17:25

    By default, when building an executable references to variables are done internally, with a fixed offset and no relocation.

    However, you're passing -fPIC and access to global data are converted to access via GOT and GOT relocations are added.

    0 讨论(0)
提交回复
热议问题