-
Notifications
You must be signed in to change notification settings - Fork 108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Porting JWasm to ELKS using OpenWatcom C #2103
Comments
The reasons for the undefined references and what needs to be done are as follows:
We'll need to find a
These are OW library routines called from the OWC code generator. We'll need to pull them from the OWC source at bld/clib/cgsupp/a/pia.asm etc and copy them to libc/watcom/asm.
I'm pretty sure
We'll need to read the OWC man page on its libc All of this shouldn't a big deal, the question is how seriously you want to work get JWasm ported. I can help a lot and do some of the OW heavy lifting, but I'm not doing the actual port of JWasm. Let me know what you're thinking. |
@rafael2k, I went ahead and added almost everything you should need for getting JWasm linked to #2104. Reading the JWasm source, it seems Also, for now create a dummy clock() routine and add it to JWasm as follows. I haven't added a full version since I haven't been able to test it.
Later, we can add a full version, something like this (untested):
|
Thanks a lot @ghaerr! Today at night I'll see if I can get jwasm linked! |
Btw, I'm doing the port and it is very close to working. Next I'll just need some help / advice on what to use to link the object file in ELKS (or may be just add rudimentary support for ELKS binary directly in JWasm?) |
Just tested it again, now I get:
I'll investigate a bit further.
|
Got alloca. I had to include malloc.h first to alloca.h.
Which header provides them? |
Btw copying over parts of your commit, now I get:
|
Btw, the linking line:
out.lib from the watcom/asm directory |
For now, add |
Actually, a |
This is already included in the ELKS libc/libc.lib library - make sure that is both compiled by OWC correctly and in your link line. The ELKS Watcom libc.lib is compiled using
There are lots of potential issues but you've probably only set -Wl,option -Wl,heapsize=0x1000 which is a 4k heap. Try -Wl,option -Wl,heapsize=0x8000 which is 32k and see what happens. |
Compiled ELKS/OS2 binary: |
I can definitely see less brk() errors, but I can not raise too much the heap, as I get:
|
I'm thinking is switching to ow wasm, as it might be that we are importing a lot of features / size from jwasm that might not even be useful on a 8086 arch. |
Oh, don't do that - you're just getting started with the real work of of moving a 32-bit application to 16-bits, and certain things need to be learned about JWasm to know how to proceed (or not). More on that below. With regards to wasm, I briefly tried compiling it (with OWC) yesterday. It's going to be quite a bit harder to port, as it relies on a number of host-based tools that also need to be ported, which convert various "*.hg" files to "*.h" files just in order to get the header files pre-processed. And the entire process is built around the non-Makefile driven OWC build environment which is very complicated and involves yet more tools that need porting. And after all that, I suspect it'll require OWC's complex memory management routines in order to run, and I haven't ported them over (yet). Instead ELKS' normal C library memory allocation routines are being used. How does one port 32-bit software to ELKS?One must understand that porting present day software to a legacy 16-bit CPU almost always involves more work than just compiling C files and getting them linked. An analogy that comes to mind is stuffing 10 lbs of stuff into a 5 lb bag. There's never enough room to do things the simple way nowadays of just allocating memory; instead one has to figure out how to memory is going to be managed, as 64k blocks of memory just aren't enough to do much useful. Consider, for instance, that on any modern (now old) 32-bit system, one can allocate 100MB of memory with a single OK, what actually needs to be done to port 32-bit software to ELKS?The first step has already been done (for JWasm) - getting the source to compile, look at the unresolved symbols, and resolve them with an eye towards grouping them into external libraries needed, or unimplemented system calls, and what that looks like. After that, a successful link indicates they're all resolved. Usually at this time the software is run, and it usually crashes. The next step is to learn how the application manages memory. As described above, this has to be known, since 16-bit applications can't allocate more than 32K at a time, and very little 32-bit software bothers with that. This can only be learned by reading (mostly) every file in the source base. For this step, what I do is:
A big benefit of a quick read-through of the entire source base will also give the programmer a basic understanding of how the application basically works, which goes a long way when thinking about how to get it ported. We'll stop there. @rafael2k, my questions for you at this point are:
Sometimes, even though memory allocations in the source are large, they can be reduced to a lot smaller, with the effect of adding a limitation on the size of input data files that can be processed. There's a lot more that can be said, but I generally will do these things (not necessarily in the same order) when questions come up about trying to get this or that program made available for ELKS. |
Thanks a lot for the explanation. I'm using the large model, and the application runs (help and so on, all good). It crashes when I try to assemble even a small file, during the first pass in the file, it crashes with brk() errors (most likely from malloc() calls I think). |
It seems JWasm uses MemAlloc as an allocation wrapper. I sometimes will just add a printf in that routine the shows the size requested, rather than chasing down the call in the source. Also, as you know, the size_t (or int) parameter changes from 32 to 16 bits when porting, so you'll want to get an idea of how the parameter to MemAlloc gets calculated to ensure its not being truncated, etc. when chasing this down. Depending on what you learn about JWasm, I have some ideas about a replacement memory allocator that would allocate large chunks from main memory, but smaller (< 1-2k?) from the near heap. Also, any instances of If you can successfully trim down the default allocations to much smaller, you might be able to get JWasm running for very small input files. That'd be great, because then we can work on adding a better memory allocator while the program is runnable. I use the analogy of "keeping the patient alive" while doing heart surgery for this kind of work, since it much harder to get feedback when dead! |
Hello @rafael2k, After adding #2106, we are able to write a alloc/free wrapper that allocates memory from the near heap if below a certain threshold, or otherwise allocate from main memory. I've written the following which you might be able to use in JWasm. The MAX_NEAR_ALLOC may have to be lowered, especially if you're already out of data segment size at 0x3000 (12k). In general though it'd be a good idea to keep very small allocations out of the main memory area. Note that the experimental functions take an unsigned long argument, not size_t.
You'll want to ensure that the memalloc/memfree function prototypes are in a JWasm header file so that the normal size_t argument is properly expanded to unsigned long, and then using this inside MemAlloc/MemFree in JWasm. You might also want to check for NULL and printf an error inside memalloc to see directly when its failing. With some tuning, I think this should allow you to get JWasm running so we can understand its memory usage better. Thank you! |
Thanks. I tested already and now the brk() errors are gone. The first pass of the assembler seems to work (with an input assembly with error, errors are shown), but then at some point it crashes (nothing on the screen, just freezes). I need to reboot. I'll instrument a bit more the code. |
It seems its going through the assembly passes (3 of them?). The 3600 byte allocation looks good, returning a segment of 37f1 offset 0 in main memory. The other allocations are local, with your application data segment 7e72. |
So, after a bit of frustration, I managed to port nasm! Already compiled some code, so far so good. I'm not compiling all output formats, as I need to understand which output format is better. The only dirty stuff I did was implement the realloc using fmemalloc and fmemfree... It is a nasm fork with some tasm compatibility added. It reports version 0.98.35. |
I've answered over at #1443 (comment). Have you officially given up on JWasm then? I have no idea whether NASM and JWasm are compatible, nor whether OWC will work with NASM. |
I learned some caveats when successfully porting NASM. I'll go back to to JWASM at some point (like erasing > 8086 instructions and using fmemalloc even for small allocations). |
@rafael2k writes (from #1443 (comment)):
The text was updated successfully, but these errors were encountered: