1 /* Definitions for targets which report shared library events.
4 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
31 #include "gdb_string.h"
35 /* Private data for each loaded library. */
38 /* The library's name. The name is normally kept in the struct
39 so_list; it is only here during XML parsing. */
42 /* The base addresses for each independently relocatable segment of
43 this shared library. */
44 VEC(CORE_ADDR) *segment_bases;
46 /* The cached offsets for each section of this shared library,
47 determined from SEGMENT_BASES. */
48 struct section_offsets *offsets;
51 typedef struct lm_info *lm_info_p;
54 #if !defined(HAVE_LIBEXPAT)
57 solib_target_parse_libraries (const char *library)
59 static int have_warned;
64 warning (_("Can not parse XML library list; XML support was disabled "
71 #else /* HAVE_LIBEXPAT */
73 #include "xml-support.h"
75 /* Handle the start of a <segment> element. */
78 library_list_start_segment (struct gdb_xml_parser *parser,
79 const struct gdb_xml_element *element,
80 void *user_data, VEC(gdb_xml_value_s) *attributes)
82 VEC(lm_info_p) **list = user_data;
83 struct lm_info *last = VEC_last (lm_info_p, *list);
84 ULONGEST *address_p = VEC_index (gdb_xml_value_s, attributes, 0)->value;
85 CORE_ADDR address = (CORE_ADDR) *address_p;
87 VEC_safe_push (CORE_ADDR, last->segment_bases, &address);
90 /* Handle the start of a <library> element. */
93 library_list_start_library (struct gdb_xml_parser *parser,
94 const struct gdb_xml_element *element,
95 void *user_data, VEC(gdb_xml_value_s) *attributes)
97 VEC(lm_info_p) **list = user_data;
98 struct lm_info *item = XZALLOC (struct lm_info);
99 const char *name = VEC_index (gdb_xml_value_s, attributes, 0)->value;
101 item->name = xstrdup (name);
102 VEC_safe_push (lm_info_p, *list, item);
105 /* Handle the start of a <library-list> element. */
108 library_list_start_list (struct gdb_xml_parser *parser,
109 const struct gdb_xml_element *element,
110 void *user_data, VEC(gdb_xml_value_s) *attributes)
112 char *version = VEC_index (gdb_xml_value_s, attributes, 0)->value;
114 if (strcmp (version, "1.0") != 0)
115 gdb_xml_error (parser,
116 _("Library list has unsupported version \"%s\""),
120 /* Discard the constructed library list. */
123 solib_target_free_library_list (void *p)
125 VEC(lm_info_p) **result = p;
126 struct lm_info *info;
129 for (ix = 0; VEC_iterate (lm_info_p, *result, ix, info); ix++)
132 VEC_free (CORE_ADDR, info->segment_bases);
135 VEC_free (lm_info_p, *result);
139 /* The allowed elements and attributes for an XML library list.
140 The root element is a <library-list>. */
142 const struct gdb_xml_attribute segment_attributes[] = {
143 { "address", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
144 { NULL, GDB_XML_AF_NONE, NULL, NULL }
147 const struct gdb_xml_element library_children[] = {
148 { "segment", segment_attributes, NULL, GDB_XML_EF_REPEATABLE,
149 library_list_start_segment, NULL },
150 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
153 const struct gdb_xml_attribute library_attributes[] = {
154 { "name", GDB_XML_AF_NONE, NULL, NULL },
155 { NULL, GDB_XML_AF_NONE, NULL, NULL }
158 const struct gdb_xml_element library_list_children[] = {
159 { "library", library_attributes, library_children,
160 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
161 library_list_start_library, NULL },
162 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
165 const struct gdb_xml_attribute library_list_attributes[] = {
166 { "version", GDB_XML_AF_NONE, NULL, NULL },
167 { NULL, GDB_XML_AF_NONE, NULL, NULL }
170 const struct gdb_xml_element library_list_elements[] = {
171 { "library-list", library_list_attributes, library_list_children,
172 GDB_XML_EF_NONE, library_list_start_list, NULL },
173 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
176 static VEC(lm_info_p) *
177 solib_target_parse_libraries (const char *library)
179 struct gdb_xml_parser *parser;
180 VEC(lm_info_p) *result = NULL;
181 struct cleanup *before_deleting_result, *back_to;
183 back_to = make_cleanup (null_cleanup, NULL);
184 parser = gdb_xml_create_parser_and_cleanup (_("target library list"),
185 library_list_elements, &result);
186 gdb_xml_use_dtd (parser, "library-list.dtd");
188 before_deleting_result = make_cleanup (solib_target_free_library_list,
191 if (gdb_xml_parse (parser, library) == 0)
192 /* Parsed successfully, don't need to delete the result. */
193 discard_cleanups (before_deleting_result);
195 do_cleanups (back_to);
200 static struct so_list *
201 solib_target_current_sos (void)
203 struct so_list *new_solib, *start = NULL, *last = NULL;
204 const char *library_document;
205 VEC(lm_info_p) *library_list;
206 struct lm_info *info;
209 /* Fetch the list of shared libraries. */
210 library_document = target_read_stralloc (¤t_target,
211 TARGET_OBJECT_LIBRARIES,
213 if (library_document == NULL)
216 /* Parse the list. */
217 library_list = solib_target_parse_libraries (library_document);
218 if (library_list == NULL)
221 /* Build a struct so_list for each entry on the list. */
222 for (ix = 0; VEC_iterate (lm_info_p, library_list, ix, info); ix++)
224 new_solib = XZALLOC (struct so_list);
225 strncpy (new_solib->so_name, info->name, SO_NAME_MAX_PATH_SIZE - 1);
226 new_solib->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
227 strncpy (new_solib->so_original_name, info->name,
228 SO_NAME_MAX_PATH_SIZE - 1);
229 new_solib->so_original_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
230 new_solib->lm_info = info;
232 /* We no longer need this copy of the name. */
236 /* Add it to the list. */
238 last = start = new_solib;
241 last->next = new_solib;
246 /* Free the library list, but not its members. */
247 VEC_free (lm_info_p, library_list);
253 solib_target_special_symbol_handling (void)
255 /* Nothing needed. */
259 solib_target_solib_create_inferior_hook (void)
261 /* Nothing needed. */
265 solib_target_clear_solib (void)
267 /* Nothing needed. */
271 solib_target_free_so (struct so_list *so)
273 gdb_assert (so->lm_info->name == NULL);
274 xfree (so->lm_info->offsets);
275 VEC_free (CORE_ADDR, so->lm_info->segment_bases);
280 solib_target_relocate_section_addresses (struct so_list *so,
281 struct section_table *sec)
283 int flags = bfd_get_section_flags (sec->bfd, sec->the_bfd_section);
286 /* Build the offset table only once per object file. We can not do
287 it any earlier, since we need to open the file first. */
288 if (so->lm_info->offsets == NULL)
290 struct symfile_segment_data *data;
291 int num_sections = bfd_count_sections (so->abfd);
293 so->lm_info->offsets = xzalloc (SIZEOF_N_SECTION_OFFSETS (num_sections));
295 data = get_symfile_segment_data (so->abfd);
297 warning (_("Could not relocate shared library \"%s\": no segments"),
303 int num_bases = VEC_length (CORE_ADDR, so->lm_info->segment_bases);
304 CORE_ADDR *segment_bases = VEC_address (CORE_ADDR,
305 so->lm_info->segment_bases);
307 if (!symfile_map_offsets_to_segments (so->abfd, data,
308 so->lm_info->offsets,
309 num_bases, segment_bases))
310 warning (_("Could not relocate shared library \"%s\": bad offsets"),
313 /* Find the range of addresses to report for this library in
314 "info sharedlibrary". Report any consecutive segments
315 which were relocated as a single unit. */
316 gdb_assert (num_bases > 0);
317 orig_delta = segment_bases[0] - data->segment_bases[0];
319 for (i = 1; i < data->num_segments; i++)
321 /* If we have run out of offsets, assume all remaining segments
322 have the same offset. */
326 /* If this segment does not have the same offset, do not include
327 it in the library's range. */
328 if (segment_bases[i] - data->segment_bases[i] != orig_delta)
332 so->addr_low = segment_bases[0];
333 so->addr_high = (data->segment_bases[i - 1]
334 + data->segment_sizes[i - 1]
336 gdb_assert (so->addr_low <= so->addr_high);
338 free_symfile_segment_data (data);
342 offset = so->lm_info->offsets->offsets[sec->the_bfd_section->index];
344 sec->endaddr += offset;
348 solib_target_open_symbol_file_object (void *from_ttyp)
350 /* We can't locate the main symbol file based on the target's
351 knowledge; the user has to specify it. */
356 solib_target_in_dynsym_resolve_code (CORE_ADDR pc)
358 /* We don't have a range of addresses for the dynamic linker; there
359 may not be one in the program's address space. So only report
360 PLT entries (which may be import stubs). */
361 return in_plt_section (pc, NULL);
364 static struct target_so_ops solib_target_so_ops;
366 extern initialize_file_ftype _initialize_solib_target; /* -Wmissing-prototypes */
369 _initialize_solib_target (void)
371 solib_target_so_ops.relocate_section_addresses
372 = solib_target_relocate_section_addresses;
373 solib_target_so_ops.free_so = solib_target_free_so;
374 solib_target_so_ops.clear_solib = solib_target_clear_solib;
375 solib_target_so_ops.solib_create_inferior_hook
376 = solib_target_solib_create_inferior_hook;
377 solib_target_so_ops.special_symbol_handling
378 = solib_target_special_symbol_handling;
379 solib_target_so_ops.current_sos = solib_target_current_sos;
380 solib_target_so_ops.open_symbol_file_object
381 = solib_target_open_symbol_file_object;
382 solib_target_so_ops.in_dynsym_resolve_code
383 = solib_target_in_dynsym_resolve_code;
385 current_target_so_ops = &solib_target_so_ops;