diff options
author | Stefan Berger <stefanb@linux.ibm.com> | 2020-05-12 11:44:50 -0400 |
---|---|---|
committer | Alexey Kardashevskiy <aik@ozlabs.ru> | 2020-05-13 11:25:23 +1000 |
commit | 8df4a179ab5a8a2e4a185998aa0dd5d8e80b254e (patch) | |
tree | 74abb178f4199319b4e87547291b270b8014cbf3 /lib/libelf/elf.c | |
parent | d04fea6ab0431e81e43a9ce5b1ec33e0c2ea66eb (diff) | |
download | SLOF-8df4a179ab5a8a2e4a185998aa0dd5d8e80b254e.zip SLOF-8df4a179ab5a8a2e4a185998aa0dd5d8e80b254e.tar.gz SLOF-8df4a179ab5a8a2e4a185998aa0dd5d8e80b254e.tar.bz2 |
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 <stefanb@linux.ibm.com>
Signed-off-by: Alexey Kardashevskiy <aik@ozlabs.ru>
Diffstat (limited to 'lib/libelf/elf.c')
-rw-r--r-- | lib/libelf/elf.c | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/lib/libelf/elf.c b/lib/libelf/elf.c index 5204bc3..d368454 100644 --- a/lib/libelf/elf.c +++ b/lib/libelf/elf.c @@ -196,3 +196,29 @@ elf_get_base_addr(void *file_addr) return -1; } + +/** + * Get the file size of the ELF image that has been loaded into a + * buffer larger than the size of the file + * @return The size of the ELF image or < 0 for error + */ +long elf_get_file_size(const void *buffer, const long buffer_size) +{ + const struct ehdr *ehdr = (const struct ehdr *)buffer; + + if (buffer_size < sizeof(struct ehdr)) + return -1; + + /* check if it is an ELF image at all */ + if (cpu_to_be32(ehdr->ei_ident) != 0x7f454c46) + return -1; + + switch (ehdr->ei_class) { + case 1: + return elf_get_file_size32(buffer, buffer_size); + case 2: + return elf_get_file_size64(buffer, buffer_size); + } + + return -1; +} |