summaryrefslogtreecommitdiff
path: root/src/format/elf/program.c
blob: f55b7c795ad0260bc9f105bc0dcb0c6ecd31543b (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

/* OpenIDA - Outil d'analyse de fichiers binaires
 * program.c - gestion des en-têtes de programme d'un ELF
 *
 * Copyright (C) 2010 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  : 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;

}