summaryrefslogtreecommitdiff
path: root/src/format/elf/section.c
blob: a055f471196a83948314ea15ae401a2a173f1dfd (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
146
147
148
149
150
151
152
153
154
155
156

/* OpenIDA - Outil d'analyse de fichiers binaires
 * section.h - prototypes pour la gestion des sections d'un ELF
 *
 * Copyright (C) 2008 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 "section.h"


#include <malloc.h>
#include <string.h>


#include "elf-int.h"



/* Teste si une section correspond à celle recherchée. */
bool find_target_elf_section(const elf_format *, const char *, Elf32_Off, Elf32_Shdr *);





/******************************************************************************
*                                                                             *
*  Paramètres  : format = description de l'exécutable à compléter.            *
*                                                                             *
*  Description : Charge en mémoire la liste humaine des sections.             *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool read_elf_section_names(elf_format *format)
{
    off_t offset;                           /* Position des données        */
    Elf32_Shdr section;                     /* Section visée               */

    offset = format->header.e_shoff + format->header.e_shentsize * format->header.e_shstrndx;
    if ((offset + sizeof(Elf32_Shdr)) >= EXE_FORMAT(format)->length) return false;

    memcpy(&section, &EXE_FORMAT(format)->content[offset], sizeof(Elf32_Shdr));

    if ((section.sh_offset + section.sh_size) >= EXE_FORMAT(format)->length) return false;

    format->sec_names = (char *)calloc(section.sh_size + 1, sizeof(char));
    format->sec_size = section.sh_size;

    memcpy(format->sec_names, &EXE_FORMAT(format)->content[section.sh_offset], section.sh_size);

    return true;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : format = description de l'exécutable à consulter.            *
*                target = nom de la section recherchée.                       *
*                offset = position de la section à tester.                    *
*                data   = description de la section trouvée. [OUT]            *
*                                                                             *
*  Description : Teste si une section correspond à celle recherchée.          *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool find_target_elf_section(const elf_format *format, const char *target, Elf32_Off offset, Elf32_Shdr *data)
{
    bool result;                            /* Conclusion à retourner      */
    Elf32_Shdr section;                     /* Section à analyser          */

    result = false;

    if ((offset + sizeof(Elf32_Shdr)) >= EXE_FORMAT(format)->length) return false;

    memcpy(&section, &EXE_FORMAT(format)->content[offset], sizeof(Elf32_Shdr));

    result = (strcmp(target, &format->sec_names[section.sh_name]) == 0);

    if (result) *data = section;

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : format  = description de l'exécutable à consulter.           *
*                target  = nom de la section recherchée.                      *
*                offset  = position de la section trouvée. [OUT]              *
*                size    = taille de la section trouvée. [OUT]                *
*                voffset = adresse virtuelle de la section trouvée. [OUT]     *
*                                                                             *
*  Description : Recherche une section donnée au sein de binaire.             *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool find_elf_section(const elf_format *format, const char *target, off_t *offset, off_t *size, uint64_t *voffset)
{
    bool result;
    Elf32_Half i;
    Elf32_Shdr data;

    result = false;

    for (i = 0; i < format->header.e_shnum; i++)
    {
        if (i == format->header.e_shstrndx) continue;

        if (find_target_elf_section(format, target,
                                    format->header.e_shoff + format->header.e_shentsize * i, &data))
        {
            *offset = data.sh_offset;
            *size = data.sh_size;

            if (voffset != NULL)
                *voffset = data.sh_addr;

            result = true;

        }

    }

    return result;

}