Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to solve the error in linker script?

Tags:

c

linux

gcc

linker

ld

I created a memory linker script and saved it as memory.ld in the eclipse ide : Project : properties : gcc linker : miscellaneous : I added -M -T memory.ld

memory.ld :

    MEMORY
{
        ram (rw)   : ORIGIN = 0x4000000 ,  LENGTH = 2M  
}

SECTIONS
{
  RAM : { *(.myvarloc) 

} > ram }

In my c program : I made a global declaration as:

__attribute__ ((section(".myvarloc")))

 uint8 measurements [30];

ERRORS:

/usr/bin/ld: FEBRUARY section `.text' will not fit in region `ram'
/usr/bin/ld: region `ram' overflowed by 20018 bytes
/usr/lib/i386-linux-gnu/libc_nonshared.a(elf-init.oS): In function `__libc_csu_init':
(.text+0x2b): undefined reference to `__init_array_end'
/usr/lib/i386-linux-gnu/libc_nonshared.a(elf-init.oS): In function `__libc_csu_init':
(.text+0x31): undefined reference to `__init_array_start'
/usr/lib/i386-linux-gnu/libc_nonshared.a(elf-init.oS): In function `__libc_csu_init':
(.text+0x57): undefined reference to `__init_array_start'
/usr/bin/ld: FEBRUARY: hidden symbol `__init_array_end' isn't defined
/usr/bin/ld: final link failed: Bad value
collect2: error: ld returned 1 exit status
like image 283
user3252048 Avatar asked Nov 11 '22 13:11

user3252048


1 Answers

Depending on the compiler you are using (GCC?) and the processor for which you are compiling (x86?), the compiler will generate several segment references in the object files. The most common ones are .text for code segments, .data for initialized data and .bss for uninitialized data. You can see which segments your compiler generates by using the nm utility on your object files.
I assume that until you provided your own linker script, the environment has provided some default script automatically and/or implicitly. But now that you have decided to "roll your own", you have to take care of all details yourself.

I cannot verify the details, but you could start with the following SECTIONS:

SECTIONS
{
  .bss : { *(.myvarloc) } 
  .bss : { *(.bss) }
  .data : { *(.data) }
  .text : { *(.text) }
}

I'm not sure if this is the exact syntax your GCC linker (it depends a little on the version), but you can find more information in the manual.

like image 119
Johan Bezem Avatar answered Nov 14 '22 22:11

Johan Bezem