sgp = (kernel_segment_command_t *)
((uintptr_t)header + sizeof(kernel_mach_header_t));
for (i = 0; i < header->ncmds; i++){
- if ( sgp->cmd == LC_SEGMENT_KERNEL) {
+ if (sgp->cmd == LC_SEGMENT_KERNEL) {
if (sgp->vmaddr + sgp->vmsize > last_addr)
last_addr = sgp->vmaddr + sgp->vmsize;
}
}
/*
- * Find the UUID load command in the Mach-O headers, and return
- * the address of the UUID blob and size in "*size". If the
- * Mach-O image is missing a UUID, NULL is returned.
+ * Find the specified load command in the Mach-O headers, and return
+ * the command. If there is no such load command, NULL is returned.
*/
void *
-getuuidfromheader(kernel_mach_header_t *mhp, unsigned long *size)
-{
- struct uuid_command *uuidp;
+getcommandfromheader(kernel_mach_header_t *mhp, uint32_t cmd) {
+ struct load_command *lcp;
unsigned long i;
- uuidp = (struct uuid_command *)
- ((uintptr_t)mhp + sizeof(kernel_mach_header_t));
+ lcp = (struct load_command *) (mhp + 1);
for(i = 0; i < mhp->ncmds; i++){
- if(uuidp->cmd == LC_UUID) {
- if (size)
- *size = sizeof(uuidp->uuid);
-
- return (void *)uuidp->uuid;
+ if(lcp->cmd == cmd) {
+ return (void *)lcp;
}
- uuidp = (struct uuid_command *)((uintptr_t)uuidp + uuidp->cmdsize);
+ lcp = (struct load_command *)((uintptr_t)lcp + lcp->cmdsize);
}
return NULL;
}
+/*
+ * Find the UUID load command in the Mach-O headers, and return
+ * the address of the UUID blob and size in "*size". If the
+ * Mach-O image is missing a UUID, NULL is returned.
+ */
+void *
+getuuidfromheader(kernel_mach_header_t *mhp, unsigned long *size)
+{
+ struct uuid_command *cmd = (struct uuid_command *)
+ getcommandfromheader(mhp, LC_UUID);
+
+ if (cmd != NULL) {
+ if (size) {
+ *size = sizeof(cmd->uuid);
+ }
+ return cmd->uuid;
+ }
+
+ return NULL;
+}
+
/*
* This routine returns the a pointer to the data for the named section in the
* named segment if it exist in the mach header passed to it. Also it returns
return result;
}
+/*
+ * This routine returns the offset for the named section in the
+ * named segment if it exist in the mach header passed to it. Otherwise
+ * it returns zero.
+ *
+ * This routine can operate against any kernel mach header.
+ */
+uint32_t
+getsectoffsetfromheader(
+ kernel_mach_header_t *mhp,
+ const char *segname,
+ const char *sectname)
+{
+ const kernel_section_t *sp;
+
+ sp = getsectbynamefromheader(mhp, segname, sectname);
+ if(sp == (kernel_section_t *)0){
+ return(0);
+ }
+
+ return sp->offset;
+}
+
/*
* This routine returns the a pointer to the data for the named segment
* if it exist in the mach header passed to it. Also it returns
return sp+1;
}
-
-#ifdef MACH_KDB
-/*
- * This routine returns the section command for the symbol table in the
- * named segment for the mach_header pointer passed to it if it exist.
- * Otherwise it returns zero.
- */
-static struct symtab_command *
-getsectcmdsymtabfromheader(
- kernel_mach_header_t *mhp)
-{
- kernel_segment_command_t *sgp;
- unsigned long i;
-
- sgp = (kernel_segment_command_t *)
- ((uintptr_t)mhp + sizeof(kernel_mach_header_t));
- for(i = 0; i < mhp->ncmds; i++){
- if(sgp->cmd == LC_SYMTAB)
- return((struct symtab_command *)sgp);
- sgp = (kernel_segment_command_t *)((uintptr_t)sgp + sgp->cmdsize);
- }
- return((struct symtab_command *)NULL);
-}
-
-boolean_t getsymtab(kernel_mach_header_t *header,
- vm_offset_t *symtab,
- int *nsyms,
- vm_offset_t *strtab,
- vm_size_t *strtabsize)
-{
- kernel_segment_command_t *seglink_cmd;
- struct symtab_command *symtab_cmd;
-
- seglink_cmd = NULL;
-
- if((header->magic != MH_MAGIC)
- && (header->magic != MH_MAGIC_64)) { /* Check if this is a valid header format */
- return (FALSE); /* Bye y'all... */
- }
-
- seglink_cmd = getsegbynamefromheader(header,"__LINKEDIT");
- if (seglink_cmd == NULL) {
- return(FALSE);
- }
-
- symtab_cmd = NULL;
- symtab_cmd = getsectcmdsymtabfromheader(header);
- if (symtab_cmd == NULL)
- return(FALSE);
-
- *nsyms = symtab_cmd->nsyms;
- if(symtab_cmd->nsyms == 0) return (FALSE); /* No symbols */
-
- *strtabsize = symtab_cmd->strsize;
- if(symtab_cmd->strsize == 0) return (FALSE); /* Symbol length is 0 */
-
- *symtab = seglink_cmd->vmaddr + symtab_cmd->symoff -
- seglink_cmd->fileoff;
-
- *strtab = seglink_cmd->vmaddr + symtab_cmd->stroff -
- seglink_cmd->fileoff;
-
- return(TRUE);
-}
-#endif