// get sections table Elf32_Shdr* ElfGetSectionHeadersTable (ELF_FILE *elffile) { struct filemap_t *fmap; Elf32_Ehdr *elfHeader; Elf32_Shdr *sectionsTable; if (!elffile) return NULL; if (!elffile->fp) return NULL; // create filemap fmap = filemap_create (elffile->fp); if (!fmap) return NULL; // get elf header elfHeader = ElfGetHeader(elffile->fp); if (!elfHeader) return NULL; // if no section headers table if (elfHeader->e_shnum == 0) return NULL; // else we have one sectionsTable = fmap->map + elfHeader->e_shoff; return sectionsTable; }
// get program headers table Elf32_Phdr* ElfGetProgramHeadersTable (ELF_FILE *elffile) { struct filemap_t *fmap; Elf32_Ehdr *elfHeader; Elf32_Phdr *programHeadersTable; if (!elffile) return NULL; // create filemap fmap = filemap_create (elffile->fp); if (!fmap) return NULL; // get elf header elfHeader = ElfGetHeader(elffile->fp); if (!elfHeader) return NULL; // if no program headers table if (elfHeader->e_phnum == 0) return NULL; // else we have one programHeadersTable = fmap->map + elfHeader->e_phoff; return programHeadersTable; }
// get elf header Elf32_Ehdr* ElfGetHeader (ELF_FILE *elffile) { struct filemap_t *fmap; if (!elffile) return -1; // create filemap fmap = filemap_create (elffile->fp); if (!fmap) return NULL; return fmap->map; }
ELF_FILE* ElfLoad (char *filename) { FILE *fp; ELF_FILE *elf; fp = fopen(filename, "r"); if (!fp) return NULL; elf = calloc(1, sizeof(*elf)); if (!elf) { fclose(fp); return NULL; } elf->filename = strdup(filename); elf->fp = fp; elf->fmap = filemap_create(elf->fp); return elf; }
int fcache_initialize(fcache_context * pfcache, unsigned short islocal, unsigned short cachekey, unsigned int cachesize, unsigned int maxfsize TSRMLS_DC) { int result = NONFATAL; size_t size = 0; fcache_header * header = NULL; unsigned short mapclass = FILEMAP_MAP_SRANDOM; unsigned short locktype = LOCK_TYPE_SHARED; unsigned char isfirst = 1; char evtname[ MAX_PATH]; dprintverbose("start fcache_initialize"); _ASSERT(pfcache != NULL); _ASSERT(cachekey != 0); _ASSERT(cachesize >= FCACHE_SIZE_MINIMUM && cachesize <= FCACHE_SIZE_MAXIMUM); _ASSERT(maxfsize >= FILE_SIZE_MINIMUM && maxfsize <= FILE_SIZE_MAXIMUM); /* Initialize memory map to store code files */ result = filemap_create(&pfcache->pfilemap); if(FAILED(result)) { goto Finished; } pfcache->cachekey = cachekey; if(islocal) { mapclass = FILEMAP_MAP_LRANDOM; locktype = LOCK_TYPE_LOCAL; pfcache->islocal = islocal; } /* shmfilepath = NULL to use page file for shared memory */ result = filemap_initialize(pfcache->pfilemap, FILEMAP_TYPE_FILECONTENT, cachekey, mapclass, cachesize, NULL TSRMLS_CC); if(FAILED(result)) { goto Finished; } pfcache->memaddr = (char *)pfcache->pfilemap->mapaddr; size = filemap_getsize(pfcache->pfilemap TSRMLS_CC); /* Create allocator for filecache segment */ result = alloc_create(&pfcache->palloc); if(FAILED(result)) { goto Finished; } /* initmemory = 1 for all page file backed shared memory allocators */ result = alloc_initialize(pfcache->palloc, islocal, "FILECONTENT_SEGMENT", cachekey, pfcache->memaddr, size, 1 TSRMLS_CC); if(FAILED(result)) { goto Finished; } /* Get memory for cache header */ pfcache->header = (fcache_header *)alloc_get_cacheheader(pfcache->palloc, sizeof(fcache_header), CACHE_TYPE_FILECONTENT); if(pfcache->header == NULL) { result = FATAL_FCACHE_INITIALIZE; goto Finished; } header = pfcache->header; /* Create xread xwrite lock for the filecache */ result = lock_create(&pfcache->prwlock); if(FAILED(result)) { goto Finished; } result = lock_initialize(pfcache->prwlock, "FILECONTENT_CACHE", cachekey, locktype, LOCK_USET_XREAD_XWRITE, NULL TSRMLS_CC); if(FAILED(result)) { goto Finished; } result = lock_getnewname(pfcache->prwlock, "FCACHE_INIT", evtname, MAX_PATH); if(FAILED(result)) { goto Finished; } pfcache->hinitdone = CreateEvent(NULL, TRUE, FALSE, evtname); if(pfcache->hinitdone == NULL) { result = FATAL_FCACHE_INIT_EVENT; goto Finished; } if(GetLastError() == ERROR_ALREADY_EXISTS) { _ASSERT(islocal == 0); isfirst = 0; /* Wait for other process to initialize completely */ WaitForSingleObject(pfcache->hinitdone, INFINITE); } /* Initialize the fcache_header if its not initialized already */ if(islocal || isfirst) { lock_writelock(pfcache->prwlock); header->mapcount = 1; header->itemcount = 0; header->hitcount = 0; header->misscount = 0; SetEvent(pfcache->hinitdone); lock_writeunlock(pfcache->prwlock); } else { /* Increment the mapcount */ InterlockedIncrement(&header->mapcount); } /* Keep maxfsize in fcache_context */ pfcache->maxfsize = maxfsize * 1024; Finished: if(FAILED(result)) { dprintimportant("failure %d in fcache_initialize", result); _ASSERT(result > WARNING_COMMON_BASE); if(pfcache->palloc != NULL) { alloc_terminate(pfcache->palloc); alloc_destroy(pfcache->palloc); pfcache->palloc = NULL; } if(pfcache->pfilemap != NULL) { filemap_terminate(pfcache->pfilemap); filemap_destroy(pfcache->pfilemap); pfcache->pfilemap = NULL; } if(pfcache->prwlock != NULL) { lock_terminate(pfcache->prwlock); lock_destroy(pfcache->prwlock); pfcache->prwlock = NULL; } if(pfcache->hinitdone != NULL) { CloseHandle(pfcache->hinitdone); pfcache->hinitdone = NULL; } } dprintverbose("end fcache_initialize"); return result; }
void elf_header_print_section_info (char *binaryName) { struct filemap_t *fmap; size_t idx, offName; char **sectionNamesTable; // section names length size_t maxSectionNameLength = 0, sectionNameLength; // number of spaces to print size_t numSpaces, iSpace; FILE *fp; ELF_FILE *elf_file; Elf32_Ehdr *elfHeader; Elf32_Shdr *sectionsHeadersTable; Elf32_Shdr *sectionNamesTableHeader; if (!binaryName) return; // fp = fopen(binaryName, "r"); if (!fp) return; // create filemap fmap = filemap_create (binaryName); if (!fmap) return; // load file elf_file = ElfLoad (binaryName); if(!elf_file) return; // get elf header elfHeader = ElfGetHeader (elf_file); if(!elfHeader) return; // get sections headers sectionsHeadersTable = ElfGetSectionHeadersTable (fp); if (!sectionsHeadersTable) { debug_printf (MESSAGE_ERROR, stderr, "error: elf_section_names_print: No section headers table\n"); return; } // get section names table sectionNamesTable = ElfGetSectionNamesTable (fp); if (!sectionNamesTable) { debug_printf (MESSAGE_ERROR, stderr, "error: elf_section_names_print: No section names table\n"); return; } // get section names table start sectionNamesTableHeader = &(ElfGetSectionHeadersTable (fp)[elfHeader->e_shstrndx]); // we search longest section name length for (idx = 0; idx < elfHeader->e_shnum; idx++) { offName = sectionsHeadersTable[idx].sh_name; sectionNameLength = strlen(sectionNamesTable[idx]); if (sectionNameLength > maxSectionNameLength) maxSectionNameLength = sectionNameLength; } // print section names printf("== SECTIONS\n"); printf(" Idx\tName"); // print spaces for (iSpace = 0; iSpace < maxSectionNameLength; iSpace++) putchar(' '); printf("\tSize\t VMA\tLMA File Off Algn\n"); for (idx = 0; idx < elfHeader->e_shnum; idx++) { // index of name in section names table, offName = offset from beginning of table offName = sectionsHeadersTable[idx].sh_name; // //printf("%6u\t%s", idx, sectionNamesTable[idx]); printf("%6u\t%s", idx, fmap->map + sectionNamesTableHeader->sh_offset + offName); // get number of spaces to print numSpaces = maxSectionNameLength - strlen(fmap->map + sectionNamesTableHeader->sh_offset + offName); // print spaces for (iSpace = 0; iSpace < numSpaces; iSpace++) putchar(' '); printf("\t%u\t 0x%08x\n", sectionsHeadersTable[idx].sh_size, sectionsHeadersTable[idx].sh_addr); } printf("== SECTIONS END\n\n"); free(sectionNamesTable); }