Skip to content

Commit 93ffece

Browse files
awojasinskinashif
authored andcommitted
llext: Add new methods to loader API
Introducing `llext_prepare()` and `llext_finalize()` APIs that are invoked and the beginning and the end of the `llext_load()` function. The purpose of these functions is to bring possibility of initializing loader before it is used and uninitialize or clean up when it is no longer needed. Both functions are optional. The buffer loader has been aligned to methods introduced in the patch. Signed-off-by: Adam Wojasinski <[email protected]>
1 parent b5addc8 commit 93ffece

File tree

3 files changed

+53
-10
lines changed

3 files changed

+53
-10
lines changed

include/zephyr/llext/buf_loader.h

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,19 @@ void *llext_buf_peek(struct llext_loader *ldr, size_t pos);
4747
* @param _buf Buffer containing the ELF binary
4848
* @param _buf_len Buffer length in bytes
4949
*/
50-
#define LLEXT_BUF_LOADER(_buf, _buf_len) \
51-
{ \
52-
.loader = { \
53-
.read = llext_buf_read, \
54-
.seek = llext_buf_seek, \
55-
.peek = llext_buf_peek, \
56-
}, \
57-
.buf = (_buf), \
58-
.len = (_buf_len), \
59-
.pos = 0 \
50+
#define LLEXT_BUF_LOADER(_buf, _buf_len) \
51+
{ \
52+
.loader = \
53+
{ \
54+
.prepare = NULL, \
55+
.read = llext_buf_read, \
56+
.seek = llext_buf_seek, \
57+
.peek = llext_buf_peek, \
58+
.finalize = NULL, \
59+
}, \
60+
.buf = (_buf), \
61+
.len = (_buf_len), \
62+
.pos = 0, \
6063
}
6164

6265
/**

include/zephyr/llext/loader.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ struct llext_elf_sect_map; /* defined in llext_priv.h */
4040
* extension is loaded, this object is no longer needed.
4141
*/
4242
struct llext_loader {
43+
/**
44+
* @brief Optional function to prepare the loader for loading extension.
45+
*
46+
* @param[in] ldr Loader
47+
*
48+
* @returns 0 on success, or a negative error.
49+
*/
50+
int (*prepare)(struct llext_loader *ldr);
51+
4352
/**
4453
* @brief Function to read (copy) from the loader
4554
*
@@ -79,6 +88,13 @@ struct llext_loader {
7988
*/
8089
void *(*peek)(struct llext_loader *ldr, size_t pos);
8190

91+
/**
92+
* @brief Optional function to clean after the extension has been loaded or error occurred.
93+
*
94+
* @param[in] ldr Loader
95+
*/
96+
void (*finalize)(struct llext_loader *ldr);
97+
8298
/** @cond ignore */
8399
elf_ehdr_t hdr;
84100
elf_shdr_t sects[LLEXT_MEM_COUNT];
@@ -90,6 +106,15 @@ struct llext_loader {
90106
};
91107

92108
/** @cond ignore */
109+
static inline int llext_prepare(struct llext_loader *l)
110+
{
111+
if (l->prepare) {
112+
return l->prepare(l);
113+
}
114+
115+
return 0;
116+
}
117+
93118
static inline int llext_read(struct llext_loader *l, void *buf, size_t len)
94119
{
95120
return l->read(l, buf, len);
@@ -108,6 +133,13 @@ static inline void *llext_peek(struct llext_loader *l, size_t pos)
108133

109134
return NULL;
110135
}
136+
137+
static inline void llext_finalize(struct llext_loader *l)
138+
{
139+
if (l->finalize) {
140+
l->finalize(l);
141+
}
142+
}
111143
/* @endcond */
112144

113145
/**

subsys/llext/llext_load.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,12 @@ int do_llext_load(struct llext_loader *ldr, struct llext *ext,
556556
ldr->sect_map = NULL;
557557

558558
LOG_DBG("Loading ELF data...");
559+
ret = llext_prepare(ldr);
560+
if (ret != 0) {
561+
LOG_ERR("Failed to prepare the loader, ret %d", ret);
562+
goto out;
563+
}
564+
559565
ret = llext_load_elf_data(ldr, ext);
560566
if (ret != 0) {
561567
LOG_ERR("Failed to load basic ELF data, ret %d", ret);
@@ -674,5 +680,7 @@ int do_llext_load(struct llext_loader *ldr, struct llext *ext,
674680
ext->mem[LLEXT_MEM_RODATA]);
675681
}
676682

683+
llext_finalize(ldr);
684+
677685
return ret;
678686
}

0 commit comments

Comments
 (0)