Skip to content

Musl build - #4580

Open
peppergrayxyz wants to merge 23 commits into
ptitSeb:mainfrom
peppergrayxyz:musl_build
Open

peppergrayxyz wants to merge 23 commits into
ptitSeb:mainfrom
peppergrayxyz:musl_build

Conversation

@peppergrayxyz

@peppergrayxyz peppergrayxyz commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

These patches add inital musl support (#2012).

Screenshot_2026-10-20_16-46-43

This is x86-musl vcube running on an arm64-musl host. x86-glibc vcube has missing symbols and how to handle them is to be decide.

I haven't spitted the patches into multiple PRs yet, because rebasing everything is a lot of work and I would like to do this in a coordinated way. I have re-ordered my commits into an order that is somewhat logical and made them small and pointed so they can be easily cherry-picked. Happy to shuffle things around, just let me know.

Patches

1. houskeeping

1.1 global GNU_SOURCE

pick 0501bb1 # global GNU_SOURCE

This removes GNU_SOURCE from the files and moves it to CMakeLists.txt. GNU_SOURCE should be consistently applied bevore includes, but is not and some files are included bevore GNU_SOURCE.

1.2. global large file support (lfs)

pick 948da8f # global lfs
pick 4e2c3c0 # lfs: use fseeko
pick 93334e9 # lfs: use ino_t, off_t
pick 14183a4 # lfs: mmap
pick a92baa7 # lfs: fsblkcnt_t, fsfilcnt_t
pick efbdf2e # lfs: stat
pick 71280a2 # lfs: dirent
pick ccb07ba # lfs: alphasort
pick 15a561e # lfs: f/open
pick 6bde1ec # lfs: l/f/stat, fstatat, statfs
pick d20deea # lfs: get/setrlimit, struct rlimit
pick ba082aa # lfs: n/ftw
pick 66b1a31 # lfs: scandir/at, readdir
pick 817c444 # lfs: glob, globfree, glob_t

This renames all compatibility *64_t types to their unprefixed names and enables 64 bit file types globally.

1.3. isnan/isinf

pick f88fca2 # replace legacy isnanf/isinff with isnan/isinf

This consisently moves to isnan and isinf which are used in parallel to deprecated function names

2. Bugs

2.1 my_pthread_getaffinity_np_old

pick 9f41c3c # fix my_pthread_getaffinity_np_old return a value
my_pthread_getaffinity_np_old is not returning a value

2.2 early init segfault

pick 03f659f # fix early init segfault

if GetNativeSymbolUnversioned returns NULL in libtools/libdl.c there is no message and the app segfaults. Adding a message, doesn't work, becase core.c initializes ftrace = stderr only after the failure already happend.

This patch:

  • checks the result of GetNativeSymbolUnversioned and prints a message and aborts on error
  • initalizes ftrace=stderr in PrintfFtrace when it is to be used, but not yet set

3. GNU-ism

3.1 rename symbols to not use glibc internals

pick 1ae022f # musl fix glibc names: __sigset_t, __pid_t, __uid_t
pick 10de8e3 # musl fix glibc names: qsort __compar_d_fn_t

3.2 macro redefinition

pick dd7be61 # fix musl build: os.h: macro redefined

3.3 fix type mismatch

pick 46edc2a # fix musl build: threads

4. conditional dependencies

These symbols do not exist on musl. Currently a lot of things are handled via ANDROID and __GLIBC__. I didn't want to switch based on libc but used cmake tests to determine if headers, symbols and functions are available and then act accordingly.

pick 1b76649 # conditional: execinfo.h
pick ad717c3 # conditional: mallopt
pick a721751 # conditional: malloc_trim
pick 36ff077 # conditional: mallinfo, mallinfo2
pick 88d7403 # conditional: _pthread_cleanup_push/pop
pick fa7ec47 # conditional: pthread_attr_get/setaffinity_np
pick f22c702 # conditional: __pthread_mutex_s->__kind
pick 8cd2665 # conditional: dlvsym
pick 6a99b16 # conditional: obstack_vprintf
pick 420ff22 # conditional: struct obstack
pick 3e609d6 # conditional: siginfo_t
pick 7bf2e51 # conditional: getprotobyname/number_r
pick 94ea179 # conditional: struct __res_state
pick c6b558f # conditional: RTLD_DL_SYMENT/LINKMAP
pick 38637ef # conditional: scandirat
pick 379621c # conditional: __ctype_b/tolower/toupper_loc
pick 649c198 # conditional: glob_t
pick d74203c # conditional: posix_spawn_file_actions_t
pick 87f6053 # conditional: __NFDBITS
pick 8a0d530 # conditional: __chk_fail

I'm not sure how to handle glibc symbols in general. For those that are needed by the build I added workarounds, added libraries and implemented missing functions. Symbols that are just passed through will fail. I think it is nice to have fall-backs, but I'm not sure to which extend box64 wants to become a gcompat like thing

5. libs for missing features

for obstack_vprintf and getprotobyname_r/getprotobynumber_r I implemented fallback. For other functions I added argp, obstack, fts, ucontext and error when targeting musl. All of them are packaged for alpine and gentoo

pick 8bda8b3 # musl: link additional libs

6. musl specifics

These last packages implement new behavior specifically for musl

pick de0abd2 # musl: update malloc hook
musl doesn't have __libc_* symbols, find them through dlsym instead

pick 1a90b08 # musl: elfloader find symbol
elfloader is looking for versioned libs which work differntly on musl

Testing

Test #11: tlsData .............................***Failed (is disabled on `Android`)
Test #44: cppThreads_32bits_nocosim ...........***Failed
Test #45: tlsData_32bits ......................***Failed

95% tests passed, 3 tests failed out of 58

Test 11 crashes and is disabled on Android
Test 44 and 45 are missing symbols

AI Disclosure

I built the project and manually fixed compiler and linker issues until I got the musl build working. So I'm pretty confident of the patch set 1-5. Then I got a segfaulting app for which i used AI to pinpoint the issues addressed in patch set 6. The code quality of set 6 is rather low for that reason and probably needs rework.

@ptitSeb

ptitSeb commented Oct 21, 2026

Copy link
Copy Markdown
Owner

Thank you for this. This is a huge change so I will need time to review it.

To answer the question about the libc fallback: I guess this depend on ow you plan to use box64. If it's only to run musl x64 program (like Wine?) then fallback are not usefull. But if the musl x64 landscape is to small and the target is regular glibc x64 binaries, then fallback are a must.

@ptitSeb

ptitSeb commented Nov 5, 2026 •

Copy link
Copy Markdown
Owner

@peppergrayxyz I don't understand what pick https://github.com/ptitSeb/box64/commit/1c0c48fbd77ae26b37cb10fcc4e63b123ea1eac6 # musl fix glibc names: qsort __compar_d_fn_t does. It just rename __compar_d_fn_t to iFcpcpp_t and that's all (ah well, declaring it also in a new file). That seems useless. Also iFcpcpp_t doesn't corespond to the signature anyway. const are not part of GO signatures as it as no impact on ABI. iFcpcpp_t in fact if int Func(char, void*, char, void*, void*). So I think this commit should be removed from the list (maybe just add the function declation if one is missing).

@ptitSeb

ptitSeb commented Nov 5, 2026 •

Copy link
Copy Markdown
Owner

@peppergrayxyz I found this PR anyway. Can we split it? You have already separated the commit in various chapter, so why not start with chapter 1 in a PR? Or we can go to 1+2+3 as a start. 4 should then be another PR then 5+6 together as a last one seems reasonable.

@peppergrayxyz

Copy link
Copy Markdown
Contributor Author

I cannot create stacked PRs here, only in my repo. Also I cannot rebase each slice onto main (doesn't make much sense). I'll try something else.

@peppergrayxyz

Copy link
Copy Markdown
Contributor Author

I splitted them, but I couldn't create stacked PRs, so each contains the prevouis ones

@peppergrayxyz

peppergrayxyz commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor Author

@peppergrayxyz I don't understand what pick https://github.com/ptitSeb/box64/commit/1c0c48fbd77ae26b37cb10fcc4e63b123ea1eac6 # musl fix glibc names: qsort __compar_d_fn_t does.

I think that was a missing function name, I'll check and correct it!

@peppergrayxyz

Copy link
Copy Markdown
Contributor Author

@ptitSeb I rewrote the qsort __compar_d_fn_t fix in 10de8e3

Signed-off-by: Pepper Gray <hello@peppergray.xyz>
Signed-off-by: Pepper Gray <hello@peppergray.xyz>
Signed-off-by: Pepper Gray <hello@peppergray.xyz>
Signed-off-by: Pepper Gray <hello@peppergray.xyz>
Signed-off-by: Pepper Gray <hello@peppergray.xyz>
Signed-off-by: Pepper Gray <hello@peppergray.xyz>
my32_pthread_mutex_init: fake->i386__kind:
incomplete definition of type 'struct __pthread_mutex_s'

Signed-off-by: Pepper Gray <hello@peppergray.xyz>
Signed-off-by: Pepper Gray <hello@peppergray.xyz>
Signed-off-by: Pepper Gray <hello@peppergray.xyz>
Signed-off-by: Pepper Gray <hello@peppergray.xyz>
Signed-off-by: Pepper Gray <hello@peppergray.xyz>
Signed-off-by: Pepper Gray <hello@peppergray.xyz>
Signed-off-by: Pepper Gray <hello@peppergray.xyz>
Signed-off-by: Pepper Gray <hello@peppergray.xyz>
Signed-off-by: Pepper Gray <hello@peppergray.xyz>
Signed-off-by: Pepper Gray <hello@peppergray.xyz>
Signed-off-by: Pepper Gray <hello@peppergray.xyz>
Signed-off-by: Pepper Gray <hello@peppergray.xyz>
Signed-off-by: Pepper Gray <hello@peppergray.xyz>
Signed-off-by: Pepper Gray <hello@peppergray.xyz>
Signed-off-by: Pepper Gray <hello@peppergray.xyz>
Signed-off-by: Pepper Gray <hello@peppergray.xyz>
Signed-off-by: Pepper Gray <hello@peppergray.xyz>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants