Commit | Line | Data |
---|---|---|
cfa9d6d9 DJ |
1 | /* Definitions for targets which report shared library events. |
2 | ||
42a4f53d | 3 | Copyright (C) 2007-2019 Free Software Foundation, Inc. |
cfa9d6d9 DJ |
4 | |
5 | This file is part of GDB. | |
6 | ||
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 | |
a9762ec7 | 9 | the Free Software Foundation; either version 3 of the License, or |
cfa9d6d9 DJ |
10 | (at your option) any later version. |
11 | ||
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. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
a9762ec7 | 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
cfa9d6d9 DJ |
19 | |
20 | #include "defs.h" | |
21 | #include "objfiles.h" | |
22 | #include "solist.h" | |
23 | #include "symtab.h" | |
24 | #include "symfile.h" | |
25 | #include "target.h" | |
268a13a5 | 26 | #include "gdbsupport/vec.h" |
8d465389 | 27 | #include "solib-target.h" |
f2f46dfc | 28 | #include <vector> |
cfa9d6d9 | 29 | |
cfa9d6d9 | 30 | /* Private data for each loaded library. */ |
d0e449a1 | 31 | struct lm_info_target : public lm_info_base |
cfa9d6d9 DJ |
32 | { |
33 | /* The library's name. The name is normally kept in the struct | |
34 | so_list; it is only here during XML parsing. */ | |
51046d9e | 35 | std::string name; |
cfa9d6d9 | 36 | |
1fddbabb PA |
37 | /* The target can either specify segment bases or section bases, not |
38 | both. */ | |
39 | ||
cfa9d6d9 DJ |
40 | /* The base addresses for each independently relocatable segment of |
41 | this shared library. */ | |
f2f46dfc | 42 | std::vector<CORE_ADDR> segment_bases; |
cfa9d6d9 | 43 | |
1fddbabb PA |
44 | /* The base addresses for each independently allocatable, |
45 | relocatable section of this shared library. */ | |
f2f46dfc | 46 | std::vector<CORE_ADDR> section_bases; |
1fddbabb | 47 | |
cfa9d6d9 | 48 | /* The cached offsets for each section of this shared library, |
1fddbabb | 49 | determined from SEGMENT_BASES, or SECTION_BASES. */ |
51046d9e | 50 | section_offsets *offsets = NULL; |
cfa9d6d9 DJ |
51 | }; |
52 | ||
776489e0 | 53 | typedef std::vector<std::unique_ptr<lm_info_target>> lm_info_vector; |
cfa9d6d9 DJ |
54 | |
55 | #if !defined(HAVE_LIBEXPAT) | |
56 | ||
776489e0 | 57 | static lm_info_vector |
cfa9d6d9 DJ |
58 | solib_target_parse_libraries (const char *library) |
59 | { | |
60 | static int have_warned; | |
61 | ||
62 | if (!have_warned) | |
63 | { | |
64 | have_warned = 1; | |
65 | warning (_("Can not parse XML library list; XML support was disabled " | |
66 | "at compile time")); | |
67 | } | |
68 | ||
776489e0 | 69 | return lm_info_vector (); |
cfa9d6d9 DJ |
70 | } |
71 | ||
72 | #else /* HAVE_LIBEXPAT */ | |
73 | ||
74 | #include "xml-support.h" | |
75 | ||
76 | /* Handle the start of a <segment> element. */ | |
77 | ||
78 | static void | |
79 | library_list_start_segment (struct gdb_xml_parser *parser, | |
80 | const struct gdb_xml_element *element, | |
4d0fdd9b SM |
81 | void *user_data, |
82 | std::vector<gdb_xml_value> &attributes) | |
cfa9d6d9 | 83 | { |
776489e0 TT |
84 | lm_info_vector *list = (lm_info_vector *) user_data; |
85 | lm_info_target *last = list->back ().get (); | |
19ba03f4 | 86 | ULONGEST *address_p |
4d0fdd9b | 87 | = (ULONGEST *) xml_find_attribute (attributes, "address")->value.get (); |
358eb95e | 88 | CORE_ADDR address = (CORE_ADDR) *address_p; |
cfa9d6d9 | 89 | |
f2f46dfc | 90 | if (!last->section_bases.empty ()) |
1fddbabb PA |
91 | gdb_xml_error (parser, |
92 | _("Library list with both segments and sections")); | |
93 | ||
f2f46dfc | 94 | last->segment_bases.push_back (address); |
cfa9d6d9 DJ |
95 | } |
96 | ||
1fddbabb PA |
97 | static void |
98 | library_list_start_section (struct gdb_xml_parser *parser, | |
99 | const struct gdb_xml_element *element, | |
4d0fdd9b SM |
100 | void *user_data, |
101 | std::vector<gdb_xml_value> &attributes) | |
1fddbabb | 102 | { |
776489e0 TT |
103 | lm_info_vector *list = (lm_info_vector *) user_data; |
104 | lm_info_target *last = list->back ().get (); | |
19ba03f4 | 105 | ULONGEST *address_p |
4d0fdd9b | 106 | = (ULONGEST *) xml_find_attribute (attributes, "address")->value.get (); |
1fddbabb PA |
107 | CORE_ADDR address = (CORE_ADDR) *address_p; |
108 | ||
f2f46dfc | 109 | if (!last->segment_bases.empty ()) |
1fddbabb PA |
110 | gdb_xml_error (parser, |
111 | _("Library list with both segments and sections")); | |
112 | ||
f2f46dfc | 113 | last->section_bases.push_back (address); |
1fddbabb PA |
114 | } |
115 | ||
cfa9d6d9 DJ |
116 | /* Handle the start of a <library> element. */ |
117 | ||
118 | static void | |
119 | library_list_start_library (struct gdb_xml_parser *parser, | |
120 | const struct gdb_xml_element *element, | |
4d0fdd9b SM |
121 | void *user_data, |
122 | std::vector<gdb_xml_value> &attributes) | |
cfa9d6d9 | 123 | { |
776489e0 | 124 | lm_info_vector *list = (lm_info_vector *) user_data; |
51046d9e | 125 | lm_info_target *item = new lm_info_target; |
2f667667 | 126 | item->name |
4d0fdd9b | 127 | = (const char *) xml_find_attribute (attributes, "name")->value.get (); |
cfa9d6d9 | 128 | |
776489e0 | 129 | list->emplace_back (item); |
cfa9d6d9 DJ |
130 | } |
131 | ||
1fddbabb PA |
132 | static void |
133 | library_list_end_library (struct gdb_xml_parser *parser, | |
134 | const struct gdb_xml_element *element, | |
135 | void *user_data, const char *body_text) | |
136 | { | |
776489e0 TT |
137 | lm_info_vector *list = (lm_info_vector *) user_data; |
138 | lm_info_target *lm_info = list->back ().get (); | |
433759f7 | 139 | |
f2f46dfc SM |
140 | if (lm_info->segment_bases.empty () && lm_info->section_bases.empty ()) |
141 | gdb_xml_error (parser, _("No segment or section bases defined")); | |
1fddbabb PA |
142 | } |
143 | ||
144 | ||
cfa9d6d9 DJ |
145 | /* Handle the start of a <library-list> element. */ |
146 | ||
147 | static void | |
148 | library_list_start_list (struct gdb_xml_parser *parser, | |
149 | const struct gdb_xml_element *element, | |
4d0fdd9b SM |
150 | void *user_data, |
151 | std::vector<gdb_xml_value> &attributes) | |
cfa9d6d9 | 152 | { |
24c05f46 | 153 | struct gdb_xml_value *version = xml_find_attribute (attributes, "version"); |
cfa9d6d9 | 154 | |
24c05f46 | 155 | /* #FIXED attribute may be omitted, Expat returns NULL in such case. */ |
8847cac2 | 156 | if (version != NULL) |
24c05f46 | 157 | { |
4d0fdd9b | 158 | const char *string = (const char *) version->value.get (); |
24c05f46 JK |
159 | |
160 | if (strcmp (string, "1.0") != 0) | |
161 | gdb_xml_error (parser, | |
162 | _("Library list has unsupported version \"%s\""), | |
21047726 | 163 | string); |
24c05f46 | 164 | } |
cfa9d6d9 DJ |
165 | } |
166 | ||
cfa9d6d9 DJ |
167 | /* The allowed elements and attributes for an XML library list. |
168 | The root element is a <library-list>. */ | |
169 | ||
d6c10e95 | 170 | static const struct gdb_xml_attribute segment_attributes[] = { |
cfa9d6d9 DJ |
171 | { "address", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL }, |
172 | { NULL, GDB_XML_AF_NONE, NULL, NULL } | |
173 | }; | |
174 | ||
d6c10e95 | 175 | static const struct gdb_xml_attribute section_attributes[] = { |
1fddbabb PA |
176 | { "address", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL }, |
177 | { NULL, GDB_XML_AF_NONE, NULL, NULL } | |
178 | }; | |
179 | ||
d6c10e95 | 180 | static const struct gdb_xml_element library_children[] = { |
1fddbabb PA |
181 | { "segment", segment_attributes, NULL, |
182 | GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL, | |
cfa9d6d9 | 183 | library_list_start_segment, NULL }, |
1fddbabb PA |
184 | { "section", section_attributes, NULL, |
185 | GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL, | |
186 | library_list_start_section, NULL }, | |
cfa9d6d9 DJ |
187 | { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL } |
188 | }; | |
189 | ||
d6c10e95 | 190 | static const struct gdb_xml_attribute library_attributes[] = { |
cfa9d6d9 DJ |
191 | { "name", GDB_XML_AF_NONE, NULL, NULL }, |
192 | { NULL, GDB_XML_AF_NONE, NULL, NULL } | |
193 | }; | |
194 | ||
d6c10e95 | 195 | static const struct gdb_xml_element library_list_children[] = { |
cfa9d6d9 DJ |
196 | { "library", library_attributes, library_children, |
197 | GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL, | |
1fddbabb | 198 | library_list_start_library, library_list_end_library }, |
cfa9d6d9 DJ |
199 | { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL } |
200 | }; | |
201 | ||
d6c10e95 | 202 | static const struct gdb_xml_attribute library_list_attributes[] = { |
24c05f46 | 203 | { "version", GDB_XML_AF_OPTIONAL, NULL, NULL }, |
cfa9d6d9 DJ |
204 | { NULL, GDB_XML_AF_NONE, NULL, NULL } |
205 | }; | |
206 | ||
d6c10e95 | 207 | static const struct gdb_xml_element library_list_elements[] = { |
cfa9d6d9 DJ |
208 | { "library-list", library_list_attributes, library_list_children, |
209 | GDB_XML_EF_NONE, library_list_start_list, NULL }, | |
210 | { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL } | |
211 | }; | |
212 | ||
776489e0 | 213 | static lm_info_vector |
cfa9d6d9 DJ |
214 | solib_target_parse_libraries (const char *library) |
215 | { | |
776489e0 | 216 | lm_info_vector result; |
cfa9d6d9 | 217 | |
efc0eabd PA |
218 | if (gdb_xml_parse_quick (_("target library list"), "library-list.dtd", |
219 | library_list_elements, library, &result) == 0) | |
220 | { | |
776489e0 | 221 | /* Parsed successfully. */ |
efc0eabd PA |
222 | return result; |
223 | } | |
cfa9d6d9 | 224 | |
776489e0 TT |
225 | result.clear (); |
226 | return result; | |
cfa9d6d9 DJ |
227 | } |
228 | #endif | |
229 | ||
230 | static struct so_list * | |
231 | solib_target_current_sos (void) | |
232 | { | |
233 | struct so_list *new_solib, *start = NULL, *last = NULL; | |
cfa9d6d9 DJ |
234 | |
235 | /* Fetch the list of shared libraries. */ | |
9018be22 | 236 | gdb::optional<gdb::char_vector> library_document |
8b88a78e PA |
237 | = target_read_stralloc (current_top_target (), TARGET_OBJECT_LIBRARIES, |
238 | NULL); | |
9018be22 | 239 | if (!library_document) |
cfa9d6d9 DJ |
240 | return NULL; |
241 | ||
242 | /* Parse the list. */ | |
776489e0 TT |
243 | lm_info_vector library_list |
244 | = solib_target_parse_libraries (library_document->data ()); | |
b25d79d4 | 245 | |
776489e0 | 246 | if (library_list.empty ()) |
cfa9d6d9 DJ |
247 | return NULL; |
248 | ||
249 | /* Build a struct so_list for each entry on the list. */ | |
776489e0 | 250 | for (auto &&info : library_list) |
cfa9d6d9 | 251 | { |
41bf6aca | 252 | new_solib = XCNEW (struct so_list); |
51046d9e SM |
253 | strncpy (new_solib->so_name, info->name.c_str (), |
254 | SO_NAME_MAX_PATH_SIZE - 1); | |
cfa9d6d9 | 255 | new_solib->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0'; |
51046d9e | 256 | strncpy (new_solib->so_original_name, info->name.c_str (), |
cfa9d6d9 DJ |
257 | SO_NAME_MAX_PATH_SIZE - 1); |
258 | new_solib->so_original_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0'; | |
cfa9d6d9 DJ |
259 | |
260 | /* We no longer need this copy of the name. */ | |
51046d9e | 261 | info->name.clear (); |
cfa9d6d9 | 262 | |
776489e0 TT |
263 | new_solib->lm_info = info.release (); |
264 | ||
cfa9d6d9 DJ |
265 | /* Add it to the list. */ |
266 | if (!start) | |
267 | last = start = new_solib; | |
268 | else | |
269 | { | |
270 | last->next = new_solib; | |
271 | last = new_solib; | |
272 | } | |
273 | } | |
274 | ||
cfa9d6d9 DJ |
275 | return start; |
276 | } | |
277 | ||
cfa9d6d9 | 278 | static void |
268a4a75 | 279 | solib_target_solib_create_inferior_hook (int from_tty) |
cfa9d6d9 DJ |
280 | { |
281 | /* Nothing needed. */ | |
282 | } | |
283 | ||
284 | static void | |
285 | solib_target_clear_solib (void) | |
286 | { | |
287 | /* Nothing needed. */ | |
288 | } | |
289 | ||
290 | static void | |
291 | solib_target_free_so (struct so_list *so) | |
292 | { | |
d0e449a1 SM |
293 | lm_info_target *li = (lm_info_target *) so->lm_info; |
294 | ||
51046d9e SM |
295 | gdb_assert (li->name.empty ()); |
296 | ||
297 | delete li; | |
cfa9d6d9 DJ |
298 | } |
299 | ||
300 | static void | |
301 | solib_target_relocate_section_addresses (struct so_list *so, | |
0542c86d | 302 | struct target_section *sec) |
cfa9d6d9 | 303 | { |
cfa9d6d9 | 304 | CORE_ADDR offset; |
d0e449a1 | 305 | lm_info_target *li = (lm_info_target *) so->lm_info; |
cfa9d6d9 DJ |
306 | |
307 | /* Build the offset table only once per object file. We can not do | |
308 | it any earlier, since we need to open the file first. */ | |
d0e449a1 | 309 | if (li->offsets == NULL) |
cfa9d6d9 | 310 | { |
d445b2f6 | 311 | int num_sections = gdb_bfd_count_sections (so->abfd); |
cfa9d6d9 | 312 | |
d0e449a1 | 313 | li->offsets |
224c3ddb SM |
314 | = ((struct section_offsets *) |
315 | xzalloc (SIZEOF_N_SECTION_OFFSETS (num_sections))); | |
cfa9d6d9 | 316 | |
f2f46dfc | 317 | if (!li->section_bases.empty ()) |
cfa9d6d9 | 318 | { |
cfa9d6d9 | 319 | int i; |
1fddbabb | 320 | asection *sect; |
1fddbabb PA |
321 | int num_alloc_sections = 0; |
322 | ||
323 | for (i = 0, sect = so->abfd->sections; | |
324 | sect != NULL; | |
325 | i++, sect = sect->next) | |
fd361982 | 326 | if ((bfd_section_flags (sect) & SEC_ALLOC)) |
1fddbabb PA |
327 | num_alloc_sections++; |
328 | ||
d17f7b36 | 329 | if (num_alloc_sections != li->section_bases.size ()) |
1fddbabb PA |
330 | warning (_("\ |
331 | Could not relocate shared library \"%s\": wrong number of ALLOC sections"), | |
cfa9d6d9 | 332 | so->so_name); |
1fddbabb | 333 | else |
cfa9d6d9 | 334 | { |
1fddbabb PA |
335 | int bases_index = 0; |
336 | int found_range = 0; | |
1fddbabb PA |
337 | |
338 | so->addr_low = ~(CORE_ADDR) 0; | |
339 | so->addr_high = 0; | |
340 | for (i = 0, sect = so->abfd->sections; | |
341 | sect != NULL; | |
342 | i++, sect = sect->next) | |
343 | { | |
fd361982 | 344 | if (!(bfd_section_flags (sect) & SEC_ALLOC)) |
1fddbabb | 345 | continue; |
fd361982 | 346 | if (bfd_section_size (sect) > 0) |
1fddbabb PA |
347 | { |
348 | CORE_ADDR low, high; | |
433759f7 | 349 | |
f2f46dfc | 350 | low = li->section_bases[i]; |
fd361982 | 351 | high = low + bfd_section_size (sect) - 1; |
1fddbabb PA |
352 | |
353 | if (low < so->addr_low) | |
354 | so->addr_low = low; | |
355 | if (high > so->addr_high) | |
356 | so->addr_high = high; | |
357 | gdb_assert (so->addr_low <= so->addr_high); | |
358 | found_range = 1; | |
359 | } | |
f2f46dfc | 360 | li->offsets->offsets[i] = li->section_bases[bases_index]; |
1fddbabb PA |
361 | bases_index++; |
362 | } | |
363 | if (!found_range) | |
364 | so->addr_low = so->addr_high = 0; | |
365 | gdb_assert (so->addr_low <= so->addr_high); | |
366 | } | |
367 | } | |
f2f46dfc | 368 | else if (!li->segment_bases.empty ()) |
1fddbabb PA |
369 | { |
370 | struct symfile_segment_data *data; | |
433759f7 | 371 | |
1fddbabb PA |
372 | data = get_symfile_segment_data (so->abfd); |
373 | if (data == NULL) | |
374 | warning (_("\ | |
375 | Could not relocate shared library \"%s\": no segments"), so->so_name); | |
376 | else | |
377 | { | |
378 | ULONGEST orig_delta; | |
379 | int i; | |
1fddbabb | 380 | |
d0e449a1 | 381 | if (!symfile_map_offsets_to_segments (so->abfd, data, li->offsets, |
d17f7b36 SM |
382 | li->segment_bases.size (), |
383 | li->segment_bases.data ())) | |
1fddbabb PA |
384 | warning (_("\ |
385 | Could not relocate shared library \"%s\": bad offsets"), so->so_name); | |
386 | ||
387 | /* Find the range of addresses to report for this library in | |
388 | "info sharedlibrary". Report any consecutive segments | |
389 | which were relocated as a single unit. */ | |
d17f7b36 SM |
390 | gdb_assert (li->segment_bases.size () > 0); |
391 | orig_delta = li->segment_bases[0] - data->segment_bases[0]; | |
1fddbabb PA |
392 | |
393 | for (i = 1; i < data->num_segments; i++) | |
394 | { | |
395 | /* If we have run out of offsets, assume all | |
396 | remaining segments have the same offset. */ | |
d17f7b36 | 397 | if (i >= li->segment_bases.size ()) |
1fddbabb PA |
398 | continue; |
399 | ||
400 | /* If this segment does not have the same offset, do | |
401 | not include it in the library's range. */ | |
d17f7b36 SM |
402 | if (li->segment_bases[i] - data->segment_bases[i] |
403 | != orig_delta) | |
1fddbabb PA |
404 | break; |
405 | } | |
406 | ||
d17f7b36 | 407 | so->addr_low = li->segment_bases[0]; |
1fddbabb PA |
408 | so->addr_high = (data->segment_bases[i - 1] |
409 | + data->segment_sizes[i - 1] | |
410 | + orig_delta); | |
411 | gdb_assert (so->addr_low <= so->addr_high); | |
412 | ||
413 | free_symfile_segment_data (data); | |
cfa9d6d9 | 414 | } |
cfa9d6d9 DJ |
415 | } |
416 | } | |
417 | ||
d0e449a1 SM |
418 | offset = li->offsets->offsets[gdb_bfd_section_index |
419 | (sec->the_bfd_section->owner, | |
420 | sec->the_bfd_section)]; | |
cfa9d6d9 DJ |
421 | sec->addr += offset; |
422 | sec->endaddr += offset; | |
423 | } | |
424 | ||
425 | static int | |
bf469271 | 426 | solib_target_open_symbol_file_object (int from_tty) |
cfa9d6d9 DJ |
427 | { |
428 | /* We can't locate the main symbol file based on the target's | |
429 | knowledge; the user has to specify it. */ | |
430 | return 0; | |
431 | } | |
432 | ||
433 | static int | |
434 | solib_target_in_dynsym_resolve_code (CORE_ADDR pc) | |
435 | { | |
436 | /* We don't have a range of addresses for the dynamic linker; there | |
437 | may not be one in the program's address space. So only report | |
438 | PLT entries (which may be import stubs). */ | |
3e5d3a5a | 439 | return in_plt_section (pc); |
cfa9d6d9 DJ |
440 | } |
441 | ||
8d465389 | 442 | struct target_so_ops solib_target_so_ops; |
cfa9d6d9 | 443 | |
cfa9d6d9 DJ |
444 | void |
445 | _initialize_solib_target (void) | |
446 | { | |
447 | solib_target_so_ops.relocate_section_addresses | |
448 | = solib_target_relocate_section_addresses; | |
449 | solib_target_so_ops.free_so = solib_target_free_so; | |
450 | solib_target_so_ops.clear_solib = solib_target_clear_solib; | |
451 | solib_target_so_ops.solib_create_inferior_hook | |
452 | = solib_target_solib_create_inferior_hook; | |
cfa9d6d9 DJ |
453 | solib_target_so_ops.current_sos = solib_target_current_sos; |
454 | solib_target_so_ops.open_symbol_file_object | |
455 | = solib_target_open_symbol_file_object; | |
456 | solib_target_so_ops.in_dynsym_resolve_code | |
457 | = solib_target_in_dynsym_resolve_code; | |
831a0c44 | 458 | solib_target_so_ops.bfd_open = solib_bfd_open; |
98d64339 PA |
459 | |
460 | /* Set current_target_so_ops to solib_target_so_ops if not already | |
461 | set. */ | |
462 | if (current_target_so_ops == 0) | |
463 | current_target_so_ops = &solib_target_so_ops; | |
cfa9d6d9 | 464 | } |