summaryrefslogtreecommitdiff
path: root/libdw/dwarf_getalt.c
blob: 0a12dfae96772bf49033da744d5db9255aaf3440 (plain)
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
/* Retrieves the DWARF descriptor for debugaltlink data.
   Copyright (C) 2014, 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 "libdwelfP.h"
#include "system.h"

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


char *
internal_function
__libdw_filepath (const char *debugdir, const char *dir, const char *file)
{
  if (file == NULL)
    return NULL;

  if (file[0] == '/')
    return strdup (file);

  if (dir != NULL && dir[0] == '/')
    {
      size_t dirlen = strlen (dir);
      size_t filelen = strlen (file);
      size_t len = dirlen + 1 + filelen + 1;
      char *path = malloc (len);
      if (path != NULL)
	{
	  char *c = mempcpy (path, dir, dirlen);
	  if (dir[dirlen - 1] != '/')
	    *c++ = '/';
	  mempcpy (c, file, filelen + 1);
	}
      return path;
    }

  if (debugdir != NULL)
    {
      size_t debugdirlen = strlen (debugdir);
      size_t dirlen = dir != NULL ? strlen (dir) : 0;
      size_t filelen = strlen (file);
      size_t len = debugdirlen + 1 + dirlen + 1 + filelen + 1;
      char *path = malloc (len);
      if (path != NULL)
	{
	  char *c = mempcpy (path, debugdir, debugdirlen);
	  if (dirlen > 0)
	    {
	      c = mempcpy (c, dir, dirlen);
	      if (dir[dirlen - 1] != '/')
		*c++ = '/';
	    }
	  mempcpy (c, file, filelen + 1);
	  return path;
	}
    }

  return NULL;
}

static void
find_debug_altlink (Dwarf *dbg)
{
  const char *altname;
  const void *build_id;
  ssize_t build_id_len = INTUSE(dwelf_dwarf_gnu_debugaltlink) (dbg,
							       &altname,
							       &build_id);

  /* Couldn't even get the debugaltlink.  It probably doesn't exist.  */
  if (build_id_len <= 0)
    return;

  const uint8_t *id = (const uint8_t *) build_id;
  size_t id_len = build_id_len;
  int fd = -1;

  /* We only look in the standard path.  And relative to the dbg file.  */
#define DEBUGINFO_PATH "/usr/lib/debug"

  /* We don't handle very short or really large build-ids.  We need at
     at least 3 and allow for up to 64 (normally ids are 20 long).  */
#define MIN_BUILD_ID_BYTES 3
#define MAX_BUILD_ID_BYTES 64
  if (id_len >= MIN_BUILD_ID_BYTES && id_len <= MAX_BUILD_ID_BYTES)
    {
      /* Note sizeof a string literal includes the trailing zero.  */
      char id_path[sizeof DEBUGINFO_PATH - 1 + sizeof "/.build-id/" - 1
		   + 2 + 1 + (MAX_BUILD_ID_BYTES - 1) * 2 + sizeof ".debug"];
      sprintf (&id_path[0], "%s%s", DEBUGINFO_PATH, "/.build-id/");
      sprintf (&id_path[sizeof DEBUGINFO_PATH - 1 + sizeof "/.build-id/" - 1],
	       "%02" PRIx8 "/", (uint8_t) id[0]);
      for (size_t i = 1; i < id_len; ++i)
	sprintf (&id_path[sizeof DEBUGINFO_PATH - 1 + sizeof "/.build-id/" - 1
			  + 3 + (i - 1) * 2], "%02" PRIx8, (uint8_t) id[i]);
      strcpy (&id_path[sizeof DEBUGINFO_PATH - 1 + sizeof "/.build-id/" - 1
		       + 3 + (id_len - 1) * 2], ".debug");

      fd = TEMP_FAILURE_RETRY (open (id_path, O_RDONLY));
    }

  /* Fall back on (possible relative) alt file path.  */
  if (fd < 0)
    {
      char *altpath = __libdw_filepath (dbg->debugdir, NULL, altname);
      if (altpath != NULL)
	{
	  fd = TEMP_FAILURE_RETRY (open (altpath, O_RDONLY));
	  free (altpath);
	}
    }

  if (fd >= 0)
    {
      Dwarf *alt = dwarf_begin (fd, O_RDONLY);
      if (alt != NULL)
	{
	  dbg->alt_dwarf = alt;
	  dbg->alt_fd = fd;
	}
      else
	close (fd);
    }
}

Dwarf *
dwarf_getalt (Dwarf *main)
{
  /* Only try once.  */
  if (main == NULL || main->alt_dwarf == (void *) -1)
    return NULL;

  if (main->alt_dwarf != NULL)
    return main->alt_dwarf;

  find_debug_altlink (main);

  /* If we found nothing, make sure we don't try again.  */
  if (main->alt_dwarf == NULL)
    {
      main->alt_dwarf = (void *) -1;
      return NULL;
    }

  return main->alt_dwarf;
}
INTDEF (dwarf_getalt)