summaryrefslogtreecommitdiff
path: root/src/format/elf/elf_def.h
blob: bbd6117383eac2df8ec73f6393366f6907cb19f1 (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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388

/* OpenIDA - Outil d'analyse de fichiers binaires
 * elf_def.h - liste des structures et constantes utilisées par le 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/>.
 */


#ifndef _FORMAT_ELF_ELF_DEF_H
#define _FORMAT_ELF_ELF_DEF_H


#include <stdint.h>



/* Adresses virtuelles */
typedef union _elf_addr
{
    uint32_t addr32;                        /* Elf 32 bits                 */
    uint64_t addr64;                        /* Elf 64 bits                 */

} elf_addr;

/* Positions dans le fichier */
typedef union _elf_off
{
    uint32_t off32;                         /* Elf 32 bits                 */
    uint64_t off64;                         /* Elf 64 bits                 */

} elf_off;


#define ELF_OFF(fmt, off) (fmt->is_32b ? (off).off32 : (off).off64)



/* ---------------------------- EN-TETE DES FICHIERS ELF ---------------------------- */


#define EI_NIDENT   16


/* En-tête de fichier ELF */
typedef struct _elf_header
{
    uint8_t e_ident[EI_NIDENT];             /* Magic number + informations */
    uint16_t e_type;                        /* Type de fichier             */
    uint16_t e_machine;                     /* Architecture                */
    uint32_t e_version;                     /* Version du type de fichier  */
    elf_addr e_entry;                       /* Point d'entrée du programme */
    elf_off e_phoff;                        /* Début de la table 'Program' */
    elf_off e_shoff;                        /* Début de la table 'Section' */
    uint32_t e_flags;                       /* Prop. spécifiques au proc.  */
    uint16_t e_ehsize;                      /* Taille de l'en-tête en oct. */
    uint16_t e_phentsize;                   /* Taille d'une entrée Program */
    uint16_t e_phnum;                       /* Nombre d'éléments 'Program' */
    uint16_t e_shentsize;                   /* Taille d'une entrée Section */
    uint16_t e_shnum;                       /* Nombre d'éléments 'Section' */
    uint16_t e_shstrndx;                    /* Indice de la section chaînes*/

} elf_header;


/* Composition du champ e_ident */


#define EI_CLASS        4                   /* Indice de classe du fichier */
#define EI_DATA         5                   /* Indice de l'encodage        */


/* ... EI_CLASS */

#define ELFCLASSNONE    0                   /* Objet invalide              */
#define ELFCLASS32      1                   /* Objet 32 bits               */
#define ELFCLASS64      2                   /* Objet 64 bits               */

/* ... EI_DATA */

#define ELFDATANONE     0                   /* Encodage invalide           */
#define ELFDATA2LSB     1                   /* Complément à 2, petit bout. */
#define ELFDATA2MSB     2                   /* Complément à 2, grand bout. */


/* Valeurs possibles pour e_machine */

#define EM_NONE         0                   /* Aucune machine              */
#define EM_386          3                   /* Intel 80386                 */
#define EM_MIPS         8                   /* MIPS R3000 big-endian       */
#define EM_MIPS_RS3_LE  10                  /* MIPS R3000 little-endian    */



/* --------------------------- EN-TETE DES PROGRAMMES ELF --------------------------- */


/* Version 32 et 64 bits */


typedef struct _elf32_phdr
{
    uint32_t p_type;                        /* Type de segment             */
    uint32_t p_offset;                      /* Position dans le fichier    */
    uint32_t p_vaddr;                       /* Adresse virtuelle du segment*/
    uint32_t p_paddr;                       /* Adresse physique du segment */
    uint32_t p_filesz;                      /* Taille dans le fichier      */
    uint32_t p_memsz;                       /* Taille en mémoire           */
    uint32_t p_flags;                       /* Drapeaux pour le segment    */
    uint32_t p_align;                       /* Alignement du segment       */

} elf32_phdr;

typedef struct _elf64_phdr
{
    uint32_t p_type;                        /* Type de segment             */
    uint32_t p_flags;                       /* Drapeaux pour le segment    */
    uint64_t p_offset;                      /* Position dans le fichier    */
    uint64_t p_vaddr;                       /* Adresse virtuelle du segment*/
    uint64_t p_paddr;                       /* Adresse physique du segment */
    uint64_t p_filesz;                      /* Taille dans le fichier      */
    uint64_t p_memsz;                       /* Taille en mémoire           */
    uint64_t p_align;                       /* Alignement du segment       */

} elf64_phdr;

typedef union _elf_phdr
{
    elf32_phdr phdr32;                      /* Version 32 bits             */
    elf64_phdr phdr64;                      /* Version 32 bits             */

} elf_phdr;


#define ELF_PHDR(fmt, hdr, fld) (fmt->is_32b ? hdr.phdr32.fld : hdr.phdr64.fld)

#define ELF_SIZEOF_PHDR(fmt) (fmt->is_32b ? sizeof(elf32_phdr) : sizeof(elf64_phdr))


/* Valeurs possibles pour p_flags */

#define PF_X    (1 << 0)                    /* Le segment est exécutable   */
#define PF_W    (1 << 1)                    /* Le segment est écrasable    */
#define PF_R    (1 << 2)                    /* Le segment est lisible      */



/* ---------------------------- EN-TETE DES SECTIONS ELF ---------------------------- */


/* Version 32 et 64 bits */

typedef struct _elf32_shdr
{
    uint32_t sh_name;                       /* Indice du nom de la section */
    uint32_t sh_type;                       /* Type de section             */
    uint32_t sh_flags;                      /* Drapeaux pour la section    */
    uint32_t sh_addr;                       /* Adresse virtuelle à l'exec. */
    uint32_t sh_offset;                     /* Position dans le fichier    */
    uint32_t sh_size;                       /* Taille en octets            */
    uint32_t sh_link;                       /* Lien vers une autre section */
    uint32_t sh_info;                       /* Infos. complémentaires      */
    uint32_t sh_addralign;                  /* Alignement de la section    */
    uint32_t sh_entsize;                    /* Eventuelle taille d'élément */

} elf32_shdr;

typedef struct _elf64_shdr
{
    uint32_t sh_name;                       /* Indice du nom de la section */
    uint32_t sh_type;                       /* Type de section             */
    uint64_t sh_flags;                      /* Drapeaux pour la section    */
    uint64_t sh_addr;                       /* Adresse virtuelle à l'exec. */
    uint64_t sh_offset;                     /* Position dans le fichier    */
    uint64_t sh_size;                       /* Taille en octets            */
    uint32_t sh_link;                       /* Lien vers une autre section */
    uint32_t sh_info;                       /* Infos. complémentaires      */
    uint64_t sh_addralign;                  /* Alignement de la section    */
    uint64_t sh_entsize;                    /* Eventuelle taille d'élément */

} elf64_shdr;

typedef union _elf_shdr
{
    elf32_shdr shdr32;                      /* Version 32 bits             */
    elf64_shdr shdr64;                      /* Version 32 bits             */

} elf_shdr;


#define ELF_SHDR(fmt, shdr, fld) (fmt->is_32b ? (shdr).shdr32.fld : (shdr).shdr64.fld)

#define ELF_SIZEOF_SHDR(fmt) (fmt->is_32b ? sizeof(elf32_shdr) : sizeof(elf64_shdr))


/* Valeurs possibles pour sh_type */

#define SHT_NULL        0                   /* Entrée non utilisée         */
#define SHT_PROGBITS    1                   /* Données de programme        */
#define SHT_SYMTAB      2                   /* Table des symboles          */
#define SHT_STRTAB      3                   /* Table de chaînes de carac.  */
#define SHT_DYNAMIC     6                   /* Info. de liaison dynamique  */


/* Valeurs possibles pour sh_flags */

#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') */



/* ----------------------------- DONNEES POUR LE LINKER ----------------------------- */


/* Entrées de la section dynamique (version 32 et 64 bits) */

typedef struct _elf32_dyn
{
    int32_t d_tag;                          /* Type de l'entrée            */

    union
    {
        uint32_t d_val;                     /* Valeur entière              */
        uint32_t d_ptr;                     /* Valeur d'adresse            */

    } d_un;

} elf32_dyn;

typedef struct _elf64_dyn
{
    int64_t d_tag;                          /* Type de l'entrée            */

    union
    {
        uint64_t d_val;                     /* Valeur entière              */
        uint64_t d_ptr;                     /* Valeur d'adresse            */

    } d_un;

} elf64_dyn;

typedef union _elf_dyn
{
    elf32_dyn dyn32;                        /* Version 32 bits             */
    elf64_dyn dyn64;                        /* Version 32 bits             */

} elf_dyn;


#define ELF_DYN(fmt, dyn, fld) (fmt->is_32b ? (dyn).dyn32.fld : (dyn).dyn64.fld)

#define ELF_SIZEOF_DYN(fmt) (fmt->is_32b ? sizeof(elf32_dyn) : sizeof(elf64_dyn))



/* Valeurs possibles pour d_tag */

#define DT_SYMTAB       6                   /* Table des symboles          */
#define DT_JMPREL       23                  /* Relocalisations PLT         */



/* ---------------------------- SYMBOLES DE BINAIRES ELF ---------------------------- */


/* Elément de la table des symboles */

typedef struct _elf32_sym
{
    uint32_t st_name;                       /* Indice pour le nom          */
    uint32_t st_value;                      /* Valeur du symbole           */
    uint32_t st_size;                       /* Taille du symbole           */
    unsigned char st_info;                  /* Type et infos. du symbole   */
    unsigned char st_other;                 /* Visibilité du symbole       */
    uint16_t st_shndx;                      /* Indice de la section        */

} elf32_sym;

typedef struct _elf64_sym
{
    uint32_t st_name;                       /* Indice pour le nom          */
    unsigned char st_info;                  /* Type et infos. du symbole   */
    unsigned char st_other;                 /* Visibilité du symbole       */
    uint16_t st_shndx;                      /* Indice de la section        */
    uint64_t st_value;                      /* Valeur du symbole           */
    uint64_t st_size;                       /* Taille du symbole           */

} elf64_sym;

typedef union _elf_sym
{
    elf32_sym sym32;                        /* Version 32 bits             */
    elf64_sym sym64;                        /* Version 64 bits             */

} elf_sym;


#define ELF_SYM(fmt, sb, fld) (fmt->is_32b ? sb.sym32.fld : sb.sym64.fld)

#define ELF_ST_BIND(fmt, sym) (fmt->is_32b ? ELF32_ST_BIND(sym.sym32.st_info) : ELF64_ST_BIND(sym.sym64.st_info))
#define ELF_ST_TYPE(fmt, sym) (fmt->is_32b ? ELF32_ST_TYPE(sym.sym32.st_info) : ELF64_ST_TYPE(sym.sym64.st_info))

#define ELF_SIZEOF_SYM(fmt) (fmt->is_32b ? sizeof(elf32_sym) : sizeof(elf64_sym))


/* Extraction des informations de st_info */

#define ELF32_ST_BIND(val)      (((unsigned char)(val)) >> 4)
#define ELF32_ST_TYPE(val)      ((val) & 0xf)

#define ELF64_ST_BIND(val)      ELF32_ST_BIND(val)
#define ELF64_ST_TYPE(val)      ELF32_ST_TYPE(val)

/* Valeurs pour le sous-champ ST_TYPE de st_info  */

#define STT_NOTYPE  0                       /* Type de symbole non spécifié*/
#define STT_OBJECT  1                       /* Symbole, objet de données   */
#define STT_FUNC    2                       /* Symbole, objet de code      */



/* ------------------------- INFORMATIONS DE RELOCALISATION ------------------------- */


/* Entrée de la table de relocalisation */

typedef struct _elf32_rel
{
    uint32_t r_offset;                      /* Adresse                     */
    uint32_t r_info;			            /* Indice de type et symbole   */

} elf32_rel;

typedef struct _elf64_rel
{
    uint64_t r_offset;                      /* Adresse                     */
    uint64_t r_info;			            /* Indice de type et symbole   */

} elf64_rel;

typedef union _elf_rel
{
    elf32_rel rel32;                        /* Version 32 bits             */
    elf64_rel rel64;                        /* Version 64 bits             */

} elf_rel;


#define ELF_REL(fmt, rl, fld) (fmt->is_32b ? rl.rel32.fld : rl.rel64.fld)

#define ELF_REL_SYM(fmt, rl) (fmt->is_32b ? ELF32_R_SYM(rl.rel32.r_info) : ELF64_R_SYM(rl.rel64.r_info))
#define ELF_REL_TYPE(fmt, rl) (fmt->is_32b ? ELF32_R_TYPE(rl.rel32.r_info) : ELF64_R_TYPE(rl.rel64.r_info))

#define ELF_SIZEOF_REL(fmt) (fmt->is_32b ? sizeof(elf32_rel) : sizeof(elf64_rel))


/* Extraction des informations de r_info */

#define ELF32_R_SYM(val)        ((val) >> 8)
#define ELF32_R_TYPE(val)       ((val) & 0xff)

#define ELF64_R_SYM(val)        ((val) >> 32)
#define ELF64_R_TYPE(val)       ((val) & 0xffffffff)

/* Type de relocalisation (x86) */

#define R_386_NONE          0               /* Pas de relocalisation       */
#define R_386_JMP_SLOT      7               /* Entrée PLT                  */



#endif  /* _FORMAT_ELF_ELF_DEF_H */