1 /* Retrieves the DWARF descriptor for debugaltlink data.
2 Copyright (C) 2014, 2018 Red Hat, Inc.
3 This file is part of elfutils.
5 This file is free software; you can redistribute it and/or modify
6 it under the terms of either
8 * the GNU Lesser General Public License as published by the Free
9 Software Foundation; either version 3 of the License, or (at
10 your option) any later version
14 * the GNU General Public License as published by the Free
15 Software Foundation; either version 2 of the License, or (at
16 your option) any later version
18 or both in parallel, as here.
20 elfutils is distributed in the hope that it will be useful, but
21 WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 General Public License for more details.
25 You should have received copies of the GNU General Public License and
26 the GNU Lesser General Public License along with this program. If
27 not, see <http://www.gnu.org/licenses/>. */
35 #include "libdwelfP.h"
44 #include <sys/types.h>
50 __libdw_filepath (const char *debugdir, const char *dir, const char *file)
58 if (dir != NULL && dir[0] == '/')
60 size_t dirlen = strlen (dir);
61 size_t filelen = strlen (file);
62 size_t len = dirlen + 1 + filelen + 1;
63 char *path = malloc (len);
66 char *c = mempcpy (path, dir, dirlen);
67 if (dir[dirlen - 1] != '/')
69 mempcpy (c, file, filelen + 1);
76 size_t debugdirlen = strlen (debugdir);
77 size_t dirlen = dir != NULL ? strlen (dir) : 0;
78 size_t filelen = strlen (file);
79 size_t len = debugdirlen + 1 + dirlen + 1 + filelen + 1;
80 char *path = malloc (len);
83 char *c = mempcpy (path, debugdir, debugdirlen);
86 c = mempcpy (c, dir, dirlen);
87 if (dir[dirlen - 1] != '/')
90 mempcpy (c, file, filelen + 1);
99 find_debug_altlink (Dwarf *dbg)
102 const void *build_id;
103 ssize_t build_id_len = INTUSE(dwelf_dwarf_gnu_debugaltlink) (dbg,
107 /* Couldn't even get the debugaltlink. It probably doesn't exist. */
108 if (build_id_len <= 0)
111 const uint8_t *id = (const uint8_t *) build_id;
112 size_t id_len = build_id_len;
115 /* We only look in the standard path. And relative to the dbg file. */
116 #define DEBUGINFO_PATH "/usr/lib/debug"
118 /* We don't handle very short or really large build-ids. We need at
119 at least 3 and allow for up to 64 (normally ids are 20 long). */
120 #define MIN_BUILD_ID_BYTES 3
121 #define MAX_BUILD_ID_BYTES 64
122 if (id_len >= MIN_BUILD_ID_BYTES && id_len <= MAX_BUILD_ID_BYTES)
124 /* Note sizeof a string literal includes the trailing zero. */
125 char id_path[sizeof DEBUGINFO_PATH - 1 + sizeof "/.build-id/" - 1
126 + 2 + 1 + (MAX_BUILD_ID_BYTES - 1) * 2 + sizeof ".debug"];
127 sprintf (&id_path[0], "%s%s", DEBUGINFO_PATH, "/.build-id/");
128 sprintf (&id_path[sizeof DEBUGINFO_PATH - 1 + sizeof "/.build-id/" - 1],
129 "%02" PRIx8 "/", (uint8_t) id[0]);
130 for (size_t i = 1; i < id_len; ++i)
131 sprintf (&id_path[sizeof DEBUGINFO_PATH - 1 + sizeof "/.build-id/" - 1
132 + 3 + (i - 1) * 2], "%02" PRIx8, (uint8_t) id[i]);
133 strcpy (&id_path[sizeof DEBUGINFO_PATH - 1 + sizeof "/.build-id/" - 1
134 + 3 + (id_len - 1) * 2], ".debug");
136 fd = TEMP_FAILURE_RETRY (open (id_path, O_RDONLY));
139 /* Fall back on (possible relative) alt file path. */
142 char *altpath = __libdw_filepath (dbg->debugdir, NULL, altname);
145 fd = TEMP_FAILURE_RETRY (open (altpath, O_RDONLY));
152 Dwarf *alt = dwarf_begin (fd, O_RDONLY);
155 dbg->alt_dwarf = alt;
164 dwarf_getalt (Dwarf *main)
167 if (main == NULL || main->alt_dwarf == (void *) -1)
170 if (main->alt_dwarf != NULL)
171 return main->alt_dwarf;
173 find_debug_altlink (main);
175 /* If we found nothing, make sure we don't try again. */
176 if (main->alt_dwarf == NULL)
178 main->alt_dwarf = (void *) -1;
182 return main->alt_dwarf;
184 INTDEF (dwarf_getalt)