This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

about link order

hello, I compile a lib test.lib, it include 3 file, file1.c file2.c file3.c.
and I want to use cmd file to set link order, such as

.lib_text :
{
 -ltest.lib<file1.obj> (.text)
 -ltest.lib<file2.obj> (.text)
 -ltest.lib<file3.obj> (.text)
} > SDRAM

but in map file, it is

.lib_text  0    80529060    0000e400    
                80529060    000062a0     test.lib : file2.obj (.text)
                8052f300    00006280              : file3.obj (.text)
                80535580    00001400              : file1.obj (.text)


how can I get the correct link order according to  my cmd file? Thanks

  • In this case I believe you would have to define individual memory segments for each object file (like SDRAM_FILE1:  origin = 0x12345678 length = 0x1400, SDRAM_FILE2 ... , SDRAM_FILE3 ...) and allocate the object files individually to the given segment, the order in which the linker places code sections by default is not something you can control directly without explicit segment assignments by my understanding.

    I am curious why you would want to have the object files in a particular sequence in your normal .text section? Typically the assignment is arbitrary, perhaps you have cache alignment concerns?

  • Bernie is right. The linker assigns them to memory based on the size of each section. If you will notice, file2.obj is placed first as it is largest, then file3.obj, then file1.obj (smallest). I think you can try the --default_order switch to disable size-based allocation in the linker.

  • yes, I have cache alignment concerns, maybe I should collect relevant functions from different source files together .