link.ld problem

Programming, for all ages and all languages.
Post Reply
cheeky
Posts: 5
Joined: Thu Jun 07, 2007 11:38 am
Contact:

link.ld problem

Post by cheeky »

I was studying bran's kernel tutorial.

http://www.osdever.net/bkerndev/index.php

It works perfectly before I compile the source that prints on screen.

When I try to link scrn.c file on "printing onscreen" step, the string "Hello, world" exists at the beginning of "aout" binary file.

The hexdump which doesn't have a string "Hello, world!" shows as follows:

Code: Select all

0000000 00bc 1030 e900 0022 0000 9090 b002 1bad
0000010 0003 0001 4ffb e451 000c 0010 0000 0010
0000020 1000 0010 3000 0010 0000 0010 03e8 0000
0000030 9000 9090 8955 83e5 08ec e483 83f0 10ec
0000040 feeb 0000 c3c3 c3c3 548b 0424 0fec c0b6
0000050 8bc3 2454 0f04 44b6 0824 c3ee 0000 0000
0000060 0000 0000 0000 0000 0000 0000 0000 0000
*
0001000
You may see 00bc, blahblah... that's a code section.

However, If I link everything as described in tutorial, "Hello, world!" comes at the beginning as follows:

Code: Select all

0000000 6548 6c6c 2c6f 7720 726f 646c 000a 0000
0000010 0000 0000 0000 0000 0000 0000 0000 0000
*
0100000 00bc 1040 e900 0022 0000 9090 b002 1bad
0100010 0003 0001 4ffb e451 000c 0010 0000 0010
0100020 1004 0010 400c 0010 0000 0010 4de8 0000 
6548 blablah contains "Hello, world!" string, the actual code begins after this string. I believe that's why GRUB doesn't recognize this file.

The file GRUB recognize is described when using "file" utility:

kernel.bin: data

However, the latter one looks like ASCII format file.

Code: Select all

kernel.bin: ASCII text 

lastly, I post my linker script file that tutorial gave me.

Code: Select all

OUTPUT_FORMAT("binary")
ENTRY(start)
phys = 0x00100000;
SECTIONS
{
    .text phys : AT(phys)
    {
        code = .;
        *(.text)
        *(.rodata)
        . = ALIGN(4096);
    }
    .data : AT(phys + (data - code))
    {
        data = .;
        *(.data)
        . = ALIGN(4096);
    }
    .bss : AT(phys + (bss - code))
    {
        bss = .;
        *(.bss)
        . = ALIGN(4096);
    }
    end = .;
} 

I hope to get a good answer soon.
riding a bicycle in Seoul
urxae
Member
Member
Posts: 149
Joined: Sun Jul 30, 2006 8:16 am
Location: The Netherlands

Post by urxae »

Bran's tutorial has a bit of an omission in the linker script: the .rodata section. (This issue comes up frequently; you'll probably find tons of threads about this if you use the search function)
Anyway, try adding something like this to your linker script:

Code: Select all

    .rodata : AT(phys + (rodata - code))
    {
        rodata = .;
        *(.rodata)
        . = ALIGN(4096);
    }
Also, this post probably belongs in the "OS Developement" forum (if anywhere).
Post Reply