|
ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
|
Bounded raw-descriptor ELF32 symbol and string streaming. More...
#include <stddef.h>#include <stdint.h>#include <string.h>#include "emu_elf.h"#include "emu_elf_source_internal.h"Go to the source code of this file.
Data Structures | |
| struct | emu_elf_section_table_t |
| Decoded section-header table geometry. More... | |
| struct | emu_elf_symbol_table_t |
| Decoded usable symbol/string table pair. More... | |
| struct | emu_elf_name_match_t |
| Incremental exact string comparison state. More... | |
| struct | emu_elf_lookup_t |
| Symbol-address lookup callback state. More... | |
Enumerations | |
| enum | emu_elf_symbol_limit_t : uint32_t { k_elf_symbol_entry_min = 16U , k_elf_string_chunk = 256U , k_elf_sht_symtab = 2U , k_elf_word_high_shift = 24U , k_elf_data_offset = 5U , k_elf_shentsize_offset = 46U , k_elf_section_count_offset = 48U } |
| Fixed bytes consumed from section and symbol entries. More... | |
Functions | |
| static RA8_INTERNAL uint16_t | internal_symbol_u16 (const uint8_t *bytes) |
| Decode one little-endian 16-bit field. | |
| static RA8_INTERNAL uint32_t | internal_symbol_u32 (const uint8_t *bytes) |
| Decode one little-endian 32-bit field without alignment assumptions. | |
| static RA8_INTERNAL bool | internal_symbol_read (const emu_elf_source_t *elf, uint64_t offset, uint8_t *bytes, size_t length) |
| Read one fixed-size source object into supplied scratch. | |
| static RA8_INTERNAL bool | internal_section_table (const emu_elf_source_t *elf, emu_elf_section_table_t *table) |
| Decode and validate section-header table geometry. | |
| static RA8_INTERNAL bool | internal_section_read (const emu_elf_source_t *elf, const emu_elf_section_table_t *sections, uint32_t index, uint8_t bytes[k_elf_shentsize_min]) |
| Read one section header from validated table geometry. | |
| static RA8_INTERNAL bool | internal_symbol_table (const emu_elf_source_t *elf, const emu_elf_section_table_t *sections, const uint8_t bytes[k_elf_shentsize_min], emu_elf_symbol_table_t *table) |
| Decode one SHT_SYMTAB and its linked string-table bounds. | |
| static RA8_INTERNAL bool | internal_symbol_walk (const emu_elf_source_t *elf, const emu_elf_symbol_table_t *table, emu_elf_symbol_fn fn, void *ctx, uint32_t *visited) |
| Walk every entry of one validated symbol table. | |
| uint32_t | elf_foreach_symbol (const emu_elf_source_t *elf, emu_elf_symbol_fn fn, void *ctx) |
| Walk every symbol in every usable SHT_SYMTAB section. | |
| bool | elf_string_foreach (const emu_elf_source_t *elf, uint64_t offset, emu_elf_string_fn fn, void *ctx) |
| Stream one NUL-terminated ELF string through bounded stack chunks. | |
| static RA8_INTERNAL bool | internal_name_chunk (const char *bytes, size_t length, void *opaque) |
| Compare one streamed source-name chunk with the expected suffix. | |
| static RA8_INTERNAL bool | internal_lookup_symbol (const emu_elf_symbol_t *symbol, void *opaque) |
| Compare one symbol name and stop on an exact hit. | |
| uint32_t | elf_sym_addr (const emu_elf_source_t *elf, const char *name, uint32_t *size_out) |
| Resolve a function symbol's entry address from the ELF .symtab. | |
Bounded raw-descriptor ELF32 symbol and string streaming.
Section headers, symbols, and names are decoded through small stack views. Consumers retain source offsets rather than pointers into a whole-file allocation.
Definition in file emu_elf_symbols.c.
| enum emu_elf_symbol_limit_t : uint32_t |
Fixed bytes consumed from section and symbol entries.
Definition at line 21 of file emu_elf_symbols.c.
| uint32_t elf_foreach_symbol | ( | const emu_elf_source_t * | elf, |
| emu_elf_symbol_fn | fn, | ||
| void * | ctx ) |
Walk every symbol in every usable SHT_SYMTAB section.
Decodes bounded entries and retains only absolute name offsets.
| [in] | elf | Open immutable ELF source. |
| [in] | fn | Callback invoked for each decoded entry. |
| [in,out] | ctx | Opaque callback context. |
fn. | 0 | The table is malformed or no symbol was delivered. |
elf remains open for the complete walk. fn is non-null. fn returns false. Definition at line 274 of file emu_elf_symbols.c.
References emu_elf_section_table_t::count, internal_section_read(), internal_section_table(), internal_symbol_table(), internal_symbol_walk(), and k_elf_shentsize_min.
Referenced by elf_sym_addr(), and prof_load().
| bool elf_string_foreach | ( | const emu_elf_source_t * | elf, |
| uint64_t | offset, | ||
| emu_elf_string_fn | fn, | ||
| void * | ctx ) |
Stream one NUL-terminated ELF string through bounded stack chunks.
Reads at most 256 bytes per step and never retains source bytes.
| [in] | elf | Open immutable ELF source. |
| [in] | offset | Absolute first string byte. |
| [in] | fn | Chunk consumer. |
| [in,out] | ctx | Opaque callback context. |
| true | A terminating NUL was found and every callback accepted. |
| false | A read failed, no NUL was found, or the callback stopped. |
elf remains open through the stream. fn is non-null. Definition at line 298 of file emu_elf_symbols.c.
References internal_symbol_read(), k_elf_string_chunk, emu_elf_source_t::length, and memchr().
Referenced by internal_lookup_symbol(), and internal_prof_name().
| uint32_t elf_sym_addr | ( | const emu_elf_source_t * | elf, |
| const char * | name, | ||
| uint32_t * | size_out ) |
Resolve a function symbol's entry address from the ELF .symtab.
Walks the ELF32 section headers for the SHT_SYMTAB table and its linked string table, then matches name and returns its st_value with the Thumb bit cleared (so it can be used as a UC_HOOK_CODE address). Used by the seam installers to shim first-party firmware APIs and by the --dump-sym / --stop-sym probes. Returns 0 if the symbol (or a symbol table) is absent.
| [in] | elf | Open immutable ELF source. |
| [in] | name | NUL-terminated symbol name to find. |
| [out] | size_out | If non-NULL, receives the symbol's st_size (0 if absent). |
| 0 | Symbol not present, or the image carries no symbol table. |
elf remains open throughout the bounded symbol walk. name is NUL-terminated. size_out (when non-NULL) holds the symbol size. Definition at line 401 of file emu_elf_symbols.c.
References emu_elf_lookup_t::address, elf_foreach_symbol(), internal_lookup_symbol(), emu_elf_source_t::length, emu_elf_lookup_t::size, and strlen().
Referenced by emu_tz_install(), emu_tz_patch_cmse(), eth_seam_hook(), fast_sd_seam_install(), internal_main_resolve_symbols(), sym_trace_install(), and usbh_seam_install().
|
static |
Compare one symbol name and stop on an exact hit.
Streams the referenced name and publishes address/size only on equality.
| [in] | symbol | Bounds-checked symbol with an absolute name offset. |
| [in,out] | opaque | emu_elf_lookup_t lookup state. |
| true | This symbol did not exactly match the requested name. |
| false | The symbol matched and the lookup is complete. |
symbol is non-null. opaque points to writable lookup state with an open source. Definition at line 387 of file emu_elf_symbols.c.
References emu_elf_lookup_t::address, emu_elf_name_match_t::compared, elf_string_foreach(), emu_elf_name_match_t::equal, internal_name_chunk(), emu_elf_lookup_t::length, emu_elf_name_match_t::length, emu_elf_lookup_t::name, emu_elf_symbol_t::name_offset, RA8_INTERNAL, emu_elf_lookup_t::size, emu_elf_symbol_t::size, emu_elf_lookup_t::source, and emu_elf_symbol_t::value.
Referenced by elf_sym_addr().
|
static |
Compare one streamed source-name chunk with the expected suffix.
Advances the match only while the complete chunk remains equal.
| [in] | bytes | Non-empty transient name bytes. |
| [in] | length | Chunk length. |
| [in,out] | opaque | emu_elf_name_match_t comparison state. |
| true | The chunk matched the expected suffix. |
| false | The chunk exceeded or differed from the expected suffix. |
bytes is non-null and spans length bytes. opaque points to writable comparison state. length. Definition at line 351 of file emu_elf_symbols.c.
References emu_elf_name_match_t::compared, emu_elf_name_match_t::equal, emu_elf_name_match_t::expected, emu_elf_name_match_t::length, memcmp(), and RA8_INTERNAL.
Referenced by internal_lookup_symbol().
|
static |
Read one section header from validated table geometry.
Computes the entry offset in 64 bits and reads only the fixed prefix.
| [in] | elf | Open source. |
| [in] | sections | Validated section table. |
| [in] | index | Entry index below sections->count. |
| [out] | bytes | Forty-byte section-header scratch. |
| true | The fixed section header was read completely. |
| false | The positioned read failed. |
index is in range. bytes spans k_elf_shentsize_min bytes. Definition at line 166 of file emu_elf_symbols.c.
References emu_elf_section_table_t::entry_size, internal_symbol_read(), k_elf_shentsize_min, emu_elf_section_table_t::offset, and RA8_INTERNAL.
Referenced by elf_foreach_symbol(), and internal_symbol_table().
|
static |
Decode and validate section-header table geometry.
Rejects wrong magic, class, byte order, and out-of-range tables.
| [in] | elf | Open source. |
| [out] | table | Receives bounded geometry. |
| true | Complete validated geometry was published. |
| false | The fixed header or table geometry is unusable. |
table is non-null. elf remains open. table. Definition at line 123 of file emu_elf_symbols.c.
References emu_elf_section_table_t::count, emu_elf_section_table_t::entry_size, internal_symbol_read(), internal_symbol_u16(), internal_symbol_u32(), k_elf_data_offset, k_elf_ehdr_size, k_elf_section_count_offset, k_elf_shentsize_min, k_elf_shentsize_offset, emu_elf_source_t::length, memcmp(), emu_elf_section_table_t::offset, and RA8_INTERNAL.
Referenced by elf_foreach_symbol().
|
static |
Read one fixed-size source object into supplied scratch.
Delegates one exact positioned read and publishes only success.
| [in] | elf | Open source. |
| [in] | offset | Source byte offset. |
| [out] | bytes | Destination scratch. |
| [in] | length | Exact object length. |
| true | Every requested byte was read. |
| false | Validation, EOF, capacity, or host I/O failed. |
bytes spans length writable bytes. elf remains open during the read. Definition at line 102 of file emu_elf_symbols.c.
References k_emu_elf_io_ok, priv_emu_elf_read(), and emu_elf_io_result_t::status.
Referenced by elf_string_foreach(), internal_section_read(), internal_section_table(), and internal_symbol_walk().
|
static |
Decode one SHT_SYMTAB and its linked string-table bounds.
Validates both source ranges and the linked section index before publication.
| [in] | elf | Open source. |
| [in] | sections | Validated section table. |
| [in] | bytes | Candidate section-header bytes. |
| [out] | table | Receives usable paired-table geometry. |
| true | A usable symbol/string pair was published. |
| false | The candidate or its linked table is unusable. |
bytes contains one exact section header. table with nonzero entry size. table untouched. Definition at line 192 of file emu_elf_symbols.c.
References emu_elf_section_table_t::count, internal_section_read(), internal_symbol_u32(), k_elf_sh_entsize_off, k_elf_sh_link_off, k_elf_sh_size_off, k_elf_shentsize_min, k_elf_sht_symtab, k_elf_symbol_entry_min, emu_elf_source_t::length, and RA8_INTERNAL.
Referenced by elf_foreach_symbol().
|
static |
Decode one little-endian 16-bit field.
Combines exact bytes so host byte order is irrelevant.
| [in] | bytes | Two-byte little-endian field. |
| uint16_t | The decoded field. |
bytes is non-null. bytes spans at least two readable bytes. Definition at line 60 of file emu_elf_symbols.c.
References RA8_INTERNAL.
Referenced by internal_section_table().
|
static |
Decode one little-endian 32-bit field without alignment assumptions.
Combines exact bytes so host byte order is irrelevant.
| [in] | bytes | Four-byte little-endian field. |
| uint32_t | The decoded field. |
bytes is non-null. bytes spans at least four readable bytes. Definition at line 78 of file emu_elf_symbols.c.
References k_elf_word_high_shift, and RA8_INTERNAL.
Referenced by internal_section_table(), internal_symbol_table(), and internal_symbol_walk().
|
static |
Walk every entry of one validated symbol table.
Reads only fixed entry prefixes and skips invalid string offsets.
| [in] | elf | Open source. |
| [in] | table | Validated paired-table geometry. |
| [in] | fn | Caller symbol consumer. |
| [in,out] | ctx | Opaque callback context. |
| [in,out] | visited | Running delivered-entry count. |
| true | Every usable entry was delivered. |
| false | A read failed or the callback stopped the walk. |
visited advances only for delivered entries. Definition at line 244 of file emu_elf_symbols.c.
References emu_elf_symbol_table_t::entry_size, internal_symbol_read(), internal_symbol_u32(), k_elf_sym_info_off, k_elf_symbol_entry_min, RA8_INTERNAL, emu_elf_symbol_table_t::string_offset, emu_elf_symbol_table_t::string_size, emu_elf_symbol_table_t::symbol_count, and emu_elf_symbol_table_t::symbol_offset.
Referenced by elf_foreach_symbol().