int elf_checkFile(void *elfFile) { return ISELF32 (elfFile) ? elf32_checkFile(elfFile) : elf64_checkFile(elfFile); }
static void load_image(void** entry) { char* start = mod_dite_start; char* end = mod_dite_end; if(start == end) { printf("elf-loader: error, dite image is empty!\n"); abort(); } /* awiggins (2004-12-21): Should probably add an alignment check? */ /** awiggins (2005-05-29): Probably should add checks to see * the correct kind of elf file is being loaded for the platform, * or does the elf library do that somewhere?. */ if(!elf32_checkFile((struct Elf32_Header*)start)) { *entry = (void*)(uintptr_t)elf32_getEntryPoint((struct Elf32_Header*)start); } else if(!elf64_checkFile((struct Elf64_Header*)start)) { *entry = (void*)(uintptr_t)elf64_getEntryPoint((struct Elf64_Header*)start); } else { printf("elf-loader: error, dite image not a valid elf file!\n"); abort(); } #ifdef __PPC64__ if (arch_claim_memory((struct Elf32_Header*)start)) { printf("could not claim memory for elf file!\n"); abort(); } #endif if(!elf_loadFile(start, 1)) { printf("elf-loader: error, unable to load dite image!\n"); abort(); } }
/* * prints out some details of one elf file */ void elf64_showDetails(void *elfFile, int size, char *name) { struct Elf64_Phdr *segments; unsigned numSegments; struct Elf64_Shdr *sections; unsigned numSections; int i, r; char *str_table; printf("Found an elf64 file called \"%s\" located " "at address 0x%lx\n", name, elfFile); if ((r = elf64_checkFile(elfFile)) != 0) { char *magic = elfFile; printf("Invalid elf file (%d)\n", r); printf("Magic is: %02.2hhx %02.2hhx %02.2hhx %02.2hhx\n", magic[0], magic[1], magic[2], magic[3]); return; } str_table = elf64_getSegmentStringTable(elfFile); printf("Got str_table... %p\n", str_table); /* * get a pointer to the table of program segments */ segments = elf64_getProgramSegmentTable(elfFile); numSegments = elf64_getNumProgramSegments(elfFile); sections = elf64_getSectionTable(elfFile); numSections = elf64_getNumSections(elfFile); if ((void *) sections > (void *) elfFile + size || (((uintptr_t) sections & 0xf) != 0)) { printf("Corrupted elfFile..\n"); return; } printf("Sections: %p\n", sections); /* * print out info about each section */ /* * print out info about each program segment */ printf("Program Headers:\n"); printf(" Type Offset VirtAddr PhysAddr " "FileSiz MemSiz Flg Align\n"); for (i = 0; i < numSegments; i++) { if (segments[i].p_type != 1) { printf("segment %d is not loadable, " "skipping\n", i); } else { printf(" LOAD 0x%06lx 0x%08lx 0x%08lx" " 0x%05lx 0x%05lx %c%c%c 0x%04lx\n", segments[i].p_offset, segments[i].p_vaddr, segments[i].p_vaddr, segments[i].p_filesz, segments[i].p_memsz, segments[i].p_flags & PF_R ? 'R' : ' ', segments[i].p_flags & PF_W ? 'W' : ' ', segments[i].p_flags & PF_X ? 'E' : ' ', segments[i].p_align); } } printf("Section Headers:\n"); printf(" [Nr] Name Type Addr Off\n"); for (i = 0; i < numSections; i++) { if (elf_checkSection(elfFile, i) == 0) { printf("%-17.17s %-15.15s %08x %06x\n", elf64_getSectionName(elfFile, i), " " /* sections[i].sh_type */ , sections[i].sh_addr, sections[i].sh_offset); } } }