Friday, April 14, 2017

Re: programming in Assembly

On Fri, 14 Apr 2017, Kartik Agaram wrote:
> I'm trying out a simple Assembly program on a freshly installed "OpenBSD
> openbsd 6.1 GENERIC#291 i386":
>
> --- begin exit.s
...
> --- end
>
> This program assembles and links without error. However when I try to
> run it, I get this:
>
> $ as exit.s -o exit.o
> $ ld exit.o -o exit
> $ ./exit
> zsh: abort ./exit
>
> The identical program runs on 32-bit Linux.
>
> Any Assembly programmers in the house know what's up?

You need to decide what sort of executable you're trying to generate:
static or dynamic? By default ld generates a dynamic executables, but if
you do that you need to pass other options to set the interpreter. As is:

$ readelf -l exit

Elf file type is DYN (Shared object file)
Entry point 0x1b4
There are 8 program headers, starting at offset 52

Program Headers:
Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
PHDR 0x000034 0x00000034 0x00000034 0x00180 0x00180 R E 0x4
INTERP 0x001000 0x20000000 0x20000000 0x00013 0x00013 R 0x1
[Requesting program interpreter: /usr/lib/libc.so.1]
LOAD 0x000000 0x00000000 0x00000000 0x001bf 0x001bf R E 0x1000
LOAD 0x001000 0x20000000 0x20000000 0x000f2 0x000f2 R 0x1000
LOAD 0x001f94 0x20001f94 0x20001f94 0x0006c 0x0006c RW 0x1000
DYNAMIC 0x001f94 0x20001f94 0x20001f94 0x00060 0x00060 RW 0x4
NOTE 0x001014 0x20000014 0x20000014 0x00018 0x00018 R 0x4
GNU_RELRO 0x001f94 0x20001f94 0x20001f94 0x0006c 0x0006c R 0x1
...


The GNU ld default interpreter of /usr/lib/libc.so.1 isn't supported on
OpenBSD: we expect you to tell ld to record a real interpreter like
/usr/libexec/ld.so, or build static executables with -static.

$ ld exit.o -o exit -static
$ ./exit
$


Note that by default OpenBSD marks executables as PIE (position
independent executable) which places constraints on how code generates and
uses addresses. You'll need to link with the -nopie option if you can't
meet that requirement.


Philip Guenther

No comments:

Post a Comment