esp-idf linker script question
Posted: Sat May 15, 2021 6:12 pm
Hello,
I'm trying to keep an unreferenced function in the memory and later call it from another file. My files are like this:
main/linker.lf:
main/main.c:
main/fn1.c:
main/CMakeLists.txt:
When I compile my code with `idf.py build`, the `fn1` function or section `fnreg` is removed in the main.elf file. Is it possible to force the linker to keep fn1 without referencing it directly in the code?
Note that I'm using the latest esp-idf version at github.
I'm trying to keep an unreferenced function in the memory and later call it from another file. My files are like this:
main/linker.lf:
Code: Select all
[sections:fnreg]
entries:
fnreg
[scheme:fnreg]
entries:
fnreg -> dram0_data
[mapping:map]
archive: libmain.a
entries:
fn1 (fnreg);
fnreg -> dram0_data KEEP() SURROUND(fnreg)
Code: Select all
#include "esp_log.h"
extern void (*_fnreg_start)();
extern void (*_fnreg_end)();
void call_fns() {
void (**fn)() = &_fnreg_start;
ESP_LOGI("CALL_FNS", "%x - %x", (uint32_t) &_fnreg_start, (uint32_t) &_fnreg_end);
for (; fn < &_fnreg_end; fn++) {
(*fn)();
}
}
void app_main(void) {
call_fns();
}
Code: Select all
#include "esp_log.h"
static void fn1() {
ESP_LOGI("FN1", "fn1 is called");
}
static void (*reg_func)()
__attribute__((__section__("fnreg")))
__attribute__((__used__)) = fn1;
Code: Select all
idf_component_register(SRCS "registry.c" "fn1.c"
INCLUDE_DIRS "."
LDFRAGMENTS "linker.lf")
Note that I'm using the latest esp-idf version at github.