1

Is there a way to keep the offsets of all functions and variables as they were, everytime I'm compiling the program?

HamZa
  • 14,671
  • 11
  • 54
  • 75
Peter
  • 15
  • 4
  • Are you sure it changes with every run of the compiler or with each run of the program? In the latter case it might be a kernel security measure changing address spaces with every run of a program. Very inconvenient for a software developer indeed. – Bryan Olivier Mar 30 '13 at 21:26
  • Would would be the benefit of this? Even if a given function or object happens to have the same address from one compilation of the program to the next, I can't think of any reasonable way to take advantage of that. – Keith Thompson Mar 30 '13 at 21:59
  • @KeithThompson some embedded devices and gaming consoles need stuff at specific addresses. – m0skit0 Mar 31 '13 at 01:19
  • @m0skit0: Then you'd need to use some (probably compiler-specific) mechanism to map those things to those specific addresses, which would be stable from one compilation to the next. That doesn't seem to be what the OP is asking about. – Keith Thompson Mar 31 '13 at 01:34
  • @KeithThompson AFAIK there's no compiler specific for that, e.g. Sony uses `gcc` to compile for PlayStation models. What we did to copy this was to use a linker script and be careful... – m0skit0 Mar 31 '13 at 11:59

2 Answers2

4

No. This is not a limitation of the compiler per se, but a "logical" limitation. Imagine you have a box that is full of stuff. Now you want to add stuff on box A but you don't want a new box. Well, you can't, it's a physical limitation.

Or talking more "computerish":

Function a() occupies from address 0 to 0xA00 (size 0xA00)

Function b() occupies from address 0xA01 to 0xB00 (size 0x100)

Now you modify a() so that's it's bigger, let's say its size is now 0xB00. How would you keep both a() and b() in the same address? You can't unless you do some nasty tricks like splitting the function a() into 2 parts, but I think this is not what you want.

This without considering that modern OS have ASLR and other security methods.

m0skit0
  • 25,268
  • 11
  • 79
  • 127
  • 1
    That said, you could write a linker script that specifies the memory address of each function. (Which can be problematic if future changes to a function makes it overlap with another function) – nos Mar 30 '13 at 22:28
  • Yes, sure, you can specify the linker where to put stuff (I actually did so before in console development, for example), but you have to be aware of the above situation :) – m0skit0 Mar 31 '13 at 01:18
2

Yes and no. No because what m0skit0 says. Yes becayse you can tell the linker to set certain symbols at specified addresses - at least I know that's possible with GCC's ports for various embedded targets. It's relatively frequent to see code that uses this technique for instance to load firmware from a module that's loaded later or something like that.

The answers to this question tells you some ways to do it. This is very non-portable though, but I'm sure you understand all the pitfalls of doing this :)

Community
  • 1
  • 1
Morten Jensen
  • 5,818
  • 3
  • 43
  • 55