ld: howto link a whole library or object file content to a specific section

Gc giancarlo.parodi@gmail.com
Wed Dec 15 14:27:00 GMT 2010


Hello everybody,
after reviewing the manual for ld, and searching extensively on
several forums and mailing lists, I still have a doubt if it is at all
possible to instruct the ld linker to place whatever content will be
linked from a specific library (like libc.a) into a section of choice
(or alternatively the content of a whole object file into a section of
choice) by using linker commands

The target is an ARM CortexM3 device, using the code sourcery lite
tools v. 2010q1-188.

1. first option, whole file into a section:

assume I want to link the whole file content called syscalls_minimal.o
within a defined RAM_lib section I would try do it in the way below:

MEMORY
{
  /* Define each memory region */
  RAM (rwx) : ORIGIN = 0x12345678, LENGTH = 0x10000
...
  RAM_lib (rwx) : ORIGIN = 0x12345678, LENGTH = 0x10000

}

...
...

SECTIONS
{

    .my_lib:
    {
        syscalls_minimal.o

    } > RAM_lib

    .text :
    {
        KEEP(*(.isr_vector))
        *(EXCLUDE_FILE (syscalls_minimal.o) .text*)
        *(.rodata*)

    } > RAM

...etc etc for data, bss and so on
...

however this does not work, and it seems that the linker is including
again the text, data, bss sections within syscalls_minimal.o in the
rest of the default sections I have defined later on - so I get errors
of multiple definition of symbols like _close _fstat etc. I thought
the text data bss from syscalls_minimal.o would be used only once.
Using EXCLUDE_FILE (*syscalls_minimal.o) within the other sections
defined, to tell the linker to explicitly ignore the file at that
time, does not solve the problem.

The only reliable way I found so far was to define with __attribute__
((section(".xyz"))) a custom name in the c module, then assign this
custom name to a specified section of the linker
However I wonder if there is a way to achieve this also with the
linker syntax, and without the __attribute pragma

2. second option mentioned in section 3.6.4.1 of the ld manual
suggests using the 'archive:file' method to indicate an archive (or a
file located within an archive) to be taken as an input to be placed
in a specific section.

Assuming the archive is called libc.a, I tried several ways like using
(for example) the notation

  .my_lib:
    {
        libc.a:lib_a-memcmp.o

    } > RAM_lib

but this does not work either. I assume I misunderstood something on
the syntax, or there are some other switches or options which I am
missing, but so far no luck.



Any help in trying to solve this issue would be appreciated !

Best regards, and nice X-mas holidays to everyone
Gc



More information about the Gcc-help mailing list