[SymbolInfo.cc] Support compilation with binutils >= 2.33.1 on Debian.

bfd_get_section_vma macro was removed.
bfd_section_size changed signature.

See inline comment for details.
pull/299/head
Giuseppe Corbelli 2020-01-14 11:50:20 +01:00
parent a3cce71643
commit b4d6ac055f
1 changed files with 21 additions and 14 deletions

View File

@ -49,9 +49,9 @@ SymbolInfo::SymbolInfo( )
m_impl->m_bfd = 0 ; m_impl->m_bfd = 0 ;
m_impl->m_symbols = 0 ; m_impl->m_symbols = 0 ;
m_impl->m_symbol_count = 0 ; m_impl->m_symbol_count = 0 ;
bfd_init( ) ; bfd_init( ) ;
// opening itself // opening itself
bfd *b = bfd_openr( "/proc/self/exe", 0 ) ; bfd *b = bfd_openr( "/proc/self/exe", 0 ) ;
if ( b == NULL ) if ( b == NULL )
@ -60,13 +60,13 @@ SymbolInfo::SymbolInfo( )
<< bfd_errmsg( bfd_get_error() ) << std::endl ; << bfd_errmsg( bfd_get_error() ) << std::endl ;
return ; return ;
} }
if ( bfd_check_format( b, bfd_archive ) ) if ( bfd_check_format( b, bfd_archive ) )
{ {
bfd_close( b ) ; bfd_close( b ) ;
return ; return ;
} }
char **matching ; char **matching ;
if ( !bfd_check_format_matches( b, bfd_object, &matching ) ) if ( !bfd_check_format_matches( b, bfd_object, &matching ) )
{ {
@ -78,7 +78,7 @@ SymbolInfo::SymbolInfo( )
std::cerr << bfd_get_filename( b ) << ": Matching formats: " ; std::cerr << bfd_get_filename( b ) << ": Matching formats: " ;
for ( char **p = matching ; *p != 0 ; p++ ) for ( char **p = matching ; *p != 0 ; p++ )
std::cerr << " " << *p ; std::cerr << " " << *p ;
std::cerr << std::endl ; std::cerr << std::endl ;
std::free( matching ) ; std::free( matching ) ;
} }
@ -107,7 +107,7 @@ struct SymbolInfo::BacktraceInfo
const char *m_func_name ; const char *m_func_name ;
unsigned int m_lineno ; unsigned int m_lineno ;
unsigned int m_is_found ; unsigned int m_is_found ;
static void Callback( bfd *abfd, asection *section, void* addr ) ; static void Callback( bfd *abfd, asection *section, void* addr ) ;
} ; } ;
@ -117,17 +117,24 @@ void SymbolInfo::BacktraceInfo::Callback( bfd *abfd, asection *section,
BacktraceInfo *info = (BacktraceInfo *)data ; BacktraceInfo *info = (BacktraceInfo *)data ;
if ((section->flags & SEC_ALLOC) == 0) if ((section->flags & SEC_ALLOC) == 0)
return ; return ;
bfd_vma vma = bfd_get_section_vma(abfd, section); // bfd_get_section_vma works up to 7b1cfbcf1a27951fb1b3a212995075dd6fdf985b,
// removed in 7c13bc8c91abf291f0206b6608b31955c5ea70d8 (binutils 2.33.1 or so)
// so it's substituted by its implementation to avoid checking for binutils
// version (which at least on Debian SID it's not that easy because the
// version.h is not included with the official package)
bfd_vma vma = section->vma;
unsigned long address = (unsigned long)(info->m_addr); unsigned long address = (unsigned long)(info->m_addr);
if ( address < vma ) if ( address < vma )
return; return;
bfd_size_type size = bfd_section_size(abfd, section); // bfd_section_size changed between the two objects described above,
// same rationale applies
bfd_size_type size = section->size;
if ( address > (vma + size)) if ( address > (vma + size))
return ; return ;
const SymbolInfo *pthis = info->m_pthis ; const SymbolInfo *pthis = info->m_pthis ;
info->m_is_found = bfd_find_nearest_line( abfd, section, info->m_is_found = bfd_find_nearest_line( abfd, section,
pthis->m_impl->m_symbols, pthis->m_impl->m_symbols,
@ -149,7 +156,7 @@ void SymbolInfo::PrintTrace( void *addr, std::ostream& os, std::size_t idx )
{ {
this, addr, 0, 0, 0, false this, addr, 0, 0, 0, false
} ; } ;
Dl_info sym ; Dl_info sym ;
bfd_map_over_sections( m_impl->m_bfd, bfd_map_over_sections( m_impl->m_bfd,
&SymbolInfo::BacktraceInfo::Callback, &SymbolInfo::BacktraceInfo::Callback,
@ -165,7 +172,7 @@ if ( btinfo.m_is_found )
filename.erase( pos, std::strlen( SRC_DIR ) ) ; filename.erase( pos, std::strlen( SRC_DIR ) ) ;
#endif #endif
os << "#" << idx << " " << addr << " " os << "#" << idx << " " << addr << " "
<< filename << ":" << btinfo.m_lineno << filename << ":" << btinfo.m_lineno
<< " " << " "
<< (btinfo.m_func_name != 0 ? Demangle(btinfo.m_func_name) : "" ) << (btinfo.m_func_name != 0 ? Demangle(btinfo.m_func_name) : "" )
<< std::endl ; << std::endl ;