exports overhaul
cover exports overhaul
This all started from me wondering "why does this standalone app end up being so huge"? Oh, it essentially links in its own standard C library to get strlen() and snprintf(). Which then led to asking "why don't we export all those standard C functions when we have them anyway?".
CI has chewed on these and seem happy - it was CI which told me about the necessity of [1/9]: https://github.com/u-boot/u-boot/pull/813
Rasmus Villemoes (9):
malloc.h: be a bit more consistent with macro definitions _exports.h: drop creating dummy i2c entries and fixup config
dependency
_exports.h: simplify condition for including spi functions _exports.h: drop the last dummy entries
exports.c: drop unused dummy function
exports.h: make sure declarations are in sync with the actual exports _exports.h: reorganize a bit
_exports.h: export standard memory/string handling functions exports.h: bump XF_VERSION
common/exports.c | 11 ---------- include/_exports.h | 50 ++++++++++++++++++++++------------------------ include/exports.h | 38 ++++------------------------------- include/malloc.h | 6 +++--- 4 files changed, 31 insertions(+), 74 deletions(-)
Signature · 1 line
2.51.0
PATCH 1/9 malloc.h: be a bit more consistent with macro definitions
Currrently, malloc and free are function-like macros, while calloc, realloc and memalign are object-like macros.
Usually, this doesn't matter, but it does when the identifiers appear without a following open parenthesis, such as when their address is taken for building the export table. Adding calloc or realloc to that table breaks the build on sandbox due to this inconsistency.
--- include/malloc.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
include/malloc.h +3 -3
@@ -904,11 +904,11 @@ void *realloc_simple(void *ptr, size_t size);# define mALLOPt dlmallopt/* Ensure that U-Boot actually uses these too */ -#define calloc dlcalloc +#define calloc (x,y) dlcalloc(x,y)#define free (ptr) dlfree(ptr)#define malloc (x) dlmalloc(x) -#define memalign dlmemalign -#define realloc dlrealloc +#define memalign (a,x) dlmemalign(a,x) +#define realloc (p,x) dlrealloc(p,x)#define valloc dlvalloc#define pvalloc dlpvalloc#define mallinfo () dlmallinfo()
Signature · 1 line
2.51.0
PATCH 2/9 _exports.h: drop creating dummy i2c entries and fixup config dependency
In reply to Rasmus VillemoesThere's really no good reason to create stub entries that would call a function that doesn't even return anything sensible.
The existence of these two i2c_* functions depends on CONFIG_IS_ENABLED(SYS_I2C_LEGACY), which does depend on !DM_I2C, but is not equivalent to it. They are probably rather hard to use unless CMD_I2C and something in U-Boot has called "i2c dev foo" to set the current i2c bus before calling the standalone app, so keep that dependency.
--- include/_exports.h | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-)
include/_exports.h +1 -5
@@ -1,7 +1,6 @@/* * You need to use #ifdef around functions that may not exist * in the final configuration (such as i2c). -* use a dummyfunction as first parameter to EXPORT_FUNC. * As an example see the CONFIG_CMD_I2C section below */ #ifndef EXPORT_FUNC
@@ -43,12 +42,9 @@EXPORT_FUNC (simple_strtol, long, simple_strtol, const char *, char **, unsigned int) EXPORT_FUNC(strcmp, int, strcmp, const char *cs, const char *ct) -#if defined(CONFIG_CMD_I2C ) && !CONFIG_IS_ENABLED (DM_I2C ) +#if defined(CONFIG_CMD_I2C ) &&CONFIG_IS_ENABLED (SYS_I2C_LEGACY ) EXPORT_FUNC(i2c_write, int, i2c_write, uchar, uint, int , uchar * , int) EXPORT_FUNC(i2c_read, int, i2c_read, uchar, uint, int , uchar * , int) -#else - EXPORT_FUNC(dummy, void, i2c_write, void) - EXPORT_FUNC(dummy, void, i2c_read, void)#endif #if !defined(CONFIG_CMD_SPI ) || defined(CONFIG_DM_SPI )
Signature · 1 line
2.51.0
PATCH 3/9 _exports.h: simplify condition for including spi functions
In reply to Rasmus VillemoesAs for the i2c functions, drop the dummy entries that, if ever used, would just have the standalone app get some random content in the return register.
While deprecated, the spi_{setup,free}_slave functions do exist even with CONFIG_DM_SPI - and a standalone app can't really do anything but refer to a spi device via a (bus, cs) pair.
Eventually, one should probably export some function that could allow a standalone app to get a struct udevice* corresponding to either a full DT path, an alias, or perhaps a label (provided one builds with -@), and then export functions that can operate on that.
--- include/_exports.h | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-)
include/_exports.h +1 -10
@@ -47,19 +47,10 @@ EXPORT_FUNC(i2c_read, int, i2c_read, uchar, uint, int , uchar * , int) #endif -#if !defined(CONFIG_CMD_SPI) || defined(CONFIG_DM_SPI) - EXPORT_FUNC(dummy, void, spi_setup_slave, void) - EXPORT_FUNC(dummy, void, spi_free_slave, void) -#else +#if defined(CONFIG_CMD_SPI) EXPORT_FUNC(spi_setup_slave, struct spi_slave *, spi_setup_slave, unsigned int, unsigned int, unsigned int, unsigned int) EXPORT_FUNC(spi_free_slave, void, spi_free_slave, struct spi_slave *) -#endif -#ifndef CONFIG_CMD_SPI - EXPORT_FUNC(dummy, void, spi_claim_bus, void) - EXPORT_FUNC(dummy, void, spi_release_bus, void) - EXPORT_FUNC(dummy, void, spi_xfer, void) -#else EXPORT_FUNC(spi_claim_bus, int, spi_claim_bus, struct spi_slave *) EXPORT_FUNC(spi_release_bus, void, spi_release_bus, struct spi_slave *) EXPORT_FUNC(spi_xfer, int, spi_xfer, struct spi_slave *,
Signature · 1 line
2.51.0
PATCH 4/9 _exports.h: drop the last dummy entries
In reply to Rasmus Villemoes--- include/_exports.h | 3 --- 1 file changed, 3 deletions(-)
include/_exports.h +0 -3
@@ -20,9 +20,6 @@ int, interrupt_handler_t, void*) EXPORT_FUNC(irq_free_handler, void, free_hdlr, int) -#else - EXPORT_FUNC(dummy, void, install_hdlr, void) - EXPORT_FUNC(dummy, void, free_hdlr, void) #endif EXPORT_FUNC(malloc, void *, malloc, size_t) #if !CONFIG_IS_ENABLED(SYS_MALLOC_SIMPLE)
Signature · 1 line
2.51.0
PATCH 5/9 exports.c: drop unused dummy function
In reply to Rasmus VillemoesThe !CONFIG_PHY_AQUANTIA defines were already superfluous since _exports.h does have a CONFIG_PHY_AQUANTIA, so the entries never existed. In fact, it couldn't have worked, because the defines would affect both occurences of the mdio_get_current_dev identifier in the
EXPORT_FUNC(mdio_get_current_dev, struct mii_dev *,
mdio_get_current_dev, void)
so the C code would end up containing four copies of
gd->jt->dummy = dummy
but struct jt_funcs would not and does not have any 'dummy' member.
Now that nothing in _exports.h refers to dummy(), remove the empty function.
--- common/exports.c | 11 ----------- 1 file changed, 11 deletions(-)
common/exports.c +0 -11
@@ -7,10 +7,6 @@DECLARE_GLOBAL_DATA_PTR ; -__attribute__((unused))static void dummy (void ) -{ -} -unsigned long get_version (void ) {return XF_VERSION ;
@@ -18,13 +14,6 @@ unsigned long get_version(void)#define EXPORT_FUNC (f, a, x, ...) gd->jt->x = f; -#ifndef CONFIG_PHY_AQUANTIA -# define mdio_get_current_dev dummy -# define phy_find_by_mask dummy -# define mdio_phydev_for_ethname dummy -# define miiphy_set_current_dev dummy -#endif -int jumptable_init (void ) { gd->jt =malloc (sizeof (struct jt_funcs ));
Signature · 1 line
2.51.0
PATCH 6/9 exports.h: make sure declarations are in sync with the actual exports
In reply to Rasmus VillemoesAfter finishing a later patch in this series, I discovered I had neglected to update the list of declarations in exports.h to match. But then I realized I wasn't the first to do that.
Use the existing mechanism and DRY it out.
--- include/exports.h | 36 +++--------------------------------- 1 file changed, 3 insertions(+), 33 deletions(-)
include/exports.h +3 -33
@@ -26,39 +26,9 @@ struct spi_slave;int jumptable_init (void );/* These are declarations of exported functions available in C code */ -unsigned long get_version (void ); -int getc (void ); -int tstc (void ); -void putc (const char ); -void puts (const char *); -int printf (const char * fmt, ...); -void install_hdlr (int ,interrupt_handler_t ,void *); -void free_hdlr (int ); -void *malloc (size_t ); -#if !CONFIG_IS_ENABLED (SYS_MALLOC_SIMPLE ) -void free (void *); -#endif -void __udelay (unsigned long ); -unsigned long get_timer (unsigned long ); -int vprintf (const char *,va_list ); -unsigned long simple_strtoul (const char *cp,char **endp,unsigned int base); -int strict_strtoul (const char *cp,unsigned int base,unsigned long *res); -char *env_get (const char *name); -int env_set (const char *varname,const char *value); -long simple_strtol (const char *cp,char **endp,unsigned int base); -int strcmp (const char *cs,const char *ct); -unsigned long ustrtoul (const char *cp,char **endp,unsigned int base); -unsigned long long ustrtoull (const char *cp,char **endp,unsigned int base); -#if defined(CONFIG_CMD_I2C ) && !CONFIG_IS_ENABLED (DM_I2C ) -int i2c_write (uchar ,uint ,int ,uchar * ,int ); -int i2c_read (uchar ,uint ,int ,uchar * ,int ); -#endif -#ifdef CONFIG_PHY_AQUANTIA -struct mii_dev *mdio_get_current_dev (void ); -struct phy_device *phy_find_by_mask (struct mii_dev *bus,unsigned phy_mask); -struct phy_device *mdio_phydev_for_ethname (const char *ethname); -int miiphy_set_current_dev (const char *devname); -#endif +#define EXPORT_FUNC (impl, res, func, ...) res func(__VA_ARGS__); +#include <_exports.h> +#undef EXPORT_FUNCvoid app_startup (char *const *);
Signature · 1 line
2.51.0
PATCH 7/9 _exports.h: reorganize a bit
In reply to Rasmus VillemoesThe current list of exported functions is somewhat of a mess. Reorganize them so that related functionality is kept together:
- console I/O: move vprintf next to printf and the getc/putc functions
- integer parsing: move the *strto* functions together
- standard string.h stuff: move memset() and strcpy() next to strcmp()
- time: move mdelay() next to udelay() and get_timer()
--- include/_exports.h | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)
include/_exports.h +8 -8
@@ -15,6 +15,7 @@ EXPORT_FUNC(flush, void, flush, void)#endif EXPORT_FUNC(printf, int, printf, const char*, ...) + EXPORT_FUNC(vprintf, int, vprintf, const char *, va_list)#if (defined(CONFIG_X86 ) && !defined(CONFIG_X86_64 )) || defined(CONFIG_PPC )EXPORT_FUNC (irq_install_handler, void, install_hdlr, int, interrupt_handler_t, void*)
@@ -25,9 +26,9 @@#if !CONFIG_IS_ENABLED (SYS_MALLOC_SIMPLE ) EXPORT_FUNC(free, void, free, void *) #endif + EXPORT_FUNC(mdelay, void, mdelay, unsigned long msec) EXPORT_FUNC(udelay, void, udelay, unsigned long) EXPORT_FUNC(get_timer, unsigned long, get_timer, unsigned long) - EXPORT_FUNC(vprintf, int, vprintf, const char *, va_list) EXPORT_FUNC(do_reset, int, do_reset, struct cmd_tbl *, int , int , char * const []) EXPORT_FUNC(env_get, char *, env_get, const char*)
@@ -38,7 +39,13 @@ const char *, unsigned int , unsigned long *)EXPORT_FUNC (simple_strtol ,long ,simple_strtol ,const char *,char **,unsigned int ) + EXPORT_FUNC(ustrtoul, unsigned long, ustrtoul, + const char *, char **, unsigned int) + EXPORT_FUNC(ustrtoull, unsigned long long, ustrtoull, + const char *, char **, unsigned int) + EXPORT_FUNC(memset, void *, memset, void *, int, size_t) EXPORT_FUNC(strcmp, int, strcmp, const char *cs, const char *ct) + EXPORT_FUNC(strcpy, char *, strcpy, char *dest, const char *src) #if defined(CONFIG_CMD_I2C) && CONFIG_IS_ENABLED(SYS_I2C_LEGACY) EXPORT_FUNC(i2c_write, int, i2c_write, uchar, uint, int , uchar * , int) EXPORT_FUNC(i2c_read, int, i2c_read, uchar, uint, int , uchar * , int)
@@ -53,13 +60,6 @@ EXPORT_FUNC(spi_xfer, int, spi_xfer, struct spi_slave *, unsigned int, const void *, void *, unsigned long)#endif - EXPORT_FUNC(ustrtoul, unsigned long, ustrtoul, - const char *, char **, unsigned int) - EXPORT_FUNC(ustrtoull, unsigned long long, ustrtoull, - const char *, char **, unsigned int) - EXPORT_FUNC(strcpy, char *, strcpy, char *dest, const char *src) - EXPORT_FUNC(mdelay, void, mdelay, unsigned long msec) - EXPORT_FUNC(memset, void *, memset, void *, int, size_t)#ifdef CONFIG_PHY_AQUANTIA EXPORT_FUNC(mdio_get_current_dev, struct mii_dev *, mdio_get_current_dev, void)
Signature · 1 line
2.51.0
PATCH 8/9 _exports.h: export standard memory/string handling functions
In reply to Rasmus VillemoesThe current list of exported functions lacks quite a few bog-standard C library functions that we might as well expose, since U-Boot certainly has them implemented anyway. There's no reason a standalone application should have its own strlen() implementation or link in a copy from some tiny libc.
For a customer's standalone app, this means it goes from 95K to 10K. More importantly, we can ditch the custom toolchain including a newlibc used to build the standalone app and just use the same toolchain as used to build u-boot itself.
--- include/_exports.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
include/_exports.h +14 -0
@@ -23,6 +23,8 @@ EXPORT_FUNC(irq_free_handler, void, free_hdlr, int)#endif EXPORT_FUNC(malloc, void *, malloc, size_t) + EXPORT_FUNC(realloc, void *, realloc, void *, size_t) + EXPORT_FUNC(calloc, void *, calloc, size_t, size_t)#if !CONFIG_IS_ENABLED (SYS_MALLOC_SIMPLE ) EXPORT_FUNC(free, void, free, void *)#endif
@@ -43,9 +45,21 @@ const char *, char **, unsigned int) EXPORT_FUNC(ustrtoull, unsigned long long, ustrtoull, const char *, char **, unsigned int) + EXPORT_FUNC(memcmp, int, memcmp, const void *, const void *, size_t) + EXPORT_FUNC(memcpy, void *, memcpy, void *, const void *, size_t) + EXPORT_FUNC(memmove, void *, memmove, void *, const void *, size_t) EXPORT_FUNC(memset, void *, memset, void *, int, size_t) + EXPORT_FUNC(strchr, char *, strchr, const char *cs, int c) + EXPORT_FUNC(strlen, size_t, strlen, const char *s) + EXPORT_FUNC(strncmp, int, strncmp, const char *cs, const char *ct, size_t n) + EXPORT_FUNC(strncpy, char *, strncpy, char *dest, const char *src, size_t n) + EXPORT_FUNC(strnlen, size_t, strnlen, const char *s, size_t n) EXPORT_FUNC(strcmp, int, strcmp, const char *cs, const char *ct) EXPORT_FUNC(strcpy, char *, strcpy, char *dest, const char *src) + EXPORT_FUNC(sprintf, int, sprintf, char *, const char *, ...) + EXPORT_FUNC(snprintf, int, snprintf, char *, size_t, const char *, ...) + EXPORT_FUNC(vsprintf, int, vsprintf, char *, const char *, va_list) + EXPORT_FUNC(vsnprintf, int, vsnprintf, char *, size_t, const char *, va_list) #if defined(CONFIG_CMD_I2C) && CONFIG_IS_ENABLED(SYS_I2C_LEGACY) EXPORT_FUNC(i2c_write, int, i2c_write, uchar, uint, int , uchar * , int) EXPORT_FUNC(i2c_read, int, i2c_read, uchar, uint, int , uchar * , int)
Signature · 1 line
2.51.0
PATCH 9/9 exports.h: bump XF_VERSION
In reply to Rasmus VillemoesThere have been quite a few changes to _exports.h since the last update of XF_VERSION, also before the previous patches in this series.
I doubt the mechanism is actually being used in practice, it is simply too fragile: Not only does the list of exported functions depend on .config, so with the same XF_VERSION the jump table entries could have different offsets. But getting to the jump table itself from gd to even call the ->get_version() is fragile, since offsetof(gd_t, jt) can, and does, change. For example, as recently as commit d9902107027 ("global_data: Remove jump table in SPL").
One really must build one's standalone app against the proper U-Boot version and config.h. But for good measure, do bump it now.
--- include/exports.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
include/exports.h +1 -1
@@ -40,7 +40,7 @@ struct jt_funcs {#undef EXPORT_FUNC }; -#define XF_VERSION 9 +#define XF_VERSION 10#if defined(CONFIG_X86 )extern gd_t *global_data;
Signature · 1 line
2.51.0
Quoted 8 lines · Rasmus Villemoes
This all started from me wondering "why does this standalone app end up being so huge"? Oh, it essentially links in its own standard C library to get
strlen()andsnprintf(). Which then led to asking "why don't we export all those standard C functions when we have them anyway?".CI has chewed on these and seem happy - it was CI which told me about the necessity of [1/9]: https://github.com/u-boot/u-boot/pull/813
For the series:
That said, in general and looking towards the future, I do think "needs a non-trivial standalone app" is better done as a standalone EFI app rather than U-Boot's standalone API support. Our standalone API is weird and fragile (as this series shows you're well aware) but the EFI one is well defined and we just implement it.
Signature · 1 line
Tom
Quoted 10 lines · Rasmus Villemoes
This all started from me wondering "why does this standalone app end up being so huge"? Oh, it essentially links in its own standard C library to get
strlen()andsnprintf(). Which then led to asking "why don't we export all those standard C functions when we have them anyway?".CI has chewed on these and seem happy - it was CI which told me about the necessity of [1/9]: https://github.com/u-boot/u-boot/pull/813
[...]
Now, it's important to note that this does increase the size of the API option because it adds more functionality there. Some size constrained platforms may wish to disable API support altogther and gain more space savings.
Applied to u-boot/next, thanks!
[1/9] malloc.h: be a bit more consistent with macro definitions
commit: e54bf98c3afc6dc6e6ce9496c217cb1c660aac4d
[2/9] _exports.h: drop creating dummy i2c entries and fixup config dependency
commit: 0933358163a3c0ef13577853194769504cfb39ea
[3/9] _exports.h: simplify condition for including spi functions
commit: 475a72f6471194633fd85a813547bebe846611e0
[4/9] _exports.h: drop the last dummy entries
commit: eb178c849c25024225941db79c1c3b9681a46a80
[5/9] exports.c: drop unused dummy function
commit: a78941bc7ff72ab3b67197d53216306429e6a831
[6/9] exports.h: make sure declarations are in sync with the actual exports
commit: bdbaaee65c69eebe1ee47296fa2fb12a4677697c
[7/9] _exports.h: reorganize a bit
commit: 2ef6c17f7f46a908e99e24de53d2c153dc1972ab
[8/9] _exports.h: export standard memory/string handling functions
commit: d95706158203db0b2281e134e8dfbad87e3d668c
[9/9] exports.h: bump XF_VERSION
commit: 07588f4ad9b80e73726fd7ecb8e17e21785322a0Signature · 1 line
Tom