feat(poc-a): gate-a dynamic elf proof of concept (#13)
## Summary PoC-A proved that dynamically loaded ELF plugins are workable on ESP32-S3: plugins ship as signed MPB packages, are parsed by a strict single-type parser shared between host and target, and execute from PSRAM+IRAM within a measured admission budget. A 1000-cycle load/unload soak passed 18/18 trend gates with stable heaps, the capacity ladder settled at 8 coexisting plugin instances with a 3356B per-instance budget, and the fail-closed corpus executed zero attacker-controlled bytes across 14 rejection classes. Scheduler semantics (strand FIFO ordering, completion-cancel CAS, quiesce-as-noop ABI) were validated on both host (TSan suite) and target. **Gate-A verdict: PASS** — ADR-0002 moves to **Accepted** with the capacity value 8 and global-sum admission rule as PoC-derived proposals. ## Changes by area Grouped from `git log main..HEAD --oneline` (18 commits): - **Vendored loader + patches**: `4bb53f2` vendor elf_loader 1.3.3 + harness scaffold; `2f67b4f` plugin_iram loader patch with budget consistency (p1-p5 patch series in `PATCHES.md`). - **MPB toolchain**: `4793cc2` deterministic MPB manifest builder with test signing; `408df44` strict single-type MPB parser shared host/target; `796f9d2` host MPB corpus + policy CI lane. - **Plugin pipeline + probes**: `21d2842` plugin pipeline and first PSRAM execution on target; `cab9664` relocation/import allowlist matrix with probes; `c5d6c6c` C++ feature rejection matrix. - **Harness commands**: `e0e7159` generation coexistence with measured admission budget; `2a37556` thousand-cycle lifecycle soak with heap trend gate; `093a72b` on-target scheduler semantics validation (+ `eea3876` clang-format-18 sweep); `a302151` capacity ladder with signed budget; `f34dc72` fail-closed corpus with execution sentinel. - **Host prototypes**: `6198bca` strand FIFO prototype with TSan suite; `1c3ad59` completion-cancel CAS and cleanup semantics. - **Evidence + ADR/docs**: `887d0f7` Gate-A manifest and report; `1c29e8a` ADR-0002 outcome + frozen PoC values. ## QA evidence - `poc/evidence/poc-a-20260901/manifest.json` — 67-artifact hash index, every sha256 recomputed at assembly. - `poc/evidence/poc-a-20260901/GATE-A-REPORT.md` — full nine-item development-plan §4 mapping; TEST-003 10/10 clauses. - 14 verbatim per-task transcripts (`task-1` … `task-15-*.log`) + fresh build artifacts (`build/frame_poc_a.map`, size components). - Frozen in-repo references: `ALLOWLIST.md`, `REJECT-MATRIX.md`, `ADMISSION-BUDGET.md`, `soak_samples_1000.csv`, fc corpus (33 committed `.mpb` containers). - F1-F4 final-wave verdicts: **APPROVE**. - Key numbers: soak **18/18** trend gates over 1000 cycles; capacity **8/8** instances, **3356B** per-instance; fc corpus **14 rejection classes, zero attacker bytes executed**; **4 upstream loader defects found and patched** (p1-p5). ## Requirement IDs (GOV-006) ADR-0002; TEST-002, TEST-003; PLUG-001, PLUG-004, PLUG-008, PLUG-009; MEM-001, MEM-003, MEM-007, MEM-008; LIFE-001, LIFE-002, LIFE-005, LIFE-007; SEC-002, SEC-003, SEC-004, SEC-005; GH-003; GOV-004, GOV-005. ## Risks / known limitations From `GATE-A-REPORT.md` §(f) plus the trade-off table: 1. **Capacity descent path 7-5 unexercised** — rung 8 passed first attempt; rungs 7/6/5 and retry-once logic never ran on hardware (untested against real OOM). 2. **No Wi-Fi in the harness (MEM-001 caveat)** — all PSRAM/IRAM budget numbers are radio-off harness-class; product admission budgets must be re-measured with the network stack active. 3. **On-target scheduler vectors are a subset of the host suite** (256-node pool / 2e4 storm posts vs host 31000 / 1e5); consistency declaration covers only the vectors actually run. 4. **Quiesce is a no-op by ABI semantics** — plugin ABI 1.2 has no quiesce slot; NULL entry is defined as trivial success; real quiesce coordination is later-gate work. 5. **MEMPROT=n trade-off** — `CONFIG_ESP_SYSTEM_MEMPROT=n` is required for the IDF exec heap (`MALLOC_CAP_EXEC`) that `.plugin_iram` needs; software-only PMP config, no eFuse burned (documented in `PATCHES.md` p5); production must re-decide security vs plugin IRAM. 6. **Owner sign-off PENDING** — Gate-A PASS is the PoC team's verdict; formal acceptance of ADR-0002 awaits the owner. ## Not in scope - Product components (GOV-004) — this PR is PoC evidence only; no product code paths change. - **Merge is the owner's call** — do not merge without owner sign-off on ADR-0002. --------- Co-authored-by: ania-lain <[email protected]>
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <assert.h>
|
||||
#include <sys/errno.h>
|
||||
#include "esp_elf.h"
|
||||
#include "esp_log.h"
|
||||
#include "private/elf_platform.h"
|
||||
|
||||
/** @brief RISC-V relocations defined by the ABIs */
|
||||
|
||||
#define R_RISCV_NONE 0
|
||||
#define R_RISCV_32 1
|
||||
#define R_RISCV_64 2
|
||||
#define R_RISCV_RELATIVE 3
|
||||
#define R_RISCV_COPY 4
|
||||
#define R_RISCV_JUMP_SLOT 5
|
||||
#define R_RISCV_TLS_DTPMOD32 6
|
||||
#define R_RISCV_TLS_DTPMOD64 7
|
||||
#define R_RISCV_TLS_DTPREL32 8
|
||||
#define R_RISCV_TLS_DTPREL64 9
|
||||
#define R_RISCV_TLS_TPREL32 10
|
||||
#define R_RISCV_TLS_TPREL64 11
|
||||
#define R_RISCV_TLS_DESC 12
|
||||
#define R_RISCV_BRANCH 16
|
||||
#define R_RISCV_JAL 17
|
||||
#define R_RISCV_CALL 18
|
||||
#define R_RISCV_CALL_PLT 19
|
||||
#define R_RISCV_GOT_HI20 20
|
||||
#define R_RISCV_TLS_GOT_HI20 21
|
||||
#define R_RISCV_TLS_GD_HI20 22
|
||||
#define R_RISCV_PCREL_HI20 23
|
||||
#define R_RISCV_PCREL_LO12_I 24
|
||||
#define R_RISCV_PCREL_LO12_S 25
|
||||
#define R_RISCV_HI20 26
|
||||
#define R_RISCV_LO12_I 27
|
||||
#define R_RISCV_LO12_S 28
|
||||
#define R_RISCV_TPREL_HI20 29
|
||||
#define R_RISCV_TPREL_LO12_I 30
|
||||
#define R_RISCV_TPREL_LO12_S 31
|
||||
#define R_RISCV_TPREL_ADD 32
|
||||
#define R_RISCV_ADD8 33
|
||||
#define R_RISCV_ADD16 34
|
||||
#define R_RISCV_ADD32 35
|
||||
#define R_RISCV_ADD64 36
|
||||
#define R_RISCV_SUB8 37
|
||||
#define R_RISCV_SUB16 38
|
||||
#define R_RISCV_SUB32 39
|
||||
#define R_RISCV_SUB64 40
|
||||
#define R_RISCV_GNU_VTINHERIT 41
|
||||
#define R_RISCV_GNU_VTENTRY 42
|
||||
#define R_RISCV_ALIGN 43
|
||||
#define R_RISCV_RVC_BRANCH 44
|
||||
#define R_RISCV_RVC_JUMP 45
|
||||
#define R_RISCV_RVC_LUI 46
|
||||
#define R_RISCV_RELAX 51
|
||||
#define R_RISCV_SUB6 52
|
||||
#define R_RISCV_SET6 53
|
||||
#define R_RISCV_SET8 54
|
||||
#define R_RISCV_SET16 55
|
||||
#define R_RISCV_SET32 56
|
||||
#define R_RISCV_32_PCREL 57
|
||||
#define R_RISCV_IRELATIVE 58
|
||||
#define R_RISCV_PLT32 59
|
||||
|
||||
static const char *TAG = "elf_arch";
|
||||
|
||||
/**
|
||||
* @brief Relocates target architecture symbol of ELF
|
||||
*
|
||||
* @param elf - ELF object pointer
|
||||
* @param rela - Relocated symbol data
|
||||
* @param sym - ELF symbol table
|
||||
* @param addr - Jumping target address
|
||||
*
|
||||
* @return ESP_OK if success or other if failed.
|
||||
*/
|
||||
int esp_elf_arch_relocate(esp_elf_t *elf, const elf32_rela_t *rela,
|
||||
const elf32_sym_t *sym, uint32_t addr)
|
||||
{
|
||||
uint32_t *where;
|
||||
|
||||
assert(elf && rela);
|
||||
|
||||
where = (uint32_t *)((uint8_t *)elf->psegment + rela->offset + elf->svaddr);
|
||||
ESP_LOGD(TAG, "type: %d, where=%p addr=0x%x offset=0x%x",
|
||||
ELF_R_TYPE(rela->info), where, (int)elf->psegment, (int)rela->offset);
|
||||
|
||||
/* Do relocation based on relocation type */
|
||||
|
||||
switch (ELF_R_TYPE(rela->info)) {
|
||||
case R_RISCV_NONE:
|
||||
break;
|
||||
case R_RISCV_32:
|
||||
*where = addr + rela->addend;
|
||||
break;
|
||||
case R_RISCV_RELATIVE:
|
||||
*where = (Elf32_Addr)((uint8_t *)elf->psegment - elf->svaddr + rela->addend);
|
||||
break;
|
||||
case R_RISCV_JUMP_SLOT:
|
||||
*where = addr;
|
||||
break;
|
||||
default:
|
||||
ESP_LOGE(TAG, "info=%d is not supported\n", ELF_R_TYPE(rela->info));
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <assert.h>
|
||||
#include <sys/errno.h>
|
||||
#include "esp_elf.h"
|
||||
#include "esp_log.h"
|
||||
#include "private/elf_platform.h"
|
||||
|
||||
/** @brief Xtensa relocations defined by the ABIs */
|
||||
|
||||
#define R_XTENSA_NONE 0
|
||||
#define R_XTENSA_32 1
|
||||
#define R_XTENSA_RTLD 2
|
||||
#define R_XTENSA_GLOB_DAT 3
|
||||
#define R_XTENSA_JMP_SLOT 4
|
||||
#define R_XTENSA_RELATIVE 5
|
||||
#define R_XTENSA_PLT 6
|
||||
#define R_XTENSA_OP0 8
|
||||
#define R_XTENSA_OP1 9
|
||||
#define R_XTENSA_OP2 10
|
||||
#define R_XTENSA_ASM_EXPAND 11
|
||||
#define R_XTENSA_ASM_SIMPLIFY 12
|
||||
#define R_XTENSA_GNU_VTINHERIT 15
|
||||
#define R_XTENSA_GNU_VTENTRY 16
|
||||
#define R_XTENSA_DIFF8 17
|
||||
#define R_XTENSA_DIFF16 18
|
||||
#define R_XTENSA_DIFF32 19
|
||||
#define R_XTENSA_SLOT0_OP 20
|
||||
#define R_XTENSA_SLOT1_OP 21
|
||||
#define R_XTENSA_SLOT2_OP 22
|
||||
#define R_XTENSA_SLOT3_OP 23
|
||||
#define R_XTENSA_SLOT4_OP 24
|
||||
#define R_XTENSA_SLOT5_OP 25
|
||||
#define R_XTENSA_SLOT6_OP 26
|
||||
#define R_XTENSA_SLOT7_OP 27
|
||||
#define R_XTENSA_SLOT8_OP 28
|
||||
#define R_XTENSA_SLOT9_OP 29
|
||||
#define R_XTENSA_SLOT10_OP 30
|
||||
#define R_XTENSA_SLOT11_OP 31
|
||||
#define R_XTENSA_SLOT12_OP 32
|
||||
#define R_XTENSA_SLOT13_OP 33
|
||||
#define R_XTENSA_SLOT14_OP 34
|
||||
#define R_XTENSA_SLOT0_ALT 35
|
||||
#define R_XTENSA_SLOT1_ALT 36
|
||||
#define R_XTENSA_SLOT2_ALT 37
|
||||
#define R_XTENSA_SLOT3_ALT 38
|
||||
#define R_XTENSA_SLOT4_ALT 39
|
||||
#define R_XTENSA_SLOT5_ALT 40
|
||||
#define R_XTENSA_SLOT6_ALT 41
|
||||
#define R_XTENSA_SLOT7_ALT 42
|
||||
#define R_XTENSA_SLOT8_ALT 43
|
||||
#define R_XTENSA_SLOT9_ALT 44
|
||||
#define R_XTENSA_SLOT10_ALT 45
|
||||
#define R_XTENSA_SLOT11_ALT 46
|
||||
#define R_XTENSA_SLOT12_ALT 47
|
||||
#define R_XTENSA_SLOT13_ALT 48
|
||||
#define R_XTENSA_SLOT14_ALT 49
|
||||
|
||||
static const char *TAG = "elf_arch";
|
||||
|
||||
/**
|
||||
* @brief Relocates target architecture symbol of ELF
|
||||
*
|
||||
* @param elf - ELF object pointer
|
||||
* @param rela - Relocated symbol data
|
||||
* @param sym - ELF symbol table
|
||||
* @param addr - Jumping target address
|
||||
*
|
||||
* @return ESP_OK if success or other if failed.
|
||||
*/
|
||||
int esp_elf_arch_relocate(esp_elf_t *elf, const elf32_rela_t *rela,
|
||||
const elf32_sym_t *sym, uint32_t addr)
|
||||
{
|
||||
uint32_t val;
|
||||
uint32_t *where;
|
||||
|
||||
assert(elf && rela);
|
||||
|
||||
where = (uint32_t *)esp_elf_map_sym(elf, rela->offset);
|
||||
|
||||
ESP_LOGD(TAG, "type: %d, where=%p addr=0x%x offset=0x%x\n",
|
||||
ELF_R_TYPE(rela->info), where, (int)addr, (int)rela->offset);
|
||||
|
||||
switch (ELF_R_TYPE(rela->info)) {
|
||||
case R_XTENSA_RELATIVE:
|
||||
val = esp_elf_map_sym(elf, *where);
|
||||
#ifdef CONFIG_ELF_LOADER_CACHE_OFFSET
|
||||
*where = elf_remap_text(elf, val);
|
||||
#else
|
||||
*where = val;
|
||||
#endif
|
||||
break;
|
||||
case R_XTENSA_RTLD:
|
||||
break;
|
||||
case R_XTENSA_GLOB_DAT:
|
||||
case R_XTENSA_JMP_SLOT:
|
||||
#ifdef CONFIG_ELF_LOADER_CACHE_OFFSET
|
||||
*where = elf_remap_text(elf, addr);
|
||||
#else
|
||||
*where = addr;
|
||||
#endif
|
||||
break;
|
||||
default:
|
||||
ESP_LOGE(TAG, "info=%d is not supported", ELF_R_TYPE(rela->info));
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,238 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2026 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/semphr.h"
|
||||
|
||||
#include "esp_log.h"
|
||||
#include "esp_dlfcn.h"
|
||||
|
||||
#include "private/esp_dlmod.h"
|
||||
#include "private/elf_platform.h"
|
||||
|
||||
static const char *TAG = "DLFCN";
|
||||
|
||||
#define DL_ERROR_MSG_MAX_LEN 128
|
||||
static char g_dlerror_msg[DL_ERROR_MSG_MAX_LEN] = {0};
|
||||
static bool g_dlerror_set = false;
|
||||
|
||||
/**
|
||||
* @brief Initialize the mutex for thread-safe error state access
|
||||
* @note Uses static allocation to avoid race conditions
|
||||
*/
|
||||
static StaticSemaphore_t g_dlerror_mutex_buffer;
|
||||
static SemaphoreHandle_t g_dlerror_mutex = NULL;
|
||||
|
||||
__attribute__((constructor))
|
||||
static void dlerror_mutex_init(void)
|
||||
{
|
||||
if (g_dlerror_mutex == NULL) {
|
||||
g_dlerror_mutex = xSemaphoreCreateMutexStatic(&g_dlerror_mutex_buffer);
|
||||
if (g_dlerror_mutex == NULL) {
|
||||
ESP_LOGE(TAG, "Failed to create mutex for dlerror");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Set error message for dlerror()
|
||||
*/
|
||||
static void dlerror_set(const char *msg)
|
||||
{
|
||||
dlerror_mutex_init();
|
||||
if (g_dlerror_mutex == NULL) {
|
||||
// If mutex creation failed, still try to set error (best effort)
|
||||
if (msg) {
|
||||
strncpy(g_dlerror_msg, msg, DL_ERROR_MSG_MAX_LEN - 1);
|
||||
g_dlerror_msg[DL_ERROR_MSG_MAX_LEN - 1] = '\0';
|
||||
g_dlerror_set = true;
|
||||
} else {
|
||||
g_dlerror_set = false;
|
||||
g_dlerror_msg[0] = '\0';
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (xSemaphoreTake(g_dlerror_mutex, portMAX_DELAY) != pdTRUE) {
|
||||
ESP_LOGE(TAG, "Failed to take dlerror mutex");
|
||||
return;
|
||||
}
|
||||
|
||||
if (msg) {
|
||||
strncpy(g_dlerror_msg, msg, DL_ERROR_MSG_MAX_LEN - 1);
|
||||
g_dlerror_msg[DL_ERROR_MSG_MAX_LEN - 1] = '\0';
|
||||
g_dlerror_set = true;
|
||||
} else {
|
||||
g_dlerror_set = false;
|
||||
g_dlerror_msg[0] = '\0';
|
||||
}
|
||||
|
||||
xSemaphoreGive(g_dlerror_mutex);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Dynamic loader compatibility interface - Get error message.
|
||||
*
|
||||
* @note Thread safety: The returned pointer points to a static buffer that
|
||||
* may be overwritten by subsequent calls to dl* functions from any
|
||||
* thread. Callers should copy the string immediately if needed.
|
||||
*
|
||||
* @return Pointer to error message string, or NULL if no error occurred.
|
||||
*/
|
||||
const char *dlerror(void)
|
||||
{
|
||||
dlerror_mutex_init();
|
||||
if (g_dlerror_mutex == NULL) {
|
||||
// If mutex creation failed, still try to read error (best effort)
|
||||
if (g_dlerror_set) {
|
||||
g_dlerror_set = false;
|
||||
return g_dlerror_msg;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (xSemaphoreTake(g_dlerror_mutex, portMAX_DELAY) != pdTRUE) {
|
||||
ESP_LOGE(TAG, "Failed to take dlerror mutex");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const char *result = NULL;
|
||||
if (g_dlerror_set) {
|
||||
g_dlerror_set = false;
|
||||
result = g_dlerror_msg;
|
||||
}
|
||||
|
||||
xSemaphoreGive(g_dlerror_mutex);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Dynamic loader compatibility interface - Load a shared object.
|
||||
*
|
||||
* @param file - Filesystem path to the ELF binary
|
||||
* @param mode - Mode flags (RTLD_LAZY, RTLD_NOW, etc.)
|
||||
*
|
||||
* @return Module handle on success, NULL on failure.
|
||||
*/
|
||||
void *dlopen(const char *file, int mode)
|
||||
{
|
||||
(void)mode; // TODO: The current implementation ignores the mode flag.
|
||||
|
||||
if (!file) {
|
||||
dlerror_set("Invalid NULL file path");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char filename[FILE_NAME_MAX] = {0};
|
||||
if (!dlmod_getname(file, filename, FILE_NAME_MAX)) {
|
||||
dlerror_set("Failed to extract filename from path");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void *handle = dlmod_insert(file, filename);
|
||||
if (!handle) {
|
||||
dlerror_set("Failed to load module");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
dlerror_set(NULL); // Clear error on success
|
||||
return handle;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Dynamic loader compatibility interface - Close a module handle.
|
||||
*
|
||||
* @param handle - Module handle to close
|
||||
*
|
||||
* @return 0 on success, -1 on invalid handle.
|
||||
*/
|
||||
int dlclose(void *handle)
|
||||
{
|
||||
if (!handle) {
|
||||
dlerror_set("Invalid NULL handle");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!dlmod_validate_handle(handle)) {
|
||||
dlerror_set("Invalid module handle");
|
||||
return -1;
|
||||
}
|
||||
|
||||
int ret = dlmod_remove(handle);
|
||||
if (ret != 0) {
|
||||
dlerror_set("Failed to close module");
|
||||
return -1;
|
||||
}
|
||||
|
||||
dlerror_set(NULL); // Clear error on success
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Dynamic loader compatibility interface - Look up symbol address.
|
||||
*
|
||||
* @param handle - Module handle from dlopen()
|
||||
* @param name - Symbol name to locate
|
||||
*
|
||||
* @return Memory address of the symbol if found, NULL otherwise.
|
||||
*/
|
||||
void *dlsym(void *handle, const char *name)
|
||||
{
|
||||
if (!handle) {
|
||||
dlerror_set("Invalid NULL handle");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!dlmod_validate_handle(handle)) {
|
||||
dlerror_set("Invalid module handle");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!name) {
|
||||
dlerror_set("Invalid NULL symbol name");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct dlmod_slist_t *mod = (struct dlmod_slist_t *)handle;
|
||||
|
||||
if (!mod->elf || !mod->elf->symtab) {
|
||||
dlerror_set("Module has no symbol table");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (mod->elf->num > 0) {
|
||||
for (size_t i = 0; i < mod->elf->num; i++) {
|
||||
if (mod->elf->symtab[i].name &&
|
||||
strcmp(mod->elf->symtab[i].name, name) == 0) {
|
||||
dlerror_set(NULL); // Clear error on success
|
||||
return mod->elf->symtab[i].addr;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dlerror_set("Symbol not found");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Display system module/symbol information.
|
||||
*
|
||||
* @param type - Listing type (LIST_MODULE for modules, LIST_SYMBOL for symbols)
|
||||
*/
|
||||
void dllist(dl_list_t type)
|
||||
{
|
||||
if (type == LIST_MODULE) {
|
||||
dlmod_listname();
|
||||
} else if (type == LIST_SYMBOL) {
|
||||
dlmod_listsymbol();
|
||||
} else {
|
||||
ESP_LOGW(TAG, "Unknown list type: %d", type);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,455 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2026 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/semphr.h"
|
||||
|
||||
#include "esp_elf.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_dlfcn.h"
|
||||
#include "private/esp_dlmod.h"
|
||||
#include "private/elf_platform.h"
|
||||
|
||||
static const char *TAG = "DLMOD";
|
||||
|
||||
typedef SLIST_HEAD(dlmod_slist_head, dlmod_slist_t) dlmod_slist_head_t;
|
||||
|
||||
static dlmod_slist_head_t g_dlmod_slist_head = SLIST_HEAD_INITIALIZER(g_dlmod_slist_head);
|
||||
static StaticSemaphore_t g_dlmod_mutex_buffer;
|
||||
static SemaphoreHandle_t g_dlmod_mutex = NULL;
|
||||
static portMUX_TYPE g_dlmod_mutex_init_lock = portMUX_INITIALIZER_UNLOCKED;
|
||||
|
||||
static void dlmod_mutex_init(void)
|
||||
{
|
||||
taskENTER_CRITICAL(&g_dlmod_mutex_init_lock);
|
||||
if (g_dlmod_mutex == NULL) {
|
||||
g_dlmod_mutex = xSemaphoreCreateMutexStatic(&g_dlmod_mutex_buffer);
|
||||
if (g_dlmod_mutex == NULL) {
|
||||
ESP_LOGE(TAG, "Failed to create mutex for dlmod");
|
||||
}
|
||||
}
|
||||
taskEXIT_CRITICAL(&g_dlmod_mutex_init_lock);
|
||||
}
|
||||
|
||||
bool dlmod_validate_handle(struct dlmod_slist_t *handle)
|
||||
{
|
||||
if (!handle) {
|
||||
return false;
|
||||
}
|
||||
|
||||
dlmod_mutex_init();
|
||||
if (g_dlmod_mutex == NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (xSemaphoreTake(g_dlmod_mutex, portMAX_DELAY) != pdTRUE) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool valid = false;
|
||||
struct dlmod_slist_t *current;
|
||||
SLIST_FOREACH(current, &g_dlmod_slist_head, next) {
|
||||
if (current == handle && current->magic == DLMOD_HANDLE_MAGIC) {
|
||||
valid = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
xSemaphoreGive(g_dlmod_mutex);
|
||||
return valid;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Extract the base filename from a given path, removing directory and extension.
|
||||
*
|
||||
* @param path - Full path to the file
|
||||
* @param filename - Buffer to store the extracted filename
|
||||
* @param len - Length of the filename buffer
|
||||
*
|
||||
* @return Pointer to the filename buffer on success, NULL on invalid parameters or truncation.
|
||||
*/
|
||||
char *dlmod_getname(const char *path, char *filename, size_t len)
|
||||
{
|
||||
if (!path || path[0] == '\0' || !filename || len == 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const char *filename_start = strrchr(path, '/');
|
||||
if (filename_start == NULL) {
|
||||
filename_start = path;
|
||||
} else {
|
||||
filename_start++;
|
||||
}
|
||||
|
||||
const char *extension_start = strrchr(filename_start, '.');
|
||||
size_t filename_len = (extension_start ? extension_start - filename_start : strlen(filename_start));
|
||||
if (filename_len >= len) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
strncpy(filename, filename_start, filename_len);
|
||||
filename[filename_len] = '\0';
|
||||
|
||||
if (filename[0] == '\0') {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return filename;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Retrieve a module handle by name from the global module list.
|
||||
*
|
||||
* @param name - Name of the module to find (without .so extension)
|
||||
*
|
||||
* @return Pointer to the module handle if found, NULL if not found or invalid parameters.
|
||||
*/
|
||||
void *dlmod_gethandle(const char *name)
|
||||
{
|
||||
if (!name || name[0] == '\0') {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
dlmod_mutex_init();
|
||||
if (g_dlmod_mutex == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (xSemaphoreTake(g_dlmod_mutex, portMAX_DELAY) != pdTRUE) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void *result = NULL;
|
||||
if (SLIST_EMPTY(&g_dlmod_slist_head)) {
|
||||
ESP_LOGD(TAG, "The list is empty, no module found.");
|
||||
} else {
|
||||
struct dlmod_slist_t *current;
|
||||
SLIST_FOREACH(current, &g_dlmod_slist_head, next) {
|
||||
if (strcmp(current->name, name) == 0) {
|
||||
result = current;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
xSemaphoreGive(g_dlmod_mutex);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief List all loaded module names in the system.
|
||||
*/
|
||||
void dlmod_listname(void)
|
||||
{
|
||||
dlmod_mutex_init();
|
||||
if (g_dlmod_mutex == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (xSemaphoreTake(g_dlmod_mutex, portMAX_DELAY) != pdTRUE) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (SLIST_EMPTY(&g_dlmod_slist_head)) {
|
||||
ESP_LOGI(TAG, "No shared objects loaded");
|
||||
} else {
|
||||
struct dlmod_slist_t *current;
|
||||
ESP_LOGI(TAG, "Shared objects in the list:");
|
||||
SLIST_FOREACH(current, &g_dlmod_slist_head, next) {
|
||||
ESP_LOGI(TAG, "%s.so", current->name);
|
||||
}
|
||||
}
|
||||
|
||||
xSemaphoreGive(g_dlmod_mutex);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief List all symbols from all loaded modules with their addresses.
|
||||
*/
|
||||
void dlmod_listsymbol(void)
|
||||
{
|
||||
dlmod_mutex_init();
|
||||
if (g_dlmod_mutex == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (xSemaphoreTake(g_dlmod_mutex, portMAX_DELAY) != pdTRUE) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (SLIST_EMPTY(&g_dlmod_slist_head)) {
|
||||
ESP_LOGI(TAG, "Symbols table is empty");
|
||||
} else {
|
||||
struct dlmod_slist_t *current;
|
||||
char *name;
|
||||
ESP_LOGI(TAG, "Shared objects symbols table in the list:");
|
||||
SLIST_FOREACH(current, &g_dlmod_slist_head, next) {
|
||||
if (current->elf && current->elf->symtab && current->elf->num) {
|
||||
for (uint16_t i = 0; i < current->elf->num; i++) {
|
||||
name = current->elf->symtab[i].name;
|
||||
ESP_LOGI(TAG, "%s, addr: %p", name ? name : "(null)",
|
||||
current->elf->symtab[i].addr);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
xSemaphoreGive(g_dlmod_mutex);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Relocate and load an ELF file from the specified path.
|
||||
*
|
||||
* @param path - Filesystem path to the ELF binary
|
||||
*
|
||||
* @return Pointer to the relocated ELF structure (esp_elf_t), NULL on relocation failure.
|
||||
*/
|
||||
esp_elf_t *dlmod_relocate(const char *path)
|
||||
{
|
||||
if (!path || path[0] == '\0') {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int ret;
|
||||
elf_file_t file;
|
||||
ret = esp_elf_open(&file, path);
|
||||
if (ret < 0) {
|
||||
ESP_LOGE(TAG, "Failed to open file %s", path);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ESP_LOGD(TAG, "Open file:%s, len=%d", path, file.size);
|
||||
|
||||
esp_elf_t *elf_dl = (esp_elf_t *)esp_elf_malloc(sizeof(esp_elf_t), false);
|
||||
if (!elf_dl) {
|
||||
ESP_LOGE(TAG, "Failed to allocate memory for ELF");
|
||||
esp_elf_close(&file);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ret = esp_elf_init(elf_dl);
|
||||
if (ret < 0) {
|
||||
ESP_LOGE(TAG, "Failed to initialize ELF file errno=%d", ret);
|
||||
esp_elf_free(elf_dl);
|
||||
esp_elf_close(&file);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ret = esp_elf_relocate(elf_dl, file.payload);
|
||||
if (ret < 0) {
|
||||
ESP_LOGE(TAG, "Failed to relocate ELF file errno=%d", ret);
|
||||
esp_elf_deinit(elf_dl);
|
||||
esp_elf_free(elf_dl);
|
||||
esp_elf_close(&file);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
esp_elf_close(&file);
|
||||
return elf_dl;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Insert a new module into the global module list.
|
||||
*
|
||||
* @param path - Filesystem path to the ELF binary
|
||||
* @param name - Module name to register (should match filename base)
|
||||
*
|
||||
* @return Pointer to the new module entry (struct dlmod_slist_t), NULL on failure
|
||||
* (existing entry or relocation error).
|
||||
*/
|
||||
struct dlmod_slist_t *dlmod_insert(const char *path, const char *name)
|
||||
{
|
||||
if (!path || path[0] == '\0' || !name || name[0] == '\0') {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
dlmod_mutex_init();
|
||||
if (g_dlmod_mutex == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Check if module already exists (with mutex protection)
|
||||
if (xSemaphoreTake(g_dlmod_mutex, portMAX_DELAY) != pdTRUE) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!SLIST_EMPTY(&g_dlmod_slist_head)) {
|
||||
struct dlmod_slist_t *current;
|
||||
SLIST_FOREACH(current, &g_dlmod_slist_head, next) {
|
||||
if (strcmp(current->name, name) == 0) {
|
||||
ESP_LOGD(TAG, "%s.so has been dynamically loaded", name);
|
||||
xSemaphoreGive(g_dlmod_mutex);
|
||||
return current;
|
||||
}
|
||||
}
|
||||
}
|
||||
xSemaphoreGive(g_dlmod_mutex);
|
||||
|
||||
// Module doesn't exist, create new one (without mutex - these are not list operations)
|
||||
struct dlmod_slist_t *new_node;
|
||||
new_node = esp_elf_malloc(sizeof(struct dlmod_slist_t), false);
|
||||
if (!new_node) {
|
||||
ESP_LOGE(TAG, "Failed to allocate memory for new node");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
new_node->magic = DLMOD_HANDLE_MAGIC;
|
||||
strncpy(new_node->name, name, FILE_NAME_MAX - 1);
|
||||
new_node->name[FILE_NAME_MAX - 1] = '\0';
|
||||
new_node->elf = dlmod_relocate(path);
|
||||
if (!new_node->elf) {
|
||||
ESP_LOGE(TAG, "Failed to relocate");
|
||||
esp_elf_free(new_node);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Insert into list (with mutex protection)
|
||||
if (xSemaphoreTake(g_dlmod_mutex, portMAX_DELAY) != pdTRUE) {
|
||||
esp_elf_deinit(new_node->elf);
|
||||
esp_elf_free(new_node->elf);
|
||||
esp_elf_free(new_node);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Re-check if module was inserted by another thread
|
||||
struct dlmod_slist_t *existing;
|
||||
SLIST_FOREACH(existing, &g_dlmod_slist_head, next) {
|
||||
if (strcmp(existing->name, name) == 0) {
|
||||
ESP_LOGD(TAG, "%s.so already loaded by another thread", name);
|
||||
xSemaphoreGive(g_dlmod_mutex);
|
||||
esp_elf_deinit(new_node->elf);
|
||||
esp_elf_free(new_node->elf);
|
||||
esp_elf_free(new_node);
|
||||
return existing;
|
||||
}
|
||||
}
|
||||
|
||||
SLIST_INSERT_HEAD(&g_dlmod_slist_head, new_node, next);
|
||||
xSemaphoreGive(g_dlmod_mutex);
|
||||
|
||||
return new_node;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Remove a module from the global module list and free resources.
|
||||
*
|
||||
* @param handle - Module handle to remove
|
||||
*
|
||||
* @return 0 on successful removal, -1 if handle not found.
|
||||
*/
|
||||
int dlmod_remove(void *handle)
|
||||
{
|
||||
if (!handle) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
dlmod_mutex_init();
|
||||
if (g_dlmod_mutex == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (xSemaphoreTake(g_dlmod_mutex, portMAX_DELAY) != pdTRUE) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Find and remove from list (with mutex protection)
|
||||
struct dlmod_slist_t *mod = (struct dlmod_slist_t *)handle;
|
||||
struct dlmod_slist_t *current;
|
||||
struct dlmod_slist_t *prev = NULL;
|
||||
int ret = -1;
|
||||
|
||||
current = SLIST_FIRST(&g_dlmod_slist_head);
|
||||
while (current != NULL) {
|
||||
if (current == handle) {
|
||||
if (current->magic != DLMOD_HANDLE_MAGIC) {
|
||||
break;
|
||||
}
|
||||
if (prev == NULL) {
|
||||
SLIST_REMOVE_HEAD(&g_dlmod_slist_head, next);
|
||||
} else {
|
||||
SLIST_REMOVE(&g_dlmod_slist_head, current, dlmod_slist_t, next);
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG, "Removed node with name: %s.so", current->name);
|
||||
ret = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
prev = current;
|
||||
current = SLIST_NEXT(current, next);
|
||||
}
|
||||
|
||||
if (ret != 0) {
|
||||
ESP_LOGD(TAG, "Node not found in the list");
|
||||
}
|
||||
|
||||
xSemaphoreGive(g_dlmod_mutex);
|
||||
|
||||
// Free resources (without mutex - not a list operation)
|
||||
if (ret == 0 && mod && mod->elf) {
|
||||
esp_elf_deinit(mod->elf);
|
||||
esp_elf_free(mod->elf);
|
||||
}
|
||||
if (ret == 0) {
|
||||
mod->magic = 0;
|
||||
esp_elf_free(mod);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Find a symbol address by name across all loaded modules.
|
||||
*
|
||||
* @param sym_name - Symbol name to search for
|
||||
*
|
||||
* @return Memory address of the symbol if found, NULL otherwise.
|
||||
*/
|
||||
void *dlmod_getaddr(const char *sym_name)
|
||||
{
|
||||
if (!sym_name || sym_name[0] == '\0') {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
dlmod_mutex_init();
|
||||
if (g_dlmod_mutex == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (xSemaphoreTake(g_dlmod_mutex, portMAX_DELAY) != pdTRUE) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void *result = NULL;
|
||||
struct dlmod_slist_t *current;
|
||||
SLIST_FOREACH(current, &g_dlmod_slist_head, next) {
|
||||
if (current->elf && current->elf->symtab && current->elf->num) {
|
||||
for (uint16_t i = 0; i < current->elf->num; i++) {
|
||||
const char *name = current->elf->symtab[i].name;
|
||||
if (name && strcmp(name, sym_name) == 0) {
|
||||
ESP_LOGD(TAG, "Found node with sym_name: %s, addr: %p",
|
||||
sym_name, current->elf->symtab[i].addr);
|
||||
result = current->elf->symtab[i].addr;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (result != NULL) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (result == NULL) {
|
||||
ESP_LOGD(TAG, "Node with elf name '%s' not found", sym_name);
|
||||
}
|
||||
|
||||
xSemaphoreGive(g_dlmod_mutex);
|
||||
return result;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#include "private/elf_symbol.h"
|
||||
|
||||
/* Extern declarations from ELF symbol table */
|
||||
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wbuiltin-declaration-mismatch"
|
||||
#pragma GCC diagnostic pop
|
||||
|
||||
/* Available ELF symbols table: g_customer_elfsyms */
|
||||
|
||||
const struct esp_elfsym g_customer_elfsyms[] = {
|
||||
ESP_ELFSYM_END
|
||||
};
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,165 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2023-2026 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <assert.h>
|
||||
#include <sys/errno.h>
|
||||
#include "esp_idf_version.h"
|
||||
#include "esp_attr.h"
|
||||
#include "esp_heap_caps.h"
|
||||
#include "soc/soc.h"
|
||||
#if CONFIG_IDF_TARGET_ESP32S31
|
||||
#include "esp32s31/rom/cache.h"
|
||||
#include "soc/cache_reg.h"
|
||||
#endif
|
||||
#include "private/elf_platform.h"
|
||||
|
||||
#ifdef CONFIG_ELF_LOADER_LOAD_PSRAM
|
||||
#ifdef CONFIG_IDF_TARGET_ESP32S3
|
||||
#define OFFSET_TEXT_VALUE (SOC_IROM_LOW - SOC_DROM_LOW)
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Allocate block of memory.
|
||||
*
|
||||
* @param n - Memory size in byte
|
||||
* @param exec - True: memory can run executable code; False: memory can R/W data
|
||||
*
|
||||
* @return Memory pointer if success or NULL if failed.
|
||||
*/
|
||||
void *esp_elf_malloc(uint32_t n, bool exec)
|
||||
{
|
||||
uint32_t caps;
|
||||
|
||||
#if CONFIG_ELF_LOADER_BUS_ADDRESS_MIRROR
|
||||
#ifdef CONFIG_ELF_LOADER_LOAD_PSRAM
|
||||
caps = MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT;
|
||||
#else
|
||||
#ifdef MALLOC_CAP_EXEC
|
||||
caps = exec ? MALLOC_CAP_EXEC : MALLOC_CAP_8BIT;
|
||||
#else
|
||||
caps = MALLOC_CAP_8BIT | MALLOC_CAP_32BIT;
|
||||
#endif
|
||||
#endif
|
||||
#else
|
||||
#ifdef CONFIG_ELF_LOADER_LOAD_PSRAM
|
||||
caps = MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT;
|
||||
#else
|
||||
caps = MALLOC_CAP_8BIT;
|
||||
#endif
|
||||
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 3, 0)
|
||||
caps |= MALLOC_CAP_CACHE_ALIGNED;
|
||||
#endif
|
||||
#endif
|
||||
|
||||
return heap_caps_malloc(n, caps);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Free block of memory.
|
||||
*
|
||||
* @param ptr - memory block pointer allocated by "esp_elf_malloc"
|
||||
*
|
||||
* @return None
|
||||
*/
|
||||
void esp_elf_free(void *ptr)
|
||||
{
|
||||
heap_caps_free(ptr);
|
||||
}
|
||||
|
||||
/* [patch p5] Internal-EXEC allocation for the .plugin_iram section copy
|
||||
* (task T7). Mirrors the adapter's own internal-exec selection used when
|
||||
* PSRAM loading is disabled (esp_elf_malloc()'s exec branch): explicit
|
||||
* EXEC|INTERNAL caps, so the section copy lands in the native IRAM
|
||||
* execution window (0x4037_0000..0x403E_0000 on ESP32-S3) instead of the
|
||||
* PSRAM data window. MALLOC_CAP_EXEC exists only when the IDF exec heap is
|
||||
* available (CONFIG_HEAP_HAS_EXEC_HEAP, i.e. CONFIG_ESP_SYSTEM_MEMPROT=n;
|
||||
* the IDF Kconfig help states EXEC allocation is impossible under memory
|
||||
* protection), EXEC cannot be combined with 8BIT/DMA, and heap_caps
|
||||
* transparently returns the IRAM-alias pointer with a stash word that
|
||||
* heap_caps_free() unconverts - so esp_elf_free() stays correct. Fail
|
||||
* closed (NULL) when the exec heap is not configured. */
|
||||
void *esp_elf_malloc_iram(uint32_t n)
|
||||
{
|
||||
#ifdef MALLOC_CAP_EXEC
|
||||
return heap_caps_malloc(n, MALLOC_CAP_EXEC | MALLOC_CAP_INTERNAL);
|
||||
#else
|
||||
ESP_LOGE("elf_adapter", "MALLOC_CAP_EXEC unavailable (enable by setting "
|
||||
"CONFIG_ESP_SYSTEM_MEMPROT=n); cannot load .plugin_iram");
|
||||
return NULL;
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Remap symbol from ".data" to ".text" section.
|
||||
*
|
||||
* @param elf - ELF object pointer
|
||||
* @param sym - ELF symbol table
|
||||
*
|
||||
* @return Remapped symbol value
|
||||
*/
|
||||
#ifdef CONFIG_ELF_LOADER_CACHE_OFFSET
|
||||
uintptr_t elf_remap_text(esp_elf_t *elf, uintptr_t sym)
|
||||
{
|
||||
uintptr_t mapped_sym;
|
||||
esp_elf_sec_t *sec = &elf->sec[ELF_SEC_TEXT];
|
||||
|
||||
if ((sym >= sec->addr) &&
|
||||
(sym < (sec->addr + sec->size))) {
|
||||
#ifdef CONFIG_ELF_LOADER_SET_MMU
|
||||
mapped_sym = sym + elf->text_off;
|
||||
#else
|
||||
mapped_sym = sym + OFFSET_TEXT_VALUE;
|
||||
#endif
|
||||
} else {
|
||||
mapped_sym = sym;
|
||||
}
|
||||
|
||||
return mapped_sym;
|
||||
}
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Flush data from cache to external RAM.
|
||||
*
|
||||
* @param None
|
||||
*
|
||||
* @return None
|
||||
*/
|
||||
#ifdef CONFIG_ELF_LOADER_LOAD_PSRAM
|
||||
void IRAM_ATTR esp_elf_arch_flush(void)
|
||||
{
|
||||
extern void spi_flash_disable_interrupts_caches_and_other_cpu(void);
|
||||
extern void spi_flash_enable_interrupts_caches_and_other_cpu(void);
|
||||
|
||||
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
|
||||
|
||||
#if CONFIG_IDF_TARGET_ESP32S31
|
||||
/* ESP32-S31: Ranged cache APIs (Cache_WriteBack_Addr/Cache_Invalidate_Addr)
|
||||
* cause intermittent Instruction access faults (MCAUSE=0x01,
|
||||
* MEPC=0x00000000) during long-term ELF execution from PSRAM, likely
|
||||
* related to unaligned addr/size (e.g. seg_size=0x136a8, cache_line=64B).
|
||||
* Use full D-writeback + I-invalidate like other targets. */
|
||||
Cache_WriteBack_All(CACHE_MAP_L1_DCACHE);
|
||||
spi_flash_disable_interrupts_caches_and_other_cpu();
|
||||
Cache_Invalidate_All(CACHE_MAP_L1_ICACHE_MASK);
|
||||
spi_flash_enable_interrupts_caches_and_other_cpu();
|
||||
REG_CLR_BIT(CACHE_L1_ICACHE_CTRL_REG, CACHE_L1_ICACHE_SHUT_IBUS1);
|
||||
#else
|
||||
extern void Cache_WriteBack_All(void);
|
||||
Cache_WriteBack_All();
|
||||
spi_flash_disable_interrupts_caches_and_other_cpu();
|
||||
spi_flash_enable_interrupts_caches_and_other_cpu();
|
||||
#endif
|
||||
#else
|
||||
void esp_spiram_writeback_cache(void);
|
||||
|
||||
esp_spiram_writeback_cache();
|
||||
spi_flash_disable_interrupts_caches_and_other_cpu();
|
||||
spi_flash_enable_interrupts_caches_and_other_cpu();
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,262 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2023-2026 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <time.h>
|
||||
#include <reent.h>
|
||||
#include <errno.h>
|
||||
#include <pthread.h>
|
||||
#include <setjmp.h>
|
||||
#include <getopt.h>
|
||||
#include <sys/socket.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#if CONFIG_LIBC_PICOLIBC
|
||||
/*
|
||||
* picolibc compatibility wrappers.
|
||||
*
|
||||
* In picolibc:
|
||||
* - __errno is not a function (errno is a _Thread_local int variable).
|
||||
* - __getreent() is a macro defined as NULL by ESP-IDF platform reent.h.
|
||||
* - _ctype_ is a macro (_ctype_b + _CTYPE_OFFSET) in ctype.h; picolibc
|
||||
* exports the underlying _ctype_b[384] table. Newlib-built ELFs resolve
|
||||
* _ctype_ and index it as (_ctype_ + 1)[c]; _CTYPE_OFFSET is 127.
|
||||
*/
|
||||
static int *s_picolibc_errno_func(void)
|
||||
{
|
||||
return &errno;
|
||||
}
|
||||
|
||||
static struct _reent *s_picolibc_getreent_func(void)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
extern const char _ctype_b[];
|
||||
#ifndef _CTYPE_OFFSET
|
||||
#define _CTYPE_OFFSET 127
|
||||
#endif
|
||||
static const char * const s_picolibc_ctype_tbl = _ctype_b + _CTYPE_OFFSET;
|
||||
#endif /* CONFIG_LIBC_PICOLIBC */
|
||||
|
||||
#include "rom/ets_sys.h"
|
||||
|
||||
#include "esp_log.h"
|
||||
#include "esp_elf.h"
|
||||
|
||||
#if CONFIG_ELF_DYNAMIC_LOAD_SHARED_OBJECT
|
||||
#include "private/esp_dlmod.h"
|
||||
#endif
|
||||
#include "private/elf_symbol.h"
|
||||
|
||||
extern int __ltdf2(double a, double b);
|
||||
extern unsigned int __fixunsdfsi(double a);
|
||||
extern int __gtdf2(double a, double b);
|
||||
extern double __floatunsidf(unsigned int i);
|
||||
extern double __divdf3(double a, double b);
|
||||
|
||||
static const char *TAG = "ELF_SYMBOL";
|
||||
|
||||
/** @brief Libc public functions symbols look-up table */
|
||||
|
||||
static const struct esp_elfsym g_esp_libc_elfsyms[] = {
|
||||
|
||||
/* string.h */
|
||||
|
||||
ESP_ELFSYM_EXPORT(strerror),
|
||||
ESP_ELFSYM_EXPORT(memset),
|
||||
ESP_ELFSYM_EXPORT(memcpy),
|
||||
ESP_ELFSYM_EXPORT(strlen),
|
||||
ESP_ELFSYM_EXPORT(strtod),
|
||||
ESP_ELFSYM_EXPORT(strrchr),
|
||||
ESP_ELFSYM_EXPORT(strchr),
|
||||
ESP_ELFSYM_EXPORT(strcmp),
|
||||
ESP_ELFSYM_EXPORT(strtol),
|
||||
ESP_ELFSYM_EXPORT(strcspn),
|
||||
ESP_ELFSYM_EXPORT(strncat),
|
||||
|
||||
/* stdio.h */
|
||||
|
||||
ESP_ELFSYM_EXPORT(puts),
|
||||
ESP_ELFSYM_EXPORT(putchar),
|
||||
ESP_ELFSYM_EXPORT(fputc),
|
||||
ESP_ELFSYM_EXPORT(fputs),
|
||||
ESP_ELFSYM_EXPORT(printf),
|
||||
ESP_ELFSYM_EXPORT(vfprintf),
|
||||
ESP_ELFSYM_EXPORT(fprintf),
|
||||
ESP_ELFSYM_EXPORT(fwrite),
|
||||
|
||||
/* unistd.h */
|
||||
|
||||
ESP_ELFSYM_EXPORT(usleep),
|
||||
ESP_ELFSYM_EXPORT(sleep),
|
||||
ESP_ELFSYM_EXPORT(exit),
|
||||
ESP_ELFSYM_EXPORT(close),
|
||||
|
||||
/* stdlib.h */
|
||||
|
||||
ESP_ELFSYM_EXPORT(malloc),
|
||||
ESP_ELFSYM_EXPORT(calloc),
|
||||
ESP_ELFSYM_EXPORT(realloc),
|
||||
ESP_ELFSYM_EXPORT(free),
|
||||
|
||||
/* time.h */
|
||||
|
||||
ESP_ELFSYM_EXPORT(clock_gettime),
|
||||
ESP_ELFSYM_EXPORT(strftime),
|
||||
|
||||
/* pthread.h */
|
||||
|
||||
ESP_ELFSYM_EXPORT(pthread_create),
|
||||
ESP_ELFSYM_EXPORT(pthread_attr_init),
|
||||
ESP_ELFSYM_EXPORT(pthread_attr_setstacksize),
|
||||
ESP_ELFSYM_EXPORT(pthread_detach),
|
||||
ESP_ELFSYM_EXPORT(pthread_join),
|
||||
ESP_ELFSYM_EXPORT(pthread_exit),
|
||||
|
||||
/* libc (newlib / picolibc) */
|
||||
#if CONFIG_LIBC_PICOLIBC
|
||||
{ "__errno", (void*)s_picolibc_errno_func },
|
||||
{ "__getreent", (void*)s_picolibc_getreent_func },
|
||||
{ "_ctype_", (void*)s_picolibc_ctype_tbl },
|
||||
#elif defined(__HAVE_LOCALE_INFO__) && __HAVE_LOCALE_INFO__
|
||||
ESP_ELFSYM_EXPORT(__errno),
|
||||
ESP_ELFSYM_EXPORT(__getreent),
|
||||
ESP_ELFSYM_EXPORT(__locale_ctype_ptr),
|
||||
#else
|
||||
ESP_ELFSYM_EXPORT(__errno),
|
||||
ESP_ELFSYM_EXPORT(__getreent),
|
||||
ESP_ELFSYM_EXPORT(_ctype_),
|
||||
#endif
|
||||
|
||||
/* math */
|
||||
|
||||
ESP_ELFSYM_EXPORT(__ltdf2),
|
||||
ESP_ELFSYM_EXPORT(__fixunsdfsi),
|
||||
ESP_ELFSYM_EXPORT(__gtdf2),
|
||||
ESP_ELFSYM_EXPORT(__floatunsidf),
|
||||
ESP_ELFSYM_EXPORT(__divdf3),
|
||||
|
||||
/* getopt.h */
|
||||
|
||||
ESP_ELFSYM_EXPORT(getopt_long),
|
||||
ESP_ELFSYM_EXPORT(optind),
|
||||
ESP_ELFSYM_EXPORT(opterr),
|
||||
ESP_ELFSYM_EXPORT(optarg),
|
||||
ESP_ELFSYM_EXPORT(optopt),
|
||||
|
||||
/* setjmp.h */
|
||||
|
||||
ESP_ELFSYM_EXPORT(longjmp),
|
||||
ESP_ELFSYM_EXPORT(setjmp),
|
||||
|
||||
ESP_ELFSYM_END
|
||||
};
|
||||
|
||||
/** @brief ESP-IDF public functions symbols look-up table */
|
||||
|
||||
static const struct esp_elfsym g_esp_espidf_elfsyms[] = {
|
||||
|
||||
/* sys/socket.h */
|
||||
|
||||
ESP_ELFSYM_EXPORT(lwip_bind),
|
||||
ESP_ELFSYM_EXPORT(lwip_setsockopt),
|
||||
ESP_ELFSYM_EXPORT(lwip_socket),
|
||||
ESP_ELFSYM_EXPORT(lwip_listen),
|
||||
ESP_ELFSYM_EXPORT(lwip_accept),
|
||||
ESP_ELFSYM_EXPORT(lwip_recv),
|
||||
ESP_ELFSYM_EXPORT(lwip_recvfrom),
|
||||
ESP_ELFSYM_EXPORT(lwip_send),
|
||||
ESP_ELFSYM_EXPORT(lwip_sendto),
|
||||
ESP_ELFSYM_EXPORT(lwip_connect),
|
||||
|
||||
/* arpa/inet.h */
|
||||
|
||||
ESP_ELFSYM_EXPORT(ipaddr_addr),
|
||||
ESP_ELFSYM_EXPORT(lwip_htons),
|
||||
ESP_ELFSYM_EXPORT(lwip_htonl),
|
||||
ESP_ELFSYM_EXPORT(ip4addr_ntoa),
|
||||
|
||||
/* ROM functions */
|
||||
|
||||
ESP_ELFSYM_EXPORT(ets_printf),
|
||||
|
||||
ESP_ELFSYM_END
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Find symbol address by name.
|
||||
*
|
||||
* @param sym_name - Symbol name
|
||||
*
|
||||
* @return Symbol address if success or 0 if failed.
|
||||
*/
|
||||
uintptr_t elf_find_sym_default(const char *sym_name)
|
||||
{
|
||||
if (!sym_name) {
|
||||
ESP_LOGE(TAG, "Invalid parameter: sym_name is NULL");
|
||||
return 0;
|
||||
}
|
||||
|
||||
esp_elf_symbol_table_t *syms;
|
||||
|
||||
#ifdef CONFIG_ELF_LOADER_LIBC_SYMBOLS
|
||||
syms = g_esp_libc_elfsyms;
|
||||
while (syms->name) {
|
||||
if (!strcmp(syms->name, sym_name)) {
|
||||
return (uintptr_t)syms->sym;
|
||||
}
|
||||
|
||||
syms++;
|
||||
}
|
||||
#else
|
||||
syms = g_esp_libc_elfsyms;
|
||||
(void)syms;
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_ELF_LOADER_ESPIDF_SYMBOLS
|
||||
syms = g_esp_espidf_elfsyms;
|
||||
while (syms->name) {
|
||||
if (!strcmp(syms->name, sym_name)) {
|
||||
return (uintptr_t)syms->sym;
|
||||
}
|
||||
|
||||
syms++;
|
||||
}
|
||||
#else
|
||||
syms = g_esp_espidf_elfsyms;
|
||||
(void)syms;
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_ELF_LOADER_CUSTOMER_SYMBOLS
|
||||
extern const struct esp_elfsym g_customer_elfsyms[];
|
||||
|
||||
syms = g_customer_elfsyms;
|
||||
while (syms->name) {
|
||||
if (!strcmp(syms->name, sym_name)) {
|
||||
return (uintptr_t)syms->sym;
|
||||
}
|
||||
|
||||
syms++;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
uintptr_t sym_addr = esp_elf_find_symbol(sym_name);
|
||||
if (sym_addr) {
|
||||
return sym_addr;
|
||||
}
|
||||
|
||||
#if CONFIG_ELF_DYNAMIC_LOAD_SHARED_OBJECT
|
||||
return (uintptr_t)dlmod_getaddr(sym_name);
|
||||
#else
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <assert.h>
|
||||
#include <sys/errno.h>
|
||||
#include "esp_idf_version.h"
|
||||
#include "esp_attr.h"
|
||||
#include "esp_heap_caps.h"
|
||||
#include "esp32s2/rom/cache.h"
|
||||
#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0, 0)
|
||||
#include "esp32s2/spiram.h"
|
||||
#endif
|
||||
#include "soc/mmu.h"
|
||||
#include "private/elf_platform.h"
|
||||
|
||||
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
|
||||
#define PSRAM_VADDR_START 0x3f800000
|
||||
#else
|
||||
#define PSRAM_VADDR_START (DRAM0_CACHE_ADDRESS_HIGH - esp_spiram_get_size())
|
||||
#endif
|
||||
|
||||
#define MMU_INVALID BIT(14)
|
||||
#define MMU_UNIT_SIZE 0x10000
|
||||
#define MMU_REG ((volatile uint32_t *)DR_REG_MMU_TABLE)
|
||||
|
||||
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 2, 0)
|
||||
#define IRAM0_ADDRESS_LOW SOC_IRAM0_ADDRESS_LOW
|
||||
#define IRAM0_ADDRESS_HIGH SOC_IRAM0_ADDRESS_HIGH
|
||||
#define MMU_ACCESS_SPIRAM SOC_MMU_ACCESS_SPIRAM
|
||||
#endif
|
||||
|
||||
#define ESP_MMU_IBUS_BASE IRAM0_ADDRESS_LOW
|
||||
#define ESP_MMU_IBUS_MAX ((IRAM0_ADDRESS_HIGH - IRAM0_ADDRESS_LOW) / \
|
||||
MMU_UNIT_SIZE)
|
||||
#define ESP_MMU_IBUS_OFF (8)
|
||||
|
||||
#define DADDR_2_SECS(v) ((v) / MMU_UNIT_SIZE)
|
||||
#define IADDR_2_SECS(v) ((v) / MMU_UNIT_SIZE)
|
||||
|
||||
#define PSRAM_OFF(v) ((v) - PSRAM_VADDR_START)
|
||||
#define PSRAM_SECS(v) DADDR_2_SECS(PSRAM_OFF((uintptr_t)(v)))
|
||||
#define PSRAM_ALIGN(v) ((uintptr_t)(v) & (~(MMU_UNIT_SIZE - 1)))
|
||||
|
||||
#define ICACHE_ADDR(s) (ESP_MMU_IBUS_BASE + (s) * MMU_UNIT_SIZE)
|
||||
|
||||
extern void spi_flash_disable_interrupts_caches_and_other_cpu(void);
|
||||
extern void spi_flash_enable_interrupts_caches_and_other_cpu(void);
|
||||
|
||||
/**
|
||||
* @brief Initialize MMU hardware remapping function.
|
||||
*
|
||||
* @param elf - ELF object pointer
|
||||
*
|
||||
* @return 0 if success or a negative value if failed.
|
||||
*/
|
||||
int IRAM_ATTR esp_elf_arch_init_mmu(esp_elf_t *elf)
|
||||
{
|
||||
int ret;
|
||||
int off;
|
||||
uint32_t ibus_secs;
|
||||
volatile uint32_t dbus_secs;
|
||||
esp_elf_sec_t *sec = &elf->sec[ELF_SEC_TEXT];
|
||||
volatile uint32_t *p = MMU_REG;
|
||||
|
||||
if (sec->size % MMU_UNIT_SIZE) {
|
||||
ibus_secs = IADDR_2_SECS(sec->size) + 1;
|
||||
} else {
|
||||
ibus_secs = IADDR_2_SECS(sec->size);
|
||||
}
|
||||
|
||||
dbus_secs = PSRAM_SECS(elf->ptext);
|
||||
|
||||
spi_flash_disable_interrupts_caches_and_other_cpu();
|
||||
|
||||
for (off = ESP_MMU_IBUS_OFF; off < ESP_MMU_IBUS_MAX; off++) {
|
||||
if (p[off] == MMU_INVALID) {
|
||||
int j;
|
||||
|
||||
for (j = 1; j < ibus_secs; j++) {
|
||||
if (p[off + j] != MMU_INVALID) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (j >= ibus_secs) {
|
||||
for (int k = 0; k < ibus_secs; k++) {
|
||||
p [off + k] = MMU_ACCESS_SPIRAM | (dbus_secs + k);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
spi_flash_enable_interrupts_caches_and_other_cpu();
|
||||
|
||||
if (off >= ESP_MMU_IBUS_MAX) {
|
||||
ret = -EIO;
|
||||
} else {
|
||||
elf->mmu_off = off;
|
||||
elf->mmu_num = ibus_secs;
|
||||
elf->text_off = ICACHE_ADDR(off) - PSRAM_ALIGN(elf->ptext);
|
||||
|
||||
ret = 0;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief De-initialize MMU hardware remapping function.
|
||||
*
|
||||
* @param elf - ELF object pointer
|
||||
*
|
||||
* @return None
|
||||
*/
|
||||
void IRAM_ATTR esp_elf_arch_deinit_mmu(esp_elf_t *elf)
|
||||
{
|
||||
volatile int num = elf->mmu_num;
|
||||
volatile int off = elf->mmu_off;
|
||||
volatile uint32_t *p = MMU_REG;
|
||||
|
||||
spi_flash_disable_interrupts_caches_and_other_cpu();
|
||||
|
||||
for (int i = 0; i < num; i++) {
|
||||
p [off + i] = MMU_INVALID;
|
||||
}
|
||||
|
||||
spi_flash_enable_interrupts_caches_and_other_cpu();
|
||||
}
|
||||
Reference in New Issue
Block a user