Package vstruct :: Package defs :: Package macho :: Module loader
[hide private]
[frames] | no frames]

Source Code for Module vstruct.defs.macho.loader

  1  import vstruct 
  2   
  3  from vstruct.primitives import * 
  4  from vstruct.defs.macho.const import * 
  5   
  6  vm_prot_t = v_uint32 
  7  cpu_type_t = v_uint32 
  8  cpu_subtype_t = v_uint32 
  9  lc_str = v_uint32 
 10   
11 -class mach_header(vstruct.VStruct):
12
13 - def __init__(self):
14 vstruct.VStruct.__init__(self) 15 self.magic = v_uint32() # mach magic number identifier 16 self.cputype = cpu_type_t() # cpu specifier 17 self.cpusubtype = cpu_subtype_t() # machine specifier 18 self.filetype = v_uint32() # type of file 19 self.ncmds = v_uint32() # number of load commands 20 self.sizeofcmds = v_uint32() # the size of all the load commands 21 self.flags = v_uint32() # flags
22
23 - def vsParse(self, bytes, offset=0):
24 # Over-ride this so we can do the parse, and make sure we 25 # had the right endianness. 26 ret = vstruct.VStruct.vsParse(self, bytes, offset=offset) 27 if self.magic == MH_CIGAM: 28 self._vs_fmtbase = '>' 29 ret = vstruct.VStruct.vsParse(self, bytes, offset=offset) 30 return ret
31
32 -class mach_header_64(vstruct.VStruct):
33
34 - def __init__(self):
35 vstruct.VStruct.__init__(self) 36 self.magic = v_uint32() # mach magic number identifier 37 self.cputype = cpu_type_t() # cpu specifier 38 self.cpusubtype = cpu_subtype_t() # machine specifier 39 self.filetype = v_uint32() # type of file 40 self.ncmds = v_uint32() # number of load commands 41 self.sizeofcmds = v_uint32() # the size of all the load commands 42 self.flags = v_uint32() # flags 43 self.reserved = v_uint32() # reserved
44 45 46 # FIXME all commands should subclass this one!
47 -class load_command(vstruct.VStruct):
48
49 - def __init__(self):
50 vstruct.VStruct.__init__(self) 51 self.cmd = v_uint32() # type of load command 52 self.cmdsize = v_uint32() # total size of command in bytes
53
54 -class segment_command(vstruct.VStruct):
55
56 - def __init__(self):
57 vstruct.VStruct.__init__(self) 58 self.cmd = v_uint32() # LC_SEGMENT 59 self.cmdsize = v_uint32() # includes sizeof section structs 60 self.segname = v_str(size=16) # segment name 61 self.vmaddr = v_uint32() # memory address of this segment 62 self.vmsize = v_uint32() # memory size of this segment 63 self.fileoff = v_uint32() # file offset of this segment 64 self.filesize = v_uint32() # amount to map from the file 65 self.maxprot = vm_prot_t() # maximum VM protection 66 self.initprot = vm_prot_t() # initial VM protection 67 self.nsects = v_uint32() # number of sections in segment 68 self.flags = v_uint32() # flags
69
70 -class segment_command_64(vstruct.VStruct):
71
72 - def __init__(self):
73 vstruct.VStruct.__init__(self) 74 self.cmd = v_uint32() # LC_SEGMENT_64 75 self.cmdsize = v_uint32() # includes sizeof section_64 structs 76 self.segname[16] = v_uint8() # segment name 77 self.vmaddr = v_uint64() # memory address of this segment 78 self.vmsize = v_uint64() # memory size of this segment 79 self.fileoff = v_uint64() # file offset of this segment 80 self.filesize = v_uint64() # amount to map from the file 81 self.maxprot = vm_prot_t() # maximum VM protection 82 self.initprot = vm_prot_t() # initial VM protection 83 self.nsects = v_uint32() # number of sections in segment 84 self.flags = v_uint32() # flags
85 86
87 -class section(vstruct.VStruct):
88
89 - def __init__(self):
90 vstruct.VStruct.__init__(self) 91 self.sectname = v_str(size=16) # name of this section 92 self.segname = v_str(size=16) # segment this section goes in 93 self.addr = v_uint32() # memory address of this section 94 self.size = v_uint32() # size in bytes of this section 95 self.offset = v_uint32() # file offset of this section 96 self.align = v_uint32() # section alignment (power of 2) 97 self.reloff = v_uint32() # file offset of relocation entries 98 self.nreloc = v_uint32() # number of relocation entries 99 self.flags = v_uint32() # flags (section type and attributes) 100 self.reserved1 = v_uint32() # reserved (for offset or index) 101 self.reserved2 = v_uint32() # reserved (for count or sizeof)
102 103
104 -class section_64(vstruct.VStruct):
105
106 - def __init__(self):
107 vstruct.VStruct.__init__(self) 108 self.sectname = v_str(size=16) # name of this section 109 self.segname = v_str(size=16) # segment this section goes in 110 self.addr = v_uint64() # memory address of this section 111 self.size = v_uint64() # size in bytes of this section 112 self.offset = v_uint32() # file offset of this section 113 self.align = v_uint32() # section alignment (power of 2) 114 self.reloff = v_uint32() # file offset of relocation entries 115 self.nreloc = v_uint32() # number of relocation entries 116 self.flags = v_uint32() # flags (section type and attributes) 117 self.reserved1 = v_uint32() # reserved (for offset or index) 118 self.reserved2 = v_uint32() # reserved (for count or sizeof) 119 self.reserved3 = v_uint32() # reserved
120
121 -class fvmlib_command(vstruct.VStruct):
122
123 - def __init__(self):
124 vstruct.VStruct.__init__(self) 125 self.cmd = v_uint32() # LC_IDFVMLIB or LC_LOADFVMLIB 126 self.cmdsize = v_uint32() # includes pathname string 127 self.name = lc_str() # library's target pathname 128 self.minor_version = v_uint32() # library's minor version number 129 self.header_addr = v_uint32() # library's header address
130
131 -class dylib_command(vstruct.VStruct):
132
133 - def __init__(self):
134 vstruct.VStruct.__init__(self) 135 self.cmd = v_uint32() # LC_ID_DYLIB, LC_LOAD_{,WEAK_}DYLIB, LC_REEXPORT_DYLIB 136 self.cmdsize = v_uint32() # includes pathname string 137 self.name = lc_str() # library's path name 138 self.timestamp = v_uint32() # library's build time stamp 139 self.current_version = v_uint32() # library's current version number 140 self.compatibility_version = v_uint32() # library's compatibility vers number 141 self.namedata = v_bytes(size=0)
142
143 - def vsParse(self, bytes, offset=0):
144 # So we can grab the name data 145 retoff = vstruct.VStruct.vsParse(self, bytes, offset=offset) 146 # Grab the name from the inline data... 147 name = bytes[ offset + self.name : offset + self.cmdsize ] 148 name = name.split('\x00', 1)[0] 149 self.vsGetField('namedata').vsSetLength(len(name)) 150 self.namedata = name 151 return retoff
152
153 -class sub_framework_command(vstruct.VStruct):
154
155 - def __init__(self):
156 vstruct.VStruct.__init__(self) 157 self.cmd = v_uint32() # LC_SUB_FRAMEWORK 158 self.cmdsize = v_uint32() # includes umbrella string 159 self.umbrella = lc_str() # the umbrella framework name
160 161
162 -class sub_client_command(vstruct.VStruct):
163
164 - def __init__(self):
165 vstruct.VStruct.__init__(self) 166 self.cmd = v_uint32() # LC_SUB_CLIENT 167 self.cmdsize = v_uint32() # includes client string 168 self.client = lc_str() # the client name
169 170
171 -class sub_umbrella_command(vstruct.VStruct):
172
173 - def __init__(self):
174 vstruct.VStruct.__init__(self) 175 self.cmd = v_uint32() # LC_SUB_UMBRELLA 176 self.cmdsize = v_uint32() # includes sub_umbrella string 177 self.sub_umbrella = lc_str() # the sub_umbrella framework name
178 179
180 -class sub_library_command(vstruct.VStruct):
181
182 - def __init__(self):
183 vstruct.VStruct.__init__(self) 184 self.cmd = v_uint32() # LC_SUB_LIBRARY 185 self.cmdsize = v_uint32() # includes sub_library string 186 self.sub_library = lc_str() # the sub_library name
187 188
189 -class prebound_dylib_command(vstruct.VStruct):
190
191 - def __init__(self):
192 vstruct.VStruct.__init__(self) 193 self.cmd = v_uint32() # LC_PREBOUND_DYLIB 194 self.cmdsize = v_uint32() # includes strings 195 self.name = lc_str() # library's path name 196 self.nmodules = v_uint32() # number of modules in library 197 self.linked_modules = lc_str() # bit vector of linked modules
198 199
200 -class dylinker_command(vstruct.VStruct):
201
202 - def __init__(self):
203 vstruct.VStruct.__init__(self) 204 self.cmd = v_uint32() # LC_ID_DYLINKER or LC_LOAD_DYLINKER 205 self.cmdsize = v_uint32() # includes pathname string 206 self.name = lc_str() # dynamic linker's path name
207 208
209 -class thread_command(vstruct.VStruct):
210
211 - def __init__(self):
212 vstruct.VStruct.__init__(self) 213 self.cmd = v_uint32() # LC_THREAD or LC_UNIXTHREAD 214 self.cmdsize = v_uint32() # total size of this command
215 216
217 -class routines_command(vstruct.VStruct):
218
219 - def __init__(self):
220 vstruct.VStruct.__init__(self) 221 self.cmd = v_uint32() # LC_ROUTINES 222 self.cmdsize = v_uint32() # total size of this command 223 self.init_address = v_uint32() # address of initialization routine 224 self.init_module = v_uint32() # index into the module table that 225 self.reserved1 = v_uint32() 226 self.reserved2 = v_uint32() 227 self.reserved3 = v_uint32() 228 self.reserved4 = v_uint32() 229 self.reserved5 = v_uint32() 230 self.reserved6 = v_uint32()
231 232
233 -class routines_command_64(vstruct.VStruct):
234
235 - def __init__(self):
236 vstruct.VStruct.__init__(self) 237 self.cmd = v_uint32() # LC_ROUTINES_64 238 self.cmdsize = v_uint32() # total size of this command 239 self.init_address = v_uint64() # address of initialization routine 240 self.init_module = v_uint64() # index into the module table that 241 self.reserved1 = v_uint64() 242 self.reserved2 = v_uint64() 243 self.reserved3 = v_uint64() 244 self.reserved4 = v_uint64() 245 self.reserved5 = v_uint64() 246 self.reserved6 = v_uint64()
247 248
249 -class symtab_command(vstruct.VStruct):
250
251 - def __init__(self):
252 vstruct.VStruct.__init__(self) 253 self.cmd = v_uint32() # LC_SYMTAB 254 self.cmdsize = v_uint32() # sizeof(struct symtab_command) 255 self.symoff = v_uint32() # symbol table offset 256 self.nsyms = v_uint32() # number of symbol table entries 257 self.stroff = v_uint32() # string table offset 258 self.strsize = v_uint32() # string table size in bytes
259
260 -class dysymtab_command(vstruct.VStruct):
261
262 - def __init__(self):
263 vstruct.VStruct.__init__(self) 264 self.cmd = v_uint32() # LC_DYSYMTAB 265 self.cmdsize = v_uint32() # sizeof(struct dysymtab_command) 266 self.ilocalsym = v_uint32() # index to local symbols 267 self.nlocalsym = v_uint32() # number of local symbols 268 self.iextdefsym = v_uint32() # index to externally defined symbols 269 self.nextdefsym = v_uint32() # number of externally defined symbols 270 self.iundefsym = v_uint32() # index to undefined symbols 271 self.nundefsym = v_uint32() # number of undefined symbols 272 self.tocoff = v_uint32() # file offset to table of contents 273 self.ntoc = v_uint32() # number of entries in table of contents 274 self.modtaboff = v_uint32() # file offset to module table 275 self.nmodtab = v_uint32() # number of module table entries 276 self.extrefsymoff = v_uint32() # offset to referenced symbol table 277 self.nextrefsyms = v_uint32() # number of referenced symbol table entries 278 self.indirectsymoff = v_uint32() # file offset to the indirect symbol table 279 self.nindirectsyms = v_uint32() # number of indirect symbol table entries 280 self.extreloff = v_uint32() # offset to external relocation entries 281 self.nextrel = v_uint32() # number of external relocation entries 282 self.locreloff = v_uint32() # offset to local relocation entries 283 self.nlocrel = v_uint32() # number of local relocation entries
284 285
286 -class dylib_table_of_contents(vstruct.VStruct):
287
288 - def __init__(self):
289 vstruct.VStruct.__init__(self) 290 self.symbol_index = v_uint32() # the defined external symbol (index into the symbol table) 291 self.module_index = v_uint32() # index into the module table this symbol is defined in
292 293
294 -class dylib_module(vstruct.VStruct):
295
296 - def __init__(self):
297 vstruct.VStruct.__init__(self) 298 self.module_name = v_uint32() # the module name (index into string table) 299 self.iextdefsym = v_uint32() # index into externally defined symbols 300 self.nextdefsym = v_uint32() # number of externally defined symbols 301 self.irefsym = v_uint32() # index into reference symbol table 302 self.nrefsym = v_uint32() # number of reference symbol table entries 303 self.ilocalsym = v_uint32() # index into symbols for local symbols 304 self.nlocalsym = v_uint32() # number of local symbols 305 self.iextrel = v_uint32() # index into external relocation entries 306 self.nextrel = v_uint32() # number of external relocation entries 307 self.iinit_iterm = v_uint32() # low 16 bits are the index into the init section, high 16 bits are the index into the term section 308 self.ninit_nterm = v_uint32() # low 16 bits are the number of init section entries, high 16 bits are the number of term section entries 309 self.objc_module_info_addr = v_uint32() # the (__OBJC,__module_info) section 310 self.objc_module_info_size = v_uint32() # the (__OBJC,__module_info) section
311 312
313 -class dylib_module_64(vstruct.VStruct):
314
315 - def __init__(self):
316 vstruct.VStruct.__init__(self) 317 self.module_name = v_uint32() # the module name (index into string table) 318 self.iextdefsym = v_uint32() # index into externally defined symbols 319 self.nextdefsym = v_uint32() # number of externally defined symbols 320 self.irefsym = v_uint32() # index into reference symbol table 321 self.nrefsym = v_uint32() # number of reference symbol table entries 322 self.ilocalsym = v_uint32() # index into symbols for local symbols 323 self.nlocalsym = v_uint32() # number of local symbols 324 self.iextrel = v_uint32() # index into external relocation entries 325 self.nextrel = v_uint32() # number of external relocation entries 326 self.iinit_iterm = v_uint32() # low 16 bits are the index into the init section, high 16 bits are the index into the term section 327 self.ninit_nterm = v_uint32() # low 16 bits are the number of init section entries, high 16 bits are the number of term section entries 328 self.objc_module_info_size = v_uint32() # the (__OBJC,__module_info) section 329 self.objc_module_info_addr = v_uint64() # the (__OBJC,__module_info) section
330 331
332 -class dylib_reference(vstruct.VStruct):
333
334 - def __init__(self):
335 vstruct.VStruct.__init__(self) 336 self.flags = v_uint32() # flags to indicate the type of reference
337 338
339 -class twolevel_hints_command(vstruct.VStruct):
340
341 - def __init__(self):
342 vstruct.VStruct.__init__(self) 343 self.cmd = v_uint32() # LC_TWOLEVEL_HINTS 344 self.cmdsize = v_uint32() # sizeof(struct twolevel_hints_command) 345 self.offset = v_uint32() # offset to the hint table 346 self.nhints = v_uint32() # number of hints in the hint table
347 348
349 -class twolevel_hint(vstruct.VStruct):
350
351 - def __init__(self):
352 vstruct.VStruct.__init__(self) 353 self.itoc = v_uint32() # index into the table of contents
354
355 -class prebind_cksum_command(vstruct.VStruct):
356
357 - def __init__(self):
358 vstruct.VStruct.__init__(self) 359 self.cmd = v_uint32() # LC_PREBIND_CKSUM 360 self.cmdsize = v_uint32() # sizeof(struct prebind_cksum_command) 361 self.cksum = v_uint32() # the check sum or zero
362 363
364 -class uuid_command(vstruct.VStruct):
365
366 - def __init__(self):
367 vstruct.VStruct.__init__(self) 368 self.cmd = v_uint32() # LC_UUID 369 self.cmdsize = v_uint32() # sizeof(struct uuid_command) 370 self.uuid[16] = v_uint8() # the 128-bit uuid
371 372
373 -class rpath_command(vstruct.VStruct):
374
375 - def __init__(self):
376 vstruct.VStruct.__init__(self) 377 self.cmd = v_uint32() # LC_RPATH 378 self.cmdsize = v_uint32() # includes string 379 self.path = lc_str() # path to add to run path
380 381
382 -class linkedit_data_command(vstruct.VStruct):
383
384 - def __init__(self):
385 vstruct.VStruct.__init__(self) 386 self.cmd = v_uint32() # LC_CODE_SIGNATURE or LC_SEGMENT_SPLIT_INFO 387 self.cmdsize = v_uint32() # sizeof(struct linkedit_data_command) 388 self.dataoff = v_uint32() # file offset of data in __LINKEDIT segment 389 self.datasize = v_uint32() # file size of data in __LINKEDIT segment
390 391
392 -class encryption_info_command(vstruct.VStruct):
393
394 - def __init__(self):
395 vstruct.VStruct.__init__(self) 396 self.cmd = v_uint32() # LC_ENCRYPTION_INFO 397 self.cmdsize = v_uint32() # sizeof(struct encryption_info_command) 398 self.cryptoff = v_uint32() # file offset of encrypted range 399 self.cryptsize = v_uint32() # file size of encrypted range 400 self.cryptid = v_uint32() # which enryption system, 0 means not-encrypted yet
401 402
403 -class symseg_command(vstruct.VStruct):
404
405 - def __init__(self):
406 vstruct.VStruct.__init__(self) 407 self.cmd = v_uint32() # LC_SYMSEG 408 self.cmdsize = v_uint32() # sizeof(struct symseg_command) 409 self.offset = v_uint32() # symbol segment offset 410 self.size = v_uint32() # symbol segment size in bytes
411 412
413 -class ident_command(vstruct.VStruct):
414
415 - def __init__(self):
416 vstruct.VStruct.__init__(self) 417 self.cmd = v_uint32() # LC_IDENT 418 self.cmdsize = v_uint32() # strings that follow this command
419
420 -class fvmfile_command(vstruct.VStruct):
421
422 - def __init__(self):
423 vstruct.VStruct.__init__(self) 424 self.cmd = v_uint32() # LC_FVMFILE 425 self.cmdsize = v_uint32() # includes pathname string 426 self.name = lc_str() # files pathname 427 self.header_addr = v_uint32() # files virtual address
428 429 command_classes = { 430 LC_SEGMENT: segment_command, 431 LC_SYMTAB: symtab_command, 432 LC_LOAD_DYLIB: dylib_command, 433 } 434
435 -def getCommandClass(cmdtype):
436 cls = command_classes.get(cmdtype) 437 if cls != None: 438 return cls 439 return load_command
440