File: elf.h

package info (click to toggle)
picotool 2.2.0-a4%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,084 kB
  • sloc: cpp: 61,059; ansic: 2,999; asm: 2,048; perl: 219; sh: 212; python: 97; makefile: 41; xml: 18
file content (126 lines) | stat: -rw-r--r-- 2,573 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#ifndef _ELF_H
#define _ELF_H

#include <stdint.h>

#define ELF_MAGIC 0x464c457fu

#define EM_ARM 0x28u
#define EM_RISCV 0xf3u

#define EF_ARM_ABI_FLOAT_HARD 0x00000400u

#define PT_LOAD 0x00000001u

#define PF_X 0x1u
#define PF_W 0x2u
#define PF_R 0x4u

#define SHT_NULL 0x00000000u
#define SHT_PROGBITS 0x00000001u
#define SHT_NOBITS 0x00000008u
#define SHF_ALLOC 0x00000002u

#ifdef __cplusplus
#include <string>
#include <sstream>
#endif

#pragma pack(push, 1)
struct elf_header {
    uint32_t    magic;
    uint8_t     arch_class;
    uint8_t     endianness;
    uint8_t     version;
    uint8_t     abi;
    uint8_t     abi_version;
    uint8_t     _pad[7];
    uint16_t    type;
    uint16_t    machine;
    uint32_t    version2;
};

struct elf32_header {
    struct elf_header common;
    uint32_t    entry;
    uint32_t    ph_offset;
    uint32_t    sh_offset;
    uint32_t    flags;
    uint16_t    eh_size;
    uint16_t    ph_entry_size;
    uint16_t    ph_num;
    uint16_t    sh_entry_size;
    uint16_t    sh_num;
    uint16_t    sh_str_index;
};

struct elf32_ph_entry {
#ifdef __cplusplus
    uint32_t physical_address(void) const {return paddr;};
    uint32_t physical_size(void) const {return filez;};
    uint32_t virtual_address(void) const {return vaddr;};
    uint32_t virtual_size(void) const {return memsz;};
    bool is_load(void) const {return type == 0x1;};
#endif
    uint32_t type;
    uint32_t offset;
    uint32_t vaddr;
    uint32_t paddr;
    uint32_t filez;
    uint32_t memsz;
    uint32_t flags;
    uint32_t align;
};

#ifdef __cplusplus
typedef elf32_ph_entry Segment;
inline std::string to_string(const elf32_ph_entry &ph)  {
    std::stringstream ss;
    ss << "segment paddr " << std::hex << ph.paddr << " vaddr " << std::hex << ph.vaddr;
    return ss.str();
}
#endif

struct elf32_sh_entry {
#ifdef __cplusplus
    uint32_t virtual_address(void) const {return addr;};
#endif
    uint32_t name;
    uint32_t type;
    uint32_t flags;
    uint32_t addr;
    uint32_t offset;
    uint32_t size;
    uint32_t link;
    uint32_t info;
    uint32_t addralign;
    uint32_t entsize;
};

#ifdef __cplusplus
typedef elf32_sh_entry Section;
inline std::string to_string(const elf32_sh_entry &sh)  {
    std::stringstream ss;
    ss << std::hex << sh.addr;
    return ss.str();
}
#endif

struct elf32_sym_entry {
	uint32_t name;
	uint32_t value;
	uint32_t size;
	uint8_t info;
	uint8_t other;
	uint16_t shndx;
};

#pragma pack(pop)

#endif