Empty ctor section with 32bit bootstrap code

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
nilres
Posts: 6
Joined: Sun Nov 08, 2015 1:20 pm

Empty ctor section with 32bit bootstrap code

Post by nilres »

Hey guys,

I'm building a long mode kernel with an embedded 32 bit bootstrap code. I have one global constructor in the bootstrap code. I compile the bootstrap code with -m elf_i386 and objcopy it into and x86_64 elf for the linking step. The output object of the objcopy still consists of the ctor section:

>build$ objdump -h boot.cpp.32.o

boot.cpp.32.o: Dateiformat elf64-x86-64

Sektionen:
Idx Name Größe VMA LMA Datei-Off Ausr.
0 .text 0000011d 0000000000000000 0000000000000000 00000040 2**0
CONTENTS, ALLOC, LOAD, RELOC, READONLY, CODE
1 .data 00000000 0000000000000000 0000000000000000 0000015d 2**0
CONTENTS, ALLOC, LOAD, DATA
2 .bss 00000040 0000000000000000 0000000000000000 00000160 2**5
ALLOC
3 .rodata 0000000c 0000000000000000 0000000000000000 00000160 2**0
CONTENTS, ALLOC, LOAD, READONLY, DATA
4 .ctors 00000004 0000000000000000 0000000000000000 0000016c 2**2
CONTENTS, ALLOC, LOAD, RELOC, DATA
5 .comment 00000012 0000000000000000 0000000000000000 00000170 2**0
CONTENTS, READONLY
6 .eh_frame 000000bc 0000000000000000 0000000000000000 00000184 2**2
CONTENTS, ALLOC, LOAD, RELOC, READONLY, DATA


My linking script looks like that:

Code: Select all

ENTRY(_start)

SECTIONS {
        . = 1M;
   
        .multiboot BLOCK(4K) : ALIGN(4K) {
                *(.multiboot)
                <<ALL_BOOTSTRAP_FILES>>  (.text)
        }

        .rodata BLOCK(4K) : ALIGN(4K) {
                start_ctors = .;
                KEEP (*(SORT(.ctors*)))
                end_ctors = .;

                start_dtors = .;
                *(SORT(.dtors*))
                end_dtors = .;
   
                *(.rodata)
        }
         .text BLOCK(4K) : ALIGN(4K) {
                _code64 = .;
                *(EXCLUDE_FILE(<<ALL_BOOTSTRAP_FILES>>) .text)
                *(.rodata*)
                . = ALIGN(4096);
        }

        .data BLOCK(4K) : ALIGN(4K) {
                *(.data)
        }

        .bss BLOCK(4K) : ALIGN(4K) {
                *(COMMON)
                *(.bss)
        }
}


In the resulting binary the linker discards the ctor section:

>build$ readelf -s src/kernel/x86_64_pc.bin | grep ctor
60: 0000000000106000 0 NOTYPE GLOBAL DEFAULT 2 start_ctors
107: 0000000000106000 0 NOTYPE GLOBAL DEFAULT 2 end_


I tried like 100 different things: Instead of the "KEEP (*(SORT(.ctors*)))" use "KEEP (boot32.cpp.32.o (SORT(.ctors*)))", disabling optimization, ...

Has anyone any idea why this is happening?

Thanks
Nils
Post Reply