开发者

Linking pe-i386 bootsector

I made two object files. One is made with nasm -f win32:

C:\home\os> objdump -d boot.obj

boot.obj:     file format pe-i386

Disassembly of section .text:

00000000 <.text>:
   0:   02 d0                   add    %al,%dl
   2:   ba 01 03 00 00          mov    $0x301,%edx
   7:   00 fb                   add    %bh,%bl
   9:   2f                      das
   a:   45                      inc    %ebp
   b:   fe 00                   incb   (%eax)
        ...
  19:   00 00                   add    %al,(%eax)
  1b:   00 20                   add    %ah,(%eax)
  1d:   00 00                   add    %al,(%eax)
  1f:   00 53 fa                add    %dl,-0x6(%ebx)
  22:   e8 00 00 00 00          call   27 <.text+0x27>
  27:   fa                      cli
  28:   f4                      hlt

And the other is made with gcc -c main.c -nostdlib -nostdinc -fno-builtin -o main.o

C:\home\os>objdump -d main.o

main.o:     file format pe-i386


Disassembly of section .text:

00000000 <_main>:
   0:   55                      push   %ebp
   1:   89 e5                   mov    %esp,%ebp
   3:   83 ec 08                sub    $0x8,%esp
   6:   83 e4 f0                and    $0xfffffff0,%esp
   9:   b8 00 00 00 00          mov    $0x0,%eax
   e:   83 c0 0f                add    $0xf,%eax
  11:   83 c0 0f                add    $0xf,%eax
  14:   c1 e8 04                shr    $0x4,%eax
  17:   c1 e0 04                shl    $0x4,%eax
  1a:   89 45 fc                mov    %eax,-0x4(%ebp)
  1d:   8b 45 fc                mov    -0x4(%ebp),%eax
  20:   e8 00 00 00 00          call   25 <_main+0x25>
  25:   e8 00 00 00 00          call   2a <_main+0x2a>
  2a:   b8 ef be ad de          mov    $0xdeadbeef,%eax
  2f:   c9                      leave
  30:   c3                      ret
  31:   90                      nop
  32:   90                      nop
  33:   90                      nop

Now, I try to link these using.. ld It uses this template:

OUTPUT_FORMAT("pe-i386")
ENTRY(start)
SECTIONS
{
    .text 0x100000 :
    {
        code = .; _code = .; __code = .;
        *(.text)
        . = ALIGN(4096);
    }
    .data :
    {
        data = .; _data = .; __data = .;
        *(.data)
        *(.rodata)
        . = ALIGN(4096);
    }
    .bss :
    {
        bss = .; _bss = .; __bss = .;
        *(.bss)
        . = ALIGN(4096);
    }
    end = .; _end = .; __end = .;
}

But it doesn't work:

ld -Tlink.ld -o kernel boot.obj main.o
ld: warning: cannot find entry symbol start; defaulting to 00100000
boot.obj:boot.s:(.text+0x23): undefined reference to `main'
main.o:main.c:(.text+0x21): undefined reference to `_alloca'
main.o:main.c:(.text+0x26): undefined reference to `__main'
make: *** [link] Fout 1

What am I missing? I'm not used to working with windows, with ELF files I do not really run into problems. Am I supposed to use a different linker? I supposes it calls alloca because I'm missing a -fno-stack-protecto开发者_StackOverflow社区r, but my gcc does not grok that flag..


Ok, I managed to "fix" it by adding a dummy __main function to main.c, and calling _main in boot.s instead of "main". Can anyone explain this, or provide a more elegant solution?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜