1

I just started to learn assembly by following "Programming From The Ground Up" and already hit my first issue with the first ever program. I got a segfault for the following code which is supposed to be an exit program:

.section .data

.section .text

.global _start
_start:
    movl $1, %eax
    movl $0, %edi
    int $0x80

I've looked into it and one thing suggested was to not use int $0x80 anymore since its a legacy way to invoke system call so I tried to use syscall instead but it didn't fix it.

the commands I used are as follow:

as test.s -o test.o
ld test.o -o test
./test

I am using the Windows Subsystem for Linux.

I tried to look at it in a debugger and what I found was that after my code, there would be an endless stream of add %al, (%rax) with each memory address from 0x40100c and onwards having this line.

I have absolutely no idea what is happening and would appreciate any help.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
undertaker
  • 17
  • 5
  • 2
    It works for me. What commands are you using to assemble, link and run the program? And you are running this on a Linux system, correct? – Nate Eldredge May 09 '21 at 19:54
  • @NateEldredge Im using `as` to assemble and `ld` to link and to run im simply just doing `./` – undertaker May 09 '21 at 19:55
  • The `add %al, (%rax)` is normal; the rest of the page following your code is filled with 0 bytes, which happen to be the encoding of the instruction `add %al, (%rax)`. It shouldn't matter because if your `exit` system call works, those bytes will never be executed. – Nate Eldredge May 09 '21 at 19:56
  • 2
    The full, exact commands please. The options you use are important. And please edit them into the question; in comments they are hard to read and may get deleted. – Nate Eldredge May 09 '21 at 19:56
  • `as test.s -o test.o` for the assemble, `ld test.o -o test` to link and then `./test` to run – undertaker May 09 '21 at 19:58
  • Running those exact commands successfully builds and runs the program for me, with no segfault. I'm on Ubuntu 20.04 x86-64. You're sure this is the source code you're building? (For instance, you saved your source file in your editor before assembling?) – Nate Eldredge May 09 '21 at 20:00
  • Are you using Windows Subsystem for Linux? (Although that would normally make int 0x80 itself segfault, at least WSL v1). If not, what distro are you using? Are you sure you built this exact source, with EAX=1, the correct `__NR_exit` for 32-bit code? (BTW, the 32-bit int 0x80 ABI takes its first arg in EBX, although that's already 0 in `_start` in a static executable.) – Peter Cordes May 09 '21 at 20:01
  • Also, try running `strace ./test` to see what system call it thinks you're making. – Peter Cordes May 09 '21 at 20:04
  • 2
    Note for your `syscall` test that you can't simply replace `int $0x80` with `syscall` and nothing else; they use different registers and different system call numbers. See https://stackoverflow.com/a/19256967/634919. You'll have to replace `movl $1, %eax` with `movl $60, %eax`. – Nate Eldredge May 09 '21 at 20:04
  • I am using the WSL, I should have mentioned that in the post I do apologize. – undertaker May 09 '21 at 20:04
  • 1
    Ok, so does the exact code in your question actually segfault? Or are you using WSL2 where a real Linux kernel runs in a VM, so it can support CONFIG_IA32_EMULATION? [edit] to include full details, and result of running under strace. And make sure the code block in the question matches what you're actually running. – Peter Cordes May 09 '21 at 20:06
  • I am running WSl 1 so I'll download WSL 2 and see if that fixes the issue, if not I'll update you with more information. Other than the the exact code was what was run and as for strace this was what I got: `execve("./exit", ["./exit"], 0x7fffded5c6c0 /* 19 vars */) = 0 --- SIGSEGV {si_signo=SIGSEGV, si_code=SI_KERNEL, si_addr=NULL} --- +++ killed by SIGSEGV (core dumped) +++ Segmentation fault (core dumped)` – undertaker May 09 '21 at 20:11
  • @PeterCordes yes so having as WSL1 was the issue, it works on WSL 2. Thank you very much – undertaker May 09 '21 at 20:29
  • Yup, if you want to follow examples from a book / tutorial, don't try to port them to a different ISA / system-calling convention while you're still learning the basics they're trying to teach. See [Assembling 32-bit binaries on a 64-bit system (GNU toolchain)](https://stackoverflow.com/q/36861903) for how to build an actual 32-bit binary, instead of using 32-bit system calls in a 64-bit binary. – Peter Cordes May 09 '21 at 22:30

0 Answers0