Page 1 of 1

Another undefined reference to _main...

Posted: Wed Sep 12, 2012 1:12 am
by phillid
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:

Code: Select all

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:

Code: Select all

gcc -c main.c -o o\main.o -nostdlib -nostdinc -fno-builtin -fno-stack-protector -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:

Code: Select all

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 are two things, one with the symbol 'main' and the other '__main', & I've declared my main function like this:

Code: Select all

int main()
{
    return 0xDEADBABA;
}
Here's boot.asm:

Code: Select all

MBOOT_PAGE_ALIGN    equ 1<<0
MBOOT_MEM_INFO      equ 1<<1
MBOOT_HEADER_MAGIC  equ 0x1BADB002
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 $
Can anyone please provide a solution to this problem?

Thanks guys

Re: Another undefined reference to _main...

Posted: Wed Sep 12, 2012 3:23 am
by Combuster
- Code from the assembly file is not assigned to a section.
- Not using a cross-compiler, and worse, using a windows toolchain with all its quirks.
- Using the A.out kludge without saying so.

Re: Another undefined reference to _main...

Posted: Wed Sep 12, 2012 6:22 am
by egos
phillid wrote:Here's the script I'm compilng, assembling and linking with:

Code: Select all

gcc -c main.c -o o\main.o -nostdlib -nostdinc -fno-builtin -fno-stack-protector -fno-leading-underscore
nasm boot.asm -o o\boot.o -fcoff
ld -o bin\kernel.bin o\boot.o o\main.o -Tlink.ld
Use _main stub. GCC uses _main function to call constructors when no startup files.

Re: Another undefined reference to _main...

Posted: Wed Sep 12, 2012 10:29 am
by Kazinsal
Combuster wrote:- Not using a cross-compiler, and worse, using a windows toolchain with all its quirks.
A lot of people seem to be doing this these days. I don't quite understand why. I mean, even a cross-compiler built on Windows with cygwin or MinGW is better than a native toolchain, and even then, you're much better off using even a VM running Linux or BSD...

But yeah. OP, build a cross-compiler. It's not that hard. At the very least, if you don't know how to ./configure and make install stuff (there's even instructions on the wiki), you probably have chosen the wrong hobby.

Re: Another undefined reference to _main...

Posted: Thu Nov 08, 2012 3:19 am
by BringerOfShadows
I'm having a similar problem. Currently running Debian Linux, everthing compiled directly on the system. heres the command chain I used:

Code: Select all

nasm -f elf -o start.o start.S
gcc -Wall -O -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -I./include -c -o main.o main.c
ld -T link.ld -o kernel.bin start.o main.o
output is

Code: Select all

start.o: In function 'stublet':
start.S:(.text+0x29): undefined reference to '_main'
Any idea how to fix this?

Re: Another undefined reference to _main...

Posted: Thu Nov 08, 2012 4:01 am
by Griwes
When you are shamelessly plugging into somebody else's thread, could you at least read it before?

Re: Another undefined reference to _main...

Posted: Thu Nov 08, 2012 4:15 am
by BringerOfShadows
I did, and all I've seen is people using windows, not linux. So sorry, but I cannot find any hints about compiling the sources on linux without problems.

Re: Another undefined reference to _main...

Posted: Thu Nov 08, 2012 4:21 am
by bluemoon
all I've seen is you do not read the answers. s/Windows/Linux/ and you got the answers(or recommendations).

Re: Another undefined reference to _main...

Posted: Thu Nov 08, 2012 9:01 am
by JAAman
the answer is always the same, doesn't matter if you are using windows, linux, or anything else -- the answer is always the same: use a cross-compiler

if you think that the cross compiler is only for windows, you are wrong

look here: GCC Cross-Compiler