From e0eeb7630a38dc6024559e906c44fd50d56b72a5 Mon Sep 17 00:00:00 2001 From: Samuel Holland Date: Sat, 5 Jan 2019 08:52:59 -0600 Subject: Makefile: depend on internal.h; format headers Signed-off-by: Samuel Holland --- Makefile | 5 +++-- libgcompat/alias.h | 4 ++-- libgcompat/internal.h | 13 ++++++------- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index 934422f..a7437d1 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,6 @@ LIBGCOMPAT_INCLUDE = \ - libgcompat/alias.h + libgcompat/alias.h \ + libgcompat/internal.h LIBGCOMPAT_SRC = \ libgcompat/ctype.c \ libgcompat/cxx_thread.c \ @@ -72,7 +73,7 @@ clean: rm -f libgcompat/*.o loader/*.o ${LIBGCOMPAT_NAME} ${LOADER_NAME} format: - clang-format -i ${LIBGCOMPAT_SRC} ${LOADER_SRC} + clang-format -i ${LIBGCOMPAT_INCLUDE} ${LIBGCOMPAT_SRC} ${LOADER_SRC} install: all install -D -m755 ${LIBGCOMPAT_NAME} ${DESTDIR}/${LIBGCOMPAT_PATH} diff --git a/libgcompat/alias.h b/libgcompat/alias.h index 3f54672..bb99690 100644 --- a/libgcompat/alias.h +++ b/libgcompat/alias.h @@ -1,9 +1,9 @@ #ifndef _ALIAS_H_ #define _ALIAS_H_ -#define alias(old, new) \ +#define alias(old, new) \ extern __typeof(old) new __attribute__((__alias__(#old))) -#define weak_alias(old, new) \ +#define weak_alias(old, new) \ extern __typeof(old) new __attribute__((weak, __alias__(#old))) #endif /* _ALIAS_H_ */ diff --git a/libgcompat/internal.h b/libgcompat/internal.h index 3b08271..a246547 100644 --- a/libgcompat/internal.h +++ b/libgcompat/internal.h @@ -3,12 +3,11 @@ void GCOMPAT__panic(const char *fmt, ...) __attribute__((noreturn)); -#define GCOMPAT__assert_with_reason(chk, ...) \ - do { \ - if (!(chk)) { \ - GCOMPAT__panic(__VA_ARGS__); \ - } \ - } \ - while(0); +#define GCOMPAT__assert_with_reason(chk, ...) \ + do { \ + if (!(chk)) { \ + GCOMPAT__panic(__VA_ARGS__); \ + } \ + } while (0); #endif -- cgit v1.2.3-60-g2f50 From edd703b4171c5fd0790b6a0eb86709b01e435f26 Mon Sep 17 00:00:00 2001 From: Samuel Holland Date: Sat, 5 Jan 2019 09:04:02 -0600 Subject: run clang-format on the source code Signed-off-by: Samuel Holland --- libgcompat/wchar.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libgcompat/wchar.c b/libgcompat/wchar.c index 823c3e3..c60963e 100644 --- a/libgcompat/wchar.c +++ b/libgcompat/wchar.c @@ -45,7 +45,7 @@ int __vswprintf_chk(wchar_t *s, size_t n, int flag, size_t slen, * LSB 5.0: LSB-Core-generic/baselib---wcstol-internal-1.html */ long int __wcstol_internal(const wchar_t *nptr, wchar_t **endptr, int base, - int group) + int group) { assert(group == 0); return wcstol(nptr, endptr, base); -- cgit v1.2.3-60-g2f50 From 98b8025edf172ae8d3e0a619249dc1670067b0f0 Mon Sep 17 00:00:00 2001 From: Samuel Holland Date: Sat, 5 Jan 2019 08:58:45 -0600 Subject: Makefile: Clean up compiler invocations - Respect user CFLAGS and LDFLAGS consistently. - Consistent use of curly braces for variables over parentheses. - Sort flags, putting '-o' at the end for readability of `make` output. - -fPIE doesn't mean anything to the linker Signed-off-by: Samuel Holland --- Makefile | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index a7437d1..b2c3fd3 100644 --- a/Makefile +++ b/Makefile @@ -46,28 +46,30 @@ LOADER_PATH = /lib/${LOADER_NAME} ifdef WITH_LIBUCONTEXT -LIBUCONTEXT_LIBS = -Wl,--no-as-needed -lucontext LIBUCONTEXT_CFLAGS = -DWITH_LIBUCONTEXT +LIBUCONTEXT_LIBS = -lucontext endif all: ${LIBGCOMPAT_NAME} ${LOADER_NAME} ${LIBGCOMPAT_NAME}: ${LIBGCOMPAT_OBJ} - $(CC) -o ${LIBGCOMPAT_NAME} -Wl,-soname,${LIBGCOMPAT_NAME} \ - -shared ${LIBGCOMPAT_OBJ} ${LIBUCONTEXT_LIBS} + ${CC} ${CFLAGS} ${LDFLAGS} -shared -Wl,-soname,${LIBGCOMPAT_NAME} \ + -o ${LIBGCOMPAT_NAME} ${LIBGCOMPAT_OBJ} \ + -Wl,--no-as-needed ${LIBUCONTEXT_LIBS} ${LIBGCOMPAT_OBJ}: ${LIBGCOMPAT_INCLUDE} ${LOADER_NAME}: ${LOADER_OBJ} - $(CC) -o ${LOADER_NAME} -fPIE -static ${LOADER_OBJ} + ${CC} ${CFLAGS} ${LDFLAGS} -static -o ${LOADER_NAME} ${LOADER_OBJ} .c.o: - $(CC) -c -D_BSD_SOURCE -DLIBGCOMPAT=\"${LIBGCOMPAT_PATH}\" \ - -DLINKER=\"${LINKER_PATH}\" -DLOADER=\"${LOADER_NAME}\" \ - -Ilibgcompat ${LIBUCONTEXT_CFLAGS} \ - -fPIC -std=c99 -Wall -Wextra -Wno-frame-address \ - -Wno-unused-parameter ${CFLAGS} ${CPPFLAGS} -o $@ $< + ${CC} ${CPPFLAGS} ${CFLAGS} -c -D_BSD_SOURCE \ + -DLIBGCOMPAT='"${LIBGCOMPAT_PATH}"' \ + -DLINKER='"${LINKER_PATH}"' -DLOADER='"${LOADER_NAME}"' \ + -fPIC -Ilibgcompat -std=c99 \ + -Wall -Wextra -Wno-frame-address -Wno-unused-parameter \ + ${LIBUCONTEXT_CFLAGS} -o $@ $< clean: rm -f libgcompat/*.o loader/*.o ${LIBGCOMPAT_NAME} ${LOADER_NAME} -- cgit v1.2.3-60-g2f50 From 59d0cbb49bc0c69d5bd754215faaec16e2c20057 Mon Sep 17 00:00:00 2001 From: Samuel Holland Date: Sat, 5 Jan 2019 09:00:35 -0600 Subject: Makefile: Detect and link an obstack library Signed-off-by: Samuel Holland --- CHANGELOG.rst | 5 +++++ Makefile | 18 +++++++++++++++--- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 7c5365c..7df686f 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -11,6 +11,11 @@ 0.4.0 (201?-??-??) ================== +Build system +------------ + +* Allow building against libobstack. + wchar ----- diff --git a/Makefile b/Makefile index b2c3fd3..0f18af0 100644 --- a/Makefile +++ b/Makefile @@ -44,11 +44,23 @@ LOADER_OBJ = ${LOADER_SRC:.c=.o} LOADER_NAME = ld-linux.so.2 LOADER_PATH = /lib/${LOADER_NAME} -ifdef WITH_LIBUCONTEXT +PKG_CONFIG ?= pkg-config +ifdef WITH_LIBUCONTEXT LIBUCONTEXT_CFLAGS = -DWITH_LIBUCONTEXT LIBUCONTEXT_LIBS = -lucontext +endif + +ifndef WITH_OBSTACK +WITH_OBSTACK = $(shell \ + for pkg in obstack obstack-standalone; do \ + ${PKG_CONFIG} --exists "$$pkg" && { echo "$$pkg"; exit 0; } \ + done; echo "no") +endif +ifneq (${WITH_OBSTACK},no) +OBSTACK_CFLAGS = $(shell ${PKG_CONFIG} --cflags ${WITH_OBSTACK}) -DWITH_OBSTACK +OBSTACK_LIBS = $(shell ${PKG_CONFIG} --libs ${WITH_OBSTACK}) endif all: ${LIBGCOMPAT_NAME} ${LOADER_NAME} @@ -56,7 +68,7 @@ all: ${LIBGCOMPAT_NAME} ${LOADER_NAME} ${LIBGCOMPAT_NAME}: ${LIBGCOMPAT_OBJ} ${CC} ${CFLAGS} ${LDFLAGS} -shared -Wl,-soname,${LIBGCOMPAT_NAME} \ -o ${LIBGCOMPAT_NAME} ${LIBGCOMPAT_OBJ} \ - -Wl,--no-as-needed ${LIBUCONTEXT_LIBS} + -Wl,--no-as-needed ${LIBUCONTEXT_LIBS} ${OBSTACK_LIBS} ${LIBGCOMPAT_OBJ}: ${LIBGCOMPAT_INCLUDE} @@ -69,7 +81,7 @@ ${LOADER_NAME}: ${LOADER_OBJ} -DLINKER='"${LINKER_PATH}"' -DLOADER='"${LOADER_NAME}"' \ -fPIC -Ilibgcompat -std=c99 \ -Wall -Wextra -Wno-frame-address -Wno-unused-parameter \ - ${LIBUCONTEXT_CFLAGS} -o $@ $< + ${LIBUCONTEXT_CFLAGS} ${OBSTACK_CFLAGS} -o $@ $< clean: rm -f libgcompat/*.o loader/*.o ${LIBGCOMPAT_NAME} ${LOADER_NAME} -- cgit v1.2.3-60-g2f50 From de9e45d7f27dfe70634e9add7c3348aa3628a68e Mon Sep 17 00:00:00 2001 From: Samuel Holland Date: Sat, 5 Jan 2019 09:08:48 -0600 Subject: pthread: Implement pthread_getname_np Signed-off-by: Samuel Holland --- CHANGELOG.rst | 5 +++++ libgcompat/pthread.c | 25 ++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 7df686f..25b57e0 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -16,6 +16,11 @@ Build system * Allow building against libobstack. +pthread +------- + +* Add pthread_getname_np. + wchar ----- diff --git a/libgcompat/pthread.c b/libgcompat/pthread.c index 4ebbe6b..0456c40 100644 --- a/libgcompat/pthread.c +++ b/libgcompat/pthread.c @@ -1,4 +1,8 @@ -#include +#define _GNU_SOURCE +#include /* errno */ +#include /* O_CLOEXEC, O_RDONLY */ +#include /* pthread_atfork */ +#include /* open, read */ #include "alias.h" /* weak_alias */ @@ -27,3 +31,22 @@ int __register_atfork(void (*prepare)(void), void (*parent)(void), return pthread_atfork(prepare, parent, child); } weak_alias(__register_atfork, register_atfork); + +/** + * Get the name of a thread. + */ +int pthread_getname_np(pthread_t thread, char *name, size_t len) +{ + int fd = open("/proc/thread-self/comm", O_RDONLY | O_CLOEXEC); + char dummy; + + if (fd < 0) + return errno; + if (read(fd, name, len) < 0) + return errno; + /* If there's more to read, the buffer was too small. */ + if (read(fd, &dummy, 1) > 0) + return ERANGE; + + return 0; +} -- cgit v1.2.3-60-g2f50 From d186323a2db1f8bf65b24236ce8d2ddff1b4385a Mon Sep 17 00:00:00 2001 From: Samuel Holland Date: Sat, 5 Jan 2019 09:36:32 -0600 Subject: Fix compatibility with Linux 4.17+ The gcompat loader compiled as a static binary doesn't work on Linux since commit a4ff8e8620d3f4, failing with the error: ``` Uhuuh, elf segment at 0000000000400000 requested but the memory is mapped already ``` This is because the binary we are the ELF interpreter for is also loaded at 0x400000. While it's actually okay to overlap the binary, since we're going to call `execve` again, the kernel doesn't allow the overlap anymore. Fix that by compiling the loader as static PIE, so it can be loaded at a different address. Signed-off-by: Samuel Holland --- CHANGELOG.rst | 1 + Makefile | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 25b57e0..c494a38 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -15,6 +15,7 @@ Build system ------------ * Allow building against libobstack. +* Fix compatibility with Linux 4.17 and newer. pthread ------- diff --git a/Makefile b/Makefile index 0f18af0..6a36129 100644 --- a/Makefile +++ b/Makefile @@ -73,7 +73,7 @@ ${LIBGCOMPAT_NAME}: ${LIBGCOMPAT_OBJ} ${LIBGCOMPAT_OBJ}: ${LIBGCOMPAT_INCLUDE} ${LOADER_NAME}: ${LOADER_OBJ} - ${CC} ${CFLAGS} ${LDFLAGS} -static -o ${LOADER_NAME} ${LOADER_OBJ} + ${CC} ${CFLAGS} ${LDFLAGS} -static-pie -o ${LOADER_NAME} ${LOADER_OBJ} .c.o: ${CC} ${CPPFLAGS} ${CFLAGS} -c -D_BSD_SOURCE \ -- cgit v1.2.3-60-g2f50