error: undefined reference to __main

841 views
Skip to first unread message

test orange

unread,
Jun 19, 2010, 3:20:49 PM6/19/10
to minix3
Hi all,

See blow:

----------------------------------------------------------------------------------------
=========================
Proc A: entry.asm
=========================

extern    _main
extern    _exit

bits 32

[section .text]

global _start

_start:
    push    eax
    push    ecx
    call    _main
   

    push    eax
    call    _exit

    hlt   
----------------------------------------------------------------------------------------


----------------------------------------------------------------------------------------
=========================
Proc B: b.c
=========================
#include "stdio.h"

int main(int argc, char * argv[])
{
    printf("/\n");
    return 0;
}
----------------------------------------------------------------------------------------


========================
Compile command
========================
nasm entry.asm -o entry.o -I ../include/ -f aoutm
gcc -c -fno-builtin -fno-stack-protector -Wall -I ../include/type.h ../include/stdio.h b.c -o b.o
gld -Ttext 0x0 -o b b.o entry.o   

=========
Error
=========
error: undefined reference to __main

?? Why error ??

Furthermore, my env was minix3,gcc,gld,nasm(support aout). Above was part of my project,not all code.

Erik van der Kouwe

unread,
Jun 19, 2010, 3:39:55 PM6/19/10
to minix3
Hi,

There is this comment in kernel/klib.S:

* GNU CC likes to call ___main from main() for nonobvious reasons.

Maybe this is related, in which case you could define the symbol as a
function that only returns.

To debug this, you can use the /usr/gnu/bin/{gnm,objdump} tools to
find referenced symbols and/or disassemble the object files.

If you find something weird, I recommend you also try with the ACK and/
or GNU assemblers. NASM is not used by the MINIX team, so we don't
know whether and how well it works. Note that being a.out is not a
guarantee for binary compatibility, especially for object files. There
are different formats that are incompatible to different degrees.

Another remarks: leading underscores in C symbols have at some point
been removed and were later added again. Check in the .S files in the
source trees which is the default the MINIX revision you are using.

With kind regards,
Erik

test orange

unread,
Jun 19, 2010, 3:58:44 PM6/19/10
to min...@googlegroups.com
I also tried to used another name instead of main and I attached the program blow:

------------------------------
----------------------------------------------------------
=========================
Proc A: entry.asm
=========================

extern    _shell_main

extern    _exit

bits 32

[section .text]

global _start

_start:
    push    eax
    push    ecx
    call    _shell_main

   

    push    eax
    call    _exit

    hlt   
----------------------------------------------------------------------------------------


----------------------------------------------------------------------------------------
=========================
Proc B: b.c
=========================
#include "stdio.h"

int shell_main(int argc, char * argv[])

{
    printf("/\n");
    return 0;
}
----------------------------------------------------------------------------------------


I could compile successfully and no warning or error. But failed in running(Only print "\" on the screen but could not return successfully). I debugged step by step and found that the program could enter shell_main() successfully but failed to return. I compiled the code above(unchange the function name, still used main) in linux with ELF format and found that it could both compile and run successfully. Is it the problem in the function name of "main"? I was not sure . I only remembered that the name of "main" was not a "must" , for it could be another name instead(such as shell_main). Just made sure to the command of jmp/call to shell_main was correct........


2010/6/20 Erik van der Kouwe <eri...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "minix3" group.
To post to this group, send email to min...@googlegroups.com.
To unsubscribe from this group, send email to minix3+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/minix3?hl=en.


Erik van der Kouwe

unread,
Jun 19, 2010, 4:03:59 PM6/19/10
to min...@googlegroups.com
Hi,

> I could compile successfully and no warning or error. But failed in
> running(Only print "\" on the screen but could not return successfully).
> I debugged step by step and found that the program could enter
> shell_main() successfully but failed to return. I compiled the code
> above(unchange the function name, still used main) in linux with ELF
> format and found that it could both compile and run successfully. Is it
> the problem in the function name of "main"? I was not sure . I only
> remembered that the name of "main" was not a "must" , for it could be
> another name instead(such as shell_main). Just made sure to the command
> of jmp/call to shell_main was correct........

Still, my advice would be to use objdump to check out the generated
binaries. Who knows what magic the C compiler may do to main to 'help'
the run-time start-off.

With kind regards,
Erik

Reply all
Reply to author
Forward
0 new messages