elf_open_binary() returns a dynamically allocated struct elf_image *. We
do not have malloc in the PBL, so for better PBL support create
elf_open_binary_into() which takes a struct elf_image * as argument.

Signed-off-by: Sascha Hauer <[email protected]>
Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
---
 common/elf.c  | 26 +++++++++++++++++++-------
 include/elf.h |  1 +
 2 files changed, 20 insertions(+), 7 deletions(-)

diff --git a/common/elf.c b/common/elf.c
index 
565b283b694773727ef77917cfd8c1d4ee83a8d1..ee5e31dc58bbb6432d82ff96d9dbeb4c6ce72e39
 100644
--- a/common/elf.c
+++ b/common/elf.c
@@ -318,6 +318,23 @@ static void elf_init_struct(struct elf_image *elf)
        elf->filename = NULL;
 }
 
+int elf_open_binary_into(struct elf_image *elf, void *buf)
+{
+       int ret;
+
+       memset(elf, 0, sizeof(*elf));
+       elf_init_struct(elf);
+
+       elf->hdr_buf = buf;
+       ret = elf_check_image(elf, buf);
+       if (ret)
+               return ret;
+
+       elf->entry = elf_hdr_e_entry(elf, elf->hdr_buf);
+
+       return 0;
+}
+
 struct elf_image *elf_open_binary(void *buf)
 {
        int ret;
@@ -327,17 +344,12 @@ struct elf_image *elf_open_binary(void *buf)
        if (!elf)
                return ERR_PTR(-ENOMEM);
 
-       elf_init_struct(elf);
-
-       elf->hdr_buf = buf;
-       ret = elf_check_image(elf, buf);
+       ret = elf_open_binary_into(elf, buf);
        if (ret) {
                free(elf);
-               return ERR_PTR(-EINVAL);
+               return ERR_PTR(ret);
        }
 
-       elf->entry = elf_hdr_e_entry(elf, elf->hdr_buf);
-
        return elf;
 }
 
diff --git a/include/elf.h b/include/elf.h
index 
5b1d75c14ce198292b052159de310ac14c3bd2d9..ea2c399cca7f70a2c8c6d239be1fa0ff0519a683
 100644
--- a/include/elf.h
+++ b/include/elf.h
@@ -410,6 +410,7 @@ static inline size_t elf_get_mem_size(struct elf_image *elf)
        return elf->high_addr - elf->low_addr;
 }
 
+int elf_open_binary_into(struct elf_image *elf, void *buf);
 struct elf_image *elf_open_binary(void *buf);
 struct elf_image *elf_open(const char *filename);
 void elf_close(struct elf_image *elf);

-- 
2.47.3


Reply via email to