[PATCH v3 09/23] elf: create elf_open_binary_into()

Ahmad Fatoum a.fatoum at pengutronix.de
Mon Jan 12 05:06:37 PST 2026


On 1/8/26 4:50 PM, Sascha Hauer wrote:
> 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.

Strictly speaking, we have the <linux/mm/decompress.h> allocator and as
this runs after decompression, the early malloc area is already
allocated too, but I don't feel strongly about it.

Cheers,
Ahmad

> 
> Co-Authored-By: Claude Sonnet 4.5 <noreply at anthropic.com>
> Reviewed-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
> Signed-off-by: Sascha Hauer <s.hauer at pengutronix.de>
> ---
>  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);
> 

-- 
Pengutronix e.K.                  |                             |
Steuerwalder Str. 21              | http://www.pengutronix.de/  |
31137 Hildesheim, Germany         | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686  | Fax:   +49-5121-206917-5555 |




More information about the barebox mailing list