1 /* Definitions for targets which report shared library events.
3 Copyright (C) 2007-2014 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"
31 /* Private data for each loaded library. */
34 /* The library's name. The name is normally kept in the struct
35 so_list; it is only here during XML parsing. */
38 /* The target can either specify segment bases or section bases, not
41 /* The base addresses for each independently relocatable segment of
42 this shared library. */
43 VEC(CORE_ADDR) *segment_bases;
45 /* The base addresses for each independently allocatable,
46 relocatable section of this shared library. */
47 VEC(CORE_ADDR) *section_bases;
49 /* The cached offsets for each section of this shared library,
50 determined from SEGMENT_BASES, or SECTION_BASES. */
51 struct section_offsets *offsets;
54 typedef struct lm_info *lm_info_p;
57 #if !defined(HAVE_LIBEXPAT)
59 static VEC(lm_info_p) *
60 solib_target_parse_libraries (const char *library)
62 static int have_warned;
67 warning (_("Can not parse XML library list; XML support was disabled "
74 #else /* HAVE_LIBEXPAT */
76 #include "xml-support.h"
78 /* Handle the start of a <segment> element. */
81 library_list_start_segment (struct gdb_xml_parser *parser,
82 const struct gdb_xml_element *element,
83 void *user_data, VEC(gdb_xml_value_s) *attributes)
85 VEC(lm_info_p) **list = user_data;
86 struct lm_info *last = VEC_last (lm_info_p, *list);
87 ULONGEST *address_p = xml_find_attribute (attributes, "address")->value;
88 CORE_ADDR address = (CORE_ADDR) *address_p;
90 if (last->section_bases != NULL)
91 gdb_xml_error (parser,
92 _("Library list with both segments and sections"));
94 VEC_safe_push (CORE_ADDR, last->segment_bases, address);
98 library_list_start_section (struct gdb_xml_parser *parser,
99 const struct gdb_xml_element *element,
100 void *user_data, VEC(gdb_xml_value_s) *attributes)
102 VEC(lm_info_p) **list = user_data;
103 struct lm_info *last = VEC_last (lm_info_p, *list);
104 ULONGEST *address_p = 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 = user_data;
122 struct lm_info *item = XCNEW (struct lm_info);
123 const char *name = xml_find_attribute (attributes, "name")->value;
125 item->name = xstrdup (name);
126 VEC_safe_push (lm_info_p, *list, item);
130 library_list_end_library (struct gdb_xml_parser *parser,
131 const struct gdb_xml_element *element,
132 void *user_data, const char *body_text)
134 VEC(lm_info_p) **list = user_data;
135 struct lm_info *lm_info = VEC_last (lm_info_p, *list);
137 if (lm_info->segment_bases == NULL
138 && lm_info->section_bases == NULL)
139 gdb_xml_error (parser,
140 _("No segment or section bases defined"));
144 /* Handle the start of a <library-list> element. */
147 library_list_start_list (struct gdb_xml_parser *parser,
148 const struct gdb_xml_element *element,
149 void *user_data, VEC(gdb_xml_value_s) *attributes)
151 char *version = xml_find_attribute (attributes, "version")->value;
153 if (strcmp (version, "1.0") != 0)
154 gdb_xml_error (parser,
155 _("Library list has unsupported version \"%s\""),
159 /* Discard the constructed library list. */
162 solib_target_free_library_list (void *p)
164 VEC(lm_info_p) **result = p;
165 struct lm_info *info;
168 for (ix = 0; VEC_iterate (lm_info_p, *result, ix, info); ix++)
171 VEC_free (CORE_ADDR, info->segment_bases);
172 VEC_free (CORE_ADDR, info->section_bases);
175 VEC_free (lm_info_p, *result);
179 /* The allowed elements and attributes for an XML library list.
180 The root element is a <library-list>. */
182 static const struct gdb_xml_attribute segment_attributes[] = {
183 { "address", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
184 { NULL, GDB_XML_AF_NONE, NULL, NULL }
187 static const struct gdb_xml_attribute section_attributes[] = {
188 { "address", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
189 { NULL, GDB_XML_AF_NONE, NULL, NULL }
192 static const struct gdb_xml_element library_children[] = {
193 { "segment", segment_attributes, NULL,
194 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
195 library_list_start_segment, NULL },
196 { "section", section_attributes, NULL,
197 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
198 library_list_start_section, NULL },
199 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
202 static const struct gdb_xml_attribute library_attributes[] = {
203 { "name", GDB_XML_AF_NONE, NULL, NULL },
204 { NULL, GDB_XML_AF_NONE, NULL, NULL }
207 static const struct gdb_xml_element library_list_children[] = {
208 { "library", library_attributes, library_children,
209 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
210 library_list_start_library, library_list_end_library },
211 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
214 static const struct gdb_xml_attribute library_list_attributes[] = {
215 { "version", GDB_XML_AF_NONE, NULL, NULL },
216 { NULL, GDB_XML_AF_NONE, NULL, NULL }
219 static const struct gdb_xml_element library_list_elements[] = {
220 { "library-list", library_list_attributes, library_list_children,
221 GDB_XML_EF_NONE, library_list_start_list, NULL },
222 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
225 static VEC(lm_info_p) *
226 solib_target_parse_libraries (const char *library)
228 VEC(lm_info_p) *result = NULL;
229 struct cleanup *back_to = make_cleanup (solib_target_free_library_list,
232 if (gdb_xml_parse_quick (_("target library list"), "library-list.dtd",
233 library_list_elements, library, &result) == 0)
235 /* Parsed successfully, keep the result. */
236 discard_cleanups (back_to);
240 do_cleanups (back_to);
245 static struct so_list *
246 solib_target_current_sos (void)
248 struct so_list *new_solib, *start = NULL, *last = NULL;
249 char *library_document;
250 struct cleanup *old_chain;
251 VEC(lm_info_p) *library_list;
252 struct lm_info *info;
255 /* Fetch the list of shared libraries. */
256 library_document = target_read_stralloc (¤t_target,
257 TARGET_OBJECT_LIBRARIES,
259 if (library_document == NULL)
262 /* solib_target_parse_libraries may throw, so we use a cleanup. */
263 old_chain = make_cleanup (xfree, library_document);
265 /* Parse the list. */
266 library_list = solib_target_parse_libraries (library_document);
268 /* library_document string is not needed behind this point. */
269 do_cleanups (old_chain);
271 if (library_list == NULL)
274 /* Build a struct so_list for each entry on the list. */
275 for (ix = 0; VEC_iterate (lm_info_p, library_list, ix, info); ix++)
277 new_solib = XCNEW (struct so_list);
278 strncpy (new_solib->so_name, info->name, SO_NAME_MAX_PATH_SIZE - 1);
279 new_solib->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
280 strncpy (new_solib->so_original_name, info->name,
281 SO_NAME_MAX_PATH_SIZE - 1);
282 new_solib->so_original_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
283 new_solib->lm_info = info;
285 /* We no longer need this copy of the name. */
289 /* Add it to the list. */
291 last = start = new_solib;
294 last->next = new_solib;
299 /* Free the library list, but not its members. */
300 VEC_free (lm_info_p, library_list);
306 solib_target_special_symbol_handling (void)
308 /* Nothing needed. */
312 solib_target_solib_create_inferior_hook (int from_tty)
314 /* Nothing needed. */
318 solib_target_clear_solib (void)
320 /* Nothing needed. */
324 solib_target_free_so (struct so_list *so)
326 gdb_assert (so->lm_info->name == NULL);
327 xfree (so->lm_info->offsets);
328 VEC_free (CORE_ADDR, so->lm_info->segment_bases);
333 solib_target_relocate_section_addresses (struct so_list *so,
334 struct target_section *sec)
338 /* Build the offset table only once per object file. We can not do
339 it any earlier, since we need to open the file first. */
340 if (so->lm_info->offsets == NULL)
342 int num_sections = gdb_bfd_count_sections (so->abfd);
344 so->lm_info->offsets = xzalloc (SIZEOF_N_SECTION_OFFSETS (num_sections));
346 if (so->lm_info->section_bases)
350 int num_section_bases
351 = VEC_length (CORE_ADDR, so->lm_info->section_bases);
352 int num_alloc_sections = 0;
354 for (i = 0, sect = so->abfd->sections;
356 i++, sect = sect->next)
357 if ((bfd_get_section_flags (so->abfd, sect) & SEC_ALLOC))
358 num_alloc_sections++;
360 if (num_alloc_sections != num_section_bases)
362 Could not relocate shared library \"%s\": wrong number of ALLOC sections"),
368 CORE_ADDR *section_bases;
370 section_bases = VEC_address (CORE_ADDR,
371 so->lm_info->section_bases);
373 so->addr_low = ~(CORE_ADDR) 0;
375 for (i = 0, sect = so->abfd->sections;
377 i++, sect = sect->next)
379 if (!(bfd_get_section_flags (so->abfd, sect) & SEC_ALLOC))
381 if (bfd_section_size (so->abfd, sect) > 0)
385 low = section_bases[i];
386 high = low + bfd_section_size (so->abfd, sect) - 1;
388 if (low < so->addr_low)
390 if (high > so->addr_high)
391 so->addr_high = high;
392 gdb_assert (so->addr_low <= so->addr_high);
395 so->lm_info->offsets->offsets[i]
396 = section_bases[bases_index];
400 so->addr_low = so->addr_high = 0;
401 gdb_assert (so->addr_low <= so->addr_high);
404 else if (so->lm_info->segment_bases)
406 struct symfile_segment_data *data;
408 data = get_symfile_segment_data (so->abfd);
411 Could not relocate shared library \"%s\": no segments"), so->so_name);
417 CORE_ADDR *segment_bases;
419 num_bases = VEC_length (CORE_ADDR, so->lm_info->segment_bases);
420 segment_bases = VEC_address (CORE_ADDR,
421 so->lm_info->segment_bases);
423 if (!symfile_map_offsets_to_segments (so->abfd, data,
424 so->lm_info->offsets,
425 num_bases, segment_bases))
427 Could not relocate shared library \"%s\": bad offsets"), so->so_name);
429 /* Find the range of addresses to report for this library in
430 "info sharedlibrary". Report any consecutive segments
431 which were relocated as a single unit. */
432 gdb_assert (num_bases > 0);
433 orig_delta = segment_bases[0] - data->segment_bases[0];
435 for (i = 1; i < data->num_segments; i++)
437 /* If we have run out of offsets, assume all
438 remaining segments have the same offset. */
442 /* If this segment does not have the same offset, do
443 not include it in the library's range. */
444 if (segment_bases[i] - data->segment_bases[i] != orig_delta)
448 so->addr_low = segment_bases[0];
449 so->addr_high = (data->segment_bases[i - 1]
450 + data->segment_sizes[i - 1]
452 gdb_assert (so->addr_low <= so->addr_high);
454 free_symfile_segment_data (data);
459 offset = so->lm_info->offsets->offsets[gdb_bfd_section_index
460 (sec->the_bfd_section->owner,
461 sec->the_bfd_section)];
463 sec->endaddr += offset;
467 solib_target_open_symbol_file_object (void *from_ttyp)
469 /* We can't locate the main symbol file based on the target's
470 knowledge; the user has to specify it. */
475 solib_target_in_dynsym_resolve_code (CORE_ADDR pc)
477 /* We don't have a range of addresses for the dynamic linker; there
478 may not be one in the program's address space. So only report
479 PLT entries (which may be import stubs). */
480 return in_plt_section (pc);
483 struct target_so_ops solib_target_so_ops;
485 /* -Wmissing-prototypes */
486 extern initialize_file_ftype _initialize_solib_target;
489 _initialize_solib_target (void)
491 solib_target_so_ops.relocate_section_addresses
492 = solib_target_relocate_section_addresses;
493 solib_target_so_ops.free_so = solib_target_free_so;
494 solib_target_so_ops.clear_solib = solib_target_clear_solib;
495 solib_target_so_ops.solib_create_inferior_hook
496 = solib_target_solib_create_inferior_hook;
497 solib_target_so_ops.special_symbol_handling
498 = solib_target_special_symbol_handling;
499 solib_target_so_ops.current_sos = solib_target_current_sos;
500 solib_target_so_ops.open_symbol_file_object
501 = solib_target_open_symbol_file_object;
502 solib_target_so_ops.in_dynsym_resolve_code
503 = solib_target_in_dynsym_resolve_code;
504 solib_target_so_ops.bfd_open = solib_bfd_open;
506 /* Set current_target_so_ops to solib_target_so_ops if not already
508 if (current_target_so_ops == 0)
509 current_target_so_ops = &solib_target_so_ops;