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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
|
/* OpenIDA - Outil d'analyse de fichiers binaires
* elf-int.c - structures internes du format ELF
*
* Copyright (C) 2009 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 "elf-int.h"
/******************************************************************************
* *
* Paramètres : format = informations chargées à consulter. *
* pos = position de début de lecture. [OUT] *
* header = structure lue à retourner. [OUT] *
* *
* Description : Procède à la lecture d'une en-tête de programme ELF. *
* *
* Retour : Bilan de l'opération. *
* *
* Remarques : - *
* *
******************************************************************************/
bool read_elf_program_header(const GElfFormat *format, off_t *pos, elf_phdr *header)
{
bool result; /* Bilan à retourner */
const bin_t *content; /* Contenu binaire à lire */
off_t length; /* Taille totale du contenu */
content = G_BIN_FORMAT(format)->content;
length = G_BIN_FORMAT(format)->length;
if (format->is_32b)
{
result = read_u32(&header->phdr32.p_type, content, pos, length, format->endian);
result &= read_u32(&header->phdr32.p_offset, content, pos, length, format->endian);
result &= read_u32(&header->phdr32.p_vaddr, content, pos, length, format->endian);
result &= read_u32(&header->phdr32.p_paddr, content, pos, length, format->endian);
result &= read_u32(&header->phdr32.p_filesz, content, pos, length, format->endian);
result &= read_u32(&header->phdr32.p_memsz, content, pos, length, format->endian);
result &= read_u32(&header->phdr32.p_flags, content, pos, length, format->endian);
result &= read_u32(&header->phdr32.p_align, content, pos, length, format->endian);
}
else
{
result = read_u32(&header->phdr64.p_type, content, pos, length, format->endian);
result &= read_u32(&header->phdr64.p_flags, content, pos, length, format->endian);
result &= read_u64(&header->phdr64.p_offset, content, pos, length, format->endian);
result &= read_u64(&header->phdr64.p_vaddr, content, pos, length, format->endian);
result &= read_u64(&header->phdr64.p_paddr, content, pos, length, format->endian);
result &= read_u64(&header->phdr64.p_filesz, content, pos, length, format->endian);
result &= read_u64(&header->phdr64.p_memsz, content, pos, length, format->endian);
result &= read_u64(&header->phdr64.p_align, content, pos, length, format->endian);
}
return result;
}
/******************************************************************************
* *
* Paramètres : format = informations chargées à consulter. *
* pos = position de la tête de lecture. *
* section = section lue. [OUT] *
* *
* Description : Procède à la lecture d'une en-tête de section ELF. *
* *
* Retour : Bilan de l'opération. *
* *
* Remarques : - *
* *
******************************************************************************/
bool read_elf_section_header(const GElfFormat *format, off_t pos, elf_shdr *section)
{
bool result; /* Bilan à retourner */
const bin_t *content; /* Contenu binaire à lire */
off_t length; /* Taille totale du contenu */
elf32_shdr *shdr32; /* Version 32 bits */
elf64_shdr *shdr64; /* Version 32 bits */
result = true;
content = G_BIN_FORMAT(format)->content;
length = G_BIN_FORMAT(format)->length;
if (format->is_32b)
{
shdr32 = §ion->shdr32;
result = read_u32(&shdr32->sh_name, content, &pos, length, format->endian);
result &= read_u32(&shdr32->sh_type, content, &pos, length, format->endian);
result &= read_u32(&shdr32->sh_flags, content, &pos, length, format->endian);
result &= read_u32(&shdr32->sh_addr, content, &pos, length, format->endian);
result &= read_u32(&shdr32->sh_offset, content, &pos, length, format->endian);
result &= read_u32(&shdr32->sh_size, content, &pos, length, format->endian);
result &= read_u32(&shdr32->sh_link, content, &pos, length, format->endian);
result &= read_u32(&shdr32->sh_info, content, &pos, length, format->endian);
result &= read_u32(&shdr32->sh_addralign, content, &pos, length, format->endian);
result &= read_u32(&shdr32->sh_entsize, content, &pos, length, format->endian);
}
else
{
shdr64 = §ion->shdr64;
result = read_u32(&shdr64->sh_name, content, &pos, length, format->endian);
result &= read_u32(&shdr64->sh_type, content, &pos, length, format->endian);
result &= read_u64(&shdr64->sh_flags, content, &pos, length, format->endian);
result &= read_u64(&shdr64->sh_addr, content, &pos, length, format->endian);
result &= read_u64(&shdr64->sh_offset, content, &pos, length, format->endian);
result &= read_u64(&shdr64->sh_size, content, &pos, length, format->endian);
result &= read_u32(&shdr64->sh_link, content, &pos, length, format->endian);
result &= read_u32(&shdr64->sh_info, content, &pos, length, format->endian);
result &= read_u64(&shdr64->sh_addralign, content, &pos, length, format->endian);
result &= read_u64(&shdr64->sh_entsize, content, &pos, length, format->endian);
}
return result;
}
/******************************************************************************
* *
* Paramètres : format = informations chargées à consulter. *
* pos = position de début de lecture. [OUT] *
* sym = structure lue à retourner. [OUT] *
* *
* Description : Procède à la lecture d'un symbole ELF. *
* *
* Retour : Bilan de l'opération. *
* *
* Remarques : - *
* *
******************************************************************************/
bool read_elf_symbol(const GElfFormat *format, off_t *pos, elf_sym *sym)
{
bool result; /* Bilan à retourner */
const bin_t *content; /* Contenu binaire à lire */
off_t length; /* Taille totale du contenu */
content = G_BIN_FORMAT(format)->content;
length = G_BIN_FORMAT(format)->length;
if (format->is_32b)
{
result = read_u32(&sym->sym32.st_name, content, pos, length, format->endian);
result &= read_u32(&sym->sym32.st_value, content, pos, length, format->endian);
result &= read_u32(&sym->sym32.st_size, content, pos, length, format->endian);
result &= read_u8(&sym->sym32.st_info, content, pos, length, format->endian);
result &= read_u8(&sym->sym32.st_other, content, pos, length, format->endian);
result &= read_u16(&sym->sym32.st_shndx, content, pos, length, format->endian);
}
else
{
result = read_u32(&sym->sym64.st_name, content, pos, length, format->endian);
result &= read_u8(&sym->sym64.st_info, content, pos, length, format->endian);
result &= read_u8(&sym->sym64.st_other, content, pos, length, format->endian);
result &= read_u16(&sym->sym64.st_shndx, content, pos, length, format->endian);
result &= read_u64(&sym->sym64.st_value, content, pos, length, format->endian);
result &= read_u64(&sym->sym64.st_size, content, pos, length, format->endian);
}
return result;
}
/******************************************************************************
* *
* Paramètres : format = informations chargées à consulter. *
* pos = position de début de lecture. [OUT] *
* reloc = structure lue à retourner. [OUT] *
* *
* Description : Procède à la lecture d'une relocalisation ELF. *
* *
* Retour : Bilan de l'opération. *
* *
* Remarques : - *
* *
******************************************************************************/
bool read_elf_relocation(const GElfFormat *format, off_t *pos, elf_rel *reloc)
{
bool result; /* Bilan à retourner */
const bin_t *content; /* Contenu binaire à lire */
off_t length; /* Taille totale du contenu */
content = G_BIN_FORMAT(format)->content;
length = G_BIN_FORMAT(format)->length;
if (format->is_32b)
{
result = read_u32(&reloc->rel32.r_offset, content, pos, length, format->endian);
result &= read_u32(&reloc->rel32.r_info, content, pos, length, format->endian);
}
else
{
result = read_u64(&reloc->rel64.r_offset, content, pos, length, format->endian);
result &= read_u64(&reloc->rel64.r_info, content, pos, length, format->endian);
}
return result;
}
|