From a01af4cb4991f6d5f0320f03d1f1d918df07caec Mon Sep 17 00:00:00 2001 From: Marian Buschsieweke Date: Sat, 11 May 2024 17:51:38 +0200 Subject: [PATCH] Wrapper targets: Add endpoints for -Wl,wrap=... This adds aliases needed to wrap printf() and friends. --- printf.c | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) diff --git a/printf.c b/printf.c index edf41d1..e00c3e5 100644 --- a/printf.c +++ b/printf.c @@ -32,6 +32,9 @@ #include #include +#include +#include +#include #include "printf.h" #include "stdio_base.h" @@ -920,3 +923,99 @@ static void _putchar(char character) { stdio_write(&character, sizeof(character)); } + + +/* provide entry points for linker to redirect stdio */ +__attribute__((alias("printf_"))) +int __wrap_printf(const char* format, ...); + + +__attribute__((alias("sprintf_"))) +int __wrap_sprintf(char* buffer, const char* format, ...); + + +__attribute__((alias("snprintf_"))) +int __wrap_snprintf(char* buffer, size_t count, const char* format, ...); + + +__attribute__((alias("vprintf_"))) +int __wrap_vprintf(const char* format, va_list va); + + +__attribute__((alias("vsnprintf_"))) +int __wrap_vsnprintf(char* buffer, size_t count, const char* format, va_list va); + + +int __wrap_putchar(int c) +{ + _putchar((char)c); + return 1; +} + + +int __wrap_putc(int c, FILE *stream) +{ + (void)stream; + _putchar((char)c); + return 1; +} + + +__attribute__((alias("__wrap_putc"))) +int __wrap_fputc(int c, FILE *stream); + + +int __wrap_puts(const char *s) +{ + size_t len = strlen(s); + stdio_write(s, len); + stdio_write("\n", 1); + return len + 1; +} + + +int __wrap_vfprintf(FILE *stream, const char *format, va_list va) +{ + if (stream != stdout) { + return 0; + } + + return vprintf_(format, va); +} + + +int __wrap_fprintf(FILE *stream, const char *format, ...) +{ + va_list va; + va_start(va, format); + int result = __wrap_vfprintf(stream, format, va); + va_end(va); + return result; +} + + +int __wrap_vdprintf(int fd, const char *format, va_list va) +{ + if (fd != STDOUT_FILENO) { + return 0; + } + + return vprintf_(format, va); +} + + +int __wrap_vsprintf(char* buffer, const char* format, va_list va) +{ + return __wrap_vsnprintf(buffer, (size_t)-1, format, va); +} + + +int __wrap_dprintf(int fd, const char *format, ...) +{ + va_list va; + va_start(va, format); + int result = __wrap_vdprintf(fd, format, va); + va_end(va); + return result; + +} -- 2.43.0