Import gdb-7.10.1
[dragonfly.git] / contrib / gdb-7 / gdb / build-id.c
1 /* build-id-related functions.
2
3    Copyright (C) 1991-2015 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 #include "defs.h"
21 #include "bfd.h"
22 #include "gdb_bfd.h"
23 #include "build-id.h"
24 #include "gdb_vecs.h"
25 #include "symfile.h"
26 #include "objfiles.h"
27 #include "filenames.h"
28 #include "gdbcore.h"
29
30 /* See build-id.h.  */
31
32 const struct bfd_build_id *
33 build_id_bfd_get (bfd *abfd)
34 {
35   if (!bfd_check_format (abfd, bfd_object))
36     return NULL;
37
38   if (abfd->build_id != NULL)
39     return abfd->build_id;
40
41   /* No build-id */
42   return NULL;
43 }
44
45 /* See build-id.h.  */
46
47 int
48 build_id_verify (bfd *abfd, size_t check_len, const bfd_byte *check)
49 {
50   const struct bfd_build_id *found;
51   int retval = 0;
52
53   found = build_id_bfd_get (abfd);
54
55   if (found == NULL)
56     warning (_("File \"%s\" has no build-id, file skipped"),
57              bfd_get_filename (abfd));
58   else if (found->size != check_len
59            || memcmp (found->data, check, found->size) != 0)
60     warning (_("File \"%s\" has a different build-id, file skipped"),
61              bfd_get_filename (abfd));
62   else
63     retval = 1;
64
65   return retval;
66 }
67
68 /* See build-id.h.  */
69
70 bfd *
71 build_id_to_debug_bfd (size_t build_id_len, const bfd_byte *build_id)
72 {
73   char *link, *debugdir;
74   VEC (char_ptr) *debugdir_vec;
75   struct cleanup *back_to;
76   int ix;
77   bfd *abfd = NULL;
78
79   /* DEBUG_FILE_DIRECTORY/.build-id/ab/cdef */
80   link = alloca (strlen (debug_file_directory) + (sizeof "/.build-id/" - 1) + 1
81                  + 2 * build_id_len + (sizeof ".debug" - 1) + 1);
82
83   /* Keep backward compatibility so that DEBUG_FILE_DIRECTORY being "" will
84      cause "/.build-id/..." lookups.  */
85
86   debugdir_vec = dirnames_to_char_ptr_vec (debug_file_directory);
87   back_to = make_cleanup_free_char_ptr_vec (debugdir_vec);
88
89   for (ix = 0; VEC_iterate (char_ptr, debugdir_vec, ix, debugdir); ++ix)
90     {
91       size_t debugdir_len = strlen (debugdir);
92       const gdb_byte *data = build_id;
93       size_t size = build_id_len;
94       char *s;
95       char *filename = NULL;
96       struct cleanup *inner;
97
98       memcpy (link, debugdir, debugdir_len);
99       s = &link[debugdir_len];
100       s += sprintf (s, "/.build-id/");
101       if (size > 0)
102         {
103           size--;
104           s += sprintf (s, "%02x", (unsigned) *data++);
105         }
106       if (size > 0)
107         *s++ = '/';
108       while (size-- > 0)
109         s += sprintf (s, "%02x", (unsigned) *data++);
110       strcpy (s, ".debug");
111
112       /* lrealpath() is expensive even for the usually non-existent files.  */
113       if (access (link, F_OK) == 0)
114         filename = lrealpath (link);
115
116       if (filename == NULL)
117         continue;
118
119       /* We expect to be silent on the non-existing files.  */
120       inner = make_cleanup (xfree, filename);
121       abfd = gdb_bfd_open (filename, gnutarget, -1);
122       do_cleanups (inner);
123
124       if (abfd == NULL)
125         continue;
126
127       if (build_id_verify (abfd, build_id_len, build_id))
128         break;
129
130       gdb_bfd_unref (abfd);
131       abfd = NULL;
132     }
133
134   do_cleanups (back_to);
135   return abfd;
136 }
137
138 /* See build-id.h.  */
139
140 char *
141 find_separate_debug_file_by_buildid (struct objfile *objfile)
142 {
143   const struct bfd_build_id *build_id;
144
145   build_id = build_id_bfd_get (objfile->obfd);
146   if (build_id != NULL)
147     {
148       bfd *abfd;
149
150       abfd = build_id_to_debug_bfd (build_id->size, build_id->data);
151       /* Prevent looping on a stripped .debug file.  */
152       if (abfd != NULL
153           && filename_cmp (bfd_get_filename (abfd),
154                            objfile_name (objfile)) == 0)
155         {
156           warning (_("\"%s\": separate debug info file has no debug info"),
157                    bfd_get_filename (abfd));
158           gdb_bfd_unref (abfd);
159         }
160       else if (abfd != NULL)
161         {
162           char *result = xstrdup (bfd_get_filename (abfd));
163
164           gdb_bfd_unref (abfd);
165           return result;
166         }
167     }
168   return NULL;
169 }