]>
Commit | Line | Data |
---|---|---|
ab31aa69 | 1 | /* Handle SVR4 shared libraries for GDB, the GNU Debugger. |
2f4950cd | 2 | |
1d506c26 | 3 | Copyright (C) 1990-2024 Free Software Foundation, Inc. |
13437d4b KB |
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 |
13437d4b KB |
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/>. */ |
13437d4b | 19 | |
13437d4b | 20 | |
13437d4b | 21 | #include "elf/external.h" |
21479ded | 22 | #include "elf/common.h" |
f7856c8f | 23 | #include "elf/mips.h" |
13437d4b | 24 | |
ec452525 | 25 | #include "extract-store-integer.h" |
13437d4b KB |
26 | #include "symtab.h" |
27 | #include "bfd.h" | |
28 | #include "symfile.h" | |
29 | #include "objfiles.h" | |
30 | #include "gdbcore.h" | |
13437d4b | 31 | #include "target.h" |
13437d4b | 32 | #include "inferior.h" |
45741a9c | 33 | #include "infrun.h" |
fb14de7b | 34 | #include "regcache.h" |
76727919 | 35 | #include "observable.h" |
13437d4b KB |
36 | |
37 | #include "solist.h" | |
bba93f6c | 38 | #include "solib.h" |
13437d4b KB |
39 | #include "solib-svr4.h" |
40 | ||
2f4950cd | 41 | #include "bfd-target.h" |
cc10cae3 | 42 | #include "elf-bfd.h" |
2f4950cd | 43 | #include "exec.h" |
8d4e36ba | 44 | #include "auxv.h" |
695c3173 | 45 | #include "gdb_bfd.h" |
f9e14852 | 46 | #include "probe.h" |
2f4950cd | 47 | |
8d56636a MM |
48 | #include <map> |
49 | ||
e5e2b9ff | 50 | static struct link_map_offsets *svr4_fetch_link_map_offsets (void); |
d5a921c9 | 51 | static int svr4_have_link_map_offsets (void); |
9f2982ff | 52 | static void svr4_relocate_main_executable (void); |
7905fc35 | 53 | static void probes_table_remove_objfile_probes (struct objfile *objfile); |
6e9cd73e SM |
54 | static void svr4_iterate_over_objfiles_in_search_order |
55 | (gdbarch *gdbarch, iterate_over_objfiles_in_search_order_cb_ftype cb, | |
56 | objfile *current_objfile); | |
626ca2c0 | 57 | |
1c4dcb57 | 58 | |
13437d4b KB |
59 | /* On SVR4 systems, a list of symbols in the dynamic linker where |
60 | GDB can try to place a breakpoint to monitor shared library | |
61 | events. | |
62 | ||
63 | If none of these symbols are found, or other errors occur, then | |
64 | SVR4 systems will fall back to using a symbol as the "startup | |
65 | mapping complete" breakpoint address. */ | |
66 | ||
bc043ef3 | 67 | static const char * const solib_break_names[] = |
13437d4b KB |
68 | { |
69 | "r_debug_state", | |
70 | "_r_debug_state", | |
71 | "_dl_debug_state", | |
72 | "rtld_db_dlactivity", | |
4c7dcb84 | 73 | "__dl_rtld_db_dlactivity", |
1f72e589 | 74 | "_rtld_debug_state", |
4c0122c8 | 75 | |
13437d4b KB |
76 | NULL |
77 | }; | |
13437d4b | 78 | |
bc043ef3 | 79 | static const char * const bkpt_names[] = |
13437d4b | 80 | { |
13437d4b | 81 | "_start", |
ad3dcc5c | 82 | "__start", |
13437d4b KB |
83 | "main", |
84 | NULL | |
85 | }; | |
13437d4b | 86 | |
bc043ef3 | 87 | static const char * const main_name_list[] = |
13437d4b KB |
88 | { |
89 | "main_$main", | |
90 | NULL | |
91 | }; | |
92 | ||
f9e14852 GB |
93 | /* What to do when a probe stop occurs. */ |
94 | ||
95 | enum probe_action | |
96 | { | |
97 | /* Something went seriously wrong. Stop using probes and | |
98 | revert to using the older interface. */ | |
99 | PROBES_INTERFACE_FAILED, | |
100 | ||
101 | /* No action is required. The shared object list is still | |
102 | valid. */ | |
103 | DO_NOTHING, | |
104 | ||
105 | /* The shared object list should be reloaded entirely. */ | |
106 | FULL_RELOAD, | |
107 | ||
108 | /* Attempt to incrementally update the shared object list. If | |
109 | the update fails or is not possible, fall back to reloading | |
110 | the list in full. */ | |
111 | UPDATE_OR_RELOAD, | |
112 | }; | |
113 | ||
114 | /* A probe's name and its associated action. */ | |
115 | ||
116 | struct probe_info | |
117 | { | |
118 | /* The name of the probe. */ | |
119 | const char *name; | |
120 | ||
121 | /* What to do when a probe stop occurs. */ | |
122 | enum probe_action action; | |
123 | }; | |
124 | ||
125 | /* A list of named probes and their associated actions. If all | |
126 | probes are present in the dynamic linker then the probes-based | |
127 | interface will be used. */ | |
128 | ||
129 | static const struct probe_info probe_info[] = | |
130 | { | |
131 | { "init_start", DO_NOTHING }, | |
132 | { "init_complete", FULL_RELOAD }, | |
133 | { "map_start", DO_NOTHING }, | |
134 | { "map_failed", DO_NOTHING }, | |
135 | { "reloc_complete", UPDATE_OR_RELOAD }, | |
136 | { "unmap_start", DO_NOTHING }, | |
137 | { "unmap_complete", FULL_RELOAD }, | |
138 | }; | |
139 | ||
140 | #define NUM_PROBES ARRAY_SIZE (probe_info) | |
141 | ||
4d7b2d5b JB |
142 | /* Return non-zero if GDB_SO_NAME and INFERIOR_SO_NAME represent |
143 | the same shared library. */ | |
144 | ||
145 | static int | |
146 | svr4_same_1 (const char *gdb_so_name, const char *inferior_so_name) | |
147 | { | |
148 | if (strcmp (gdb_so_name, inferior_so_name) == 0) | |
149 | return 1; | |
150 | ||
151 | /* On Solaris, when starting inferior we think that dynamic linker is | |
d989b283 PP |
152 | /usr/lib/ld.so.1, but later on, the table of loaded shared libraries |
153 | contains /lib/ld.so.1. Sometimes one file is a link to another, but | |
4d7b2d5b JB |
154 | sometimes they have identical content, but are not linked to each |
155 | other. We don't restrict this check for Solaris, but the chances | |
156 | of running into this situation elsewhere are very low. */ | |
157 | if (strcmp (gdb_so_name, "/usr/lib/ld.so.1") == 0 | |
158 | && strcmp (inferior_so_name, "/lib/ld.so.1") == 0) | |
159 | return 1; | |
160 | ||
7307a73a | 161 | /* Similarly, we observed the same issue with amd64 and sparcv9, but with |
4d7b2d5b | 162 | different locations. */ |
7307a73a RO |
163 | if (strcmp (gdb_so_name, "/usr/lib/amd64/ld.so.1") == 0 |
164 | && strcmp (inferior_so_name, "/lib/amd64/ld.so.1") == 0) | |
165 | return 1; | |
166 | ||
4d7b2d5b JB |
167 | if (strcmp (gdb_so_name, "/usr/lib/sparcv9/ld.so.1") == 0 |
168 | && strcmp (inferior_so_name, "/lib/sparcv9/ld.so.1") == 0) | |
169 | return 1; | |
170 | ||
171 | return 0; | |
172 | } | |
173 | ||
96bb3873 SM |
174 | static bool |
175 | svr4_same (const char *gdb_name, const char *inferior_name, | |
176 | const lm_info_svr4 &gdb_lm_info, | |
177 | const lm_info_svr4 &inferior_lm_info) | |
4d7b2d5b | 178 | { |
96bb3873 | 179 | if (!svr4_same_1 (gdb_name, inferior_name)) |
8d56636a MM |
180 | return false; |
181 | ||
182 | /* There may be different instances of the same library, in different | |
183 | namespaces. Each instance, however, must have been loaded at a | |
184 | different address so its relocation offset would be different. */ | |
96bb3873 SM |
185 | return gdb_lm_info.l_addr_inferior == inferior_lm_info.l_addr_inferior; |
186 | } | |
187 | ||
188 | static int | |
7b323785 | 189 | svr4_same (const solib &gdb, const solib &inferior) |
96bb3873 | 190 | { |
7ad0a42e SM |
191 | auto *lmg |
192 | = gdb::checked_static_cast<const lm_info_svr4 *> (gdb.lm_info.get ()); | |
193 | auto *lmi | |
194 | = gdb::checked_static_cast<const lm_info_svr4 *> (inferior.lm_info.get ()); | |
8d56636a | 195 | |
98107b0b SM |
196 | return svr4_same (gdb.so_original_name.c_str (), |
197 | inferior.so_original_name.c_str (), *lmg, *lmi); | |
4d7b2d5b JB |
198 | } |
199 | ||
96bb3873 | 200 | static lm_info_svr4_up |
3957565a | 201 | lm_info_read (CORE_ADDR lm_addr) |
13437d4b | 202 | { |
4b188b9f | 203 | struct link_map_offsets *lmo = svr4_fetch_link_map_offsets (); |
96bb3873 | 204 | lm_info_svr4_up lm_info; |
3957565a | 205 | |
a7961323 | 206 | gdb::byte_vector lm (lmo->link_map_size); |
3957565a | 207 | |
a7961323 TT |
208 | if (target_read_memory (lm_addr, lm.data (), lmo->link_map_size) != 0) |
209 | warning (_("Error reading shared library list entry at %s"), | |
99d9c3b9 | 210 | paddress (current_inferior ()->arch (), lm_addr)); |
3957565a JK |
211 | else |
212 | { | |
99d9c3b9 SM |
213 | type *ptr_type |
214 | = builtin_type (current_inferior ()->arch ())->builtin_data_ptr; | |
13437d4b | 215 | |
6b62451a | 216 | lm_info = std::make_unique<lm_info_svr4> (); |
3957565a JK |
217 | lm_info->lm_addr = lm_addr; |
218 | ||
219 | lm_info->l_addr_inferior = extract_typed_address (&lm[lmo->l_addr_offset], | |
220 | ptr_type); | |
221 | lm_info->l_ld = extract_typed_address (&lm[lmo->l_ld_offset], ptr_type); | |
222 | lm_info->l_next = extract_typed_address (&lm[lmo->l_next_offset], | |
223 | ptr_type); | |
224 | lm_info->l_prev = extract_typed_address (&lm[lmo->l_prev_offset], | |
225 | ptr_type); | |
226 | lm_info->l_name = extract_typed_address (&lm[lmo->l_name_offset], | |
227 | ptr_type); | |
228 | } | |
229 | ||
3957565a | 230 | return lm_info; |
13437d4b KB |
231 | } |
232 | ||
cc10cae3 | 233 | static int |
b23518f0 | 234 | has_lm_dynamic_from_link_map (void) |
cc10cae3 AO |
235 | { |
236 | struct link_map_offsets *lmo = svr4_fetch_link_map_offsets (); | |
237 | ||
cfaefc65 | 238 | return lmo->l_ld_offset >= 0; |
cc10cae3 AO |
239 | } |
240 | ||
cc10cae3 | 241 | static CORE_ADDR |
7b323785 | 242 | lm_addr_check (const solib &so, bfd *abfd) |
cc10cae3 | 243 | { |
7ad0a42e | 244 | auto *li = gdb::checked_static_cast<lm_info_svr4 *> (so.lm_info.get ()); |
d0e449a1 SM |
245 | |
246 | if (!li->l_addr_p) | |
cc10cae3 AO |
247 | { |
248 | struct bfd_section *dyninfo_sect; | |
28f34a8f | 249 | CORE_ADDR l_addr, l_dynaddr, dynaddr; |
cc10cae3 | 250 | |
d0e449a1 | 251 | l_addr = li->l_addr_inferior; |
cc10cae3 | 252 | |
b23518f0 | 253 | if (! abfd || ! has_lm_dynamic_from_link_map ()) |
cc10cae3 AO |
254 | goto set_addr; |
255 | ||
d0e449a1 | 256 | l_dynaddr = li->l_ld; |
cc10cae3 AO |
257 | |
258 | dyninfo_sect = bfd_get_section_by_name (abfd, ".dynamic"); | |
259 | if (dyninfo_sect == NULL) | |
260 | goto set_addr; | |
261 | ||
fd361982 | 262 | dynaddr = bfd_section_vma (dyninfo_sect); |
cc10cae3 AO |
263 | |
264 | if (dynaddr + l_addr != l_dynaddr) | |
265 | { | |
28f34a8f | 266 | CORE_ADDR align = 0x1000; |
4e1fc9c9 | 267 | CORE_ADDR minpagesize = align; |
28f34a8f | 268 | |
cc10cae3 AO |
269 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour) |
270 | { | |
271 | Elf_Internal_Ehdr *ehdr = elf_tdata (abfd)->elf_header; | |
272 | Elf_Internal_Phdr *phdr = elf_tdata (abfd)->phdr; | |
273 | int i; | |
274 | ||
275 | align = 1; | |
276 | ||
277 | for (i = 0; i < ehdr->e_phnum; i++) | |
278 | if (phdr[i].p_type == PT_LOAD && phdr[i].p_align > align) | |
279 | align = phdr[i].p_align; | |
4e1fc9c9 JK |
280 | |
281 | minpagesize = get_elf_backend_data (abfd)->minpagesize; | |
cc10cae3 AO |
282 | } |
283 | ||
284 | /* Turn it into a mask. */ | |
285 | align--; | |
286 | ||
287 | /* If the changes match the alignment requirements, we | |
288 | assume we're using a core file that was generated by the | |
289 | same binary, just prelinked with a different base offset. | |
290 | If it doesn't match, we may have a different binary, the | |
291 | same binary with the dynamic table loaded at an unrelated | |
292 | location, or anything, really. To avoid regressions, | |
293 | don't adjust the base offset in the latter case, although | |
294 | odds are that, if things really changed, debugging won't | |
5c0d192f JK |
295 | quite work. |
296 | ||
297 | One could expect more the condition | |
298 | ((l_addr & align) == 0 && ((l_dynaddr - dynaddr) & align) == 0) | |
299 | but the one below is relaxed for PPC. The PPC kernel supports | |
300 | either 4k or 64k page sizes. To be prepared for 64k pages, | |
301 | PPC ELF files are built using an alignment requirement of 64k. | |
302 | However, when running on a kernel supporting 4k pages, the memory | |
303 | mapping of the library may not actually happen on a 64k boundary! | |
304 | ||
305 | (In the usual case where (l_addr & align) == 0, this check is | |
4e1fc9c9 JK |
306 | equivalent to the possibly expected check above.) |
307 | ||
308 | Even on PPC it must be zero-aligned at least for MINPAGESIZE. */ | |
5c0d192f | 309 | |
02835898 JK |
310 | l_addr = l_dynaddr - dynaddr; |
311 | ||
4e1fc9c9 JK |
312 | if ((l_addr & (minpagesize - 1)) == 0 |
313 | && (l_addr & align) == ((l_dynaddr - dynaddr) & align)) | |
cc10cae3 | 314 | { |
701ed6dc | 315 | if (info_verbose) |
6cb06a8c TT |
316 | gdb_printf (_("Using PIC (Position Independent Code) " |
317 | "prelink displacement %s for \"%s\".\n"), | |
99d9c3b9 | 318 | paddress (current_inferior ()->arch (), l_addr), |
98107b0b | 319 | so.so_name.c_str ()); |
cc10cae3 | 320 | } |
79d4c408 | 321 | else |
02835898 JK |
322 | { |
323 | /* There is no way to verify the library file matches. prelink | |
324 | can during prelinking of an unprelinked file (or unprelinking | |
325 | of a prelinked file) shift the DYNAMIC segment by arbitrary | |
326 | offset without any page size alignment. There is no way to | |
327 | find out the ELF header and/or Program Headers for a limited | |
328 | verification if it they match. One could do a verification | |
329 | of the DYNAMIC segment. Still the found address is the best | |
330 | one GDB could find. */ | |
331 | ||
332 | warning (_(".dynamic section for \"%s\" " | |
333 | "is not at the expected address " | |
98107b0b SM |
334 | "(wrong library or version mismatch?)"), |
335 | so.so_name.c_str ()); | |
02835898 | 336 | } |
cc10cae3 AO |
337 | } |
338 | ||
339 | set_addr: | |
d0e449a1 SM |
340 | li->l_addr = l_addr; |
341 | li->l_addr_p = 1; | |
cc10cae3 AO |
342 | } |
343 | ||
d0e449a1 | 344 | return li->l_addr; |
cc10cae3 AO |
345 | } |
346 | ||
96bb3873 SM |
347 | struct svr4_so |
348 | { | |
349 | svr4_so (const char *name, lm_info_svr4_up lm_info) | |
350 | : name (name), lm_info (std::move (lm_info)) | |
351 | {} | |
352 | ||
353 | std::string name; | |
354 | lm_info_svr4_up lm_info; | |
355 | }; | |
356 | ||
6c95b8df | 357 | /* Per pspace SVR4 specific data. */ |
13437d4b | 358 | |
1a816a87 PA |
359 | struct svr4_info |
360 | { | |
8d56636a | 361 | /* Base of dynamic linker structures in default namespace. */ |
09232438 | 362 | CORE_ADDR debug_base = 0; |
1a816a87 PA |
363 | |
364 | /* Validity flag for debug_loader_offset. */ | |
09232438 | 365 | int debug_loader_offset_p = 0; |
1a816a87 PA |
366 | |
367 | /* Load address for the dynamic linker, inferred. */ | |
09232438 | 368 | CORE_ADDR debug_loader_offset = 0; |
1a816a87 PA |
369 | |
370 | /* Name of the dynamic linker, valid if debug_loader_offset_p. */ | |
09232438 | 371 | char *debug_loader_name = nullptr; |
1a816a87 | 372 | |
8d56636a | 373 | /* Load map address for the main executable in default namespace. */ |
09232438 | 374 | CORE_ADDR main_lm_addr = 0; |
1a816a87 | 375 | |
09232438 TT |
376 | CORE_ADDR interp_text_sect_low = 0; |
377 | CORE_ADDR interp_text_sect_high = 0; | |
378 | CORE_ADDR interp_plt_sect_low = 0; | |
379 | CORE_ADDR interp_plt_sect_high = 0; | |
f9e14852 | 380 | |
8d56636a | 381 | /* True if the list of objects was last obtained from the target |
f9e14852 | 382 | via qXfer:libraries-svr4:read. */ |
8d56636a | 383 | bool using_xfer = false; |
f9e14852 GB |
384 | |
385 | /* Table of struct probe_and_action instances, used by the | |
386 | probes-based interface to map breakpoint addresses to probes | |
387 | and their associated actions. Lookup is performed using | |
935676c9 | 388 | probe_and_action->prob->address. */ |
09232438 | 389 | htab_up probes_table; |
f9e14852 | 390 | |
8d56636a MM |
391 | /* List of objects loaded into the inferior per namespace, used by the |
392 | probes-based interface. | |
393 | ||
394 | The namespace is represented by the address of its corresponding | |
33b5899f | 395 | r_debug[_ext] object. We get the namespace id as argument to the |
8d56636a MM |
396 | 'reloc_complete' probe but we don't get it when scanning the load map |
397 | on attach. | |
398 | ||
399 | The r_debug[_ext] objects may move when ld.so itself moves. In that | |
400 | case, we expect also the global _r_debug to move so we can detect | |
401 | this and reload everything. The r_debug[_ext] objects are not | |
402 | expected to move individually. | |
403 | ||
404 | The special entry zero is reserved for a linear list to support | |
405 | gdbstubs that do not support namespaces. */ | |
96bb3873 | 406 | std::map<CORE_ADDR, std::vector<svr4_so>> solib_lists; |
6c95b8df | 407 | }; |
1a816a87 | 408 | |
6c95b8df | 409 | /* Per-program-space data key. */ |
08b8a139 | 410 | static const registry<program_space>::key<svr4_info> solib_svr4_pspace_data; |
1a816a87 | 411 | |
8d56636a MM |
412 | /* Return whether DEBUG_BASE is the default namespace of INFO. */ |
413 | ||
414 | static bool | |
415 | svr4_is_default_namespace (const svr4_info *info, CORE_ADDR debug_base) | |
416 | { | |
417 | return (debug_base == info->debug_base); | |
418 | } | |
419 | ||
f9e14852 GB |
420 | /* Free the probes table. */ |
421 | ||
422 | static void | |
423 | free_probes_table (struct svr4_info *info) | |
424 | { | |
09232438 | 425 | info->probes_table.reset (nullptr); |
f9e14852 GB |
426 | } |
427 | ||
d70cc3ba SM |
428 | /* Get the svr4 data for program space PSPACE. If none is found yet, add it now. |
429 | This function always returns a valid object. */ | |
34439770 | 430 | |
6c95b8df | 431 | static struct svr4_info * |
d70cc3ba | 432 | get_svr4_info (program_space *pspace) |
1a816a87 | 433 | { |
09232438 | 434 | struct svr4_info *info = solib_svr4_pspace_data.get (pspace); |
1a816a87 | 435 | |
09232438 TT |
436 | if (info == NULL) |
437 | info = solib_svr4_pspace_data.emplace (pspace); | |
34439770 | 438 | |
6c95b8df | 439 | return info; |
1a816a87 | 440 | } |
93a57060 | 441 | |
13437d4b KB |
442 | /* Local function prototypes */ |
443 | ||
bc043ef3 | 444 | static int match_main (const char *); |
13437d4b | 445 | |
97ec2c2f | 446 | /* Read program header TYPE from inferior memory. The header is found |
17658d46 | 447 | by scanning the OS auxiliary vector. |
97ec2c2f | 448 | |
09919ac2 JK |
449 | If TYPE == -1, return the program headers instead of the contents of |
450 | one program header. | |
451 | ||
17658d46 SM |
452 | Return vector of bytes holding the program header contents, or an empty |
453 | optional on failure. If successful and P_ARCH_SIZE is non-NULL, the target | |
454 | architecture size (32-bit or 64-bit) is returned to *P_ARCH_SIZE. Likewise, | |
455 | the base address of the section is returned in *BASE_ADDR. */ | |
97ec2c2f | 456 | |
6b09f134 | 457 | static std::optional<gdb::byte_vector> |
17658d46 | 458 | read_program_header (int type, int *p_arch_size, CORE_ADDR *base_addr) |
97ec2c2f | 459 | { |
99d9c3b9 | 460 | bfd_endian byte_order = gdbarch_byte_order (current_inferior ()->arch ()); |
43136979 | 461 | CORE_ADDR at_phdr, at_phent, at_phnum, pt_phdr = 0; |
97ec2c2f UW |
462 | int arch_size, sect_size; |
463 | CORE_ADDR sect_addr; | |
43136979 | 464 | int pt_phdr_p = 0; |
97ec2c2f UW |
465 | |
466 | /* Get required auxv elements from target. */ | |
82d23ca8 | 467 | if (target_auxv_search (AT_PHDR, &at_phdr) <= 0) |
17658d46 | 468 | return {}; |
82d23ca8 | 469 | if (target_auxv_search (AT_PHENT, &at_phent) <= 0) |
17658d46 | 470 | return {}; |
82d23ca8 | 471 | if (target_auxv_search (AT_PHNUM, &at_phnum) <= 0) |
17658d46 | 472 | return {}; |
97ec2c2f | 473 | if (!at_phdr || !at_phnum) |
17658d46 | 474 | return {}; |
97ec2c2f UW |
475 | |
476 | /* Determine ELF architecture type. */ | |
477 | if (at_phent == sizeof (Elf32_External_Phdr)) | |
478 | arch_size = 32; | |
479 | else if (at_phent == sizeof (Elf64_External_Phdr)) | |
480 | arch_size = 64; | |
481 | else | |
17658d46 | 482 | return {}; |
97ec2c2f | 483 | |
09919ac2 JK |
484 | /* Find the requested segment. */ |
485 | if (type == -1) | |
486 | { | |
487 | sect_addr = at_phdr; | |
488 | sect_size = at_phent * at_phnum; | |
489 | } | |
490 | else if (arch_size == 32) | |
97ec2c2f UW |
491 | { |
492 | Elf32_External_Phdr phdr; | |
493 | int i; | |
494 | ||
495 | /* Search for requested PHDR. */ | |
496 | for (i = 0; i < at_phnum; i++) | |
497 | { | |
43136979 AR |
498 | int p_type; |
499 | ||
97ec2c2f UW |
500 | if (target_read_memory (at_phdr + i * sizeof (phdr), |
501 | (gdb_byte *)&phdr, sizeof (phdr))) | |
17658d46 | 502 | return {}; |
97ec2c2f | 503 | |
43136979 AR |
504 | p_type = extract_unsigned_integer ((gdb_byte *) phdr.p_type, |
505 | 4, byte_order); | |
506 | ||
507 | if (p_type == PT_PHDR) | |
508 | { | |
509 | pt_phdr_p = 1; | |
510 | pt_phdr = extract_unsigned_integer ((gdb_byte *) phdr.p_vaddr, | |
511 | 4, byte_order); | |
512 | } | |
513 | ||
514 | if (p_type == type) | |
97ec2c2f UW |
515 | break; |
516 | } | |
517 | ||
518 | if (i == at_phnum) | |
17658d46 | 519 | return {}; |
97ec2c2f UW |
520 | |
521 | /* Retrieve address and size. */ | |
e17a4113 UW |
522 | sect_addr = extract_unsigned_integer ((gdb_byte *)phdr.p_vaddr, |
523 | 4, byte_order); | |
524 | sect_size = extract_unsigned_integer ((gdb_byte *)phdr.p_memsz, | |
525 | 4, byte_order); | |
97ec2c2f UW |
526 | } |
527 | else | |
528 | { | |
529 | Elf64_External_Phdr phdr; | |
530 | int i; | |
531 | ||
532 | /* Search for requested PHDR. */ | |
533 | for (i = 0; i < at_phnum; i++) | |
534 | { | |
43136979 AR |
535 | int p_type; |
536 | ||
97ec2c2f UW |
537 | if (target_read_memory (at_phdr + i * sizeof (phdr), |
538 | (gdb_byte *)&phdr, sizeof (phdr))) | |
17658d46 | 539 | return {}; |
97ec2c2f | 540 | |
43136979 AR |
541 | p_type = extract_unsigned_integer ((gdb_byte *) phdr.p_type, |
542 | 4, byte_order); | |
543 | ||
544 | if (p_type == PT_PHDR) | |
545 | { | |
546 | pt_phdr_p = 1; | |
547 | pt_phdr = extract_unsigned_integer ((gdb_byte *) phdr.p_vaddr, | |
548 | 8, byte_order); | |
549 | } | |
550 | ||
551 | if (p_type == type) | |
97ec2c2f UW |
552 | break; |
553 | } | |
554 | ||
555 | if (i == at_phnum) | |
17658d46 | 556 | return {}; |
97ec2c2f UW |
557 | |
558 | /* Retrieve address and size. */ | |
e17a4113 UW |
559 | sect_addr = extract_unsigned_integer ((gdb_byte *)phdr.p_vaddr, |
560 | 8, byte_order); | |
561 | sect_size = extract_unsigned_integer ((gdb_byte *)phdr.p_memsz, | |
562 | 8, byte_order); | |
97ec2c2f UW |
563 | } |
564 | ||
43136979 AR |
565 | /* PT_PHDR is optional, but we really need it |
566 | for PIE to make this work in general. */ | |
567 | ||
568 | if (pt_phdr_p) | |
569 | { | |
570 | /* at_phdr is real address in memory. pt_phdr is what pheader says it is. | |
571 | Relocation offset is the difference between the two. */ | |
572 | sect_addr = sect_addr + (at_phdr - pt_phdr); | |
573 | } | |
574 | ||
97ec2c2f | 575 | /* Read in requested program header. */ |
17658d46 SM |
576 | gdb::byte_vector buf (sect_size); |
577 | if (target_read_memory (sect_addr, buf.data (), sect_size)) | |
578 | return {}; | |
97ec2c2f UW |
579 | |
580 | if (p_arch_size) | |
581 | *p_arch_size = arch_size; | |
a738da3a MF |
582 | if (base_addr) |
583 | *base_addr = sect_addr; | |
97ec2c2f UW |
584 | |
585 | return buf; | |
586 | } | |
587 | ||
588 | ||
589 | /* Return program interpreter string. */ | |
6b09f134 | 590 | static std::optional<gdb::byte_vector> |
97ec2c2f UW |
591 | find_program_interpreter (void) |
592 | { | |
7e10abd1 TT |
593 | /* If we have a current exec_bfd, use its section table. */ |
594 | if (current_program_space->exec_bfd () | |
595 | && (bfd_get_flavour (current_program_space->exec_bfd ()) | |
596 | == bfd_target_elf_flavour)) | |
97ec2c2f UW |
597 | { |
598 | struct bfd_section *interp_sect; | |
599 | ||
7e10abd1 TT |
600 | interp_sect = bfd_get_section_by_name (current_program_space->exec_bfd (), |
601 | ".interp"); | |
97ec2c2f UW |
602 | if (interp_sect != NULL) |
603 | { | |
fd361982 | 604 | int sect_size = bfd_section_size (interp_sect); |
97ec2c2f | 605 | |
17658d46 | 606 | gdb::byte_vector buf (sect_size); |
8e94bb3e TV |
607 | bool res |
608 | = bfd_get_section_contents (current_program_space->exec_bfd (), | |
609 | interp_sect, buf.data (), 0, sect_size); | |
610 | if (res) | |
611 | return buf; | |
97ec2c2f UW |
612 | } |
613 | } | |
614 | ||
17658d46 SM |
615 | /* If we didn't find it, use the target auxiliary vector. */ |
616 | return read_program_header (PT_INTERP, NULL, NULL); | |
97ec2c2f UW |
617 | } |
618 | ||
619 | ||
b6d7a4bf SM |
620 | /* Scan for DESIRED_DYNTAG in .dynamic section of the target's main executable, |
621 | found by consulting the OS auxillary vector. If DESIRED_DYNTAG is found, 1 | |
622 | is returned and the corresponding PTR is set. */ | |
97ec2c2f UW |
623 | |
624 | static int | |
a738da3a MF |
625 | scan_dyntag_auxv (const int desired_dyntag, CORE_ADDR *ptr, |
626 | CORE_ADDR *ptr_addr) | |
97ec2c2f | 627 | { |
99d9c3b9 | 628 | bfd_endian byte_order = gdbarch_byte_order (current_inferior ()->arch ()); |
17658d46 | 629 | int arch_size, step; |
b6d7a4bf | 630 | long current_dyntag; |
97ec2c2f | 631 | CORE_ADDR dyn_ptr; |
a738da3a | 632 | CORE_ADDR base_addr; |
97ec2c2f UW |
633 | |
634 | /* Read in .dynamic section. */ | |
6b09f134 | 635 | std::optional<gdb::byte_vector> ph_data |
17658d46 SM |
636 | = read_program_header (PT_DYNAMIC, &arch_size, &base_addr); |
637 | if (!ph_data) | |
97ec2c2f UW |
638 | return 0; |
639 | ||
640 | /* Iterate over BUF and scan for DYNTAG. If found, set PTR and return. */ | |
641 | step = (arch_size == 32) ? sizeof (Elf32_External_Dyn) | |
642 | : sizeof (Elf64_External_Dyn); | |
17658d46 SM |
643 | for (gdb_byte *buf = ph_data->data (), *bufend = buf + ph_data->size (); |
644 | buf < bufend; buf += step) | |
97ec2c2f UW |
645 | { |
646 | if (arch_size == 32) | |
647 | { | |
648 | Elf32_External_Dyn *dynp = (Elf32_External_Dyn *) buf; | |
433759f7 | 649 | |
b6d7a4bf | 650 | current_dyntag = extract_unsigned_integer ((gdb_byte *) dynp->d_tag, |
e17a4113 UW |
651 | 4, byte_order); |
652 | dyn_ptr = extract_unsigned_integer ((gdb_byte *) dynp->d_un.d_ptr, | |
653 | 4, byte_order); | |
97ec2c2f UW |
654 | } |
655 | else | |
656 | { | |
657 | Elf64_External_Dyn *dynp = (Elf64_External_Dyn *) buf; | |
433759f7 | 658 | |
b6d7a4bf | 659 | current_dyntag = extract_unsigned_integer ((gdb_byte *) dynp->d_tag, |
e17a4113 UW |
660 | 8, byte_order); |
661 | dyn_ptr = extract_unsigned_integer ((gdb_byte *) dynp->d_un.d_ptr, | |
662 | 8, byte_order); | |
97ec2c2f | 663 | } |
b6d7a4bf | 664 | if (current_dyntag == DT_NULL) |
97ec2c2f UW |
665 | break; |
666 | ||
b6d7a4bf | 667 | if (current_dyntag == desired_dyntag) |
97ec2c2f UW |
668 | { |
669 | if (ptr) | |
670 | *ptr = dyn_ptr; | |
671 | ||
a738da3a | 672 | if (ptr_addr) |
17658d46 | 673 | *ptr_addr = base_addr + buf - ph_data->data (); |
a738da3a | 674 | |
97ec2c2f UW |
675 | return 1; |
676 | } | |
677 | } | |
678 | ||
97ec2c2f UW |
679 | return 0; |
680 | } | |
681 | ||
7f86f058 PA |
682 | /* Locate the base address of dynamic linker structs for SVR4 elf |
683 | targets. | |
13437d4b KB |
684 | |
685 | For SVR4 elf targets the address of the dynamic linker's runtime | |
686 | structure is contained within the dynamic info section in the | |
687 | executable file. The dynamic section is also mapped into the | |
688 | inferior address space. Because the runtime loader fills in the | |
689 | real address before starting the inferior, we have to read in the | |
690 | dynamic info section from the inferior address space. | |
691 | If there are any errors while trying to find the address, we | |
7f86f058 | 692 | silently return 0, otherwise the found address is returned. */ |
13437d4b KB |
693 | |
694 | static CORE_ADDR | |
695 | elf_locate_base (void) | |
696 | { | |
3b7344d5 | 697 | struct bound_minimal_symbol msymbol; |
a738da3a | 698 | CORE_ADDR dyn_ptr, dyn_ptr_addr; |
13437d4b | 699 | |
60d09f0a MM |
700 | if (!svr4_have_link_map_offsets ()) |
701 | return 0; | |
702 | ||
65728c26 DJ |
703 | /* Look for DT_MIPS_RLD_MAP first. MIPS executables use this |
704 | instead of DT_DEBUG, although they sometimes contain an unused | |
705 | DT_DEBUG. */ | |
8ddf4645 AM |
706 | if (gdb_bfd_scan_elf_dyntag (DT_MIPS_RLD_MAP, |
707 | current_program_space->exec_bfd (), | |
708 | &dyn_ptr, NULL) | |
a738da3a | 709 | || scan_dyntag_auxv (DT_MIPS_RLD_MAP, &dyn_ptr, NULL)) |
3a40aaa0 | 710 | { |
99d9c3b9 SM |
711 | type *ptr_type |
712 | = builtin_type (current_inferior ()->arch ())->builtin_data_ptr; | |
3a40aaa0 | 713 | gdb_byte *pbuf; |
df86565b | 714 | int pbuf_size = ptr_type->length (); |
433759f7 | 715 | |
224c3ddb | 716 | pbuf = (gdb_byte *) alloca (pbuf_size); |
3a40aaa0 UW |
717 | /* DT_MIPS_RLD_MAP contains a pointer to the address |
718 | of the dynamic link structure. */ | |
719 | if (target_read_memory (dyn_ptr, pbuf, pbuf_size)) | |
e499d0f1 | 720 | return 0; |
b6da22b0 | 721 | return extract_typed_address (pbuf, ptr_type); |
e499d0f1 DJ |
722 | } |
723 | ||
a738da3a MF |
724 | /* Then check DT_MIPS_RLD_MAP_REL. MIPS executables now use this form |
725 | because of needing to support PIE. DT_MIPS_RLD_MAP will also exist | |
726 | in non-PIE. */ | |
8ddf4645 AM |
727 | if (gdb_bfd_scan_elf_dyntag (DT_MIPS_RLD_MAP_REL, |
728 | current_program_space->exec_bfd (), | |
729 | &dyn_ptr, &dyn_ptr_addr) | |
a738da3a MF |
730 | || scan_dyntag_auxv (DT_MIPS_RLD_MAP_REL, &dyn_ptr, &dyn_ptr_addr)) |
731 | { | |
99d9c3b9 SM |
732 | type *ptr_type |
733 | = builtin_type (current_inferior ()->arch ())->builtin_data_ptr; | |
a738da3a | 734 | gdb_byte *pbuf; |
df86565b | 735 | int pbuf_size = ptr_type->length (); |
a738da3a | 736 | |
224c3ddb | 737 | pbuf = (gdb_byte *) alloca (pbuf_size); |
a738da3a MF |
738 | /* DT_MIPS_RLD_MAP_REL contains an offset from the address of the |
739 | DT slot to the address of the dynamic link structure. */ | |
740 | if (target_read_memory (dyn_ptr + dyn_ptr_addr, pbuf, pbuf_size)) | |
741 | return 0; | |
742 | return extract_typed_address (pbuf, ptr_type); | |
743 | } | |
744 | ||
65728c26 | 745 | /* Find DT_DEBUG. */ |
8ddf4645 AM |
746 | if (gdb_bfd_scan_elf_dyntag (DT_DEBUG, current_program_space->exec_bfd (), |
747 | &dyn_ptr, NULL) | |
a738da3a | 748 | || scan_dyntag_auxv (DT_DEBUG, &dyn_ptr, NULL)) |
65728c26 DJ |
749 | return dyn_ptr; |
750 | ||
3a40aaa0 UW |
751 | /* This may be a static executable. Look for the symbol |
752 | conventionally named _r_debug, as a last resort. */ | |
a42d7dd8 TT |
753 | msymbol = lookup_minimal_symbol ("_r_debug", NULL, |
754 | current_program_space->symfile_object_file); | |
3b7344d5 | 755 | if (msymbol.minsym != NULL) |
4aeddc50 | 756 | return msymbol.value_address (); |
13437d4b KB |
757 | |
758 | /* DT_DEBUG entry not found. */ | |
759 | return 0; | |
760 | } | |
761 | ||
e4cd0d6a | 762 | /* Find the first element in the inferior's dynamic link map, and |
6f992fbf JB |
763 | return its address in the inferior. Return zero if the address |
764 | could not be determined. | |
13437d4b | 765 | |
e4cd0d6a MK |
766 | FIXME: Perhaps we should validate the info somehow, perhaps by |
767 | checking r_version for a known version number, or r_state for | |
768 | RT_CONSISTENT. */ | |
13437d4b KB |
769 | |
770 | static CORE_ADDR | |
8d56636a | 771 | solib_svr4_r_map (CORE_ADDR debug_base) |
13437d4b | 772 | { |
4b188b9f | 773 | struct link_map_offsets *lmo = svr4_fetch_link_map_offsets (); |
99d9c3b9 SM |
774 | type *ptr_type |
775 | = builtin_type (current_inferior ()->arch ())->builtin_data_ptr; | |
08597104 | 776 | CORE_ADDR addr = 0; |
13437d4b | 777 | |
a70b8144 | 778 | try |
08597104 | 779 | { |
8d56636a | 780 | addr = read_memory_typed_address (debug_base + lmo->r_map_offset, |
dda83cd7 | 781 | ptr_type); |
08597104 | 782 | } |
230d2906 | 783 | catch (const gdb_exception_error &ex) |
492d29ea PA |
784 | { |
785 | exception_print (gdb_stderr, ex); | |
786 | } | |
492d29ea | 787 | |
08597104 | 788 | return addr; |
e4cd0d6a | 789 | } |
13437d4b | 790 | |
7cd25cfc DJ |
791 | /* Find r_brk from the inferior's debug base. */ |
792 | ||
793 | static CORE_ADDR | |
1a816a87 | 794 | solib_svr4_r_brk (struct svr4_info *info) |
7cd25cfc DJ |
795 | { |
796 | struct link_map_offsets *lmo = svr4_fetch_link_map_offsets (); | |
99d9c3b9 SM |
797 | type *ptr_type |
798 | = builtin_type (current_inferior ()->arch ())->builtin_data_ptr; | |
7cd25cfc | 799 | |
1a816a87 PA |
800 | return read_memory_typed_address (info->debug_base + lmo->r_brk_offset, |
801 | ptr_type); | |
7cd25cfc DJ |
802 | } |
803 | ||
e4cd0d6a MK |
804 | /* Find the link map for the dynamic linker (if it is not in the |
805 | normal list of loaded shared objects). */ | |
13437d4b | 806 | |
e4cd0d6a | 807 | static CORE_ADDR |
1a816a87 | 808 | solib_svr4_r_ldsomap (struct svr4_info *info) |
e4cd0d6a MK |
809 | { |
810 | struct link_map_offsets *lmo = svr4_fetch_link_map_offsets (); | |
99d9c3b9 SM |
811 | type *ptr_type |
812 | = builtin_type (current_inferior ()->arch ())->builtin_data_ptr; | |
34877895 | 813 | enum bfd_endian byte_order = type_byte_order (ptr_type); |
416f679e SDJ |
814 | ULONGEST version = 0; |
815 | ||
a70b8144 | 816 | try |
416f679e SDJ |
817 | { |
818 | /* Check version, and return zero if `struct r_debug' doesn't have | |
819 | the r_ldsomap member. */ | |
820 | version | |
821 | = read_memory_unsigned_integer (info->debug_base + lmo->r_version_offset, | |
822 | lmo->r_version_size, byte_order); | |
823 | } | |
230d2906 | 824 | catch (const gdb_exception_error &ex) |
416f679e SDJ |
825 | { |
826 | exception_print (gdb_stderr, ex); | |
827 | } | |
13437d4b | 828 | |
e4cd0d6a MK |
829 | if (version < 2 || lmo->r_ldsomap_offset == -1) |
830 | return 0; | |
13437d4b | 831 | |
1a816a87 | 832 | return read_memory_typed_address (info->debug_base + lmo->r_ldsomap_offset, |
b6da22b0 | 833 | ptr_type); |
13437d4b KB |
834 | } |
835 | ||
8d56636a MM |
836 | /* Find the next namespace from the r_next field. */ |
837 | ||
838 | static CORE_ADDR | |
839 | solib_svr4_r_next (CORE_ADDR debug_base) | |
840 | { | |
841 | link_map_offsets *lmo = svr4_fetch_link_map_offsets (); | |
99d9c3b9 SM |
842 | type *ptr_type |
843 | = builtin_type (current_inferior ()->arch ())->builtin_data_ptr; | |
8d56636a MM |
844 | bfd_endian byte_order = type_byte_order (ptr_type); |
845 | ULONGEST version = 0; | |
846 | ||
847 | try | |
848 | { | |
849 | version | |
850 | = read_memory_unsigned_integer (debug_base + lmo->r_version_offset, | |
851 | lmo->r_version_size, byte_order); | |
852 | } | |
853 | catch (const gdb_exception_error &ex) | |
854 | { | |
855 | exception_print (gdb_stderr, ex); | |
856 | } | |
857 | ||
858 | /* The r_next field is added with r_version == 2. */ | |
859 | if (version < 2 || lmo->r_next_offset == -1) | |
860 | return 0; | |
861 | ||
862 | return read_memory_typed_address (debug_base + lmo->r_next_offset, | |
863 | ptr_type); | |
864 | } | |
865 | ||
de18c1d8 JM |
866 | /* On Solaris systems with some versions of the dynamic linker, |
867 | ld.so's l_name pointer points to the SONAME in the string table | |
868 | rather than into writable memory. So that GDB can find shared | |
869 | libraries when loading a core file generated by gcore, ensure that | |
870 | memory areas containing the l_name string are saved in the core | |
871 | file. */ | |
872 | ||
873 | static int | |
874 | svr4_keep_data_in_core (CORE_ADDR vaddr, unsigned long size) | |
875 | { | |
876 | struct svr4_info *info; | |
877 | CORE_ADDR ldsomap; | |
74de0234 | 878 | CORE_ADDR name_lm; |
de18c1d8 | 879 | |
d70cc3ba | 880 | info = get_svr4_info (current_program_space); |
de18c1d8 | 881 | |
60d09f0a MM |
882 | info->debug_base = elf_locate_base (); |
883 | if (info->debug_base == 0) | |
de18c1d8 JM |
884 | return 0; |
885 | ||
886 | ldsomap = solib_svr4_r_ldsomap (info); | |
887 | if (!ldsomap) | |
888 | return 0; | |
889 | ||
a7961323 | 890 | std::unique_ptr<lm_info_svr4> li = lm_info_read (ldsomap); |
d0e449a1 | 891 | name_lm = li != NULL ? li->l_name : 0; |
de18c1d8 | 892 | |
74de0234 | 893 | return (name_lm >= vaddr && name_lm < vaddr + size); |
de18c1d8 JM |
894 | } |
895 | ||
bf469271 | 896 | /* See solist.h. */ |
13437d4b KB |
897 | |
898 | static int | |
bf469271 | 899 | open_symbol_file_object (int from_tty) |
13437d4b KB |
900 | { |
901 | CORE_ADDR lm, l_name; | |
4b188b9f | 902 | struct link_map_offsets *lmo = svr4_fetch_link_map_offsets (); |
99d9c3b9 SM |
903 | type *ptr_type |
904 | = builtin_type (current_inferior ()->arch ())->builtin_data_ptr; | |
df86565b | 905 | int l_name_size = ptr_type->length (); |
a7961323 | 906 | gdb::byte_vector l_name_buf (l_name_size); |
d70cc3ba | 907 | struct svr4_info *info = get_svr4_info (current_program_space); |
ecf45d2c SL |
908 | symfile_add_flags add_flags = 0; |
909 | ||
910 | if (from_tty) | |
911 | add_flags |= SYMFILE_VERBOSE; | |
13437d4b | 912 | |
a42d7dd8 | 913 | if (current_program_space->symfile_object_file) |
9e2f0ad4 | 914 | if (!query (_("Attempt to reload symbols from process? "))) |
a7961323 | 915 | return 0; |
13437d4b | 916 | |
7cd25cfc | 917 | /* Always locate the debug struct, in case it has moved. */ |
60d09f0a MM |
918 | info->debug_base = elf_locate_base (); |
919 | if (info->debug_base == 0) | |
a7961323 | 920 | return 0; /* failed somehow... */ |
13437d4b KB |
921 | |
922 | /* First link map member should be the executable. */ | |
8d56636a | 923 | lm = solib_svr4_r_map (info->debug_base); |
e4cd0d6a | 924 | if (lm == 0) |
a7961323 | 925 | return 0; /* failed somehow... */ |
13437d4b KB |
926 | |
927 | /* Read address of name from target memory to GDB. */ | |
a7961323 | 928 | read_memory (lm + lmo->l_name_offset, l_name_buf.data (), l_name_size); |
13437d4b | 929 | |
cfaefc65 | 930 | /* Convert the address to host format. */ |
a7961323 | 931 | l_name = extract_typed_address (l_name_buf.data (), ptr_type); |
13437d4b | 932 | |
13437d4b | 933 | if (l_name == 0) |
a7961323 | 934 | return 0; /* No filename. */ |
13437d4b KB |
935 | |
936 | /* Now fetch the filename from target memory. */ | |
66920317 TT |
937 | gdb::unique_xmalloc_ptr<char> filename |
938 | = target_read_string (l_name, SO_NAME_MAX_PATH_SIZE - 1); | |
13437d4b | 939 | |
66920317 | 940 | if (filename == nullptr) |
13437d4b | 941 | { |
66920317 | 942 | warning (_("failed to read exec filename from attached file")); |
13437d4b KB |
943 | return 0; |
944 | } | |
945 | ||
13437d4b | 946 | /* Have a pathname: read the symbol file. */ |
e83e4e24 | 947 | symbol_file_add_main (filename.get (), add_flags); |
13437d4b KB |
948 | |
949 | return 1; | |
950 | } | |
13437d4b | 951 | |
2268b414 JK |
952 | /* Data exchange structure for the XML parser as returned by |
953 | svr4_current_sos_via_xfer_libraries. */ | |
954 | ||
955 | struct svr4_library_list | |
956 | { | |
8971d278 | 957 | /* The so list for the current namespace. This is internal to XML |
8d56636a | 958 | parsing. */ |
96bb3873 | 959 | std::vector<svr4_so> *cur_list; |
2268b414 JK |
960 | |
961 | /* Inferior address of struct link_map used for the main executable. It is | |
962 | NULL if not known. */ | |
963 | CORE_ADDR main_lm; | |
8d56636a MM |
964 | |
965 | /* List of objects loaded into the inferior per namespace. This does | |
966 | not include any default sos. | |
967 | ||
968 | See comment on struct svr4_info.solib_lists. */ | |
96bb3873 | 969 | std::map<CORE_ADDR, std::vector<svr4_so>> solib_lists; |
2268b414 JK |
970 | }; |
971 | ||
7905fc35 PA |
972 | /* This module's 'free_objfile' observer. */ |
973 | ||
974 | static void | |
975 | svr4_free_objfile_observer (struct objfile *objfile) | |
976 | { | |
977 | probes_table_remove_objfile_probes (objfile); | |
978 | } | |
979 | ||
6cedf3bc | 980 | /* Implement solib_ops.clear_so. */ |
0892cb63 DE |
981 | |
982 | static void | |
7b323785 | 983 | svr4_clear_so (const solib &so) |
0892cb63 | 984 | { |
7ad0a42e | 985 | auto *li = gdb::checked_static_cast<lm_info_svr4 *> (so.lm_info.get ()); |
d0e449a1 SM |
986 | |
987 | if (li != NULL) | |
988 | li->l_addr_p = 0; | |
0892cb63 DE |
989 | } |
990 | ||
96bb3873 | 991 | /* Create the so_list objects equivalent to the svr4_sos in SOS. */ |
93f2a35e | 992 | |
7b323785 | 993 | static intrusive_list<solib> |
96bb3873 | 994 | so_list_from_svr4_sos (const std::vector<svr4_so> &sos) |
f9e14852 | 995 | { |
7b323785 | 996 | intrusive_list<solib> dst; |
f9e14852 | 997 | |
96bb3873 | 998 | for (const svr4_so &so : sos) |
f9e14852 | 999 | { |
7b323785 | 1000 | struct solib *newobj = new struct solib; |
f9e14852 | 1001 | |
98107b0b SM |
1002 | newobj->so_name = so.name; |
1003 | newobj->so_original_name = so.name; | |
6b62451a | 1004 | newobj->lm_info = std::make_unique<lm_info_svr4> (*so.lm_info); |
f9e14852 | 1005 | |
8971d278 | 1006 | dst.push_back (*newobj); |
f9e14852 GB |
1007 | } |
1008 | ||
1009 | return dst; | |
1010 | } | |
1011 | ||
2268b414 JK |
1012 | #ifdef HAVE_LIBEXPAT |
1013 | ||
1014 | #include "xml-support.h" | |
1015 | ||
1016 | /* Handle the start of a <library> element. Note: new elements are added | |
1017 | at the tail of the list, keeping the list in order. */ | |
1018 | ||
1019 | static void | |
1020 | library_list_start_library (struct gdb_xml_parser *parser, | |
1021 | const struct gdb_xml_element *element, | |
4d0fdd9b SM |
1022 | void *user_data, |
1023 | std::vector<gdb_xml_value> &attributes) | |
2268b414 | 1024 | { |
19ba03f4 SM |
1025 | struct svr4_library_list *list = (struct svr4_library_list *) user_data; |
1026 | const char *name | |
4d0fdd9b | 1027 | = (const char *) xml_find_attribute (attributes, "name")->value.get (); |
19ba03f4 | 1028 | ULONGEST *lmp |
4d0fdd9b | 1029 | = (ULONGEST *) xml_find_attribute (attributes, "lm")->value.get (); |
19ba03f4 | 1030 | ULONGEST *l_addrp |
4d0fdd9b | 1031 | = (ULONGEST *) xml_find_attribute (attributes, "l_addr")->value.get (); |
19ba03f4 | 1032 | ULONGEST *l_ldp |
4d0fdd9b | 1033 | = (ULONGEST *) xml_find_attribute (attributes, "l_ld")->value.get (); |
2268b414 | 1034 | |
6b62451a | 1035 | lm_info_svr4_up li = std::make_unique<lm_info_svr4> (); |
d0e449a1 SM |
1036 | li->lm_addr = *lmp; |
1037 | li->l_addr_inferior = *l_addrp; | |
1038 | li->l_ld = *l_ldp; | |
2268b414 | 1039 | |
96bb3873 | 1040 | std::vector<svr4_so> *solist; |
2268b414 | 1041 | |
2733d9d5 MM |
1042 | /* Older versions did not supply lmid. Put the element into the flat |
1043 | list of the special namespace zero in that case. */ | |
1044 | gdb_xml_value *at_lmid = xml_find_attribute (attributes, "lmid"); | |
1045 | if (at_lmid == nullptr) | |
96bb3873 | 1046 | solist = list->cur_list; |
2733d9d5 MM |
1047 | else |
1048 | { | |
1049 | ULONGEST lmid = *(ULONGEST *) at_lmid->value.get (); | |
96bb3873 | 1050 | solist = &list->solib_lists[lmid]; |
2733d9d5 | 1051 | } |
96bb3873 SM |
1052 | |
1053 | solist->emplace_back (name, std::move (li)); | |
2268b414 JK |
1054 | } |
1055 | ||
1056 | /* Handle the start of a <library-list-svr4> element. */ | |
1057 | ||
1058 | static void | |
1059 | svr4_library_list_start_list (struct gdb_xml_parser *parser, | |
1060 | const struct gdb_xml_element *element, | |
4d0fdd9b SM |
1061 | void *user_data, |
1062 | std::vector<gdb_xml_value> &attributes) | |
2268b414 | 1063 | { |
19ba03f4 SM |
1064 | struct svr4_library_list *list = (struct svr4_library_list *) user_data; |
1065 | const char *version | |
4d0fdd9b | 1066 | = (const char *) xml_find_attribute (attributes, "version")->value.get (); |
2268b414 JK |
1067 | struct gdb_xml_value *main_lm = xml_find_attribute (attributes, "main-lm"); |
1068 | ||
1069 | if (strcmp (version, "1.0") != 0) | |
1070 | gdb_xml_error (parser, | |
1071 | _("SVR4 Library list has unsupported version \"%s\""), | |
1072 | version); | |
1073 | ||
1074 | if (main_lm) | |
4d0fdd9b | 1075 | list->main_lm = *(ULONGEST *) main_lm->value.get (); |
8d56636a MM |
1076 | |
1077 | /* Older gdbserver do not support namespaces. We use the special | |
1078 | namespace zero for a linear list of libraries. */ | |
96bb3873 | 1079 | list->cur_list = &list->solib_lists[0]; |
2268b414 JK |
1080 | } |
1081 | ||
1082 | /* The allowed elements and attributes for an XML library list. | |
1083 | The root element is a <library-list>. */ | |
1084 | ||
1085 | static const struct gdb_xml_attribute svr4_library_attributes[] = | |
1086 | { | |
1087 | { "name", GDB_XML_AF_NONE, NULL, NULL }, | |
1088 | { "lm", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL }, | |
1089 | { "l_addr", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL }, | |
1090 | { "l_ld", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL }, | |
2733d9d5 | 1091 | { "lmid", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL }, |
2268b414 JK |
1092 | { NULL, GDB_XML_AF_NONE, NULL, NULL } |
1093 | }; | |
1094 | ||
1095 | static const struct gdb_xml_element svr4_library_list_children[] = | |
1096 | { | |
1097 | { | |
1098 | "library", svr4_library_attributes, NULL, | |
1099 | GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL, | |
1100 | library_list_start_library, NULL | |
1101 | }, | |
1102 | { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL } | |
1103 | }; | |
1104 | ||
1105 | static const struct gdb_xml_attribute svr4_library_list_attributes[] = | |
1106 | { | |
1107 | { "version", GDB_XML_AF_NONE, NULL, NULL }, | |
1108 | { "main-lm", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL }, | |
1109 | { NULL, GDB_XML_AF_NONE, NULL, NULL } | |
1110 | }; | |
1111 | ||
1112 | static const struct gdb_xml_element svr4_library_list_elements[] = | |
1113 | { | |
1114 | { "library-list-svr4", svr4_library_list_attributes, svr4_library_list_children, | |
1115 | GDB_XML_EF_NONE, svr4_library_list_start_list, NULL }, | |
1116 | { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL } | |
1117 | }; | |
1118 | ||
2268b414 JK |
1119 | /* Parse qXfer:libraries:read packet into *SO_LIST_RETURN. Return 1 if |
1120 | ||
1121 | Return 0 if packet not supported, *SO_LIST_RETURN is not modified in such | |
1122 | case. Return 1 if *SO_LIST_RETURN contains the library list, it may be | |
1123 | empty, caller is responsible for freeing all its entries. */ | |
1124 | ||
1125 | static int | |
1126 | svr4_parse_libraries (const char *document, struct svr4_library_list *list) | |
1127 | { | |
8d56636a | 1128 | auto cleanup = make_scope_exit ([list] () |
96bb3873 | 1129 | { list->solib_lists.clear (); }); |
2268b414 | 1130 | |
96bb3873 | 1131 | list->cur_list = nullptr; |
8d56636a MM |
1132 | list->main_lm = 0; |
1133 | list->solib_lists.clear (); | |
2eca4a8d | 1134 | if (gdb_xml_parse_quick (_("target library list"), "library-list-svr4.dtd", |
2268b414 JK |
1135 | svr4_library_list_elements, document, list) == 0) |
1136 | { | |
1137 | /* Parsed successfully, keep the result. */ | |
2b6ff1c0 | 1138 | cleanup.release (); |
2268b414 JK |
1139 | return 1; |
1140 | } | |
1141 | ||
2268b414 JK |
1142 | return 0; |
1143 | } | |
1144 | ||
f9e14852 | 1145 | /* Attempt to get so_list from target via qXfer:libraries-svr4:read packet. |
2268b414 JK |
1146 | |
1147 | Return 0 if packet not supported, *SO_LIST_RETURN is not modified in such | |
1148 | case. Return 1 if *SO_LIST_RETURN contains the library list, it may be | |
f9e14852 GB |
1149 | empty, caller is responsible for freeing all its entries. |
1150 | ||
1151 | Note that ANNEX must be NULL if the remote does not explicitly allow | |
1152 | qXfer:libraries-svr4:read packets with non-empty annexes. Support for | |
1153 | this can be checked using target_augmented_libraries_svr4_read (). */ | |
2268b414 JK |
1154 | |
1155 | static int | |
f9e14852 GB |
1156 | svr4_current_sos_via_xfer_libraries (struct svr4_library_list *list, |
1157 | const char *annex) | |
2268b414 | 1158 | { |
f9e14852 GB |
1159 | gdb_assert (annex == NULL || target_augmented_libraries_svr4_read ()); |
1160 | ||
2268b414 | 1161 | /* Fetch the list of shared libraries. */ |
6b09f134 | 1162 | std::optional<gdb::char_vector> svr4_library_document |
328d42d8 SM |
1163 | = target_read_stralloc (current_inferior ()->top_target (), |
1164 | TARGET_OBJECT_LIBRARIES_SVR4, | |
b7b030ad | 1165 | annex); |
9018be22 | 1166 | if (!svr4_library_document) |
2268b414 JK |
1167 | return 0; |
1168 | ||
9018be22 | 1169 | return svr4_parse_libraries (svr4_library_document->data (), list); |
2268b414 JK |
1170 | } |
1171 | ||
1172 | #else | |
1173 | ||
1174 | static int | |
f9e14852 GB |
1175 | svr4_current_sos_via_xfer_libraries (struct svr4_library_list *list, |
1176 | const char *annex) | |
2268b414 JK |
1177 | { |
1178 | return 0; | |
1179 | } | |
1180 | ||
1181 | #endif | |
1182 | ||
34439770 DJ |
1183 | /* If no shared library information is available from the dynamic |
1184 | linker, build a fallback list from other sources. */ | |
1185 | ||
7b323785 | 1186 | static intrusive_list<solib> |
d70cc3ba | 1187 | svr4_default_sos (svr4_info *info) |
34439770 | 1188 | { |
8e5c319d | 1189 | if (!info->debug_loader_offset_p) |
8971d278 | 1190 | return {}; |
34439770 | 1191 | |
7b323785 | 1192 | solib *newobj = new solib; |
6b62451a | 1193 | auto li = std::make_unique<lm_info_svr4> (); |
34439770 | 1194 | |
3957565a | 1195 | /* Nothing will ever check the other fields if we set l_addr_p. */ |
8d56636a | 1196 | li->l_addr = li->l_addr_inferior = info->debug_loader_offset; |
d0e449a1 | 1197 | li->l_addr_p = 1; |
34439770 | 1198 | |
7ad0a42e | 1199 | newobj->lm_info = std::move (li); |
98107b0b SM |
1200 | newobj->so_name = info->debug_loader_name; |
1201 | newobj->so_original_name = newobj->so_name; | |
34439770 | 1202 | |
7b323785 | 1203 | intrusive_list<solib> sos; |
8971d278 SM |
1204 | sos.push_back (*newobj); |
1205 | ||
1206 | return sos; | |
34439770 DJ |
1207 | } |
1208 | ||
f9e14852 GB |
1209 | /* Read the whole inferior libraries chain starting at address LM. |
1210 | Expect the first entry in the chain's previous entry to be PREV_LM. | |
96bb3873 SM |
1211 | Add the entries to SOS. Ignore the first entry if IGNORE_FIRST and set |
1212 | global MAIN_LM_ADDR according to it. Returns nonzero upon success. If zero | |
1213 | is returned the entries stored to LINK_PTR_PTR are still valid although they may | |
f9e14852 | 1214 | represent only part of the inferior library list. */ |
13437d4b | 1215 | |
f9e14852 | 1216 | static int |
d70cc3ba | 1217 | svr4_read_so_list (svr4_info *info, CORE_ADDR lm, CORE_ADDR prev_lm, |
96bb3873 | 1218 | std::vector<svr4_so> &sos, int ignore_first) |
13437d4b | 1219 | { |
c725e7b6 | 1220 | CORE_ADDR first_l_name = 0; |
f9e14852 | 1221 | CORE_ADDR next_lm; |
13437d4b | 1222 | |
cb08cc53 | 1223 | for (; lm != 0; prev_lm = lm, lm = next_lm) |
13437d4b | 1224 | { |
96bb3873 | 1225 | lm_info_svr4_up li = lm_info_read (lm); |
d0e449a1 | 1226 | if (li == NULL) |
b3bc8453 | 1227 | return 0; |
13437d4b | 1228 | |
d0e449a1 | 1229 | next_lm = li->l_next; |
492928e4 | 1230 | |
d0e449a1 | 1231 | if (li->l_prev != prev_lm) |
492928e4 | 1232 | { |
2268b414 | 1233 | warning (_("Corrupted shared library list: %s != %s"), |
99d9c3b9 SM |
1234 | paddress (current_inferior ()->arch (), prev_lm), |
1235 | paddress (current_inferior ()->arch (), li->l_prev)); | |
f9e14852 | 1236 | return 0; |
492928e4 | 1237 | } |
13437d4b KB |
1238 | |
1239 | /* For SVR4 versions, the first entry in the link map is for the | |
dda83cd7 SM |
1240 | inferior executable, so we must ignore it. For some versions of |
1241 | SVR4, it has no name. For others (Solaris 2.3 for example), it | |
1242 | does have a name, so we can no longer use a missing name to | |
1243 | decide when to ignore it. */ | |
d0e449a1 | 1244 | if (ignore_first && li->l_prev == 0) |
93a57060 | 1245 | { |
d0e449a1 SM |
1246 | first_l_name = li->l_name; |
1247 | info->main_lm_addr = li->lm_addr; | |
cb08cc53 | 1248 | continue; |
93a57060 | 1249 | } |
13437d4b | 1250 | |
cb08cc53 | 1251 | /* Extract this shared object's name. */ |
96bb3873 | 1252 | gdb::unique_xmalloc_ptr<char> name |
66920317 | 1253 | = target_read_string (li->l_name, SO_NAME_MAX_PATH_SIZE - 1); |
96bb3873 | 1254 | if (name == nullptr) |
cb08cc53 | 1255 | { |
7d760051 UW |
1256 | /* If this entry's l_name address matches that of the |
1257 | inferior executable, then this is not a normal shared | |
1258 | object, but (most likely) a vDSO. In this case, silently | |
1259 | skip it; otherwise emit a warning. */ | |
d0e449a1 | 1260 | if (first_l_name == 0 || li->l_name != first_l_name) |
66920317 | 1261 | warning (_("Can't read pathname for load map.")); |
cb08cc53 | 1262 | continue; |
13437d4b KB |
1263 | } |
1264 | ||
cb08cc53 JK |
1265 | /* If this entry has no name, or its name matches the name |
1266 | for the main executable, don't include it in the list. */ | |
96bb3873 | 1267 | if (*name == '\0' || match_main (name.get ())) |
b3bc8453 | 1268 | continue; |
e4cd0d6a | 1269 | |
96bb3873 | 1270 | sos.emplace_back (name.get (), std::move (li)); |
13437d4b | 1271 | } |
f9e14852 GB |
1272 | |
1273 | return 1; | |
cb08cc53 JK |
1274 | } |
1275 | ||
f9e14852 GB |
1276 | /* Read the full list of currently loaded shared objects directly |
1277 | from the inferior, without referring to any libraries read and | |
1278 | stored by the probes interface. Handle special cases relating | |
8d56636a | 1279 | to the first elements of the list in default namespace. */ |
cb08cc53 | 1280 | |
8d56636a | 1281 | static void |
f9e14852 | 1282 | svr4_current_sos_direct (struct svr4_info *info) |
cb08cc53 JK |
1283 | { |
1284 | CORE_ADDR lm; | |
8d56636a | 1285 | bool ignore_first; |
2268b414 JK |
1286 | struct svr4_library_list library_list; |
1287 | ||
8d56636a | 1288 | /* Remove any old libraries. We're going to read them back in again. */ |
96bb3873 | 1289 | info->solib_lists.clear (); |
8d56636a | 1290 | |
0c5bf5a9 JK |
1291 | /* Fall back to manual examination of the target if the packet is not |
1292 | supported or gdbserver failed to find DT_DEBUG. gdb.server/solib-list.exp | |
1293 | tests a case where gdbserver cannot find the shared libraries list while | |
1294 | GDB itself is able to find it via SYMFILE_OBJFILE. | |
1295 | ||
1296 | Unfortunately statically linked inferiors will also fall back through this | |
1297 | suboptimal code path. */ | |
1298 | ||
f9e14852 GB |
1299 | info->using_xfer = svr4_current_sos_via_xfer_libraries (&library_list, |
1300 | NULL); | |
1301 | if (info->using_xfer) | |
2268b414 JK |
1302 | { |
1303 | if (library_list.main_lm) | |
f9e14852 | 1304 | info->main_lm_addr = library_list.main_lm; |
2268b414 | 1305 | |
8d56636a MM |
1306 | /* Remove an empty special zero namespace so we know that when there |
1307 | is one, it is actually used, and we have a flat list without | |
1308 | namespace information. */ | |
96bb3873 SM |
1309 | auto it_0 = library_list.solib_lists.find (0); |
1310 | if (it_0 != library_list.solib_lists.end () | |
1311 | && it_0->second.empty ()) | |
1312 | library_list.solib_lists.erase (it_0); | |
8d56636a MM |
1313 | |
1314 | /* Replace the (empty) solib_lists in INFO with the one generated | |
1315 | from the target. We don't want to copy it on assignment and then | |
1316 | delete the original afterwards, so let's just swap the | |
1317 | internals. */ | |
1318 | std::swap (info->solib_lists, library_list.solib_lists); | |
1319 | return; | |
2268b414 | 1320 | } |
cb08cc53 | 1321 | |
cb08cc53 JK |
1322 | /* If we can't find the dynamic linker's base structure, this |
1323 | must not be a dynamically linked executable. Hmm. */ | |
60d09f0a MM |
1324 | info->debug_base = elf_locate_base (); |
1325 | if (info->debug_base == 0) | |
8d56636a | 1326 | return; |
cb08cc53 JK |
1327 | |
1328 | /* Assume that everything is a library if the dynamic loader was loaded | |
1329 | late by a static executable. */ | |
7e10abd1 TT |
1330 | if (current_program_space->exec_bfd () |
1331 | && bfd_get_section_by_name (current_program_space->exec_bfd (), | |
1332 | ".dynamic") == NULL) | |
8d56636a | 1333 | ignore_first = false; |
cb08cc53 | 1334 | else |
8d56636a | 1335 | ignore_first = true; |
cb08cc53 | 1336 | |
8d56636a | 1337 | auto cleanup = make_scope_exit ([info] () |
96bb3873 | 1338 | { info->solib_lists.clear (); }); |
cb08cc53 | 1339 | |
8d56636a MM |
1340 | /* Collect the sos in each namespace. */ |
1341 | CORE_ADDR debug_base = info->debug_base; | |
1342 | for (; debug_base != 0; | |
1343 | ignore_first = false, debug_base = solib_svr4_r_next (debug_base)) | |
1344 | { | |
1345 | /* Walk the inferior's link map list, and build our so_list list. */ | |
1346 | lm = solib_svr4_r_map (debug_base); | |
1347 | if (lm != 0) | |
96bb3873 SM |
1348 | svr4_read_so_list (info, lm, 0, info->solib_lists[debug_base], |
1349 | ignore_first); | |
8d56636a | 1350 | } |
cb08cc53 JK |
1351 | |
1352 | /* On Solaris, the dynamic linker is not in the normal list of | |
1353 | shared objects, so make sure we pick it up too. Having | |
1354 | symbol information for the dynamic linker is quite crucial | |
8d56636a MM |
1355 | for skipping dynamic linker resolver code. |
1356 | ||
1357 | Note that we interpret the ldsomap load map address as 'virtual' | |
1358 | r_debug object. If we added it to the default namespace (as it was), | |
1359 | we would probably run into inconsistencies with the load map's | |
1360 | prev/next links (I wonder if we did). */ | |
1361 | debug_base = solib_svr4_r_ldsomap (info); | |
1362 | if (debug_base != 0) | |
1363 | { | |
1364 | /* Add the dynamic linker's namespace unless we already did. */ | |
1365 | if (info->solib_lists.find (debug_base) == info->solib_lists.end ()) | |
96bb3873 SM |
1366 | svr4_read_so_list (info, debug_base, 0, info->solib_lists[debug_base], |
1367 | 0); | |
8d56636a | 1368 | } |
cb08cc53 | 1369 | |
2b6ff1c0 | 1370 | cleanup.release (); |
8d56636a MM |
1371 | } |
1372 | ||
1373 | /* Collect sos read and stored by the probes interface. */ | |
1374 | ||
7b323785 | 1375 | static intrusive_list<solib> |
8d56636a MM |
1376 | svr4_collect_probes_sos (svr4_info *info) |
1377 | { | |
7b323785 | 1378 | intrusive_list<solib> res; |
8d56636a | 1379 | |
96bb3873 | 1380 | for (const auto &tuple : info->solib_lists) |
8d56636a | 1381 | { |
96bb3873 | 1382 | const std::vector<svr4_so> &sos = tuple.second; |
8971d278 | 1383 | res.splice (so_list_from_svr4_sos (sos)); |
8d56636a | 1384 | } |
34439770 | 1385 | |
96bb3873 | 1386 | return res; |
13437d4b KB |
1387 | } |
1388 | ||
6cedf3bc | 1389 | /* Implement the main part of the "current_sos" solib_ops |
8b9a549d | 1390 | method. */ |
f9e14852 | 1391 | |
7b323785 | 1392 | static intrusive_list<solib> |
d70cc3ba | 1393 | svr4_current_sos_1 (svr4_info *info) |
f9e14852 | 1394 | { |
7b323785 | 1395 | intrusive_list<solib> sos; |
8d56636a MM |
1396 | |
1397 | /* If we're using the probes interface, we can use the cache as it will | |
1398 | be maintained by probe update/reload actions. */ | |
1399 | if (info->probes_table != nullptr) | |
1400 | sos = svr4_collect_probes_sos (info); | |
f9e14852 | 1401 | |
8d56636a MM |
1402 | /* If we're not using the probes interface or if we didn't cache |
1403 | anything, read the sos to fill the cache, then collect them from the | |
1404 | cache. */ | |
8971d278 | 1405 | if (sos.empty ()) |
8d56636a MM |
1406 | { |
1407 | svr4_current_sos_direct (info); | |
1408 | ||
1409 | sos = svr4_collect_probes_sos (info); | |
8971d278 | 1410 | if (sos.empty ()) |
8d56636a MM |
1411 | sos = svr4_default_sos (info); |
1412 | } | |
1413 | ||
1414 | return sos; | |
f9e14852 GB |
1415 | } |
1416 | ||
6cedf3bc | 1417 | /* Implement the "current_sos" solib_ops method. */ |
8b9a549d | 1418 | |
7b323785 | 1419 | static intrusive_list<solib> |
8971d278 | 1420 | svr4_current_sos () |
8b9a549d | 1421 | { |
d70cc3ba | 1422 | svr4_info *info = get_svr4_info (current_program_space); |
7b323785 | 1423 | intrusive_list<solib> sos = svr4_current_sos_1 (info); |
8b9a549d PA |
1424 | struct mem_range vsyscall_range; |
1425 | ||
1426 | /* Filter out the vDSO module, if present. Its symbol file would | |
1427 | not be found on disk. The vDSO/vsyscall's OBJFILE is instead | |
1428 | managed by symfile-mem.c:add_vsyscall_page. */ | |
99d9c3b9 | 1429 | if (gdbarch_vsyscall_range (current_inferior ()->arch (), &vsyscall_range) |
8b9a549d PA |
1430 | && vsyscall_range.length != 0) |
1431 | { | |
8971d278 | 1432 | for (auto so = sos.begin (); so != sos.end (); ) |
8b9a549d | 1433 | { |
8b9a549d PA |
1434 | /* We can't simply match the vDSO by starting address alone, |
1435 | because lm_info->l_addr_inferior (and also l_addr) do not | |
1436 | necessarily represent the real starting address of the | |
1437 | ELF if the vDSO's ELF itself is "prelinked". The l_ld | |
1438 | field (the ".dynamic" section of the shared object) | |
1439 | always points at the absolute/resolved address though. | |
1440 | So check whether that address is inside the vDSO's | |
1441 | mapping instead. | |
1442 | ||
1443 | E.g., on Linux 3.16 (x86_64) the vDSO is a regular | |
1444 | 0-based ELF, and we see: | |
1445 | ||
1446 | (gdb) info auxv | |
1447 | 33 AT_SYSINFO_EHDR System-supplied DSO's ELF header 0x7ffff7ffb000 | |
1448 | (gdb) p/x *_r_debug.r_map.l_next | |
1449 | $1 = {l_addr = 0x7ffff7ffb000, ..., l_ld = 0x7ffff7ffb318, ...} | |
1450 | ||
1451 | And on Linux 2.6.32 (x86_64) we see: | |
1452 | ||
1453 | (gdb) info auxv | |
1454 | 33 AT_SYSINFO_EHDR System-supplied DSO's ELF header 0x7ffff7ffe000 | |
1455 | (gdb) p/x *_r_debug.r_map.l_next | |
1456 | $5 = {l_addr = 0x7ffff88fe000, ..., l_ld = 0x7ffff7ffe580, ... } | |
1457 | ||
1458 | Dumping that vDSO shows: | |
1459 | ||
1460 | (gdb) info proc mappings | |
1461 | 0x7ffff7ffe000 0x7ffff7fff000 0x1000 0 [vdso] | |
1462 | (gdb) dump memory vdso.bin 0x7ffff7ffe000 0x7ffff7fff000 | |
1463 | # readelf -Wa vdso.bin | |
1464 | [...] | |
1465 | Entry point address: 0xffffffffff700700 | |
1466 | [...] | |
1467 | Section Headers: | |
1468 | [Nr] Name Type Address Off Size | |
1469 | [ 0] NULL 0000000000000000 000000 000000 | |
1470 | [ 1] .hash HASH ffffffffff700120 000120 000038 | |
1471 | [ 2] .dynsym DYNSYM ffffffffff700158 000158 0000d8 | |
1472 | [...] | |
1473 | [ 9] .dynamic DYNAMIC ffffffffff700580 000580 0000f0 | |
1474 | */ | |
d0e449a1 | 1475 | |
7ad0a42e | 1476 | auto *li = gdb::checked_static_cast<lm_info_svr4 *> (so->lm_info.get ()); |
d0e449a1 SM |
1477 | |
1478 | if (address_in_mem_range (li->l_ld, &vsyscall_range)) | |
8b9a549d | 1479 | { |
8971d278 | 1480 | auto next = sos.erase (so); |
30932f40 | 1481 | delete &*so; |
8971d278 | 1482 | so = next; |
8b9a549d PA |
1483 | break; |
1484 | } | |
1485 | ||
8971d278 | 1486 | ++so; |
8b9a549d PA |
1487 | } |
1488 | } | |
1489 | ||
8971d278 | 1490 | return sos; |
8b9a549d PA |
1491 | } |
1492 | ||
93a57060 | 1493 | /* Get the address of the link_map for a given OBJFILE. */ |
bc4a16ae EZ |
1494 | |
1495 | CORE_ADDR | |
1496 | svr4_fetch_objfile_link_map (struct objfile *objfile) | |
1497 | { | |
d70cc3ba | 1498 | struct svr4_info *info = get_svr4_info (objfile->pspace); |
bc4a16ae | 1499 | |
93a57060 | 1500 | /* Cause svr4_current_sos() to be run if it hasn't been already. */ |
1a816a87 | 1501 | if (info->main_lm_addr == 0) |
e696b3ad | 1502 | solib_add (NULL, 0, auto_solib_add); |
bc4a16ae | 1503 | |
93a57060 | 1504 | /* svr4_current_sos() will set main_lm_addr for the main executable. */ |
a42d7dd8 | 1505 | if (objfile == current_program_space->symfile_object_file) |
1a816a87 | 1506 | return info->main_lm_addr; |
93a57060 DJ |
1507 | |
1508 | /* The other link map addresses may be found by examining the list | |
1509 | of shared libraries. */ | |
7b323785 | 1510 | for (const solib &so : current_program_space->solibs ()) |
8971d278 | 1511 | if (so.objfile == objfile) |
d0e449a1 | 1512 | { |
7ad0a42e | 1513 | auto *li |
8971d278 | 1514 | = gdb::checked_static_cast<lm_info_svr4 *> (so.lm_info.get ()); |
d0e449a1 SM |
1515 | |
1516 | return li->lm_addr; | |
1517 | } | |
93a57060 DJ |
1518 | |
1519 | /* Not found! */ | |
bc4a16ae EZ |
1520 | return 0; |
1521 | } | |
13437d4b KB |
1522 | |
1523 | /* On some systems, the only way to recognize the link map entry for | |
1524 | the main executable file is by looking at its name. Return | |
1525 | non-zero iff SONAME matches one of the known main executable names. */ | |
1526 | ||
1527 | static int | |
bc043ef3 | 1528 | match_main (const char *soname) |
13437d4b | 1529 | { |
bc043ef3 | 1530 | const char * const *mainp; |
13437d4b KB |
1531 | |
1532 | for (mainp = main_name_list; *mainp != NULL; mainp++) | |
1533 | { | |
1534 | if (strcmp (soname, *mainp) == 0) | |
1535 | return (1); | |
1536 | } | |
1537 | ||
1538 | return (0); | |
1539 | } | |
1540 | ||
13437d4b KB |
1541 | /* Return 1 if PC lies in the dynamic symbol resolution code of the |
1542 | SVR4 run time loader. */ | |
13437d4b | 1543 | |
7d522c90 | 1544 | int |
d7fa2ae2 | 1545 | svr4_in_dynsym_resolve_code (CORE_ADDR pc) |
13437d4b | 1546 | { |
d70cc3ba | 1547 | struct svr4_info *info = get_svr4_info (current_program_space); |
6c95b8df PA |
1548 | |
1549 | return ((pc >= info->interp_text_sect_low | |
1550 | && pc < info->interp_text_sect_high) | |
1551 | || (pc >= info->interp_plt_sect_low | |
1552 | && pc < info->interp_plt_sect_high) | |
3e5d3a5a | 1553 | || in_plt_section (pc) |
0875794a | 1554 | || in_gnu_ifunc_stub (pc)); |
13437d4b | 1555 | } |
13437d4b | 1556 | |
2f4950cd AC |
1557 | /* Given an executable's ABFD and target, compute the entry-point |
1558 | address. */ | |
1559 | ||
1560 | static CORE_ADDR | |
1561 | exec_entry_point (struct bfd *abfd, struct target_ops *targ) | |
1562 | { | |
8c2b9656 YQ |
1563 | CORE_ADDR addr; |
1564 | ||
2f4950cd AC |
1565 | /* KevinB wrote ... for most targets, the address returned by |
1566 | bfd_get_start_address() is the entry point for the start | |
1567 | function. But, for some targets, bfd_get_start_address() returns | |
1568 | the address of a function descriptor from which the entry point | |
1569 | address may be extracted. This address is extracted by | |
1570 | gdbarch_convert_from_func_ptr_addr(). The method | |
1571 | gdbarch_convert_from_func_ptr_addr() is the merely the identify | |
1572 | function for targets which don't use function descriptors. */ | |
99d9c3b9 | 1573 | addr = gdbarch_convert_from_func_ptr_addr (current_inferior ()->arch (), |
2f4950cd AC |
1574 | bfd_get_start_address (abfd), |
1575 | targ); | |
99d9c3b9 | 1576 | return gdbarch_addr_bits_remove (current_inferior ()->arch (), addr); |
2f4950cd | 1577 | } |
13437d4b | 1578 | |
f9e14852 GB |
1579 | /* A probe and its associated action. */ |
1580 | ||
1581 | struct probe_and_action | |
1582 | { | |
1583 | /* The probe. */ | |
935676c9 | 1584 | probe *prob; |
f9e14852 | 1585 | |
729662a5 TT |
1586 | /* The relocated address of the probe. */ |
1587 | CORE_ADDR address; | |
1588 | ||
f9e14852 GB |
1589 | /* The action. */ |
1590 | enum probe_action action; | |
7905fc35 PA |
1591 | |
1592 | /* The objfile where this probe was found. */ | |
1593 | struct objfile *objfile; | |
f9e14852 GB |
1594 | }; |
1595 | ||
1596 | /* Returns a hash code for the probe_and_action referenced by p. */ | |
1597 | ||
1598 | static hashval_t | |
1599 | hash_probe_and_action (const void *p) | |
1600 | { | |
19ba03f4 | 1601 | const struct probe_and_action *pa = (const struct probe_and_action *) p; |
f9e14852 | 1602 | |
729662a5 | 1603 | return (hashval_t) pa->address; |
f9e14852 GB |
1604 | } |
1605 | ||
1606 | /* Returns non-zero if the probe_and_actions referenced by p1 and p2 | |
1607 | are equal. */ | |
1608 | ||
1609 | static int | |
1610 | equal_probe_and_action (const void *p1, const void *p2) | |
1611 | { | |
19ba03f4 SM |
1612 | const struct probe_and_action *pa1 = (const struct probe_and_action *) p1; |
1613 | const struct probe_and_action *pa2 = (const struct probe_and_action *) p2; | |
f9e14852 | 1614 | |
729662a5 | 1615 | return pa1->address == pa2->address; |
f9e14852 GB |
1616 | } |
1617 | ||
7905fc35 PA |
1618 | /* Traversal function for probes_table_remove_objfile_probes. */ |
1619 | ||
1620 | static int | |
1621 | probes_table_htab_remove_objfile_probes (void **slot, void *info) | |
1622 | { | |
1623 | probe_and_action *pa = (probe_and_action *) *slot; | |
1624 | struct objfile *objfile = (struct objfile *) info; | |
1625 | ||
1626 | if (pa->objfile == objfile) | |
09232438 TT |
1627 | htab_clear_slot (get_svr4_info (objfile->pspace)->probes_table.get (), |
1628 | slot); | |
7905fc35 PA |
1629 | |
1630 | return 1; | |
1631 | } | |
1632 | ||
1633 | /* Remove all probes that belong to OBJFILE from the probes table. */ | |
1634 | ||
1635 | static void | |
1636 | probes_table_remove_objfile_probes (struct objfile *objfile) | |
1637 | { | |
d70cc3ba | 1638 | svr4_info *info = get_svr4_info (objfile->pspace); |
7905fc35 | 1639 | if (info->probes_table != nullptr) |
09232438 | 1640 | htab_traverse_noresize (info->probes_table.get (), |
7905fc35 PA |
1641 | probes_table_htab_remove_objfile_probes, objfile); |
1642 | } | |
1643 | ||
f9e14852 GB |
1644 | /* Register a solib event probe and its associated action in the |
1645 | probes table. */ | |
1646 | ||
1647 | static void | |
d70cc3ba | 1648 | register_solib_event_probe (svr4_info *info, struct objfile *objfile, |
7905fc35 | 1649 | probe *prob, CORE_ADDR address, |
729662a5 | 1650 | enum probe_action action) |
f9e14852 | 1651 | { |
f9e14852 GB |
1652 | struct probe_and_action lookup, *pa; |
1653 | void **slot; | |
1654 | ||
1655 | /* Create the probes table, if necessary. */ | |
1656 | if (info->probes_table == NULL) | |
09232438 TT |
1657 | info->probes_table.reset (htab_create_alloc (1, hash_probe_and_action, |
1658 | equal_probe_and_action, | |
1659 | xfree, xcalloc, xfree)); | |
f9e14852 | 1660 | |
729662a5 | 1661 | lookup.address = address; |
09232438 | 1662 | slot = htab_find_slot (info->probes_table.get (), &lookup, INSERT); |
f9e14852 GB |
1663 | gdb_assert (*slot == HTAB_EMPTY_ENTRY); |
1664 | ||
1665 | pa = XCNEW (struct probe_and_action); | |
935676c9 | 1666 | pa->prob = prob; |
729662a5 | 1667 | pa->address = address; |
f9e14852 | 1668 | pa->action = action; |
7905fc35 | 1669 | pa->objfile = objfile; |
f9e14852 GB |
1670 | |
1671 | *slot = pa; | |
1672 | } | |
1673 | ||
1674 | /* Get the solib event probe at the specified location, and the | |
1675 | action associated with it. Returns NULL if no solib event probe | |
1676 | was found. */ | |
1677 | ||
1678 | static struct probe_and_action * | |
1679 | solib_event_probe_at (struct svr4_info *info, CORE_ADDR address) | |
1680 | { | |
f9e14852 GB |
1681 | struct probe_and_action lookup; |
1682 | void **slot; | |
1683 | ||
729662a5 | 1684 | lookup.address = address; |
09232438 | 1685 | slot = htab_find_slot (info->probes_table.get (), &lookup, NO_INSERT); |
f9e14852 GB |
1686 | |
1687 | if (slot == NULL) | |
1688 | return NULL; | |
1689 | ||
1690 | return (struct probe_and_action *) *slot; | |
1691 | } | |
1692 | ||
1693 | /* Decide what action to take when the specified solib event probe is | |
1694 | hit. */ | |
1695 | ||
1696 | static enum probe_action | |
1697 | solib_event_probe_action (struct probe_and_action *pa) | |
1698 | { | |
1699 | enum probe_action action; | |
73c6b475 | 1700 | unsigned probe_argc = 0; |
bd2b40ac | 1701 | frame_info_ptr frame = get_current_frame (); |
f9e14852 GB |
1702 | |
1703 | action = pa->action; | |
1704 | if (action == DO_NOTHING || action == PROBES_INTERFACE_FAILED) | |
1705 | return action; | |
1706 | ||
1707 | gdb_assert (action == FULL_RELOAD || action == UPDATE_OR_RELOAD); | |
1708 | ||
1709 | /* Check that an appropriate number of arguments has been supplied. | |
1710 | We expect: | |
1711 | arg0: Lmid_t lmid (mandatory) | |
1712 | arg1: struct r_debug *debug_base (mandatory) | |
1713 | arg2: struct link_map *new (optional, for incremental updates) */ | |
a70b8144 | 1714 | try |
3bd7e5b7 | 1715 | { |
fe01123e | 1716 | probe_argc = pa->prob->get_argument_count (get_frame_arch (frame)); |
3bd7e5b7 | 1717 | } |
230d2906 | 1718 | catch (const gdb_exception_error &ex) |
3bd7e5b7 SDJ |
1719 | { |
1720 | exception_print (gdb_stderr, ex); | |
1721 | probe_argc = 0; | |
1722 | } | |
3bd7e5b7 | 1723 | |
935676c9 SDJ |
1724 | /* If get_argument_count throws an exception, probe_argc will be set |
1725 | to zero. However, if pa->prob does not have arguments, then | |
1726 | get_argument_count will succeed but probe_argc will also be zero. | |
1727 | Both cases happen because of different things, but they are | |
1728 | treated equally here: action will be set to | |
3bd7e5b7 | 1729 | PROBES_INTERFACE_FAILED. */ |
f9e14852 GB |
1730 | if (probe_argc == 2) |
1731 | action = FULL_RELOAD; | |
1732 | else if (probe_argc < 2) | |
1733 | action = PROBES_INTERFACE_FAILED; | |
1734 | ||
1735 | return action; | |
1736 | } | |
1737 | ||
1738 | /* Populate the shared object list by reading the entire list of | |
1739 | shared objects from the inferior. Handle special cases relating | |
1740 | to the first elements of the list. Returns nonzero on success. */ | |
1741 | ||
1742 | static int | |
1743 | solist_update_full (struct svr4_info *info) | |
1744 | { | |
8d56636a | 1745 | svr4_current_sos_direct (info); |
f9e14852 GB |
1746 | |
1747 | return 1; | |
1748 | } | |
1749 | ||
1750 | /* Update the shared object list starting from the link-map entry | |
1751 | passed by the linker in the probe's third argument. Returns | |
1752 | nonzero if the list was successfully updated, or zero to indicate | |
1753 | failure. */ | |
1754 | ||
1755 | static int | |
8d56636a MM |
1756 | solist_update_incremental (svr4_info *info, CORE_ADDR debug_base, |
1757 | CORE_ADDR lm) | |
f9e14852 | 1758 | { |
f9e14852 GB |
1759 | /* Fall back to a full update if we are using a remote target |
1760 | that does not support incremental transfers. */ | |
1761 | if (info->using_xfer && !target_augmented_libraries_svr4_read ()) | |
1762 | return 0; | |
1763 | ||
8d56636a MM |
1764 | /* Fall back to a full update if we used the special namespace zero. We |
1765 | wouldn't be able to find the last item in the DEBUG_BASE namespace | |
1766 | and hence get the prev link wrong. */ | |
1767 | if (info->solib_lists.find (0) != info->solib_lists.end ()) | |
1768 | return 0; | |
1769 | ||
96bb3873 | 1770 | std::vector<svr4_so> &solist = info->solib_lists[debug_base]; |
8d56636a MM |
1771 | CORE_ADDR prev_lm; |
1772 | ||
96bb3873 | 1773 | if (solist.empty ()) |
8d56636a MM |
1774 | { |
1775 | /* svr4_current_sos_direct contains logic to handle a number of | |
1776 | special cases relating to the first elements of the list in | |
1777 | default namespace. To avoid duplicating this logic we defer to | |
1778 | solist_update_full in this case. */ | |
1779 | if (svr4_is_default_namespace (info, debug_base)) | |
1780 | return 0; | |
1781 | ||
1782 | prev_lm = 0; | |
8d56636a MM |
1783 | } |
1784 | else | |
96bb3873 | 1785 | prev_lm = solist.back ().lm_info->lm_addr; |
f9e14852 GB |
1786 | |
1787 | /* Read the new objects. */ | |
1788 | if (info->using_xfer) | |
1789 | { | |
1790 | struct svr4_library_list library_list; | |
1791 | char annex[64]; | |
1792 | ||
2733d9d5 MM |
1793 | /* Unknown key=value pairs are ignored by the gdbstub. */ |
1794 | xsnprintf (annex, sizeof (annex), "lmid=%s;start=%s;prev=%s", | |
1795 | phex_nz (debug_base, sizeof (debug_base)), | |
f9e14852 GB |
1796 | phex_nz (lm, sizeof (lm)), |
1797 | phex_nz (prev_lm, sizeof (prev_lm))); | |
1798 | if (!svr4_current_sos_via_xfer_libraries (&library_list, annex)) | |
1799 | return 0; | |
1800 | ||
8d56636a | 1801 | /* Get the so list from the target. We replace the list in the |
287de656 SM |
1802 | target response so we can easily check that the response only |
1803 | covers one namespace. | |
8d56636a MM |
1804 | |
1805 | We expect gdbserver to provide updates for the namespace that | |
33b5899f | 1806 | contains LM, which would be this namespace... */ |
96bb3873 SM |
1807 | std::vector<svr4_so> sos; |
1808 | auto it_debug_base = library_list.solib_lists.find (debug_base); | |
1809 | if (it_debug_base != library_list.solib_lists.end ()) | |
1810 | std::swap (sos, it_debug_base->second); | |
1811 | else | |
8d56636a MM |
1812 | { |
1813 | /* ...or for the special zero namespace for earlier versions... */ | |
96bb3873 SM |
1814 | auto it_0 = library_list.solib_lists.find (0); |
1815 | if (it_0 != library_list.solib_lists.end ()) | |
1816 | std::swap (sos, it_0->second); | |
8d56636a MM |
1817 | } |
1818 | ||
1819 | /* ...but nothing else. */ | |
96bb3873 SM |
1820 | for (const auto &tuple : library_list.solib_lists) |
1821 | gdb_assert (tuple.second.empty ()); | |
8d56636a | 1822 | |
96bb3873 | 1823 | std::move (sos.begin (), sos.end (), std::back_inserter (solist)); |
f9e14852 GB |
1824 | } |
1825 | else | |
1826 | { | |
f9e14852 GB |
1827 | /* IGNORE_FIRST may safely be set to zero here because the |
1828 | above check and deferral to solist_update_full ensures | |
1829 | that this call to svr4_read_so_list will never see the | |
1830 | first element. */ | |
96bb3873 | 1831 | if (!svr4_read_so_list (info, lm, prev_lm, solist, 0)) |
f9e14852 GB |
1832 | return 0; |
1833 | } | |
1834 | ||
1835 | return 1; | |
1836 | } | |
1837 | ||
1838 | /* Disable the probes-based linker interface and revert to the | |
1839 | original interface. We don't reset the breakpoints as the | |
1840 | ones set up for the probes-based interface are adequate. */ | |
1841 | ||
1842 | static void | |
d70cc3ba | 1843 | disable_probes_interface (svr4_info *info) |
f9e14852 | 1844 | { |
f9e14852 | 1845 | warning (_("Probes-based dynamic linker interface failed.\n" |
422186a9 | 1846 | "Reverting to original interface.")); |
f9e14852 GB |
1847 | |
1848 | free_probes_table (info); | |
96bb3873 | 1849 | info->solib_lists.clear (); |
f9e14852 GB |
1850 | } |
1851 | ||
1852 | /* Update the solib list as appropriate when using the | |
1853 | probes-based linker interface. Do nothing if using the | |
1854 | standard interface. */ | |
1855 | ||
1856 | static void | |
1857 | svr4_handle_solib_event (void) | |
1858 | { | |
d70cc3ba | 1859 | struct svr4_info *info = get_svr4_info (current_program_space); |
f9e14852 GB |
1860 | struct probe_and_action *pa; |
1861 | enum probe_action action; | |
ad1c917a | 1862 | struct value *val = NULL; |
f9e14852 | 1863 | CORE_ADDR pc, debug_base, lm = 0; |
bd2b40ac | 1864 | frame_info_ptr frame = get_current_frame (); |
f9e14852 GB |
1865 | |
1866 | /* Do nothing if not using the probes interface. */ | |
1867 | if (info->probes_table == NULL) | |
1868 | return; | |
1869 | ||
9c742269 | 1870 | pc = regcache_read_pc (get_thread_regcache (inferior_thread ())); |
17467c10 SM |
1871 | pa = solib_event_probe_at (info, pc); |
1872 | if (pa == nullptr) | |
1873 | { | |
1874 | /* When some solib ops sits above us, it can respond to a solib event | |
1875 | by calling in here. This is done assuming that if the current event | |
1876 | is not an SVR4 solib event, calling here should be a no-op. */ | |
1877 | return; | |
1878 | } | |
1879 | ||
f9e14852 GB |
1880 | /* If anything goes wrong we revert to the original linker |
1881 | interface. */ | |
d70cc3ba SM |
1882 | auto cleanup = make_scope_exit ([info] () |
1883 | { | |
1884 | disable_probes_interface (info); | |
1885 | }); | |
f9e14852 | 1886 | |
f9e14852 GB |
1887 | action = solib_event_probe_action (pa); |
1888 | if (action == PROBES_INTERFACE_FAILED) | |
d01c5877 | 1889 | return; |
f9e14852 GB |
1890 | |
1891 | if (action == DO_NOTHING) | |
1892 | { | |
d01c5877 | 1893 | cleanup.release (); |
f9e14852 GB |
1894 | return; |
1895 | } | |
1896 | ||
935676c9 | 1897 | /* evaluate_argument looks up symbols in the dynamic linker |
f9e14852 GB |
1898 | using find_pc_section. find_pc_section is accelerated by a cache |
1899 | called the section map. The section map is invalidated every | |
1900 | time a shared library is loaded or unloaded, and if the inferior | |
1901 | is generating a lot of shared library events then the section map | |
1902 | will be updated every time svr4_handle_solib_event is called. | |
1903 | We called find_pc_section in svr4_create_solib_event_breakpoints, | |
1904 | so we can guarantee that the dynamic linker's sections are in the | |
1905 | section map. We can therefore inhibit section map updates across | |
935676c9 | 1906 | these calls to evaluate_argument and save a lot of time. */ |
06424eac TT |
1907 | { |
1908 | scoped_restore inhibit_updates | |
1909 | = inhibit_section_map_updates (current_program_space); | |
f9e14852 | 1910 | |
a70b8144 | 1911 | try |
06424eac TT |
1912 | { |
1913 | val = pa->prob->evaluate_argument (1, frame); | |
1914 | } | |
230d2906 | 1915 | catch (const gdb_exception_error &ex) |
06424eac TT |
1916 | { |
1917 | exception_print (gdb_stderr, ex); | |
1918 | val = NULL; | |
1919 | } | |
f9e14852 | 1920 | |
06424eac | 1921 | if (val == NULL) |
d01c5877 | 1922 | return; |
f9e14852 | 1923 | |
06424eac TT |
1924 | debug_base = value_as_address (val); |
1925 | if (debug_base == 0) | |
d01c5877 | 1926 | return; |
f9e14852 | 1927 | |
8d56636a MM |
1928 | /* If the global _r_debug object moved, we need to reload everything |
1929 | since we cannot identify namespaces (by the location of their | |
1930 | r_debug_ext object) anymore. */ | |
1931 | CORE_ADDR global_debug_base = elf_locate_base (); | |
1932 | if (global_debug_base != info->debug_base) | |
1933 | { | |
1934 | info->debug_base = global_debug_base; | |
1935 | action = FULL_RELOAD; | |
1936 | } | |
1937 | ||
60d09f0a | 1938 | if (info->debug_base == 0) |
cb736441 GB |
1939 | { |
1940 | /* It's possible for the reloc_complete probe to be triggered before | |
1941 | the linker has set the DT_DEBUG pointer (for example, when the | |
1942 | linker has finished relocating an LD_AUDIT library or its | |
1943 | dependencies). Since we can't yet handle libraries from other link | |
1944 | namespaces, we don't lose anything by ignoring them here. */ | |
1945 | struct value *link_map_id_val; | |
1946 | try | |
1947 | { | |
1948 | link_map_id_val = pa->prob->evaluate_argument (0, frame); | |
1949 | } | |
1950 | catch (const gdb_exception_error) | |
1951 | { | |
1952 | link_map_id_val = NULL; | |
1953 | } | |
1954 | /* glibc and illumos' libc both define LM_ID_BASE as zero. */ | |
1955 | if (link_map_id_val != NULL && value_as_long (link_map_id_val) != 0) | |
1956 | action = DO_NOTHING; | |
1957 | else | |
1958 | return; | |
1959 | } | |
3bd7e5b7 | 1960 | |
06424eac TT |
1961 | if (action == UPDATE_OR_RELOAD) |
1962 | { | |
a70b8144 | 1963 | try |
06424eac TT |
1964 | { |
1965 | val = pa->prob->evaluate_argument (2, frame); | |
1966 | } | |
230d2906 | 1967 | catch (const gdb_exception_error &ex) |
06424eac TT |
1968 | { |
1969 | exception_print (gdb_stderr, ex); | |
06424eac TT |
1970 | return; |
1971 | } | |
06424eac TT |
1972 | |
1973 | if (val != NULL) | |
1974 | lm = value_as_address (val); | |
1975 | ||
1976 | if (lm == 0) | |
1977 | action = FULL_RELOAD; | |
1978 | } | |
f9e14852 | 1979 | |
06424eac TT |
1980 | /* Resume section map updates. Closing the scope is |
1981 | sufficient. */ | |
1982 | } | |
f9e14852 GB |
1983 | |
1984 | if (action == UPDATE_OR_RELOAD) | |
1985 | { | |
8d56636a | 1986 | if (!solist_update_incremental (info, debug_base, lm)) |
f9e14852 GB |
1987 | action = FULL_RELOAD; |
1988 | } | |
1989 | ||
1990 | if (action == FULL_RELOAD) | |
1991 | { | |
1992 | if (!solist_update_full (info)) | |
d01c5877 | 1993 | return; |
f9e14852 GB |
1994 | } |
1995 | ||
d01c5877 | 1996 | cleanup.release (); |
f9e14852 GB |
1997 | } |
1998 | ||
1999 | /* Helper function for svr4_update_solib_event_breakpoints. */ | |
2000 | ||
95da600f CB |
2001 | static bool |
2002 | svr4_update_solib_event_breakpoint (struct breakpoint *b) | |
f9e14852 | 2003 | { |
f9e14852 GB |
2004 | if (b->type != bp_shlib_event) |
2005 | { | |
2006 | /* Continue iterating. */ | |
95da600f | 2007 | return false; |
f9e14852 GB |
2008 | } |
2009 | ||
b00b30b2 | 2010 | for (bp_location &loc : b->locations ()) |
f9e14852 GB |
2011 | { |
2012 | struct svr4_info *info; | |
2013 | struct probe_and_action *pa; | |
2014 | ||
b00b30b2 | 2015 | info = solib_svr4_pspace_data.get (loc.pspace); |
f9e14852 GB |
2016 | if (info == NULL || info->probes_table == NULL) |
2017 | continue; | |
2018 | ||
b00b30b2 | 2019 | pa = solib_event_probe_at (info, loc.address); |
f9e14852 GB |
2020 | if (pa == NULL) |
2021 | continue; | |
2022 | ||
2023 | if (pa->action == DO_NOTHING) | |
2024 | { | |
2025 | if (b->enable_state == bp_disabled && stop_on_solib_events) | |
2026 | enable_breakpoint (b); | |
2027 | else if (b->enable_state == bp_enabled && !stop_on_solib_events) | |
2028 | disable_breakpoint (b); | |
2029 | } | |
2030 | ||
2031 | break; | |
2032 | } | |
2033 | ||
2034 | /* Continue iterating. */ | |
95da600f | 2035 | return false; |
f9e14852 GB |
2036 | } |
2037 | ||
2038 | /* Enable or disable optional solib event breakpoints as appropriate. | |
2039 | Called whenever stop_on_solib_events is changed. */ | |
2040 | ||
2041 | static void | |
2042 | svr4_update_solib_event_breakpoints (void) | |
2043 | { | |
a1decfc1 SM |
2044 | for (breakpoint &bp : all_breakpoints_safe ()) |
2045 | svr4_update_solib_event_breakpoint (&bp); | |
f9e14852 GB |
2046 | } |
2047 | ||
2048 | /* Create and register solib event breakpoints. PROBES is an array | |
2049 | of NUM_PROBES elements, each of which is vector of probes. A | |
2050 | solib event breakpoint will be created and registered for each | |
2051 | probe. */ | |
2052 | ||
2053 | static void | |
d70cc3ba | 2054 | svr4_create_probe_breakpoints (svr4_info *info, struct gdbarch *gdbarch, |
45461e0d | 2055 | const std::vector<probe *> *probes, |
729662a5 | 2056 | struct objfile *objfile) |
f9e14852 | 2057 | { |
45461e0d | 2058 | for (int i = 0; i < NUM_PROBES; i++) |
f9e14852 GB |
2059 | { |
2060 | enum probe_action action = probe_info[i].action; | |
f9e14852 | 2061 | |
45461e0d | 2062 | for (probe *p : probes[i]) |
f9e14852 | 2063 | { |
935676c9 | 2064 | CORE_ADDR address = p->get_relocated_address (objfile); |
729662a5 | 2065 | |
f8baaa2b SM |
2066 | solib_debug_printf ("name=%s, addr=%s", probe_info[i].name, |
2067 | paddress (gdbarch, address)); | |
2068 | ||
729662a5 | 2069 | create_solib_event_breakpoint (gdbarch, address); |
d70cc3ba | 2070 | register_solib_event_probe (info, objfile, p, address, action); |
f9e14852 GB |
2071 | } |
2072 | } | |
2073 | ||
2074 | svr4_update_solib_event_breakpoints (); | |
2075 | } | |
2076 | ||
e661ef01 AH |
2077 | /* Find all the glibc named probes. Only if all of the probes are found, then |
2078 | create them and return true. Otherwise return false. If WITH_PREFIX is set | |
2079 | then add "rtld" to the front of the probe names. */ | |
2080 | static bool | |
2081 | svr4_find_and_create_probe_breakpoints (svr4_info *info, | |
2082 | struct gdbarch *gdbarch, | |
2083 | struct obj_section *os, | |
2084 | bool with_prefix) | |
2085 | { | |
f8baaa2b SM |
2086 | SOLIB_SCOPED_DEBUG_START_END ("objfile=%s, with_prefix=%d", |
2087 | os->objfile->original_name, with_prefix); | |
2088 | ||
e661ef01 | 2089 | std::vector<probe *> probes[NUM_PROBES]; |
e661ef01 AH |
2090 | |
2091 | for (int i = 0; i < NUM_PROBES; i++) | |
2092 | { | |
2093 | const char *name = probe_info[i].name; | |
2094 | char buf[32]; | |
2095 | ||
2096 | /* Fedora 17 and Red Hat Enterprise Linux 6.2-6.4 shipped with an early | |
2097 | version of the probes code in which the probes' names were prefixed | |
2098 | with "rtld_" and the "map_failed" probe did not exist. The locations | |
2099 | of the probes are otherwise the same, so we check for probes with | |
2100 | prefixed names if probes with unprefixed names are not present. */ | |
2101 | if (with_prefix) | |
2102 | { | |
2103 | xsnprintf (buf, sizeof (buf), "rtld_%s", name); | |
2104 | name = buf; | |
2105 | } | |
2106 | ||
2107 | probes[i] = find_probes_in_objfile (os->objfile, "rtld", name); | |
f8baaa2b | 2108 | solib_debug_printf ("probe=%s, num found=%zu", name, probes[i].size ()); |
e661ef01 | 2109 | |
e661ef01 AH |
2110 | /* Ensure at least one probe for the current name was found. */ |
2111 | if (probes[i].empty ()) | |
79d40365 AB |
2112 | { |
2113 | /* The "map_failed" probe did not exist in early versions of the | |
2114 | probes code in which the probes' names were prefixed with | |
2115 | "rtld_". | |
2116 | ||
2117 | Additionally, the "map_failed" probe was accidentally removed | |
2118 | from glibc 2.35 and 2.36, when changes in glibc meant the | |
2119 | probe could no longer be reached, and the compiler optimized | |
2120 | the probe away. In this case the probe name doesn't have the | |
2121 | "rtld_" prefix. | |
2122 | ||
2123 | To handle this, and give GDB as much flexibility as possible, | |
2124 | we make the rule that, if a probe isn't required for the | |
2125 | correct operation of GDB (i.e. its action is DO_NOTHING), then | |
2126 | we will still use the probes interface, even if that probe is | |
2127 | missing. | |
2128 | ||
2129 | The only (possible) downside of this is that, if the user has | |
2130 | 'set stop-on-solib-events on' in effect, then they might get | |
2131 | fewer events using the probes interface than with the classic | |
2132 | non-probes interface. */ | |
2133 | if (probe_info[i].action == DO_NOTHING) | |
2134 | continue; | |
2135 | else | |
2136 | return false; | |
2137 | } | |
e661ef01 AH |
2138 | |
2139 | /* Ensure probe arguments can be evaluated. */ | |
d90b8f26 | 2140 | for (probe *p : probes[i]) |
e661ef01 | 2141 | { |
e661ef01 AH |
2142 | if (!p->can_evaluate_arguments ()) |
2143 | return false; | |
d90b8f26 AH |
2144 | /* This will fail if the probe is invalid. This has been seen on Arm |
2145 | due to references to symbols that have been resolved away. */ | |
2146 | try | |
2147 | { | |
2148 | p->get_argument_count (gdbarch); | |
2149 | } | |
2150 | catch (const gdb_exception_error &ex) | |
2151 | { | |
2152 | exception_print (gdb_stderr, ex); | |
2153 | warning (_("Initializing probes-based dynamic linker interface " | |
2154 | "failed.\nReverting to original interface.")); | |
2155 | return false; | |
2156 | } | |
e661ef01 AH |
2157 | } |
2158 | } | |
2159 | ||
2160 | /* All probes found. Now create them. */ | |
f8baaa2b | 2161 | solib_debug_printf ("using probes interface"); |
e661ef01 AH |
2162 | svr4_create_probe_breakpoints (info, gdbarch, probes, os->objfile); |
2163 | return true; | |
2164 | } | |
2165 | ||
f9e14852 GB |
2166 | /* Both the SunOS and the SVR4 dynamic linkers call a marker function |
2167 | before and after mapping and unmapping shared libraries. The sole | |
2168 | purpose of this method is to allow debuggers to set a breakpoint so | |
2169 | they can track these changes. | |
2170 | ||
2171 | Some versions of the glibc dynamic linker contain named probes | |
2172 | to allow more fine grained stopping. Given the address of the | |
2173 | original marker function, this function attempts to find these | |
2174 | probes, and if found, sets breakpoints on those instead. If the | |
2175 | probes aren't found, a single breakpoint is set on the original | |
2176 | marker function. */ | |
2177 | ||
2178 | static void | |
d70cc3ba | 2179 | svr4_create_solib_event_breakpoints (svr4_info *info, struct gdbarch *gdbarch, |
f9e14852 GB |
2180 | CORE_ADDR address) |
2181 | { | |
e661ef01 | 2182 | struct obj_section *os = find_pc_section (address); |
f9e14852 | 2183 | |
e661ef01 AH |
2184 | if (os == nullptr |
2185 | || (!svr4_find_and_create_probe_breakpoints (info, gdbarch, os, false) | |
2186 | && !svr4_find_and_create_probe_breakpoints (info, gdbarch, os, true))) | |
f8baaa2b SM |
2187 | { |
2188 | solib_debug_printf ("falling back to r_brk breakpoint: addr=%s", | |
2189 | paddress (gdbarch, address)); | |
2190 | create_solib_event_breakpoint (gdbarch, address); | |
2191 | } | |
f9e14852 GB |
2192 | } |
2193 | ||
7f86f058 | 2194 | /* Arrange for dynamic linker to hit breakpoint. |
13437d4b KB |
2195 | |
2196 | Both the SunOS and the SVR4 dynamic linkers have, as part of their | |
2197 | debugger interface, support for arranging for the inferior to hit | |
2198 | a breakpoint after mapping in the shared libraries. This function | |
2199 | enables that breakpoint. | |
2200 | ||
2201 | For SunOS, there is a special flag location (in_debugger) which we | |
2202 | set to 1. When the dynamic linker sees this flag set, it will set | |
2203 | a breakpoint at a location known only to itself, after saving the | |
2204 | original contents of that place and the breakpoint address itself, | |
f4afd6cb | 2205 | in its own internal structures. When we resume the inferior, it |
13437d4b KB |
2206 | will eventually take a SIGTRAP when it runs into the breakpoint. |
2207 | We handle this (in a different place) by restoring the contents of | |
2208 | the breakpointed location (which is only known after it stops), | |
2209 | chasing around to locate the shared libraries that have been | |
2210 | loaded, then resuming. | |
2211 | ||
2212 | For SVR4, the debugger interface structure contains a member (r_brk) | |
2213 | which is statically initialized at the time the shared library is | |
2214 | built, to the offset of a function (_r_debug_state) which is guaran- | |
2215 | teed to be called once before mapping in a library, and again when | |
2216 | the mapping is complete. At the time we are examining this member, | |
2217 | it contains only the unrelocated offset of the function, so we have | |
2218 | to do our own relocation. Later, when the dynamic linker actually | |
2219 | runs, it relocates r_brk to be the actual address of _r_debug_state(). | |
2220 | ||
2221 | The debugger interface structure also contains an enumeration which | |
2222 | is set to either RT_ADD or RT_DELETE prior to changing the mapping, | |
2223 | depending upon whether or not the library is being mapped or unmapped, | |
7f86f058 | 2224 | and then set to RT_CONSISTENT after the library is mapped/unmapped. */ |
13437d4b KB |
2225 | |
2226 | static int | |
268a4a75 | 2227 | enable_break (struct svr4_info *info, int from_tty) |
13437d4b | 2228 | { |
3b7344d5 | 2229 | struct bound_minimal_symbol msymbol; |
bc043ef3 | 2230 | const char * const *bkpt_namep; |
13437d4b | 2231 | asection *interp_sect; |
7cd25cfc | 2232 | CORE_ADDR sym_addr; |
13437d4b | 2233 | |
6c95b8df PA |
2234 | info->interp_text_sect_low = info->interp_text_sect_high = 0; |
2235 | info->interp_plt_sect_low = info->interp_plt_sect_high = 0; | |
13437d4b | 2236 | |
7cd25cfc DJ |
2237 | /* If we already have a shared library list in the target, and |
2238 | r_debug contains r_brk, set the breakpoint there - this should | |
2239 | mean r_brk has already been relocated. Assume the dynamic linker | |
2240 | is the object containing r_brk. */ | |
2241 | ||
e696b3ad | 2242 | solib_add (NULL, from_tty, auto_solib_add); |
7cd25cfc | 2243 | sym_addr = 0; |
8d56636a | 2244 | if (info->debug_base && solib_svr4_r_map (info->debug_base) != 0) |
1a816a87 | 2245 | sym_addr = solib_svr4_r_brk (info); |
7cd25cfc DJ |
2246 | |
2247 | if (sym_addr != 0) | |
2248 | { | |
2249 | struct obj_section *os; | |
2250 | ||
b36ec657 | 2251 | sym_addr = gdbarch_addr_bits_remove |
99d9c3b9 | 2252 | (current_inferior ()->arch (), |
328d42d8 | 2253 | gdbarch_convert_from_func_ptr_addr |
99d9c3b9 SM |
2254 | (current_inferior ()->arch (), sym_addr, |
2255 | current_inferior ()->top_target ())); | |
b36ec657 | 2256 | |
48379de6 DE |
2257 | /* On at least some versions of Solaris there's a dynamic relocation |
2258 | on _r_debug.r_brk and SYM_ADDR may not be relocated yet, e.g., if | |
2259 | we get control before the dynamic linker has self-relocated. | |
2260 | Check if SYM_ADDR is in a known section, if it is assume we can | |
2261 | trust its value. This is just a heuristic though, it could go away | |
2262 | or be replaced if it's getting in the way. | |
2263 | ||
2264 | On ARM we need to know whether the ISA of rtld_db_dlactivity (or | |
2265 | however it's spelled in your particular system) is ARM or Thumb. | |
2266 | That knowledge is encoded in the address, if it's Thumb the low bit | |
2267 | is 1. However, we've stripped that info above and it's not clear | |
2268 | what all the consequences are of passing a non-addr_bits_remove'd | |
f9e14852 | 2269 | address to svr4_create_solib_event_breakpoints. The call to |
48379de6 DE |
2270 | find_pc_section verifies we know about the address and have some |
2271 | hope of computing the right kind of breakpoint to use (via | |
2272 | symbol info). It does mean that GDB needs to be pointed at a | |
2273 | non-stripped version of the dynamic linker in order to obtain | |
2274 | information it already knows about. Sigh. */ | |
2275 | ||
7cd25cfc DJ |
2276 | os = find_pc_section (sym_addr); |
2277 | if (os != NULL) | |
2278 | { | |
2279 | /* Record the relocated start and end address of the dynamic linker | |
2280 | text and plt section for svr4_in_dynsym_resolve_code. */ | |
2281 | bfd *tmp_bfd; | |
2282 | CORE_ADDR load_addr; | |
2283 | ||
98badbfd | 2284 | tmp_bfd = os->objfile->obfd.get (); |
b3b3bada | 2285 | load_addr = os->objfile->text_section_offset (); |
7cd25cfc DJ |
2286 | |
2287 | interp_sect = bfd_get_section_by_name (tmp_bfd, ".text"); | |
2288 | if (interp_sect) | |
2289 | { | |
fd361982 AM |
2290 | info->interp_text_sect_low |
2291 | = bfd_section_vma (interp_sect) + load_addr; | |
2292 | info->interp_text_sect_high | |
2293 | = info->interp_text_sect_low + bfd_section_size (interp_sect); | |
7cd25cfc DJ |
2294 | } |
2295 | interp_sect = bfd_get_section_by_name (tmp_bfd, ".plt"); | |
2296 | if (interp_sect) | |
2297 | { | |
fd361982 AM |
2298 | info->interp_plt_sect_low |
2299 | = bfd_section_vma (interp_sect) + load_addr; | |
2300 | info->interp_plt_sect_high | |
2301 | = info->interp_plt_sect_low + bfd_section_size (interp_sect); | |
7cd25cfc DJ |
2302 | } |
2303 | ||
99d9c3b9 SM |
2304 | svr4_create_solib_event_breakpoints |
2305 | (info, current_inferior ()->arch (), sym_addr); | |
7cd25cfc DJ |
2306 | return 1; |
2307 | } | |
2308 | } | |
2309 | ||
97ec2c2f | 2310 | /* Find the program interpreter; if not found, warn the user and drop |
13437d4b | 2311 | into the old breakpoint at symbol code. */ |
6b09f134 | 2312 | std::optional<gdb::byte_vector> interp_name_holder |
17658d46 SM |
2313 | = find_program_interpreter (); |
2314 | if (interp_name_holder) | |
13437d4b | 2315 | { |
17658d46 | 2316 | const char *interp_name = (const char *) interp_name_holder->data (); |
8ad2fcde KB |
2317 | CORE_ADDR load_addr = 0; |
2318 | int load_addr_found = 0; | |
2ec9a4f8 | 2319 | int loader_found_in_list = 0; |
57685738 | 2320 | target_ops_up tmp_bfd_target; |
13437d4b | 2321 | |
7cd25cfc | 2322 | sym_addr = 0; |
13437d4b KB |
2323 | |
2324 | /* Now we need to figure out where the dynamic linker was | |
dda83cd7 SM |
2325 | loaded so that we can load its symbols and place a breakpoint |
2326 | in the dynamic linker itself. | |
13437d4b | 2327 | |
dda83cd7 SM |
2328 | This address is stored on the stack. However, I've been unable |
2329 | to find any magic formula to find it for Solaris (appears to | |
2330 | be trivial on GNU/Linux). Therefore, we have to try an alternate | |
2331 | mechanism to find the dynamic linker's base address. */ | |
e4f7b8c8 | 2332 | |
192b62ce | 2333 | gdb_bfd_ref_ptr tmp_bfd; |
a70b8144 | 2334 | try |
dda83cd7 | 2335 | { |
97ec2c2f | 2336 | tmp_bfd = solib_bfd_open (interp_name); |
f1838a98 | 2337 | } |
230d2906 | 2338 | catch (const gdb_exception &ex) |
492d29ea PA |
2339 | { |
2340 | } | |
492d29ea | 2341 | |
13437d4b KB |
2342 | if (tmp_bfd == NULL) |
2343 | goto bkpt_at_symbol; | |
2344 | ||
2f4950cd | 2345 | /* Now convert the TMP_BFD into a target. That way target, as |
15908a11 TT |
2346 | well as BFD operations can be used. */ |
2347 | tmp_bfd_target = target_bfd_reopen (tmp_bfd); | |
2f4950cd | 2348 | |
f8766ec1 | 2349 | /* On a running target, we can get the dynamic linker's base |
dda83cd7 | 2350 | address from the shared library table. */ |
7b323785 | 2351 | for (const solib &so : current_program_space->solibs ()) |
8ad2fcde | 2352 | { |
8971d278 | 2353 | if (svr4_same_1 (interp_name, so.so_original_name.c_str ())) |
8ad2fcde KB |
2354 | { |
2355 | load_addr_found = 1; | |
2ec9a4f8 | 2356 | loader_found_in_list = 1; |
8971d278 | 2357 | load_addr = lm_addr_check (so, tmp_bfd.get ()); |
8ad2fcde KB |
2358 | break; |
2359 | } | |
8ad2fcde KB |
2360 | } |
2361 | ||
8d4e36ba | 2362 | /* If we were not able to find the base address of the loader |
dda83cd7 | 2363 | from our so_list, then try using the AT_BASE auxilliary entry. */ |
8d4e36ba | 2364 | if (!load_addr_found) |
82d23ca8 | 2365 | if (target_auxv_search (AT_BASE, &load_addr) > 0) |
ad3a0e5b | 2366 | { |
99d9c3b9 | 2367 | int addr_bit = gdbarch_addr_bit (current_inferior ()->arch ()); |
ad3a0e5b JK |
2368 | |
2369 | /* Ensure LOAD_ADDR has proper sign in its possible upper bits so | |
2370 | that `+ load_addr' will overflow CORE_ADDR width not creating | |
2371 | invalid addresses like 0x101234567 for 32bit inferiors on 64bit | |
2372 | GDB. */ | |
2373 | ||
d182d057 | 2374 | if (addr_bit < (sizeof (CORE_ADDR) * HOST_CHAR_BIT)) |
ad3a0e5b | 2375 | { |
d182d057 | 2376 | CORE_ADDR space_size = (CORE_ADDR) 1 << addr_bit; |
57685738 TT |
2377 | CORE_ADDR tmp_entry_point |
2378 | = exec_entry_point (tmp_bfd.get (), tmp_bfd_target.get ()); | |
ad3a0e5b JK |
2379 | |
2380 | gdb_assert (load_addr < space_size); | |
2381 | ||
2382 | /* TMP_ENTRY_POINT exceeding SPACE_SIZE would be for prelinked | |
2383 | 64bit ld.so with 32bit executable, it should not happen. */ | |
2384 | ||
2385 | if (tmp_entry_point < space_size | |
2386 | && tmp_entry_point + load_addr >= space_size) | |
2387 | load_addr -= space_size; | |
2388 | } | |
2389 | ||
2390 | load_addr_found = 1; | |
2391 | } | |
8d4e36ba | 2392 | |
8ad2fcde KB |
2393 | /* Otherwise we find the dynamic linker's base address by examining |
2394 | the current pc (which should point at the entry point for the | |
8d4e36ba JB |
2395 | dynamic linker) and subtracting the offset of the entry point. |
2396 | ||
dda83cd7 SM |
2397 | This is more fragile than the previous approaches, but is a good |
2398 | fallback method because it has actually been working well in | |
2399 | most cases. */ | |
8ad2fcde | 2400 | if (!load_addr_found) |
fb14de7b | 2401 | { |
74387712 SM |
2402 | regcache *regcache |
2403 | = get_thread_arch_regcache (current_inferior (), inferior_ptid, | |
99d9c3b9 | 2404 | current_inferior ()->arch ()); |
433759f7 | 2405 | |
fb14de7b | 2406 | load_addr = (regcache_read_pc (regcache) |
57685738 TT |
2407 | - exec_entry_point (tmp_bfd.get (), |
2408 | tmp_bfd_target.get ())); | |
fb14de7b | 2409 | } |
2ec9a4f8 DJ |
2410 | |
2411 | if (!loader_found_in_list) | |
34439770 | 2412 | { |
1a816a87 PA |
2413 | info->debug_loader_name = xstrdup (interp_name); |
2414 | info->debug_loader_offset_p = 1; | |
2415 | info->debug_loader_offset = load_addr; | |
e696b3ad | 2416 | solib_add (NULL, from_tty, auto_solib_add); |
34439770 | 2417 | } |
13437d4b KB |
2418 | |
2419 | /* Record the relocated start and end address of the dynamic linker | |
dda83cd7 | 2420 | text and plt section for svr4_in_dynsym_resolve_code. */ |
192b62ce | 2421 | interp_sect = bfd_get_section_by_name (tmp_bfd.get (), ".text"); |
13437d4b KB |
2422 | if (interp_sect) |
2423 | { | |
fd361982 AM |
2424 | info->interp_text_sect_low |
2425 | = bfd_section_vma (interp_sect) + load_addr; | |
2426 | info->interp_text_sect_high | |
2427 | = info->interp_text_sect_low + bfd_section_size (interp_sect); | |
13437d4b | 2428 | } |
192b62ce | 2429 | interp_sect = bfd_get_section_by_name (tmp_bfd.get (), ".plt"); |
13437d4b KB |
2430 | if (interp_sect) |
2431 | { | |
fd361982 AM |
2432 | info->interp_plt_sect_low |
2433 | = bfd_section_vma (interp_sect) + load_addr; | |
2434 | info->interp_plt_sect_high | |
2435 | = info->interp_plt_sect_low + bfd_section_size (interp_sect); | |
13437d4b KB |
2436 | } |
2437 | ||
2438 | /* Now try to set a breakpoint in the dynamic linker. */ | |
2439 | for (bkpt_namep = solib_break_names; *bkpt_namep != NULL; bkpt_namep++) | |
2440 | { | |
6b3a2759 TT |
2441 | sym_addr |
2442 | = (gdb_bfd_lookup_symbol | |
2443 | (tmp_bfd.get (), | |
2444 | [=] (const asymbol *sym) | |
2445 | { | |
2446 | return (strcmp (sym->name, *bkpt_namep) == 0 | |
2447 | && ((sym->section->flags & (SEC_CODE | SEC_DATA)) | |
2448 | != 0)); | |
2449 | })); | |
13437d4b KB |
2450 | if (sym_addr != 0) |
2451 | break; | |
2452 | } | |
2453 | ||
2bbe3cc1 DJ |
2454 | if (sym_addr != 0) |
2455 | /* Convert 'sym_addr' from a function pointer to an address. | |
2456 | Because we pass tmp_bfd_target instead of the current | |
2457 | target, this will always produce an unrelocated value. */ | |
99d9c3b9 SM |
2458 | sym_addr = gdbarch_convert_from_func_ptr_addr |
2459 | (current_inferior ()->arch (), sym_addr, | |
2460 | tmp_bfd_target.get ()); | |
13437d4b KB |
2461 | |
2462 | if (sym_addr != 0) | |
2463 | { | |
99d9c3b9 SM |
2464 | svr4_create_solib_event_breakpoints (info, |
2465 | current_inferior ()->arch (), | |
f9e14852 | 2466 | load_addr + sym_addr); |
13437d4b KB |
2467 | return 1; |
2468 | } | |
2469 | ||
2470 | /* For whatever reason we couldn't set a breakpoint in the dynamic | |
dda83cd7 | 2471 | linker. Warn and drop into the old code. */ |
13437d4b | 2472 | bkpt_at_symbol: |
82d03102 | 2473 | warning (_("Unable to find dynamic linker breakpoint function.\n" |
dda83cd7 SM |
2474 | "GDB will be unable to debug shared library initializers\n" |
2475 | "and track explicitly loaded dynamic code.")); | |
13437d4b | 2476 | } |
13437d4b | 2477 | |
e499d0f1 DJ |
2478 | /* Scan through the lists of symbols, trying to look up the symbol and |
2479 | set a breakpoint there. Terminate loop when we/if we succeed. */ | |
2480 | ||
a42d7dd8 | 2481 | objfile *objf = current_program_space->symfile_object_file; |
e499d0f1 DJ |
2482 | for (bkpt_namep = solib_break_names; *bkpt_namep != NULL; bkpt_namep++) |
2483 | { | |
a42d7dd8 | 2484 | msymbol = lookup_minimal_symbol (*bkpt_namep, NULL, objf); |
3b7344d5 | 2485 | if ((msymbol.minsym != NULL) |
4aeddc50 | 2486 | && (msymbol.value_address () != 0)) |
e499d0f1 | 2487 | { |
4aeddc50 | 2488 | sym_addr = msymbol.value_address (); |
328d42d8 | 2489 | sym_addr = gdbarch_convert_from_func_ptr_addr |
99d9c3b9 SM |
2490 | (current_inferior ()->arch (), sym_addr, |
2491 | current_inferior ()->top_target ()); | |
2492 | svr4_create_solib_event_breakpoints (info, | |
2493 | current_inferior ()->arch (), | |
d70cc3ba | 2494 | sym_addr); |
e499d0f1 DJ |
2495 | return 1; |
2496 | } | |
2497 | } | |
13437d4b | 2498 | |
17658d46 | 2499 | if (interp_name_holder && !current_inferior ()->attach_flag) |
13437d4b | 2500 | { |
c6490bf2 | 2501 | for (bkpt_namep = bkpt_names; *bkpt_namep != NULL; bkpt_namep++) |
13437d4b | 2502 | { |
a42d7dd8 | 2503 | msymbol = lookup_minimal_symbol (*bkpt_namep, NULL, objf); |
3b7344d5 | 2504 | if ((msymbol.minsym != NULL) |
4aeddc50 | 2505 | && (msymbol.value_address () != 0)) |
c6490bf2 | 2506 | { |
4aeddc50 | 2507 | sym_addr = msymbol.value_address (); |
328d42d8 | 2508 | sym_addr = gdbarch_convert_from_func_ptr_addr |
99d9c3b9 | 2509 | (current_inferior ()->arch (), sym_addr, |
328d42d8 | 2510 | current_inferior ()->top_target ()); |
99d9c3b9 SM |
2511 | svr4_create_solib_event_breakpoints |
2512 | (info, current_inferior ()->arch (), sym_addr); | |
c6490bf2 KB |
2513 | return 1; |
2514 | } | |
13437d4b KB |
2515 | } |
2516 | } | |
542c95c2 | 2517 | return 0; |
13437d4b KB |
2518 | } |
2519 | ||
d1012b8e | 2520 | /* Read the ELF program headers from ABFD. */ |
e2a44558 | 2521 | |
6b09f134 | 2522 | static std::optional<gdb::byte_vector> |
d1012b8e | 2523 | read_program_headers_from_bfd (bfd *abfd) |
e2a44558 | 2524 | { |
d1012b8e SM |
2525 | Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd); |
2526 | int phdrs_size = ehdr->e_phnum * ehdr->e_phentsize; | |
2527 | if (phdrs_size == 0) | |
2528 | return {}; | |
09919ac2 | 2529 | |
d1012b8e | 2530 | gdb::byte_vector buf (phdrs_size); |
09919ac2 | 2531 | if (bfd_seek (abfd, ehdr->e_phoff, SEEK_SET) != 0 |
226f9f4f | 2532 | || bfd_read (buf.data (), phdrs_size, abfd) != phdrs_size) |
d1012b8e | 2533 | return {}; |
09919ac2 JK |
2534 | |
2535 | return buf; | |
b8040f19 JK |
2536 | } |
2537 | ||
01c30d6e JK |
2538 | /* Return 1 and fill *DISPLACEMENTP with detected PIE offset of inferior |
2539 | exec_bfd. Otherwise return 0. | |
2540 | ||
2541 | We relocate all of the sections by the same amount. This | |
c378eb4e | 2542 | behavior is mandated by recent editions of the System V ABI. |
b8040f19 JK |
2543 | According to the System V Application Binary Interface, |
2544 | Edition 4.1, page 5-5: | |
2545 | ||
2546 | ... Though the system chooses virtual addresses for | |
2547 | individual processes, it maintains the segments' relative | |
2548 | positions. Because position-independent code uses relative | |
85102364 | 2549 | addressing between segments, the difference between |
b8040f19 JK |
2550 | virtual addresses in memory must match the difference |
2551 | between virtual addresses in the file. The difference | |
2552 | between the virtual address of any segment in memory and | |
2553 | the corresponding virtual address in the file is thus a | |
2554 | single constant value for any one executable or shared | |
2555 | object in a given process. This difference is the base | |
2556 | address. One use of the base address is to relocate the | |
2557 | memory image of the program during dynamic linking. | |
2558 | ||
2559 | The same language also appears in Edition 4.0 of the System V | |
09919ac2 JK |
2560 | ABI and is left unspecified in some of the earlier editions. |
2561 | ||
2562 | Decide if the objfile needs to be relocated. As indicated above, we will | |
2563 | only be here when execution is stopped. But during attachment PC can be at | |
2564 | arbitrary address therefore regcache_read_pc can be misleading (contrary to | |
2565 | the auxv AT_ENTRY value). Moreover for executable with interpreter section | |
2566 | regcache_read_pc would point to the interpreter and not the main executable. | |
2567 | ||
2568 | So, to summarize, relocations are necessary when the start address obtained | |
2569 | from the executable is different from the address in auxv AT_ENTRY entry. | |
d989b283 | 2570 | |
09919ac2 JK |
2571 | [ The astute reader will note that we also test to make sure that |
2572 | the executable in question has the DYNAMIC flag set. It is my | |
2573 | opinion that this test is unnecessary (undesirable even). It | |
2574 | was added to avoid inadvertent relocation of an executable | |
2575 | whose e_type member in the ELF header is not ET_DYN. There may | |
2576 | be a time in the future when it is desirable to do relocations | |
2577 | on other types of files as well in which case this condition | |
33b5899f | 2578 | should either be removed or modified to accommodate the new file |
09919ac2 | 2579 | type. - Kevin, Nov 2000. ] */ |
b8040f19 | 2580 | |
01c30d6e JK |
2581 | static int |
2582 | svr4_exec_displacement (CORE_ADDR *displacementp) | |
b8040f19 | 2583 | { |
41752192 JK |
2584 | /* ENTRY_POINT is a possible function descriptor - before |
2585 | a call to gdbarch_convert_from_func_ptr_addr. */ | |
8f61baf8 | 2586 | CORE_ADDR entry_point, exec_displacement; |
b8040f19 | 2587 | |
7e10abd1 | 2588 | if (current_program_space->exec_bfd () == NULL) |
b8040f19 JK |
2589 | return 0; |
2590 | ||
09919ac2 JK |
2591 | /* Therefore for ELF it is ET_EXEC and not ET_DYN. Both shared libraries |
2592 | being executed themselves and PIE (Position Independent Executable) | |
2593 | executables are ET_DYN. */ | |
2594 | ||
7e10abd1 | 2595 | if ((bfd_get_file_flags (current_program_space->exec_bfd ()) & DYNAMIC) == 0) |
09919ac2 JK |
2596 | return 0; |
2597 | ||
82d23ca8 | 2598 | if (target_auxv_search (AT_ENTRY, &entry_point) <= 0) |
09919ac2 JK |
2599 | return 0; |
2600 | ||
7e10abd1 TT |
2601 | exec_displacement |
2602 | = entry_point - bfd_get_start_address (current_program_space->exec_bfd ()); | |
09919ac2 | 2603 | |
8f61baf8 | 2604 | /* Verify the EXEC_DISPLACEMENT candidate complies with the required page |
09919ac2 JK |
2605 | alignment. It is cheaper than the program headers comparison below. */ |
2606 | ||
7e10abd1 TT |
2607 | if (bfd_get_flavour (current_program_space->exec_bfd ()) |
2608 | == bfd_target_elf_flavour) | |
09919ac2 | 2609 | { |
7e10abd1 TT |
2610 | const struct elf_backend_data *elf |
2611 | = get_elf_backend_data (current_program_space->exec_bfd ()); | |
09919ac2 JK |
2612 | |
2613 | /* p_align of PT_LOAD segments does not specify any alignment but | |
2614 | only congruency of addresses: | |
2615 | p_offset % p_align == p_vaddr % p_align | |
2616 | Kernel is free to load the executable with lower alignment. */ | |
2617 | ||
8f61baf8 | 2618 | if ((exec_displacement & (elf->minpagesize - 1)) != 0) |
09919ac2 JK |
2619 | return 0; |
2620 | } | |
2621 | ||
2622 | /* Verify that the auxilliary vector describes the same file as exec_bfd, by | |
2623 | comparing their program headers. If the program headers in the auxilliary | |
2624 | vector do not match the program headers in the executable, then we are | |
2625 | looking at a different file than the one used by the kernel - for | |
2626 | instance, "gdb program" connected to "gdbserver :PORT ld.so program". */ | |
2627 | ||
7e10abd1 TT |
2628 | if (bfd_get_flavour (current_program_space->exec_bfd ()) |
2629 | == bfd_target_elf_flavour) | |
09919ac2 | 2630 | { |
d1012b8e | 2631 | /* Be optimistic and return 0 only if GDB was able to verify the headers |
09919ac2 | 2632 | really do not match. */ |
0a1e94c7 | 2633 | int arch_size; |
09919ac2 | 2634 | |
6b09f134 | 2635 | std::optional<gdb::byte_vector> phdrs_target |
17658d46 | 2636 | = read_program_header (-1, &arch_size, NULL); |
6b09f134 | 2637 | std::optional<gdb::byte_vector> phdrs_binary |
7e10abd1 | 2638 | = read_program_headers_from_bfd (current_program_space->exec_bfd ()); |
d1012b8e | 2639 | if (phdrs_target && phdrs_binary) |
0a1e94c7 | 2640 | { |
99d9c3b9 | 2641 | bfd_endian byte_order = gdbarch_byte_order (current_inferior ()->arch ()); |
0a1e94c7 JK |
2642 | |
2643 | /* We are dealing with three different addresses. EXEC_BFD | |
2644 | represents current address in on-disk file. target memory content | |
2645 | may be different from EXEC_BFD as the file may have been prelinked | |
2646 | to a different address after the executable has been loaded. | |
2647 | Moreover the address of placement in target memory can be | |
3e43a32a MS |
2648 | different from what the program headers in target memory say - |
2649 | this is the goal of PIE. | |
0a1e94c7 JK |
2650 | |
2651 | Detected DISPLACEMENT covers both the offsets of PIE placement and | |
2652 | possible new prelink performed after start of the program. Here | |
2653 | relocate BUF and BUF2 just by the EXEC_BFD vs. target memory | |
2654 | content offset for the verification purpose. */ | |
2655 | ||
d1012b8e | 2656 | if (phdrs_target->size () != phdrs_binary->size () |
7e10abd1 | 2657 | || bfd_get_arch_size (current_program_space->exec_bfd ()) != arch_size) |
d1012b8e | 2658 | return 0; |
3e43a32a | 2659 | else if (arch_size == 32 |
17658d46 | 2660 | && phdrs_target->size () >= sizeof (Elf32_External_Phdr) |
dda83cd7 | 2661 | && phdrs_target->size () % sizeof (Elf32_External_Phdr) == 0) |
0a1e94c7 | 2662 | { |
7e10abd1 TT |
2663 | Elf_Internal_Ehdr *ehdr2 |
2664 | = elf_tdata (current_program_space->exec_bfd ())->elf_header; | |
2665 | Elf_Internal_Phdr *phdr2 | |
2666 | = elf_tdata (current_program_space->exec_bfd ())->phdr; | |
0a1e94c7 JK |
2667 | CORE_ADDR displacement = 0; |
2668 | int i; | |
2669 | ||
2670 | /* DISPLACEMENT could be found more easily by the difference of | |
2671 | ehdr2->e_entry. But we haven't read the ehdr yet, and we | |
2672 | already have enough information to compute that displacement | |
2673 | with what we've read. */ | |
2674 | ||
2675 | for (i = 0; i < ehdr2->e_phnum; i++) | |
2676 | if (phdr2[i].p_type == PT_LOAD) | |
2677 | { | |
2678 | Elf32_External_Phdr *phdrp; | |
2679 | gdb_byte *buf_vaddr_p, *buf_paddr_p; | |
2680 | CORE_ADDR vaddr, paddr; | |
2681 | CORE_ADDR displacement_vaddr = 0; | |
2682 | CORE_ADDR displacement_paddr = 0; | |
2683 | ||
17658d46 | 2684 | phdrp = &((Elf32_External_Phdr *) phdrs_target->data ())[i]; |
0a1e94c7 JK |
2685 | buf_vaddr_p = (gdb_byte *) &phdrp->p_vaddr; |
2686 | buf_paddr_p = (gdb_byte *) &phdrp->p_paddr; | |
2687 | ||
2688 | vaddr = extract_unsigned_integer (buf_vaddr_p, 4, | |
2689 | byte_order); | |
2690 | displacement_vaddr = vaddr - phdr2[i].p_vaddr; | |
2691 | ||
2692 | paddr = extract_unsigned_integer (buf_paddr_p, 4, | |
2693 | byte_order); | |
2694 | displacement_paddr = paddr - phdr2[i].p_paddr; | |
2695 | ||
2696 | if (displacement_vaddr == displacement_paddr) | |
2697 | displacement = displacement_vaddr; | |
2698 | ||
2699 | break; | |
2700 | } | |
2701 | ||
17658d46 | 2702 | /* Now compare program headers from the target and the binary |
dda83cd7 | 2703 | with optional DISPLACEMENT. */ |
0a1e94c7 | 2704 | |
17658d46 SM |
2705 | for (i = 0; |
2706 | i < phdrs_target->size () / sizeof (Elf32_External_Phdr); | |
2707 | i++) | |
0a1e94c7 JK |
2708 | { |
2709 | Elf32_External_Phdr *phdrp; | |
2710 | Elf32_External_Phdr *phdr2p; | |
2711 | gdb_byte *buf_vaddr_p, *buf_paddr_p; | |
2712 | CORE_ADDR vaddr, paddr; | |
43b8e241 | 2713 | asection *plt2_asect; |
0a1e94c7 | 2714 | |
17658d46 | 2715 | phdrp = &((Elf32_External_Phdr *) phdrs_target->data ())[i]; |
0a1e94c7 JK |
2716 | buf_vaddr_p = (gdb_byte *) &phdrp->p_vaddr; |
2717 | buf_paddr_p = (gdb_byte *) &phdrp->p_paddr; | |
d1012b8e | 2718 | phdr2p = &((Elf32_External_Phdr *) phdrs_binary->data ())[i]; |
0a1e94c7 JK |
2719 | |
2720 | /* PT_GNU_STACK is an exception by being never relocated by | |
2721 | prelink as its addresses are always zero. */ | |
2722 | ||
2723 | if (memcmp (phdrp, phdr2p, sizeof (*phdrp)) == 0) | |
2724 | continue; | |
2725 | ||
2726 | /* Check also other adjustment combinations - PR 11786. */ | |
2727 | ||
3e43a32a MS |
2728 | vaddr = extract_unsigned_integer (buf_vaddr_p, 4, |
2729 | byte_order); | |
0a1e94c7 JK |
2730 | vaddr -= displacement; |
2731 | store_unsigned_integer (buf_vaddr_p, 4, byte_order, vaddr); | |
2732 | ||
3e43a32a MS |
2733 | paddr = extract_unsigned_integer (buf_paddr_p, 4, |
2734 | byte_order); | |
0a1e94c7 JK |
2735 | paddr -= displacement; |
2736 | store_unsigned_integer (buf_paddr_p, 4, byte_order, paddr); | |
2737 | ||
2738 | if (memcmp (phdrp, phdr2p, sizeof (*phdrp)) == 0) | |
2739 | continue; | |
2740 | ||
204b5331 DE |
2741 | /* Strip modifies the flags and alignment of PT_GNU_RELRO. |
2742 | CentOS-5 has problems with filesz, memsz as well. | |
be2d111a | 2743 | Strip also modifies memsz of PT_TLS. |
204b5331 | 2744 | See PR 11786. */ |
c44deb73 SM |
2745 | if (phdr2[i].p_type == PT_GNU_RELRO |
2746 | || phdr2[i].p_type == PT_TLS) | |
204b5331 DE |
2747 | { |
2748 | Elf32_External_Phdr tmp_phdr = *phdrp; | |
2749 | Elf32_External_Phdr tmp_phdr2 = *phdr2p; | |
2750 | ||
2751 | memset (tmp_phdr.p_filesz, 0, 4); | |
2752 | memset (tmp_phdr.p_memsz, 0, 4); | |
2753 | memset (tmp_phdr.p_flags, 0, 4); | |
2754 | memset (tmp_phdr.p_align, 0, 4); | |
2755 | memset (tmp_phdr2.p_filesz, 0, 4); | |
2756 | memset (tmp_phdr2.p_memsz, 0, 4); | |
2757 | memset (tmp_phdr2.p_flags, 0, 4); | |
2758 | memset (tmp_phdr2.p_align, 0, 4); | |
2759 | ||
2760 | if (memcmp (&tmp_phdr, &tmp_phdr2, sizeof (tmp_phdr)) | |
2761 | == 0) | |
2762 | continue; | |
2763 | } | |
2764 | ||
43b8e241 | 2765 | /* prelink can convert .plt SHT_NOBITS to SHT_PROGBITS. */ |
7e10abd1 | 2766 | bfd *exec_bfd = current_program_space->exec_bfd (); |
43b8e241 JK |
2767 | plt2_asect = bfd_get_section_by_name (exec_bfd, ".plt"); |
2768 | if (plt2_asect) | |
2769 | { | |
2770 | int content2; | |
2771 | gdb_byte *buf_filesz_p = (gdb_byte *) &phdrp->p_filesz; | |
2772 | CORE_ADDR filesz; | |
2773 | ||
fd361982 | 2774 | content2 = (bfd_section_flags (plt2_asect) |
43b8e241 JK |
2775 | & SEC_HAS_CONTENTS) != 0; |
2776 | ||
2777 | filesz = extract_unsigned_integer (buf_filesz_p, 4, | |
2778 | byte_order); | |
2779 | ||
2780 | /* PLT2_ASECT is from on-disk file (exec_bfd) while | |
2781 | FILESZ is from the in-memory image. */ | |
2782 | if (content2) | |
fd361982 | 2783 | filesz += bfd_section_size (plt2_asect); |
43b8e241 | 2784 | else |
fd361982 | 2785 | filesz -= bfd_section_size (plt2_asect); |
43b8e241 JK |
2786 | |
2787 | store_unsigned_integer (buf_filesz_p, 4, byte_order, | |
2788 | filesz); | |
2789 | ||
2790 | if (memcmp (phdrp, phdr2p, sizeof (*phdrp)) == 0) | |
2791 | continue; | |
2792 | } | |
2793 | ||
d1012b8e | 2794 | return 0; |
0a1e94c7 JK |
2795 | } |
2796 | } | |
3e43a32a | 2797 | else if (arch_size == 64 |
17658d46 | 2798 | && phdrs_target->size () >= sizeof (Elf64_External_Phdr) |
dda83cd7 | 2799 | && phdrs_target->size () % sizeof (Elf64_External_Phdr) == 0) |
0a1e94c7 | 2800 | { |
7e10abd1 TT |
2801 | Elf_Internal_Ehdr *ehdr2 |
2802 | = elf_tdata (current_program_space->exec_bfd ())->elf_header; | |
2803 | Elf_Internal_Phdr *phdr2 | |
2804 | = elf_tdata (current_program_space->exec_bfd ())->phdr; | |
0a1e94c7 JK |
2805 | CORE_ADDR displacement = 0; |
2806 | int i; | |
2807 | ||
2808 | /* DISPLACEMENT could be found more easily by the difference of | |
2809 | ehdr2->e_entry. But we haven't read the ehdr yet, and we | |
2810 | already have enough information to compute that displacement | |
2811 | with what we've read. */ | |
2812 | ||
2813 | for (i = 0; i < ehdr2->e_phnum; i++) | |
2814 | if (phdr2[i].p_type == PT_LOAD) | |
2815 | { | |
2816 | Elf64_External_Phdr *phdrp; | |
2817 | gdb_byte *buf_vaddr_p, *buf_paddr_p; | |
2818 | CORE_ADDR vaddr, paddr; | |
2819 | CORE_ADDR displacement_vaddr = 0; | |
2820 | CORE_ADDR displacement_paddr = 0; | |
2821 | ||
17658d46 | 2822 | phdrp = &((Elf64_External_Phdr *) phdrs_target->data ())[i]; |
0a1e94c7 JK |
2823 | buf_vaddr_p = (gdb_byte *) &phdrp->p_vaddr; |
2824 | buf_paddr_p = (gdb_byte *) &phdrp->p_paddr; | |
2825 | ||
2826 | vaddr = extract_unsigned_integer (buf_vaddr_p, 8, | |
2827 | byte_order); | |
2828 | displacement_vaddr = vaddr - phdr2[i].p_vaddr; | |
2829 | ||
2830 | paddr = extract_unsigned_integer (buf_paddr_p, 8, | |
2831 | byte_order); | |
2832 | displacement_paddr = paddr - phdr2[i].p_paddr; | |
2833 | ||
2834 | if (displacement_vaddr == displacement_paddr) | |
2835 | displacement = displacement_vaddr; | |
2836 | ||
2837 | break; | |
2838 | } | |
2839 | ||
2840 | /* Now compare BUF and BUF2 with optional DISPLACEMENT. */ | |
2841 | ||
17658d46 SM |
2842 | for (i = 0; |
2843 | i < phdrs_target->size () / sizeof (Elf64_External_Phdr); | |
2844 | i++) | |
0a1e94c7 JK |
2845 | { |
2846 | Elf64_External_Phdr *phdrp; | |
2847 | Elf64_External_Phdr *phdr2p; | |
2848 | gdb_byte *buf_vaddr_p, *buf_paddr_p; | |
2849 | CORE_ADDR vaddr, paddr; | |
43b8e241 | 2850 | asection *plt2_asect; |
0a1e94c7 | 2851 | |
17658d46 | 2852 | phdrp = &((Elf64_External_Phdr *) phdrs_target->data ())[i]; |
0a1e94c7 JK |
2853 | buf_vaddr_p = (gdb_byte *) &phdrp->p_vaddr; |
2854 | buf_paddr_p = (gdb_byte *) &phdrp->p_paddr; | |
d1012b8e | 2855 | phdr2p = &((Elf64_External_Phdr *) phdrs_binary->data ())[i]; |
0a1e94c7 JK |
2856 | |
2857 | /* PT_GNU_STACK is an exception by being never relocated by | |
2858 | prelink as its addresses are always zero. */ | |
2859 | ||
2860 | if (memcmp (phdrp, phdr2p, sizeof (*phdrp)) == 0) | |
2861 | continue; | |
2862 | ||
2863 | /* Check also other adjustment combinations - PR 11786. */ | |
2864 | ||
3e43a32a MS |
2865 | vaddr = extract_unsigned_integer (buf_vaddr_p, 8, |
2866 | byte_order); | |
0a1e94c7 JK |
2867 | vaddr -= displacement; |
2868 | store_unsigned_integer (buf_vaddr_p, 8, byte_order, vaddr); | |
2869 | ||
3e43a32a MS |
2870 | paddr = extract_unsigned_integer (buf_paddr_p, 8, |
2871 | byte_order); | |
0a1e94c7 JK |
2872 | paddr -= displacement; |
2873 | store_unsigned_integer (buf_paddr_p, 8, byte_order, paddr); | |
2874 | ||
2875 | if (memcmp (phdrp, phdr2p, sizeof (*phdrp)) == 0) | |
2876 | continue; | |
2877 | ||
204b5331 DE |
2878 | /* Strip modifies the flags and alignment of PT_GNU_RELRO. |
2879 | CentOS-5 has problems with filesz, memsz as well. | |
be2d111a | 2880 | Strip also modifies memsz of PT_TLS. |
204b5331 | 2881 | See PR 11786. */ |
c44deb73 SM |
2882 | if (phdr2[i].p_type == PT_GNU_RELRO |
2883 | || phdr2[i].p_type == PT_TLS) | |
204b5331 DE |
2884 | { |
2885 | Elf64_External_Phdr tmp_phdr = *phdrp; | |
2886 | Elf64_External_Phdr tmp_phdr2 = *phdr2p; | |
2887 | ||
2888 | memset (tmp_phdr.p_filesz, 0, 8); | |
2889 | memset (tmp_phdr.p_memsz, 0, 8); | |
2890 | memset (tmp_phdr.p_flags, 0, 4); | |
2891 | memset (tmp_phdr.p_align, 0, 8); | |
2892 | memset (tmp_phdr2.p_filesz, 0, 8); | |
2893 | memset (tmp_phdr2.p_memsz, 0, 8); | |
2894 | memset (tmp_phdr2.p_flags, 0, 4); | |
2895 | memset (tmp_phdr2.p_align, 0, 8); | |
2896 | ||
2897 | if (memcmp (&tmp_phdr, &tmp_phdr2, sizeof (tmp_phdr)) | |
2898 | == 0) | |
2899 | continue; | |
2900 | } | |
2901 | ||
43b8e241 | 2902 | /* prelink can convert .plt SHT_NOBITS to SHT_PROGBITS. */ |
7e10abd1 TT |
2903 | plt2_asect |
2904 | = bfd_get_section_by_name (current_program_space->exec_bfd (), | |
2905 | ".plt"); | |
43b8e241 JK |
2906 | if (plt2_asect) |
2907 | { | |
2908 | int content2; | |
2909 | gdb_byte *buf_filesz_p = (gdb_byte *) &phdrp->p_filesz; | |
2910 | CORE_ADDR filesz; | |
2911 | ||
fd361982 | 2912 | content2 = (bfd_section_flags (plt2_asect) |
43b8e241 JK |
2913 | & SEC_HAS_CONTENTS) != 0; |
2914 | ||
2915 | filesz = extract_unsigned_integer (buf_filesz_p, 8, | |
2916 | byte_order); | |
2917 | ||
7e10abd1 TT |
2918 | /* PLT2_ASECT is from on-disk file (current |
2919 | exec_bfd) while FILESZ is from the in-memory | |
2920 | image. */ | |
43b8e241 | 2921 | if (content2) |
fd361982 | 2922 | filesz += bfd_section_size (plt2_asect); |
43b8e241 | 2923 | else |
fd361982 | 2924 | filesz -= bfd_section_size (plt2_asect); |
43b8e241 JK |
2925 | |
2926 | store_unsigned_integer (buf_filesz_p, 8, byte_order, | |
2927 | filesz); | |
2928 | ||
2929 | if (memcmp (phdrp, phdr2p, sizeof (*phdrp)) == 0) | |
2930 | continue; | |
2931 | } | |
2932 | ||
d1012b8e | 2933 | return 0; |
0a1e94c7 JK |
2934 | } |
2935 | } | |
2936 | else | |
d1012b8e | 2937 | return 0; |
0a1e94c7 | 2938 | } |
09919ac2 | 2939 | } |
b8040f19 | 2940 | |
ccf26247 JK |
2941 | if (info_verbose) |
2942 | { | |
2943 | /* It can be printed repeatedly as there is no easy way to check | |
2944 | the executable symbols/file has been already relocated to | |
2945 | displacement. */ | |
2946 | ||
6cb06a8c TT |
2947 | gdb_printf (_("Using PIE (Position Independent Executable) " |
2948 | "displacement %s for \"%s\".\n"), | |
99d9c3b9 | 2949 | paddress (current_inferior ()->arch (), exec_displacement), |
6cb06a8c | 2950 | bfd_get_filename (current_program_space->exec_bfd ())); |
ccf26247 JK |
2951 | } |
2952 | ||
8f61baf8 | 2953 | *displacementp = exec_displacement; |
01c30d6e | 2954 | return 1; |
b8040f19 JK |
2955 | } |
2956 | ||
2957 | /* Relocate the main executable. This function should be called upon | |
c378eb4e | 2958 | stopping the inferior process at the entry point to the program. |
b8040f19 JK |
2959 | The entry point from BFD is compared to the AT_ENTRY of AUXV and if they are |
2960 | different, the main executable is relocated by the proper amount. */ | |
2961 | ||
2962 | static void | |
2963 | svr4_relocate_main_executable (void) | |
2964 | { | |
01c30d6e JK |
2965 | CORE_ADDR displacement; |
2966 | ||
4e5799b6 JK |
2967 | /* If we are re-running this executable, SYMFILE_OBJFILE->SECTION_OFFSETS |
2968 | probably contains the offsets computed using the PIE displacement | |
2969 | from the previous run, which of course are irrelevant for this run. | |
2970 | So we need to determine the new PIE displacement and recompute the | |
2971 | section offsets accordingly, even if SYMFILE_OBJFILE->SECTION_OFFSETS | |
2972 | already contains pre-computed offsets. | |
01c30d6e | 2973 | |
4e5799b6 | 2974 | If we cannot compute the PIE displacement, either: |
01c30d6e | 2975 | |
4e5799b6 JK |
2976 | - The executable is not PIE. |
2977 | ||
2978 | - SYMFILE_OBJFILE does not match the executable started in the target. | |
2979 | This can happen for main executable symbols loaded at the host while | |
2980 | `ld.so --ld-args main-executable' is loaded in the target. | |
2981 | ||
2982 | Then we leave the section offsets untouched and use them as is for | |
2983 | this run. Either: | |
2984 | ||
2985 | - These section offsets were properly reset earlier, and thus | |
2986 | already contain the correct values. This can happen for instance | |
2987 | when reconnecting via the remote protocol to a target that supports | |
2988 | the `qOffsets' packet. | |
2989 | ||
2990 | - The section offsets were not reset earlier, and the best we can | |
c378eb4e | 2991 | hope is that the old offsets are still applicable to the new run. */ |
01c30d6e JK |
2992 | |
2993 | if (! svr4_exec_displacement (&displacement)) | |
2994 | return; | |
b8040f19 | 2995 | |
01c30d6e JK |
2996 | /* Even DISPLACEMENT 0 is a valid new difference of in-memory vs. in-file |
2997 | addresses. */ | |
b8040f19 | 2998 | |
a42d7dd8 TT |
2999 | objfile *objf = current_program_space->symfile_object_file; |
3000 | if (objf) | |
e2a44558 | 3001 | { |
a42d7dd8 | 3002 | section_offsets new_offsets (objf->section_offsets.size (), |
6a053cb1 | 3003 | displacement); |
a42d7dd8 | 3004 | objfile_relocate (objf, new_offsets); |
e2a44558 | 3005 | } |
7e10abd1 | 3006 | else if (current_program_space->exec_bfd ()) |
51bee8e9 JK |
3007 | { |
3008 | asection *asect; | |
3009 | ||
7e10abd1 | 3010 | bfd *exec_bfd = current_program_space->exec_bfd (); |
51bee8e9 JK |
3011 | for (asect = exec_bfd->sections; asect != NULL; asect = asect->next) |
3012 | exec_set_section_address (bfd_get_filename (exec_bfd), asect->index, | |
fd361982 | 3013 | bfd_section_vma (asect) + displacement); |
51bee8e9 | 3014 | } |
e2a44558 KB |
3015 | } |
3016 | ||
7f86f058 | 3017 | /* Implement the "create_inferior_hook" target_solib_ops method. |
13437d4b KB |
3018 | |
3019 | For SVR4 executables, this first instruction is either the first | |
3020 | instruction in the dynamic linker (for dynamically linked | |
3021 | executables) or the instruction at "start" for statically linked | |
3022 | executables. For dynamically linked executables, the system | |
3023 | first exec's /lib/libc.so.N, which contains the dynamic linker, | |
3024 | and starts it running. The dynamic linker maps in any needed | |
3025 | shared libraries, maps in the actual user executable, and then | |
3026 | jumps to "start" in the user executable. | |
3027 | ||
7f86f058 PA |
3028 | We can arrange to cooperate with the dynamic linker to discover the |
3029 | names of shared libraries that are dynamically linked, and the base | |
3030 | addresses to which they are linked. | |
13437d4b KB |
3031 | |
3032 | This function is responsible for discovering those names and | |
3033 | addresses, and saving sufficient information about them to allow | |
d2e5c99a | 3034 | their symbols to be read at a later time. */ |
13437d4b | 3035 | |
e2a44558 | 3036 | static void |
268a4a75 | 3037 | svr4_solib_create_inferior_hook (int from_tty) |
13437d4b | 3038 | { |
1a816a87 PA |
3039 | struct svr4_info *info; |
3040 | ||
d70cc3ba | 3041 | info = get_svr4_info (current_program_space); |
2020b7ab | 3042 | |
f9e14852 GB |
3043 | /* Clear the probes-based interface's state. */ |
3044 | free_probes_table (info); | |
96bb3873 | 3045 | info->solib_lists.clear (); |
f9e14852 | 3046 | |
e2a44558 | 3047 | /* Relocate the main executable if necessary. */ |
86e4bafc | 3048 | svr4_relocate_main_executable (); |
e2a44558 | 3049 | |
c91c8c16 PA |
3050 | /* No point setting a breakpoint in the dynamic linker if we can't |
3051 | hit it (e.g., a core file, or a trace file). */ | |
55f6301a | 3052 | if (!target_has_execution ()) |
c91c8c16 PA |
3053 | return; |
3054 | ||
d5a921c9 | 3055 | if (!svr4_have_link_map_offsets ()) |
513f5903 | 3056 | return; |
d5a921c9 | 3057 | |
268a4a75 | 3058 | if (!enable_break (info, from_tty)) |
542c95c2 | 3059 | return; |
13437d4b KB |
3060 | } |
3061 | ||
3062 | static void | |
581b34c2 | 3063 | svr4_clear_solib (program_space *pspace) |
13437d4b | 3064 | { |
581b34c2 | 3065 | svr4_info *info = get_svr4_info (pspace); |
6c95b8df PA |
3066 | info->debug_base = 0; |
3067 | info->debug_loader_offset_p = 0; | |
3068 | info->debug_loader_offset = 0; | |
3069 | xfree (info->debug_loader_name); | |
3070 | info->debug_loader_name = NULL; | |
13437d4b KB |
3071 | } |
3072 | ||
6bb7be43 JB |
3073 | /* Clear any bits of ADDR that wouldn't fit in a target-format |
3074 | data pointer. "Data pointer" here refers to whatever sort of | |
3075 | address the dynamic linker uses to manage its sections. At the | |
3076 | moment, we don't support shared libraries on any processors where | |
3077 | code and data pointers are different sizes. | |
3078 | ||
3079 | This isn't really the right solution. What we really need here is | |
3080 | a way to do arithmetic on CORE_ADDR values that respects the | |
3081 | natural pointer/address correspondence. (For example, on the MIPS, | |
3082 | converting a 32-bit pointer to a 64-bit CORE_ADDR requires you to | |
3083 | sign-extend the value. There, simply truncating the bits above | |
819844ad | 3084 | gdbarch_ptr_bit, as we do below, is no good.) This should probably |
6bb7be43 JB |
3085 | be a new gdbarch method or something. */ |
3086 | static CORE_ADDR | |
3087 | svr4_truncate_ptr (CORE_ADDR addr) | |
3088 | { | |
99d9c3b9 | 3089 | if (gdbarch_ptr_bit (current_inferior ()->arch ()) == sizeof (CORE_ADDR) * 8) |
6bb7be43 JB |
3090 | /* We don't need to truncate anything, and the bit twiddling below |
3091 | will fail due to overflow problems. */ | |
3092 | return addr; | |
3093 | else | |
99d9c3b9 | 3094 | return addr & (((CORE_ADDR) 1 << gdbarch_ptr_bit (current_inferior ()->arch ())) - 1); |
6bb7be43 JB |
3095 | } |
3096 | ||
3097 | ||
749499cb | 3098 | static void |
7b323785 | 3099 | svr4_relocate_section_addresses (solib &so, target_section *sec) |
749499cb | 3100 | { |
2b2848e2 DE |
3101 | bfd *abfd = sec->the_bfd_section->owner; |
3102 | ||
3103 | sec->addr = svr4_truncate_ptr (sec->addr + lm_addr_check (so, abfd)); | |
3104 | sec->endaddr = svr4_truncate_ptr (sec->endaddr + lm_addr_check (so, abfd)); | |
749499cb | 3105 | } |
4b188b9f | 3106 | \f |
749499cb | 3107 | |
4b188b9f | 3108 | /* Architecture-specific operations. */ |
6bb7be43 | 3109 | |
4b188b9f | 3110 | struct solib_svr4_ops |
e5e2b9ff | 3111 | { |
4b188b9f | 3112 | /* Return a description of the layout of `struct link_map'. */ |
cb275538 | 3113 | struct link_map_offsets *(*fetch_link_map_offsets)(void) = nullptr; |
4b188b9f | 3114 | }; |
e5e2b9ff | 3115 | |
cb275538 TT |
3116 | /* Per-architecture data key. */ |
3117 | static const registry<gdbarch>::key<struct solib_svr4_ops> solib_svr4_data; | |
3118 | ||
4b188b9f | 3119 | /* Return a default for the architecture-specific operations. */ |
e5e2b9ff | 3120 | |
cb275538 TT |
3121 | static struct solib_svr4_ops * |
3122 | get_ops (struct gdbarch *gdbarch) | |
e5e2b9ff | 3123 | { |
cb275538 TT |
3124 | struct solib_svr4_ops *ops = solib_svr4_data.get (gdbarch); |
3125 | if (ops == nullptr) | |
3126 | ops = solib_svr4_data.emplace (gdbarch); | |
4b188b9f | 3127 | return ops; |
e5e2b9ff KB |
3128 | } |
3129 | ||
4b188b9f | 3130 | /* Set the architecture-specific `struct link_map_offsets' fetcher for |
7e3cb44c | 3131 | GDBARCH to FLMO. Also, install SVR4 solib_ops into GDBARCH. */ |
1c4dcb57 | 3132 | |
21479ded | 3133 | void |
e5e2b9ff | 3134 | set_solib_svr4_fetch_link_map_offsets (struct gdbarch *gdbarch, |
dda83cd7 | 3135 | struct link_map_offsets *(*flmo) (void)) |
21479ded | 3136 | { |
cb275538 | 3137 | struct solib_svr4_ops *ops = get_ops (gdbarch); |
4b188b9f MK |
3138 | |
3139 | ops->fetch_link_map_offsets = flmo; | |
7e3cb44c | 3140 | |
9e468e95 | 3141 | set_gdbarch_so_ops (gdbarch, &svr4_so_ops); |
626ca2c0 CB |
3142 | set_gdbarch_iterate_over_objfiles_in_search_order |
3143 | (gdbarch, svr4_iterate_over_objfiles_in_search_order); | |
21479ded KB |
3144 | } |
3145 | ||
4b188b9f MK |
3146 | /* Fetch a link_map_offsets structure using the architecture-specific |
3147 | `struct link_map_offsets' fetcher. */ | |
1c4dcb57 | 3148 | |
4b188b9f MK |
3149 | static struct link_map_offsets * |
3150 | svr4_fetch_link_map_offsets (void) | |
21479ded | 3151 | { |
99d9c3b9 | 3152 | struct solib_svr4_ops *ops = get_ops (current_inferior ()->arch ()); |
4b188b9f MK |
3153 | |
3154 | gdb_assert (ops->fetch_link_map_offsets); | |
3155 | return ops->fetch_link_map_offsets (); | |
21479ded KB |
3156 | } |
3157 | ||
4b188b9f MK |
3158 | /* Return 1 if a link map offset fetcher has been defined, 0 otherwise. */ |
3159 | ||
3160 | static int | |
3161 | svr4_have_link_map_offsets (void) | |
3162 | { | |
99d9c3b9 | 3163 | struct solib_svr4_ops *ops = get_ops (current_inferior ()->arch ()); |
433759f7 | 3164 | |
4b188b9f MK |
3165 | return (ops->fetch_link_map_offsets != NULL); |
3166 | } | |
3167 | \f | |
3168 | ||
e4bbbda8 MK |
3169 | /* Most OS'es that have SVR4-style ELF dynamic libraries define a |
3170 | `struct r_debug' and a `struct link_map' that are binary compatible | |
85102364 | 3171 | with the original SVR4 implementation. */ |
e4bbbda8 MK |
3172 | |
3173 | /* Fetch (and possibly build) an appropriate `struct link_map_offsets' | |
3174 | for an ILP32 SVR4 system. */ | |
d989b283 | 3175 | |
e4bbbda8 MK |
3176 | struct link_map_offsets * |
3177 | svr4_ilp32_fetch_link_map_offsets (void) | |
3178 | { | |
3179 | static struct link_map_offsets lmo; | |
3180 | static struct link_map_offsets *lmp = NULL; | |
3181 | ||
3182 | if (lmp == NULL) | |
3183 | { | |
3184 | lmp = &lmo; | |
3185 | ||
e4cd0d6a MK |
3186 | lmo.r_version_offset = 0; |
3187 | lmo.r_version_size = 4; | |
e4bbbda8 | 3188 | lmo.r_map_offset = 4; |
7cd25cfc | 3189 | lmo.r_brk_offset = 8; |
e4cd0d6a | 3190 | lmo.r_ldsomap_offset = 20; |
8d56636a | 3191 | lmo.r_next_offset = -1; |
e4bbbda8 MK |
3192 | |
3193 | /* Everything we need is in the first 20 bytes. */ | |
3194 | lmo.link_map_size = 20; | |
3195 | lmo.l_addr_offset = 0; | |
e4bbbda8 | 3196 | lmo.l_name_offset = 4; |
cc10cae3 | 3197 | lmo.l_ld_offset = 8; |
e4bbbda8 | 3198 | lmo.l_next_offset = 12; |
e4bbbda8 | 3199 | lmo.l_prev_offset = 16; |
e4bbbda8 MK |
3200 | } |
3201 | ||
3202 | return lmp; | |
3203 | } | |
3204 | ||
3205 | /* Fetch (and possibly build) an appropriate `struct link_map_offsets' | |
3206 | for an LP64 SVR4 system. */ | |
d989b283 | 3207 | |
e4bbbda8 MK |
3208 | struct link_map_offsets * |
3209 | svr4_lp64_fetch_link_map_offsets (void) | |
3210 | { | |
3211 | static struct link_map_offsets lmo; | |
3212 | static struct link_map_offsets *lmp = NULL; | |
3213 | ||
3214 | if (lmp == NULL) | |
3215 | { | |
3216 | lmp = &lmo; | |
3217 | ||
e4cd0d6a MK |
3218 | lmo.r_version_offset = 0; |
3219 | lmo.r_version_size = 4; | |
e4bbbda8 | 3220 | lmo.r_map_offset = 8; |
7cd25cfc | 3221 | lmo.r_brk_offset = 16; |
e4cd0d6a | 3222 | lmo.r_ldsomap_offset = 40; |
8d56636a | 3223 | lmo.r_next_offset = -1; |
e4bbbda8 MK |
3224 | |
3225 | /* Everything we need is in the first 40 bytes. */ | |
3226 | lmo.link_map_size = 40; | |
3227 | lmo.l_addr_offset = 0; | |
e4bbbda8 | 3228 | lmo.l_name_offset = 8; |
cc10cae3 | 3229 | lmo.l_ld_offset = 16; |
e4bbbda8 | 3230 | lmo.l_next_offset = 24; |
e4bbbda8 | 3231 | lmo.l_prev_offset = 32; |
e4bbbda8 MK |
3232 | } |
3233 | ||
3234 | return lmp; | |
3235 | } | |
3236 | \f | |
3237 | ||
aebb370b MM |
3238 | /* Return the DSO matching OBJFILE or nullptr if none can be found. */ |
3239 | ||
7b323785 | 3240 | static const solib * |
aebb370b MM |
3241 | find_solib_for_objfile (struct objfile *objfile) |
3242 | { | |
3243 | if (objfile == nullptr) | |
3244 | return nullptr; | |
3245 | ||
3246 | /* If OBJFILE is a separate debug object file, look for the original | |
3247 | object file. */ | |
3248 | if (objfile->separate_debug_objfile_backlink != nullptr) | |
3249 | objfile = objfile->separate_debug_objfile_backlink; | |
3250 | ||
7b323785 | 3251 | for (const solib &so : current_program_space->solibs ()) |
8971d278 SM |
3252 | if (so.objfile == objfile) |
3253 | return &so; | |
aebb370b MM |
3254 | |
3255 | return nullptr; | |
3256 | } | |
3257 | ||
3258 | /* Return the address of the r_debug object for the namespace containing | |
3259 | SOLIB or zero if it cannot be found. This may happen when symbol files | |
3260 | are added manually, for example, or with the main executable. | |
3261 | ||
3262 | Current callers treat zero as initial namespace so they are doing the | |
3263 | right thing for the main executable. */ | |
3264 | ||
3265 | static CORE_ADDR | |
7b323785 | 3266 | find_debug_base_for_solib (const solib *solib) |
aebb370b MM |
3267 | { |
3268 | if (solib == nullptr) | |
3269 | return 0; | |
3270 | ||
3271 | svr4_info *info = get_svr4_info (current_program_space); | |
3272 | gdb_assert (info != nullptr); | |
96bb3873 | 3273 | |
7ad0a42e SM |
3274 | auto *lm_info |
3275 | = gdb::checked_static_cast<const lm_info_svr4 *> (solib->lm_info.get ()); | |
96bb3873 SM |
3276 | |
3277 | for (const auto &tuple : info->solib_lists) | |
aebb370b MM |
3278 | { |
3279 | CORE_ADDR debug_base = tuple.first; | |
96bb3873 | 3280 | const std::vector<svr4_so> &sos = tuple.second; |
aebb370b | 3281 | |
96bb3873 | 3282 | for (const svr4_so &so : sos) |
98107b0b | 3283 | if (svr4_same (solib->so_original_name.c_str (), so.name.c_str (), |
96bb3873 | 3284 | *lm_info, *so.lm_info)) |
aebb370b MM |
3285 | return debug_base; |
3286 | } | |
3287 | ||
3288 | return 0; | |
3289 | } | |
3290 | ||
626ca2c0 | 3291 | /* Search order for ELF DSOs linked with -Bsymbolic. Those DSOs have a |
aebb370b MM |
3292 | different rule for symbol lookup. The lookup begins here in the DSO, |
3293 | not in the main executable. When starting from CURRENT_OBJFILE, we | |
3294 | stay in the same namespace as that file. Otherwise, we only consider | |
3295 | the initial namespace. */ | |
3a40aaa0 | 3296 | |
626ca2c0 CB |
3297 | static void |
3298 | svr4_iterate_over_objfiles_in_search_order | |
6e9cd73e SM |
3299 | (gdbarch *gdbarch, iterate_over_objfiles_in_search_order_cb_ftype cb, |
3300 | objfile *current_objfile) | |
3a40aaa0 | 3301 | { |
626ca2c0 CB |
3302 | bool checked_current_objfile = false; |
3303 | if (current_objfile != nullptr) | |
61f0d762 | 3304 | { |
626ca2c0 | 3305 | bfd *abfd; |
61f0d762 | 3306 | |
626ca2c0 | 3307 | if (current_objfile->separate_debug_objfile_backlink != nullptr) |
dda83cd7 | 3308 | current_objfile = current_objfile->separate_debug_objfile_backlink; |
61f0d762 | 3309 | |
a42d7dd8 | 3310 | if (current_objfile == current_program_space->symfile_object_file) |
7e10abd1 | 3311 | abfd = current_program_space->exec_bfd (); |
626ca2c0 | 3312 | else |
98badbfd | 3313 | abfd = current_objfile->obfd.get (); |
626ca2c0 | 3314 | |
7ab78ccb | 3315 | if (abfd != nullptr |
8ddf4645 | 3316 | && gdb_bfd_scan_elf_dyntag (DT_SYMBOLIC, abfd, nullptr, nullptr) == 1) |
626ca2c0 CB |
3317 | { |
3318 | checked_current_objfile = true; | |
6e9cd73e | 3319 | if (cb (current_objfile)) |
626ca2c0 CB |
3320 | return; |
3321 | } | |
3322 | } | |
3a40aaa0 | 3323 | |
aebb370b MM |
3324 | /* The linker namespace to iterate identified by the address of its |
3325 | r_debug object, defaulting to the initial namespace. */ | |
3326 | CORE_ADDR initial = elf_locate_base (); | |
7b323785 | 3327 | const solib *curr_solib = find_solib_for_objfile (current_objfile); |
aebb370b MM |
3328 | CORE_ADDR debug_base = find_debug_base_for_solib (curr_solib); |
3329 | if (debug_base == 0) | |
3330 | debug_base = initial; | |
3331 | ||
626ca2c0 CB |
3332 | for (objfile *objfile : current_program_space->objfiles ()) |
3333 | { | |
3334 | if (checked_current_objfile && objfile == current_objfile) | |
3335 | continue; | |
aebb370b MM |
3336 | |
3337 | /* Try to determine the namespace into which objfile was loaded. | |
3338 | ||
3339 | If we fail, e.g. for manually added symbol files or for the main | |
3340 | executable, we assume that they were added to the initial | |
3341 | namespace. */ | |
7b323785 | 3342 | const solib *solib = find_solib_for_objfile (objfile); |
aebb370b MM |
3343 | CORE_ADDR solib_base = find_debug_base_for_solib (solib); |
3344 | if (solib_base == 0) | |
3345 | solib_base = initial; | |
3346 | ||
3347 | /* Ignore objfiles that were added to a different namespace. */ | |
3348 | if (solib_base != debug_base) | |
3349 | continue; | |
3350 | ||
6e9cd73e | 3351 | if (cb (objfile)) |
626ca2c0 CB |
3352 | return; |
3353 | } | |
3a40aaa0 UW |
3354 | } |
3355 | ||
6cedf3bc | 3356 | const struct solib_ops svr4_so_ops = |
549dfc51 TT |
3357 | { |
3358 | svr4_relocate_section_addresses, | |
549dfc51 TT |
3359 | svr4_clear_so, |
3360 | svr4_clear_solib, | |
3361 | svr4_solib_create_inferior_hook, | |
3362 | svr4_current_sos, | |
3363 | open_symbol_file_object, | |
3364 | svr4_in_dynsym_resolve_code, | |
3365 | solib_bfd_open, | |
3366 | nullptr, | |
3367 | svr4_same, | |
3368 | svr4_keep_data_in_core, | |
3369 | svr4_update_solib_event_breakpoints, | |
3370 | svr4_handle_solib_event, | |
3371 | }; | |
3372 | ||
6c265988 | 3373 | void _initialize_svr4_solib (); |
13437d4b | 3374 | void |
6c265988 | 3375 | _initialize_svr4_solib () |
13437d4b | 3376 | { |
c90e7d63 SM |
3377 | gdb::observers::free_objfile.attach (svr4_free_objfile_observer, |
3378 | "solib-svr4"); | |
13437d4b | 3379 | } |