aboutsummaryrefslogtreecommitdiffhomepage
path: root/elf.h
blob: 4328e01e1d1a07e3f18b847637a729e463685b2a (plain) (blame)
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
#include "common.h"

#define ELFMAG "\177ELF"
enum {
   ELFCLASS32 = 1,
   ELFCLASS64 = 2,

   ELFDATA2LSB = 1,
   ELFDATA2MSB = 2,

   ELFVERSION = 1,

   ELFOSABI_SYSV       = 0,
   ELFOSABI_ARM        = 97,
   ELFOSABI_STANDALONE = 255,

   ET_NONE = 0,
   ET_REL, ET_EXEC, ET_DYN, ET_CORE,

   EM_NONE         = 0,
   EM_386          = 3,
   EM_486          = 6,
   EM_MIPS         = 8,
   EM_MIPS_RS4_BE  = 0xA,
   EM_ARM          = 0x28,
   EM_X86_64       = 0x3E,
   EM_ARM64        = 0xB7,
};

#define ELF_HDRIDENT          \
   union {                    \
      uchar ident[16];        \
      struct {                \
         uchar  i_mag[4],     \
                i_class,      \
                i_data,       \
                i_version,    \
                i_osabi,      \
                i_abiversion, \
                i_pad[7];     \
      };                      \
   }

struct elf64hdr {
   ELF_HDRIDENT;
   ushort type,
          machine;
   uint   version;
   uvlong entry,
          phoff,
          shoff;
   uint   flags;
   ushort ehsize,
          phentsize,
          phnum,
          shentsize,
          shnum,
          shstrndx;
};
static_assert(sizeof(struct elf64hdr) == 64);

struct elf32hdr {
   ELF_HDRIDENT;
   ushort type,
          machine;
   uint   version;
   uint   entry,
          phoff,
          shoff;
   uint   flags;
   ushort ehsize,
          phentsize,
          phnum,
          shentsize,
          shnum,
          shstrndx;
};
static_assert(sizeof(struct elf32hdr) == 52);

enum {
   SHT_NULL          = 0x0,
   SHT_PROGBITS      = 0x1,
   SHT_SYMTAB        = 0x2,
   SHT_STRTAB        = 0x3,
   SHT_RELA          = 0x4,
   SHT_HASH          = 0x5,
   SHT_DYNAMIC       = 0x6,
   SHT_NOTE          = 0x7,
   SHT_NOBITS        = 0x8,
   SHT_REL           = 0x9,
   SHT_SHLIB         = 0xA,
   SHT_DYNSYM        = 0xB,
   SHT_INIT_ARRAY    = 0xE,
   SHT_FINI_ARRAY    = 0xF,
   SHT_PREINIT_ARRAY = 0x10,
   SHT_GROUP         = 0x12,
   SHT_SYMTAB_SHNDX  = 0x13,
};

enum {
   SHF_WRITE            = 0x1,
   SHF_ALLOC            = 0x2,
   SHF_EXECINSTR        = 0x4,
   SHF_MERGE            = 0x10,
   SHF_STRINGS          = 0x20,
   SHF_INFO_LINK        = 0x40,
   SHF_LINK_ORDER       = 0x80,
   SHF_OS_NONCONFORMING = 0x100,
   SHF_GROUP            = 0x200,
   SHF_TLS              = 0x400,
};

struct elf64shdr {
   uint   name,
          type;
   uvlong flags,
          addr,
          offset,
          size;
   uint   link,
          info;
   uvlong addralign,
          entsize;
};
static_assert(sizeof(struct elf64shdr) == 64);

struct elf32shdr {
   uint name,
        type,
        flags,
        addr,
        offset,
        size,
        link,
        info,
        addralign,
        entsize;
};
static_assert(sizeof(struct elf32shdr) == 40);

enum {
   STB_LOCAL,
   STB_GLOBAL,
   STB_WEAK
};

enum {
   STT_NOTYPE,
   STT_OBJECT,
   STT_FUNC,
   STT_SECTION,
   STT_FILE,
};

enum {
   SHN_UND = 0,
   SHN_ABS = 0xFFF1,
};

#define ELF_S_INFO(b,t) ((b) << 4 | (t))

struct elf64sym {
   uint   name;
   uchar  info,
          other;
   ushort shndx;
   uvlong value,
          size;
};
static_assert(sizeof(struct elf64sym) == 24);

struct elf32sym {
   uint   name,
          value,
          size;
   uchar  info,
          other;
   ushort shndx;
};
static_assert(sizeof(struct elf32sym) == 16);

#define ELF64_R_INFO(s,t) ((uvlong) (s) << 32 | (uint)(t))
struct elf64rel {
   uvlong offset, info;
};
static_assert(sizeof(struct elf64rel) == 16);

#define ELF32_R_INFO(s,t) ((s) << 8 | (uchar)(t))
struct elf32rel {
   uint offset, info;
};
static_assert(sizeof(struct elf32rel) == 8);

struct elf64rela {
   uvlong offset, info;
   vlong addend;
};
static_assert(sizeof(struct elf64rela) == 24);

struct elf32rela {
   uint offset, info;
   int addend;
};
static_assert(sizeof(struct elf32rela) == 12);

/* vim:set ts=3 sw=3 expandtab: */