aboutsummaryrefslogtreecommitdiff
path: root/libdw
diff options
context:
space:
mode:
authorMark Wielaard <mark@klomp.org>2018-01-18 15:03:43 +0100
committerMark Wielaard <mark@klomp.org>2018-05-19 16:06:21 +0200
commit383eef45f6951482bdb929782422fd8550c9e6e6 (patch)
tree537ffd5d3ed34e2b32ca5c26ac4c1c1a2b7f40d4 /libdw
parent77cbbbd40432ca79f052c3d5bb620afbced0c1a1 (diff)
downloadelfutils-383eef45f6951482bdb929782422fd8550c9e6e6.tar.gz
libdw: dwarf_get_units find split units from .dwo.
For a skeleton DIE dwarf_get_units should provide the split DIE as subdie. This implements that by trying to find the (named) .dwo file and finding the matching CU in it. The .dwo file is search relative to the current DWARF or using the comp_dir of the skeleton DIE. Also fixes a small issue with str_offsets_base_off which is tested in the new testcase (by getting the name of the split CU DIE). Signed-off-by: Mark Wielaard <mark@klomp.org>
Diffstat (limited to 'libdw')
-rw-r--r--libdw/ChangeLog15
-rw-r--r--libdw/Makefile.am3
-rw-r--r--libdw/dwarf_end.c5
-rw-r--r--libdw/dwarf_get_units.c23
-rw-r--r--libdw/libdwP.h14
-rw-r--r--libdw/libdw_find_split_unit.c118
-rw-r--r--libdw/libdw_findcu.c1
7 files changed, 173 insertions, 6 deletions
diff --git a/libdw/ChangeLog b/libdw/ChangeLog
index 385f52c2..a87a7092 100644
--- a/libdw/ChangeLog
+++ b/libdw/ChangeLog
@@ -1,5 +1,20 @@
2018-05-15 Mark Wielaard <mark@klomp.org>
+ * Makefile.am (libdw_a_SOURCES): Add libdw_find_split_unit.c.
+ * dwarf_end.c (cu_free): Free split Dwarf.
+ * dwarf_get_units.c (dwarf_get_units): Handle DW_UT_skeleton by
+ calling __libdw_find_split_unit.
+ * libdwP.h (struct Dwarf_CU): Add split Dwarf_CU field.
+ (__libdw_find_split_unit): New function prototype.
+ (str_offsets_base_off): Use cu Dwarf if dbg is NULL.
+ (filepath): Rename to ...
+ (__libdw_filepath): This. Which is the actual function name in
+ dwarf_getalt.c.
+ (libdw_find_split_unit.c): New file.
+ * libdw_findcu.c (__libdw_intern_next_unit): Initialize split to -1.
+
+2018-05-15 Mark Wielaard <mark@klomp.org>
+
* libdwP.h (__libdw_first_die_from_cu_start): Adjust commented out
asserts.
* libdw_findcu.c (__libdw_intern_next_unit): For version 4 DWARF if
diff --git a/libdw/Makefile.am b/libdw/Makefile.am
index 8848f141..ba9919c4 100644
--- a/libdw/Makefile.am
+++ b/libdw/Makefile.am
@@ -90,7 +90,8 @@ libdw_a_SOURCES = dwarf_begin.c dwarf_begin_elf.c dwarf_end.c dwarf_getelf.c \
dwarf_getlocation_die.c dwarf_getlocation_attr.c \
dwarf_getalt.c dwarf_setalt.c dwarf_cu_getdwarf.c \
dwarf_cu_die.c dwarf_peel_type.c dwarf_default_lower_bound.c \
- dwarf_die_addr_die.c dwarf_get_units.c
+ dwarf_die_addr_die.c dwarf_get_units.c \
+ libdw_find_split_unit.c
if MAINTAINER_MODE
BUILT_SOURCES = $(srcdir)/known-dwarf.h
diff --git a/libdw/dwarf_end.c b/libdw/dwarf_end.c
index f6915abf..43223de0 100644
--- a/libdw/dwarf_end.c
+++ b/libdw/dwarf_end.c
@@ -55,6 +55,11 @@ cu_free (void *arg)
Dwarf_Abbrev_Hash_free (&p->abbrev_hash);
tdestroy (p->locs, noop_free);
+
+ /* Free split dwarf one way (from skeleton to split). */
+ if (p->unit_type == DW_UT_skeleton
+ && p->split != NULL && p->split != (void *)-1)
+ INTUSE(dwarf_end) (p->split->dbg);
}
diff --git a/libdw/dwarf_get_units.c b/libdw/dwarf_get_units.c
index 19ff5de1..aece17ef 100644
--- a/libdw/dwarf_get_units.c
+++ b/libdw/dwarf_get_units.c
@@ -100,10 +100,25 @@ dwarf_get_units (Dwarf *dwarf, Dwarf_CU *cu, Dwarf_CU **next_cu,
if (subdie != NULL)
{
- if (next->version >= 2 && next->version <= 5
- && (next->unit_type == DW_UT_type
- || next->unit_type == DW_UT_split_type))
- *subdie = SUBDIE(next);
+ if (next->version >= 2 && next->version <= 5)
+ {
+ /* For types, return the actual type DIE. For skeletons,
+ find the associated split compile unit and return its
+ DIE. */
+ if (next->unit_type == DW_UT_type
+ || next->unit_type == DW_UT_split_type)
+ *subdie = SUBDIE(next);
+ else if (next->unit_type == DW_UT_skeleton)
+ {
+ Dwarf_CU *split_cu = __libdw_find_split_unit (next);
+ if (split_cu != NULL)
+ *subdie = CUDIE(split_cu);
+ else
+ memset (subdie, '\0', sizeof (Dwarf_Die));
+ }
+ else
+ memset (subdie, '\0', sizeof (Dwarf_Die));
+ }
else
memset (subdie, '\0', sizeof (Dwarf_Die));
}
diff --git a/libdw/libdwP.h b/libdw/libdwP.h
index 25a5ad31..60572276 100644
--- a/libdw/libdwP.h
+++ b/libdw/libdwP.h
@@ -314,6 +314,12 @@ struct Dwarf_CU
size_t subdie_offset;
uint64_t unit_id8;
+ /* If this is a skeleton unit this points to the split compile unit.
+ Or the other way around if this is a split compile unit. Set to -1
+ if not yet searched. Always use __libdw_find_split_unit to access
+ this field. */
+ struct Dwarf_CU *split;
+
/* Hash table for the abbreviations. */
Dwarf_Abbrev_Hash abbrev_hash;
/* Offset of the first abbreviation. */
@@ -589,6 +595,9 @@ extern struct Dwarf_CU *__libdw_findcu (Dwarf *dbg, Dwarf_Off offset, bool tu)
extern struct Dwarf_CU *__libdw_findcu_addr (Dwarf *dbg, void *addr)
__nonnull_attribute__ (1) internal_function;
+/* Find the split (or skeleton) unit. */
+extern struct Dwarf_CU *__libdw_find_split_unit (Dwarf_CU *cu);
+
/* Get abbreviation with given code. */
extern Dwarf_Abbrev *__libdw_findabbrev (struct Dwarf_CU *cu,
unsigned int code)
@@ -972,6 +981,9 @@ str_offsets_base_off (Dwarf *dbg, Dwarf_CU *cu)
cu->str_off_base = 0;
return cu->str_off_base;
}
+
+ if (dbg == NULL)
+ dbg = cu->dbg;
}
else
return cu->str_off_base;
@@ -1051,7 +1063,7 @@ static inline Dwarf_Off __libdw_cu_str_off_base (Dwarf_CU *cu)
constructed NULL is returned.
The caller is responsible for freeing the result if not NULL. */
-char * filepath (int fd, const char *dir, const char *file)
+char * __libdw_filepath (int fd, const char *dir, const char *file)
internal_function;
diff --git a/libdw/libdw_find_split_unit.c b/libdw/libdw_find_split_unit.c
new file mode 100644
index 00000000..0f74b39a
--- /dev/null
+++ b/libdw/libdw_find_split_unit.c
@@ -0,0 +1,118 @@
+/* Find the split (or skeleton) unit for a given unit.
+ Copyright (C) 2018 Red Hat, Inc.
+ This file is part of elfutils.
+
+ This file is free software; you can redistribute it and/or modify
+ it under the terms of either
+
+ * the GNU Lesser General Public License as published by the Free
+ Software Foundation; either version 3 of the License, or (at
+ your option) any later version
+
+ or
+
+ * the GNU General Public License as published by the Free
+ Software Foundation; either version 2 of the License, or (at
+ your option) any later version
+
+ or both in parallel, as here.
+
+ elfutils is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received copies of the GNU General Public License and
+ the GNU Lesser General Public License along with this program. If
+ not, see <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include "libdwP.h"
+#include "libelfP.h"
+
+#include <limits.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+
+Dwarf_CU *
+internal_function
+__libdw_find_split_unit (Dwarf_CU *cu)
+{
+ /* Only try once. */
+ if (cu->split != (Dwarf_CU *) -1)
+ return cu->split;
+
+ /* We need a skeleton unit with a comp_dir and [GNU_]dwo_name attributes.
+ The split unit will be the first in the dwo file and should have the
+ same id as the skeleton. */
+ if (cu->unit_type == DW_UT_skeleton)
+ {
+ Dwarf_Die cudie = CUDIE (cu);
+ Dwarf_Attribute compdir, dwo_name;
+ /* It is fine if compdir doesn't exists, but then dwo_name needs
+ to be an absolute path. Also try relative path first. */
+ dwarf_attr (&cudie, DW_AT_comp_dir, &compdir);
+ if (dwarf_attr (&cudie, DW_AT_dwo_name, &dwo_name) != NULL
+ || dwarf_attr (&cudie, DW_AT_GNU_dwo_name, &dwo_name) != NULL)
+ {
+ const char *comp_dir = dwarf_formstring (&compdir);
+ const char *dwo_file = dwarf_formstring (&dwo_name);
+ int fd = cu->dbg->elf->fildes;
+ char *dwo_path = __libdw_filepath (fd, NULL, dwo_file);
+ if (dwo_path == NULL && comp_dir != NULL)
+ dwo_path = __libdw_filepath (fd, comp_dir, dwo_file);
+ if (dwo_path != NULL)
+ {
+ int split_fd = open (dwo_path, O_RDONLY);
+ if (split_fd != -1)
+ {
+ Dwarf *split_dwarf = dwarf_begin (split_fd, DWARF_C_READ);
+ if (split_dwarf != NULL)
+ {
+ Dwarf_CU *split = NULL;
+ while (dwarf_get_units (split_dwarf, split, &split,
+ NULL, NULL, NULL, NULL) == 0)
+ {
+ if (split->unit_type == DW_UT_split_compile
+ && cu->unit_id8 == split->unit_id8)
+ {
+ /* Link skeleton and split compule units. */
+ cu->split = split;
+ split->split = cu;
+
+ /* We have everything we need from this
+ ELF file. And we are going to close
+ the fd to not run out of file
+ descriptors. */
+ elf_cntl (split_dwarf->elf, ELF_C_FDDONE);
+ break;
+ }
+
+ if (cu->split == (Dwarf_CU *) -1)
+ dwarf_end (split_dwarf);
+ }
+ /* Always close, because we don't want to run
+ out of file descriptors. See also the
+ elf_fcntl ELF_C_FDDONE call above. */
+ }
+ close (split_fd);
+ }
+ free (dwo_path);
+ }
+ }
+ }
+
+ /* If we found nothing, make sure we don't try again. */
+ if (cu->split == (Dwarf_CU *) -1)
+ cu->split = NULL;
+
+ return cu->split;
+}
diff --git a/libdw/libdw_findcu.c b/libdw/libdw_findcu.c
index 0a65c973..d6975f34 100644
--- a/libdw/libdw_findcu.c
+++ b/libdw/libdw_findcu.c
@@ -116,6 +116,7 @@ __libdw_intern_next_unit (Dwarf *dbg, bool debug_types)
newp->orig_abbrev_offset = newp->last_abbrev_offset = abbrev_offset;
newp->lines = NULL;
newp->locs = NULL;
+ newp->split = (Dwarf_CU *) -1;
newp->base_address = (Dwarf_Addr) -1;
newp->addr_base = (Dwarf_Off) -1;
newp->str_off_base = (Dwarf_Off) -1;