andreasjhkarlsson / gbdk-n

gbdk libraries updated for newer versions of sdcc
174 stars 25 forks source link

Unify runtime & stdlib with SDCC #6

Open andreasjhkarlsson opened 8 years ago

andreasjhkarlsson commented 8 years ago

There are currently overlaps & incompatibilities between the runtime (crt) and standard libraries (stdlib) included in gbdk-n and the ones included in SDCC.

Currently, the included crt in SDCC is disabled in favor of the gbdk-n. This is because gbdk-n breaks with SDCC runtime. This is done by passing --no-std-crt0 to sdcc.

For the standard library, both the gbdk-n library and sdcc library are used because there are functions in both libraries that are needed for successful builds. The problem is when the libraries overlap, resulting in failing builds (multiple symbols defined when linking). An example is the rand() function that is defined in both gbdk-n and sdcc.

andreasjhkarlsson commented 8 years ago

Improved compatibility with SDCC stdlib by renaming gbdk-n rand function. See: https://github.com/rotmoset/gbdk-n/commit/e4a5bdf6fff7853c4e09276b284d49e18eea2768

Kusefiru commented 8 years ago

Don't know if it's related to this issue, but if I try to make the library it gets stucked at the first .c file. Here's the result I have (I'm using make through msys on windows) :

C:\Users\Kusefiru\github\gbdk-n>make
chmod u+x bin/*
mkdir -p ./obj
bin/gbdk-n-compile.sh ./libc/digits.c -o ./obj/digits.rel
+ sdcc -mgbz80 --no-std-crt0 -I /c/Users/Kusefiru/github/gbdk-n/bin/../include -I /c/Users/Kusefiru/github/gbdk-n/bin/../include/asm -c ./libc/digits.c -o ./obj/digits.rel
./libc/digits.c:1: warning 115: unknown or unsupported #pragma directive 'bank=BASE'
Caught signal 2: SIGINT
make: *** [lib/crt0.rel] Error 1

Ctrl+C is the only way to stop the process, I tried waiting for a while (around 10 minutes) and it doesn't move.

andreasjhkarlsson commented 8 years ago

What version of sdcc are you using?

Kusefiru commented 8 years ago

sdcc-3.6.0, the latest version available.

pepijndevos commented 8 years ago

I looked into this a little bit, and it seems the SDCC crt0 is very minimal compared to the GBDK one. The SDCC one just sets up the stack and calls main, while the GBDK sets up a ton of registers and provides interrupt handling infrastructure.

I don't think it is advantageous to switch to the SDCC crt0 and the GBDK seems too opinionated to upstream. But using a custom crt0 does not seems to be a problem.

Unifying the standard library does seem useful. Could you give any indication what needs to be done here? Do parts of GBDK need to be rewritten to use the new libc?

andreasjhkarlsson commented 8 years ago

Yeah, you're definitely right about the runtime library.

I don't know how much more is needed for full stdlib compatibility, since I fixed the rand problem (mentioned above) I hadn't had much problems, but I haven't used a majority of the functions in gbdk-n either.

But in general the issue is:

pepijndevos commented 8 years ago

So the challenge is finding all functions defined by both. I thought I could either grep the source or use objdump to create a listing. I tried this, which gives somewhat promising results,

cat obj/*.rel | grep -oE "^S [^.]\w*" | sort -u

On first sight the only thing that would conflict with standard C functions is indeed the random function and the printf family. No wait, GBDK uses the g prefix, so it should be fine.

I read somewhere that SDCC defines printf but not putchar, so that might be interesting in the future to implement putchar and alias the GBDK functions to the stdlib.

Any idea in what way the random functions are incompatible? Maybe a simple adjustment couls make GBDB use the SDCC version.

pepijndevos commented 8 years ago

Update: I did the same for SDCC and compared.

ar -xf '/usr/share/sdcc/lib/gbz80/gbz80.lib'
cat *.rel | grep -oE "^S [^.]\w*" | sort -u  > ../sdcclib.txt
grep -f sdcclib.txt gbdklib.txt 
S __divuchar
S __divuint
S _getchar
S _gets
S _malloc_heap_start
S __moduchar
S __moduint
S _putchar
S _rand

Now only remaining question is how to remove them and test everything still works.

(it does appear I still have a _rand in GBDK, why?)