00001 #ifndef _OSX_LOADER_H_ 00002 #define _OSX_LOADER_H_ 00003 00004 #include <sst_stdint.h> 00005 00006 #define PPC_THREAD_STATE_COUNT ((mach_msg_type_number_t) \ 00007 (sizeof(ppc_thread_state_t) / sizeof(int))) 00008 00009 00010 #define PPC_THREAD_STATE 1 00011 #define PPC_FLOAT_STATE 2 00012 #define PPC_EXCEPTION_STATE 3 00013 #define PPC_VECTOR_STATE 4 00014 #define PPC_THREAD_STATE64 5 00015 #define PPC_EXCEPTION_STATE64 6 00016 #define THREAD_STATE_NONE 7 00017 00018 #define S_REGULAR 0x0 /* regular section */ 00019 #define S_ZEROFILL 0x1 /* zero fill on demand section */ 00020 #define S_CSTRING_LITERALS 0x2 /* section with only literal C strings*/ 00021 #define S_4BYTE_LITERALS 0x3 /* section with only 4 byte literals */ 00022 #define S_8BYTE_LITERALS 0x4 /* section with only 8 byte literals */ 00023 #define S_LITERAL_POINTERS 0x5 /* section with only pointers to */ 00024 00025 #define VM_PROT_NONE ((vm_prot_t) 0x00) 00026 #define VM_PROT_READ ((vm_prot_t) 0x01) /* read permission */ 00027 #define VM_PROT_WRITE ((vm_prot_t) 0x02) /* write permission */ 00028 #define VM_PROT_EXECUTE ((vm_prot_t) 0x04) /* execute permission */ 00029 00030 /* Constants for the cmd field of all load commands, the type */ 00031 #define LC_SEGMENT 0x1 /* segment of this file to be mapped */ 00032 #define LC_SYMTAB 0x2 /* link-edit stab symbol table info */ 00033 #define LC_SYMSEG 0x3 /* link-edit gdb symbol table info (obsolete) */ 00034 #define LC_THREAD 0x4 /* thread */ 00035 #define LC_UNIXTHREAD 0x5 /* unix thread (includes a stack) */ 00036 #define LC_LOADFVMLIB 0x6 /* load a specified fixed VM shared library */ 00037 #define LC_IDFVMLIB 0x7 /* fixed VM shared library identification */ 00038 #define LC_IDENT 0x8 /* object identification info (obsolete) */ 00039 #define LC_FVMFILE 0x9 /* fixed VM file inclusion (internal use) */ 00040 #define LC_PREPAGE 0xa /* prepage command (internal use) */ 00041 #define LC_DYSYMTAB 0xb /* dynamic link-edit symbol table info */ 00042 #define LC_LOAD_DYLIB 0xc /* load a dynamically linked shared library */ 00043 #define LC_ID_DYLIB 0xd /* dynamically linked shared lib ident */ 00044 #define LC_LOAD_DYLINKER 0xe /* load a dynamic linker */ 00045 #define LC_ID_DYLINKER 0xf /* dynamic linker identification */ 00046 #define LC_PREBOUND_DYLIB 0x10 /* modules prebound for a dynamically */ 00047 /* linked shared library */ 00048 #define LC_ROUTINES 0x11 /* image routines */ 00049 #define LC_SUB_FRAMEWORK 0x12 /* sub framework */ 00050 #define LC_SUB_UMBRELLA 0x13 /* sub umbrella */ 00051 #define LC_SUB_CLIENT 0x14 /* sub client */ 00052 #define LC_SUB_LIBRARY 0x15 /* sub library */ 00053 #define LC_TWOLEVEL_HINTS 0x16 /* two-level namespace lookup hints */ 00054 #define LC_PREBIND_CKSUM 0x17 /* prebind checksum */ 00055 00056 #define MH_NOUNDEFS 0x1 /* the object file has no undefined 00057 references */ 00058 #define MH_INCRLINK 0x2 /* the object file is the output of an 00059 incremental link against a base file 00060 and can't be link edited again */ 00061 #define MH_DYLDLINK 0x4 /* the object file is input for the 00062 dynamic linker and can't be staticly 00063 link edited again */ 00064 #define MH_BINDATLOAD 0x8 /* the object file's undefined 00065 references are bound by the dynamic 00066 linker when loaded. */ 00067 #define MH_PREBOUND 0x10 /* the file has its dynamic undefined 00068 references prebound. */ 00069 #define MH_SPLIT_SEGS 0x20 /* the file has its read-only and 00070 read-write segments split */ 00071 #define MH_LAZY_INIT 0x40 /* the shared library init routine is 00072 to be run lazily via catching memory 00073 faults to its writeable segments 00074 (obsolete) */ 00075 #define MH_TWOLEVEL 0x80 /* the image is using two-level name 00076 space bindings */ 00077 #define MH_FORCE_FLAT 0x100 /* the executable is forcing all images 00078 to use flat name space bindings */ 00079 #define MH_NOMULTIDEFS 0x200 /* this umbrella guarantees no multiple 00080 defintions of symbols in its 00081 sub-images so the two-level namespace 00082 hints can always be used. */ 00083 #define MH_NOFIXPREBINDING 0x400 /* do not have dyld notify the 00084 prebinding agent about this 00085 executable */ 00086 #define MH_PREBINDABLE 0x800 /* the binary is not prebound but can 00087 have its prebinding redone. only used 00088 when MH_PREBOUND is not set. */ 00089 #define MH_ALLMODSBOUND 0x1000 /* indicates that this binary binds to 00090 all two-level namespace modules of 00091 its dependent libraries. only used 00092 when MH_PREBINDABLE and MH_TWOLEVEL 00093 are both set. */ 00094 #define MH_SUBSECTIONS_VIA_SYMBOLS 0x2000/* safe to divide up the sections into 00095 sub-sections via symbols for dead 00096 code stripping */ 00097 #define MH_CANONICAL 0x4000 /* the binary has been canonicalized 00098 via the unprebind operation */ 00099 #define MH_WEAK_DEFINES 0x8000 /* the final linked image contains 00100 external weak symbols */ 00101 #define MH_BINDS_TO_WEAK 0x10000 /* the final linked image uses 00102 weak symbols */ 00103 #define MH_ALLOW_STACK_EXECUTION 0x20000/* When this bit is set, all stacks 00104 in the task will be given stack 00105 execution privilege. Only used in 00106 MH_EXECUTE filetypes. */ 00107 00108 #define MH_OBJECT 0x1 /* relocatable object file */ 00109 #define MH_EXECUTE 0x2 /* demand paged executable file */ 00110 #define MH_FVMLIB 0x3 /* fixed VM shared library file */ 00111 #define MH_CORE 0x4 /* core file */ 00112 #define MH_PRELOAD 0x5 /* preloaded executable file */ 00113 #define MH_DYLIB 0x6 /* dynamically bound shared library */ 00114 #define MH_DYLINKER 0x7 /* dynamic link editor */ 00115 #define MH_BUNDLE 0x8 /* dynamically bound bundle file */ 00116 #define MH_DYLIB_STUB 0x9 /* shared library stub for static */ 00117 /* linking only, no section contents */ 00118 00119 /* Constants for the flags field of the mach_header */ 00120 #define MH_NOUNDEFS 0x1 /* the object file has no undefined 00121 references */ 00122 #define MH_INCRLINK 0x2 /* the object file is the output of an 00123 incremental link against a base file 00124 and can't be link edited again */ 00125 #define MH_DYLDLINK 0x4 /* the object file is input for the 00126 dynamic linker and can't be staticly 00127 link edited again */ 00128 #define MH_BINDATLOAD 0x8 /* the object file's undefined 00129 references are bound by the dynamic 00130 linker when loaded. */ 00131 #define MH_PREBOUND 0x10 /* the file has its dynamic undefined 00132 references prebound. */ 00133 #define MH_SPLIT_SEGS 0x20 /* the file has its read-only and 00134 read-write segments split */ 00135 #define MH_LAZY_INIT 0x40 /* the shared library init routine is 00136 to be run lazily via catching memory 00137 faults to its writeable segments 00138 (obsolete) */ 00139 #define MH_TWOLEVEL 0x80 /* the image is using two-level name 00140 space bindings */ 00141 #define MH_FORCE_FLAT 0x100 /* the executable is forcing all images 00142 to use flat name space bindings */ 00143 #define MH_NOMULTIDEFS 0x200 /* this umbrella guarantees no multiple 00144 defintions of symbols in its 00145 sub-images so the two-level namespace 00146 hints can always be used. */ 00147 #define MH_NOFIXPREBINDING 0x400 /* do not have dyld notify the 00148 prebinding agent about this 00149 executable */ 00150 #define MH_PREBINDABLE 0x800 /* the binary is not prebound but can 00151 have its prebinding redone. only used 00152 when MH_PREBOUND is not set. */ 00153 #define MH_ALLMODSBOUND 0x1000 /* indicates that this binary binds to 00154 all two-level namespace modules of 00155 its dependent libraries. only used 00156 when MH_PREBINDABLE and MH_TWOLEVEL*/ 00157 #define MH_SUBSECTIONS_VIA_SYMBOLS 0x2000 /* safe to divide up the sections into 00158 sub-sections via symbols for dead 00159 code stripping */ 00160 #define MH_CANONICAL 0x4000 /* the binary has been canonicalized 00161 via the unprebind operation */ 00162 #define MH_WEAK_DEFINES 0x8000 /* the final linked image contains 00163 external weak symbols */ 00164 #define MH_BINDS_TO_WEAK 0x10000 /* the final linked image uses 00165 weak symbols */ 00166 00167 #define MH_ALLOW_STACK_EXECUTION 0x20000/* When this bit is set, all stacks 00168 in the task will be given stack 00169 execution privilege. Only used in 00170 MH_EXECUTE filetypes. */ 00171 00172 /* Constant for the magic field of the mach_header (32-bit architectures) */ 00173 #define MH_MAGIC 0xfeedface /* the mach magic number */ 00174 #define MH_CIGAM 0xcefaedfe /* NXSwapInt(MH_MAGIC) */ 00175 00176 00177 #define CPU_ARCH_MASK 0xff000000 /* mask for architecture bits */ 00178 #define CPU_ARCH_ABI64 0x01000000 /* 64 bit ABI */ 00179 00180 #define CPU_TYPE_ANY ((cpu_type_t) -1) 00181 00182 00183 #define CPU_TYPE_VAX ((cpu_type_t) 1) 00184 /* skip ((cpu_type_t) 2) */ 00185 /* skip ((cpu_type_t) 3) */ 00186 /* skip ((cpu_type_t) 4) */ 00187 /* skip ((cpu_type_t) 5) */ 00188 #define CPU_TYPE_MC680x0 ((cpu_type_t) 6) 00189 #define CPU_TYPE_X86 ((cpu_type_t) 7) 00190 #define CPU_TYPE_I386 CPU_TYPE_X86 /* compatibility */ 00191 /* skip CPU_TYPE_MIPS ((cpu_type_t) 8) */ 00192 /* skip ((cpu_type_t) 9) */ 00193 #define CPU_TYPE_MC98000 ((cpu_type_t) 10) 00194 #define CPU_TYPE_HPPA ((cpu_type_t) 11) 00195 /* skip CPU_TYPE_ARM ((cpu_type_t) 12) */ 00196 #define CPU_TYPE_MC88000 ((cpu_type_t) 13) 00197 #define CPU_TYPE_SPARC ((cpu_type_t) 14) 00198 #define CPU_TYPE_I860 ((cpu_type_t) 15) 00199 /* skip CPU_TYPE_ALPHA ((cpu_type_t) 16) */ 00200 /* skip ((cpu_type_t) 17) */ 00201 #define CPU_TYPE_POWERPC ((cpu_type_t) 18) 00202 #define CPU_TYPE_POWERPC64 (CPU_TYPE_POWERPC | CPU_ARCH_ABI64) 00203 00204 00205 #define CPU_SUBTYPE_POWERPC_ALL ((cpu_subtype_t) 0) 00206 #define CPU_SUBTYPE_POWERPC_601 ((cpu_subtype_t) 1) 00207 #define CPU_SUBTYPE_POWERPC_602 ((cpu_subtype_t) 2) 00208 #define CPU_SUBTYPE_POWERPC_603 ((cpu_subtype_t) 3) 00209 #define CPU_SUBTYPE_POWERPC_603e ((cpu_subtype_t) 4) 00210 #define CPU_SUBTYPE_POWERPC_603ev ((cpu_subtype_t) 5) 00211 #define CPU_SUBTYPE_POWERPC_604 ((cpu_subtype_t) 6) 00212 #define CPU_SUBTYPE_POWERPC_604e ((cpu_subtype_t) 7) 00213 #define CPU_SUBTYPE_POWERPC_620 ((cpu_subtype_t) 8) 00214 #define CPU_SUBTYPE_POWERPC_750 ((cpu_subtype_t) 9) 00215 #define CPU_SUBTYPE_POWERPC_7400 ((cpu_subtype_t) 10) 00216 #define CPU_SUBTYPE_POWERPC_7450 ((cpu_subtype_t) 11) 00217 #ifndef _OPEN_SOURCE_ 00218 #define CPU_SUBTYPE_POWERPC_Max ((cpu_subtype_t) 10) 00219 #define CPU_SUBTYPE_POWERPC_SCVger ((cpu_subtype_t) 11) 00220 #endif 00221 #define CPU_SUBTYPE_POWERPC_970 ((cpu_subtype_t) 100) 00222 00223 00224 typedef int integer_t; 00225 typedef unsigned int natural_t; 00226 typedef integer_t cpu_type_t; 00227 typedef integer_t cpu_subtype_t; 00228 typedef int vm_prot_t; 00229 typedef natural_t mach_msg_type_size_t; 00230 typedef natural_t mach_msg_type_number_t; 00231 00232 /* 00233 * The 32-bit mach header appears at the very beginning of the object file for 00234 * 32-bit architectures. 00235 */ 00236 struct mach_header { 00237 uint32_t magic; /* mach magic number identifier */ 00238 cpu_type_t cputype; /* cpu specifier */ 00239 cpu_subtype_t cpusubtype; /* machine specifier */ 00240 uint32_t filetype; /* type of file */ 00241 uint32_t ncmds; /* number of load commands */ 00242 uint32_t sizeofcmds; /* the size of all the load commands */ 00243 uint32_t flags; /* flags */ 00244 }; 00245 00246 00247 /* 00248 * The 64-bit mach header appears at the very beginning of object files for 00249 * 64-bit architectures. 00250 */ 00251 struct mach_header_64 { 00252 uint32_t magic; /* mach magic number identifier */ 00253 cpu_type_t cputype; /* cpu specifier */ 00254 cpu_subtype_t cpusubtype; /* machine specifier */ 00255 uint32_t filetype; /* type of file */ 00256 uint32_t ncmds; /* number of load commands */ 00257 uint32_t sizeofcmds; /* the size of all the load commands */ 00258 uint32_t flags; /* flags */ 00259 uint32_t reserved; /* reserved */ 00260 }; 00261 00262 00263 /* 00264 * The load commands directly follow the mach_header. The total size of all 00265 * of the commands is given by the sizeofcmds field in the mach_header. All 00266 * load commands must have as their first two fields cmd and cmdsize. The cmd 00267 * field is filled in with a constant for that command type. Each command type 00268 * has a structure specifically for it. The cmdsize field is the size in bytes 00269 * of the particular load command structure plus anything that follows it that 00270 * is a part of the load command (i.e. section structures, strings, etc.). To 00271 * advance to the next load command the cmdsize can be added to the offset or 00272 * pointer of the current load command. The cmdsize for 32-bit architectures 00273 * MUST be a multiple of 4 bytes and for 64-bit architectures MUST be a multiple 00274 * of 8 bytes (these are forever the maximum alignment of any load commands). 00275 * The padded bytes must be zero. All tables in the object file must also 00276 * follow these rules so the file can be memory mapped. Otherwise the pointers 00277 * to these tables will not work well or at all on some machines. With all 00278 * padding zeroed like objects will compare byte for byte. 00279 */ 00280 struct load_command { 00281 uint32_t cmd; /* type of load command */ 00282 uint32_t cmdsize; /* total size of command in bytes */ 00283 }; 00284 00285 00286 /* 00287 * Thread commands contain machine-specific data structures suitable for 00288 * use in the thread state primitives. The machine specific data structures 00289 * follow the struct thread_command as follows. 00290 * Each flavor of machine specific data structure is preceded by an unsigned 00291 * long constant for the flavor of that data structure, an uint32_t 00292 * that is the count of longs of the size of the state data structure and then 00293 * the state data structure follows. This triple may be repeated for many 00294 * flavors. The constants for the flavors, counts and state data structure 00295 * definitions are expected to be in the header file <machine/thread_status.h>. 00296 * These machine specific data structures sizes must be multiples of 00297 * 4 bytes The cmdsize reflects the total size of the thread_command 00298 * and all of the sizes of the constants for the flavors, counts and state 00299 * data structures. 00300 * 00301 * For executable objects that are unix processes there will be one 00302 * thread_command (cmd == LC_UNIXTHREAD) created for it by the link-editor. 00303 * This is the same as a LC_THREAD, except that a stack is automatically 00304 * created (based on the shell's limit for the stack size). Command arguments 00305 * and environment variables are copied onto that stack. 00306 */ 00307 struct thread_command { 00308 uint32_t cmd; /* LC_THREAD or LC_UNIXTHREAD */ 00309 uint32_t cmdsize; /* total size of this command */ 00310 /* uint32_t flavor flavor of thread state */ 00311 /* uint32_t count count of longs in thread state */ 00312 /* struct XXX_thread_state state thread state for this flavor */ 00313 /* ... */ 00314 }; 00315 00316 00317 /* 00318 * The segment load command indicates that a part of this file is to be 00319 * mapped into the task's address space. The size of this segment in memory, 00320 * vmsize, maybe equal to or larger than the amount to map from this file, 00321 * filesize. The file is mapped starting at fileoff to the beginning of 00322 * the segment in memory, vmaddr. The rest of the memory of the segment, 00323 * if any, is allocated zero fill on demand. The segment's maximum virtual 00324 * memory protection and initial virtual memory protection are specified 00325 * by the maxprot and initprot fields. If the segment has sections then the 00326 * section structures directly follow the segment command and their size is 00327 * reflected in cmdsize. 00328 */ 00329 struct segment_command { /* for 32-bit architectures */ 00330 uint32_t cmd; /* LC_SEGMENT */ 00331 uint32_t cmdsize; /* includes sizeof section structs */ 00332 char segname[16]; /* segment name */ 00333 uint32_t vmaddr; /* memory address of this segment */ 00334 uint32_t vmsize; /* memory size of this segment */ 00335 uint32_t fileoff; /* file offset of this segment */ 00336 uint32_t filesize; /* amount to map from the file */ 00337 vm_prot_t maxprot; /* maximum VM protection */ 00338 vm_prot_t initprot; /* initial VM protection */ 00339 uint32_t nsects; /* number of sections in segment */ 00340 uint32_t flags; /* flags */ 00341 }; 00342 00343 00344 /* 00345 * The 64-bit segment load command indicates that a part of this file is to be 00346 * mapped into a 64-bit task's address space. If the 64-bit segment has 00347 * sections then section_64 structures directly follow the 64-bit segment 00348 * command and their size is reflected in cmdsize. 00349 */ 00350 struct segment_command_64 { /* for 64-bit architectures */ 00351 uint32_t cmd; /* LC_SEGMENT_64 */ 00352 uint32_t cmdsize; /* includes sizeof section_64 structs */ 00353 char segname[16]; /* segment name */ 00354 uint64_t vmaddr; /* memory address of this segment */ 00355 uint64_t vmsize; /* memory size of this segment */ 00356 uint64_t fileoff; /* file offset of this segment */ 00357 uint64_t filesize; /* amount to map from the file */ 00358 vm_prot_t maxprot; /* maximum VM protection */ 00359 vm_prot_t initprot; /* initial VM protection */ 00360 uint32_t nsects; /* number of sections in segment */ 00361 uint32_t flags; /* flags */ 00362 }; 00363 00364 00365 struct ppc_thread_state 00366 { 00367 unsigned int srr0; /* Instruction address register (PC) */ 00368 unsigned int srr1; /* Machine state register (supervisor) */ 00369 unsigned int r0; 00370 unsigned int r1; 00371 unsigned int r2; 00372 unsigned int r3; 00373 unsigned int r4; 00374 unsigned int r5; 00375 unsigned int r6; 00376 unsigned int r7; 00377 unsigned int r8; 00378 unsigned int r9; 00379 unsigned int r10; 00380 unsigned int r11; 00381 unsigned int r12; 00382 unsigned int r13; 00383 unsigned int r14; 00384 unsigned int r15; 00385 unsigned int r16; 00386 unsigned int r17; 00387 unsigned int r18; 00388 unsigned int r19; 00389 unsigned int r20; 00390 unsigned int r21; 00391 unsigned int r22; 00392 unsigned int r23; 00393 unsigned int r24; 00394 unsigned int r25; 00395 unsigned int r26; 00396 unsigned int r27; 00397 unsigned int r28; 00398 unsigned int r29; 00399 unsigned int r30; 00400 unsigned int r31; 00401 00402 unsigned int cr; /* Condition register */ 00403 unsigned int xer; /* User's integer exception register */ 00404 unsigned int lr; /* Link register */ 00405 unsigned int ctr; /* Count register */ 00406 unsigned int mq; /* MQ register (601 only) */ 00407 00408 unsigned int vrsave; /* Vector Save Register */ 00409 }; 00410 00411 typedef struct ppc_thread_state ppc_thread_state_t; 00412 00413 00414 /* 00415 * A segment is made up of zero or more sections. Non-MH_OBJECT files have 00416 * all of their segments with the proper sections in each, and padded to the 00417 * specified segment alignment when produced by the link editor. The first 00418 * segment of a MH_EXECUTE and MH_FVMLIB format file contains the mach_header 00419 * and load commands of the object file before its first section. The zero 00420 * fill sections are always last in their segment (in all formats). This 00421 * allows the zeroed segment padding to be mapped into memory where zero fill 00422 * sections might be. The gigabyte zero fill sections, those with the section 00423 * type S_GB_ZEROFILL, can only be in a segment with sections of this type. 00424 * These segments are then placed after all other segments. 00425 * 00426 * The MH_OBJECT format has all of its sections in one segment for 00427 * compactness. There is no padding to a specified segment boundary and the 00428 * mach_header and load commands are not part of the segment. 00429 * 00430 * Sections with the same section name, sectname, going into the same segment, 00431 * segname, are combined by the link editor. The resulting section is aligned 00432 * to the maximum alignment of the combined sections and is the new section's 00433 * alignment. The combined sections are aligned to their original alignment in 00434 * the combined section. Any padded bytes to get the specified alignment are 00435 * zeroed. 00436 * 00437 * The format of the relocation entries referenced by the reloff and nreloc 00438 * fields of the section structure for mach object files is described in the 00439 * header file <reloc.h>. 00440 */ 00441 struct section { /* for 32-bit architectures */ 00442 char sectname[16]; /* name of this section */ 00443 char segname[16]; /* segment this section goes in */ 00444 uint32_t addr; /* memory address of this section */ 00445 uint32_t size; /* size in bytes of this section */ 00446 uint32_t offset; /* file offset of this section */ 00447 uint32_t align; /* section alignment (power of 2) */ 00448 uint32_t reloff; /* file offset of relocation entries */ 00449 uint32_t nreloc; /* number of relocation entries */ 00450 uint32_t flags; /* flags (section type and attributes)*/ 00451 uint32_t reserved1; /* reserved (for offset or index) */ 00452 uint32_t reserved2; /* reserved (for count or sizeof) */ 00453 }; 00454 00455 struct section_64 { /* for 64-bit architectures */ 00456 char sectname[16]; /* name of this section */ 00457 char segname[16]; /* segment this section goes in */ 00458 uint64_t addr; /* memory address of this section */ 00459 uint64_t size; /* size in bytes of this section */ 00460 uint32_t offset; /* file offset of this section */ 00461 uint32_t align; /* section alignment (power of 2) */ 00462 uint32_t reloff; /* file offset of relocation entries */ 00463 uint32_t nreloc; /* number of relocation entries */ 00464 uint32_t flags; /* flags (section type and attributes)*/ 00465 uint32_t reserved1; /* reserved (for offset or index) */ 00466 uint32_t reserved2; /* reserved (for count or sizeof) */ 00467 uint32_t reserved3; /* reserved */ 00468 }; 00469 00470 00471 #endif