00001 /* 00002 * HT Editor 00003 * machostruc.h 00004 * 00005 * Copyright (C) 1999-2002 Stefan Weyergraf (stefan@weyergraf.de) 00006 * 00007 * This program is free software; you can redistribute it and/or modify 00008 * it under the terms of the GNU General Public License version 2 as 00009 * published by the Free Software Foundation. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program; if not, write to the Free Software 00018 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00019 */ 00020 00021 #ifndef __MACHOSTRUC_H__ 00022 #define __MACHOSTRUC_H__ 00023 00024 #include "global.h" 00025 00026 struct MACHO_HEADER { 00027 byte magic[4]; 00028 uint32 cputype; 00029 uint32 cpusubtype; 00030 uint32 filetype; 00031 uint32 ncmds; 00032 uint32 sizeofcmds; 00033 uint32 flags; 00034 }; 00035 00036 /* Constants for the filetype field of the mach_header */ 00037 #define MH_OBJECT 0x1 /* relocatable object file */ 00038 #define MH_EXECUTE 0x2 /* demand paged executable file */ 00039 #define MH_FVMLIB 0x3 /* fixed VM shared library file */ 00040 #define MH_CORE 0x4 /* core file */ 00041 #define MH_PRELOAD 0x5 /* preloaded executable file */ 00042 #define MH_DYLIB 0x6 /* dynamicly bound shared library file*/ 00043 #define MH_DYLINKER 0x7 /* dynamic link editor */ 00044 #define MH_BUNDLE 0x8 /* dynamicly bound bundle file */ 00045 00046 /* Constants for the flags field of the mach_header */ 00047 #define MH_NOUNDEFS 0x1 /* the object file has no undefined references, can be executed */ 00048 #define MH_INCRLINK 0x2 /* the object file is the output of an incremental link against a base file and can't be link edited again */ 00049 #define MH_DYLDLINK 0x4 /* the object file is input for the dynamic linker and can't be staticly link edited again */ 00050 #define MH_BINDATLOAD 0x8 /* the object file's undefined references are bound by the dynamic linker when loaded. */ 00051 #define MH_PREBOUND 0x10 /* the file has it's dynamic undefined references prebound. */ 00052 00053 struct MACHO_COMMAND { 00054 uint32 cmd; /* type of load command */ 00055 uint32 cmdsize; /* total size of command in bytes */ 00056 }; 00057 00058 /* Constants for the cmd field of all load commands, the type */ 00059 #define LC_SEGMENT 0x1 /* segment of this file to be mapped */ 00060 #define LC_SYMTAB 0x2 /* link-edit stab symbol table info */ 00061 #define LC_SYMSEG 0x3 /* link-edit gdb symbol table info (obsolete) */ 00062 #define LC_THREAD 0x4 /* thread */ 00063 #define LC_UNIXTHREAD 0x5 /* unix thread (includes a stack) */ 00064 #define LC_LOADFVMLIB 0x6 /* load a specified fixed VM shared library */ 00065 #define LC_IDFVMLIB 0x7 /* fixed VM shared library identification */ 00066 #define LC_IDENT 0x8 /* object identification info (obsolete) */ 00067 #define LC_FVMFILE 0x9 /* fixed VM file inclusion (internal use) */ 00068 #define LC_PREPAGE 0xa /* prepage command (internal use) */ 00069 #define LC_DYSYMTAB 0xb /* dynamic link-edit symbol table info */ 00070 #define LC_LOAD_DYLIB 0xc /* load a dynamicly linked shared library */ 00071 #define LC_ID_DYLIB 0xd /* dynamicly linked shared lib identification */ 00072 #define LC_LOAD_DYLINKER 0xe /* load a dynamic linker */ 00073 #define LC_ID_DYLINKER 0xf /* dynamic linker identification */ 00074 #define LC_PREBOUND_DYLIB 0x10 /* modules prebound for a dynamicly linked shared library */ 00075 00076 struct MACHO_SEGMENT_COMMAND { 00077 uint32 cmd; /* LC_SEGMENT */ 00078 uint32 cmdsize; /* includes sizeof section structs */ 00079 byte segname[16]; /* segment name */ 00080 uint32 vmaddr; /* memory address of this segment */ 00081 uint32 vmsize; /* memory size of this segment */ 00082 uint32 fileoff; /* file offset of this segment */ 00083 uint32 filesize; /* amount to map from the file */ 00084 uint32 maxprot; /* maximum VM protection */ 00085 uint32 initprot; /* initial VM protection */ 00086 uint32 nsects; /* number of sections in segment */ 00087 uint32 flags; /* flags */ 00088 }; 00089 00090 /* Constants for the flags field of the segment_command */ 00091 #define SG_HIGHVM 0x1 /* the file contents for this segment is for the high part of the VM space, the low part is zero filled (for stacks in core files) */ 00092 #define SG_FVMLIB 0x2 /* this segment is the VM that is allocated by a fixed VM library, for overlap checking in the link editor */ 00093 #define SG_NORELOC 0x4 /* this segment has nothing that was relocated in it and nothing relocated to it, that is it maybe safely replaced without relocation */ 00094 00095 struct MACHO_SECTION { 00096 byte sectname[16]; /* name of this section */ 00097 byte segname[16]; /* segment this section goes in */ 00098 uint32 vmaddr; /* memory address of this section */ 00099 uint32 vmsize; /* size in bytes of this section */ 00100 uint32 fileoff; /* file offset of this section */ 00101 uint32 align; /* section alignment (power of 2) */ 00102 uint32 reloff; /* file offset of relocation entries */ 00103 uint32 nreloc; /* number of relocation entries */ 00104 uint32 flags; /* flags (section type and attributes)*/ 00105 uint32 reserved1; /* reserved */ 00106 uint32 reserved2; /* reserved */ 00107 }; 00108 00109 /* 00110 * The flags field of a section structure is separated into two parts a section 00111 * type and section attributes. The section types are mutually exclusive (it 00112 * can only have one type) but the section attributes are not (it may have more 00113 * than one attribute). 00114 */ 00115 00116 #define MACHO_SECTION_TYPE 0x000000ff /* 256 section types */ 00117 #define MACHO_SECTION_ATTRIBUTES 0xffffff00 /* 24 section attributes */ 00118 00119 /* Constants for the type of a section */ 00120 #define MACHO_S_REGULAR 0x0 /* regular section */ 00121 #define MACHO_S_ZEROFILL 0x1 /* zero fill on demand section */ 00122 #define MACHO_S_CSTRING_LITERALS 0x2 /* section with only literal C strings*/ 00123 #define MACHO_S_4BYTE_LITERALS 0x3 /* section with only 4 byte literals */ 00124 #define MACHO_S_8BYTE_LITERALS 0x4 /* section with only 8 byte literals */ 00125 #define MACHO_S_LITERAL_POINTERS 0x5 /* section with only pointers to */ 00126 /* literals */ 00127 /* 00128 * For the two types of symbol pointers sections and the symbol stubs section 00129 * they have indirect symbol table entries. For each of the entries in the 00130 * section the indirect symbol table entries, in corresponding order in the 00131 * indirect symbol table, start at the index stored in the reserved1 field 00132 * of the section structure. Since the indirect symbol table entries 00133 * correspond to the entries in the section the number of indirect symbol table 00134 * entries is inferred from the size of the section divided by the size of the 00135 * entries in the section. For symbol pointers sections the size of the entries 00136 * in the section is 4 bytes and for symbol stubs sections the byte size of the 00137 * stubs is stored in the reserved2 field of the section structure. 00138 */ 00139 #define MACHO_S_NON_LAZY_SYMBOL_POINTERS 0x6 /* section with only non-lazy 00140 symbol pointers */ 00141 #define MACHO_S_LAZY_SYMBOL_POINTERS 0x7 /* section with only lazy symbol 00142 pointers */ 00143 #define MACHO_S_SYMBOL_STUBS 0x8 /* section with only symbol 00144 stubs, byte size of stub in 00145 the reserved2 field */ 00146 #define MACHO_S_MOD_INIT_FUNC_POINTERS 0x9 /* section with only function 00147 pointers for initialization*/ 00148 /* 00149 * Constants for the section attributes part of the flags field of a section 00150 * structure. 00151 */ 00152 #define MACHO_SECTION_ATTRIBUTES_USR 0xff000000 /* User setable attributes */ 00153 #define MACHO_S_ATTR_PURE_INSTRUCTIONS 0x80000000 /* section contains only true 00154 machine instructions */ 00155 #define MACHO_SECTION_ATTRIBUTES_SYS 0x00ffff00 /* system setable attributes */ 00156 #define MACHO_S_ATTR_SOME_INSTRUCTIONS 0x00000400 /* section contains some 00157 machine instructions */ 00158 #define MACHO_S_ATTR_EXT_RELOC 0x00000200 /* section has external 00159 relocation entries */ 00160 #define MACHO_S_ATTR_LOC_RELOC 0x00000100 /* section has local 00161 relocation entries */ 00162 00163 struct MACHO_PPC_THREAD_STATE { 00164 uint32 srr0; /* Instruction address register (PC) */ 00165 uint32 srr1; /* Machine state register (supervisor) */ 00166 uint32 r0; 00167 uint32 r1; 00168 uint32 r2; 00169 uint32 r3; 00170 uint32 r4; 00171 uint32 r5; 00172 uint32 r6; 00173 uint32 r7; 00174 uint32 r8; 00175 uint32 r9; 00176 uint32 r10; 00177 uint32 r11; 00178 uint32 r12; 00179 uint32 r13; 00180 uint32 r14; 00181 uint32 r15; 00182 uint32 r16; 00183 uint32 r17; 00184 uint32 r18; 00185 uint32 r19; 00186 uint32 r20; 00187 uint32 r21; 00188 uint32 r22; 00189 uint32 r23; 00190 uint32 r24; 00191 uint32 r25; 00192 uint32 r26; 00193 uint32 r27; 00194 uint32 r28; 00195 uint32 r29; 00196 uint32 r30; 00197 uint32 r31; 00198 00199 uint32 cr; /* Condition register */ 00200 uint32 xer; /* User's integer exception register */ 00201 uint32 lr; /* Link register */ 00202 uint32 ctr; /* Count register */ 00203 uint32 mq; /* MQ register (601 only) */ 00204 00205 uint32 vrsave; /* Vector Save Register */ 00206 }; 00207 00208 #define FLAVOR_PPC_THREAD_STATE 1 00209 #define FLAVOR_PPC_FLOAT_STATE 2 00210 #define FLAVOR_PPC_EXCEPTION_STATE 3 00211 #define FLAVOR_PPC_VECTOR_STATE 4 00212 #define FLAVOR_THREAD_STATE_NONE 7 00213 00214 struct MACHO_I386_THREAD_STATE { 00215 uint32 eax; 00216 uint32 ebx; 00217 uint32 ecx; 00218 uint32 edx; 00219 uint32 edi; 00220 uint32 esi; 00221 uint32 ebp; 00222 uint32 esp; 00223 uint32 ss; 00224 uint32 eflags; 00225 uint32 eip; 00226 uint32 cs; 00227 uint32 ds; 00228 uint32 es; 00229 uint32 fs; 00230 uint32 gs; 00231 }; 00232 00233 #define i386_NEW_THREAD_STATE 1 /* used to be i386_THREAD_STATE */ 00234 #define i386_FLOAT_STATE 2 00235 #define i386_ISA_PORT_MAP_STATE 3 00236 #define i386_V86_ASSIST_STATE 4 00237 #define i386_REGS_SEGS_STATE 5 00238 #define THREAD_SYSCALL_STATE 6 00239 #define THREAD_STATE_NONE 7 00240 #define i386_SAVED_STATE 8 00241 00242 union MACHO_THREAD_STATE { 00243 MACHO_PPC_THREAD_STATE state_ppc; 00244 MACHO_I386_THREAD_STATE state_i386; 00245 }; 00246 00247 struct MACHO_THREAD_COMMAND { 00248 uint32 cmd; /* LC_THREAD or LC_UNIXTHREAD */ 00249 uint32 cmdsize; /* total size of this command */ 00250 uint32 flavor; /* flavor of thread state */ 00251 uint32 count; /* count of longs in thread state */ 00252 MACHO_THREAD_STATE state; 00253 }; 00254 00255 union MACHO_COMMAND_U { 00256 MACHO_COMMAND cmd; 00257 MACHO_SEGMENT_COMMAND segment; 00258 MACHO_THREAD_COMMAND thread; 00259 }; 00260 00261 struct MACHO_SYMTAB_COMMAND { 00262 uint32 cmd; /* LC_SYMTAB */ 00263 uint32 cmdsize; /* sizeof(struct symtab_command) */ 00264 uint32 symoff; /* symbol table offset */ 00265 uint32 nsyms; /* number of symbol table entries */ 00266 uint32 stroff; /* string table offset */ 00267 uint32 strsize; /* string table size in bytes */ 00268 }; 00269 00270 struct MACHO_SYMTAB_NLIST { 00271 uint32 strx; 00272 uint8 type; 00273 uint8 sect; 00274 uint16 desc; 00275 uint32 value; 00276 }; 00277 00278 // masks for type 00279 #define MACHO_SYMBOL_N_STAB 0xe0 00280 #define MACHO_SYMBOL_N_PEXT 0x10 00281 #define MACHO_SYMBOL_N_TYPE 0x0e 00282 #define MACHO_SYMBOL_N_EXT 0x01 00283 00284 #define MACHO_SYMBOL_TYPE_N_UNDF 0x00 00285 #define MACHO_SYMBOL_TYPE_N_ABS 0x02 00286 #define MACHO_SYMBOL_TYPE_N_INDR 0x0a 00287 #define MACHO_SYMBOL_TYPE_N_PBUD 0x0c 00288 #define MACHO_SYMBOL_TYPE_N_SECT 0x0e 00289 00290 /* 00291 * Machine types known by all. 00292 */ 00293 00294 #define MACHO_CPU_TYPE_ANY -1 00295 00296 #define MACHO_CPU_TYPE_VAX 1 00297 /* skip 2 */ 00298 /* skip 3 */ 00299 /* skip 4 */ 00300 /* skip 5 */ 00301 #define MACHO_CPU_TYPE_MC680x0 6 00302 #define MACHO_CPU_TYPE_I386 7 00303 /* skip MACHO_CPU_TYPE_MIPS 8 */ 00304 /* skip 9 */ 00305 #define MACHO_CPU_TYPE_MC98000 10 00306 #define MACHO_CPU_TYPE_HPPA 11 00307 /* skip MACHO_CPU_TYPE_ARM 12 */ 00308 #define MACHO_CPU_TYPE_MC88000 13 00309 #define MACHO_CPU_TYPE_SPARC 14 00310 #define MACHO_CPU_TYPE_I860 15 00311 /* skip MACHO_CPU_TYPE_ALPHA 16 */ 00312 /* skip 17 */ 00313 #define MACHO_CPU_TYPE_POWERPC 18 00314 00315 00316 /* 00317 * Machine subtypes (these are defined here, instead of in a machine 00318 * dependent directory, so that any program can get all definitions 00319 * regardless of where is it compiled. 00320 */ 00321 00322 /* 00323 * Object files that are hand-crafted to run on any 00324 * implementation of an architecture are tagged with 00325 * MACHO_CPU_SUBTYPE_MULTIPLE. This functions essentially the same as 00326 * the "ALL" subtype of an architecture except that it allows us 00327 * to easily find object files that may need to be modified 00328 * whenever a new implementation of an architecture comes out. 00329 * 00330 * It is the responsibility of the implementor to make sure the 00331 * software handles unsupported implementations elegantly. 00332 */ 00333 #define MACHO_CPU_SUBTYPE_MULTIPLE -1 00334 #define MACHO_CPU_SUBTYPE_LITTLE_ENDIAN 0 00335 #define MACHO_CPU_SUBTYPE_BIG_ENDIAN 1 00336 00337 /* 00338 * VAX subtypes (these do *not* necessary conform to the actual cpu 00339 * ID assigned by DEC available via the SID register. 00340 */ 00341 00342 #define MACHO_CPU_SUBTYPE_VAX_ALL 0 00343 #define MACHO_CPU_SUBTYPE_VAX780 1 00344 #define MACHO_CPU_SUBTYPE_VAX785 2 00345 #define MACHO_CPU_SUBTYPE_VAX750 3 00346 #define MACHO_CPU_SUBTYPE_VAX730 4 00347 #define MACHO_CPU_SUBTYPE_UVAXI 5 00348 #define MACHO_CPU_SUBTYPE_UVAXII 6 00349 #define MACHO_CPU_SUBTYPE_VAX8200 7 00350 #define MACHO_CPU_SUBTYPE_VAX8500 8 00351 #define MACHO_CPU_SUBTYPE_VAX8600 9 00352 #define MACHO_CPU_SUBTYPE_VAX8650 10 00353 #define MACHO_CPU_SUBTYPE_VAX8800 11 00354 #define MACHO_CPU_SUBTYPE_UVAXIII 12 00355 00356 /* 00357 * 680x0 subtypes 00358 * 00359 * The subtype definitions here are unusual for historical reasons. 00360 * NeXT used to consider 68030 code as generic 68000 code. For 00361 * backwards compatability: 00362 * 00363 * MACHO_CPU_SUBTYPE_MC68030 symbol has been preserved for source code 00364 * compatability. 00365 * 00366 * MACHO_CPU_SUBTYPE_MC680x0_ALL has been defined to be the same 00367 * subtype as MACHO_CPU_SUBTYPE_MC68030 for binary comatability. 00368 * 00369 * MACHO_CPU_SUBTYPE_MC68030_ONLY has been added to allow new object 00370 * files to be tagged as containing 68030-specific instructions. 00371 */ 00372 00373 #define MACHO_CPU_SUBTYPE_MC680x0_ALL 1 00374 #define MACHO_CPU_SUBTYPE_MC68030 1 /* compat */ 00375 #define MACHO_CPU_SUBTYPE_MC68040 2 00376 #define MACHO_CPU_SUBTYPE_MC68030_ONLY 3 00377 00378 /* 00379 * I386 subtypes. 00380 */ 00381 00382 #define MACHO_CPU_SUBTYPE_I386_ALL 3 00383 #define MACHO_CPU_SUBTYPE_386 3 00384 #define MACHO_CPU_SUBTYPE_486 4 00385 #define MACHO_CPU_SUBTYPE_486SX 4 + 128 00386 #define MACHO_CPU_SUBTYPE_586 5 00387 #define MACHO_CPU_SUBTYPE_INTEL(f, m) (f + ((m) << 4) 00388 #define MACHO_CPU_SUBTYPE_PENT MACHO_CPU_SUBTYPE_INTEL(5, 0) 00389 #define MACHO_CPU_SUBTYPE_PENTPRO MACHO_CPU_SUBTYPE_INTEL(6, 1) 00390 #define MACHO_CPU_SUBTYPE_PENTII_M3 MACHO_CPU_SUBTYPE_INTEL(6, 3) 00391 #define MACHO_CPU_SUBTYPE_PENTII_M5 MACHO_CPU_SUBTYPE_INTEL(6, 5) 00392 00393 #define MACHO_CPU_SUBTYPE_INTEL_FAMILY(x) ((x) & 15) 00394 #define MACHO_CPU_SUBTYPE_INTEL_FAMILY_MAX 15 00395 00396 #define MACHO_CPU_SUBTYPE_INTEL_MODEL(x) ((x) >> 4) 00397 #define MACHO_CPU_SUBTYPE_INTEL_MODEL_ALL 0 00398 00399 /* 00400 * Mips subtypes. 00401 */ 00402 00403 #define MACHO_CPU_SUBTYPE_MIPS_ALL 0 00404 #define MACHO_CPU_SUBTYPE_MIPS_R2300 1 00405 #define MACHO_CPU_SUBTYPE_MIPS_R2600 2 00406 #define MACHO_CPU_SUBTYPE_MIPS_R2800 3 00407 #define MACHO_CPU_SUBTYPE_MIPS_R2000a 4 /* pmax */ 00408 #define MACHO_CPU_SUBTYPE_MIPS_R2000 5 00409 #define MACHO_CPU_SUBTYPE_MIPS_R3000a 6 /* 3max */ 00410 #define MACHO_CPU_SUBTYPE_MIPS_R3000 7 00411 00412 /* 00413 * MC98000 (PowerPC subtypes 00414 */ 00415 #define MACHO_CPU_SUBTYPE_MC98000_ALL 0 00416 #define MACHO_CPU_SUBTYPE_MC98601 1 00417 00418 /* 00419 * HPPA subtypes for Hewlett-Packard HP-PA family of 00420 * risc processors. Port by NeXT to 700 series. 00421 */ 00422 00423 #define MACHO_CPU_SUBTYPE_HPPA_ALL 0 00424 #define MACHO_CPU_SUBTYPE_HPPA_7100 0 /* compat */ 00425 #define MACHO_CPU_SUBTYPE_HPPA_7100LC 1 00426 00427 /* 00428 * MC88000 subtypes. 00429 */ 00430 #define MACHO_CPU_SUBTYPE_MC88000_ALL 0 00431 #define MACHO_CPU_SUBTYPE_MC88100 1 00432 #define MACHO_CPU_SUBTYPE_MC88110 2 00433 00434 /* 00435 * SPARC subtypes 00436 */ 00437 #define MACHO_CPU_SUBTYPE_SPARC_ALL 0 00438 00439 /* 00440 * I860 subtypes 00441 */ 00442 #define MACHO_CPU_SUBTYPE_I860_ALL 0 00443 #define MACHO_CPU_SUBTYPE_I860_860 1 00444 00445 /* 00446 * PowerPC subtypes 00447 */ 00448 #define MACHO_CPU_SUBTYPE_POWERPC_ALL 0 00449 #define MACHO_CPU_SUBTYPE_POWERPC_601 1 00450 #define MACHO_CPU_SUBTYPE_POWERPC_602 2 00451 #define MACHO_CPU_SUBTYPE_POWERPC_603 3 00452 #define MACHO_CPU_SUBTYPE_POWERPC_603e 4 00453 #define MACHO_CPU_SUBTYPE_POWERPC_603ev 5 00454 #define MACHO_CPU_SUBTYPE_POWERPC_604 6 00455 #define MACHO_CPU_SUBTYPE_POWERPC_604e 7 00456 #define MACHO_CPU_SUBTYPE_POWERPC_620 8 00457 #define MACHO_CPU_SUBTYPE_POWERPC_750 9 00458 #define MACHO_CPU_SUBTYPE_POWERPC_7400 10 00459 #define MACHO_CPU_SUBTYPE_POWERPC_7450 11 00460 00461 extern byte MACHO_HEADER_struct[]; 00462 extern byte MACHO_COMMAND_struct[]; 00463 extern byte MACHO_SEGMENT_COMMAND_struct[]; 00464 extern byte MACHO_SECTION_struct[]; 00465 extern byte MACHO_THREAD_COMMAND_struct[]; // .state not included ! 00466 extern byte MACHO_PPC_THREAD_STATE_struct[]; 00467 extern byte MACHO_I386_THREAD_STATE_struct[]; 00468 extern byte MACHO_SYMTAB_COMMAND_struct[]; 00469 extern byte MACHO_SYMTAB_NLIST_struct[]; 00470 00471 #endif /* __MACHOSTRUC_H__ */