summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSamuel Holland <samuel@sholland.org>2018-01-14 18:40:59 -0600
committerSamuel Holland <samuel@sholland.org>2018-01-14 18:57:52 -0600
commitdfafb0735f09d65a392d03a2d1ad3d0b934981da (patch)
treef5a1c49f22df2e72494cd114785763b111d6924a
parentd40369b0e09e4e2228ebff305067ec2d99220848 (diff)
downloadgcompat-dfafb0735f09d65a392d03a2d1ad3d0b934981da.tar.gz
gcompat-dfafb0735f09d65a392d03a2d1ad3d0b934981da.tar.bz2
gcompat-dfafb0735f09d65a392d03a2d1ad3d0b934981da.tar.xz
gcompat-dfafb0735f09d65a392d03a2d1ad3d0b934981da.zip
libgcompat: Add and use a macro for defining symbol aliases
* Prefer providing the underscore-prefixed symbol as the strong definition. * Do not use a weak alias if the alias is also underscore-prefixed. * Make libgcompat objects depend on the new header. [NOTE: I originally took the weak_alias macro from musl's libc.h, but it's trivial and the same pattern already in use. If desired, I can add the musl copyright notice.] Signed-off-by: Samuel Holland <samuel@sholland.org>
-rw-r--r--Makefile4
-rw-r--r--libgcompat/alias.h9
-rw-r--r--libgcompat/malloc.c14
-rw-r--r--libgcompat/math.c11
-rw-r--r--libgcompat/pthread.c7
-rw-r--r--libgcompat/resolv.c13
-rw-r--r--libgcompat/string.c8
7 files changed, 38 insertions, 28 deletions
diff --git a/Makefile b/Makefile
index 6c532fd..b941dcf 100644
--- a/Makefile
+++ b/Makefile
@@ -1,3 +1,5 @@
+LIBGCOMPAT_INCLUDE = \
+ libgcompat/alias.h
LIBGCOMPAT_SRC = \
libgcompat/backtrace.c \
libgcompat/dlmopen.c \
@@ -33,6 +35,8 @@ ${LIBGCOMPAT_NAME}: ${LIBGCOMPAT_OBJ}
$(CC) -o ${LIBGCOMPAT_NAME} -Wl,-soname,${LIBGCOMPAT_NAME} \
-shared ${LIBGCOMPAT_OBJ}
+${LIBGCOMPAT_OBJ}: ${LIBGCOMPAT_INCLUDE}
+
${LOADER_NAME}: ${LOADER_OBJ}
$(CC) -o ${LOADER_NAME} -fPIE -static ${LOADER_OBJ}
diff --git a/libgcompat/alias.h b/libgcompat/alias.h
new file mode 100644
index 0000000..3f54672
--- /dev/null
+++ b/libgcompat/alias.h
@@ -0,0 +1,9 @@
+#ifndef _ALIAS_H_
+#define _ALIAS_H_
+
+#define alias(old, new) \
+ extern __typeof(old) new __attribute__((__alias__(#old)))
+#define weak_alias(old, new) \
+ extern __typeof(old) new __attribute__((weak, __alias__(#old)))
+
+#endif /* _ALIAS_H_ */
diff --git a/libgcompat/malloc.c b/libgcompat/malloc.c
index db85d8d..dc9d25e 100644
--- a/libgcompat/malloc.c
+++ b/libgcompat/malloc.c
@@ -27,6 +27,8 @@
#include <stdlib.h> /* {m,c,re}alloc, free */
#include <string.h> /* memset */
+#include "alias.h" /* alias */
+
struct mallinfo {
int arena; /* Non-mmapped space allocated (bytes) */
int ordblks; /* Number of free chunks */
@@ -51,31 +53,27 @@ void *__libc_malloc(size_t size)
{
return malloc(size);
}
+alias(__libc_malloc, __malloc);
void __libc_free(void *ptr)
{
return free(ptr);
}
+alias(__libc_free, __free);
void *__libc_calloc(size_t nmemb, size_t size)
{
return calloc(nmemb, size);
}
+alias(__libc_calloc, __calloc);
void *__libc_realloc(void *ptr, size_t size)
{
return realloc(ptr, size);
}
+alias(__libc_realloc, __realloc);
void *__libc_memalign(size_t align, size_t len)
{
return memalign(align, len);
}
-
-extern __typeof(__libc_malloc) __malloc
- __attribute__((weak, alias("__libc_malloc")));
-extern __typeof(__libc_calloc) __calloc
- __attribute__((weak, alias("__libc_calloc")));
-extern __typeof(__libc_realloc) __realloc
- __attribute__((weak, alias("__libc_realloc")));
-extern __typeof(__libc_free) __free __attribute__((weak, alias("__libc_free")));
diff --git a/libgcompat/math.c b/libgcompat/math.c
index 42ed032..2982116 100644
--- a/libgcompat/math.c
+++ b/libgcompat/math.c
@@ -1,26 +1,27 @@
#include <math.h> /* isinf, isnan */
+#include "alias.h" /* weak_alias */
+
int __isinff(float number)
{
return isinf(number);
}
+weak_alias(__isinff, isinff);
int __isinf(double number)
{
return isinf(number);
}
+weak_alias(__isinf, isinf);
int __isnanf(float number)
{
return isnan(number);
}
+weak_alias(__isnanf, isnanf);
int __isnan(double number)
{
return isnan(number);
}
-
-extern __typeof(__isnanf) isnanf __attribute__((weak, alias("__isnanf")));
-extern __typeof(__isnan) isnan __attribute__((weak, alias("__isnan")));
-extern __typeof(__isinff) isinff __attribute__((weak, alias("__isinff")));
-extern __typeof(__isinf) isinf __attribute__((weak, alias("__isinf")));
+weak_alias(__isnan, isnan);
diff --git a/libgcompat/pthread.c b/libgcompat/pthread.c
index 52daceb..9333554 100644
--- a/libgcompat/pthread.c
+++ b/libgcompat/pthread.c
@@ -1,14 +1,13 @@
#include <pthread.h>
+#include "alias.h" /* weak_alias */
+
int __register_atfork(void (*prepare)(void), void (*parent)(void),
void (*child)(void))
{
return pthread_atfork(prepare, parent, child);
}
-
-int register_atfork(void (*prepare)(void), void (*parent)(void),
- void (*child)(void))
- __attribute__((weak, alias("__register_atfork")));
+weak_alias(__register_atfork, register_atfork);
void __pthread_register_cancel(void *buf)
{
diff --git a/libgcompat/resolv.c b/libgcompat/resolv.c
index 3566dcd..7ad984f 100644
--- a/libgcompat/resolv.c
+++ b/libgcompat/resolv.c
@@ -9,7 +9,9 @@
#include <resolv.h> /* res_state */
#include <string.h> /* memcpy, memset */
-static inline int res_ninit(res_state statp)
+#include "alias.h" /* weak_alias */
+
+static inline int __res_ninit(res_state statp)
{
int rc = res_init();
if (statp != &_res) {
@@ -17,8 +19,9 @@ static inline int res_ninit(res_state statp)
}
return rc;
}
+weak_alias(__res_ninit, res_ninit);
-static inline int res_nclose(res_state statp)
+static inline int __res_nclose(res_state statp)
{
if (!statp) {
return -1;
@@ -28,8 +31,4 @@ static inline int res_nclose(res_state statp)
}
return 0;
}
-
-extern __typeof(res_ninit) __res_ninit
- __attribute__((weak, alias("res_ninit")));
-extern __typeof(res_nclose) __res_nclose
- __attribute__((weak, alias("res_nclose")));
+weak_alias(__res_nclose, res_nclose);
diff --git a/libgcompat/string.c b/libgcompat/string.c
index 60f61fc..7ca0bf6 100644
--- a/libgcompat/string.c
+++ b/libgcompat/string.c
@@ -2,6 +2,8 @@
#include <stdlib.h> /* strto[u?]ll */
#include <string.h> /* memcpy, strcpy, strncat, strndup */
+#include "alias.h" /* weak_alias */
+
/* "Checked" memcpy */
void *__memcpy_chk(void *dest, const void *src, size_t len, size_t destlen)
{
@@ -60,7 +62,7 @@ char *__strndup(const char *str, size_t count)
/* The existance of this method, and the fact it is used in real code, gives
* me nightmares. */
-void *rawmemchr(const void *s, int c)
+void *__rawmemchr(const void *s, int c)
{
const unsigned char *haystack = s;
unsigned char needle = (unsigned char) c;
@@ -68,9 +70,7 @@ void *rawmemchr(const void *s, int c)
;
return (void *) haystack;
}
-
-extern __typeof(rawmemchr) __rawmemchr
- __attribute__((weak, alias("rawmemchr")));
+weak_alias(__rawmemchr, rawmemchr);
/* Another useless __ alias */
char *__strtok_r(char *str, const char *delim, char **saveptr)