Skip to content
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

Compiling on musl #36

Closed
ghost opened this issue Jul 19, 2015 · 6 comments
Closed

Compiling on musl #36

ghost opened this issue Jul 19, 2015 · 6 comments

Comments

@ghost
Copy link

ghost commented Jul 19, 2015

I've tried installing plan9port on void linux (musl version) to find it fails. Paul Onyschuk posted some patches at https://groups.google.com/forum/#!topic/plan9port-dev/trEfAGRHPfA . I wonder if these could be incorporated? His post has no replies and there are no other issues about this open or closed here, so I'm not sure what's the status on this.

@pcoutin
Copy link

pcoutin commented Jul 22, 2015

I get the following when trying to compile on Alpine:

>>> cd /usr/local/plan9/src/cmd; mk all  
9l -o o.9import 9import.o 
/usr/local/plan9/src/libthread/thread.c:130: undefined reference to `getcontext'
/usr/local/plan9/src/libthread/thread.c:154: undefined reference to `makecontext'
/usr/local/plan9/src/libthread/thread.c:308: undefined reference to `swapcontext'
/usr/local/plan9/src/libthread/thread.c:308: undefined reference to `swapcontext'
collect2: error: ld returned 1 exit status

It turns out that musl does not implement those functions. "Legacy functions operating on ucontext_t (getcontext, setcontext, makecontext, swapcontext) are not implemented. They are no longer part of POSIX, but cooperative multi-tasking applications use them. ucontext_t also appears as an argument to sigaction handlers which cannot be used portably." http://wiki.musl-libc.org/wiki/Open_Issues

This is similar to how OS X is lacking such functions as explained in /usr/local/plan9/src/libthread/threadimpl.h, so I just added #include "x86_64-ucontext.h" to that file and I am hoping that everything I need (Venti) compiles fine. I also removed the #include <ucontext.h> to stop errors about redefinitions.

Now I'm stuck with

/usr/lib/gcc/x86_64-alpine-linux-musl/4.9.2/../../../../x86_64-alpine-linux-musl/bin/ld: cannot find -lthread

libthread didn't compile?!?!?!!?!

x86_64-ucontext.h:3:25: error: conflicting types for 'mcontext_t'
 typedef struct mcontext mcontext_t;
                         ^
/usr/include/bits/signal.h:61:3: note: previous declaration of 'mcontext_t' was here
 } mcontext_t;
   ^

I add a #define mcontext_t right above that line in x86_64-ucon, and it compiled, but

9l -o o.9import 9import.o 
/usr/local/plan9/src/libthread/thread.c:308: undefined reference to `swapcontext'
/usr/local/plan9/src/libthread/thread.c:130: undefined reference to `libthread_getmcontext'
/usr/local/plan9/src/libthread/thread.c:154: undefined reference to `makecontext'
/usr/local/plan9/src/libthread/thread.c:308: undefined reference to `swapcontext'

They're implemented for other platforms in that directory, but not for 64-bit x86 Linux.

@0intro
Copy link
Member

0intro commented Jul 23, 2015

undefined reference to swapcontext' undefined reference tolibthread_getmcontext'
undefined reference to makecontext' undefined reference toswapcontext'

These functions are indeed deprecated in POSIX. I think one of the reason
is because the POSIX people considered the threads to be a replacement
for them, while it's not the same thing at all.

These functions are still very useful, but easy to implement locally.
I think the right way would be to implement them conditionally, like
it was done for Linux/sparc64 and other architectures.

You can also take a look to libtask, where Russ already did most of the code
for Linux/x86_64.

https://github.com/0intro/libtask

@xerz-one
Copy link

Any progress on this? I'm getting similar errors on a current Void x86_64-musl

>>> cd /builddir/plan9port-df2d9ec9d169626cdc2a23829bb2831738215722/src/cmd; mk all
9l -o o.9import 9import.o -Wl,-z,relro -Wl,-z,now -Wl,--as-needed    
/bin/ld: /builddir/plan9port-df2d9ec9d169626cdc2a23829bb2831738215722/lib/libthread.a(thread.o): in function `threadalloc':
/builddir/plan9port-df2d9ec9d169626cdc2a23829bb2831738215722/src/libthread/thread.c:130: undefined reference to `getcontext'
/bin/ld: /builddir/plan9port-df2d9ec9d169626cdc2a23829bb2831738215722/src/libthread/thread.c:154: undefined reference to `makecontext'
/bin/ld: /builddir/plan9port-df2d9ec9d169626cdc2a23829bb2831738215722/lib/libthread.a(thread.o): in function `contextswitch':
/builddir/plan9port-df2d9ec9d169626cdc2a23829bb2831738215722/src/libthread/thread.c:308: undefined reference to `swapcontext'
/bin/ld: /builddir/plan9port-df2d9ec9d169626cdc2a23829bb2831738215722/src/libthread/thread.c:308: undefined reference to `swapcontext'
collect2: error: ld returned 1 exit status
mk: 9l -o o.9import ...  : exit status=exit(1)
mk: for i in ...  : exit status=exit(1)

@huglovefan
Copy link

i found this library that implements the ucontext functions for musl:

https://code.foxkit.us/adelie/libucontext/

i was able to build plan9port on void x86_64-musl with the library (xbps-install libucontext-devel) and just these changes:

diff --git a/bin/9l b/bin/9l
index 6195815f..d1a980cc 100755
--- a/bin/9l
+++ b/bin/9l
@@ -38,9 +38,9 @@ case "$tag" in
 *Linux*)
 	ld=${CC9:-gcc}
 	userpath=true
-	extralibs="$extralibs -lutil -lresolv"
+	extralibs="$extralibs -lutil -lresolv -lucontext"
 	case "${SYSVERSION:-`uname -r`}" in
-	2.6.* | 3.* | 4.*)
+	2.6.* | [3-9].* | [1-9][0-9].*)
 		extralibs="$extralibs -lpthread"
 		;;
 	esac
diff --git a/lib/linux-isnptl.c b/lib/linux-isnptl.c
index e4c23c63..c44d97ad 100644
--- a/lib/linux-isnptl.c
+++ b/lib/linux-isnptl.c
@@ -1,3 +1,4 @@
+#include <sys/types.h>
 #include <pthread.h>
 #include <unistd.h>
 #include <stdlib.h>
diff --git a/src/lib9/dirread.c b/src/lib9/dirread.c
index 40fbe3c7..b13803e6 100644
--- a/src/lib9/dirread.c
+++ b/src/lib9/dirread.c
@@ -10,13 +10,7 @@ extern int _p9dir(struct stat*, struct stat*, char*, Dir*, char**, char*);
 static int
 mygetdents(int fd, struct dirent *buf, int n)
 {
-	off_t off;
-	int nn;
-
-	/* This doesn't match the man page, but it works in Debian with a 2.2 kernel */
-	off = p9seek(fd, 0, 1);
-	nn = getdirentries(fd, (void*)buf, n, &off);
-	return nn;
+	return getdents(fd, (void*)buf, n);
 }
 #elif defined(__APPLE__) 
 static int

@gottaeat
Copy link

same here, on musl-1.1.24 and gcc-9.2.1-20200215:

>>> cd /mss/work/table/plan9port/src/cmd; mk all
9l -o o.9import 9import.o
/usr/bin/ld: /mss/work/table/plan9port/lib/libthread.a(thread.o): in function `threadalloc':
/mss/work/table/plan9port/src/libthread/thread.c:135: undefined reference to `getcontext'
/usr/bin/ld: /mss/work/table/plan9port/src/libthread/thread.c:165: undefined reference to `makecontext'
/usr/bin/ld: /mss/work/table/plan9port/lib/libthread.a(thread.o): in function `contextswitch':
/mss/work/table/plan9port/src/libthread/thread.c:324: undefined reference to `swapcontext'
/usr/bin/ld: /mss/work/table/plan9port/src/libthread/thread.c:324: undefined reference to `swapcontext'
collect2: error: ld returned 1 exit status
mk: 9l -o o.9import ...  : exit status=exit(1)
mk: for i in ...  : exit status=exit(1)

tried applying the patch that @huglovefan posted to the version that is in the repo as i'm writing this but it failed due to modifications and lack of a linux-isnptl.c under lib/. modifying their patch to this got plan9port built:

 bin/9l             | 2 +-
 src/lib9/dirread.c | 8 +-------
 2 files changed, 2 insertions(+), 8 deletions(-)

diff --git a/bin/9l b/bin/9l
index a10aab7..103fd54 100755
--- a/bin/9l
+++ b/bin/9l
@@ -25,7 +25,7 @@ case "$tag" in
 *Linux*)
        ld=${CC9:-gcc}
        userpath=true
-       extralibs="$extralibs -lutil -lresolv -lpthread"
+       extralibs="$extralibs -lutil -lresolv -lpthread -lucontext"
        ;;
 *Darwin*x86_64*)
        ld="${CC9:-gcc} -m64"
diff --git a/src/lib9/dirread.c b/src/lib9/dirread.c
index c232eb8..7c8d48d 100644
--- a/src/lib9/dirread.c
+++ b/src/lib9/dirread.c
@@ -10,13 +10,7 @@ extern int _p9dir(struct stat*, struct stat*, char*, Dir*, char**, char*);
 static int
 mygetdents(int fd, struct dirent *buf, int n)
 {
-       off_t off;
-       int nn;
-
-       /* This doesn't match the man page, but it works in Debian with a 2.2 kernel */
-       off = p9seek(fd, 0, 1);
-       nn = getdirentries(fd, (void*)buf, n, &off);
-       return nn;
+       return getdents(fd, (void*)buf, n);
 }
 #elif defined(__APPLE__)
 static int

@bhuntsman
Copy link
Contributor

This issue is resolved by commit 1857120.

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

No branches or pull requests

7 participants