5
I’m trying to do a study of compilers, 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
at the OSX terminal do $ nasm -f macho out.asm
, and I have build error because of main:function
, if you remove the :function
ends with this first error when trying to compile again. Another 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’m wrong:
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
.
From this point with the file out.o
which I must compile with the Clang, I do $clang out.o -v
to return all the verbose and 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’t 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 receive 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 of the initial way of the code and the $ gcc out.o
also generating the.out file that when running returned the answer Hello
of the expected message. However, as I have to finish this project in OSX, I ask the community for help.
Nothing certain, generates error
out.asm:12: error: invalid operand size specification
. In this case I added to the beginning of the documentdefault rel
and as requested I put in place ofmove edi, $_L1
the rel to staymove edi, rel $_L1
.– Thiago Prado
@Thiagoprado -- I wrote my solution attempt. You can check if it works and if it makes sense?
– JJoao
So @Jjoao, sorry for the delay to check, however I was unsuccessful, in the case of your first example when compiling I got the return of the message:
Undefined symbols for architecture x86_64:
 "_main", referenced from:
 implicit entry/start for main executable
 "puts", referenced from:
 main in alt02.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
. The second example also had a similar error. All examples I tried to compile withnasm -f macho file.asm
andnasm -f macho64 file.asm
.– Thiago Prado
@Thiagoprado, let’s see step by step: 1)
nasm -v
(to check the version) 2)nasm -f macho64 file.asm
(made mistakes? ) 3)cc file.o
(error) 4) ./a.out
– JJoao
Let’s go then. 1) NASM version 2.11.08 Compiled on Mar 10 2015, soon last version of the Brew repositories. 2) Here ok, creates the file . o without problems and errors and without messages. 3)
Undefined symbols for architecture x86_64:
 "_main", referenced from:
 implicit entry/start for main executable
 "puts", referenced from:
 main in alt02.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
. This is the error when compiling with cc.– Thiago Prado
To help the version of my Clang
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
.– Thiago Prado