kernel_mach_header.c [plain text]
#include <vm/vm_map.h>
#include <vm/vm_kern.h>
#include <libkern/kernel_mach_header.h>
#include <string.h> // from libsa
vm_offset_t
getlastaddr(void)
{
kernel_segment_command_t *sgp;
vm_offset_t last_addr = 0;
kernel_mach_header_t *header = &_mh_execute_header;
unsigned long i;
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->vmaddr + sgp->vmsize > last_addr)
last_addr = sgp->vmaddr + sgp->vmsize;
}
sgp = (kernel_segment_command_t *)((uintptr_t)sgp + sgp->cmdsize);
}
return last_addr;
}
void *
getsectdatafromheader(
kernel_mach_header_t *mhp,
const char *segname,
const char *sectname,
unsigned long *size)
{
const kernel_section_t *sp;
void *result;
sp = getsectbynamefromheader(mhp, segname, sectname);
if(sp == (kernel_section_t *)0){
*size = 0;
return((char *)0);
}
*size = sp->size;
result = (void *)sp->addr;
return result;
}
void *
getsegdatafromheader(
kernel_mach_header_t *mhp,
const char *segname,
unsigned long *size)
{
const kernel_segment_command_t *sc;
void *result;
sc = getsegbynamefromheader(mhp, segname);
if(sc == (kernel_segment_command_t *)0){
*size = 0;
return((char *)0);
}
*size = sc->vmsize;
result = (void *)sc->vmaddr;
return result;
}
kernel_section_t *
getsectbynamefromheader(
kernel_mach_header_t *mhp,
const char *segname,
const char *sectname)
{
kernel_segment_command_t *sgp;
kernel_section_t *sp;
unsigned long i, j;
sgp = (kernel_segment_command_t *)
((uintptr_t)mhp + sizeof(kernel_mach_header_t));
for(i = 0; i < mhp->ncmds; i++){
if(sgp->cmd == LC_SEGMENT_KERNEL)
if(strncmp(sgp->segname, segname, sizeof(sgp->segname)) == 0 ||
mhp->filetype == MH_OBJECT){
sp = (kernel_section_t *)((uintptr_t)sgp +
sizeof(kernel_segment_command_t));
for(j = 0; j < sgp->nsects; j++){
if(strncmp(sp->sectname, sectname,
sizeof(sp->sectname)) == 0 &&
strncmp(sp->segname, segname,
sizeof(sp->segname)) == 0)
return(sp);
sp = (kernel_section_t *)((uintptr_t)sp +
sizeof(kernel_section_t));
}
}
sgp = (kernel_segment_command_t *)((uintptr_t)sgp + sgp->cmdsize);
}
return((kernel_section_t *)NULL);
}
kernel_segment_command_t *
getsegbynamefromheader(
kernel_mach_header_t *header,
const char *seg_name)
{
kernel_segment_command_t *sgp;
unsigned long i;
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
&& !strncmp(sgp->segname, seg_name, sizeof(sgp->segname)))
return sgp;
sgp = (kernel_segment_command_t *)((uintptr_t)sgp + sgp->cmdsize);
}
return (kernel_segment_command_t *)NULL;
}
kernel_segment_command_t *
firstseg(void)
{
return firstsegfromheader(&_mh_execute_header);
}
kernel_segment_command_t *
firstsegfromheader(kernel_mach_header_t *header)
{
u_int i = 0;
kernel_segment_command_t *sgp = (kernel_segment_command_t *)
((uintptr_t)header + sizeof(*header));
for (i = 0; i < header->ncmds; i++){
if (sgp->cmd == LC_SEGMENT_KERNEL)
return sgp;
sgp = (kernel_segment_command_t *)((uintptr_t)sgp + sgp->cmdsize);
}
return (kernel_segment_command_t *)NULL;
}
kernel_segment_command_t *
nextsegfromheader(
kernel_mach_header_t *header,
kernel_segment_command_t *seg)
{
u_int i = 0;
kernel_segment_command_t *sgp = (kernel_segment_command_t *)
((uintptr_t)header + sizeof(*header));
for (i = 0; sgp != seg && i < header->ncmds; i++) {
sgp = (kernel_segment_command_t *)((uintptr_t)sgp + sgp->cmdsize);
}
i++;
sgp = (kernel_segment_command_t *)((uintptr_t)sgp + sgp->cmdsize);
for (; i < header->ncmds; i++) {
if (sgp->cmd == LC_SEGMENT_KERNEL) return sgp;
sgp = (kernel_segment_command_t *)((uintptr_t)sgp + sgp->cmdsize);
}
return (kernel_segment_command_t *)NULL;
}
kernel_segment_command_t *
getsegbyname(const char *seg_name)
{
return(getsegbynamefromheader(&_mh_execute_header, seg_name));
}
kernel_section_t *
getsectbyname(
const char *segname,
const char *sectname)
{
return(getsectbynamefromheader(
(kernel_mach_header_t *)&_mh_execute_header, segname, sectname));
}
kernel_section_t *
firstsect(kernel_segment_command_t *sgp)
{
if (!sgp || sgp->nsects == 0)
return (kernel_section_t *)NULL;
return (kernel_section_t *)(sgp+1);
}
kernel_section_t *
nextsect(kernel_segment_command_t *sgp, kernel_section_t *sp)
{
kernel_section_t *fsp = firstsect(sgp);
if (((uintptr_t)(sp - fsp) + 1) >= sgp->nsects)
return (kernel_section_t *)NULL;
return sp+1;
}
#ifdef MACH_KDB
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)) {
return (FALSE);
}
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);
*strtabsize = symtab_cmd->strsize;
if(symtab_cmd->strsize == 0) return (FALSE);
*symtab = seglink_cmd->vmaddr + symtab_cmd->symoff -
seglink_cmd->fileoff;
*strtab = seglink_cmd->vmaddr + symtab_cmd->stroff -
seglink_cmd->fileoff;
return(TRUE);
}
#endif