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 = user_data;
84 struct lm_info *last = VEC_last (lm_info_p, *list);
85 ULONGEST *address_p = xml_find_attribute (attributes, "address")->value;
86 CORE_ADDR address = (CORE_ADDR) *address_p;
88 if (last->section_bases != NULL)
89 gdb_xml_error (parser,
90 _("Library list with both segments and sections"));
92 VEC_safe_push (CORE_ADDR, last->segment_bases, address);
96 library_list_start_section (struct gdb_xml_parser *parser,
97 const struct gdb_xml_element *element,
98 void *user_data, VEC(gdb_xml_value_s) *attributes)
100 VEC(lm_info_p) **list = user_data;
101 struct lm_info *last = VEC_last (lm_info_p, *list);
102 ULONGEST *address_p = xml_find_attribute (attributes, "address")->value;
103 CORE_ADDR address = (CORE_ADDR) *address_p;
105 if (last->segment_bases != NULL)
106 gdb_xml_error (parser,
107 _("Library list with both segments and sections"));
109 VEC_safe_push (CORE_ADDR, last->section_bases, address);
112 /* Handle the start of a <library> element. */
115 library_list_start_library (struct gdb_xml_parser *parser,
116 const struct gdb_xml_element *element,
117 void *user_data, VEC(gdb_xml_value_s) *attributes)
119 VEC(lm_info_p) **list = user_data;
120 struct lm_info *item = XCNEW (struct lm_info);
121 const char *name = xml_find_attribute (attributes, "name")->value;
123 item->name = xstrdup (name);
124 VEC_safe_push (lm_info_p, *list, item);
128 library_list_end_library (struct gdb_xml_parser *parser,
129 const struct gdb_xml_element *element,
130 void *user_data, const char *body_text)
132 VEC(lm_info_p) **list = user_data;
133 struct lm_info *lm_info = VEC_last (lm_info_p, *list);
135 if (lm_info->segment_bases == NULL
136 && lm_info->section_bases == NULL)
137 gdb_xml_error (parser,
138 _("No segment or section bases defined"));
142 /* Handle the start of a <library-list> element. */
145 library_list_start_list (struct gdb_xml_parser *parser,
146 const struct gdb_xml_element *element,
147 void *user_data, VEC(gdb_xml_value_s) *attributes)
149 struct gdb_xml_value *version = xml_find_attribute (attributes, "version");
151 /* #FIXED attribute may be omitted, Expat returns NULL in such case. */
154 const char *string = version->value;
156 if (strcmp (string, "1.0") != 0)
157 gdb_xml_error (parser,
158 _("Library list has unsupported version \"%s\""),
163 /* Discard the constructed library list. */
166 solib_target_free_library_list (void *p)
168 VEC(lm_info_p) **result = p;
169 struct lm_info *info;
172 for (ix = 0; VEC_iterate (lm_info_p, *result, ix, info); ix++)
175 VEC_free (CORE_ADDR, info->segment_bases);
176 VEC_free (CORE_ADDR, info->section_bases);
179 VEC_free (lm_info_p, *result);
183 /* The allowed elements and attributes for an XML library list.
184 The root element is a <library-list>. */
186 static const struct gdb_xml_attribute segment_attributes[] = {
187 { "address", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
188 { NULL, GDB_XML_AF_NONE, NULL, NULL }
191 static const struct gdb_xml_attribute section_attributes[] = {
192 { "address", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
193 { NULL, GDB_XML_AF_NONE, NULL, NULL }
196 static const struct gdb_xml_element library_children[] = {
197 { "segment", segment_attributes, NULL,
198 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
199 library_list_start_segment, NULL },
200 { "section", section_attributes, NULL,
201 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
202 library_list_start_section, NULL },
203 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
206 static const struct gdb_xml_attribute library_attributes[] = {
207 { "name", GDB_XML_AF_NONE, NULL, NULL },
208 { NULL, GDB_XML_AF_NONE, NULL, NULL }
211 static const struct gdb_xml_element library_list_children[] = {
212 { "library", library_attributes, library_children,
213 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
214 library_list_start_library, library_list_end_library },
215 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
218 static const struct gdb_xml_attribute library_list_attributes[] = {
219 { "version", GDB_XML_AF_OPTIONAL, NULL, NULL },
220 { NULL, GDB_XML_AF_NONE, NULL, NULL }
223 static const struct gdb_xml_element library_list_elements[] = {
224 { "library-list", library_list_attributes, library_list_children,
225 GDB_XML_EF_NONE, library_list_start_list, NULL },
226 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
229 static VEC(lm_info_p) *
230 solib_target_parse_libraries (const char *library)
232 VEC(lm_info_p) *result = NULL;
233 struct cleanup *back_to = make_cleanup (solib_target_free_library_list,
236 if (gdb_xml_parse_quick (_("target library list"), "library-list.dtd",
237 library_list_elements, library, &result) == 0)
239 /* Parsed successfully, keep the result. */
240 discard_cleanups (back_to);
244 do_cleanups (back_to);
249 static struct so_list *
250 solib_target_current_sos (void)
252 struct so_list *new_solib, *start = NULL, *last = NULL;
253 char *library_document;
254 struct cleanup *old_chain;
255 VEC(lm_info_p) *library_list;
256 struct lm_info *info;
259 /* Fetch the list of shared libraries. */
260 library_document = target_read_stralloc (¤t_target,
261 TARGET_OBJECT_LIBRARIES,
263 if (library_document == NULL)
266 /* solib_target_parse_libraries may throw, so we use a cleanup. */
267 old_chain = make_cleanup (xfree, library_document);
269 /* Parse the list. */
270 library_list = solib_target_parse_libraries (library_document);
272 /* library_document string is not needed behind this point. */
273 do_cleanups (old_chain);
275 if (library_list == NULL)
278 /* Build a struct so_list for each entry on the list. */
279 for (ix = 0; VEC_iterate (lm_info_p, library_list, ix, info); ix++)
281 new_solib = XCNEW (struct so_list);
282 strncpy (new_solib->so_name, info->name, SO_NAME_MAX_PATH_SIZE - 1);
283 new_solib->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
284 strncpy (new_solib->so_original_name, info->name,
285 SO_NAME_MAX_PATH_SIZE - 1);
286 new_solib->so_original_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
287 new_solib->lm_info = info;
289 /* We no longer need this copy of the name. */
293 /* Add it to the list. */
295 last = start = new_solib;
298 last->next = new_solib;
303 /* Free the library list, but not its members. */
304 VEC_free (lm_info_p, library_list);
310 solib_target_special_symbol_handling (void)
312 /* Nothing needed. */
316 solib_target_solib_create_inferior_hook (int from_tty)
318 /* Nothing needed. */
322 solib_target_clear_solib (void)
324 /* Nothing needed. */
328 solib_target_free_so (struct so_list *so)
330 gdb_assert (so->lm_info->name == NULL);
331 xfree (so->lm_info->offsets);
332 VEC_free (CORE_ADDR, so->lm_info->segment_bases);
337 solib_target_relocate_section_addresses (struct so_list *so,
338 struct target_section *sec)
342 /* Build the offset table only once per object file. We can not do
343 it any earlier, since we need to open the file first. */
344 if (so->lm_info->offsets == NULL)
346 int num_sections = gdb_bfd_count_sections (so->abfd);
349 = ((struct section_offsets *)
350 xzalloc (SIZEOF_N_SECTION_OFFSETS (num_sections)));
352 if (so->lm_info->section_bases)
356 int num_section_bases
357 = VEC_length (CORE_ADDR, so->lm_info->section_bases);
358 int num_alloc_sections = 0;
360 for (i = 0, sect = so->abfd->sections;
362 i++, sect = sect->next)
363 if ((bfd_get_section_flags (so->abfd, sect) & SEC_ALLOC))
364 num_alloc_sections++;
366 if (num_alloc_sections != num_section_bases)
368 Could not relocate shared library \"%s\": wrong number of ALLOC sections"),
374 CORE_ADDR *section_bases;
376 section_bases = VEC_address (CORE_ADDR,
377 so->lm_info->section_bases);
379 so->addr_low = ~(CORE_ADDR) 0;
381 for (i = 0, sect = so->abfd->sections;
383 i++, sect = sect->next)
385 if (!(bfd_get_section_flags (so->abfd, sect) & SEC_ALLOC))
387 if (bfd_section_size (so->abfd, sect) > 0)
391 low = section_bases[i];
392 high = low + bfd_section_size (so->abfd, sect) - 1;
394 if (low < so->addr_low)
396 if (high > so->addr_high)
397 so->addr_high = high;
398 gdb_assert (so->addr_low <= so->addr_high);
401 so->lm_info->offsets->offsets[i]
402 = section_bases[bases_index];
406 so->addr_low = so->addr_high = 0;
407 gdb_assert (so->addr_low <= so->addr_high);
410 else if (so->lm_info->segment_bases)
412 struct symfile_segment_data *data;
414 data = get_symfile_segment_data (so->abfd);
417 Could not relocate shared library \"%s\": no segments"), so->so_name);
423 CORE_ADDR *segment_bases;
425 num_bases = VEC_length (CORE_ADDR, so->lm_info->segment_bases);
426 segment_bases = VEC_address (CORE_ADDR,
427 so->lm_info->segment_bases);
429 if (!symfile_map_offsets_to_segments (so->abfd, data,
430 so->lm_info->offsets,
431 num_bases, segment_bases))
433 Could not relocate shared library \"%s\": bad offsets"), so->so_name);
435 /* Find the range of addresses to report for this library in
436 "info sharedlibrary". Report any consecutive segments
437 which were relocated as a single unit. */
438 gdb_assert (num_bases > 0);
439 orig_delta = segment_bases[0] - data->segment_bases[0];
441 for (i = 1; i < data->num_segments; i++)
443 /* If we have run out of offsets, assume all
444 remaining segments have the same offset. */
448 /* If this segment does not have the same offset, do
449 not include it in the library's range. */
450 if (segment_bases[i] - data->segment_bases[i] != orig_delta)
454 so->addr_low = segment_bases[0];
455 so->addr_high = (data->segment_bases[i - 1]
456 + data->segment_sizes[i - 1]
458 gdb_assert (so->addr_low <= so->addr_high);
460 free_symfile_segment_data (data);
465 offset = so->lm_info->offsets->offsets[gdb_bfd_section_index
466 (sec->the_bfd_section->owner,
467 sec->the_bfd_section)];
469 sec->endaddr += offset;
473 solib_target_open_symbol_file_object (void *from_ttyp)
475 /* We can't locate the main symbol file based on the target's
476 knowledge; the user has to specify it. */
481 solib_target_in_dynsym_resolve_code (CORE_ADDR pc)
483 /* We don't have a range of addresses for the dynamic linker; there
484 may not be one in the program's address space. So only report
485 PLT entries (which may be import stubs). */
486 return in_plt_section (pc);
489 struct target_so_ops solib_target_so_ops;
491 /* -Wmissing-prototypes */
492 extern initialize_file_ftype _initialize_solib_target;
495 _initialize_solib_target (void)
497 solib_target_so_ops.relocate_section_addresses
498 = solib_target_relocate_section_addresses;
499 solib_target_so_ops.free_so = solib_target_free_so;
500 solib_target_so_ops.clear_solib = solib_target_clear_solib;
501 solib_target_so_ops.solib_create_inferior_hook
502 = solib_target_solib_create_inferior_hook;
503 solib_target_so_ops.special_symbol_handling
504 = solib_target_special_symbol_handling;
505 solib_target_so_ops.current_sos = solib_target_current_sos;
506 solib_target_so_ops.open_symbol_file_object
507 = solib_target_open_symbol_file_object;
508 solib_target_so_ops.in_dynsym_resolve_code
509 = solib_target_in_dynsym_resolve_code;
510 solib_target_so_ops.bfd_open = solib_bfd_open;
512 /* Set current_target_so_ops to solib_target_so_ops if not already
514 if (current_target_so_ops == 0)
515 current_target_so_ops = &solib_target_so_ops;