From 8df4a179ab5a8a2e4a185998aa0dd5d8e80b254e Mon Sep 17 00:00:00 2001 From: Stefan Berger Date: Tue, 12 May 2020 11:44:50 -0400 Subject: elf: Implement elf_get_file_size to determine size of an ELF image Implement elf_get_file_size to determine the size of an ELF image that has been loaded into a buffer much larger than the actual size of the original file. We determine the size by searching for the farthest offset declared by the ELF headers. Signed-off-by: Stefan Berger Signed-off-by: Alexey Kardashevskiy --- include/helpers.h | 2 ++ include/libelf.h | 14 ++++++++++++++ 2 files changed, 16 insertions(+) (limited to 'include') diff --git a/include/helpers.h b/include/helpers.h index 47b2674..112184f 100644 --- a/include/helpers.h +++ b/include/helpers.h @@ -51,5 +51,7 @@ extern unsigned long SLOF_get_vtpm_unit(void); const typeof(((type *)0)->member)* struct_ptr = (ptr); \ (type *)((char *)struct_ptr - offset_of(type, member)); }) #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0])) +#define ROUNDUP(x,v) ((((x) + ((v) - 1)) / (v)) * (v)) +#define MAX(x,y) ((x) > (y) ? (x) : (y)) #endif diff --git a/include/libelf.h b/include/libelf.h index 5fbf279..48ff4d7 100644 --- a/include/libelf.h +++ b/include/libelf.h @@ -96,4 +96,18 @@ void elf_relocate64(void *file_addr, signed long offset); int elf_forth_claim(void *addr, long size); +long elf_get_file_size(const void *buffer, const long buffer_size); +long elf_get_file_size32(const void *buffer, const long buffer_size); +long elf_get_file_size64(const void *buffer, const long buffer_size); + +#ifdef __BIG_ENDIAN__ +#define elf64_to_cpu(x, ehdr) ((ehdr)->ei_data == ELFDATA2MSB ? (x) : bswap_64(x)) +#define elf32_to_cpu(x, ehdr) ((ehdr)->ei_data == ELFDATA2MSB ? (x) : bswap_32(x)) +#define elf16_to_cpu(x, ehdr) ((ehdr)->ei_data == ELFDATA2MSB ? (x) : bswap_16(x)) +#else +#define elf64_to_cpu(x, ehdr) ((ehdr)->ei_data == ELFDATA2LSB ? (x) : bswap_64(x)) +#define elf32_to_cpu(x, ehdr) ((ehdr)->ei_data == ELFDATA2LSB ? (x) : bswap_32(x)) +#define elf16_to_cpu(x, ehdr) ((ehdr)->ei_data == ELFDATA2LSB ? (x) : bswap_16(x)) +#endif + #endif /* __LIBELF_H */ -- cgit v1.1