1 /* Definitions for targets which report shared library events.
3 Copyright (C) 2007-2023 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/>. */
26 #include "solib-target.h"
30 /* Private data for each loaded library. */
31 struct lm_info_target final
: public lm_info
33 /* The library's name. The name is normally kept in the struct
34 so_list; it is only here during XML parsing. */
37 /* The target can either specify segment bases or section bases, not
40 /* The base addresses for each independently relocatable segment of
41 this shared library. */
42 std::vector
<CORE_ADDR
> segment_bases
;
44 /* The base addresses for each independently allocatable,
45 relocatable section of this shared library. */
46 std::vector
<CORE_ADDR
> section_bases
;
48 /* The cached offsets for each section of this shared library,
49 determined from SEGMENT_BASES, or SECTION_BASES. */
50 section_offsets offsets
;
53 using lm_info_target_up
= std::unique_ptr
<lm_info_target
>;
55 #if !defined(HAVE_LIBEXPAT)
57 static std::vector
<lm_info_target_up
>
58 solib_target_parse_libraries (const char *library
)
60 static int have_warned
;
65 warning (_("Can not parse XML library list; XML support was disabled "
72 #else /* HAVE_LIBEXPAT */
74 #include "xml-support.h"
76 /* Handle the start of a <segment> element. */
79 library_list_start_segment (struct gdb_xml_parser
*parser
,
80 const struct gdb_xml_element
*element
,
82 std::vector
<gdb_xml_value
> &attributes
)
84 auto *list
= (std::vector
<lm_info_target_up
> *) user_data
;
85 lm_info_target
*last
= list
->back ().get ();
87 = (ULONGEST
*) xml_find_attribute (attributes
, "address")->value
.get ();
88 CORE_ADDR address
= (CORE_ADDR
) *address_p
;
90 if (!last
->section_bases
.empty ())
91 gdb_xml_error (parser
,
92 _("Library list with both segments and sections"));
94 last
->segment_bases
.push_back (address
);
98 library_list_start_section (struct gdb_xml_parser
*parser
,
99 const struct gdb_xml_element
*element
,
101 std::vector
<gdb_xml_value
> &attributes
)
103 auto *list
= (std::vector
<lm_info_target_up
> *) user_data
;
104 lm_info_target
*last
= list
->back ().get ();
106 = (ULONGEST
*) xml_find_attribute (attributes
, "address")->value
.get ();
107 CORE_ADDR address
= (CORE_ADDR
) *address_p
;
109 if (!last
->segment_bases
.empty ())
110 gdb_xml_error (parser
,
111 _("Library list with both segments and sections"));
113 last
->section_bases
.push_back (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
,
122 std::vector
<gdb_xml_value
> &attributes
)
124 auto *list
= (std::vector
<lm_info_target_up
> *) user_data
;
125 lm_info_target
*item
= new lm_info_target
;
127 = (const char *) xml_find_attribute (attributes
, "name")->value
.get ();
129 list
->emplace_back (item
);
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
)
137 auto *list
= (std::vector
<lm_info_target_up
> *) user_data
;
138 lm_info_target
*lm_info
= list
->back ().get ();
140 if (lm_info
->segment_bases
.empty () && lm_info
->section_bases
.empty ())
141 gdb_xml_error (parser
, _("No segment or section bases defined"));
145 /* Handle the start of a <library-list> element. */
148 library_list_start_list (struct gdb_xml_parser
*parser
,
149 const struct gdb_xml_element
*element
,
151 std::vector
<gdb_xml_value
> &attributes
)
153 struct gdb_xml_value
*version
= xml_find_attribute (attributes
, "version");
155 /* #FIXED attribute may be omitted, Expat returns NULL in such case. */
158 const char *string
= (const char *) version
->value
.get ();
160 if (strcmp (string
, "1.0") != 0)
161 gdb_xml_error (parser
,
162 _("Library list has unsupported version \"%s\""),
167 /* The allowed elements and attributes for an XML library list.
168 The root element is a <library-list>. */
170 static const struct gdb_xml_attribute segment_attributes
[] = {
171 { "address", GDB_XML_AF_NONE
, gdb_xml_parse_attr_ulongest
, NULL
},
172 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
175 static const struct gdb_xml_attribute section_attributes
[] = {
176 { "address", GDB_XML_AF_NONE
, gdb_xml_parse_attr_ulongest
, NULL
},
177 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
180 static const struct gdb_xml_element library_children
[] = {
181 { "segment", segment_attributes
, NULL
,
182 GDB_XML_EF_REPEATABLE
| GDB_XML_EF_OPTIONAL
,
183 library_list_start_segment
, NULL
},
184 { "section", section_attributes
, NULL
,
185 GDB_XML_EF_REPEATABLE
| GDB_XML_EF_OPTIONAL
,
186 library_list_start_section
, NULL
},
187 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
190 static const struct gdb_xml_attribute library_attributes
[] = {
191 { "name", GDB_XML_AF_NONE
, NULL
, NULL
},
192 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
195 static const struct gdb_xml_element library_list_children
[] = {
196 { "library", library_attributes
, library_children
,
197 GDB_XML_EF_REPEATABLE
| GDB_XML_EF_OPTIONAL
,
198 library_list_start_library
, library_list_end_library
},
199 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
202 static const struct gdb_xml_attribute library_list_attributes
[] = {
203 { "version", GDB_XML_AF_OPTIONAL
, NULL
, NULL
},
204 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
207 static const struct gdb_xml_element library_list_elements
[] = {
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
}
213 static std::vector
<lm_info_target_up
>
214 solib_target_parse_libraries (const char *library
)
216 std::vector
<lm_info_target_up
> result
;
218 if (gdb_xml_parse_quick (_("target library list"), "library-list.dtd",
219 library_list_elements
, library
, &result
) == 0)
221 /* Parsed successfully. */
230 static intrusive_list
<shobj
>
231 solib_target_current_sos (void)
233 intrusive_list
<shobj
> sos
;
235 /* Fetch the list of shared libraries. */
236 gdb::optional
<gdb::char_vector
> library_document
237 = target_read_stralloc (current_inferior ()->top_target (),
238 TARGET_OBJECT_LIBRARIES
, NULL
);
239 if (!library_document
)
242 /* Parse the list. */
243 std::vector
<lm_info_target_up
> library_list
244 = solib_target_parse_libraries (library_document
->data ());
246 /* Build a struct shobj for each entry on the list. */
247 for (lm_info_target_up
&info
: library_list
)
249 shobj
*new_solib
= new shobj
;
251 /* We don't need a copy of the name in INFO anymore. */
252 new_solib
->so_name
= std::move (info
->name
);
253 new_solib
->so_original_name
= new_solib
->so_name
;
254 new_solib
->lm_info
= std::move (info
);
256 /* Add it to the list. */
257 sos
.push_back (*new_solib
);
264 solib_target_solib_create_inferior_hook (int from_tty
)
266 /* Nothing needed. */
270 solib_target_relocate_section_addresses (shobj
&so
, target_section
*sec
)
273 auto *li
= gdb::checked_static_cast
<lm_info_target
*> (so
.lm_info
.get ());
275 /* Build the offset table only once per object file. We can not do
276 it any earlier, since we need to open the file first. */
277 if (li
->offsets
.empty ())
279 int num_sections
= gdb_bfd_count_sections (so
.abfd
.get ());
281 li
->offsets
.assign (num_sections
, 0);
283 if (!li
->section_bases
.empty ())
287 int num_alloc_sections
= 0;
289 for (i
= 0, sect
= so
.abfd
->sections
;
291 i
++, sect
= sect
->next
)
292 if ((bfd_section_flags (sect
) & SEC_ALLOC
))
293 num_alloc_sections
++;
295 if (num_alloc_sections
!= li
->section_bases
.size ())
297 Could not relocate shared library \"%s\": wrong number of ALLOC sections"),
298 so
.so_name
.c_str ());
304 so
.addr_low
= ~(CORE_ADDR
) 0;
306 for (i
= 0, sect
= so
.abfd
->sections
;
308 i
++, sect
= sect
->next
)
310 if (!(bfd_section_flags (sect
) & SEC_ALLOC
))
312 if (bfd_section_size (sect
) > 0)
316 low
= li
->section_bases
[i
];
317 high
= low
+ bfd_section_size (sect
) - 1;
319 if (low
< so
.addr_low
)
321 if (high
> so
.addr_high
)
323 gdb_assert (so
.addr_low
<= so
.addr_high
);
326 li
->offsets
[i
] = li
->section_bases
[bases_index
];
330 so
.addr_low
= so
.addr_high
= 0;
331 gdb_assert (so
.addr_low
<= so
.addr_high
);
334 else if (!li
->segment_bases
.empty ())
336 symfile_segment_data_up data
337 = get_symfile_segment_data (so
.abfd
.get ());
341 Could not relocate shared library \"%s\": no segments"), so
.so_name
.c_str ());
347 if (!symfile_map_offsets_to_segments (so
.abfd
.get (), data
.get (),
349 li
->segment_bases
.size (),
350 li
->segment_bases
.data ()))
352 Could not relocate shared library \"%s\": bad offsets"), so
.so_name
.c_str ());
354 /* Find the range of addresses to report for this library in
355 "info sharedlibrary". Report any consecutive segments
356 which were relocated as a single unit. */
357 gdb_assert (li
->segment_bases
.size () > 0);
358 orig_delta
= li
->segment_bases
[0] - data
->segments
[0].base
;
360 for (i
= 1; i
< data
->segments
.size (); i
++)
362 /* If we have run out of offsets, assume all
363 remaining segments have the same offset. */
364 if (i
>= li
->segment_bases
.size ())
367 /* If this segment does not have the same offset, do
368 not include it in the library's range. */
369 if (li
->segment_bases
[i
] - data
->segments
[i
].base
374 so
.addr_low
= li
->segment_bases
[0];
375 so
.addr_high
= (data
->segments
[i
- 1].base
376 + data
->segments
[i
- 1].size
378 gdb_assert (so
.addr_low
<= so
.addr_high
);
383 offset
= li
->offsets
[gdb_bfd_section_index (sec
->the_bfd_section
->owner
,
384 sec
->the_bfd_section
)];
386 sec
->endaddr
+= offset
;
390 solib_target_open_symbol_file_object (int from_tty
)
392 /* We can't locate the main symbol file based on the target's
393 knowledge; the user has to specify it. */
398 solib_target_in_dynsym_resolve_code (CORE_ADDR pc
)
400 /* We don't have a range of addresses for the dynamic linker; there
401 may not be one in the program's address space. So only report
402 PLT entries (which may be import stubs). */
403 return in_plt_section (pc
);
406 const struct target_so_ops solib_target_so_ops
=
408 solib_target_relocate_section_addresses
,
411 solib_target_solib_create_inferior_hook
,
412 solib_target_current_sos
,
413 solib_target_open_symbol_file_object
,
414 solib_target_in_dynsym_resolve_code
,