diff options
-rw-r--r-- | src/dlfcn.c | 59 |
1 files changed, 6 insertions, 53 deletions
diff --git a/src/dlfcn.c b/src/dlfcn.c index b52b307..2b2ea07 100644 --- a/src/dlfcn.c +++ b/src/dlfcn.c | |||
@@ -512,39 +512,6 @@ static BOOL get_image_section( HMODULE module, int index, void **ptr, DWORD *siz | |||
512 | return TRUE; | 512 | return TRUE; |
513 | } | 513 | } |
514 | 514 | ||
515 | /* Return symbol name for a given address from import table */ | ||
516 | static const char *get_import_symbol_name( HMODULE module, IMAGE_IMPORT_DESCRIPTOR *iid, void *addr, void **func_address ) | ||
517 | { | ||
518 | int i; | ||
519 | void *candidateAddr = NULL; | ||
520 | const char *candidateName = NULL; | ||
521 | BYTE *base = (BYTE *) module; /* Required to have correct calculations */ | ||
522 | |||
523 | for( i = 0; iid[i].Characteristics != 0 && iid[i].FirstThunk != 0; i++ ) | ||
524 | { | ||
525 | IMAGE_THUNK_DATA *thunkILT = (IMAGE_THUNK_DATA *)( base + iid[i].Characteristics ); | ||
526 | IMAGE_THUNK_DATA *thunkIAT = (IMAGE_THUNK_DATA *)( base + iid[i].FirstThunk ); | ||
527 | |||
528 | for( ; thunkILT->u1.AddressOfData != 0; thunkILT++, thunkIAT++ ) | ||
529 | { | ||
530 | IMAGE_IMPORT_BY_NAME *nameData; | ||
531 | |||
532 | if( IMAGE_SNAP_BY_ORDINAL( thunkILT->u1.Ordinal ) ) | ||
533 | continue; | ||
534 | |||
535 | if( (void *) thunkIAT->u1.Function > addr || candidateAddr >= (void *) thunkIAT->u1.Function ) | ||
536 | continue; | ||
537 | |||
538 | candidateAddr = (void *) thunkIAT->u1.Function; | ||
539 | nameData = (IMAGE_IMPORT_BY_NAME *)( base + (ULONG_PTR) thunkILT->u1.AddressOfData ); | ||
540 | candidateName = (const char *) nameData->Name; | ||
541 | } | ||
542 | } | ||
543 | |||
544 | *func_address = candidateAddr; | ||
545 | return candidateName; | ||
546 | } | ||
547 | |||
548 | /* Return symbol name for a given address from export table */ | 515 | /* Return symbol name for a given address from export table */ |
549 | static const char *get_export_symbol_name( HMODULE module, IMAGE_EXPORT_DIRECTORY *ied, void *addr, void **func_address ) | 516 | static const char *get_export_symbol_name( HMODULE module, IMAGE_EXPORT_DIRECTORY *ied, void *addr, void **func_address ) |
550 | { | 517 | { |
@@ -642,13 +609,11 @@ static void *get_address_from_import_address_table( void *iat, DWORD iat_size, v | |||
642 | /* Holds module filename */ | 609 | /* Holds module filename */ |
643 | static char module_filename[2*MAX_PATH]; | 610 | static char module_filename[2*MAX_PATH]; |
644 | 611 | ||
645 | static BOOL fill_info( HMODULE hModuleImport, void *addr, Dl_info *info ) | 612 | static BOOL fill_info( void *addr, Dl_info *info ) |
646 | { | 613 | { |
647 | HMODULE hModule; | 614 | HMODULE hModule; |
648 | DWORD dwSize; | 615 | DWORD dwSize; |
649 | IMAGE_EXPORT_DIRECTORY *ied; | 616 | IMAGE_EXPORT_DIRECTORY *ied; |
650 | IMAGE_IMPORT_DESCRIPTOR *iid; | ||
651 | const char *name; | ||
652 | void *funcAddress = NULL; | 617 | void *funcAddress = NULL; |
653 | 618 | ||
654 | /* Get module of the specified address */ | 619 | /* Get module of the specified address */ |
@@ -662,23 +627,12 @@ static BOOL fill_info( HMODULE hModuleImport, void *addr, Dl_info *info ) | |||
662 | 627 | ||
663 | info->dli_fname = module_filename; | 628 | info->dli_fname = module_filename; |
664 | info->dli_fbase = (void *) hModule; | 629 | info->dli_fbase = (void *) hModule; |
665 | info->dli_sname = NULL; | ||
666 | 630 | ||
667 | /* First try to find function name and function address in module's export table */ | 631 | /* Find function name and function address in module's export table */ |
668 | if( get_image_section( hModule, IMAGE_DIRECTORY_ENTRY_EXPORT, (void **) &ied, NULL ) ) | 632 | if( get_image_section( hModule, IMAGE_DIRECTORY_ENTRY_EXPORT, (void **) &ied, NULL ) ) |
669 | info->dli_sname = get_export_symbol_name( hModule, ied, addr, &funcAddress ); | 633 | info->dli_sname = get_export_symbol_name( hModule, ied, addr, &funcAddress ); |
670 | 634 | else | |
671 | /* If symbol name is not known and we know which module is importing this address | 635 | info->dli_sname = NULL; |
672 | * then try to find symbol name in this module's import table as the last resort. */ | ||
673 | if( info->dli_sname == NULL && hModuleImport != NULL ) | ||
674 | { | ||
675 | if( get_image_section( hModuleImport, IMAGE_DIRECTORY_ENTRY_IMPORT, (void **) &iid, NULL ) ) | ||
676 | { | ||
677 | name = get_import_symbol_name( hModuleImport, iid, addr, &funcAddress ); | ||
678 | if( name != NULL ) | ||
679 | info->dli_sname = name; | ||
680 | } | ||
681 | } | ||
682 | 636 | ||
683 | info->dli_saddr = info->dli_sname == NULL ? NULL : funcAddress != NULL ? funcAddress : addr; | 637 | info->dli_saddr = info->dli_sname == NULL ? NULL : funcAddress != NULL ? funcAddress : addr; |
684 | 638 | ||
@@ -688,8 +642,6 @@ static BOOL fill_info( HMODULE hModuleImport, void *addr, Dl_info *info ) | |||
688 | DLFCN_EXPORT | 642 | DLFCN_EXPORT |
689 | int dladdr( void *addr, Dl_info *info ) | 643 | int dladdr( void *addr, Dl_info *info ) |
690 | { | 644 | { |
691 | HMODULE hModule = NULL; | ||
692 | |||
693 | if( addr == NULL || info == NULL ) | 645 | if( addr == NULL || info == NULL ) |
694 | return 0; | 646 | return 0; |
695 | 647 | ||
@@ -700,6 +652,7 @@ int dladdr( void *addr, Dl_info *info ) | |||
700 | { | 652 | { |
701 | void *iat; | 653 | void *iat; |
702 | DWORD iatSize; | 654 | DWORD iatSize; |
655 | HMODULE hModule; | ||
703 | 656 | ||
704 | /* Get module of the import thunk address */ | 657 | /* Get module of the import thunk address */ |
705 | if( !GetModuleHandleExA( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, addr, &hModule ) || hModule == NULL ) | 658 | if( !GetModuleHandleExA( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, addr, &hModule ) || hModule == NULL ) |
@@ -732,7 +685,7 @@ int dladdr( void *addr, Dl_info *info ) | |||
732 | return 0; | 685 | return 0; |
733 | } | 686 | } |
734 | 687 | ||
735 | if( !fill_info( hModule, addr, info ) ) | 688 | if( !fill_info( addr, info ) ) |
736 | return 0; | 689 | return 0; |
737 | 690 | ||
738 | return 1; | 691 | return 1; |