问题
I have been reading the sections documentation for ld and something isn't clear.
Given the partial linker script:
MEMORY
{
FLASH1 (rx) : ORIGIN = 0x000FB000, LENGTH = 0x05000
FLASH2 (r) : ORIGIN = 0x000FA000, LENGTH = 0x01000
}
SECTIONS
{
.some_code :
{
KEEP(*(SORT(.some_code*)))
} > FLASH1
}
SECTIONS
{
.my_page :
{
KEEP(*(SORT(.my_page*)))
} > FLASH2
}
Is there ever a reason to have 2 section commands in a single linker script as above rather than having it all in 1 section commands as below?
SECTIONS
{
.some_code :
{
KEEP(*(SORT(.some_code*)))
} > FLASH1
.my_page :
{
KEEP(*(SORT(.my_page*)))
} > FLASH2
}
Or are they equivalent. Are there other cases where you'd want to split it up like that?
Additional clarification: I am asking for a reason to have separate "SECTIONS" commands (each with their own block of section definitions) not asking for the reason for having separate sections in general.
回答1:
There isn't ever an unavoidable need to write more than one SECTIONS
command,
or MEMORY
command, in a linker script.
SECTIONS
{
/* Commands... */
}
SECTIONS
{
/* More commands... */
}
is equivalent to:
SECTIONS
{
/* Commands... */
/* More commands... */
}
and likewise for MEMORY
.
However, the maintainability of a non-trivial body of linker-scripts is
assisted if a linker script can include multiple SECTIONS
or MEMORY
commands. E.g. if:
foobar.lds (1)
MEMORY
{
FOOMEM (rx) : ORIGIN = 0x000FB000, LENGTH = 0x05000
BARMEM (r) : ORIGIN = 0x000FA000, LENGTH = 0x01000
}
SECTIONS
{
.foo : {
*(.foo)
} > FOOMEM
.bar : {
*(.bar)
} > BARMEM
}
were something much less trivial than it is, it might be better refactored as:
foobar.lds (2)
INCLUDE ./foo.lds
INCLUDE ./bar.lds
foo.lds
MEMORY
{
FOOMEM (rx) : ORIGIN = 0x000FB000, LENGTH = 0x05000
}
SECTIONS
{
.foo : {
*(.foo)
} > FOOMEM
}
bar.lds
MEMORY
{
BARMEM (r) : ORIGIN = 0x000FA000, LENGTH = 0x01000
}
SECTIONS
{
.bar : {
*(.bar)
} > BARMEM
}
so that foobar.lds
(2) is expanded by the linker to
foobar.lds (3)
MEMORY
{
FOOMEM (rx) : ORIGIN = 0x000FB000, LENGTH = 0x05000
}
SECTIONS
{
.foo : {
*(.foo)
} > FOOMEM
}
MEMORY
{
BARMEM (r) : ORIGIN = 0x000FA000, LENGTH = 0x01000
}
SECTIONS
{
.bar : {
*(.bar)
} > BARMEM
}
An historical note: In this obselete GNU ld manual from bygone days, we can find the constraints:
You may use at most one SECTIONS command in a script file, but you can have as many statements within it as you wish.
and:
A command file may contain at most one use of the MEMORY command; however, you can define as many blocks of memory within it as you wish.
which are absent from the current manual.
来源:https://stackoverflow.com/questions/55638429/why-would-a-linker-script-have-multiple-section-commands