@@ -206,11 +206,16 @@ CPointer* CBinaryFile::FindSignature(object oSignature)
206206CPointer* CBinaryFile::FindSymbol (char * szSymbol)
207207{
208208#ifdef _WIN32
209- return new CPointer ((unsigned long ) GetProcAddress ((HMODULE) m_ulModule, szSymbol));
209+ void * pAddr = GetProcAddress ((HMODULE) m_ulModule, szSymbol);
210+ if (!pAddr)
211+ BOOST_RAISE_EXCEPTION (PyExc_ValueError, " Could not find symbol: %s" , szSymbol)
212+
213+ return new CPointer ((unsigned long ) pAddr);
210214
211215#elif defined(__linux__)
216+ dlerror ();
212217 void * pResult = dlsym ((void *) m_ulModule, szSymbol);
213- if (pResult )
218+ if (! dlerror () )
214219 return new CPointer ((unsigned long ) pResult);
215220
216221 // -----------------------------------------
@@ -240,23 +245,20 @@ CPointer* CBinaryFile::FindSymbol(char* szSymbol)
240245 if (dlfile == -1 || fstat (dlfile, &dlstat) == -1 )
241246 {
242247 close (dlfile);
243- return new CPointer ();
248+ BOOST_RAISE_EXCEPTION (PyExc_ValueError, " Failed to open file. Symbol: %s " , szSymbol)
244249 }
245250
246251 /* Map library file into memory */
247252 file_hdr = (Elf32_Ehdr *)mmap (NULL , dlstat.st_size , PROT_READ, MAP_PRIVATE, dlfile, 0 );
248253 map_base = (uintptr_t )file_hdr;
249- if (file_hdr == MAP_FAILED)
250- {
251- close (dlfile);
252- return new CPointer ();
253- }
254254 close (dlfile);
255+ if (file_hdr == MAP_FAILED)
256+ BOOST_RAISE_EXCEPTION (PyExc_ValueError, " Failed to map file. Symbol: %s" , szSymbol)
255257
256258 if (file_hdr->e_shoff == 0 || file_hdr->e_shstrndx == SHN_UNDEF)
257259 {
258260 munmap (file_hdr, dlstat.st_size );
259- return new CPointer ();
261+ BOOST_RAISE_EXCEPTION (PyExc_ValueError, " No section header string table has been found. Symbol: %s " , szSymbol)
260262 }
261263
262264 sections = (Elf32_Shdr *)(map_base + file_hdr->e_shoff );
@@ -282,13 +284,12 @@ CPointer* CBinaryFile::FindSymbol(char* szSymbol)
282284 if (symtab_hdr == NULL || strtab_hdr == NULL )
283285 {
284286 munmap (file_hdr, dlstat.st_size );
285- return new CPointer ();
287+ BOOST_RAISE_EXCEPTION (PyExc_ValueError, " No symbol table or string table found. Symbol: %s " , szSymbol)
286288 }
287289
288290 symtab = (Elf32_Sym *)(map_base + symtab_hdr->sh_offset );
289291 strtab = (const char *)(map_base + strtab_hdr->sh_offset );
290292 symbol_count = symtab_hdr->sh_size / symtab_hdr->sh_entsize ;
291- void * sym_addr = NULL ;
292293
293294 /* Iterate symbol table starting from the position we were at last time */
294295 for (uint32_t i = 0 ; i < symbol_count; i++)
@@ -303,15 +304,17 @@ CPointer* CBinaryFile::FindSymbol(char* szSymbol)
303304
304305 if (strcmp (szSymbol, sym_name) == 0 )
305306 {
306- sym_addr = (void *)(dlmap->l_addr + sym.st_value );
307+ pResult = (void *)(dlmap->l_addr + sym.st_value );
307308 break ;
308309 }
309310 }
310311
311312 // Unmap the file now.
312313 munmap (file_hdr, dlstat.st_size );
313- return new CPointer ((unsigned long ) sym_addr);
314+ if (!pResult)
315+ BOOST_RAISE_EXCEPTION (PyExc_ValueError, " Could not find symbol: %s" , szSymbol)
314316
317+ return new CPointer ((unsigned long ) pResult);
315318#else
316319#error "BinaryFile::FindSymbol() is not implemented on this OS"
317320#endif
0 commit comments