implement support for "enum class"
[platform/upstream/binutils.git] / gdb / build-id.c
1 /* build-id-related functions.
2
3    Copyright (C) 1991-2014 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 "elf-bfd.h"
23 #include "gdb_bfd.h"
24 #include "build-id.h"
25 #include <string.h>
26 #include "gdb_vecs.h"
27 #include "symfile.h"
28 #include "objfiles.h"
29 #include "filenames.h"
30
31 /* Locate NT_GNU_BUILD_ID from ABFD and return its content.  */
32
33 static const struct elf_build_id *
34 build_id_bfd_get (bfd *abfd)
35 {
36   if (!bfd_check_format (abfd, bfd_object)
37       || bfd_get_flavour (abfd) != bfd_target_elf_flavour
38       /* Although this is ELF_specific, it is safe to do in generic
39          code because it does not rely on any ELF-specific symbols at
40          link time, and if the ELF code is not available in BFD, then
41          ABFD will not have the ELF flavour.  */
42       || elf_tdata (abfd)->build_id == NULL)
43     return NULL;
44
45   return elf_tdata (abfd)->build_id;
46 }
47
48 /* See build-id.h.  */
49
50 int
51 build_id_verify (bfd *abfd, size_t check_len, const bfd_byte *check)
52 {
53   const struct elf_build_id *found;
54   int retval = 0;
55
56   found = build_id_bfd_get (abfd);
57
58   if (found == NULL)
59     warning (_("File \"%s\" has no build-id, file skipped"),
60              bfd_get_filename (abfd));
61   else if (found->size != check_len
62            || memcmp (found->data, check, found->size) != 0)
63     warning (_("File \"%s\" has a different build-id, file skipped"),
64              bfd_get_filename (abfd));
65   else
66     retval = 1;
67
68   return retval;
69 }
70
71 /* See build-id.h.  */
72
73 bfd *
74 build_id_to_debug_bfd (size_t build_id_len, const bfd_byte *build_id)
75 {
76   char *link, *debugdir;
77   VEC (char_ptr) *debugdir_vec;
78   struct cleanup *back_to;
79   int ix;
80   bfd *abfd = NULL;
81
82   /* DEBUG_FILE_DIRECTORY/.build-id/ab/cdef */
83   link = alloca (strlen (debug_file_directory) + (sizeof "/.build-id/" - 1) + 1
84                  + 2 * build_id_len + (sizeof ".debug" - 1) + 1);
85
86   /* Keep backward compatibility so that DEBUG_FILE_DIRECTORY being "" will
87      cause "/.build-id/..." lookups.  */
88
89   debugdir_vec = dirnames_to_char_ptr_vec (debug_file_directory);
90   back_to = make_cleanup_free_char_ptr_vec (debugdir_vec);
91
92   for (ix = 0; VEC_iterate (char_ptr, debugdir_vec, ix, debugdir); ++ix)
93     {
94       size_t debugdir_len = strlen (debugdir);
95       const gdb_byte *data = build_id;
96       size_t size = build_id_len;
97       char *s;
98       char *filename = NULL;
99
100       memcpy (link, debugdir, debugdir_len);
101       s = &link[debugdir_len];
102       s += sprintf (s, "/.build-id/");
103       if (size > 0)
104         {
105           size--;
106           s += sprintf (s, "%02x", (unsigned) *data++);
107         }
108       if (size > 0)
109         *s++ = '/';
110       while (size-- > 0)
111         s += sprintf (s, "%02x", (unsigned) *data++);
112       strcpy (s, ".debug");
113
114       /* lrealpath() is expensive even for the usually non-existent files.  */
115       if (access (link, F_OK) == 0)
116         filename = lrealpath (link);
117
118       if (filename == NULL)
119         continue;
120
121       /* We expect to be silent on the non-existing files.  */
122       abfd = gdb_bfd_open_maybe_remote (filename);
123       if (abfd == NULL)
124         continue;
125
126       if (build_id_verify (abfd, build_id_len, build_id))
127         break;
128
129       gdb_bfd_unref (abfd);
130       abfd = NULL;
131     }
132
133   do_cleanups (back_to);
134   return abfd;
135 }
136
137 /* See build-id.h.  */
138
139 char *
140 find_separate_debug_file_by_buildid (struct objfile *objfile)
141 {
142   const struct elf_build_id *build_id;
143
144   build_id = build_id_bfd_get (objfile->obfd);
145   if (build_id != NULL)
146     {
147       bfd *abfd;
148
149       abfd = build_id_to_debug_bfd (build_id->size, build_id->data);
150       /* Prevent looping on a stripped .debug file.  */
151       if (abfd != NULL
152           && filename_cmp (bfd_get_filename (abfd),
153                            objfile_name (objfile)) == 0)
154         {
155           warning (_("\"%s\": separate debug info file has no debug info"),
156                    bfd_get_filename (abfd));
157           gdb_bfd_unref (abfd);
158         }
159       else if (abfd != NULL)
160         {
161           char *result = xstrdup (bfd_get_filename (abfd));
162
163           gdb_bfd_unref (abfd);
164           return result;
165         }
166     }
167   return NULL;
168 }