Code: Select all
class Test
{
public:
Test()
{
mem = reinterpret_cast<unsigned short *>(0xB8000);
* mem++ = 0x4041;
}
~Test()
{
* mem++ = 0x4042;
}
private:
unsigned short * mem;
};
Test test;
extern "C" unsigned long start_ctors, end_ctors, start_dtors, end_dtors;
extern "C" init(unsigned long magic, unsigned long addr)
{
for(unsigned long * constructor(& start_ctors); constructor < & end_ctors; ++constructor)
((void (*) (void)) (*constructor)) ();
// Kernel main
for(unsigned long * destructor(& start_dtors); destructor < & end_dtors; ++destructor)
((void (*) (void)) (* destructor)) ();
}
Follow my linker script and the output of the readelf:
Code: Select all
ENTRY(start)
SECTIONS
{
.text 0x100000 :
{
code = .; _code = .; __code = .;
*(.text*)
*(.gnu.linkonce.t*)
. = ALIGN(0x1000);
}
.rodata :
{
*(.rodata*)
*(.rdata*)
*(.gnu.linkonce.r*)
. = ALIGN(0x1000);
}
.data :
{
data = .; _data = .; __data = .;
*(.data*)
*(.gnu.linkonce.d*)
. = ALIGN(0x1000);
}
.ctors :
{
start_ctors = .; _start_ctors = .; __start_ctors = .;
*(.ctor*)
end_ctors = .; _end_ctors = .; __end_ctors = .;
. = ALIGN(0x1000);
}
.dtors :
{
start_dtors = .; _start_dtors = .; __start_dtors = .;
*(.dtor*)
end_dtors = .; _end_dtors = .; __end_dtors = .;*/
}
.bss :
{
bss = .; _bss = .; __bss = .;
*(.bss*)
*(.gnu.linkonce.b*)
. = ALIGN(0x1000);
}
end = .; _end = .; __end = .;
}
Code: Select all
There are 6 section headers, starting at offset 0x4024:
Section Headers:
[Nr] Name Type Addr Off Size ES Flg Lk Inf Al
[ 0] NULL 00000000 000000 000000 00 0 0 0
[ 1] .text PROGBITS 00100000 001000 001000 00 AX 0 0 4
[ 2] .data PROGBITS 00101000 002000 001000 00 WA 0 0 4
[ 3] .ctors PROGBITS 00102000 003000 001000 00 WA 0 0 4
[ 4] .bss NOBITS 00103000 004000 009000 00 WA 0 0 32
[ 5] .shstrtab STRTAB 00000000 004000 000023 00 0 0 1
Key to Flags:
W (write), A (alloc), X (execute), M (merge), S (strings)
I (info), L (link order), G (group), x (unknown)
O (extra OS processing required) o (OS specific), p (processor specific)