1 // Inferno libmach/elf.h
2 // http://code.google.com/p/inferno-os/source/browse/utils/libmach/elf.h
3 //
4 // Copyright © 1994-1999 Lucent Technologies Inc.
5 // Power PC support Copyright © 1995-2004 C H Forsyth (forsyth@terzarima.net).
6 // Portions Copyright © 1997-1999 Vita Nuova Limited.
7 // Portions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com).
8 // Revisions Copyright © 2000-2004 Lucent Technologies Inc. and others.
9 // Portions Copyright © 2009 The Go Authors. All rights reserved.
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining a copy
12 // of this software and associated documentation files (the "Software"), to deal
13 // in the Software without restriction, including without limitation the rights
14 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 // copies of the Software, and to permit persons to whom the Software is
16 // furnished to do so, subject to the following conditions:
17 //
18 // The above copyright notice and this permission notice shall be included in
19 // all copies or substantial portions of the Software.
20 //
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 // THE SOFTWARE.
28
29 /*
30 * Definitions needed for accessing ELF headers.
31 * 32-bit and 64-bit structs differ.
32 */
33 typedef struct {
34 uchar ident[16]; /* ident bytes */
35 ushort type; /* file type */
36 ushort machine; /* target machine */
37 int version; /* file version */
38 uint32 elfentry; /* start address */
39 uint32 phoff; /* phdr file offset */
40 uint32 shoff; /* shdr file offset */
41 int flags; /* file flags */
42 ushort ehsize; /* sizeof ehdr */
43 ushort phentsize; /* sizeof phdr */
44 ushort phnum; /* number phdrs */
45 ushort shentsize; /* sizeof shdr */
46 ushort shnum; /* number shdrs */
47 ushort shstrndx; /* shdr string index */
48 } Ehdr32;
49
50 typedef struct {
51 uchar ident[16]; /* ident bytes */
52 ushort type; /* file type */
53 ushort machine; /* target machine */
54 int version; /* file version */
55 uvlong elfentry; /* start address */
56 uvlong phoff; /* phdr file offset */
57 uvlong shoff; /* shdr file offset */
58 int flags; /* file flags */
59 ushort ehsize; /* sizeof ehdr */
60 ushort phentsize; /* sizeof phdr */
61 ushort phnum; /* number phdrs */
62 ushort shentsize; /* sizeof shdr */
63 ushort shnum; /* number shdrs */
64 ushort shstrndx; /* shdr string index */
65 } Ehdr64;
66
67 typedef struct {
68 int type; /* entry type */
69 uint32 offset; /* file offset */
70 uint32 vaddr; /* virtual address */
71 uint32 paddr; /* physical address */
72 int filesz; /* file size */
73 uint32 memsz; /* memory size */
74 int flags; /* entry flags */
75 int align; /* memory/file alignment */
76 } Phdr32;
77
78 typedef struct {
79 int type; /* entry type */
80 int flags; /* entry flags */
81 uvlong offset; /* file offset */
82 uvlong vaddr; /* virtual address */
83 uvlong paddr; /* physical address */
84 uvlong filesz; /* file size */
85 uvlong memsz; /* memory size */
86 uvlong align; /* memory/file alignment */
87 } Phdr64;
88
89 typedef struct {
90 uint32 name; /* section name */
91 uint32 type; /* SHT_... */
92 uint32 flags; /* SHF_... */
93 uint32 addr; /* virtual address */
94 uint32 offset; /* file offset */
95 uint32 size; /* section size */
96 uint32 link; /* misc info */
97 uint32 info; /* misc info */
98 uint32 addralign; /* memory alignment */
99 uint32 entsize; /* entry size if table */
100 } Shdr32;
101
102 typedef struct {
103 uint32 name; /* section name */
104 uint32 type; /* SHT_... */
105 uvlong flags; /* SHF_... */
106 uvlong addr; /* virtual address */
107 uvlong offset; /* file offset */
108 uvlong size; /* section size */
109 uint32 link; /* misc info */
110 uint32 info; /* misc info */
111 uvlong addralign; /* memory alignment */
112 uvlong entsize; /* entry size if table */
113 } Shdr64;
114
115 enum {
116 /* Ehdr codes */
117 MAG0 = 0, /* ident[] indexes */
118 MAG1 = 1,
119 MAG2 = 2,
120 MAG3 = 3,
121 CLASS = 4,
122 DATA = 5,
123 VERSION = 6,
124
125 ELFCLASSNONE = 0, /* ident[CLASS] */
126 ELFCLASS32 = 1,
127 ELFCLASS64 = 2,
128 ELFCLASSNUM = 3,
129
130 ELFDATANONE = 0, /* ident[DATA] */
131 ELFDATA2LSB = 1,
132 ELFDATA2MSB = 2,
133 ELFDATANUM = 3,
134
135 NOETYPE = 0, /* type */
136 REL = 1,
137 EXEC = 2,
138 DYN = 3,
139 CORE = 4,
140
141 NONE = 0, /* machine */
142 M32 = 1, /* AT&T WE 32100 */
143 SPARC = 2, /* Sun SPARC */
144 I386 = 3, /* Intel 80386 */
145 M68K = 4, /* Motorola 68000 */
146 M88K = 5, /* Motorola 88000 */
147 I486 = 6, /* Intel 80486 */
148 I860 = 7, /* Intel i860 */
149 MIPS = 8, /* Mips R2000 */
150 S370 = 9, /* Amdhal */
151 SPARC64 = 18, /* Sun SPARC v9 */
152 POWER = 20, /* PowerPC */
153 ARM = 40, /* ARM */
154 AMD64 = 62, /* Amd64 */
155
156 NO_VERSION = 0, /* version, ident[VERSION] */
157 CURRENT = 1,
158
159 /* Phdr Codes */
160 NOPTYPE = 0, /* type */
161 LOAD = 1,
162 DYNAMIC = 2,
163 INTERP = 3,
164 NOTE = 4,
165 SHLIB = 5,
166 PHDR = 6,
167
168 R = 0x4, /* flags */
169 W = 0x2,
170 X = 0x1,
171
172 /* Shdr Codes */
173 Progbits = 1, /* section types */
174 Strtab = 3,
175 Nobits = 8,
176
177 Swrite = 1, /* section attributes */
178 Salloc = 2,
179 Sexec = 4,
180 };
181
182 #define ELF_MAG ((0x7f<<24) | ('E'<<16) | ('L'<<8) | 'F')