链接器指令文件-GCC [英] Linker Directive File - GCC

查看:62
本文介绍了链接器指令文件-GCC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用GCC中的链接器指令文件将内存分配给指定位置的变量?

例如

在default.ld

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) :
}


在test.c


In test.c

int main()
{
  *((unsigned int*)0xF100000) = 0x12345678;
  return 0;
}

推荐答案

只有在使用微控制器或没有OS时,才能完成此操作.在这种情况下,您也不需要声明该内存,因为您可以完全控制.您只需要记住不要在其他任何地方使用该内存,因此您的alloc实现应该以某种方式知道它.当在Windows之类的环境中工作时,您可以例如为所使用的dll设置一个基址,并从中获取数据地址.当然,当要求两个dll应该在相同的基址上加载时,会有一个问题.因此,它只是通常被授予但不能保证的OS提示.您自己的部分也是如此.从您加载到内存的起点开始,它始终是相对的.您在dll中调用的方法也是如此.方法地址是从方法地址表和dll加载到内存中的地址派生的. x86程序集具有非常适合此用途的寻址模式,其中包括基数,索引,比例,位移.

好吧,总的来说,这归结为以下事实:这是不可能的,或者如果可能的话,您将想做的事情.这显然是一种不好的做法,在预保护模式时代已被放弃.

该链接也可能会有所帮助:
http://www.gotw.ca/gotw/009.htm [
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!


这篇关于链接器指令文件-GCC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆