tyfkda / xcc

Standalone C compiler/assembler/linker/libc for x86-64/aarch64/riscv64/wasm
https://tyfkda.github.io/xcc/
MIT License
197 stars 14 forks source link

compiling musl #134

Closed ghost closed 7 months ago

ghost commented 10 months ago

I’m not sure whether you are interested in using a different libc for your project, but I feel like it would be a very neat approach to get more programs to compile under it more easily.

I’ve been experimenting with porting musl to xcc, and I found only a few things that seemed to prevent me from compiling most of the library!

Porobably the biggest issue is xcc’s lack of support for the alias("") attribute. Other than that, I also ran into some syntactical issues:

Other than that, I haven’t tried compiling some parts of the library like pthread and I have just added stubs for syscalls, but given that (with few changes) most of musl could be compiled with xcc already, it seems very promising!

Ultimately, maybe it would be more sensible to remove the need to implement syscalls in the musl port by instead taking the low level function implementations (like read, etc.) from xcc’s libsrc, so that it can work more easily across xcc’s platforms like Mac and Wasm. (Something similar to what wasi-libc does.)

tyfkda commented 10 months ago

@zamfofex Sounds great!

I think alias looks difficult to support, but others are nice to have.

Would you please tell me how to compile musl using xcc? Thanks!

ghost commented 10 months ago

Would you please tell me how to compile musl using xcc? Thanks!

First, apply this patch to musl 1.2.4: https://gist.github.com/zamfofex/5caf961fe7012929de0aadf71b7c4898

Then, make sure to replace the file name for CC in config.mak with the file name of xcc on your actual computer. Finally, just run make from the patched musl’s directory.

Note that there is still work to be done to get it to “work” correctly, but it should compile, at least! (I tried on Linux only.) Also, it skips compiling some parts of the library like src/thread and src/math which currently seem problematic.

tyfkda commented 10 months ago

@zamfofex Thanks! Using the diff, I succeed to compile musl using xcc!

My memo: patch -p1 -d . < ../musl-xcc.diff

tyfkda commented 9 months ago

I have updated the compiler to allow __asm declaration in base text (top level), now you can put the code in crt_arch.h:

__asm(
".text \n"
".globl " START " \n"
START ": \n"
"   xor %rbp,%rbp \n"
"   mov %rsp,%rdi \n"
// ".weak _DYNAMIC \n"
// ".hidden _DYNAMIC \n"
// "    lea _DYNAMIC(%rip),%rsi \n"
"   and $-16,%rsp \n"
"   call " START "_c \n"
);
tyfkda commented 7 months ago

@zamfofex I'm going to close this issue. Please open new issue if you still have a problem.

ghost commented 7 months ago

Hello, thank you for your work! :tada: I wonder whether it’d be desirable to replace xcc’s own C library (in libsrc) with musl. Is that something you are interested in? If you feel like that’s the direction you want to go, I could see if I can progress further in getting musl to compile and work fully, and eventually submit a pull request to replace it in.

tyfkda commented 7 months ago

I don't currently have a strong motivation to replace library code, but I think it's worth having the flexibility to choose other libraries.