0

I recently started learning assembly and was wondering if it is possible for us to have our own defined entry point for an assembly code when compiling with gcc?

For example the standard code that compiles with gcc is

global main

section .data

section .bss

section .text

main:

I would like to change the entry point to a more defined name such as "addition", something like this below.

global addition

section .data

section .bss

section .text

addition:

A reason for why im using gcc to compile in the first place as well is that im using c libraries in my assembly code for "printf" and "scanf", and everytime I tried to change the entry point, I would get an undefined reference to main error.

  • 1
    No, you can't do that. You can choose the entry point for the process, but if you use C that is then inside the C library which calls your `main` and that can't be changed. If your program does addition, then name the executable file addition, not the function. Or, create a separate addition function and call it from main. – Jester Feb 07 '19 at 22:37
  • thanks for the answer! I have a follow up question, if the entry point has to be defined as main, then if I were to try to call my assembly code from a c file with extern "C" int main(int); , would it still work even though the name of the entry point is main, which I think might conflict with the c body of int main()? – Kingsley Tran Feb 07 '19 at 22:43
  • It won't work. You will have linker error, symbol defined multiple times. If you have a `main` already then name your function whatever you want and invoke it. – Jester Feb 07 '19 at 23:14
  • the object files produced by compiling .c and assembling .asm are "equal", i.e. if two object files contain function "main", the linker will report duplicity. If none object file contains required symbol by some code, it will report missing symbol. So it doesn't matter whether you define your `main` in C or assembly, but you must do that only once. Usually it's simpler to have `main` in C, and write in assembly only specific subroutines like `addition`. – Ped7g Feb 08 '19 at 02:02

2 Answers2

3

If you are writing in assembly and not using the C runtime library, then you can call your entry point whatever you want. You tell the linker what the name of the entry point is, using either the gcc command line option -Wl,--entry=<symbol> or the ENTRY directive in the linker script. The linker writes the address of this entry point in the executable file.

If you are using the C runtime library, then the entry point in the executable file needs to be the entry point of the C runtime library, so that it can perform initialization. This entry point is typically called crt0. When crt0 finishes initializing, it calls main, so in this case, you cannot change the name.

prl
  • 11,716
  • 2
  • 13
  • 31
2

You can put multiple labels on the same address. So you can stick the main label at whatever place you want the CRT startup code to call.

global main
main:
addition:
   lea  eax, [rdi+rdi]    ; return argc*2
   ret

I checked, and GDB chooses to show main in the disassembly for the block of code following the label, regardless of which one you declare first. (`global addition doesn't help either.)


Of if you want to be able to change one line at the top of your file to select which function is the main entry point, you could maybe do

%define addition main

I'm not sure if NASM lets you create an alias or weak-alias for a symbol, like with GAS
.weakref main, addition. (Call a function in another object file without using PLT within a shared library?)

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847