Bug 21002 - Incompatible with MUSL libc
Summary: Incompatible with MUSL libc
Status: UNCONFIRMED
Alias: None
Product: elfutils
Classification: Unclassified
Component: general (show other bugs)
Version: unspecified
: P2 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on: 21008 21009 21010
Blocks:
  Show dependency treegraph
 
Reported: 2016-12-29 03:50 UTC by Luiz Angelo Daros de Luca
Modified: 2018-07-04 10:12 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments
MUSL compatibility patch (2.51 KB, patch)
2016-12-29 03:50 UTC, Luiz Angelo Daros de Luca
Details | Diff
Minor sync fix (2.55 KB, patch)
2016-12-29 04:13 UTC, Luiz Angelo Daros de Luca
Details | Diff
MUSL compatibility patch (2.55 KB, patch)
2016-12-29 04:17 UTC, Luiz Angelo Daros de Luca
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Luiz Angelo Daros de Luca 2016-12-29 03:50:08 UTC
Created attachment 9727 [details]
MUSL compatibility patch

Hello,

libelf is incompatible with musl. Most of it is because error() is used and error.h is included in many places. If added to system.h, error.h could be removed from all subfiles without compilation error.

Other problems are the use of missing strndupa, canonicalize_file_name and a change the way strerror_r is used.

The patch attached allows elfutils to compile nicely using musl
Comment 1 Luiz Angelo Daros de Luca 2016-12-29 04:13:24 UTC
Created attachment 9728 [details]
Minor sync fix
Comment 2 Luiz Angelo Daros de Luca 2016-12-29 04:17:37 UTC
Created attachment 9731 [details]
MUSL compatibility patch
Comment 3 Mark Wielaard 2016-12-29 23:36:28 UTC
Could you split this into separate patches for each issue?
That makes it easier to review and easier to track down if there happens to be some issue later on.

- The idea of using system.h to define error () seems fine. But the current replacement doesn't seem complete. error () also flushes stdout first. Which we don't really seem to use, so might not be essential. But it also calls exit (status) if status != 0. Which is essential to how error is used in elfutils. Also error () updates error_message_count which is used in a couple of cases to know what exit code a program should use (if error itself didn't exit).

- I have to think about that strerror_r replacement. Normally we expect just a static string describing a known errno. But I see we have to handle unknown numbers too. hmmm.

- We only use strndupa in one place in the code in src/unstrip.c to create a directory. Could we just rewrite that?

- Since canonicalize_file_name (path) is defined as realpath(path, NULL) that change seems fine.
Comment 4 Luiz Angelo Daros de Luca 2016-12-30 21:29:05 UTC
- error(): created a new bug 21008

- canonicalize_file_name(): create a new bug 21009

- strerror_r: create a new bug 21010

- strndupa is provided by MUSL if defined _GNU_SOURCE. Not a problem anymore
  https://github.com/esmil/musl/blob/master/include/string.h#L89

Until now, I still only tested compiling elfutils libraries.
Comment 5 Luiz Angelo Daros de Luca 2016-12-30 23:55:52 UTC
It is not part of my use case but besides the problems already mentioned, elfutils/src will still miss from glibc:

- obstack
- fts (I already use a patch to remove its dependency on lib*)

The approach used by voidlinux is to create independent libraries and checking for them on ./configure as in https://github.com/voidlinux/void-packages/blob/master/srcpkgs/elfutils/patches/musl-fts-obstack.patch

If I manually remove src from Makefile, everything else builds nicely.
Comment 6 Mark Wielaard 2016-12-31 15:19:55 UTC
(In reply to Luiz Angelo Daros de Luca from comment #5)
> It is not part of my use case but besides the problems already mentioned,
> elfutils/src will still miss from glibc:
> 
> - obstack
> - fts (I already use a patch to remove its dependency on lib*)

We could probably add these using gnulib if it is useful for alternative glibc implementations that are missing them. But we haven't used gnulib modules before so we would need tweak the infrastructure a little to accommodate them.
Comment 7 Luiz Angelo Daros de Luca 2018-03-10 02:23:36 UTC
I'm back with news.

I manage to compile all elfutils for OpenWrt, not just libs.

As expected, obstack and fts were missing. I managed to build using:
https://github.com/pullmoll/musl-obstack
https://github.com/pullmoll/musl-fts

I created patches for elfutils allowing it to look for a libfts.so or libobstack.so. It builds but I feel I got too much/too little usage of both in linking.

https://raw.githubusercontent.com/luizluca/openwrt/elfutils-full/package/libs/elfutils/patches/101-musl-fts.patch
https://github.com/luizluca/openwrt/blob/elfutils-full/package/libs/elfutils/patches/102-musl-obstack.patch

At least for obstack, I need to merge some Makefile.am changes that got into another patch (https://github.com/luizluca/openwrt/blob/elfutils-full/package/libs/elfutils/patches/103-musl_elftools_full). I'll update soon.

As expected, the error_message_count used in some programs did break the build.
They are very few cases, most of them looks like could be replaced even with a local variable. Maybe it would be better to simply get rid of it.

Some missing macros: ACCESSPERMS, ALLPERMS, DEFFILEMODE.

qsort_r() is not provided in MUSL

Some elfutils programs (arlib, elfcmp, elfcompress, findtextrel, unstrip, ) do not include system.h. So, all ifdefs that solves some musl incompatibility where not present. I simply added them.

fnmatch() does not support FNM_EXTMATCH in musl (just 3 uses).

src/unstrip.c:void warn (const char *msg) might be using a too generic name. It collides with err.h warn. I simply renamed it.

Almost there!
Comment 8 Mark Wielaard 2018-03-11 14:22:30 UTC
(In reply to Luiz Angelo Daros de Luca from comment #7)
> I'm back with news.
> [...]
> Almost there!

Thanks for the overview. If at all possible, could you open separate bugs for these issues? Then we can use this bug as tracker.