I am trying to make a simple kernel of the operating system higher. When using Grub as a loader, like me, there should also be some code with the bottom half (32 bits). Since I want this 32-bit code to be as concise as possible, I don’t want to write an ELF loader in it, just loading 64-bit code, because that would be absurd (in fact, this is the most common solution, but I wanted to to avoid this, if possible).
I found that linker scripts let you load addresses that are different from virtual addresses. This is useful so that I can load 64-bit partitions to fit in a small binary, and then use virtual memory to map the correct virtual addresses to the physical addresses to which they were downloaded. This works, except that the low-text section does not fit in the text segment. The entry point,, _startis in this section.
I cannot place a section with low text (where _start) in a text segment unless I specify a text segment in the command PHDRS. Of course, using this command allows the linker to decide on the generation of normally expected segments. When I do this, the sections eventually overlap, and I'm not quite sure why. I indicate the segments in the order, rodata, text and sections are the same, and yet their download memory addresses are assigned by rotates, and the data is exchanged and all three overlap.
Here is my linker script:
ENTRY(_start)
PHDRS {
.low PT_LOAD FILEHDR PHDRS;
.data PT_LOAD;
.rodata PT_LOAD;
.text PT_LOAD;
}
SECTIONS {
. = 1M;
.data_low BLOCK(4K) : ALIGN(4K) {
*(.bss_low)
} : .low
.rodata_low BLOCK(4K) : ALIGN(4K) {
KEEP(*(.multiboot_low))
*(.rodata_low)
} : .low
.text_low BLOCK(4K) : ALIGN(4K) {
*(.text_low)
} : .low
.stack 0xC0000000 : AT(0x200000) ALIGN(4K) {
*(.bootstrap_stack)
} : .data
_LADD_ = LOADADDR(.stack) + SIZEOF(.stack);
.data BLOCK(4K) : AT(_LADD_) ALIGN(4K) {
*(COMMON)
*(.bss)
} : .data
_LADD_ += SIZEOF(.data);
.rodata BLOCK(4K) : AT(_LADD_) ALIGN(4K) {
*(.rodata)
} : .rodata
_LADD_ += SIZEOF(.rodata);
.text BLOCK(4K) : AT(_LADD_) ALIGN(4K) {
*(.text)
} : .text
}
I do not think the code refers to this error. When I link my object files using this linker script (optionally with -n --gc-sections), I get this error:
ld: section .data loaded at [0000000000200020,000000000020103f] overlaps section .rodata loaded at [0000000000200010,00000000002000d0]
ld: section .text loaded at [00000000002000d1,00000000002017ce] overlaps section .data loaded at [0000000000200020,000000000020103f]
, , , , , , , AT ( , ).
, "" ELF (PHDRS script) "", ELF (SECTIONS script). , , , ELF, . - Grub ELF , .
, , ? .