Porting Nasm

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
Muneer
Member
Member
Posts: 104
Joined: Tue Nov 02, 2010 2:05 am
Location: India

Porting Nasm

Post by Muneer »

Having successfully ported newlib, I am now trying to port nasm. There are fewer docs written on the subject. I have downloaded the nasm src code. what are the changes that should be made to src in order to successfully "configure" and "make". I am planning to write a tutorial on it once I am successful seeing as there are fewer or no tutorial written for porting nasm.
Even the smallest person could change the course of the future - Lord Of The Rings.

In the end all that matters is what you have done - Alexander.

Even after a decade oh god those still gives me the shivers.
Kevin
Member
Member
Posts: 1071
Joined: Sun Feb 01, 2009 6:11 am
Location: Germany
Contact:

Re: Porting Nasm

Post by Kevin »

You just need to tell configure to use your cross compiler (you may have to patch and recompile it so that it picks up your libc by default). Something like this:

Code: Select all

./configure --host=i386-pc-tyndur
Depending on the directory layout in your OS you may want to set some of the other configure options as well.
Developer of tyndur - community OS of Lowlevel (German)
User avatar
Muneer
Member
Member
Posts: 104
Joined: Tue Nov 02, 2010 2:05 am
Location: India

Re: Porting Nasm

Post by Muneer »

@kevin
Sorry for not making the question clear. I have a cross-compiler compiled --without-headers. and I have build newlib and links it along with a custom linker script with the bare cross-compiler so as to compile my apps. In this situation how will I build nasm. I have tried modifying the makefile after "configure" but to no success.

I changed the CC = i586-elf-gcc and in the CFLAGS, I appended -I./newlib/include (I placed a copy of the newlib and link.ld in the nasm src directory) and LDFLAGS = ./link.ld ./newlib/lib/libc.a.
once I make I get all sorts of things dumped at me. first strings.h not found. I copied the string.h to strings.h. then lots of strcmp blah blah.. I am trying to port newlib without cross compiling gcc again.
Even the smallest person could change the course of the future - Lord Of The Rings.

In the end all that matters is what you have done - Alexander.

Even after a decade oh god those still gives me the shivers.
Kevin
Member
Member
Posts: 1071
Joined: Sun Feb 01, 2009 6:11 am
Location: Germany
Contact:

Re: Porting Nasm

Post by Kevin »

The proper way to do it is updating your cross compiler.

But well, if you insist on doing it with the limited set of tools that you have ready now, you can probably get it working by fiddling with environment variables... Of course then you're doing it the non-standard way, which means that you need to provide even more information than usual. "strcmp blah blah" doesn't really tell me what's going on. You don't even tell me if compiling or linking fails.

Basically the approach is the same as with any bug: Look at the error, try to find out why it fails, fix it. If it doesn't find an include file, check the gcc command line. Are the right -I options there? If a source file doesn't compile, check if it includes the right headers. (Yes, programs are broken, even if other people wrote them. They may happen to work on Linux or Windows, but for other OSes you may need to patch them. Build systems are even more broken. Get used to it.) In short: Just do whatever you would do if your own code refused to build.
Developer of tyndur - community OS of Lowlevel (German)
fronty
Member
Member
Posts: 188
Joined: Mon Jan 14, 2008 5:53 am
Location: Helsinki

Re: Porting Nasm

Post by fronty »

HardCoder wrote:first strings.h not found. I copied the string.h to strings.h. then lots of strcmp blah blah..
strings.h isn't string.h. strings.h is POSIX header file which declares for example strcasecmp, which nasmlib.h refers to.
User avatar
Muneer
Member
Member
Posts: 104
Joined: Tue Nov 02, 2010 2:05 am
Location: India

Re: Porting Nasm

Post by Muneer »

Thanks for the info.
Checking. Will post the results. If it fails then I might go for the long(standard) way as kevin pointed out.
Even the smallest person could change the course of the future - Lord Of The Rings.

In the end all that matters is what you have done - Alexander.

Even after a decade oh god those still gives me the shivers.
User avatar
Muneer
Member
Member
Posts: 104
Joined: Tue Nov 02, 2010 2:05 am
Location: India

Re: Porting Nasm

Post by Muneer »

At last after a long battle I figured I was compiling the wrong nasm source. I also managed to build my gcc cross compiler with newlib support though I did it with my own patches. Real Happy now that everything is working. GCC/Toolchain is next in line.

Here is a screenshot.

Image

Its 4:00 AM in the morning. I might as well get some sleep.
Even the smallest person could change the course of the future - Lord Of The Rings.

In the end all that matters is what you have done - Alexander.

Even after a decade oh god those still gives me the shivers.
User avatar
Muneer
Member
Member
Posts: 104
Joined: Tue Nov 02, 2010 2:05 am
Location: India

Re: Porting Nasm

Post by Muneer »

As for the tutorial I figured that there are very few steps involved to actually write one. So for any future help for those trying to port nasm, here is how I did it.

Pre-Requisites:

* Os with basic stdin/stdout/stderr & system calls support and must be capable of loading elf files

* Gcc cross compiler with newlib support. I am using the latest newlib 1.19.0 as it has some headers like
strings.h that are missing in the older versions like 1.15.0 which nasm needs inorder to compile

* Include the <cross compilers>/bin (assuming it is i586-elf-gcc,etc) in the PATH

Now to the actual compiling

1) download lastest stable nasm source. As of this writing, it is http://www.nasm.us/pub/nasm/releasebuil ... .10.tar.gz
2) extract it to say ~/nasm/nasm-2.09.10
3) create directory ~/nasm/nasm-final
4) from the nasm-2.09.10 "./configure --host=i586-elf --prefix=/home/<username>/nasm/nasm-final"
5) make
6) sudo make install

The ./configure --prefix arguement must have the absolute path and the --host=i586-elf, if it is i586-elf-gcc,etc.
There you have your nasm as well as ndisasm compiled for your Os in the ~/nasm/nasm-final/bin directory.

In order to see it running "-v" as soon as possible with a dirty hack if you havent implemented argc/argv pairs, edit your crt0.S include (nasm syntax) and compile it to crt0.o and copy it to <cross-compiler>/<appropriate directory> (in this case lib/gcc/i586-elf/4.5.2)

Code: Select all

[BITS 32]
[GLOBAL _start]
[EXTERN main]
_start:
	
	mov dword [addr], argv_0           ;  argv[0] points to "nasm"
	mov dword [addr + 4], argv_1     ;  argv[1] points to "v"

	push addr;                                ; push the pointer
	push 2;                                    ; push the count of arguements. 2 in this case

	Call main       
	jmp $

addr: dd 0,0
argv_0: db "nasm" , 0
argv_1: db "-v" , 0
Even the smallest person could change the course of the future - Lord Of The Rings.

In the end all that matters is what you have done - Alexander.

Even after a decade oh god those still gives me the shivers.
Kevin
Member
Member
Posts: 1071
Joined: Sun Feb 01, 2009 6:11 am
Location: Germany
Contact:

Re: Porting Nasm

Post by Kevin »

--prefix should really be the path that nasm will be installed to on your OS, not on the build system. It probably doesn't matter for nasm, but other programs use that path to find their other files.

In order to get the binaries copied to the right directory on your build system, specify it during make install:

Code: Select all

make install INSTALLROOT=/home/user/foo/
Other programs commonly call the variable to set during make install DESTDIR.
Developer of tyndur - community OS of Lowlevel (German)
Post Reply