File: libdw_find_split_unit.c

package info (click to toggle)
elfutils 0.194-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 27,680 kB
  • sloc: ansic: 114,970; sh: 35,537; cpp: 4,998; makefile: 1,986; yacc: 1,388; lex: 130; asm: 77; sed: 39; awk: 35
file content (222 lines) | stat: -rw-r--r-- 6,377 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/* 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 "eu-search.h"

#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

static void
try_split_file (Dwarf_CU *cu, const char *dwo_path)
{
  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 (INTUSE(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)
		{
		  if (eu_tsearch (split->dbg, &cu->dbg->split_tree,
				  __libdw_finddbg_cb) == NULL)
		    {
		      /* Something went wrong.  Don't link.  */
		      __libdw_seterrno (DWARF_E_NOMEM);
		      break;
		    }

		  /* Link skeleton and split compile units.  */
		  __libdw_link_skel_split (cu, split);

		  /* 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);
    }
}

static void
try_dwp_file (Dwarf_CU *cu)
{
  if (cu->dbg->dwp_dwarf == NULL)
    {
      if (cu->dbg->elfpath != NULL)
	{
	  /* The DWARF 5 standard says "the package file is typically placed in
	     the same directory as the application, and is given the same name
	     with a '.dwp' extension".  */
	  size_t elfpath_len = strlen (cu->dbg->elfpath);
	  char *dwp_path = malloc (elfpath_len + 5);
	  if (dwp_path == NULL)
	    {
	      __libdw_seterrno (DWARF_E_NOMEM);
	      return;
	    }
	  memcpy (dwp_path, cu->dbg->elfpath, elfpath_len);
	  strcpy (dwp_path + elfpath_len, ".dwp");
	  int dwp_fd = open (dwp_path, O_RDONLY);
	  free (dwp_path);
	  if (dwp_fd != -1)
	    {
	      Dwarf *dwp_dwarf = dwarf_begin (dwp_fd, DWARF_C_READ);
	      /* There's no way to know whether we got the correct file until
		 we look up the unit, but it should at least be a dwp file.  */
	      if (dwp_dwarf != NULL
		  && (dwp_dwarf->sectiondata[IDX_debug_cu_index] != NULL
		      || dwp_dwarf->sectiondata[IDX_debug_tu_index] != NULL))
		{
		  cu->dbg->dwp_dwarf = dwp_dwarf;
		  cu->dbg->dwp_fd = dwp_fd;
		}
	      else
		close (dwp_fd);
	    }
	}
      if (cu->dbg->dwp_dwarf == NULL)
	cu->dbg->dwp_dwarf = (Dwarf *) -1;
    }

  if (cu->dbg->dwp_dwarf != (Dwarf *) -1)
    {
      Dwarf_CU *split = __libdw_dwp_findcu_id (cu->dbg->dwp_dwarf,
					       cu->unit_id8);
      if (split != NULL)
	{
	  if (eu_tsearch (split->dbg, &cu->dbg->split_tree,
			  __libdw_finddbg_cb) == NULL)
	    {
	      /* Something went wrong.  Don't link.  */
	      __libdw_seterrno (DWARF_E_NOMEM);
	      return;
	    }

	  /* Link skeleton and split compile units.  */
	  __libdw_link_skel_split (cu, split);
	}
    }
}

Dwarf_CU *
internal_function
__libdw_find_split_unit (Dwarf_CU *cu)
{
  /* Set result before releasing the lock.  */
  Dwarf_CU *result;

  rwlock_wrlock(cu->split_lock);

  /* Only try once.  */
  if (cu->split != (Dwarf_CU *) -1)
    {
      result = cu->split;
      rwlock_unlock(cu->split_lock);
      return result;
    }

  /* 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)
    {
      /* First, try the dwp file.  */
      try_dwp_file (cu);

      Dwarf_Die cudie = CUDIE (cu);
      Dwarf_Attribute dwo_name;
      /* Try a dwo file.  It is fine if dwo_dir doesn't exist, but then
	 dwo_name needs to be an absolute path.  */
      if (cu->split == (Dwarf_CU *) -1
	  && (dwarf_attr (&cudie, DW_AT_dwo_name, &dwo_name) != NULL
	      || dwarf_attr (&cudie, DW_AT_GNU_dwo_name, &dwo_name) != NULL))
	{
	  /* Try the dwo file name in the same directory
	     as we found the skeleton file.  */
	  const char *dwo_file = dwarf_formstring (&dwo_name);
	  const char *debugdir = cu->dbg->debugdir;
	  char *dwo_path = __libdw_filepath (debugdir, NULL, dwo_file);
	  if (dwo_path != NULL)
	    {
	      try_split_file (cu, dwo_path);
	      free (dwo_path);
	    }

	  if (cu->split == (Dwarf_CU *) -1)
	    {
	      /* Try compdir plus dwo_name.  */
	      Dwarf_Attribute compdir;
	      dwarf_attr (&cudie, DW_AT_comp_dir, &compdir);
	      const char *dwo_dir = dwarf_formstring (&compdir);
	      if (dwo_dir != NULL)
		{
		  dwo_path = __libdw_filepath (debugdir, dwo_dir, dwo_file);
		  if (dwo_path != NULL)
		    {
		      try_split_file (cu, dwo_path);
		      free (dwo_path);
		    }
		}
	    }
	  /* XXX If still not found we could try stripping dirs from the
	     comp_dir and adding them from the comp_dir, assuming
	     someone moved a whole build tree around.  */
	}
    }

  /* If we found nothing, make sure we don't try again.  */
  if (cu->split == (Dwarf_CU *) -1)
    cu->split = NULL;

  result = cu->split;
  rwlock_unlock(cu->split_lock);
  return result;
}