Skip to content

Commit

Permalink
[Zephyr] Fix sys_heap malloc build with LTO
Browse files Browse the repository at this point in the history
Fix the build of Matter samples with sys_heap malloc enabled.
The linker errors would occur because:
1. The realloc() function is not used outside the LTO-
   optimized code.
2. Thus, the LTO linker plugin would mark __wrap_realloc
   symbol as PREVAILING_DEF_IRONLY, which would enable
   the compiler to optimize away the function.
3. As a result, undefined references to realloc() could not
   be resolved by the linker.

Mark malloc/calloc/realloc/free as externally visible.

Signed-off-by: Damian Krolik <[email protected]>
  • Loading branch information
Damian-Nordic committed Oct 26, 2023
1 parent fd9be71 commit 212abc8
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions src/platform/Zephyr/SysHeapMalloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ extern "C" {
#include <cstdint>
#include <cstring>

// Construct name of the given function wrapped with the `--wrap=symbol` GCC option.
#define WRAP(f) __wrap_##f

using namespace chip;

namespace {
Expand Down Expand Up @@ -99,7 +96,7 @@ void * Calloc(size_t num, size_t size)
return nullptr;
}

void * mem = malloc(totalSize);
void * mem = Malloc(totalSize);

if (mem)
{
Expand Down Expand Up @@ -156,27 +153,37 @@ void ResetMaxStats()

extern "C" {

void * WRAP(malloc)(size_t size) __attribute((alias("_ZN4chip11DeviceLayer6Malloc6MallocEj")));
void * WRAP(calloc)(size_t num, size_t size) __attribute((alias("_ZN4chip11DeviceLayer6Malloc6CallocEjj")));
void * WRAP(realloc)(void * mem, size_t size) __attribute((alias("_ZN4chip11DeviceLayer6Malloc7ReallocEPvj")));
void WRAP(free)(void * mem) __attribute((alias("_ZN4chip11DeviceLayer6Malloc4FreeEPv")));
// Construct the name of a function wrapped with the `--wrap=symbol` GCC option.
#define WRAP(f) __wrap_##f

// Define a function as an alias of another function.
#define ALIAS(f) __attribute((alias(f)))

// Mark a function as externally visible so that it is not optimized-away even
// if LTO or whole-program optimization is enabled.
#define EXTERNALLY_VISIBLE __attribute((externally_visible))

EXTERNALLY_VISIBLE void * WRAP(malloc)(size_t size) ALIAS("_ZN4chip11DeviceLayer6Malloc6MallocEj");
EXTERNALLY_VISIBLE void * WRAP(calloc)(size_t num, size_t size) ALIAS("_ZN4chip11DeviceLayer6Malloc6CallocEjj");
EXTERNALLY_VISIBLE void * WRAP(realloc)(void * mem, size_t size) ALIAS("_ZN4chip11DeviceLayer6Malloc7ReallocEPvj");
EXTERNALLY_VISIBLE void WRAP(free)(void * mem) ALIAS("_ZN4chip11DeviceLayer6Malloc4FreeEPv");

void * WRAP(_malloc_r)(_reent *, size_t size)
EXTERNALLY_VISIBLE void * WRAP(_malloc_r)(_reent *, size_t size)
{
return WRAP(malloc)(size);
}

void * WRAP(_calloc_r)(_reent *, size_t num, size_t size)
EXTERNALLY_VISIBLE void * WRAP(_calloc_r)(_reent *, size_t num, size_t size)
{
return WRAP(calloc)(num, size);
}

void * WRAP(_realloc_r)(_reent *, void * mem, size_t size)
EXTERNALLY_VISIBLE void * WRAP(_realloc_r)(_reent *, void * mem, size_t size)
{
return WRAP(realloc)(mem, size);
}

void WRAP(_free_r)(_reent *, void * mem)
EXTERNALLY_VISIBLE void WRAP(_free_r)(_reent *, void * mem)
{
WRAP(free)(mem);
}
Expand Down

0 comments on commit 212abc8

Please sign in to comment.