Author Topic: OpenBSD errors with NASM  (Read 21748 times)

Offline MrCBofBCinTX

  • Jr. Member
  • *
  • Posts: 18
    • Capuchado!
OpenBSD errors with NASM
« on: October 14, 2012, 03:20:22 PM »
I sent a message to the OpenBSD mailing list, but since it's the weekend, I am still waiting a reply.
I haven't used NASM for a while, too busy.
So this is a newer version of both NASM and OpenBSD.
nasm-2.10.04

Just running a quick test and I get these errors
Any thoughts?

I am getting errors with NASM. ( I also tried many other combinations. )

Code: [Select]
# nasm -f aoutb hello.asm
# ld -e _start -o hello hello.o
ld: warning: cannot find entry symbol _start; defaulting to 1c000114
# ./hello
./hello[1]: syntax error: `(' unexpected

or

Code: [Select]
# nasm -f aout  hello.asm
# ld -s -o hello hello.o
hello.o: file not recognized: File format not recognized

or

Code: [Select]
# nasm -f elf  hello.asm
# ld -s -o hello hello.o
ld: warning: cannot find entry symbol _start; defaulting to 1c000120
# ./hello
./hello[1]: syntax error: `(' unexpected

I have never had a problem previously.

I am running:

OpenBSD 5.2-current (GENERIC) #22: Mon Sep 24 18:31:52 MDT 2012

hello.asm:
Code: [Select]
%include    'system.inc'

section .data

hello   db  'Hello, World!', 0Ah
hbytes  equ $-hello

section .text

global  _start

_start:
push    dword hbytes
push    dword hello
push    dword stdout
sys.write
push    dword 0
sys.exit

system.inc:
Code: [Select]
%define stdin       0
%define stdout      1
%define stderr      2
%define SYS_nosys   0
%define SYS_exit    1
%define SYS_fork    2
%define SYS_read    3
%define SYS_write   4
%define SYS_open    5
%define SYS_close   6



section    .text
align 4
access.the.bsd.kernel:
    int 80h
    ret

%macro system           1
    mov eax, %1
    call    access.the.bsd.kernel
%endmacro

%macro sys.exit         0
    system  SYS_exit
%endmacro

%macro  sys.fork        0
    system  SYS_fork
%endmacro

%macro  sys.read        0
    system  SYS_read
%endmacro

%macro  sys.write       0
    system  SYS_write
%endmacro

%macro sys.open    0
    system  SYS_open
%endmacro

%macro  sys.close   0
    system  SYS_close
%endmacro
« Last Edit: October 14, 2012, 04:25:35 PM by Keith Kanios »

Offline Keith Kanios

  • Full Member
  • **
  • Posts: 383
  • Country: us
    • Personal Homepage
Re: OpenBSD errors with NASM
« Reply #1 on: October 14, 2012, 04:26:38 PM »
Please use code tags -- available by pressing the # button in the WYSIWYG editor.

Offline Keith Kanios

  • Full Member
  • **
  • Posts: 383
  • Country: us
    • Personal Homepage
Re: OpenBSD errors with NASM
« Reply #2 on: October 14, 2012, 04:48:45 PM »
1.) Split the problem in half. What version of NASM did you have before? Does that known working version create the same errors on newer OpenBSD. If possible, test the opposite as well, i.e. newer NASM on older OpenBSD.

2.) NASM 2.10.05 is available... update and check again.

3.) Try an alternate/simpler example and compare, such as http://asm.sourceforge.net/intro/hello.html#AEN124

Offline MrCBofBCinTX

  • Jr. Member
  • *
  • Posts: 18
    • Capuchado!
Re: OpenBSD errors with NASM
« Reply #3 on: October 14, 2012, 05:48:30 PM »
3.) Try an alternate/simpler example and compare, such as http://asm.sourceforge.net/intro/hello.html#AEN124

I tried this but got same error.
I am building 2.10.05, and will report back

Offline MrCBofBCinTX

  • Jr. Member
  • *
  • Posts: 18
    • Capuchado!
Re: OpenBSD errors with NASM
« Reply #4 on: October 14, 2012, 05:58:19 PM »
2.10.5 gave same error.

I cannot download a newer OpenBSD, due to bad internet connection where I am at.
Need to wait until I have time to go to better connection.
I have an older version in storage room.

I was either using nasm-2.10.tgz or nasm-2.08.01.tgz
not sure which one.
« Last Edit: October 14, 2012, 05:59:59 PM by MrCBofBCinTX »

Offline MrCBofBCinTX

  • Jr. Member
  • *
  • Posts: 18
    • Capuchado!
Re: OpenBSD errors with NASM
« Reply #5 on: October 14, 2012, 07:41:37 PM »
I also get error from nasm-2.10
at this point I am going to guess that it is OpenBSD -current version I am using.
nasm-2.08.01 is too old to load on my laptop

Offline Keith Kanios

  • Full Member
  • **
  • Posts: 383
  • Country: us
    • Personal Homepage
Re: OpenBSD errors with NASM
« Reply #6 on: October 15, 2012, 01:47:29 AM »
If you are on 64-bit (AMD64) OpenBSD, try the following:

Code: [Select]
nasm -f elf32 -o hello.o hello.asm
ld -m elf_i386 -o hello hello.o

Offline Keith Kanios

  • Full Member
  • **
  • Posts: 383
  • Country: us
    • Personal Homepage
Re: OpenBSD errors with NASM
« Reply #7 on: October 15, 2012, 02:18:13 AM »
Found this interesting older OpenBSD message: http://monkey.org/openbsd/archive/misc/0403/msg01148.html

Makes sense, as it seems like the binary is being interpreted as a shell script per the error message.

Based on the above message, try adding the following (mostly verbatim conversion) to your source code:

Code: [Select]
section .note.openbsd.ident
 align 2
 dd 8
 dd 4
 dd 1
 db 'OpenBSD',0
 dd 0
 align 2

Alternatively, use "main" in your source instead of "_start" and link via GCC instead of LD.

Offline MrCBofBCinTX

  • Jr. Member
  • *
  • Posts: 18
    • Capuchado!
Re: OpenBSD errors with NASM
« Reply #8 on: October 15, 2012, 02:35:13 PM »
This did not work.

I get error:
sfhello.asm:1: error: segment name `.note.openbsd.ident' not recognized

I will try with gcc

Offline MrCBofBCinTX

  • Jr. Member
  • *
  • Posts: 18
    • Capuchado!
Re: OpenBSD errors with NASM
« Reply #9 on: October 15, 2012, 03:02:57 PM »
Nevermind gcc. This works fine:

add note section then compile like this:

nasm -f elf32 -o sfhello.o sfhello.asm
ld -m elf_i386_obsd -o sfhello sfhello.o
./sfhello
Hello, world!