On Sun, Feb 06, 2022 at 03:36:49PM +0100, Gabriele Pelissetto wrote:
> Hi, I'm having problems running a program I wrote.
> I wrote this program that should just exit with exitcode 44:
>
> // prog.S
> #include <sys/syscall.h>
> .text
> .globl _start
> _start:
> subl $8, %esp
> pushl $44
> movl $SYS_exit, %eax
> pushl $0
> int $0x80
>
> I compiled with
>
> $ cc prog.S -nostdlib -o a.out
>
> and run
>
> $./a.out
>
> Doing so on FreeBSD 13.0-RELEASE i386 (clang 11.0.1) worked fine.
> In fact, the executable runs and the exit code of the program is 44
> as it should be.
>
> However, doing the same on OpenBSD 7.0 GENERIC.MP#5 i386
> and on NetBSD 9.2 i386 (gcc 7.5.0), the kernel refused to execute the
> code and it was passed to the shell, which of course failed:
>
> openbsd$ ./a.out ./a.out[1]: syntax error: `(' unexpected
>
> openbsd$ file a.out
>
> a.out: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV),
> statically linked, not stripped
>
> openbsd$ objdump -d a.out
>
> a.out: file format elf32-i386
>
> Disassembly of section .text:
>
> 00001184 <_start>:
> 1184: 83 ec 08 sub $0x8,%esp
> 1187: 6a 2c push $0x2c
> 1189: b8 01 00 00 00 mov $0x1,%eax
> 118e: 6a 00 push $0x0
> 1190: cd 80 int $0x80
>
>
> I saw, however, that changing _start to main and compiling without
> -nostdlib worked fine.
> What am I doing wrong?
Hi, I tried your asm program and came up with the following:
uranus$ cc -nostdlib -static -o asmtest asmtest.S
uranus$ file asmtest
asmtest: ELF 32-bit LSB shared object, Intel 80386, version 1
uranus$ ./asmtest
uranus$ echo $?
44
I believe this is what you want. I had to add the following before the
.text section (based on my work with the RISCV group document found here:
https://github.com/pbug44/openbsd-riscv-misc/blob/master/init.S, originally
from Brian Bamsch):
.section ".note.openbsd.ident", "a"
.p2align 2
.long 8
.long 4
.long 1
.ascii "OpenBSD\0"
.long 0
.previous
If you google for ".section \".note.openbsd.ident\"..." you'll find a few
hits and they will explain to you how and why this works hopefully.
I'm very much a ASM newbie, so I can't tell you why this works exactly. It
does modify the ELF sections though.
Best Regards,
-peter