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.

Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
Reviewed-by: Ahmad Fatoum <[email protected]>
Signed-off-by: Sascha Hauer <[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 
cba11640e52204add6cf601335b1904bd9188be0..e2e6c821cc6a673ee08d82a82176f39770e5eb42
 100644
--- a/common/elf.c
+++ b/common/elf.c
@@ -316,6 +316,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;
@@ -325,17 +342,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 
236e38fb29887315e01a165795e1bb861f738054..d0e9ae2afd36c74fa50aa0cb3dab060167ba0326
 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