Skip to content

Commit

Permalink
Merge pull request #240 from ntoxeg/master
Browse files Browse the repository at this point in the history
Fix for Binutils API problems
  • Loading branch information
linas committed Jan 23, 2021
2 parents 0f206a3 + c624eb2 commit fa61e4e
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 11 deletions.
73 changes: 73 additions & 0 deletions opencog/util/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ ELSE (APPLE)
SET(APPLE_STRNDUP_FILES)
ENDIF (APPLE)

include(CheckSymbolExists)
include(CheckCSourceCompiles)

# The below are used to automatically find module load paths
ADD_DEFINITIONS(
-DCMAKE_INSTALL_PREFIX="\\"${CMAKE_INSTALL_PREFIX}\\""
Expand Down Expand Up @@ -59,6 +62,76 @@ TARGET_LINK_LIBRARIES(cogutil
)

IF (HAVE_BFD AND HAVE_IBERTY)
check_symbol_exists(bfd_get_section_flags "bfd.h" HAVE_DECL_BFD_GET_SECTION_FLAGS)
check_symbol_exists(bfd_section_flags "bfd.h" HAVE_DECL_BFD_SECTION_FLAGS)

check_symbol_exists(bfd_get_section_vma "bfd.h" HAVE_DECL_BFD_GET_SECTION_VMA)
check_symbol_exists(bfd_section_vma "bfd.h" HAVE_DECL_BFD_SECTION_VMA)

if(HAVE_DECL_BFD_GET_SECTION_FLAGS)
add_compile_definitions(
HAVE_DECL_BFD_GET_SECTION_FLAGS
)
endif()
if(HAVE_DECL_BFD_SECTION_FLAGS)
add_compile_definitions(
HAVE_DECL_BFD_SECTION_FLAGS
)
endif()
if(HAVE_DECL_BFD_GET_SECTION_VMA)
add_compile_definitions(
HAVE_DECL_BFD_GET_SECTION_VMA
)
endif()
if(HAVE_DECL_BFD_SECTION_VMA)
add_compile_definitions(
HAVE_DECL_BFD_SECTION_VMA
)
endif()

# Check if bfd_section_size takes one or two arguments.
set(CMAKE_REQUIRED_LIBRARIES ${BFD_LIBRARY})
check_c_source_compiles(
"#include <bfd.h>
int main(int argc, char** argv) {
asection *sec = malloc(sizeof(*sec));
bfd_section_size(sec);
free(sec);
return 0;
}"
HAVE_1_ARG_BFD_SECTION_SIZE
)
if(HAVE_1_ARG_BFD_SECTION_SIZE)
add_compile_definitions(
HAVE_1_ARG_BFD_SECTION_SIZE
)
else()
add_compile_definitions(
HAVE_2_ARG_BFD_SECTION_SIZE
)
endif()

# Check if bfd_section_vma takes one or two arguments.
check_c_source_compiles(
"#include <bfd.h>
int main(int argc, char** argv) {
asection *sec = malloc(sizeof(*sec));
bfd_section_vma(sec);
free(sec);
return 0;
}"
HAVE_1_ARG_BFD_SECTION_VMA
)
if(HAVE_1_ARG_BFD_SECTION_VMA)
add_compile_definitions(
HAVE_1_ARG_BFD_SECTION_VMA
)
else()
add_compile_definitions(
HAVE_2_ARG_BFD_SECTION_VMA
)
endif()

INCLUDE_DIRECTORIES(${IBERTY_INCLUDE_DIR})
TARGET_LINK_LIBRARIES(cogutil
${BFD_LIBRARY}
Expand Down
35 changes: 24 additions & 11 deletions opencog/util/backtrace-symbols.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,26 +115,39 @@ static void find_address_in_section(bfd *abfd, asection *section, void *data)

if (spot->found) return;

#ifdef bfd_section_flags
#define NEW_BFD_API 1
#endif
#ifdef NEW_BFD_API
#ifdef HAVE_DECL_BFD_SECTION_FLAGS
if ((bfd_section_flags(section) & SEC_ALLOC) == 0) return;
#elif defined HAVE_DECL_BFD_GET_SECTION_FLAGS
if ((bfd_get_section_flags(abfd, section) & SEC_ALLOC) == 0) return;
#else
#error "Unsupported BFD API"
#endif

#ifdef HAVE_DECL_BFD_SECTION_VMA
#ifdef HAVE_1_ARG_BFD_SECTION_VMA
vma = bfd_section_vma(section);
if (spot->pc < vma) return;

size = bfd_section_size(section);
if (spot->pc >= vma + size) return;
#elif defined HAVE_2_ARG_BFD_SECTION_VMA
vma = bfd_section_vma(abfd, section);
#else
#error "Unsupported BFD API"
#endif
#elif defined HAVE_DECL_BFD_GET_SECTION_VMA
vma = bfd_get_section_vma(abfd, section);
#else
if ((bfd_get_section_flags(abfd, section) & SEC_ALLOC) == 0) return;
#error "Unsupported BFD API"
#endif

vma = bfd_get_section_vma(abfd, section);
if (spot->pc < vma) return;

#ifdef HAVE_1_ARG_BFD_SECTION_SIZE
size = bfd_section_size(section);
#elif defined HAVE_2_ARG_BFD_SECTION_SIZE
size = bfd_section_size(abfd, section);
if (spot->pc >= vma + size) return;
#else
#error "Unsupported BFD API"
#endif

if (spot->pc >= vma + size) return;
spot->found = bfd_find_nearest_line(abfd, section, spot->syms, spot->pc - vma,
&(spot->filename), &(spot->functionname), &(spot->line));
}
Expand Down

0 comments on commit fa61e4e

Please sign in to comment.