OpenCores
URL https://opencores.org/ocsvn/riscv_vhdl/riscv_vhdl/trunk

Subversion Repositories riscv_vhdl

[/] [riscv_vhdl/] [trunk/] [debugger/] [src/] [libdbg64g/] [services/] [elfloader/] [elf_types.h] - Blame information for rev 4

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 4 sergeykhbr
/**
2
 * @file
3
 * @copyright  Copyright 2016 GNSS Sensor Ltd. All right reserved.
4
 * @author     Sergey Khabarov - sergeykhbr@gmail.com
5
 * @brief      elf-file loader class declaration.
6
 */
7
 
8
#ifndef __DEBUGGER_ELF_TYPES_H__
9
#define __DEBUGGER_ELF_TYPES_H__
10
 
11
#include <inttypes.h>
12
 
13
namespace debugger {
14
 
15
//#define EI_NIDENT 16
16
#define ARCH_64BITS
17
 
18
#ifdef ARCH_64BITS
19
typedef uint64_t   ElfAddr64;
20
typedef uint64_t   ElfOff64;
21
#else
22
typedef unsigned int   Elf32_Addr;
23
typedef unsigned int   Elf32_Off;
24
#endif
25
typedef unsigned short ElfHalf;
26
typedef signed   int   ElfSword;
27
typedef unsigned int   ElfWord;
28
typedef uint64_t       ElfDWord;
29
 
30
typedef unsigned int   ElfAddr32;
31
typedef unsigned int   ElfOff32;
32
 
33
static const char MAGIC_BYTES[]  = {0x7f,'E','L','F',0};
34
 
35
enum E_EI {
36
    EI_MAG0,
37
    EI_MAG1,
38
    EI_MAG2,
39
    EI_MAG3,
40
    EI_CLASS,
41
    EI_DATA,
42
    EI_VERSION,
43
    EI_PAD,
44
    EI_NIDENT=16
45
};
46
static const uint8_t ELFCLASSNONE = 0;
47
static const uint8_t ELFCLASS32   = 1;
48
static const uint8_t ELFCLASS64   = 2;
49
 
50
static const uint8_t ELFDATANONE  = 0;
51
static const uint8_t ELFDATA2LSB  = 1;
52
static const uint8_t ELFDATA2MSB  = 2;
53
 
54
static const uint8_t EV_NONE      = 0;          // Invalid version
55
static const uint8_t EV_CURRENT   = 1;          // Current version
56
//etype values:
57
static const ElfHalf ET_NONE      = 0;       // no file type
58
static const ElfHalf ET_REL       = 1;       // rellocatable file
59
static const ElfHalf ET_EXEC      = 2;       // executable file
60
static const ElfHalf ET_DYN       = 3;       // shared object file
61
static const ElfHalf ET_CORE      = 4;       // core file
62
static const ElfHalf ET_LOPROC    = 0xff00;  // Processor-specific
63
static const ElfHalf ET_HIPROC    = 0xffff;  // Processor-specific
64
//emachine values:
65
static const ElfHalf EM_NONE      = 0;       // No machine
66
static const ElfHalf EM_M32       = 1;       // AT&T WE 32100
67
static const ElfHalf EM_SPARC     = 2;       // SPARC
68
static const ElfHalf EM_386       = 3;       // Intel 386
69
static const ElfHalf EM_68K       = 4;       // Motorola 68000
70
static const ElfHalf EM_88K       = 5;       // Motorola 88000
71
static const ElfHalf EM_860       = 7;       // Intel 80860
72
static const ElfHalf EM_MIPS      = 8;       // MIPS RS3000
73
 
74
struct Elf32_Ehdr {
75
    unsigned char e_ident[EI_NIDENT];
76
    ElfHalf e_type;      // Shared/Executable/Rellocalable etc
77
    ElfHalf e_machine;   // SPARC, X86 etc
78
    ElfWord e_version;   //
79
    ElfAddr32 e_entry;     // entry point
80
    ElfOff32  e_phoff;     // Program header offset
81
    ElfOff32  e_shoff;     // Section Header offset
82
    ElfWord e_flags;
83
    ElfHalf e_ehsize;
84
    ElfHalf e_phentsize; // size of one entry in the Program header. All entries are the same size
85
    ElfHalf e_phnum;     // number of entries in a Program header
86
    ElfHalf e_shentsize; // entry size in the section header table. all entries are the same size
87
    ElfHalf e_shnum;     // number of section header entries
88
    ElfHalf e_shstrndx;
89
};
90
 
91
struct Elf64_Ehdr {
92
    unsigned char e_ident[EI_NIDENT];
93
    ElfHalf e_type;      // Shared/Executable/Rellocalable etc
94
    ElfHalf e_machine;   // SPARC, X86 etc
95
    ElfWord e_version;   //
96
    ElfAddr64 e_entry;     // entry point
97
    ElfOff64  e_phoff;     // Program header offset
98
    ElfOff64  e_shoff;     // Section Header offset
99
    ElfWord e_flags;
100
    ElfHalf e_ehsize;
101
    ElfHalf e_phentsize; // size of one entry in the Program header. All entries are the same size
102
    ElfHalf e_phnum;     // number of entries in a Program header
103
    ElfHalf e_shentsize; // entry size in the section header table. all entries are the same size
104
    ElfHalf e_shnum;     // number of section header entries
105
    ElfHalf e_shstrndx;
106
};
107
 
108
 
109
static ElfHalf SwapBytes(ElfHalf v) {
110
     v = ((v>>8)&0xff) | ((v&0xFF)<<8);
111
     return v;
112
}
113
 
114
static ElfWord SwapBytes(ElfWord v) {
115
    v = ((v >> 24) & 0xff) | ((v >> 8) & 0xff00)
116
      | ((v << 8) & 0xff0000) | ((v & 0xFF) << 24);
117
    return v;
118
}
119
 
120
static ElfDWord SwapBytes(ElfDWord v) {
121
    v = ((v >> 56) & 0xffull) | ((v >> 48) & 0xff00ull)
122
      | ((v >> 40) & 0xff0000ull) | ((v >> 32) & 0xff000000ull)
123
      | ((v << 8) & 0xff000000ull) | ((v << 16) & 0xff0000000000ull)
124
      | ((v << 24) & 0xff0000000000ull) | ((v << 32) & 0xff00000000000000ull);
125
    return v;
126
}
127
 
128
class ElfHeaderType {
129
 public:
130
    ElfHeaderType(uint8_t *img) {
131
        pimg_ = img;
132
        isElf_ = true;
133
        for (int i = 0; i < 4; i++) {
134
            if (pimg_[i] != MAGIC_BYTES[i]) {
135
                isElf_ = false;
136
            }
137
        }
138
        is32b_ = pimg_[EI_CLASS] == ELFCLASS32;
139
        isMsb_ = pimg_[EI_DATA] == ELFDATA2MSB;
140
        if (is32b_) {
141
            Elf32_Ehdr *h = reinterpret_cast<Elf32_Ehdr *>(pimg_);
142
            if (isMsb_) {
143
                e_shoff_ = SwapBytes(h->e_shoff);
144
                e_shnum_ = SwapBytes(h->e_shnum);
145
                e_phoff_ = SwapBytes(h->e_phoff);
146
            } else {
147
                e_shoff_ = h->e_shoff;
148
                e_shnum_ = h->e_shnum;
149
                e_phoff_ = h->e_phoff;
150
            }
151
        } else {
152
            Elf64_Ehdr *h = reinterpret_cast<Elf64_Ehdr *>(pimg_);
153
            if (isMsb_) {
154
                e_shoff_ = SwapBytes(h->e_shoff);
155
                e_shnum_ = SwapBytes(h->e_shnum);
156
                e_phoff_ = SwapBytes(h->e_phoff);
157
            } else {
158
                e_shoff_ = h->e_shoff;
159
                e_shnum_ = h->e_shnum;
160
                e_phoff_ = h->e_phoff;
161
            }
162
        }
163
    }
164
 
165
    virtual bool isElf() { return isElf_; }
166
    virtual bool isElf32() { return is32b_; }
167
    virtual bool isElfMsb() { return isMsb_; }
168
    virtual uint64_t get_shoff() { return e_shoff_; }
169
    virtual ElfHalf get_shnum() { return e_shnum_; }
170
    virtual uint64_t get_phoff() { return e_phoff_; }
171
 protected:
172
    uint8_t *pimg_;
173
    bool isElf_;
174
    bool is32b_;
175
    bool isMsb_;
176
    uint64_t e_shoff_;
177
    ElfHalf e_shnum_;
178
    uint64_t e_phoff_;
179
};
180
 
181
 
182
//sh_type:
183
static const ElfWord SHT_NULL      = 0;          // section header is inactive
184
static const ElfWord SHT_PROGBITS  = 1;          // section with CPU instructions
185
static const ElfWord SHT_SYMTAB    = 2;          // section contains symbols table (also as SHT_DYNSIM)
186
static const ElfWord SHT_STRTAB    = 3;          // section holds string table
187
static const ElfWord SHT_RELA      = 4;          // section with relocation data
188
static const ElfWord SHT_HASH      = 5;          // section with hash table. Must be for the dynamic lib
189
static const ElfWord SHT_DYNAMIC   = 6;          // section holds information for dynamic linking.
190
static const ElfWord SHT_NOTE      = 7;
191
static const ElfWord SHT_NOBITS    = 8;          // section with not initialized data
192
static const ElfWord SHT_REL       = 9;
193
static const ElfWord SHT_SHLIB     = 10;
194
static const ElfWord SHT_DYNSYM    = 11;         // section contains debug symbol table
195
static const ElfWord SHT_LOPROC    = 0x70000000;
196
static const ElfWord SHT_HIPROC    = 0x7fffffff;
197
static const ElfWord SHT_HIUSER    = 0xffffffff;
198
//sh_flags:
199
static const ElfWord SHF_WRITE     = 0x1;        // section contains data that should be writable during process execution.
200
static const ElfWord SHF_ALLOC     = 0x2;        // section occupies memory during process execution. 
201
static const ElfWord SHF_EXECINSTR = 0x4;        // section contains executable machine instructions.
202
static const ElfWord SHF_MASKPROC  = 0xf0000000; // processor-specific sematic
203
 
204
struct Elf32_Shdr {
205
    ElfWord    sh_name;//Index in a header section table (gives name of section)
206
    ElfWord    sh_type;
207
        ElfWord sh_flags;       /* SHF_... */
208
        ElfAddr32       sh_addr;
209
        ElfOff32        sh_offset;
210
        ElfWord sh_size;
211
    ElfWord    sh_link;
212
    ElfWord    sh_info;
213
    ElfWord   sh_addralign;
214
    ElfWord   sh_entsize;
215
};
216
 
217
struct Elf64_Shdr {
218
    ElfWord    sh_name;//Index in a header section table (gives name of section)
219
    ElfWord    sh_type;
220
    ElfDWord   sh_flags;
221
    ElfAddr64  sh_addr;
222
    ElfOff64   sh_offset;
223
    ElfDWord   sh_size;
224
    ElfWord    sh_link;
225
    ElfWord    sh_info;
226
    ElfDWord   sh_addralign;
227
    ElfDWord   sh_entsize;
228
};
229
 
230
class SectionHeaderType {
231
 public:
232
    SectionHeaderType(uint8_t *img, ElfHeaderType *h) {
233
        if (h->isElf32()) {
234
            Elf32_Shdr *sh = reinterpret_cast<Elf32_Shdr *>(img);
235
            if (h->isElfMsb()) {
236
                sh_name_ = SwapBytes(sh->sh_name);
237
                sh_type_ = SwapBytes(sh->sh_type);
238
                sh_flags_ = SwapBytes(sh->sh_flags);
239
                sh_addr_ = SwapBytes(sh->sh_addr);
240
                sh_offset_ = SwapBytes(sh->sh_offset);
241
                sh_size_ = SwapBytes(sh->sh_size);
242
                sh_entsize_ = SwapBytes(sh->sh_entsize);
243
            } else {
244
                sh_name_ = sh->sh_name;
245
                sh_type_ = sh->sh_type;
246
                sh_flags_ = sh->sh_flags;
247
                sh_addr_ = sh->sh_addr;
248
                sh_offset_ = sh->sh_offset;
249
                sh_size_ = sh->sh_size;
250
                sh_entsize_ = sh->sh_entsize;
251
            }
252
        } else {
253
            Elf64_Shdr *sh = reinterpret_cast<Elf64_Shdr *>(img);
254
            if (h->isElfMsb()) {
255
                sh_name_ = SwapBytes(sh->sh_name);
256
                sh_type_ = SwapBytes(sh->sh_type);
257
                sh_flags_ = SwapBytes(sh->sh_flags);
258
                sh_addr_ = SwapBytes(sh->sh_addr);
259
                sh_offset_ = SwapBytes(sh->sh_offset);
260
                sh_size_ = SwapBytes(sh->sh_size);
261
                sh_entsize_ = SwapBytes(sh->sh_entsize);
262
            } else {
263
                sh_name_ = sh->sh_name;
264
                sh_type_ = sh->sh_type;
265
                sh_flags_ = sh->sh_flags;
266
                sh_addr_ = sh->sh_addr;
267
                sh_offset_ = sh->sh_offset;
268
                sh_size_ = sh->sh_size;
269
                sh_entsize_ = sh->sh_entsize;
270
            }
271
        }
272
    }
273
    virtual ElfWord get_name() { return sh_name_; }
274
    virtual ElfWord get_type() { return sh_type_; }
275
    virtual uint64_t get_offset() { return sh_offset_; }
276
    virtual uint64_t get_size() { return sh_size_; }
277
    virtual uint64_t get_addr() { return sh_addr_; }
278
    virtual uint64_t get_flags() { return sh_flags_; }
279
    virtual uint64_t get_entsize() { return sh_entsize_; }
280
 protected:
281
    ElfWord sh_name_;
282
    ElfWord sh_type_;
283
    uint64_t sh_offset_;
284
    uint64_t sh_size_;
285
    uint64_t sh_addr_;
286
    uint64_t sh_flags_;
287
    uint64_t sh_entsize_;
288
};
289
 
290
 
291
#define ELF32_ST_BIND(i) ((i)>>4)
292
#define ELF32_ST_TYPE(i) ((i)&0xf)
293
#define ELF32_ST_INFO(b,t) (((b)<<4) + ((t)&0xf))
294
 
295
static const unsigned char STB_LOCAL   = 0;
296
static const unsigned char STB_GLOBAL  = 1;
297
static const unsigned char STB_WEAK    = 2;
298
static const unsigned char STB_LOPROC  = 13;
299
static const unsigned char STB_HIPROC  = 15;
300
 
301
static const unsigned char STT_NOTYPE  = 0;
302
static const unsigned char STT_OBJECT  = 1;
303
static const unsigned char STT_FUNC    = 2;
304
static const unsigned char STT_SECTION = 3;
305
static const unsigned char STT_FILE    = 4;
306
static const unsigned char STT_LOPROC  = 13;
307
static const unsigned char STT_HIPROC  = 15;
308
 
309
struct Elf32_Sym {
310
    ElfWord    st_name;
311
        ElfAddr32       st_value;
312
        ElfWord st_size;
313
        unsigned char   st_info;
314
        unsigned char   st_other;
315
        ElfHalf st_shndx;
316
};
317
 
318
struct Elf64_Sym {
319
    ElfWord    st_name;
320
        unsigned char   st_info;
321
        unsigned char   st_other;
322
        ElfHalf st_shndx;
323
        ElfAddr64       st_value;
324
        ElfDWord        st_size;
325
};
326
 
327
class SymbolTableType {
328
 public:
329
    SymbolTableType(uint8_t *img, ElfHeaderType *h) {
330
        if (h->isElf32()) {
331
            Elf32_Sym *st = reinterpret_cast<Elf32_Sym *>(img);
332
            if (h->isElfMsb()) {
333
                st_name_ = SwapBytes(st->st_name);
334
                st_value_ = SwapBytes(st->st_value);
335
                st_size_ = SwapBytes(st->st_size);
336
            } else {
337
                st_name_ = st->st_name;
338
                st_value_ = st->st_value;
339
                st_size_ = st->st_size;
340
            }
341
            st_info_ = st->st_info;
342
        } else {
343
            Elf64_Sym *st = reinterpret_cast<Elf64_Sym *>(img);
344
            if (h->isElfMsb()) {
345
                st_name_ = SwapBytes(st->st_name);
346
                st_value_ = SwapBytes(st->st_value);
347
                st_size_ = SwapBytes(st->st_size);
348
            } else {
349
                st_name_ = st->st_name;
350
                st_value_ = st->st_value;
351
                st_size_ = st->st_size;
352
            }
353
            st_info_ = st->st_info;
354
        }
355
    }
356
    virtual ElfWord get_name() { return st_name_; }
357
    virtual uint64_t get_value() { return st_value_; }
358
    virtual uint64_t get_size() { return st_size_; }
359
    virtual unsigned char get_info() { return st_info_; }
360
 protected:
361
    ElfWord    st_name_;
362
    unsigned char st_info_;
363
    uint64_t st_value_;
364
    uint64_t st_size_;
365
};
366
 
367
 
368
//p_type:
369
static const ElfWord PT_NULL     = 0;
370
static const ElfWord PT_LOAD     = 1;
371
static const ElfWord PT_DYNAMIC  = 2;
372
static const ElfWord PT_INTERP   = 3;
373
static const ElfWord PT_NOTE     = 4;
374
static const ElfWord PT_SHLIB    = 5;
375
static const ElfWord PT_PHDR     = 6;
376
static const ElfWord PT_LOPROC   = 0x70000000;
377
static const ElfWord PT_HIPROC   = 0x7fffffff;
378
 
379
typedef struct ProgramHeaderType64
380
{
381
  uint32_t    p_type;
382
  uint32_t    p_offset;
383
  uint64_t    p_vaddr;
384
  uint64_t    p_paddr;
385
#ifdef ARCH_64BITS
386
    ElfDWord    p_filesz;
387
    ElfDWord    p_memsz;
388
    ElfDWord    p_flags;
389
    ElfDWord    p_align;
390
#else
391
        Elf32_Word      p_filesz;
392
        Elf32_Word      p_memsz;
393
        Elf32_Word      p_flags;
394
        Elf32_Word      p_align;
395
#endif
396
} ProgramHeaderType64;
397
 
398
typedef struct ProgramHeaderType32
399
{
400
  uint32_t    p_type;
401
  uint32_t    p_offset;
402
  ElfAddr32    p_vaddr;
403
  ElfAddr32    p_paddr;
404
  ElfWord       p_filesz;
405
  ElfWord       p_memsz;
406
  ElfWord       p_flags;
407
  ElfWord       p_align;
408
} ProgramHeaderType32;
409
 
410
}  // namespace debugger
411
 
412
#endif  // __DEBUGGER_ELF_TYPES_H__

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.