problems at defining own gcc linker script

Asked by Thomas_Z

hello!

i try to write/configure my own linker script for programming with C++ for the stm32303vc with ARM Cortex M4. i downloaded the newest available toolchain (4.9-2015-q2-update). this package comes with some example linker scripts and i tried to configure gcc.ld (from \share\gcc-arm-none-eabi\samples\ldscripts) for my own needs.

i link with:
--specs=nano.specs
-mthumb
-nostartfiles
-Wl,--gc-sections
--specs=nosys.specs

my 1st question is: what are sections .ARM.extab and .ARM.exidx stand for and used for?
what does extab and exidx mean? do i realy need them?
***************
...
 .ARM.extab :
 {
  *(.ARM.extab* .gnu.linkonce.armextab.*)
 } > FLASH

 __exidx_start = .;
 .ARM.exidx :
 {
  *(.ARM.exidx* .gnu.linkonce.armexidx.*)
 } > FLASH
 __exidx_end = .;
...
***************

next, when initalizing ram at startup i copy all data beginning at __data_start__ to __data_end__ from rom (LMA) to ram (VMA). section data includes *(.data*) but also includes *(vtable), preinit data, init data, fini data and *(.jcr*).
for me, it makes sense to copy all data from *(.data*). but what is vtable exactly for? does it deal with virtual functions defined by c++classes? do i have to copy it too?
preinit/init/fini array contain addresses to functions and should be called, not copied. am i right?
what exactly is *(.jcr)?
isn't it better to put must parts of section .data into FLASH or ROM instead of RAM (except *(.data*) and maybe *(vtable) ?) ?

***************
...
__etext = .;

 .data : AT (__etext)
 {
  __data_start__ = .;
  *(vtable) /* what is vtable? */
  *(.data*)

  . = ALIGN(4); /* shouldn't preinit/init and fini data better be put to ROM (eg. section .text) ? */
  /* preinit data */
  PROVIDE_HIDDEN (__preinit_array_start = .);
  KEEP(*(.preinit_array))
  PROVIDE_HIDDEN (__preinit_array_end = .);

  . = ALIGN(4);
  /* init data */
  PROVIDE_HIDDEN (__init_array_start = .);
  KEEP(*(SORT(.init_array.*)))
  KEEP(*(.init_array))
  PROVIDE_HIDDEN (__init_array_end = .);

  . = ALIGN(4);
  /* finit data */
  PROVIDE_HIDDEN (__fini_array_start = .);
  KEEP(*(SORT(.fini_array.*)))
  KEEP(*(.fini_array))
  PROVIDE_HIDDEN (__fini_array_end = .);

  KEEP(*(.jcr*)) /* what is .jcr? */
  . = ALIGN(4);
  /* All data end */
  __data_end__ = .;

 } > RAM
...
***************

hope my questions are prased clear enough. i'm new to linker scripts an a bit confused about all the symbols, sections, alignments and so on.

Tom

Question information

Language:
English Edit question
Status:
Answered
For:
GNU Arm Embedded Toolchain Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Thomas Preud'homme (thomas-preudhomme) said :
#1

Hi Thomas,

1) .ARM.extab and .ARM.exidx are related to unwinding. You can find more information in the ELF for the ARM Architecture document (aka IHI0044E), available at [1]. You don't need them if you don't care about unwinding (unwinding is useful for C++ exception and for debugging).

[1] http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044e/index.html

2) .vtable is indeed for virtual functions defined in C++ classes. You do need to copy them if your program contains some virtual methods (if not, the section should be empty anyway) since that's how the function call can be dispatched to the method of the right class.

3) Preinit/init/fini arrays contain pointer for constructors and destructors functions indeed. These are used notably for constructors and destructors of static objects in C++ but can also be used to run specific functions at program startup and end in C, by using the eponymous function attributes. If your program is simple and don't need any constructor to be called, your startup code can call directly main and skip running constructors. Otherwise the function pointed to in these arrays need to be called. Whether to copy the array depends on how they are called: via their LMA or via their VMA.

4) .jcr seems to be for registering compiled java class, you probably won't need this.

5) I have no opinion as to whether most data should be kept in FLASH or ROM instead of loading it in RAM. I guess it depends on how often you access them, how much RAM you have and how you access them (if they are in FLASH or ROM and the LMA is different from the VMA you will need to make sure they are access through their LMA).

Best regards.

Revision history for this message
Thomas_Z (th-zoechbauer) said :
#2

Hello,

thank you for your answers, that helped a lot and made things clearer. But I still need some help.

add 1) I found the information in the document. This point is solved :-)

add 2) Does vtable contain static information and can be put to ROM?
2.1) Does it make a difference if I write "*(.vtable)" or "*(vtable)"? I ask this, beacause you wrote ".vtable" and in the example linker script gcc.ld it is typed as "vtable" (without the dot).
2.2) I found some information on .vtable [1] that deals with data alignment. The thread is from the year 2010, is the alingment still important and is it the reason why it is the first entry in .data section (and therefore the first data in RAM)?

[1] https://e2e.ti.com/support/development_tools/compiler/f/343/t/36644

add 3) Ok, that sounds logically. It depends on how I want to call them. Initializing brings up the next question: .preinit_array, .init_array and .fini_array are not the only sections that sound like (de)initialization. Section .text additionally contains .init, .fini, ctors and dtors. What are this sections for? According to my map-file they are empty. Is it a compatibility issue?

 .text :
 {
  KEEP(*(.isr_vector))
  *(.text*)

  KEEP(*(.init))
  KEEP(*(.fini))

  /* .ctors */
  *crtbegin.o(.ctors)
  *crtbegin?.o(.ctors)
  *(EXCLUDE_FILE(*crtend?.o *crtend.o) .ctors)
  *(SORT(.ctors.*))
                *(.ctors)

  /* .dtors */
   *crtbegin.o(.dtors)
   *crtbegin?.o(.dtors)
   *(EXCLUDE_FILE(*crtend?.o *crtend.o) .dtors)
   *(SORT(.dtors.*))
   *(.dtors)

  *(.rodata*)

  KEEP(*(.eh_frame*))
 } > FLASH

add 4) Indeed, if it is java I won't need it. Solved too.

add 5) I see, it's a decision of design. Solved too.

Thanks for your help!
Best regards.

Revision history for this message
Launchpad Janitor (janitor) said :
#3

This question was expired because it remained in the 'Open' state without activity for the last 15 days.

Revision history for this message
Thomas_Z (th-zoechbauer) said :
#4

Hello,

if someone can help me, I still need some information on vtable (point 2) and initialization (point 3).

vtable:
----------------
I included vtable in .data segment:

 /* The ROM-to-RAM initialized data section */
 .data :
 {
  _data_begin = .;
  KEEP(*(vtable))
  . = ALIGN(4);
  KEEP(*(.data))
  . = ALIGN(4);
  KEEP(*(.data*))
  . = ALIGN(4);
  _data_end = .;
 } > RAM AT > FLASH

When I compile some code that uses virtual functions and polymorphism, the vtable-segment seems to be empty.
Map file:
...
.data 0x20000000 0x8c8 load address 0x08014578
                     0x20000000 _data_begin = .
 *(vtable)
                     0x20000000 . = ALIGN (0x4)
 *(.data)
 .data 0x20000000 0x0 obj\debug\src\another.o
...

I found some vtable entries in .rodata segment:
...
 .rodata 0x08012490 0x738 c:/embedded/gcc-arm-embedded/4.9_2015q2/bin/../lib/gcc/arm-none-eabi/4.9.3/../../../../arm-none-eabi/lib/armv7e-m\libstdc++.a(cp-demangle.o)
 *(.rodata*)
 .rodata._ZTVN5shape8circle_tE
                0x08012bc8 0x18 obj\debug\src\circle.o
                0x08012bc8 vtable for shape::circle_t
 .rodata._ZTVN5shape7shape_tE
                0x08012be0 0x18 obj\debug\src\circle.o
                0x08012be0 vtable for shape::shape_t
...

Shouldn't they be put to vtable segment instead of rodata? Did I configure something wrong?

Initialization:
------------------------
What are .init, .fini, .dtors and .ctors for and how can I use them or prevent the toolchain to using them?

I look forward for help.

Best regards.
Thomas

Revision history for this message
Tejas Belagod (belagod-tejas) said :
#5

Hi,

The link that your refer to talks about vector table(vtable), I think. I'll get back to your other questions soon.

Thanks,
Tejas.

Can you help with this problem?

Provide an answer of your own, or ask Thomas_Z for more information if necessary.

To post a message you must log in.