Mercurial > pub > dyncall > dyncall
comparison dynload/dynload_syms_elf.c @ 231:1d774010ddb9
- dynload_syms_elf.c cleanup
author | Tassilo Philipp |
---|---|
date | Sun, 16 Apr 2017 15:05:50 +0200 |
parents | bff2b940ea39 |
children | f5577f6bf97a |
comparison
equal
deleted
inserted
replaced
230:6c7591cef6a8 | 231:1d774010ddb9 |
---|---|
4 Library: dynload | 4 Library: dynload |
5 File: dynload/dynload_syms_elf.c | 5 File: dynload/dynload_syms_elf.c |
6 Description: | 6 Description: |
7 License: | 7 License: |
8 | 8 |
9 Copyright (c) 2007-2015 Daniel Adler <dadler@uni-goettingen.de>, | 9 Copyright (c) 2007-2017 Daniel Adler <dadler@uni-goettingen.de>, |
10 Tassilo Philipp <tphilipp@potion-studios.com>, | 10 Tassilo Philipp <tphilipp@potion-studios.com>, |
11 Olivier Chafik <olivier.chafik@gmail.com> | 11 Olivier Chafik <olivier.chafik@gmail.com> |
12 | 12 |
13 Permission to use, copy, modify, and distribute this software for any | 13 Permission to use, copy, modify, and distribute this software for any |
14 purpose with or without fee is hereby granted, provided that the above | 14 purpose with or without fee is hereby granted, provided that the above |
122 | 122 |
123 | 123 |
124 DLSyms* dlSymsInit(const char* libPath) | 124 DLSyms* dlSymsInit(const char* libPath) |
125 { | 125 { |
126 unsigned char* pMem; | 126 unsigned char* pMem; |
127 void* pSectionContent; | |
128 int i; | 127 int i; |
129 struct stat st; | 128 struct stat st; |
130 Elf_Shdr* pS; | 129 Elf_Shdr* pS; |
131 DLSyms* pSyms = (DLSyms*)dlAllocMem(sizeof(DLSyms)); | 130 DLSyms* pSyms; |
131 | |
132 if(stat(libPath, &st) == -1) | |
133 return NULL; | |
134 | |
135 i = open(libPath, O_RDONLY); | |
136 if(i == -1) | |
137 return NULL; | |
138 | |
139 pSyms = (DLSyms*)dlAllocMem(sizeof(DLSyms)); | |
132 memset(pSyms, 0, sizeof(DLSyms)); | 140 memset(pSyms, 0, sizeof(DLSyms)); |
133 pSyms->file = open(libPath, O_RDONLY); | 141 pSyms->file = i; |
134 stat(libPath, &st); | |
135 pSyms->fileSize = st.st_size; | 142 pSyms->fileSize = st.st_size; |
136 pSyms->pElf_Ehdr = (Elf_Ehdr*) mmap((void*) NULL, pSyms->fileSize, PROT_READ, MAP_SHARED, pSyms->file, 0); | 143 pSyms->pElf_Ehdr = (Elf_Ehdr*) mmap((void*) NULL, pSyms->fileSize, PROT_READ, MAP_SHARED, pSyms->file, 0); |
137 | 144 |
138 #ifdef ABI_ELF32 | 145 #ifdef ABI_ELF32 |
139 assert(pSyms->pElf_Ehdr->e_ident[EI_CLASS] == ELFCLASS32); | 146 assert(pSyms->pElf_Ehdr->e_ident[EI_CLASS] == ELFCLASS32); |
151 pS = (Elf_Shdr*) ( pMem + pSyms->pElf_Ehdr->e_shoff ); | 158 pS = (Elf_Shdr*) ( pMem + pSyms->pElf_Ehdr->e_shoff ); |
152 /* skip section 0 which is always zero due to the Elf standard. */ | 159 /* skip section 0 which is always zero due to the Elf standard. */ |
153 for (i = 1; i < pSyms->pElf_Ehdr->e_shnum; i++) | 160 for (i = 1; i < pSyms->pElf_Ehdr->e_shnum; i++) |
154 { | 161 { |
155 Elf_Shdr* pSection = &pS[i]; | 162 Elf_Shdr* pSection = &pS[i]; |
156 pSectionContent = ((char*)pMem) + pSection->sh_offset; | 163 void* pSectionContent = ((char*)pMem) + pSection->sh_offset; |
157 switch (pSection->sh_type) | 164 switch (pSection->sh_type) |
158 { | 165 { |
159 case SHT_DYNSYM: | 166 case SHT_DYNSYM: |
160 if (!pSyms->pSymTab) { | 167 if (!pSyms->pSymTab) { |
161 pSyms->pSymTab = (Elf_Sym*)pSectionContent; | 168 pSyms->pSymTab = (Elf_Sym*)pSectionContent; |
187 } | 194 } |
188 | 195 |
189 | 196 |
190 int dlSymsCount(DLSyms* pSyms) | 197 int dlSymsCount(DLSyms* pSyms) |
191 { | 198 { |
192 if (!pSyms) | 199 return pSyms ? pSyms->nSymbols : 0; |
193 return 0; | |
194 return pSyms->nSymbols; | |
195 } | 200 } |
196 | 201 |
197 | 202 |
198 const char* dlSymsName(DLSyms* pSyms, int index) | 203 const char* dlSymsName(DLSyms* pSyms, int index) |
199 { | 204 { |
200 int str_index; | 205 int str_index; |
201 if(!pSyms || !pSyms->pSymTab || index < 0 || index >= pSyms->nSymbols) | 206 if(!pSyms || !pSyms->pSymTab || index < 0 || index >= pSyms->nSymbols) |
202 return NULL; | 207 return NULL; |
203 | 208 |
204 str_index = pSyms->pSymTab[index].st_name; | 209 str_index = pSyms->pSymTab[index].st_name; |
205 if (str_index < 0 || str_index >= pSyms->strTabSize) | 210 if (str_index < 0 || str_index >= pSyms->strTabSize) |
206 return NULL; | 211 return NULL; |
207 return &pSyms->pStrTab[str_index]; | 212 return &pSyms->pStrTab[str_index]; |
208 } | 213 } |