The variability of pointers of variables :)

This is a discussion on The variability of pointers of variables :) within the C Programming forums, part of the General Programming Boards category; Hey there, it bugs me that I am not experienced enough with asm to clearly point this out. If I ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    68

    The variability of pointers of variables :)

    Hey there, it bugs me that I am not experienced enough with asm to clearly point this out.

    If I use an IDE and let the compiler compile some C code where I also declare variables, will the pointers to these change if I run the program on different computers/at different times? (Speaking of not explicitly changing the destination, like it does in flash based applications)

    Well I don't think so, because I wouldn't be able to "hack" the same PE games on several computers without getting new pointers.

    Does this also apply for different OS? If yes, where is the hardcode, where is the source? How is the base of new variables declared? How does the compiler choose this base - or - is it being handled by the OS?

    Yeah, thanks in advance for any help or a reference

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,273
    Quote Originally Posted by Hawkin View Post
    Hey there, it bugs me that I am not experienced enough with asm to clearly point this out.

    If I use an IDE and let the compiler compile some C code where I also declare variables, will the pointers to these change if I run the program on different computers/at different times? (Speaking of not explicitly changing the destination, like it does in flash based applications)
    Quite possible. It could be different from run to run on the same machine, even. Typically data in global space is assigned specific addresses, and the segment MUST load at that address or everything is wrong. But for shared library (DLL) code, global data may be placed anywhere.

    If the code is compiled with PC-relative data addressing, then it is immune to shifting of the image in memory. But in that case, pointers will not be embedded throughout the code -- instead, indexes are used into a global table which adjusts the base of each offset. Or, the PC is dynamically queried when necessary and used to adjust the offset only when a data member is actually accessed. On Intel chips, most PC-relative code assigns a register specifically to this purpose.

    Does this also apply for different OS? If yes, where is the hardcode, where is the source? How is the base of new variables declared? How does the compiler choose this base - or - is it being handled by the OS?
    The linker usually chooses the base, not the compiler.

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    68
    Quote Originally Posted by brewbuck View Post
    Quite possible. It could be different from run to run on the same machine, even. Typically data in global space is assigned specific addresses, and the segment MUST load at that address or everything is wrong. But for shared library (DLL) code, global data may be placed anywhere.
    Maybe I can't follow that correctly, but what is 'global data' in a DLL? (Usually you save named functions in a DLL and you can gain a pointer to a function with just the name.. so what is global data or: what's non global data?!)


    'PC-relative data addressing' - can I determine whether my compiler uses such a method? I am using gnu which comes with DevC++ IDE and I can only tell that on several machines the pointers to variables stay the same, meaning they got to use the same base. But how can I find the base as a number in the PE? Is it hardcoded in the header? Elsewhere? Not even in the Executable? Not even hardcoded? What I want to know is, how to actually find these bases.
    Thanks for your answer and sorry for the long delay.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The base address of your executable is in the executable in itself. DLL's are shared between multiple applications, and as such has to be stuck at an address that is available to ALL applications that share them - this in turn means that the DLL may be "relocated" [which means that it's changing it's base address]. By using PC relative addressing, ALL the contents of the DLL itself is "relative to the program counter", which means that the code itself doesn't have any "fixed addresses" in the code. This, I believe, is not used in Windows, but it is used in Linux. This avoids an expensive run through all the the code in the DLL that refer to something within the DLL and changing those addresses when there is a need to relocate the DLL.

    With GCC you can use -fPIC to make the code "PC relative" [PIC here stands for "position independent code"].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers and variables
    By sailor_girl in forum C Programming
    Replies: 7
    Last Post: 01-08-2009, 09:58 AM
  2. Best way to avoid using global variables
    By Canadian0469 in forum C++ Programming
    Replies: 7
    Last Post: 12-18-2008, 12:02 PM
  3. Replies: 5
    Last Post: 12-09-2008, 02:18 PM
  4. Replies: 2
    Last Post: 11-28-2003, 11:50 AM
  5. Passing variables by pointers...what am I doing wrong?
    By Shadow12345 in forum C++ Programming
    Replies: 2
    Last Post: 06-07-2002, 03:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21