Use is_pic field instead of reading ELF header
[babeltrace.git] / lib / so-info.c
index 404425dcf0ba7e98808427f8a4ea28ed9392d367..a5ddfe51ed866a76ac1a71252140829b04970dd4 100644 (file)
@@ -40,6 +40,7 @@
 #include <babeltrace/so-info.h>
 #include <babeltrace/crc32.h>
 #include <babeltrace/babeltrace-internal.h>
+#include <babeltrace/utils.h>
 
 /*
  * An address printed in hex is at most 20 bytes (16 for 64-bits +
@@ -64,7 +65,7 @@ int so_info_init(void)
 
 BT_HIDDEN
 struct so_info *so_info_create(const char *path, uint64_t low_addr,
-               uint64_t memsz)
+               uint64_t memsz, bool is_pic)
 {
        struct so_info *so = NULL;
        GElf_Ehdr *ehdr = NULL;
@@ -112,8 +113,7 @@ struct so_info *so_info_create(const char *path, uint64_t low_addr,
                goto error;
        }
 
-       /* Position independent code has an e_type value of ET_DYN. */
-       so->is_pic = ehdr->e_type == ET_DYN;
+       so->is_pic = is_pic;
        so->memsz = memsz;
        so->low_addr = low_addr;
        so->high_addr = so->low_addr + so->memsz;
@@ -826,11 +826,12 @@ error:
 }
 
 BT_HIDDEN
-int so_info_lookup_function_name(struct so_info *so, uint64_t addr,
+int so_info_lookup_function_name(struct so_info *so, uint64_t ip,
                char **func_name)
 {
        int ret = 0;
        char *_func_name = NULL;
+       uint64_t relative_addr;
 
        if (!so || !func_name) {
                goto error;
@@ -845,32 +846,46 @@ int so_info_lookup_function_name(struct so_info *so, uint64_t addr,
                }
        }
 
-       if (!so_info_has_address(so, addr)) {
+       if (!so_info_has_address(so, ip)) {
                goto error;
        }
 
+       relative_addr = ip - so->low_addr;
        /*
         * Addresses in ELF and DWARF are relative to base address for
         * PIC, so make the address argument relative too if needed.
         */
-       if (so->is_pic) {
-               addr -= so->low_addr;
-       }
-
        if (so->is_elf_only) {
-               ret = so_info_lookup_elf_function_name(so, addr, &_func_name);
+               ret = so_info_lookup_elf_function_name(so,
+                               so->is_pic ? relative_addr : ip,
+                               &_func_name);
        } else {
-               ret = so_info_lookup_dwarf_function_name(so, addr, &_func_name);
+               ret = so_info_lookup_dwarf_function_name(so,
+                               so->is_pic ? relative_addr : ip,
+                               &_func_name);
        }
 
        if (ret) {
                goto error;
        }
 
-       if (_func_name) {
-               *func_name = _func_name;
+       if (!_func_name) {
+               /*
+                * Can't map to a function; fallback to a generic output of the
+                * form binary+/@address.
+                *
+                * FIXME check position independence flag.
+                */
+               const char *binary_name = get_filename_from_path(so->elf_path);
+
+               ret = asprintf(&_func_name, "%s+%#0" PRIx64, binary_name,
+                               relative_addr);
+               if (!_func_name) {
+                       goto error;
+               }
        }
 
+       *func_name = _func_name;
        return 0;
 
 error:
This page took 0.023708 seconds and 4 git commands to generate.