Skip to content

Commit 3b59f6c

Browse files
committedJan 13, 2020
Minimal-printf: Fix wrapping of printf functions for the ARM compiler
As the ARM compiler is in GNU compatible mode, it defines __GNU__ which (based on pre-processor condition in mbed_printf_implentation.h) causes the printf functions to be wrapped using _wrap_* instead of $Sub$$*. This commit modifies the pre-processor conditions to check for __GNUC__last in order to correctly substitute the printf functions depending on the toolchain in use. It also gets rid of $Super$$* substitution as it is not needed. $Super$$ is used to identify the original unpatched functions. Missing substitutions for ARM compiler internal optimized "printfs" are also added.

File tree

2 files changed

+31
-47
lines changed

2 files changed

+31
-47
lines changed
 

‎platform/source/minimal-printf/mbed_printf_armlink_overrides.c

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,34 @@ int $Sub$$__2snprintf(char *buffer, size_t length, const char *format, ...)
6666
return result;
6767
}
6868

69-
int $Sub$$__2vprintf(char *buffer, const char *format, ...)
69+
int $Sub$$__2vprintf(const char *format, va_list arguments)
70+
{
71+
return mbed_minimal_formatted_string(NULL, LONG_MAX, format, arguments, stdout);
72+
}
73+
74+
int $Sub$$__2vsprintf(char *buffer, const char *format, va_list arguments)
75+
{
76+
return mbed_minimal_formatted_string(buffer, LONG_MAX, format, arguments, NULL);
77+
}
78+
79+
int $Sub$$__2vsnprintf(char *buffer, size_t length, const char *format, va_list arguments)
80+
{
81+
return mbed_minimal_formatted_string(buffer, length, format, arguments, NULL);
82+
}
83+
84+
int $Sub$$__2fprintf(FILE *stream, const char *format, ...)
7085
{
7186
va_list arguments;
7287
va_start(arguments, format);
73-
int result = mbed_minimal_formatted_string(buffer, LONG_MAX, format, arguments, stdout);
88+
int result = mbed_minimal_formatted_string(NULL, LONG_MAX, format, arguments, stream);
7489
va_end(arguments);
7590

7691
return result;
7792
}
7893

79-
int $Sub$$__2vsnprintf(char *buffer, size_t length, const char *format, va_list arguments)
94+
int $Sub$$__2vfprintf(FILE *stream, const char *format, va_list arguments)
8095
{
81-
return mbed_minimal_formatted_string(buffer, length, format, arguments, NULL);
96+
return mbed_minimal_formatted_string(NULL, LONG_MAX, format, arguments, stream);
8297
}
8398

8499
/**

‎platform/source/minimal-printf/mbed_printf_wrapper.c

Lines changed: 12 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -21,46 +21,15 @@
2121
#include <limits.h>
2222

2323

24-
#if defined(__GNUC__) /* GCC */
25-
#define SUPER_PRINTF __real_printf
26-
#define SUB_PRINTF __wrap_printf
27-
#define SUPER_SPRINTF __real_sprintf
28-
#define SUB_SPRINTF __wrap_sprintf
29-
#define SUPER_SNPRINTF __real_snprintf
30-
#define SUB_SNPRINTF __wrap_snprintf
31-
#define SUPER_VPRINTF __real_vprintf
32-
#define SUB_VPRINTF __wrap_vprintf
33-
#define SUPER_VSPRINTF __real_vsprintf
34-
#define SUB_VSPRINTF __wrap_vsprintf
35-
#define SUPER_VSNPRINTF __real_vsnprintf
36-
#define SUB_VSNPRINTF __wrap_vsnprintf
37-
#define SUPER_FPRINTF __real_fprintf
38-
#define SUB_FPRINTF __wrap_fprintf
39-
#define SUPER_VFPRINTF __real_vfprintf
40-
#define SUB_VFPRINTF __wrap_vfprintf
41-
#elif defined(TOOLCHAIN_ARM) /* ARMC5/ARMC6 */\
42-
|| defined(__ICCARM__) /* IAR */
43-
#define SUPER_PRINTF $Super$$printf
44-
#define SUB_PRINTF $Sub$$printf
45-
#define SUPER_SPRINTF $Super$$sprintf
46-
#define SUB_SPRINTF $Sub$$sprintf
47-
#define SUPER_SNPRINTF $Super$$snprintf
48-
#define SUB_SNPRINTF $Sub$$snprintf
49-
#define SUPER_VPRINTF $Super$$vprintf
50-
#define SUB_VPRINTF $Sub$$vprintf
51-
#define SUPER_VSPRINTF $Super$$vsprintf
52-
#define SUB_VSPRINTF $Sub$$vsprintf
53-
#define SUPER_VSNPRINTF $Super$$vsnprintf
54-
#define SUB_VSNPRINTF $Sub$$vsnprintf
55-
#define SUPER_FPRINTF $Super$$fprintf
56-
#define SUB_FPRINTF $Sub$$fprintf
57-
#define SUPER_VFPRINTF $Super$$vfprintf
58-
#define SUB_VFPRINTF $Sub$$vfprintf
24+
#if defined(__ARMCC_VERSION) || defined(__ICCARM__)
25+
# define PREFIX(x) $Sub$$##x
26+
#elif defined(__GNUC__)
27+
# define PREFIX(x) __wrap_##x
5928
#else
6029
#warning "This compiler is not yet supported."
6130
#endif
6231

63-
int SUB_PRINTF(const char *format, ...)
32+
int PREFIX(printf)(const char *format, ...)
6433
{
6534
va_list arguments;
6635
va_start(arguments, format);
@@ -70,7 +39,7 @@ int SUB_PRINTF(const char *format, ...)
7039
return result;
7140
}
7241

73-
int SUB_SPRINTF(char *buffer, const char *format, ...)
42+
int PREFIX(sprintf)(char *buffer, const char *format, ...)
7443
{
7544
va_list arguments;
7645
va_start(arguments, format);
@@ -80,7 +49,7 @@ int SUB_SPRINTF(char *buffer, const char *format, ...)
8049
return result;
8150
}
8251

83-
int SUB_SNPRINTF(char *buffer, size_t length, const char *format, ...)
52+
int PREFIX(snprintf)(char *buffer, size_t length, const char *format, ...)
8453
{
8554
va_list arguments;
8655
va_start(arguments, format);
@@ -90,22 +59,22 @@ int SUB_SNPRINTF(char *buffer, size_t length, const char *format, ...)
9059
return result;
9160
}
9261

93-
int SUB_VPRINTF(const char *format, va_list arguments)
62+
int PREFIX(vprintf)(const char *format, va_list arguments)
9463
{
9564
return mbed_minimal_formatted_string(NULL, LONG_MAX, format, arguments, stdout);
9665
}
9766

98-
int SUB_VSPRINTF(char *buffer, const char *format, va_list arguments)
67+
int PREFIX(vsprintf)(char *buffer, const char *format, va_list arguments)
9968
{
10069
return mbed_minimal_formatted_string(buffer, LONG_MAX, format, arguments, NULL);
10170
}
10271

103-
int SUB_VSNPRINTF(char *buffer, size_t length, const char *format, va_list arguments)
72+
int PREFIX(vsnprintf)(char *buffer, size_t length, const char *format, va_list arguments)
10473
{
10574
return mbed_minimal_formatted_string(buffer, length, format, arguments, NULL);
10675
}
10776

108-
int SUB_FPRINTF(FILE *stream, const char *format, ...)
77+
int PREFIX(fprintf)(FILE *stream, const char *format, ...)
10978
{
11079
va_list arguments;
11180
va_start(arguments, format);
@@ -115,7 +84,7 @@ int SUB_FPRINTF(FILE *stream, const char *format, ...)
11584
return result;
11685
}
11786

118-
int SUB_VFPRINTF(FILE *stream, const char *format, va_list arguments)
87+
int PREFIX(vfprintf)(FILE *stream, const char *format, va_list arguments)
11988
{
12089
return mbed_minimal_formatted_string(NULL, LONG_MAX, format, arguments, stream);
12190
}

0 commit comments

Comments
 (0)
Please sign in to comment.