diff options
Diffstat (limited to 'src/format/elf/elf-int.c')
-rw-r--r-- | src/format/elf/elf-int.c | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/src/format/elf/elf-int.c b/src/format/elf/elf-int.c index 69363d2..85bdfc0 100644 --- a/src/format/elf/elf-int.c +++ b/src/format/elf/elf-int.c @@ -24,6 +24,108 @@ #include "elf-int.h" +#include <string.h> + + + +/****************************************************************************** +* * +* Paramètres : content = contenu binaire à parcourir. * +* length = taille du contenu en question. * +* header = en-tête à déterminer. [OUT] * +* is_32b = indique si le format est en 32 ou 64 bits. [OUT] * +* endian = boutisme reconnu dans le format. [OUT] * +* * +* Description : Procède à la lecture de l'en-tête d'un contenu binaire ELF. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool read_elf_header(const bin_t *content, off_t length, elf_header *header, bool *is_32b, SourceEndian *endian) +{ + bool result; /* Bilan à retourner */ + off_t pos; /* Position de lecture */ + + result = (length >= EI_NIDENT); + + pos = 0; + + if (result) + { + memcpy(header->hdr32.e_ident, content, EI_NIDENT); + pos += EI_NIDENT; + } + + /* Détermination de l'espace d'adressage */ + if (result) + switch (header->hdr32.e_ident[EI_CLASS]) + { + case ELFCLASS32: + *is_32b = true; + break; + case ELFDATA2MSB: + *is_32b = false; + break; + default: + result = false; + break; + } + + /* Détermination du boutisme */ + if (result) + switch (header->hdr32.e_ident[EI_DATA]) + { + case ELFDATA2LSB: + *endian = SRE_LITTLE; + break; + case ELFDATA2MSB: + *endian = SRE_BIG; + break; + default: + result = false; + break; + } + + if (*is_32b) + { + result &= read_u16(&header->hdr32.e_type, content, &pos, length, *endian); + result &= read_u16(&header->hdr32.e_machine, content, &pos, length, *endian); + result &= read_u32(&header->hdr32.e_version, content, &pos, length, *endian); + result &= read_u32(&header->hdr32.e_entry, content, &pos, length, *endian); + result &= read_u32(&header->hdr32.e_phoff, content, &pos, length, *endian); + result &= read_u32(&header->hdr32.e_shoff, content, &pos, length, *endian); + result &= read_u32(&header->hdr32.e_flags, content, &pos, length, *endian); + result &= read_u16(&header->hdr32.e_ehsize, content, &pos, length, *endian); + result &= read_u16(&header->hdr32.e_phentsize, content, &pos, length, *endian); + result &= read_u16(&header->hdr32.e_phnum, content, &pos, length, *endian); + result &= read_u16(&header->hdr32.e_shentsize, content, &pos, length, *endian); + result &= read_u16(&header->hdr32.e_shnum, content, &pos, length, *endian); + result &= read_u16(&header->hdr32.e_shstrndx, content, &pos, length, *endian); + } + else + { + result &= read_u16(&header->hdr64.e_type, content, &pos, length, *endian); + result &= read_u16(&header->hdr64.e_machine, content, &pos, length, *endian); + result &= read_u32(&header->hdr64.e_version, content, &pos, length, *endian); + result &= read_u64(&header->hdr64.e_entry, content, &pos, length, *endian); + result &= read_u64(&header->hdr64.e_phoff, content, &pos, length, *endian); + result &= read_u64(&header->hdr64.e_shoff, content, &pos, length, *endian); + result &= read_u32(&header->hdr64.e_flags, content, &pos, length, *endian); + result &= read_u16(&header->hdr64.e_ehsize, content, &pos, length, *endian); + result &= read_u16(&header->hdr64.e_phentsize, content, &pos, length, *endian); + result &= read_u16(&header->hdr64.e_phnum, content, &pos, length, *endian); + result &= read_u16(&header->hdr64.e_shentsize, content, &pos, length, *endian); + result &= read_u16(&header->hdr64.e_shnum, content, &pos, length, *endian); + result &= read_u16(&header->hdr64.e_shstrndx, content, &pos, length, *endian); + } + + return result; + +} + /****************************************************************************** * * |