1 /* Definitions for targets which report shared library events.
3 Copyright (C) 2007-2018 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"
30 /* Private data for each loaded library. */
31 struct lm_info_target : public lm_info_base
33 /* The library's name. The name is normally kept in the struct
34 so_list; it is only here during XML parsing. */
37 /* The target can either specify segment bases or section bases, not
40 /* The base addresses for each independently relocatable segment of
41 this shared library. */
42 std::vector<CORE_ADDR> segment_bases;
44 /* The base addresses for each independently allocatable,
45 relocatable section of this shared library. */
46 std::vector<CORE_ADDR> section_bases;
48 /* The cached offsets for each section of this shared library,
49 determined from SEGMENT_BASES, or SECTION_BASES. */
50 section_offsets *offsets = NULL;
53 typedef lm_info_target *lm_info_target_p;
54 DEF_VEC_P(lm_info_target_p);
56 #if !defined(HAVE_LIBEXPAT)
58 static VEC(lm_info_target_p) *
59 solib_target_parse_libraries (const char *library)
61 static int have_warned;
66 warning (_("Can not parse XML library list; XML support was disabled "
73 #else /* HAVE_LIBEXPAT */
75 #include "xml-support.h"
77 /* Handle the start of a <segment> element. */
80 library_list_start_segment (struct gdb_xml_parser *parser,
81 const struct gdb_xml_element *element,
83 std::vector<gdb_xml_value> &attributes)
85 VEC(lm_info_target_p) **list = (VEC(lm_info_target_p) **) user_data;
86 lm_info_target *last = VEC_last (lm_info_target_p, *list);
88 = (ULONGEST *) xml_find_attribute (attributes, "address")->value.get ();
89 CORE_ADDR address = (CORE_ADDR) *address_p;
91 if (!last->section_bases.empty ())
92 gdb_xml_error (parser,
93 _("Library list with both segments and sections"));
95 last->segment_bases.push_back (address);
99 library_list_start_section (struct gdb_xml_parser *parser,
100 const struct gdb_xml_element *element,
102 std::vector<gdb_xml_value> &attributes)
104 VEC(lm_info_target_p) **list = (VEC(lm_info_target_p) **) user_data;
105 lm_info_target *last = VEC_last (lm_info_target_p, *list);
107 = (ULONGEST *) xml_find_attribute (attributes, "address")->value.get ();
108 CORE_ADDR address = (CORE_ADDR) *address_p;
110 if (!last->segment_bases.empty ())
111 gdb_xml_error (parser,
112 _("Library list with both segments and sections"));
114 last->section_bases.push_back (address);
117 /* Handle the start of a <library> element. */
120 library_list_start_library (struct gdb_xml_parser *parser,
121 const struct gdb_xml_element *element,
123 std::vector<gdb_xml_value> &attributes)
125 VEC(lm_info_target_p) **list = (VEC(lm_info_target_p) **) user_data;
126 lm_info_target *item = new lm_info_target;
128 = (const char *) xml_find_attribute (attributes, "name")->value.get ();
130 item->name = xstrdup (name);
131 VEC_safe_push (lm_info_target_p, *list, item);
135 library_list_end_library (struct gdb_xml_parser *parser,
136 const struct gdb_xml_element *element,
137 void *user_data, const char *body_text)
139 VEC(lm_info_target_p) **list = (VEC(lm_info_target_p) **) user_data;
140 lm_info_target *lm_info = VEC_last (lm_info_target_p, *list);
142 if (lm_info->segment_bases.empty () && lm_info->section_bases.empty ())
143 gdb_xml_error (parser, _("No segment or section bases defined"));
147 /* Handle the start of a <library-list> element. */
150 library_list_start_list (struct gdb_xml_parser *parser,
151 const struct gdb_xml_element *element,
153 std::vector<gdb_xml_value> &attributes)
155 struct gdb_xml_value *version = xml_find_attribute (attributes, "version");
157 /* #FIXED attribute may be omitted, Expat returns NULL in such case. */
160 const char *string = (const char *) version->value.get ();
162 if (strcmp (string, "1.0") != 0)
163 gdb_xml_error (parser,
164 _("Library list has unsupported version \"%s\""),
169 /* Discard the constructed library list. */
172 solib_target_free_library_list (void *p)
174 VEC(lm_info_target_p) **result = (VEC(lm_info_target_p) **) p;
175 lm_info_target *info;
178 for (ix = 0; VEC_iterate (lm_info_target_p, *result, ix, info); ix++)
181 VEC_free (lm_info_target_p, *result);
185 /* The allowed elements and attributes for an XML library list.
186 The root element is a <library-list>. */
188 static const struct gdb_xml_attribute segment_attributes[] = {
189 { "address", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
190 { NULL, GDB_XML_AF_NONE, NULL, NULL }
193 static const struct gdb_xml_attribute section_attributes[] = {
194 { "address", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
195 { NULL, GDB_XML_AF_NONE, NULL, NULL }
198 static const struct gdb_xml_element library_children[] = {
199 { "segment", segment_attributes, NULL,
200 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
201 library_list_start_segment, NULL },
202 { "section", section_attributes, NULL,
203 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
204 library_list_start_section, NULL },
205 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
208 static const struct gdb_xml_attribute library_attributes[] = {
209 { "name", GDB_XML_AF_NONE, NULL, NULL },
210 { NULL, GDB_XML_AF_NONE, NULL, NULL }
213 static const struct gdb_xml_element library_list_children[] = {
214 { "library", library_attributes, library_children,
215 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
216 library_list_start_library, library_list_end_library },
217 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
220 static const struct gdb_xml_attribute library_list_attributes[] = {
221 { "version", GDB_XML_AF_OPTIONAL, NULL, NULL },
222 { NULL, GDB_XML_AF_NONE, NULL, NULL }
225 static const struct gdb_xml_element library_list_elements[] = {
226 { "library-list", library_list_attributes, library_list_children,
227 GDB_XML_EF_NONE, library_list_start_list, NULL },
228 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
231 static VEC(lm_info_target_p) *
232 solib_target_parse_libraries (const char *library)
234 VEC(lm_info_target_p) *result = NULL;
235 struct cleanup *back_to = make_cleanup (solib_target_free_library_list,
238 if (gdb_xml_parse_quick (_("target library list"), "library-list.dtd",
239 library_list_elements, library, &result) == 0)
241 /* Parsed successfully, keep the result. */
242 discard_cleanups (back_to);
246 do_cleanups (back_to);
251 static struct so_list *
252 solib_target_current_sos (void)
254 struct so_list *new_solib, *start = NULL, *last = NULL;
255 VEC(lm_info_target_p) *library_list;
256 lm_info_target *info;
259 /* Fetch the list of shared libraries. */
260 gdb::optional<gdb::char_vector> library_document
261 = target_read_stralloc (¤t_target, TARGET_OBJECT_LIBRARIES, NULL);
262 if (!library_document)
265 /* Parse the list. */
266 library_list = solib_target_parse_libraries (library_document->data ());
268 if (library_list == NULL)
271 /* Build a struct so_list for each entry on the list. */
272 for (ix = 0; VEC_iterate (lm_info_target_p, library_list, ix, info); ix++)
274 new_solib = XCNEW (struct so_list);
275 strncpy (new_solib->so_name, info->name.c_str (),
276 SO_NAME_MAX_PATH_SIZE - 1);
277 new_solib->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
278 strncpy (new_solib->so_original_name, info->name.c_str (),
279 SO_NAME_MAX_PATH_SIZE - 1);
280 new_solib->so_original_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
281 new_solib->lm_info = info;
283 /* We no longer need this copy of the name. */
286 /* Add it to the list. */
288 last = start = new_solib;
291 last->next = new_solib;
296 /* Free the library list, but not its members. */
297 VEC_free (lm_info_target_p, library_list);
303 solib_target_solib_create_inferior_hook (int from_tty)
305 /* Nothing needed. */
309 solib_target_clear_solib (void)
311 /* Nothing needed. */
315 solib_target_free_so (struct so_list *so)
317 lm_info_target *li = (lm_info_target *) so->lm_info;
319 gdb_assert (li->name.empty ());
325 solib_target_relocate_section_addresses (struct so_list *so,
326 struct target_section *sec)
329 lm_info_target *li = (lm_info_target *) so->lm_info;
331 /* Build the offset table only once per object file. We can not do
332 it any earlier, since we need to open the file first. */
333 if (li->offsets == NULL)
335 int num_sections = gdb_bfd_count_sections (so->abfd);
338 = ((struct section_offsets *)
339 xzalloc (SIZEOF_N_SECTION_OFFSETS (num_sections)));
341 if (!li->section_bases.empty ())
345 int num_alloc_sections = 0;
347 for (i = 0, sect = so->abfd->sections;
349 i++, sect = sect->next)
350 if ((bfd_get_section_flags (so->abfd, sect) & SEC_ALLOC))
351 num_alloc_sections++;
353 if (num_alloc_sections != li->section_bases.size ())
355 Could not relocate shared library \"%s\": wrong number of ALLOC sections"),
362 so->addr_low = ~(CORE_ADDR) 0;
364 for (i = 0, sect = so->abfd->sections;
366 i++, sect = sect->next)
368 if (!(bfd_get_section_flags (so->abfd, sect) & SEC_ALLOC))
370 if (bfd_section_size (so->abfd, sect) > 0)
374 low = li->section_bases[i];
375 high = low + bfd_section_size (so->abfd, sect) - 1;
377 if (low < so->addr_low)
379 if (high > so->addr_high)
380 so->addr_high = high;
381 gdb_assert (so->addr_low <= so->addr_high);
384 li->offsets->offsets[i] = li->section_bases[bases_index];
388 so->addr_low = so->addr_high = 0;
389 gdb_assert (so->addr_low <= so->addr_high);
392 else if (!li->segment_bases.empty ())
394 struct symfile_segment_data *data;
396 data = get_symfile_segment_data (so->abfd);
399 Could not relocate shared library \"%s\": no segments"), so->so_name);
405 if (!symfile_map_offsets_to_segments (so->abfd, data, li->offsets,
406 li->segment_bases.size (),
407 li->segment_bases.data ()))
409 Could not relocate shared library \"%s\": bad offsets"), so->so_name);
411 /* Find the range of addresses to report for this library in
412 "info sharedlibrary". Report any consecutive segments
413 which were relocated as a single unit. */
414 gdb_assert (li->segment_bases.size () > 0);
415 orig_delta = li->segment_bases[0] - data->segment_bases[0];
417 for (i = 1; i < data->num_segments; i++)
419 /* If we have run out of offsets, assume all
420 remaining segments have the same offset. */
421 if (i >= li->segment_bases.size ())
424 /* If this segment does not have the same offset, do
425 not include it in the library's range. */
426 if (li->segment_bases[i] - data->segment_bases[i]
431 so->addr_low = li->segment_bases[0];
432 so->addr_high = (data->segment_bases[i - 1]
433 + data->segment_sizes[i - 1]
435 gdb_assert (so->addr_low <= so->addr_high);
437 free_symfile_segment_data (data);
442 offset = li->offsets->offsets[gdb_bfd_section_index
443 (sec->the_bfd_section->owner,
444 sec->the_bfd_section)];
446 sec->endaddr += offset;
450 solib_target_open_symbol_file_object (int from_tty)
452 /* We can't locate the main symbol file based on the target's
453 knowledge; the user has to specify it. */
458 solib_target_in_dynsym_resolve_code (CORE_ADDR pc)
460 /* We don't have a range of addresses for the dynamic linker; there
461 may not be one in the program's address space. So only report
462 PLT entries (which may be import stubs). */
463 return in_plt_section (pc);
466 struct target_so_ops solib_target_so_ops;
469 _initialize_solib_target (void)
471 solib_target_so_ops.relocate_section_addresses
472 = solib_target_relocate_section_addresses;
473 solib_target_so_ops.free_so = solib_target_free_so;
474 solib_target_so_ops.clear_solib = solib_target_clear_solib;
475 solib_target_so_ops.solib_create_inferior_hook
476 = solib_target_solib_create_inferior_hook;
477 solib_target_so_ops.current_sos = solib_target_current_sos;
478 solib_target_so_ops.open_symbol_file_object
479 = solib_target_open_symbol_file_object;
480 solib_target_so_ops.in_dynsym_resolve_code
481 = solib_target_in_dynsym_resolve_code;
482 solib_target_so_ops.bfd_open = solib_bfd_open;
484 /* Set current_target_so_ops to solib_target_so_ops if not already
486 if (current_target_so_ops == 0)
487 current_target_so_ops = &solib_target_so_ops;