phillid
phillid

Reputation: 198

Yet another 'undefined reference to _main'

I've been attempting to convert the bulk of my current OS project from x86 Assembly over to C and assemble with NASM and compile with MinGW. When linking, I get these errors:

ld: warning: cannot find entry symbol start; defaulting to 00100000
o\main.o:main.c:(.text+0x7): undefined reference to `_main'

Here's the script I'm compilng, assembling and linking with:

gcc -c main.c -o o\main.o -fno-leading-underscore
nasm boot.asm -o o\boot.o -fcoff
ld -o bin\kernel.bin o\boot.o o\main.o -Tlink.ld

...and my linker script is the following:

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 = .;
}

When I use nm on main.o, it says that there is something with the symbol '__main', but I've declared it like this:

int main()
{
    return 0xDEADBABA;
}

Here's boot.asm:

MBOOT_PAGE_ALIGN    equ 1<<0    ; Load kernel and modules on a page boundary
MBOOT_MEM_INFO      equ 1<<1    ; Provide your kernel with memory info
MBOOT_HEADER_MAGIC  equ 0x1BADB002 ; Multiboot Magic value
MBOOT_HEADER_FLAGS  equ MBOOT_PAGE_ALIGN | MBOOT_MEM_INFO
MBOOT_CHECKSUM      equ -(MBOOT_HEADER_MAGIC + MBOOT_HEADER_FLAGS)
[bits 32]
[global mboot]
[extern code]
[extern bss]
[extern end]

mboot:
    dd  MBOOT_HEADER_MAGIC
    dd  MBOOT_HEADER_FLAGS
    dd  MBOOT_CHECKSUM
    dd  mboot
    dd  code
    dd  bss
    dd  end
    dd  start

[extern main]
[global start]

start:
    push ebx
    cli
    call main
    jmp $

I suspect that this problem I'm having is because of Microsoft being their usual, stupid selves and requiring some sort of underscore prefix or something. Can anyone please provide a solution to this problem? Cheers.

Upvotes: 0

Views: 2350

Answers (1)

epsalon
epsalon

Reputation: 2294

I suspect the issue is you did not link in the standard C library, which implements _main as the main entry point of the program. _main calls main and does some initialization before and cleanup afterwards. Try using void _main() and exit() instead of return.

Upvotes: 1

Related Questions