- int fd;
- struct stat stat_buf;
- struct mach_header *mh;
- char *p;
-
- struct nlist nl[] = {
- { "_kmod_info" },
- { "" },
- };
-
- if((fd = open(module_path, O_RDONLY)) == -1){
- fprintf(stderr, "%s: Can't open: %s\n", progname, module_path);
- exit(KMOD_ERROR_USAGE);
- }
- if (nlist(module_path, nl)) {
- fprintf(stderr, "%s: %s is not a valid kernel module.\n", progname, module_path);
- exit(KMOD_ERROR_USAGE);
- }
- if(fstat(fd, &stat_buf) == -1){
- fprintf(stderr, "%s: Can't stat file: %s\n", progname, module_path);
- exit(KMOD_ERROR_PERMS);
- }
- *object_size = stat_buf.st_size;
- if(map_fd(fd, 0, (vm_offset_t *)object_addr, TRUE, *object_size) != KERN_SUCCESS){
- fprintf(stderr, "%s: Can't map file: %s\n", progname, module_path);
- exit(KMOD_ERROR_INTERNAL);
- }
- close(fd);
-
- if (NXSwapBigLongToHost(*((long *)*object_addr)) == FAT_MAGIC) {
- struct host_basic_info hbi;
- struct fat_header *fh;
- struct fat_arch *fat_archs, *fap;
- unsigned i, nfat_arch;
-
- /* Get our host info */
- i = HOST_BASIC_INFO_COUNT;
- if (host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t)(&hbi), &i) != KERN_SUCCESS) {
- fprintf(stderr, "%s: Can't get host's basic info\n", progname);
- exit(KMOD_ERROR_INTERNAL);
- }
-
- // get number of architectures
- fh = (struct fat_header *)*object_addr;
- nfat_arch = NXSwapBigLongToHost(fh->nfat_arch);
-
- // find beginning of fat_arch struct
- fat_archs = (struct fat_arch *)((char *)fh + sizeof(struct fat_header));
-
- /*
- * Convert archs to host byte ordering (a constraint of
- * cpusubtype_getbestarch()
- */
- for (i = 0; i < nfat_arch; i++) {
- fat_archs[i].cputype =
- NXSwapBigLongToHost(fat_archs[i].cputype);
- fat_archs[i].cpusubtype =
- NXSwapBigLongToHost(fat_archs[i].cpusubtype);
- fat_archs[i].offset =
- NXSwapBigLongToHost(fat_archs[i].offset);
- fat_archs[i].size =
- NXSwapBigLongToHost(fat_archs[i].size);
- fat_archs[i].align =
- NXSwapBigLongToHost(fat_archs[i].align);
- }
-
-// this code was lifted from Darwin/Libraries/NeXT/libc/gen.subproj/nlist.c
-// when cpusubtype_getbestarch exists this code should also be changed.
-#define CPUSUBTYPE_SUPPORT 0
-
-#if CPUSUBTYPE_SUPPORT
- fap = cpusubtype_getbestarch(hbi.cpu_type, hbi.cpu_subtype,
- fat_archs, nfat_arch);
-#else CPUSUBTYPE_SUPPORT
-#warning Use the cpusubtype functions!!!
- fap = NULL;
- for (i = 0; i < nfat_arch; i++) {
- if (fat_archs[i].cputype == hbi.cpu_type) {
- fap = &fat_archs[i];
- break;
- }
- }
-#endif CPUSUBTYPE_SUPPORT
- if (!fap) {
- fprintf(stderr, "%s: could not find the correct architecture in %s.\n", progname, module_path);
- exit(KMOD_ERROR_USAGE);