Difference between *(.text) and *(.text*) in linker scripts - compilation

In linker scripts, *(.text) means that all text section in all input files, but what does exactly *(.text*) mean? I couldn't find it in GNU linker documentation. Thanks in advance.

I believe the answer to this is the same as the answer to this question:
Difference between *(.data), *(.data*) and *(.data.*) in linker script
The * symbol is a wildcard pattern, it will match any file name.
*(.text) will match any file with the suffix ".text", *(.text*) will match any file name containing ".text" anywhere in its name.
Wildcard documentation: https://sourceware.org/binutils/docs-2.27/ld/Input-Section-Wildcards.html#Input-Section-Wildcards

Related

How to map LTO sections to output sections in GCC (Arm) linker?

I have a linker file that I use as input for Gnu Arm Embedded Toolchain.
It contains, among other things, an output section like this:
.text :
{
. = ALIGN(4);
*(.text*)
*(.rodata*)
} > FLASH
, where FLASH is a MEMORY block.
My issue is that, when I compile a C++ file with LTO (link-time optimization), some of my code is put in non-".text" sections in the resulting object files. These sections seems to be consistently named:
.gnu.lto<SOMETHING>
As a result, they are not placed in my .text output section.
How can I map LTO input sections to my .text output section?
It turns out my issue was related to a misunderstanding in how the VMA and LMA statements work in GNU linker files. So, in essense the LTO section are mapped correctly. Thanks to anyone who spent time on this.

How can I copy all .rodata section using onjcopy?

I've tried:
objcopy -j .rodata ...
but it copies only the ".rodata" section to the output file.
However, gcc creates ".rodata.str*" sections for read-only strings! And objcopy doesn't seem to recognize those string sections.
How can I extract the whole .rodata section including all string sections?
Okay, I found the answer by myself right after asking this question...
To dump all .rodata sections to a single file, you just need to use wildcard:
objcopy -j .rodata* ...

Is it possible to change default sections for a whole file with GNU Arm embedded?

With GNU Arm embedded toolchain, is it possible to change the default sections for symbols for a whole file?
I've previously worked with Rowley Crossworks, which has a parameter you can set for any source file or folder to change various default sections, for instance the default section for zeroed variables:
(from the Crossworks manual)
This is very useful to make sure a big application fit in flash on and RAM resource constrained microcontrollers. However, I'm unable to find any way to do this with the regular GNU Arm toolchain.
I'm aware that I can use __attribute__((section(".sectionname"))), but this requires code modifications, which is problematic when compiling the same code for different targets, some of which may not have more than one section.
The ideal solution would be a GCC command-line parameter to put for instance zeroed data in a custom section for a specific compilation unit. I could then apply this to specific files, folders or projects from CMake, without making any changes to the actual source code. Does something like this exist?
I was not able to find a command line parameter or similar for this functionality, but the comment from Lundin made me look into linker scripts in some more detail, and ended up with this:
.bss :
{
. = ALIGN(4);
__bss_start__ = .;
*main.cpp.obj*(.bss*)
*(COMMON)
. = ALIGN(4);
__bss_end__ = .;
} >RAM
.ethram (NOLOAD):
{
__ethram_start__ = .;
*(.ethram)
*(.bss*)
__ethram_end__ = .;
} >ETHRAM
In the above I explicitly state that in the output section .bss only main.cpp's .bss section should be included, and that it should be placed in regular RAM. By then having an unconstrained .bss in ETHRAM, the linker places other files' .bss sections there, and this was sufficient for my use.
It is also possible to explicitly exclude files from an output section, like this, but my application didn't need it:
.bss :
{
. = ALIGN(4);
__bss_start__ = .;
EXCLUDE_FILE(*main.cpp.obj*)*(.bss*)
*(COMMON)
. = ALIGN(4);
__bss_end__ = .;
} >RAM
It is possible with __attribute__, but to simply type out a section for the variable isn't sufficient. There must be such a section present in the linker script, which means you you have to manually modify that one (often called .ld/.lcf or some such, depending on target). Various dialects seem to exist depending on target, see the GCC linker manual for details.
Crossworks does that part for you - from what I recall they let you modify a more easy to use XML format so that you don't have to meddle with the the linker script directly. You can look at the output files from Crossworks and you'll find one with extension .ld or equivalent. And that's likely how your linker file for gcc for the given target platform should look like.
Could you change the compilation recipe to generate the .s file, apply awk / sed / etc... to adjust the sections to your liking, then assemble the result. It is kind of an old way of doing it, but is quite portable and reliable.

gcc linker script difference between *(.rodata*) and *(.rodata.*)

We have a linker script where a part of the .text section has the read only data input section specified as:
.text:
{
...
*(.rodata .rodata* .gnu.linkonce.r.*)
...
}
However, other input sections that have a trailing wildcard usually have a . between name and wildcard. Like: *(.text .text.* .gnu.linkonce.t.*)
Is there a difference in how the linker treats .'s and wildcard combinations or is there no difference?
Should the .rodata* actually be .rodata.*?
.text* is simply shorter than the more explicit .text .text.*, though not equivalent. It would pick up other sections like .text_foo. This can be intended (to make it more independent of the compiler convention maybe?) or not.
https://sourceware.org/binutils/docs/ld/Input-Section-Wildcards.html
The .* sections are generated by gcc if you pass -ffunction-sections. The same holds for data and -fdata-sections.

Why does ld include sections in the output file that are not specified in the link command file?

I am struggling to understand how to make ld (in the gcc arm embedded tool kit) do what I want:
just as an example, I have a couple of .o files I want to link. say I only want a section called .foo in the output. I put this in the command file link.ld:
SECTIONS{
.foo : {*(.text)}
}
and go "ld -T link.ld file1.o fil2.o -o output.o"
then i use objdump on output.o and it simply contains every section that was in the input files (), and absolutely no section called .foo this is the most confusing part why is there no section in the output called .foo?
I have read this http://www.scoberlin.de/content/media/http/informatik/gcc_docs/ld_3.html and it all seems straight foward, I just cannot understand why such a simple linker script does nowhere near what I want it to do.
I must be missing something extremely obvious; can someone explain how I can control exactly which sections appear in the output file and what they are called?
Well, you cannot control that so easily - as you see. If you don't link some sections "explicitly" (in which case you have control over things like order, alignment, location, fill, ...), everything else that the linker finds necessary is just placed "after that". It's a simplification, but this mostly works like this.
I don't think there's a way to tell linker to include ONLY the section you requested. But there's a way to tell it to exclude some sections. But the problem is that you have to be explicit about the sections you want to exclude.
3.6.7 Output Section Discarding
...
The special output section name /DISCARD/ may be used to discard input sections. Any input sections
which are assigned to an output section named /DISCARD/ are not
included in the output file.
Maybe something like this would work?
SECTIONS{
.foo : {*(.text)}
/DISCARD/ : {*(*)}
}

Resources