section .data and loading a kernel:

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
rubymonk
Posts: 14
Joined: Sat Mar 03, 2012 6:31 am

section .data and loading a kernel:

Post by rubymonk »

Hello,

I wrote a simple 16 bits piece of code and fail to understand something.
How is the .data section placed correctly in memory?

I have defined this ld script:

Code: Select all

OUTPUT_FORMAT(binary) 
ENTRY(kmain)

SECTIONS
{

	. = 0x1000;
	.text : {
	    code = .; _code = .; __code = .;
		*(.text)
		. = ALIGN(4096); 

	}
	. = 0x5000;
	.data :
	{
		data = .; _data = .; __data = .;
		*(.data)
		*(.rodata)
		. = ALIGN(4096);
	}

	.bss :
	{
		bss = .; _bss = .; __bss = .;
		*(.bss)
		. = ALIGN(4096);
	}

	end = .; _end = .; __end = .;
	kernelEnd = .;
}
Meaning I expect the .data section to be placed at 0x5000
EXTERN __data is correctly filled with 0x5000

In the following code, msg32gdt address is 0x5224 (I wrote it in memory and dumped to know that.)

Code: Select all

00005000  50 44 45 41 44 42 45 45  46 24 52 00 00 00 00 00  |PDEADBEEF$R.....|

Note the 24 52 after the DEADBEEF, address is 5224 (mov word [ds:bx +9], msg32gdt)

Code: Select all

[BITS 16]
[SECTION .data]
msg32gdt:				db 'K16 - Loading 32 bits GDT.', 0
---------------------------------------
The issue is that the "K16 - Load...." is placed at address 0x14224, so, 0x9000 after where expected.

Code: Select all

00014220  00 00 fa f4 4b 31 36 20  2d 20 4c 6f 61 64 69 6e  |....K16 - Loadin|

What are the mecanisms placing the .data section in memory?
Did I mess up my boot sector?
The difference is actually of 0xF000, this is the value I set the stack pointer at, is this impacting? How?

Here's my code:



Code: Select all

; init segs in 0x7c0
    mov ax, 0x07C0
    mov ds, ax
    mov es, ax
    mov ax, 0x8000    ; stack at 0xFFFF
    mov ss, ax
    mov sp, 0xf000


;cli
;hlt


; get boot drive
    mov [bootdrv], dl

; show msg
    mov si, msgload
    call asm16_display_writestring

; load kernel
    xor ax, ax
    int 0x13

    push es
    mov ax, BASE
    mov es, ax
    mov bx, 0
    mov ah, 2
    mov al, KSIZE
    mov ch, 0
    mov cl, 2
    mov dh, 0
    mov dl, [bootdrv]
    int 0x13
    pop es

    mov si, msgboot
    call asm16_display_writestring

	db 0xea
	dw 0, BASE




Thank you!
palk
Posts: 16
Joined: Mon Nov 15, 2010 8:30 pm

Re: section .data and loading a kernel:

Post by palk »

you're specifying virtual addresses instead of physical addresses in your linker script.

try something along the lines of:

Code: Select all

   .text 0x1000 : AT(0x1000) {
       ...
   }
   .data 0x5000: AT(0x5000) {
      ...
   }
rubymonk
Posts: 14
Joined: Sat Mar 03, 2012 6:31 am

Re: section .data and loading a kernel:

Post by rubymonk »

Thank you for this suggestion I didn't know about the AT keyword to define the LMA.

However, this leaves me in the same situation as before.
:(
rubymonk
Posts: 14
Joined: Sat Mar 03, 2012 6:31 am

Re: section .data and loading a kernel:

Post by rubymonk »

Found!

I was loading the kernel at 0x10000 (I specified 0x1000) not 0x1000 (I now specify) 0x100, this is now fixed.

This however doesn't quite answer the root question of all, what mecanism places the .data section in memory?
Also, the output format I use is "binary", where are specified the destination in memory of the different sections?

Thank you.
Post Reply