bzt wrote:
Hi,
First of all, the multiboot struct must be located in the first 8K IN THE FILE. This means it doesn't matter what VMA you choose (1M or 2M), the only thing that matters is the file offset. Likewise, the 8 byte alignment refers to the file offset, not the memory address where the struct is loaded.
Second, assuming you have put your struct in the ".multiboot" section, this script should work:
Code:
SECTIONS
{
.text BLOCK(4K) : ALIGN(4K)
{
KEEP(*(.multiboot))
*(.text)
}
...
Normally the linker will place the section in the same order as they appear in the source. But to be sure, you can use the KEEP() pseudo-function to tell the linker not to rearrange that certain ".multiboot" section. If you experience problems with the alignment, I would suggest to put the ELF header inside the text section too, that way your text segment will share the same alignment as the file offset. The ". = X" directive only sets the VMA, and not the file offset. (Think about this: it is possible that a text segment should be aligned at 4K when loaded in memory, but it is stored in the file at offset 0xE4 for example. Use readelf to print out the segments (Program Headers). Here's such an example
readelf output: according to the Program Headers, the text segment must be loaded at 0xffffffffffe02000 (which is 4K aligned), but stored at offset 0x78 (which is not 4K aligned)).
Cheers,
bzt
Hi,
Thanks for your reply. Adding KEEP() does not solve this problem. Here are some more details.
Objdump output:
Code:
Sections:
Idx Name Size VMA LMA File off Algn
0 .text 000003d9 0000000000100000 0000000000100000 00100000 2**12
CONTENTS, ALLOC, LOAD, READONLY, CODE
1 .rodata 00000034 0000000000101000 0000000000101000 00101000 2**12
CONTENTS, ALLOC, LOAD, READONLY, DATA
2 .eh_frame 0000010c 0000000000101038 0000000000101038 00101038 2**3
CONTENTS, ALLOC, LOAD, READONLY, DATA
3 .data 00000008 0000000000102000 0000000000102000 00102000 2**12
CONTENTS, ALLOC, LOAD, DATA
4 .bss 00004018 0000000000103000 0000000000103000 00102008 2**12
ALLOC
5 .comment 00000011 0000000000000000 0000000000000000 00102008 2**0
CONTENTS, READONLY
Output of readelf:
Code:
ELF Header:
Magic: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
Class: ELF64
Data: 2's complement, little endian
Version: 1 (current)
OS/ABI: UNIX - System V
ABI Version: 0
Type: EXEC (Executable file)
Machine: Advanced Micro Devices X86-64
Version: 0x1
Entry point address: 0x100000
Start of program headers: 64 (bytes into file)
Start of section headers: 1058344 (bytes into file)
Flags: 0x0
Size of this header: 64 (bytes)
Size of program headers: 56 (bytes)
Number of program headers: 1
Size of section headers: 64 (bytes)
Number of section headers: 10
Section header string table index: 9
Section Headers:
[Nr] Name Type Address Offset
Size EntSize Flags Link Info Align
[ 0] NULL 0000000000000000 00000000
0000000000000000 0000000000000000 0 0 0
[ 1] .text PROGBITS 0000000000100000 00100000
00000000000003d9 0000000000000000 AX 0 0 4096
[ 2] .rodata PROGBITS 0000000000101000 00101000
0000000000000034 0000000000000000 A 0 0 4096
[ 3] .eh_frame PROGBITS 0000000000101038 00101038
000000000000010c 0000000000000000 A 0 0 8
[ 4] .data PROGBITS 0000000000102000 00102000
0000000000000008 0000000000000000 WA 0 0 4096
[ 5] .bss NOBITS 0000000000103000 00102008
0000000000004018 0000000000000000 WA 0 0 4096
[ 6] .comment PROGBITS 0000000000000000 00102008
0000000000000011 0000000000000001 MS 0 0 1
[ 7] .symtab SYMTAB 0000000000000000 00102020
0000000000000408 0000000000000018 8 17 8
[ 8] .strtab STRTAB 0000000000000000 00102428
00000000000001b2 0000000000000000 0 0 1
[ 9] .shstrtab STRTAB 0000000000000000 001025da
0000000000000047 0000000000000000 0 0 1
Key to Flags:
W (write), A (alloc), X (execute), M (merge), S (strings), I (info),
L (link order), O (extra OS processing required), G (group), T (TLS),
C (compressed), x (unknown), o (OS specific), E (exclude),
l (large), p (processor specific)
There are no section groups in this file.
Program Headers:
Type Offset VirtAddr PhysAddr
FileSiz MemSiz Flags Align
LOAD 0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000102008 0x0000000000107018 RWE 0x200000
Section to Segment mapping:
Segment Sections...
00 .text .rodata .eh_frame .data .bss
There is no dynamic section in this file.
There are no relocations in this file.
The decoding of unwind sections for machine type Advanced Micro Devices X86-64 is not currently supported.
Symbol table '.symtab' contains 43 entries:
Num: Value Size Type Bind Vis Ndx Name
0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
1: 0000000000100000 0 SECTION LOCAL DEFAULT 1
2: 0000000000101000 0 SECTION LOCAL DEFAULT 2
3: 0000000000101038 0 SECTION LOCAL DEFAULT 3
4: 0000000000102000 0 SECTION LOCAL DEFAULT 4
5: 0000000000103000 0 SECTION LOCAL DEFAULT 5
6: 0000000000000000 0 SECTION LOCAL DEFAULT 6
7: 0000000000000000 0 FILE LOCAL DEFAULT ABS obj/loader.o
8: 0000000000000001 0 NOTYPE LOCAL DEFAULT ABS ALIGN
9: 0000000000000002 0 NOTYPE LOCAL DEFAULT ABS MEMINFO
10: 0000000000000003 0 NOTYPE LOCAL DEFAULT ABS FLAGS
11: 000000001badb002 0 NOTYPE LOCAL DEFAULT ABS MAGIC
12: ffffffffe4524ffb 0 NOTYPE LOCAL DEFAULT ABS CHECKSUM
13: 0000000000004000 0 NOTYPE LOCAL DEFAULT ABS STACKSIZE
14: 0000000000103000 0 NOTYPE LOCAL DEFAULT 5 stack_bottom
15: 0000000000107000 0 NOTYPE LOCAL DEFAULT 5 stack_top
16: 0000000000000000 0 FILE LOCAL DEFAULT ABS kernel.c
17: 0000000000100040 83 FUNC GLOBAL DEFAULT 1 terminal_cls
18: 0000000000107000 1 OBJECT GLOBAL DEFAULT 5 terminal_color
19: 0000000000100110 113 FUNC GLOBAL DEFAULT 1 terminal_nsputc
20: 0000000000100280 35 FUNC GLOBAL DEFAULT 1 terminal_nswrite
21: 0000000000100380 21 FUNC GLOBAL DEFAULT 1 terminal_init
22: 0000000000102000 8 OBJECT GLOBAL DEFAULT 4 VGA_POINTER
23: 000000000010102c 4 OBJECT GLOBAL DEFAULT 2 VGA_HEIGHT
24: 0000000000100190 183 FUNC GLOBAL DEFAULT 1 terminal_putc
25: 0000000000101030 4 OBJECT GLOBAL DEFAULT 2 VGA_WIDTH
26: 00000000001003a0 57 FUNC GLOBAL DEFAULT 1 kernel_main
27: 0000000000107004 4 OBJECT GLOBAL DEFAULT 5 terminal_foreground_color
28: 00000000001002b0 35 FUNC GLOBAL DEFAULT 1 terminal_write
29: 000000000010000c 14 FUNC GLOBAL DEFAULT 1 _start
30: 0000000000107008 4 OBJECT GLOBAL DEFAULT 5 terminal_column
31: 00000000001002e0 68 FUNC GLOBAL DEFAULT 1 terminal_nsputs
32: 0000000000107000 0 NOTYPE GLOBAL DEFAULT 5 __bss_start
33: 00000000001000a0 98 FUNC GLOBAL DEFAULT 1 terminal_scroll
34: 000000000010700c 4 OBJECT GLOBAL DEFAULT 5 terminal_background_color
35: 0000000000000000 0 NOTYPE GLOBAL DEFAULT UND loader
36: 0000000000101028 4 OBJECT GLOBAL DEFAULT 2 TAB_SIZE
37: 0000000000102008 0 NOTYPE GLOBAL DEFAULT 4 _edata
38: 0000000000107018 0 NOTYPE GLOBAL DEFAULT 5 _end
39: 0000000000100250 33 FUNC GLOBAL DEFAULT 1 strlen
40: 0000000000100330 68 FUNC GLOBAL DEFAULT 1 terminal_puts
41: 0000000000107010 4 OBJECT GLOBAL DEFAULT 5 terminal_row
42: 0000000000100020 29 FUNC GLOBAL DEFAULT 1 terminal_putentryat
No version information found in this file.
OS was compiled with the latest gcc && binutils releases (9.1.0 and 2.32). Target is x86_64. Full source code is available on GitHub here:
https://github.com/YuRaZaKa/CactOS (linker script is located at src folder).
UPD: I don't understand the differences between VMA and file offset. Where I could read about that?