Click here to Skip to main content
15,889,651 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Is it possible to assign memory to a variable at a specified location using the linker directive file in GCC?

e.g.

In default.ld

SECTION
{
  MY_MEMORY 0xF100000 LENGTH(4) :
}

In test.c

int main()
{
  *((unsigned int*)0xF100000) = 0x12345678;
  return 0;
}
Posted
Comments
R. Erasmus 4-Jan-11 8:49am    
Any brainiacs online?

1 solution

This can only be done when working with a microcontroller or without an OS. In that case you also don't need to declare that memory because you are in complete control. You would just need to remember not to use that memory for anything else so your alloc implementation should somehow know about it. When working in an environment like windows you could for example set a base address for a dll you use and derive the data address from that. There is of course a problem when requested that two dll's should load at the same base address. It is therefor only a hint to the OS that is often granted but not guaranteed. The same would be true for your own section. It would always be relative from the start of where you are loaded into memory. This also is true for methods you call in dll's. The method address is derived from the method address table and the address where the dll is loaded into memory. The x86 assembly has addressing modes using base, index, scale, displacement that are very suited for this.

Well, all in all it comes down to the fact that this isn't possible or something you would want to do if it could. It is clearly bad practice and abandoned in the pre protected-mode era.

This link might also be helpful:
http://www.gotw.ca/gotw/009.htm[^]

Good luck!
 
Share this answer
 
Comments
R. Erasmus 5-Jan-11 2:16am    
I've been told that you can define a section area at a specific location in the linker directive file and then attach a variable that section (something in that line in any case)... just need to have a look how to implement it though. My problem I have is that I'm testing some software written for a powerpc micro processor... I use GCC to speed up the process whereafter I use Green Hills MULTI (powerpc) to run my completed test. Some tests takes only 30 seconds to run in GCC whereas in GHS can take up to an hour. The software at times writes/reads to/from specific memory addresses which is linked to hardware components. I need to define these addresses for GCC to prevent memory violations.
E.F. Nijboer 5-Jan-11 4:18am    
Maybe this link can help out:
http://www.programmersheaven.com/mb/CandCPP/326685/326702/re-set-a-pointer-to-a-certain-memory-address/

Haven't got much time right now but will have another look if needed. I would assume that it must be possible to simply point to the address and silence any compiler messages about violations.
E.F. Nijboer 6-Jan-11 8:07am    
This link will help you definitely:
http://stackoverflow.com/questions/2417195/whats-the-shortest-code-to-write-directly-to-a-memory-address-in-c-c

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900