示例#1
0
DLSyms* dlSymsInit(const char* libPath)
{
  unsigned char* pMem;
  void* pSectionContent;
  int i;
  struct stat st;
  Elf_Shdr* pS;
  DLSyms* pSyms = (DLSyms*)dlAllocMem(sizeof(DLSyms));
  memset(pSyms, 0, sizeof(DLSyms));
  pSyms->file = open(libPath, O_RDONLY);
  stat(libPath, &st);
  pSyms->fileSize = st.st_size;
  pSyms->pElf_Ehdr = (Elf_Ehdr*) mmap((void*) NULL, pSyms->fileSize, PROT_READ, MAP_SHARED, pSyms->file, 0);

#ifdef ABI_ELF32
  assert(pSyms->pElf_Ehdr->e_ident[EI_CLASS] == ELFCLASS32);
#else
  assert(pSyms->pElf_Ehdr->e_ident[EI_CLASS] == ELFCLASS64);
#endif

  assert(pSyms->pElf_Ehdr->e_phoff > 0);
  assert(pSyms->pElf_Ehdr->e_shoff > 0);


  pMem = (unsigned char*)pSyms->pElf_Ehdr;
  
  /* traverse section headers */
  pS = (Elf_Shdr*) ( pMem + pSyms->pElf_Ehdr->e_shoff );
  /* skip section 0 which is always zero due to the Elf standard. */
  for (i = 1; i < pSyms->pElf_Ehdr->e_shnum; i++) 
  {
    Elf_Shdr* pSection = &pS[i];
    pSectionContent = ((char*)pMem) + pSection->sh_offset;
    switch (pSection->sh_type)
    {
      case SHT_DYNSYM:
        if (!pSyms->pSymTab) {
          pSyms->pSymTab  = (Elf_Sym*)pSectionContent;
          pSyms->nSymbols = pSection->sh_size / pSection->sh_entsize;
        }
        break;
      case SHT_STRTAB:
        // Do not trust pSyms->pElf_Ehdr->e_shstrndx!
        if (!pSyms->pStrTab) {
          pSyms->pStrTab  = (const char*)pSectionContent;
          pSyms->strTabSize = pSection->sh_size;
        }
        break;
    }
    if (pSyms->pSymTab && pSyms->pStrTab)
      break;
  }
  return pSyms;
}
示例#2
0
DLSyms* dlSymsInit(const char* libPath)
{
  DLLib* pLib = dlLoadLibrary(libPath);
  DLSyms* pSyms = (DLSyms*)dlAllocMem(sizeof(DLSyms));
  const char* base = (const char*) pLib;
  IMAGE_DOS_HEADER*       pDOSHeader      = (IMAGE_DOS_HEADER*) base;  
  IMAGE_NT_HEADERS*       pNTHeader       = (IMAGE_NT_HEADERS*) ( base + pDOSHeader->e_lfanew );  
  IMAGE_DATA_DIRECTORY*   pExportsDataDir = &pNTHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT];
  IMAGE_EXPORT_DIRECTORY* pExports        = (IMAGE_EXPORT_DIRECTORY*) (base + pExportsDataDir->VirtualAddress);  

  pSyms->pBase  = base;
  pSyms->pNames = (DWORD*)(base + pExports->AddressOfNames);
  pSyms->pFuncs = (DWORD*)(base + pExports->AddressOfFunctions);
  pSyms->pOrds  = (unsigned short*)(base + pExports->AddressOfNameOrdinals);
  pSyms->count  = (size_t)pExports->NumberOfNames;
  pSyms->pLib   = pLib;

  return pSyms;
}