clinuxassemblygnu-assembler

cannot find entry symbol _start when trying to link c main with assembly function


Can read assembly but trying to learn to write small functions in it, but I can't seem to link a main written in C that calls an extern function written with gas. The target is linux x64.

Here is main.c:

#include <stdio.h>

extern int somefunc();

int main() {
  int x = somefunc();
  printf("returned %d\n", x);
  return 0;
}

here is sf.s:

.global somefunc

.data
.text

somefunc:
  movl $123, %eax
  ret

Here are the compilation commands:

> gcc -c -fno-pie main.c -o main.o
> as sf.s -o sf.o
> ld -lc main.o sf.o -o m
ld: warning: cannot find entry symbol _start; defaulting to 0000000000401020

Solution

  • Those fings in gcc are usually defined in specfiles. You can see the default file by using -dumpspecs command line option.