1 /* Definitions for targets which report shared library events.
3 Copyright (C) 2007-2015 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
27 #include "solib-target.h"
29 /* Private data for each loaded library. */
32 /* The library's name. The name is normally kept in the struct
33 so_list; it is only here during XML parsing. */
36 /* The target can either specify segment bases or section bases, not
39 /* The base addresses for each independently relocatable segment of
40 this shared library. */
41 VEC(CORE_ADDR) *segment_bases;
43 /* The base addresses for each independently allocatable,
44 relocatable section of this shared library. */
45 VEC(CORE_ADDR) *section_bases;
47 /* The cached offsets for each section of this shared library,
48 determined from SEGMENT_BASES, or SECTION_BASES. */
49 struct section_offsets *offsets;
52 typedef struct lm_info *lm_info_p;
55 #if !defined(HAVE_LIBEXPAT)
57 static VEC(lm_info_p) *
58 solib_target_parse_libraries (const char *library)
60 static int have_warned;
65 warning (_("Can not parse XML library list; XML support was disabled "
72 #else /* HAVE_LIBEXPAT */
74 #include "xml-support.h"
76 /* Handle the start of a <segment> element. */
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)
83 VEC(lm_info_p) **list = (VEC(lm_info_p) **) user_data;
84 struct lm_info *last = VEC_last (lm_info_p, *list);
86 = (long unsigned int *) xml_find_attribute (attributes, "address")->value;
87 CORE_ADDR address = (CORE_ADDR) *address_p;
89 if (last->section_bases != NULL)
90 gdb_xml_error (parser,
91 _("Library list with both segments and sections"));
93 VEC_safe_push (CORE_ADDR, last->segment_bases, address);
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)
101 VEC(lm_info_p) **list = (VEC(lm_info_p) **) user_data;
102 struct lm_info *last = VEC_last (lm_info_p, *list);
104 = (long unsigned int *) xml_find_attribute (attributes, "address")->value;
105 CORE_ADDR address = (CORE_ADDR) *address_p;
107 if (last->segment_bases != NULL)
108 gdb_xml_error (parser,
109 _("Library list with both segments and sections"));
111 VEC_safe_push (CORE_ADDR, last->section_bases, address);
114 /* Handle the start of a <library> element. */
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)
121 VEC(lm_info_p) **list = (VEC(lm_info_p) **) user_data;
122 struct lm_info *item = XCNEW (struct lm_info);
124 = (const char *) xml_find_attribute (attributes, "name")->value;
126 item->name = xstrdup (name);
127 VEC_safe_push (lm_info_p, *list, item);
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)
135 VEC(lm_info_p) **list = (VEC(lm_info_p) **) user_data;
136 struct lm_info *lm_info = VEC_last (lm_info_p, *list);
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"));
145 /* Handle the start of a <library-list> element. */
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)
152 struct gdb_xml_value *version = xml_find_attribute (attributes, "version");
154 /* #FIXED attribute may be omitted, Expat returns NULL in such case. */
157 const char *string = (const char *) version->value;
159 if (strcmp (string, "1.0") != 0)
160 gdb_xml_error (parser,
161 _("Library list has unsupported version \"%s\""),
166 /* Discard the constructed library list. */
169 solib_target_free_library_list (void *p)
171 VEC(lm_info_p) **result = (VEC(lm_info_p) **) p;
172 struct lm_info *info;
175 for (ix = 0; VEC_iterate (lm_info_p, *result, ix, info); ix++)
178 VEC_free (CORE_ADDR, info->segment_bases);
179 VEC_free (CORE_ADDR, info->section_bases);
182 VEC_free (lm_info_p, *result);
186 /* The allowed elements and attributes for an XML library list.
187 The root element is a <library-list>. */
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 }
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 }
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 }
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 }
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 }
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 }
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 }
232 static VEC(lm_info_p) *
233 solib_target_parse_libraries (const char *library)
235 VEC(lm_info_p) *result = NULL;
236 struct cleanup *back_to = make_cleanup (solib_target_free_library_list,
239 if (gdb_xml_parse_quick (_("target library list"), "library-list.dtd",
240 library_list_elements, library, &result) == 0)
242 /* Parsed successfully, keep the result. */
243 discard_cleanups (back_to);
247 do_cleanups (back_to);
252 static struct so_list *
253 solib_target_current_sos (void)
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;
262 /* Fetch the list of shared libraries. */
263 library_document = target_read_stralloc (¤t_target,
264 TARGET_OBJECT_LIBRARIES,
266 if (library_document == NULL)
269 /* solib_target_parse_libraries may throw, so we use a cleanup. */
270 old_chain = make_cleanup (xfree, library_document);
272 /* Parse the list. */
273 library_list = solib_target_parse_libraries (library_document);
275 /* library_document string is not needed behind this point. */
276 do_cleanups (old_chain);
278 if (library_list == NULL)
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++)
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;
292 /* We no longer need this copy of the name. */
296 /* Add it to the list. */
298 last = start = new_solib;
301 last->next = new_solib;
306 /* Free the library list, but not its members. */
307 VEC_free (lm_info_p, library_list);
313 solib_target_special_symbol_handling (void)
315 /* Nothing needed. */
319 solib_target_solib_create_inferior_hook (int from_tty)
321 /* Nothing needed. */
325 solib_target_clear_solib (void)
327 /* Nothing needed. */
331 solib_target_free_so (struct so_list *so)
333 gdb_assert (so->lm_info->name == NULL);
334 xfree (so->lm_info->offsets);
335 VEC_free (CORE_ADDR, so->lm_info->segment_bases);
340 solib_target_relocate_section_addresses (struct so_list *so,
341 struct target_section *sec)
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)
349 int num_sections = gdb_bfd_count_sections (so->abfd);
352 = ((struct section_offsets *)
353 xzalloc (SIZEOF_N_SECTION_OFFSETS (num_sections)));
355 if (so->lm_info->section_bases)
359 int num_section_bases
360 = VEC_length (CORE_ADDR, so->lm_info->section_bases);
361 int num_alloc_sections = 0;
363 for (i = 0, sect = so->abfd->sections;
365 i++, sect = sect->next)
366 if ((bfd_get_section_flags (so->abfd, sect) & SEC_ALLOC))
367 num_alloc_sections++;
369 if (num_alloc_sections != num_section_bases)
371 Could not relocate shared library \"%s\": wrong number of ALLOC sections"),
377 CORE_ADDR *section_bases;
379 section_bases = VEC_address (CORE_ADDR,
380 so->lm_info->section_bases);
382 so->addr_low = ~(CORE_ADDR) 0;
384 for (i = 0, sect = so->abfd->sections;
386 i++, sect = sect->next)
388 if (!(bfd_get_section_flags (so->abfd, sect) & SEC_ALLOC))
390 if (bfd_section_size (so->abfd, sect) > 0)
394 low = section_bases[i];
395 high = low + bfd_section_size (so->abfd, sect) - 1;
397 if (low < so->addr_low)
399 if (high > so->addr_high)
400 so->addr_high = high;
401 gdb_assert (so->addr_low <= so->addr_high);
404 so->lm_info->offsets->offsets[i]
405 = section_bases[bases_index];
409 so->addr_low = so->addr_high = 0;
410 gdb_assert (so->addr_low <= so->addr_high);
413 else if (so->lm_info->segment_bases)
415 struct symfile_segment_data *data;
417 data = get_symfile_segment_data (so->abfd);
420 Could not relocate shared library \"%s\": no segments"), so->so_name);
426 CORE_ADDR *segment_bases;
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);
432 if (!symfile_map_offsets_to_segments (so->abfd, data,
433 so->lm_info->offsets,
434 num_bases, segment_bases))
436 Could not relocate shared library \"%s\": bad offsets"), so->so_name);
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];
444 for (i = 1; i < data->num_segments; i++)
446 /* If we have run out of offsets, assume all
447 remaining segments have the same offset. */
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)
457 so->addr_low = segment_bases[0];
458 so->addr_high = (data->segment_bases[i - 1]
459 + data->segment_sizes[i - 1]
461 gdb_assert (so->addr_low <= so->addr_high);
463 free_symfile_segment_data (data);
468 offset = so->lm_info->offsets->offsets[gdb_bfd_section_index
469 (sec->the_bfd_section->owner,
470 sec->the_bfd_section)];
472 sec->endaddr += offset;
476 solib_target_open_symbol_file_object (void *from_ttyp)
478 /* We can't locate the main symbol file based on the target's
479 knowledge; the user has to specify it. */
484 solib_target_in_dynsym_resolve_code (CORE_ADDR pc)
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);
492 struct target_so_ops solib_target_so_ops;
494 /* -Wmissing-prototypes */
495 extern initialize_file_ftype _initialize_solib_target;
498 _initialize_solib_target (void)
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;
515 /* Set current_target_so_ops to solib_target_so_ops if not already
517 if (current_target_so_ops == 0)
518 current_target_so_ops = &solib_target_so_ops;