1

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

4

2 回答 2

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.

于 2013-03-30T21:27:35.860 回答
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 :)

于 2013-03-30T21:31:15.007 回答