Mercurial > pub > dyncall > dyncall
view dynload/dynload_syms_elf.c @ 533:71c884e610f0
- integration of patches from Raphael Luba, Thekla, Inc.:
* integration of aggregate-by-value (struct, union) support patch for x64 (win and sysv)
* windows/x64 asm additions to specify how stack unwinds (help for debuggers, exception handling, etc.)
* see Changelog for details
- new calling convention modes for thiscalls (platform agnostic, was specific before)
* new signature character for platform agnostic thiscalls ('*' / DC_SIGCHAR_CC_THISCALL)
- dcCallF(), dcVCallF(), dcArgF() and dcVArgF():
* added support for aggregates-by-value (wasn't part of patch)
* change that those functions don't implicitly call dcReset() anymore, which was unflexible (breaking change)
- added macros to feature test implementation for aggregate-by-value and syscall support
- changed libdyncall_s.lib and libdyncallback_s.lib order in callback test makefiles, as some toolchains are picky about order
- doc:
* man page updates to describe aggregate interface
* manual overview changes to highlight platforms with aggregate-by-value support
- test/plain: replaced tests w/ old/stale sctruct interface with new aggregate one
author | Tassilo Philipp |
---|---|
date | Thu, 21 Apr 2022 13:35:47 +0200 |
parents | c7f879fef352 |
children | 6a8aac9b2bcf |
line wrap: on
line source
/* Package: dyncall Library: dynload File: dynload/dynload_syms_elf.c Description: License: Copyright (c) 2007-2018 Daniel Adler <dadler@uni-goettingen.de>, Tassilo Philipp <tphilipp@potion-studios.com>, Olivier Chafik <olivier.chafik@gmail.com> Permission to use, copy, modify, and distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #include "../autovar/autovar_OS.h" /* dynamic symbol resolver for elf */ #include "dynload.h" #if defined(OS_OpenBSD) # include <stdint.h> # include <elf_abi.h> #elif defined(OS_NetBSD) # include <stddef.h> # include <elf.h> #elif defined(OS_SunOS) # include <libelf.h> #else # include <elf.h> #endif #if defined(__GLIBC__) # define _GNU_SOURCE # define __USE_GNU #endif #include "dynload_alloc.h" #include <assert.h> #include <fcntl.h> #include <dlfcn.h> #include <stdio.h> #include <string.h> #include <sys/stat.h> #include <sys/types.h> #include <sys/mman.h> #include <unistd.h> /* run-time configuration 64/32 */ #if defined(OS_OpenBSD) #else # include "../autovar/autovar_ABI.h" # ifdef ABI_ELF64 typedef Elf64_Ehdr Elf_Ehdr; typedef Elf64_Phdr Elf_Phdr; typedef Elf64_Shdr Elf_Shdr; typedef Elf64_Sym Elf_Sym; # ifndef OS_SunOS typedef Elf64_Dyn Elf_Dyn; # endif typedef Elf64_Sxword Elf_tag; typedef Elf64_Addr Elf_Addr; # else typedef Elf32_Ehdr Elf_Ehdr; typedef Elf32_Phdr Elf_Phdr; typedef Elf32_Shdr Elf_Shdr; typedef Elf32_Sym Elf_Sym; # ifndef OS_SunOS typedef Elf32_Dyn Elf_Dyn; # endif typedef Elf32_Sword Elf_tag; typedef Elf32_Addr Elf_Addr; # endif #endif struct DLSyms_ { const char* pStrTab; Elf_Sym* pSymTab; size_t strTabSize; size_t nSymbols; Elf_Ehdr* pElf_Ehdr; /* pointer to elf header */ int file; /* fd of lib */ size_t fileSize; /* filesize of open lib */ }; DLSyms* dlSymsInit(const char* libPath) { unsigned char* pMem; int i; struct stat st; Elf_Shdr* pS; DLSyms* pSyms; if(stat(libPath, &st) == -1) return NULL; i = open(libPath, O_RDONLY); if(i == -1) return NULL; pSyms = (DLSyms*)dlAllocMem(sizeof(DLSyms)); memset(pSyms, 0, sizeof(DLSyms)); pSyms->file = i; 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]; void* 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; } void dlSymsCleanup(DLSyms* pSyms) { if(pSyms) { munmap((void*) pSyms->pElf_Ehdr, pSyms->fileSize); close(pSyms->file); dlFreeMem(pSyms); } } int dlSymsCount(DLSyms* pSyms) { return pSyms ? pSyms->nSymbols : 0; } const char* dlSymsName(DLSyms* pSyms, int index) { int str_index; if(!pSyms || !pSyms->pSymTab || index < 0 || index >= pSyms->nSymbols) return NULL; str_index = pSyms->pSymTab[index].st_name; if (str_index < 0 || str_index >= pSyms->strTabSize) return NULL; return &pSyms->pStrTab[str_index]; } const char* dlSymsNameFromValue(DLSyms* pSyms, void* value) { Dl_info info; return (dladdr(value, &info) && (value == info.dli_saddr)) ? info.dli_sname : NULL; }