true_casey
true_casey

Reputation: 79

How to write this ld script ?

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 ????

Upvotes: 0

Views: 1797

Answers (1)

artless-noise-bye-due2AI
artless-noise-bye-due2AI

Reputation: 22430

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.

Upvotes: 1

Related Questions