Validate symbol file using build-id
[external/binutils.git] / gdb / solib-target.c
1 /* Definitions for targets which report shared library events.
2
3    Copyright (C) 2007-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 "objfiles.h"
22 #include "solist.h"
23 #include "symtab.h"
24 #include "symfile.h"
25 #include "target.h"
26 #include "vec.h"
27 #include "solib-target.h"
28 #include "solib.h"
29
30 /* Private data for each loaded library.  */
31 struct lm_info
32 {
33   /* The library's name.  The name is normally kept in the struct
34      so_list; it is only here during XML parsing.  */
35   char *name;
36
37   /* The target can either specify segment bases or section bases, not
38      both.  */
39
40   /* The base addresses for each independently relocatable segment of
41      this shared library.  */
42   VEC(CORE_ADDR) *segment_bases;
43
44   /* The base addresses for each independently allocatable,
45      relocatable section of this shared library.  */
46   VEC(CORE_ADDR) *section_bases;
47
48   /* The cached offsets for each section of this shared library,
49      determined from SEGMENT_BASES, or SECTION_BASES.  */
50   struct section_offsets *offsets;
51 };
52
53 typedef struct lm_info *lm_info_p;
54 DEF_VEC_P(lm_info_p);
55
56 #if !defined(HAVE_LIBEXPAT)
57
58 static VEC(lm_info_p) *
59 solib_target_parse_libraries (const char *library)
60 {
61   static int have_warned;
62
63   if (!have_warned)
64     {
65       have_warned = 1;
66       warning (_("Can not parse XML library list; XML support was disabled "
67                  "at compile time"));
68     }
69
70   return NULL;
71 }
72
73 #else /* HAVE_LIBEXPAT */
74
75 #include "xml-support.h"
76
77 /* Handle the start of a <segment> element.  */
78
79 static void
80 library_list_start_segment (struct gdb_xml_parser *parser,
81                             const struct gdb_xml_element *element,
82                             void *user_data, VEC(gdb_xml_value_s) *attributes)
83 {
84   VEC(lm_info_p) **list = user_data;
85   struct lm_info *last = VEC_last (lm_info_p, *list);
86   ULONGEST *address_p = xml_find_attribute (attributes, "address")->value;
87   CORE_ADDR address = (CORE_ADDR) *address_p;
88
89   if (last->section_bases != NULL)
90     gdb_xml_error (parser,
91                    _("Library list with both segments and sections"));
92
93   VEC_safe_push (CORE_ADDR, last->segment_bases, address);
94 }
95
96 static void
97 library_list_start_section (struct gdb_xml_parser *parser,
98                             const struct gdb_xml_element *element,
99                             void *user_data, VEC(gdb_xml_value_s) *attributes)
100 {
101   VEC(lm_info_p) **list = user_data;
102   struct lm_info *last = VEC_last (lm_info_p, *list);
103   ULONGEST *address_p = xml_find_attribute (attributes, "address")->value;
104   CORE_ADDR address = (CORE_ADDR) *address_p;
105
106   if (last->segment_bases != NULL)
107     gdb_xml_error (parser,
108                    _("Library list with both segments and sections"));
109
110   VEC_safe_push (CORE_ADDR, last->section_bases, address);
111 }
112
113 /* Handle the start of a <library> element.  */
114
115 static void
116 library_list_start_library (struct gdb_xml_parser *parser,
117                             const struct gdb_xml_element *element,
118                             void *user_data, VEC(gdb_xml_value_s) *attributes)
119 {
120   VEC(lm_info_p) **list = user_data;
121   struct lm_info *item = XCNEW (struct lm_info);
122   const char *name = xml_find_attribute (attributes, "name")->value;
123
124   item->name = xstrdup (name);
125   VEC_safe_push (lm_info_p, *list, item);
126 }
127
128 static void
129 library_list_end_library (struct gdb_xml_parser *parser,
130                           const struct gdb_xml_element *element,
131                           void *user_data, const char *body_text)
132 {
133   VEC(lm_info_p) **list = user_data;
134   struct lm_info *lm_info = VEC_last (lm_info_p, *list);
135
136   if (lm_info->segment_bases == NULL
137       && lm_info->section_bases == NULL)
138     gdb_xml_error (parser,
139                    _("No segment or section bases defined"));
140 }
141
142
143 /* Handle the start of a <library-list> element.  */
144
145 static void
146 library_list_start_list (struct gdb_xml_parser *parser,
147                          const struct gdb_xml_element *element,
148                          void *user_data, VEC(gdb_xml_value_s) *attributes)
149 {
150   struct gdb_xml_value *version = xml_find_attribute (attributes, "version");
151
152   /* #FIXED attribute may be omitted, Expat returns NULL in such case.  */
153   if (version != NULL)
154     {
155       const char *string = version->value;
156
157       if (strcmp (string, "1.0") != 0)
158         gdb_xml_error (parser,
159                        _("Library list has unsupported version \"%s\""),
160                        version);
161     }
162 }
163
164 /* Discard the constructed library list.  */
165
166 static void
167 solib_target_free_library_list (void *p)
168 {
169   VEC(lm_info_p) **result = p;
170   struct lm_info *info;
171   int ix;
172
173   for (ix = 0; VEC_iterate (lm_info_p, *result, ix, info); ix++)
174     {
175       xfree (info->name);
176       VEC_free (CORE_ADDR, info->segment_bases);
177       VEC_free (CORE_ADDR, info->section_bases);
178       xfree (info);
179     }
180   VEC_free (lm_info_p, *result);
181   *result = NULL;
182 }
183
184 /* The allowed elements and attributes for an XML library list.
185    The root element is a <library-list>.  */
186
187 static const struct gdb_xml_attribute segment_attributes[] = {
188   { "address", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
189   { NULL, GDB_XML_AF_NONE, NULL, NULL }
190 };
191
192 static const struct gdb_xml_attribute section_attributes[] = {
193   { "address", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
194   { NULL, GDB_XML_AF_NONE, NULL, NULL }
195 };
196
197 static const struct gdb_xml_element library_children[] = {
198   { "segment", segment_attributes, NULL,
199     GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
200     library_list_start_segment, NULL },
201   { "section", section_attributes, NULL,
202     GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
203     library_list_start_section, NULL },
204   { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
205 };
206
207 static const struct gdb_xml_attribute library_attributes[] = {
208   { "name", GDB_XML_AF_NONE, NULL, NULL },
209   { NULL, GDB_XML_AF_NONE, NULL, NULL }
210 };
211
212 static const struct gdb_xml_element library_list_children[] = {
213   { "library", library_attributes, library_children,
214     GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
215     library_list_start_library, library_list_end_library },
216   { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
217 };
218
219 static const struct gdb_xml_attribute library_list_attributes[] = {
220   { "version", GDB_XML_AF_OPTIONAL, NULL, NULL },
221   { NULL, GDB_XML_AF_NONE, NULL, NULL }
222 };
223
224 static const struct gdb_xml_element library_list_elements[] = {
225   { "library-list", library_list_attributes, library_list_children,
226     GDB_XML_EF_NONE, library_list_start_list, NULL },
227   { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
228 };
229
230 static VEC(lm_info_p) *
231 solib_target_parse_libraries (const char *library)
232 {
233   VEC(lm_info_p) *result = NULL;
234   struct cleanup *back_to = make_cleanup (solib_target_free_library_list,
235                                           &result);
236
237   if (gdb_xml_parse_quick (_("target library list"), "library-list.dtd",
238                            library_list_elements, library, &result) == 0)
239     {
240       /* Parsed successfully, keep the result.  */
241       discard_cleanups (back_to);
242       return result;
243     }
244
245   do_cleanups (back_to);
246   return NULL;
247 }
248 #endif
249
250 static struct so_list *
251 solib_target_current_sos (void)
252 {
253   struct so_list *new_solib, *start = NULL, *last = NULL;
254   char *library_document;
255   struct cleanup *old_chain;
256   VEC(lm_info_p) *library_list;
257   struct lm_info *info;
258   int ix;
259
260   /* Fetch the list of shared libraries.  */
261   library_document = target_read_stralloc (&current_target,
262                                            TARGET_OBJECT_LIBRARIES,
263                                            NULL);
264   if (library_document == NULL)
265     return NULL;
266
267   /* solib_target_parse_libraries may throw, so we use a cleanup.  */
268   old_chain = make_cleanup (xfree, library_document);
269
270   /* Parse the list.  */
271   library_list = solib_target_parse_libraries (library_document);
272
273   /* library_document string is not needed behind this point.  */
274   do_cleanups (old_chain);
275
276   if (library_list == NULL)
277     return NULL;
278
279   /* Build a struct so_list for each entry on the list.  */
280   for (ix = 0; VEC_iterate (lm_info_p, library_list, ix, info); ix++)
281     {
282       new_solib = XCNEW (struct so_list);
283       strncpy (new_solib->so_name, info->name, SO_NAME_MAX_PATH_SIZE - 1);
284       new_solib->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
285       strncpy (new_solib->so_original_name, info->name,
286                SO_NAME_MAX_PATH_SIZE - 1);
287       new_solib->so_original_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
288       new_solib->lm_info = info;
289
290       /* We no longer need this copy of the name.  */
291       xfree (info->name);
292       info->name = NULL;
293
294       /* Add it to the list.  */
295       if (!start)
296         last = start = new_solib;
297       else
298         {
299           last->next = new_solib;
300           last = new_solib;
301         }
302     }
303
304   /* Free the library list, but not its members.  */
305   VEC_free (lm_info_p, library_list);
306
307   return start;
308 }
309
310 static void
311 solib_target_special_symbol_handling (void)
312 {
313   /* Nothing needed.  */
314 }
315
316 static void
317 solib_target_solib_create_inferior_hook (int from_tty)
318 {
319   /* Nothing needed.  */
320 }
321
322 static void
323 solib_target_clear_solib (void)
324 {
325   /* Nothing needed.  */
326 }
327
328 static void
329 solib_target_free_so (struct so_list *so)
330 {
331   gdb_assert (so->lm_info->name == NULL);
332   xfree (so->lm_info->offsets);
333   VEC_free (CORE_ADDR, so->lm_info->segment_bases);
334   xfree (so->lm_info);
335 }
336
337 static void
338 solib_target_relocate_section_addresses (struct so_list *so,
339                                          struct target_section *sec)
340 {
341   CORE_ADDR offset;
342
343   /* Build the offset table only once per object file.  We can not do
344      it any earlier, since we need to open the file first.  */
345   if (so->lm_info->offsets == NULL)
346     {
347       int num_sections = gdb_bfd_count_sections (so->abfd);
348
349       so->lm_info->offsets = xzalloc (SIZEOF_N_SECTION_OFFSETS (num_sections));
350
351       if (so->lm_info->section_bases)
352         {
353           int i;
354           asection *sect;
355           int num_section_bases
356             = VEC_length (CORE_ADDR, so->lm_info->section_bases);
357           int num_alloc_sections = 0;
358
359           for (i = 0, sect = so->abfd->sections;
360                sect != NULL;
361                i++, sect = sect->next)
362             if ((bfd_get_section_flags (so->abfd, sect) & SEC_ALLOC))
363               num_alloc_sections++;
364
365           if (num_alloc_sections != num_section_bases)
366             warning (_("\
367 Could not relocate shared library \"%s\": wrong number of ALLOC sections"),
368                      so->so_name);
369           else
370             {
371               int bases_index = 0;
372               int found_range = 0;
373               CORE_ADDR *section_bases;
374
375               section_bases = VEC_address (CORE_ADDR,
376                                            so->lm_info->section_bases);
377
378               so->addr_low = ~(CORE_ADDR) 0;
379               so->addr_high = 0;
380               for (i = 0, sect = so->abfd->sections;
381                    sect != NULL;
382                    i++, sect = sect->next)
383                 {
384                   if (!(bfd_get_section_flags (so->abfd, sect) & SEC_ALLOC))
385                     continue;
386                   if (bfd_section_size (so->abfd, sect) > 0)
387                     {
388                       CORE_ADDR low, high;
389
390                       low = section_bases[i];
391                       high = low + bfd_section_size (so->abfd, sect) - 1;
392
393                       if (low < so->addr_low)
394                         so->addr_low = low;
395                       if (high > so->addr_high)
396                         so->addr_high = high;
397                       gdb_assert (so->addr_low <= so->addr_high);
398                       found_range = 1;
399                     }
400                   so->lm_info->offsets->offsets[i]
401                     = section_bases[bases_index];
402                   bases_index++;
403                 }
404               if (!found_range)
405                 so->addr_low = so->addr_high = 0;
406               gdb_assert (so->addr_low <= so->addr_high);
407             }
408         }
409       else if (so->lm_info->segment_bases)
410         {
411           struct symfile_segment_data *data;
412
413           data = get_symfile_segment_data (so->abfd);
414           if (data == NULL)
415             warning (_("\
416 Could not relocate shared library \"%s\": no segments"), so->so_name);
417           else
418             {
419               ULONGEST orig_delta;
420               int i;
421               int num_bases;
422               CORE_ADDR *segment_bases;
423
424               num_bases = VEC_length (CORE_ADDR, so->lm_info->segment_bases);
425               segment_bases = VEC_address (CORE_ADDR,
426                                            so->lm_info->segment_bases);
427
428               if (!symfile_map_offsets_to_segments (so->abfd, data,
429                                                     so->lm_info->offsets,
430                                                     num_bases, segment_bases))
431                 warning (_("\
432 Could not relocate shared library \"%s\": bad offsets"), so->so_name);
433
434               /* Find the range of addresses to report for this library in
435                  "info sharedlibrary".  Report any consecutive segments
436                  which were relocated as a single unit.  */
437               gdb_assert (num_bases > 0);
438               orig_delta = segment_bases[0] - data->segment_bases[0];
439
440               for (i = 1; i < data->num_segments; i++)
441                 {
442                   /* If we have run out of offsets, assume all
443                      remaining segments have the same offset.  */
444                   if (i >= num_bases)
445                     continue;
446
447                   /* If this segment does not have the same offset, do
448                      not include it in the library's range.  */
449                   if (segment_bases[i] - data->segment_bases[i] != orig_delta)
450                     break;
451                 }
452
453               so->addr_low = segment_bases[0];
454               so->addr_high = (data->segment_bases[i - 1]
455                                + data->segment_sizes[i - 1]
456                                + orig_delta);
457               gdb_assert (so->addr_low <= so->addr_high);
458
459               free_symfile_segment_data (data);
460             }
461         }
462     }
463
464   offset = so->lm_info->offsets->offsets[gdb_bfd_section_index
465                                          (sec->the_bfd_section->owner,
466                                           sec->the_bfd_section)];
467   sec->addr += offset;
468   sec->endaddr += offset;
469 }
470
471 static int
472 solib_target_open_symbol_file_object (void *from_ttyp)
473 {
474   /* We can't locate the main symbol file based on the target's
475      knowledge; the user has to specify it.  */
476   return 0;
477 }
478
479 static int
480 solib_target_in_dynsym_resolve_code (CORE_ADDR pc)
481 {
482   /* We don't have a range of addresses for the dynamic linker; there
483      may not be one in the program's address space.  So only report
484      PLT entries (which may be import stubs).  */
485   return in_plt_section (pc);
486 }
487
488 struct target_so_ops solib_target_so_ops;
489
490 /* -Wmissing-prototypes */
491 extern initialize_file_ftype _initialize_solib_target;
492
493 void
494 _initialize_solib_target (void)
495 {
496   solib_target_so_ops.relocate_section_addresses
497     = solib_target_relocate_section_addresses;
498   solib_target_so_ops.free_so = solib_target_free_so;
499   solib_target_so_ops.clear_solib = solib_target_clear_solib;
500   solib_target_so_ops.solib_create_inferior_hook
501     = solib_target_solib_create_inferior_hook;
502   solib_target_so_ops.special_symbol_handling
503     = solib_target_special_symbol_handling;
504   solib_target_so_ops.current_sos = solib_target_current_sos;
505   solib_target_so_ops.open_symbol_file_object
506     = solib_target_open_symbol_file_object;
507   solib_target_so_ops.in_dynsym_resolve_code
508     = solib_target_in_dynsym_resolve_code;
509   solib_target_so_ops.bfd_open = solib_bfd_open;
510   solib_target_so_ops.validate = default_solib_validate;
511
512   /* Set current_target_so_ops to solib_target_so_ops if not already
513      set.  */
514   if (current_target_so_ops == 0)
515     current_target_so_ops = &solib_target_so_ops;
516 }