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.