sections .gnu.linkonce.*

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
rootel77

sections .gnu.linkonce.*

Post by rootel77 »

Hi,

I had some problems with my code when I wanted to switch to GCC under linux (from cygwin). I discovered that the linker added a sort of "gnu.linkonce.r..." sections just after the variable end at the end of script. this made the prog to crash (i found that a vtable for a virtual class was stored in thoses sections so after my end variable) .

someone know why these sections are used?
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:sections .gnu.linkonce.*

Post by Candy »

rootel77 wrote: someone know why these sections are used?
Shortest answer: yes.

Second-shortest answer: to ensure those objects are linked in only once (hence linkonce).

Long:
The linker should (iirc) filter out those linkonce sections and link them to their proper names. Are you linking to a proper final binary?
rootel77

Re:sections .gnu.linkonce.*

Post by rootel77 »

Candy wrote: Shortest answer: yes.

Second-shortest answer: to ensure those objects are linked in only once (hence linkonce).

Long:
The linker should (iirc) filter out those linkonce sections and link them to their proper names. Are you linking to a proper final binary?
my linker script does not use an explicit target. thus i'm linking to the format used by the host os. when i used cygwin, i invoked objcopy to convert the original executable to elf. but after, i decided to switch to the GCC cross compiler and here the problem appears (the same as under a linux natif compiler).
there are 2 sort of sections:
.gnu.linkonce.t* added just after the text sections
.gnu.linkonce.r* added at the end

<EDIT> i've found a doc explaining the use of those sections at
http://www.iecc.com/linker/linker04.rtf.
i didnt understood all stuf but it seems that those sections are used to avoid duplicate code among many object files like vtable or templates. i also found an ld script suitable for C++ code in the OSD site

Code: Select all

ENTRY(entry)
SECTIONS {
/* use ld -Ttext=nnn ... to set starting virtual address */
    .text : {
        g_code = .; _g_code = .;    /* symbols to mark start of section */
/* code */
        *(.text*)                   /* wildcard for "gcc -ffunction-sections" */
        *(.gnu.linkonce.t.*)        /* C++ templates? */
        *(.rodata*)                 /* read-only data (ELF only) */
   *(.gnu.linkonce.r.*)
/* C++ static constructors and destructors */
        start_ctors = .;
   *(SORT(.ctor*))
   end_ctors = .;              /* accessed from asm code only... */
   start_dtors = .;            /* ...so no leading underscore */
   *(SORT(.dtor*))
   end_dtors = .;
   . = ALIGN(4096);
    }
    .data : {
        g_data = .; _g_data = .;
/* data */
        *(.data*)                   /* wildcard for "gcc -fdata-sections" */
   *(.gnu.linkonce.d.*)
   . = ALIGN(4096);
    }
    .bss : {
        g_bss = .; _g_bss = .;
/* BSS */
        *(.bss*)                    /* wildcard for "gcc -fdata-sections" */
   *(.gnu.linkonce.b.*)
        *(COMMON)                   /* "common" variables */
   . = ALIGN(4096);
    }
    g_end = .; _g_end = .;
/* MinGW debug sections
Omit these and you may get an invalid executable file */
    .stab : {
   *(.stab)
    }
    .stabstr : {
   *(.stabstr)
    }
}
</EDIT>
Post Reply