Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entry point of different languages

I know that in C/C++ and Java, the entry point of a program is the function main(),
now I have the following two questions,

  1. What's the program's entry point written in MASM, NASM, and other languages?

  2. What's the convention from which CPU knows where to find entry point of a program?

==-EDIT-==

Question 2 is not a meaningful question since it is wrong that CPU takes responsibility of finding the entry point. There is no such convention. See Eric Lippert's clarification.

like image 463
dastan Avatar asked Dec 12 '25 15:12

dastan


2 Answers

In assembly (both MASM and NASM are merely assemblers, i.e. programs that convert assembly source code to machine code) there is no default entrypoint. You typically specify it with an assembler directive.

  • With NASM, you use the .start directive to place the entrypoint.
  • With MASM it seems more complicated, but the end directive is important.

The address referenced then ends up in the binary (executable) file's header, so that the operating system can figure out where to jump.

For ELF binaries (used on many operating systems) see the e_entry header field:

e_entry

This member gives the virtual address to which the system first transfers control, thus starting the process. If the file has no associated entry point, this member holds zero.

This happens with C too, except there of course the compiler sits between your source and the executable file, so it inserts the required reference to main() (or, actually, typically to an init routine that runs before main().

Java does not work with raw binaries, its programs on a JVM, so it doesn't really compare.

like image 157
unwind Avatar answered Dec 14 '25 10:12

unwind


The entry point is not usually defined by the compiler, but by the linker. See for example ld --entry for GCC or link /ENTRY: for VC++.

In C and C++, the default entry point is usually in the standard library (_start in GCC IIRC) and that function eventually calls the user main().

Java, having the JVM, the program entry point is hard coded in the JVM.

If you program in assembly, it depens on whether you use some language's standard library:

  • If you use the standard C library, add a main() function to your program, and all will just work as in C.
  • If you do not use the standard C library, choose your own entry point and pass its name to the linker. Alternatively, you can name your entry point with the default name for your os (_start).

For example, compare the error messages of these two commands, when compiling and linking an empty C file:

$ gcc empty.c
/usr/lib/gcc/i686-linux-gnu/4.8/../../../i386-linux-gnu/crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: error: ld returned 1 exit status

$ gcc -nostdlib empty.c
/usr/bin/ld: warning: cannot find entry symbol _start; defaulting to 00000000080480b8

You can change the entry name with:

$ gcc -nostdlib -Wl,-entry,begin empty.c
/usr/bin/ld: warning: cannot find entry symbol begin; defaulting to 00000000080480b8
like image 30
rodrigo Avatar answered Dec 14 '25 09:12

rodrigo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!