How can I statically tell Visual C++ to place a global variable at a given absolute address in memory, like what __attribute__((at(address)))
does?

- 205,094
- 128
- 528
- 886
-
Wouldn't that normally be the linker's job? I'm afraid I'm not really a VC++ expert. – Carl Norum Feb 09 '13 at 04:00
-
I had the [same question](http://stackoverflow.com/q/1554774/186834) a while back. – Chris Feb 09 '13 at 04:04
-
@CarlNorum: Yes, sometimes... and sometimes there are `#pragma`s. If there is a linker option to do it then that works too, I didn't mean to restrict it to the compiler specifically (although pragmas would be preferred). – user541686 Feb 09 '13 at 04:05
-
1@Chris, that's not the same question. – Carl Norum Feb 09 '13 at 04:05
-
@CarlNorum: How is it not? – Chris Feb 09 '13 at 04:06
-
@Cheersandhth.-Alf: It's for an application which needs a global variable to be statically allocated at a specific absolute address in memory. – user541686 Feb 09 '13 at 04:07
-
@Chris: Your solution requires the memory to be manually allocated at runtime. This question is about how to tell the compiler to map the variable to a given position at *compile* time. – user541686 Feb 09 '13 at 04:08
-
@CarlNorman: Oh, I see, you're right, it's different. Sorry. – Chris Feb 09 '13 at 04:08
-
2@Chris, Mehrdad wants to place a variable at a specific build-time-defined address, not put it at a system-provided address at runtime. – Carl Norum Feb 09 '13 at 04:08
-
@Mehrdad: so, what you're going to use that variable for? – Cheers and hth. - Alf Feb 09 '13 at 04:41
-
@Cheersandhth.-Alf: For the rest of my program that uses it, obviously? I'm not going to explain the functionality of my program here... – user541686 Feb 09 '13 at 04:50
-
The OS these days uses randomization of the program memory location, so you'll have to find a way to disable it or there's no way this will work. And I have to question the wisdom of it in the first place on anything that isn't an embedded system. – Mark Ransom Feb 09 '13 at 05:42
-
@MarkRansom: Yes, I'm aware of the implications/caveats. – user541686 Feb 09 '13 at 05:53
-
You should have mentioned those implications/caveats in the question to eliminate some of the second guessing. – Mark Ransom Feb 09 '13 at 05:57
-
`#define myvar (*(int*)0x12345678)` – Raymond Chen Feb 09 '13 at 06:05
-
@RaymondChen: `#define` does not create a variable... if somehow you didn't know that already. – user541686 Feb 09 '13 at 06:06
-
It acts like a variable, which is the important thing. – Raymond Chen Feb 09 '13 at 06:10
-
@RaymondChen: Uhm, no, it doesn't. I need it to exist in the binary and in memory, not just in the source code, so it's useless for that purpose (as I'm sure you already know). Did you really think I didn't know about `#define` when I posted this question?! – user541686 Feb 09 '13 at 06:10
-
@MarkRansom: I don't try to post questions defensively, and I don't think I should need to; I post a question and hope for an answer. It's a Q/A site after all. I generally assume that people like you with so much rep/experience here know what you're talking about, and I hope for the same in return. I think StackOverflow would be a *much* better place if people could trust each other and be able to just post a *question* and get an *answer*, instead of spending half an hour listing arguments and counterarguments just to please the devils' advocates, etc. who might be reading it. – user541686 Feb 09 '13 at 06:28
-
@Mehrdad I don't know what you mean by "exist in the binary" since the point of an absolute variable is that doesn't exist in the binary; it's at an absolute address. And it has an address: 0x12345678. Do you mean "it doesn't exist in a way the debugger understands"? – Raymond Chen Feb 09 '13 at 13:20
-
@RaymondChen: I mean that another translation unit cannot reference it via `extern`, it's not an accessible symbol in the binary. – user541686 Feb 09 '13 at 19:36
-
Oh, okay, you need the link-time resolution behavior of an absolute symbol. I don't think there's a way to do this from VC++ but you can always create a tiny .asm file with an absolute symbol in it and add it to your project. (The reason why you need to list arguments and counterarguments is that people are trying to *solve your program* instead of merely *answering your question*. You still haven't said what your problem is; all you've said is "I need an absolute symbol.") – Raymond Chen Feb 10 '13 at 15:46
-
@RaymondChen: Ah okay, I'll see if I can try assembler then. (Regarding the problem/question thing: At *some point* you have to *stop* second-guessing everyone and realize, *sometimes people know what they're talking about*. Note that I *specifically* mentioned `__attribute__((at(address)))` to show that my question is completely reasonable. So unless you think what I'm asking for is nonsense, you should have some sort of faith in me that I know what I'm talking about. No one likes being doubted all the time, especially with an obviously unhelpful response like "use `#define`".) – user541686 Feb 10 '13 at 19:17
-
The #define approach is a good enough approximation to __attribute__((at(address))) 99% of the time. It's very rare that absolute addresses need to be resolved at link time rather than compile time. Another approximation is with an extern reference to an address. int& myvar = *(int*)0x12345678: This may end up generating the same code as an absolute symbol after optimization. Since you didn't describe your problem, I do not know whether these approximations are sufficient but it sounds like you would prefer that I not offer them. I will bear that in mind with your future questions. – Raymond Chen Feb 10 '13 at 19:57
1 Answers
It can be done but I don't believe there is a predefined way to do it so it will take some experimentation. Even though I don't see much benefit if you create your variable at run time just at the start of user code execution.
So first specify the section/segment where to init your variable using the allocate MS specific specifier. Then either start your application in real scenario, dump it or debug it and see where your variable appears. Watch for relocations (there is some ways to try to enforce no relocation but they are not guaranteed to be honored all the time). Another way is to use some code in your app like this one to find the address of the section you defined.
If you for some reason cannot get a consistent behavior you can use this utility to manipulate the virtual address of your object file. All in all except hurdles along the way but overall I don't see why you wouldn't be able to get it to work for your specific scenario if you are persistent enough.

- 1
- 1

- 498
- 2
- 7
-
So if I can make the linker place a given section into a specific location in memory, then the rest is easy. (As you said, just use `allocate` to create a new section for the variable.) So I guess the question is if we can force the linker to place a specific section somewhere. – user541686 Feb 09 '13 at 05:32
-
I cannot find a way to specify this explicitly (change the virtual address of object file that is). What can be done I guess is write a small utility or script to manipulate the object file before the link step ... but I think relocation can still cause a problem when it gets loaded in memory. I don't know if there is a sound solution to this which would work all the time for all scenarios. There may be but if it's possible I'd do it at runtime it's super simple but I guess you have some special requirements which don't allow you to do it at runtime. – user1416420 Feb 09 '13 at 06:01
-
Yeah I can't do it at runtime because dynamic memory allocation when I need this variable isn't an option unfortunately. :\ I'll look into your answer, thanks for the suggestions. +1 – user541686 Feb 09 '13 at 06:04
-
Where does the EDITBIN manual mention the ability to manipulate the virtual address of a section? https://msdn.microsoft.com/en-us/library/ddxkx21d.aspx doesn't mention this... – PatrickvL Feb 27 '17 at 10:46