Compile Linux-ready assembly on OSX

5

I'm trying to do a compiler study, so I have to compile an assembly of a language called howto with nasm.

The intermediate code generated by the howto compiler is as follows:

extern printf
segment .text
align 8
global main:function
main:
segment .rodata
align 8
_L1: db 'hello', 10, 0
segment .text
    mov rax, 0
    mov edi, $_L1
    call printf
    mov rax, 0
    ret

On the OSX terminal I make $ nasm -f macho out.asm , and I get compile error because of main:function , if removing :function ends with this first error when trying to compile again. One more error, but this time it just says I should use macho64 because of the system architecture, I try to compile with $ nasm -f macho64 out.asm and I get the error:

  

out.asm: 11: error: Mach-O 64-bit format does not support 32-bit absolute addresses

After this I can no longer compile, but if I use ELF64 I can create the file out.o .

At this point with the out.o file to compile with clang, I make $clang out.o -v to return the whole verbose and I get the error:

Apple LLVM version 6.1.0 (clang-602.0.53) (based on LLVM 3.6.0svn)
Target: x86_64-apple-darwin14.4.0
Thread model: posix
  "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -dynamic -arch x86_64 -macosx_version_min 10.10.0 -o a.out out.o -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/6.1.0/lib/darwin/libclang_rt.osx.a
  ld: warning: ignoring file out.o, file was built for unsupported file format ( 0x7F 0x45 0x4C 0x46 0x02 0x01 0x01 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 ) which is not the architecture being linked (x86_64): out.o
Undefined symbols for architecture x86_64:
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

From this point I can not go any further, I tried to link by ld with $ ld -e _main out.o and $ ld -e _main out.o -arch x86_64 but in these cases I get the error message:

Undefined symbols for architecture x86_64:
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64

In linux the compilation with $ nasm -f elf64 out.asm was without problems the initial way of code and $ gcc out.o also generating the file a.out that when executing returned the response Hello of the expected message. But as I have to finish this project on OSX I ask the community for help.

    
asked by anonymous 22.07.2015 / 01:28

2 answers

1

I'm not sure what I'm saying !!! but here's how it goes:

The architecture of intel 80-86 and descendants is a mess!

(unlike Linux) on OSX the address space exceeds 2 ^ 32 so it does not usually support 32-bit absolute addressing.

update : Therefore: use 64bit or relative records:

64-bit registers:
global main
extern puts

segment .text
main:
    push rbp      ; (guardar o frame pointer -- *1)

    mov rax, 0
    mov rdi, _L1  ; usando o registo de 64 bits corresp ao edi
    call puts

    pop rbp       ; (retaurar o frame pointer -- *1)
    mov rax, 0    ; (return 0 do C)
    ret

_L1: 
   db "hello",  0

Notes:

  • lines *1 are not needed for this case but are good practice ...

  • I used puts instead of printf to get simpler

alternatively - use relative addresses (no good practices ...):

global main
extern puts

segment .text
main:
    mov eax, 0          ; registo de 32 bits correspondente ao rax
    lea edi, [rel _L1]  ; registo de 32 bits...             ao rdi
    call puts
    ret

_L1:
   db "hello",  0

Untested ... I would expect these versions to work on both macho64 (OSX) and elf64 (linux). FEDERAL POLICE. tells me if it works: - I do not have 64bit machines to test

    
22.07.2015 / 11:56
0

According to this post in English:

The address of the 'hello' message must be relative.

And, according to this post here:
link

Functions are prefixed by an underline '_' .

Therefore, the main function should be changed to _main (program entry point), as well as printf , changed to _printf .

The register rbp must be saved to avoid a segmentation error.

Below, follow the program already with the changes:

        extern _printf
        global _main

_main:

segment .rodata

        align 8

_L1:    db 'hello', 10, 0

segment .text

        push rbp          

        mov rax, 0
        lea rdi, [rel _L1]
        call _printf
        mov rax, 0

        pop rbp
        ret

To compile:

nasm -f macho64 out.asm

To link:

clang out.o

Below, the execution output:

$ ./a.out
hello
$ 

Compiled with NASM version 2.11.08 in a OS X El Capitan system.

    
29.01.2016 / 04:25