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,79 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2026 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/** @brief The MODE argument to `dlopen' contains one of the following: */
|
||||
|
||||
#define RTLD_LAZY 0x00001 /*!< Lazy function call binding. */
|
||||
#define RTLD_NOW 0x00002 /*!< Immediate function call binding. */
|
||||
#define RTLD_BINDING_MASK 0x00003 /*!< Mask of binding time value. */
|
||||
#define RTLD_NOLOAD 0x00004 /*!< Do not load the object. */
|
||||
#define RTLD_DEEPBIND 0x00008 /*!< Use deep binding. */
|
||||
|
||||
/**
|
||||
* @brief Enum for symbol table list type
|
||||
*
|
||||
*/
|
||||
typedef enum {
|
||||
LIST_MODULE,
|
||||
LIST_SYMBOL,
|
||||
} dl_list_t;
|
||||
|
||||
/**
|
||||
* @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);
|
||||
|
||||
/**
|
||||
* @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);
|
||||
|
||||
/**
|
||||
* @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);
|
||||
|
||||
/**
|
||||
* @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);
|
||||
|
||||
/**
|
||||
* @brief Display system module/symbol information.
|
||||
*
|
||||
* @param type - Listing type (LIST_MODULE for modules, LIST_SYMBOL for symbols)
|
||||
*/
|
||||
void dllist(dl_list_t type);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,181 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2023-2026 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#include "private/elf_types.h"
|
||||
#include "private/elf_symbol.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct elf_file {
|
||||
uint8_t *payload;
|
||||
size_t size;
|
||||
} elf_file_t;
|
||||
|
||||
/**
|
||||
* @brief Symbol table type
|
||||
*
|
||||
* A symbol table is an array of esp_elfsym structures terminated with ESP_ELFSYM_END.
|
||||
*/
|
||||
typedef const struct esp_elfsym esp_elf_symbol_table_t;
|
||||
|
||||
/**
|
||||
* @brief Open and load an ELF file into memory.
|
||||
*
|
||||
* @param file - Pointer to elf_file_t structure to store loaded file content
|
||||
* @param name - Filename (without path) of the ELF file to open
|
||||
*
|
||||
* @return 0 on success, -1 on failure with errno set. Error cases include:
|
||||
* - Invalid parameters
|
||||
* - Path generation failure
|
||||
* - File open/read errors
|
||||
* - Memory allocation failures
|
||||
*
|
||||
* @note The actual file path will be constructed as "FS_PATH/name"
|
||||
* @note Allocates memory for file content using esp_elf_malloc()
|
||||
*/
|
||||
int esp_elf_open(elf_file_t *file, const char *name);
|
||||
|
||||
/**
|
||||
* @brief Close ELF file and release associated resources.
|
||||
*
|
||||
* @param file - Pointer to opened elf_file_t structure
|
||||
*
|
||||
* @note Releases memory allocated by esp_elf_open() for payload data
|
||||
* @note Should be called paired with esp_elf_open() to prevent memory leaks
|
||||
* @note If file is NULL, this function does nothing (null-safe)
|
||||
*/
|
||||
void esp_elf_close(elf_file_t *file);
|
||||
|
||||
/**
|
||||
* @brief Map symbol's address of ELF to physic space.
|
||||
*
|
||||
* @param elf - ELF object pointer
|
||||
* @param sym - ELF symbol address
|
||||
*
|
||||
* @return Mapped physic address.
|
||||
*/
|
||||
uintptr_t esp_elf_map_sym(esp_elf_t *elf, uintptr_t sym);
|
||||
|
||||
/**
|
||||
* @brief Initialize ELF object.
|
||||
*
|
||||
* @param elf - ELF object pointer
|
||||
*
|
||||
* @return ESP_OK if success or other if failed.
|
||||
*/
|
||||
int esp_elf_init(esp_elf_t *elf);
|
||||
|
||||
/**
|
||||
* @brief Decode and relocate ELF data.
|
||||
*
|
||||
* @param elf - ELF object pointer
|
||||
* @param pbuf - ELF data buffer
|
||||
*
|
||||
* @return ESP_OK if success or other if failed.
|
||||
*/
|
||||
int esp_elf_relocate(esp_elf_t *elf, const uint8_t *pbuf);
|
||||
|
||||
/**
|
||||
* @brief Request running relocated ELF function.
|
||||
*
|
||||
* @param elf - ELF object pointer
|
||||
* @param opt - Request options
|
||||
* @param argc - Arguments number
|
||||
* @param argv - Arguments value array
|
||||
*
|
||||
* @return ESP_OK if success or other if failed.
|
||||
*/
|
||||
int esp_elf_request(esp_elf_t *elf, int opt, int argc, char *argv[]);
|
||||
|
||||
/**
|
||||
* @brief Deinitialize ELF object.
|
||||
*
|
||||
* @param elf - ELF object pointer
|
||||
*
|
||||
* @return None
|
||||
*/
|
||||
void esp_elf_deinit(esp_elf_t *elf);
|
||||
|
||||
/**
|
||||
* @brief Print header description information of ELF.
|
||||
*
|
||||
* @param pbuf - ELF data buffer
|
||||
*
|
||||
* @return None
|
||||
*/
|
||||
void esp_elf_print_ehdr(const uint8_t *pbuf);
|
||||
|
||||
/**
|
||||
* @brief Print program header description information of ELF.
|
||||
*
|
||||
* @param pbuf - ELF data buffer
|
||||
*
|
||||
* @return None
|
||||
*/
|
||||
void esp_elf_print_phdr(const uint8_t *pbuf);
|
||||
|
||||
/**
|
||||
* @brief Print section header description information of ELF.
|
||||
*
|
||||
* @param pbuf - ELF data buffer
|
||||
*
|
||||
* @return None
|
||||
*/
|
||||
void esp_elf_print_shdr(const uint8_t *pbuf);
|
||||
|
||||
/**
|
||||
* @brief Print section information of ELF.
|
||||
*
|
||||
* @param pbuf - ELF data buffer
|
||||
*
|
||||
* @return None
|
||||
*/
|
||||
void esp_elf_print_sec(esp_elf_t *elf);
|
||||
|
||||
/**
|
||||
* @brief Register symbol table to global symbol tables array.
|
||||
*
|
||||
* @param symbol_table - Pointer to symbol table structure (array of esp_elfsym terminated by ESP_ELFSYM_END)
|
||||
*
|
||||
* @return 0 if success, -EINVAL if symbol_table is NULL, -EEXIST if already registered, -ENOMEM if no space.
|
||||
*
|
||||
* @note This function is not thread-safe. External synchronization must be used if calling
|
||||
* this function concurrently from multiple threads.
|
||||
*/
|
||||
int esp_elf_register_symbol(esp_elf_symbol_table_t *symbol_table);
|
||||
|
||||
/**
|
||||
* @brief Unregister symbol table from global symbol tables array.
|
||||
*
|
||||
* @param symbol_table - Pointer to symbol table structure to remove
|
||||
*
|
||||
* @return 0 if success, -EINVAL if symbol_table is NULL or symbol table not found.
|
||||
*
|
||||
* @note This function is not thread-safe. External synchronization must be used if calling
|
||||
* this function concurrently from multiple threads.
|
||||
*/
|
||||
int esp_elf_unregister_symbol(esp_elf_symbol_table_t *symbol_table);
|
||||
|
||||
/**
|
||||
* @brief Find symbol address by symbol name in registered tables.
|
||||
*
|
||||
* @param sym_name - Symbol name string to search
|
||||
*
|
||||
* @return Symbol address if found, 0 if not found.
|
||||
* @note Search order is reverse registration order (latest registered first).
|
||||
*/
|
||||
uintptr_t esp_elf_find_symbol(const char *sym_name);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,110 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2023-2026 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "private/elf_types.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Notes: align_size needs to be a power of 2 */
|
||||
|
||||
#define ELF_ALIGN(_a, align_size) (((_a) + (align_size - 1)) & \
|
||||
~(align_size - 1))
|
||||
|
||||
/**
|
||||
* @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);
|
||||
|
||||
/**
|
||||
* @brief Free block of memory.
|
||||
*
|
||||
* @param ptr - memory block pointer allocated by "esp_elf_malloc"
|
||||
*
|
||||
* @return None
|
||||
*/
|
||||
void esp_elf_free(void *ptr);
|
||||
|
||||
/* [patch p5] Allocate n bytes of internal executable memory for the
|
||||
* .plugin_iram section copy (task T7). The regular esp_elf_malloc()
|
||||
* hard-routes to SPIRAM under CONFIG_ELF_LOADER_LOAD_PSRAM, which is right
|
||||
* for .text/.data but wrong for the IRAM budget; this helper keeps the
|
||||
* allocation policy in the adapter like every other allocation of the
|
||||
* component. */
|
||||
void *esp_elf_malloc_iram(uint32_t n);
|
||||
|
||||
/**
|
||||
* @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);
|
||||
|
||||
/**
|
||||
* @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);
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Flush data from cache to external RAM.
|
||||
*
|
||||
* @return None
|
||||
*/
|
||||
#ifdef CONFIG_ELF_LOADER_LOAD_PSRAM
|
||||
void esp_elf_arch_flush(void);
|
||||
#endif
|
||||
|
||||
/* [patch p5] Number of times the IRAM copy/relocation cache-sync routine
|
||||
* has run (task T7 cache-sync observability: the counter must increment
|
||||
* with every .plugin_iram load). */
|
||||
uint32_t esp_elf_iram_cache_sync_count(void);
|
||||
|
||||
/**
|
||||
* @brief Initialize MMU hardware remapping function.
|
||||
*
|
||||
* @param elf - ELF object pointer
|
||||
*
|
||||
* @return 0 if success or a negative value if failed.
|
||||
*/
|
||||
#ifdef CONFIG_ELF_LOADER_SET_MMU
|
||||
int esp_elf_arch_init_mmu(esp_elf_t *elf);
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief De-initialize MMU hardware remapping function.
|
||||
*
|
||||
* @param elf - ELF object pointer
|
||||
*
|
||||
* @return None
|
||||
*/
|
||||
#ifdef CONFIG_ELF_LOADER_SET_MMU
|
||||
void esp_elf_arch_deinit_mmu(esp_elf_t *elf);
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,128 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2023-2026 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Macro to export a symbol for ELF loading.
|
||||
*
|
||||
* @param _sym Symbol name (function or variable)
|
||||
*
|
||||
* @note The (void*) cast is necessary to convert the symbol address
|
||||
* to the generic pointer type used in esp_elfsym structure.
|
||||
* This is safe for functions and objects with static storage duration.
|
||||
*/
|
||||
#define ESP_ELFSYM_EXPORT(_sym) { #_sym, (void*)&_sym }
|
||||
|
||||
/* End of symbol table marker */
|
||||
#define ESP_ELFSYM_END { NULL, NULL }
|
||||
|
||||
/** @brief Function symbol description */
|
||||
|
||||
struct esp_elfsym {
|
||||
const char *name; /*!< Function name */
|
||||
const void *sym; /*!< Function pointer */
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Symbol resolver function type.
|
||||
*
|
||||
* Resolves a symbol name (e.g. function name) to its address.
|
||||
* Custom resolvers allow dynamic symbol lookup beyond the static
|
||||
* symbol tables configured by KConfig.
|
||||
*
|
||||
* @param sym_name Symbol name to resolve (null-terminated string)
|
||||
* @return Symbol address if success, or 0 if the symbol is not found.
|
||||
*
|
||||
* @note The resolver function should be thread-safe if used in multi-threaded contexts.
|
||||
* @note The resolver should return 0 for unknown symbols, not a random address.
|
||||
*
|
||||
* @example Example implementation:
|
||||
* ```c
|
||||
* uintptr_t my_resolver(const char *sym_name)
|
||||
* {
|
||||
* // Check custom symbol table first
|
||||
* if (strcmp(sym_name, "my_function") == 0) {
|
||||
* return (uintptr_t)my_function;
|
||||
* }
|
||||
*
|
||||
* // Fall back to default resolver
|
||||
* return elf_find_sym_default(sym_name);
|
||||
* }
|
||||
*
|
||||
* // Usage:
|
||||
* elf_set_symbol_resolver(my_resolver);
|
||||
* ```
|
||||
*/
|
||||
typedef uintptr_t (*symbol_resolver)(const char *sym_name);
|
||||
|
||||
/**
|
||||
* @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(const char *sym_name);
|
||||
|
||||
/**
|
||||
* @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);
|
||||
|
||||
/**
|
||||
* @brief Override the internal symbol resolver.
|
||||
*
|
||||
* This override allows for an arbitrary implementation, enabling:
|
||||
* - Dynamic symbol loading
|
||||
* - Custom symbol resolution strategies
|
||||
* - Symbol interception and hooking
|
||||
*
|
||||
* @param resolver The resolver function. Must not be NULL.
|
||||
* To reset to default resolver, use elf_reset_symbol_resolver().
|
||||
*
|
||||
* @note This function is not thread-safe. Ensure proper synchronization
|
||||
* when calling from multiple threads.
|
||||
* @note The resolver function pointer must remain valid during its lifetime.
|
||||
*
|
||||
* @warning Calling this function affects all subsequent symbol resolutions
|
||||
* globally. Use with caution.
|
||||
*
|
||||
* @example Example usage:
|
||||
* ```c
|
||||
* // Set custom resolver
|
||||
* elf_set_symbol_resolver(my_custom_resolver);
|
||||
*
|
||||
* // Load and use ELF file
|
||||
* esp_elf_t elf;
|
||||
* esp_elf_load(&elf, elf_data);
|
||||
*
|
||||
* // Reset to default if needed
|
||||
* elf_reset_symbol_resolver();
|
||||
* ```
|
||||
*/
|
||||
void elf_set_symbol_resolver(symbol_resolver resolver);
|
||||
|
||||
/**
|
||||
* @brief Reset the symbol resolver to the default (static tables from KConfig).
|
||||
*
|
||||
* Equivalent to elf_set_symbol_resolver(elf_find_sym_default).
|
||||
*/
|
||||
void elf_reset_symbol_resolver(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,284 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2023-2026 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <unistd.h>
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
#include "sdkconfig.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define EI_NIDENT 16 /*!< Magic number and other information length */
|
||||
|
||||
/** @brief Type of segment */
|
||||
|
||||
#define PT_NULL 0 /*!< Program header table entry unused */
|
||||
#define PT_LOAD 1 /*!< Loadable program segment */
|
||||
#define PT_DYNAMIC 2 /*!< Dynamic linking information */
|
||||
#define PT_INTERP 3 /*!< Program interpreter */
|
||||
#define PT_NOTE 4 /*!< Auxiliary information */
|
||||
#define PT_SHLIB 5 /*!< Reserved */
|
||||
#define PT_PHDR 6 /*!< Entry for header table itself */
|
||||
#define PT_TLS 7 /*!< Thread-local storage segment */
|
||||
#define PT_NUM 8 /*!< Number of defined types */
|
||||
#define PT_LOOS 0x60000000 /*!< Start of OS-specific */
|
||||
#define PT_GNU_EH_FRAME 0x6474e550 /*!< GCC .eh_frame_hdr segment */
|
||||
#define PT_GNU_STACK 0x6474e551 /*!< Indicates stack executability */
|
||||
#define PT_GNU_RELRO 0x6474e552 /*!< Read-only after relocation */
|
||||
#define PT_LOSUNW 0x6ffffffa
|
||||
#define PT_SUNWBSS 0x6ffffffa /*!< Sun Specific segment */
|
||||
#define PT_SUNWSTACK 0x6ffffffb /*!< Stack segment */
|
||||
#define PT_HISUNW 0x6fffffff
|
||||
#define PT_HIOS 0x6fffffff /*!< End of OS-specific */
|
||||
#define PT_LOPROC 0x70000000 /*!< Start of processor-specific */
|
||||
#define PT_HIPROC 0x7fffffff /*!< End of processor-specific */
|
||||
|
||||
/** @brief Section Type */
|
||||
|
||||
#define SHT_NULL 0 /*!< invalid section header */
|
||||
#define SHT_PROGBITS 1 /*!< some useful section like .text, .data .got, .plt .rodata .interp and so on */
|
||||
#define SHT_SYMTAB 2 /*!< symbol table */
|
||||
#define SHT_STRTAB 3 /*!< string table */
|
||||
#define SHT_RELA 4 /*!< relocation table */
|
||||
#define SHT_HASH 5 /*!< HASH table */
|
||||
#define SHT_SYNAMIC 6 /*!< dynamic symbol table */
|
||||
#define SHT_NOTE 7 /*!< note information */
|
||||
#define SHT_NOBITS 8 /*!< .bss */
|
||||
#define SHT_REL 9 /*!< relocation table */
|
||||
#define SHT_SHKIB 10 /*!< reserved but has unspecified semantics. */
|
||||
#define SHT_SYNSYM 11 /*!< dynamic symbol */
|
||||
#define SHT_LOPROC 0x70000000 /*!< reserved for processor-specific semantics */
|
||||
#define SHT_LOUSER 0x7fffffff /*!< lower bound of the range of indexes reserved for application programs */
|
||||
#define SHT_HIUSER 0xffffffff /*!< upper bound of the range of indexes reserved for application programs. */
|
||||
|
||||
/** @brief Section Attribute Flags */
|
||||
|
||||
#define SHF_WRITE 1 /*!< writable when task runs */
|
||||
#define SHF_ALLOC 2 /*!< allocated when task runs */
|
||||
#define SHF_EXECINSTR 4 /*!< machine code */
|
||||
#define SHF_MASKPROG 0xf0000000 /*!< reserved for processor-specific semantics */
|
||||
|
||||
/** @brief Symbol Binding */
|
||||
|
||||
#define STB_LOCAL 0 /*!< Local symbols are not visible outside the object file containing their definition. */
|
||||
#define STB_GLOBAL 1 /*!< Global symbols are visible to all object files being combined. */
|
||||
#define STB_WEAK 2 /*!< Weak symbols resemble global symbols, but their definitions have lower precedence. */
|
||||
#define STB_NUM 3 /*!< Number of defined types */
|
||||
|
||||
/** @brief Symbol Types */
|
||||
|
||||
#define STT_NOTYPE 0 /*!< symbol type is unspecified */
|
||||
#define STT_OBJECT 1 /*!< data object */
|
||||
#define STT_FUNC 2 /*!< code object */
|
||||
#define STT_SECTION 3 /*!< symbol identifies an ELF section */
|
||||
#define STT_FILE 4 /*!< symbol's name is file name */
|
||||
#define STT_COMMON 5 /*!< common data object */
|
||||
#define STT_TLS 6 /*!< thread-local data object */
|
||||
#define STT_NUM 7 /*!< defined types in generic range */
|
||||
#define STT_LOOS 10 /*!< Low OS specific range */
|
||||
#define STT_HIOS 12 /*!< High OS specific range */
|
||||
#define STT_LOPROC 13 /*!< processor specific range */
|
||||
#define STT_HIPROC 15 /*!< processor specific link range */
|
||||
|
||||
/** @brief Special Section Indices */
|
||||
|
||||
#define SHN_UNDEF 0 /*!< undefined */
|
||||
|
||||
/** @brief Section names */
|
||||
|
||||
#define ELF_BSS ".bss" /*!< uninitialized data */
|
||||
#define ELF_DATA ".data" /*!< initialized data */
|
||||
#define ELF_DEBUG ".debug" /*!< debug */
|
||||
#define ELF_DYNAMIC ".dynamic" /*!< dynamic linking information */
|
||||
#define ELF_DYNSTR ".dynstr" /*!< dynamic string table */
|
||||
#define ELF_DYNSYM ".dynsym" /*!< dynamic symbol table */
|
||||
#define ELF_FINI ".fini" /*!< termination code */
|
||||
#define ELF_GOT ".got" /*!< global offset table */
|
||||
#define ELF_HASH ".hash" /*!< symbol hash table */
|
||||
#define ELF_INIT ".init" /*!< initialization code */
|
||||
#define ELF_REL_DATA ".rel.data" /*!< relocation data */
|
||||
#define ELF_REL_FINI ".rel.fini" /*!< relocation termination code */
|
||||
#define ELF_REL_INIT ".rel.init" /*!< relocation initialization code */
|
||||
#define ELF_REL_DYN ".rel.dyn" /*!< relocaltion dynamic link info */
|
||||
#define ELF_REL_RODATA ".rel.rodata" /*!< relocation read-only data */
|
||||
#define ELF_REL_TEXT ".rel.text" /*!< relocation code */
|
||||
#define ELF_RODATA ".rodata" /*!< read-only data */
|
||||
#define ELF_SHSTRTAB ".shstrtab" /*!< section header string table */
|
||||
#define ELF_STRTAB ".strtab" /*!< string table */
|
||||
#define ELF_SYMTAB ".symtab" /*!< symbol table */
|
||||
#define ELF_TEXT ".text" /*!< code */
|
||||
#define ELF_DATA_REL_RO ".data.rel.ro" /*!< dynamic read-only data */
|
||||
#define ELF_PLT ".plt" /*!< procedure linkage table. */
|
||||
#define ELF_GOT_PLT ".got.plt" /*!< a table where resolved addresses from external functions are stored */
|
||||
|
||||
/* [patch p5] Plugin hot-function section (task T7 of poc-a-dynamic-elf,
|
||||
* requirements 4.10.3/4.10.4): plugin code the manifest budgets into
|
||||
* internal executable RAM. Parsed only on the BUS_ADDRESS_MIRROR path. */
|
||||
#define ELF_IRAM ".plugin_iram" /*!< plugin IRAM-resident code */
|
||||
|
||||
/** @brief ELF section and symbol operation */
|
||||
|
||||
#define ELF_SEC_TEXT 0
|
||||
#define ELF_SEC_BSS 1
|
||||
#define ELF_SEC_DATA 2
|
||||
#define ELF_SEC_RODATA 3
|
||||
#define ELF_SEC_DRLRO 4
|
||||
#define ELF_SEC_IRAM 5 /* [patch p5] */
|
||||
#define ELF_SECS 6
|
||||
|
||||
#define ELF_ST_BIND(_i) ((_i) >> 4)
|
||||
#define ELF_ST_TYPE(_i) ((_i) & 0xf)
|
||||
#define ELF_ST_INFO(_b, _t) (((_b)<<4) + ((_t) & 0xf))
|
||||
|
||||
#define ELF_R_SYM(_i) ((_i) >> 8)
|
||||
#define ELF_R_TYPE(_i) ((unsigned char)(_i))
|
||||
#define ELF_R_INFO(_s, _t) (((_s) << 8) + (unsigned char)(_t))
|
||||
|
||||
#define ELF_SEC_MAP(_elf, _sec, _addr) \
|
||||
((_elf)->sec[(_sec)].addr - \
|
||||
(_elf)->sec[(_sec)].v_addr + \
|
||||
(_addr))
|
||||
|
||||
typedef unsigned int Elf32_Addr;
|
||||
typedef unsigned int Elf32_Off;
|
||||
typedef unsigned int Elf32_Word;
|
||||
typedef unsigned short Elf32_Half;
|
||||
typedef int Elf32_Sword;
|
||||
|
||||
/** @brief ELF Header */
|
||||
|
||||
typedef struct elf32_hdr {
|
||||
unsigned char ident[EI_NIDENT]; /*!< ELF Identification */
|
||||
Elf32_Half type; /*!< object file type */
|
||||
Elf32_Half machine; /*!< machine */
|
||||
Elf32_Word version; /*!< object file version */
|
||||
Elf32_Addr entry; /*!< virtual entry point */
|
||||
Elf32_Off phoff; /*!< program header table offset */
|
||||
Elf32_Off shoff; /*!< section header table offset */
|
||||
Elf32_Word flags; /*!< processor-specific flags */
|
||||
Elf32_Half ehsize; /*!< ELF header size */
|
||||
Elf32_Half phentsize; /*!< program header entry size */
|
||||
Elf32_Half phnum; /*!< number of program header entries */
|
||||
Elf32_Half shentsize; /*!< section header entry size */
|
||||
Elf32_Half shnum; /*!< number of section header entries */
|
||||
Elf32_Half shstrndx; /*!< section header table's "section header string table" entry offset */
|
||||
} elf32_hdr_t;
|
||||
|
||||
/** @brief Program Header */
|
||||
|
||||
typedef struct elf32_phdr {
|
||||
Elf32_Word type; /* segment type */
|
||||
Elf32_Off offset; /* segment offset */
|
||||
Elf32_Addr vaddr; /* virtual address of segment */
|
||||
Elf32_Addr paddr; /* physical address - ignored? */
|
||||
Elf32_Word filesz; /* number of bytes in file for seg. */
|
||||
Elf32_Word memsz; /* number of bytes in mem. for seg. */
|
||||
Elf32_Word flags; /* flags */
|
||||
Elf32_Word align; /* memory alignment */
|
||||
} elf32_phdr_t;
|
||||
|
||||
/** @brief Section Header */
|
||||
|
||||
typedef struct elf32_shdr {
|
||||
Elf32_Word name; /*!< section index and it is offset in section .shstrtab */
|
||||
Elf32_Word type; /*!< type */
|
||||
Elf32_Word flags; /*!< flags */
|
||||
Elf32_Addr addr; /*!< start address when map to task space */
|
||||
Elf32_Off offset; /*!< offset address from file start address */
|
||||
Elf32_Word size; /*!< size */
|
||||
Elf32_Word link; /*!< link to another section */
|
||||
Elf32_Word info; /*!< additional section information */
|
||||
Elf32_Word addralign; /*!< address align */
|
||||
Elf32_Word entsize; /*!< index table size */
|
||||
} elf32_shdr_t;
|
||||
|
||||
/** @brief Symbol Table Entry */
|
||||
|
||||
typedef struct elf32_sym {
|
||||
Elf32_Word name; /*!< name - index into string table */
|
||||
Elf32_Addr value; /*!< symbol value */
|
||||
Elf32_Word size; /*!< symbol size */
|
||||
unsigned char info; /*!< type and binding */
|
||||
unsigned char other; /*!< 0 - no defined meaning */
|
||||
Elf32_Half shndx; /*!< section header index */
|
||||
} elf32_sym_t;
|
||||
|
||||
/** @brief Relocation entry with implicit addend */
|
||||
|
||||
typedef struct elf32_rel {
|
||||
Elf32_Addr offset; /*!< offset of relocation */
|
||||
Elf32_Word info; /*!< symbol table index and type */
|
||||
} elf32_rel_t;
|
||||
|
||||
/** @brief Relocation entry with explicit addend */
|
||||
|
||||
typedef struct elf32_rela {
|
||||
Elf32_Addr offset; /*!< offset of relocation */
|
||||
Elf32_Word info; /*!< symbol table index and type */
|
||||
Elf32_Sword addend; /*!< Added information */
|
||||
} elf32_rela_t;
|
||||
|
||||
/** @brief ELF section object */
|
||||
|
||||
typedef struct esp_elf_sec {
|
||||
uintptr_t v_addr; /*!< symbol virtual address */
|
||||
off_t offset; /*!< offset in ELF */
|
||||
|
||||
uintptr_t addr; /*!< section physic address in memory */
|
||||
size_t size; /*!< section size */
|
||||
} esp_elf_sec_t;
|
||||
|
||||
typedef struct esp_symtab {
|
||||
void *addr; /*!< symbol address */
|
||||
char *name; /*!< symbol name */
|
||||
} esp_symtab_t;
|
||||
|
||||
/** @brief ELF object */
|
||||
|
||||
typedef struct esp_elf {
|
||||
#ifdef CONFIG_ELF_LOADER_BUS_ADDRESS_MIRROR
|
||||
unsigned char *ptext; /*!< instruction buffer pointer */
|
||||
unsigned char *pdata; /*!< data buffer pointer */
|
||||
unsigned char *piram; /*!< [patch p5] .plugin_iram buffer (internal EXEC) */
|
||||
#else
|
||||
unsigned char *psegment; /*!< segment buffer pointer */
|
||||
uint32_t svaddr; /*!< start virtual address of segment */
|
||||
#endif
|
||||
|
||||
esp_elf_sec_t sec[ELF_SECS]; /*!< ".bss", "data", "rodata", ".text" */
|
||||
|
||||
int (*entry)(int argc, char *argv[]); /*!< Entry pointer of ELF */
|
||||
|
||||
/* [patch p5] Manifest-declared .plugin_iram budget (PLUG-009 / MEM
|
||||
* 4.2.4): the caller copies iram_required_bytes from the verified mpb
|
||||
* manifest view into this field between esp_elf_init() and
|
||||
* esp_elf_relocate(); the load aborts with -EINVAL before any
|
||||
* allocation unless it equals the section's measured sh_size. Zero
|
||||
* (the esp_elf_init() reset) is the correct value for images without
|
||||
* a .plugin_iram section. */
|
||||
uint32_t iram_required_bytes;
|
||||
|
||||
#ifdef CONFIG_ELF_LOADER_SET_MMU
|
||||
uint32_t text_off; /*!< .text symbol offset */
|
||||
|
||||
uint32_t mmu_off; /*!< MMU unit offset */
|
||||
uint32_t mmu_num; /*!< MMU unit total number */
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_ELF_DYNAMIC_LOAD_SHARED_OBJECT
|
||||
uint16_t num; /*!< number of symbols in the dynamic object */
|
||||
esp_symtab_t *symtab; /*!< symbol table of dynamic object pointer */
|
||||
#endif
|
||||
} esp_elf_t;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,112 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2026 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/queue.h>
|
||||
|
||||
#include "private/elf_types.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define FILE_NAME_MAX 64
|
||||
#define DLMOD_HANDLE_MAGIC 0x444C4D4FUL
|
||||
|
||||
/**
|
||||
* @brief Module list entry used as a module handle.
|
||||
*/
|
||||
struct dlmod_slist_t {
|
||||
uint32_t magic;
|
||||
char name[FILE_NAME_MAX];
|
||||
esp_elf_t *elf;
|
||||
SLIST_ENTRY(dlmod_slist_t) next;
|
||||
};
|
||||
|
||||
/**
|
||||
* @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);
|
||||
|
||||
/**
|
||||
* @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);
|
||||
|
||||
/**
|
||||
* @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);
|
||||
|
||||
/**
|
||||
* @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);
|
||||
|
||||
/**
|
||||
* @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);
|
||||
|
||||
/**
|
||||
* @brief Validate a module handle returned by dlmod_insert() or dlmod_gethandle().
|
||||
*
|
||||
* @param handle - Module handle to validate
|
||||
*
|
||||
* @return true if the handle is a valid module entry, false otherwise.
|
||||
*/
|
||||
bool dlmod_validate_handle(struct dlmod_slist_t *handle);
|
||||
|
||||
/**
|
||||
* @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);
|
||||
|
||||
/**
|
||||
* @brief List all loaded module names in the system.
|
||||
*/
|
||||
void dlmod_listname(void);
|
||||
|
||||
/**
|
||||
* @brief List all symbols from all loaded modules with their addresses.
|
||||
*/
|
||||
void dlmod_listsymbol(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user