How to write this ld script ?

隐身守侯 提交于 2019-12-11 20:45:06

问题


If I want to link all .o files except z.o .All .o (no z.o) is relocated at 0xC0000000 while z.o is at 0xFFFF0000 but z.o is located at file offset 0x8000.

So.how to write this ld script ?

Here is my loader.lds

SECTIONS { 
    loader      0x00000000 : { start.o loader.o }
    kloader     0x30100000 : AT(4096) { loaderk.o ../lib/klib.a }
    vect        0xFFFF0000 : AT(0x4000) { high_vect.o }
} 

Is this right ????


回答1:


It is much easier if you use input sections. Just using filename is not the normal way to do this. The issue is that at some point the source modules will inter-act and you will have code and/or data from multiple location used in the same module. So, make sections loader, kloader and vect and use gcc attributes or pragmas to place code/data in sections.

Your question is answered in Gnu ld's Input section example. The output section list does not have to be in memory order. Place the wildcard { *.o(.text*) } last and input objects that aren't matched will be placed in this section.

An example annotated function might look like,

 void data_abort(unsigned int fsr, void* fault) __attribute__ ((section ("vector)))

Often functions/data in different sections must co-operate, so being able to mix them in the same source file allows the compiler to perform optimizations on static items and keeps functionally similar items grouped together, even though they might reside in different sections.

I think this might generally follow what you requested.

 SECTIONS { 
     loader      0x00000000 : { start.o loader.o }
     kloader     0x30100000 : AT(4096) { loaderk.o ../lib/klib.a }
     vect        0xFFFF0000 : AT(0x4000) { high_vect.o }
     vect2       0xFFFF0000 : AT(0x8000) { z.o } /* overlay? */
     text        0xC0000000 : { *.o }
 } 

I am not sure if you intend to over-lay the vectors or not. You can overlay init code with some data tables. Usually you want to separate at a minimum .text, .data and .bss.

Always generate a map file and double check the addresses there. This is much quicker than loading and instrumenting the code to determine that something has been placed at the wrong address.

See: Running code from RAM, Gnu Linker giving unexpected address, and the related links to this question.



来源:https://stackoverflow.com/questions/16508946/how-to-write-this-ld-script

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!