1 /* Definitions for targets which report shared library events.
3 Copyright (C) 2007, 2008, 2009, 2010 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 #include "gdb_string.h"
33 /* Private data for each loaded library. */
36 /* The library's name. The name is normally kept in the struct
37 so_list; it is only here during XML parsing. */
40 /* The target can either specify segment bases or section bases, not
43 /* The base addresses for each independently relocatable segment of
44 this shared library. */
45 VEC(CORE_ADDR
) *segment_bases
;
47 /* The base addresses for each independently allocatable,
48 relocatable section of this shared library. */
49 VEC(CORE_ADDR
) *section_bases
;
51 /* The cached offsets for each section of this shared library,
52 determined from SEGMENT_BASES, or SECTION_BASES. */
53 struct section_offsets
*offsets
;
56 typedef struct lm_info
*lm_info_p
;
59 #if !defined(HAVE_LIBEXPAT)
61 static VEC(lm_info_p
) *
62 solib_target_parse_libraries (const char *library
)
64 static int have_warned
;
69 warning (_("Can not parse XML library list; XML support was disabled "
76 #else /* HAVE_LIBEXPAT */
78 #include "xml-support.h"
80 /* Handle the start of a <segment> element. */
83 library_list_start_segment (struct gdb_xml_parser
*parser
,
84 const struct gdb_xml_element
*element
,
85 void *user_data
, VEC(gdb_xml_value_s
) *attributes
)
87 VEC(lm_info_p
) **list
= user_data
;
88 struct lm_info
*last
= VEC_last (lm_info_p
, *list
);
89 ULONGEST
*address_p
= VEC_index (gdb_xml_value_s
, attributes
, 0)->value
;
90 CORE_ADDR address
= (CORE_ADDR
) *address_p
;
92 if (last
->section_bases
!= NULL
)
93 gdb_xml_error (parser
,
94 _("Library list with both segments and sections"));
96 VEC_safe_push (CORE_ADDR
, last
->segment_bases
, &address
);
100 library_list_start_section (struct gdb_xml_parser
*parser
,
101 const struct gdb_xml_element
*element
,
102 void *user_data
, VEC(gdb_xml_value_s
) *attributes
)
104 VEC(lm_info_p
) **list
= user_data
;
105 struct lm_info
*last
= VEC_last (lm_info_p
, *list
);
106 ULONGEST
*address_p
= VEC_index (gdb_xml_value_s
, attributes
, 0)->value
;
107 CORE_ADDR address
= (CORE_ADDR
) *address_p
;
109 if (last
->segment_bases
!= NULL
)
110 gdb_xml_error (parser
,
111 _("Library list with both segments and sections"));
113 VEC_safe_push (CORE_ADDR
, last
->section_bases
, &address
);
116 /* Handle the start of a <library> element. */
119 library_list_start_library (struct gdb_xml_parser
*parser
,
120 const struct gdb_xml_element
*element
,
121 void *user_data
, VEC(gdb_xml_value_s
) *attributes
)
123 VEC(lm_info_p
) **list
= user_data
;
124 struct lm_info
*item
= XZALLOC (struct lm_info
);
125 const char *name
= VEC_index (gdb_xml_value_s
, attributes
, 0)->value
;
127 item
->name
= xstrdup (name
);
128 VEC_safe_push (lm_info_p
, *list
, item
);
132 library_list_end_library (struct gdb_xml_parser
*parser
,
133 const struct gdb_xml_element
*element
,
134 void *user_data
, const char *body_text
)
136 VEC(lm_info_p
) **list
= user_data
;
137 struct lm_info
*lm_info
= VEC_last (lm_info_p
, *list
);
139 if (lm_info
->segment_bases
== NULL
140 && lm_info
->section_bases
== NULL
)
141 gdb_xml_error (parser
,
142 _("No segment or section bases defined"));
146 /* Handle the start of a <library-list> element. */
149 library_list_start_list (struct gdb_xml_parser
*parser
,
150 const struct gdb_xml_element
*element
,
151 void *user_data
, VEC(gdb_xml_value_s
) *attributes
)
153 char *version
= VEC_index (gdb_xml_value_s
, attributes
, 0)->value
;
155 if (strcmp (version
, "1.0") != 0)
156 gdb_xml_error (parser
,
157 _("Library list has unsupported version \"%s\""),
161 /* Discard the constructed library list. */
164 solib_target_free_library_list (void *p
)
166 VEC(lm_info_p
) **result
= p
;
167 struct lm_info
*info
;
170 for (ix
= 0; VEC_iterate (lm_info_p
, *result
, ix
, info
); ix
++)
173 VEC_free (CORE_ADDR
, info
->segment_bases
);
174 VEC_free (CORE_ADDR
, info
->section_bases
);
177 VEC_free (lm_info_p
, *result
);
181 /* The allowed elements and attributes for an XML library list.
182 The root element is a <library-list>. */
184 const struct gdb_xml_attribute segment_attributes
[] = {
185 { "address", GDB_XML_AF_NONE
, gdb_xml_parse_attr_ulongest
, NULL
},
186 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
189 const struct gdb_xml_attribute section_attributes
[] = {
190 { "address", GDB_XML_AF_NONE
, gdb_xml_parse_attr_ulongest
, NULL
},
191 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
194 const struct gdb_xml_element library_children
[] = {
195 { "segment", segment_attributes
, NULL
,
196 GDB_XML_EF_REPEATABLE
| GDB_XML_EF_OPTIONAL
,
197 library_list_start_segment
, NULL
},
198 { "section", section_attributes
, NULL
,
199 GDB_XML_EF_REPEATABLE
| GDB_XML_EF_OPTIONAL
,
200 library_list_start_section
, NULL
},
201 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
204 const struct gdb_xml_attribute library_attributes
[] = {
205 { "name", GDB_XML_AF_NONE
, NULL
, NULL
},
206 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
209 const struct gdb_xml_element library_list_children
[] = {
210 { "library", library_attributes
, library_children
,
211 GDB_XML_EF_REPEATABLE
| GDB_XML_EF_OPTIONAL
,
212 library_list_start_library
, library_list_end_library
},
213 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
216 const struct gdb_xml_attribute library_list_attributes
[] = {
217 { "version", GDB_XML_AF_NONE
, NULL
, NULL
},
218 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
221 const struct gdb_xml_element library_list_elements
[] = {
222 { "library-list", library_list_attributes
, library_list_children
,
223 GDB_XML_EF_NONE
, library_list_start_list
, NULL
},
224 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
227 static VEC(lm_info_p
) *
228 solib_target_parse_libraries (const char *library
)
230 struct gdb_xml_parser
*parser
;
231 VEC(lm_info_p
) *result
= NULL
;
232 struct cleanup
*before_deleting_result
, *back_to
;
234 back_to
= make_cleanup (null_cleanup
, NULL
);
235 parser
= gdb_xml_create_parser_and_cleanup (_("target library list"),
236 library_list_elements
, &result
);
237 gdb_xml_use_dtd (parser
, "library-list.dtd");
239 before_deleting_result
= make_cleanup (solib_target_free_library_list
,
242 if (gdb_xml_parse (parser
, library
) == 0)
243 /* Parsed successfully, don't need to delete the result. */
244 discard_cleanups (before_deleting_result
);
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 const char *library_document
;
256 VEC(lm_info_p
) *library_list
;
257 struct lm_info
*info
;
260 /* Fetch the list of shared libraries. */
261 library_document
= target_read_stralloc (¤t_target
,
262 TARGET_OBJECT_LIBRARIES
,
264 if (library_document
== NULL
)
267 /* Parse the list. */
268 library_list
= solib_target_parse_libraries (library_document
);
269 if (library_list
== NULL
)
272 /* Build a struct so_list for each entry on the list. */
273 for (ix
= 0; VEC_iterate (lm_info_p
, library_list
, ix
, info
); ix
++)
275 new_solib
= XZALLOC (struct so_list
);
276 strncpy (new_solib
->so_name
, info
->name
, 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
,
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. */
287 /* Add it to the list. */
289 last
= start
= new_solib
;
292 last
->next
= new_solib
;
297 /* Free the library list, but not its members. */
298 VEC_free (lm_info_p
, library_list
);
304 solib_target_special_symbol_handling (void)
306 /* Nothing needed. */
310 solib_target_solib_create_inferior_hook (int from_tty
)
312 /* Nothing needed. */
316 solib_target_clear_solib (void)
318 /* Nothing needed. */
322 solib_target_free_so (struct so_list
*so
)
324 gdb_assert (so
->lm_info
->name
== NULL
);
325 xfree (so
->lm_info
->offsets
);
326 VEC_free (CORE_ADDR
, so
->lm_info
->segment_bases
);
331 solib_target_relocate_section_addresses (struct so_list
*so
,
332 struct target_section
*sec
)
334 int flags
= bfd_get_section_flags (sec
->bfd
, sec
->the_bfd_section
);
337 /* Build the offset table only once per object file. We can not do
338 it any earlier, since we need to open the file first. */
339 if (so
->lm_info
->offsets
== NULL
)
341 int num_sections
= bfd_count_sections (so
->abfd
);
343 so
->lm_info
->offsets
= xzalloc (SIZEOF_N_SECTION_OFFSETS (num_sections
));
345 if (so
->lm_info
->section_bases
)
349 int num_section_bases
350 = VEC_length (CORE_ADDR
, so
->lm_info
->section_bases
);
351 int num_alloc_sections
= 0;
353 for (i
= 0, sect
= so
->abfd
->sections
;
355 i
++, sect
= sect
->next
)
356 if ((bfd_get_section_flags (so
->abfd
, sect
) & SEC_ALLOC
))
357 num_alloc_sections
++;
359 if (num_alloc_sections
!= num_section_bases
)
361 Could not relocate shared library \"%s\": wrong number of ALLOC sections"),
367 CORE_ADDR
*section_bases
;
369 section_bases
= VEC_address (CORE_ADDR
,
370 so
->lm_info
->section_bases
);
372 so
->addr_low
= ~(CORE_ADDR
) 0;
374 for (i
= 0, sect
= so
->abfd
->sections
;
376 i
++, sect
= sect
->next
)
378 if (!(bfd_get_section_flags (so
->abfd
, sect
) & SEC_ALLOC
))
380 if (bfd_section_size (so
->abfd
, sect
) > 0)
384 low
= section_bases
[i
];
385 high
= low
+ bfd_section_size (so
->abfd
, sect
) - 1;
387 if (low
< so
->addr_low
)
389 if (high
> so
->addr_high
)
390 so
->addr_high
= high
;
391 gdb_assert (so
->addr_low
<= so
->addr_high
);
394 so
->lm_info
->offsets
->offsets
[i
] = section_bases
[bases_index
];
398 so
->addr_low
= so
->addr_high
= 0;
399 gdb_assert (so
->addr_low
<= so
->addr_high
);
402 else if (so
->lm_info
->segment_bases
)
404 struct symfile_segment_data
*data
;
406 data
= get_symfile_segment_data (so
->abfd
);
409 Could not relocate shared library \"%s\": no segments"), so
->so_name
);
415 CORE_ADDR
*segment_bases
;
417 num_bases
= VEC_length (CORE_ADDR
, so
->lm_info
->segment_bases
);
418 segment_bases
= VEC_address (CORE_ADDR
,
419 so
->lm_info
->segment_bases
);
421 if (!symfile_map_offsets_to_segments (so
->abfd
, data
,
422 so
->lm_info
->offsets
,
423 num_bases
, segment_bases
))
425 Could not relocate shared library \"%s\": bad offsets"), so
->so_name
);
427 /* Find the range of addresses to report for this library in
428 "info sharedlibrary". Report any consecutive segments
429 which were relocated as a single unit. */
430 gdb_assert (num_bases
> 0);
431 orig_delta
= segment_bases
[0] - data
->segment_bases
[0];
433 for (i
= 1; i
< data
->num_segments
; i
++)
435 /* If we have run out of offsets, assume all
436 remaining segments have the same offset. */
440 /* If this segment does not have the same offset, do
441 not include it in the library's range. */
442 if (segment_bases
[i
] - data
->segment_bases
[i
] != orig_delta
)
446 so
->addr_low
= segment_bases
[0];
447 so
->addr_high
= (data
->segment_bases
[i
- 1]
448 + data
->segment_sizes
[i
- 1]
450 gdb_assert (so
->addr_low
<= so
->addr_high
);
452 free_symfile_segment_data (data
);
457 offset
= so
->lm_info
->offsets
->offsets
[sec
->the_bfd_section
->index
];
459 sec
->endaddr
+= offset
;
463 solib_target_open_symbol_file_object (void *from_ttyp
)
465 /* We can't locate the main symbol file based on the target's
466 knowledge; the user has to specify it. */
471 solib_target_in_dynsym_resolve_code (CORE_ADDR pc
)
473 /* We don't have a range of addresses for the dynamic linker; there
474 may not be one in the program's address space. So only report
475 PLT entries (which may be import stubs). */
476 return in_plt_section (pc
, NULL
);
479 struct target_so_ops solib_target_so_ops
;
481 extern initialize_file_ftype _initialize_solib_target
; /* -Wmissing-prototypes */
484 _initialize_solib_target (void)
486 solib_target_so_ops
.relocate_section_addresses
487 = solib_target_relocate_section_addresses
;
488 solib_target_so_ops
.free_so
= solib_target_free_so
;
489 solib_target_so_ops
.clear_solib
= solib_target_clear_solib
;
490 solib_target_so_ops
.solib_create_inferior_hook
491 = solib_target_solib_create_inferior_hook
;
492 solib_target_so_ops
.special_symbol_handling
493 = solib_target_special_symbol_handling
;
494 solib_target_so_ops
.current_sos
= solib_target_current_sos
;
495 solib_target_so_ops
.open_symbol_file_object
496 = solib_target_open_symbol_file_object
;
497 solib_target_so_ops
.in_dynsym_resolve_code
498 = solib_target_in_dynsym_resolve_code
;
499 solib_target_so_ops
.bfd_open
= solib_bfd_open
;
501 /* Set current_target_so_ops to solib_target_so_ops if not already
503 if (current_target_so_ops
== 0)
504 current_target_so_ops
= &solib_target_so_ops
;