Update copyright year in gdbarch.sh doc/gdb.texinfo and doc/refcard.tex
[binutils-gdb.git] / gdb / solib-target.c
blob113211a92bceb2accf5918b1b9bad1afabf6094a
1 /* Definitions for targets which report shared library events.
3 Copyright (C) 2007-2020 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/>. */
20 #include "defs.h"
21 #include "objfiles.h"
22 #include "solist.h"
23 #include "symtab.h"
24 #include "symfile.h"
25 #include "target.h"
26 #include "solib-target.h"
27 #include <vector>
29 /* Private data for each loaded library. */
30 struct lm_info_target : public lm_info_base
32 /* The library's name. The name is normally kept in the struct
33 so_list; it is only here during XML parsing. */
34 std::string name;
36 /* The target can either specify segment bases or section bases, not
37 both. */
39 /* The base addresses for each independently relocatable segment of
40 this shared library. */
41 std::vector<CORE_ADDR> segment_bases;
43 /* The base addresses for each independently allocatable,
44 relocatable section of this shared library. */
45 std::vector<CORE_ADDR> section_bases;
47 /* The cached offsets for each section of this shared library,
48 determined from SEGMENT_BASES, or SECTION_BASES. */
49 section_offsets *offsets = NULL;
52 typedef std::vector<std::unique_ptr<lm_info_target>> lm_info_vector;
54 #if !defined(HAVE_LIBEXPAT)
56 static lm_info_vector
57 solib_target_parse_libraries (const char *library)
59 static int have_warned;
61 if (!have_warned)
63 have_warned = 1;
64 warning (_("Can not parse XML library list; XML support was disabled "
65 "at compile time"));
68 return lm_info_vector ();
71 #else /* HAVE_LIBEXPAT */
73 #include "xml-support.h"
75 /* Handle the start of a <segment> element. */
77 static void
78 library_list_start_segment (struct gdb_xml_parser *parser,
79 const struct gdb_xml_element *element,
80 void *user_data,
81 std::vector<gdb_xml_value> &attributes)
83 lm_info_vector *list = (lm_info_vector *) user_data;
84 lm_info_target *last = list->back ().get ();
85 ULONGEST *address_p
86 = (ULONGEST *) xml_find_attribute (attributes, "address")->value.get ();
87 CORE_ADDR address = (CORE_ADDR) *address_p;
89 if (!last->section_bases.empty ())
90 gdb_xml_error (parser,
91 _("Library list with both segments and sections"));
93 last->segment_bases.push_back (address);
96 static void
97 library_list_start_section (struct gdb_xml_parser *parser,
98 const struct gdb_xml_element *element,
99 void *user_data,
100 std::vector<gdb_xml_value> &attributes)
102 lm_info_vector *list = (lm_info_vector *) user_data;
103 lm_info_target *last = list->back ().get ();
104 ULONGEST *address_p
105 = (ULONGEST *) xml_find_attribute (attributes, "address")->value.get ();
106 CORE_ADDR address = (CORE_ADDR) *address_p;
108 if (!last->segment_bases.empty ())
109 gdb_xml_error (parser,
110 _("Library list with both segments and sections"));
112 last->section_bases.push_back (address);
115 /* Handle the start of a <library> element. */
117 static void
118 library_list_start_library (struct gdb_xml_parser *parser,
119 const struct gdb_xml_element *element,
120 void *user_data,
121 std::vector<gdb_xml_value> &attributes)
123 lm_info_vector *list = (lm_info_vector *) user_data;
124 lm_info_target *item = new lm_info_target;
125 item->name
126 = (const char *) xml_find_attribute (attributes, "name")->value.get ();
128 list->emplace_back (item);
131 static void
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 lm_info_vector *list = (lm_info_vector *) user_data;
137 lm_info_target *lm_info = list->back ().get ();
139 if (lm_info->segment_bases.empty () && lm_info->section_bases.empty ())
140 gdb_xml_error (parser, _("No segment or section bases defined"));
144 /* Handle the start of a <library-list> element. */
146 static void
147 library_list_start_list (struct gdb_xml_parser *parser,
148 const struct gdb_xml_element *element,
149 void *user_data,
150 std::vector<gdb_xml_value> &attributes)
152 struct gdb_xml_value *version = xml_find_attribute (attributes, "version");
154 /* #FIXED attribute may be omitted, Expat returns NULL in such case. */
155 if (version != NULL)
157 const char *string = (const char *) version->value.get ();
159 if (strcmp (string, "1.0") != 0)
160 gdb_xml_error (parser,
161 _("Library list has unsupported version \"%s\""),
162 string);
166 /* The allowed elements and attributes for an XML library list.
167 The root element is a <library-list>. */
169 static const struct gdb_xml_attribute segment_attributes[] = {
170 { "address", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
171 { NULL, GDB_XML_AF_NONE, NULL, NULL }
174 static const struct gdb_xml_attribute section_attributes[] = {
175 { "address", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
176 { NULL, GDB_XML_AF_NONE, NULL, NULL }
179 static const struct gdb_xml_element library_children[] = {
180 { "segment", segment_attributes, NULL,
181 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
182 library_list_start_segment, NULL },
183 { "section", section_attributes, NULL,
184 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
185 library_list_start_section, NULL },
186 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
189 static const struct gdb_xml_attribute library_attributes[] = {
190 { "name", GDB_XML_AF_NONE, NULL, NULL },
191 { NULL, GDB_XML_AF_NONE, NULL, NULL }
194 static const struct gdb_xml_element library_list_children[] = {
195 { "library", library_attributes, library_children,
196 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
197 library_list_start_library, library_list_end_library },
198 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
201 static const struct gdb_xml_attribute library_list_attributes[] = {
202 { "version", GDB_XML_AF_OPTIONAL, NULL, NULL },
203 { NULL, GDB_XML_AF_NONE, NULL, NULL }
206 static const struct gdb_xml_element library_list_elements[] = {
207 { "library-list", library_list_attributes, library_list_children,
208 GDB_XML_EF_NONE, library_list_start_list, NULL },
209 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
212 static lm_info_vector
213 solib_target_parse_libraries (const char *library)
215 lm_info_vector result;
217 if (gdb_xml_parse_quick (_("target library list"), "library-list.dtd",
218 library_list_elements, library, &result) == 0)
220 /* Parsed successfully. */
221 return result;
224 result.clear ();
225 return result;
227 #endif
229 static struct so_list *
230 solib_target_current_sos (void)
232 struct so_list *new_solib, *start = NULL, *last = NULL;
234 /* Fetch the list of shared libraries. */
235 gdb::optional<gdb::char_vector> library_document
236 = target_read_stralloc (current_top_target (), TARGET_OBJECT_LIBRARIES,
237 NULL);
238 if (!library_document)
239 return NULL;
241 /* Parse the list. */
242 lm_info_vector library_list
243 = solib_target_parse_libraries (library_document->data ());
245 if (library_list.empty ())
246 return NULL;
248 /* Build a struct so_list for each entry on the list. */
249 for (auto &&info : library_list)
251 new_solib = XCNEW (struct so_list);
252 strncpy (new_solib->so_name, info->name.c_str (),
253 SO_NAME_MAX_PATH_SIZE - 1);
254 new_solib->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
255 strncpy (new_solib->so_original_name, info->name.c_str (),
256 SO_NAME_MAX_PATH_SIZE - 1);
257 new_solib->so_original_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
259 /* We no longer need this copy of the name. */
260 info->name.clear ();
262 new_solib->lm_info = info.release ();
264 /* Add it to the list. */
265 if (!start)
266 last = start = new_solib;
267 else
269 last->next = new_solib;
270 last = new_solib;
274 return start;
277 static void
278 solib_target_solib_create_inferior_hook (int from_tty)
280 /* Nothing needed. */
283 static void
284 solib_target_clear_solib (void)
286 /* Nothing needed. */
289 static void
290 solib_target_free_so (struct so_list *so)
292 lm_info_target *li = (lm_info_target *) so->lm_info;
294 gdb_assert (li->name.empty ());
296 delete li;
299 static void
300 solib_target_relocate_section_addresses (struct so_list *so,
301 struct target_section *sec)
303 CORE_ADDR offset;
304 lm_info_target *li = (lm_info_target *) so->lm_info;
306 /* Build the offset table only once per object file. We can not do
307 it any earlier, since we need to open the file first. */
308 if (li->offsets == NULL)
310 int num_sections = gdb_bfd_count_sections (so->abfd);
312 li->offsets
313 = ((struct section_offsets *)
314 xzalloc (SIZEOF_N_SECTION_OFFSETS (num_sections)));
316 if (!li->section_bases.empty ())
318 int i;
319 asection *sect;
320 int num_alloc_sections = 0;
322 for (i = 0, sect = so->abfd->sections;
323 sect != NULL;
324 i++, sect = sect->next)
325 if ((bfd_section_flags (sect) & SEC_ALLOC))
326 num_alloc_sections++;
328 if (num_alloc_sections != li->section_bases.size ())
329 warning (_("\
330 Could not relocate shared library \"%s\": wrong number of ALLOC sections"),
331 so->so_name);
332 else
334 int bases_index = 0;
335 int found_range = 0;
337 so->addr_low = ~(CORE_ADDR) 0;
338 so->addr_high = 0;
339 for (i = 0, sect = so->abfd->sections;
340 sect != NULL;
341 i++, sect = sect->next)
343 if (!(bfd_section_flags (sect) & SEC_ALLOC))
344 continue;
345 if (bfd_section_size (sect) > 0)
347 CORE_ADDR low, high;
349 low = li->section_bases[i];
350 high = low + bfd_section_size (sect) - 1;
352 if (low < so->addr_low)
353 so->addr_low = low;
354 if (high > so->addr_high)
355 so->addr_high = high;
356 gdb_assert (so->addr_low <= so->addr_high);
357 found_range = 1;
359 li->offsets->offsets[i] = li->section_bases[bases_index];
360 bases_index++;
362 if (!found_range)
363 so->addr_low = so->addr_high = 0;
364 gdb_assert (so->addr_low <= so->addr_high);
367 else if (!li->segment_bases.empty ())
369 struct symfile_segment_data *data;
371 data = get_symfile_segment_data (so->abfd);
372 if (data == NULL)
373 warning (_("\
374 Could not relocate shared library \"%s\": no segments"), so->so_name);
375 else
377 ULONGEST orig_delta;
378 int i;
380 if (!symfile_map_offsets_to_segments (so->abfd, data, li->offsets,
381 li->segment_bases.size (),
382 li->segment_bases.data ()))
383 warning (_("\
384 Could not relocate shared library \"%s\": bad offsets"), so->so_name);
386 /* Find the range of addresses to report for this library in
387 "info sharedlibrary". Report any consecutive segments
388 which were relocated as a single unit. */
389 gdb_assert (li->segment_bases.size () > 0);
390 orig_delta = li->segment_bases[0] - data->segment_bases[0];
392 for (i = 1; i < data->num_segments; i++)
394 /* If we have run out of offsets, assume all
395 remaining segments have the same offset. */
396 if (i >= li->segment_bases.size ())
397 continue;
399 /* If this segment does not have the same offset, do
400 not include it in the library's range. */
401 if (li->segment_bases[i] - data->segment_bases[i]
402 != orig_delta)
403 break;
406 so->addr_low = li->segment_bases[0];
407 so->addr_high = (data->segment_bases[i - 1]
408 + data->segment_sizes[i - 1]
409 + orig_delta);
410 gdb_assert (so->addr_low <= so->addr_high);
412 free_symfile_segment_data (data);
417 offset = li->offsets->offsets[gdb_bfd_section_index
418 (sec->the_bfd_section->owner,
419 sec->the_bfd_section)];
420 sec->addr += offset;
421 sec->endaddr += offset;
424 static int
425 solib_target_open_symbol_file_object (int from_tty)
427 /* We can't locate the main symbol file based on the target's
428 knowledge; the user has to specify it. */
429 return 0;
432 static int
433 solib_target_in_dynsym_resolve_code (CORE_ADDR pc)
435 /* We don't have a range of addresses for the dynamic linker; there
436 may not be one in the program's address space. So only report
437 PLT entries (which may be import stubs). */
438 return in_plt_section (pc);
441 struct target_so_ops solib_target_so_ops;
443 void
444 _initialize_solib_target (void)
446 solib_target_so_ops.relocate_section_addresses
447 = solib_target_relocate_section_addresses;
448 solib_target_so_ops.free_so = solib_target_free_so;
449 solib_target_so_ops.clear_solib = solib_target_clear_solib;
450 solib_target_so_ops.solib_create_inferior_hook
451 = solib_target_solib_create_inferior_hook;
452 solib_target_so_ops.current_sos = solib_target_current_sos;
453 solib_target_so_ops.open_symbol_file_object
454 = solib_target_open_symbol_file_object;
455 solib_target_so_ops.in_dynsym_resolve_code
456 = solib_target_in_dynsym_resolve_code;
457 solib_target_so_ops.bfd_open = solib_bfd_open;
459 /* Set current_target_so_ops to solib_target_so_ops if not already
460 set. */
461 if (current_target_so_ops == 0)
462 current_target_so_ops = &solib_target_so_ops;