Changeset 25 for trunk/kLdr/kLdrModMachO.c
- Timestamp:
- Feb 19, 2009, 1:56:15 AM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/kLdr/kLdrModMachO.c
r23 r25 127 127 void *pvMapping; 128 128 129 /** The offset of the image. (FAT fun.) */ 130 KLDRFOFF offImage; 129 131 /** The link address. */ 130 132 KLDRADDR LinkAddress; … … 184 186 PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser); 185 187 186 static int kldrModMachODoCreate(PKRDR pRdr, PKLDRMODMACHO *ppMod);187 static int kldrModMachOPreParseLoadCommands(KU8 *pbLoadCommands, const mach_header_32_t *pHdr, PKRDR pRdr, 188 static int kldrModMachODoCreate(PKRDR pRdr, KLDRFOFF offImage, PKLDRMODMACHO *ppMod); 189 static int kldrModMachOPreParseLoadCommands(KU8 *pbLoadCommands, const mach_header_32_t *pHdr, PKRDR pRdr, KLDRFOFF offImage, 188 190 KU32 *pcSegments, KU32 *pcSections, KU32 *pcbStringPool); 189 191 static int kldrModMachOParseLoadCommands(PKLDRMODMACHO pModMachO, char *pbStringPool, KU32 cbStringPool); … … 228 230 * @param pOps Pointer to the registered method table. 229 231 * @param pRdr The file provider instance to use. 232 * @param fFlags Flags, MBZ. 233 * @param enmCpuArch The desired CPU architecture. KCPUARCH_UNKNOWN means 234 * anything goes, but with a preference for the current 235 * host architecture. 230 236 * @param offNewHdr The offset of the new header in MZ files. -1 if not found. 231 237 * @param ppMod Where to store the module instance pointer. 232 238 */ 233 static int kldrModMachOCreate(PCKLDRMODOPS pOps, PKRDR pRdr, K LDRFOFF offNewHdr, PPKLDRMOD ppMod)239 static int kldrModMachOCreate(PCKLDRMODOPS pOps, PKRDR pRdr, KU32 fFlags, KCPUARCH enmCpuArch, KLDRFOFF offNewHdr, PPKLDRMOD ppMod) 234 240 { 235 241 PKLDRMODMACHO pModMachO; … … 239 245 * Create the instance data and do a minimal header validation. 240 246 */ 241 rc = kldrModMachODoCreate(pRdr, &pModMachO);247 rc = kldrModMachODoCreate(pRdr, offNewHdr == -1 ? 0 : offNewHdr, &pModMachO); 242 248 if (!rc) 243 249 { 244 pModMachO->pMod->pOps = pOps; 245 pModMachO->pMod->u32Magic = KLDRMOD_MAGIC; 246 *ppMod = pModMachO->pMod; 247 return 0; 250 251 /* 252 * Match up against the requested CPU architecture. 253 */ 254 if ( enmCpuArch == KCPUARCH_UNKNOWN 255 || pModMachO->pMod->enmArch == enmCpuArch) 256 { 257 pModMachO->pMod->pOps = pOps; 258 pModMachO->pMod->u32Magic = KLDRMOD_MAGIC; 259 *ppMod = pModMachO->pMod; 260 return 0; 261 } 262 rc = KLDR_ERR_CPU_ARCH_MISMATCH; 248 263 } 249 264 if (pModMachO) … … 260 275 * simplify cleanup on failure. 261 276 */ 262 static int kldrModMachODoCreate(PKRDR pRdr, PKLDRMODMACHO *ppModMachO)277 static int kldrModMachODoCreate(PKRDR pRdr, KLDRFOFF offImage, PKLDRMODMACHO *ppModMachO) 263 278 { 264 279 union … … 286 301 * Read the Mach-O header. 287 302 */ 288 rc = kRdrRead(pRdr, &s, sizeof(s), 0);303 rc = kRdrRead(pRdr, &s, sizeof(s), offImage); 289 304 if (rc) 290 305 return rc; … … 327 342 return KERR_NO_MEMORY; 328 343 rc = kRdrRead(pRdr, pbLoadCommands, s.Hdr32.sizeofcmds, 329 s.Hdr32.magic == IMAGE_MACHO32_SIGNATURE 330 || s.Hdr32.magic == IMAGE_MACHO32_SIGNATURE_OE 331 ? sizeof(mach_header_32_t) : sizeof(mach_header_64_t)); 344 s.Hdr32.magic == IMAGE_MACHO32_SIGNATURE 345 || s.Hdr32.magic == IMAGE_MACHO32_SIGNATURE_OE 346 ? sizeof(mach_header_32_t) + offImage 347 : sizeof(mach_header_64_t) + offImage); 332 348 if (!rc) 333 rc = kldrModMachOPreParseLoadCommands(pbLoadCommands, &s.Hdr32, pRdr, &cSegments, &cSections, &cbStringPool);349 rc = kldrModMachOPreParseLoadCommands(pbLoadCommands, &s.Hdr32, pRdr, offImage, &cSegments, &cSections, &cbStringPool); 334 350 if (rc) 335 351 { … … 354 370 *ppModMachO = pModMachO; 355 371 pModMachO->pbLoadCommands = pbLoadCommands; 372 pModMachO->offImage = offImage; 356 373 357 374 /* KLDRMOD */ … … 475 492 * @param pHdr The header. 476 493 * @param pRdr The file reader. 494 * @param offImage The image header (FAT fun). 477 495 * @param pcSegments Where to store the segment count. 478 496 * @param pcSegments Where to store the section count. 479 497 * @param pcbStringPool Where to store the string pool size. 480 498 */ 481 static int kldrModMachOPreParseLoadCommands(KU8 *pbLoadCommands, const mach_header_32_t *pHdr, PKRDR pRdr, 499 static int kldrModMachOPreParseLoadCommands(KU8 *pbLoadCommands, const mach_header_32_t *pHdr, PKRDR pRdr, KLDRFOFF offImage, 482 500 KU32 *pcSegments, KU32 *pcSections, KU32 *pcbStringPool) 483 501 { … … 492 510 uuid_command_t *pUuid; 493 511 } u; 494 const KU64 cbFile = kRdrSize(pRdr) ;512 const KU64 cbFile = kRdrSize(pRdr) - offImage; 495 513 KU32 cSegments = 0; 496 514 KU32 cSections = 0; … … 1062 1080 pSectExtra->RVA = pSect->addr; 1063 1081 pSectExtra->LinkAddress = pSect->addr; 1064 pSectExtra->offFile = pSect->offset ? pSect->offset : -1;1082 pSectExtra->offFile = pSect->offset ? pSect->offset + pModMachO->offImage : -1; 1065 1083 pSectExtra->cFixups = pSect->nreloc; 1066 1084 pSectExtra->paFixups = NULL; 1067 pSectExtra->offFixups = pSect->nreloc ? pSect->reloff : -1;1085 pSectExtra->offFixups = pSect->nreloc ? pSect->reloff + pModMachO->offImage : -1; 1068 1086 pSectExtra->fFlags = pSect->flags; 1069 1087 pSectExtra->iSegment = pSegExtra - &pModMachO->aSegments[0]; … … 1099 1117 pSeg->Alignment = (1 << pSect->align); 1100 1118 pSeg->LinkAddress = pSect->addr; 1101 pSeg->offFile = pSect->offset ? pSect->offset : -1;1119 pSeg->offFile = pSect->offset ? pSect->offset + pModMachO->offImage : -1; 1102 1120 pSeg->cbFile = pSect->offset ? pSect->size : -1; 1103 1121 pSeg->RVA = pSect->addr - pModMachO->LinkAddress; … … 1125 1143 && pSeg[-1].cbFile == pSeg[-1].cb) 1126 1144 { 1127 int fOk = pSeg[-1].offFile + (pSect->addr - pSeg[-1].LinkAddress) == pSect->offset 1145 int fOk = pSeg[-1].offFile + (pSect->addr - pSeg[-1].LinkAddress) == pSect->offset + pModMachO->offImage 1128 1146 && pSect[-1].offset 1129 && pSeg[-1].offFile + pSeg[-1].cbFile == pSect[-1].offset + p Sect[-1].size;1147 && pSeg[-1].offFile + pSeg[-1].cbFile == pSect[-1].offset + pModMachO->offImage + pSect[-1].size; 1130 1148 /* more checks? */ 1131 1149 if (fOk) … … 1179 1197 pSectExtra->RVA = pSect->addr; 1180 1198 pSectExtra->LinkAddress = pSect->addr; 1181 pSectExtra->offFile = pSect->offset ? pSect->offset : -1;1199 pSectExtra->offFile = pSect->offset ? pSect->offset + pModMachO->offImage : -1; 1182 1200 pSectExtra->cFixups = pSect->nreloc; 1183 1201 pSectExtra->paFixups = NULL; 1184 pSectExtra->offFixups = pSect->nreloc ? pSect->reloff : -1;1202 pSectExtra->offFixups = pSect->nreloc ? pSect->reloff + pModMachO->offImage : -1; 1185 1203 pSectExtra->fFlags = pSect->flags; 1186 1204 pSectExtra->iSegment = pSegExtra - &pModMachO->aSegments[0]; … … 1216 1234 pSeg->Alignment = (1 << pSect->align); 1217 1235 pSeg->LinkAddress = pSect->addr; 1218 pSeg->offFile = pSect->offset ? pSect->offset : -1;1236 pSeg->offFile = pSect->offset ? pSect->offset + pModMachO->offImage : -1; 1219 1237 pSeg->cbFile = pSect->offset ? pSect->size : -1; 1220 1238 pSeg->RVA = pSect->addr - pModMachO->LinkAddress; … … 1242 1260 && pSeg[-1].cbFile == pSeg[-1].cb) 1243 1261 { 1244 int fOk = pSeg[-1].offFile + (pSect->addr - pSeg[-1].LinkAddress) == pSect->offset 1262 int fOk = pSeg[-1].offFile + (pSect->addr - pSeg[-1].LinkAddress) == pSect->offset + pModMachO->offImage 1245 1263 && pSect[-1].offset 1246 && pSeg[-1].offFile + pSeg[-1].cbFile == pSect[-1].offset + p Sect[-1].size;1264 && pSeg[-1].offFile + pSeg[-1].cbFile == pSect[-1].offset + pModMachO->offImage + pSect[-1].size; 1247 1265 /* more checks? */ 1248 1266 if (fOk) … … 1277 1295 { 1278 1296 case MH_OBJECT: 1279 pModMachO->offSymbols = u.pSymTab->symoff ;1297 pModMachO->offSymbols = u.pSymTab->symoff + pModMachO->offImage; 1280 1298 pModMachO->cSymbols = u.pSymTab->nsyms; 1281 pModMachO->offStrings = u.pSymTab->stroff ;1299 pModMachO->offStrings = u.pSymTab->stroff + pModMachO->offImage; 1282 1300 pModMachO->cchStrings = u.pSymTab->strsize; 1283 1301 break;
Note:
See TracChangeset
for help on using the changeset viewer.