Another undefined reference to _main...

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
phillid
Member
Member
Posts: 58
Joined: Mon Jan 31, 2011 6:07 pm

Another undefined reference to _main...

Post 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
phillid - Newbie-ish operating system developer with a toy OS on the main burner
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Another undefined reference to _main...

Post 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.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
egos
Member
Member
Posts: 612
Joined: Fri Nov 16, 2007 1:59 pm

Re: Another undefined reference to _main...

Post 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.
If you have seen bad English in my words, tell me what's wrong, please.
User avatar
Kazinsal
Member
Member
Posts: 559
Joined: Wed Jul 13, 2011 7:38 pm
Libera.chat IRC: Kazinsal
Location: Vancouver
Contact:

Re: Another undefined reference to _main...

Post 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.
BringerOfShadows
Posts: 15
Joined: Thu Nov 08, 2012 2:59 am

Re: Another undefined reference to _main...

Post 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?
User avatar
Griwes
Member
Member
Posts: 374
Joined: Sat Jul 30, 2011 10:07 am
Libera.chat IRC: Griwes
Location: Wrocław/Racibórz, Poland
Contact:

Re: Another undefined reference to _main...

Post by Griwes »

When you are shamelessly plugging into somebody else's thread, could you at least read it before?
Reaver Project :: Repository :: Ohloh project page
<klange> This is a horror story about what happens when you need a hammer and all you have is the skulls of the damned.
<drake1> as long as the lock is read and modified by atomic operations
BringerOfShadows
Posts: 15
Joined: Thu Nov 08, 2012 2:59 am

Re: Another undefined reference to _main...

Post 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.
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: Another undefined reference to _main...

Post by bluemoon »

all I've seen is you do not read the answers. s/Windows/Linux/ and you got the answers(or recommendations).
User avatar
JAAman
Member
Member
Posts: 879
Joined: Wed Oct 27, 2004 11:00 pm
Location: WA

Re: Another undefined reference to _main...

Post 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
Post Reply