1 /****************************************************************************
2 *
3 *                            Open Watcom Project
4 *
5 *    Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved.
6 *
7 *  ========================================================================
8 *
9 *    This file contains Original Code and/or Modifications of Original
10 *    Code as defined in and that are subject to the Sybase Open Watcom
11 *    Public License version 1.0 (the 'License'). You may not use this file
12 *    except in compliance with the License. BY USING THIS FILE YOU AGREE TO
13 *    ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is
14 *    provided with the Original Code and Modifications, and is also
15 *    available at www.sybase.com/developer/opensource.
16 *
17 *    The Original Code and all software distributed under the License are
18 *    distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
19 *    EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM
20 *    ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF
21 *    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR
22 *    NON-INFRINGEMENT. Please see the License for the specific language
23 *    governing rights and limitations under the License.
24 *
25 *  ========================================================================
26 *
27 * Description:  Executable and Linkable Format (ELF) definitions.
28 *
29 ****************************************************************************/
30 
31 
32 #ifndef _ELFSPEC_H_INCLUDED_
33 #define _ELFSPEC_H_INCLUDED_
34 
35 // the main header
36 
37 #define EI_NIDENT       16
38 
39 typedef struct {
40     uint_8   e_ident[EI_NIDENT];     // +0 signature & ID info
41     uint_16  e_type;                 // +16 file type (i.e. obj file, exe file)
42     uint_16  e_machine;              // +18 required architecture
43     uint_32  e_version;              // +20 version of the file
44     uint_32  e_entry;                // +24 program entry point
45     uint_32  e_phoff;                // +28 program header offset
46     uint_32  e_shoff;                // +32 section header offset
47     uint_32  e_flags;                // +36 processor specific flags
48     uint_16  e_ehsize;               // +40 elf header size
49     uint_16  e_phentsize;            // +42 program header entry size
50     uint_16  e_phnum;                // +44 number of program header entries
51     uint_16  e_shentsize;            // +46 section header entry size
52     uint_16  e_shnum;                // +48 number of section header entries
53     uint_16  e_shstrndx;             // +50 section name string table index.
54 } Elf32_Ehdr;
55 
56 typedef struct {
57     uint_8   e_ident[EI_NIDENT];     // +0 signature & ID info
58     uint_16  e_type;                 // +16 file type (i.e. obj file, exe file)
59     uint_16  e_machine;              // +18 required architecture
60     uint_32  e_version;              // +20 version of the file
61     uint_64  e_entry;                // +24 program entry point
62     uint_64  e_phoff;                // +32 program header offset
63     uint_64  e_shoff;                // +40 section header offset
64     uint_32  e_flags;                // +48 processor specific flags
65     uint_16  e_ehsize;               // +52 elf header size
66     uint_16  e_phentsize;            // +54 program header entry size
67     uint_16  e_phnum;                // +56 number of program header entries
68     uint_16  e_shentsize;            // +58 section header entry size
69     uint_16  e_shnum;                // +60 number of section header entries
70     uint_16  e_shstrndx;             // +62 section name string table index.
71 } Elf64_Ehdr;
72 
73 // e_ident field indicies
74 
75 #define ELF_SIGNATURE   "\177ELF"
76 #define ELF_SIGNATURE_LEN 4
77 #define ELFMAG0         '\177'  // bytes of signature
78 #define ELFMAG1         'E'     // bytes of signature
79 #define ELFMAG2         'L'     // bytes of signature
80 #define ELFMAG3         'F'     // bytes of signature
81 #define EI_MAG0         0       // signature bytes
82 #define EI_MAG1         1       // signature bytes
83 #define EI_MAG2         2       // signature bytes
84 #define EI_MAG3         3       // signature bytes
85 #define EI_CLASS        4       // "file class", i.e. 32-bit vs. 64-bit
86 #define EI_DATA         5       // data encoding (big-endian vs. little-endian)
87 #define EI_VERSION      6       // header version #
88 #define EI_OSABI        7       // OS/ABI identification
89 #define EI_ABIVERSION   8       // ABI version
90 #define EI_PAD          9       // start of padding bytes
91 
92 // contents of the EI_CLASS field index
93 
94 #define ELFCLASSNONE    0       // invalid class
95 #define ELFCLASS32      1       // 32-bit objects
96 #define ELFCLASS64      2       // 64-bit objects
97 
98 // contents of the EI_DATA field index
99 
100 #define ELFDATANONE     0       // invalid data encoding
101 #define ELFDATA2LSB     1       // "little-endian"
102 #define ELFDATA2MSB     2       // "big-endian"
103 
104 // the current elf version number (EI_VERSION)
105 
106 #define EV_CURRENT      1
107 
108 
109 // contents of the EI_OSABI field index
110 
111 #define ELFOSABI_NONE       0   // No extensions or unspecified
112 #define ELFOSABI_HPUX       1   // Hewlett-Packard HP-UX
113 #define ELFOSABI_NETBSD     2   // NetBSD
114 #define ELFOSABI_LINUX      3   // Linux
115 #define ELFOSABI_SOLARIS    6   // Sun Solaris
116 #define ELFOSABI_AIX        7   // IBM AIX
117 #define ELFOSABI_IRIX       8   // SGI IRIX
118 #define ELFOSABI_FREEBSD    9   // FreeBSD
119 #define ELFOSABI_TRU64      10  // Compaq TRU64 UNIX
120 #define ELFOSABI_MODESTO    11  // Novell Modesto
121 #define ELFOSABI_OPENBSD    12  // Open BSD
122 #define ELFOSABI_OPENVMS    13  // Open VMS
123 #define ELFOSABI_NSK        14  // Hewlett-Packard Non-Stop Kernel
124 #define ELFOSABI_AROS       15  // Amiga Research OS
125 
126 // elf object file types
127 
128 #define ET_NONE         0       // no file type
129 #define ET_REL          1       // relocatable file
130 #define ET_EXEC         2       // executable file
131 #define ET_DYN          3       // shared object file
132 #define ET_CORE         4       // core file
133 #define ET_LOPROC       0xff00  // processor specific file types
134 #define ET_HIPROC       0xffff
135 
136 // elf machine types
137 
138 #define EM_NONE         0       // No machine
139 #define EM_386          3       // Intel 80386
140 #define EM_IA_64        50      // Intel IA-64 processor architecture
141 #define EM_X86_64       62      // AMD x86-64 architecture
142 
143 // version number info
144 
145 #define EV_NONE         0
146 #define EV_CURRENT      1
147 #define EV_WPIBM        2       // version identifier for extended ELF
148 
149 // flags (machine specific)
150 //EM_M32
151 #define EF_M32_MAU      0x1
152 
153 // special section indicies
154 
155 #define SHN_UNDEF       0
156 #define SHN_LORESERVE   0xff00
157 #define SHN_LOPROC      0xff00  // reserved for processor-specific semantics
158 #define SHN_HIPROC      0xff1f
159 #define SHN_ABS         0xfff1  // references to this section are absolute
160 #define SHN_COMMON      0xfff2  // references to this section are common.
161 #define SHN_HIRESERVE   0xffff
162 
163 // section header
164 
165 typedef struct {
166     uint_32  sh_name;        // +0 name of the section
167     uint_32  sh_type;        // +4 section type
168     uint_32  sh_flags;       // +8
169     uint_32  sh_addr;        // +12 starting address of section in image
170     uint_32  sh_offset;      // +16 start of section in file
171     uint_32  sh_size;        // +20 size of section (in file if type != SHT_NOBITS)
172     uint_32  sh_link;        // +24 multipurpose field   (based on type)
173     uint_32  sh_info;        // +28 another multipurpose field (based on type)
174     uint_32  sh_addralign;   // +32 address alignment
175     uint_32  sh_entsize;     // +36 entry size for sects with fixed sized entries
176 } Elf32_Shdr;
177 
178 typedef struct {
179     uint_32  sh_name;        // +0 name of the section
180     uint_32  sh_type;        // +4 section type
181     uint_64  sh_flags;       // +8
182     uint_64  sh_addr;        // +16 starting address of section in image
183     uint_64  sh_offset;      // +24 start of section in file
184     uint_64  sh_size;        // +32 size of section (in file if type != SHT_NOBITS)
185     uint_32  sh_link;        // +40 multipurpose field   (based on type)
186     uint_32  sh_info;        // +44 another multipurpose field (based on type)
187     uint_64  sh_addralign;   // +48 address alignment
188     uint_64  sh_entsize;     // +56 entry size for sects with fixed sized entries
189 } Elf64_Shdr;
190 
191 // section types
192 
193 #define SHT_NULL        0               // inactive
194 #define SHT_PROGBITS    1               // meaning defined by program
195 #define SHT_SYMTAB      2               // symbol table
196 #define SHT_STRTAB      3               // string table
197 #define SHT_RELA        4               // reloc entries with explicit addends
198 #define SHT_HASH        5               // symbol hash table
199 #define SHT_DYNAMIC     6               // dynamic linking information
200 #define SHT_NOTE        7               // comment information
201 #define SHT_NOBITS      8               // like PROGBITS but no space in file.
202 #define SHT_REL         9               // as RELA but no explicit addends
203 #define SHT_SHLIB       10              // reserved but evil
204 #define SHT_DYNSYM      11              // dynamic link symbol table
205 
206 #define SHT_LOOS        0x60000000      /* reserved for environment specific use */
207 #define SHT_OS          0x60000001      // info to identify target OS
208 #define SHT_IMPORTS     0x60000002      // info on refs to external symbols
209 #define SHT_EXPORTS     0x60000003      // info on symbols exported by ordinal
210 #define SHT_RES         0x60000004      // read-only resource data.
211 #define SHT_PROGFRAGS   0x60001001      // similar to SHT_PROGBITS
212 #define SHT_IDMDLL      0x60001002      // symbol name demangling information
213 #define SHT_DEFLIB      0x60001003      // default static libraries
214 #define SHT_HIOS        0x6fffffff
215 
216 #define SHT_LOPROC      0x70000000      // processor specific
217 #define SHT_X86_64_UNWIND 0x70000001    // contains entries for stack unwinding
218 #define SHT_HIPROC      0x7fffffff
219 #define SHT_LOUSER      0x80000000      // user defined sections
220 #define SHT_HIUSER      0xffffffff
221 
222 // Old section types.  Readers should handle these, writers must use the above
223 
224 #define SHT_OS_O        12              // info to identify target OS
225 #define SHT_IMPORTS_O   13              // info on refs to external symbols
226 #define SHT_EXPORTS_O   14              // info on symbols exported by ordinal
227 #define SHT_RES_O       15              // read-only resource data.
228 
229 /* sh_flags values */
230 
231 #define SHF_WRITE       0x00000001      // 0 section writable during execution
232 #define SHF_ALLOC       0x00000002      // 1 section occupies space during exec.
233 #define SHF_EXECINSTR   0x00000004      // 2 section contains code.
234 #define SHF_MERGE       0x00000010      // 4 section can be merged
235 #define SHF_STRINGS     0x00000020      // 5 section contains asciiz strings
236 #define SHF_BEGIN       0x01000000      // 24 section to be placed at the beginning
237                                         // of like-named sections by static link
238 #define SHF_END         0x02000000      // 25 same, end.
239 #define SHF_MASKPROC    0xf0000000      // processor specific flags
240 
241 #define SHF_X86_64_LARGE 0x1000000      // section with more than 2GB - value may be wrong!!!
242 #define SHF_ALPHA_GPREL 0x10000000
243 
244 /* symbol table entry */
245 
246 typedef struct {
247     uint_32  st_name;        // symbol name index into string table
248     uint_32  st_value;       // symbol "value"
249     uint_32  st_size;        // symbol size
250     uint_8   st_info;        // symbol's type and binding attribs.
251     uint_8   st_other;       // no meaning yet.
252     uint_16  st_shndx;       // section index
253 } Elf32_Sym;
254 
255 typedef struct {
256     uint_32  st_name;        // symbol name index into string table
257     uint_8   st_info;        // symbol's type and binding attribs.
258     uint_8   st_other;       // no meaning yet.
259     uint_16  st_shndx;       // section index
260     uint_64  st_value;       // symbol "value"
261     uint_64  st_size;        // symbol size
262 } Elf64_Sym;
263 
264 // symbol info field contents
265 
266 #define ELF32_ST_BIND(i)        ((i)>>4)        // get the "bind" subfield
267 #define ELF32_ST_TYPE(i)        ((i)&0xf)       // get the type subfield
268 #define ELF32_ST_INFO(b,t)      (((b)<<4)+((t)&0xf)) // make a new st_info
269 
270 // the macros for 64bits are a guess only
271 #define ELF64_ST_BIND(i)        ((i)>>4)        // get the "bind" subfield
272 #define ELF64_ST_TYPE(i)        ((i)&0xf)       // get the type subfield
273 #define ELF64_ST_INFO(b,t)      (((b)<<4)+((t)&0xf)) // make a new st_info
274 
275 // bind subfield contents
276 
277 #define STB_LOCAL       0       // symbol has local binding
278 #define STB_GLOBAL      1       // symbol has global binding
279 #define STB_WEAK        2       // symbol has weak binding
280 #define STB_ENTRY       12      // symbol is entry-point for the load module
281 #define STB_LOPROC      13      // processor specific semantics
282 #define STB_HIPROC      15
283 
284 // type subfield contents
285 
286 #define STT_NOTYPE      0       // not specified
287 #define STT_OBJECT      1       // symbol is a data object
288 #define STT_FUNC        2       // symbol is a code symbol
289 #define STT_SECTION     3       // symbol associated with a section
290 #define STT_FILE        4       // symbol gives name of the source file.
291 #define STT_COMMON      5       // symbol is common
292 #define STT_IMPORT      11      // reference to a symbol in another module
293 #define STT_LOPROC      13      // processor specific semantics
294 #define STT_HIPROC      15
295 
296 // relocation entries
297 
298 typedef struct {
299     uint_32  r_offset;       // place to apply reloc (from begin of section)
300     uint_32  r_info;         // symbol idx, and type of reloc
301 } Elf32_Rel;
302 
303 typedef struct {
304     uint_32  r_offset;       // place to apply reloc (from begin of section)
305     uint_32  r_info;         // symbol idx, and type of reloc
306     int_32   r_addend;       // value used as a basis for the reloc.
307 } Elf32_Rela;
308 
309 typedef struct {
310     uint_64  r_offset;       // place to apply reloc (from begin of section)
311     uint_64  r_info;         // symbol idx, and type of reloc
312 } Elf64_Rel;
313 
314 typedef struct {
315     uint_64  r_offset;       // place to apply reloc (from begin of section)
316     uint_64  r_info;         // symbol idx, and type of reloc
317     int_64   r_addend;       // value used as a basis for the reloc.
318 } Elf64_Rela;
319 
320 // r_info field contents
321 
322 #define ELF32_R_SYM(i)  ((i)>>8)                // gets the symbol index
323 #define ELF32_R_TYPE(i) ((uint_8)(i))           // gets the symbol type
324 #define ELF32_R_INFO(s,t) (((s)<<8)+(uint_8)(t))    // make a new r_info
325 
326 #define ELF64_R_SYM(i)  ((i)>>32)               // gets the symbol index
327 #define ELF64_R_TYPE(i) ((i)&0xffffffffL)       // gets the symbol type
328 #define ELF64_R_INFO(s,t) ((((uint_64)s)<<32)+((t)&0xffffffffL)) // make a new r_info
329 
330 // relocation types.
331 //386
332 enum elf32_relocations {
333  R_386_NONE      =  0,
334  R_386_32        =  1, /* direct,       S + A     */
335  R_386_PC32      =  2, /* PC-relative,  S + A - P */
336  R_386_GOT32     =  3, /* GOT entry,    G + A     */
337  R_386_PLT32     =  4, /* PLT entry,    L + A - P */
338  R_386_COPY      =  5,
339  R_386_GLOB_DAT  =  6, /* create GOT entry, S */
340  R_386_JMP_SLOT  =  7, /* create PLT entry, S */
341  R_386_RELATIVE  =  8, /* rel. to program base, B + A */
342  R_386_GOTOFF    =  9, /* offset to GOT, S + A - GOT */
343  R_386_GOTPC     = 10, /* GOT + A - P */
344  R_386_32PLT     = 11, /* L + A       */
345 /* GNU extensions for LD */
346  R_386_16        = 20, /* 16-bit direct,      S + A     */
347  R_386_PC16      = 21, /* 16-bit PC-relative, S + A - P */
348  R_386_8         = 22, /* 8-bit direct,       S + A     */
349  R_386_PC8       = 23  /* 8-bit PC-relative,  S + A - P */
350 };
351 
352 //X86_64
353 enum elf64_relocations {
354  R_X86_64_NONE        =   0,
355  R_X86_64_64          =   1,    /* S + A     */
356  R_X86_64_PC32        =   2,    /* S + A - P */
357  R_X86_64_GOT32       =   3,    /* G + A     */
358  R_X86_64_PLT32       =   4,    /* L + A - P */
359  R_X86_64_COPY        =   5,    /*           */
360  R_X86_64_GLOB_DAT    =   6,    /* S         */
361  R_X86_64_JUMP_SLOT   =   7,    /* S         */
362  R_X86_64_RELATIVE    =   8,    /* B + A     */
363  R_X86_64_GOTPCREL    =   9,    /* G + GOT + A - P */
364  R_X86_64_32          =  10,    /* S + A     */
365  R_X86_64_32S         =  11,    /* S + A     */
366  R_X86_64_16          =  12,    /* S + A     */
367  R_X86_64_PC16        =  13,    /* S + A - P */
368  R_X86_64_8           =  14,    /* S + A     */
369  R_X86_64_PC8         =  15,    /* S + A - P */
370  R_X86_64_DPTMOD64    =  16,
371  R_X86_64_DTPOFF64    =  17,
372  R_X86_64_TPOFF64     =  18,
373  R_X86_64_TLSGD       =  19,
374  R_X86_64_TLSLD       =  20,
375  R_X86_64_DTPOFF32    =  21,
376  R_X86_64_GOTTPOFF    =  22,
377  R_X86_64_TPOFF32     =  23,
378  R_X86_64_PC64        =  24,    /* S + A - P   */
379  R_X86_64_GOTOFF64    =  25,    /* S + A - GOT */
380  R_X86_64_GOTPC32     =  26,    /* GOT + A - P */
381  R_X86_64_SIZE32      =  32,
382  R_X86_64_SIZE64      =  33
383 };
384 
385 // program header
386 
387 typedef struct {
388     uint_32  p_type;         // type of segment
389     uint_32  p_offset;       // offset of segment from beginnning of file
390     uint_32  p_vaddr;        // segment virtual address
391     uint_32  p_paddr;        // segment physical address
392     uint_32  p_filesz;       // size of segment in file
393     uint_32  p_memsz;        // size of segment in memory
394     uint_32  p_flags;
395     uint_32  p_align;        // segment align value (in mem & file)
396 } Elf32_Phdr;
397 
398 // segment types
399 
400 #define PT_NULL         0               // unused segment
401 #define PT_LOAD         1               // loadable segment
402 #define PT_DYNAMIC      2               // contains dynamic linking information
403 #define PT_INTERP       3               // reference to a program interpreter
404 #define PT_NOTE         4               // comments & auxiliary information
405 #define PT_SHLIB        5               // here be dragons
406 #define PT_PHDR         6               // address of prog. header in mem (for interp.)
407 #define PT_OS           0x60000001      // target os information
408 #define PT_RES          0x60000002      // read-only resource information
409 #define PT_LOPROC       0x70000000      // processor specific
410 #define PT_HIPROC       0x7fffffff
411 
412 // Old segment types.  Readers should handle these, writers must use the above
413 
414 #define PT_OS_O         7       // target os information
415 #define PT_RES_O        9       // read-only resource information
416 
417 // note entry format
418 
419 typedef struct {
420     uint_32       n_namesz; // length of name
421     uint_32       n_descsz; // length of descriptor
422     uint_32       n_type;   // user defined "type" of the note
423     //char        name[];   // variable length name
424     //uint_32     desc[];   // descriptors go here
425 } Elf_Note;
426 
427 // note types (used in core files)
428 
429 #define NT_PRSTATUS     1       // process status
430 #define NT_FPREGSET     2       // floating point registers
431 #define NT_PRPSINFO     3       // process state info
432 
433 // dynamic segment entry information.
434 
435 typedef struct {
436     int_32           d_tag;
437     union {
438         uint_32      d_val;
439         uint_32      d_ptr;
440     } d_un;
441 } Elf32_Dyn;
442 
443 // dynamic array tags
444 
445 #define DT_NULL         0
446 #define DT_NEEDED       1               // name of a needed library
447 #define DT_PLTRELSZ     2               // size of reloc entries for PLT
448 #define DT_PLTGOT       3               // address with PLT or GOT
449 #define DT_HASH         4               // symbol hash table address
450 #define DT_STRTAB       5               // string table address
451 #define DT_SYMTAB       6               // symbol table address
452 #define DT_RELA         7               // address of reloc table with addends
453 #define DT_RELASZ       8               // size of the DT_RELA table
454 #define DT_RELAENT      9               // size of a DT_RELA entry
455 #define DT_STRSZ        10              // size of the string table
456 #define DT_SYMENT       11              // size of a symbol table entry
457 #define DT_SONAME       14              // shared object name
458 #define DT_REL          17              // address of reloc table without addends
459 #define DT_RELSZ        18              // size of the DT_REL table
460 #define DT_RELENT       19              // size of a DT_REL entry
461 #define DT_PLTREL       20              // type of reloc entry for PLT
462 #define DT_DEBUG        21              // for debugging information
463 #define DT_JMPREL       23              // reloc entries only with PLT
464 #define DT_EXPORT       0x60000001      // address of export table
465 #define DT_EXPORTSZ     0x60000002      // size of export table
466 #define DT_EXPENT       0x60000003      // size of export table entry
467 #define DT_IMPORT       0x60000004      // address of import table
468 #define DT_IMPORTSZ     0x60000005      // size of import table
469 #define DT_IMPENT       0x60000006      // size of import table entry
470 #define DT_IT           0x60000007      // init and term types for a DLL.
471 #define DT_ITPRTY       0x60000008      // relative priority of init and term to other functions
472 #define DT_INITTERM     0x60000009      // address of init and term function
473 #define DT_PPC_GOT      0x70000001      // address of Global Offset Table
474 #define DT_PPC_GOTSZ    0x70000002      // size of Global Offset Table
475 #define DT_PPC_PLTSZ    0x70000003      // size of Procedure Linkage Table
476 #define DT_LOPROC       0x70000000      // range of processor-defined tags
477 #define DT_HIPROC       0x7FFFFFFF
478 
479 // Old dynamic tags.  Readers should handle these, writers must use the above
480 
481 #define DT_INIT_O       12      // address of initialization function
482 #define DT_FINI_O       13      // address of finialization function
483 #define DT_RPATH_O      15      // library search path
484 #define DT_SYMBOLIC_O   16      // affects dyn. linker's sym. resolution
485 #define DT_TEXTREL_O    22      // signal we might mod. a non-writable segment
486 #define DT_IT_O         24      // init and term types for a DLL.
487 #define DT_EXPORT_O     25      // address of export table
488 #define DT_EXPORTSZ_O   26      // size of export table
489 #define DT_IMPORT_O     27      // address of import table
490 #define DT_IMPORTSZ_O   28      // size of import table
491 #define DT_GOT_O        29      // address of Global Offset Table
492 #define DT_GOTSZ_O      30      // size of Global Offset Table
493 #define DT_PLTSZ_O      32      // size of Procedure Linkage Table
494 #define DT_ITPRTY_O     33      // relative priority of init and term to other functions
495 #define DT_LOUSER_O     0x60000000      // range of user-definable tags. will not
496 #define DT_HIUSER_O     0x6FFFFFFF      // conflict with system-defined tags
497                                         // Ha Ha Ha!
498 
499 // description of DT_IT tag:
500 // Describe type for initalization and termination of DLL
501 // Required if DT_INIT and DT_FINI also specified
502 
503 #define ELF_32_IT_INIT(it)      ((it) & 0x0f)
504 #define ELF_32_IT_TERM(it)      (((it) >> 4) & 0x0f)
505 #define ELF_32_IT_INFO(i,t)     (((i) & 0x0f)|(((t) & 0x0f) << 4))
506 
507 #define IT_NONE         0       // no initialization or termination
508 #define IT_GLOBAL       1       // global init, term
509 #define IT_INSTANCE     2       // process init, term
510 #define IT_THREAD       3       // thread init, term
511 
512 // DT_INITTERM function prototype
513 
514 typedef unsigned long INITTERM ( unsigned long modhandle, unsigned long flag );
515 
516 // elf segment flag bits
517 
518 #define PF_X            0x1             // seg has execute permissions
519 #define PF_W            0x2             // seg has write permissions
520 #define PF_R            0x4             // seg has read permissions
521 #define PF_S            0x01000000      // segment is shared.
522 #define PF_MASKPROC     0xf0000000      // processor-specific flag mask
523 
524 // operating system information
525 
526 typedef struct {
527     uint_32  os_type;
528     uint_32  os_size;
529 } Elf32_Os;
530 
531 #define EOS_NONE        0       // bad or unknown
532 #define EOS_PN          1       // IBM Microkernel personality neutral
533 #define EOS_SVR4        2       // UNIX System V Release 4
534 #define EOS_AIX         3       // IBM AIX
535 #define EOS_OS2         4       // IBM OS/2, 32 bit
536 #define EOS_NT          5       // Microsoft Windows NT, 32 bit
537 #define EOS_VMS         6       // DEC VMS/VAX
538 #define EOS_OS400       7       // IBM OS/400
539 #define EOS_NEXT        8       // NEXT
540 #define EOS_SYSTEM7     9       // Apple System 7
541 
542 // OS/2-specific information
543 
544 typedef struct {
545     unsigned char       os2_sessiontype;
546     unsigned char       os2_sessionflags;
547     unsigned char       os2_reserved[14];
548 } Elf32_OS2Info;
549 
550 // os2_sessiontype values
551 
552 #define OS2_SES_NONE    0       // no session type.  Only valid for DLL's
553 #define OS2_SES_FS      1       // Full Screen session.
554 #define OS2_SES_PM      2       // Presentation Manager session.
555 #define OS2_SES_VIO     3       // Windowed (character-mode) session
556 
557 // import table entry
558 
559 typedef struct {
560     uint_32  imp_ordinal;
561     uint_32  imp_name;
562     uint_32  imp_info;
563     uint_32  imp_reserved;
564 } Elf32_Import;
565 
566 #define ELF32_IMP_TYPE(i)       ((i) >> 24)
567 #define ELF32_IMP_DLL(i)        ((i) & 0x00FFFFFF)
568 #define ELF32_IMP_INFO(t,d)     (((t)<<24) | ((d) & 0x00FFFFFF)))
569 
570 #define IMP_IGNORED     0       // This import entry to be ignored
571 #define IMP_STR_IDX     1       // Value is string table index to load module
572 #define IMP_DT_IDX      2       // Value is ref to DT_NEEDED in Dynamic Segment
573 
574 // export table entry
575 
576 typedef struct {
577     uint_32  exp_ordinal;
578     uint_32  exp_symbol;
579     uint_32  exp_name;
580     uint_32  exp_reserved;
581 } Elf32_Export;
582 
583 // Resource header
584 
585 #define RH_NIDENT       16
586 
587 typedef struct {
588     unsigned char    rh_ident[RH_NIDENT];
589     uint_32          rh_name;
590     uint_32          rh_itnum;
591     uint_32          rh_rhsize;
592     uint_32          rh_size;
593     uint_32          rh_strtab;
594 } Elf32_Rhdr;
595 
596 // rh_ident field indices
597 
598 #define ELFRESMAG0      '\002'  // bytes of signature
599 #define ELFRESMAG1      'R'     // bytes of signature
600 #define ELFRESMAG2      'E'     // bytes of signature
601 #define ELFRESMAG3      'S'     // bytes of signature
602 #define RH_MAG0         0       // signature bytes
603 #define RH_MAG1         1       // signature bytes
604 #define RH_MAG2         2       // signature bytes
605 #define RH_MAG3         3       // signature bytes
606 #define RH_CLASS        4       // class of resource collection
607 #define RH_DATA         5       // data encoding of resource collection
608 #define RH_VERSION      6       // version
609 #define RH_PAD          7       // start of padding bytes - set to 0
610 
611 // contents of RH_CLASS field
612 
613 #define RESCLASSNONE    0       // invalid class
614 #define RESCLASS32      2       // 32-bit architecture
615 #define RESCLASS64      3       // reserved for 64-bit architecture
616 
617 // contents of RH_DATA field
618 
619 #define RESDATANONE     0       // invalid data
620 #define RESDATA2LSB     1       // Little Endian data encoding
621 #define RESDATA2MSB     2       // Bit Endian data encoding
622 
623 // contents of RH_VERSION field
624 
625 #define RV_NONE         0       // invalid version
626 #define RV_CURRENT      1       // current version.  will change in future.
627 
628 // resource item
629 
630 #define RI_NIDENT       4
631 
632 typedef struct {
633     unsigned char       ri_ident[RI_NIDENT];
634     uint_32          ri_type;
635     uint_32          ri_typename;
636     uint_32          ri_ordinal;
637     uint_32          ri_ordname;
638     uint_32          ri_data;
639     uint_32          ri_flags;
640     uint_32          ri_size;
641     uint_32          ri_reserved;
642 } Elf32_Ritem;
643 
644 // ri_ident field indices
645 
646 #define RI_VERSION      0       // version
647 #define RI_PAD          1       // start of padding bytes - set to 0
648 
649 // contents of RI_VERSION field
650 
651 #define IV_NONE         0       // invalid version
652 #define IV_CURRENT      1       // current version.  will change in future
653 
654 // demangle information structure
655 
656 typedef struct {
657     uint_32  idm_dllname;
658     uint_32  idm_initparms;
659 } Elf32_Demangle;
660 
661 // default library structure
662 
663 typedef struct {
664     uint_32  lib_name;
665 } Elf32_Library;
666 
667 #endif // _ELFSPEC_H_INCLUDED_
668