xref: /netbsd-src/external/bsd/elftosb/dist/common/ELF.h (revision 993229b6fea628ff8b1fa09146c80b0cfb2768eb)
1 /*
2  * File:	ELF.h
3  *
4  * Copyright (c) Freescale Semiconductor, Inc. All rights reserved.
5  * See included license file for license details.
6  */
7 #if !defined(_ELF_h_)
8 #define _ELF_h_
9 
10 //! \name ELF types
11 //! Types used in ELF file structures.
12 //@{
13 typedef uint32_t Elf32_Addr;
14 typedef uint16_t Elf32_Half;
15 typedef /*off_t*/ uint32_t Elf32_Off;
16 typedef int32_t Elf32_Sword;
17 typedef uint32_t Elf32_Word;
18 //@}
19 
20 // All ELF structures are byte aligned. Any alignment padding is explicit.
21 #pragma pack(1)
22 
23 //! \name File header
24 //@{
25 
26 /*!
27  * Constants for the various fields of Elf32_Ehdr.e_ident.
28  */
29 enum {
30 	EI_MAG0 = 0,
31 	EI_MAG1,
32 	EI_MAG2,
33 	EI_MAG3,
34 	EI_CLASS,
35 	EI_DATA,
36 	EI_VERSION,
37 	EI_PAD,
38 	EI_NIDENT = 16,
39 
40 	// Magic number.
41 	ELFMAG0 = 0x7f,
42 	ELFMAG1 = 'E',
43 	ELFMAG2 = 'L',
44 	ELFMAG3 = 'F',
45 
46 	// EI_CLASS
47 	ELFCLASSNONE = 0,
48 	ELFCLASS32 = 1,
49 	ELFCLASS64 = 2,
50 
51 	// EI_DATA
52 	ELFDATANONE = 0,
53 	ELFDATA2LSB = 1,
54 	ELFDATA2MSB = 2
55 };
56 
57 /*!
58  * \brief ELF file header.
59  */
60 struct Elf32_Ehdr
61 {
62 	unsigned char e_ident[EI_NIDENT];	//!< Magic number identifying the file format.
63 	Elf32_Half e_type;		//!< Identifies the object file format.
64 	Elf32_Half e_machine;	//!< Specified the architecture for the object file.
65 	Elf32_Word e_version;	//!< Object file version.
66 	Elf32_Addr e_entry;		//!< Virtual address of the entry point, or 0.
67 	Elf32_Off e_phoff;		//!< Program header table offset in bytes, or 0 if no program header table.
68 	Elf32_Off e_shoff;		//!< Section header table offset in bytes, or 0 if no section header table.
69 	Elf32_Word e_flags;		//!< Processor-specific flags associated with the file.
70 	Elf32_Half e_ehsize;	//!< The ELF header's size in bytes.
71 	Elf32_Half e_phentsize;	//!< Size in bytes of one entry in the program header table.
72 	Elf32_Half e_phnum;		//!< Number of entries in the program header table.
73 	Elf32_Half e_shentsize;	//!< Size in bytes of an entry in the section header table.
74 	Elf32_Half e_shnum;		//!< Number of entries in the section header table.
75 	Elf32_Half e_shstrndx;	//!< Section header table index of the section name string table.
76 };
77 
78 /*!
79  * Constants for #Elf32_Ehdr.e_type.
80  */
81 enum {
82 	ET_NONE,	//!< No file type.
83 	ET_REL,		//!< Relocatable file.
84 	ET_EXEC,	//!< Executable file.
85 	ET_DYN,		//!< Shared object file.
86 	ET_CORE,	//!< Core file.
87 	ET_LOPROC,	//!< Low bound of processor-specific file types.
88 	ET_HIPROC	//!< High bound of processor-specific file types.
89 };
90 
91 /*!
92  * ARM-specific #Elf32_Ehdr.e_flags
93  */
94 enum {
95 	EF_ARM_HASENTRY = 0x02,			//!< #Elf32_Ehdr.e_entry contains a program-loader entry point.
96 	EF_ARM_SYMSARESORTED = 0x04,	//!< Each subsection of the symbol table is sorted by symbol value.
97 	EF_ARM_DYNSYMSUSESEGIDX = 0x08,	//!< Symbols in dynamic symbol tables that are defined in sections included in program segment n have #Elf32_Sym.st_shndx = n + 1.
98 	EF_ARM_MAPSYMSFIRST = 0x10,		//!< Mapping symbols precede other local symbols in the symbol table.
99 	EF_ARM_EABIMASK = 0xff000000,	//!< This masks an 8-bit version number, the version of the ARM EABI to which this ELF file conforms. The current EABI version is #ARM_EABI_VERSION.
100 
101 	ARM_EABI_VERSION = 0x02000000	//!< Current ARM EABI version.
102 };
103 
104 //@}
105 
106 //! \name Sections
107 //@{
108 
109 /*!
110  * \brief ELF section header.
111  *
112  * An object file's section header table lets one locate all the file's
113  * sections. The section header table is an array of #Elf32_Shdr structures.
114  * A section header table index is a subscript into this array. The ELF
115  * header's #Elf32_Ehdr::e_shoff member gives the byte offset from the beginning of
116  * the file to the section header table; #Elf32_Ehdr::e_shnum tells how many entries
117  * the section header table contains; #Elf32_Ehdr::e_shentsize gives the size in bytes
118  * of each entry.
119  *
120  * Some section header table indexes are reserved. An object file will not
121  * have sections for these special indexes:
122  *  - #SHN_UNDEF
123  *  - #SHN_LORESERVE
124  *  - #SHN_LOPROC
125  *  - #SHN_HIPROC
126  *  - #SHN_ABS
127  *  - #SHN_COMMON
128  *  - #SHN_HIRESERVE
129  */
130 struct Elf32_Shdr
131 {
132 	Elf32_Word sh_name;		//!< The section's name. Index into the section header string table section.
133 	Elf32_Word sh_type;		//!< Section type, describing the contents and semantics.
134 	Elf32_Word sh_flags;	//!< Section flags describing various attributes.
135 	Elf32_Addr sh_addr;		//!< The address at which the section will appear in the memory image, or 0.
136 	Elf32_Off sh_offset;	//!< Offset from beginning of the file to the first byte in the section.
137 	Elf32_Word sh_size;		//!< The section's size in bytes.
138 	Elf32_Word sh_link;		//!< Section header table link index. Interpretation depends on section type.
139 	Elf32_Word sh_info;		//!< Extra information about the section. Depends on section type.
140 	Elf32_Word sh_addralign;	//!< Address alignment constraint. Values are 0 and positive powers of 2.
141 	Elf32_Word sh_entsize;	//!< Size in bytes of section entries, or 0 if the section does not have fixed-size entries.
142 };
143 
144 /*!
145  * Special section indexes.
146  */
147 enum {
148 	SHN_UNDEF = 0,
149 	SHN_LORESERVE = 0xff00,
150 	SHN_LOPROC = 0xff00,
151 	SHN_HIPROC = 0xff1f,
152 	SHN_ABS = 0xfff1,		//!< The symbol has an absolute value that will not change because of relocation.
153 	SHN_COMMON = 0xfff2,	//!< The symbol labels a common block that has not yet been allocated.
154 	SHN_HIRESERVE = 0xffff
155 };
156 
157 /*!
158  * Section type constants.
159  */
160 enum {
161 	SHT_NULL = 0,
162 	SHT_PROGBITS = 1,
163 	SHT_SYMTAB = 2,
164 	SHT_STRTAB = 3,
165 	SHT_RELA = 4,
166 	SHT_HASH = 5,
167 	SHT_DYNAMIC = 6,
168 	SHT_NOTE = 7,
169 	SHT_NOBITS = 8,
170 	SHT_REL = 9,
171 	SHT_SHLIB = 10,
172 	SHT_DYNSYM = 11
173 };
174 
175 /*!
176  * Section flag constants.
177  */
178 enum {
179 	SHF_WRITE = 0x1,	//!< Section is writable.
180 	SHF_ALLOC = 0x2,	//!< Allocate section.
181 	SHF_EXECINSTR = 0x4	//!< Section contains executable instructions.
182 };
183 
184 /*!
185  * ARM-specific section flag constants
186  */
187 enum {
188 	SHF_ENTRYSECT = 0x10000000,	//!< The section contains an entry point.
189 	SHF_COMDEF = 0x80000000		//!< The section may be multiply defined in the input to a link step.
190 };
191 
192 #define BSS_SECTION_NAME ".bss"
193 #define DATA_SECTION_NAME ".data"
194 #define TEXT_SECTION_NAME ".text"
195 #define SHSTRTAB_SECTION_NAME ".shstrtab"
196 #define STRTAB_SECTION_NAME ".strtab"
197 #define SYMTAB_SECTION_NAME ".symtab"
198 
199 //@}
200 
201 //! \name Segments
202 //@{
203 
204 /*!
205  * \brief ELF program header.
206  *
207  * An executable or shared object file's program header table is an array of
208  * structures, each describing a segment or other information the system needs
209  * to prepare the program for execution. An object file segment contains one
210  * or more sections. Program headers are meaningful only for executable and
211  * shared object files. A file specifies its own program header size with the
212  * ELF header's #Elf32_Ehdr::e_phentsize and #Elf32_Ehdr::e_phnum members.
213  */
214 struct Elf32_Phdr
215 {
216 	Elf32_Word p_type;		//!< What type of segment this header describes.
217 	Elf32_Off p_offset;		//!< Offset in bytes from start of file to the first byte of the segment.
218 	Elf32_Addr p_vaddr;		//!< Virtual address at which the segment will reside in memory.
219 	Elf32_Addr p_paddr;		//!< Physical address, for systems where this is relevant.
220 	Elf32_Word p_filesz;	//!< Number of bytes of file data the segment consumes. May be zero.
221 	Elf32_Word p_memsz;		//!< Size in bytes of the segment in memory. May be zero.
222 	Elf32_Word p_flags;		//!< Flags relevant to the segment.
223 	Elf32_Word p_align;		//!< Alignment constraint for segment addresses. Possible values are 0 and positive powers of 2.
224 };
225 
226 /*!
227  * Segment type constants.
228  */
229 enum {
230 	PT_NULL = 0,
231 	PT_LOAD = 1,
232 	PT_DYNAMIC = 2,
233 	PT_INTERP = 3,
234 	PT_NOTE = 4,
235 	PT_SHLIB = 5,
236 	PT_PHDR = 6
237 };
238 
239 /*!
240  * Program header flag constants.
241  */
242 enum {
243 	PF_X = 0x1,	//!< Segment is executable.
244 	PF_W = 0x2,	//!< Segment is writable.
245 	PF_R = 0x4	//!< Segment is readable.
246 };
247 
248 //@}
249 
250 //! \name Symbol table
251 //@{
252 
253 enum {
254 	STN_UNDEF = 0	//!< Undefined symbol index.
255 };
256 
257 /*!
258  * \brief ELF symbol table entry.
259  *
260  * An object file's symbol table holds information needed to locate and
261  * relocate a program's symbolic definitions and references. A symbol
262  * table index is a subscript into this array. Index 0 both designates
263  * the first entry in the table and serves as the undefined symbol index.
264  */
265 struct Elf32_Sym
266 {
267 	Elf32_Word st_name;		//!< Index into file's string table.
268 	Elf32_Addr st_value;	//!< Value associated with the symbol. Depends on context.
269 	Elf32_Word st_size;		//!< Size associated with symbol. 0 if the symbol has no size or an unknown size.
270 	unsigned char st_info;	//!< Specified the symbol's type and binding attributes.
271 	unsigned char st_other;	//!< Currently 0 (reserved).
272 	Elf32_Half st_shndx;	//!< Section header table index for this symbol.
273 };
274 
275 //! \name st_info macros
276 //! Macros for manipulating the st_info field of Elf32_Sym struct.
277 //@{
278 #define ELF32_ST_BIND(i) ((i) >> 4)			//!< Get binding attributes.
279 #define ELF32_ST_TYPE(i) ((i) & 0x0f)		//!< Get symbol type.
280 #define ELF32_ST_INFO(b, t) (((b) << 4) + ((t) & 0x0f))	//!< Construct st_info value from binding attributes and symbol type.
281 //@}
282 
283 /*!
284  * \brief Symbol binding attributes.
285  *
286  * These constants are mask values.
287  */
288 enum {
289 	STB_LOCAL = 0,	//!< Local symbol not visible outside the object file.
290 	STB_GLOBAL = 1,	//!< Symbol is visible to all object files being linked together.
291 	STB_WEAK = 2,	//!< Like global symbols, but with lower precedence.
292 
293 	// Processor-specific semantics.
294 	STB_LOPROC = 13,
295 	STB_HIPROC = 15
296 };
297 
298 /*!
299  * \brief Symbol types.
300  */
301 enum {
302 	STT_NOTYPE = 0,		//!< The symbol's type is not specified.
303 	STT_OBJECT = 1,		//!< The symbol is associated with a data object, such as a variable or array.
304 	STT_FUNC = 2,		//!< The symbol is associated with a function or other executable code.
305 	STT_SECTION = 3,	//!< The synmbol is associated with a section. Primarily used for relocation.
306 	STT_FILE = 4,		//!< A file symbol has STB_LOCAL binding, its section index is SHN_ABS, and it precedes the other STB_LOCAL symbols for the file, if it is present.
307 
308 	STT_LOPROC = 13,	//!< Low bound of processor-specific symbol types.
309 	STT_HIPROC = 15		//!< High bound of processor-specific symbol types.
310 };
311 
312 /*!
313  * GHS-specific constants
314  */
315 enum {
316 	STO_THUMB = 1	//!< This flag is set on #Elf32_Sym.st_other if the symbol is Thumb mode code.
317 };
318 
319 #define ARM_SEQUENCE_MAPSYM "$a"
320 #define DATA_SEQUENCE_MAPSYM "$d"
321 #define THUMB_SEQUENCE_MAPSYM "$t"
322 
323 #define THUMB_BL_TAGSYM "$b"
324 #define FN_PTR_CONST_TAGSYM "$f"
325 #define INDIRECT_FN_CALL_TAGSYM "$p"
326 #define MAPPING_SYMBOL_COUNT_TAGSYM "$m"
327 
328 //@}
329 
330 #pragma pack()
331 
332 #endif // _ELF_h_
333