News for hackers. Stuff that matters.
Re:What happens before main() is executed in C and why is it... by textmode on Friday May 04, 2018 @06:34PM (#16998972)
I am a neophyte x86 asm user.

I link using gcc and -nostdlib plus a very simple start.S:

   .globl _start
   _start:
   call main
   movl $1,%eax
   xorl %ebx,%ebx
   int $0x80
fasm 1.asm;gcc -nostdlib start.S 1.o lib1.a;a.out

"lib1.a" contains only the functions needed by a.out instead of, e.g., every function in libc.a

I use ar -M < 1.mri to script changes to lib1.a

(Did I forget about crt0.o?)

Re:What happens before main() is executed in C and why is it... by textmode on Saturday May 05, 2018 @12:31PM (#17002497)
Below is a sample "1.asm" source file which in one expt yields a 2.2K static binary.

"main" has been renamed to "xyz".

    format ELF
    section '.text' executable
    public xyz 

    extrn _exit
    extrn write

    xyz: 
    mov eax,len00
    push eax
    push char00
    mov eax,1
    push eax
    call write
    call _exit

    section '.data' writeable

    char00 db "21 symbols", 0xA 
    len00 = $ - char00
Re:What happens before main() is executed in C and why is it... by textmode on Saturday May 05, 2018 @11:11PM (#17005059)
(1.3K stripped)
Re:What happens before main() is executed in C and why is it... by saagarjha on Friday May 04, 2018 @06:52PM (#16999085)
Why even have the overhead of a function call to main in that case?
Re:What happens before main() is executed in C and why is it... by Vogtinator on Saturday May 05, 2018 @08:55AM (#17001607)
Because you need a syscall to exit the process.
Re:What happens before main() is executed in C and why is it... by saagarjha on Saturday May 05, 2018 @07:54PM (#17004534)
Is that not what this part of the code does?

  movl $1,%eax
  int $0x80