[PATCH 065/112] common: add PE/COFF loader

Sascha Hauer sha at pengutronix.de
Mon Jan 8 00:37:22 PST 2024


On Wed, Jan 03, 2024 at 07:12:25PM +0100, Ahmad Fatoum wrote:
> EFI loader will need to parse and load PE executables. Add functions to
> facilitate that. The API is inspired by the already existing ELF API.
> 
> Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
> ---
>  common/Kconfig           |   3 +
>  common/Makefile          |   1 +
>  common/pe.c              | 377 +++++++++++++++++++++++++++++++
>  include/asm-generic/pe.h |  56 +++++
>  include/linux/pagemap.h  |   1 +
>  include/linux/pe.h       | 468 +++++++++++++++++++++++++++++++++++++++
>  include/pe.h             | 316 ++++++++++++++++++++++++++
>  7 files changed, 1222 insertions(+)
>  create mode 100644 common/pe.c
>  create mode 100644 include/asm-generic/pe.h
>  create mode 100644 include/linux/pe.h
>  create mode 100644 include/pe.h
> 
> +struct pe_image *pe_open(const char *filename)
> +{
> +	struct pe_image *pe;
> +	size_t size;
> +	void *bin;
> +
> +	bin = read_file(filename, &size);
> +	if (!bin)
> +		return NULL;

The static inline wrapper of this function indicates you should return
an error pointer here.

> +
> +	pe = pe_open_buf(bin, size);
> +
> +	//free(bin); // FIXME

Isn't this only a matter of adding free(pe->bin) to pe_close() and
freeing it here in case pe_open_buf() fails?


> diff --git a/include/asm-generic/pe.h b/include/asm-generic/pe.h
> new file mode 100644
> index 000000000000..a1df7471348e
> --- /dev/null
> +++ b/include/asm-generic/pe.h
> @@ -0,0 +1,56 @@
> +/* SPDX-License-Identifier: GPL-2.0+ */
> +/*
> + *  Portable Executable and Common Object Constants
> + *
> + *  Copyright (c) 2018 Heinrich Schuchardt
> + *
> + *  based on the "Microsoft Portable Executable and Common Object File Format
> + *  Specification", revision 11, 2017-01-23
> + */
> +
> +#ifndef _ASM_PE_H
> +#define _ASM_PE_H
> +
> +/* Characteristics */
> +#define IMAGE_FILE_RELOCS_STRIPPED		0x0001
> +#define IMAGE_FILE_EXECUTABLE_IMAGE		0x0002
> +#define IMAGE_FILE_LINE_NUMS_STRIPPED		0x0004
> +#define IMAGE_FILE_LOCAL_SYMS_STRIPPED		0x0008
> +#define IMAGE_FILE_AGGRESSIVE_WS_TRIM		0x0010
> +#define IMAGE_FILE_LARGE_ADDRESS_AWARE		0x0020
> +/* Reserved					0x0040 */
> +#define IMAGE_FILE_BYTES_REVERSED_LO		0x0080
> +#define IMAGE_FILE_32BIT_MACHINE		0x0100
> +#define IMAGE_FILE_DEBUG_STRIPPED		0x0200
> +#define IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP	0x0400
> +#define IMAGE_FILE_NET_RUN_FROM_SWAP		0x0800
> +#define IMAGE_FILE_SYSTEM			0x1000
> +#define IMAGE_FILE_DLL				0x2000
> +#define IMAGE_FILE_UP_SYSTEM_ONLY		0x4000
> +#define IMAGE_FILE_BYTES_REVERSED_HI		0x8000

These defines are duplicated in include/linux/pe.h

Sascha

-- 
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