diff options
Diffstat (limited to 'src/format/elf')
| -rw-r--r-- | src/format/elf/elf.c | 135 | ||||
| -rw-r--r-- | src/format/elf/elf_def.h | 25 | ||||
| -rw-r--r-- | src/format/elf/program.c | 51 | ||||
| -rw-r--r-- | src/format/elf/program.h | 5 | ||||
| -rw-r--r-- | src/format/elf/symbols.c | 6 | 
5 files changed, 213 insertions, 9 deletions
| diff --git a/src/format/elf/elf.c b/src/format/elf/elf.c index 158a6b6..6807e36 100644 --- a/src/format/elf/elf.c +++ b/src/format/elf/elf.c @@ -26,9 +26,13 @@  #include <malloc.h>  #include <stddef.h> +#include <stdio.h>  #include <string.h> +#include <i18n.h> + +  #include "elf-int.h"  #include "program.h"  #include "section.h" @@ -39,11 +43,11 @@ -#ifndef _ -#   define _(str) (str) -#endif +/* Taille maximale d'une description */ +#define MAX_PORTION_DESC 256 +  /* Initialise la classe des formats d'exécutables ELF. */ @@ -58,6 +62,9 @@ static FormatTargetMachine g_elf_format_get_target_machine(const GElfFormat *);  /* Fournit l'adresse mémoire du point d'entrée du programme. */  static vmpa_t g_elf_format_get_entry_point(const GElfFormat *); +/* Etend la définition des portions au sein d'un binaire. */ +static void g_elf_format_refine_portions(const GElfFormat *, GBinPortion *); +  /* Fournit les références aux zones binaires à analyser. */  static GBinPart **g_elf_format_get_parts(const GElfFormat *, size_t *); @@ -139,6 +146,7 @@ static void g_elf_format_init(GElfFormat *format)      exe_format->get_machine = (get_target_machine_fc)g_elf_format_get_target_machine;      exe_format->get_entry_point = (get_entry_point_fc)g_elf_format_get_entry_point; +    exe_format->refine_portions = (refine_portions_fc)g_elf_format_refine_portions;      exe_format->get_parts = (get_parts_fc)g_elf_format_get_parts;      exe_format->translate_addr = (translate_addr_fc)g_elf_format_translate_address_into_offset; @@ -291,6 +299,127 @@ static vmpa_t g_elf_format_get_entry_point(const GElfFormat *format)  /******************************************************************************  *                                                                             *  *  Paramètres  : format = informations chargées à consulter.                  * +*                raw    = portion de binaire brut à raffiner.                 * +*                                                                             * +*  Description : Etend la définition des portions au sein d'un binaire.       * +*                                                                             * +*  Retour      : -                                                            * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +static void g_elf_format_refine_portions(const GElfFormat *format, GBinPortion *raw) +{ +    uint16_t i;                             /* Boucle de parcours          */ +    off_t offset;                           /* Début de part de programme  */ +    elf_phdr phdr;                          /* En-tête de programme ELF    */ +    uint32_t p_flags;                       /* Droits associés à une partie*/ +    const char *background;                 /* Fond signigicatif           */ +    GBinPortion *new;                       /* Nouvelle portion définie    */ +    char desc[MAX_PORTION_DESC];            /* Description d'une portion   */ +    PortionAccessRights rights;             /* Droits d'une portion        */ +    elf_shdr strings;                       /* Section des descriptions    */ +    bool has_strings;                       /* Section trouvée ?           */ +    elf_shdr section;                       /* En-tête de section ELF      */ +    uint64_t sh_flags;                      /* Droits associés à une partie*/ +    const char *name;                       /* Nom trouvé ou NULL          */ + +    /* Côté segments basiques */ + +#if 0 +    for (i = 0; i < ELF_HDR(format, format->header, e_phnum); i++) +    { +        offset = ELF_HDR(format, format->header, e_phoff) +            + ELF_HDR(format, format->header, e_phentsize) * i; + +        if (!read_elf_program_header(format, &offset, &phdr)) +            continue; + +        p_flags = ELF_PHDR(format, phdr, p_flags); + +        if (p_flags & PF_X) background = BPC_CODE; +        else if (p_flags & PF_W) background = BPC_DATA; +        else background = BPC_DATA_RO; + +        new = g_binary_portion_new(background); + +        sprintf(desc, "%s %s", +                _("Segment"), +                get_elf_program_type_desc(ELF_PHDR(format, phdr, p_type))); + +        g_binary_portion_set_desc(new, desc); + +        g_binary_portion_set_values(new, +                                    ELF_PHDR(format, phdr, p_offset), +                                    ELF_PHDR(format, phdr, p_filesz), +                                    ELF_PHDR(format, phdr, p_vaddr)); + +        rights = PAC_NONE; +        if (p_flags & PF_R) rights |= PAC_READ; +        if (p_flags & PF_W) rights |= PAC_WRITE; +        if (p_flags & PF_X) rights |= PAC_EXEC; + +        g_binary_portion_set_rights(new, rights); + +        g_binary_portion_include(raw, new); + +    } +#endif + +    /* Inclusion des sections, si possible... */ + +    has_strings = find_elf_section_by_index(format, +                                            ELF_HDR(format, format->header, e_shstrndx), +                                            &strings); + +    for (i = 0; i < ELF_HDR(format, format->header, e_shnum); i++) +    { +        if (!find_elf_section_by_index(format, i, §ion)) +            continue; + +        sh_flags = ELF_SHDR(format, section, sh_flags); + +        if (sh_flags & SHF_EXECINSTR) background = BPC_CODE; +        else if (sh_flags & SHF_WRITE) background = BPC_DATA; +        else background = BPC_DATA_RO; + +        new = g_binary_portion_new(background); + +        if (has_strings) +            name = extract_name_from_elf_string_section(format, &strings, +                                                        ELF_SHDR(format, section, sh_name)); +        else name = NULL; + +        if (name != NULL) +            sprintf(desc, "%s %s", _("Section"), name); +        else +            sprintf(desc, "%s ???", _("Section")); + +        g_binary_portion_set_desc(new, desc); + +        rights = PAC_NONE; +        if (sh_flags & SHF_ALLOC) rights |= PAC_READ; +        if (sh_flags & SHF_WRITE) rights |= PAC_WRITE; +        if (sh_flags & SHF_EXECINSTR) rights |= PAC_EXEC; + +        g_binary_portion_set_rights(new, rights); + +        g_binary_portion_set_values(new, +                                    ELF_SHDR(format, section, sh_offset), +                                    ELF_SHDR(format, section, sh_size), +                                    ELF_SHDR(format, section, sh_addr)); + +        g_binary_portion_include(raw, new); + +    } + +} + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : format = informations chargées à consulter.                  *  *                count  = quantité de zones listées. [OUT]                    *  *                                                                             *  *  Description : Fournit les références aux zones binaires à analyser.        * diff --git a/src/format/elf/elf_def.h b/src/format/elf/elf_def.h index dda1b1c..831e4ff 100644 --- a/src/format/elf/elf_def.h +++ b/src/format/elf/elf_def.h @@ -2,7 +2,7 @@  /* OpenIDA - Outil d'analyse de fichiers binaires   * elf_def.h - liste des structures et constantes utilisées par le format ELF   * - * Copyright (C) 2009-2010 Cyrille Bagard + * Copyright (C) 2009-2013 Cyrille Bagard   *   *  This file is part of OpenIDA.   * @@ -178,6 +178,28 @@ typedef union _elf_phdr  #define ELF_SIZEOF_PHDR(fmt) (fmt->is_32b ? sizeof(elf32_phdr) : sizeof(elf64_phdr)) +/* Valeurs possibles pour p_type */ + +#define PT_NULL         0                   /* Program header table entry unused */ +#define PT_LOAD         1                   /* Loadable program segment */ +#define PT_DYNAMIC      2                   /* Dynamic linking information */ +#define PT_INTERP       3                   /* Program interpreter */ +#define PT_NOTE         4                   /* Auxiliary information */ +#define PT_SHLIB        5                   /* Reserved */ +#define PT_PHDR         6                   /* Entry for header table itself */ +#define PT_TLS          7                   /* Thread-local storage segment */ +#define PT_NUM          8                   /* Number of defined types */ +#define PT_LOOS         0x60000000          /* Start of OS-specific */ +#define PT_GNU_EH_FRAME 0x6474e550          /* GCC .eh_frame_hdr segment */ +#define PT_GNU_STACK    0x6474e551          /* Indicates stack executability */ +#define PT_GNU_RELRO    0x6474e552          /* Read-only after relocation */ +#define PT_LOSUNW       0x6ffffffa +#define PT_SUNWBSS      0x6ffffffa          /* Sun Specific segment */ +#define PT_SUNWSTACK    0x6ffffffb          /* Stack segment */ +#define PT_HISUNW       0x6fffffff +#define PT_HIOS         0x6fffffff          /* End of OS-specific */ +#define PT_LOPROC       0x70000000          /* Start of processor-specific */ +#define PT_HIPROC       0x7fffffff          /* End of processor-specific */  /* Valeurs possibles pour p_flags */ @@ -246,6 +268,7 @@ typedef union _elf_shdr  /* Valeurs possibles pour sh_flags */ +#define SHF_WRITE       (1 << 0)            /* Accessible en écriture      */  #define SHF_ALLOC       (1 << 1)            /* Copie en mémoire pdt l'exec.*/  #define SHF_EXECINSTR   (1 << 2)            /* Section exécutable          */  #define SHF_STRINGS     (1 << 5)            /* Contient des chaînes ('\0') */ diff --git a/src/format/elf/program.c b/src/format/elf/program.c index f55b7c7..48d6d19 100644 --- a/src/format/elf/program.c +++ b/src/format/elf/program.c @@ -2,7 +2,7 @@  /* OpenIDA - Outil d'analyse de fichiers binaires   * program.c - gestion des en-têtes de programme d'un ELF   * - * Copyright (C) 2010 Cyrille Bagard + * Copyright (C) 2010-2013 Cyrille Bagard   *   *  This file is part of OpenIDA.   * @@ -30,6 +30,55 @@  /******************************************************************************  *                                                                             * +*  Paramètres  : p_type = type associé à un en-tête de programme.             * +*                                                                             * +*  Description : Fournit la description humaine d'un type de segment ELF.     * +*                                                                             * +*  Retour      : Désignation prête à emploi.                                  * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +const char *get_elf_program_type_desc(uint32_t p_type) +{ +    const char *result;                     /* Description à renvoyer      */ + +#define MAKE_STRING_FROM_PT(pt) case pt: result = #pt; break; + +    switch(p_type) +    { +        MAKE_STRING_FROM_PT(PT_NULL); +        MAKE_STRING_FROM_PT(PT_LOAD); +        MAKE_STRING_FROM_PT(PT_DYNAMIC); +        MAKE_STRING_FROM_PT(PT_INTERP); +        MAKE_STRING_FROM_PT(PT_NOTE); +        MAKE_STRING_FROM_PT(PT_SHLIB); +        MAKE_STRING_FROM_PT(PT_PHDR); +        MAKE_STRING_FROM_PT(PT_TLS); +        MAKE_STRING_FROM_PT(PT_NUM); +        MAKE_STRING_FROM_PT(PT_LOOS); +        MAKE_STRING_FROM_PT(PT_GNU_EH_FRAME); +        MAKE_STRING_FROM_PT(PT_GNU_STACK); +        MAKE_STRING_FROM_PT(PT_GNU_RELRO); +        MAKE_STRING_FROM_PT(PT_LOSUNW); +        MAKE_STRING_FROM_PT(PT_SUNWSTACK); +        MAKE_STRING_FROM_PT(PT_HIOS); +        MAKE_STRING_FROM_PT(PT_LOPROC); +        MAKE_STRING_FROM_PT(PT_HIPROC); + +        default: +            result = "PT_???"; +            break; +    } + +    return result; + +} + + +/****************************************************************************** +*                                                                             *  *  Paramètres  : format  = description de l'exécutable à consulter.           *  *                index   = indice de la section recherchée.                   *  *                program = ensemble d'informations à faire remonter. [OUT]    * diff --git a/src/format/elf/program.h b/src/format/elf/program.h index 1718ca7..276e720 100644 --- a/src/format/elf/program.h +++ b/src/format/elf/program.h @@ -2,7 +2,7 @@  /* OpenIDA - Outil d'analyse de fichiers binaires   * program.h - prototypes pour la gestion des en-têtes de programme d'un ELF   * - * Copyright (C) 2010 Cyrille Bagard + * Copyright (C) 2010-2013 Cyrille Bagard   *   *  This file is part of OpenIDA.   * @@ -30,6 +30,9 @@ +/* Fournit la description humaine d'un type de segment ELF. */ +const char *get_elf_program_type_desc(uint32_t); +  /* Recherche un programme donné au sein de binaire par indice. */  bool find_elf_program_by_index(const GElfFormat *, uint16_t, elf_phdr *); diff --git a/src/format/elf/symbols.c b/src/format/elf/symbols.c index df1b613..50750ae 100644 --- a/src/format/elf/symbols.c +++ b/src/format/elf/symbols.c @@ -28,6 +28,9 @@  #include <string.h> +#include <i18n.h> + +  #include "elf-int.h"  #include "helper_x86.h"  #include "section.h" @@ -38,9 +41,6 @@ -#define _(str) str - -  /* Récupère la désignation d'un symbole donné. */ | 
