summaryrefslogtreecommitdiff
path: root/src/format/elf/program.c
blob: 48d6d19c8d3202ba4538b12782154727ff477925 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145

/* OpenIDA - Outil d'analyse de fichiers binaires
 * program.c - gestion des en-têtes de programme d'un ELF
 *
 * Copyright (C) 2010-2013 Cyrille Bagard
 *
 *  This file is part of OpenIDA.
 *
 *  OpenIDA is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  OpenIDA is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
 */


#include "program.h"


#include "elf-int.h"



/******************************************************************************
*                                                                             *
*  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]    *
*                                                                             *
*  Description : Recherche un programme donné au sein de binaire par indice.  *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool find_elf_program_by_index(const GElfFormat *format, uint16_t index, elf_phdr *program)
{
    off_t offset;                           /* Emplacement à venir lire    */

    if (index >= ELF_HDR(format, format->header, e_phnum)) return false;

    offset = ELF_HDR(format, format->header, e_phoff)
        + ELF_HDR(format, format->header, e_phentsize) * index;

    return read_elf_program_header(format, &offset, program);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : format  = description de l'exécutable à consulter.           *
*                addr    = adresse virtuelle à retrouver.                     *
*                pos     = position correspondante. [OUT]                     *
*                                                                             *
*  Description : Fournit la position correspondant à une adresse virtuelle.   *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool translate_address_into_offset_using_elf_programs(const GElfFormat *format, vmpa_t addr, off_t *pos)
{
    bool result;                            /* Bilan à retourner           */
    uint16_t i;                             /* Boucle de parcours          */
    elf_phdr program;                       /* Programme à analyser        */

    result = false;

    for (i = 0; i < ELF_HDR(format, format->header, e_phnum) && !result; i++)
    {
        find_elf_program_by_index(format, i, &program);

        if (ELF_PHDR(format, program, p_vaddr) <= addr
            && addr < (ELF_PHDR(format, program, p_vaddr) + ELF_PHDR(format, program, p_filesz)))
        {
            *pos = ELF_PHDR(format, program, p_offset) + addr - ELF_PHDR(format, program, p_vaddr);
            result = true;
        }

    }

    return result;

}