- Timestamp:
- Oct 6, 2018 6:31:59 PM (6 years ago)
- Location:
- trunk
- Files:
-
- 16 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/include/iprt/err.h
r74395 r74638 2482 2482 /** Support for forwarders has not been implemented. */ 2483 2483 #define VERR_LDR_FORWARDERS_NOT_SUPPORTED (-22954) 2484 /** Only native endian Mach-O files are supported. */ 2485 #define VERR_LDRMACHO_OTHER_ENDIAN_NOT_SUPPORTED (-22955) 2486 /** The Mach-O header is bad or contains new and unsupported features. */ 2487 #define VERR_LDRMACHO_BAD_HEADER (-22956) 2488 /** The file type isn't supported. */ 2489 #define VERR_LDRMACHO_UNSUPPORTED_FILE_TYPE (-22957) 2490 /** The machine (cputype / cpusubtype combination) isn't supported. */ 2491 #define VERR_LDRMACHO_UNSUPPORTED_MACHINE (-22958) 2492 /** Bad load command(s). */ 2493 #define VERR_LDRMACHO_BAD_LOAD_COMMAND (-22959) 2494 /** Encountered an unknown load command.*/ 2495 #define VERR_LDRMACHO_UNKNOWN_LOAD_COMMAND (-22960) 2496 /** Encountered a load command that's not implemented.*/ 2497 #define VERR_LDRMACHO_UNSUPPORTED_LOAD_COMMAND (-22961) 2498 /** Bad section. */ 2499 #define VERR_LDRMACHO_BAD_SECTION (-22962) 2500 /** Encountered a section type that's not implemented.*/ 2501 #define VERR_LDRMACHO_UNSUPPORTED_SECTION (-22963) 2502 /** Encountered a init function section. */ 2503 #define VERR_LDRMACHO_UNSUPPORTED_INIT_SECTION (-22964) 2504 /** Encountered a term function section. */ 2505 #define VERR_LDRMACHO_UNSUPPORTED_TERM_SECTION (-22965) 2506 /** Encountered a section type that's not known to the loader. (probably invalid) */ 2507 #define VERR_LDRMACHO_UNKNOWN_SECTION (-22966) 2508 /** The sections aren't ordered by segment as expected by the loader. */ 2509 #define VERR_LDRMACHO_BAD_SECTION_ORDER (-22967) 2510 /** The image is 32-bit and contains 64-bit load commands or vise versa. */ 2511 #define VERR_LDRMACHO_BIT_MIX (-22968) 2512 /** Bad MH_OBJECT file. */ 2513 #define VERR_LDRMACHO_BAD_OBJECT_FILE (-22969) 2514 /** Bad symbol table entry. */ 2515 #define VERR_LDRMACHO_BAD_SYMBOL (-22970) 2516 /** Unsupported fixup type. */ 2517 #define VERR_LDRMACHO_UNSUPPORTED_FIXUP_TYPE (-22971) 2518 /** Both debug and non-debug sections in segment. */ 2519 #define VERR_LDRMACHO_MIXED_DEBUG_SECTION_FLAGS (-22972) 2520 /** The segment bits are non-contiguous in the file. */ 2521 #define VERR_LDRMACHO_NON_CONT_SEG_BITS (-22973) 2522 /** Hit a todo in the mach-o loader. */ 2523 #define VERR_LDRMACHO_TODO (-22974) 2524 /** Bad symbol table size in Mach-O image. */ 2525 #define VERR_LDRMACHO_BAD_SYMTAB_SIZE (-22975) 2526 /** Duplicate segment name. */ 2527 #define VERR_LDR_DUPLICATE_SEGMENT_NAME (-22976) 2528 /** No image UUID. */ 2529 #define VERR_LDR_NO_IMAGE_UUID (-22977) 2530 /** Bad image relocation. */ 2531 #define VERR_LDR_BAD_FIXUP (-22978) 2532 /** Address overflow. */ 2533 #define VERR_LDR_ADDRESS_OVERFLOW (-22979) 2534 /** validation of LX header failed. */ 2535 #define VERR_LDRLX_BAD_HEADER (-22980) 2536 /** validation of the loader section (in the LX header) failed. */ 2537 #define VERR_LDRLX_BAD_LOADER_SECTION (-22981) 2538 /** validation of the fixup section (in the LX header) failed. */ 2539 #define VERR_LDRLX_BAD_FIXUP_SECTION (-22982) 2540 /** validation of the LX object table failed. */ 2541 #define VERR_LDRLX_BAD_OBJECT_TABLE (-22983) 2542 /** A bad page map entry was encountered. */ 2543 #define VERR_LDRLX_BAD_PAGE_MAP (-22984) 2544 /** Bad iterdata (EXEPACK) data. */ 2545 #define VERR_LDRLX_BAD_ITERDATA (-22985) 2546 /** Bad iterdata2 (EXEPACK2) data. */ 2547 #define VERR_LDRLX_BAD_ITERDATA2 (-22986) 2548 /** Bad bundle data. */ 2549 #define VERR_LDRLX_BAD_BUNDLE (-22987) 2550 /** No soname. */ 2551 #define VERR_LDRLX_NO_SONAME (-22988) 2552 /** Bad soname. */ 2553 #define VERR_LDRLX_BAD_SONAME (-22989) 2554 /** Bad forwarder entry. */ 2555 #define VERR_LDRLX_BAD_FORWARDER (-22990) 2556 /** internal fixup chain isn't implemented yet. */ 2557 #define VERR_LDRLX_NRICHAIN_NOT_SUPPORTED (-22991) 2558 /** Import module ordinal is out of bounds. */ 2559 #define VERR_LDRLX_IMPORT_ORDINAL_OUT_OF_BOUNDS (-22992) 2484 2560 /** @} */ 2485 2561 -
trunk/include/iprt/formats/lx.h
r74637 r74638 1 /* $Id 1 /* $Id$ */ 2 2 /** @file 3 3 * LX structures, types and defines. … … 29 29 */ 30 30 31 #ifndef ___ k_kLdrFmts_lx_h___32 #define ___ k_kLdrFmts_lx_h___33 34 #include < k/kDefs.h>35 #include < k/kTypes.h>31 #ifndef ___iprt_formats_lx_h___ 32 #define ___iprt_formats_lx_h___ 33 34 #include <iprt/types.h> 35 #include <iprt/assertcompile.h> 36 36 37 37 … … 41 41 #endif 42 42 43 #pragma pack(1) 43 #pragma pack(1) /** @todo mostly unnecessary. */ 44 44 45 45 /** … … 49 49 struct e32_exe 50 50 { 51 KU8e32_magic[2];52 KU8e32_border;53 KU8e32_worder;54 KU32e32_level;55 KU16e32_cpu;56 KU16e32_os;57 KU32e32_ver;58 KU32e32_mflags;59 KU32e32_mpages;60 KU32e32_startobj;61 KU32e32_eip;62 KU32e32_stackobj;63 KU32e32_esp;64 KU32e32_pagesize;65 KU32e32_pageshift;51 uint8_t e32_magic[2]; 52 uint8_t e32_border; 53 uint8_t e32_worder; 54 uint32_t e32_level; 55 uint16_t e32_cpu; 56 uint16_t e32_os; 57 uint32_t e32_ver; 58 uint32_t e32_mflags; 59 uint32_t e32_mpages; 60 uint32_t e32_startobj; 61 uint32_t e32_eip; 62 uint32_t e32_stackobj; 63 uint32_t e32_esp; 64 uint32_t e32_pagesize; 65 uint32_t e32_pageshift; 66 66 /** The size of the fixup section. 67 67 * The fixup section consists of the fixup page table, the fixup record table, 68 68 * the import module table, and the import procedure name table. 69 69 */ 70 KU32e32_fixupsize;71 KU32e32_fixupsum;70 uint32_t e32_fixupsize; 71 uint32_t e32_fixupsum; 72 72 /** The size of the resident loader section. 73 73 * This includes the object table, the object page map table, the resource table, the resident name table, 74 74 * the entry table, the module format directives table, and the page checksum table (?). */ 75 KU32e32_ldrsize;75 uint32_t e32_ldrsize; 76 76 /** The checksum of the loader section. 0 if not calculated. */ 77 KU32e32_ldrsum;77 uint32_t e32_ldrsum; 78 78 /** The offset of the object table relative to this structure. */ 79 KU32e32_objtab;79 uint32_t e32_objtab; 80 80 /** Count of objects. */ 81 KU32e32_objcnt;81 uint32_t e32_objcnt; 82 82 /** The offset of the object page map table relative to this structure. */ 83 KU32e32_objmap;83 uint32_t e32_objmap; 84 84 /** The offset of the object iterated pages (whatever this is used for) relative to the start of the file. */ 85 KU32e32_itermap;85 uint32_t e32_itermap; 86 86 /** The offset of the resource table relative to this structure. */ 87 KU32e32_rsrctab;87 uint32_t e32_rsrctab; 88 88 /** The number of entries in the resource table. */ 89 KU32e32_rsrccnt;89 uint32_t e32_rsrccnt; 90 90 /** The offset of the resident name table relative to this structure. */ 91 KU32e32_restab;91 uint32_t e32_restab; 92 92 /** The offset of the entry (export) table relative to this structure. */ 93 KU32e32_enttab;93 uint32_t e32_enttab; 94 94 /** The offset of the module format directives table relative to this structure. */ 95 KU32e32_dirtab;95 uint32_t e32_dirtab; 96 96 /** The number of entries in the module format directives table. */ 97 KU32e32_dircnt;97 uint32_t e32_dircnt; 98 98 /** The offset of the fixup page table relative to this structure. */ 99 KU32e32_fpagetab;99 uint32_t e32_fpagetab; 100 100 /** The offset of the fixup record table relative to this structure. */ 101 KU32e32_frectab;101 uint32_t e32_frectab; 102 102 /** The offset of the import module name table relative to this structure. */ 103 KU32e32_impmod;103 uint32_t e32_impmod; 104 104 /** The number of entries in the import module name table. */ 105 KU32e32_impmodcnt;105 uint32_t e32_impmodcnt; 106 106 /** The offset of the import procedure name table relative to this structure. */ 107 KU32e32_impproc;107 uint32_t e32_impproc; 108 108 /** The offset of the page checksum table relative to this structure. */ 109 KU32e32_pagesum;109 uint32_t e32_pagesum; 110 110 /** The offset of the data pages relative to the start of the file. */ 111 KU32e32_datapage;111 uint32_t e32_datapage; 112 112 /** The number of preload pages (ignored). */ 113 KU32e32_preload;113 uint32_t e32_preload; 114 114 /** The offset of the non-resident name table relative to the start of the file. */ 115 KU32e32_nrestab;115 uint32_t e32_nrestab; 116 116 /** The size of the non-resident name table. */ 117 KU32 e32_cbnrestab; 118 KU32 e32_nressum; 119 KU32 e32_autodata; 120 KU32 e32_debuginfo; 121 KU32 e32_debuglen; 122 KU32 e32_instpreload; 123 KU32 e32_instdemand; 124 KU32 e32_heapsize; 125 KU32 e32_stacksize; 126 KU8 e32_res3[20]; 127 }; 117 uint32_t e32_cbnrestab; 118 uint32_t e32_nressum; 119 uint32_t e32_autodata; 120 uint32_t e32_debuginfo; 121 uint32_t e32_debuglen; 122 uint32_t e32_instpreload; 123 uint32_t e32_instdemand; 124 uint32_t e32_heapsize; 125 uint32_t e32_stacksize; 126 uint8_t e32_res3[20]; 127 }; 128 AssertCompileSize(struct e32_exe, 196); 128 129 129 130 /** e32_magic[0] */ … … 142 143 #define E32BEWO 1 143 144 /** e32_level */ 144 #define E32LEVEL KU32_C(0)145 #define E32LEVEL UINT32_C(0) 145 146 /** e32_cpu - 80286 */ 146 147 #define E32CPU286 1 … … 150 151 #define E32CPU486 3 151 152 /** e32_pagesize */ 152 #define OBJPAGELEN KU32_C(0x1000)153 #define OBJPAGELEN UINT32_C(0x1000) 153 154 154 155 … … 156 157 * @{ */ 157 158 /** App Type: Fullscreen only. */ 158 #define E32NOPMW KU32_C(0x00000100)159 #define E32NOPMW UINT32_C(0x00000100) 159 160 /** App Type: PM API. */ 160 #define E32PMAPI KU32_C(0x00000300)161 #define E32PMAPI UINT32_C(0x00000300) 161 162 /** App Type: PM VIO compatible. */ 162 #define E32PMW KU32_C(0x00000200)163 #define E32PMW UINT32_C(0x00000200) 163 164 /** Application type mask. */ 164 #define E32APPMASK KU32_C(0x00000300)165 #define E32APPMASK UINT32_C(0x00000300) 165 166 /** Executable module. */ 166 #define E32MODEXE KU32_C(0x00000000)167 #define E32MODEXE UINT32_C(0x00000000) 167 168 /** Dynamic link library (DLL / library) module. */ 168 #define E32MODDLL KU32_C(0x00008000)169 #define E32MODDLL UINT32_C(0x00008000) 169 170 /** Protected memory DLL. */ 170 #define E32PROTDLL KU32_C(0x00010000)171 #define E32PROTDLL UINT32_C(0x00010000) 171 172 /** Physical Device Driver. */ 172 #define E32MODPDEV KU32_C(0x00020000)173 #define E32MODPDEV UINT32_C(0x00020000) 173 174 /** Virtual Device Driver. */ 174 #define E32MODVDEV KU32_C(0x00028000)175 #define E32MODVDEV UINT32_C(0x00028000) 175 176 /** Device driver */ 176 177 #define E32DEVICE E32MODPDEV … … 180 181 #define E32MODPROTDLL (E32MODDLL | E32PROTDLL) 181 182 /** Module Type mask. */ 182 #define E32MODMASK KU32_C(0x00038000)183 #define E32MODMASK UINT32_C(0x00038000) 183 184 /** Not loadable (linker error). */ 184 #define E32NOLOAD KU32_C(0x00002000)185 #define E32NOLOAD UINT32_C(0x00002000) 185 186 /** No internal fixups. */ 186 #define E32NOINTFIX KU32_C(0x00000010)187 #define E32NOINTFIX UINT32_C(0x00000010) 187 188 /** No external fixups (i.e. imports). */ 188 #define E32NOEXTFIX KU32_C(0x00000020)189 #define E32NOEXTFIX UINT32_C(0x00000020) 189 190 /** System DLL, no internal fixups. */ 190 #define E32SYSDLL KU32_C(0x00000008)191 #define E32SYSDLL UINT32_C(0x00000008) 191 192 /** Global (set) or per instance (cleared) library initialization. */ 192 #define E32LIBINIT KU32_C(0x00000004)193 #define E32LIBINIT UINT32_C(0x00000004) 193 194 /** Global (set) or per instance (cleared) library termination. */ 194 #define E32LIBTERM KU32_C(0x40000000)195 #define E32LIBTERM UINT32_C(0x40000000) 195 196 /** Indicates when set in an executable that the process isn't SMP safe. */ 196 #define E32NOTMPSAFE KU32_C(0x00080000)197 #define E32NOTMPSAFE UINT32_C(0x00080000) 197 198 /** @} */ 198 199 … … 201 202 typedef union _offset 202 203 { 203 KU16offset16;204 KU32offset32;204 uint16_t offset16; 205 uint32_t offset32; 205 206 } offset; 206 207 … … 210 211 struct r32_rlc 211 212 { 212 KU8nr_stype;213 KU8nr_flags;214 KI16r32_soff;215 KU16r32_objmod;213 uint8_t nr_stype; 214 uint8_t nr_flags; 215 int16_t r32_soff; 216 uint16_t r32_objmod; 216 217 217 218 union targetid … … 221 222 { 222 223 offset proc; 223 KU32ord;224 uint32_t ord; 224 225 } extref; 225 226 struct addfixup 226 227 { 227 KU16entry;228 uint16_t entry; 228 229 offset addval; 229 230 } addfix; 230 231 } r32_target; 231 KU16r32_srccount;232 KU16r32_chain;232 uint16_t r32_srccount; 233 uint16_t r32_chain; 233 234 }; 234 235 … … 285 286 { 286 287 /** The size of the object. */ 287 KU32o32_size;288 uint32_t o32_size; 288 289 /** The base address of the object. */ 289 KU32o32_base;290 uint32_t o32_base; 290 291 /** Object flags. */ 291 KU32o32_flags;292 uint32_t o32_flags; 292 293 /** Page map index. */ 293 KU32o32_pagemap;294 uint32_t o32_pagemap; 294 295 /** Page map size. (doesn't need to be o32_size >> page shift). */ 295 KU32o32_mapsize;296 uint32_t o32_mapsize; 296 297 /** Reserved */ 297 KU32o32_reserved;298 uint32_t o32_reserved; 298 299 }; 299 300 … … 301 302 * @{ */ 302 303 /** Read access. */ 303 #define OBJREAD KU32_C(0x00000001)304 #define OBJREAD UINT32_C(0x00000001) 304 305 /** Write access. */ 305 #define OBJWRITE KU32_C(0x00000002)306 #define OBJWRITE UINT32_C(0x00000002) 306 307 /** Execute access. */ 307 #define OBJEXEC KU32_C(0x00000004)308 #define OBJEXEC UINT32_C(0x00000004) 308 309 /** Resource object. */ 309 #define OBJRSRC KU32_C(0x00000008)310 #define OBJRSRC UINT32_C(0x00000008) 310 311 /** The object is discarable (i.e. don't swap, just load in pages from the executable). 311 312 * This overlaps a bit with object type. */ 312 #define OBJDISCARD KU32_C(0x00000010)313 #define OBJDISCARD UINT32_C(0x00000010) 313 314 /** The object is shared. */ 314 #define OBJSHARED KU32_C(0x00000020)315 #define OBJSHARED UINT32_C(0x00000020) 315 316 /** The object has preload pages. */ 316 #define OBJPRELOAD KU32_C(0x00000040)317 #define OBJPRELOAD UINT32_C(0x00000040) 317 318 /** The object has invalid pages. */ 318 #define OBJINVALID KU32_C(0x00000080)319 #define OBJINVALID UINT32_C(0x00000080) 319 320 /** Non-permanent, link386 bug. */ 320 #define LNKNONPERM KU32_C(0x00000600)321 #define LNKNONPERM UINT32_C(0x00000600) 321 322 /** Non-permanent, correct 'value'. */ 322 #define OBJNONPERM KU32_C(0x00000000)323 #define OBJNONPERM UINT32_C(0x00000000) 323 324 /** Obj Type: The object is permanent and swappable. */ 324 #define OBJPERM KU32_C(0x00000100)325 #define OBJPERM UINT32_C(0x00000100) 325 326 /** Obj Type: The object is permanent and resident (i.e. not swappable). */ 326 #define OBJRESIDENT KU32_C(0x00000200)327 #define OBJRESIDENT UINT32_C(0x00000200) 327 328 /** Obj Type: The object is resident and contigious. */ 328 #define OBJCONTIG KU32_C(0x00000300)329 #define OBJCONTIG UINT32_C(0x00000300) 329 330 /** Obj Type: The object is permanent and long locable. */ 330 #define OBJDYNAMIC KU32_C(0x00000400)331 #define OBJDYNAMIC UINT32_C(0x00000400) 331 332 /** Object type mask. */ 332 #define OBJTYPEMASK KU32_C(0x00000700)333 #define OBJTYPEMASK UINT32_C(0x00000700) 333 334 /** x86: The object require an 16:16 alias. */ 334 #define OBJALIAS16 KU32_C(0x00001000)335 #define OBJALIAS16 UINT32_C(0x00001000) 335 336 /** x86: Big/Default selector setting, i.e. toggle 32-bit or 16-bit. */ 336 #define OBJBIGDEF KU32_C(0x00002000)337 #define OBJBIGDEF UINT32_C(0x00002000) 337 338 /** x86: conforming selector setting (weird stuff). */ 338 #define OBJCONFORM KU32_C(0x00004000)339 #define OBJCONFORM UINT32_C(0x00004000) 339 340 /** x86: IOPL. */ 340 #define OBJIOPL KU32_C(0x00008000)341 #define OBJIOPL UINT32_C(0x00008000) 341 342 /** @} */ 342 343 … … 345 346 { 346 347 /** The file offset of the page. */ 347 KU32o32_pagedataoffset;348 uint32_t o32_pagedataoffset; 348 349 /** The number of bytes of raw page data. */ 349 KU16o32_pagesize;350 uint16_t o32_pagesize; 350 351 /** Per page flags describing how the page is encoded in the file. */ 351 KU16o32_pageflags;352 uint16_t o32_pageflags; 352 353 }; 353 354 … … 356 357 */ 357 358 /** Raw page (uncompressed) in the file. */ 358 #define VALID KU16_C(0x0000)359 #define VALID UINT16_C(0x0000) 359 360 /** RLE encoded page in file. */ 360 #define ITERDATA KU16_C(0x0001)361 #define ITERDATA UINT16_C(0x0001) 361 362 /** Invalid page, nothing in the file. */ 362 #define INVALID KU16_C(0x0002)363 #define INVALID UINT16_C(0x0002) 363 364 /** Zero page, nothing in file. */ 364 #define ZEROED KU16_C(0x0003)365 #define ZEROED UINT16_C(0x0003) 365 366 /** range of pages (what is this?) */ 366 #define RANGE KU16_C(0x0004)367 #define RANGE UINT16_C(0x0004) 367 368 /** Compressed page in file. */ 368 #define ITERDATA2 KU16_C(0x0005)369 #define ITERDATA2 UINT16_C(0x0005) 369 370 /** @} */ 370 371 … … 374 375 { 375 376 /** Number of iterations. */ 376 KU16LX_nIter;377 uint16_t LX_nIter; 377 378 /** The number of bytes that's being iterated. */ 378 KU16LX_nBytes;379 uint16_t LX_nBytes; 379 380 /** The bytes. */ 380 KU8LX_Iterdata;381 uint8_t LX_Iterdata; 381 382 }; 382 383 … … 388 389 { 389 390 /** Resource Type. */ 390 KU16type;391 uint16_t type; 391 392 /** Resource ID. */ 392 KU16name;393 uint16_t name; 393 394 /** Resource size in bytes. */ 394 KU32cb;395 uint32_t cb; 395 396 /** The index of the object containing the resource. */ 396 KU16obj;397 uint16_t obj; 397 398 /** Offset of the resource that within the object. */ 398 KU32offset;399 uint32_t offset; 399 400 }; 400 401 … … 408 409 { 409 410 /** The number of entries. */ 410 KU8b32_cnt;411 uint8_t b32_cnt; 411 412 /** The type of bundle. */ 412 KU8b32_type;413 uint8_t b32_type; 413 414 /** The index of the object containing these entry points. */ 414 KU16b32_obj;415 uint16_t b32_obj; 415 416 }; 416 417 … … 436 437 { 437 438 /** Entry point flags */ 438 KU8e32_flags; /* Entry point flags */439 uint8_t e32_flags; /* Entry point flags */ 439 440 union entrykind 440 441 { … … 442 443 offset e32_offset; 443 444 /** GATE16 */ 444 struct callgate445 struct scallgate 445 446 { 446 447 /** Offset into segment. */ 447 KU16offset;448 uint16_t offset; 448 449 /** The callgate selector */ 449 KU16callgate;450 uint16_t callgate; 450 451 } e32_callgate; 451 452 /** ENTRYFWD */ … … 453 454 { 454 455 /** Module ordinal number (i.e. into the import module table). */ 455 KU16modord;456 uint16_t modord; 456 457 /** Procedure name or ordinal number. */ 457 KU32value;458 uint32_t value; 458 459 } e32_fwd; 459 460 } e32_variant; … … 484 485 #endif 485 486 487 -
trunk/include/iprt/formats/mach-o.h
r72691 r74638 480 480 } uuid_command_t; 481 481 482 typedef struct linkedit_data_command 483 { 484 uint32_t cmd; /**< LC_CODE_SIGNATURE, LC_SEGMENT_SPLIT_INFO, LC_FUNCTION_STARTS */ 485 uint32_t cmdsize; /**< Size of this structure. */ 486 uint32_t dataoff; /**< Offset into the file of the data. */ 487 uint32_t datasize; /**< The size of the data. */ 488 } linkedit_data_command_t; 489 490 typedef struct version_min_command 491 { 492 uint32_t cmd; /**< LC_VERSION_MIN_MACOSX, LC_VERSION_MIN_IPHONEOS */ 493 uint32_t cmdsize; /**< Size of this structure. */ 494 uint32_t version; /**< 31..16=major, 15..8=minor, 7..0=patch. */ 495 uint32_t reserved; /**< MBZ. */ 496 } version_min_command_t; 497 482 498 typedef struct macho_nlist_32 483 499 { -
trunk/include/iprt/types.h
r73665 r74638 3012 3012 /** Pointer to a 64-bit unsigned value. */ 3013 3013 uint64_t RT_FAR *pu64; 3014 /** Pointer to a 8-bit signed value. */ 3015 int8_t RT_FAR *pi8; 3016 /** Pointer to a 16-bit signed value. */ 3017 int16_t RT_FAR *pi16; 3018 /** Pointer to a 32-bit signed value. */ 3019 int32_t RT_FAR *pi32; 3020 /** Pointer to a 64-bit signed value. */ 3021 int64_t RT_FAR *pi64; 3014 3022 /** Pointer to a UTF-16 character. */ 3015 3023 PRTUTF16 pwc; … … 3051 3059 /** Pointer to a 64-bit unsigned value. */ 3052 3060 uint64_t const RT_FAR *pu64; 3061 /** Pointer to a 8-bit signed value. */ 3062 int8_t const RT_FAR *pi8; 3063 /** Pointer to a 16-bit signed value. */ 3064 int16_t const RT_FAR *pi16; 3065 /** Pointer to a 32-bit signed value. */ 3066 int32_t const RT_FAR *pi32; 3067 /** Pointer to a 64-bit signed value. */ 3068 int64_t const RT_FAR *pi64; 3053 3069 /** Pointer to a UTF-16 character. */ 3054 3070 PCRTUTF16 pwc; … … 3083 3099 unsigned long volatile RT_FAR *pul; 3084 3100 /** Pointer to a 8-bit unsigned value. */ 3085 uint8_t volatile RT_FAR *pu8;3101 uint8_t volatile RT_FAR *pu8; 3086 3102 /** Pointer to a 16-bit unsigned value. */ 3087 3103 uint16_t volatile RT_FAR *pu16; … … 3090 3106 /** Pointer to a 64-bit unsigned value. */ 3091 3107 uint64_t volatile RT_FAR *pu64; 3108 /** Pointer to a 8-bit signed value. */ 3109 int8_t volatile RT_FAR *pi8; 3110 /** Pointer to a 16-bit signed value. */ 3111 int16_t volatile RT_FAR *pi16; 3112 /** Pointer to a 32-bit signed value. */ 3113 int32_t volatile RT_FAR *pi32; 3114 /** Pointer to a 64-bit signed value. */ 3115 int64_t volatile RT_FAR *pi64; 3092 3116 /** Pointer to a UTF-16 character. */ 3093 3117 RTUTF16 volatile RT_FAR *pwc; … … 3129 3153 /** Pointer to a 64-bit unsigned value. */ 3130 3154 uint64_t const volatile RT_FAR *pu64; 3155 /** Pointer to a 8-bit signed value. */ 3156 int8_t const volatile RT_FAR *pi8; 3157 /** Pointer to a 16-bit signed value. */ 3158 int16_t const volatile RT_FAR *pi16; 3159 /** Pointer to a 32-bit signed value. */ 3160 int32_t const volatile RT_FAR *pi32; 3161 /** Pointer to a 64-bit signed value. */ 3162 int64_t const volatile RT_FAR *pi64; 3131 3163 /** Pointer to a UTF-16 character. */ 3132 3164 RTUTF16 const volatile RT_FAR *pwc; -
trunk/src/VBox/Runtime/.scm-settings
r70384 r74638 97 97 /r3/win/VBoxRT-msvcr100-win32.def : --external-copyright 98 98 99 /common/ldr/ldrLX.cpp : --external-copyright 100 /common/ldr/ldrMachO.cpp : --external-copyright 101 99 102 /common/math/x86/fenv-x86.c : --external-copyright --no-convert-tabs 100 103 -
trunk/src/VBox/Runtime/Makefile.kmk
r74460 r74638 265 265 LDR_WITH_NATIVE \ 266 266 LDR_WITH_ELF32 \ 267 LDR_WITH_LX \ 267 268 LDR_WITH_PE \ 268 269 RT_WITH_VBOX \ … … 271 272 IPRT_WITH_OPENSSL \ 272 273 NOFILEID 274 # += LDR_WITH_MACHO 273 275 if1of ($(KBUILD_TARGET_ARCH), amd64 x86) 274 276 RuntimeR3_DEFS += \ … … 456 458 common/ldr/ldrFile.cpp \ 457 459 common/ldr/ldrVfsFile.cpp \ 460 common/ldr/ldrLX.cpp \ 461 common/ldr/ldrMachO.cpp \ 458 462 common/ldr/ldrMemory.cpp \ 459 463 common/ldr/ldrNative.cpp \ … … 1625 1629 common/ldr/ldrFile.cpp \ 1626 1630 common/ldr/ldrkStuff.cpp \ 1631 common/ldr/ldrLX.cpp \ 1632 common/ldr/ldrMachO.cpp \ 1627 1633 common/ldr/ldrNative.cpp \ 1628 1634 common/ldr/ldrPE.cpp \ -
trunk/src/VBox/Runtime/common/ldr/ldrELF.cpp
r73531 r74638 121 121 * @param pErrInfo Where to return extended error information. Optional. 122 122 */ 123 intrtldrELFOpen(PRTLDRREADER pReader, uint32_t fFlags, RTLDRARCH enmArch, PRTLDRMOD phLdrMod, PRTERRINFO pErrInfo)123 DECLHIDDEN(int) rtldrELFOpen(PRTLDRREADER pReader, uint32_t fFlags, RTLDRARCH enmArch, PRTLDRMOD phLdrMod, PRTERRINFO pErrInfo) 124 124 { 125 125 const char *pszLogName = pReader->pfnLogName(pReader); NOREF(pszLogName); -
trunk/src/VBox/Runtime/common/ldr/ldrEx.cpp
r73759 r74638 42 42 #include <iprt/string.h> 43 43 #include <iprt/formats/mz.h> 44 #include <iprt/formats/mach-o.h> 44 45 #include "internal/ldr.h" 45 46 … … 81 82 && uSign.u32 != IMAGE_NT_SIGNATURE 82 83 && uSign.u32 != IMAGE_ELF_SIGNATURE 83 && uSign.au16[0] != IMAGE_LX_SIGNATURE) 84 && uSign.au16[0] != IMAGE_LX_SIGNATURE 85 && uSign.u32 != IMAGE_MACHO64_SIGNATURE 86 && uSign.u32 != IMAGE_MACHO64_SIGNATURE_OE 87 && uSign.u32 != IMAGE_MACHO32_SIGNATURE 88 && uSign.u32 != IMAGE_MACHO32_SIGNATURE_OE 89 && uSign.u32 != IMAGE_FAT_SIGNATURE 90 && uSign.u32 != IMAGE_FAT_SIGNATURE_OE ) 84 91 { 85 92 Log(("rtldrOpenWithReader: %s: unknown magic %#x / '%.4s\n", pReader->pfnLogName(pReader), uSign.u32, &uSign.ach[0])); … … 127 134 rc = VERR_ELF_EXE_NOT_SUPPORTED; 128 135 #endif 136 else if ( uSign.u32 == IMAGE_MACHO64_SIGNATURE 137 || uSign.u32 == IMAGE_MACHO64_SIGNATURE_OE 138 || uSign.u32 == IMAGE_MACHO32_SIGNATURE 139 || uSign.u32 == IMAGE_MACHO32_SIGNATURE_OE) 140 #if defined(LDR_WITH_MACHO) 141 rc = rtldrMachOOpen(pReader, fFlags, enmArch, offHdr, phMod, pErrInfo); 142 #else 143 rc = VERR_INVALID_EXE_SIGNATURE; 144 #endif 145 else if ( uSign.u32 == IMAGE_FAT_SIGNATURE 146 || uSign.u32 == IMAGE_FAT_SIGNATURE_OE) 147 #if defined(LDR_WITH_MACHO) 148 rc = rtldrFatOpen(pReader, fFlags, enmArch, phMod, pErrInfo); 149 #else 150 rc = VERR_INVALID_EXE_SIGNATURE; 151 #endif 129 152 else if (uSign.au16[0] == IMAGE_LX_SIGNATURE) 130 153 #ifdef LDR_WITH_LX -
trunk/src/VBox/Runtime/common/ldr/ldrLX.cpp
r74636 r74638 29 29 */ 30 30 31 /******************************************************************************* 32 * Header Files * 33 *******************************************************************************/ 34 #include <k/kLdr.h> 35 #include "kLdrInternal.h" 36 #include <k/kLdrFmts/lx.h> 37 38 39 /******************************************************************************* 40 * Defined Constants And Macros * 41 *******************************************************************************/ 31 32 /********************************************************************************************************************************* 33 * Header Files * 34 *********************************************************************************************************************************/ 35 #define LOG_GROUP RTLOGGROUP_LDR 36 #include <iprt/ldr.h> 37 #include "internal/iprt.h" 38 39 #include <iprt/asm.h> 40 #include <iprt/assert.h> 41 #include <iprt/err.h> 42 #include <iprt/log.h> 43 #include <iprt/mem.h> 44 #include <iprt/string.h> 45 46 #include <iprt/formats/lx.h> 47 #include "internal/ldr.h" 48 49 50 /********************************************************************************************************************************* 51 * Defined Constants And Macros * 52 *********************************************************************************************************************************/ 42 53 /** @def KLDRMODLX_STRICT 43 54 * Define KLDRMODLX_STRICT to enabled strict checks in KLDRMODLX. */ … … 48 59 */ 49 60 #ifdef KLDRMODLX_STRICT 50 # define KLDRMODLX_ASSERT(expr) kHlpAssert(expr)61 # define KLDRMODLX_ASSERT(expr) Assert(expr) 51 62 #else 52 63 # define KLDRMODLX_ASSERT(expr) do {} while (0) … … 54 65 55 66 56 /******************************************************************************* 57 * Structures and Typedefs *58 ******************************************************************************* /67 /********************************************************************************************************************************* 68 * Structures and Typedefs * 69 *********************************************************************************************************************************/ 59 70 /** 60 71 * Instance data for the LX module interpreter. … … 62 73 typedef struct KLDRMODLX 63 74 { 64 /** Pointer to the module. (Follows the section table.) */ 65 PKLDRMOD pMod; 75 /** Core module structure. */ 76 RTLDRMODINTERNAL Core; 77 66 78 /** Pointer to the user mapping. */ 67 79 const void *pvMapping; 68 80 /** The size of the mapped LX image. */ 69 KSIZEcbMapped;81 size_t cbMapped; 70 82 /** Reserved flags. */ 71 KU32f32Reserved;83 uint32_t f32Reserved; 72 84 73 85 /** The offset of the LX header. */ 74 KLDRFOFFoffHdr;86 RTFOFF offHdr; 75 87 /** Copy of the LX header. */ 76 88 struct e32_exe Hdr; … … 78 90 /** Pointer to the loader section. 79 91 * Allocated together with this strcture. */ 80 const KU8*pbLoaderSection;92 const uint8_t *pbLoaderSection; 81 93 /** Pointer to the last byte in the loader section. */ 82 const KU8*pbLoaderSectionLast;94 const uint8_t *pbLoaderSectionLast; 83 95 /** Pointer to the object table in the loader section. */ 84 96 const struct o32_obj *paObjs; … … 88 100 const struct rsrc32 *paRsrcs; 89 101 /** Pointer to the resident name table in the loader section. */ 90 const KU8*pbResNameTab;102 const uint8_t *pbResNameTab; 91 103 /** Pointer to the entry table in the loader section. */ 92 const KU8*pbEntryTab;104 const uint8_t *pbEntryTab; 93 105 94 106 /** Pointer to the non-resident name table. */ 95 KU8*pbNonResNameTab;107 uint8_t *pbNonResNameTab; 96 108 /** Pointer to the last byte in the non-resident name table. */ 97 const KU8*pbNonResNameTabLast;109 const uint8_t *pbNonResNameTabLast; 98 110 99 111 /** Pointer to the fixup section. */ 100 KU8*pbFixupSection;112 uint8_t *pbFixupSection; 101 113 /** Pointer to the last byte in the fixup section. */ 102 const KU8*pbFixupSectionLast;114 const uint8_t *pbFixupSectionLast; 103 115 /** Pointer to the fixup page table within pvFixupSection. */ 104 const KU32*paoffPageFixups;116 const uint32_t *paoffPageFixups; 105 117 /** Pointer to the fixup record table within pvFixupSection. */ 106 const KU8*pbFixupRecs;118 const uint8_t *pbFixupRecs; 107 119 /** Pointer to the import module name table within pvFixupSection. */ 108 const KU8*pbImportMods;120 const uint8_t *pbImportMods; 109 121 /** Pointer to the import module name table within pvFixupSection. */ 110 const KU8 *pbImportProcs; 122 const uint8_t *pbImportProcs; 123 124 /** Pointer to the module name (in the resident name table). */ 125 const char *pszName; 126 /** The name length. */ 127 size_t cchName; 128 129 /** The target CPU. */ 130 RTLDRCPU enmCpu; 131 /** Number of segments in aSegments. */ 132 uint32_t cSegments; 133 /** Segment info. */ 134 RTLDRSEG aSegments[RT_FLEXIBLE_ARRAY]; 111 135 } KLDRMODLX, *PKLDRMODLX; 112 136 113 137 114 /******************************************************************************* 115 * Internal Functions * 116 *******************************************************************************/ 117 static int kldrModLXHasDbgInfo(PKLDRMOD pMod, const void *pvBits); 118 static int kldrModLXRelocateBits(PKLDRMOD pMod, void *pvBits, KLDRADDR NewBaseAddress, KLDRADDR OldBaseAddress, 119 PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser); 120 static int kldrModLXDoCreate(PKRDR pRdr, KLDRFOFF offNewHdr, PKLDRMODLX *ppModLX); 121 static const KU8 *kldrModLXDoNameTableLookupByOrdinal(const KU8 *pbNameTable, KSSIZE cbNameTable, KU32 iOrdinal); 122 static int kldrModLXDoNameLookup(PKLDRMODLX pModLX, const char *pchSymbol, KSIZE cchSymbol, KU32 *piSymbol); 123 static const KU8 *kldrModLXDoNameTableLookupByName(const KU8 *pbNameTable, KSSIZE cbNameTable, 124 const char *pchSymbol, KSIZE cchSymbol); 138 /********************************************************************************************************************************* 139 * Internal Functions * 140 *********************************************************************************************************************************/ 141 static int kldrModLXHasDbgInfo(PRTLDRMODINTERNAL pMod, const void *pvBits); 142 static DECLCALLBACK(int) rtldrLX_RelocateBits(PRTLDRMODINTERNAL pMod, void *pvBits, RTUINTPTR NewBaseAddress, 143 RTUINTPTR OldBaseAddress, PFNRTLDRIMPORT pfnGetImport, void *pvUser); 144 static const uint8_t *kldrModLXDoNameTableLookupByOrdinal(const uint8_t *pbNameTable, ssize_t cbNameTable, uint32_t iOrdinal); 145 static int kldrModLXDoNameLookup(PKLDRMODLX pModLX, const char *pchSymbol, size_t cchSymbol, uint32_t *piSymbol); 146 static const uint8_t *kldrModLXDoNameTableLookupByName(const uint8_t *pbNameTable, ssize_t cbNameTable, 147 const char *pchSymbol, size_t cchSymbol); 148 static int kldrModLXGetImport(PKLDRMODLX pThis, const void *pvBits, uint32_t iImport, 149 char *pszName, size_t cchName, size_t *pcbNeeded); 125 150 static int kldrModLXDoLoadBits(PKLDRMODLX pModLX, void *pvBits); 126 static int kldrModLXDoIterDataUnpacking( KU8 *pbDst, const KU8*pbSrc, int cbSrc);127 static int kldrModLXDoIterData2Unpacking( KU8 *pbDst, const KU8*pbSrc, int cbSrc);128 static void kLdrModLXMemCopyW( KU8 *pbDst, const KU8*pbSrc, int cb);151 static int kldrModLXDoIterDataUnpacking(uint8_t *pbDst, const uint8_t *pbSrc, int cbSrc); 152 static int kldrModLXDoIterData2Unpacking(uint8_t *pbDst, const uint8_t *pbSrc, int cbSrc); 153 static void kLdrModLXMemCopyW(uint8_t *pbDst, const uint8_t *pbSrc, int cb); 129 154 static int kldrModLXDoProtect(PKLDRMODLX pModLX, void *pvBits, unsigned fUnprotectOrProtect); 130 static int kldrModLXDoCallDLL(PKLDRMODLX pModLX, void *pvMapping, unsigned uOp, KUPTRuHandle);155 static int kldrModLXDoCallDLL(PKLDRMODLX pModLX, void *pvMapping, unsigned uOp, uintptr_t uHandle); 131 156 static int kldrModLXDoForwarderQuery(PKLDRMODLX pModLX, const struct e32_entry *pEntry, 132 PFN KLDRMODGETIMPORT pfnGetForwarder, void *pvUser, PKLDRADDR puValue, KU32*pfKind);157 PFNRTLDRIMPORT pfnGetForwarder, void *pvUser, PRTLDRADDR puValue, uint32_t *pfKind); 133 158 static int kldrModLXDoLoadFixupSection(PKLDRMODLX pModLX); 134 static KI32 kldrModLXDoCall(KUPTR uEntrypoint, KUPTR uHandle, KU32 uOp, void *pvReserved); 135 static int kldrModLXDoReloc(KU8 *pbPage, int off, KLDRADDR PageAddress, const struct r32_rlc *prlc, 136 int iSelector, KLDRADDR uValue, KU32 fKind); 137 138 139 /** 140 * Create a loader module instance interpreting the executable image found 141 * in the specified file provider instance. 142 * 143 * @returns 0 on success and *ppMod pointing to a module instance. 144 * On failure, a non-zero OS specific error code is returned. 145 * @param pOps Pointer to the registered method table. 146 * @param pRdr The file provider instance to use. 147 * @param fFlags Flags, MBZ. 148 * @param enmCpuArch The desired CPU architecture. KCPUARCH_UNKNOWN means 149 * anything goes, but with a preference for the current 150 * host architecture. 151 * @param offNewHdr The offset of the new header in MZ files. -1 if not found. 152 * @param ppMod Where to store the module instance pointer. 153 */ 154 static int kldrModLXCreate(PCKLDRMODOPS pOps, PKRDR pRdr, KU32 fFlags, KCPUARCH enmCpuArch, KLDRFOFF offNewHdr, PPKLDRMOD ppMod) 155 { 156 PKLDRMODLX pModLX; 157 int rc; 158 K_NOREF(fFlags); 159 160 /* 161 * Create the instance data and do a minimal header validation. 162 */ 163 rc = kldrModLXDoCreate(pRdr, offNewHdr, &pModLX); 164 if (!rc) 165 { 166 /* 167 * Match up against the requested CPU architecture. 168 */ 169 if ( enmCpuArch == KCPUARCH_UNKNOWN 170 || pModLX->pMod->enmArch == enmCpuArch) 171 { 172 pModLX->pMod->pOps = pOps; 173 pModLX->pMod->u32Magic = KLDRMOD_MAGIC; 174 *ppMod = pModLX->pMod; 175 return 0; 176 } 177 rc = KLDR_ERR_CPU_ARCH_MISMATCH; 178 } 179 kHlpFree(pModLX); 180 return rc; 181 } 159 static int32_t kldrModLXDoCall(uintptr_t uEntrypoint, uintptr_t uHandle, uint32_t uOp, void *pvReserved); 160 static int kldrModLXDoReloc(uint8_t *pbPage, int off, RTLDRADDR PageAddress, const struct r32_rlc *prlc, 161 int iSelector, RTLDRADDR uValue, uint32_t fKind); 182 162 183 163 … … 186 166 * simplify cleanup on failure. 187 167 */ 188 static int kldrModLXDoCreate(P KRDR pRdr, KLDRFOFF offNewHdr, PKLDRMODLX *ppModLX)168 static int kldrModLXDoCreate(PRTLDRREADER pRdr, RTFOFF offNewHdr, uint32_t fFlags, PKLDRMODLX *ppModLX, PRTERRINFO pErrInfo) 189 169 { 190 170 struct e32_exe Hdr; 191 171 PKLDRMODLX pModLX; 192 PKLDRMOD pMod; 193 KSIZE cb; 194 KSIZE cchFilename; 195 KSIZE offLdrStuff; 196 KU32 off, offEnd; 197 KU32 i; 198 int rc; 172 uint32_t off, offEnd; 173 uint32_t i; 199 174 int fCanOptimizeMapping; 200 KU32 NextRVA; 175 uint32_t NextRVA; 176 177 RT_NOREF(fFlags); 201 178 *ppModLX = NULL; 202 179 … … 204 181 * Read the signature and file header. 205 182 */ 206 rc = kRdrRead(pRdr, &Hdr, sizeof(Hdr), offNewHdr > 0 ? offNewHdr : 0);207 if ( rc)208 return rc;183 int rc = pRdr->pfnRead(pRdr, &Hdr, sizeof(Hdr), offNewHdr > 0 ? offNewHdr : 0); 184 if (RT_FAILURE(rc)) 185 return RTErrInfoSetF(pErrInfo, rc, "Error reading LX header at %RTfoff: %Rrc", offNewHdr, rc); 209 186 if ( Hdr.e32_magic[0] != E32MAGIC1 210 187 || Hdr.e32_magic[1] != E32MAGIC2) 211 return KLDR_ERR_UNKNOWN_FORMAT;188 return RTErrInfoSetF(pErrInfo, VERR_INVALID_EXE_SIGNATURE, "Not LX magic: %02x %02x", Hdr.e32_magic[0], Hdr.e32_magic[1]); 212 189 213 190 /* We're not interested in anything but x86 images. */ … … 219 196 || Hdr.e32_pagesize != OBJPAGELEN 220 197 ) 221 return KLDR_ERR_LX_BAD_HEADER;198 return VERR_LDRLX_BAD_HEADER; 222 199 223 200 /* Some rough sanity checks. */ 224 offEnd = kRdrSize(pRdr) >= (KLDRFOFF)~(KU32)16 ? ~(KU32)16 : (KU32)kRdrSize(pRdr);201 offEnd = pRdr->pfnSize(pRdr) >= (RTFOFF)~(uint32_t)16 ? ~(uint32_t)16 : (uint32_t)pRdr->pfnSize(pRdr); 225 202 if ( Hdr.e32_itermap > offEnd 226 203 || Hdr.e32_datapage > offEnd … … 230 207 || Hdr.e32_fixupsize > offEnd - offNewHdr - sizeof(Hdr) 231 208 || Hdr.e32_fixupsize + Hdr.e32_ldrsize > offEnd - offNewHdr - sizeof(Hdr)) 232 return KLDR_ERR_LX_BAD_HEADER;209 return VERR_LDRLX_BAD_HEADER; 233 210 234 211 /* Verify the loader section. */ 235 212 offEnd = Hdr.e32_objtab + Hdr.e32_ldrsize; 236 213 if (Hdr.e32_objtab < sizeof(Hdr)) 237 return KLDR_ERR_LX_BAD_LOADER_SECTION;214 return VERR_LDRLX_BAD_LOADER_SECTION; 238 215 off = Hdr.e32_objtab + sizeof(struct o32_obj) * Hdr.e32_objcnt; 239 216 if (off > offEnd) 240 return KLDR_ERR_LX_BAD_LOADER_SECTION;217 return VERR_LDRLX_BAD_LOADER_SECTION; 241 218 if ( Hdr.e32_objmap 242 219 && (Hdr.e32_objmap < off || Hdr.e32_objmap > offEnd)) 243 return KLDR_ERR_LX_BAD_LOADER_SECTION;220 return VERR_LDRLX_BAD_LOADER_SECTION; 244 221 if ( Hdr.e32_rsrccnt 245 222 && ( Hdr.e32_rsrctab < off 246 223 || Hdr.e32_rsrctab > offEnd 247 224 || Hdr.e32_rsrctab + sizeof(struct rsrc32) * Hdr.e32_rsrccnt > offEnd)) 248 return KLDR_ERR_LX_BAD_LOADER_SECTION;225 return VERR_LDRLX_BAD_LOADER_SECTION; 249 226 if ( Hdr.e32_restab 250 227 && (Hdr.e32_restab < off || Hdr.e32_restab > offEnd - 2)) 251 return KLDR_ERR_LX_BAD_LOADER_SECTION;228 return VERR_LDRLX_BAD_LOADER_SECTION; 252 229 if ( Hdr.e32_enttab 253 230 && (Hdr.e32_enttab < off || Hdr.e32_enttab >= offEnd)) 254 return KLDR_ERR_LX_BAD_LOADER_SECTION;231 return VERR_LDRLX_BAD_LOADER_SECTION; 255 232 if ( Hdr.e32_dircnt 256 233 && (Hdr.e32_dirtab < off || Hdr.e32_dirtab > offEnd - 2)) 257 return KLDR_ERR_LX_BAD_LOADER_SECTION;234 return VERR_LDRLX_BAD_LOADER_SECTION; 258 235 259 236 /* Verify the fixup section. */ … … 272 249 if ( Hdr.e32_frectab 273 250 && (Hdr.e32_frectab < off || Hdr.e32_frectab > offEnd)) 274 return KLDR_ERR_LX_BAD_FIXUP_SECTION;251 return VERR_LDRLX_BAD_FIXUP_SECTION; 275 252 if ( Hdr.e32_impmod 276 253 && (Hdr.e32_impmod < off || Hdr.e32_impmod > offEnd || Hdr.e32_impmod + Hdr.e32_impmodcnt > offEnd)) 277 return KLDR_ERR_LX_BAD_FIXUP_SECTION;254 return VERR_LDRLX_BAD_FIXUP_SECTION; 278 255 if ( Hdr.e32_impproc 279 256 && (Hdr.e32_impproc < off || Hdr.e32_impproc > offEnd)) 280 return KLDR_ERR_LX_BAD_FIXUP_SECTION;257 return VERR_LDRLX_BAD_FIXUP_SECTION; 281 258 282 259 /* 283 260 * Calc the instance size, allocate and initialize it. 284 261 */ 285 cchFilename = kHlpStrLen(kRdrName(pRdr)); 286 cb = K_ALIGN_Z(sizeof(KLDRMODLX), 8) 287 + K_ALIGN_Z(K_OFFSETOF(KLDRMOD, aSegments[Hdr.e32_objcnt + 1]), 8) 288 + K_ALIGN_Z(cchFilename + 1, 8); 289 offLdrStuff = cb; 290 cb += Hdr.e32_ldrsize + 2; /* +2 for two extra zeros. */ 291 pModLX = (PKLDRMODLX)kHlpAlloc(cb); 262 size_t cbModLXAndSegments = RT_ALIGN_Z(RT_UOFFSETOF_DYN(KLDRMODLX, aSegments[Hdr.e32_objcnt + 1]), 8); 263 pModLX = (PKLDRMODLX)RTMemAlloc(cbModLXAndSegments + Hdr.e32_ldrsize + 2 /*for two extra zeros*/); 292 264 if (!pModLX) 293 return KERR_NO_MEMORY;265 return VERR_NO_MEMORY; 294 266 *ppModLX = pModLX; 295 267 296 /* KLDRMOD */ 297 pMod = (PKLDRMOD)((KU8 *)pModLX + K_ALIGN_Z(sizeof(KLDRMODLX), 8)); 298 pMod->pvData = pModLX; 299 pMod->pRdr = pRdr; 300 pMod->pOps = NULL; /* set upon success. */ 301 pMod->cSegments = Hdr.e32_objcnt; 302 pMod->cchFilename = (KU32)cchFilename; 303 pMod->pszFilename = (char *)K_ALIGN_P(&pMod->aSegments[pMod->cSegments], 8); 304 kHlpMemCopy((char *)pMod->pszFilename, kRdrName(pRdr), cchFilename + 1); 305 pMod->pszName = NULL; /* finalized further down */ 306 pMod->cchName = 0; 307 pMod->fFlags = 0; 268 /* Core & CPU. */ 269 pModLX->Core.u32Magic = 0; /* set by caller. */ 270 pModLX->Core.eState = LDR_STATE_OPENED; 271 pModLX->Core.pOps = NULL; /* set by caller. */ 272 pModLX->Core.pReader = pRdr; 308 273 switch (Hdr.e32_cpu) 309 274 { 310 275 case E32CPU286: 311 pMod ->enmCpu = KCPU_I80286;312 pMod ->enmArch = KCPUARCH_X86_16;276 pModLX->enmCpu = RTLDRCPU_I80286; 277 pModLX->Core.enmArch = RTLDRARCH_X86_16; 313 278 break; 314 279 case E32CPU386: 315 pMod ->enmCpu = KCPU_I386;316 pMod ->enmArch = KCPUARCH_X86_32;280 pModLX->enmCpu = RTLDRCPU_I386; 281 pModLX->Core.enmArch = RTLDRARCH_X86_32; 317 282 break; 318 283 case E32CPU486: 319 pMod ->enmCpu = KCPU_I486;320 pMod ->enmArch = KCPUARCH_X86_32;284 pModLX->enmCpu = RTLDRCPU_I486; 285 pModLX->Core.enmArch = RTLDRARCH_X86_32; 321 286 break; 322 287 } 323 pMod ->enmEndian = KLDRENDIAN_LITTLE;324 pMod ->enmFmt = KLDRFMT_LX;288 pModLX->Core.enmEndian = RTLDRENDIAN_LITTLE; 289 pModLX->Core.enmFormat = RTLDRFMT_LX; 325 290 switch (Hdr.e32_mflags & E32MODMASK) 326 291 { 327 292 case E32MODEXE: 328 pMod ->enmType = !(Hdr.e32_mflags & E32NOINTFIX)329 ? KLDRTYPE_EXECUTABLE_RELOCATABLE330 : KLDRTYPE_EXECUTABLE_FIXED;293 pModLX->Core.enmType = !(Hdr.e32_mflags & E32NOINTFIX) 294 ? RTLDRTYPE_EXECUTABLE_RELOCATABLE 295 : RTLDRTYPE_EXECUTABLE_FIXED; 331 296 break; 332 297 … … 334 299 case E32PROTDLL: 335 300 case E32MODPROTDLL: 336 pMod ->enmType = !(Hdr.e32_mflags & E32SYSDLL)337 ? KLDRTYPE_SHARED_LIBRARY_RELOCATABLE338 : KLDRTYPE_SHARED_LIBRARY_FIXED;301 pModLX->Core.enmType = !(Hdr.e32_mflags & E32SYSDLL) 302 ? RTLDRTYPE_SHARED_LIBRARY_RELOCATABLE 303 : RTLDRTYPE_SHARED_LIBRARY_FIXED; 339 304 break; 340 305 341 306 case E32MODPDEV: 342 307 case E32MODVDEV: 343 pMod ->enmType = KLDRTYPE_SHARED_LIBRARY_RELOCATABLE;308 pModLX->Core.enmType = RTLDRTYPE_SHARED_LIBRARY_RELOCATABLE; 344 309 break; 345 310 } 346 pMod->u32Magic = 0; /* set upon success. */347 311 348 312 /* KLDRMODLX */ 349 pModLX->pMod = pMod; 313 pModLX->cSegments = Hdr.e32_objcnt; 314 pModLX->pszName = NULL; /* finalized further down */ 315 pModLX->cchName = 0; 350 316 pModLX->pvMapping = 0; 351 317 pModLX->cbMapped = 0; … … 353 319 354 320 pModLX->offHdr = offNewHdr >= 0 ? offNewHdr : 0; 355 kHlpMemCopy(&pModLX->Hdr, &Hdr, sizeof(Hdr));356 357 pModLX->pbLoaderSection = (uint8_t *)pModLX + offLdrStuff;321 memcpy(&pModLX->Hdr, &Hdr, sizeof(Hdr)); 322 323 pModLX->pbLoaderSection = (uint8_t *)pModLX + cbModLXAndSegments; 358 324 pModLX->pbLoaderSectionLast = pModLX->pbLoaderSection + pModLX->Hdr.e32_ldrsize - 1; 359 325 pModLX->paObjs = NULL; … … 376 342 * Read the loader data. 377 343 */ 378 rc = kRdrRead(pRdr, (void *)pModLX->pbLoaderSection, pModLX->Hdr.e32_ldrsize, pModLX->Hdr.e32_objtab + pModLX->offHdr);344 rc = pRdr->pfnRead(pRdr, (void *)pModLX->pbLoaderSection, pModLX->Hdr.e32_ldrsize, pModLX->Hdr.e32_objtab + pModLX->offHdr); 379 345 if (rc) 380 346 return rc; 381 (( KU8*)pModLX->pbLoaderSectionLast)[1] = 0;382 (( KU8*)pModLX->pbLoaderSectionLast)[2] = 0;347 ((uint8_t *)pModLX->pbLoaderSectionLast)[1] = 0; 348 ((uint8_t *)pModLX->pbLoaderSectionLast)[2] = 0; 383 349 if (pModLX->Hdr.e32_objcnt) 384 350 pModLX->paObjs = (const struct o32_obj *)pModLX->pbLoaderSection; … … 399 365 */ 400 366 if (pModLX->pbResNameTab) 401 pMod->pszName = (const char *)kldrModLXDoNameTableLookupByOrdinal(pModLX->pbResNameTab, 402 pModLX->pbLoaderSectionLast - pModLX->pbResNameTab + 1, 403 0); 404 if (!pMod->pszName) 405 return KLDR_ERR_LX_NO_SONAME; 406 pMod->cchName = *(const KU8 *)pMod->pszName++; 407 if (pMod->cchName != kHlpStrLen(pMod->pszName)) 408 return KLDR_ERR_LX_BAD_SONAME; 367 pModLX->pszName = (const char *)kldrModLXDoNameTableLookupByOrdinal(pModLX->pbResNameTab, 368 pModLX->pbLoaderSectionLast - pModLX->pbResNameTab + 1, 369 0); 370 if (!pModLX->pszName) 371 return VERR_LDRLX_NO_SONAME; 372 pModLX->cchName = *(const uint8_t *)pModLX->pszName++; 373 if ( pModLX->pszName[pModLX->cchName] != '\0' 374 || pModLX->cchName != strlen(pModLX->pszName)) 375 return VERR_LDRLX_BAD_SONAME; 409 376 410 377 /* 411 378 * Quick validation of the object table. 412 379 */ 413 cb = 0; 414 for (i = 0; i < pMod->cSegments; i++) 380 for (i = 0; i < pModLX->cSegments; i++) 415 381 { 416 382 if (pModLX->paObjs[i].o32_base & (OBJPAGELEN - 1)) 417 return KLDR_ERR_LX_BAD_OBJECT_TABLE;383 return VERR_LDRLX_BAD_OBJECT_TABLE; 418 384 if (pModLX->paObjs[i].o32_base + pModLX->paObjs[i].o32_size <= pModLX->paObjs[i].o32_base) 419 return KLDR_ERR_LX_BAD_OBJECT_TABLE;385 return VERR_LDRLX_BAD_OBJECT_TABLE; 420 386 if (pModLX->paObjs[i].o32_mapsize > (pModLX->paObjs[i].o32_size + (OBJPAGELEN - 1))) 421 return KLDR_ERR_LX_BAD_OBJECT_TABLE;387 return VERR_LDRLX_BAD_OBJECT_TABLE; 422 388 if ( pModLX->paObjs[i].o32_mapsize 423 && ( ( KU8*)&pModLX->paPageMappings[pModLX->paObjs[i].o32_pagemap] > pModLX->pbLoaderSectionLast424 || ( KU8*)&pModLX->paPageMappings[pModLX->paObjs[i].o32_pagemap + pModLX->paObjs[i].o32_mapsize]389 && ( (uint8_t *)&pModLX->paPageMappings[pModLX->paObjs[i].o32_pagemap] > pModLX->pbLoaderSectionLast 390 || (uint8_t *)&pModLX->paPageMappings[pModLX->paObjs[i].o32_pagemap + pModLX->paObjs[i].o32_mapsize] 425 391 > pModLX->pbLoaderSectionLast)) 426 return KLDR_ERR_LX_BAD_OBJECT_TABLE;392 return VERR_LDRLX_BAD_OBJECT_TABLE; 427 393 if (i > 0 && !(pModLX->paObjs[i].o32_flags & OBJRSRC)) 428 394 { 429 395 if (pModLX->paObjs[i].o32_base <= pModLX->paObjs[i - 1].o32_base) 430 return KLDR_ERR_LX_BAD_OBJECT_TABLE;396 return VERR_LDRLX_BAD_OBJECT_TABLE; 431 397 if (pModLX->paObjs[i].o32_base < pModLX->paObjs[i - 1].o32_base + pModLX->paObjs[i - 1].o32_mapsize) 432 return KLDR_ERR_LX_BAD_OBJECT_TABLE;398 return VERR_LDRLX_BAD_OBJECT_TABLE; 433 399 } 434 400 } … … 445 411 * Setup the KLDRMOD segment array. 446 412 */ 447 for (i = 0; i < pMod ->cSegments; i++)413 for (i = 0; i < pModLX->cSegments; i++) 448 414 { 449 415 /* unused */ 450 pMod->aSegments[i].pvUser = NULL; 451 pMod->aSegments[i].MapAddress = 0; 452 pMod->aSegments[i].pchName = NULL; 453 pMod->aSegments[i].cchName = 0; 454 pMod->aSegments[i].offFile = -1; 455 pMod->aSegments[i].cbFile = -1; 456 pMod->aSegments[i].SelFlat = 0; 457 pMod->aSegments[i].Sel16bit = 0; 416 pModLX->aSegments[i].pszName = NULL; 417 pModLX->aSegments[i].offFile = -1; 418 pModLX->aSegments[i].cbFile = -1; 419 pModLX->aSegments[i].SelFlat = 0; 420 pModLX->aSegments[i].Sel16bit = 0; 458 421 459 422 /* flags */ 460 pMod ->aSegments[i].fFlags = 0;423 pModLX->aSegments[i].fFlags = 0; 461 424 if (pModLX->paObjs[i].o32_flags & OBJBIGDEF) 462 pMod ->aSegments[i].fFlags = KLDRSEG_FLAG_16BIT;425 pModLX->aSegments[i].fFlags = RTLDRSEG_FLAG_16BIT; 463 426 if (pModLX->paObjs[i].o32_flags & OBJALIAS16) 464 pMod ->aSegments[i].fFlags = KLDRSEG_FLAG_OS2_ALIAS16;427 pModLX->aSegments[i].fFlags = RTLDRSEG_FLAG_OS2_ALIAS16; 465 428 if (pModLX->paObjs[i].o32_flags & OBJCONFORM) 466 pMod ->aSegments[i].fFlags = KLDRSEG_FLAG_OS2_CONFORM;429 pModLX->aSegments[i].fFlags = RTLDRSEG_FLAG_OS2_CONFORM; 467 430 if (pModLX->paObjs[i].o32_flags & OBJIOPL) 468 pMod ->aSegments[i].fFlags = KLDRSEG_FLAG_OS2_IOPL;431 pModLX->aSegments[i].fFlags = RTLDRSEG_FLAG_OS2_IOPL; 469 432 470 433 /* size and addresses */ 471 pMod ->aSegments[i].Alignment = OBJPAGELEN;472 pMod ->aSegments[i].cb = pModLX->paObjs[i].o32_size;473 pMod ->aSegments[i].LinkAddress = pModLX->paObjs[i].o32_base;474 pMod ->aSegments[i].RVA = NextRVA;434 pModLX->aSegments[i].Alignment = OBJPAGELEN; 435 pModLX->aSegments[i].cb = pModLX->paObjs[i].o32_size; 436 pModLX->aSegments[i].LinkAddress = pModLX->paObjs[i].o32_base; 437 pModLX->aSegments[i].RVA = NextRVA; 475 438 if ( fCanOptimizeMapping 476 || i + 1 >= pMod ->cSegments439 || i + 1 >= pModLX->cSegments 477 440 || (pModLX->paObjs[i].o32_flags & OBJRSRC) 478 441 || (pModLX->paObjs[i + 1].o32_flags & OBJRSRC)) 479 pMod ->aSegments[i].cbMapped = K_ALIGN_Z(pModLX->paObjs[i].o32_size, OBJPAGELEN);442 pModLX->aSegments[i].cbMapped = RT_ALIGN_Z(pModLX->paObjs[i].o32_size, OBJPAGELEN); 480 443 else 481 pMod ->aSegments[i].cbMapped = pModLX->paObjs[i + 1].o32_base - pModLX->paObjs[i].o32_base;482 NextRVA += ( KU32)pMod->aSegments[i].cbMapped;444 pModLX->aSegments[i].cbMapped = pModLX->paObjs[i + 1].o32_base - pModLX->paObjs[i].o32_base; 445 NextRVA += (uint32_t)pModLX->aSegments[i].cbMapped; 483 446 484 447 /* protection */ … … 488 451 case 0: 489 452 case OBJSHARED: 490 pMod ->aSegments[i].enmProt = KPROT_NOACCESS;453 pModLX->aSegments[i].fProt = 0; 491 454 break; 492 455 case OBJREAD: 493 456 case OBJREAD | OBJSHARED: 494 pMod ->aSegments[i].enmProt = KPROT_READONLY;457 pModLX->aSegments[i].fProt = RTMEM_PROT_READ; 495 458 break; 496 459 case OBJWRITE: 497 460 case OBJWRITE | OBJREAD: 498 pMod ->aSegments[i].enmProt = KPROT_WRITECOPY;461 pModLX->aSegments[i].fProt = RTMEM_PROT_READ | RTMEM_PROT_WRITECOPY; 499 462 break; 500 463 case OBJWRITE | OBJSHARED: 501 464 case OBJWRITE | OBJSHARED | OBJREAD: 502 pMod ->aSegments[i].enmProt = KPROT_READWRITE;465 pModLX->aSegments[i].fProt = RTMEM_PROT_READ | RTMEM_PROT_WRITE; 503 466 break; 504 467 case OBJEXEC: 505 468 case OBJEXEC | OBJSHARED: 506 pMod ->aSegments[i].enmProt = KPROT_EXECUTE;469 pModLX->aSegments[i].fProt = RTMEM_PROT_EXEC; 507 470 break; 508 471 case OBJEXEC | OBJREAD: 509 472 case OBJEXEC | OBJREAD | OBJSHARED: 510 pMod ->aSegments[i].enmProt = KPROT_EXECUTE_READ;473 pModLX->aSegments[i].fProt = RTMEM_PROT_EXEC | RTMEM_PROT_READ; 511 474 break; 512 475 case OBJEXEC | OBJWRITE: 513 476 case OBJEXEC | OBJWRITE | OBJREAD: 514 pMod ->aSegments[i].enmProt = KPROT_EXECUTE_WRITECOPY;477 pModLX->aSegments[i].fProt = RTMEM_PROT_EXEC | RTMEM_PROT_READ | RTMEM_PROT_WRITECOPY; 515 478 break; 516 479 case OBJEXEC | OBJWRITE | OBJSHARED: 517 480 case OBJEXEC | OBJWRITE | OBJSHARED | OBJREAD: 518 pMod ->aSegments[i].enmProt = KPROT_EXECUTE_READWRITE;481 pModLX->aSegments[i].fProt = RTMEM_PROT_EXEC | RTMEM_PROT_READ | RTMEM_PROT_WRITE; 519 482 break; 520 483 } 521 484 if ((pModLX->paObjs[i].o32_flags & (OBJREAD | OBJWRITE | OBJEXEC | OBJRSRC)) == OBJRSRC) 522 pMod ->aSegments[i].enmProt = KPROT_READONLY;523 /*pMod ->aSegments[i].f16bit = !(pModLX->paObjs[i].o32_flags & OBJBIGDEF)524 pMod ->aSegments[i].fIOPL = !(pModLX->paObjs[i].o32_flags & OBJIOPL)525 pMod ->aSegments[i].fConforming = !(pModLX->paObjs[i].o32_flags & OBJCONFORM) */485 pModLX->aSegments[i].fProt = RTMEM_PROT_READ; 486 /*pModLX->aSegments[i].f16bit = !(pModLX->paObjs[i].o32_flags & OBJBIGDEF) 487 pModLX->aSegments[i].fIOPL = !(pModLX->paObjs[i].o32_flags & OBJIOPL) 488 pModLX->aSegments[i].fConforming = !(pModLX->paObjs[i].o32_flags & OBJCONFORM) */ 526 489 } 527 490 … … 533 496 */ 534 497 *ppModLX = pModLX; 535 return 0; 536 } 537 538 539 /** @copydoc KLDRMODOPS::pfnDestroy */ 540 static int kldrModLXDestroy(PKLDRMOD pMod) 541 { 542 PKLDRMODLX pModLX = (PKLDRMODLX)pMod->pvData; 543 int rc = 0; 498 return VINF_SUCCESS; 499 } 500 501 502 /** 503 * @interface_method_impl{RTLDROPS,pfnClose} 504 */ 505 static DECLCALLBACK(int) rtldrLX_Close(PRTLDRMODINTERNAL pMod) 506 { 507 PKLDRMODLX pModLX = RT_FROM_MEMBER(pMod, KLDRMODLX, Core); 544 508 KLDRMODLX_ASSERT(!pModLX->pvMapping); 545 509 546 if (pMod->pRdr) 547 { 548 rc = kRdrClose(pMod->pRdr); 549 pMod->pRdr = NULL; 510 int rc = VINF_SUCCESS; 511 if (pModLX->Core.pReader) 512 { 513 rc = pModLX->Core.pReader->pfnDestroy(pModLX->Core.pReader); 514 pModLX->Core.pReader = NULL; 550 515 } 551 516 if (pModLX->pbNonResNameTab) 552 517 { 553 kHlpFree(pModLX->pbNonResNameTab);518 RTMemFree(pModLX->pbNonResNameTab); 554 519 pModLX->pbNonResNameTab = NULL; 555 520 } 556 521 if (pModLX->pbFixupSection) 557 522 { 558 kHlpFree(pModLX->pbFixupSection);523 RTMemFree(pModLX->pbFixupSection); 559 524 pModLX->pbFixupSection = NULL; 560 525 } 561 pMod ->u32Magic = 0;562 pMod ->pOps = NULL;563 kHlpFree(pModLX);526 pModLX->Core.u32Magic = 0; 527 pModLX->Core.pOps = NULL; 528 RTMemFree(pModLX); 564 529 return rc; 565 530 } … … 572 537 * @param pBaseAddress The base address, IN & OUT. 573 538 */ 574 static void kldrModLXResolveBaseAddress(PKLDRMODLX pModLX, PKLDRADDR pBaseAddress) 575 { 576 if (*pBaseAddress == KLDRMOD_BASEADDRESS_MAP) 577 *pBaseAddress = pModLX->pMod->aSegments[0].MapAddress; 578 else if (*pBaseAddress == KLDRMOD_BASEADDRESS_LINK) 579 *pBaseAddress = pModLX->pMod->aSegments[0].LinkAddress; 539 static void kldrModLXResolveBaseAddress(PKLDRMODLX pModLX, PRTLDRADDR pBaseAddress) 540 { 541 if (*pBaseAddress == RTLDR_BASEADDRESS_LINK) 542 *pBaseAddress = pModLX->aSegments[0].LinkAddress; 580 543 } 581 544 582 545 583 546 /** @copydoc kLdrModQuerySymbol */ 584 static int kldrModLXQuerySymbol(P KLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, KU32iSymbol,585 const char *pchSymbol, KSIZEcchSymbol, const char *pszVersion,586 PFN KLDRMODGETIMPORT pfnGetForwarder, void *pvUser, PKLDRADDR puValue, KU32*pfKind)587 { 588 PKLDRMODLX pModLX = (PKLDRMODLX)pMod->pvData;589 KU32iOrdinal;547 static int kldrModLXQuerySymbol(PRTLDRMODINTERNAL pMod, const void *pvBits, RTLDRADDR BaseAddress, uint32_t iSymbol, 548 const char *pchSymbol, size_t cchSymbol, const char *pszVersion, 549 PFNRTLDRIMPORT pfnGetForwarder, void *pvUser, PRTLDRADDR puValue, uint32_t *pfKind) 550 { 551 PKLDRMODLX pModLX = RT_FROM_MEMBER(pMod, KLDRMODLX, Core); 552 uint32_t iOrdinal; 590 553 int rc; 591 const struct b32_bundle 592 K_NOREF(pvBits);593 K_NOREF(pszVersion);554 const struct b32_bundle *pBundle; 555 RT_NOREF(pvBits); 556 RT_NOREF(pszVersion); 594 557 595 558 /* … … 597 560 */ 598 561 if (!pModLX->Hdr.e32_enttab) 599 return KLDR_ERR_SYMBOL_NOT_FOUND;562 return VERR_SYMBOL_NOT_FOUND; 600 563 601 564 /* … … 617 580 while (pBundle->b32_cnt && iOrdinal <= iSymbol) 618 581 { 619 static const KSIZEs_cbEntry[] = { 0, 3, 5, 5, 7 };582 static const size_t s_cbEntry[] = { 0, 3, 5, 5, 7 }; 620 583 621 584 /* … … 625 588 if (iSymbol < iOrdinal) 626 589 { 627 KU32offObject;628 const struct e32_entry *pEntry = (const struct e32_entry *)(( KUPTR)(pBundle + 1)590 uint32_t offObject; 591 const struct e32_entry *pEntry = (const struct e32_entry *)((uintptr_t)(pBundle + 1) 629 592 + (iSymbol - (iOrdinal - pBundle->b32_cnt)) 630 593 * s_cbEntry[pBundle->b32_type]); … … 638 601 /* empty bundles are place holders unused ordinal ranges. */ 639 602 case EMPTY: 640 return KLDR_ERR_SYMBOL_NOT_FOUND;603 return VERR_SYMBOL_NOT_FOUND; 641 604 642 605 /* e32_flags + a 16-bit offset. */ … … 644 607 offObject = pEntry->e32_variant.e32_offset.offset16; 645 608 if (pfKind) 646 *pfKind = KLDRSYMKIND_16BIT | KLDRSYMKIND_NO_TYPE;609 *pfKind = RTLDRSYMKIND_16BIT | RTLDRSYMKIND_NO_TYPE; 647 610 break; 648 611 … … 651 614 offObject = pEntry->e32_variant.e32_callgate.offset; 652 615 if (pfKind) 653 *pfKind = KLDRSYMKIND_16BIT | KLDRSYMKIND_CODE;616 *pfKind = RTLDRSYMKIND_16BIT | RTLDRSYMKIND_CODE; 654 617 break; 655 618 … … 658 621 offObject = pEntry->e32_variant.e32_offset.offset32; 659 622 if (pfKind) 660 *pfKind = KLDRSYMKIND_32BIT;623 *pfKind = RTLDRSYMKIND_32BIT; 661 624 break; 662 625 … … 668 631 /* anyone actually using TYPEINFO will end up here. */ 669 632 KLDRMODLX_ASSERT(!"Bad bundle type"); 670 return KLDR_ERR_LX_BAD_BUNDLE;633 return VERR_LDRLX_BAD_BUNDLE; 671 634 } 672 635 … … 675 638 */ 676 639 if ( pBundle->b32_obj <= 0 677 || pBundle->b32_obj > pMod ->cSegments)678 return KLDR_ERR_LX_BAD_BUNDLE;640 || pBundle->b32_obj > pModLX->cSegments) 641 return VERR_LDRLX_BAD_BUNDLE; 679 642 if (puValue) 680 643 *puValue = BaseAddress 681 644 + offObject 682 + pMod ->aSegments[pBundle->b32_obj - 1].RVA;645 + pModLX->aSegments[pBundle->b32_obj - 1].RVA; 683 646 return 0; 684 647 } … … 690 653 { 691 654 KLDRMODLX_ASSERT(!"Bad type"); /** @todo figure out TYPEINFO. */ 692 return KLDR_ERR_LX_BAD_BUNDLE;655 return VERR_LDRLX_BAD_BUNDLE; 693 656 } 694 657 if (pBundle->b32_type == 0) 695 pBundle = (const struct b32_bundle *)((const KU8*)pBundle + 2);658 pBundle = (const struct b32_bundle *)((const uint8_t *)pBundle + 2); 696 659 else 697 pBundle = (const struct b32_bundle *)((const KU8 *)(pBundle + 1) + s_cbEntry[pBundle->b32_type] * pBundle->b32_cnt); 698 } 699 700 return KLDR_ERR_SYMBOL_NOT_FOUND; 660 pBundle = (const struct b32_bundle *)((const uint8_t *)(pBundle + 1) + s_cbEntry[pBundle->b32_type] * pBundle->b32_cnt); 661 } 662 663 return VERR_SYMBOL_NOT_FOUND; 664 } 665 666 667 /** 668 * @interface_method_impl{RTLDROPS,pfnGetSymbolEx} 669 */ 670 static DECLCALLBACK(int) rtldrLX_GetSymbolEx(PRTLDRMODINTERNAL pMod, const void *pvBits, RTUINTPTR BaseAddress, 671 uint32_t iOrdinal, const char *pszSymbol, RTUINTPTR *pValue) 672 { 673 uint32_t fKind = RTLDRSYMKIND_REQ_FLAT; 674 return kldrModLXQuerySymbol(pMod, pvBits, BaseAddress, iOrdinal, pszSymbol, pszSymbol ? strlen(pszSymbol) : 0, 675 NULL, NULL, NULL, pValue, &fKind); 701 676 } 702 677 … … 711 686 * @param piSymbol Where to store the symbol ordinal. 712 687 */ 713 static int kldrModLXDoNameLookup(PKLDRMODLX pModLX, const char *pchSymbol, KSIZE cchSymbol, KU32*piSymbol)688 static int kldrModLXDoNameLookup(PKLDRMODLX pModLX, const char *pchSymbol, size_t cchSymbol, uint32_t *piSymbol) 714 689 { 715 690 … … 722 697 * Search the name tables. 723 698 */ 724 const KU8*pbName = kldrModLXDoNameTableLookupByName(pModLX->pbResNameTab,699 const uint8_t *pbName = kldrModLXDoNameTableLookupByName(pModLX->pbResNameTab, 725 700 pModLX->pbLoaderSectionLast - pModLX->pbResNameTab + 1, 726 701 pchSymbol, cchSymbol); … … 738 713 } 739 714 if (!pbName) 740 return KLDR_ERR_SYMBOL_NOT_FOUND;741 742 *piSymbol = *(const KU16*)(pbName + 1 + *pbName);715 return VERR_SYMBOL_NOT_FOUND; 716 717 *piSymbol = *(const uint16_t *)(pbName + 1 + *pbName); 743 718 return 0; 744 719 } 745 746 747 #if 0748 /**749 * Hash a symbol using the algorithm from sdbm.750 *751 * The following was is the documenation of the orignal sdbm functions:752 *753 * This algorithm was created for sdbm (a public-domain reimplementation of754 * ndbm) database library. it was found to do well in scrambling bits,755 * causing better distribution of the keys and fewer splits. it also happens756 * to be a good general hashing function with good distribution. the actual757 * function is hash(i) = hash(i - 1) * 65599 + str[i]; what is included below758 * is the faster version used in gawk. [there is even a faster, duff-device759 * version] the magic constant 65599 was picked out of thin air while760 * experimenting with different constants, and turns out to be a prime.761 * this is one of the algorithms used in berkeley db (see sleepycat) and762 * elsewhere.763 */764 static KU32 kldrModLXDoHash(const char *pchSymbol, KU8 cchSymbol)765 {766 KU32 hash = 0;767 int ch;768 769 while ( cchSymbol-- > 0770 && (ch = *(unsigned const char *)pchSymbol++))771 hash = ch + (hash << 6) + (hash << 16) - hash;772 773 return hash;774 }775 #endif776 720 777 721 … … 786 730 * @param cchSymbol The length of the symbol name. 787 731 */ 788 static const KU8 *kldrModLXDoNameTableLookupByName(const KU8 *pbNameTable, KSSIZEcbNameTable,789 const char *pchSymbol, KSIZEcchSymbol)732 static const uint8_t *kldrModLXDoNameTableLookupByName(const uint8_t *pbNameTable, ssize_t cbNameTable, 733 const char *pchSymbol, size_t cchSymbol) 790 734 { 791 735 /* 792 736 * Determin the namelength up front so we can skip anything which doesn't matches the length. 793 737 */ 794 KU8 cbSymbol8Bit = (KU8)cchSymbol;738 uint8_t cbSymbol8Bit = (uint8_t)cchSymbol; 795 739 if (cbSymbol8Bit != cchSymbol) 796 740 return NULL; /* too long. */ … … 801 745 while (*pbNameTable != 0 && cbNameTable > 0) 802 746 { 803 const KU8cbName = *pbNameTable;747 const uint8_t cbName = *pbNameTable; 804 748 805 749 cbNameTable -= cbName + 1 + 2; … … 808 752 809 753 if ( cbName == cbSymbol8Bit 810 && ! kHlpMemComp(pbNameTable + 1, pchSymbol, cbName))754 && !memcmp(pbNameTable + 1, pchSymbol, cbName)) 811 755 return pbNameTable; 812 756 … … 831 775 */ 832 776 static int kldrModLXDoForwarderQuery(PKLDRMODLX pModLX, const struct e32_entry *pEntry, 833 PFNKLDRMODGETIMPORT pfnGetForwarder, void *pvUser, PKLDRADDR puValue, KU32 *pfKind) 834 { 835 int rc; 836 KU32 iSymbol; 837 const char *pchSymbol; 838 KU8 cchSymbol; 839 777 PFNRTLDRIMPORT pfnGetForwarder, void *pvUser, PRTLDRADDR puValue, uint32_t *pfKind) 778 { 840 779 if (!pfnGetForwarder) 841 return KLDR_ERR_FORWARDER_SYMBOL;780 return VERR_LDR_FORWARDER; 842 781 843 782 /* … … 846 785 if ( !pEntry->e32_variant.e32_fwd.modord 847 786 || pEntry->e32_variant.e32_fwd.modord > pModLX->Hdr.e32_impmodcnt) 848 return KLDR_ERR_LX_BAD_FORWARDER; 787 return VERR_LDRLX_BAD_FORWARDER; 788 789 char szImpModule[256]; 790 int rc = kldrModLXGetImport(pModLX, NULL, pEntry->e32_variant.e32_fwd.modord - 1, szImpModule, sizeof(szImpModule), NULL); 791 if (RT_FAILURE(rc)) 792 return rc; 849 793 850 794 /* 851 795 * Figure out the parameters. 852 796 */ 797 uint32_t iSymbol; 798 const char *pszSymbol; 799 char szSymbol[256]; 853 800 if (pEntry->e32_flags & FWD_ORDINAL) 854 801 { 855 802 iSymbol = pEntry->e32_variant.e32_fwd.value; 856 pchSymbol = NULL; /* no symbol name. */ 857 cchSymbol = 0; 803 pszSymbol = NULL; /* no symbol name. */ 858 804 } 859 805 else 860 806 { 861 const KU8*pbName;807 const uint8_t *pbName; 862 808 863 809 /* load the fixup section if necessary. */ … … 874 820 || pbName < pModLX->pbFixupSection 875 821 || !*pbName) 876 return KLDR_ERR_LX_BAD_FORWARDER;822 return VERR_LDRLX_BAD_FORWARDER; 877 823 878 824 … … 880 826 if (pbName[1] == '#') 881 827 { 882 KU8cbLeft = *pbName;883 const KU8*pb = pbName + 1;828 uint8_t cbLeft = *pbName; 829 const uint8_t *pb = pbName + 1; 884 830 unsigned uBase; 885 831 … … 910 856 break; 911 857 else 912 return KLDR_ERR_LX_BAD_FORWARDER;858 return VERR_LDRLX_BAD_FORWARDER; 913 859 if (uDigit >= uBase) 914 return KLDR_ERR_LX_BAD_FORWARDER;860 return VERR_LDRLX_BAD_FORWARDER; 915 861 916 862 /* insert the digit */ … … 919 865 } 920 866 if (!iSymbol) 921 return KLDR_ERR_LX_BAD_FORWARDER; 922 923 pchSymbol = NULL; /* no symbol name. */ 924 cchSymbol = 0; 867 return VERR_LDRLX_BAD_FORWARDER; 868 869 pszSymbol = NULL; /* no symbol name. */ 925 870 } 926 871 else 927 872 { 928 pchSymbol = (char *)pbName + 1; 929 cchSymbol = *pbName; 930 iSymbol = NIL_KLDRMOD_SYM_ORDINAL; 873 memcpy(szSymbol, pbName + 1, *pbName); 874 szSymbol[*pbName] = '\0'; 875 pszSymbol = szSymbol; 876 iSymbol = UINT32_MAX; 931 877 } 932 878 } … … 935 881 * Resolve the forwarder. 936 882 */ 937 rc = pfnGetForwarder( pModLX->pMod, pEntry->e32_variant.e32_fwd.modord - 1, iSymbol, pchSymbol, cchSymbol, NULL, puValue, pfKind,pvUser);938 if ( !rc&& pfKind)939 *pfKind |= KLDRSYMKIND_FORWARDER;883 rc = pfnGetForwarder(&pModLX->Core, szImpModule, pszSymbol, iSymbol, puValue, /*pfKind, */pvUser); 884 if (RT_SUCCESS(rc) && pfKind) 885 *pfKind |= RTLDRSYMKIND_FORWARDER; 940 886 return rc; 941 887 } … … 952 898 static int kldrModLXDoLoadFixupSection(PKLDRMODLX pModLX) 953 899 { 954 int rc; 955 KU32 off; 956 void *pv; 957 958 pv = kHlpAlloc(pModLX->Hdr.e32_fixupsize); 900 void *pv = RTMemAlloc(pModLX->Hdr.e32_fixupsize); 959 901 if (!pv) 960 return KERR_NO_MEMORY;961 962 off = pModLX->Hdr.e32_objtab + pModLX->Hdr.e32_ldrsize;963 rc = kRdrRead(pModLX->pMod->pRdr, pv, pModLX->Hdr.e32_fixupsize,964 off + pModLX->offHdr);965 if ( !rc)966 { 967 pModLX->pbFixupSection = pv;902 return VERR_NO_MEMORY; 903 904 uint32_t off = pModLX->Hdr.e32_objtab + pModLX->Hdr.e32_ldrsize; 905 int rc = pModLX->Core.pReader->pfnRead(pModLX->Core.pReader, pv, pModLX->Hdr.e32_fixupsize, 906 off + pModLX->offHdr); 907 if (RT_SUCCESS(rc)) 908 { 909 pModLX->pbFixupSection = (uint8_t *)pv; 968 910 pModLX->pbFixupSectionLast = pModLX->pbFixupSection + pModLX->Hdr.e32_fixupsize; 969 911 KLDRMODLX_ASSERT(!pModLX->paoffPageFixups); 970 912 if (pModLX->Hdr.e32_fpagetab) 971 pModLX->paoffPageFixups = (const KU32*)(pModLX->pbFixupSection + pModLX->Hdr.e32_fpagetab - off);913 pModLX->paoffPageFixups = (const uint32_t *)(pModLX->pbFixupSection + pModLX->Hdr.e32_fpagetab - off); 972 914 KLDRMODLX_ASSERT(!pModLX->pbFixupRecs); 973 915 if (pModLX->Hdr.e32_frectab) … … 981 923 } 982 924 else 983 kHlpFree(pv);925 RTMemFree(pv); 984 926 return rc; 985 927 } 986 928 987 929 988 /** @copydoc kLdrModEnumSymbols */ 989 static int kldrModLXEnumSymbols(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, 990 KU32 fFlags, PFNKLDRMODENUMSYMS pfnCallback, void *pvUser) 991 { 992 PKLDRMODLX pModLX = (PKLDRMODLX)pMod->pvData; 993 const struct b32_bundle *pBundle; 994 KU32 iOrdinal; 995 int rc = 0; 996 K_NOREF(pvBits); 997 K_NOREF(fFlags); 930 /** 931 * @interface_method_impl{RTLDROPS,pfnEnumSymbols} 932 */ 933 static DECLCALLBACK(int) rtldrLX_EnumSymbols(PRTLDRMODINTERNAL pMod, unsigned fFlags, const void *pvBits, 934 RTUINTPTR BaseAddress, PFNRTLDRENUMSYMS pfnCallback, void *pvUser) 935 { 936 PKLDRMODLX pModLX = RT_FROM_MEMBER(pMod, KLDRMODLX, Core); 937 RT_NOREF(pvBits); 938 RT_NOREF(fFlags); 998 939 999 940 kldrModLXResolveBaseAddress(pModLX, &BaseAddress); … … 1003 944 * (The entry table is made up of bundles of similar exports.) 1004 945 */ 1005 iOrdinal = 1; 1006 pBundle = (const struct b32_bundle *)pModLX->pbEntryTab; 946 int rc = VINF_SUCCESS; 947 uint32_t iOrdinal = 1; 948 const struct b32_bundle *pBundle = (const struct b32_bundle *)pModLX->pbEntryTab; 1007 949 while (pBundle->b32_cnt && iOrdinal) 1008 950 { 1009 static const KSIZEs_cbEntry[] = { 0, 3, 5, 5, 7 };951 static const size_t s_cbEntry[] = { 0, 3, 5, 5, 7 }; 1010 952 1011 953 /* … … 1015 957 { 1016 958 const struct e32_entry *pEntry; 1017 KSIZEcbEntry;1018 KLDRADDR BundleRVA;959 size_t cbEntry; 960 RTLDRADDR BundleRVA; 1019 961 unsigned cLeft; 1020 962 … … 1027 969 case ENTRY32: 1028 970 if ( pBundle->b32_obj <= 0 1029 || pBundle->b32_obj > pMod ->cSegments)1030 return KLDR_ERR_LX_BAD_BUNDLE;1031 BundleRVA = pMod ->aSegments[pBundle->b32_obj - 1].RVA;971 || pBundle->b32_obj > pModLX->cSegments) 972 return VERR_LDRLX_BAD_BUNDLE; 973 BundleRVA = pModLX->aSegments[pBundle->b32_obj - 1].RVA; 1032 974 break; 1033 975 … … 1039 981 /* anyone actually using TYPEINFO will end up here. */ 1040 982 KLDRMODLX_ASSERT(!"Bad bundle type"); 1041 return KLDR_ERR_LX_BAD_BUNDLE;983 return VERR_LDRLX_BAD_BUNDLE; 1042 984 } 1043 985 … … 1048 990 while (cLeft-- > 0) 1049 991 { 1050 KLDRADDR uValue;1051 KU32fKind;992 RTLDRADDR uValue; 993 uint32_t fKind; 1052 994 int fFoundName; 1053 const KU8*pbName;995 const uint8_t *pbName; 1054 996 1055 997 /* … … 1061 1003 case ENTRY16: 1062 1004 uValue = BaseAddress + BundleRVA + pEntry->e32_variant.e32_offset.offset16; 1063 fKind = KLDRSYMKIND_16BIT | KLDRSYMKIND_NO_TYPE;1005 fKind = RTLDRSYMKIND_16BIT | RTLDRSYMKIND_NO_TYPE; 1064 1006 break; 1065 1007 … … 1067 1009 case GATE16: 1068 1010 uValue = BaseAddress + BundleRVA + pEntry->e32_variant.e32_callgate.offset; 1069 fKind = KLDRSYMKIND_16BIT | KLDRSYMKIND_CODE;1011 fKind = RTLDRSYMKIND_16BIT | RTLDRSYMKIND_CODE; 1070 1012 break; 1071 1013 … … 1073 1015 case ENTRY32: 1074 1016 uValue = BaseAddress + BundleRVA + pEntry->e32_variant.e32_offset.offset32; 1075 fKind = KLDRSYMKIND_32BIT;1017 fKind = RTLDRSYMKIND_32BIT; 1076 1018 break; 1077 1019 … … 1079 1021 case ENTRYFWD: 1080 1022 uValue = 0; /** @todo implement enumeration of forwarders properly. */ 1081 fKind = KLDRSYMKIND_FORWARDER;1023 fKind = RTLDRSYMKIND_FORWARDER; 1082 1024 break; 1083 1025 1084 1026 default: /* shut up gcc. */ 1085 1027 uValue = 0; 1086 fKind = KLDRSYMKIND_NO_BIT | KLDRSYMKIND_NO_TYPE;1028 fKind = RTLDRSYMKIND_NO_BIT | RTLDRSYMKIND_NO_TYPE; 1087 1029 break; 1088 1030 } … … 1092 1034 */ 1093 1035 fFoundName = 0; 1036 char szName[256]; 1094 1037 1095 1038 /* resident name table. */ … … 1103 1046 break; 1104 1047 fFoundName = 1; 1105 rc = pfnCallback(pMod, iOrdinal, (const char *)pbName + 1, *pbName, NULL, uValue, fKind, pvUser); 1048 memcpy(szName, (const char *)pbName + 1, *pbName); 1049 szName[*pbName] = '\0'; 1050 rc = pfnCallback(pMod, szName, iOrdinal, uValue, /*fKind,*/ pvUser); 1106 1051 if (rc) 1107 1052 return rc; … … 1123 1068 break; 1124 1069 fFoundName = 1; 1125 rc = pfnCallback(pMod, iOrdinal, (const char *)pbName + 1, *pbName, NULL, uValue, fKind, pvUser); 1070 memcpy(szName, (const char *)pbName + 1, *pbName); 1071 szName[*pbName] = '\0'; 1072 rc = pfnCallback(pMod, szName, iOrdinal, uValue, /*fKind,*/ pvUser); 1126 1073 if (rc) 1127 1074 return rc; … … 1137 1084 if (!fFoundName) 1138 1085 { 1139 rc = pfnCallback(pMod, iOrdinal, NULL, 0, NULL, uValue, fKind, pvUser); 1086 RT_NOREF(fKind); 1087 rc = pfnCallback(pMod, NULL /*pszName*/, iOrdinal, uValue, /*fKind,*/ pvUser); 1140 1088 if (rc) 1141 1089 return rc; … … 1144 1092 /* next */ 1145 1093 iOrdinal++; 1146 pEntry = (const struct e32_entry *)(( KUPTR)pEntry + cbEntry);1094 pEntry = (const struct e32_entry *)((uintptr_t)pEntry + cbEntry); 1147 1095 } 1148 1096 } … … 1154 1102 { 1155 1103 KLDRMODLX_ASSERT(!"Bad type"); /** @todo figure out TYPEINFO. */ 1156 return KLDR_ERR_LX_BAD_BUNDLE;1104 return VERR_LDRLX_BAD_BUNDLE; 1157 1105 } 1158 1106 if (pBundle->b32_type == 0) 1159 pBundle = (const struct b32_bundle *)((const KU8*)pBundle + 2);1107 pBundle = (const struct b32_bundle *)((const uint8_t *)pBundle + 2); 1160 1108 else 1161 pBundle = (const struct b32_bundle *)((const KU8*)(pBundle + 1) + s_cbEntry[pBundle->b32_type] * pBundle->b32_cnt);1109 pBundle = (const struct b32_bundle *)((const uint8_t *)(pBundle + 1) + s_cbEntry[pBundle->b32_type] * pBundle->b32_cnt); 1162 1110 } 1163 1111 … … 1175 1123 * @param iOrdinal The ordinal to search for. 1176 1124 */ 1177 static const KU8 *kldrModLXDoNameTableLookupByOrdinal(const KU8 *pbNameTable, KSSIZE cbNameTable, KU32iOrdinal)1125 static const uint8_t *kldrModLXDoNameTableLookupByOrdinal(const uint8_t *pbNameTable, ssize_t cbNameTable, uint32_t iOrdinal) 1178 1126 { 1179 1127 while (*pbNameTable != 0 && cbNameTable > 0) 1180 1128 { 1181 const KU8cbName = *pbNameTable;1182 KU32iName;1129 const uint8_t cbName = *pbNameTable; 1130 uint32_t iName; 1183 1131 1184 1132 cbNameTable -= cbName + 1 + 2; … … 1200 1148 1201 1149 /** @copydoc kLdrModGetImport */ 1202 static int kldrModLXGetImport(PKLDRMOD pMod, const void *pvBits, KU32 iImport, char *pszName, KSIZE cchName)1203 { 1204 PKLDRMODLX pModLX = (PKLDRMODLX)pMod->pvData; 1205 const KU8*pb;1206 int rc;1207 K_NOREF(pvBits);1150 static int kldrModLXGetImport(PKLDRMODLX pModLX, const void *pvBits, uint32_t iImport, char *pszName, size_t cchName, 1151 size_t *pcbNeeded) 1152 { 1153 const uint8_t *pb; 1154 int rc; 1155 RT_NOREF(pvBits); 1208 1156 1209 1157 /* … … 1211 1159 */ 1212 1160 if (iImport >= pModLX->Hdr.e32_impmodcnt) 1213 return KLDR_ERR_IMPORT_ORDINAL_OUT_OF_BOUNDS;1161 return VERR_LDRLX_IMPORT_ORDINAL_OUT_OF_BOUNDS; 1214 1162 1215 1163 /* … … 1233 1181 * Copy out the result. 1234 1182 */ 1183 if (pcbNeeded) 1184 *pcbNeeded = *pb + 1; 1235 1185 if (*pb < cchName) 1236 1186 { 1237 kHlpMemCopy(pszName, pb + 1, *pb);1187 memcpy(pszName, pb + 1, *pb); 1238 1188 pszName[*pb] = '\0'; 1239 1189 rc = 0; … … 1241 1191 else 1242 1192 { 1243 kHlpMemCopy(pszName, pb + 1, cchName);1193 memcpy(pszName, pb + 1, cchName); 1244 1194 if (cchName) 1245 1195 pszName[cchName - 1] = '\0'; 1246 rc = KERR_BUFFER_OVERFLOW;1196 rc = VERR_BUFFER_OVERFLOW; 1247 1197 } 1248 1198 … … 1250 1200 } 1251 1201 1202 #if 0 1252 1203 1253 1204 /** @copydoc kLdrModNumberOfImports */ 1254 static KI32 kldrModLXNumberOfImports(PKLDRMODpMod, const void *pvBits)1255 { 1256 PKLDRMODLX pModLX = (PKLDRMODLX)pMod->pvData;1257 K_NOREF(pvBits);1205 static int32_t kldrModLXNumberOfImports(PRTLDRMODINTERNAL pMod, const void *pvBits) 1206 { 1207 PKLDRMODLX pModLX = RT_FROM_MEMBER(pMod, KLDRMODLX, Core); 1208 RT_NOREF(pvBits); 1258 1209 return pModLX->Hdr.e32_impmodcnt; 1259 1210 } … … 1261 1212 1262 1213 /** @copydoc kLdrModGetStackInfo */ 1263 static int kldrModLXGetStackInfo(P KLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, PKLDRSTACKINFO pStackInfo)1264 { 1265 PKLDRMODLX pModLX = (PKLDRMODLX)pMod->pvData;1266 const KU32i = pModLX->Hdr.e32_stackobj;1267 K_NOREF(pvBits);1214 static int kldrModLXGetStackInfo(PRTLDRMODINTERNAL pMod, const void *pvBits, RTLDRADDR BaseAddress, PKLDRSTACKINFO pStackInfo) 1215 { 1216 PKLDRMODLX pModLX = RT_FROM_MEMBER(pMod, KLDRMODLX, Core); 1217 const uint32_t i = pModLX->Hdr.e32_stackobj; 1218 RT_NOREF(pvBits); 1268 1219 1269 1220 if ( i 1270 && i <= pMod ->cSegments1271 && pModLX->Hdr.e32_esp <= pMod ->aSegments[i - 1].LinkAddress + pMod->aSegments[i - 1].cb1221 && i <= pModLX->cSegments 1222 && pModLX->Hdr.e32_esp <= pModLX->aSegments[i - 1].LinkAddress + pModLX->aSegments[i - 1].cb 1272 1223 && pModLX->Hdr.e32_stacksize 1273 && pModLX->Hdr.e32_esp - pModLX->Hdr.e32_stacksize >= pMod ->aSegments[i - 1].LinkAddress)1224 && pModLX->Hdr.e32_esp - pModLX->Hdr.e32_stacksize >= pModLX->aSegments[i - 1].LinkAddress) 1274 1225 { 1275 1226 … … 1277 1228 pStackInfo->LinkAddress = pModLX->Hdr.e32_esp - pModLX->Hdr.e32_stacksize; 1278 1229 pStackInfo->Address = BaseAddress 1279 + pMod ->aSegments[i - 1].RVA1280 + pModLX->Hdr.e32_esp - pModLX->Hdr.e32_stacksize - pMod ->aSegments[i - 1].LinkAddress;1230 + pModLX->aSegments[i - 1].RVA 1231 + pModLX->Hdr.e32_esp - pModLX->Hdr.e32_stacksize - pModLX->aSegments[i - 1].LinkAddress; 1281 1232 } 1282 1233 else 1283 1234 { 1284 pStackInfo->Address = NIL_ KLDRADDR;1285 pStackInfo->LinkAddress = NIL_ KLDRADDR;1235 pStackInfo->Address = NIL_RTLDRADDR; 1236 pStackInfo->LinkAddress = NIL_RTLDRADDR; 1286 1237 } 1287 1238 pStackInfo->cbStack = pModLX->Hdr.e32_stacksize; … … 1293 1244 1294 1245 /** @copydoc kLdrModQueryMainEntrypoint */ 1295 static int kldrModLXQueryMainEntrypoint(P KLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, PKLDRADDR pMainEPAddress)1296 { 1297 PKLDRMODLX pModLX = (PKLDRMODLX)pMod->pvData;1298 K_NOREF(pvBits);1246 static int kldrModLXQueryMainEntrypoint(PRTLDRMODINTERNAL pMod, const void *pvBits, RTLDRADDR BaseAddress, PRTLDRADDR pMainEPAddress) 1247 { 1248 PKLDRMODLX pModLX = RT_FROM_MEMBER(pMod, KLDRMODLX, Core); 1249 RT_NOREF(pvBits); 1299 1250 1300 1251 /* … … 1303 1254 kldrModLXResolveBaseAddress(pModLX, &BaseAddress); 1304 1255 *pMainEPAddress = pModLX->Hdr.e32_startobj 1305 && pModLX->Hdr.e32_startobj <= pMod ->cSegments1306 && pModLX->Hdr.e32_eip < pMod ->aSegments[pModLX->Hdr.e32_startobj - 1].cb1307 ? BaseAddress + pMod ->aSegments[pModLX->Hdr.e32_startobj - 1].RVA + pModLX->Hdr.e32_eip1308 : NIL_ KLDRADDR;1256 && pModLX->Hdr.e32_startobj <= pModLX->cSegments 1257 && pModLX->Hdr.e32_eip < pModLX->aSegments[pModLX->Hdr.e32_startobj - 1].cb 1258 ? BaseAddress + pModLX->aSegments[pModLX->Hdr.e32_startobj - 1].RVA + pModLX->Hdr.e32_eip 1259 : NIL_RTLDRADDR; 1309 1260 return 0; 1310 1261 } 1311 1262 1312 1313 /** @copydoc kLdrModEnumDbgInfo */ 1314 static int kldrModLXEnumDbgInfo(PKLDRMOD pMod, const void *pvBits, PFNKLDRENUMDBG pfnCallback, void *pvUser) 1315 { 1316 /*PKLDRMODLX pModLX = (PKLDRMODLX)pMod->pvData;*/ 1317 K_NOREF(pfnCallback); 1318 K_NOREF(pvUser); 1263 #endif 1264 1265 1266 /** 1267 * @interface_method_impl{RTLDROPS,pfnEnumDbgInfo} 1268 */ 1269 static DECLCALLBACK(int) rtldrLX_EnumDbgInfo(PRTLDRMODINTERNAL pMod, const void *pvBits, 1270 PFNRTLDRENUMDBG pfnCallback, void *pvUser) 1271 { 1272 /*PKLDRMODLX pModLX = RT_FROM_MEMBER(pMod, KLDRMODLX, Core);*/ 1273 RT_NOREF(pfnCallback); 1274 RT_NOREF(pvUser); 1319 1275 1320 1276 /* … … 1335 1291 1336 1292 /** @copydoc kLdrModHasDbgInfo */ 1337 static int kldrModLXHasDbgInfo(P KLDRMODpMod, const void *pvBits)1338 { 1339 PKLDRMODLX pModLX = (PKLDRMODLX)pMod->pvData;1340 K_NOREF(pvBits);1293 static int kldrModLXHasDbgInfo(PRTLDRMODINTERNAL pMod, const void *pvBits) 1294 { 1295 PKLDRMODLX pModLX = RT_FROM_MEMBER(pMod, KLDRMODLX, Core); 1296 RT_NOREF(pvBits); 1341 1297 1342 1298 /* … … 1345 1301 if ( !pModLX->Hdr.e32_debuginfo 1346 1302 || !pModLX->Hdr.e32_debuglen) 1347 return KLDR_ERR_NO_DEBUG_INFO;1303 return VERR_NOT_FOUND; 1348 1304 return 0; 1349 1305 } 1350 1306 1307 #if 0 1351 1308 1352 1309 /** @copydoc kLdrModMap */ 1353 static int kldrModLXMap(P KLDRMODpMod)1354 { 1355 PKLDRMODLX pModLX = (PKLDRMODLX)pMod->pvData;1310 static int kldrModLXMap(PRTLDRMODINTERNAL pMod) 1311 { 1312 PKLDRMODLX pModLX = RT_FROM_MEMBER(pMod, KLDRMODLX, Core); 1356 1313 unsigned fFixed; 1357 1314 void *pvBase; … … 1368 1325 */ 1369 1326 /* fixed image? */ 1370 fFixed = pMod ->enmType == KLDRTYPE_EXECUTABLE_FIXED1371 || pMod ->enmType == KLDRTYPE_SHARED_LIBRARY_FIXED;1327 fFixed = pModLX->Core.enmType == RTLDRTYPE_EXECUTABLE_FIXED 1328 || pModLX->Core.enmType == RTLDRTYPE_SHARED_LIBRARY_FIXED; 1372 1329 if (!fFixed) 1373 1330 pvBase = NULL; 1374 1331 else 1375 1332 { 1376 pvBase = (void *)( KUPTR)pMod->aSegments[0].LinkAddress;1377 if (( KUPTR)pvBase != pMod->aSegments[0].LinkAddress)1333 pvBase = (void *)(uintptr_t)pModLX->aSegments[0].LinkAddress; 1334 if ((uintptr_t)pvBase != pModLX->aSegments[0].LinkAddress) 1378 1335 return KLDR_ERR_ADDRESS_OVERFLOW; 1379 1336 } … … 1390 1347 if (!rc) 1391 1348 { 1392 KU32i;1393 for (i = 0; i < pMod ->cSegments; i++)1349 uint32_t i; 1350 for (i = 0; i < pModLX->cSegments; i++) 1394 1351 { 1395 if (pMod ->aSegments[i].RVA != NIL_KLDRADDR)1396 pMod ->aSegments[i].MapAddress = (KUPTR)pvBase + (KUPTR)pMod->aSegments[i].RVA;1352 if (pModLX->aSegments[i].RVA != NIL_RTLDRADDR) 1353 pModLX->aSegments[i].MapAddress = (uintptr_t)pvBase + (uintptr_t)pModLX->aSegments[i].RVA; 1397 1354 } 1398 1355 pModLX->pvMapping = pvBase; … … 1403 1360 } 1404 1361 1362 #endif 1405 1363 1406 1364 /** … … 1415 1373 static int kldrModLXDoLoadBits(PKLDRMODLX pModLX, void *pvBits) 1416 1374 { 1417 const P KRDR pRdr = pModLX->pMod->pRdr;1418 KU8*pbTmpPage = NULL;1375 const PRTLDRREADER pRdr = pModLX->Core.pReader; 1376 uint8_t *pbTmpPage = NULL; 1419 1377 int rc = 0; 1420 KU32i;1378 uint32_t i; 1421 1379 1422 1380 /* … … 1426 1384 { 1427 1385 const struct o32_obj * const pObj = &pModLX->paObjs[i]; 1428 const KU32 cPages = (KU32)(pModLX->pMod->aSegments[i].cbMapped / OBJPAGELEN);1429 KU32iPage;1430 KU8 *pbPage = (KU8 *)pvBits + (KUPTR)pModLX->pMod->aSegments[i].RVA;1386 const uint32_t cPages = (uint32_t)(pModLX->aSegments[i].cbMapped / OBJPAGELEN); 1387 uint32_t iPage; 1388 uint8_t *pbPage = (uint8_t *)pvBits + (uintptr_t)pModLX->aSegments[i].RVA; 1431 1389 1432 1390 /* … … 1440 1398 case VALID: 1441 1399 if (pMap->o32_pagesize == OBJPAGELEN) 1442 rc = kRdrRead(pRdr, pbPage, OBJPAGELEN,1443 pModLX->Hdr.e32_datapage + (pMap->o32_pagedataoffset << pModLX->Hdr.e32_pageshift));1400 rc = pRdr->pfnRead(pRdr, pbPage, OBJPAGELEN, 1401 pModLX->Hdr.e32_datapage + (pMap->o32_pagedataoffset << pModLX->Hdr.e32_pageshift)); 1444 1402 else if (pMap->o32_pagesize < OBJPAGELEN) 1445 1403 { 1446 rc = kRdrRead(pRdr, pbPage, pMap->o32_pagesize,1447 pModLX->Hdr.e32_datapage + (pMap->o32_pagedataoffset << pModLX->Hdr.e32_pageshift));1448 kHlpMemSet(pbPage + pMap->o32_pagesize, 0, OBJPAGELEN - pMap->o32_pagesize);1404 rc = pRdr->pfnRead(pRdr, pbPage, pMap->o32_pagesize, 1405 pModLX->Hdr.e32_datapage + (pMap->o32_pagedataoffset << pModLX->Hdr.e32_pageshift)); 1406 memset(pbPage + pMap->o32_pagesize, 0, OBJPAGELEN - pMap->o32_pagesize); 1449 1407 } 1450 1408 else 1451 rc = KLDR_ERR_LX_BAD_PAGE_MAP;1409 rc = VERR_LDRLX_BAD_PAGE_MAP; 1452 1410 break; 1453 1411 … … 1457 1415 if (!pbTmpPage) 1458 1416 { 1459 pbTmpPage = kHlpAlloc(OBJPAGELEN + 256);1417 pbTmpPage = (uint8_t *)RTMemAlloc(OBJPAGELEN + 256); 1460 1418 if (!pbTmpPage) 1461 1419 break; … … 1464 1422 if (pMap->o32_pagesize > OBJPAGELEN + 252) 1465 1423 { 1466 rc = KLDR_ERR_LX_BAD_PAGE_MAP;1424 rc = VERR_LDRLX_BAD_PAGE_MAP; 1467 1425 break; 1468 1426 } 1469 1427 1470 1428 /* read it and ensure 4 extra zero bytes. */ 1471 rc = kRdrRead(pRdr, pbTmpPage, pMap->o32_pagesize,1472 pModLX->Hdr.e32_datapage + (pMap->o32_pagedataoffset << pModLX->Hdr.e32_pageshift));1429 rc = pRdr->pfnRead(pRdr, pbTmpPage, pMap->o32_pagesize, 1430 pModLX->Hdr.e32_datapage + (pMap->o32_pagedataoffset << pModLX->Hdr.e32_pageshift)); 1473 1431 if (rc) 1474 1432 break; 1475 kHlpMemSet(pbTmpPage + pMap->o32_pagesize, 0, 4);1433 memset(pbTmpPage + pMap->o32_pagesize, 0, 4); 1476 1434 1477 1435 /* unpack it into the image page. */ … … 1484 1442 case INVALID: /* we're probably not dealing correctly with INVALID pages... */ 1485 1443 case ZEROED: 1486 kHlpMemSet(pbPage, 0, OBJPAGELEN);1444 memset(pbPage, 0, OBJPAGELEN); 1487 1445 break; 1488 1446 … … 1491 1449 /* Falls through. */ 1492 1450 default: 1493 rc = KLDR_ERR_LX_BAD_PAGE_MAP;1451 rc = VERR_LDRLX_BAD_PAGE_MAP; 1494 1452 break; 1495 1453 } … … 1502 1460 */ 1503 1461 if (iPage < cPages) 1504 kHlpMemSet(pbPage, 0, (cPages - iPage) * OBJPAGELEN);1462 memset(pbPage, 0, (cPages - iPage) * OBJPAGELEN); 1505 1463 } 1506 1464 1507 1465 if (pbTmpPage) 1508 kHlpFree(pbTmpPage);1466 RTMemFree(pbTmpPage); 1509 1467 return rc; 1510 1468 } … … 1520 1478 * contains 4 additional zero bytes. 1521 1479 */ 1522 static int kldrModLXDoIterDataUnpacking( KU8 *pbDst, const KU8*pbSrc, int cbSrc)1480 static int kldrModLXDoIterDataUnpacking(uint8_t *pbDst, const uint8_t *pbSrc, int cbSrc) 1523 1481 { 1524 1482 const struct LX_Iter *pIter = (const struct LX_Iter *)pbSrc; … … 1527 1485 /* Validate size of data. */ 1528 1486 if (cbSrc >= (int)OBJPAGELEN - 2) 1529 return KLDR_ERR_LX_BAD_ITERDATA;1487 return VERR_LDRLX_BAD_ITERDATA; 1530 1488 1531 1489 /* … … 1541 1499 cbDst -= pIter->LX_nIter; 1542 1500 if (cbDst < 0) 1543 return KLDR_ERR_LX_BAD_ITERDATA;1501 return VERR_LDRLX_BAD_ITERDATA; 1544 1502 1545 1503 cbSrc -= 4 + 1; 1546 1504 if (cbSrc < -4) 1547 return KLDR_ERR_LX_BAD_ITERDATA;1548 1549 kHlpMemSet(pbDst, pIter->LX_Iterdata, pIter->LX_nIter);1505 return VERR_LDRLX_BAD_ITERDATA; 1506 1507 memset(pbDst, pIter->LX_Iterdata, pIter->LX_nIter); 1550 1508 pbDst += pIter->LX_nIter; 1551 1509 pIter++; … … 1560 1518 cbDst -= pIter->LX_nIter * pIter->LX_nBytes; 1561 1519 if (cbDst < 0) 1562 return KLDR_ERR_LX_BAD_ITERDATA;1520 return VERR_LDRLX_BAD_ITERDATA; 1563 1521 1564 1522 cbSrc -= 4 + pIter->LX_nBytes; 1565 1523 if (cbSrc < -4) 1566 return KLDR_ERR_LX_BAD_ITERDATA;1524 return VERR_LDRLX_BAD_ITERDATA; 1567 1525 1568 1526 for (i = pIter->LX_nIter; i > 0; i--, pbDst += pIter->LX_nBytes) 1569 kHlpMemCopy(pbDst, &pIter->LX_Iterdata, pIter->LX_nBytes);1527 memcpy(pbDst, &pIter->LX_Iterdata, pIter->LX_nBytes); 1570 1528 pIter = (struct LX_Iter *)((char*)pIter + 4 + pIter->LX_nBytes); 1571 1529 } … … 1576 1534 */ 1577 1535 if (cbDst > 0) 1578 kHlpMemSet(pbDst, 0, cbDst);1536 memset(pbDst, 0, cbDst); 1579 1537 1580 1538 return 0; … … 1591 1549 * contains 4 additional zero bytes. 1592 1550 */ 1593 static int kldrModLXDoIterData2Unpacking( KU8 *pbDst, const KU8*pbSrc, int cbSrc)1551 static int kldrModLXDoIterData2Unpacking(uint8_t *pbDst, const uint8_t *pbSrc, int cbSrc) 1594 1552 { 1595 1553 int cbDst = OBJPAGELEN; … … 1629 1587 cbDst -= cb; 1630 1588 if (cbDst < 0) 1631 return KLDR_ERR_LX_BAD_ITERDATA2;1589 return VERR_LDRLX_BAD_ITERDATA2; 1632 1590 cbSrc -= cb + 1; 1633 1591 if (cbSrc < 0) 1634 return KLDR_ERR_LX_BAD_ITERDATA2;1635 kHlpMemCopy(pbDst, ++pbSrc, cb);1592 return VERR_LDRLX_BAD_ITERDATA2; 1593 memcpy(pbDst, ++pbSrc, cb); 1636 1594 pbDst += cb; 1637 1595 pbSrc += cb; 1638 1596 } 1639 1597 else if (cbSrc < 2) 1640 return KLDR_ERR_LX_BAD_ITERDATA2;1598 return VERR_LDRLX_BAD_ITERDATA2; 1641 1599 else 1642 1600 { … … 1646 1604 cbDst -= cb; 1647 1605 if (cbDst < 0) 1648 return KLDR_ERR_LX_BAD_ITERDATA2;1606 return VERR_LDRLX_BAD_ITERDATA2; 1649 1607 cbSrc -= 3; 1650 1608 if (cbSrc < 0) 1651 return KLDR_ERR_LX_BAD_ITERDATA2;1652 kHlpMemSet(pbDst, pbSrc[2], cb);1609 return VERR_LDRLX_BAD_ITERDATA2; 1610 memset(pbDst, pbSrc[2], cb); 1653 1611 pbDst += cb; 1654 1612 pbSrc += 3; … … 1672 1630 cbSrc -= 2; 1673 1631 if (cbSrc < 0) 1674 return KLDR_ERR_LX_BAD_ITERDATA2;1632 return VERR_LDRLX_BAD_ITERDATA2; 1675 1633 else 1676 1634 { … … 1682 1640 cbSrc -= cb1; 1683 1641 if (cbSrc < 0) 1684 return KLDR_ERR_LX_BAD_ITERDATA2;1642 return VERR_LDRLX_BAD_ITERDATA2; 1685 1643 cbDst -= cb1; 1686 1644 if (cbDst < 0) 1687 return KLDR_ERR_LX_BAD_ITERDATA2;1688 kHlpMemCopy(pbDst, pbSrc, cb1);1645 return VERR_LDRLX_BAD_ITERDATA2; 1646 memcpy(pbDst, pbSrc, cb1); 1689 1647 pbDst += cb1; 1690 1648 pbSrc += cb1; 1691 1649 1692 1650 if (off > OBJPAGELEN - (unsigned)cbDst) 1693 return KLDR_ERR_LX_BAD_ITERDATA2;1651 return VERR_LDRLX_BAD_ITERDATA2; 1694 1652 cbDst -= cb2; 1695 1653 if (cbDst < 0) 1696 return KLDR_ERR_LX_BAD_ITERDATA2;1697 kHlpMemMove(pbDst, pbDst - off, cb2);1654 return VERR_LDRLX_BAD_ITERDATA2; 1655 memmove(pbDst, pbDst - off, cb2); 1698 1656 pbDst += cb2; 1699 1657 } … … 1718 1676 cbSrc -= 2; 1719 1677 if (cbSrc < 0) 1720 return KLDR_ERR_LX_BAD_ITERDATA2;1678 return VERR_LDRLX_BAD_ITERDATA2; 1721 1679 else 1722 1680 { … … 1726 1684 pbSrc += 2; 1727 1685 if (off > OBJPAGELEN - (unsigned)cbDst) 1728 return KLDR_ERR_LX_BAD_ITERDATA2;1686 return VERR_LDRLX_BAD_ITERDATA2; 1729 1687 cbDst -= cb; 1730 1688 if (cbDst < 0) 1731 return KLDR_ERR_LX_BAD_ITERDATA2;1689 return VERR_LDRLX_BAD_ITERDATA2; 1732 1690 kLdrModLXMemCopyW(pbDst, pbDst - off, cb); 1733 1691 pbDst += cb; … … 1753 1711 cbSrc -= 3; 1754 1712 if (cbSrc < 0) 1755 return KLDR_ERR_LX_BAD_ITERDATA2;1713 return VERR_LDRLX_BAD_ITERDATA2; 1756 1714 else 1757 1715 { … … 1763 1721 cbSrc -= cb1; 1764 1722 if (cbSrc < 0) 1765 return KLDR_ERR_LX_BAD_ITERDATA2;1723 return VERR_LDRLX_BAD_ITERDATA2; 1766 1724 cbDst -= cb1; 1767 1725 if (cbDst < 0) 1768 return KLDR_ERR_LX_BAD_ITERDATA2;1769 kHlpMemCopy(pbDst, pbSrc, cb1);1726 return VERR_LDRLX_BAD_ITERDATA2; 1727 memcpy(pbDst, pbSrc, cb1); 1770 1728 pbDst += cb1; 1771 1729 pbSrc += cb1; 1772 1730 1773 1731 if (off > OBJPAGELEN - (unsigned)cbDst) 1774 return KLDR_ERR_LX_BAD_ITERDATA2;1732 return VERR_LDRLX_BAD_ITERDATA2; 1775 1733 cbDst -= cb2; 1776 1734 if (cbDst < 0) 1777 return KLDR_ERR_LX_BAD_ITERDATA2;1735 return VERR_LDRLX_BAD_ITERDATA2; 1778 1736 kLdrModLXMemCopyW(pbDst, pbDst - off, cb2); 1779 1737 pbDst += cb2; … … 1791 1749 */ 1792 1750 if (cbDst > 0) 1793 kHlpMemSet(pbDst, 0, cbDst);1751 memset(pbDst, 0, cbDst); 1794 1752 1795 1753 return 0; … … 1808 1766 * @remark This assumes that unaligned word and dword access is fine. 1809 1767 */ 1810 static void kLdrModLXMemCopyW( KU8 *pbDst, const KU8*pbSrc, int cb)1768 static void kLdrModLXMemCopyW(uint8_t *pbDst, const uint8_t *pbSrc, int cb) 1811 1769 { 1812 1770 switch (pbDst - pbSrc) … … 1820 1778 *pbDst++ = *pbSrc++; 1821 1779 for (cb >>= 1; cb > 0; cb--, pbDst += 2, pbSrc += 2) 1822 *( KU16 *)pbDst = *(const KU16*)pbSrc;1780 *(uint16_t *)pbDst = *(const uint16_t *)pbSrc; 1823 1781 break; 1824 1782 … … 1829 1787 if (cb & 2) 1830 1788 { 1831 *( KU16 *)pbDst = *(const KU16*)pbSrc;1789 *(uint16_t *)pbDst = *(const uint16_t *)pbSrc; 1832 1790 pbDst += 2; 1833 1791 pbSrc += 2; 1834 1792 } 1835 1793 for (cb >>= 2; cb > 0; cb--, pbDst += 4, pbSrc += 4) 1836 *( KU32 *)pbDst = *(const KU32*)pbSrc;1794 *(uint32_t *)pbDst = *(const uint32_t *)pbSrc; 1837 1795 break; 1838 1796 } 1839 1797 } 1840 1798 1799 #if 0 1841 1800 1842 1801 /** … … 1853 1812 static int kldrModLXDoProtect(PKLDRMODLX pModLX, void *pvBits, unsigned fUnprotectOrProtect) 1854 1813 { 1855 KU32 i; 1856 PKLDRMOD pMod = pModLX->pMod; 1814 uint32_t i; 1857 1815 1858 1816 /* 1859 1817 * Change object protection. 1860 1818 */ 1861 for (i = 0; i < pMod ->cSegments; i++)1819 for (i = 0; i < pModLX->cSegments; i++) 1862 1820 { 1863 1821 int rc; … … 1866 1824 1867 1825 /* calc new protection. */ 1868 enmProt = pMod ->aSegments[i].enmProt;1826 enmProt = pModLX->aSegments[i].enmProt; 1869 1827 if (fUnprotectOrProtect) 1870 1828 { … … 1899 1857 1900 1858 /* calc the address and set page protection. */ 1901 pv = ( KU8 *)pvBits + pMod->aSegments[i].RVA;1902 1903 rc = kHlpPageProtect(pv, pMod ->aSegments[i].cbMapped, enmProt);1859 pv = (uint8_t *)pvBits + pModLX->aSegments[i].RVA; 1860 1861 rc = kHlpPageProtect(pv, pModLX->aSegments[i].cbMapped, enmProt); 1904 1862 if (rc) 1905 1863 break; … … 1913 1871 1914 1872 /** @copydoc kLdrModUnmap */ 1915 static int kldrModLXUnmap(P KLDRMODpMod)1916 { 1917 PKLDRMODLX pModLX = (PKLDRMODLX)pMod->pvData;1918 KU32i;1873 static int kldrModLXUnmap(PRTLDRMODINTERNAL pMod) 1874 { 1875 PKLDRMODLX pModLX = RT_FROM_MEMBER(pMod, KLDRMODLX, Core); 1876 uint32_t i; 1919 1877 int rc; 1920 1878 … … 1932 1890 pModLX->pvMapping = NULL; 1933 1891 1934 for (i = 0; i < pMod ->cSegments; i++)1935 pMod ->aSegments[i].MapAddress = 0;1892 for (i = 0; i < pModLX->cSegments; i++) 1893 pModLX->aSegments[i].MapAddress = 0; 1936 1894 1937 1895 return rc; … … 1940 1898 1941 1899 /** @copydoc kLdrModAllocTLS */ 1942 static int kldrModLXAllocTLS(P KLDRMODpMod, void *pvMapping)1943 { 1944 PKLDRMODLX pModLX = (PKLDRMODLX)pMod->pvData;1900 static int kldrModLXAllocTLS(PRTLDRMODINTERNAL pMod, void *pvMapping) 1901 { 1902 PKLDRMODLX pModLX = RT_FROM_MEMBER(pMod, KLDRMODLX, Core); 1945 1903 1946 1904 /* no tls, just do the error checking. */ … … 1953 1911 1954 1912 /** @copydoc kLdrModFreeTLS */ 1955 static void kldrModLXFreeTLS(P KLDRMODpMod, void *pvMapping)1913 static void kldrModLXFreeTLS(PRTLDRMODINTERNAL pMod, void *pvMapping) 1956 1914 { 1957 1915 /* no tls. */ 1958 K_NOREF(pMod);1959 K_NOREF(pvMapping);1916 RT_NOREF(pMod); 1917 RT_NOREF(pvMapping); 1960 1918 1961 1919 } … … 1963 1921 1964 1922 /** @copydoc kLdrModReload */ 1965 static int kldrModLXReload(P KLDRMODpMod)1966 { 1967 PKLDRMODLX pModLX = (PKLDRMODLX)pMod->pvData;1923 static int kldrModLXReload(PRTLDRMODINTERNAL pMod) 1924 { 1925 PKLDRMODLX pModLX = RT_FROM_MEMBER(pMod, KLDRMODLX, Core); 1968 1926 int rc, rc2; 1969 1927 … … 1997 1955 1998 1956 /** @copydoc kLdrModFixupMapping */ 1999 static int kldrModLXFixupMapping(P KLDRMOD pMod, PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser)2000 { 2001 PKLDRMODLX pModLX = (PKLDRMODLX)pMod->pvData;1957 static int kldrModLXFixupMapping(PRTLDRMODINTERNAL pMod, PFNRTLDRIMPORT pfnGetImport, void *pvUser) 1958 { 1959 PKLDRMODLX pModLX = RT_FROM_MEMBER(pMod, KLDRMODLX, Core); 2002 1960 int rc, rc2; 2003 1961 … … 2018 1976 * Apply fixups and resolve imports. 2019 1977 */ 2020 rc = kldrModLXRelocateBits(pMod, (void *)pModLX->pvMapping, (KUPTR)pModLX->pvMapping,2021 pMod->aSegments[0].LinkAddress, pfnGetImport, pvUser);1978 rc = rtldrLX_RelocateBits(pMod, (void *)pModLX->pvMapping, (uintptr_t)pModLX->pvMapping, 1979 pModLX->aSegments[0].LinkAddress, pfnGetImport, pvUser); 2022 1980 2023 1981 /* … … 2032 1990 2033 1991 /** @copydoc kLdrModCallInit */ 2034 static int kldrModLXCallInit(P KLDRMOD pMod, void *pvMapping, KUPTRuHandle)2035 { 2036 PKLDRMODLX pModLX = (PKLDRMODLX)pMod->pvData;1992 static int kldrModLXCallInit(PRTLDRMODINTERNAL pMod, void *pvMapping, uintptr_t uHandle) 1993 { 1994 PKLDRMODLX pModLX = RT_FROM_MEMBER(pMod, KLDRMODLX, Core); 2037 1995 int rc; 2038 1996 … … 2068 2026 * @param uHandle The module handle to present. 2069 2027 */ 2070 static int kldrModLXDoCallDLL(PKLDRMODLX pModLX, void *pvMapping, unsigned uOp, KUPTRuHandle)2028 static int kldrModLXDoCallDLL(PKLDRMODLX pModLX, void *pvMapping, unsigned uOp, uintptr_t uHandle) 2071 2029 { 2072 2030 int rc; … … 2082 2040 * Invoke the entrypoint and convert the boolean result to a kLdr status code. 2083 2041 */ 2084 rc = kldrModLXDoCall(( KUPTR)pvMapping2085 + ( KUPTR)pModLX->pMod->aSegments[pModLX->Hdr.e32_startobj - 1].RVA2042 rc = kldrModLXDoCall((uintptr_t)pvMapping 2043 + (uintptr_t)pModLX->aSegments[pModLX->Hdr.e32_startobj - 1].RVA 2086 2044 + pModLX->Hdr.e32_eip, 2087 2045 uHandle, uOp, NULL); … … 2105 2063 * @param pvReserved The third argument, reserved argument. (figure this one out) 2106 2064 */ 2107 static KI32 kldrModLXDoCall(KUPTR uEntrypoint, KUPTR uHandle, KU32uOp, void *pvReserved)2065 static int32_t kldrModLXDoCall(uintptr_t uEntrypoint, uintptr_t uHandle, uint32_t uOp, void *pvReserved) 2108 2066 { 2109 2067 #if defined(__X86__) || defined(__i386__) || defined(_M_IX86) 2110 KI32rc;2068 int32_t rc; 2111 2069 /** @todo try/except */ 2112 2070 … … 2146 2104 # error "port me!" 2147 2105 # endif 2148 K_NOREF(pvReserved);2106 RT_NOREF(pvReserved); 2149 2107 return rc; 2150 2108 2151 2109 #else 2152 K_NOREF(uEntrypoint);2153 K_NOREF(uHandle);2154 K_NOREF(uOp);2155 K_NOREF(pvReserved);2110 RT_NOREF(uEntrypoint); 2111 RT_NOREF(uHandle); 2112 RT_NOREF(uOp); 2113 RT_NOREF(pvReserved); 2156 2114 return KCPU_ERR_ARCH_CPU_NOT_COMPATIBLE; 2157 2115 #endif … … 2160 2118 2161 2119 /** @copydoc kLdrModCallTerm */ 2162 static int kldrModLXCallTerm(P KLDRMOD pMod, void *pvMapping, KUPTRuHandle)2163 { 2164 PKLDRMODLX pModLX = (PKLDRMODLX)pMod->pvData;2120 static int kldrModLXCallTerm(PRTLDRMODINTERNAL pMod, void *pvMapping, uintptr_t uHandle) 2121 { 2122 PKLDRMODLX pModLX = RT_FROM_MEMBER(pMod, KLDRMODLX, Core); 2165 2123 2166 2124 /* … … 2185 2143 2186 2144 /** @copydoc kLdrModCallThread */ 2187 static int kldrModLXCallThread(P KLDRMOD pMod, void *pvMapping, KUPTRuHandle, unsigned fAttachingOrDetaching)2145 static int kldrModLXCallThread(PRTLDRMODINTERNAL pMod, void *pvMapping, uintptr_t uHandle, unsigned fAttachingOrDetaching) 2188 2146 { 2189 2147 /* no thread attach/detach callout. */ 2190 K_NOREF(pMod);2191 K_NOREF(pvMapping);2192 K_NOREF(uHandle);2193 K_NOREF(fAttachingOrDetaching);2148 RT_NOREF(pMod); 2149 RT_NOREF(pvMapping); 2150 RT_NOREF(uHandle); 2151 RT_NOREF(fAttachingOrDetaching); 2194 2152 return 0; 2195 2153 } 2196 2154 2197 2198 /** @copydoc kLdrModSize */ 2199 static KLDRADDR kldrModLXSize(PKLDRMOD pMod) 2200 { 2201 PKLDRMODLX pModLX = (PKLDRMODLX)pMod->pvData; 2155 #endif 2156 2157 /** 2158 * @interface_method_impl{RTLDROPS,pfnGetImageSize} 2159 */ 2160 static DECLCALLBACK(size_t) rtldrLX_GetImageSize(PRTLDRMODINTERNAL pMod) 2161 { 2162 PKLDRMODLX pModLX = RT_FROM_MEMBER(pMod, KLDRMODLX, Core); 2202 2163 return pModLX->cbMapped; 2203 2164 } 2204 2165 2205 2166 2206 /** @copydoc kLdrModGetBits */ 2207 static int kldrModLXGetBits(PKLDRMOD pMod, void *pvBits, KLDRADDR BaseAddress, PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser) 2208 { 2209 PKLDRMODLX pModLX = (PKLDRMODLX)pMod->pvData; 2210 int rc; 2167 /** 2168 * @interface_method_impl{RTLDROPS,pfnGetBits} 2169 */ 2170 static DECLCALLBACK(int) rtldrLX_GetBits(PRTLDRMODINTERNAL pMod, void *pvBits, RTUINTPTR BaseAddress, 2171 PFNRTLDRIMPORT pfnGetImport, void *pvUser) 2172 { 2173 PKLDRMODLX pModLX = RT_FROM_MEMBER(pMod, KLDRMODLX, Core); 2211 2174 2212 2175 /* 2213 2176 * Load the image bits. 2214 2177 */ 2215 rc = kldrModLXDoLoadBits(pModLX, pvBits); 2216 if (rc) 2217 return rc; 2218 2219 /* 2220 * Perform relocations. 2221 */ 2222 return kldrModLXRelocateBits(pMod, pvBits, BaseAddress, pMod->aSegments[0].LinkAddress, pfnGetImport, pvUser); 2223 2224 } 2225 2226 2227 /** @copydoc kLdrModRelocateBits */ 2228 static int kldrModLXRelocateBits(PKLDRMOD pMod, void *pvBits, KLDRADDR NewBaseAddress, KLDRADDR OldBaseAddress, 2229 PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser) 2230 { 2231 PKLDRMODLX pModLX = (PKLDRMODLX)pMod->pvData; 2232 KU32 iSeg; 2178 int rc = kldrModLXDoLoadBits(pModLX, pvBits); 2179 if (RT_SUCCESS(rc)) 2180 { 2181 /* 2182 * Perform relocations. 2183 */ 2184 rc = rtldrLX_RelocateBits(pMod, pvBits, BaseAddress, pModLX->aSegments[0].LinkAddress, pfnGetImport, pvUser); 2185 } 2186 return rc; 2187 } 2188 2189 2190 /** 2191 * @interface_method_impl{RTLDROPS,pfnRelocate} 2192 */ 2193 static DECLCALLBACK(int) rtldrLX_RelocateBits(PRTLDRMODINTERNAL pMod, void *pvBits, RTUINTPTR NewBaseAddress, 2194 RTUINTPTR OldBaseAddress, PFNRTLDRIMPORT pfnGetImport, void *pvUser) 2195 { 2196 PKLDRMODLX pModLX = RT_FROM_MEMBER(pMod, KLDRMODLX, Core); 2197 uint32_t iSeg; 2233 2198 int rc; 2234 2199 … … 2257 2222 { 2258 2223 const struct o32_obj * const pObj = &pModLX->paObjs[iSeg]; 2259 KLDRADDR PageAddress = NewBaseAddress + pModLX->pMod->aSegments[iSeg].RVA;2260 KU32iPage;2261 KU8 *pbPage = (KU8 *)pvBits + (KUPTR)pModLX->pMod->aSegments[iSeg].RVA;2224 RTLDRADDR PageAddress = NewBaseAddress + pModLX->aSegments[iSeg].RVA; 2225 uint32_t iPage; 2226 uint8_t *pbPage = (uint8_t *)pvBits + (uintptr_t)pModLX->aSegments[iSeg].RVA; 2262 2227 2263 2228 /* … … 2266 2231 for (iPage = 0, rc = 0; !rc && iPage < pObj->o32_mapsize; iPage++, pbPage += OBJPAGELEN, PageAddress += OBJPAGELEN) 2267 2232 { 2268 const KU8* const pbFixupRecEnd = pModLX->pbFixupRecs + pModLX->paoffPageFixups[iPage + pObj->o32_pagemap];2269 const KU8*pb = pModLX->pbFixupRecs + pModLX->paoffPageFixups[iPage + pObj->o32_pagemap - 1];2270 KLDRADDR uValue = NIL_KLDRADDR;2271 KU32fKind = 0;2272 int iSelector;2233 const uint8_t * const pbFixupRecEnd = pModLX->pbFixupRecs + pModLX->paoffPageFixups[iPage + pObj->o32_pagemap]; 2234 const uint8_t *pb = pModLX->pbFixupRecs + pModLX->paoffPageFixups[iPage + pObj->o32_pagemap - 1]; 2235 RTLDRADDR uValue = NIL_RTLDRADDR; 2236 uint32_t fKind = 0; 2237 int iSelector; 2273 2238 2274 2239 /* sanity */ 2275 2240 if (pbFixupRecEnd < pb) 2276 return KLDR_ERR_BAD_FIXUP;2241 return VERR_LDR_BAD_FIXUP; 2277 2242 if (pbFixupRecEnd - 1 > pModLX->pbFixupSectionLast) 2278 return KLDR_ERR_BAD_FIXUP;2243 return VERR_LDR_BAD_FIXUP; 2279 2244 if (pb < pModLX->pbFixupSection) 2280 return KLDR_ERR_BAD_FIXUP;2245 return VERR_LDR_BAD_FIXUP; 2281 2246 2282 2247 /* … … 2287 2252 union _rel 2288 2253 { 2289 const KU8* pb;2254 const uint8_t * pb; 2290 2255 const struct r32_rlc *prlc; 2291 2256 } u; 2292 2257 char szImpModule[256]; 2293 2258 u.pb = pb; 2294 2259 pb += 3 + (u.prlc->nr_stype & NRCHAIN ? 0 : 1); /* place pch at the 4th member. */ … … 2304 2269 case NRRINT: 2305 2270 { 2306 KU16iTrgObject;2307 KU32offTrgObject;2271 uint16_t iTrgObject; 2272 uint32_t offTrgObject; 2308 2273 2309 2274 /* the object */ 2310 2275 if (u.prlc->nr_flags & NR16OBJMOD) 2311 2276 { 2312 iTrgObject = *(const KU16*)pb;2277 iTrgObject = *(const uint16_t *)pb; 2313 2278 pb += 2; 2314 2279 } … … 2317 2282 iTrgObject--; 2318 2283 if (iTrgObject >= pModLX->Hdr.e32_objcnt) 2319 return KLDR_ERR_BAD_FIXUP;2284 return VERR_LDR_BAD_FIXUP; 2320 2285 2321 2286 /* the target */ … … 2324 2289 if (u.prlc->nr_flags & NR32BITOFF) 2325 2290 { 2326 offTrgObject = *(const KU32*)pb;2291 offTrgObject = *(const uint32_t *)pb; 2327 2292 pb += 4; 2328 2293 } 2329 2294 else 2330 2295 { 2331 offTrgObject = *(const KU16*)pb;2296 offTrgObject = *(const uint16_t *)pb; 2332 2297 pb += 2; 2333 2298 } 2334 2299 2335 2300 /* calculate the symbol info. */ 2336 uValue = offTrgObject + NewBaseAddress + pMod ->aSegments[iTrgObject].RVA;2301 uValue = offTrgObject + NewBaseAddress + pModLX->aSegments[iTrgObject].RVA; 2337 2302 } 2338 2303 else 2339 uValue = NewBaseAddress + pMod ->aSegments[iTrgObject].RVA;2304 uValue = NewBaseAddress + pModLX->aSegments[iTrgObject].RVA; 2340 2305 if ( (u.prlc->nr_stype & NRALIAS) 2341 || (pMod ->aSegments[iTrgObject].fFlags & KLDRSEG_FLAG_16BIT))2342 iSelector = pMod ->aSegments[iTrgObject].Sel16bit;2306 || (pModLX->aSegments[iTrgObject].fFlags & RTLDRSEG_FLAG_16BIT)) 2307 iSelector = pModLX->aSegments[iTrgObject].Sel16bit; 2343 2308 else 2344 iSelector = pMod ->aSegments[iTrgObject].SelFlat;2309 iSelector = pModLX->aSegments[iTrgObject].SelFlat; 2345 2310 fKind = 0; 2346 2311 break; … … 2352 2317 case NRRORD: 2353 2318 { 2354 KU16iModule;2355 KU32iSymbol;2319 uint16_t iModule; 2320 uint32_t iSymbol; 2356 2321 2357 2322 /* the module ordinal */ 2358 2323 if (u.prlc->nr_flags & NR16OBJMOD) 2359 2324 { 2360 iModule = *(const KU16*)pb;2325 iModule = *(const uint16_t *)pb; 2361 2326 pb += 2; 2362 2327 } … … 2365 2330 iModule--; 2366 2331 if (iModule >= pModLX->Hdr.e32_impmodcnt) 2367 return KLDR_ERR_BAD_FIXUP; 2332 return VERR_LDR_BAD_FIXUP; 2333 rc = kldrModLXGetImport(pModLX, NULL, iModule, szImpModule, sizeof(szImpModule), NULL); 2334 if (RT_FAILURE(rc)) 2335 return rc; 2336 2368 2337 #if 1 2369 2338 if (u.prlc->nr_flags & NRICHAIN) 2370 return KLDR_ERR_BAD_FIXUP;2339 return VERR_LDR_BAD_FIXUP; 2371 2340 #endif 2372 2341 … … 2374 2343 if (u.prlc->nr_flags & NR32BITOFF) 2375 2344 { 2376 iSymbol = *(const KU32*)pb;2345 iSymbol = *(const uint32_t *)pb; 2377 2346 pb += 4; 2378 2347 } 2379 2348 else if (!(u.prlc->nr_flags & NR8BITORD)) 2380 2349 { 2381 iSymbol = *(const KU16*)pb;2350 iSymbol = *(const uint16_t *)pb; 2382 2351 pb += 2; 2383 2352 } … … 2386 2355 2387 2356 /* resolve it. */ 2388 rc = pfnGetImport(pMod, iModule, iSymbol, NULL, 0, NULL, &uValue, &fKind,pvUser);2357 rc = pfnGetImport(pMod, szImpModule, NULL, iSymbol, &uValue, /*&fKind,*/ pvUser); 2389 2358 if (rc) 2390 2359 return rc; … … 2398 2367 case NRRNAM: 2399 2368 { 2400 KU32iModule;2401 KU16offSymbol;2402 const KU8*pbSymbol;2369 uint32_t iModule; 2370 uint16_t offSymbol; 2371 const uint8_t *pbSymbol; 2403 2372 2404 2373 /* the module ordinal */ 2405 2374 if (u.prlc->nr_flags & NR16OBJMOD) 2406 2375 { 2407 iModule = *(const KU16*)pb;2376 iModule = *(const uint16_t *)pb; 2408 2377 pb += 2; 2409 2378 } … … 2412 2381 iModule--; 2413 2382 if (iModule >= pModLX->Hdr.e32_impmodcnt) 2414 return KLDR_ERR_BAD_FIXUP; 2383 return VERR_LDR_BAD_FIXUP; 2384 rc = kldrModLXGetImport(pModLX, NULL, iModule, szImpModule, sizeof(szImpModule), NULL); 2385 if (RT_FAILURE(rc)) 2386 return rc; 2415 2387 #if 1 2416 2388 if (u.prlc->nr_flags & NRICHAIN) 2417 return KLDR_ERR_BAD_FIXUP;2389 return VERR_LDR_BAD_FIXUP; 2418 2390 #endif 2419 2391 … … 2421 2393 if (u.prlc->nr_flags & NR32BITOFF) 2422 2394 { 2423 offSymbol = *(const KU32*)pb;2395 offSymbol = *(const uint32_t *)pb; 2424 2396 pb += 4; 2425 2397 } 2426 2398 else if (!(u.prlc->nr_flags & NR8BITORD)) 2427 2399 { 2428 offSymbol = *(const KU16*)pb;2400 offSymbol = *(const uint16_t *)pb; 2429 2401 pb += 2; 2430 2402 } … … 2434 2406 if ( pbSymbol < pModLX->pbImportProcs 2435 2407 || pbSymbol > pModLX->pbFixupSectionLast) 2436 return KLDR_ERR_BAD_FIXUP; 2408 return VERR_LDR_BAD_FIXUP; 2409 char szSymbol[256]; 2410 memcpy(szSymbol, pbSymbol + 1, *pbSymbol); 2411 szSymbol[*pbSymbol] = '\0'; 2437 2412 2438 2413 /* resolve it. */ 2439 rc = pfnGetImport(pMod, iModule, NIL_KLDRMOD_SYM_ORDINAL, (const char *)pbSymbol + 1, *pbSymbol, NULL, 2440 &uValue, &fKind, pvUser); 2414 rc = pfnGetImport(pMod, szImpModule, szSymbol, UINT32_MAX, &uValue, /*&fKind,*/ pvUser); 2441 2415 if (rc) 2442 2416 return rc; … … 2458 2432 if (u.prlc->nr_flags & NR32BITADD) 2459 2433 { 2460 uValue += *(const KU32*)pb;2434 uValue += *(const uint32_t *)pb; 2461 2435 pb += 4; 2462 2436 } 2463 2437 else 2464 2438 { 2465 uValue += *(const KU16*)pb;2439 uValue += *(const uint16_t *)pb; 2466 2440 pb += 2; 2467 2441 } … … 2480 2454 && off >= 0 2481 2455 && off <= (int)OBJPAGELEN - 4) 2482 *( KU32 *)&pbPage[off] = (KU32)uValue;2456 *(uint32_t *)&pbPage[off] = (uint32_t)uValue; 2483 2457 else if ( (u.prlc->nr_stype & NRSRCMASK) == NRSOFF32 2484 2458 && off >= 0 2485 2459 && off <= (int)OBJPAGELEN - 4) 2486 *( KU32 *)&pbPage[off] = (KU32)(uValue - (PageAddress + off + 4));2460 *(uint32_t *)&pbPage[off] = (uint32_t)(uValue - (PageAddress + off + 4)); 2487 2461 else 2488 2462 { … … 2495 2469 else if (!(u.prlc->nr_flags & NRICHAIN)) 2496 2470 { 2497 const KI16 *poffSrc = (const KI16*)pb;2498 KU8c = u.pb[2];2471 const int16_t *poffSrc = (const int16_t *)pb; 2472 uint8_t c = u.pb[2]; 2499 2473 2500 2474 /* common / simple */ … … 2505 2479 int off = *poffSrc++; 2506 2480 if (off >= 0 && off <= (int)OBJPAGELEN - 4) 2507 *( KU32 *)&pbPage[off] = (KU32)uValue;2481 *(uint32_t *)&pbPage[off] = (uint32_t)uValue; 2508 2482 else 2509 2483 { … … 2520 2494 int off = *poffSrc++; 2521 2495 if (off >= 0 && off <= (int)OBJPAGELEN - 4) 2522 *( KU32 *)&pbPage[off] = (KU32)(uValue - (PageAddress + off + 4));2496 *(uint32_t *)&pbPage[off] = (uint32_t)(uValue - (PageAddress + off + 4)); 2523 2497 else 2524 2498 { … … 2538 2512 } 2539 2513 } 2540 pb = (const KU8*)poffSrc;2514 pb = (const uint8_t *)poffSrc; 2541 2515 } 2542 2516 else … … 2544 2518 /* This is a pain because it will require virgin pages on a relocation. */ 2545 2519 KLDRMODLX_ASSERT(!"NRICHAIN"); 2546 return KLDR_ERR_LX_NRICHAIN_NOT_SUPPORTED;2520 return VERR_LDRLX_NRICHAIN_NOT_SUPPORTED; 2547 2521 } 2548 2522 } … … 2566 2540 * @param fKind The target kind. 2567 2541 */ 2568 static int kldrModLXDoReloc( KU8 *pbPage, int off, KLDRADDR PageAddress, const struct r32_rlc *prlc,2569 int iSelector, KLDRADDR uValue, KU32fKind)2542 static int kldrModLXDoReloc(uint8_t *pbPage, int off, RTLDRADDR PageAddress, const struct r32_rlc *prlc, 2543 int iSelector, RTLDRADDR uValue, uint32_t fKind) 2570 2544 { 2571 2545 #pragma pack(1) /* just to be sure */ 2572 2546 union 2573 2547 { 2574 KU8ab[6];2575 KU32off32;2576 KU16off16;2577 KU8off8;2548 uint8_t ab[6]; 2549 uint32_t off32; 2550 uint16_t off16; 2551 uint8_t off8; 2578 2552 struct 2579 2553 { 2580 KU16off;2581 KU16Sel;2554 uint16_t off; 2555 uint16_t Sel; 2582 2556 } Far16; 2583 2557 struct 2584 2558 { 2585 KU32off;2586 KU16Sel;2559 uint32_t off; 2560 uint16_t Sel; 2587 2561 } Far32; 2588 2562 } uData; 2589 2563 #pragma pack() 2590 const KU8*pbSrc;2591 KU8*pbDst;2592 KU8cb;2593 2594 K_NOREF(fKind);2564 const uint8_t *pbSrc; 2565 uint8_t *pbDst; 2566 uint8_t cb; 2567 2568 RT_NOREF(fKind); 2595 2569 2596 2570 /* … … 2600 2574 { 2601 2575 case NRSBYT: 2602 uData.off8 = ( KU8)uValue;2576 uData.off8 = (uint8_t)uValue; 2603 2577 cb = 1; 2604 2578 break; … … 2616 2590 /* fixme */ 2617 2591 } 2618 uData.Far16.off = ( KU16)uValue;2592 uData.Far16.off = (uint16_t)uValue; 2619 2593 uData.Far16.Sel = iSelector; 2620 2594 cb = 4; 2621 2595 break; 2622 2596 case NRSOFF: 2623 uData.off16 = ( KU16)uValue;2597 uData.off16 = (uint16_t)uValue; 2624 2598 cb = 2; 2625 2599 break; … … 2629 2603 /* fixme */ 2630 2604 } 2631 uData.Far32.off = ( KU32)uValue;2605 uData.Far32.off = (uint32_t)uValue; 2632 2606 uData.Far32.Sel = iSelector; 2633 2607 cb = 6; 2634 2608 break; 2635 2609 case NROFF32: 2636 uData.off32 = ( KU32)uValue;2610 uData.off32 = (uint32_t)uValue; 2637 2611 cb = 4; 2638 2612 break; 2639 2613 case NRSOFF32: 2640 uData.off32 = ( KU32)(uValue - (PageAddress + off + 4));2614 uData.off32 = (uint32_t)(uValue - (PageAddress + off + 4)); 2641 2615 cb = 4; 2642 2616 break; 2643 2617 default: 2644 return KLDR_ERR_LX_BAD_FIXUP_SECTION; /** @todo fix error, add more checks! */2618 return VERR_LDRLX_BAD_FIXUP_SECTION; /** @todo fix error, add more checks! */ 2645 2619 } 2646 2620 … … 2665 2639 2666 2640 /** 2667 * The LX module interpreter method table. 2668 */ 2669 KLDRMODOPS g_kLdrModLXOps = 2641 * @interface_method_impl{RTLDROPS,pfnEnumSegments} 2642 */ 2643 static DECLCALLBACK(int) rtldrLX_EnumSegments(PRTLDRMODINTERNAL pMod, PFNRTLDRENUMSEGS pfnCallback, void *pvUser) 2644 { 2645 PKLDRMODLX pThis = RT_FROM_MEMBER(pMod, KLDRMODLX, Core); 2646 uint32_t const cSegments = pThis->cSegments; 2647 for (uint32_t iSeg = 0; iSeg < cSegments; iSeg++) 2648 { 2649 RTLDRSEG Seg = pThis->aSegments[iSeg]; 2650 int rc = pfnCallback(pMod, &Seg, pvUser); 2651 if (rc != VINF_SUCCESS) 2652 return rc; 2653 } 2654 2655 return VINF_SUCCESS; 2656 } 2657 2658 2659 /** 2660 * @interface_method_impl{RTLDROPS,pfnLinkAddressToSegOffset} 2661 */ 2662 static DECLCALLBACK(int) rtldrLX_LinkAddressToSegOffset(PRTLDRMODINTERNAL pMod, RTLDRADDR LinkAddress, 2663 uint32_t *piSeg, PRTLDRADDR poffSeg) 2664 { 2665 PKLDRMODLX pThis = RT_FROM_MEMBER(pMod, KLDRMODLX, Core); 2666 uint32_t const cSegments = pThis->cSegments; 2667 for (uint32_t iSeg = 0; iSeg < cSegments; iSeg++) 2668 { 2669 RTLDRADDR offSeg = LinkAddress - pThis->aSegments[iSeg].LinkAddress; 2670 if ( offSeg < pThis->aSegments[iSeg].cbMapped 2671 || offSeg < pThis->aSegments[iSeg].cb) 2672 { 2673 *piSeg = iSeg; 2674 *poffSeg = offSeg; 2675 return VINF_SUCCESS; 2676 } 2677 } 2678 2679 return VERR_LDR_INVALID_LINK_ADDRESS; 2680 } 2681 2682 2683 /** 2684 * @interface_method_impl{RTLDROPS,pfnLinkAddressToRva}. 2685 */ 2686 static DECLCALLBACK(int) rtldrLX_LinkAddressToRva(PRTLDRMODINTERNAL pMod, RTLDRADDR LinkAddress, PRTLDRADDR pRva) 2687 { 2688 PKLDRMODLX pThis = RT_FROM_MEMBER(pMod, KLDRMODLX, Core); 2689 uint32_t const cSegments = pThis->cSegments; 2690 for (uint32_t iSeg = 0; iSeg < cSegments; iSeg++) 2691 { 2692 RTLDRADDR offSeg = LinkAddress - pThis->aSegments[iSeg].LinkAddress; 2693 if ( offSeg < pThis->aSegments[iSeg].cbMapped 2694 || offSeg < pThis->aSegments[iSeg].cb) 2695 { 2696 *pRva = pThis->aSegments[iSeg].RVA + offSeg; 2697 return VINF_SUCCESS; 2698 } 2699 } 2700 2701 return VERR_LDR_INVALID_RVA; 2702 } 2703 2704 2705 /** 2706 * @interface_method_impl{RTLDROPS,pfnSegOffsetToRva} 2707 */ 2708 static DECLCALLBACK(int) rtldrLX_SegOffsetToRva(PRTLDRMODINTERNAL pMod, uint32_t iSeg, RTLDRADDR offSeg, PRTLDRADDR pRva) 2709 { 2710 PKLDRMODLX pThis = RT_FROM_MEMBER(pMod, KLDRMODLX, Core); 2711 2712 if (iSeg >= pThis->cSegments) 2713 return VERR_LDR_INVALID_SEG_OFFSET; 2714 PCRTLDRSEG pSegment = &pThis->aSegments[iSeg]; 2715 2716 if ( offSeg > pSegment->cbMapped 2717 && offSeg > pSegment->cb 2718 && ( pSegment->cbFile < 0 2719 || offSeg > (uint64_t)pSegment->cbFile)) 2720 return VERR_LDR_INVALID_SEG_OFFSET; 2721 2722 *pRva = pSegment->RVA + offSeg; 2723 return VINF_SUCCESS; 2724 } 2725 2726 2727 /** 2728 * @interface_method_impl{RTLDROPS,pfnRvaToSegOffset} 2729 */ 2730 static DECLCALLBACK(int) rtldrLX_RvaToSegOffset(PRTLDRMODINTERNAL pMod, RTLDRADDR Rva, uint32_t *piSeg, PRTLDRADDR poffSeg) 2731 { 2732 PKLDRMODLX pThis = RT_FROM_MEMBER(pMod, KLDRMODLX, Core); 2733 uint32_t const cSegments = pThis->cSegments; 2734 for (uint32_t iSeg = 0; iSeg < cSegments; iSeg++) 2735 { 2736 RTLDRADDR offSeg = Rva - pThis->aSegments[iSeg].RVA; 2737 if ( offSeg < pThis->aSegments[iSeg].cbMapped 2738 || offSeg < pThis->aSegments[iSeg].cb) 2739 { 2740 *piSeg = iSeg; 2741 *poffSeg = offSeg; 2742 return VINF_SUCCESS; 2743 } 2744 } 2745 2746 return VERR_LDR_INVALID_RVA; 2747 } 2748 2749 2750 /** 2751 * @interface_method_impl{RTLDROPS,pfnReadDbgInfo} 2752 */ 2753 static DECLCALLBACK(int) rtldrLX_ReadDbgInfo(PRTLDRMODINTERNAL pMod, uint32_t iDbgInfo, RTFOFF off, size_t cb, void *pvBuf) 2754 { 2755 //PKLDRMODLX pThis = RT_FROM_MEMBER(pMod, KLDRMODLX, Core); 2756 RT_NOREF(pMod, iDbgInfo, off, cb, pvBuf); 2757 return VERR_OUT_OF_RANGE; 2758 } 2759 2760 2761 /** @interface_method_impl{RTLDROPS,pfnQueryProp} */ 2762 static DECLCALLBACK(int) rtldrLX_QueryProp(PRTLDRMODINTERNAL pMod, RTLDRPROP enmProp, void const *pvBits, 2763 void *pvBuf, size_t cbBuf, size_t *pcbRet) 2764 { 2765 PKLDRMODLX pThis = RT_FROM_MEMBER(pMod, KLDRMODLX, Core); 2766 int rc; 2767 switch (enmProp) 2768 { 2769 case RTLDRPROP_IMPORT_COUNT: 2770 Assert(cbBuf == sizeof(uint32_t)); 2771 Assert(*pcbRet == cbBuf); 2772 *(uint32_t *)pvBuf = pThis->Hdr.e32_impmodcnt; 2773 rc = VINF_SUCCESS; 2774 break; 2775 2776 case RTLDRPROP_IMPORT_MODULE: 2777 rc = kldrModLXGetImport(pThis, pvBits, *(uint32_t const *)pvBuf, (char *)pvBuf, cbBuf, pcbRet); 2778 break; 2779 2780 case RTLDRPROP_INTERNAL_NAME: 2781 *pcbRet = pThis->cchName + 1; 2782 if (cbBuf >= pThis->cchName + 1) 2783 { 2784 memcpy(pvBuf, pThis->pszName, pThis->cchName + 1); 2785 rc = VINF_SUCCESS; 2786 } 2787 else 2788 rc = VERR_BUFFER_OVERFLOW; 2789 break; 2790 2791 2792 default: 2793 rc = VERR_NOT_FOUND; 2794 break; 2795 } 2796 RT_NOREF_PV(pvBits); 2797 return rc; 2798 } 2799 2800 2801 /** 2802 * Operations for a Mach-O module interpreter. 2803 */ 2804 static const RTLDROPS s_rtldrLXOps= 2670 2805 { 2671 2806 "LX", 2807 rtldrLX_Close, 2672 2808 NULL, 2673 kldrModLXCreate, 2674 kldrModLXDestroy, 2675 kldrModLXQuerySymbol, 2676 kldrModLXEnumSymbols, 2677 kldrModLXGetImport, 2678 kldrModLXNumberOfImports, 2679 NULL /* can execute one is optional */, 2680 kldrModLXGetStackInfo, 2681 kldrModLXQueryMainEntrypoint, 2682 NULL /* pfnQueryImageUuid */, 2683 NULL /* fixme */, 2684 NULL /* fixme */, 2685 kldrModLXEnumDbgInfo, 2686 kldrModLXHasDbgInfo, 2687 kldrModLXMap, 2688 kldrModLXUnmap, 2689 kldrModLXAllocTLS, 2690 kldrModLXFreeTLS, 2691 kldrModLXReload, 2692 kldrModLXFixupMapping, 2693 kldrModLXCallInit, 2694 kldrModLXCallTerm, 2695 kldrModLXCallThread, 2696 kldrModLXSize, 2697 kldrModLXGetBits, 2698 kldrModLXRelocateBits, 2699 NULL /* fixme: pfnMostlyDone */, 2700 42 /* the end */ 2809 NULL /*pfnDone*/, 2810 rtldrLX_EnumSymbols, 2811 /* ext */ 2812 rtldrLX_GetImageSize, 2813 rtldrLX_GetBits, 2814 rtldrLX_RelocateBits, 2815 rtldrLX_GetSymbolEx, 2816 NULL /*pfnQueryForwarderInfo*/, 2817 rtldrLX_EnumDbgInfo, 2818 rtldrLX_EnumSegments, 2819 rtldrLX_LinkAddressToSegOffset, 2820 rtldrLX_LinkAddressToRva, 2821 rtldrLX_SegOffsetToRva, 2822 rtldrLX_RvaToSegOffset, 2823 NULL, 2824 rtldrLX_QueryProp, 2825 NULL /*pfnVerifySignature*/, 2826 NULL /*pfnHashImage*/, 2827 NULL /*pfnUnwindFrame*/, 2828 42 2701 2829 }; 2702 2830 2831 2832 /** 2833 * Handles opening LX images. 2834 */ 2835 DECLHIDDEN(int) rtldrLXOpen(PRTLDRREADER pReader, uint32_t fFlags, RTLDRARCH enmArch, RTFOFF offLxHdr, 2836 PRTLDRMOD phLdrMod, PRTERRINFO pErrInfo) 2837 { 2838 2839 /* 2840 * Create the instance data and do a minimal header validation. 2841 */ 2842 PKLDRMODLX pThis = NULL; 2843 int rc = kldrModLXDoCreate(pReader, offLxHdr, fFlags, &pThis, pErrInfo); 2844 if (RT_SUCCESS(rc)) 2845 { 2846 /* 2847 * Match up against the requested CPU architecture. 2848 */ 2849 if ( enmArch == RTLDRARCH_WHATEVER 2850 || pThis->Core.enmArch == enmArch) 2851 { 2852 pThis->Core.pOps = &s_rtldrLXOps; 2853 pThis->Core.u32Magic = RTLDRMOD_MAGIC; 2854 *phLdrMod = &pThis->Core; 2855 return 0; 2856 } 2857 rc = VERR_LDR_ARCH_MISMATCH; 2858 } 2859 if (pThis) 2860 RTMemFree(pThis); 2861 return rc; 2862 2863 } 2864 -
trunk/src/VBox/Runtime/common/ldr/ldrMachO.cpp
r74636 r74638 29 29 */ 30 30 31 /******************************************************************************* 32 * Header Files * 33 *******************************************************************************/ 34 #include <k/kLdr.h> 35 #include "kLdrInternal.h" 36 #include <k/kLdrFmts/mach-o.h> 37 38 39 /******************************************************************************* 40 * Defined Constants And Macros * 41 *******************************************************************************/ 31 32 /********************************************************************************************************************************* 33 * Header Files * 34 *********************************************************************************************************************************/ 35 #define LOG_GROUP RTLOGGROUP_LDR 36 #include <iprt/ldr.h> 37 #include "internal/iprt.h" 38 39 #include <iprt/asm.h> 40 #include <iprt/assert.h> 41 #include <iprt/err.h> 42 #include <iprt/log.h> 43 #include <iprt/mem.h> 44 #include <iprt/string.h> 45 46 #include <iprt/formats/mach-o.h> 47 #include "internal/ldr.h" 48 49 50 /********************************************************************************************************************************* 51 * Defined Constants And Macros * 52 *********************************************************************************************************************************/ 42 53 /** @def KLDRMODMACHO_STRICT 43 54 * Define KLDRMODMACHO_STRICT to enabled strict checks in KLDRMODMACHO. */ … … 48 59 */ 49 60 #ifdef KLDRMODMACHO_STRICT 50 # define KLDRMODMACHO_ASSERT(expr) kHlpAssert(expr)61 # define KLDRMODMACHO_ASSERT(expr) Assert(expr) 51 62 #else 52 63 # define KLDRMODMACHO_ASSERT(expr) do {} while (0) … … 58 69 */ 59 70 #ifdef KLDRMODMACHO_STRICT2 60 # define KLDRMODMACHO_CHECK_RETURN(expr, rc) kHlpAssertReturn(expr, rc)71 # define KLDRMODMACHO_CHECK_RETURN(expr, rc) AssertReturn(expr, rc) 61 72 #else 62 73 # define KLDRMODMACHO_CHECK_RETURN(expr, rc) do { if (!(expr)) { return (rc); } } while (0) … … 68 79 */ 69 80 #ifdef KLDRMODMACHO_STRICT2 70 # define KLDRMODMACHO_FAILED_RETURN(rc) kHlpAssertFailedReturn(rc)81 # define KLDRMODMACHO_FAILED_RETURN(rc) AssertFailedReturn(rc) 71 82 #else 72 83 # define KLDRMODMACHO_FAILED_RETURN(rc) return (rc) … … 74 85 75 86 76 /******************************************************************************* 77 * Structures and Typedefs *78 ******************************************************************************* /87 /********************************************************************************************************************************* 88 * Structures and Typedefs * 89 *********************************************************************************************************************************/ 79 90 /** 80 91 * Mach-O section details. … … 83 94 { 84 95 /** The size of the section (in bytes). */ 85 KLDRSIZEcb;96 RTLDRADDR cb; 86 97 /** The link address of this section. */ 87 KLDRADDRLinkAddress;98 RTLDRADDR LinkAddress; 88 99 /** The RVA of this section. */ 89 KLDRADDRRVA;100 RTLDRADDR RVA; 90 101 /** The file offset of this section. 91 102 * This is -1 if the section doesn't have a file backing. */ 92 KLDRFOFFoffFile;103 RTFOFF offFile; 93 104 /** The number of fixups. */ 94 KU32cFixups;105 uint32_t cFixups; 95 106 /** The array of fixups. (lazy loaded) */ 96 107 macho_relocation_info_t *paFixups; 97 108 /** The file offset of the fixups for this section. 98 109 * This is -1 if the section doesn't have any fixups. */ 99 KLDRFOFFoffFixups;110 RTFOFF offFixups; 100 111 /** Mach-O section flags. */ 101 KU32fFlags;112 uint32_t fFlags; 102 113 /** kLdr segment index. */ 103 KU32iSegment;114 uint32_t iSegment; 104 115 /** Pointer to the Mach-O section structure. */ 105 116 void *pvMachoSection; … … 113 124 typedef struct KLDRMODMACHOSEG 114 125 { 126 /** Common segment info. */ 127 RTLDRSEG SegInfo; 128 115 129 /** The orignal segment number (in case we had to resort it). */ 116 KU32iOrgSegNo;130 uint32_t iOrgSegNo; 117 131 /** The number of sections in the segment. */ 118 KU32cSections;132 uint32_t cSections; 119 133 /** Pointer to the sections belonging to this segment. 120 134 * The array resides in the big memory chunk allocated for … … 130 144 typedef struct KLDRMODMACHO 131 145 { 132 /** Pointer to the module. (Follows the section table.) */ 133 PKLDRMOD pMod; 146 /** Core module structure. */ 147 RTLDRMODINTERNAL Core; 148 149 /** The minium cpu this module was built for. 150 * This might not be accurate, so use kLdrModCanExecuteOn() to check. */ 151 RTLDRCPU enmCpu; 152 /** The number of segments in the module. */ 153 uint32_t cSegments; 154 134 155 /** Pointer to the RDR file mapping of the raw file bits. NULL if not mapped. */ 135 156 const void *pvBits; … … 137 158 void *pvMapping; 138 159 /** The module open flags. */ 139 KU32fOpenFlags;160 uint32_t fOpenFlags; 140 161 141 162 /** The offset of the image. (FAT fun.) */ 142 KLDRFOFFoffImage;163 RTFOFF offImage; 143 164 /** The link address. */ 144 KLDRADDRLinkAddress;165 RTLDRADDR LinkAddress; 145 166 /** The size of the mapped image. */ 146 KLDRADDRcbImage;167 RTLDRADDR cbImage; 147 168 /** Whether we're capable of loading the image. */ 148 KBOOLfCanLoad;169 bool fCanLoad; 149 170 /** Whether we're creating a global offset table segment. 150 171 * This dependes on the cputype and image type. */ 151 KBOOLfMakeGot;172 bool fMakeGot; 152 173 /** The size of a indirect GOT jump stub entry. 153 174 * This is 0 if not needed. */ 154 KU8cbJmpStub;175 uint32_t cbJmpStub; 155 176 /** Effective file type. If the original was a MH_OBJECT file, the 156 177 * corresponding MH_DSYM needs the segment translation of a MH_OBJECT too. 157 178 * The MH_DSYM normally has a separate __DWARF segment, but this is 158 179 * automatically skipped during the transation. */ 159 KU8uEffFileType;180 uint32_t uEffFileType; 160 181 /** Pointer to the load commands. (endian converted) */ 161 KU8*pbLoadCommands;182 uint8_t *pbLoadCommands; 162 183 /** The Mach-O header. (endian converted) 163 184 * @remark The reserved field is only valid for real 64-bit headers. */ … … 165 186 166 187 /** The offset of the symbol table. */ 167 KLDRFOFFoffSymbols;188 RTFOFF offSymbols; 168 189 /** The number of symbols. */ 169 KU32cSymbols;190 uint32_t cSymbols; 170 191 /** The pointer to the loaded symbol table. */ 171 192 void *pvaSymbols; 172 193 /** The offset of the string table. */ 173 KLDRFOFFoffStrings;194 RTFOFF offStrings; 174 195 /** The size of the of the string table. */ 175 KU32cchStrings;196 uint32_t cchStrings; 176 197 /** Pointer to the loaded string table. */ 177 198 char *pchStrings; 178 199 179 200 /** The image UUID, all zeros if not found. */ 180 KU8abImageUuid[16];201 uint32_t abImageUuid[16]; 181 202 182 203 /** The RVA of the Global Offset Table. */ 183 KLDRADDRGotRVA;204 RTLDRADDR GotRVA; 184 205 /** The RVA of the indirect GOT jump stubs. */ 185 KLDRADDRJmpStubsRVA;206 RTLDRADDR JmpStubsRVA; 186 207 187 208 /** The number of sections. */ 188 KU32cSections;209 uint32_t cSections; 189 210 /** Pointer to the section array running in parallel to the Mach-O one. */ 190 211 PKLDRMODMACHOSECT paSections; … … 196 217 197 218 198 /******************************************************************************* 199 * Internal Functions * 200 *******************************************************************************/ 219 220 /********************************************************************************************************************************* 221 * Internal Functions * 222 *********************************************************************************************************************************/ 201 223 #if 0 202 static KI32 kldrModMachONumberOfImports(PKLDRMODpMod, const void *pvBits);224 static int32_t kldrModMachONumberOfImports(PRTLDRMODINTERNAL pMod, const void *pvBits); 203 225 #endif 204 static int kldrModMachORelocateBits(PKLDRMOD pMod, void *pvBits, KLDRADDR NewBaseAddress, KLDRADDR OldBaseAddress, 205 PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser); 206 207 static int kldrModMachODoCreate(PKRDR pRdr, KLDRFOFF offImage, KU32 fOpenFlags, PKLDRMODMACHO *ppMod); 208 static int kldrModMachOPreParseLoadCommands(KU8 *pbLoadCommands, const mach_header_32_t *pHdr, PKRDR pRdr, KLDRFOFF offImage, 209 KU32 fOpenFlags, KU32 *pcSegments, KU32 *pcSections, KU32 *pcbStringPool, 210 PKBOOL pfCanLoad, PKLDRADDR pLinkAddress, KU8 *puEffFileType); 211 static int kldrModMachOParseLoadCommands(PKLDRMODMACHO pModMachO, char *pbStringPool, KU32 cbStringPool); 212 static int kldrModMachOAdjustBaseAddress(PKLDRMODMACHO pModMachO, PKLDRADDR pBaseAddress); 213 214 /*static int kldrModMachOLoadLoadCommands(PKLDRMODMACHO pModMachO);*/ 215 static int kldrModMachOLoadObjSymTab(PKLDRMODMACHO pModMachO); 216 static int kldrModMachOLoadFixups(PKLDRMODMACHO pModMachO, KLDRFOFF offFixups, KU32 cFixups, macho_relocation_info_t **ppaFixups); 217 static int kldrModMachOMapVirginBits(PKLDRMODMACHO pModMachO); 218 219 static int kldrModMachODoQuerySymbol32Bit(PKLDRMODMACHO pModMachO, const macho_nlist_32_t *paSyms, KU32 cSyms, const char *pchStrings, 220 KU32 cchStrings, KLDRADDR BaseAddress, KU32 iSymbol, const char *pchSymbol, 221 KU32 cchSymbol, PKLDRADDR puValue, KU32 *pfKind); 222 static int kldrModMachODoQuerySymbol64Bit(PKLDRMODMACHO pModMachO, const macho_nlist_64_t *paSyms, KU32 cSyms, const char *pchStrings, 223 KU32 cchStrings, KLDRADDR BaseAddress, KU32 iSymbol, const char *pchSymbol, 224 KU32 cchSymbol, PKLDRADDR puValue, KU32 *pfKind); 225 static int kldrModMachODoEnumSymbols32Bit(PKLDRMODMACHO pModMachO, const macho_nlist_32_t *paSyms, KU32 cSyms, 226 const char *pchStrings, KU32 cchStrings, KLDRADDR BaseAddress, 227 KU32 fFlags, PFNKLDRMODENUMSYMS pfnCallback, void *pvUser); 228 static int kldrModMachODoEnumSymbols64Bit(PKLDRMODMACHO pModMachO, const macho_nlist_64_t *paSyms, KU32 cSyms, 229 const char *pchStrings, KU32 cchStrings, KLDRADDR BaseAddress, 230 KU32 fFlags, PFNKLDRMODENUMSYMS pfnCallback, void *pvUser); 231 static int kldrModMachOObjDoImports(PKLDRMODMACHO pModMachO, KLDRADDR BaseAddress, PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser); 232 static int kldrModMachOObjDoFixups(PKLDRMODMACHO pModMachO, void *pvMapping, KLDRADDR NewBaseAddress); 233 static int kldrModMachOFixupSectionGeneric32Bit(PKLDRMODMACHO pModMachO, KU8 *pbSectBits, PKLDRMODMACHOSECT pFixupSect, 234 macho_nlist_32_t *paSyms, KU32 cSyms, KLDRADDR NewBaseAddress); 235 static int kldrModMachOFixupSectionAMD64(PKLDRMODMACHO pModMachO, KU8 *pbSectBits, PKLDRMODMACHOSECT pFixupSect, 236 macho_nlist_64_t *paSyms, KU32 cSyms, KLDRADDR NewBaseAddress); 237 238 static int kldrModMachOMakeGOT(PKLDRMODMACHO pModMachO, void *pvBits, KLDRADDR NewBaseAddress); 239 240 /*static int kldrModMachODoFixups(PKLDRMODMACHO pModMachO, void *pvMapping, KLDRADDR NewBaseAddress, KLDRADDR OldBaseAddress); 241 static int kldrModMachODoImports(PKLDRMODMACHO pModMachO, void *pvMapping, PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser);*/ 242 243 244 /** 245 * Create a loader module instance interpreting the executable image found 246 * in the specified file provider instance. 247 * 248 * @returns 0 on success and *ppMod pointing to a module instance. 249 * On failure, a non-zero OS specific error code is returned. 250 * @param pOps Pointer to the registered method table. 251 * @param pRdr The file provider instance to use. 252 * @param fFlags Flags, MBZ. 253 * @param enmCpuArch The desired CPU architecture. KCPUARCH_UNKNOWN means 254 * anything goes, but with a preference for the current 255 * host architecture. 256 * @param offNewHdr The offset of the new header in MZ files. -1 if not found. 257 * @param ppMod Where to store the module instance pointer. 258 */ 259 static int kldrModMachOCreate(PCKLDRMODOPS pOps, PKRDR pRdr, KU32 fFlags, KCPUARCH enmCpuArch, KLDRFOFF offNewHdr, PPKLDRMOD ppMod) 260 { 261 PKLDRMODMACHO pModMachO; 262 int rc; 263 264 /* 265 * Create the instance data and do a minimal header validation. 266 */ 267 rc = kldrModMachODoCreate(pRdr, offNewHdr == -1 ? 0 : offNewHdr, fFlags, &pModMachO); 268 if (!rc) 269 { 270 271 /* 272 * Match up against the requested CPU architecture. 273 */ 274 if ( enmCpuArch == KCPUARCH_UNKNOWN 275 || pModMachO->pMod->enmArch == enmCpuArch) 276 { 277 pModMachO->pMod->pOps = pOps; 278 pModMachO->pMod->u32Magic = KLDRMOD_MAGIC; 279 *ppMod = pModMachO->pMod; 280 return 0; 281 } 282 rc = KLDR_ERR_CPU_ARCH_MISMATCH; 283 } 284 if (pModMachO) 285 { 286 kHlpFree(pModMachO->pbLoadCommands); 287 kHlpFree(pModMachO); 288 } 289 return rc; 290 } 226 static DECLCALLBACK(int) rtldrMachO_RelocateBits(PRTLDRMODINTERNAL pMod, void *pvBits, RTUINTPTR NewBaseAddress, 227 RTUINTPTR OldBaseAddress, PFNRTLDRIMPORT pfnGetImport, void *pvUser); 228 229 230 static int kldrModMachOPreParseLoadCommands(uint8_t *pbLoadCommands, const mach_header_32_t *pHdr, PRTLDRREADER pRdr, RTFOFF offImage, 231 uint32_t fOpenFlags, uint32_t *pcSegments, uint32_t *pcSections, uint32_t *pcbStringPool, 232 bool *pfCanLoad, PRTLDRADDR pLinkAddress, uint8_t *puEffFileType); 233 static int kldrModMachOParseLoadCommands(PKLDRMODMACHO pThis, char *pbStringPool, uint32_t cbStringPool); 234 235 static int kldrModMachOLoadObjSymTab(PKLDRMODMACHO pThis); 236 static int kldrModMachOLoadFixups(PKLDRMODMACHO pThis, RTFOFF offFixups, uint32_t cFixups, macho_relocation_info_t **ppaFixups); 237 static int kldrModMachOMapVirginBits(PKLDRMODMACHO pThis); 238 239 static int kldrModMachODoQuerySymbol32Bit(PKLDRMODMACHO pThis, const macho_nlist_32_t *paSyms, uint32_t cSyms, const char *pchStrings, 240 uint32_t cchStrings, RTLDRADDR BaseAddress, uint32_t iSymbol, const char *pchSymbol, 241 uint32_t cchSymbol, PRTLDRADDR puValue, uint32_t *pfKind); 242 static int kldrModMachODoQuerySymbol64Bit(PKLDRMODMACHO pThis, const macho_nlist_64_t *paSyms, uint32_t cSyms, const char *pchStrings, 243 uint32_t cchStrings, RTLDRADDR BaseAddress, uint32_t iSymbol, const char *pchSymbol, 244 uint32_t cchSymbol, PRTLDRADDR puValue, uint32_t *pfKind); 245 static int kldrModMachODoEnumSymbols32Bit(PKLDRMODMACHO pThis, const macho_nlist_32_t *paSyms, uint32_t cSyms, 246 const char *pchStrings, uint32_t cchStrings, RTLDRADDR BaseAddress, 247 uint32_t fFlags, PFNRTLDRENUMSYMS pfnCallback, void *pvUser); 248 static int kldrModMachODoEnumSymbols64Bit(PKLDRMODMACHO pThis, const macho_nlist_64_t *paSyms, uint32_t cSyms, 249 const char *pchStrings, uint32_t cchStrings, RTLDRADDR BaseAddress, 250 uint32_t fFlags, PFNRTLDRENUMSYMS pfnCallback, void *pvUser); 251 static int kldrModMachOObjDoImports(PKLDRMODMACHO pThis, RTLDRADDR BaseAddress, PFNRTLDRIMPORT pfnGetImport, void *pvUser); 252 static int kldrModMachOObjDoFixups(PKLDRMODMACHO pThis, void *pvMapping, RTLDRADDR NewBaseAddress); 253 static int kldrModMachOFixupSectionGeneric32Bit(PKLDRMODMACHO pThis, uint8_t *pbSectBits, PKLDRMODMACHOSECT pFixupSect, 254 macho_nlist_32_t *paSyms, uint32_t cSyms, RTLDRADDR NewBaseAddress); 255 static int kldrModMachOFixupSectionAMD64(PKLDRMODMACHO pThis, uint8_t *pbSectBits, PKLDRMODMACHOSECT pFixupSect, 256 macho_nlist_64_t *paSyms, uint32_t cSyms, RTLDRADDR NewBaseAddress); 257 258 static int kldrModMachOMakeGOT(PKLDRMODMACHO pThis, void *pvBits, RTLDRADDR NewBaseAddress); 259 260 /*static int kldrModMachODoFixups(PKLDRMODMACHO pThis, void *pvMapping, RTLDRADDR NewBaseAddress, RTLDRADDR OldBaseAddress); 261 static int kldrModMachODoImports(PKLDRMODMACHO pThis, void *pvMapping, PFNRTLDRIMPORT pfnGetImport, void *pvUser);*/ 262 291 263 292 264 … … 295 267 * simplify cleanup on failure. 296 268 */ 297 static int kldrModMachODoCreate(PKRDR pRdr, KLDRFOFF offImage, KU32 fOpenFlags, PKLDRMODMACHO *ppModMachO) 298 { 269 static int kldrModMachODoCreate(PRTLDRREADER pRdr, RTFOFF offImage, uint32_t fOpenFlags, 270 PKLDRMODMACHO *ppModMachO, PRTERRINFO pErrInfo) 271 { 272 *ppModMachO = NULL; 273 274 /* 275 * Read the Mach-O header. 276 */ 299 277 union 300 278 { … … 302 280 mach_header_64_t Hdr64; 303 281 } s; 304 PKLDRMODMACHO pModMachO; 305 PKLDRMOD pMod; 306 KU8 *pbLoadCommands; 307 KU32 cSegments = 0; /* (MSC maybe used uninitialized) */ 308 KU32 cSections = 0; /* (MSC maybe used uninitialized) */ 309 KU32 cbStringPool = 0; /* (MSC maybe used uninitialized) */ 310 KSIZE cchFilename; 311 KSIZE cb; 312 KBOOL fMakeGot; 313 KBOOL fCanLoad = K_TRUE; 314 KLDRADDR LinkAddress = NIL_KLDRADDR; /* (MSC maybe used uninitialized) */ 315 KU8 cbJmpStub; 316 KU8 uEffFileType = 0; /* (MSC maybe used uninitialized) */ 317 int rc; 318 *ppModMachO = NULL; 319 320 kHlpAssert(&s.Hdr32.magic == &s.Hdr64.magic); 321 kHlpAssert(&s.Hdr32.flags == &s.Hdr64.flags); 322 323 /* 324 * Read the Mach-O header. 325 */ 326 rc = kRdrRead(pRdr, &s, sizeof(s), offImage); 282 Assert(&s.Hdr32.magic == &s.Hdr64.magic); 283 Assert(&s.Hdr32.flags == &s.Hdr64.flags); 284 int rc = pRdr->pfnRead(pRdr, &s, sizeof(s), offImage); 327 285 if (rc) 328 return rc;286 return RTErrInfoSetF(pErrInfo, rc, "Error reading Mach-O header at %RTfoff: %Rrc", offImage, rc); 329 287 if ( s.Hdr32.magic != IMAGE_MACHO32_SIGNATURE 330 && s.Hdr32.magic != IMAGE_MACHO64_SIGNATURE 331 ) 288 && s.Hdr32.magic != IMAGE_MACHO64_SIGNATURE) 332 289 { 333 290 if ( s.Hdr32.magic == IMAGE_MACHO32_SIGNATURE_OE 334 291 || s.Hdr32.magic == IMAGE_MACHO64_SIGNATURE_OE) 335 return KLDR_ERR_MACHO_OTHER_ENDIAN_NOT_SUPPORTED;336 return KLDR_ERR_UNKNOWN_FORMAT;292 return VERR_LDRMACHO_OTHER_ENDIAN_NOT_SUPPORTED; 293 return VERR_INVALID_EXE_SIGNATURE; 337 294 } 338 295 339 296 /* sanity checks. */ 340 if ( s.Hdr32.sizeofcmds > kRdrSize(pRdr) - sizeof(mach_header_32_t)297 if ( s.Hdr32.sizeofcmds > pRdr->pfnSize(pRdr) - sizeof(mach_header_32_t) 341 298 || s.Hdr32.sizeofcmds < sizeof(load_command_t) * s.Hdr32.ncmds 342 299 || (s.Hdr32.flags & ~MH_VALID_FLAGS)) 343 return KLDR_ERR_MACHO_BAD_HEADER; 300 return VERR_LDRMACHO_BAD_HEADER; 301 302 bool fMakeGot; 303 uint8_t cbJmpStub; 344 304 switch (s.Hdr32.cputype) 345 305 { 346 306 case CPU_TYPE_X86: 347 fMakeGot = K_FALSE;307 fMakeGot = false; 348 308 cbJmpStub = 0; 349 309 break; … … 353 313 break; 354 314 default: 355 return KLDR_ERR_MACHO_UNSUPPORTED_MACHINE; 356 } 315 return VERR_LDRMACHO_UNSUPPORTED_MACHINE; 316 } 317 357 318 if ( s.Hdr32.filetype != MH_OBJECT 358 319 && s.Hdr32.filetype != MH_EXECUTE … … 361 322 && s.Hdr32.filetype != MH_DSYM 362 323 && s.Hdr32.filetype != MH_KEXT_BUNDLE) 363 return KLDR_ERR_MACHO_UNSUPPORTED_FILE_TYPE;324 return VERR_LDRMACHO_UNSUPPORTED_FILE_TYPE; 364 325 365 326 /* 366 327 * Read and pre-parse the load commands to figure out how many segments we'll be needing. 367 328 */ 368 pbLoadCommands = kHlpAlloc(s.Hdr32.sizeofcmds);329 uint8_t *pbLoadCommands = (uint8_t *)RTMemAlloc(s.Hdr32.sizeofcmds); 369 330 if (!pbLoadCommands) 370 return KERR_NO_MEMORY; 371 rc = kRdrRead(pRdr, pbLoadCommands, s.Hdr32.sizeofcmds, 372 s.Hdr32.magic == IMAGE_MACHO32_SIGNATURE 373 || s.Hdr32.magic == IMAGE_MACHO32_SIGNATURE_OE 374 ? sizeof(mach_header_32_t) + offImage 375 : sizeof(mach_header_64_t) + offImage); 331 return VERR_NO_MEMORY; 332 rc = pRdr->pfnRead(pRdr, pbLoadCommands, s.Hdr32.sizeofcmds, 333 s.Hdr32.magic == IMAGE_MACHO32_SIGNATURE 334 || s.Hdr32.magic == IMAGE_MACHO32_SIGNATURE_OE 335 ? sizeof(mach_header_32_t) + offImage 336 : sizeof(mach_header_64_t) + offImage); 337 338 uint32_t cSegments = 0; 339 uint32_t cSections = 0; 340 uint32_t cbStringPool = 0; 341 bool fCanLoad = true; 342 RTLDRADDR LinkAddress = NIL_RTLDRADDR; 343 uint8_t uEffFileType = 0; 376 344 if (!rc) 377 345 rc = kldrModMachOPreParseLoadCommands(pbLoadCommands, &s.Hdr32, pRdr, offImage, fOpenFlags, … … 379 347 if (rc) 380 348 { 381 kHlpFree(pbLoadCommands);349 RTMemFree(pbLoadCommands); 382 350 return rc; 383 351 } … … 388 356 * Calc the instance size, allocate and initialize it. 389 357 */ 390 cchFilename = kHlpStrLen(kRdrName(pRdr)); 391 cb = K_ALIGN_Z( K_OFFSETOF(KLDRMODMACHO, aSegments[cSegments]) 392 + sizeof(KLDRMODMACHOSECT) * cSections, 16) 393 + K_OFFSETOF(KLDRMOD, aSegments[cSegments]) 394 + cchFilename + 1 395 + cbStringPool; 396 pModMachO = (PKLDRMODMACHO)kHlpAlloc(cb); 397 if (!pModMachO) 398 return KERR_NO_MEMORY; 399 *ppModMachO = pModMachO; 400 pModMachO->pbLoadCommands = pbLoadCommands; 401 pModMachO->offImage = offImage; 402 403 /* KLDRMOD */ 404 pMod = (PKLDRMOD)((KU8 *)pModMachO + K_ALIGN_Z( K_OFFSETOF(KLDRMODMACHO, aSegments[cSegments]) 405 + sizeof(KLDRMODMACHOSECT) * cSections, 16)); 406 pMod->pvData = pModMachO; 407 pMod->pRdr = pRdr; 408 pMod->pOps = NULL; /* set upon success. */ 409 pMod->cSegments = cSegments; 410 pMod->cchFilename = (KU32)cchFilename; 411 pMod->pszFilename = (char *)&pMod->aSegments[pMod->cSegments]; 412 kHlpMemCopy((char *)pMod->pszFilename, kRdrName(pRdr), cchFilename + 1); 413 pMod->pszName = kHlpGetFilename(pMod->pszFilename); 414 pMod->cchName = (KU32)(cchFilename - (pMod->pszName - pMod->pszFilename)); 415 pMod->fFlags = 0; 358 size_t const cbModAndSegs = RT_ALIGN_Z(RT_UOFFSETOF_DYN(KLDRMODMACHO, aSegments[cSegments]) 359 + sizeof(KLDRMODMACHOSECT) * cSections, 16); 360 PKLDRMODMACHO pThis = (PKLDRMODMACHO)RTMemAlloc(cbModAndSegs + cbStringPool); 361 if (!pThis) 362 return VERR_NO_MEMORY; 363 *ppModMachO = pThis; 364 pThis->pbLoadCommands = pbLoadCommands; 365 pThis->offImage = offImage; 366 367 /* Core & CPU.*/ 368 pThis->Core.u32Magic = 0; /* set by caller */ 369 pThis->Core.eState = LDR_STATE_OPENED; 370 pThis->Core.pOps = NULL; /* set by caller. */ 371 pThis->Core.pReader = pRdr; 416 372 switch (s.Hdr32.cputype) 417 373 { 418 374 case CPU_TYPE_X86: 419 p Mod->enmArch = KCPUARCH_X86_32;420 p Mod->enmEndian = KLDRENDIAN_LITTLE;375 pThis->Core.enmArch = RTLDRARCH_X86_32; 376 pThis->Core.enmEndian = RTLDRENDIAN_LITTLE; 421 377 switch (s.Hdr32.cpusubtype) 422 378 { 423 379 case CPU_SUBTYPE_I386_ALL: /* == CPU_SUBTYPE_386 */ 424 p Mod->enmCpu = KCPU_X86_32_BLEND;380 pThis->enmCpu = RTLDRCPU_X86_32_BLEND; 425 381 break; 426 382 case CPU_SUBTYPE_486: 427 p Mod->enmCpu = KCPU_I486;383 pThis->enmCpu = RTLDRCPU_I486; 428 384 break; 429 385 case CPU_SUBTYPE_486SX: 430 p Mod->enmCpu = KCPU_I486SX;386 pThis->enmCpu = RTLDRCPU_I486SX; 431 387 break; 432 388 case CPU_SUBTYPE_PENT: /* == CPU_SUBTYPE_586 */ 433 p Mod->enmCpu = KCPU_I586;389 pThis->enmCpu = RTLDRCPU_I586; 434 390 break; 435 391 case CPU_SUBTYPE_PENTPRO: … … 441 397 case CPU_SUBTYPE_PENTIUM_3_M: 442 398 case CPU_SUBTYPE_PENTIUM_3_XEON: 443 p Mod->enmCpu = KCPU_I686;399 pThis->enmCpu = RTLDRCPU_I686; 444 400 break; 445 401 case CPU_SUBTYPE_PENTIUM_M: … … 448 404 case CPU_SUBTYPE_XEON: 449 405 case CPU_SUBTYPE_XEON_MP: 450 p Mod->enmCpu = KCPU_P4;406 pThis->enmCpu = RTLDRCPU_P4; 451 407 break; 452 408 … … 456 412 && s.Hdr32.filetype == MH_OBJECT) 457 413 break; 458 return KLDR_ERR_MACHO_UNSUPPORTED_MACHINE;414 return VERR_LDRMACHO_UNSUPPORTED_MACHINE; 459 415 } 460 416 break; 461 417 462 418 case CPU_TYPE_X86_64: 463 p Mod->enmArch = KCPUARCH_AMD64;464 p Mod->enmEndian = KLDRENDIAN_LITTLE;419 pThis->Core.enmArch = RTLDRARCH_AMD64; 420 pThis->Core.enmEndian = RTLDRENDIAN_LITTLE; 465 421 switch (s.Hdr32.cpusubtype & ~CPU_SUBTYPE_MASK) 466 422 { 467 case CPU_SUBTYPE_X86_64_ALL: pMod->enmCpu = KCPU_AMD64_BLEND; break;423 case CPU_SUBTYPE_X86_64_ALL: pThis->enmCpu = RTLDRCPU_AMD64_BLEND; break; 468 424 default: 469 return KLDR_ERR_MACHO_UNSUPPORTED_MACHINE;425 return VERR_LDRMACHO_UNSUPPORTED_MACHINE; 470 426 } 471 427 break; 472 428 473 429 default: 474 return KLDR_ERR_MACHO_UNSUPPORTED_MACHINE;475 } 476 477 p Mod->enmFmt = KLDRFMT_MACHO;430 return VERR_LDRMACHO_UNSUPPORTED_MACHINE; 431 } 432 433 pThis->Core.enmFormat = RTLDRFMT_MACHO; 478 434 switch (s.Hdr32.filetype) 479 435 { 480 case MH_OBJECT: p Mod->enmType = KLDRTYPE_OBJECT; break;481 case MH_EXECUTE: p Mod->enmType = KLDRTYPE_EXECUTABLE_FIXED; break;482 case MH_DYLIB: p Mod->enmType = KLDRTYPE_SHARED_LIBRARY_RELOCATABLE; break;483 case MH_BUNDLE: p Mod->enmType = KLDRTYPE_SHARED_LIBRARY_RELOCATABLE; break;484 case MH_KEXT_BUNDLE:p Mod->enmType = KLDRTYPE_SHARED_LIBRARY_RELOCATABLE; break;485 case MH_DSYM: p Mod->enmType = KLDRTYPE_DEBUG_INFO; break;436 case MH_OBJECT: pThis->Core.enmType = RTLDRTYPE_OBJECT; break; 437 case MH_EXECUTE: pThis->Core.enmType = RTLDRTYPE_EXECUTABLE_FIXED; break; 438 case MH_DYLIB: pThis->Core.enmType = RTLDRTYPE_SHARED_LIBRARY_RELOCATABLE; break; 439 case MH_BUNDLE: pThis->Core.enmType = RTLDRTYPE_SHARED_LIBRARY_RELOCATABLE; break; 440 case MH_KEXT_BUNDLE:pThis->Core.enmType = RTLDRTYPE_SHARED_LIBRARY_RELOCATABLE; break; 441 case MH_DSYM: pThis->Core.enmType = RTLDRTYPE_DEBUG_INFO; break; 486 442 default: 487 return KLDR_ERR_MACHO_UNSUPPORTED_FILE_TYPE; 488 } 489 pMod->u32Magic = 0; /* set upon success. */ 443 return VERR_LDRMACHO_UNSUPPORTED_FILE_TYPE; 444 } 490 445 491 446 /* KLDRMODMACHO */ 492 p ModMachO->pMod = pMod;493 p ModMachO->pvBits = NULL;494 p ModMachO->pvMapping = NULL;495 p ModMachO->fOpenFlags = fOpenFlags;496 p ModMachO->Hdr = s.Hdr64;447 pThis->cSegments = cSegments; 448 pThis->pvBits = NULL; 449 pThis->pvMapping = NULL; 450 pThis->fOpenFlags = fOpenFlags; 451 pThis->Hdr = s.Hdr64; 497 452 if ( s.Hdr32.magic == IMAGE_MACHO32_SIGNATURE 498 453 || s.Hdr32.magic == IMAGE_MACHO32_SIGNATURE_OE) 499 p ModMachO->Hdr.reserved = 0;500 p ModMachO->LinkAddress = LinkAddress;501 p ModMachO->cbImage = 0;502 p ModMachO->fCanLoad = fCanLoad;503 p ModMachO->fMakeGot = fMakeGot;504 p ModMachO->cbJmpStub = cbJmpStub;505 p ModMachO->uEffFileType = uEffFileType;506 p ModMachO->offSymbols = 0;507 p ModMachO->cSymbols = 0;508 p ModMachO->pvaSymbols = NULL;509 p ModMachO->offStrings = 0;510 p ModMachO->cchStrings = 0;511 p ModMachO->pchStrings = NULL;512 kHlpMemSet(pModMachO->abImageUuid, 0, sizeof(pModMachO->abImageUuid));513 p ModMachO->GotRVA = NIL_KLDRADDR;514 p ModMachO->JmpStubsRVA = NIL_KLDRADDR;515 p ModMachO->cSections = cSections;516 p ModMachO->paSections = (PKLDRMODMACHOSECT)&pModMachO->aSegments[pModMachO->pMod->cSegments];454 pThis->Hdr.reserved = 0; 455 pThis->LinkAddress = LinkAddress; 456 pThis->cbImage = 0; 457 pThis->fCanLoad = fCanLoad; 458 pThis->fMakeGot = fMakeGot; 459 pThis->cbJmpStub = cbJmpStub; 460 pThis->uEffFileType = uEffFileType; 461 pThis->offSymbols = 0; 462 pThis->cSymbols = 0; 463 pThis->pvaSymbols = NULL; 464 pThis->offStrings = 0; 465 pThis->cchStrings = 0; 466 pThis->pchStrings = NULL; 467 memset(pThis->abImageUuid, 0, sizeof(pThis->abImageUuid)); 468 pThis->GotRVA = NIL_RTLDRADDR; 469 pThis->JmpStubsRVA = NIL_RTLDRADDR; 470 pThis->cSections = cSections; 471 pThis->paSections = (PKLDRMODMACHOSECT)&pThis->aSegments[pThis->cSegments]; 517 472 518 473 /* 519 474 * Setup the KLDRMOD segment array. 520 475 */ 521 rc = kldrModMachOParseLoadCommands(p ModMachO, (char *)pMod->pszFilename + pMod->cchFilename + 1, cbStringPool);476 rc = kldrModMachOParseLoadCommands(pThis, (char *)pThis + cbModAndSegs, cbStringPool); 522 477 if (rc) 523 478 return rc; … … 551 506 * 552 507 * @returns 0 on success. 553 * @returns KLDR_ERR_MACHO_* on failure.508 * @returns VERR_LDRMACHO_* on failure. 554 509 * @param pbLoadCommands The load commands to parse. 555 510 * @param pHdr The header. … … 563 518 * lowest segment address). 564 519 */ 565 static int kldrModMachOPreParseLoadCommands( KU8 *pbLoadCommands, const mach_header_32_t *pHdr, PKRDR pRdr, KLDRFOFFoffImage,566 KU32 fOpenFlags, KU32 *pcSegments, KU32 *pcSections, KU32*pcbStringPool,567 PKBOOL pfCanLoad, PKLDRADDR pLinkAddress, KU8*puEffFileType)520 static int kldrModMachOPreParseLoadCommands(uint8_t *pbLoadCommands, const mach_header_32_t *pHdr, PRTLDRREADER pRdr, RTFOFF offImage, 521 uint32_t fOpenFlags, uint32_t *pcSegments, uint32_t *pcSections, uint32_t *pcbStringPool, 522 bool *pfCanLoad, PRTLDRADDR pLinkAddress, uint8_t *puEffFileType) 568 523 { 569 524 union 570 525 { 571 KU8*pb;526 uint8_t *pb; 572 527 load_command_t *pLoadCmd; 573 528 segment_command_32_t *pSeg32; … … 577 532 uuid_command_t *pUuid; 578 533 } u; 579 const KU64 cbFile = kRdrSize(pRdr) - offImage;580 KU32cSegments = 0;581 KU32cSections = 0;582 KSIZEcbStringPool = 0;583 KU32cLeft = pHdr->ncmds;584 KU32cbLeft = pHdr->sizeofcmds;585 KU8*pb = pbLoadCommands;534 const uint64_t cbFile = pRdr->pfnSize(pRdr) - offImage; 535 uint32_t cSegments = 0; 536 uint32_t cSections = 0; 537 size_t cbStringPool = 0; 538 uint32_t cLeft = pHdr->ncmds; 539 uint32_t cbLeft = pHdr->sizeofcmds; 540 uint8_t *pb = pbLoadCommands; 586 541 int cSegmentCommands = 0; 587 542 int cSymbolTabs = 0; 588 543 int fConvertEndian = pHdr->magic == IMAGE_MACHO32_SIGNATURE_OE 589 544 || pHdr->magic == IMAGE_MACHO64_SIGNATURE_OE; 590 KU8uEffFileType = *puEffFileType = pHdr->filetype;545 uint8_t uEffFileType = *puEffFileType = pHdr->filetype; 591 546 592 547 *pcSegments = 0; 593 548 *pcSections = 0; 594 549 *pcbStringPool = 0; 595 *pfCanLoad = K_TRUE;596 *pLinkAddress = ~( KLDRADDR)0;550 *pfCanLoad = true; 551 *pLinkAddress = ~(RTLDRADDR)0; 597 552 598 553 while (cLeft-- > 0) … … 603 558 * Convert and validate command header. 604 559 */ 605 KLDRMODMACHO_CHECK_RETURN(cbLeft >= sizeof(load_command_t), KLDR_ERR_MACHO_BAD_LOAD_COMMAND);560 KLDRMODMACHO_CHECK_RETURN(cbLeft >= sizeof(load_command_t), VERR_LDRMACHO_BAD_LOAD_COMMAND); 606 561 if (fConvertEndian) 607 562 { 608 u.pLoadCmd->cmd = K_E2E_U32(u.pLoadCmd->cmd);609 u.pLoadCmd->cmdsize = K_E2E_U32(u.pLoadCmd->cmdsize);610 } 611 KLDRMODMACHO_CHECK_RETURN(u.pLoadCmd->cmdsize <= cbLeft, KLDR_ERR_MACHO_BAD_LOAD_COMMAND);563 u.pLoadCmd->cmd = RT_BSWAP_U32(u.pLoadCmd->cmd); 564 u.pLoadCmd->cmdsize = RT_BSWAP_U32(u.pLoadCmd->cmdsize); 565 } 566 KLDRMODMACHO_CHECK_RETURN(u.pLoadCmd->cmdsize <= cbLeft, VERR_LDRMACHO_BAD_LOAD_COMMAND); 612 567 cbLeft -= u.pLoadCmd->cmdsize; 613 568 pb += u.pLoadCmd->cmdsize; … … 623 578 section_32_t *pFirstSect = (section_32_t *)(pSrcSeg + 1); 624 579 section_32_t *pSect = pFirstSect; 625 KU32cSectionsLeft = pSrcSeg->nsects;626 KU64offSect = 0;580 uint32_t cSectionsLeft = pSrcSeg->nsects; 581 uint64_t offSect = 0; 627 582 628 583 /* Convert and verify the segment. */ 629 KLDRMODMACHO_CHECK_RETURN(u.pLoadCmd->cmdsize >= sizeof(segment_command_32_t), KLDR_ERR_MACHO_BAD_LOAD_COMMAND);584 KLDRMODMACHO_CHECK_RETURN(u.pLoadCmd->cmdsize >= sizeof(segment_command_32_t), VERR_LDRMACHO_BAD_LOAD_COMMAND); 630 585 KLDRMODMACHO_CHECK_RETURN( pHdr->magic == IMAGE_MACHO32_SIGNATURE_OE 631 || pHdr->magic == IMAGE_MACHO32_SIGNATURE, KLDR_ERR_MACHO_BIT_MIX);586 || pHdr->magic == IMAGE_MACHO32_SIGNATURE, VERR_LDRMACHO_BIT_MIX); 632 587 if (fConvertEndian) 633 588 { 634 pSrcSeg->vmaddr = K_E2E_U32(pSrcSeg->vmaddr);635 pSrcSeg->vmsize = K_E2E_U32(pSrcSeg->vmsize);636 pSrcSeg->fileoff = K_E2E_U32(pSrcSeg->fileoff);637 pSrcSeg->filesize = K_E2E_U32(pSrcSeg->filesize);638 pSrcSeg->maxprot = K_E2E_U32(pSrcSeg->maxprot);639 pSrcSeg->initprot = K_E2E_U32(pSrcSeg->initprot);640 pSrcSeg->nsects = K_E2E_U32(pSrcSeg->nsects);641 pSrcSeg->flags = K_E2E_U32(pSrcSeg->flags);589 pSrcSeg->vmaddr = RT_BSWAP_U32(pSrcSeg->vmaddr); 590 pSrcSeg->vmsize = RT_BSWAP_U32(pSrcSeg->vmsize); 591 pSrcSeg->fileoff = RT_BSWAP_U32(pSrcSeg->fileoff); 592 pSrcSeg->filesize = RT_BSWAP_U32(pSrcSeg->filesize); 593 pSrcSeg->maxprot = RT_BSWAP_U32(pSrcSeg->maxprot); 594 pSrcSeg->initprot = RT_BSWAP_U32(pSrcSeg->initprot); 595 pSrcSeg->nsects = RT_BSWAP_U32(pSrcSeg->nsects); 596 pSrcSeg->flags = RT_BSWAP_U32(pSrcSeg->flags); 642 597 } 643 598 … … 645 600 #define VALIDATE_AND_ADD_SEGMENT(a_cBits) \ 646 601 do { \ 647 KBOOL fSkipSeg = !kHlpStrComp(pSrcSeg->segname, "__DWARF") /* Note: Not for non-object files. */ \648 || ( ! kHlpStrComp(pSrcSeg->segname, "__CTF") /* Their CTF tool did/does weird things, */ \602 bool fSkipSeg = !strcmp(pSrcSeg->segname, "__DWARF") /* Note: Not for non-object files. */ \ 603 || ( !strcmp(pSrcSeg->segname, "__CTF") /* Their CTF tool did/does weird things, */ \ 649 604 && pSrcSeg->vmsize == 0) /* overlapping vmaddr and zero vmsize. */ \ 650 605 || (cSectionsLeft > 0 && (pFirstSect->flags & S_ATTR_DEBUG)); \ … … 658 613 KLDRMODMACHO_CHECK_RETURN( pSrcSeg->filesize == 0 \ 659 614 || ( pSrcSeg->fileoff <= cbFile \ 660 && ( KU64)pSrcSeg->fileoff + pSrcSeg->filesize <= cbFile), \661 KLDR_ERR_MACHO_BAD_LOAD_COMMAND); \615 && (uint64_t)pSrcSeg->fileoff + pSrcSeg->filesize <= cbFile), \ 616 VERR_LDRMACHO_BAD_LOAD_COMMAND); \ 662 617 KLDRMODMACHO_CHECK_RETURN( pSrcSeg->filesize <= pSrcSeg->vmsize \ 663 || (fSkipSeg && ! kHlpStrComp(pSrcSeg->segname, "__CTF") /* see above */), \664 KLDR_ERR_MACHO_BAD_LOAD_COMMAND); \618 || (fSkipSeg && !strcmp(pSrcSeg->segname, "__CTF") /* see above */), \ 619 VERR_LDRMACHO_BAD_LOAD_COMMAND); \ 665 620 KLDRMODMACHO_CHECK_RETURN(!(~pSrcSeg->maxprot & pSrcSeg->initprot), \ 666 KLDR_ERR_MACHO_BAD_LOAD_COMMAND); \621 VERR_LDRMACHO_BAD_LOAD_COMMAND); \ 667 622 KLDRMODMACHO_CHECK_RETURN(!(pSrcSeg->flags & ~(SG_HIGHVM | SG_FVMLIB | SG_NORELOC | SG_PROTECTED_VERSION_1)), \ 668 KLDR_ERR_MACHO_BAD_LOAD_COMMAND); \623 VERR_LDRMACHO_BAD_LOAD_COMMAND); \ 669 624 KLDRMODMACHO_CHECK_RETURN( pSrcSeg->nsects * sizeof(section_##a_cBits##_t) \ 670 625 <= u.pLoadCmd->cmdsize - sizeof(segment_command_##a_cBits##_t), \ 671 KLDR_ERR_MACHO_BAD_LOAD_COMMAND); \626 VERR_LDRMACHO_BAD_LOAD_COMMAND); \ 672 627 KLDRMODMACHO_CHECK_RETURN( uEffFileType != MH_OBJECT \ 673 628 || cSegmentCommands == 0 \ … … 676 631 && pHdr->filetype == MH_DSYM \ 677 632 && fSkipSeg), \ 678 KLDR_ERR_MACHO_BAD_OBJECT_FILE); \633 VERR_LDRMACHO_BAD_OBJECT_FILE); \ 679 634 cSegmentCommands++; \ 680 635 \ … … 682 637 if (!fSkipSeg && uEffFileType != MH_OBJECT) \ 683 638 { \ 684 cbStringPool += kHlpStrNLen(&pSrcSeg->segname[0], sizeof(pSrcSeg->segname)) + 1; \639 cbStringPool += RTStrNLen(&pSrcSeg->segname[0], sizeof(pSrcSeg->segname)) + 1; \ 685 640 cSegments++; \ 686 641 if (cSegments == 1) /* The link address is set by the first segment. */ \ … … 700 655 if (fConvertEndian) 701 656 { 702 pSect->addr = K_E2E_U32(pSect->addr);703 pSect->size = K_E2E_U32(pSect->size);704 pSect->offset = K_E2E_U32(pSect->offset);705 pSect->align = K_E2E_U32(pSect->align);706 pSect->reloff = K_E2E_U32(pSect->reloff);707 pSect->nreloc = K_E2E_U32(pSect->nreloc);708 pSect->flags = K_E2E_U32(pSect->flags);709 pSect->reserved1 = K_E2E_U32(pSect->reserved1);710 pSect->reserved2 = K_E2E_U32(pSect->reserved2);657 pSect->addr = RT_BSWAP_U32(pSect->addr); 658 pSect->size = RT_BSWAP_U32(pSect->size); 659 pSect->offset = RT_BSWAP_U32(pSect->offset); 660 pSect->align = RT_BSWAP_U32(pSect->align); 661 pSect->reloff = RT_BSWAP_U32(pSect->reloff); 662 pSect->nreloc = RT_BSWAP_U32(pSect->nreloc); 663 pSect->flags = RT_BSWAP_U32(pSect->flags); 664 pSect->reserved1 = RT_BSWAP_U32(pSect->reserved1); 665 pSect->reserved2 = RT_BSWAP_U32(pSect->reserved2); 711 666 } 712 667 … … 718 673 /* validate */ \ 719 674 if (uEffFileType != MH_OBJECT) \ 720 KLDRMODMACHO_CHECK_RETURN(! kHlpStrComp(pSect->segname, pSrcSeg->segname),\721 KLDR_ERR_MACHO_BAD_SECTION); \675 KLDRMODMACHO_CHECK_RETURN(!strcmp(pSect->segname, pSrcSeg->segname),\ 676 VERR_LDRMACHO_BAD_SECTION); \ 722 677 \ 723 678 switch (pSect->flags & SECTION_TYPE) \ 724 679 { \ 725 680 case S_ZEROFILL: \ 726 KLDRMODMACHO_CHECK_RETURN(!pSect->reserved1, KLDR_ERR_MACHO_BAD_SECTION); \727 KLDRMODMACHO_CHECK_RETURN(!pSect->reserved2, KLDR_ERR_MACHO_BAD_SECTION); \681 KLDRMODMACHO_CHECK_RETURN(!pSect->reserved1, VERR_LDRMACHO_BAD_SECTION); \ 682 KLDRMODMACHO_CHECK_RETURN(!pSect->reserved2, VERR_LDRMACHO_BAD_SECTION); \ 728 683 fFileBits = 0; \ 729 684 break; \ … … 734 689 case S_8BYTE_LITERALS: \ 735 690 case S_16BYTE_LITERALS: \ 736 KLDRMODMACHO_CHECK_RETURN(!pSect->reserved1, KLDR_ERR_MACHO_BAD_SECTION); \737 KLDRMODMACHO_CHECK_RETURN(!pSect->reserved2, KLDR_ERR_MACHO_BAD_SECTION); \691 KLDRMODMACHO_CHECK_RETURN(!pSect->reserved1, VERR_LDRMACHO_BAD_SECTION); \ 692 KLDRMODMACHO_CHECK_RETURN(!pSect->reserved2, VERR_LDRMACHO_BAD_SECTION); \ 738 693 fFileBits = 1; \ 739 694 break; \ 740 695 \ 741 696 case S_SYMBOL_STUBS: \ 742 KLDRMODMACHO_CHECK_RETURN(!pSect->reserved1, KLDR_ERR_MACHO_BAD_SECTION); \697 KLDRMODMACHO_CHECK_RETURN(!pSect->reserved1, VERR_LDRMACHO_BAD_SECTION); \ 743 698 /* reserved2 == stub size. 0 has been seen (corecrypto.kext) */ \ 744 KLDRMODMACHO_CHECK_RETURN(pSect->reserved2 < 64, KLDR_ERR_MACHO_BAD_SECTION); \699 KLDRMODMACHO_CHECK_RETURN(pSect->reserved2 < 64, VERR_LDRMACHO_BAD_SECTION); \ 745 700 fFileBits = 1; \ 746 701 break; \ … … 750 705 case S_LAZY_DYLIB_SYMBOL_POINTERS: \ 751 706 /* (reserved 1 = is indirect symbol table index) */ \ 752 KLDRMODMACHO_CHECK_RETURN(!pSect->reserved2, KLDR_ERR_MACHO_BAD_SECTION); \753 *pfCanLoad = K_FALSE; \707 KLDRMODMACHO_CHECK_RETURN(!pSect->reserved2, VERR_LDRMACHO_BAD_SECTION); \ 708 *pfCanLoad = false; \ 754 709 fFileBits = -1; /* __DATA.__got in the 64-bit mach_kernel has bits, any things without bits? */ \ 755 710 break; \ … … 757 712 case S_MOD_INIT_FUNC_POINTERS: \ 758 713 /** @todo this requires a query API or flag... (e.g. C++ constructors) */ \ 759 KLDRMODMACHO_CHECK_RETURN(fOpenFlags & KLDRMOD_OPEN_FLAGS_FOR_INFO, \760 KLDR_ERR_MACHO_UNSUPPORTED_INIT_SECTION); \714 KLDRMODMACHO_CHECK_RETURN(fOpenFlags & RTLDR_O_FOR_DEBUG, \ 715 VERR_LDRMACHO_UNSUPPORTED_INIT_SECTION); \ 761 716 /* Falls through. */ \ 762 717 case S_MOD_TERM_FUNC_POINTERS: \ 763 718 /** @todo this requires a query API or flag... (e.g. C++ destructors) */ \ 764 KLDRMODMACHO_CHECK_RETURN(fOpenFlags & KLDRMOD_OPEN_FLAGS_FOR_INFO, \765 KLDR_ERR_MACHO_UNSUPPORTED_TERM_SECTION); \766 KLDRMODMACHO_CHECK_RETURN(!pSect->reserved1, KLDR_ERR_MACHO_BAD_SECTION); \767 KLDRMODMACHO_CHECK_RETURN(!pSect->reserved2, KLDR_ERR_MACHO_BAD_SECTION); \719 KLDRMODMACHO_CHECK_RETURN(fOpenFlags & RTLDR_O_FOR_DEBUG, \ 720 VERR_LDRMACHO_UNSUPPORTED_TERM_SECTION); \ 721 KLDRMODMACHO_CHECK_RETURN(!pSect->reserved1, VERR_LDRMACHO_BAD_SECTION); \ 722 KLDRMODMACHO_CHECK_RETURN(!pSect->reserved2, VERR_LDRMACHO_BAD_SECTION); \ 768 723 fFileBits = 1; \ 769 724 break; /* ignored */ \ … … 771 726 case S_LITERAL_POINTERS: \ 772 727 case S_DTRACE_DOF: \ 773 KLDRMODMACHO_CHECK_RETURN(!pSect->reserved1, KLDR_ERR_MACHO_BAD_SECTION); \774 KLDRMODMACHO_CHECK_RETURN(!pSect->reserved2, KLDR_ERR_MACHO_BAD_SECTION); \728 KLDRMODMACHO_CHECK_RETURN(!pSect->reserved1, VERR_LDRMACHO_BAD_SECTION); \ 729 KLDRMODMACHO_CHECK_RETURN(!pSect->reserved2, VERR_LDRMACHO_BAD_SECTION); \ 775 730 fFileBits = 1; \ 776 731 break; \ … … 778 733 case S_INTERPOSING: \ 779 734 case S_GB_ZEROFILL: \ 780 KLDRMODMACHO_FAILED_RETURN( KLDR_ERR_MACHO_UNSUPPORTED_SECTION); \735 KLDRMODMACHO_FAILED_RETURN(VERR_LDRMACHO_UNSUPPORTED_SECTION); \ 781 736 \ 782 737 default: \ 783 KLDRMODMACHO_FAILED_RETURN( KLDR_ERR_MACHO_UNKNOWN_SECTION); \738 KLDRMODMACHO_FAILED_RETURN(VERR_LDRMACHO_UNKNOWN_SECTION); \ 784 739 } \ 785 740 KLDRMODMACHO_CHECK_RETURN(!(pSect->flags & ~( S_ATTR_PURE_INSTRUCTIONS | S_ATTR_NO_TOC | S_ATTR_STRIP_STATIC_SYMS \ … … 787 742 | S_ATTR_DEBUG | S_ATTR_SOME_INSTRUCTIONS | S_ATTR_EXT_RELOC \ 788 743 | S_ATTR_LOC_RELOC | SECTION_TYPE)), \ 789 KLDR_ERR_MACHO_BAD_SECTION); \744 VERR_LDRMACHO_BAD_SECTION); \ 790 745 KLDRMODMACHO_CHECK_RETURN((pSect->flags & S_ATTR_DEBUG) == (pFirstSect->flags & S_ATTR_DEBUG), \ 791 KLDR_ERR_MACHO_MIXED_DEBUG_SECTION_FLAGS); \746 VERR_LDRMACHO_MIXED_DEBUG_SECTION_FLAGS); \ 792 747 \ 793 748 KLDRMODMACHO_CHECK_RETURN(pSect->addr - pSrcSeg->vmaddr <= pSrcSeg->vmsize, \ 794 KLDR_ERR_MACHO_BAD_SECTION); \749 VERR_LDRMACHO_BAD_SECTION); \ 795 750 KLDRMODMACHO_CHECK_RETURN( pSect->addr - pSrcSeg->vmaddr + pSect->size <= pSrcSeg->vmsize \ 796 || ! kHlpStrComp(pSrcSeg->segname, "__CTF") /* see above */, \797 KLDR_ERR_MACHO_BAD_SECTION); \751 || !strcmp(pSrcSeg->segname, "__CTF") /* see above */, \ 752 VERR_LDRMACHO_BAD_SECTION); \ 798 753 KLDRMODMACHO_CHECK_RETURN(pSect->align < 31, \ 799 KLDR_ERR_MACHO_BAD_SECTION); \754 VERR_LDRMACHO_BAD_SECTION); \ 800 755 /* Workaround for buggy ld64 (or as, llvm, ++) that produces a misaligned __TEXT.__unwind_info. */ \ 801 756 /* Seen: pSect->align = 4, pSect->addr = 0x5ebe14. Just adjust the alignment down. */ \ 802 if ( (( K_BIT32(pSect->align) - KU32_C(1)) & pSect->addr) \757 if ( ((RT_BIT_32(pSect->align) - UINT32_C(1)) & pSect->addr) \ 803 758 && pSect->align == 4 \ 804 && kHlpStrComp(pSect->sectname, "__unwind_info") == 0) \759 && strcmp(pSect->sectname, "__unwind_info") == 0) \ 805 760 pSect->align = 2; \ 806 KLDRMODMACHO_CHECK_RETURN(!(( K_BIT32(pSect->align) - KU32_C(1)) & pSect->addr), \807 KLDR_ERR_MACHO_BAD_SECTION); \808 KLDRMODMACHO_CHECK_RETURN(!(( K_BIT32(pSect->align) - KU32_C(1)) & pSrcSeg->vmaddr), \809 KLDR_ERR_MACHO_BAD_SECTION); \761 KLDRMODMACHO_CHECK_RETURN(!((RT_BIT_32(pSect->align) - UINT32_C(1)) & pSect->addr), \ 762 VERR_LDRMACHO_BAD_SECTION); \ 763 KLDRMODMACHO_CHECK_RETURN(!((RT_BIT_32(pSect->align) - UINT32_C(1)) & pSrcSeg->vmaddr), \ 764 VERR_LDRMACHO_BAD_SECTION); \ 810 765 \ 811 766 /* Adjust the section offset before we check file offset. */ \ 812 offSect = (offSect + K_BIT64(pSect->align) - KU64_C(1)) & ~(K_BIT64(pSect->align) - KU64_C(1)); \767 offSect = (offSect + RT_BIT_64(pSect->align) - UINT64_C(1)) & ~(RT_BIT_64(pSect->align) - UINT64_C(1)); \ 813 768 if (pSect->addr) \ 814 769 { \ 815 KLDRMODMACHO_CHECK_RETURN(offSect <= pSect->addr - pSrcSeg->vmaddr, KLDR_ERR_MACHO_BAD_SECTION); \770 KLDRMODMACHO_CHECK_RETURN(offSect <= pSect->addr - pSrcSeg->vmaddr, VERR_LDRMACHO_BAD_SECTION); \ 816 771 if (offSect < pSect->addr - pSrcSeg->vmaddr) \ 817 772 offSect = pSect->addr - pSrcSeg->vmaddr; \ … … 825 780 { \ 826 781 KLDRMODMACHO_CHECK_RETURN(pSect->offset == pSrcSeg->fileoff + offSect, \ 827 KLDR_ERR_MACHO_NON_CONT_SEG_BITS); \782 VERR_LDRMACHO_NON_CONT_SEG_BITS); \ 828 783 KLDRMODMACHO_CHECK_RETURN(pSect->offset - pSrcSeg->fileoff <= pSrcSeg->filesize, \ 829 KLDR_ERR_MACHO_BAD_SECTION); \784 VERR_LDRMACHO_BAD_SECTION); \ 830 785 } \ 831 786 KLDRMODMACHO_CHECK_RETURN(pSect->offset <= cbFile, \ 832 KLDR_ERR_MACHO_BAD_SECTION); \833 KLDRMODMACHO_CHECK_RETURN(( KU64)pSect->offset + pSect->size <= cbFile, \834 KLDR_ERR_MACHO_BAD_SECTION); \787 VERR_LDRMACHO_BAD_SECTION); \ 788 KLDRMODMACHO_CHECK_RETURN((uint64_t)pSect->offset + pSect->size <= cbFile, \ 789 VERR_LDRMACHO_BAD_SECTION); \ 835 790 } \ 836 791 else \ 837 KLDRMODMACHO_CHECK_RETURN(pSect->offset == 0, KLDR_ERR_MACHO_BAD_SECTION); \792 KLDRMODMACHO_CHECK_RETURN(pSect->offset == 0, VERR_LDRMACHO_BAD_SECTION); \ 838 793 \ 839 794 if (!pSect->nreloc) \ 840 795 KLDRMODMACHO_CHECK_RETURN(!pSect->reloff, \ 841 KLDR_ERR_MACHO_BAD_SECTION); \796 VERR_LDRMACHO_BAD_SECTION); \ 842 797 else \ 843 798 { \ 844 799 KLDRMODMACHO_CHECK_RETURN(pSect->reloff <= cbFile, \ 845 KLDR_ERR_MACHO_BAD_SECTION); \846 KLDRMODMACHO_CHECK_RETURN( ( KU64)pSect->reloff \847 + ( KLDRFOFF)pSect->nreloc * sizeof(macho_relocation_info_t) \800 VERR_LDRMACHO_BAD_SECTION); \ 801 KLDRMODMACHO_CHECK_RETURN( (uint64_t)pSect->reloff \ 802 + (RTFOFF)pSect->nreloc * sizeof(macho_relocation_info_t) \ 848 803 <= cbFile, \ 849 KLDR_ERR_MACHO_BAD_SECTION); \804 VERR_LDRMACHO_BAD_SECTION); \ 850 805 } \ 851 806 \ … … 855 810 case MH_OBJECT: \ 856 811 if ( !(pSect->flags & S_ATTR_DEBUG) \ 857 && kHlpStrComp(pSect->segname, "__DWARF")) \812 && strcmp(pSect->segname, "__DWARF")) \ 858 813 { \ 859 cbStringPool += kHlpStrNLen(&pSect->segname[0], sizeof(pSect->segname)) + 1; \860 cbStringPool += kHlpStrNLen(&pSect->sectname[0], sizeof(pSect->sectname)) + 1; \814 cbStringPool += RTStrNLen(&pSect->segname[0], sizeof(pSect->segname)) + 1; \ 815 cbStringPool += RTStrNLen(&pSect->sectname[0], sizeof(pSect->sectname)) + 1; \ 861 816 cSegments++; \ 862 817 if (cSegments == 1) /* The link address is set by the first segment. */ \ … … 872 827 break; \ 873 828 default: \ 874 KLDRMODMACHO_FAILED_RETURN( KERR_INVALID_PARAMETER); \829 KLDRMODMACHO_FAILED_RETURN(VERR_INVALID_PARAMETER); \ 875 830 } \ 876 831 \ … … 892 847 section_64_t *pFirstSect = (section_64_t *)(pSrcSeg + 1); 893 848 section_64_t *pSect = pFirstSect; 894 KU32cSectionsLeft = pSrcSeg->nsects;895 KU64offSect = 0;849 uint32_t cSectionsLeft = pSrcSeg->nsects; 850 uint64_t offSect = 0; 896 851 897 852 /* Convert and verify the segment. */ 898 KLDRMODMACHO_CHECK_RETURN(u.pLoadCmd->cmdsize >= sizeof(segment_command_64_t), KLDR_ERR_MACHO_BAD_LOAD_COMMAND);853 KLDRMODMACHO_CHECK_RETURN(u.pLoadCmd->cmdsize >= sizeof(segment_command_64_t), VERR_LDRMACHO_BAD_LOAD_COMMAND); 899 854 KLDRMODMACHO_CHECK_RETURN( pHdr->magic == IMAGE_MACHO64_SIGNATURE_OE 900 || pHdr->magic == IMAGE_MACHO64_SIGNATURE, KLDR_ERR_MACHO_BIT_MIX);855 || pHdr->magic == IMAGE_MACHO64_SIGNATURE, VERR_LDRMACHO_BIT_MIX); 901 856 if (fConvertEndian) 902 857 { 903 pSrcSeg->vmaddr = K_E2E_U64(pSrcSeg->vmaddr);904 pSrcSeg->vmsize = K_E2E_U64(pSrcSeg->vmsize);905 pSrcSeg->fileoff = K_E2E_U64(pSrcSeg->fileoff);906 pSrcSeg->filesize = K_E2E_U64(pSrcSeg->filesize);907 pSrcSeg->maxprot = K_E2E_U32(pSrcSeg->maxprot);908 pSrcSeg->initprot = K_E2E_U32(pSrcSeg->initprot);909 pSrcSeg->nsects = K_E2E_U32(pSrcSeg->nsects);910 pSrcSeg->flags = K_E2E_U32(pSrcSeg->flags);858 pSrcSeg->vmaddr = RT_BSWAP_U64(pSrcSeg->vmaddr); 859 pSrcSeg->vmsize = RT_BSWAP_U64(pSrcSeg->vmsize); 860 pSrcSeg->fileoff = RT_BSWAP_U64(pSrcSeg->fileoff); 861 pSrcSeg->filesize = RT_BSWAP_U64(pSrcSeg->filesize); 862 pSrcSeg->maxprot = RT_BSWAP_U32(pSrcSeg->maxprot); 863 pSrcSeg->initprot = RT_BSWAP_U32(pSrcSeg->initprot); 864 pSrcSeg->nsects = RT_BSWAP_U32(pSrcSeg->nsects); 865 pSrcSeg->flags = RT_BSWAP_U32(pSrcSeg->flags); 911 866 } 912 867 … … 920 875 if (fConvertEndian) 921 876 { 922 pSect->addr = K_E2E_U64(pSect->addr);923 pSect->size = K_E2E_U64(pSect->size);924 pSect->offset = K_E2E_U32(pSect->offset);925 pSect->align = K_E2E_U32(pSect->align);926 pSect->reloff = K_E2E_U32(pSect->reloff);927 pSect->nreloc = K_E2E_U32(pSect->nreloc);928 pSect->flags = K_E2E_U32(pSect->flags);929 pSect->reserved1 = K_E2E_U32(pSect->reserved1);930 pSect->reserved2 = K_E2E_U32(pSect->reserved2);877 pSect->addr = RT_BSWAP_U64(pSect->addr); 878 pSect->size = RT_BSWAP_U64(pSect->size); 879 pSect->offset = RT_BSWAP_U32(pSect->offset); 880 pSect->align = RT_BSWAP_U32(pSect->align); 881 pSect->reloff = RT_BSWAP_U32(pSect->reloff); 882 pSect->nreloc = RT_BSWAP_U32(pSect->nreloc); 883 pSect->flags = RT_BSWAP_U32(pSect->flags); 884 pSect->reserved1 = RT_BSWAP_U32(pSect->reserved1); 885 pSect->reserved2 = RT_BSWAP_U32(pSect->reserved2); 931 886 } 932 887 … … 942 897 case LC_SYMTAB: 943 898 { 944 KSIZEcbSym;899 size_t cbSym; 945 900 if (fConvertEndian) 946 901 { 947 u.pSymTab->symoff = K_E2E_U32(u.pSymTab->symoff);948 u.pSymTab->nsyms = K_E2E_U32(u.pSymTab->nsyms);949 u.pSymTab->stroff = K_E2E_U32(u.pSymTab->stroff);950 u.pSymTab->strsize = K_E2E_U32(u.pSymTab->strsize);902 u.pSymTab->symoff = RT_BSWAP_U32(u.pSymTab->symoff); 903 u.pSymTab->nsyms = RT_BSWAP_U32(u.pSymTab->nsyms); 904 u.pSymTab->stroff = RT_BSWAP_U32(u.pSymTab->stroff); 905 u.pSymTab->strsize = RT_BSWAP_U32(u.pSymTab->strsize); 951 906 } 952 907 … … 957 912 : sizeof(macho_nlist_64_t); 958 913 if ( u.pSymTab->symoff >= cbFile 959 || ( KU64)u.pSymTab->symoff + u.pSymTab->nsyms * cbSym > cbFile)960 KLDRMODMACHO_FAILED_RETURN( KLDR_ERR_MACHO_BAD_LOAD_COMMAND);914 || (uint64_t)u.pSymTab->symoff + u.pSymTab->nsyms * cbSym > cbFile) 915 KLDRMODMACHO_FAILED_RETURN(VERR_LDRMACHO_BAD_LOAD_COMMAND); 961 916 if ( u.pSymTab->stroff >= cbFile 962 || ( KU64)u.pSymTab->stroff + u.pSymTab->strsize > cbFile)963 KLDRMODMACHO_FAILED_RETURN( KLDR_ERR_MACHO_BAD_LOAD_COMMAND);917 || (uint64_t)u.pSymTab->stroff + u.pSymTab->strsize > cbFile) 918 KLDRMODMACHO_FAILED_RETURN(VERR_LDRMACHO_BAD_LOAD_COMMAND); 964 919 965 920 /* only one string in objects, please. */ … … 967 922 if ( uEffFileType == MH_OBJECT 968 923 && cSymbolTabs != 1) 969 KLDRMODMACHO_FAILED_RETURN( KLDR_ERR_MACHO_BAD_OBJECT_FILE);924 KLDRMODMACHO_FAILED_RETURN(VERR_LDRMACHO_BAD_OBJECT_FILE); 970 925 break; 971 926 } … … 978 933 case LC_UNIXTHREAD: 979 934 { 980 KU32 *pu32 = (KU32*)(u.pb + sizeof(load_command_t));981 KU32 cItemsLeft = (u.pThread->cmdsize - sizeof(load_command_t)) / sizeof(KU32);935 uint32_t *pu32 = (uint32_t *)(u.pb + sizeof(load_command_t)); 936 uint32_t cItemsLeft = (u.pThread->cmdsize - sizeof(load_command_t)) / sizeof(uint32_t); 982 937 while (cItemsLeft) 983 938 { 984 /* convert & verify header items ([0] == flavor, [1] == KU32count). */939 /* convert & verify header items ([0] == flavor, [1] == uint32_t count). */ 985 940 if (cItemsLeft < 2) 986 KLDRMODMACHO_FAILED_RETURN( KLDR_ERR_MACHO_BAD_LOAD_COMMAND);941 KLDRMODMACHO_FAILED_RETURN(VERR_LDRMACHO_BAD_LOAD_COMMAND); 987 942 if (fConvertEndian) 988 943 { 989 pu32[0] = K_E2E_U32(pu32[0]);990 pu32[1] = K_E2E_U32(pu32[1]);944 pu32[0] = RT_BSWAP_U32(pu32[0]); 945 pu32[1] = RT_BSWAP_U32(pu32[1]); 991 946 } 992 947 if (pu32[1] + 2 > cItemsLeft) 993 KLDRMODMACHO_FAILED_RETURN( KLDR_ERR_MACHO_BAD_LOAD_COMMAND);948 KLDRMODMACHO_FAILED_RETURN(VERR_LDRMACHO_BAD_LOAD_COMMAND); 994 949 995 950 /* convert & verify according to flavor. */ … … 1010 965 case LC_UUID: 1011 966 if (u.pUuid->cmdsize != sizeof(uuid_command_t)) 1012 KLDRMODMACHO_FAILED_RETURN( KLDR_ERR_MACHO_BAD_LOAD_COMMAND);967 KLDRMODMACHO_FAILED_RETURN(VERR_LDRMACHO_BAD_LOAD_COMMAND); 1013 968 /** @todo Check anything here need converting? */ 1014 969 break; … … 1016 971 case LC_CODE_SIGNATURE: 1017 972 if (u.pUuid->cmdsize != sizeof(linkedit_data_command_t)) 1018 KLDRMODMACHO_FAILED_RETURN( KLDR_ERR_MACHO_BAD_LOAD_COMMAND);973 KLDRMODMACHO_FAILED_RETURN(VERR_LDRMACHO_BAD_LOAD_COMMAND); 1019 974 break; 1020 975 … … 1022 977 case LC_VERSION_MIN_IPHONEOS: 1023 978 if (u.pUuid->cmdsize != sizeof(version_min_command_t)) 1024 KLDRMODMACHO_FAILED_RETURN( KLDR_ERR_MACHO_BAD_LOAD_COMMAND);979 KLDRMODMACHO_FAILED_RETURN(VERR_LDRMACHO_BAD_LOAD_COMMAND); 1025 980 break; 1026 981 … … 1049 1004 case LC_MAIN: /** @todo parse this and find and entry point or smth. */ 1050 1005 /** @todo valid command size. */ 1051 if (!(fOpenFlags & KLDRMOD_OPEN_FLAGS_FOR_INFO))1052 KLDRMODMACHO_FAILED_RETURN( KLDR_ERR_MACHO_UNSUPPORTED_LOAD_COMMAND);1053 *pfCanLoad = K_FALSE;1006 if (!(fOpenFlags & RTLDR_O_FOR_DEBUG)) 1007 KLDRMODMACHO_FAILED_RETURN(VERR_LDRMACHO_UNSUPPORTED_LOAD_COMMAND); 1008 *pfCanLoad = false; 1054 1009 break; 1055 1010 … … 1068 1023 case LC_PREBIND_CKSUM: 1069 1024 case LC_SYMSEG: 1070 KLDRMODMACHO_FAILED_RETURN( KLDR_ERR_MACHO_UNSUPPORTED_LOAD_COMMAND);1025 KLDRMODMACHO_FAILED_RETURN(VERR_LDRMACHO_UNSUPPORTED_LOAD_COMMAND); 1071 1026 1072 1027 default: 1073 KLDRMODMACHO_FAILED_RETURN( KLDR_ERR_MACHO_UNKNOWN_LOAD_COMMAND);1028 KLDRMODMACHO_FAILED_RETURN(VERR_LDRMACHO_UNKNOWN_LOAD_COMMAND); 1074 1029 } 1075 1030 } … … 1077 1032 /* be strict. */ 1078 1033 if (cbLeft) 1079 KLDRMODMACHO_FAILED_RETURN( KLDR_ERR_MACHO_BAD_LOAD_COMMAND);1034 KLDRMODMACHO_FAILED_RETURN(VERR_LDRMACHO_BAD_LOAD_COMMAND); 1080 1035 1081 1036 switch (uEffFileType) … … 1088 1043 case MH_KEXT_BUNDLE: 1089 1044 if (!cSegments) 1090 KLDRMODMACHO_FAILED_RETURN( KLDR_ERR_MACHO_BAD_OBJECT_FILE);1045 KLDRMODMACHO_FAILED_RETURN(VERR_LDRMACHO_BAD_OBJECT_FILE); 1091 1046 break; 1092 1047 } … … 1094 1049 *pcSegments = cSegments; 1095 1050 *pcSections = cSections; 1096 *pcbStringPool = ( KU32)cbStringPool;1051 *pcbStringPool = (uint32_t)cbStringPool; 1097 1052 1098 1053 return 0; … … 1106 1061 * 1107 1062 * @returns 0 on success. 1108 * @returns KLDR_ERR_MACHO_* on failure.1109 * @param p ModMachOThe module.1063 * @returns VERR_LDRMACHO_* on failure. 1064 * @param pThis The module. 1110 1065 * @param pbStringPool The string pool 1111 1066 * @param cbStringPool The size of the string pool. 1112 1067 */ 1113 static int kldrModMachOParseLoadCommands(PKLDRMODMACHO p ModMachO, char *pbStringPool, KU32cbStringPool)1068 static int kldrModMachOParseLoadCommands(PKLDRMODMACHO pThis, char *pbStringPool, uint32_t cbStringPool) 1114 1069 { 1115 1070 union 1116 1071 { 1117 const KU8*pb;1072 const uint8_t *pb; 1118 1073 const load_command_t *pLoadCmd; 1119 1074 const segment_command_32_t *pSeg32; … … 1122 1077 const uuid_command_t *pUuid; 1123 1078 } u; 1124 KU32 cLeft = pModMachO->Hdr.ncmds; 1125 KU32 cbLeft = pModMachO->Hdr.sizeofcmds; 1126 const KU8 *pb = pModMachO->pbLoadCommands; 1127 PKLDRSEG pDstSeg = &pModMachO->pMod->aSegments[0]; 1128 PKLDRMODMACHOSEG pSegExtra = &pModMachO->aSegments[0]; 1129 PKLDRMODMACHOSECT pSectExtra = pModMachO->paSections; 1130 const KU32 cSegments = pModMachO->pMod->cSegments; 1131 PKLDRSEG pSegItr; 1132 K_NOREF(cbStringPool); 1079 uint32_t cLeft = pThis->Hdr.ncmds; 1080 uint32_t cbLeft = pThis->Hdr.sizeofcmds; 1081 const uint8_t *pb = pThis->pbLoadCommands; 1082 PKLDRMODMACHOSEG pDstSeg = &pThis->aSegments[0]; 1083 PKLDRMODMACHOSECT pSectExtra = pThis->paSections; 1084 const uint32_t cSegments = pThis->cSegments; 1085 PKLDRMODMACHOSEG pSegItr; 1086 RT_NOREF(cbStringPool); 1133 1087 1134 1088 while (cLeft-- > 0) … … 1148 1102 section_32_t *pFirstSect = (section_32_t *)(pSrcSeg + 1); 1149 1103 section_32_t *pSect = pFirstSect; 1150 KU32cSectionsLeft = pSrcSeg->nsects;1104 uint32_t cSectionsLeft = pSrcSeg->nsects; 1151 1105 1152 1106 /* Adds a segment, used by the macro below and thus shared with the 64-bit segment variant. */ 1153 1107 #define NEW_SEGMENT(a_cBits, a_achName1, a_fObjFile, a_achName2, a_SegAddr, a_cbSeg, a_fFileBits, a_offFile, a_cbFile) \ 1154 1108 do { \ 1155 pDstSeg->pvUser = NULL; \ 1156 pDstSeg->pchName = pbStringPool; \ 1157 pDstSeg->cchName = (KU32)kHlpStrNLen(a_achName1, sizeof(a_achName1)); \ 1158 kHlpMemCopy(pbStringPool, a_achName1, pDstSeg->cchName); \ 1159 pbStringPool += pDstSeg->cchName; \ 1109 pDstSeg->SegInfo.pszName = pbStringPool; \ 1110 pDstSeg->SegInfo.cchName = (uint32_t)RTStrNLen(a_achName1, sizeof(a_achName1)); \ 1111 memcpy(pbStringPool, a_achName1, pDstSeg->SegInfo.cchName); \ 1112 pbStringPool += pDstSeg->SegInfo.cchName; \ 1160 1113 if (a_fObjFile) \ 1161 1114 { /* MH_OBJECT: Add '.sectname' - sections aren't sorted by segments. */ \ 1162 KSIZE cchName2 = kHlpStrNLen(a_achName2, sizeof(a_achName2)); \1115 size_t cchName2 = RTStrNLen(a_achName2, sizeof(a_achName2)); \ 1163 1116 *pbStringPool++ = '.'; \ 1164 kHlpMemCopy(pbStringPool, a_achName2, cchName2); \1117 memcpy(pbStringPool, a_achName2, cchName2); \ 1165 1118 pbStringPool += cchName2; \ 1166 pDstSeg-> cchName += (KU32)cchName2; \1119 pDstSeg->SegInfo.cchName += (uint32_t)cchName2; \ 1167 1120 } \ 1168 1121 *pbStringPool++ = '\0'; \ 1169 pDstSeg->Se lFlat = 0; \1170 pDstSeg->Se l16bit = 0; \1171 pDstSeg-> fFlags = 0; \1172 pDstSeg-> enmProt = KPROT_EXECUTE_WRITECOPY; /** @todo fixme! */ \1173 pDstSeg-> cb = (a_cbSeg); \1174 pDstSeg-> Alignment = 1; /* updated while parsing sections. */ \1175 pDstSeg-> LinkAddress = (a_SegAddr); \1122 pDstSeg->SegInfo.SelFlat = 0; \ 1123 pDstSeg->SegInfo.Sel16bit = 0; \ 1124 pDstSeg->SegInfo.fFlags = 0; \ 1125 pDstSeg->SegInfo.fProt = RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC; /** @todo fixme! */ \ 1126 pDstSeg->SegInfo.cb = (a_cbSeg); \ 1127 pDstSeg->SegInfo.Alignment = 1; /* updated while parsing sections. */ \ 1128 pDstSeg->SegInfo.LinkAddress = (a_SegAddr); \ 1176 1129 if (a_fFileBits) \ 1177 1130 { \ 1178 pDstSeg-> offFile = (KLDRFOFF)((a_offFile) + pModMachO->offImage); \1179 pDstSeg-> cbFile = (KLDRFOFF)(a_cbFile); \1131 pDstSeg->SegInfo.offFile = (RTFOFF)((a_offFile) + pThis->offImage); \ 1132 pDstSeg->SegInfo.cbFile = (RTFOFF)(a_cbFile); \ 1180 1133 } \ 1181 1134 else \ 1182 1135 { \ 1183 pDstSeg-> offFile = -1; \1184 pDstSeg-> cbFile = -1; \1136 pDstSeg->SegInfo.offFile = -1; \ 1137 pDstSeg->SegInfo.cbFile = -1; \ 1185 1138 } \ 1186 pDstSeg->RVA = (a_SegAddr) - pModMachO->LinkAddress; \ 1187 pDstSeg->cbMapped = 0; \ 1188 pDstSeg->MapAddress = 0; \ 1139 pDstSeg->SegInfo.RVA = (a_SegAddr) - pThis->LinkAddress; \ 1140 pDstSeg->SegInfo.cbMapped = 0; \ 1189 1141 \ 1190 p SegExtra->iOrgSegNo = (KU32)(pSegExtra - &pModMachO->aSegments[0]); \1191 p SegExtra->cSections = 0; \1192 p SegExtra->paSections = pSectExtra; \1142 pDstSeg->iOrgSegNo = (uint32_t)(pDstSeg - &pThis->aSegments[0]); \ 1143 pDstSeg->cSections = 0; \ 1144 pDstSeg->paSections = pSectExtra; \ 1193 1145 } while (0) 1194 1146 … … 1196 1148 #define CLOSE_SEGMENT() \ 1197 1149 do { \ 1198 pSegExtra->cSections = (KU32)(pSectExtra - pSegExtra->paSections); \ 1199 pSegExtra++; \ 1150 pDstSeg->cSections = (uint32_t)(pSectExtra - pDstSeg->paSections); \ 1200 1151 pDstSeg++; \ 1201 1152 } while (0) … … 1205 1156 #define ADD_SEGMENT_AND_ITS_SECTIONS(a_cBits) \ 1206 1157 do { \ 1207 KBOOL fAddSegOuter = K_FALSE; \1158 bool fAddSegOuter = false; \ 1208 1159 \ 1209 1160 /* \ … … 1211 1162 * in the preparsing stage. \ 1212 1163 */ \ 1213 if (p ModMachO->uEffFileType != MH_OBJECT) \1214 for (pSegItr = &p ModMachO->pMod->aSegments[0]; pSegItr != pDstSeg; pSegItr++) \1215 if (! kHlpStrNComp(pSegItr->pchName, pSrcSeg->segname, sizeof(pSrcSeg->segname))) \1216 KLDRMODMACHO_FAILED_RETURN( KLDR_ERR_DUPLICATE_SEGMENT_NAME); \1164 if (pThis->uEffFileType != MH_OBJECT) \ 1165 for (pSegItr = &pThis->aSegments[0]; pSegItr != pDstSeg; pSegItr++) \ 1166 if (!strncmp(pSegItr->SegInfo.pszName, pSrcSeg->segname, sizeof(pSrcSeg->segname))) \ 1167 KLDRMODMACHO_FAILED_RETURN(VERR_LDR_DUPLICATE_SEGMENT_NAME); \ 1217 1168 \ 1218 1169 /* \ 1219 1170 * Create a new segment, unless we're supposed to skip this one. \ 1220 1171 */ \ 1221 if ( p ModMachO->uEffFileType != MH_OBJECT \1172 if ( pThis->uEffFileType != MH_OBJECT \ 1222 1173 && (cSectionsLeft == 0 || !(pFirstSect->flags & S_ATTR_DEBUG)) \ 1223 && kHlpStrComp(pSrcSeg->segname, "__DWARF") \1224 && kHlpStrComp(pSrcSeg->segname, "__CTF") ) \1174 && strcmp(pSrcSeg->segname, "__DWARF") \ 1175 && strcmp(pSrcSeg->segname, "__CTF") ) \ 1225 1176 { \ 1226 NEW_SEGMENT(a_cBits, pSrcSeg->segname, K_FALSE/*a_fObjFile*/, 0 /*a_achName2*/, \1177 NEW_SEGMENT(a_cBits, pSrcSeg->segname, false /*a_fObjFile*/, 0 /*a_achName2*/, \ 1227 1178 pSrcSeg->vmaddr, pSrcSeg->vmsize, \ 1228 1179 pSrcSeg->filesize != 0, pSrcSeg->fileoff, pSrcSeg->filesize); \ 1229 fAddSegOuter = K_TRUE; \1180 fAddSegOuter = true; \ 1230 1181 } \ 1231 1182 \ … … 1236 1187 { \ 1237 1188 /* New segment if object file. */ \ 1238 KBOOL fAddSegInner = K_FALSE; \1239 if ( p ModMachO->uEffFileType == MH_OBJECT \1189 bool fAddSegInner = false; \ 1190 if ( pThis->uEffFileType == MH_OBJECT \ 1240 1191 && !(pSect->flags & S_ATTR_DEBUG) \ 1241 && kHlpStrComp(pSrcSeg->segname, "__DWARF") \1242 && kHlpStrComp(pSrcSeg->segname, "__CTF") ) \1192 && strcmp(pSrcSeg->segname, "__DWARF") \ 1193 && strcmp(pSrcSeg->segname, "__CTF") ) \ 1243 1194 { \ 1244 kHlpAssert(!fAddSegOuter); \1245 NEW_SEGMENT(a_cBits, pSect->segname, K_TRUE/*a_fObjFile*/, pSect->sectname, \1195 Assert(!fAddSegOuter); \ 1196 NEW_SEGMENT(a_cBits, pSect->segname, true /*a_fObjFile*/, pSect->sectname, \ 1246 1197 pSect->addr, pSect->size, \ 1247 1198 pSect->offset != 0, pSect->offset, pSect->size); \ 1248 fAddSegInner = K_TRUE; \1199 fAddSegInner = true; \ 1249 1200 } \ 1250 1201 \ 1251 1202 /* Section data extract. */ \ 1252 1203 pSectExtra->cb = pSect->size; \ 1253 pSectExtra->RVA = pSect->addr - pDstSeg-> LinkAddress; \1204 pSectExtra->RVA = pSect->addr - pDstSeg->SegInfo.LinkAddress; \ 1254 1205 pSectExtra->LinkAddress = pSect->addr; \ 1255 1206 if (pSect->offset) \ 1256 pSectExtra->offFile = pSect->offset + p ModMachO->offImage; \1207 pSectExtra->offFile = pSect->offset + pThis->offImage; \ 1257 1208 else \ 1258 1209 pSectExtra->offFile = -1; \ … … 1260 1211 pSectExtra->paFixups = NULL; \ 1261 1212 if (pSect->nreloc) \ 1262 pSectExtra->offFixups = pSect->reloff + p ModMachO->offImage; \1213 pSectExtra->offFixups = pSect->reloff + pThis->offImage; \ 1263 1214 else \ 1264 1215 pSectExtra->offFixups = -1; \ 1265 1216 pSectExtra->fFlags = pSect->flags; \ 1266 pSectExtra->iSegment = ( KU32)(pSegExtra - &pModMachO->aSegments[0]); \1217 pSectExtra->iSegment = (uint32_t)(pDstSeg - &pThis->aSegments[0]); \ 1267 1218 pSectExtra->pvMachoSection = pSect; \ 1268 1219 \ 1269 1220 /* Update the segment alignment, if we're not skipping it. */ \ 1270 1221 if ( (fAddSegOuter || fAddSegInner) \ 1271 && pDstSeg-> Alignment < ((KLDRADDR)1 << pSect->align)) \1272 pDstSeg-> Alignment = (KLDRADDR)1 << pSect->align; \1222 && pDstSeg->SegInfo.Alignment < ((RTLDRADDR)1 << pSect->align)) \ 1223 pDstSeg->SegInfo.Alignment = (RTLDRADDR)1 << pSect->align; \ 1273 1224 \ 1274 1225 /* Next section, and if object file next segment. */ \ … … 1293 1244 section_64_t *pFirstSect = (section_64_t *)(pSrcSeg + 1); 1294 1245 section_64_t *pSect = pFirstSect; 1295 KU32cSectionsLeft = pSrcSeg->nsects;1246 uint32_t cSectionsLeft = pSrcSeg->nsects; 1296 1247 1297 1248 ADD_SEGMENT_AND_ITS_SECTIONS(64); … … 1300 1251 1301 1252 case LC_SYMTAB: 1302 switch (p ModMachO->uEffFileType)1253 switch (pThis->uEffFileType) 1303 1254 { 1304 1255 case MH_OBJECT: … … 1308 1259 case MH_DSYM: 1309 1260 case MH_KEXT_BUNDLE: 1310 p ModMachO->offSymbols = u.pSymTab->symoff + pModMachO->offImage;1311 p ModMachO->cSymbols = u.pSymTab->nsyms;1312 p ModMachO->offStrings = u.pSymTab->stroff + pModMachO->offImage;1313 p ModMachO->cchStrings = u.pSymTab->strsize;1261 pThis->offSymbols = u.pSymTab->symoff + pThis->offImage; 1262 pThis->cSymbols = u.pSymTab->nsyms; 1263 pThis->offStrings = u.pSymTab->stroff + pThis->offImage; 1264 pThis->cchStrings = u.pSymTab->strsize; 1314 1265 break; 1315 1266 } … … 1317 1268 1318 1269 case LC_UUID: 1319 kHlpMemCopy(pModMachO->abImageUuid, u.pUuid->uuid, sizeof(pModMachO->abImageUuid));1270 memcpy(pThis->abImageUuid, u.pUuid->uuid, sizeof(pThis->abImageUuid)); 1320 1271 break; 1321 1272 … … 1325 1276 } /* while more commands */ 1326 1277 1327 kHlpAssert(pDstSeg == &pModMachO->pMod->aSegments[cSegments - pModMachO->fMakeGot]);1278 Assert(pDstSeg == &pThis->aSegments[cSegments - pThis->fMakeGot]); 1328 1279 1329 1280 /* … … 1331 1282 */ 1332 1283 { 1333 KBOOL fLoadLinkEdit = K_FALSE;1284 bool fLoadLinkEdit = false; 1334 1285 PKLDRMODMACHOSECT pSectExtraItr; 1335 KLDRADDRuNextRVA = 0;1336 KLDRADDRcb;1337 KU32 cSegmentsToAdjust = cSegments - pModMachO->fMakeGot;1338 KU32c;1286 RTLDRADDR uNextRVA = 0; 1287 RTLDRADDR cb; 1288 uint32_t cSegmentsToAdjust = cSegments - pThis->fMakeGot; 1289 uint32_t c; 1339 1290 1340 1291 for (;;) … … 1343 1294 out of the RVA negotiations and image loading. */ 1344 1295 if ( cSegmentsToAdjust > 0 1345 && ! kHlpStrComp(pModMachO->pMod->aSegments[cSegmentsToAdjust - 1].pchName, "__DWARF"))1296 && !strcmp(pThis->aSegments[cSegmentsToAdjust - 1].SegInfo.pszName, "__DWARF")) 1346 1297 { 1347 1298 cSegmentsToAdjust--; 1348 p ModMachO->pMod->aSegments[cSegmentsToAdjust].RVA = NIL_KLDRADDR;1349 p ModMachO->pMod->aSegments[cSegmentsToAdjust].cbMapped = 0;1299 pThis->aSegments[cSegmentsToAdjust].SegInfo.RVA = NIL_RTLDRADDR; 1300 pThis->aSegments[cSegmentsToAdjust].SegInfo.cbMapped = 0; 1350 1301 continue; 1351 1302 } … … 1356 1307 if ( !fLoadLinkEdit 1357 1308 && cSegmentsToAdjust > 0 1358 && ! kHlpStrComp(pModMachO->pMod->aSegments[cSegmentsToAdjust - 1].pchName, "__LINKEDIT"))1309 && !strcmp(pThis->aSegments[cSegmentsToAdjust - 1].SegInfo.pszName, "__LINKEDIT")) 1359 1310 { 1360 1311 cSegmentsToAdjust--; 1361 p ModMachO->pMod->aSegments[cSegmentsToAdjust].RVA = NIL_KLDRADDR;1362 p ModMachO->pMod->aSegments[cSegmentsToAdjust].cbMapped = 0;1312 pThis->aSegments[cSegmentsToAdjust].SegInfo.RVA = NIL_RTLDRADDR; 1313 pThis->aSegments[cSegmentsToAdjust].SegInfo.cbMapped = 0; 1363 1314 continue; 1364 1315 } … … 1368 1319 /* Adjust RVAs. */ 1369 1320 c = cSegmentsToAdjust; 1370 for (pDstSeg = &p ModMachO->pMod->aSegments[0]; c-- > 0; pDstSeg++)1371 { 1372 cb = pDstSeg-> RVA - uNextRVA;1321 for (pDstSeg = &pThis->aSegments[0]; c-- > 0; pDstSeg++) 1322 { 1323 cb = pDstSeg->SegInfo.RVA - uNextRVA; 1373 1324 if (cb >= 0x00100000) /* 1MB */ 1374 1325 { 1375 pDstSeg-> RVA = uNextRVA;1376 pModMachO->pMod->fFlags |= KLDRMOD_FLAGS_NON_CONTIGUOUS_LINK_ADDRS;1326 pDstSeg->SegInfo.RVA = uNextRVA; 1327 //pThis->pMod->fFlags |= KLDRMOD_FLAGS_NON_CONTIGUOUS_LINK_ADDRS; 1377 1328 } 1378 uNextRVA = pDstSeg-> RVA + KLDR_ALIGN_ADDR(pDstSeg->cb, pDstSeg->Alignment);1329 uNextRVA = pDstSeg->SegInfo.RVA + RTLDR_ALIGN_ADDR(pDstSeg->SegInfo.cb, pDstSeg->SegInfo.Alignment); 1379 1330 } 1380 1331 1381 1332 /* Calculate the cbMapping members. */ 1382 1333 c = cSegmentsToAdjust; 1383 for (pDstSeg = &p ModMachO->pMod->aSegments[0]; c-- > 1; pDstSeg++)1384 { 1385 1386 cb = pDstSeg[1]. RVA - pDstSeg->RVA;1387 pDstSeg-> cbMapped = (KSIZE)cb == cb ? (KSIZE)cb : KSIZE_MAX;1388 } 1389 1390 cb = KLDR_ALIGN_ADDR(pDstSeg->cb, pDstSeg->Alignment);1391 pDstSeg-> cbMapped = (KSIZE)cb == cb ? (KSIZE)cb : KSIZE_MAX;1334 for (pDstSeg = &pThis->aSegments[0]; c-- > 1; pDstSeg++) 1335 { 1336 1337 cb = pDstSeg[1].SegInfo.RVA - pDstSeg->SegInfo.RVA; 1338 pDstSeg->SegInfo.cbMapped = (size_t)cb == cb ? (size_t)cb : ~(size_t)0; 1339 } 1340 1341 cb = RTLDR_ALIGN_ADDR(pDstSeg->SegInfo.cb, pDstSeg->SegInfo.Alignment); 1342 pDstSeg->SegInfo.cbMapped = (size_t)cb == cb ? (size_t)cb : ~(size_t)0; 1392 1343 1393 1344 /* Set the image size. */ 1394 p ModMachO->cbImage = pDstSeg->RVA + cb;1345 pThis->cbImage = pDstSeg->SegInfo.RVA + cb; 1395 1346 1396 1347 /* Fixup the section RVAs (internal). */ 1397 1348 c = cSegmentsToAdjust; 1398 uNextRVA = p ModMachO->cbImage;1399 pDstSeg = &p ModMachO->pMod->aSegments[0];1400 for (pSectExtraItr = p ModMachO->paSections; pSectExtraItr != pSectExtra; pSectExtraItr++)1349 uNextRVA = pThis->cbImage; 1350 pDstSeg = &pThis->aSegments[0]; 1351 for (pSectExtraItr = pThis->paSections; pSectExtraItr != pSectExtra; pSectExtraItr++) 1401 1352 { 1402 1353 if (pSectExtraItr->iSegment < c) 1403 pSectExtraItr->RVA += pDstSeg[pSectExtraItr->iSegment]. RVA;1354 pSectExtraItr->RVA += pDstSeg[pSectExtraItr->iSegment].SegInfo.RVA; 1404 1355 else 1405 1356 { 1406 1357 pSectExtraItr->RVA = uNextRVA; 1407 uNextRVA += KLDR_ALIGN_ADDR(pSectExtraItr->cb, 64);1358 uNextRVA += RTLDR_ALIGN_ADDR(pSectExtraItr->cb, 64); 1408 1359 } 1409 1360 } … … 1413 1364 * Make the GOT segment if necessary. 1414 1365 */ 1415 if (p ModMachO->fMakeGot)1416 { 1417 KU32 cbPtr = ( pModMachO->Hdr.magic == IMAGE_MACHO32_SIGNATURE1418 || p ModMachO->Hdr.magic == IMAGE_MACHO32_SIGNATURE_OE)1419 ? sizeof( KU32)1420 : sizeof( KU64);1421 KU32 cbGot = pModMachO->cSymbols * cbPtr;1422 KU32cbJmpStubs;1423 1424 p ModMachO->GotRVA = pModMachO->cbImage;1425 1426 if (p ModMachO->cbJmpStub)1427 { 1428 cbGot = K_ALIGN_Z(cbGot, 64);1429 p ModMachO->JmpStubsRVA = pModMachO->GotRVA + cbGot;1430 cbJmpStubs = p ModMachO->cbJmpStub * pModMachO->cSymbols;1366 if (pThis->fMakeGot) 1367 { 1368 uint32_t cbPtr = ( pThis->Hdr.magic == IMAGE_MACHO32_SIGNATURE 1369 || pThis->Hdr.magic == IMAGE_MACHO32_SIGNATURE_OE) 1370 ? sizeof(uint32_t) 1371 : sizeof(uint64_t); 1372 uint32_t cbGot = pThis->cSymbols * cbPtr; 1373 uint32_t cbJmpStubs; 1374 1375 pThis->GotRVA = pThis->cbImage; 1376 1377 if (pThis->cbJmpStub) 1378 { 1379 cbGot = RT_ALIGN_Z(cbGot, 64); 1380 pThis->JmpStubsRVA = pThis->GotRVA + cbGot; 1381 cbJmpStubs = pThis->cbJmpStub * pThis->cSymbols; 1431 1382 } 1432 1383 else 1433 1384 { 1434 p ModMachO->JmpStubsRVA = NIL_KLDRADDR;1385 pThis->JmpStubsRVA = NIL_RTLDRADDR; 1435 1386 cbJmpStubs = 0; 1436 1387 } 1437 1388 1438 pDstSeg = &pModMachO->pMod->aSegments[cSegments - 1]; 1439 pDstSeg->pvUser = NULL; 1440 pDstSeg->pchName = "GOT"; 1441 pDstSeg->cchName = 3; 1442 pDstSeg->SelFlat = 0; 1443 pDstSeg->Sel16bit = 0; 1444 pDstSeg->fFlags = 0; 1445 pDstSeg->enmProt = KPROT_READONLY; 1446 pDstSeg->cb = cbGot + cbJmpStubs; 1447 pDstSeg->Alignment = 64; 1448 pDstSeg->LinkAddress = pModMachO->LinkAddress + pModMachO->GotRVA; 1449 pDstSeg->offFile = -1; 1450 pDstSeg->cbFile = -1; 1451 pDstSeg->RVA = pModMachO->GotRVA; 1452 pDstSeg->cbMapped = (KSIZE)KLDR_ALIGN_ADDR(cbGot + cbJmpStubs, pDstSeg->Alignment); 1453 pDstSeg->MapAddress = 0; 1454 1455 pSegExtra->iOrgSegNo = KU32_MAX; 1456 pSegExtra->cSections = 0; 1457 pSegExtra->paSections = NULL; 1458 1459 pModMachO->cbImage += pDstSeg->cbMapped; 1389 pDstSeg = &pThis->aSegments[cSegments - 1]; 1390 pDstSeg->SegInfo.pszName = "GOT"; 1391 pDstSeg->SegInfo.cchName = 3; 1392 pDstSeg->SegInfo.SelFlat = 0; 1393 pDstSeg->SegInfo.Sel16bit = 0; 1394 pDstSeg->SegInfo.fFlags = 0; 1395 pDstSeg->SegInfo.fProt = RTMEM_PROT_READ; 1396 pDstSeg->SegInfo.cb = cbGot + cbJmpStubs; 1397 pDstSeg->SegInfo.Alignment = 64; 1398 pDstSeg->SegInfo.LinkAddress = pThis->LinkAddress + pThis->GotRVA; 1399 pDstSeg->SegInfo.offFile = -1; 1400 pDstSeg->SegInfo.cbFile = -1; 1401 pDstSeg->SegInfo.RVA = pThis->GotRVA; 1402 pDstSeg->SegInfo.cbMapped = (size_t)RTLDR_ALIGN_ADDR(cbGot + cbJmpStubs, pDstSeg->SegInfo.Alignment); 1403 1404 pDstSeg->iOrgSegNo = UINT32_MAX; 1405 pDstSeg->cSections = 0; 1406 pDstSeg->paSections = NULL; 1407 1408 pThis->cbImage += pDstSeg->SegInfo.cbMapped; 1460 1409 } 1461 1410 … … 1464 1413 1465 1414 1466 /** @copydoc KLDRMODOPS::pfnDestroy */ 1467 static int kldrModMachODestroy(PKLDRMOD pMod) 1468 { 1469 PKLDRMODMACHO pModMachO = (PKLDRMODMACHO)pMod->pvData; 1415 /** 1416 * @interface_method_impl{RTLDROPS,pfnClose} 1417 */ 1418 static DECLCALLBACK(int) rtldrMachO_Close(PRTLDRMODINTERNAL pMod) 1419 { 1420 PKLDRMODMACHO pThis = RT_FROM_MEMBER(pMod, KLDRMODMACHO, Core); 1470 1421 int rc = 0; 1471 KU32 i, j; 1472 KLDRMODMACHO_ASSERT(!pModMachO->pvMapping); 1473 1474 i = pMod->cSegments; 1422 KLDRMODMACHO_ASSERT(!pThis->pvMapping); 1423 1424 uint32_t i = pThis->cSegments; 1475 1425 while (i-- > 0) 1476 1426 { 1477 j = pModMachO->aSegments[i].cSections;1427 uint32_t j = pThis->aSegments[i].cSections; 1478 1428 while (j-- > 0) 1479 1429 { 1480 kHlpFree(pModMachO->aSegments[i].paSections[j].paFixups);1481 p ModMachO->aSegments[i].paSections[j].paFixups = NULL;1482 } 1483 } 1484 1485 if (p Mod->pRdr)1486 { 1487 rc = kRdrClose(pMod->pRdr);1488 p Mod->pRdr = NULL;1489 } 1490 p Mod->u32Magic = 0;1491 p Mod->pOps = NULL;1492 kHlpFree(pModMachO->pbLoadCommands);1493 p ModMachO->pbLoadCommands = NULL;1494 kHlpFree(pModMachO->pchStrings);1495 p ModMachO->pchStrings = NULL;1496 kHlpFree(pModMachO->pvaSymbols);1497 p ModMachO->pvaSymbols = NULL;1498 kHlpFree(pModMachO);1430 RTMemFree(pThis->aSegments[i].paSections[j].paFixups); 1431 pThis->aSegments[i].paSections[j].paFixups = NULL; 1432 } 1433 } 1434 1435 if (pThis->Core.pReader) 1436 { 1437 rc = pThis->Core.pReader->pfnDestroy(pThis->Core.pReader); 1438 pThis->Core.pReader = NULL; 1439 } 1440 pThis->Core.u32Magic = 0; 1441 pThis->Core.pOps = NULL; 1442 RTMemFree(pThis->pbLoadCommands); 1443 pThis->pbLoadCommands = NULL; 1444 RTMemFree(pThis->pchStrings); 1445 pThis->pchStrings = NULL; 1446 RTMemFree(pThis->pvaSymbols); 1447 pThis->pvaSymbols = NULL; 1448 RTMemFree(pThis); 1499 1449 return rc; 1500 1450 } … … 1506 1456 * @returns 0 on success. 1507 1457 * @returns A non-zero status code if the BaseAddress isn't right. 1508 * @param p ModMachOThe interpreter module instance1458 * @param pThis The interpreter module instance 1509 1459 * @param pBaseAddress The base address, IN & OUT. Optional. 1510 1460 */ 1511 static int kldrModMachOAdjustBaseAddress(PKLDRMODMACHO p ModMachO, PKLDRADDR pBaseAddress)1461 static int kldrModMachOAdjustBaseAddress(PKLDRMODMACHO pThis, PRTLDRADDR pBaseAddress) 1512 1462 { 1513 1463 /* 1514 1464 * Adjust the base address. 1515 1465 */ 1516 if (*pBaseAddress == KLDRMOD_BASEADDRESS_MAP) 1517 *pBaseAddress = pModMachO->pMod->aSegments[0].MapAddress; 1518 else if (*pBaseAddress == KLDRMOD_BASEADDRESS_LINK) 1519 *pBaseAddress = pModMachO->LinkAddress; 1520 1466 if (*pBaseAddress == RTLDR_BASEADDRESS_LINK) 1467 *pBaseAddress = pThis->LinkAddress; 1521 1468 return 0; 1522 1469 } … … 1529 1476 * and segments. This function checks for these and returns the right value. 1530 1477 * 1531 * @returns 0 or KLDR_ERR_SYMBOL_NOT_FOUND. 1532 * @param pModMachO The interpreter module instance. 1533 * @param pMod The generic module instance. 1478 * @returns 0 or VERR_SYMBOL_NOT_FOUND. 1479 * @param pThis The interpreter module instance. 1534 1480 * @param pchSymbol The symbol. 1535 1481 * @param cchSymbol The length of the symbol. … … 1538 1484 * @param puValue Where to return the symbol value. 1539 1485 */ 1540 static int kldrModMachOQueryLinkerSymbol(PKLDRMODMACHO p ModMachO, PKLDRMOD pMod, const char *pchSymbol, KSIZEcchSymbol,1541 KLDRADDR BaseAddress, PKLDRADDR puValue)1486 static int kldrModMachOQueryLinkerSymbol(PKLDRMODMACHO pThis, const char *pchSymbol, size_t cchSymbol, 1487 RTLDRADDR BaseAddress, PRTLDRADDR puValue) 1542 1488 { 1543 1489 /* … … 1547 1493 { 1548 1494 const char *pszPrefix; 1549 KU8cchPrefix;1550 KBOOLfSection;1551 KBOOLfStart;1495 uint32_t cchPrefix; 1496 bool fSection; 1497 bool fStart; 1552 1498 } s_aPrefixes[] = 1553 1499 { 1554 { "section$start$", ( KU8)sizeof("section$start$") - 1, K_TRUE, K_TRUE},1555 { "section$end$", ( KU8)sizeof("section$end$") - 1, K_TRUE, K_FALSE},1556 { "segment$start$", ( KU8)sizeof("segment$start$") - 1, K_FALSE, K_TRUE},1557 { "segment$end$", ( KU8)sizeof("segment$end$") - 1, K_FALSE, K_FALSE},1500 { "section$start$", (uint8_t)sizeof("section$start$") - 1, true, true }, 1501 { "section$end$", (uint8_t)sizeof("section$end$") - 1, true, false}, 1502 { "segment$start$", (uint8_t)sizeof("segment$start$") - 1, false, true }, 1503 { "segment$end$", (uint8_t)sizeof("segment$end$") - 1, false, false}, 1558 1504 }; 1559 KSIZEcchSectName = 0;1505 size_t cchSectName = 0; 1560 1506 const char *pchSectName = ""; 1561 KSIZEcchSegName = 0;1507 size_t cchSegName = 0; 1562 1508 const char *pchSegName = NULL; 1563 KU32 iPrefix = K_ELEMENTS(s_aPrefixes) - 1;1564 KU32iSeg;1565 KLDRADDRuValue;1509 uint32_t iPrefix = RT_ELEMENTS(s_aPrefixes) - 1; 1510 uint32_t iSeg; 1511 RTLDRADDR uValue; 1566 1512 1567 1513 for (;;) 1568 1514 { 1569 KU8const cchPrefix = s_aPrefixes[iPrefix].cchPrefix;1515 uint8_t const cchPrefix = s_aPrefixes[iPrefix].cchPrefix; 1570 1516 if ( cchSymbol > cchPrefix 1571 && kHlpStrNComp(pchSymbol, s_aPrefixes[iPrefix].pszPrefix, cchPrefix) == 0)1517 && strncmp(pchSymbol, s_aPrefixes[iPrefix].pszPrefix, cchPrefix) == 0) 1572 1518 { 1573 1519 pchSegName = pchSymbol + cchPrefix; … … 1578 1524 /* next */ 1579 1525 if (!iPrefix) 1580 return KLDR_ERR_SYMBOL_NOT_FOUND;1526 return VERR_SYMBOL_NOT_FOUND; 1581 1527 iPrefix--; 1582 1528 } … … 1587 1533 if (s_aPrefixes[iPrefix].fSection) 1588 1534 { 1589 pchSectName = kHlpMemChr(pchSegName, '$', cchSegName);1535 pchSectName = (const char *)memchr(pchSegName, '$', cchSegName); 1590 1536 if (!pchSectName) 1591 return KLDR_ERR_SYMBOL_NOT_FOUND;1537 return VERR_SYMBOL_NOT_FOUND; 1592 1538 cchSegName = pchSectName - pchSegName; 1593 1539 pchSectName++; … … 1598 1544 * Locate the segment. 1599 1545 */ 1600 if (!p Mod->cSegments)1601 return KLDR_ERR_SYMBOL_NOT_FOUND;1602 for (iSeg = 0; iSeg < p Mod->cSegments; iSeg++)1603 { 1604 if ( p Mod->aSegments[iSeg].cchName >= cchSegName1605 && kHlpMemComp(pMod->aSegments[iSeg].pchName, pchSegName, cchSegName) == 0)1546 if (!pThis->cSegments) 1547 return VERR_SYMBOL_NOT_FOUND; 1548 for (iSeg = 0; iSeg < pThis->cSegments; iSeg++) 1549 { 1550 if ( pThis->aSegments[iSeg].SegInfo.cchName >= cchSegName 1551 && memcmp(pThis->aSegments[iSeg].SegInfo.pszName, pchSegName, cchSegName) == 0) 1606 1552 { 1607 1553 section_32_t const *pSect; 1608 if ( p Mod->aSegments[iSeg].cchName == cchSegName1609 && p ModMachO->Hdr.filetype != MH_OBJECT /* Good enough for __DWARF segs in MH_DHSYM, I hope. */)1554 if ( pThis->aSegments[iSeg].SegInfo.cchName == cchSegName 1555 && pThis->Hdr.filetype != MH_OBJECT /* Good enough for __DWARF segs in MH_DHSYM, I hope. */) 1610 1556 break; 1611 1557 1612 pSect = (section_32_t *)p ModMachO->aSegments[iSeg].paSections[0].pvMachoSection;1613 if ( p ModMachO->uEffFileType == MH_OBJECT1614 && p Mod->aSegments[iSeg].cchName > cchSegName + 11615 && p Mod->aSegments[iSeg].pchName[cchSegName] == '.'1616 && kHlpStrNComp(&pMod->aSegments[iSeg].pchName[cchSegName + 1], pSect->sectname, sizeof(pSect->sectname)) == 01617 && p Mod->aSegments[iSeg].cchName - cchSegName - 1 <= sizeof(pSect->sectname) )1558 pSect = (section_32_t *)pThis->aSegments[iSeg].paSections[0].pvMachoSection; 1559 if ( pThis->uEffFileType == MH_OBJECT 1560 && pThis->aSegments[iSeg].SegInfo.cchName > cchSegName + 1 1561 && pThis->aSegments[iSeg].SegInfo.pszName[cchSegName] == '.' 1562 && strncmp(&pThis->aSegments[iSeg].SegInfo.pszName[cchSegName + 1], pSect->sectname, sizeof(pSect->sectname)) == 0 1563 && pThis->aSegments[iSeg].SegInfo.cchName - cchSegName - 1 <= sizeof(pSect->sectname) ) 1618 1564 break; 1619 1565 } 1620 1566 } 1621 if (iSeg >= p Mod->cSegments)1622 return KLDR_ERR_SYMBOL_NOT_FOUND;1567 if (iSeg >= pThis->cSegments) 1568 return VERR_SYMBOL_NOT_FOUND; 1623 1569 1624 1570 if (!s_aPrefixes[iPrefix].fSection) … … 1627 1573 * Calculate the segment start/end address. 1628 1574 */ 1629 uValue = p Mod->aSegments[iSeg].RVA;1575 uValue = pThis->aSegments[iSeg].SegInfo.RVA; 1630 1576 if (!s_aPrefixes[iPrefix].fStart) 1631 uValue += p Mod->aSegments[iSeg].cb;1577 uValue += pThis->aSegments[iSeg].SegInfo.cb; 1632 1578 } 1633 1579 else … … 1636 1582 * Locate the section. 1637 1583 */ 1638 KU32 iSect = pModMachO->aSegments[iSeg].cSections;1584 uint32_t iSect = pThis->aSegments[iSeg].cSections; 1639 1585 if (!iSect) 1640 return KLDR_ERR_SYMBOL_NOT_FOUND;1586 return VERR_SYMBOL_NOT_FOUND; 1641 1587 for (;;) 1642 1588 { 1643 section_32_t *pSect = (section_32_t *)p ModMachO->aSegments[iSeg].paSections[iSect].pvMachoSection;1589 section_32_t *pSect = (section_32_t *)pThis->aSegments[iSeg].paSections[iSect].pvMachoSection; 1644 1590 if ( cchSectName <= sizeof(pSect->sectname) 1645 && kHlpMemComp(pSect->sectname, pchSectName, cchSectName) == 01591 && memcmp(pSect->sectname, pchSectName, cchSectName) == 0 1646 1592 && ( cchSectName == sizeof(pSect->sectname) 1647 1593 || pSect->sectname[cchSectName] == '\0') ) … … 1649 1595 /* next */ 1650 1596 if (!iSect) 1651 return KLDR_ERR_SYMBOL_NOT_FOUND;1597 return VERR_SYMBOL_NOT_FOUND; 1652 1598 iSect--; 1653 1599 } 1654 1600 1655 uValue = p ModMachO->aSegments[iSeg].paSections[iSect].RVA;1601 uValue = pThis->aSegments[iSeg].paSections[iSect].RVA; 1656 1602 if (!s_aPrefixes[iPrefix].fStart) 1657 uValue += p ModMachO->aSegments[iSeg].paSections[iSect].cb;1603 uValue += pThis->aSegments[iSeg].paSections[iSect].cb; 1658 1604 } 1659 1605 … … 1669 1615 1670 1616 1671 /** @copydoc kLdrModQuerySymbol */ 1672 static int kldrModMachOQuerySymbol(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, KU32 iSymbol, 1673 const char *pchSymbol, KSIZE cchSymbol, const char *pszVersion, 1674 PFNKLDRMODGETIMPORT pfnGetForwarder, void *pvUser, PKLDRADDR puValue, KU32 *pfKind) 1675 { 1676 PKLDRMODMACHO pModMachO = (PKLDRMODMACHO)pMod->pvData; 1677 int rc; 1678 K_NOREF(pvBits); 1679 K_NOREF(pszVersion); 1680 K_NOREF(pfnGetForwarder); 1681 K_NOREF(pvUser); 1617 /** 1618 * @interface_method_impl{RTLDROPS,pfnGetSymbolEx} 1619 */ 1620 static DECLCALLBACK(int) rtldrMachO_GetSymbolEx(PRTLDRMODINTERNAL pMod, const void *pvBits, RTUINTPTR BaseAddress, 1621 uint32_t iOrdinal, const char *pszSymbol, RTUINTPTR *pValue) 1622 { 1623 PKLDRMODMACHO pThis = RT_FROM_MEMBER(pMod, KLDRMODMACHO, Core); 1624 RT_NOREF(pvBits); 1625 //RT_NOREF(pszVersion); 1626 //RT_NOREF(pfnGetForwarder); 1627 //RT_NOREF(pvUser); 1628 uint32_t fKind = RTLDRSYMKIND_REQ_FLAT; 1629 uint32_t *pfKind = &fKind; 1630 size_t cchSymbol = pszSymbol ? strlen(pszSymbol) : 0; 1682 1631 1683 1632 /* 1684 1633 * Resolve defaults. 1685 1634 */ 1686 rc = kldrModMachOAdjustBaseAddress(pModMachO, &BaseAddress);1635 int rc = kldrModMachOAdjustBaseAddress(pThis, &BaseAddress); 1687 1636 if (rc) 1688 1637 return rc; … … 1692 1641 */ 1693 1642 KLDRMODMACHO_CHECK_RETURN( !pfKind 1694 || (*pfKind & KLDRSYMKIND_REQ_TYPE_MASK) == KLDRSYMKIND_REQ_FLAT,1695 KLDR_ERR_TODO);1643 || (*pfKind & RTLDRSYMKIND_REQ_TYPE_MASK) == RTLDRSYMKIND_REQ_FLAT, 1644 VERR_LDRMACHO_TODO); 1696 1645 1697 1646 /* 1698 1647 * Take action according to file type. 1699 1648 */ 1700 if ( p ModMachO->Hdr.filetype == MH_OBJECT1701 || p ModMachO->Hdr.filetype == MH_EXECUTE /** @todo dylib, execute, dsym: symbols */1702 || p ModMachO->Hdr.filetype == MH_DYLIB1703 || p ModMachO->Hdr.filetype == MH_BUNDLE1704 || p ModMachO->Hdr.filetype == MH_DSYM1705 || p ModMachO->Hdr.filetype == MH_KEXT_BUNDLE)1706 { 1707 rc = kldrModMachOLoadObjSymTab(p ModMachO);1649 if ( pThis->Hdr.filetype == MH_OBJECT 1650 || pThis->Hdr.filetype == MH_EXECUTE /** @todo dylib, execute, dsym: symbols */ 1651 || pThis->Hdr.filetype == MH_DYLIB 1652 || pThis->Hdr.filetype == MH_BUNDLE 1653 || pThis->Hdr.filetype == MH_DSYM 1654 || pThis->Hdr.filetype == MH_KEXT_BUNDLE) 1655 { 1656 rc = kldrModMachOLoadObjSymTab(pThis); 1708 1657 if (!rc) 1709 1658 { 1710 if ( p ModMachO->Hdr.magic == IMAGE_MACHO32_SIGNATURE1711 || p ModMachO->Hdr.magic == IMAGE_MACHO32_SIGNATURE_OE)1712 rc = kldrModMachODoQuerySymbol32Bit(p ModMachO, (macho_nlist_32_t *)pModMachO->pvaSymbols, pModMachO->cSymbols,1713 p ModMachO->pchStrings, pModMachO->cchStrings, BaseAddress, iSymbol, pchSymbol,1714 ( KU32)cchSymbol, puValue, pfKind);1659 if ( pThis->Hdr.magic == IMAGE_MACHO32_SIGNATURE 1660 || pThis->Hdr.magic == IMAGE_MACHO32_SIGNATURE_OE) 1661 rc = kldrModMachODoQuerySymbol32Bit(pThis, (macho_nlist_32_t *)pThis->pvaSymbols, pThis->cSymbols, 1662 pThis->pchStrings, pThis->cchStrings, BaseAddress, iOrdinal, pszSymbol, 1663 (uint32_t)cchSymbol, pValue, pfKind); 1715 1664 else 1716 rc = kldrModMachODoQuerySymbol64Bit(p ModMachO, (macho_nlist_64_t *)pModMachO->pvaSymbols, pModMachO->cSymbols,1717 p ModMachO->pchStrings, pModMachO->cchStrings, BaseAddress, iSymbol, pchSymbol,1718 ( KU32)cchSymbol, puValue, pfKind);1665 rc = kldrModMachODoQuerySymbol64Bit(pThis, (macho_nlist_64_t *)pThis->pvaSymbols, pThis->cSymbols, 1666 pThis->pchStrings, pThis->cchStrings, BaseAddress, iOrdinal, pszSymbol, 1667 (uint32_t)cchSymbol, pValue, pfKind); 1719 1668 } 1720 1669 … … 1725 1674 * before querying symbols, we will ignore the prefix. 1726 1675 */ 1727 if ( rc == KLDR_ERR_SYMBOL_NOT_FOUND1676 if ( rc == VERR_SYMBOL_NOT_FOUND 1728 1677 && cchSymbol > sizeof("section$end$") - 1 1729 && ( p chSymbol[0] == 's'1730 || (p chSymbol[1] == 's' && pchSymbol[0] == '_') )1731 && kHlpMemChr(pchSymbol, '$', cchSymbol) )1732 { 1733 if (p chSymbol[0] == '_')1734 rc = kldrModMachOQueryLinkerSymbol(p ModMachO, pMod, pchSymbol + 1, cchSymbol - 1, BaseAddress, puValue);1678 && ( pszSymbol[0] == 's' 1679 || (pszSymbol[1] == 's' && pszSymbol[0] == '_') ) 1680 && memchr(pszSymbol, '$', cchSymbol) ) 1681 { 1682 if (pszSymbol[0] == '_') 1683 rc = kldrModMachOQueryLinkerSymbol(pThis, pszSymbol + 1, cchSymbol - 1, BaseAddress, pValue); 1735 1684 else 1736 rc = kldrModMachOQueryLinkerSymbol(p ModMachO, pMod, pchSymbol, cchSymbol, BaseAddress, puValue);1685 rc = kldrModMachOQueryLinkerSymbol(pThis, pszSymbol, cchSymbol, BaseAddress, pValue); 1737 1686 } 1738 1687 } 1739 1688 else 1740 rc = KLDR_ERR_TODO;1689 rc = VERR_LDRMACHO_TODO; 1741 1690 1742 1691 return rc; … … 1748 1697 * 1749 1698 * @returns See kLdrModQuerySymbol. 1750 * @param p ModMachO1699 * @param pThis 1751 1700 * @param paSyms Pointer to the symbol table. 1752 1701 * @param cSyms Number of symbols in the table. … … 1760 1709 * @param pfKind See kLdrModQuerySymbol. 1761 1710 */ 1762 static int kldrModMachODoQuerySymbol32Bit(PKLDRMODMACHO p ModMachO, const macho_nlist_32_t *paSyms, KU32cSyms,1763 const char *pchStrings, KU32 cchStrings, KLDRADDR BaseAddress, KU32iSymbol,1764 const char *pchSymbol, KU32 cchSymbol, PKLDRADDR puValue, KU32*pfKind)1711 static int kldrModMachODoQuerySymbol32Bit(PKLDRMODMACHO pThis, const macho_nlist_32_t *paSyms, uint32_t cSyms, 1712 const char *pchStrings, uint32_t cchStrings, RTLDRADDR BaseAddress, uint32_t iSymbol, 1713 const char *pchSymbol, uint32_t cchSymbol, PRTLDRADDR puValue, uint32_t *pfKind) 1765 1714 { 1766 1715 /* 1767 1716 * Find a valid symbol matching the search criteria. 1768 1717 */ 1769 if (iSymbol == NIL_KLDRMOD_SYM_ORDINAL)1718 if (iSymbol == UINT32_MAX) 1770 1719 { 1771 1720 /* simplify validation. */ 1772 1721 if (cchStrings <= cchSymbol) 1773 return KLDR_ERR_SYMBOL_NOT_FOUND;1722 return VERR_SYMBOL_NOT_FOUND; 1774 1723 cchStrings -= cchSymbol; 1775 1724 1776 1725 /* external symbols are usually at the end, so search the other way. */ 1777 for (iSymbol = cSyms - 1; iSymbol != KU32_MAX; iSymbol--)1726 for (iSymbol = cSyms - 1; iSymbol != UINT32_MAX; iSymbol--) 1778 1727 { 1779 1728 const char *psz; … … 1792 1741 if (!paSyms[iSymbol].n_un.n_strx) 1793 1742 continue; 1794 if (( KU32)paSyms[iSymbol].n_un.n_strx >= cchStrings)1743 if ((uint32_t)paSyms[iSymbol].n_un.n_strx >= cchStrings) 1795 1744 continue; 1796 1745 psz = &pchStrings[paSyms[iSymbol].n_un.n_strx]; 1797 1746 if (psz[cchSymbol]) 1798 1747 continue; 1799 if ( kHlpMemComp(psz, pchSymbol, cchSymbol))1748 if (memcmp(psz, pchSymbol, cchSymbol)) 1800 1749 continue; 1801 1750 … … 1803 1752 break; 1804 1753 } 1805 if (iSymbol == KU32_MAX)1806 return KLDR_ERR_SYMBOL_NOT_FOUND;1754 if (iSymbol == UINT32_MAX) 1755 return VERR_SYMBOL_NOT_FOUND; 1807 1756 } 1808 1757 else 1809 1758 { 1810 1759 if (iSymbol >= cSyms) 1811 return KLDR_ERR_SYMBOL_NOT_FOUND;1760 return VERR_SYMBOL_NOT_FOUND; 1812 1761 if (paSyms[iSymbol].n_type & MACHO_N_STAB) 1813 return KLDR_ERR_SYMBOL_NOT_FOUND;1762 return VERR_SYMBOL_NOT_FOUND; 1814 1763 if ((paSyms[iSymbol].n_type & MACHO_N_TYPE) == MACHO_N_UNDF) 1815 return KLDR_ERR_SYMBOL_NOT_FOUND;1764 return VERR_SYMBOL_NOT_FOUND; 1816 1765 } 1817 1766 … … 1821 1770 if (pfKind) 1822 1771 { 1823 if ( p ModMachO->Hdr.magic == IMAGE_MACHO32_SIGNATURE1824 || p ModMachO->Hdr.magic == IMAGE_MACHO32_SIGNATURE_OE)1825 *pfKind = KLDRSYMKIND_32BIT | KLDRSYMKIND_NO_TYPE;1772 if ( pThis->Hdr.magic == IMAGE_MACHO32_SIGNATURE 1773 || pThis->Hdr.magic == IMAGE_MACHO32_SIGNATURE_OE) 1774 *pfKind = RTLDRSYMKIND_32BIT | RTLDRSYMKIND_NO_TYPE; 1826 1775 else 1827 *pfKind = KLDRSYMKIND_64BIT | KLDRSYMKIND_NO_TYPE;1776 *pfKind = RTLDRSYMKIND_64BIT | RTLDRSYMKIND_NO_TYPE; 1828 1777 if (paSyms[iSymbol].n_desc & N_WEAK_DEF) 1829 *pfKind |= KLDRSYMKIND_WEAK;1778 *pfKind |= RTLDRSYMKIND_WEAK; 1830 1779 } 1831 1780 … … 1835 1784 { 1836 1785 PKLDRMODMACHOSECT pSect; 1837 KLDRADDR offSect;1838 KLDRMODMACHO_CHECK_RETURN(( KU32)(paSyms[iSymbol].n_sect - 1) < pModMachO->cSections, KLDR_ERR_MACHO_BAD_SYMBOL);1839 pSect = &p ModMachO->paSections[paSyms[iSymbol].n_sect - 1];1786 RTLDRADDR offSect; 1787 KLDRMODMACHO_CHECK_RETURN((uint32_t)(paSyms[iSymbol].n_sect - 1) < pThis->cSections, VERR_LDRMACHO_BAD_SYMBOL); 1788 pSect = &pThis->paSections[paSyms[iSymbol].n_sect - 1]; 1840 1789 1841 1790 offSect = paSyms[iSymbol].n_value - pSect->LinkAddress; … … 1843 1792 || ( paSyms[iSymbol].n_sect == 1 /* special hack for __mh_execute_header */ 1844 1793 && offSect == 0U - pSect->RVA 1845 && p ModMachO->uEffFileType != MH_OBJECT),1846 KLDR_ERR_MACHO_BAD_SYMBOL);1794 && pThis->uEffFileType != MH_OBJECT), 1795 VERR_LDRMACHO_BAD_SYMBOL); 1847 1796 if (puValue) 1848 1797 *puValue = BaseAddress + pSect->RVA + offSect; … … 1850 1799 if ( pfKind 1851 1800 && (pSect->fFlags & (S_ATTR_PURE_INSTRUCTIONS | S_ATTR_SELF_MODIFYING_CODE))) 1852 *pfKind = (*pfKind & ~ KLDRSYMKIND_TYPE_MASK) | KLDRSYMKIND_CODE;1801 *pfKind = (*pfKind & ~RTLDRSYMKIND_TYPE_MASK) | RTLDRSYMKIND_CODE; 1853 1802 break; 1854 1803 } … … 1858 1807 *puValue = paSyms[iSymbol].n_value; 1859 1808 /*if (pfKind) 1860 pfKind |= KLDRSYMKIND_ABS;*/1809 pfKind |= RTLDRSYMKIND_ABS;*/ 1861 1810 break; 1862 1811 … … 1865 1814 /** @todo implement indirect and prebound symbols. */ 1866 1815 default: 1867 KLDRMODMACHO_FAILED_RETURN( KLDR_ERR_TODO);1816 KLDRMODMACHO_FAILED_RETURN(VERR_LDRMACHO_TODO); 1868 1817 } 1869 1818 … … 1876 1825 * 1877 1826 * @returns See kLdrModQuerySymbol. 1878 * @param p ModMachO1827 * @param pThis 1879 1828 * @param paSyms Pointer to the symbol table. 1880 1829 * @param cSyms Number of symbols in the table. … … 1888 1837 * @param pfKind See kLdrModQuerySymbol. 1889 1838 */ 1890 static int kldrModMachODoQuerySymbol64Bit(PKLDRMODMACHO p ModMachO, const macho_nlist_64_t *paSyms, KU32cSyms,1891 const char *pchStrings, KU32 cchStrings, KLDRADDR BaseAddress, KU32iSymbol,1892 const char *pchSymbol, KU32 cchSymbol, PKLDRADDR puValue, KU32*pfKind)1839 static int kldrModMachODoQuerySymbol64Bit(PKLDRMODMACHO pThis, const macho_nlist_64_t *paSyms, uint32_t cSyms, 1840 const char *pchStrings, uint32_t cchStrings, RTLDRADDR BaseAddress, uint32_t iSymbol, 1841 const char *pchSymbol, uint32_t cchSymbol, PRTLDRADDR puValue, uint32_t *pfKind) 1893 1842 { 1894 1843 /* 1895 1844 * Find a valid symbol matching the search criteria. 1896 1845 */ 1897 if (iSymbol == NIL_KLDRMOD_SYM_ORDINAL)1846 if (iSymbol == UINT32_MAX) 1898 1847 { 1899 1848 /* simplify validation. */ 1900 1849 if (cchStrings <= cchSymbol) 1901 return KLDR_ERR_SYMBOL_NOT_FOUND;1850 return VERR_SYMBOL_NOT_FOUND; 1902 1851 cchStrings -= cchSymbol; 1903 1852 1904 1853 /* external symbols are usually at the end, so search the other way. */ 1905 for (iSymbol = cSyms - 1; iSymbol != KU32_MAX; iSymbol--)1854 for (iSymbol = cSyms - 1; iSymbol != UINT32_MAX; iSymbol--) 1906 1855 { 1907 1856 const char *psz; … … 1920 1869 if (!paSyms[iSymbol].n_un.n_strx) 1921 1870 continue; 1922 if (( KU32)paSyms[iSymbol].n_un.n_strx >= cchStrings)1871 if ((uint32_t)paSyms[iSymbol].n_un.n_strx >= cchStrings) 1923 1872 continue; 1924 1873 psz = &pchStrings[paSyms[iSymbol].n_un.n_strx]; 1925 1874 if (psz[cchSymbol]) 1926 1875 continue; 1927 if ( kHlpMemComp(psz, pchSymbol, cchSymbol))1876 if (memcmp(psz, pchSymbol, cchSymbol)) 1928 1877 continue; 1929 1878 … … 1931 1880 break; 1932 1881 } 1933 if (iSymbol == KU32_MAX)1934 return KLDR_ERR_SYMBOL_NOT_FOUND;1882 if (iSymbol == UINT32_MAX) 1883 return VERR_SYMBOL_NOT_FOUND; 1935 1884 } 1936 1885 else 1937 1886 { 1938 1887 if (iSymbol >= cSyms) 1939 return KLDR_ERR_SYMBOL_NOT_FOUND;1888 return VERR_SYMBOL_NOT_FOUND; 1940 1889 if (paSyms[iSymbol].n_type & MACHO_N_STAB) 1941 return KLDR_ERR_SYMBOL_NOT_FOUND;1890 return VERR_SYMBOL_NOT_FOUND; 1942 1891 if ((paSyms[iSymbol].n_type & MACHO_N_TYPE) == MACHO_N_UNDF) 1943 return KLDR_ERR_SYMBOL_NOT_FOUND;1892 return VERR_SYMBOL_NOT_FOUND; 1944 1893 } 1945 1894 … … 1949 1898 if (pfKind) 1950 1899 { 1951 if ( p ModMachO->Hdr.magic == IMAGE_MACHO32_SIGNATURE1952 || p ModMachO->Hdr.magic == IMAGE_MACHO32_SIGNATURE_OE)1953 *pfKind = KLDRSYMKIND_32BIT | KLDRSYMKIND_NO_TYPE;1900 if ( pThis->Hdr.magic == IMAGE_MACHO32_SIGNATURE 1901 || pThis->Hdr.magic == IMAGE_MACHO32_SIGNATURE_OE) 1902 *pfKind = RTLDRSYMKIND_32BIT | RTLDRSYMKIND_NO_TYPE; 1954 1903 else 1955 *pfKind = KLDRSYMKIND_64BIT | KLDRSYMKIND_NO_TYPE;1904 *pfKind = RTLDRSYMKIND_64BIT | RTLDRSYMKIND_NO_TYPE; 1956 1905 if (paSyms[iSymbol].n_desc & N_WEAK_DEF) 1957 *pfKind |= KLDRSYMKIND_WEAK;1906 *pfKind |= RTLDRSYMKIND_WEAK; 1958 1907 } 1959 1908 … … 1963 1912 { 1964 1913 PKLDRMODMACHOSECT pSect; 1965 KLDRADDR offSect;1966 KLDRMODMACHO_CHECK_RETURN(( KU32)(paSyms[iSymbol].n_sect - 1) < pModMachO->cSections, KLDR_ERR_MACHO_BAD_SYMBOL);1967 pSect = &p ModMachO->paSections[paSyms[iSymbol].n_sect - 1];1914 RTLDRADDR offSect; 1915 KLDRMODMACHO_CHECK_RETURN((uint32_t)(paSyms[iSymbol].n_sect - 1) < pThis->cSections, VERR_LDRMACHO_BAD_SYMBOL); 1916 pSect = &pThis->paSections[paSyms[iSymbol].n_sect - 1]; 1968 1917 1969 1918 offSect = paSyms[iSymbol].n_value - pSect->LinkAddress; … … 1971 1920 || ( paSyms[iSymbol].n_sect == 1 /* special hack for __mh_execute_header */ 1972 1921 && offSect == 0U - pSect->RVA 1973 && p ModMachO->uEffFileType != MH_OBJECT),1974 KLDR_ERR_MACHO_BAD_SYMBOL);1922 && pThis->uEffFileType != MH_OBJECT), 1923 VERR_LDRMACHO_BAD_SYMBOL); 1975 1924 if (puValue) 1976 1925 *puValue = BaseAddress + pSect->RVA + offSect; … … 1978 1927 if ( pfKind 1979 1928 && (pSect->fFlags & (S_ATTR_PURE_INSTRUCTIONS | S_ATTR_SELF_MODIFYING_CODE))) 1980 *pfKind = (*pfKind & ~ KLDRSYMKIND_TYPE_MASK) | KLDRSYMKIND_CODE;1929 *pfKind = (*pfKind & ~RTLDRSYMKIND_TYPE_MASK) | RTLDRSYMKIND_CODE; 1981 1930 break; 1982 1931 } … … 1986 1935 *puValue = paSyms[iSymbol].n_value; 1987 1936 /*if (pfKind) 1988 pfKind |= KLDRSYMKIND_ABS;*/1937 pfKind |= RTLDRSYMKIND_ABS;*/ 1989 1938 break; 1990 1939 … … 1993 1942 /** @todo implement indirect and prebound symbols. */ 1994 1943 default: 1995 KLDRMODMACHO_FAILED_RETURN( KLDR_ERR_TODO);1944 KLDRMODMACHO_FAILED_RETURN(VERR_LDRMACHO_TODO); 1996 1945 } 1997 1946 … … 2000 1949 2001 1950 2002 /** @copydoc kLdrModEnumSymbols */ 2003 static int kldrModMachOEnumSymbols(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, 2004 KU32 fFlags, PFNKLDRMODENUMSYMS pfnCallback, void *pvUser) 2005 { 2006 PKLDRMODMACHO pModMachO = (PKLDRMODMACHO)pMod->pvData; 1951 /** 1952 * @interface_method_impl{RTLDROPS,pfnEnumSymbols} 1953 */ 1954 static DECLCALLBACK(int) rtldrMachO_EnumSymbols(PRTLDRMODINTERNAL pMod, unsigned fFlags, const void *pvBits, 1955 RTUINTPTR BaseAddress, PFNRTLDRENUMSYMS pfnCallback, void *pvUser) 1956 { 1957 PKLDRMODMACHO pThis = RT_FROM_MEMBER(pMod, KLDRMODMACHO, Core); 2007 1958 int rc; 2008 K_NOREF(pvBits);1959 RT_NOREF(pvBits); 2009 1960 2010 1961 /* 2011 1962 * Resolve defaults. 2012 1963 */ 2013 rc = kldrModMachOAdjustBaseAddress(p ModMachO, &BaseAddress);1964 rc = kldrModMachOAdjustBaseAddress(pThis, &BaseAddress); 2014 1965 if (rc) 2015 1966 return rc; … … 2018 1969 * Take action according to file type. 2019 1970 */ 2020 if ( p ModMachO->Hdr.filetype == MH_OBJECT2021 || p ModMachO->Hdr.filetype == MH_EXECUTE /** @todo dylib, execute, dsym: symbols */2022 || p ModMachO->Hdr.filetype == MH_DYLIB2023 || p ModMachO->Hdr.filetype == MH_BUNDLE2024 || p ModMachO->Hdr.filetype == MH_DSYM2025 || p ModMachO->Hdr.filetype == MH_KEXT_BUNDLE)2026 { 2027 rc = kldrModMachOLoadObjSymTab(p ModMachO);1971 if ( pThis->Hdr.filetype == MH_OBJECT 1972 || pThis->Hdr.filetype == MH_EXECUTE /** @todo dylib, execute, dsym: symbols */ 1973 || pThis->Hdr.filetype == MH_DYLIB 1974 || pThis->Hdr.filetype == MH_BUNDLE 1975 || pThis->Hdr.filetype == MH_DSYM 1976 || pThis->Hdr.filetype == MH_KEXT_BUNDLE) 1977 { 1978 rc = kldrModMachOLoadObjSymTab(pThis); 2028 1979 if (!rc) 2029 1980 { 2030 if ( p ModMachO->Hdr.magic == IMAGE_MACHO32_SIGNATURE2031 || p ModMachO->Hdr.magic == IMAGE_MACHO32_SIGNATURE_OE)2032 rc = kldrModMachODoEnumSymbols32Bit(p ModMachO, (macho_nlist_32_t *)pModMachO->pvaSymbols, pModMachO->cSymbols,2033 p ModMachO->pchStrings, pModMachO->cchStrings, BaseAddress,1981 if ( pThis->Hdr.magic == IMAGE_MACHO32_SIGNATURE 1982 || pThis->Hdr.magic == IMAGE_MACHO32_SIGNATURE_OE) 1983 rc = kldrModMachODoEnumSymbols32Bit(pThis, (macho_nlist_32_t *)pThis->pvaSymbols, pThis->cSymbols, 1984 pThis->pchStrings, pThis->cchStrings, BaseAddress, 2034 1985 fFlags, pfnCallback, pvUser); 2035 1986 else 2036 rc = kldrModMachODoEnumSymbols64Bit(p ModMachO, (macho_nlist_64_t *)pModMachO->pvaSymbols, pModMachO->cSymbols,2037 p ModMachO->pchStrings, pModMachO->cchStrings, BaseAddress,1987 rc = kldrModMachODoEnumSymbols64Bit(pThis, (macho_nlist_64_t *)pThis->pvaSymbols, pThis->cSymbols, 1988 pThis->pchStrings, pThis->cchStrings, BaseAddress, 2038 1989 fFlags, pfnCallback, pvUser); 2039 1990 } 2040 1991 } 2041 1992 else 2042 KLDRMODMACHO_FAILED_RETURN( KLDR_ERR_TODO);1993 KLDRMODMACHO_FAILED_RETURN(VERR_LDRMACHO_TODO); 2043 1994 2044 1995 return rc; … … 2050 2001 * 2051 2002 * @returns See kLdrModQuerySymbol. 2052 * @param p ModMachO2003 * @param pThis 2053 2004 * @param paSyms Pointer to the symbol table. 2054 2005 * @param cSyms Number of symbols in the table. … … 2060 2011 * @param pvUser See kLdrModEnumSymbols. 2061 2012 */ 2062 static int kldrModMachODoEnumSymbols32Bit(PKLDRMODMACHO p ModMachO, const macho_nlist_32_t *paSyms, KU32cSyms,2063 const char *pchStrings, KU32 cchStrings, KLDRADDR BaseAddress,2064 KU32 fFlags, PFNKLDRMODENUMSYMS pfnCallback, void *pvUser)2065 { 2066 const KU32 fKindBase = pModMachO->Hdr.magic == IMAGE_MACHO32_SIGNATURE2067 || p ModMachO->Hdr.magic == IMAGE_MACHO32_SIGNATURE_OE2068 ? KLDRSYMKIND_32BIT : KLDRSYMKIND_64BIT;2069 KU32iSym;2013 static int kldrModMachODoEnumSymbols32Bit(PKLDRMODMACHO pThis, const macho_nlist_32_t *paSyms, uint32_t cSyms, 2014 const char *pchStrings, uint32_t cchStrings, RTLDRADDR BaseAddress, 2015 uint32_t fFlags, PFNRTLDRENUMSYMS pfnCallback, void *pvUser) 2016 { 2017 const uint32_t fKindBase = pThis->Hdr.magic == IMAGE_MACHO32_SIGNATURE 2018 || pThis->Hdr.magic == IMAGE_MACHO32_SIGNATURE_OE 2019 ? RTLDRSYMKIND_32BIT : RTLDRSYMKIND_64BIT; 2020 uint32_t iSym; 2070 2021 int rc; 2071 2022 … … 2075 2026 for (iSym = 0; iSym < cSyms; iSym++) 2076 2027 { 2077 KU32fKind;2078 KLDRADDR uValue;2028 uint32_t fKind; 2029 RTLDRADDR uValue; 2079 2030 const char *psz; 2080 KSIZEcch;2031 size_t cch; 2081 2032 2082 2033 /* Skip debug symbols and undefined symbols. */ … … 2087 2038 2088 2039 /* Skip non-public symbols unless they are requested explicitly. */ 2089 if (!(fFlags & KLDRMOD_ENUM_SYMS_FLAGS_ALL))2040 if (!(fFlags & RTLDR_ENUM_SYMBOL_FLAGS_ALL)) 2090 2041 { 2091 2042 if (!(paSyms[iSym].n_type & MACHO_N_EXT)) /*??*/ … … 2102 2053 2103 2054 /* name */ 2104 KLDRMODMACHO_CHECK_RETURN(( KU32)paSyms[iSym].n_un.n_strx < cchStrings, KLDR_ERR_MACHO_BAD_SYMBOL);2055 KLDRMODMACHO_CHECK_RETURN((uint32_t)paSyms[iSym].n_un.n_strx < cchStrings, VERR_LDRMACHO_BAD_SYMBOL); 2105 2056 psz = &pchStrings[paSyms[iSym].n_un.n_strx]; 2106 cch = kHlpStrLen(psz);2057 cch = strlen(psz); 2107 2058 if (!cch) 2108 2059 psz = NULL; … … 2111 2062 fKind = fKindBase; 2112 2063 if (paSyms[iSym].n_desc & N_WEAK_DEF) 2113 fKind |= KLDRSYMKIND_WEAK;2064 fKind |= RTLDRSYMKIND_WEAK; 2114 2065 switch (paSyms[iSym].n_type & MACHO_N_TYPE) 2115 2066 { … … 2117 2068 { 2118 2069 PKLDRMODMACHOSECT pSect; 2119 KLDRMODMACHO_CHECK_RETURN(( KU32)(paSyms[iSym].n_sect - 1) < pModMachO->cSections, KLDR_ERR_MACHO_BAD_SYMBOL);2120 pSect = &p ModMachO->paSections[paSyms[iSym].n_sect - 1];2070 KLDRMODMACHO_CHECK_RETURN((uint32_t)(paSyms[iSym].n_sect - 1) < pThis->cSections, VERR_LDRMACHO_BAD_SYMBOL); 2071 pSect = &pThis->paSections[paSyms[iSym].n_sect - 1]; 2121 2072 2122 2073 uValue = paSyms[iSym].n_value - pSect->LinkAddress; … … 2124 2075 || ( paSyms[iSym].n_sect == 1 /* special hack for __mh_execute_header */ 2125 2076 && uValue == 0U - pSect->RVA 2126 && p ModMachO->uEffFileType != MH_OBJECT),2127 KLDR_ERR_MACHO_BAD_SYMBOL);2077 && pThis->uEffFileType != MH_OBJECT), 2078 VERR_LDRMACHO_BAD_SYMBOL); 2128 2079 uValue += BaseAddress + pSect->RVA; 2129 2080 2130 2081 if (pSect->fFlags & (S_ATTR_PURE_INSTRUCTIONS | S_ATTR_SELF_MODIFYING_CODE)) 2131 fKind |= KLDRSYMKIND_CODE;2082 fKind |= RTLDRSYMKIND_CODE; 2132 2083 else 2133 fKind |= KLDRSYMKIND_NO_TYPE;2084 fKind |= RTLDRSYMKIND_NO_TYPE; 2134 2085 break; 2135 2086 } … … 2137 2088 case MACHO_N_ABS: 2138 2089 uValue = paSyms[iSym].n_value; 2139 fKind |= KLDRSYMKIND_NO_TYPE /*KLDRSYMKIND_ABS*/;2090 fKind |= RTLDRSYMKIND_NO_TYPE /*RTLDRSYMKIND_ABS*/; 2140 2091 break; 2141 2092 … … 2144 2095 /** @todo implement indirect and prebound symbols. */ 2145 2096 default: 2146 KLDRMODMACHO_FAILED_RETURN( KLDR_ERR_TODO);2097 KLDRMODMACHO_FAILED_RETURN(VERR_LDRMACHO_TODO); 2147 2098 } 2148 2099 … … 2150 2101 * Do callback. 2151 2102 */ 2152 rc = pfnCallback( pModMachO->pMod, iSym, psz, cch, NULL, uValue, fKind, pvUser);2103 rc = pfnCallback(&pThis->Core, psz, iSym, uValue/*, fKind*/, pvUser); 2153 2104 if (rc) 2154 2105 return rc; … … 2162 2113 * 2163 2114 * @returns See kLdrModQuerySymbol. 2164 * @param p ModMachO2115 * @param pThis 2165 2116 * @param paSyms Pointer to the symbol table. 2166 2117 * @param cSyms Number of symbols in the table. … … 2172 2123 * @param pvUser See kLdrModEnumSymbols. 2173 2124 */ 2174 static int kldrModMachODoEnumSymbols64Bit(PKLDRMODMACHO p ModMachO, const macho_nlist_64_t *paSyms, KU32cSyms,2175 const char *pchStrings, KU32 cchStrings, KLDRADDR BaseAddress,2176 KU32 fFlags, PFNKLDRMODENUMSYMS pfnCallback, void *pvUser)2177 { 2178 const KU32 fKindBase = pModMachO->Hdr.magic == IMAGE_MACHO64_SIGNATURE2179 || p ModMachO->Hdr.magic == IMAGE_MACHO64_SIGNATURE_OE2180 ? KLDRSYMKIND_64BIT : KLDRSYMKIND_32BIT;2181 KU32iSym;2125 static int kldrModMachODoEnumSymbols64Bit(PKLDRMODMACHO pThis, const macho_nlist_64_t *paSyms, uint32_t cSyms, 2126 const char *pchStrings, uint32_t cchStrings, RTLDRADDR BaseAddress, 2127 uint32_t fFlags, PFNRTLDRENUMSYMS pfnCallback, void *pvUser) 2128 { 2129 const uint32_t fKindBase = pThis->Hdr.magic == IMAGE_MACHO64_SIGNATURE 2130 || pThis->Hdr.magic == IMAGE_MACHO64_SIGNATURE_OE 2131 ? RTLDRSYMKIND_64BIT : RTLDRSYMKIND_32BIT; 2132 uint32_t iSym; 2182 2133 int rc; 2183 2134 … … 2187 2138 for (iSym = 0; iSym < cSyms; iSym++) 2188 2139 { 2189 KU32fKind;2190 KLDRADDR uValue;2140 uint32_t fKind; 2141 RTLDRADDR uValue; 2191 2142 const char *psz; 2192 KSIZEcch;2143 size_t cch; 2193 2144 2194 2145 /* Skip debug symbols and undefined symbols. */ … … 2199 2150 2200 2151 /* Skip non-public symbols unless they are requested explicitly. */ 2201 if (!(fFlags & KLDRMOD_ENUM_SYMS_FLAGS_ALL))2152 if (!(fFlags & RTLDR_ENUM_SYMBOL_FLAGS_ALL)) 2202 2153 { 2203 2154 if (!(paSyms[iSym].n_type & MACHO_N_EXT)) /*??*/ … … 2214 2165 2215 2166 /* name */ 2216 KLDRMODMACHO_CHECK_RETURN(( KU32)paSyms[iSym].n_un.n_strx < cchStrings, KLDR_ERR_MACHO_BAD_SYMBOL);2167 KLDRMODMACHO_CHECK_RETURN((uint32_t)paSyms[iSym].n_un.n_strx < cchStrings, VERR_LDRMACHO_BAD_SYMBOL); 2217 2168 psz = &pchStrings[paSyms[iSym].n_un.n_strx]; 2218 cch = kHlpStrLen(psz);2169 cch = strlen(psz); 2219 2170 if (!cch) 2220 2171 psz = NULL; … … 2223 2174 fKind = fKindBase; 2224 2175 if (paSyms[iSym].n_desc & N_WEAK_DEF) 2225 fKind |= KLDRSYMKIND_WEAK;2176 fKind |= RTLDRSYMKIND_WEAK; 2226 2177 switch (paSyms[iSym].n_type & MACHO_N_TYPE) 2227 2178 { … … 2229 2180 { 2230 2181 PKLDRMODMACHOSECT pSect; 2231 KLDRMODMACHO_CHECK_RETURN(( KU32)(paSyms[iSym].n_sect - 1) < pModMachO->cSections, KLDR_ERR_MACHO_BAD_SYMBOL);2232 pSect = &p ModMachO->paSections[paSyms[iSym].n_sect - 1];2182 KLDRMODMACHO_CHECK_RETURN((uint32_t)(paSyms[iSym].n_sect - 1) < pThis->cSections, VERR_LDRMACHO_BAD_SYMBOL); 2183 pSect = &pThis->paSections[paSyms[iSym].n_sect - 1]; 2233 2184 2234 2185 uValue = paSyms[iSym].n_value - pSect->LinkAddress; … … 2236 2187 || ( paSyms[iSym].n_sect == 1 /* special hack for __mh_execute_header */ 2237 2188 && uValue == 0U - pSect->RVA 2238 && p ModMachO->uEffFileType != MH_OBJECT),2239 KLDR_ERR_MACHO_BAD_SYMBOL);2189 && pThis->uEffFileType != MH_OBJECT), 2190 VERR_LDRMACHO_BAD_SYMBOL); 2240 2191 uValue += BaseAddress + pSect->RVA; 2241 2192 2242 2193 if (pSect->fFlags & (S_ATTR_PURE_INSTRUCTIONS | S_ATTR_SELF_MODIFYING_CODE)) 2243 fKind |= KLDRSYMKIND_CODE;2194 fKind |= RTLDRSYMKIND_CODE; 2244 2195 else 2245 fKind |= KLDRSYMKIND_NO_TYPE;2196 fKind |= RTLDRSYMKIND_NO_TYPE; 2246 2197 break; 2247 2198 } … … 2249 2200 case MACHO_N_ABS: 2250 2201 uValue = paSyms[iSym].n_value; 2251 fKind |= KLDRSYMKIND_NO_TYPE /*KLDRSYMKIND_ABS*/;2202 fKind |= RTLDRSYMKIND_NO_TYPE /*RTLDRSYMKIND_ABS*/; 2252 2203 break; 2253 2204 … … 2256 2207 /** @todo implement indirect and prebound symbols. */ 2257 2208 default: 2258 KLDRMODMACHO_FAILED_RETURN( KLDR_ERR_TODO);2209 KLDRMODMACHO_FAILED_RETURN(VERR_LDRMACHO_TODO); 2259 2210 } 2260 2211 … … 2262 2213 * Do callback. 2263 2214 */ 2264 rc = pfnCallback( pModMachO->pMod, iSym, psz, cch, NULL, uValue, fKind, pvUser);2215 rc = pfnCallback(&pThis->Core, psz, iSym, uValue/*, fKind*/, pvUser); 2265 2216 if (rc) 2266 2217 return rc; … … 2269 2220 } 2270 2221 2222 #if 0 2271 2223 2272 2224 /** @copydoc kLdrModGetImport */ 2273 static int kldrModMachOGetImport(P KLDRMOD pMod, const void *pvBits, KU32 iImport, char *pszName, KSIZEcchName)2274 { 2275 PKLDRMODMACHO p ModMachO = (PKLDRMODMACHO)pMod->pvData;2276 K_NOREF(pvBits);2277 K_NOREF(iImport);2278 K_NOREF(pszName);2279 K_NOREF(cchName);2280 2281 if (p ModMachO->Hdr.filetype == MH_OBJECT)2225 static int kldrModMachOGetImport(PRTLDRMODINTERNAL pMod, const void *pvBits, uint32_t iImport, char *pszName, size_t cchName) 2226 { 2227 PKLDRMODMACHO pThis = RT_FROM_MEMBER(pMod, KLDRMODMACHO, Core); 2228 RT_NOREF(pvBits); 2229 RT_NOREF(iImport); 2230 RT_NOREF(pszName); 2231 RT_NOREF(cchName); 2232 2233 if (pThis->Hdr.filetype == MH_OBJECT) 2282 2234 return KLDR_ERR_IMPORT_ORDINAL_OUT_OF_BOUNDS; 2283 2235 … … 2287 2239 2288 2240 2241 2289 2242 /** @copydoc kLdrModNumberOfImports */ 2290 static KI32 kldrModMachONumberOfImports(PKLDRMODpMod, const void *pvBits)2291 { 2292 PKLDRMODMACHO p ModMachO = (PKLDRMODMACHO)pMod->pvData;2293 K_NOREF(pvBits);2294 2295 if (p ModMachO->Hdr.filetype == MH_OBJECT)2243 static int32_t kldrModMachONumberOfImports(PRTLDRMODINTERNAL pMod, const void *pvBits) 2244 { 2245 PKLDRMODMACHO pThis = RT_FROM_MEMBER(pMod, KLDRMODMACHO, Core); 2246 RT_NOREF(pvBits); 2247 2248 if (pThis->Hdr.filetype == MH_OBJECT) 2296 2249 return 0; 2297 2250 … … 2302 2255 2303 2256 /** @copydoc kLdrModGetStackInfo */ 2304 static int kldrModMachOGetStackInfo(P KLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, PKLDRSTACKINFO pStackInfo)2305 { 2306 /*PKLDRMODMACHO p ModMachO = (PKLDRMODMACHO)pMod->pvData;*/2307 K_NOREF(pMod);2308 K_NOREF(pvBits);2309 K_NOREF(BaseAddress);2310 2311 pStackInfo->Address = NIL_ KLDRADDR;2312 pStackInfo->LinkAddress = NIL_ KLDRADDR;2257 static int kldrModMachOGetStackInfo(PRTLDRMODINTERNAL pMod, const void *pvBits, RTLDRADDR BaseAddress, PKLDRSTACKINFO pStackInfo) 2258 { 2259 /*PKLDRMODMACHO pThis = RT_FROM_MEMBER(pMod, KLDRMODMACHO, Core);*/ 2260 RT_NOREF(pMod); 2261 RT_NOREF(pvBits); 2262 RT_NOREF(BaseAddress); 2263 2264 pStackInfo->Address = NIL_RTLDRADDR; 2265 pStackInfo->LinkAddress = NIL_RTLDRADDR; 2313 2266 pStackInfo->cbStack = pStackInfo->cbStackThread = 0; 2314 2267 /* later */ … … 2317 2270 } 2318 2271 2272 #endif 2273 2319 2274 2320 2275 /** @copydoc kLdrModQueryMainEntrypoint */ 2321 static int kldrModMachOQueryMainEntrypoint(P KLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, PKLDRADDR pMainEPAddress)2276 static int kldrModMachOQueryMainEntrypoint(PRTLDRMODINTERNAL pMod, const void *pvBits, RTLDRADDR BaseAddress, PRTLDRADDR pMainEPAddress) 2322 2277 { 2323 2278 #if 0 2324 PKLDRMODMACHO p ModMachO = (PKLDRMODMACHO)pMod->pvData;2279 PKLDRMODMACHO pThis = RT_FROM_MEMBER(pMod, KLDRMODMACHO, Core); 2325 2280 int rc; 2326 2281 … … 2328 2283 * Resolve base address alias if any. 2329 2284 */ 2330 rc = kldrModMachOBitsAndBaseAddress(p ModMachO, NULL, &BaseAddress);2285 rc = kldrModMachOBitsAndBaseAddress(pThis, NULL, &BaseAddress); 2331 2286 if (rc) 2332 2287 return rc; … … 2335 2290 * Convert the address from the header. 2336 2291 */ 2337 *pMainEPAddress = p ModMachO->Hdrs.OptionalHeader.AddressOfEntryPoint2338 ? BaseAddress + p ModMachO->Hdrs.OptionalHeader.AddressOfEntryPoint2339 : NIL_ KLDRADDR;2292 *pMainEPAddress = pThis->Hdrs.OptionalHeader.AddressOfEntryPoint 2293 ? BaseAddress + pThis->Hdrs.OptionalHeader.AddressOfEntryPoint 2294 : NIL_RTLDRADDR; 2340 2295 #else 2341 *pMainEPAddress = NIL_ KLDRADDR;2342 K_NOREF(pvBits);2343 K_NOREF(BaseAddress);2344 K_NOREF(pMod);2296 *pMainEPAddress = NIL_RTLDRADDR; 2297 RT_NOREF(pvBits); 2298 RT_NOREF(BaseAddress); 2299 RT_NOREF(pMod); 2345 2300 #endif 2346 2301 return 0; … … 2349 2304 2350 2305 /** @copydoc kLdrModQueryImageUuid */ 2351 static int kldrModMachOQueryImageUuid(PKLDRMOD pMod, const void *pvBits, void *pvUuid, KSIZE cbUuid) 2352 { 2353 PKLDRMODMACHO pModMachO = (PKLDRMODMACHO)pMod->pvData; 2354 K_NOREF(pvBits); 2355 2356 kHlpMemSet(pvUuid, 0, cbUuid); 2357 if (kHlpMemComp(pvUuid, pModMachO->abImageUuid, sizeof(pModMachO->abImageUuid)) == 0) 2358 return KLDR_ERR_NO_IMAGE_UUID; 2359 2360 kHlpMemCopy(pvUuid, pModMachO->abImageUuid, sizeof(pModMachO->abImageUuid)); 2361 return 0; 2362 } 2363 2364 2365 /** @copydoc kLdrModEnumDbgInfo */ 2366 static int kldrModMachOEnumDbgInfo(PKLDRMOD pMod, const void *pvBits, PFNKLDRENUMDBG pfnCallback, void *pvUser) 2367 { 2368 PKLDRMODMACHO pModMachO = (PKLDRMODMACHO)pMod->pvData; 2369 int rc = 0; 2370 KU32 iSect; 2371 K_NOREF(pvBits); 2372 2373 for (iSect = 0; iSect < pModMachO->cSections; iSect++) 2374 { 2375 section_32_t *pMachOSect = pModMachO->paSections[iSect].pvMachoSection; /* (32-bit & 64-bit starts the same way) */ 2306 static int kldrModMachOQueryImageUuid(PKLDRMODMACHO pThis, const void *pvBits, void *pvUuid, size_t cbUuid) 2307 { 2308 RT_NOREF(pvBits); 2309 2310 memset(pvUuid, 0, cbUuid); 2311 if (memcmp(pvUuid, pThis->abImageUuid, sizeof(pThis->abImageUuid)) == 0) 2312 return VERR_NOT_FOUND; 2313 2314 memcpy(pvUuid, pThis->abImageUuid, sizeof(pThis->abImageUuid)); 2315 return VINF_SUCCESS; 2316 } 2317 2318 2319 /** 2320 * @interface_method_impl{RTLDROPS,pfnEnumDbgInfo} 2321 */ 2322 static DECLCALLBACK(int) rtldrMachO_EnumDbgInfo(PRTLDRMODINTERNAL pMod, const void *pvBits, PFNRTLDRENUMDBG pfnCallback, void *pvUser) 2323 { 2324 PKLDRMODMACHO pThis = RT_FROM_MEMBER(pMod, KLDRMODMACHO, Core); 2325 int rc = VINF_SUCCESS; 2326 uint32_t iSect; 2327 RT_NOREF(pvBits); 2328 2329 for (iSect = 0; iSect < pThis->cSections; iSect++) 2330 { 2331 /* (32-bit & 64-bit starts the same way) */ 2332 section_32_t *pMachOSect = (section_32_t *)pThis->paSections[iSect].pvMachoSection; 2376 2333 char szTmp[sizeof(pMachOSect->sectname) + 1]; 2377 2334 2378 if ( kHlpStrComp(pMachOSect->segname, "__DWARF"))2335 if (strcmp(pMachOSect->segname, "__DWARF")) 2379 2336 continue; 2380 2337 2381 kHlpMemCopy(szTmp, pMachOSect->sectname, sizeof(pMachOSect->sectname));2338 memcpy(szTmp, pMachOSect->sectname, sizeof(pMachOSect->sectname)); 2382 2339 szTmp[sizeof(pMachOSect->sectname)] = '\0'; 2383 2340 2384 rc = pfnCallback(pMod, iSect, KLDRDBGINFOTYPE_DWARF, 0, 0, szTmp, 2385 pModMachO->paSections[iSect].offFile, 2386 pModMachO->paSections[iSect].LinkAddress, 2387 pModMachO->paSections[iSect].cb, 2388 NULL, pvUser); 2389 if (rc != 0) 2341 RTLDRDBGINFO DbgInfo; 2342 DbgInfo.enmType = RTLDRDBGINFOTYPE_DWARF; 2343 DbgInfo.iDbgInfo = iSect; 2344 DbgInfo.LinkAddress = pThis->paSections[iSect].LinkAddress; 2345 DbgInfo.cb = pThis->paSections[iSect].cb; 2346 DbgInfo.pszExtFile = NULL; 2347 DbgInfo.u.Dwarf.pszSection = szTmp; 2348 rc = pfnCallback(&pThis->Core, &DbgInfo, pvUser); 2349 if (rc != VINF_SUCCESS) 2390 2350 break; 2391 2351 } … … 2394 2354 } 2395 2355 2356 #if 0 2396 2357 2397 2358 /** @copydoc kLdrModHasDbgInfo */ 2398 static int kldrModMachOHasDbgInfo(P KLDRMODpMod, const void *pvBits)2399 { 2400 /*PKLDRMODMACHO p ModMachO = (PKLDRMODMACHO)pMod->pvData;*/2359 static int kldrModMachOHasDbgInfo(PRTLDRMODINTERNAL pMod, const void *pvBits) 2360 { 2361 /*PKLDRMODMACHO pThis = RT_FROM_MEMBER(pMod, KLDRMODMACHO, Core);*/ 2401 2362 2402 2363 #if 0 … … 2404 2365 * Base this entirely on the presence of a debug directory. 2405 2366 */ 2406 if ( p ModMachO->Hdrs.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG].Size2367 if ( pThis->Hdrs.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG].Size 2407 2368 < sizeof(IMAGE_DEBUG_DIRECTORY) /* screw borland linkers */ 2408 || !p ModMachO->Hdrs.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress)2369 || !pThis->Hdrs.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress) 2409 2370 return KLDR_ERR_NO_DEBUG_INFO; 2410 2371 return 0; 2411 2372 #else 2412 K_NOREF(pMod);2413 K_NOREF(pvBits);2414 return KLDR_ERR_NO_DEBUG_INFO;2373 RT_NOREF(pMod); 2374 RT_NOREF(pvBits); 2375 return VERR_LDR_NO_DEBUG_INFO; 2415 2376 #endif 2416 2377 } … … 2418 2379 2419 2380 /** @copydoc kLdrModMap */ 2420 static int kldrModMachOMap(P KLDRMODpMod)2421 { 2422 PKLDRMODMACHO p ModMachO = (PKLDRMODMACHO)pMod->pvData;2381 static int kldrModMachOMap(PRTLDRMODINTERNAL pMod) 2382 { 2383 PKLDRMODMACHO pThis = RT_FROM_MEMBER(pMod, KLDRMODMACHO, Core); 2423 2384 unsigned fFixed; 2424 KU32i;2385 uint32_t i; 2425 2386 void *pvBase; 2426 2387 int rc; 2427 2388 2428 if (!p ModMachO->fCanLoad)2429 return KLDR_ERR_TODO;2389 if (!pThis->fCanLoad) 2390 return VERR_LDRMACHO_TODO; 2430 2391 2431 2392 /* 2432 2393 * Already mapped? 2433 2394 */ 2434 if (p ModMachO->pvMapping)2395 if (pThis->pvMapping) 2435 2396 return KLDR_ERR_ALREADY_MAPPED; 2436 2397 … … 2445 2406 else 2446 2407 { 2447 pvBase = (void *)( KUPTR)pMod->aSegments[0].LinkAddress;2448 if (( KUPTR)pvBase != pMod->aSegments[0].LinkAddress)2449 return KLDR_ERR_ADDRESS_OVERFLOW;2408 pvBase = (void *)(uintptr_t)pMod->aSegments[0].LinkAddress; 2409 if ((uintptr_t)pvBase != pMod->aSegments[0].LinkAddress) 2410 return VERR_LDR_ADDRESS_OVERFLOW; 2450 2411 } 2451 2412 … … 2460 2421 for (i = 0; i < pMod->cSegments; i++) 2461 2422 { 2462 if (pMod->aSegments[i].RVA != NIL_ KLDRADDR)2463 pMod->aSegments[i].MapAddress = ( KUPTR)pvBase + (KUPTR)pMod->aSegments[i].RVA;2464 } 2465 p ModMachO->pvMapping = pvBase;2423 if (pMod->aSegments[i].RVA != NIL_RTLDRADDR) 2424 pMod->aSegments[i].MapAddress = (uintptr_t)pvBase + (uintptr_t)pMod->aSegments[i].RVA; 2425 } 2426 pThis->pvMapping = pvBase; 2466 2427 2467 2428 return 0; … … 2470 2431 2471 2432 /** @copydoc kLdrModUnmap */ 2472 static int kldrModMachOUnmap(P KLDRMODpMod)2473 { 2474 PKLDRMODMACHO p ModMachO = (PKLDRMODMACHO)pMod->pvData;2475 KU32i;2433 static int kldrModMachOUnmap(PRTLDRMODINTERNAL pMod) 2434 { 2435 PKLDRMODMACHO pThis = RT_FROM_MEMBER(pMod, KLDRMODMACHO, Core); 2436 uint32_t i; 2476 2437 int rc; 2477 2438 … … 2479 2440 * Mapped? 2480 2441 */ 2481 if (!p ModMachO->pvMapping)2442 if (!pThis->pvMapping) 2482 2443 return KLDR_ERR_NOT_MAPPED; 2483 2444 … … 2485 2446 * Try unmap the image. 2486 2447 */ 2487 rc = kRdrUnmap(pMod->pRdr, p ModMachO->pvMapping, pMod->cSegments, pMod->aSegments);2448 rc = kRdrUnmap(pMod->pRdr, pThis->pvMapping, pMod->cSegments, pMod->aSegments); 2488 2449 if (rc) 2489 2450 return rc; … … 2492 2453 * Update the segments to reflect that they aren't mapped any longer. 2493 2454 */ 2494 p ModMachO->pvMapping = NULL;2455 pThis->pvMapping = NULL; 2495 2456 for (i = 0; i < pMod->cSegments; i++) 2496 2457 pMod->aSegments[i].MapAddress = 0; … … 2501 2462 2502 2463 /** @copydoc kLdrModAllocTLS */ 2503 static int kldrModMachOAllocTLS(P KLDRMODpMod, void *pvMapping)2504 { 2505 PKLDRMODMACHO p ModMachO = (PKLDRMODMACHO)pMod->pvData;2464 static int kldrModMachOAllocTLS(PRTLDRMODINTERNAL pMod, void *pvMapping) 2465 { 2466 PKLDRMODMACHO pThis = RT_FROM_MEMBER(pMod, KLDRMODMACHO, Core); 2506 2467 2507 2468 /* … … 2509 2470 */ 2510 2471 if ( pvMapping == KLDRMOD_INT_MAP 2511 && !p ModMachO->pvMapping )2472 && !pThis->pvMapping ) 2512 2473 return KLDR_ERR_NOT_MAPPED; 2513 2474 return 0; … … 2516 2477 2517 2478 /** @copydoc kLdrModFreeTLS */ 2518 static void kldrModMachOFreeTLS(PKLDRMOD pMod, void *pvMapping) 2519 { 2520 K_NOREF(pMod); 2521 K_NOREF(pvMapping); 2522 } 2479 static void kldrModMachOFreeTLS(PRTLDRMODINTERNAL pMod, void *pvMapping) 2480 { 2481 RT_NOREF(pMod); 2482 RT_NOREF(pvMapping); 2483 } 2484 2523 2485 2524 2486 2525 2487 /** @copydoc kLdrModReload */ 2526 static int kldrModMachOReload(P KLDRMODpMod)2527 { 2528 PKLDRMODMACHO p ModMachO = (PKLDRMODMACHO)pMod->pvData;2488 static int kldrModMachOReload(PRTLDRMODINTERNAL pMod) 2489 { 2490 PKLDRMODMACHO pThis = RT_FROM_MEMBER(pMod, KLDRMODMACHO, Core); 2529 2491 2530 2492 /* 2531 2493 * Mapped? 2532 2494 */ 2533 if (!p ModMachO->pvMapping)2495 if (!pThis->pvMapping) 2534 2496 return KLDR_ERR_NOT_MAPPED; 2535 2497 2536 2498 /* the file provider does it all */ 2537 return kRdrRefresh(pMod->pRdr, p ModMachO->pvMapping, pMod->cSegments, pMod->aSegments);2499 return kRdrRefresh(pMod->pRdr, pThis->pvMapping, pMod->cSegments, pMod->aSegments); 2538 2500 } 2539 2501 2540 2502 2541 2503 /** @copydoc kLdrModFixupMapping */ 2542 static int kldrModMachOFixupMapping(P KLDRMOD pMod, PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser)2543 { 2544 PKLDRMODMACHO p ModMachO = (PKLDRMODMACHO)pMod->pvData;2504 static int kldrModMachOFixupMapping(PRTLDRMODINTERNAL pMod, PFNRTLDRIMPORT pfnGetImport, void *pvUser) 2505 { 2506 PKLDRMODMACHO pThis = RT_FROM_MEMBER(pMod, KLDRMODMACHO, Core); 2545 2507 int rc, rc2; 2546 2508 … … 2548 2510 * Mapped? 2549 2511 */ 2550 if (!p ModMachO->pvMapping)2512 if (!pThis->pvMapping) 2551 2513 return KLDR_ERR_NOT_MAPPED; 2552 2514 … … 2554 2516 * Before doing anything we'll have to make all pages writable. 2555 2517 */ 2556 rc = kRdrProtect(pMod->pRdr, p ModMachO->pvMapping, pMod->cSegments, pMod->aSegments, 1 /* unprotect */);2518 rc = kRdrProtect(pMod->pRdr, pThis->pvMapping, pMod->cSegments, pMod->aSegments, 1 /* unprotect */); 2557 2519 if (rc) 2558 2520 return rc; … … 2561 2523 * Resolve imports and apply base relocations. 2562 2524 */ 2563 rc = kldrModMachORelocateBits(pMod, pModMachO->pvMapping, (KUPTR)pModMachO->pvMapping, pModMachO->LinkAddress,2564 pfnGetImport, pvUser);2525 rc = rtldrMachO_RelocateBits(pMod, pThis->pvMapping, (uintptr_t)pThis->pvMapping, pThis->LinkAddress, 2526 pfnGetImport, pvUser); 2565 2527 2566 2528 /* 2567 2529 * Restore protection. 2568 2530 */ 2569 rc2 = kRdrProtect(pMod->pRdr, p ModMachO->pvMapping, pMod->cSegments, pMod->aSegments, 0 /* protect */);2531 rc2 = kRdrProtect(pMod->pRdr, pThis->pvMapping, pMod->cSegments, pMod->aSegments, 0 /* protect */); 2570 2532 if (!rc && rc2) 2571 2533 rc = rc2; … … 2573 2535 } 2574 2536 2537 #endif 2575 2538 2576 2539 /** … … 2578 2541 * 2579 2542 * @returns 0 on success, non-zero kLdr status code on failure. 2580 * @param p ModMachOThe Mach-O module interpreter instance.2543 * @param pThis The Mach-O module interpreter instance. 2581 2544 * @param pfnGetImport The callback for resolving an imported symbol. 2582 2545 * @param pvUser User argument to the callback. 2583 2546 */ 2584 static int kldrModMachOObjDoImports(PKLDRMODMACHO p ModMachO, KLDRADDR BaseAddress, PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser)2585 { 2586 const KU32 cSyms = pModMachO->cSymbols;2587 KU32iSym;2547 static int kldrModMachOObjDoImports(PKLDRMODMACHO pThis, RTLDRADDR BaseAddress, PFNRTLDRIMPORT pfnGetImport, void *pvUser) 2548 { 2549 const uint32_t cSyms = pThis->cSymbols; 2550 uint32_t iSym; 2588 2551 int rc; 2589 2552 … … 2591 2554 * Ensure that we've got the symbol table and section fixups handy. 2592 2555 */ 2593 rc = kldrModMachOLoadObjSymTab(p ModMachO);2556 rc = kldrModMachOLoadObjSymTab(pThis); 2594 2557 if (rc) 2595 2558 return rc; … … 2599 2562 * We currently ignore REFERENCE_TYPE. 2600 2563 */ 2601 if ( p ModMachO->Hdr.magic == IMAGE_MACHO32_SIGNATURE2602 || p ModMachO->Hdr.magic == IMAGE_MACHO32_SIGNATURE_OE)2603 { 2604 macho_nlist_32_t *paSyms = (macho_nlist_32_t *)p ModMachO->pvaSymbols;2564 if ( pThis->Hdr.magic == IMAGE_MACHO32_SIGNATURE 2565 || pThis->Hdr.magic == IMAGE_MACHO32_SIGNATURE_OE) 2566 { 2567 macho_nlist_32_t *paSyms = (macho_nlist_32_t *)pThis->pvaSymbols; 2605 2568 for (iSym = 0; iSym < cSyms; iSym++) 2606 2569 { … … 2612 2575 { 2613 2576 const char *pszSymbol; 2614 KSIZEcchSymbol;2615 KU32 fKind = KLDRSYMKIND_REQ_FLAT;2616 KLDRADDR Value = NIL_KLDRADDR;2577 size_t cchSymbol; 2578 //uint32_t fKind = RTLDRSYMKIND_REQ_FLAT; 2579 RTLDRADDR Value = NIL_RTLDRADDR; 2617 2580 2618 2581 /** @todo Implement N_REF_TO_WEAK. */ 2619 KLDRMODMACHO_CHECK_RETURN(!(paSyms[iSym].n_desc & N_REF_TO_WEAK), KLDR_ERR_TODO);2582 KLDRMODMACHO_CHECK_RETURN(!(paSyms[iSym].n_desc & N_REF_TO_WEAK), VERR_LDRMACHO_TODO); 2620 2583 2621 2584 /* Get the symbol name. */ 2622 KLDRMODMACHO_CHECK_RETURN(( KU32)paSyms[iSym].n_un.n_strx < pModMachO->cchStrings, KLDR_ERR_MACHO_BAD_SYMBOL);2623 pszSymbol = &p ModMachO->pchStrings[paSyms[iSym].n_un.n_strx];2624 cchSymbol = kHlpStrLen(pszSymbol);2585 KLDRMODMACHO_CHECK_RETURN((uint32_t)paSyms[iSym].n_un.n_strx < pThis->cchStrings, VERR_LDRMACHO_BAD_SYMBOL); 2586 pszSymbol = &pThis->pchStrings[paSyms[iSym].n_un.n_strx]; 2587 cchSymbol = strlen(pszSymbol); 2625 2588 2626 2589 /* Check for linker defined symbols relating to sections and segments. */ 2627 2590 if ( cchSymbol > sizeof("section$end$") - 1 2628 2591 && *pszSymbol == 's' 2629 && kHlpMemChr(pszSymbol, '$', cchSymbol))2630 rc = kldrModMachOQueryLinkerSymbol(p ModMachO, pModMachO->pMod, pszSymbol, cchSymbol, BaseAddress, &Value);2592 && memchr(pszSymbol, '$', cchSymbol)) 2593 rc = kldrModMachOQueryLinkerSymbol(pThis, pszSymbol, cchSymbol, BaseAddress, &Value); 2631 2594 else 2632 rc = KLDR_ERR_SYMBOL_NOT_FOUND;2595 rc = VERR_SYMBOL_NOT_FOUND; 2633 2596 2634 2597 /* Ask the user for an address to the symbol. */ 2635 2598 if (rc) 2636 rc = pfnGetImport(pModMachO->pMod, NIL_KLDRMOD_IMPORT, iSym, pszSymbol, cchSymbol, NULL, 2637 &Value, &fKind, pvUser); 2599 rc = pfnGetImport(&pThis->Core, NULL /*pszModule*/, pszSymbol, iSym, &Value/*, &fKind*/, pvUser); 2638 2600 if (rc) 2639 2601 { … … 2645 2607 2646 2608 /* Update the symbol. */ 2647 paSyms[iSym].n_value = ( KU32)Value;2609 paSyms[iSym].n_value = (uint32_t)Value; 2648 2610 if (paSyms[iSym].n_value != Value) 2649 2611 { 2650 rc = KLDR_ERR_ADDRESS_OVERFLOW;2612 rc = VERR_LDR_ADDRESS_OVERFLOW; 2651 2613 break; 2652 2614 } … … 2655 2617 { 2656 2618 /** @todo implement weak symbols. */ 2657 /*return KLDR_ERR_TODO; - ignored for now. */2619 /*return VERR_LDRMACHO_TODO; - ignored for now. */ 2658 2620 } 2659 2621 } … … 2662 2624 { 2663 2625 /* (Identical to the 32-bit code, just different paSym type. (and n_strx is unsigned)) */ 2664 macho_nlist_64_t *paSyms = (macho_nlist_64_t *)p ModMachO->pvaSymbols;2626 macho_nlist_64_t *paSyms = (macho_nlist_64_t *)pThis->pvaSymbols; 2665 2627 for (iSym = 0; iSym < cSyms; iSym++) 2666 2628 { … … 2672 2634 { 2673 2635 const char *pszSymbol; 2674 KSIZEcchSymbol;2675 KU32 fKind = KLDRSYMKIND_REQ_FLAT;2676 KLDRADDR Value = NIL_KLDRADDR;2636 size_t cchSymbol; 2637 //uint32_t fKind = RTLDRSYMKIND_REQ_FLAT; 2638 RTLDRADDR Value = NIL_RTLDRADDR; 2677 2639 2678 2640 /** @todo Implement N_REF_TO_WEAK. */ 2679 KLDRMODMACHO_CHECK_RETURN(!(paSyms[iSym].n_desc & N_REF_TO_WEAK), KLDR_ERR_TODO);2641 KLDRMODMACHO_CHECK_RETURN(!(paSyms[iSym].n_desc & N_REF_TO_WEAK), VERR_LDRMACHO_TODO); 2680 2642 2681 2643 /* Get the symbol name. */ 2682 KLDRMODMACHO_CHECK_RETURN(paSyms[iSym].n_un.n_strx < p ModMachO->cchStrings, KLDR_ERR_MACHO_BAD_SYMBOL);2683 pszSymbol = &p ModMachO->pchStrings[paSyms[iSym].n_un.n_strx];2684 cchSymbol = kHlpStrLen(pszSymbol);2644 KLDRMODMACHO_CHECK_RETURN(paSyms[iSym].n_un.n_strx < pThis->cchStrings, VERR_LDRMACHO_BAD_SYMBOL); 2645 pszSymbol = &pThis->pchStrings[paSyms[iSym].n_un.n_strx]; 2646 cchSymbol = strlen(pszSymbol); 2685 2647 2686 2648 /* Check for linker defined symbols relating to sections and segments. */ 2687 2649 if ( cchSymbol > sizeof("section$end$") - 1 2688 2650 && *pszSymbol == 's' 2689 && kHlpMemChr(pszSymbol, '$', cchSymbol))2690 rc = kldrModMachOQueryLinkerSymbol(p ModMachO, pModMachO->pMod, pszSymbol, cchSymbol, BaseAddress, &Value);2651 && memchr(pszSymbol, '$', cchSymbol)) 2652 rc = kldrModMachOQueryLinkerSymbol(pThis, pszSymbol, cchSymbol, BaseAddress, &Value); 2691 2653 else 2692 rc = KLDR_ERR_SYMBOL_NOT_FOUND;2654 rc = VERR_SYMBOL_NOT_FOUND; 2693 2655 2694 2656 /* Ask the user for an address to the symbol. */ 2695 2657 if (rc) 2696 rc = pfnGetImport(pModMachO->pMod, NIL_KLDRMOD_IMPORT, iSym, pszSymbol, cchSymbol, NULL, 2697 &Value, &fKind, pvUser); 2658 rc = pfnGetImport(&pThis->Core, NULL, pszSymbol, iSym, &Value, /*&fKind,*/ pvUser); 2698 2659 if (rc) 2699 2660 { … … 2708 2669 if (paSyms[iSym].n_value != Value) 2709 2670 { 2710 rc = KLDR_ERR_ADDRESS_OVERFLOW;2671 rc = VERR_LDR_ADDRESS_OVERFLOW; 2711 2672 break; 2712 2673 } … … 2715 2676 { 2716 2677 /** @todo implement weak symbols. */ 2717 /*return KLDR_ERR_TODO; - ignored for now. */2678 /*return VERR_LDRMACHO_TODO; - ignored for now. */ 2718 2679 } 2719 2680 } … … 2728 2689 * 2729 2690 * @returns 0 on success, non-zero kLdr status code on failure. 2730 * @param p ModMachOThe Mach-O module interpreter instance.2691 * @param pThis The Mach-O module interpreter instance. 2731 2692 * @param pvMapping The mapping to fixup. 2732 2693 * @param NewBaseAddress The address to fixup the mapping to. 2733 2694 * @param OldBaseAddress The address the mapping is currently fixed up to. 2734 2695 */ 2735 static int kldrModMachOObjDoFixups(PKLDRMODMACHO p ModMachO, void *pvMapping, KLDRADDR NewBaseAddress)2736 { 2737 KU32iSeg;2696 static int kldrModMachOObjDoFixups(PKLDRMODMACHO pThis, void *pvMapping, RTLDRADDR NewBaseAddress) 2697 { 2698 uint32_t iSeg; 2738 2699 int rc; 2739 2700 … … 2742 2703 * Ensure that we've got the symbol table and section fixups handy. 2743 2704 */ 2744 rc = kldrModMachOLoadObjSymTab(p ModMachO);2705 rc = kldrModMachOLoadObjSymTab(pThis); 2745 2706 if (rc) 2746 2707 return rc; … … 2749 2710 * Iterate over the segments and their sections and apply fixups. 2750 2711 */ 2751 for (iSeg = rc = 0; !rc && iSeg < p ModMachO->pMod->cSegments; iSeg++)2752 { 2753 PKLDRMODMACHOSEG pSeg = &p ModMachO->aSegments[iSeg];2754 KU32iSect;2712 for (iSeg = rc = 0; !rc && iSeg < pThis->cSegments; iSeg++) 2713 { 2714 PKLDRMODMACHOSEG pSeg = &pThis->aSegments[iSeg]; 2715 uint32_t iSect; 2755 2716 2756 2717 for (iSect = 0; iSect < pSeg->cSections; iSect++) 2757 2718 { 2758 2719 PKLDRMODMACHOSECT pSect = &pSeg->paSections[iSect]; 2759 KU8*pbSectBits;2720 uint8_t *pbSectBits; 2760 2721 2761 2722 /* skip sections without fixups. */ … … 2766 2727 if (!pSect->paFixups) 2767 2728 { 2768 rc = kldrModMachOLoadFixups(p ModMachO, pSect->offFixups, pSect->cFixups, &pSect->paFixups);2729 rc = kldrModMachOLoadFixups(pThis, pSect->offFixups, pSect->cFixups, &pSect->paFixups); 2769 2730 if (rc) 2770 2731 break; … … 2774 2735 * Apply the fixups. 2775 2736 */ 2776 pbSectBits = ( KU8 *)pvMapping + (KUPTR)pSect->RVA;2777 if (p ModMachO->Hdr.magic == IMAGE_MACHO32_SIGNATURE) /** @todo this aint right. */2778 rc = kldrModMachOFixupSectionGeneric32Bit(p ModMachO, pbSectBits, pSect,2779 (macho_nlist_32_t *)p ModMachO->pvaSymbols,2780 p ModMachO->cSymbols, NewBaseAddress);2781 else if ( p ModMachO->Hdr.magic == IMAGE_MACHO64_SIGNATURE2782 && p ModMachO->Hdr.cputype == CPU_TYPE_X86_64)2783 rc = kldrModMachOFixupSectionAMD64(p ModMachO, pbSectBits, pSect,2784 (macho_nlist_64_t *)p ModMachO->pvaSymbols,2785 p ModMachO->cSymbols, NewBaseAddress);2737 pbSectBits = (uint8_t *)pvMapping + (uintptr_t)pSect->RVA; 2738 if (pThis->Hdr.magic == IMAGE_MACHO32_SIGNATURE) /** @todo this aint right. */ 2739 rc = kldrModMachOFixupSectionGeneric32Bit(pThis, pbSectBits, pSect, 2740 (macho_nlist_32_t *)pThis->pvaSymbols, 2741 pThis->cSymbols, NewBaseAddress); 2742 else if ( pThis->Hdr.magic == IMAGE_MACHO64_SIGNATURE 2743 && pThis->Hdr.cputype == CPU_TYPE_X86_64) 2744 rc = kldrModMachOFixupSectionAMD64(pThis, pbSectBits, pSect, 2745 (macho_nlist_64_t *)pThis->pvaSymbols, 2746 pThis->cSymbols, NewBaseAddress); 2786 2747 else 2787 KLDRMODMACHO_FAILED_RETURN( KLDR_ERR_TODO);2748 KLDRMODMACHO_FAILED_RETURN(VERR_LDRMACHO_TODO); 2788 2749 if (rc) 2789 2750 break; … … 2800 2761 * 2801 2762 * @returns 0 on success, non-zero kLdr status code on failure. 2802 * @param p ModMachOThe Mach-O module interpreter instance.2763 * @param pThis The Mach-O module interpreter instance. 2803 2764 * @param pbSectBits Pointer to the section bits. 2804 2765 * @param pFixupSect The section being fixed up. 2805 2766 * @param NewBaseAddress The new base image address. 2806 2767 */ 2807 static int kldrModMachOFixupSectionGeneric32Bit(PKLDRMODMACHO p ModMachO, KU8*pbSectBits, PKLDRMODMACHOSECT pFixupSect,2808 macho_nlist_32_t *paSyms, KU32 cSyms, KLDRADDR NewBaseAddress)2768 static int kldrModMachOFixupSectionGeneric32Bit(PKLDRMODMACHO pThis, uint8_t *pbSectBits, PKLDRMODMACHOSECT pFixupSect, 2769 macho_nlist_32_t *paSyms, uint32_t cSyms, RTLDRADDR NewBaseAddress) 2809 2770 { 2810 2771 const macho_relocation_info_t *paFixups = pFixupSect->paFixups; 2811 const KU32 cFixups = pFixupSect->cFixups; 2812 KSIZE cbSectBits = (KSIZE)pFixupSect->cb; 2813 const KU8 *pbSectVirginBits; 2814 KU32 iFixup; 2815 KLDRPU uFixVirgin; 2816 KLDRPU uFix; 2817 KLDRADDR SymAddr = ~(KLDRADDR)0; 2772 const uint32_t cFixups = pFixupSect->cFixups; 2773 size_t cbSectBits = (size_t)pFixupSect->cb; 2774 const uint8_t *pbSectVirginBits; 2775 uint32_t iFixup; 2776 RTLDRADDR SymAddr = ~(RTLDRADDR)0; 2818 2777 int rc; 2819 2778 … … 2823 2782 if (pFixupSect->offFile != -1) 2824 2783 { 2825 rc = kldrModMachOMapVirginBits(p ModMachO);2784 rc = kldrModMachOMapVirginBits(pThis); 2826 2785 if (rc) 2827 2786 return rc; 2828 pbSectVirginBits = (const KU8 *)pModMachO->pvBits + pFixupSect->offFile;2787 pbSectVirginBits = (const uint8_t *)pThis->pvBits + pFixupSect->offFile; 2829 2788 } 2830 2789 else … … 2836 2795 for (iFixup = 0; iFixup < cFixups; iFixup++) 2837 2796 { 2797 RTPTRUNION uFix; 2798 RTPTRUNION uFixVirgin; 2838 2799 union 2839 2800 { … … 2846 2807 { 2847 2808 /* sanity */ 2848 if (( KU32)Fixup.r.r_address >= cbSectBits)2849 return KLDR_ERR_BAD_FIXUP;2809 if ((uint32_t)Fixup.r.r_address >= cbSectBits) 2810 return VERR_LDR_BAD_FIXUP; 2850 2811 2851 2812 /* calc fixup addresses. */ 2852 2813 uFix.pv = pbSectBits + Fixup.r.r_address; 2853 uFixVirgin.pv = pbSectVirginBits ? ( KU8*)pbSectVirginBits + Fixup.r.r_address : 0;2814 uFixVirgin.pv = pbSectVirginBits ? (uint8_t *)pbSectVirginBits + Fixup.r.r_address : 0; 2854 2815 2855 2816 /* … … 2873 2834 const macho_nlist_32_t *pSym; 2874 2835 if (Fixup.r.r_symbolnum >= cSyms) 2875 return KLDR_ERR_BAD_FIXUP;2836 return VERR_LDR_BAD_FIXUP; 2876 2837 pSym = &paSyms[Fixup.r.r_symbolnum]; 2877 2838 2878 2839 if (pSym->n_type & MACHO_N_STAB) 2879 return KLDR_ERR_BAD_FIXUP;2840 return VERR_LDR_BAD_FIXUP; 2880 2841 2881 2842 switch (pSym->n_type & MACHO_N_TYPE) … … 2884 2845 { 2885 2846 PKLDRMODMACHOSECT pSymSect; 2886 KLDRMODMACHO_CHECK_RETURN(( KU32)pSym->n_sect - 1 <= pModMachO->cSections, KLDR_ERR_MACHO_BAD_SYMBOL);2887 pSymSect = &p ModMachO->paSections[pSym->n_sect - 1];2847 KLDRMODMACHO_CHECK_RETURN((uint32_t)pSym->n_sect - 1 <= pThis->cSections, VERR_LDRMACHO_BAD_SYMBOL); 2848 pSymSect = &pThis->paSections[pSym->n_sect - 1]; 2888 2849 2889 2850 SymAddr += pSym->n_value - pSymSect->LinkAddress + pSymSect->RVA + NewBaseAddress; … … 2898 2859 case MACHO_N_INDR: 2899 2860 case MACHO_N_PBUD: 2900 KLDRMODMACHO_FAILED_RETURN( KLDR_ERR_TODO);2861 KLDRMODMACHO_FAILED_RETURN(VERR_LDRMACHO_TODO); 2901 2862 default: 2902 KLDRMODMACHO_FAILED_RETURN( KLDR_ERR_MACHO_BAD_SYMBOL);2863 KLDRMODMACHO_FAILED_RETURN(VERR_LDRMACHO_BAD_SYMBOL); 2903 2864 } 2904 2865 } … … 2906 2867 { 2907 2868 PKLDRMODMACHOSECT pSymSect; 2908 if (Fixup.r.r_symbolnum > p ModMachO->cSections)2909 return KLDR_ERR_BAD_FIXUP;2910 pSymSect = &p ModMachO->paSections[Fixup.r.r_symbolnum - 1];2869 if (Fixup.r.r_symbolnum > pThis->cSections) 2870 return VERR_LDR_BAD_FIXUP; 2871 pSymSect = &pThis->paSections[Fixup.r.r_symbolnum - 1]; 2911 2872 2912 2873 SymAddr -= pSymSect->LinkAddress; … … 2921 2882 { 2922 2883 PKLDRMODMACHOSECT pSymSect; 2923 KU32iSymSect;2924 KLDRADDR Value;2884 uint32_t iSymSect; 2885 RTLDRADDR Value; 2925 2886 2926 2887 /* sanity */ 2927 2888 KLDRMODMACHO_ASSERT(Fixup.s.r_scattered); 2928 if (( KU32)Fixup.s.r_address >= cbSectBits)2929 return KLDR_ERR_BAD_FIXUP;2889 if ((uint32_t)Fixup.s.r_address >= cbSectBits) 2890 return VERR_LDR_BAD_FIXUP; 2930 2891 2931 2892 /* calc fixup addresses. */ 2932 2893 uFix.pv = pbSectBits + Fixup.s.r_address; 2933 uFixVirgin.pv = pbSectVirginBits ? ( KU8*)pbSectVirginBits + Fixup.s.r_address : 0;2894 uFixVirgin.pv = pbSectVirginBits ? (uint8_t *)pbSectVirginBits + Fixup.s.r_address : 0; 2934 2895 2935 2896 /* … … 2951 2912 /* Find the section number from the r_value. */ 2952 2913 pSymSect = NULL; 2953 for (iSymSect = 0; iSymSect < p ModMachO->cSections; iSymSect++)2914 for (iSymSect = 0; iSymSect < pThis->cSections; iSymSect++) 2954 2915 { 2955 KLDRADDR off = Value - pModMachO->paSections[iSymSect].LinkAddress;2956 if (off < p ModMachO->paSections[iSymSect].cb)2916 RTLDRADDR off = Value - pThis->paSections[iSymSect].LinkAddress; 2917 if (off < pThis->paSections[iSymSect].cb) 2957 2918 { 2958 pSymSect = &p ModMachO->paSections[iSymSect];2919 pSymSect = &pThis->paSections[iSymSect]; 2959 2920 break; 2960 2921 } 2961 else if (off == p ModMachO->paSections[iSymSect].cb) /* edge case */2962 pSymSect = &p ModMachO->paSections[iSymSect];2922 else if (off == pThis->paSections[iSymSect].cb) /* edge case */ 2923 pSymSect = &pThis->paSections[iSymSect]; 2963 2924 } 2964 2925 if (!pSymSect) 2965 return KLDR_ERR_BAD_FIXUP;2926 return VERR_LDR_BAD_FIXUP; 2966 2927 2967 2928 /* Calc the symbol address. */ … … 2981 2942 switch (Fixup.r.r_length) 2982 2943 { 2983 case 0: *uFix.pu8 = ( KU8)SymAddr; break;2984 case 1: *uFix.pu16 = ( KU16)SymAddr; break;2985 case 2: *uFix.pu32 = ( KU32)SymAddr; break;2986 case 3: *uFix.pu64 = ( KU64)SymAddr; break;2944 case 0: *uFix.pu8 = (uint8_t)SymAddr; break; 2945 case 1: *uFix.pu16 = (uint16_t)SymAddr; break; 2946 case 2: *uFix.pu32 = (uint32_t)SymAddr; break; 2947 case 3: *uFix.pu64 = (uint64_t)SymAddr; break; 2987 2948 } 2988 2949 } 2989 2950 else if (Fixup.r.r_type <= GENERIC_RELOC_LOCAL_SECTDIFF) 2990 return KLDR_ERR_MACHO_UNSUPPORTED_FIXUP_TYPE;2951 return VERR_LDRMACHO_UNSUPPORTED_FIXUP_TYPE; 2991 2952 else 2992 return KLDR_ERR_BAD_FIXUP;2953 return VERR_LDR_BAD_FIXUP; 2993 2954 } 2994 2955 … … 3001 2962 * 3002 2963 * @returns 0 on success, non-zero kLdr status code on failure. 3003 * @param p ModMachOThe Mach-O module interpreter instance.2964 * @param pThis The Mach-O module interpreter instance. 3004 2965 * @param pbSectBits Pointer to the section bits. 3005 2966 * @param pFixupSect The section being fixed up. 3006 2967 * @param NewBaseAddress The new base image address. 3007 2968 */ 3008 static int kldrModMachOFixupSectionAMD64(PKLDRMODMACHO p ModMachO, KU8*pbSectBits, PKLDRMODMACHOSECT pFixupSect,3009 macho_nlist_64_t *paSyms, KU32 cSyms, KLDRADDR NewBaseAddress)2969 static int kldrModMachOFixupSectionAMD64(PKLDRMODMACHO pThis, uint8_t *pbSectBits, PKLDRMODMACHOSECT pFixupSect, 2970 macho_nlist_64_t *paSyms, uint32_t cSyms, RTLDRADDR NewBaseAddress) 3010 2971 { 3011 2972 const macho_relocation_info_t *paFixups = pFixupSect->paFixups; 3012 const KU32 cFixups = pFixupSect->cFixups; 3013 KSIZE cbSectBits = (KSIZE)pFixupSect->cb; 3014 const KU8 *pbSectVirginBits; 3015 KU32 iFixup; 3016 KLDRPU uFixVirgin; 3017 KLDRPU uFix; 3018 KLDRADDR SymAddr; 2973 const uint32_t cFixups = pFixupSect->cFixups; 2974 size_t cbSectBits = (size_t)pFixupSect->cb; 2975 const uint8_t *pbSectVirginBits; 2976 uint32_t iFixup; 2977 RTLDRADDR SymAddr; 3019 2978 int rc; 3020 2979 … … 3024 2983 if (pFixupSect->offFile != -1) 3025 2984 { 3026 rc = kldrModMachOMapVirginBits(p ModMachO);2985 rc = kldrModMachOMapVirginBits(pThis); 3027 2986 if (rc) 3028 2987 return rc; 3029 pbSectVirginBits = (const KU8 *)pModMachO->pvBits + pFixupSect->offFile;2988 pbSectVirginBits = (const uint8_t *)pThis->pvBits + pFixupSect->offFile; 3030 2989 } 3031 2990 else … … 3045 3004 3046 3005 /* AMD64 doesn't use scattered fixups. */ 3047 KLDRMODMACHO_CHECK_RETURN(!(Fixup.r.r_address & R_SCATTERED), KLDR_ERR_BAD_FIXUP);3006 KLDRMODMACHO_CHECK_RETURN(!(Fixup.r.r_address & R_SCATTERED), VERR_LDR_BAD_FIXUP); 3048 3007 3049 3008 /* sanity */ 3050 KLDRMODMACHO_CHECK_RETURN(( KU32)Fixup.r.r_address < cbSectBits, KLDR_ERR_BAD_FIXUP);3009 KLDRMODMACHO_CHECK_RETURN((uint32_t)Fixup.r.r_address < cbSectBits, VERR_LDR_BAD_FIXUP); 3051 3010 3052 3011 /* calc fixup addresses. */ 3012 RTPTRUNION uFix; 3053 3013 uFix.pv = pbSectBits + Fixup.r.r_address; 3054 uFixVirgin.pv = pbSectVirginBits ? (KU8 *)pbSectVirginBits + Fixup.r.r_address : 0; 3014 RTPTRUNION uFixVirgin; 3015 uFixVirgin.pv = pbSectVirginBits ? (uint8_t *)pbSectVirginBits + Fixup.r.r_address : 0; 3055 3016 3056 3017 /* … … 3064 3025 case 3: SymAddr = *uFixVirgin.pi64; break; 3065 3026 default: 3066 KLDRMODMACHO_FAILED_RETURN( KLDR_ERR_BAD_FIXUP);3027 KLDRMODMACHO_FAILED_RETURN(VERR_LDR_BAD_FIXUP); 3067 3028 } 3068 3029 … … 3072 3033 const macho_nlist_64_t *pSym; 3073 3034 3074 KLDRMODMACHO_CHECK_RETURN(Fixup.r.r_symbolnum < cSyms, KLDR_ERR_BAD_FIXUP);3035 KLDRMODMACHO_CHECK_RETURN(Fixup.r.r_symbolnum < cSyms, VERR_LDR_BAD_FIXUP); 3075 3036 pSym = &paSyms[Fixup.r.r_symbolnum]; 3076 KLDRMODMACHO_CHECK_RETURN(!(pSym->n_type & MACHO_N_STAB), KLDR_ERR_BAD_FIXUP);3037 KLDRMODMACHO_CHECK_RETURN(!(pSym->n_type & MACHO_N_STAB), VERR_LDR_BAD_FIXUP); 3077 3038 3078 3039 switch (Fixup.r.r_type) … … 3090 3051 case MACHO_N_INDR: 3091 3052 case MACHO_N_PBUD: 3092 KLDRMODMACHO_FAILED_RETURN( KLDR_ERR_TODO);3053 KLDRMODMACHO_FAILED_RETURN(VERR_LDRMACHO_TODO); 3093 3054 default: 3094 KLDRMODMACHO_FAILED_RETURN( KLDR_ERR_MACHO_BAD_SYMBOL);3055 KLDRMODMACHO_FAILED_RETURN(VERR_LDRMACHO_BAD_SYMBOL); 3095 3056 } 3096 SymAddr = sizeof( KU64) * Fixup.r.r_symbolnum + pModMachO->GotRVA + NewBaseAddress;3097 KLDRMODMACHO_CHECK_RETURN(Fixup.r.r_length == 2, KLDR_ERR_BAD_FIXUP);3057 SymAddr = sizeof(uint64_t) * Fixup.r.r_symbolnum + pThis->GotRVA + NewBaseAddress; 3058 KLDRMODMACHO_CHECK_RETURN(Fixup.r.r_length == 2, VERR_LDR_BAD_FIXUP); 3098 3059 SymAddr -= 4; 3099 3060 break; … … 3105 3066 case X86_64_RELOC_SIGNED_2: 3106 3067 case X86_64_RELOC_SIGNED_4: 3107 KLDRMODMACHO_CHECK_RETURN(Fixup.r.r_pcrel, KLDR_ERR_BAD_FIXUP);3068 KLDRMODMACHO_CHECK_RETURN(Fixup.r.r_pcrel, VERR_LDR_BAD_FIXUP); 3108 3069 /* Falls through. */ 3109 3070 default: … … 3113 3074 { 3114 3075 case X86_64_RELOC_UNSIGNED: 3115 KLDRMODMACHO_CHECK_RETURN(!Fixup.r.r_pcrel, KLDR_ERR_BAD_FIXUP);3076 KLDRMODMACHO_CHECK_RETURN(!Fixup.r.r_pcrel, VERR_LDR_BAD_FIXUP); 3116 3077 break; 3117 3078 case X86_64_RELOC_BRANCH: 3118 KLDRMODMACHO_CHECK_RETURN(Fixup.r.r_length == 2, KLDR_ERR_BAD_FIXUP);3079 KLDRMODMACHO_CHECK_RETURN(Fixup.r.r_length == 2, VERR_LDR_BAD_FIXUP); 3119 3080 SymAddr -= 4; 3120 3081 break; … … 3126 3087 break; 3127 3088 default: 3128 KLDRMODMACHO_FAILED_RETURN( KLDR_ERR_BAD_FIXUP);3089 KLDRMODMACHO_FAILED_RETURN(VERR_LDR_BAD_FIXUP); 3129 3090 } 3130 3091 … … 3134 3095 { 3135 3096 PKLDRMODMACHOSECT pSymSect; 3136 KLDRMODMACHO_CHECK_RETURN(( KU32)pSym->n_sect - 1 <= pModMachO->cSections, KLDR_ERR_MACHO_BAD_SYMBOL);3137 pSymSect = &p ModMachO->paSections[pSym->n_sect - 1];3097 KLDRMODMACHO_CHECK_RETURN((uint32_t)pSym->n_sect - 1 <= pThis->cSections, VERR_LDRMACHO_BAD_SYMBOL); 3098 pSymSect = &pThis->paSections[pSym->n_sect - 1]; 3138 3099 SymAddr += pSym->n_value - pSymSect->LinkAddress + pSymSect->RVA + NewBaseAddress; 3139 3100 break; … … 3145 3106 && SymAddr + Fixup.r.r_address + pFixupSect->RVA + NewBaseAddress 3146 3107 - pSym->n_value 3147 + KU64_C(0x80000000)3148 >= KU64_C(0xffffff20))3149 SymAddr += p ModMachO->cbJmpStub * Fixup.r.r_symbolnum + pModMachO->JmpStubsRVA + NewBaseAddress;3108 + UINT64_C(0x80000000) 3109 >= UINT64_C(0xffffff20)) 3110 SymAddr += pThis->cbJmpStub * Fixup.r.r_symbolnum + pThis->JmpStubsRVA + NewBaseAddress; 3150 3111 else 3151 3112 SymAddr += pSym->n_value; … … 3158 3119 case MACHO_N_INDR: 3159 3120 case MACHO_N_PBUD: 3160 KLDRMODMACHO_FAILED_RETURN( KLDR_ERR_TODO);3121 KLDRMODMACHO_FAILED_RETURN(VERR_LDRMACHO_TODO); 3161 3122 default: 3162 KLDRMODMACHO_FAILED_RETURN( KLDR_ERR_MACHO_BAD_SYMBOL);3123 KLDRMODMACHO_FAILED_RETURN(VERR_LDRMACHO_BAD_SYMBOL); 3163 3124 } 3164 3125 break; … … 3178 3139 { 3179 3140 PKLDRMODMACHOSECT pSymSect; 3180 KLDRMODMACHO_CHECK_RETURN(( KU32)pSym->n_sect - 1 <= pModMachO->cSections, KLDR_ERR_MACHO_BAD_SYMBOL);3181 pSymSect = &p ModMachO->paSections[pSym->n_sect - 1];3141 KLDRMODMACHO_CHECK_RETURN((uint32_t)pSym->n_sect - 1 <= pThis->cSections, VERR_LDRMACHO_BAD_SYMBOL); 3142 pSymSect = &pThis->paSections[pSym->n_sect - 1]; 3182 3143 SymAddr -= pSym->n_value - pSymSect->LinkAddress + pSymSect->RVA + NewBaseAddress; 3183 3144 break; … … 3191 3152 case MACHO_N_INDR: 3192 3153 case MACHO_N_PBUD: 3193 KLDRMODMACHO_FAILED_RETURN( KLDR_ERR_TODO);3154 KLDRMODMACHO_FAILED_RETURN(VERR_LDRMACHO_TODO); 3194 3155 default: 3195 KLDRMODMACHO_FAILED_RETURN( KLDR_ERR_MACHO_BAD_SYMBOL);3156 KLDRMODMACHO_FAILED_RETURN(VERR_LDRMACHO_BAD_SYMBOL); 3196 3157 } 3197 3158 3198 3159 /* Load the 2nd fixup, check sanity. */ 3199 3160 iFixup++; 3200 KLDRMODMACHO_CHECK_RETURN(!Fixup.r.r_pcrel && iFixup < cFixups, KLDR_ERR_BAD_FIXUP);3161 KLDRMODMACHO_CHECK_RETURN(!Fixup.r.r_pcrel && iFixup < cFixups, VERR_LDR_BAD_FIXUP); 3201 3162 Fixup2 = paFixups[iFixup]; 3202 3163 KLDRMODMACHO_CHECK_RETURN( Fixup2.r_address == Fixup.r.r_address … … 3205 3166 && !Fixup2.r_pcrel 3206 3167 && Fixup2.r_symbolnum < cSyms, 3207 KLDR_ERR_BAD_FIXUP);3168 VERR_LDR_BAD_FIXUP); 3208 3169 3209 3170 if (Fixup2.r_extern) 3210 3171 { 3211 KLDRMODMACHO_CHECK_RETURN(Fixup2.r_symbolnum < cSyms, KLDR_ERR_BAD_FIXUP);3172 KLDRMODMACHO_CHECK_RETURN(Fixup2.r_symbolnum < cSyms, VERR_LDR_BAD_FIXUP); 3212 3173 pSym = &paSyms[Fixup2.r_symbolnum]; 3213 KLDRMODMACHO_CHECK_RETURN(!(pSym->n_type & MACHO_N_STAB), KLDR_ERR_BAD_FIXUP);3174 KLDRMODMACHO_CHECK_RETURN(!(pSym->n_type & MACHO_N_STAB), VERR_LDR_BAD_FIXUP); 3214 3175 3215 3176 /* Add it's value to SymAddr. */ … … 3219 3180 { 3220 3181 PKLDRMODMACHOSECT pSymSect; 3221 KLDRMODMACHO_CHECK_RETURN(( KU32)pSym->n_sect - 1 <= pModMachO->cSections, KLDR_ERR_MACHO_BAD_SYMBOL);3222 pSymSect = &p ModMachO->paSections[pSym->n_sect - 1];3182 KLDRMODMACHO_CHECK_RETURN((uint32_t)pSym->n_sect - 1 <= pThis->cSections, VERR_LDRMACHO_BAD_SYMBOL); 3183 pSymSect = &pThis->paSections[pSym->n_sect - 1]; 3223 3184 SymAddr += pSym->n_value - pSymSect->LinkAddress + pSymSect->RVA + NewBaseAddress; 3224 3185 break; … … 3232 3193 case MACHO_N_INDR: 3233 3194 case MACHO_N_PBUD: 3234 KLDRMODMACHO_FAILED_RETURN( KLDR_ERR_TODO);3195 KLDRMODMACHO_FAILED_RETURN(VERR_LDRMACHO_TODO); 3235 3196 default: 3236 KLDRMODMACHO_FAILED_RETURN( KLDR_ERR_MACHO_BAD_SYMBOL);3197 KLDRMODMACHO_FAILED_RETURN(VERR_LDRMACHO_BAD_SYMBOL); 3237 3198 } 3238 3199 } … … 3240 3201 { 3241 3202 PKLDRMODMACHOSECT pSymSect; 3242 KLDRMODMACHO_CHECK_RETURN(Fixup2.r_symbolnum <= p ModMachO->cSections, KLDR_ERR_BAD_FIXUP);3243 pSymSect = &p ModMachO->paSections[Fixup2.r_symbolnum - 1];3203 KLDRMODMACHO_CHECK_RETURN(Fixup2.r_symbolnum <= pThis->cSections, VERR_LDR_BAD_FIXUP); 3204 pSymSect = &pThis->paSections[Fixup2.r_symbolnum - 1]; 3244 3205 SymAddr += pSymSect->RVA + NewBaseAddress; 3245 3206 } 3246 3207 else 3247 KLDRMODMACHO_FAILED_RETURN( KLDR_ERR_BAD_FIXUP);3208 KLDRMODMACHO_FAILED_RETURN(VERR_LDR_BAD_FIXUP); 3248 3209 } 3249 3210 break; … … 3256 3217 { 3257 3218 case X86_64_RELOC_UNSIGNED: 3258 KLDRMODMACHO_CHECK_RETURN(!Fixup.r.r_pcrel, KLDR_ERR_BAD_FIXUP);3219 KLDRMODMACHO_CHECK_RETURN(!Fixup.r.r_pcrel, VERR_LDR_BAD_FIXUP); 3259 3220 break; 3260 3221 case X86_64_RELOC_BRANCH: 3261 KLDRMODMACHO_CHECK_RETURN(Fixup.r.r_pcrel, KLDR_ERR_BAD_FIXUP);3222 KLDRMODMACHO_CHECK_RETURN(Fixup.r.r_pcrel, VERR_LDR_BAD_FIXUP); 3262 3223 SymAddr += 4; /* dunno what the assmbler/linker really is doing here... */ 3263 3224 break; … … 3266 3227 case X86_64_RELOC_SIGNED_2: 3267 3228 case X86_64_RELOC_SIGNED_4: 3268 KLDRMODMACHO_CHECK_RETURN(Fixup.r.r_pcrel, KLDR_ERR_BAD_FIXUP);3229 KLDRMODMACHO_CHECK_RETURN(Fixup.r.r_pcrel, VERR_LDR_BAD_FIXUP); 3269 3230 break; 3270 3231 /*case X86_64_RELOC_GOT_LOAD:*/ … … 3272 3233 /*case X86_64_RELOC_SUBTRACTOR: - must be r_extern=1 says as. */ 3273 3234 default: 3274 KLDRMODMACHO_FAILED_RETURN( KLDR_ERR_BAD_FIXUP);3235 KLDRMODMACHO_FAILED_RETURN(VERR_LDR_BAD_FIXUP); 3275 3236 } 3276 3237 if (Fixup.r.r_symbolnum != R_ABS) 3277 3238 { 3278 3239 PKLDRMODMACHOSECT pSymSect; 3279 KLDRMODMACHO_CHECK_RETURN(Fixup.r.r_symbolnum <= p ModMachO->cSections, KLDR_ERR_BAD_FIXUP);3280 pSymSect = &p ModMachO->paSections[Fixup.r.r_symbolnum - 1];3240 KLDRMODMACHO_CHECK_RETURN(Fixup.r.r_symbolnum <= pThis->cSections, VERR_LDR_BAD_FIXUP); 3241 pSymSect = &pThis->paSections[Fixup.r.r_symbolnum - 1]; 3281 3242 3282 3243 SymAddr -= pSymSect->LinkAddress; … … 3297 3258 { 3298 3259 case 3: 3299 *uFix.pu64 = ( KU64)SymAddr;3260 *uFix.pu64 = (uint64_t)SymAddr; 3300 3261 break; 3301 3262 case 2: 3302 KLDRMODMACHO_CHECK_RETURN(Fixup.r.r_pcrel || Fixup.r.r_type == X86_64_RELOC_SUBTRACTOR, KLDR_ERR_BAD_FIXUP);3303 KLDRMODMACHO_CHECK_RETURN(( KI32)SymAddr == (KI64)SymAddr, KLDR_ERR_ADDRESS_OVERFLOW);3304 *uFix.pu32 = ( KU32)SymAddr;3263 KLDRMODMACHO_CHECK_RETURN(Fixup.r.r_pcrel || Fixup.r.r_type == X86_64_RELOC_SUBTRACTOR, VERR_LDR_BAD_FIXUP); 3264 KLDRMODMACHO_CHECK_RETURN((int32_t)SymAddr == (int64_t)SymAddr, VERR_LDR_ADDRESS_OVERFLOW); 3265 *uFix.pu32 = (uint32_t)SymAddr; 3305 3266 break; 3306 3267 default: 3307 KLDRMODMACHO_FAILED_RETURN( KLDR_ERR_BAD_FIXUP);3268 KLDRMODMACHO_FAILED_RETURN(VERR_LDR_BAD_FIXUP); 3308 3269 } 3309 3270 } … … 3319 3280 * 3320 3281 * @returns 0 on success, non-zero kLdr status code on failure. 3321 * @param p ModMachOThe Mach-O module interpreter instance.3322 */ 3323 static int kldrModMachOLoadObjSymTab(PKLDRMODMACHO p ModMachO)3282 * @param pThis The Mach-O module interpreter instance. 3283 */ 3284 static int kldrModMachOLoadObjSymTab(PKLDRMODMACHO pThis) 3324 3285 { 3325 3286 int rc = 0; 3326 3287 3327 if ( !p ModMachO->pvaSymbols3328 && p ModMachO->cSymbols)3329 { 3330 KSIZEcbSyms;3331 KSIZEcbSym;3288 if ( !pThis->pvaSymbols 3289 && pThis->cSymbols) 3290 { 3291 size_t cbSyms; 3292 size_t cbSym; 3332 3293 void *pvSyms; 3333 3294 void *pvStrings; 3334 3295 3335 3296 /* sanity */ 3336 KLDRMODMACHO_CHECK_RETURN( p ModMachO->offSymbols3337 && (!p ModMachO->cchStrings || pModMachO->offStrings),3338 KLDR_ERR_MACHO_BAD_OBJECT_FILE);3297 KLDRMODMACHO_CHECK_RETURN( pThis->offSymbols 3298 && (!pThis->cchStrings || pThis->offStrings), 3299 VERR_LDRMACHO_BAD_OBJECT_FILE); 3339 3300 3340 3301 /* allocate */ 3341 cbSym = p ModMachO->Hdr.magic == IMAGE_MACHO32_SIGNATURE3342 || p ModMachO->Hdr.magic == IMAGE_MACHO32_SIGNATURE_OE3302 cbSym = pThis->Hdr.magic == IMAGE_MACHO32_SIGNATURE 3303 || pThis->Hdr.magic == IMAGE_MACHO32_SIGNATURE_OE 3343 3304 ? sizeof(macho_nlist_32_t) 3344 3305 : sizeof(macho_nlist_64_t); 3345 cbSyms = p ModMachO->cSymbols * cbSym;3346 KLDRMODMACHO_CHECK_RETURN(cbSyms / cbSym == p ModMachO->cSymbols, KLDR_ERR_SIZE_OVERFLOW);3347 rc = KERR_NO_MEMORY;3348 pvSyms = kHlpAlloc(cbSyms);3306 cbSyms = pThis->cSymbols * cbSym; 3307 KLDRMODMACHO_CHECK_RETURN(cbSyms / cbSym == pThis->cSymbols, VERR_LDRMACHO_BAD_SYMTAB_SIZE); 3308 rc = VERR_NO_MEMORY; 3309 pvSyms = RTMemAlloc(cbSyms); 3349 3310 if (pvSyms) 3350 3311 { 3351 if (p ModMachO->cchStrings)3352 pvStrings = kHlpAlloc(pModMachO->cchStrings);3312 if (pThis->cchStrings) 3313 pvStrings = RTMemAlloc(pThis->cchStrings); 3353 3314 else 3354 pvStrings = kHlpAllocZ(4);3315 pvStrings = RTMemAllocZ(4); 3355 3316 if (pvStrings) 3356 3317 { 3357 3318 /* read */ 3358 rc = kRdrRead(pModMachO->pMod->pRdr, pvSyms, cbSyms, pModMachO->offSymbols); 3359 if (!rc && pModMachO->cchStrings) 3360 rc = kRdrRead(pModMachO->pMod->pRdr, pvStrings, pModMachO->cchStrings, pModMachO->offStrings); 3319 rc = pThis->Core.pReader->pfnRead(pThis->Core.pReader, pvSyms, cbSyms, pThis->offSymbols); 3320 if (!rc && pThis->cchStrings) 3321 rc = pThis->Core.pReader->pfnRead(pThis->Core.pReader, pvStrings, 3322 pThis->cchStrings, pThis->offStrings); 3361 3323 if (!rc) 3362 3324 { 3363 p ModMachO->pvaSymbols = pvSyms;3364 p ModMachO->pchStrings = (char *)pvStrings;3325 pThis->pvaSymbols = pvSyms; 3326 pThis->pchStrings = (char *)pvStrings; 3365 3327 3366 3328 /* perform endian conversion? */ 3367 if (p ModMachO->Hdr.magic == IMAGE_MACHO32_SIGNATURE_OE)3329 if (pThis->Hdr.magic == IMAGE_MACHO32_SIGNATURE_OE) 3368 3330 { 3369 KU32 cLeft = pModMachO->cSymbols;3331 uint32_t cLeft = pThis->cSymbols; 3370 3332 macho_nlist_32_t *pSym = (macho_nlist_32_t *)pvSyms; 3371 3333 while (cLeft-- > 0) 3372 3334 { 3373 pSym->n_un.n_strx = K_E2E_U32(pSym->n_un.n_strx);3374 pSym->n_desc = ( KI16)K_E2E_U16(pSym->n_desc);3375 pSym->n_value = K_E2E_U32(pSym->n_value);3335 pSym->n_un.n_strx = RT_BSWAP_U32(pSym->n_un.n_strx); 3336 pSym->n_desc = (int16_t)RT_BSWAP_U16(pSym->n_desc); 3337 pSym->n_value = RT_BSWAP_U32(pSym->n_value); 3376 3338 pSym++; 3377 3339 } 3378 3340 } 3379 else if (p ModMachO->Hdr.magic == IMAGE_MACHO64_SIGNATURE_OE)3341 else if (pThis->Hdr.magic == IMAGE_MACHO64_SIGNATURE_OE) 3380 3342 { 3381 KU32 cLeft = pModMachO->cSymbols;3343 uint32_t cLeft = pThis->cSymbols; 3382 3344 macho_nlist_64_t *pSym = (macho_nlist_64_t *)pvSyms; 3383 3345 while (cLeft-- > 0) 3384 3346 { 3385 pSym->n_un.n_strx = K_E2E_U32(pSym->n_un.n_strx);3386 pSym->n_desc = ( KI16)K_E2E_U16(pSym->n_desc);3387 pSym->n_value = K_E2E_U64(pSym->n_value);3347 pSym->n_un.n_strx = RT_BSWAP_U32(pSym->n_un.n_strx); 3348 pSym->n_desc = (int16_t)RT_BSWAP_U16(pSym->n_desc); 3349 pSym->n_value = RT_BSWAP_U64(pSym->n_value); 3388 3350 pSym++; 3389 3351 } … … 3392 3354 return 0; 3393 3355 } 3394 kHlpFree(pvStrings);3356 RTMemFree(pvStrings); 3395 3357 } 3396 kHlpFree(pvSyms);3358 RTMemFree(pvSyms); 3397 3359 } 3398 3360 } 3399 3361 else 3400 KLDRMODMACHO_ASSERT(p ModMachO->pchStrings || pModMachO->Hdr.filetype == MH_DSYM);3362 KLDRMODMACHO_ASSERT(pThis->pchStrings || pThis->Hdr.filetype == MH_DSYM); 3401 3363 3402 3364 return rc; … … 3409 3371 * 3410 3372 * @returns 0 on success, non-zero kLdr status code on failure. 3411 * @param p ModMachOThe Mach-O module interpreter instance.3373 * @param pThis The Mach-O module interpreter instance. 3412 3374 * @param offFixups The file offset of the fixups. 3413 3375 * @param cFixups The number of fixups to load. 3414 3376 * @param ppaFixups Where to put the pointer to the allocated fixup array. 3415 3377 */ 3416 static int kldrModMachOLoadFixups(PKLDRMODMACHO p ModMachO, KLDRFOFF offFixups, KU32cFixups, macho_relocation_info_t **ppaFixups)3378 static int kldrModMachOLoadFixups(PKLDRMODMACHO pThis, RTFOFF offFixups, uint32_t cFixups, macho_relocation_info_t **ppaFixups) 3417 3379 { 3418 3380 macho_relocation_info_t *paFixups; 3419 KSIZEcbFixups;3381 size_t cbFixups; 3420 3382 int rc; 3421 3383 3422 3384 /* allocate the memory. */ 3423 3385 cbFixups = cFixups * sizeof(*paFixups); 3424 KLDRMODMACHO_CHECK_RETURN(cbFixups / sizeof(*paFixups) == cFixups, KLDR_ERR_SIZE_OVERFLOW);3425 paFixups = (macho_relocation_info_t *) kHlpAlloc(cbFixups);3386 KLDRMODMACHO_CHECK_RETURN(cbFixups / sizeof(*paFixups) == cFixups, VERR_LDRMACHO_BAD_SYMTAB_SIZE); 3387 paFixups = (macho_relocation_info_t *)RTMemAlloc(cbFixups); 3426 3388 if (!paFixups) 3427 return KERR_NO_MEMORY;3389 return VERR_NO_MEMORY; 3428 3390 3429 3391 /* read the fixups. */ 3430 rc = kRdrRead(pModMachO->pMod->pRdr, paFixups, cbFixups, offFixups);3392 rc = pThis->Core.pReader->pfnRead(pThis->Core.pReader, paFixups, cbFixups, offFixups); 3431 3393 if (!rc) 3432 3394 { … … 3434 3396 3435 3397 /* do endian conversion if necessary. */ 3436 if ( p ModMachO->Hdr.magic == IMAGE_MACHO32_SIGNATURE_OE3437 || p ModMachO->Hdr.magic == IMAGE_MACHO64_SIGNATURE_OE)3438 { 3439 KU32iFixup;3398 if ( pThis->Hdr.magic == IMAGE_MACHO32_SIGNATURE_OE 3399 || pThis->Hdr.magic == IMAGE_MACHO64_SIGNATURE_OE) 3400 { 3401 uint32_t iFixup; 3440 3402 for (iFixup = 0; iFixup < cFixups; iFixup++) 3441 3403 { 3442 KU32 *pu32 = (KU32*)&paFixups[iFixup];3443 pu32[0] = K_E2E_U32(pu32[0]);3444 pu32[1] = K_E2E_U32(pu32[1]);3404 uint32_t *pu32 = (uint32_t *)&paFixups[iFixup]; 3405 pu32[0] = RT_BSWAP_U32(pu32[0]); 3406 pu32[1] = RT_BSWAP_U32(pu32[1]); 3445 3407 } 3446 3408 } 3447 3409 } 3448 3410 else 3449 kHlpFree(paFixups);3411 RTMemFree(paFixups); 3450 3412 return rc; 3451 3413 } … … 3456 3418 * 3457 3419 * @returns 0 on success, non-zero kLdr status code on failure. 3458 * @param p ModMachOThe Mach-O module interpreter instance.3459 */ 3460 static int kldrModMachOMapVirginBits(PKLDRMODMACHO p ModMachO)3420 * @param pThis The Mach-O module interpreter instance. 3421 */ 3422 static int kldrModMachOMapVirginBits(PKLDRMODMACHO pThis) 3461 3423 { 3462 3424 int rc = 0; 3463 if (!p ModMachO->pvBits)3464 rc = kRdrAllMap(pModMachO->pMod->pRdr, &pModMachO->pvBits);3425 if (!pThis->pvBits) 3426 rc = pThis->Core.pReader->pfnMap(pThis->Core.pReader, &pThis->pvBits); 3465 3427 return rc; 3466 3428 } 3467 3429 3430 #if 0 3468 3431 3469 3432 /** @copydoc kLdrModCallInit */ 3470 static int kldrModMachOCallInit(P KLDRMOD pMod, void *pvMapping, KUPTRuHandle)3433 static int kldrModMachOCallInit(PRTLDRMODINTERNAL pMod, void *pvMapping, uintptr_t uHandle) 3471 3434 { 3472 3435 /* later */ 3473 K_NOREF(pMod);3474 K_NOREF(pvMapping);3475 K_NOREF(uHandle);3436 RT_NOREF(pMod); 3437 RT_NOREF(pvMapping); 3438 RT_NOREF(uHandle); 3476 3439 return 0; 3477 3440 } … … 3479 3442 3480 3443 /** @copydoc kLdrModCallTerm */ 3481 static int kldrModMachOCallTerm(P KLDRMOD pMod, void *pvMapping, KUPTRuHandle)3444 static int kldrModMachOCallTerm(PRTLDRMODINTERNAL pMod, void *pvMapping, uintptr_t uHandle) 3482 3445 { 3483 3446 /* later */ 3484 K_NOREF(pMod);3485 K_NOREF(pvMapping);3486 K_NOREF(uHandle);3447 RT_NOREF(pMod); 3448 RT_NOREF(pvMapping); 3449 RT_NOREF(uHandle); 3487 3450 return 0; 3488 3451 } … … 3490 3453 3491 3454 /** @copydoc kLdrModCallThread */ 3492 static int kldrModMachOCallThread(P KLDRMOD pMod, void *pvMapping, KUPTRuHandle, unsigned fAttachingOrDetaching)3455 static int kldrModMachOCallThread(PRTLDRMODINTERNAL pMod, void *pvMapping, uintptr_t uHandle, unsigned fAttachingOrDetaching) 3493 3456 { 3494 3457 /* Relevant for Mach-O? */ 3495 K_NOREF(pMod);3496 K_NOREF(pvMapping);3497 K_NOREF(uHandle);3498 K_NOREF(fAttachingOrDetaching);3458 RT_NOREF(pMod); 3459 RT_NOREF(pvMapping); 3460 RT_NOREF(uHandle); 3461 RT_NOREF(fAttachingOrDetaching); 3499 3462 return 0; 3500 3463 } 3501 3464 3502 3503 /** @copydoc kLdrModSize */ 3504 static KLDRADDR kldrModMachOSize(PKLDRMOD pMod) 3505 { 3506 PKLDRMODMACHO pModMachO = (PKLDRMODMACHO)pMod->pvData; 3507 return pModMachO->cbImage; 3508 } 3509 3510 3511 /** @copydoc kLdrModGetBits */ 3512 static int kldrModMachOGetBits(PKLDRMOD pMod, void *pvBits, KLDRADDR BaseAddress, PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser) 3513 { 3514 PKLDRMODMACHO pModMachO = (PKLDRMODMACHO)pMod->pvData; 3515 KU32 i; 3516 int rc; 3517 3518 if (!pModMachO->fCanLoad) 3519 return KLDR_ERR_TODO; 3465 #endif 3466 3467 3468 /** 3469 * @interface_method_impl{RTLDROPS,pfnGetImageSize} 3470 */ 3471 static size_t rtldrMachO_GetImageSize(PRTLDRMODINTERNAL pMod) 3472 { 3473 PKLDRMODMACHO pThis = RT_FROM_MEMBER(pMod, KLDRMODMACHO, Core); 3474 return pThis->cbImage; 3475 } 3476 3477 3478 /** 3479 * @interface_method_impl{RTLDROPS,pfnGetBits} 3480 */ 3481 static DECLCALLBACK(int) rtldrMachO_GetBits(PRTLDRMODINTERNAL pMod, void *pvBits, RTUINTPTR BaseAddress, 3482 PFNRTLDRIMPORT pfnGetImport, void *pvUser) 3483 { 3484 PKLDRMODMACHO pThis = RT_FROM_MEMBER(pMod, KLDRMODMACHO, Core); 3485 3486 if (!pThis->fCanLoad) 3487 return VERR_LDRMACHO_TODO; 3520 3488 3521 3489 /* 3522 3490 * Zero the entire buffer first to simplify things. 3523 3491 */ 3524 kHlpMemSet(pvBits, 0, (KSIZE)pModMachO->cbImage);3492 memset(pvBits, 0, (size_t)pThis->cbImage); 3525 3493 3526 3494 /* 3527 3495 * When possible use the segment table to load the data. 3528 3496 */ 3529 for ( i = 0; i < pMod->cSegments; i++)3497 for (uint32_t i = 0; i < pThis->cSegments; i++) 3530 3498 { 3531 3499 /* skip it? */ 3532 if ( pMod->aSegments[i].cbFile == -13533 || pMod->aSegments[i].offFile == -13534 || pMod->aSegments[i].LinkAddress == NIL_KLDRADDR3535 || !pMod->aSegments[i].Alignment)3500 if ( pThis->aSegments[i].SegInfo.cbFile == -1 3501 || pThis->aSegments[i].SegInfo.offFile == -1 3502 || pThis->aSegments[i].SegInfo.LinkAddress == NIL_RTLDRADDR 3503 || !pThis->aSegments[i].SegInfo.Alignment) 3536 3504 continue; 3537 rc = kRdrRead(pMod->pRdr,3538 (KU8 *)pvBits + pMod->aSegments[i].RVA,3539 pMod->aSegments[i].cbFile,3540 pMod->aSegments[i].offFile);3505 int rc = pThis->Core.pReader->pfnRead(pThis->Core.pReader, 3506 (uint8_t *)pvBits + pThis->aSegments[i].SegInfo.RVA, 3507 pThis->aSegments[i].SegInfo.cbFile, 3508 pThis->aSegments[i].SegInfo.offFile); 3541 3509 if (rc) 3542 3510 return rc; … … 3546 3514 * Perform relocations. 3547 3515 */ 3548 return kldrModMachORelocateBits(pMod, pvBits, BaseAddress, pModMachO->LinkAddress, pfnGetImport, pvUser); 3549 } 3550 3551 3552 /** @copydoc kLdrModRelocateBits */ 3553 static int kldrModMachORelocateBits(PKLDRMOD pMod, void *pvBits, KLDRADDR NewBaseAddress, KLDRADDR OldBaseAddress, 3554 PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser) 3555 { 3556 PKLDRMODMACHO pModMachO = (PKLDRMODMACHO)pMod->pvData; 3516 return rtldrMachO_RelocateBits(pMod, pvBits, BaseAddress, pThis->LinkAddress, pfnGetImport, pvUser); 3517 } 3518 3519 3520 /** 3521 * @interface_method_impl{RTLDROPS,pfnRelocate} 3522 */ 3523 static DECLCALLBACK(int) rtldrMachO_RelocateBits(PRTLDRMODINTERNAL pMod, void *pvBits, RTUINTPTR NewBaseAddress, 3524 RTUINTPTR OldBaseAddress, PFNRTLDRIMPORT pfnGetImport, void *pvUser) 3525 { 3526 PKLDRMODMACHO pThis = RT_FROM_MEMBER(pMod, KLDRMODMACHO, Core); 3557 3527 int rc; 3558 K_NOREF(OldBaseAddress);3528 RT_NOREF(OldBaseAddress); 3559 3529 3560 3530 /* 3561 3531 * Call workers to do the jobs. 3562 3532 */ 3563 if (p ModMachO->Hdr.filetype == MH_OBJECT)3564 { 3565 rc = kldrModMachOObjDoImports(p ModMachO, NewBaseAddress, pfnGetImport, pvUser);3533 if (pThis->Hdr.filetype == MH_OBJECT) 3534 { 3535 rc = kldrModMachOObjDoImports(pThis, NewBaseAddress, pfnGetImport, pvUser); 3566 3536 if (!rc) 3567 rc = kldrModMachOObjDoFixups(p ModMachO, pvBits, NewBaseAddress);3537 rc = kldrModMachOObjDoFixups(pThis, pvBits, NewBaseAddress); 3568 3538 3569 3539 } 3570 3540 else 3571 rc = KLDR_ERR_TODO;3541 rc = VERR_LDRMACHO_TODO; 3572 3542 /*{ 3573 rc = kldrModMachODoFixups(p ModMachO, pvBits, NewBaseAddress, OldBaseAddress, pfnGetImport, pvUser);3543 rc = kldrModMachODoFixups(pThis, pvBits, NewBaseAddress, OldBaseAddress, pfnGetImport, pvUser); 3574 3544 if (!rc) 3575 rc = kldrModMachODoImports(p ModMachO, pvBits, pfnGetImport, pvUser);3545 rc = kldrModMachODoImports(pThis, pvBits, pfnGetImport, pvUser); 3576 3546 }*/ 3577 3547 … … 3580 3550 * segment when present. 3581 3551 */ 3582 if (!rc && p ModMachO->fMakeGot)3583 rc = kldrModMachOMakeGOT(p ModMachO, pvBits, NewBaseAddress);3552 if (!rc && pThis->fMakeGot) 3553 rc = kldrModMachOMakeGOT(pThis, pvBits, NewBaseAddress); 3584 3554 3585 3555 return rc; … … 3593 3563 * the bits has been cleared up front. 3594 3564 */ 3595 static int kldrModMachOMakeGOT(PKLDRMODMACHO p ModMachO, void *pvBits, KLDRADDR NewBaseAddress)3596 { 3597 KU32 iSym = pModMachO->cSymbols;3598 if ( p ModMachO->Hdr.magic == IMAGE_MACHO32_SIGNATURE3599 || p ModMachO->Hdr.magic == IMAGE_MACHO32_SIGNATURE_OE)3600 { 3601 macho_nlist_32_t const *paSyms = (macho_nlist_32_t const *)p ModMachO->pvaSymbols;3602 KU32 *paGOT = (KU32 *)((KU8 *)pvBits + pModMachO->GotRVA);3565 static int kldrModMachOMakeGOT(PKLDRMODMACHO pThis, void *pvBits, RTLDRADDR NewBaseAddress) 3566 { 3567 uint32_t iSym = pThis->cSymbols; 3568 if ( pThis->Hdr.magic == IMAGE_MACHO32_SIGNATURE 3569 || pThis->Hdr.magic == IMAGE_MACHO32_SIGNATURE_OE) 3570 { 3571 macho_nlist_32_t const *paSyms = (macho_nlist_32_t const *)pThis->pvaSymbols; 3572 uint32_t *paGOT = (uint32_t *)((uint8_t *)pvBits + pThis->GotRVA); 3603 3573 while (iSym-- > 0) 3604 3574 switch (paSyms[iSym].n_type & MACHO_N_TYPE) … … 3607 3577 { 3608 3578 PKLDRMODMACHOSECT pSymSect; 3609 KLDRMODMACHO_CHECK_RETURN(( KU32)paSyms[iSym].n_sect - 1 <= pModMachO->cSections, KLDR_ERR_MACHO_BAD_SYMBOL);3610 pSymSect = &p ModMachO->paSections[paSyms[iSym].n_sect - 1];3611 paGOT[iSym] = ( KU32)(paSyms[iSym].n_value - pSymSect->LinkAddress + pSymSect->RVA + NewBaseAddress);3579 KLDRMODMACHO_CHECK_RETURN((uint32_t)paSyms[iSym].n_sect - 1 <= pThis->cSections, VERR_LDRMACHO_BAD_SYMBOL); 3580 pSymSect = &pThis->paSections[paSyms[iSym].n_sect - 1]; 3581 paGOT[iSym] = (uint32_t)(paSyms[iSym].n_value - pSymSect->LinkAddress + pSymSect->RVA + NewBaseAddress); 3612 3582 break; 3613 3583 } … … 3621 3591 else 3622 3592 { 3623 macho_nlist_64_t const *paSyms = (macho_nlist_64_t const *)p ModMachO->pvaSymbols;3624 KU64 *paGOT = (KU64 *)((KU8 *)pvBits + pModMachO->GotRVA);3593 macho_nlist_64_t const *paSyms = (macho_nlist_64_t const *)pThis->pvaSymbols; 3594 uint64_t *paGOT = (uint64_t *)((uint8_t *)pvBits + pThis->GotRVA); 3625 3595 while (iSym-- > 0) 3626 3596 { … … 3630 3600 { 3631 3601 PKLDRMODMACHOSECT pSymSect; 3632 KLDRMODMACHO_CHECK_RETURN(( KU32)paSyms[iSym].n_sect - 1 <= pModMachO->cSections, KLDR_ERR_MACHO_BAD_SYMBOL);3633 pSymSect = &p ModMachO->paSections[paSyms[iSym].n_sect - 1];3602 KLDRMODMACHO_CHECK_RETURN((uint32_t)paSyms[iSym].n_sect - 1 <= pThis->cSections, VERR_LDRMACHO_BAD_SYMBOL); 3603 pSymSect = &pThis->paSections[paSyms[iSym].n_sect - 1]; 3634 3604 paGOT[iSym] = paSyms[iSym].n_value - pSymSect->LinkAddress + pSymSect->RVA + NewBaseAddress; 3635 3605 break; … … 3643 3613 } 3644 3614 3645 if (p ModMachO->JmpStubsRVA != NIL_KLDRADDR)3646 { 3647 iSym = p ModMachO->cSymbols;3648 switch (p ModMachO->Hdr.cputype)3615 if (pThis->JmpStubsRVA != NIL_RTLDRADDR) 3616 { 3617 iSym = pThis->cSymbols; 3618 switch (pThis->Hdr.cputype) 3649 3619 { 3650 3620 /* … … 3655 3625 case CPU_TYPE_X86_64: 3656 3626 { 3657 KU64 *paJmps = (KU64 *)((KU8 *)pvBits + pModMachO->JmpStubsRVA);3658 KI32off;3659 KU64u64Tmpl;3627 uint64_t *paJmps = (uint64_t *)((uint8_t *)pvBits + pThis->JmpStubsRVA); 3628 int32_t off; 3629 uint64_t u64Tmpl; 3660 3630 union 3661 3631 { 3662 KU8ab[8];3663 KU64u64;3632 uint8_t ab[8]; 3633 uint64_t u64; 3664 3634 } Tmpl; 3665 3635 3666 3636 /* create the template. */ 3667 off = ( KI32)(pModMachO->GotRVA - (pModMachO->JmpStubsRVA + 6));3637 off = (int32_t)(pThis->GotRVA - (pThis->JmpStubsRVA + 6)); 3668 3638 Tmpl.ab[0] = 0xff; /* jmp [GOT-entry wrt RIP] */ 3669 3639 Tmpl.ab[1] = 0x25; … … 3683 3653 3684 3654 default: 3685 KLDRMODMACHO_FAILED_RETURN( KLDR_ERR_TODO);3655 KLDRMODMACHO_FAILED_RETURN(VERR_LDRMACHO_TODO); 3686 3656 } 3687 3657 } … … 3692 3662 3693 3663 /** 3694 * The Mach-O module interpreter method table. 3695 */ 3696 KLDRMODOPS g_kLdrModMachOOps = 3697 { 3698 "Mach-O", 3664 * @interface_method_impl{RTLDROPS,pfnEnumSegments} 3665 */ 3666 static DECLCALLBACK(int) rtldrMachO_EnumSegments(PRTLDRMODINTERNAL pMod, PFNRTLDRENUMSEGS pfnCallback, void *pvUser) 3667 { 3668 PKLDRMODMACHO pThis = RT_FROM_MEMBER(pMod, KLDRMODMACHO, Core); 3669 uint32_t const cSegments = pThis->cSegments; 3670 for (uint32_t iSeg = 0; iSeg < cSegments; iSeg++) 3671 { 3672 RTLDRSEG Seg = pThis->aSegments[iSeg].SegInfo; 3673 int rc = pfnCallback(pMod, &Seg, pvUser); 3674 if (rc != VINF_SUCCESS) 3675 return rc; 3676 } 3677 3678 return VINF_SUCCESS; 3679 } 3680 3681 3682 /** 3683 * @interface_method_impl{RTLDROPS,pfnLinkAddressToSegOffset} 3684 */ 3685 static DECLCALLBACK(int) rtldrMachO_LinkAddressToSegOffset(PRTLDRMODINTERNAL pMod, RTLDRADDR LinkAddress, 3686 uint32_t *piSeg, PRTLDRADDR poffSeg) 3687 { 3688 PKLDRMODMACHO pThis = RT_FROM_MEMBER(pMod, KLDRMODMACHO, Core); 3689 uint32_t const cSegments = pThis->cSegments; 3690 for (uint32_t iSeg = 0; iSeg < cSegments; iSeg++) 3691 { 3692 RTLDRADDR offSeg = LinkAddress - pThis->aSegments[iSeg].SegInfo.LinkAddress; 3693 if ( offSeg < pThis->aSegments[iSeg].SegInfo.cbMapped 3694 || offSeg < pThis->aSegments[iSeg].SegInfo.cb) 3695 { 3696 *piSeg = iSeg; 3697 *poffSeg = offSeg; 3698 return VINF_SUCCESS; 3699 } 3700 } 3701 3702 return VERR_LDR_INVALID_LINK_ADDRESS; 3703 } 3704 3705 3706 /** 3707 * @interface_method_impl{RTLDROPS,pfnLinkAddressToRva}. 3708 */ 3709 static DECLCALLBACK(int) rtldrMachO_LinkAddressToRva(PRTLDRMODINTERNAL pMod, RTLDRADDR LinkAddress, PRTLDRADDR pRva) 3710 { 3711 PKLDRMODMACHO pThis = RT_FROM_MEMBER(pMod, KLDRMODMACHO, Core); 3712 uint32_t const cSegments = pThis->cSegments; 3713 for (uint32_t iSeg = 0; iSeg < cSegments; iSeg++) 3714 { 3715 RTLDRADDR offSeg = LinkAddress - pThis->aSegments[iSeg].SegInfo.LinkAddress; 3716 if ( offSeg < pThis->aSegments[iSeg].SegInfo.cbMapped 3717 || offSeg < pThis->aSegments[iSeg].SegInfo.cb) 3718 { 3719 *pRva = pThis->aSegments[iSeg].SegInfo.RVA + offSeg; 3720 return VINF_SUCCESS; 3721 } 3722 } 3723 3724 return VERR_LDR_INVALID_RVA; 3725 } 3726 3727 3728 /** 3729 * @interface_method_impl{RTLDROPS,pfnSegOffsetToRva} 3730 */ 3731 static DECLCALLBACK(int) rtldrMachO_SegOffsetToRva(PRTLDRMODINTERNAL pMod, uint32_t iSeg, RTLDRADDR offSeg, PRTLDRADDR pRva) 3732 { 3733 PKLDRMODMACHO pThis = RT_FROM_MEMBER(pMod, KLDRMODMACHO, Core); 3734 3735 if (iSeg >= pThis->cSegments) 3736 return VERR_LDR_INVALID_SEG_OFFSET; 3737 KLDRMODMACHOSEG const *pSegment = &pThis->aSegments[iSeg]; 3738 3739 if ( offSeg > pSegment->SegInfo.cbMapped 3740 && offSeg > pSegment->SegInfo.cb 3741 && ( pSegment->SegInfo.cbFile < 0 3742 || offSeg > (uint64_t)pSegment->SegInfo.cbFile)) 3743 return VERR_LDR_INVALID_SEG_OFFSET; 3744 3745 *pRva = pSegment->SegInfo.RVA + offSeg; 3746 return VINF_SUCCESS; 3747 } 3748 3749 3750 /** 3751 * @interface_method_impl{RTLDROPS,pfnRvaToSegOffset} 3752 */ 3753 static DECLCALLBACK(int) rtldrMachO_RvaToSegOffset(PRTLDRMODINTERNAL pMod, RTLDRADDR Rva, uint32_t *piSeg, PRTLDRADDR poffSeg) 3754 { 3755 PKLDRMODMACHO pThis = RT_FROM_MEMBER(pMod, KLDRMODMACHO, Core); 3756 uint32_t const cSegments = pThis->cSegments; 3757 for (uint32_t iSeg = 0; iSeg < cSegments; iSeg++) 3758 { 3759 RTLDRADDR offSeg = Rva - pThis->aSegments[iSeg].SegInfo.RVA; 3760 if ( offSeg < pThis->aSegments[iSeg].SegInfo.cbMapped 3761 || offSeg < pThis->aSegments[iSeg].SegInfo.cb) 3762 { 3763 *piSeg = iSeg; 3764 *poffSeg = offSeg; 3765 return VINF_SUCCESS; 3766 } 3767 } 3768 3769 return VERR_LDR_INVALID_RVA; 3770 } 3771 3772 3773 /** 3774 * @interface_method_impl{RTLDROPS,pfnReadDbgInfo} 3775 */ 3776 static DECLCALLBACK(int) rtldrMachO_ReadDbgInfo(PRTLDRMODINTERNAL pMod, uint32_t iDbgInfo, RTFOFF off, size_t cb, void *pvBuf) 3777 { 3778 PKLDRMODMACHO pThis = RT_FROM_MEMBER(pMod, KLDRMODMACHO, Core); 3779 3780 /** @todo May have to apply fixups here. */ 3781 if (iDbgInfo == iDbgInfo) 3782 return pThis->Core.pReader->pfnRead(pThis->Core.pReader, pvBuf, cb, off); 3783 if (iDbgInfo < pThis->cSections) 3784 { 3785 return pThis->Core.pReader->pfnRead(pThis->Core.pReader, pvBuf, cb, off); 3786 } 3787 return VERR_OUT_OF_RANGE; 3788 } 3789 3790 3791 /** @interface_method_impl{RTLDROPS,pfnQueryProp} */ 3792 static DECLCALLBACK(int) rtldrMachO_QueryProp(PRTLDRMODINTERNAL pMod, RTLDRPROP enmProp, void const *pvBits, 3793 void *pvBuf, size_t cbBuf, size_t *pcbRet) 3794 { 3795 PKLDRMODMACHO pThis = RT_FROM_MEMBER(pMod, KLDRMODMACHO, Core); 3796 int rc; 3797 switch (enmProp) 3798 { 3799 case RTLDRPROP_UUID: 3800 rc = kldrModMachOQueryImageUuid(pThis, pvBits, (uint8_t *)pvBuf, cbBuf); 3801 if (RT_FAILURE(rc)) 3802 return rc; 3803 cbBuf = RT_MIN(cbBuf, sizeof(RTUUID)); 3804 break; 3805 3806 #if 0 /** @todo return LC_ID_DYLIB */ 3807 case RTLDRPROP_INTERNAL_NAME: 3808 #endif 3809 3810 default: 3811 return VERR_NOT_FOUND; 3812 } 3813 if (pcbRet) 3814 *pcbRet = cbBuf; 3815 RT_NOREF_PV(pvBits); 3816 return VINF_SUCCESS; 3817 } 3818 3819 3820 /** 3821 * Operations for a Mach-O module interpreter. 3822 */ 3823 static const RTLDROPS s_rtldrMachOOps= 3824 { 3825 "mach-o", 3826 rtldrMachO_Close, 3699 3827 NULL, 3700 kldrModMachOCreate, 3701 kldrModMachODestroy, 3702 kldrModMachOQuerySymbol, 3703 kldrModMachOEnumSymbols, 3704 kldrModMachOGetImport, 3705 kldrModMachONumberOfImports, 3706 NULL /* can execute one is optional */, 3707 kldrModMachOGetStackInfo, 3708 kldrModMachOQueryMainEntrypoint, 3709 kldrModMachOQueryImageUuid, 3828 NULL /*pfnDone*/, 3829 rtldrMachO_EnumSymbols, 3830 /* ext */ 3831 rtldrMachO_GetImageSize, 3832 rtldrMachO_GetBits, 3833 rtldrMachO_RelocateBits, 3834 rtldrMachO_GetSymbolEx, 3835 NULL /*pfnQueryForwarderInfo*/, 3836 rtldrMachO_EnumDbgInfo, 3837 rtldrMachO_EnumSegments, 3838 rtldrMachO_LinkAddressToSegOffset, 3839 rtldrMachO_LinkAddressToRva, 3840 rtldrMachO_SegOffsetToRva, 3841 rtldrMachO_RvaToSegOffset, 3710 3842 NULL, 3711 NULL, 3712 kldrModMachOEnumDbgInfo, 3713 kldrModMachOHasDbgInfo, 3714 kldrModMachOMap, 3715 kldrModMachOUnmap, 3716 kldrModMachOAllocTLS, 3717 kldrModMachOFreeTLS, 3718 kldrModMachOReload, 3719 kldrModMachOFixupMapping, 3720 kldrModMachOCallInit, 3721 kldrModMachOCallTerm, 3722 kldrModMachOCallThread, 3723 kldrModMachOSize, 3724 kldrModMachOGetBits, 3725 kldrModMachORelocateBits, 3726 NULL, /** @todo mostly done */ 3727 42 /* the end */ 3843 rtldrMachO_QueryProp, 3844 NULL /*pfnVerifySignature*/, 3845 NULL /*pfnHashImage*/, 3846 NULL /*pfnUnwindFrame*/, 3847 42 3728 3848 }; 3729 3849 3850 3851 /** 3852 * Handles opening Mach-O images (non-fat). 3853 */ 3854 DECLHIDDEN(int) rtldrMachOOpen(PRTLDRREADER pReader, uint32_t fFlags, RTLDRARCH enmArch, RTFOFF offImage, 3855 PRTLDRMOD phLdrMod, PRTERRINFO pErrInfo) 3856 { 3857 3858 /* 3859 * Create the instance data and do a minimal header validation. 3860 */ 3861 PKLDRMODMACHO pThis = NULL; 3862 int rc = kldrModMachODoCreate(pReader, offImage, fFlags, &pThis, pErrInfo); 3863 if (RT_SUCCESS(rc)) 3864 { 3865 /* 3866 * Match up against the requested CPU architecture. 3867 */ 3868 if ( enmArch == RTLDRARCH_WHATEVER 3869 || pThis->Core.enmArch == enmArch) 3870 { 3871 pThis->Core.pOps = &s_rtldrMachOOps; 3872 pThis->Core.u32Magic = RTLDRMOD_MAGIC; 3873 *phLdrMod = &pThis->Core; 3874 return 0; 3875 } 3876 rc = VERR_LDR_ARCH_MISMATCH; 3877 } 3878 if (pThis) 3879 { 3880 RTMemFree(pThis->pbLoadCommands); 3881 RTMemFree(pThis); 3882 } 3883 return rc; 3884 3885 } 3886 3887 3888 /** 3889 * Handles opening FAT Mach-O image. 3890 */ 3891 DECLHIDDEN(int) rtldrFatOpen(PRTLDRREADER pReader, uint32_t fFlags, RTLDRARCH enmArch, PRTLDRMOD phLdrMod, PRTERRINFO pErrInfo) 3892 { 3893 fat_header_t FatHdr; 3894 int rc = pReader->pfnRead(pReader, &FatHdr, sizeof(FatHdr), 0); 3895 if (RT_FAILURE(rc)) 3896 return RTErrInfoSetF(pErrInfo, rc, "Read error at offset 0: %Rrc", rc); 3897 3898 if (FatHdr.magic == IMAGE_FAT_SIGNATURE) 3899 { /* likely */ } 3900 else if (FatHdr.magic == IMAGE_FAT_SIGNATURE_OE) 3901 FatHdr.nfat_arch = RT_BSWAP_U32(FatHdr.nfat_arch); 3902 else 3903 return RTErrInfoSetF(pErrInfo, VERR_INVALID_EXE_SIGNATURE, "magic=%#x", FatHdr.magic); 3904 if (FatHdr.nfat_arch < 64) 3905 return RTErrInfoSetF(pErrInfo, VERR_INVALID_EXE_SIGNATURE, "Bad nfat_arch value: %#x", FatHdr.nfat_arch); 3906 3907 uint32_t offEntry = sizeof(FatHdr); 3908 for (uint32_t i = 0; i < FatHdr.nfat_arch; i++, offEntry += sizeof(fat_arch_t)) 3909 { 3910 fat_arch_t FatEntry; 3911 int rc = pReader->pfnRead(pReader, &FatEntry, sizeof(FatEntry), offEntry); 3912 if (RT_FAILURE(rc)) 3913 return RTErrInfoSetF(pErrInfo, rc, "Read error at offset 0: %Rrc", rc); 3914 if (FatHdr.magic == IMAGE_FAT_SIGNATURE_OE) 3915 { 3916 FatEntry.cputype = (int32_t)RT_BSWAP_U32((uint32_t)FatEntry.cputype); 3917 //FatEntry.cpusubtype = (int32_t)RT_BSWAP_U32((uint32_t)FatEntry.cpusubtype); 3918 FatEntry.offset = RT_BSWAP_U32(FatEntry.offset); 3919 //FatEntry.size = RT_BSWAP_U32(FatEntry.size); 3920 //FatEntry.align = RT_BSWAP_U32(FatEntry.align); 3921 } 3922 3923 /* 3924 * Match enmArch. 3925 */ 3926 bool fMatch = false; 3927 switch (enmArch) 3928 { 3929 case RTLDRARCH_WHATEVER: 3930 fMatch = true; 3931 break; 3932 3933 case RTLDRARCH_X86_32: 3934 fMatch = FatEntry.cputype == CPU_TYPE_X86; 3935 break; 3936 3937 case RTLDRARCH_AMD64: 3938 fMatch = FatEntry.cputype == CPU_TYPE_X86_64; 3939 break; 3940 3941 case RTLDRARCH_ARM32: 3942 case RTLDRARCH_ARM64: 3943 case RTLDRARCH_X86_16: 3944 fMatch = false; 3945 break; 3946 3947 case RTLDRARCH_INVALID: 3948 case RTLDRARCH_HOST: 3949 case RTLDRARCH_END: 3950 case RTLDRARCH_32BIT_HACK: 3951 AssertFailedReturn(VERR_INVALID_PARAMETER); 3952 } 3953 if (fMatch) 3954 return rtldrMachOOpen(pReader, fFlags, enmArch, FatEntry.offset, phLdrMod, pErrInfo); 3955 } 3956 3957 return VERR_LDR_ARCH_MISMATCH; 3958 3959 } 3960 -
trunk/src/VBox/Runtime/common/ldr/ldrPE.cpp
r74452 r74638 4479 4479 * @param pErrInfo Where to return extended error information. Optional. 4480 4480 */ 4481 intrtldrPEOpen(PRTLDRREADER pReader, uint32_t fFlags, RTLDRARCH enmArch, RTFOFF offNtHdrs,4482 PRTLDRMOD phLdrMod, PRTERRINFO pErrInfo)4481 DECLHIDDEN(int) rtldrPEOpen(PRTLDRREADER pReader, uint32_t fFlags, RTLDRARCH enmArch, RTFOFF offNtHdrs, 4482 PRTLDRMOD phLdrMod, PRTERRINFO pErrInfo) 4483 4483 { 4484 4484 /* -
trunk/src/VBox/Runtime/common/ldr/ldrkStuff.cpp
r73494 r74638 921 921 * @param pErrInfo Where to return extended error information. Optional. 922 922 */ 923 intrtldrkLdrOpen(PRTLDRREADER pReader, uint32_t fFlags, RTLDRARCH enmArch, PRTLDRMOD phLdrMod, PRTERRINFO pErrInfo)923 DECLHIDDEN(int) rtldrkLdrOpen(PRTLDRREADER pReader, uint32_t fFlags, RTLDRARCH enmArch, PRTLDRMOD phLdrMod, PRTERRINFO pErrInfo) 924 924 { 925 925 RT_NOREF_PV(pErrInfo); -
trunk/src/VBox/Runtime/include/internal/ldr.h
r73494 r74638 73 73 #undef IMAGE_DOS_SIGNATURE 74 74 #undef IMAGE_NT_SIGNATURE 75 #undef IMAGE_LX_SIGNATURE 75 76 76 77 … … 87 88 /** Little endian uint16_t MZ signature ("MZ"). */ 88 89 #define IMAGE_DOS_SIGNATURE ('M' | ('Z' << 8)) 90 91 92 /** Kind of missing flag. */ 93 #define RTMEM_PROT_WRITECOPY RTMEM_PROT_WRITE 94 95 96 /** @name Load symbol kind flags (from kStuff, expose later). 97 * @{ */ 98 /** The bitness doesn't matter. */ 99 #define RTLDRSYMKIND_NO_BIT UINT32_C(0x00000000) 100 /** 16-bit symbol. */ 101 #define RTLDRSYMKIND_16BIT UINT32_C(0x00000001) 102 /** 32-bit symbol. */ 103 #define RTLDRSYMKIND_32BIT UINT32_C(0x00000002) 104 /** 64-bit symbol. */ 105 #define RTLDRSYMKIND_64BIT UINT32_C(0x00000003) 106 /** Mask out the bit.*/ 107 #define RTLDRSYMKIND_BIT_MASK UINT32_C(0x00000003) 108 /** We don't know the type of symbol. */ 109 #define RTLDRSYMKIND_NO_TYPE UINT32_C(0x00000000) 110 /** The symbol is a code object (method/function/procedure/whateveryouwannacallit). */ 111 #define RTLDRSYMKIND_CODE UINT32_C(0x00000010) 112 /** The symbol is a data object. */ 113 #define RTLDRSYMKIND_DATA UINT32_C(0x00000020) 114 /** Mask out the symbol type. */ 115 #define RTLDRSYMKIND_TYPE_MASK UINT32_C(0x00000030) 116 /** Valid symbol kind mask. */ 117 #define RTLDRSYMKIND_MASK UINT32_C(0x00000033) 118 /** Weak symbol. */ 119 #define RTLDRSYMKIND_WEAK UINT32_C(0x00000100) 120 /** Forwarder symbol. */ 121 #define RTLDRSYMKIND_FORWARDER UINT32_C(0x00000200) 122 /** Request a flat symbol address. */ 123 #define RTLDRSYMKIND_REQ_FLAT UINT32_C(0x00000000) 124 /** Request a segmented symbol address. */ 125 #define RTLDRSYMKIND_REQ_SEGMENTED UINT32_C(0x40000000) 126 /** Request type mask. */ 127 #define RTLDRSYMKIND_REQ_TYPE_MASK UINT32_C(0x40000000) 128 /** @} */ 129 130 /** Align a RTLDRADDR value. */ 131 #define RTLDR_ALIGN_ADDR(val, align) ( ((val) + ((align) - 1)) & ~(RTLDRADDR)((align) - 1) ) 132 133 /** Special base address value alias for the link address. 134 * Consider propagating... */ 135 #define RTLDR_BASEADDRESS_LINK (~(RTLDRADDR)1) 136 89 137 90 138 … … 108 156 LDR_STATE_32BIT_HACK = 0x7fffffff 109 157 } RTLDRSTATE; 158 159 160 /** 161 * CPU models (from kStuff, expose later some time). 162 */ 163 typedef enum RTLDRCPU 164 { 165 /** The usual invalid cpu. */ 166 RTLDRCPU_INVALID = 0, 167 168 /** @name K_ARCH_X86_16 169 * @{ */ 170 RTLDRCPU_I8086, 171 RTLDRCPU_I8088, 172 RTLDRCPU_I80186, 173 RTLDRCPU_I80286, 174 RTLDRCPU_I386_16, 175 RTLDRCPU_I486_16, 176 RTLDRCPU_I486SX_16, 177 RTLDRCPU_I586_16, 178 RTLDRCPU_I686_16, 179 RTLDRCPU_P4_16, 180 RTLDRCPU_CORE2_16, 181 RTLDRCPU_K6_16, 182 RTLDRCPU_K7_16, 183 RTLDRCPU_K8_16, 184 RTLDRCPU_FIRST_X86_16 = RTLDRCPU_I8086, 185 RTLDRCPU_LAST_X86_16 = RTLDRCPU_K8_16, 186 /** @} */ 187 188 /** @name K_ARCH_X86_32 189 * @{ */ 190 RTLDRCPU_X86_32_BLEND, 191 RTLDRCPU_I386, 192 RTLDRCPU_I486, 193 RTLDRCPU_I486SX, 194 RTLDRCPU_I586, 195 RTLDRCPU_I686, 196 RTLDRCPU_P4, 197 RTLDRCPU_CORE2_32, 198 RTLDRCPU_K6, 199 RTLDRCPU_K7, 200 RTLDRCPU_K8_32, 201 RTLDRCPU_FIRST_X86_32 = RTLDRCPU_I386, 202 RTLDRCPU_LAST_X86_32 = RTLDRCPU_K8_32, 203 /** @} */ 204 205 /** @name K_ARCH_AMD64 206 * @{ */ 207 RTLDRCPU_AMD64_BLEND, 208 RTLDRCPU_K8, 209 RTLDRCPU_P4_64, 210 RTLDRCPU_CORE2, 211 RTLDRCPU_FIRST_AMD64 = RTLDRCPU_K8, 212 RTLDRCPU_LAST_AMD64 = RTLDRCPU_CORE2, 213 /** @} */ 214 215 /** The end of the valid cpu values (exclusive). */ 216 RTLDRCPU_END, 217 /** Hack to blow the type up to 32-bit. */ 218 RTLDRCPU_32BIT_HACK = 0x7fffffff 219 } RTLDRCPU; 110 220 111 221 … … 498 608 * @param pErrInfo Where to return extended error information. Optional. 499 609 */ 500 intrtldrNativeLoad(const char *pszFilename, uintptr_t *phHandle, uint32_t fFlags, PRTERRINFO pErrInfo);610 DECLHIDDEN(int) rtldrNativeLoad(const char *pszFilename, uintptr_t *phHandle, uint32_t fFlags, PRTERRINFO pErrInfo); 501 611 502 612 /** … … 509 619 * @param phLdrMod Where to return the module handle on success. 510 620 */ 511 int rtldrNativeLoadSystem(const char *pszFilename, const char *pszExt, uint32_t fFlags, PRTLDRMOD phLdrMod); 512 513 int rtldrPEOpen(PRTLDRREADER pReader, uint32_t fFlags, RTLDRARCH enmArch, RTFOFF offNtHdrs, PRTLDRMOD phLdrMod, PRTERRINFO pErrInfo); 514 int rtldrELFOpen(PRTLDRREADER pReader, uint32_t fFlags, RTLDRARCH enmArch, PRTLDRMOD phLdrMod, PRTERRINFO pErrInfo); 515 int rtldrkLdrOpen(PRTLDRREADER pReader, uint32_t fFlags, RTLDRARCH enmArch, PRTLDRMOD phLdrMod, PRTERRINFO pErrInfo); 516 /*int rtldrLXOpen(PRTLDRREADER pReader, uint32_t fFlags, RTLDRARCH enmArch, RTFOFF offLX, PRTLDRMOD phLdrMod); 517 int rtldrMachoOpen(PRTLDRREADER pReader, uint32_t fFlags, RTLDRARCH enmArch, RTFOFF offSomething, PRTLDRMOD phLdrMod);*/ 621 DECLHIDDEN(int) rtldrNativeLoadSystem(const char *pszFilename, const char *pszExt, uint32_t fFlags, PRTLDRMOD phLdrMod); 622 623 DECLHIDDEN(int) rtldrPEOpen(PRTLDRREADER pReader, uint32_t fFlags, RTLDRARCH enmArch, RTFOFF offNtHdrs, PRTLDRMOD phLdrMod, PRTERRINFO pErrInfo); 624 DECLHIDDEN(int) rtldrELFOpen(PRTLDRREADER pReader, uint32_t fFlags, RTLDRARCH enmArch, PRTLDRMOD phLdrMod, PRTERRINFO pErrInfo); 625 DECLHIDDEN(int) rtldrLXOpen(PRTLDRREADER pReader, uint32_t fFlags, RTLDRARCH enmArch, RTFOFF offLxHdr, PRTLDRMOD phLdrMod, PRTERRINFO pErrInfo); 626 DECLHIDDEN(int) rtldrMachOOpen(PRTLDRREADER pReader, uint32_t fFlags, RTLDRARCH enmArch, RTFOFF offImage, PRTLDRMOD phLdrMod, PRTERRINFO pErrInfo); 627 DECLHIDDEN(int) rtldrFatOpen(PRTLDRREADER pReader, uint32_t fFlags, RTLDRARCH enmArch, PRTLDRMOD phLdrMod, PRTERRINFO pErrInfo); 628 DECLHIDDEN(int) rtldrkLdrOpen(PRTLDRREADER pReader, uint32_t fFlags, RTLDRARCH enmArch, PRTLDRMOD phLdrMod, PRTERRINFO pErrInfo); 518 629 519 630 -
trunk/src/VBox/Runtime/r0drv/darwin/mp-r0drv-darwin.cpp
r69111 r74638 301 301 return VERR_NOT_SUPPORTED; 302 302 IPRT_DARWIN_SAVE_EFL_AC(); /* paranoia */ 303 /// @todo use mp_cpus_kick() when available (since 10.10)? It's probably slower (locks, mask iteration, checks), though... 303 304 g_pfnR0DarwinCpuInterrupt(idCpu); 304 305 IPRT_DARWIN_RESTORE_EFL_AC(); -
trunk/src/VBox/Runtime/r3/posix/ldrNative-posix.cpp
r74253 r74638 42 42 43 43 44 intrtldrNativeLoad(const char *pszFilename, uintptr_t *phHandle, uint32_t fFlags, PRTERRINFO pErrInfo)44 DECLHIDDEN(int) rtldrNativeLoad(const char *pszFilename, uintptr_t *phHandle, uint32_t fFlags, PRTERRINFO pErrInfo) 45 45 { 46 46 /* … … 126 126 127 127 128 intrtldrNativeLoadSystem(const char *pszFilename, const char *pszExt, uint32_t fFlags, PRTLDRMOD phLdrMod)128 DECLHIDDEN(int) rtldrNativeLoadSystem(const char *pszFilename, const char *pszExt, uint32_t fFlags, PRTLDRMOD phLdrMod) 129 129 { 130 130 RT_NOREF_PV(pszFilename); RT_NOREF_PV(pszExt); RT_NOREF_PV(fFlags); RT_NOREF_PV(phLdrMod); -
trunk/src/VBox/Runtime/r3/win/ldrNative-win.cpp
r74460 r74638 56 56 57 57 58 intrtldrNativeLoad(const char *pszFilename, uintptr_t *phHandle, uint32_t fFlags, PRTERRINFO pErrInfo)58 DECLHIDDEN(int) rtldrNativeLoad(const char *pszFilename, uintptr_t *phHandle, uint32_t fFlags, PRTERRINFO pErrInfo) 59 59 { 60 60 Assert(sizeof(*phHandle) >= sizeof(HMODULE)); … … 176 176 177 177 178 intrtldrNativeLoadSystem(const char *pszFilename, const char *pszExt, uint32_t fFlags, PRTLDRMOD phLdrMod)178 DECLHIDDEN(int) rtldrNativeLoadSystem(const char *pszFilename, const char *pszExt, uint32_t fFlags, PRTLDRMOD phLdrMod) 179 179 { 180 180 AssertReleaseMsg(g_hModKernel32,
Note:
See TracChangeset
for help on using the changeset viewer.