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