0

Having this from compiler:

.file   "b.c"
    .text
    .globl  _start
    .type   _start, @function
_start:
    pushq   %rbp
    movq    %rsp, %rbp
    nop
    popq    %rbp
    ret
    .size   _start, .-_start
    .ident  "GCC: (Debian 8.3.0-6) 8.3.0"
    .section    .note.GNU-stack,"",@progbits

should be simple, but why does it sigint? (Command terminated), when there is nothing the program does?

Build with

$cc -S -fno-asynchronous-unwind-tables -fno-dwarf2-cfi-asm b.c
Herdsman
  • 799
  • 1
  • 7
  • 24
  • `nop` isn't causing sigint. If this is on Linux then the `ret` is likely causing an exception since there is no where to `ret` assuming you made the entry point of your program at `_start` – Michael Petch Jun 02 '20 at 11:28
  • by why is this default from gcc compiler? (compiled `void _start() { return; };`). Why does compiler use `ret` and not something like interupt? (to avoid the sigint). – Herdsman Jun 02 '20 at 11:33
  • 1
    GCC doesn't know `_start` is special. That C code defines `_start` as a normal function, but then you link it (presumably with `gcc -nostdlib foo.c`) as the ELF entry point. Don't do that. `_start` has to be a `noreturn` function, and also it's entered with RSP 16 byte aligned, not RSP+8 16 byte aligned. – Peter Cordes Jun 02 '20 at 11:36
  • Can you show the command you use to build the executable. I will assume you are overriding the entry point so it points at `_start` and not `main`. If that is what you do... the GCC compiler has no knowledge of who (if anything) called `_start`. returning from `main` works when the C library is used because there is C startup code that initializes things and then calls `main` and there is a place to return to (the C startup code) which will do the termination you expect. If you want this code to exit you will gave to call the exit system call yourself at bottom of `_start`. – Michael Petch Jun 02 '20 at 11:38
  • Also, are you sure you get SIGINT? I'd expect SIGSEGV. – Peter Cordes Jun 02 '20 at 11:38
  • *All identifiers that begin with an underscore are always reserved for use as identifiers with file scope in both the ordinary and tag name spaces.* - if you want to play without a net by messing around with `_name` identifiers, you have to follow the rules. GCC isn't going to hold your hand for you. [use \_ and \_\_ in C programs](https://stackoverflow.com/q/25090635) – Peter Cordes Jun 02 '20 at 11:43
  • @PeterCordes I am not building it with `-nostdlib`, I has built it as shown by default (that is - with `ret` opertation). I do not know how to make gcc to build `noreturn` as you suggest, but gcc didn't it by default in my case – Herdsman Jun 02 '20 at 12:15
  • You can't execute a `.s` file directly. If you ran this code, obviously you had to build it into an executable. If you did that with `as` + `ld` manually, that's equivalent to `gcc -nostdlib -static`, not building the standard way for that C implementation (where the implementation's `crt` `.o` files provide a `_start` that was written by hand in asm). To make a function not return, you have to call a function like `exit` or `abort` that doesn't return. – Peter Cordes Jun 02 '20 at 12:27
  • @PeterCordes so how can I add `stdlib` to `as` compilation? (not gcc). I did manually but the as compiler should be able to link with other libreries (such as the `stdlib`), then how can I do so? – Herdsman Jun 02 '20 at 12:31
  • `as` is just an assembler, not a compiler and not a linker. You can make an exit system call from inline asm as show in [Compiling without libc](https://stackoverflow.com/q/2548486). But re: linking differently: See also [Call C functions from 64-bit assembly](https://stackoverflow.com/q/43837435), and [Linking a program using printf with ld?](https://stackoverflow.com/q/55314762) / [How can I link dynamically to glibc in Ubuntu](https://stackoverflow.com/q/61432284) – Peter Cordes Jun 02 '20 at 12:39
  • @PeterCordes for your first example (compiling without libc), I need to use `-nostartfiles` and not `-nostdlib`, otherwise `undefined reference to puts'` in `printf` statement. But do not understand the difference between these two flags then, why to use one over the other? – Herdsman Jun 02 '20 at 12:49
  • That's correct. As the first line of the answer there says "*If you compile your code with `-nostdlib`, you won't be able to call any C library functions (of course)*". I don't know what you're trying to compile; all you showed was a `_start` that tried to `ret` instead of `syscall`. If you want to use stdio functions (generally a bad idea from `_start`), then yes you need libc, but `-nostartfiles` can still omit the CRT startup code that calls libc init functions. On GNU/Linux it can initialize itself if dynamically linked, otherwise libc functions will crash. – Peter Cordes Jun 02 '20 at 12:53
  • Can some link for `CRT startup`? I do not know what it is, who it calls, when it calls and its purpose. Some predefined function calls do not say me anything. I would like to know more about it – Herdsman Jun 02 '20 at 13:01
  • @PeterCordes here -> in this link you provided `https://stackoverflow.com/questions/55314762/linking-a-program-using-printf-with-ld`, you are using nasm syntax, which has `extern printf`, but I do not know how to make extern printf in gas. Also, I see there no linking with libc/glibc. From the code I see only reference to the printf by the extern, but in compilation, you only use `-no-pie`, and `-nostartfile`, but you do not `ld file.o libc.so`, so how does gcc knows about it? I would like to have it writtne in gas to see it, can you do this? – autistic456 Jun 04 '20 at 11:38
  • You don't need `extern` in GAS, any undefined symbol is just assumed to be `extern`. If you have a different question, like how to link a program that uses `printf`, ask it. (Although it's probably a duplicate of one of those: the key point is `gcc -no-pie -nostartfiles`, not `-nostdlib`, if you want GCC to still link libc. GCC links libc by default unless you tell it not to with `-nostdlib`, remember it's a front-end for a C compiler as well as an assembler. Use `gcc -v ... other options ...` to see what actual `as` and `ld` commands it runs.) – Peter Cordes Jun 04 '20 at 11:55
  • @PeterCordes but my point to LINK only the libc. That is, linked its dynamic library on `ld`, something like `ld file.o libc.so`. I do not want to go throught `gcc`, but do it manually. (as->ld). So how to REALLY **link** the libc to with gas? – autistic456 Jun 04 '20 at 12:18
  • `as` is an assembler, not a linker. With `ld`, yes, just pass your `file.o` and `-lc`, and other necessary options like `-dynamic-linker /lib64/ld-linux-x86-64.so.2`. If you want to know how GCC invokes `ld`, run `gcc -v foo.s`, then start leaving out `ld` options until it breaks. Then you'll know that one wasn't optional. – Peter Cordes Jun 04 '20 at 12:28

0 Answers0