Commit graph

60 commits

Author SHA1 Message Date
Marc Herbert 01a8202135 llext: fix llext_find_sym() not to return a "const" value
Returned values are copies, so trying to "const" return values cannot
have any effect.

Fixes the following compiler warning:
```
llext.h:165: warning: type qualifiers ignored on function return type
```

Fixes commit 41e0a4a371 ("llext: Linkable loadable extensions")

Signed-off-by: Marc Herbert <marc.herbert@intel.com>
2024-04-16 09:06:49 +02:00
Luca Burelli e24295f0f5 llext: add support for building relocatable ELF files
This commit adds support for building relocatable (partially linked)
ELF files as the binary object type for the llext subsystem.

Signed-off-by: Luca Burelli <l.burelli@arduino.cc>
2024-04-11 11:35:24 -05:00
Luca Burelli 64e7d85ef6 llext: define target binary type
Add a new Kconfig option to select the binary object type for the llext
subsystem. This will allow to fully decouple the architecture type from
the kind of binary object that is expected by the loader.

The defaults have been chosen to match the current behavior of the ARM
and Xtensa architectures, but developers can now more easily experiment
with other object types.

Signed-off-by: Luca Burelli <l.burelli@arduino.cc>
2024-04-11 11:35:24 -05:00
Cedric Lescop 7b1d9d6166 llext: Full ARM ELF relocation support
Adds support for all relocation type produced by GCC
on ARM platform using partial linking (-r flag) or
shared link (-fpic and -shared flag).

Signed-off-by: Cedric Lescop <cedric.lescop@se.com>
2024-04-10 14:13:15 -04:00
Guennadi Liakhovetski 2ccf775396 llext: add support for relocatable objects on Xtensa
Some toolchains cannot create shared objects for Xtensa, with them we
have to use relocatable objects. Add support for them to llext.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
2024-04-05 21:54:47 -05:00
Luca Burelli 4867c56a51 llext: invalidate instruction cache after loading extension
In addition to flushing the dcache, to ensure that the new code is
actually read by the CPU when loading an extension, the icache must
be invalidated as well.

Without this, some tests are failing in hardware because the CPU is
executing stale code from its instruction cache.

Fixes #70906 on arduino_giga_r1/stm32h747xx/m7.

Signed-off-by: Luca Burelli <l.burelli@arduino.cc>
2024-04-05 11:54:31 +02:00
Ederson de Souza 62b19ef65c syscalls: llext: Export z_impl symbols so they are available to kernel
commit 67bb6db3f8 ("syscall: Export all emitted syscalls, enabling
them for extensions") exports all emitted syscalls, however, it does
that only for the `z_mrsh` symbols, effectively only available for
userspace. If an extension running at kernel level tries to use a
syscall, it will fail to load.

This patch fixes that by exposing the `z_impl` symbols instead. However,
this is not as straightforward as the `z_mrsh` ones. As, in their
signatures, they can basically contain any type, it's not just a matter
of emitting `EXPORT_SYMBOL(z_impl_<syscall>)`, as the compiler will
complain about the undefined types. Here, there are a few approaches.

One of them is to have the `EXPORT_SYMBOL` being generated on the same
files where the syscall is implemented - injecting it there would allow
it to access all known symbols. But changing a lot of files is
undesirable, and it was one of the nice points of first patch.

Another one would be to reconstruct - or simply use the absolute path -
for the includes where the syscalls are defined. Reconstruct the paths
seems fragile and I'm not sure using absolute paths is portable.

Finally, the approach used in this patch is to declare, on a different
generated file, all `z_impl_` symbols as `void *` - after all, only the
address (and the name) to the function is relevant to EXPORT_SYMBOL. By
living in an compilation unit that doesn't include any header which
would expose any of the syscalls, there shouldn't be any conflicts. And
to account for the possibility that a syscall is not compiled - due
being configured out via Kconfig - all those symbols are also weak
aliases to a pointer to NULL. This file is then included in
`llext_export.c` (which should naturally not include any conflicting
header).

Signed-off-by: Ederson de Souza <ederson.desouza@intel.com>
2024-03-26 19:31:56 -04:00
Lauren Murphy 33c6bdab9a llext: add STT_OBJECT relocation
Adds STT_OBJECT relocation.

Fixes #67701.

Signed-off-by: Lauren Murphy <lauren.murphy@intel.com>
2024-03-15 08:37:13 +00:00
Bjarki Arge Andreasen 5cac834bb6 llext: arch_elf_relocate: pass opval unmodified
The opval argument of arch_elf_relocate() was modified by
adding the value stored at opaddr before passing it to
arch_elf_relocate(). This presumed that the addend would
always be stored as a raw value at opaddr, which is not the
case for all relocation types.

This PR modifies opval to be the absolute address of opval,
and moves the addition of the addend from llext_link_plt()
to the implementation of arch_elf_relocate().

Signed-off-by: Bjarki Arge Andreasen <bjarki@arge-andreasen.me>
2024-03-14 19:07:49 +00:00
Marc Herbert 7eb1d3d57d llext: elf.h: fix wrong 32 bits length of elf64_rela fields
Fixes commit a9a82d557c ("llext: use elf_rela_t instead of elf_rel_t")

Also switch sign of (unused?) `r_addend` to unsigned.

https://refspecs.linuxfoundation.org/elf/gabi4+/ch4.reloc.html

Issue found thanks to the following warnings when compiling in 64bits:

```
/__w/zephyr/zephyr/include/zephyr/llext/elf.h:349:29: error:
  right shift count >= width of type [-Werror=shift-count-overflow]
     349 | #define ELF64_R_SYM(i) ((i) >> 32)
```

The name `elf64_word` was admittedly confusing.

Signed-off-by: Marc Herbert <marc.herbert@intel.com>
2024-03-12 19:28:18 -04:00
Marc Herbert 4ad9f90638 llext.c: fix logging of 64 bits pointers
Note `elf64_addr` and friends are defined as uint64_t which is defined
as `long long unsigned` which is for some reason different from
`size_t`. So they all require a cast.

Signed-off-by: Marc Herbert <marc.herbert@intel.com>
2024-03-12 19:28:18 -04:00
Tom Burdick 84e883b611 llext: Support memory protection
Sets up memory partitions and allows for the partitions to be added to a
memory domain after loading an extension. This allows for applying
memory protection attributes to all of the needed memory regions an
extension requires to execute code correctly.

Currently only works when usermode is enabled as otherwise memory
protection APIs are unavailable.

Signed-off-by: Tom Burdick <thomas.burdick@intel.com>
2024-02-02 13:06:28 -05:00
Tom Burdick 172bc0c238 llext: Remove automatic include
clangd like to automatically add include directives, this one slipped by
and made its way in the tree. Remove it.

Signed-off-by: Tom Burdick <thomas.burdick@intel.com>
2024-02-02 13:06:28 -05:00
Luca Burelli 4d86162989 llext: merge llext_mem and llext_section enums
The only difference in the two enums are some entries related to
relocation sections. However, these entries are not used in the
code, so they can be safely removed, along with the mapping function.

Use LLEXT_MEM_* to avoid confusion with low-level "section" names.

Signed-off-by: Luca Burelli <l.burelli@arduino.cc>
2023-12-14 19:06:55 +00:00
Luca Burelli 2c7ea1dde5 llext: autoselect CACHE_MANAGEMENT on supported targets
Targets that have a data cache must enable CACHE_MANAGEMENT to allow the
llext API to flush it when loading an extension. This patch forces the
flag to be enabled when the target has a data cache.

Signed-off-by: Luca Burelli <l.burelli@arduino.cc>
2023-12-13 15:22:07 +00:00
Luca Burelli 8887402290 llext: fix: use proper global #define for cache flush
The arch_* functions are architecture-specific and may not be defined in
all targets. Use the global alias defined in the Cache API instead.

Fixes #66382.

Signed-off-by: Luca Burelli <l.burelli@arduino.cc>
2023-12-13 15:22:07 +00:00
Luca Burelli 57e1dc337a llext: shell: fix command completions
Dynamic command handlers should be fully initialized to avoid undefined
behavior. Also, fix the number of arguments required by each command,
since llext_call_fn requires both the llext and the name of the function
to be called.

Signed-off-by: Luca Burelli <l.burelli@arduino.cc>
2023-12-06 18:01:34 -05:00
Luca Burelli 1732177530 llext: flush dcache in the llext memory range
On architectures that have separate data and instruction caches, such as
the Cortex-M7, it is required to flush the reloc changes to the actual RAM
storage before trying to execute any code from the newly loaded llext.

Signed-off-by: Luca Burelli <l.burelli@arduino.cc>
2023-12-06 18:01:34 -05:00
Luca Burelli e96b713caf llext: record size of each stored section
Store the size of each section in the llext structure.

Signed-off-by: Luca Burelli <l.burelli@arduino.cc>
2023-12-06 18:01:34 -05:00
Luca Burelli cbed9fd785 llext: always initialize ext param in llext_load
It is not safe to assume that on entry to llext_load, *ext contains
either NULL or a previous reference to the same ext being loaded. For
example, the shell sample was passing an uninitialized value.

Initialize *ext from a search of the llext by name. If NULL, it is the
first instance of this llext (and on load error, it stays that way). If
not NULL, increment use count and return.

Signed-off-by: Luca Burelli <l.burelli@arduino.cc>
2023-12-06 18:01:34 -05:00
Guennadi Liakhovetski dbea13a1c7 llext: fix read-only extension image
When using the LLEXT buffer loader we now avoid copying extensions
from storage to allocated memory by pointing directly into the stored
image. We then also perform linking and relocation in that memory,
which modifies its contents. However, this is impossible if that
storage is read-only. Add a Kconfig flag to distinguish between
writable and read-only storage types. Also use that flag to decide,
whether the extension image in test_llext_simple.c should be defined
as const or not.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
2023-12-01 10:08:12 -05:00
Guennadi Liakhovetski 4e3e9a618e llext: protect the global llext list
Use an existing mutex to also protect the global llext list.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
2023-12-01 10:08:12 -05:00
Guennadi Liakhovetski b5506feed5 llext: remove redundant initialisation
a new llext object is completely initialised with zeros after
allocation, no need to additionally set members of an embedded into
it array to NULL.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
2023-12-01 10:08:12 -05:00
Guennadi Liakhovetski 7af6dea799 llext: remove llext_list()
llext_list() is an exported function that returns a pointer to the
llext internal extension list. That list should only be accessible
directly inside llext, while holding a lock. Remove the function.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
2023-12-01 10:08:12 -05:00
Guennadi Liakhovetski ee4540c46c llext: remove llext list scanning from shell.c
The llext list should be internal to llext.c, remove its scanning
from shell.c, export a function for that instead.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
2023-12-01 10:08:12 -05:00
Guennadi Liakhovetski b5ce5012e2 llext: clarify section map allocation size
Use an element size explicitly when calculating the array size and
use the calculated size for memset().

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
2023-12-01 10:08:12 -05:00
Guennadi Liakhovetski b9bdae8c07 llext: add support for exporting symbols from extensions
Extensions should be able to selectively export their global symbols.
Add a LL_EXTENSION_SYMBOL() macro for that. Change the present
.sym_tab to be a temporary symbol table of all global symbols in an
extensions, used only during linking for internal purposes. Add a new
.exp_tab symbol table to store symbols, exported by an extension
permanently.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
2023-12-01 10:08:12 -05:00
Guennadi Liakhovetski 86da7840bd llext: fix a memory leak in an error case
If a function fails it should release all the resources it has
managed to acquire. Fix llext_load() to free memory that it has
allocated in case of an error.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
2023-12-01 10:08:12 -05:00
Guennadi Liakhovetski a2acd7b2fb llext: add reference counting
Extend the llext_load() / llext_unload() API to let it be called
repeatedly for the same extension to increment or decrement its
reference counter respectively. We use a mutex to protect the counter
and make both llext_load() and llext_unload() return the use-count to
let the caller identify when the first loading and the last unloading
took place.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
2023-12-01 10:08:12 -05:00
Guennadi Liakhovetski 0b5bfd22e3 llext: add support for global functions
Loadable modules can contain global (not "static") functions, even if
they aren't exported for use by other modules, e.g. when a module is
built from multiple .c files. Such functions are then also included
in link tables and have to be re-linked.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
2023-12-01 10:08:12 -05:00
Guennadi Liakhovetski a2a62b46a3 llext: make buffer access functions accessible externally
llext_seek(), llext_read() and llext_peek() are needed outside of the
extension code too, move them to a header.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
2023-12-01 10:08:12 -05:00
Guennadi Liakhovetski b0b4b0baa0 llext: make local relocations optional
Some applications can decide to link their loadable objects for
exactly the same addresses, where they will be loaded. In those cases
local relocations aren't needed any more and can in fact break the
object if applied while the object is in a temporary storage. Add a
parameter to skip such local relocations.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
2023-12-01 10:08:12 -05:00
Guennadi Liakhovetski 69cdc32892 llext: export some symbols
Export some symbols for loadable modules. Also add an
EXPORT_SYSCALL() helper macro for exporting system calls by their
official names.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
2023-12-01 10:08:12 -05:00
Guennadi Liakhovetski f98b8bb48f llext: add a function for finding ELF sections
Applications can use custom ELF sections for their own purposes, add
a function for finding them.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
2023-12-01 10:08:12 -05:00
Guennadi Liakhovetski 03519afb84 llext: xtensa: add support for local symbol relocations
Add support for relocating local symbols, as specified in the
.rela.dyn section.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
2023-12-01 10:08:12 -05:00
Guennadi Liakhovetski d6a5a6e04c llext: add support for shared objects
Add support for linking PIC shared object, which only require
linking, using their PLT and GOT lists and don't need any relocation
otherwise.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
2023-12-01 10:08:12 -05:00
Guennadi Liakhovetski e0ea44cbfa llext: fix symbol address calculation for ET_DYN
Symbopl tables of ELF objects of type ET_REL contain offsets instead
of addresses as for ET_DYN.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
2023-11-17 09:25:42 +01:00
Guennadi Liakhovetski 21cea07b8c llext: fix a confusion between section indices
A common pattern is used throughout llext.c:

ext->mem[ldr->sect_map[sym.st_shndx]]

where ldr->sect_map[sym.st_shndx] actually contains indices from
enum llext_section but ext->mem[] is indexed, using enum llext_mem
values. Fix this by changing ldr->sect_map[] to actually contain
enum llext_mem values.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
2023-11-17 09:25:42 +01:00
Guennadi Liakhovetski eb3071ebe8 llext: check for an allocation failure
Add a missing allocation failure check in llext_export_symbols().

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
2023-11-17 09:25:42 +01:00
Guennadi Liakhovetski fb92636056 llext: remove a symbol count copy
The symbol count in struct llext_loader is redundant, we already have
one in struct llext_symtable, accessible via struct llext. Remove the
redundant copy.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
2023-11-17 09:25:42 +01:00
Guennadi Liakhovetski db43d35f61 llext: (cosmetic) remove an unused variable
op_code in llext_link() is unused, remove it.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
2023-11-17 09:25:42 +01:00
Guennadi Liakhovetski 60aef84cad llext: (cosmetic) use a local variable
Simplify llext_copy_symbols() by using a local variable to store a
pointer.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
2023-11-17 09:25:42 +01:00
Guennadi Liakhovetski fef999c06f llext: (cosmetic) remove "inline" in llext.c
Let the compiler decide which functions to inline.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
2023-11-17 09:25:42 +01:00
Guennadi Liakhovetski 6246caa643 llext: (cosmetic) rename a variable to match its role
str_cnt in llext_find_tables() is actually used to count sections,
rename it to better match that usage.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
2023-11-15 14:17:08 +00:00
Guennadi Liakhovetski 6058512b06 llext: use provided size for more generic code
Use .sh_entsize instead of sizeof(elf_rel_t) to make code suitable
for both REL and RELA types.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
2023-11-15 14:17:08 +00:00
Guennadi Liakhovetski 68b4898b7e llext: skip first dummy symbol table entry
The first entry in the symbol table is empty, skip it.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
2023-11-15 14:17:08 +00:00
Guennadi Liakhovetski a88facacaa llext: clarify the use of loop variables, simplify code
Several cosmetic changes with no change in functionality:

The pos variable in multiple functions is used as a loop variable -
it's initialised before the loop starts and then it's incremented for
each loop iteration. Move it to the loop header for readability.

"return ret" is clearer than "goto out" where the "out" label does
nothing but "out: return ret" because one sees immediately that the
function is terminated at that location with no further actions
without the need to check the "out" label.

k_heap_free(heap, NULL) is valid, no need to check the address to be
freed for NULL.

Object counters can be simple (unsigned) integers, no need to make
them size_t or elf_word.

"return 0" is simpler than "return ret" because it shows the return
value immediately without the need to check what it can be at that
location.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
2023-11-15 14:17:08 +00:00
Guennadi Liakhovetski 0bf08e5775 llext: use llext_peek() for section pointers
Try to use llext_peek() for section pointers. If it's supported and
succeeds we don't need to allocate buffers.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
2023-11-10 10:40:26 +01:00
Guennadi Liakhovetski ce4cdac3c0 llext: add llext_peek()
The only way so far to access extension images is via a memory
buffer. Since this, supposedly, will also be a rather common method,
it makes sense to add a method to access extension data quickly by
obtaining a pointer instead of copying data into local buffers. Add a
llext_peek() method for that.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
2023-11-10 10:40:26 +01:00
Guennadi Liakhovetski a9a82d557c llext: use elf_rela_t instead of elf_rel_t
elf_rela_t contains elf_rel_t exactly and contains an additional
field at the end. Therefore pointers of that type can be used for
both types, making the code generic.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
2023-11-09 18:21:27 +01:00