Commit | Line | Data |
---|---|---|
ab31aa69 | 1 | /* Handle SVR4 shared libraries for GDB, the GNU Debugger. |
2f4950cd | 2 | |
6aba47ca | 3 | Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, |
7b6bb8da | 4 | 2001, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 |
0fb0cc75 | 5 | Free Software Foundation, Inc. |
13437d4b KB |
6 | |
7 | This file is part of GDB. | |
8 | ||
9 | This program is free software; you can redistribute it and/or modify | |
10 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 11 | the Free Software Foundation; either version 3 of the License, or |
13437d4b KB |
12 | (at your option) any later version. |
13 | ||
14 | This program is distributed in the hope that it will be useful, | |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
18 | ||
19 | You should have received a copy of the GNU General Public License | |
a9762ec7 | 20 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
13437d4b | 21 | |
13437d4b KB |
22 | #include "defs.h" |
23 | ||
13437d4b | 24 | #include "elf/external.h" |
21479ded | 25 | #include "elf/common.h" |
f7856c8f | 26 | #include "elf/mips.h" |
13437d4b KB |
27 | |
28 | #include "symtab.h" | |
29 | #include "bfd.h" | |
30 | #include "symfile.h" | |
31 | #include "objfiles.h" | |
32 | #include "gdbcore.h" | |
13437d4b | 33 | #include "target.h" |
13437d4b | 34 | #include "inferior.h" |
fb14de7b | 35 | #include "regcache.h" |
2020b7ab | 36 | #include "gdbthread.h" |
1a816a87 | 37 | #include "observer.h" |
13437d4b | 38 | |
4b188b9f MK |
39 | #include "gdb_assert.h" |
40 | ||
13437d4b | 41 | #include "solist.h" |
bba93f6c | 42 | #include "solib.h" |
13437d4b KB |
43 | #include "solib-svr4.h" |
44 | ||
2f4950cd | 45 | #include "bfd-target.h" |
cc10cae3 | 46 | #include "elf-bfd.h" |
2f4950cd | 47 | #include "exec.h" |
8d4e36ba | 48 | #include "auxv.h" |
f1838a98 | 49 | #include "exceptions.h" |
2f4950cd | 50 | |
e5e2b9ff | 51 | static struct link_map_offsets *svr4_fetch_link_map_offsets (void); |
d5a921c9 | 52 | static int svr4_have_link_map_offsets (void); |
9f2982ff | 53 | static void svr4_relocate_main_executable (void); |
1c4dcb57 | 54 | |
c378eb4e | 55 | /* Link map info to include in an allocated so_list entry. */ |
13437d4b KB |
56 | |
57 | struct lm_info | |
58 | { | |
59 | /* Pointer to copy of link map from inferior. The type is char * | |
60 | rather than void *, so that we may use byte offsets to find the | |
61 | various fields without the need for a cast. */ | |
4066fc10 | 62 | gdb_byte *lm; |
cc10cae3 AO |
63 | |
64 | /* Amount by which addresses in the binary should be relocated to | |
65 | match the inferior. This could most often be taken directly | |
66 | from lm, but when prelinking is involved and the prelink base | |
67 | address changes, we may need a different offset, we want to | |
68 | warn about the difference and compute it only once. */ | |
69 | CORE_ADDR l_addr; | |
93a57060 DJ |
70 | |
71 | /* The target location of lm. */ | |
72 | CORE_ADDR lm_addr; | |
13437d4b KB |
73 | }; |
74 | ||
75 | /* On SVR4 systems, a list of symbols in the dynamic linker where | |
76 | GDB can try to place a breakpoint to monitor shared library | |
77 | events. | |
78 | ||
79 | If none of these symbols are found, or other errors occur, then | |
80 | SVR4 systems will fall back to using a symbol as the "startup | |
81 | mapping complete" breakpoint address. */ | |
82 | ||
bc043ef3 | 83 | static const char * const solib_break_names[] = |
13437d4b KB |
84 | { |
85 | "r_debug_state", | |
86 | "_r_debug_state", | |
87 | "_dl_debug_state", | |
88 | "rtld_db_dlactivity", | |
4c7dcb84 | 89 | "__dl_rtld_db_dlactivity", |
1f72e589 | 90 | "_rtld_debug_state", |
4c0122c8 | 91 | |
13437d4b KB |
92 | NULL |
93 | }; | |
13437d4b | 94 | |
bc043ef3 | 95 | static const char * const bkpt_names[] = |
13437d4b | 96 | { |
13437d4b | 97 | "_start", |
ad3dcc5c | 98 | "__start", |
13437d4b KB |
99 | "main", |
100 | NULL | |
101 | }; | |
13437d4b | 102 | |
bc043ef3 | 103 | static const char * const main_name_list[] = |
13437d4b KB |
104 | { |
105 | "main_$main", | |
106 | NULL | |
107 | }; | |
108 | ||
4d7b2d5b JB |
109 | /* Return non-zero if GDB_SO_NAME and INFERIOR_SO_NAME represent |
110 | the same shared library. */ | |
111 | ||
112 | static int | |
113 | svr4_same_1 (const char *gdb_so_name, const char *inferior_so_name) | |
114 | { | |
115 | if (strcmp (gdb_so_name, inferior_so_name) == 0) | |
116 | return 1; | |
117 | ||
118 | /* On Solaris, when starting inferior we think that dynamic linker is | |
d989b283 PP |
119 | /usr/lib/ld.so.1, but later on, the table of loaded shared libraries |
120 | contains /lib/ld.so.1. Sometimes one file is a link to another, but | |
4d7b2d5b JB |
121 | sometimes they have identical content, but are not linked to each |
122 | other. We don't restrict this check for Solaris, but the chances | |
123 | of running into this situation elsewhere are very low. */ | |
124 | if (strcmp (gdb_so_name, "/usr/lib/ld.so.1") == 0 | |
125 | && strcmp (inferior_so_name, "/lib/ld.so.1") == 0) | |
126 | return 1; | |
127 | ||
128 | /* Similarly, we observed the same issue with sparc64, but with | |
129 | different locations. */ | |
130 | if (strcmp (gdb_so_name, "/usr/lib/sparcv9/ld.so.1") == 0 | |
131 | && strcmp (inferior_so_name, "/lib/sparcv9/ld.so.1") == 0) | |
132 | return 1; | |
133 | ||
134 | return 0; | |
135 | } | |
136 | ||
137 | static int | |
138 | svr4_same (struct so_list *gdb, struct so_list *inferior) | |
139 | { | |
140 | return (svr4_same_1 (gdb->so_original_name, inferior->so_original_name)); | |
141 | } | |
142 | ||
c378eb4e | 143 | /* link map access functions. */ |
13437d4b KB |
144 | |
145 | static CORE_ADDR | |
b23518f0 | 146 | lm_addr_from_link_map (struct so_list *so) |
13437d4b | 147 | { |
4b188b9f | 148 | struct link_map_offsets *lmo = svr4_fetch_link_map_offsets (); |
b6da22b0 | 149 | struct type *ptr_type = builtin_type (target_gdbarch)->builtin_data_ptr; |
13437d4b | 150 | |
cfaefc65 | 151 | return extract_typed_address (so->lm_info->lm + lmo->l_addr_offset, |
b6da22b0 | 152 | ptr_type); |
13437d4b KB |
153 | } |
154 | ||
cc10cae3 | 155 | static int |
b23518f0 | 156 | has_lm_dynamic_from_link_map (void) |
cc10cae3 AO |
157 | { |
158 | struct link_map_offsets *lmo = svr4_fetch_link_map_offsets (); | |
159 | ||
cfaefc65 | 160 | return lmo->l_ld_offset >= 0; |
cc10cae3 AO |
161 | } |
162 | ||
163 | static CORE_ADDR | |
b23518f0 | 164 | lm_dynamic_from_link_map (struct so_list *so) |
cc10cae3 AO |
165 | { |
166 | struct link_map_offsets *lmo = svr4_fetch_link_map_offsets (); | |
b6da22b0 | 167 | struct type *ptr_type = builtin_type (target_gdbarch)->builtin_data_ptr; |
cc10cae3 | 168 | |
cfaefc65 | 169 | return extract_typed_address (so->lm_info->lm + lmo->l_ld_offset, |
b6da22b0 | 170 | ptr_type); |
cc10cae3 AO |
171 | } |
172 | ||
173 | static CORE_ADDR | |
b23518f0 | 174 | lm_addr_check (struct so_list *so, bfd *abfd) |
cc10cae3 AO |
175 | { |
176 | if (so->lm_info->l_addr == (CORE_ADDR)-1) | |
177 | { | |
178 | struct bfd_section *dyninfo_sect; | |
28f34a8f | 179 | CORE_ADDR l_addr, l_dynaddr, dynaddr; |
cc10cae3 | 180 | |
b23518f0 | 181 | l_addr = lm_addr_from_link_map (so); |
cc10cae3 | 182 | |
b23518f0 | 183 | if (! abfd || ! has_lm_dynamic_from_link_map ()) |
cc10cae3 AO |
184 | goto set_addr; |
185 | ||
b23518f0 | 186 | l_dynaddr = lm_dynamic_from_link_map (so); |
cc10cae3 AO |
187 | |
188 | dyninfo_sect = bfd_get_section_by_name (abfd, ".dynamic"); | |
189 | if (dyninfo_sect == NULL) | |
190 | goto set_addr; | |
191 | ||
192 | dynaddr = bfd_section_vma (abfd, dyninfo_sect); | |
193 | ||
194 | if (dynaddr + l_addr != l_dynaddr) | |
195 | { | |
28f34a8f | 196 | CORE_ADDR align = 0x1000; |
4e1fc9c9 | 197 | CORE_ADDR minpagesize = align; |
28f34a8f | 198 | |
cc10cae3 AO |
199 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour) |
200 | { | |
201 | Elf_Internal_Ehdr *ehdr = elf_tdata (abfd)->elf_header; | |
202 | Elf_Internal_Phdr *phdr = elf_tdata (abfd)->phdr; | |
203 | int i; | |
204 | ||
205 | align = 1; | |
206 | ||
207 | for (i = 0; i < ehdr->e_phnum; i++) | |
208 | if (phdr[i].p_type == PT_LOAD && phdr[i].p_align > align) | |
209 | align = phdr[i].p_align; | |
4e1fc9c9 JK |
210 | |
211 | minpagesize = get_elf_backend_data (abfd)->minpagesize; | |
cc10cae3 AO |
212 | } |
213 | ||
214 | /* Turn it into a mask. */ | |
215 | align--; | |
216 | ||
217 | /* If the changes match the alignment requirements, we | |
218 | assume we're using a core file that was generated by the | |
219 | same binary, just prelinked with a different base offset. | |
220 | If it doesn't match, we may have a different binary, the | |
221 | same binary with the dynamic table loaded at an unrelated | |
222 | location, or anything, really. To avoid regressions, | |
223 | don't adjust the base offset in the latter case, although | |
224 | odds are that, if things really changed, debugging won't | |
5c0d192f JK |
225 | quite work. |
226 | ||
227 | One could expect more the condition | |
228 | ((l_addr & align) == 0 && ((l_dynaddr - dynaddr) & align) == 0) | |
229 | but the one below is relaxed for PPC. The PPC kernel supports | |
230 | either 4k or 64k page sizes. To be prepared for 64k pages, | |
231 | PPC ELF files are built using an alignment requirement of 64k. | |
232 | However, when running on a kernel supporting 4k pages, the memory | |
233 | mapping of the library may not actually happen on a 64k boundary! | |
234 | ||
235 | (In the usual case where (l_addr & align) == 0, this check is | |
4e1fc9c9 JK |
236 | equivalent to the possibly expected check above.) |
237 | ||
238 | Even on PPC it must be zero-aligned at least for MINPAGESIZE. */ | |
5c0d192f | 239 | |
02835898 JK |
240 | l_addr = l_dynaddr - dynaddr; |
241 | ||
4e1fc9c9 JK |
242 | if ((l_addr & (minpagesize - 1)) == 0 |
243 | && (l_addr & align) == ((l_dynaddr - dynaddr) & align)) | |
cc10cae3 | 244 | { |
701ed6dc | 245 | if (info_verbose) |
ccf26247 JK |
246 | printf_unfiltered (_("Using PIC (Position Independent Code) " |
247 | "prelink displacement %s for \"%s\".\n"), | |
248 | paddress (target_gdbarch, l_addr), | |
249 | so->so_name); | |
cc10cae3 | 250 | } |
79d4c408 | 251 | else |
02835898 JK |
252 | { |
253 | /* There is no way to verify the library file matches. prelink | |
254 | can during prelinking of an unprelinked file (or unprelinking | |
255 | of a prelinked file) shift the DYNAMIC segment by arbitrary | |
256 | offset without any page size alignment. There is no way to | |
257 | find out the ELF header and/or Program Headers for a limited | |
258 | verification if it they match. One could do a verification | |
259 | of the DYNAMIC segment. Still the found address is the best | |
260 | one GDB could find. */ | |
261 | ||
262 | warning (_(".dynamic section for \"%s\" " | |
263 | "is not at the expected address " | |
264 | "(wrong library or version mismatch?)"), so->so_name); | |
265 | } | |
cc10cae3 AO |
266 | } |
267 | ||
268 | set_addr: | |
269 | so->lm_info->l_addr = l_addr; | |
270 | } | |
271 | ||
272 | return so->lm_info->l_addr; | |
273 | } | |
274 | ||
13437d4b | 275 | static CORE_ADDR |
b23518f0 | 276 | lm_next (struct so_list *so) |
13437d4b | 277 | { |
4b188b9f | 278 | struct link_map_offsets *lmo = svr4_fetch_link_map_offsets (); |
b6da22b0 | 279 | struct type *ptr_type = builtin_type (target_gdbarch)->builtin_data_ptr; |
13437d4b | 280 | |
cfaefc65 | 281 | return extract_typed_address (so->lm_info->lm + lmo->l_next_offset, |
b6da22b0 | 282 | ptr_type); |
13437d4b KB |
283 | } |
284 | ||
492928e4 | 285 | static CORE_ADDR |
b23518f0 | 286 | lm_prev (struct so_list *so) |
492928e4 JK |
287 | { |
288 | struct link_map_offsets *lmo = svr4_fetch_link_map_offsets (); | |
289 | struct type *ptr_type = builtin_type (target_gdbarch)->builtin_data_ptr; | |
290 | ||
291 | return extract_typed_address (so->lm_info->lm + lmo->l_prev_offset, | |
292 | ptr_type); | |
293 | } | |
294 | ||
13437d4b | 295 | static CORE_ADDR |
b23518f0 | 296 | lm_name (struct so_list *so) |
13437d4b | 297 | { |
4b188b9f | 298 | struct link_map_offsets *lmo = svr4_fetch_link_map_offsets (); |
b6da22b0 | 299 | struct type *ptr_type = builtin_type (target_gdbarch)->builtin_data_ptr; |
13437d4b | 300 | |
cfaefc65 | 301 | return extract_typed_address (so->lm_info->lm + lmo->l_name_offset, |
b6da22b0 | 302 | ptr_type); |
13437d4b KB |
303 | } |
304 | ||
6c95b8df | 305 | /* Per pspace SVR4 specific data. */ |
13437d4b | 306 | |
1a816a87 PA |
307 | struct svr4_info |
308 | { | |
c378eb4e | 309 | CORE_ADDR debug_base; /* Base of dynamic linker structures. */ |
1a816a87 PA |
310 | |
311 | /* Validity flag for debug_loader_offset. */ | |
312 | int debug_loader_offset_p; | |
313 | ||
314 | /* Load address for the dynamic linker, inferred. */ | |
315 | CORE_ADDR debug_loader_offset; | |
316 | ||
317 | /* Name of the dynamic linker, valid if debug_loader_offset_p. */ | |
318 | char *debug_loader_name; | |
319 | ||
320 | /* Load map address for the main executable. */ | |
321 | CORE_ADDR main_lm_addr; | |
1a816a87 | 322 | |
6c95b8df PA |
323 | CORE_ADDR interp_text_sect_low; |
324 | CORE_ADDR interp_text_sect_high; | |
325 | CORE_ADDR interp_plt_sect_low; | |
326 | CORE_ADDR interp_plt_sect_high; | |
327 | }; | |
1a816a87 | 328 | |
6c95b8df PA |
329 | /* Per-program-space data key. */ |
330 | static const struct program_space_data *solib_svr4_pspace_data; | |
1a816a87 | 331 | |
6c95b8df PA |
332 | static void |
333 | svr4_pspace_data_cleanup (struct program_space *pspace, void *arg) | |
1a816a87 | 334 | { |
6c95b8df | 335 | struct svr4_info *info; |
1a816a87 | 336 | |
6c95b8df PA |
337 | info = program_space_data (pspace, solib_svr4_pspace_data); |
338 | xfree (info); | |
1a816a87 PA |
339 | } |
340 | ||
6c95b8df PA |
341 | /* Get the current svr4 data. If none is found yet, add it now. This |
342 | function always returns a valid object. */ | |
34439770 | 343 | |
6c95b8df PA |
344 | static struct svr4_info * |
345 | get_svr4_info (void) | |
1a816a87 | 346 | { |
6c95b8df | 347 | struct svr4_info *info; |
1a816a87 | 348 | |
6c95b8df PA |
349 | info = program_space_data (current_program_space, solib_svr4_pspace_data); |
350 | if (info != NULL) | |
351 | return info; | |
34439770 | 352 | |
6c95b8df PA |
353 | info = XZALLOC (struct svr4_info); |
354 | set_program_space_data (current_program_space, solib_svr4_pspace_data, info); | |
355 | return info; | |
1a816a87 | 356 | } |
93a57060 | 357 | |
13437d4b KB |
358 | /* Local function prototypes */ |
359 | ||
bc043ef3 | 360 | static int match_main (const char *); |
13437d4b | 361 | |
97ec2c2f UW |
362 | /* Read program header TYPE from inferior memory. The header is found |
363 | by scanning the OS auxillary vector. | |
364 | ||
09919ac2 JK |
365 | If TYPE == -1, return the program headers instead of the contents of |
366 | one program header. | |
367 | ||
97ec2c2f UW |
368 | Return a pointer to allocated memory holding the program header contents, |
369 | or NULL on failure. If sucessful, and unless P_SECT_SIZE is NULL, the | |
370 | size of those contents is returned to P_SECT_SIZE. Likewise, the target | |
371 | architecture size (32-bit or 64-bit) is returned to P_ARCH_SIZE. */ | |
372 | ||
373 | static gdb_byte * | |
374 | read_program_header (int type, int *p_sect_size, int *p_arch_size) | |
375 | { | |
e17a4113 | 376 | enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch); |
97ec2c2f UW |
377 | CORE_ADDR at_phdr, at_phent, at_phnum; |
378 | int arch_size, sect_size; | |
379 | CORE_ADDR sect_addr; | |
380 | gdb_byte *buf; | |
381 | ||
382 | /* Get required auxv elements from target. */ | |
383 | if (target_auxv_search (¤t_target, AT_PHDR, &at_phdr) <= 0) | |
384 | return 0; | |
385 | if (target_auxv_search (¤t_target, AT_PHENT, &at_phent) <= 0) | |
386 | return 0; | |
387 | if (target_auxv_search (¤t_target, AT_PHNUM, &at_phnum) <= 0) | |
388 | return 0; | |
389 | if (!at_phdr || !at_phnum) | |
390 | return 0; | |
391 | ||
392 | /* Determine ELF architecture type. */ | |
393 | if (at_phent == sizeof (Elf32_External_Phdr)) | |
394 | arch_size = 32; | |
395 | else if (at_phent == sizeof (Elf64_External_Phdr)) | |
396 | arch_size = 64; | |
397 | else | |
398 | return 0; | |
399 | ||
09919ac2 JK |
400 | /* Find the requested segment. */ |
401 | if (type == -1) | |
402 | { | |
403 | sect_addr = at_phdr; | |
404 | sect_size = at_phent * at_phnum; | |
405 | } | |
406 | else if (arch_size == 32) | |
97ec2c2f UW |
407 | { |
408 | Elf32_External_Phdr phdr; | |
409 | int i; | |
410 | ||
411 | /* Search for requested PHDR. */ | |
412 | for (i = 0; i < at_phnum; i++) | |
413 | { | |
414 | if (target_read_memory (at_phdr + i * sizeof (phdr), | |
415 | (gdb_byte *)&phdr, sizeof (phdr))) | |
416 | return 0; | |
417 | ||
e17a4113 UW |
418 | if (extract_unsigned_integer ((gdb_byte *)phdr.p_type, |
419 | 4, byte_order) == type) | |
97ec2c2f UW |
420 | break; |
421 | } | |
422 | ||
423 | if (i == at_phnum) | |
424 | return 0; | |
425 | ||
426 | /* Retrieve address and size. */ | |
e17a4113 UW |
427 | sect_addr = extract_unsigned_integer ((gdb_byte *)phdr.p_vaddr, |
428 | 4, byte_order); | |
429 | sect_size = extract_unsigned_integer ((gdb_byte *)phdr.p_memsz, | |
430 | 4, byte_order); | |
97ec2c2f UW |
431 | } |
432 | else | |
433 | { | |
434 | Elf64_External_Phdr phdr; | |
435 | int i; | |
436 | ||
437 | /* Search for requested PHDR. */ | |
438 | for (i = 0; i < at_phnum; i++) | |
439 | { | |
440 | if (target_read_memory (at_phdr + i * sizeof (phdr), | |
441 | (gdb_byte *)&phdr, sizeof (phdr))) | |
442 | return 0; | |
443 | ||
e17a4113 UW |
444 | if (extract_unsigned_integer ((gdb_byte *)phdr.p_type, |
445 | 4, byte_order) == type) | |
97ec2c2f UW |
446 | break; |
447 | } | |
448 | ||
449 | if (i == at_phnum) | |
450 | return 0; | |
451 | ||
452 | /* Retrieve address and size. */ | |
e17a4113 UW |
453 | sect_addr = extract_unsigned_integer ((gdb_byte *)phdr.p_vaddr, |
454 | 8, byte_order); | |
455 | sect_size = extract_unsigned_integer ((gdb_byte *)phdr.p_memsz, | |
456 | 8, byte_order); | |
97ec2c2f UW |
457 | } |
458 | ||
459 | /* Read in requested program header. */ | |
460 | buf = xmalloc (sect_size); | |
461 | if (target_read_memory (sect_addr, buf, sect_size)) | |
462 | { | |
463 | xfree (buf); | |
464 | return NULL; | |
465 | } | |
466 | ||
467 | if (p_arch_size) | |
468 | *p_arch_size = arch_size; | |
469 | if (p_sect_size) | |
470 | *p_sect_size = sect_size; | |
471 | ||
472 | return buf; | |
473 | } | |
474 | ||
475 | ||
476 | /* Return program interpreter string. */ | |
477 | static gdb_byte * | |
478 | find_program_interpreter (void) | |
479 | { | |
480 | gdb_byte *buf = NULL; | |
481 | ||
482 | /* If we have an exec_bfd, use its section table. */ | |
483 | if (exec_bfd | |
484 | && bfd_get_flavour (exec_bfd) == bfd_target_elf_flavour) | |
485 | { | |
486 | struct bfd_section *interp_sect; | |
487 | ||
488 | interp_sect = bfd_get_section_by_name (exec_bfd, ".interp"); | |
489 | if (interp_sect != NULL) | |
490 | { | |
97ec2c2f UW |
491 | int sect_size = bfd_section_size (exec_bfd, interp_sect); |
492 | ||
493 | buf = xmalloc (sect_size); | |
494 | bfd_get_section_contents (exec_bfd, interp_sect, buf, 0, sect_size); | |
495 | } | |
496 | } | |
497 | ||
498 | /* If we didn't find it, use the target auxillary vector. */ | |
499 | if (!buf) | |
500 | buf = read_program_header (PT_INTERP, NULL, NULL); | |
501 | ||
502 | return buf; | |
503 | } | |
504 | ||
505 | ||
c378eb4e | 506 | /* Scan for DYNTAG in .dynamic section of ABFD. If DYNTAG is found 1 is |
3a40aaa0 UW |
507 | returned and the corresponding PTR is set. */ |
508 | ||
509 | static int | |
510 | scan_dyntag (int dyntag, bfd *abfd, CORE_ADDR *ptr) | |
511 | { | |
512 | int arch_size, step, sect_size; | |
513 | long dyn_tag; | |
b381ea14 | 514 | CORE_ADDR dyn_ptr, dyn_addr; |
65728c26 | 515 | gdb_byte *bufend, *bufstart, *buf; |
3a40aaa0 UW |
516 | Elf32_External_Dyn *x_dynp_32; |
517 | Elf64_External_Dyn *x_dynp_64; | |
518 | struct bfd_section *sect; | |
61f0d762 | 519 | struct target_section *target_section; |
3a40aaa0 UW |
520 | |
521 | if (abfd == NULL) | |
522 | return 0; | |
0763ab81 PA |
523 | |
524 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) | |
525 | return 0; | |
526 | ||
3a40aaa0 UW |
527 | arch_size = bfd_get_arch_size (abfd); |
528 | if (arch_size == -1) | |
0763ab81 | 529 | return 0; |
3a40aaa0 UW |
530 | |
531 | /* Find the start address of the .dynamic section. */ | |
532 | sect = bfd_get_section_by_name (abfd, ".dynamic"); | |
533 | if (sect == NULL) | |
534 | return 0; | |
61f0d762 JK |
535 | |
536 | for (target_section = current_target_sections->sections; | |
537 | target_section < current_target_sections->sections_end; | |
538 | target_section++) | |
539 | if (sect == target_section->the_bfd_section) | |
540 | break; | |
b381ea14 JK |
541 | if (target_section < current_target_sections->sections_end) |
542 | dyn_addr = target_section->addr; | |
543 | else | |
544 | { | |
545 | /* ABFD may come from OBJFILE acting only as a symbol file without being | |
546 | loaded into the target (see add_symbol_file_command). This case is | |
547 | such fallback to the file VMA address without the possibility of | |
548 | having the section relocated to its actual in-memory address. */ | |
549 | ||
550 | dyn_addr = bfd_section_vma (abfd, sect); | |
551 | } | |
3a40aaa0 | 552 | |
65728c26 DJ |
553 | /* Read in .dynamic from the BFD. We will get the actual value |
554 | from memory later. */ | |
3a40aaa0 | 555 | sect_size = bfd_section_size (abfd, sect); |
65728c26 DJ |
556 | buf = bufstart = alloca (sect_size); |
557 | if (!bfd_get_section_contents (abfd, sect, | |
558 | buf, 0, sect_size)) | |
559 | return 0; | |
3a40aaa0 UW |
560 | |
561 | /* Iterate over BUF and scan for DYNTAG. If found, set PTR and return. */ | |
562 | step = (arch_size == 32) ? sizeof (Elf32_External_Dyn) | |
563 | : sizeof (Elf64_External_Dyn); | |
564 | for (bufend = buf + sect_size; | |
565 | buf < bufend; | |
566 | buf += step) | |
567 | { | |
568 | if (arch_size == 32) | |
569 | { | |
570 | x_dynp_32 = (Elf32_External_Dyn *) buf; | |
571 | dyn_tag = bfd_h_get_32 (abfd, (bfd_byte *) x_dynp_32->d_tag); | |
572 | dyn_ptr = bfd_h_get_32 (abfd, (bfd_byte *) x_dynp_32->d_un.d_ptr); | |
573 | } | |
65728c26 | 574 | else |
3a40aaa0 UW |
575 | { |
576 | x_dynp_64 = (Elf64_External_Dyn *) buf; | |
577 | dyn_tag = bfd_h_get_64 (abfd, (bfd_byte *) x_dynp_64->d_tag); | |
578 | dyn_ptr = bfd_h_get_64 (abfd, (bfd_byte *) x_dynp_64->d_un.d_ptr); | |
579 | } | |
580 | if (dyn_tag == DT_NULL) | |
581 | return 0; | |
582 | if (dyn_tag == dyntag) | |
583 | { | |
65728c26 DJ |
584 | /* If requested, try to read the runtime value of this .dynamic |
585 | entry. */ | |
3a40aaa0 | 586 | if (ptr) |
65728c26 | 587 | { |
b6da22b0 | 588 | struct type *ptr_type; |
65728c26 DJ |
589 | gdb_byte ptr_buf[8]; |
590 | CORE_ADDR ptr_addr; | |
591 | ||
b6da22b0 | 592 | ptr_type = builtin_type (target_gdbarch)->builtin_data_ptr; |
b381ea14 | 593 | ptr_addr = dyn_addr + (buf - bufstart) + arch_size / 8; |
65728c26 | 594 | if (target_read_memory (ptr_addr, ptr_buf, arch_size / 8) == 0) |
b6da22b0 | 595 | dyn_ptr = extract_typed_address (ptr_buf, ptr_type); |
65728c26 DJ |
596 | *ptr = dyn_ptr; |
597 | } | |
598 | return 1; | |
3a40aaa0 UW |
599 | } |
600 | } | |
601 | ||
602 | return 0; | |
603 | } | |
604 | ||
97ec2c2f UW |
605 | /* Scan for DYNTAG in .dynamic section of the target's main executable, |
606 | found by consulting the OS auxillary vector. If DYNTAG is found 1 is | |
607 | returned and the corresponding PTR is set. */ | |
608 | ||
609 | static int | |
610 | scan_dyntag_auxv (int dyntag, CORE_ADDR *ptr) | |
611 | { | |
e17a4113 | 612 | enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch); |
97ec2c2f UW |
613 | int sect_size, arch_size, step; |
614 | long dyn_tag; | |
615 | CORE_ADDR dyn_ptr; | |
616 | gdb_byte *bufend, *bufstart, *buf; | |
617 | ||
618 | /* Read in .dynamic section. */ | |
619 | buf = bufstart = read_program_header (PT_DYNAMIC, §_size, &arch_size); | |
620 | if (!buf) | |
621 | return 0; | |
622 | ||
623 | /* Iterate over BUF and scan for DYNTAG. If found, set PTR and return. */ | |
624 | step = (arch_size == 32) ? sizeof (Elf32_External_Dyn) | |
625 | : sizeof (Elf64_External_Dyn); | |
626 | for (bufend = buf + sect_size; | |
627 | buf < bufend; | |
628 | buf += step) | |
629 | { | |
630 | if (arch_size == 32) | |
631 | { | |
632 | Elf32_External_Dyn *dynp = (Elf32_External_Dyn *) buf; | |
433759f7 | 633 | |
e17a4113 UW |
634 | dyn_tag = extract_unsigned_integer ((gdb_byte *) dynp->d_tag, |
635 | 4, byte_order); | |
636 | dyn_ptr = extract_unsigned_integer ((gdb_byte *) dynp->d_un.d_ptr, | |
637 | 4, byte_order); | |
97ec2c2f UW |
638 | } |
639 | else | |
640 | { | |
641 | Elf64_External_Dyn *dynp = (Elf64_External_Dyn *) buf; | |
433759f7 | 642 | |
e17a4113 UW |
643 | dyn_tag = extract_unsigned_integer ((gdb_byte *) dynp->d_tag, |
644 | 8, byte_order); | |
645 | dyn_ptr = extract_unsigned_integer ((gdb_byte *) dynp->d_un.d_ptr, | |
646 | 8, byte_order); | |
97ec2c2f UW |
647 | } |
648 | if (dyn_tag == DT_NULL) | |
649 | break; | |
650 | ||
651 | if (dyn_tag == dyntag) | |
652 | { | |
653 | if (ptr) | |
654 | *ptr = dyn_ptr; | |
655 | ||
656 | xfree (bufstart); | |
657 | return 1; | |
658 | } | |
659 | } | |
660 | ||
661 | xfree (bufstart); | |
662 | return 0; | |
663 | } | |
664 | ||
7f86f058 PA |
665 | /* Locate the base address of dynamic linker structs for SVR4 elf |
666 | targets. | |
13437d4b KB |
667 | |
668 | For SVR4 elf targets the address of the dynamic linker's runtime | |
669 | structure is contained within the dynamic info section in the | |
670 | executable file. The dynamic section is also mapped into the | |
671 | inferior address space. Because the runtime loader fills in the | |
672 | real address before starting the inferior, we have to read in the | |
673 | dynamic info section from the inferior address space. | |
674 | If there are any errors while trying to find the address, we | |
7f86f058 | 675 | silently return 0, otherwise the found address is returned. */ |
13437d4b KB |
676 | |
677 | static CORE_ADDR | |
678 | elf_locate_base (void) | |
679 | { | |
3a40aaa0 UW |
680 | struct minimal_symbol *msymbol; |
681 | CORE_ADDR dyn_ptr; | |
13437d4b | 682 | |
65728c26 DJ |
683 | /* Look for DT_MIPS_RLD_MAP first. MIPS executables use this |
684 | instead of DT_DEBUG, although they sometimes contain an unused | |
685 | DT_DEBUG. */ | |
97ec2c2f UW |
686 | if (scan_dyntag (DT_MIPS_RLD_MAP, exec_bfd, &dyn_ptr) |
687 | || scan_dyntag_auxv (DT_MIPS_RLD_MAP, &dyn_ptr)) | |
3a40aaa0 | 688 | { |
b6da22b0 | 689 | struct type *ptr_type = builtin_type (target_gdbarch)->builtin_data_ptr; |
3a40aaa0 | 690 | gdb_byte *pbuf; |
b6da22b0 | 691 | int pbuf_size = TYPE_LENGTH (ptr_type); |
433759f7 | 692 | |
3a40aaa0 UW |
693 | pbuf = alloca (pbuf_size); |
694 | /* DT_MIPS_RLD_MAP contains a pointer to the address | |
695 | of the dynamic link structure. */ | |
696 | if (target_read_memory (dyn_ptr, pbuf, pbuf_size)) | |
e499d0f1 | 697 | return 0; |
b6da22b0 | 698 | return extract_typed_address (pbuf, ptr_type); |
e499d0f1 DJ |
699 | } |
700 | ||
65728c26 | 701 | /* Find DT_DEBUG. */ |
97ec2c2f UW |
702 | if (scan_dyntag (DT_DEBUG, exec_bfd, &dyn_ptr) |
703 | || scan_dyntag_auxv (DT_DEBUG, &dyn_ptr)) | |
65728c26 DJ |
704 | return dyn_ptr; |
705 | ||
3a40aaa0 UW |
706 | /* This may be a static executable. Look for the symbol |
707 | conventionally named _r_debug, as a last resort. */ | |
708 | msymbol = lookup_minimal_symbol ("_r_debug", NULL, symfile_objfile); | |
709 | if (msymbol != NULL) | |
710 | return SYMBOL_VALUE_ADDRESS (msymbol); | |
13437d4b KB |
711 | |
712 | /* DT_DEBUG entry not found. */ | |
713 | return 0; | |
714 | } | |
715 | ||
7f86f058 | 716 | /* Locate the base address of dynamic linker structs. |
13437d4b KB |
717 | |
718 | For both the SunOS and SVR4 shared library implementations, if the | |
719 | inferior executable has been linked dynamically, there is a single | |
720 | address somewhere in the inferior's data space which is the key to | |
721 | locating all of the dynamic linker's runtime structures. This | |
722 | address is the value of the debug base symbol. The job of this | |
723 | function is to find and return that address, or to return 0 if there | |
724 | is no such address (the executable is statically linked for example). | |
725 | ||
726 | For SunOS, the job is almost trivial, since the dynamic linker and | |
727 | all of it's structures are statically linked to the executable at | |
728 | link time. Thus the symbol for the address we are looking for has | |
729 | already been added to the minimal symbol table for the executable's | |
730 | objfile at the time the symbol file's symbols were read, and all we | |
731 | have to do is look it up there. Note that we explicitly do NOT want | |
732 | to find the copies in the shared library. | |
733 | ||
734 | The SVR4 version is a bit more complicated because the address | |
735 | is contained somewhere in the dynamic info section. We have to go | |
736 | to a lot more work to discover the address of the debug base symbol. | |
737 | Because of this complexity, we cache the value we find and return that | |
738 | value on subsequent invocations. Note there is no copy in the | |
7f86f058 | 739 | executable symbol tables. */ |
13437d4b KB |
740 | |
741 | static CORE_ADDR | |
1a816a87 | 742 | locate_base (struct svr4_info *info) |
13437d4b | 743 | { |
13437d4b KB |
744 | /* Check to see if we have a currently valid address, and if so, avoid |
745 | doing all this work again and just return the cached address. If | |
746 | we have no cached address, try to locate it in the dynamic info | |
d5a921c9 KB |
747 | section for ELF executables. There's no point in doing any of this |
748 | though if we don't have some link map offsets to work with. */ | |
13437d4b | 749 | |
1a816a87 | 750 | if (info->debug_base == 0 && svr4_have_link_map_offsets ()) |
0763ab81 | 751 | info->debug_base = elf_locate_base (); |
1a816a87 | 752 | return info->debug_base; |
13437d4b KB |
753 | } |
754 | ||
e4cd0d6a | 755 | /* Find the first element in the inferior's dynamic link map, and |
6f992fbf JB |
756 | return its address in the inferior. Return zero if the address |
757 | could not be determined. | |
13437d4b | 758 | |
e4cd0d6a MK |
759 | FIXME: Perhaps we should validate the info somehow, perhaps by |
760 | checking r_version for a known version number, or r_state for | |
761 | RT_CONSISTENT. */ | |
13437d4b KB |
762 | |
763 | static CORE_ADDR | |
1a816a87 | 764 | solib_svr4_r_map (struct svr4_info *info) |
13437d4b | 765 | { |
4b188b9f | 766 | struct link_map_offsets *lmo = svr4_fetch_link_map_offsets (); |
b6da22b0 | 767 | struct type *ptr_type = builtin_type (target_gdbarch)->builtin_data_ptr; |
08597104 JB |
768 | CORE_ADDR addr = 0; |
769 | volatile struct gdb_exception ex; | |
13437d4b | 770 | |
08597104 JB |
771 | TRY_CATCH (ex, RETURN_MASK_ERROR) |
772 | { | |
773 | addr = read_memory_typed_address (info->debug_base + lmo->r_map_offset, | |
774 | ptr_type); | |
775 | } | |
776 | exception_print (gdb_stderr, ex); | |
777 | return addr; | |
e4cd0d6a | 778 | } |
13437d4b | 779 | |
7cd25cfc DJ |
780 | /* Find r_brk from the inferior's debug base. */ |
781 | ||
782 | static CORE_ADDR | |
1a816a87 | 783 | solib_svr4_r_brk (struct svr4_info *info) |
7cd25cfc DJ |
784 | { |
785 | struct link_map_offsets *lmo = svr4_fetch_link_map_offsets (); | |
b6da22b0 | 786 | struct type *ptr_type = builtin_type (target_gdbarch)->builtin_data_ptr; |
7cd25cfc | 787 | |
1a816a87 PA |
788 | return read_memory_typed_address (info->debug_base + lmo->r_brk_offset, |
789 | ptr_type); | |
7cd25cfc DJ |
790 | } |
791 | ||
e4cd0d6a MK |
792 | /* Find the link map for the dynamic linker (if it is not in the |
793 | normal list of loaded shared objects). */ | |
13437d4b | 794 | |
e4cd0d6a | 795 | static CORE_ADDR |
1a816a87 | 796 | solib_svr4_r_ldsomap (struct svr4_info *info) |
e4cd0d6a MK |
797 | { |
798 | struct link_map_offsets *lmo = svr4_fetch_link_map_offsets (); | |
b6da22b0 | 799 | struct type *ptr_type = builtin_type (target_gdbarch)->builtin_data_ptr; |
e17a4113 | 800 | enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch); |
e4cd0d6a | 801 | ULONGEST version; |
13437d4b | 802 | |
e4cd0d6a MK |
803 | /* Check version, and return zero if `struct r_debug' doesn't have |
804 | the r_ldsomap member. */ | |
1a816a87 PA |
805 | version |
806 | = read_memory_unsigned_integer (info->debug_base + lmo->r_version_offset, | |
e17a4113 | 807 | lmo->r_version_size, byte_order); |
e4cd0d6a MK |
808 | if (version < 2 || lmo->r_ldsomap_offset == -1) |
809 | return 0; | |
13437d4b | 810 | |
1a816a87 | 811 | return read_memory_typed_address (info->debug_base + lmo->r_ldsomap_offset, |
b6da22b0 | 812 | ptr_type); |
13437d4b KB |
813 | } |
814 | ||
de18c1d8 JM |
815 | /* On Solaris systems with some versions of the dynamic linker, |
816 | ld.so's l_name pointer points to the SONAME in the string table | |
817 | rather than into writable memory. So that GDB can find shared | |
818 | libraries when loading a core file generated by gcore, ensure that | |
819 | memory areas containing the l_name string are saved in the core | |
820 | file. */ | |
821 | ||
822 | static int | |
823 | svr4_keep_data_in_core (CORE_ADDR vaddr, unsigned long size) | |
824 | { | |
825 | struct svr4_info *info; | |
826 | CORE_ADDR ldsomap; | |
827 | struct so_list *new; | |
828 | struct cleanup *old_chain; | |
829 | struct link_map_offsets *lmo; | |
74de0234 | 830 | CORE_ADDR name_lm; |
de18c1d8 JM |
831 | |
832 | info = get_svr4_info (); | |
833 | ||
834 | info->debug_base = 0; | |
835 | locate_base (info); | |
836 | if (!info->debug_base) | |
837 | return 0; | |
838 | ||
839 | ldsomap = solib_svr4_r_ldsomap (info); | |
840 | if (!ldsomap) | |
841 | return 0; | |
842 | ||
843 | lmo = svr4_fetch_link_map_offsets (); | |
844 | new = XZALLOC (struct so_list); | |
845 | old_chain = make_cleanup (xfree, new); | |
846 | new->lm_info = xmalloc (sizeof (struct lm_info)); | |
847 | make_cleanup (xfree, new->lm_info); | |
848 | new->lm_info->l_addr = (CORE_ADDR)-1; | |
849 | new->lm_info->lm_addr = ldsomap; | |
850 | new->lm_info->lm = xzalloc (lmo->link_map_size); | |
851 | make_cleanup (xfree, new->lm_info->lm); | |
852 | read_memory (ldsomap, new->lm_info->lm, lmo->link_map_size); | |
74de0234 | 853 | name_lm = lm_name (new); |
de18c1d8 JM |
854 | do_cleanups (old_chain); |
855 | ||
74de0234 | 856 | return (name_lm >= vaddr && name_lm < vaddr + size); |
de18c1d8 JM |
857 | } |
858 | ||
7f86f058 | 859 | /* Implement the "open_symbol_file_object" target_so_ops method. |
13437d4b | 860 | |
7f86f058 PA |
861 | If no open symbol file, attempt to locate and open the main symbol |
862 | file. On SVR4 systems, this is the first link map entry. If its | |
863 | name is here, we can open it. Useful when attaching to a process | |
864 | without first loading its symbol file. */ | |
13437d4b KB |
865 | |
866 | static int | |
867 | open_symbol_file_object (void *from_ttyp) | |
868 | { | |
869 | CORE_ADDR lm, l_name; | |
870 | char *filename; | |
871 | int errcode; | |
872 | int from_tty = *(int *)from_ttyp; | |
4b188b9f | 873 | struct link_map_offsets *lmo = svr4_fetch_link_map_offsets (); |
b6da22b0 UW |
874 | struct type *ptr_type = builtin_type (target_gdbarch)->builtin_data_ptr; |
875 | int l_name_size = TYPE_LENGTH (ptr_type); | |
cfaefc65 | 876 | gdb_byte *l_name_buf = xmalloc (l_name_size); |
b8c9b27d | 877 | struct cleanup *cleanups = make_cleanup (xfree, l_name_buf); |
6c95b8df | 878 | struct svr4_info *info = get_svr4_info (); |
13437d4b KB |
879 | |
880 | if (symfile_objfile) | |
9e2f0ad4 | 881 | if (!query (_("Attempt to reload symbols from process? "))) |
3bb47e8b TT |
882 | { |
883 | do_cleanups (cleanups); | |
884 | return 0; | |
885 | } | |
13437d4b | 886 | |
7cd25cfc | 887 | /* Always locate the debug struct, in case it has moved. */ |
1a816a87 PA |
888 | info->debug_base = 0; |
889 | if (locate_base (info) == 0) | |
3bb47e8b TT |
890 | { |
891 | do_cleanups (cleanups); | |
892 | return 0; /* failed somehow... */ | |
893 | } | |
13437d4b KB |
894 | |
895 | /* First link map member should be the executable. */ | |
1a816a87 | 896 | lm = solib_svr4_r_map (info); |
e4cd0d6a | 897 | if (lm == 0) |
3bb47e8b TT |
898 | { |
899 | do_cleanups (cleanups); | |
900 | return 0; /* failed somehow... */ | |
901 | } | |
13437d4b KB |
902 | |
903 | /* Read address of name from target memory to GDB. */ | |
cfaefc65 | 904 | read_memory (lm + lmo->l_name_offset, l_name_buf, l_name_size); |
13437d4b | 905 | |
cfaefc65 | 906 | /* Convert the address to host format. */ |
b6da22b0 | 907 | l_name = extract_typed_address (l_name_buf, ptr_type); |
13437d4b | 908 | |
13437d4b | 909 | if (l_name == 0) |
3bb47e8b TT |
910 | { |
911 | do_cleanups (cleanups); | |
912 | return 0; /* No filename. */ | |
913 | } | |
13437d4b KB |
914 | |
915 | /* Now fetch the filename from target memory. */ | |
916 | target_read_string (l_name, &filename, SO_NAME_MAX_PATH_SIZE - 1, &errcode); | |
ea5bf0a1 | 917 | make_cleanup (xfree, filename); |
13437d4b KB |
918 | |
919 | if (errcode) | |
920 | { | |
8a3fe4f8 | 921 | warning (_("failed to read exec filename from attached file: %s"), |
13437d4b | 922 | safe_strerror (errcode)); |
3bb47e8b | 923 | do_cleanups (cleanups); |
13437d4b KB |
924 | return 0; |
925 | } | |
926 | ||
13437d4b | 927 | /* Have a pathname: read the symbol file. */ |
1adeb98a | 928 | symbol_file_add_main (filename, from_tty); |
13437d4b | 929 | |
3bb47e8b | 930 | do_cleanups (cleanups); |
13437d4b KB |
931 | return 1; |
932 | } | |
13437d4b | 933 | |
cb08cc53 JK |
934 | /* Implementation for target_so_ops.free_so. */ |
935 | ||
936 | static void | |
937 | svr4_free_so (struct so_list *so) | |
938 | { | |
939 | if (so->lm_info) | |
940 | xfree (so->lm_info->lm); | |
941 | xfree (so->lm_info); | |
942 | } | |
943 | ||
944 | /* Free so_list built so far (called via cleanup). */ | |
945 | ||
946 | static void | |
947 | svr4_free_library_list (void *p_list) | |
948 | { | |
949 | struct so_list *list = *(struct so_list **) p_list; | |
950 | ||
951 | while (list != NULL) | |
952 | { | |
953 | struct so_list *next = list->next; | |
954 | ||
955 | svr4_free_so (list); | |
956 | list = next; | |
957 | } | |
958 | } | |
959 | ||
34439770 DJ |
960 | /* If no shared library information is available from the dynamic |
961 | linker, build a fallback list from other sources. */ | |
962 | ||
963 | static struct so_list * | |
964 | svr4_default_sos (void) | |
965 | { | |
6c95b8df | 966 | struct svr4_info *info = get_svr4_info (); |
8e5c319d | 967 | struct so_list *new; |
1a816a87 | 968 | |
8e5c319d JK |
969 | if (!info->debug_loader_offset_p) |
970 | return NULL; | |
34439770 | 971 | |
8e5c319d | 972 | new = XZALLOC (struct so_list); |
34439770 | 973 | |
8e5c319d | 974 | new->lm_info = xmalloc (sizeof (struct lm_info)); |
34439770 | 975 | |
8e5c319d JK |
976 | /* Nothing will ever check the cached copy of the link |
977 | map if we set l_addr. */ | |
978 | new->lm_info->l_addr = info->debug_loader_offset; | |
979 | new->lm_info->lm_addr = 0; | |
980 | new->lm_info->lm = NULL; | |
34439770 | 981 | |
8e5c319d JK |
982 | strncpy (new->so_name, info->debug_loader_name, SO_NAME_MAX_PATH_SIZE - 1); |
983 | new->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0'; | |
984 | strcpy (new->so_original_name, new->so_name); | |
34439770 | 985 | |
8e5c319d | 986 | return new; |
34439770 DJ |
987 | } |
988 | ||
cb08cc53 JK |
989 | /* Read the whole inferior libraries chain starting at address LM. Add the |
990 | entries to the tail referenced by LINK_PTR_PTR. Ignore the first entry if | |
991 | IGNORE_FIRST and set global MAIN_LM_ADDR according to it. */ | |
13437d4b | 992 | |
cb08cc53 JK |
993 | static void |
994 | svr4_read_so_list (CORE_ADDR lm, struct so_list ***link_ptr_ptr, | |
995 | int ignore_first) | |
13437d4b | 996 | { |
cb08cc53 | 997 | CORE_ADDR prev_lm = 0, next_lm; |
13437d4b | 998 | |
cb08cc53 | 999 | for (; lm != 0; prev_lm = lm, lm = next_lm) |
13437d4b | 1000 | { |
4b188b9f | 1001 | struct link_map_offsets *lmo = svr4_fetch_link_map_offsets (); |
cb08cc53 JK |
1002 | struct so_list *new; |
1003 | struct cleanup *old_chain; | |
1004 | int errcode; | |
1005 | char *buffer; | |
13437d4b | 1006 | |
cb08cc53 JK |
1007 | new = XZALLOC (struct so_list); |
1008 | old_chain = make_cleanup_free_so (new); | |
13437d4b | 1009 | |
cb08cc53 JK |
1010 | new->lm_info = xmalloc (sizeof (struct lm_info)); |
1011 | new->lm_info->l_addr = (CORE_ADDR) -1; | |
93a57060 | 1012 | new->lm_info->lm_addr = lm; |
f4456994 | 1013 | new->lm_info->lm = xzalloc (lmo->link_map_size); |
13437d4b KB |
1014 | |
1015 | read_memory (lm, new->lm_info->lm, lmo->link_map_size); | |
1016 | ||
b23518f0 | 1017 | next_lm = lm_next (new); |
492928e4 | 1018 | |
b23518f0 | 1019 | if (lm_prev (new) != prev_lm) |
492928e4 JK |
1020 | { |
1021 | warning (_("Corrupted shared library list")); | |
cb08cc53 JK |
1022 | do_cleanups (old_chain); |
1023 | break; | |
492928e4 | 1024 | } |
13437d4b KB |
1025 | |
1026 | /* For SVR4 versions, the first entry in the link map is for the | |
1027 | inferior executable, so we must ignore it. For some versions of | |
1028 | SVR4, it has no name. For others (Solaris 2.3 for example), it | |
1029 | does have a name, so we can no longer use a missing name to | |
c378eb4e | 1030 | decide when to ignore it. */ |
cb08cc53 | 1031 | if (ignore_first && lm_prev (new) == 0) |
93a57060 | 1032 | { |
cb08cc53 JK |
1033 | struct svr4_info *info = get_svr4_info (); |
1034 | ||
1a816a87 | 1035 | info->main_lm_addr = new->lm_info->lm_addr; |
cb08cc53 JK |
1036 | do_cleanups (old_chain); |
1037 | continue; | |
93a57060 | 1038 | } |
13437d4b | 1039 | |
cb08cc53 JK |
1040 | /* Extract this shared object's name. */ |
1041 | target_read_string (lm_name (new), &buffer, | |
1042 | SO_NAME_MAX_PATH_SIZE - 1, &errcode); | |
1043 | if (errcode != 0) | |
1044 | { | |
1045 | warning (_("Can't read pathname for load map: %s."), | |
1046 | safe_strerror (errcode)); | |
1047 | do_cleanups (old_chain); | |
1048 | continue; | |
13437d4b KB |
1049 | } |
1050 | ||
cb08cc53 JK |
1051 | strncpy (new->so_name, buffer, SO_NAME_MAX_PATH_SIZE - 1); |
1052 | new->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0'; | |
1053 | strcpy (new->so_original_name, new->so_name); | |
1054 | xfree (buffer); | |
492928e4 | 1055 | |
cb08cc53 JK |
1056 | /* If this entry has no name, or its name matches the name |
1057 | for the main executable, don't include it in the list. */ | |
1058 | if (! new->so_name[0] || match_main (new->so_name)) | |
492928e4 | 1059 | { |
cb08cc53 JK |
1060 | do_cleanups (old_chain); |
1061 | continue; | |
492928e4 | 1062 | } |
e4cd0d6a | 1063 | |
13437d4b | 1064 | discard_cleanups (old_chain); |
cb08cc53 JK |
1065 | new->next = 0; |
1066 | **link_ptr_ptr = new; | |
1067 | *link_ptr_ptr = &new->next; | |
13437d4b | 1068 | } |
cb08cc53 JK |
1069 | } |
1070 | ||
1071 | /* Implement the "current_sos" target_so_ops method. */ | |
1072 | ||
1073 | static struct so_list * | |
1074 | svr4_current_sos (void) | |
1075 | { | |
1076 | CORE_ADDR lm; | |
1077 | struct so_list *head = NULL; | |
1078 | struct so_list **link_ptr = &head; | |
1079 | struct svr4_info *info; | |
1080 | struct cleanup *back_to; | |
1081 | int ignore_first; | |
1082 | ||
1083 | info = get_svr4_info (); | |
1084 | ||
1085 | /* Always locate the debug struct, in case it has moved. */ | |
1086 | info->debug_base = 0; | |
1087 | locate_base (info); | |
1088 | ||
1089 | /* If we can't find the dynamic linker's base structure, this | |
1090 | must not be a dynamically linked executable. Hmm. */ | |
1091 | if (! info->debug_base) | |
1092 | return svr4_default_sos (); | |
1093 | ||
1094 | /* Assume that everything is a library if the dynamic loader was loaded | |
1095 | late by a static executable. */ | |
1096 | if (exec_bfd && bfd_get_section_by_name (exec_bfd, ".dynamic") == NULL) | |
1097 | ignore_first = 0; | |
1098 | else | |
1099 | ignore_first = 1; | |
1100 | ||
1101 | back_to = make_cleanup (svr4_free_library_list, &head); | |
1102 | ||
1103 | /* Walk the inferior's link map list, and build our list of | |
1104 | `struct so_list' nodes. */ | |
1105 | lm = solib_svr4_r_map (info); | |
1106 | if (lm) | |
1107 | svr4_read_so_list (lm, &link_ptr, ignore_first); | |
1108 | ||
1109 | /* On Solaris, the dynamic linker is not in the normal list of | |
1110 | shared objects, so make sure we pick it up too. Having | |
1111 | symbol information for the dynamic linker is quite crucial | |
1112 | for skipping dynamic linker resolver code. */ | |
1113 | lm = solib_svr4_r_ldsomap (info); | |
1114 | if (lm) | |
1115 | svr4_read_so_list (lm, &link_ptr, 0); | |
1116 | ||
1117 | discard_cleanups (back_to); | |
13437d4b | 1118 | |
34439770 DJ |
1119 | if (head == NULL) |
1120 | return svr4_default_sos (); | |
1121 | ||
13437d4b KB |
1122 | return head; |
1123 | } | |
1124 | ||
93a57060 | 1125 | /* Get the address of the link_map for a given OBJFILE. */ |
bc4a16ae EZ |
1126 | |
1127 | CORE_ADDR | |
1128 | svr4_fetch_objfile_link_map (struct objfile *objfile) | |
1129 | { | |
93a57060 | 1130 | struct so_list *so; |
6c95b8df | 1131 | struct svr4_info *info = get_svr4_info (); |
bc4a16ae | 1132 | |
93a57060 | 1133 | /* Cause svr4_current_sos() to be run if it hasn't been already. */ |
1a816a87 | 1134 | if (info->main_lm_addr == 0) |
93a57060 | 1135 | solib_add (NULL, 0, ¤t_target, auto_solib_add); |
bc4a16ae | 1136 | |
93a57060 DJ |
1137 | /* svr4_current_sos() will set main_lm_addr for the main executable. */ |
1138 | if (objfile == symfile_objfile) | |
1a816a87 | 1139 | return info->main_lm_addr; |
93a57060 DJ |
1140 | |
1141 | /* The other link map addresses may be found by examining the list | |
1142 | of shared libraries. */ | |
1143 | for (so = master_so_list (); so; so = so->next) | |
1144 | if (so->objfile == objfile) | |
1145 | return so->lm_info->lm_addr; | |
1146 | ||
1147 | /* Not found! */ | |
bc4a16ae EZ |
1148 | return 0; |
1149 | } | |
13437d4b KB |
1150 | |
1151 | /* On some systems, the only way to recognize the link map entry for | |
1152 | the main executable file is by looking at its name. Return | |
1153 | non-zero iff SONAME matches one of the known main executable names. */ | |
1154 | ||
1155 | static int | |
bc043ef3 | 1156 | match_main (const char *soname) |
13437d4b | 1157 | { |
bc043ef3 | 1158 | const char * const *mainp; |
13437d4b KB |
1159 | |
1160 | for (mainp = main_name_list; *mainp != NULL; mainp++) | |
1161 | { | |
1162 | if (strcmp (soname, *mainp) == 0) | |
1163 | return (1); | |
1164 | } | |
1165 | ||
1166 | return (0); | |
1167 | } | |
1168 | ||
13437d4b KB |
1169 | /* Return 1 if PC lies in the dynamic symbol resolution code of the |
1170 | SVR4 run time loader. */ | |
13437d4b | 1171 | |
7d522c90 | 1172 | int |
d7fa2ae2 | 1173 | svr4_in_dynsym_resolve_code (CORE_ADDR pc) |
13437d4b | 1174 | { |
6c95b8df PA |
1175 | struct svr4_info *info = get_svr4_info (); |
1176 | ||
1177 | return ((pc >= info->interp_text_sect_low | |
1178 | && pc < info->interp_text_sect_high) | |
1179 | || (pc >= info->interp_plt_sect_low | |
1180 | && pc < info->interp_plt_sect_high) | |
0875794a JK |
1181 | || in_plt_section (pc, NULL) |
1182 | || in_gnu_ifunc_stub (pc)); | |
13437d4b | 1183 | } |
13437d4b | 1184 | |
2f4950cd AC |
1185 | /* Given an executable's ABFD and target, compute the entry-point |
1186 | address. */ | |
1187 | ||
1188 | static CORE_ADDR | |
1189 | exec_entry_point (struct bfd *abfd, struct target_ops *targ) | |
1190 | { | |
1191 | /* KevinB wrote ... for most targets, the address returned by | |
1192 | bfd_get_start_address() is the entry point for the start | |
1193 | function. But, for some targets, bfd_get_start_address() returns | |
1194 | the address of a function descriptor from which the entry point | |
1195 | address may be extracted. This address is extracted by | |
1196 | gdbarch_convert_from_func_ptr_addr(). The method | |
1197 | gdbarch_convert_from_func_ptr_addr() is the merely the identify | |
1198 | function for targets which don't use function descriptors. */ | |
1cf3db46 | 1199 | return gdbarch_convert_from_func_ptr_addr (target_gdbarch, |
2f4950cd AC |
1200 | bfd_get_start_address (abfd), |
1201 | targ); | |
1202 | } | |
13437d4b | 1203 | |
cb457ae2 YQ |
1204 | /* Helper function for gdb_bfd_lookup_symbol. */ |
1205 | ||
1206 | static int | |
1207 | cmp_name_and_sec_flags (asymbol *sym, void *data) | |
1208 | { | |
1209 | return (strcmp (sym->name, (const char *) data) == 0 | |
1210 | && (sym->section->flags & (SEC_CODE | SEC_DATA)) != 0); | |
1211 | } | |
7f86f058 | 1212 | /* Arrange for dynamic linker to hit breakpoint. |
13437d4b KB |
1213 | |
1214 | Both the SunOS and the SVR4 dynamic linkers have, as part of their | |
1215 | debugger interface, support for arranging for the inferior to hit | |
1216 | a breakpoint after mapping in the shared libraries. This function | |
1217 | enables that breakpoint. | |
1218 | ||
1219 | For SunOS, there is a special flag location (in_debugger) which we | |
1220 | set to 1. When the dynamic linker sees this flag set, it will set | |
1221 | a breakpoint at a location known only to itself, after saving the | |
1222 | original contents of that place and the breakpoint address itself, | |
1223 | in it's own internal structures. When we resume the inferior, it | |
1224 | will eventually take a SIGTRAP when it runs into the breakpoint. | |
1225 | We handle this (in a different place) by restoring the contents of | |
1226 | the breakpointed location (which is only known after it stops), | |
1227 | chasing around to locate the shared libraries that have been | |
1228 | loaded, then resuming. | |
1229 | ||
1230 | For SVR4, the debugger interface structure contains a member (r_brk) | |
1231 | which is statically initialized at the time the shared library is | |
1232 | built, to the offset of a function (_r_debug_state) which is guaran- | |
1233 | teed to be called once before mapping in a library, and again when | |
1234 | the mapping is complete. At the time we are examining this member, | |
1235 | it contains only the unrelocated offset of the function, so we have | |
1236 | to do our own relocation. Later, when the dynamic linker actually | |
1237 | runs, it relocates r_brk to be the actual address of _r_debug_state(). | |
1238 | ||
1239 | The debugger interface structure also contains an enumeration which | |
1240 | is set to either RT_ADD or RT_DELETE prior to changing the mapping, | |
1241 | depending upon whether or not the library is being mapped or unmapped, | |
7f86f058 | 1242 | and then set to RT_CONSISTENT after the library is mapped/unmapped. */ |
13437d4b KB |
1243 | |
1244 | static int | |
268a4a75 | 1245 | enable_break (struct svr4_info *info, int from_tty) |
13437d4b | 1246 | { |
13437d4b | 1247 | struct minimal_symbol *msymbol; |
bc043ef3 | 1248 | const char * const *bkpt_namep; |
13437d4b | 1249 | asection *interp_sect; |
97ec2c2f | 1250 | gdb_byte *interp_name; |
7cd25cfc | 1251 | CORE_ADDR sym_addr; |
13437d4b | 1252 | |
6c95b8df PA |
1253 | info->interp_text_sect_low = info->interp_text_sect_high = 0; |
1254 | info->interp_plt_sect_low = info->interp_plt_sect_high = 0; | |
13437d4b | 1255 | |
7cd25cfc DJ |
1256 | /* If we already have a shared library list in the target, and |
1257 | r_debug contains r_brk, set the breakpoint there - this should | |
1258 | mean r_brk has already been relocated. Assume the dynamic linker | |
1259 | is the object containing r_brk. */ | |
1260 | ||
268a4a75 | 1261 | solib_add (NULL, from_tty, ¤t_target, auto_solib_add); |
7cd25cfc | 1262 | sym_addr = 0; |
1a816a87 PA |
1263 | if (info->debug_base && solib_svr4_r_map (info) != 0) |
1264 | sym_addr = solib_svr4_r_brk (info); | |
7cd25cfc DJ |
1265 | |
1266 | if (sym_addr != 0) | |
1267 | { | |
1268 | struct obj_section *os; | |
1269 | ||
b36ec657 | 1270 | sym_addr = gdbarch_addr_bits_remove |
1cf3db46 | 1271 | (target_gdbarch, gdbarch_convert_from_func_ptr_addr (target_gdbarch, |
3e43a32a MS |
1272 | sym_addr, |
1273 | ¤t_target)); | |
b36ec657 | 1274 | |
48379de6 DE |
1275 | /* On at least some versions of Solaris there's a dynamic relocation |
1276 | on _r_debug.r_brk and SYM_ADDR may not be relocated yet, e.g., if | |
1277 | we get control before the dynamic linker has self-relocated. | |
1278 | Check if SYM_ADDR is in a known section, if it is assume we can | |
1279 | trust its value. This is just a heuristic though, it could go away | |
1280 | or be replaced if it's getting in the way. | |
1281 | ||
1282 | On ARM we need to know whether the ISA of rtld_db_dlactivity (or | |
1283 | however it's spelled in your particular system) is ARM or Thumb. | |
1284 | That knowledge is encoded in the address, if it's Thumb the low bit | |
1285 | is 1. However, we've stripped that info above and it's not clear | |
1286 | what all the consequences are of passing a non-addr_bits_remove'd | |
1287 | address to create_solib_event_breakpoint. The call to | |
1288 | find_pc_section verifies we know about the address and have some | |
1289 | hope of computing the right kind of breakpoint to use (via | |
1290 | symbol info). It does mean that GDB needs to be pointed at a | |
1291 | non-stripped version of the dynamic linker in order to obtain | |
1292 | information it already knows about. Sigh. */ | |
1293 | ||
7cd25cfc DJ |
1294 | os = find_pc_section (sym_addr); |
1295 | if (os != NULL) | |
1296 | { | |
1297 | /* Record the relocated start and end address of the dynamic linker | |
1298 | text and plt section for svr4_in_dynsym_resolve_code. */ | |
1299 | bfd *tmp_bfd; | |
1300 | CORE_ADDR load_addr; | |
1301 | ||
1302 | tmp_bfd = os->objfile->obfd; | |
1303 | load_addr = ANOFFSET (os->objfile->section_offsets, | |
1304 | os->objfile->sect_index_text); | |
1305 | ||
1306 | interp_sect = bfd_get_section_by_name (tmp_bfd, ".text"); | |
1307 | if (interp_sect) | |
1308 | { | |
6c95b8df | 1309 | info->interp_text_sect_low = |
7cd25cfc | 1310 | bfd_section_vma (tmp_bfd, interp_sect) + load_addr; |
6c95b8df PA |
1311 | info->interp_text_sect_high = |
1312 | info->interp_text_sect_low | |
1313 | + bfd_section_size (tmp_bfd, interp_sect); | |
7cd25cfc DJ |
1314 | } |
1315 | interp_sect = bfd_get_section_by_name (tmp_bfd, ".plt"); | |
1316 | if (interp_sect) | |
1317 | { | |
6c95b8df | 1318 | info->interp_plt_sect_low = |
7cd25cfc | 1319 | bfd_section_vma (tmp_bfd, interp_sect) + load_addr; |
6c95b8df PA |
1320 | info->interp_plt_sect_high = |
1321 | info->interp_plt_sect_low | |
1322 | + bfd_section_size (tmp_bfd, interp_sect); | |
7cd25cfc DJ |
1323 | } |
1324 | ||
a6d9a66e | 1325 | create_solib_event_breakpoint (target_gdbarch, sym_addr); |
7cd25cfc DJ |
1326 | return 1; |
1327 | } | |
1328 | } | |
1329 | ||
97ec2c2f | 1330 | /* Find the program interpreter; if not found, warn the user and drop |
13437d4b | 1331 | into the old breakpoint at symbol code. */ |
97ec2c2f UW |
1332 | interp_name = find_program_interpreter (); |
1333 | if (interp_name) | |
13437d4b | 1334 | { |
8ad2fcde KB |
1335 | CORE_ADDR load_addr = 0; |
1336 | int load_addr_found = 0; | |
2ec9a4f8 | 1337 | int loader_found_in_list = 0; |
f8766ec1 | 1338 | struct so_list *so; |
e4f7b8c8 | 1339 | bfd *tmp_bfd = NULL; |
2f4950cd | 1340 | struct target_ops *tmp_bfd_target; |
f1838a98 | 1341 | volatile struct gdb_exception ex; |
13437d4b | 1342 | |
7cd25cfc | 1343 | sym_addr = 0; |
13437d4b KB |
1344 | |
1345 | /* Now we need to figure out where the dynamic linker was | |
1346 | loaded so that we can load its symbols and place a breakpoint | |
1347 | in the dynamic linker itself. | |
1348 | ||
1349 | This address is stored on the stack. However, I've been unable | |
1350 | to find any magic formula to find it for Solaris (appears to | |
1351 | be trivial on GNU/Linux). Therefore, we have to try an alternate | |
1352 | mechanism to find the dynamic linker's base address. */ | |
e4f7b8c8 | 1353 | |
f1838a98 UW |
1354 | TRY_CATCH (ex, RETURN_MASK_ALL) |
1355 | { | |
97ec2c2f | 1356 | tmp_bfd = solib_bfd_open (interp_name); |
f1838a98 | 1357 | } |
13437d4b KB |
1358 | if (tmp_bfd == NULL) |
1359 | goto bkpt_at_symbol; | |
1360 | ||
2f4950cd AC |
1361 | /* Now convert the TMP_BFD into a target. That way target, as |
1362 | well as BFD operations can be used. Note that closing the | |
1363 | target will also close the underlying bfd. */ | |
1364 | tmp_bfd_target = target_bfd_reopen (tmp_bfd); | |
1365 | ||
f8766ec1 KB |
1366 | /* On a running target, we can get the dynamic linker's base |
1367 | address from the shared library table. */ | |
f8766ec1 KB |
1368 | so = master_so_list (); |
1369 | while (so) | |
8ad2fcde | 1370 | { |
97ec2c2f | 1371 | if (svr4_same_1 (interp_name, so->so_original_name)) |
8ad2fcde KB |
1372 | { |
1373 | load_addr_found = 1; | |
2ec9a4f8 | 1374 | loader_found_in_list = 1; |
b23518f0 | 1375 | load_addr = lm_addr_check (so, tmp_bfd); |
8ad2fcde KB |
1376 | break; |
1377 | } | |
f8766ec1 | 1378 | so = so->next; |
8ad2fcde KB |
1379 | } |
1380 | ||
8d4e36ba JB |
1381 | /* If we were not able to find the base address of the loader |
1382 | from our so_list, then try using the AT_BASE auxilliary entry. */ | |
1383 | if (!load_addr_found) | |
1384 | if (target_auxv_search (¤t_target, AT_BASE, &load_addr) > 0) | |
ad3a0e5b JK |
1385 | { |
1386 | int addr_bit = gdbarch_addr_bit (target_gdbarch); | |
1387 | ||
1388 | /* Ensure LOAD_ADDR has proper sign in its possible upper bits so | |
1389 | that `+ load_addr' will overflow CORE_ADDR width not creating | |
1390 | invalid addresses like 0x101234567 for 32bit inferiors on 64bit | |
1391 | GDB. */ | |
1392 | ||
d182d057 | 1393 | if (addr_bit < (sizeof (CORE_ADDR) * HOST_CHAR_BIT)) |
ad3a0e5b | 1394 | { |
d182d057 | 1395 | CORE_ADDR space_size = (CORE_ADDR) 1 << addr_bit; |
ad3a0e5b JK |
1396 | CORE_ADDR tmp_entry_point = exec_entry_point (tmp_bfd, |
1397 | tmp_bfd_target); | |
1398 | ||
1399 | gdb_assert (load_addr < space_size); | |
1400 | ||
1401 | /* TMP_ENTRY_POINT exceeding SPACE_SIZE would be for prelinked | |
1402 | 64bit ld.so with 32bit executable, it should not happen. */ | |
1403 | ||
1404 | if (tmp_entry_point < space_size | |
1405 | && tmp_entry_point + load_addr >= space_size) | |
1406 | load_addr -= space_size; | |
1407 | } | |
1408 | ||
1409 | load_addr_found = 1; | |
1410 | } | |
8d4e36ba | 1411 | |
8ad2fcde KB |
1412 | /* Otherwise we find the dynamic linker's base address by examining |
1413 | the current pc (which should point at the entry point for the | |
8d4e36ba JB |
1414 | dynamic linker) and subtracting the offset of the entry point. |
1415 | ||
1416 | This is more fragile than the previous approaches, but is a good | |
1417 | fallback method because it has actually been working well in | |
1418 | most cases. */ | |
8ad2fcde | 1419 | if (!load_addr_found) |
fb14de7b | 1420 | { |
c2250ad1 UW |
1421 | struct regcache *regcache |
1422 | = get_thread_arch_regcache (inferior_ptid, target_gdbarch); | |
433759f7 | 1423 | |
fb14de7b UW |
1424 | load_addr = (regcache_read_pc (regcache) |
1425 | - exec_entry_point (tmp_bfd, tmp_bfd_target)); | |
1426 | } | |
2ec9a4f8 DJ |
1427 | |
1428 | if (!loader_found_in_list) | |
34439770 | 1429 | { |
1a816a87 PA |
1430 | info->debug_loader_name = xstrdup (interp_name); |
1431 | info->debug_loader_offset_p = 1; | |
1432 | info->debug_loader_offset = load_addr; | |
268a4a75 | 1433 | solib_add (NULL, from_tty, ¤t_target, auto_solib_add); |
34439770 | 1434 | } |
13437d4b KB |
1435 | |
1436 | /* Record the relocated start and end address of the dynamic linker | |
d7fa2ae2 | 1437 | text and plt section for svr4_in_dynsym_resolve_code. */ |
13437d4b KB |
1438 | interp_sect = bfd_get_section_by_name (tmp_bfd, ".text"); |
1439 | if (interp_sect) | |
1440 | { | |
6c95b8df | 1441 | info->interp_text_sect_low = |
13437d4b | 1442 | bfd_section_vma (tmp_bfd, interp_sect) + load_addr; |
6c95b8df PA |
1443 | info->interp_text_sect_high = |
1444 | info->interp_text_sect_low | |
1445 | + bfd_section_size (tmp_bfd, interp_sect); | |
13437d4b KB |
1446 | } |
1447 | interp_sect = bfd_get_section_by_name (tmp_bfd, ".plt"); | |
1448 | if (interp_sect) | |
1449 | { | |
6c95b8df | 1450 | info->interp_plt_sect_low = |
13437d4b | 1451 | bfd_section_vma (tmp_bfd, interp_sect) + load_addr; |
6c95b8df PA |
1452 | info->interp_plt_sect_high = |
1453 | info->interp_plt_sect_low | |
1454 | + bfd_section_size (tmp_bfd, interp_sect); | |
13437d4b KB |
1455 | } |
1456 | ||
1457 | /* Now try to set a breakpoint in the dynamic linker. */ | |
1458 | for (bkpt_namep = solib_break_names; *bkpt_namep != NULL; bkpt_namep++) | |
1459 | { | |
cb457ae2 YQ |
1460 | sym_addr = gdb_bfd_lookup_symbol (tmp_bfd, cmp_name_and_sec_flags, |
1461 | (void *) *bkpt_namep); | |
13437d4b KB |
1462 | if (sym_addr != 0) |
1463 | break; | |
1464 | } | |
1465 | ||
2bbe3cc1 DJ |
1466 | if (sym_addr != 0) |
1467 | /* Convert 'sym_addr' from a function pointer to an address. | |
1468 | Because we pass tmp_bfd_target instead of the current | |
1469 | target, this will always produce an unrelocated value. */ | |
1cf3db46 | 1470 | sym_addr = gdbarch_convert_from_func_ptr_addr (target_gdbarch, |
2bbe3cc1 DJ |
1471 | sym_addr, |
1472 | tmp_bfd_target); | |
1473 | ||
2f4950cd AC |
1474 | /* We're done with both the temporary bfd and target. Remember, |
1475 | closing the target closes the underlying bfd. */ | |
1476 | target_close (tmp_bfd_target, 0); | |
13437d4b KB |
1477 | |
1478 | if (sym_addr != 0) | |
1479 | { | |
a6d9a66e | 1480 | create_solib_event_breakpoint (target_gdbarch, load_addr + sym_addr); |
97ec2c2f | 1481 | xfree (interp_name); |
13437d4b KB |
1482 | return 1; |
1483 | } | |
1484 | ||
1485 | /* For whatever reason we couldn't set a breakpoint in the dynamic | |
1486 | linker. Warn and drop into the old code. */ | |
1487 | bkpt_at_symbol: | |
97ec2c2f | 1488 | xfree (interp_name); |
82d03102 PG |
1489 | warning (_("Unable to find dynamic linker breakpoint function.\n" |
1490 | "GDB will be unable to debug shared library initializers\n" | |
1491 | "and track explicitly loaded dynamic code.")); | |
13437d4b | 1492 | } |
13437d4b | 1493 | |
e499d0f1 DJ |
1494 | /* Scan through the lists of symbols, trying to look up the symbol and |
1495 | set a breakpoint there. Terminate loop when we/if we succeed. */ | |
1496 | ||
1497 | for (bkpt_namep = solib_break_names; *bkpt_namep != NULL; bkpt_namep++) | |
1498 | { | |
1499 | msymbol = lookup_minimal_symbol (*bkpt_namep, NULL, symfile_objfile); | |
1500 | if ((msymbol != NULL) && (SYMBOL_VALUE_ADDRESS (msymbol) != 0)) | |
1501 | { | |
de64a9ac JM |
1502 | sym_addr = SYMBOL_VALUE_ADDRESS (msymbol); |
1503 | sym_addr = gdbarch_convert_from_func_ptr_addr (target_gdbarch, | |
1504 | sym_addr, | |
1505 | ¤t_target); | |
1506 | create_solib_event_breakpoint (target_gdbarch, sym_addr); | |
e499d0f1 DJ |
1507 | return 1; |
1508 | } | |
1509 | } | |
13437d4b | 1510 | |
c6490bf2 | 1511 | if (!current_inferior ()->attach_flag) |
13437d4b | 1512 | { |
c6490bf2 | 1513 | for (bkpt_namep = bkpt_names; *bkpt_namep != NULL; bkpt_namep++) |
13437d4b | 1514 | { |
c6490bf2 KB |
1515 | msymbol = lookup_minimal_symbol (*bkpt_namep, NULL, symfile_objfile); |
1516 | if ((msymbol != NULL) && (SYMBOL_VALUE_ADDRESS (msymbol) != 0)) | |
1517 | { | |
1518 | sym_addr = SYMBOL_VALUE_ADDRESS (msymbol); | |
1519 | sym_addr = gdbarch_convert_from_func_ptr_addr (target_gdbarch, | |
1520 | sym_addr, | |
1521 | ¤t_target); | |
1522 | create_solib_event_breakpoint (target_gdbarch, sym_addr); | |
1523 | return 1; | |
1524 | } | |
13437d4b KB |
1525 | } |
1526 | } | |
542c95c2 | 1527 | return 0; |
13437d4b KB |
1528 | } |
1529 | ||
7f86f058 | 1530 | /* Implement the "special_symbol_handling" target_so_ops method. */ |
13437d4b KB |
1531 | |
1532 | static void | |
1533 | svr4_special_symbol_handling (void) | |
1534 | { | |
7f86f058 | 1535 | /* Nothing to do. */ |
13437d4b KB |
1536 | } |
1537 | ||
09919ac2 JK |
1538 | /* Read the ELF program headers from ABFD. Return the contents and |
1539 | set *PHDRS_SIZE to the size of the program headers. */ | |
e2a44558 | 1540 | |
09919ac2 JK |
1541 | static gdb_byte * |
1542 | read_program_headers_from_bfd (bfd *abfd, int *phdrs_size) | |
e2a44558 | 1543 | { |
09919ac2 JK |
1544 | Elf_Internal_Ehdr *ehdr; |
1545 | gdb_byte *buf; | |
e2a44558 | 1546 | |
09919ac2 | 1547 | ehdr = elf_elfheader (abfd); |
b8040f19 | 1548 | |
09919ac2 JK |
1549 | *phdrs_size = ehdr->e_phnum * ehdr->e_phentsize; |
1550 | if (*phdrs_size == 0) | |
1551 | return NULL; | |
1552 | ||
1553 | buf = xmalloc (*phdrs_size); | |
1554 | if (bfd_seek (abfd, ehdr->e_phoff, SEEK_SET) != 0 | |
1555 | || bfd_bread (buf, *phdrs_size, abfd) != *phdrs_size) | |
1556 | { | |
1557 | xfree (buf); | |
1558 | return NULL; | |
1559 | } | |
1560 | ||
1561 | return buf; | |
b8040f19 JK |
1562 | } |
1563 | ||
01c30d6e JK |
1564 | /* Return 1 and fill *DISPLACEMENTP with detected PIE offset of inferior |
1565 | exec_bfd. Otherwise return 0. | |
1566 | ||
1567 | We relocate all of the sections by the same amount. This | |
c378eb4e | 1568 | behavior is mandated by recent editions of the System V ABI. |
b8040f19 JK |
1569 | According to the System V Application Binary Interface, |
1570 | Edition 4.1, page 5-5: | |
1571 | ||
1572 | ... Though the system chooses virtual addresses for | |
1573 | individual processes, it maintains the segments' relative | |
1574 | positions. Because position-independent code uses relative | |
1575 | addressesing between segments, the difference between | |
1576 | virtual addresses in memory must match the difference | |
1577 | between virtual addresses in the file. The difference | |
1578 | between the virtual address of any segment in memory and | |
1579 | the corresponding virtual address in the file is thus a | |
1580 | single constant value for any one executable or shared | |
1581 | object in a given process. This difference is the base | |
1582 | address. One use of the base address is to relocate the | |
1583 | memory image of the program during dynamic linking. | |
1584 | ||
1585 | The same language also appears in Edition 4.0 of the System V | |
09919ac2 JK |
1586 | ABI and is left unspecified in some of the earlier editions. |
1587 | ||
1588 | Decide if the objfile needs to be relocated. As indicated above, we will | |
1589 | only be here when execution is stopped. But during attachment PC can be at | |
1590 | arbitrary address therefore regcache_read_pc can be misleading (contrary to | |
1591 | the auxv AT_ENTRY value). Moreover for executable with interpreter section | |
1592 | regcache_read_pc would point to the interpreter and not the main executable. | |
1593 | ||
1594 | So, to summarize, relocations are necessary when the start address obtained | |
1595 | from the executable is different from the address in auxv AT_ENTRY entry. | |
d989b283 | 1596 | |
09919ac2 JK |
1597 | [ The astute reader will note that we also test to make sure that |
1598 | the executable in question has the DYNAMIC flag set. It is my | |
1599 | opinion that this test is unnecessary (undesirable even). It | |
1600 | was added to avoid inadvertent relocation of an executable | |
1601 | whose e_type member in the ELF header is not ET_DYN. There may | |
1602 | be a time in the future when it is desirable to do relocations | |
1603 | on other types of files as well in which case this condition | |
1604 | should either be removed or modified to accomodate the new file | |
1605 | type. - Kevin, Nov 2000. ] */ | |
b8040f19 | 1606 | |
01c30d6e JK |
1607 | static int |
1608 | svr4_exec_displacement (CORE_ADDR *displacementp) | |
b8040f19 | 1609 | { |
41752192 JK |
1610 | /* ENTRY_POINT is a possible function descriptor - before |
1611 | a call to gdbarch_convert_from_func_ptr_addr. */ | |
09919ac2 | 1612 | CORE_ADDR entry_point, displacement; |
b8040f19 JK |
1613 | |
1614 | if (exec_bfd == NULL) | |
1615 | return 0; | |
1616 | ||
09919ac2 JK |
1617 | /* Therefore for ELF it is ET_EXEC and not ET_DYN. Both shared libraries |
1618 | being executed themselves and PIE (Position Independent Executable) | |
1619 | executables are ET_DYN. */ | |
1620 | ||
1621 | if ((bfd_get_file_flags (exec_bfd) & DYNAMIC) == 0) | |
1622 | return 0; | |
1623 | ||
1624 | if (target_auxv_search (¤t_target, AT_ENTRY, &entry_point) <= 0) | |
1625 | return 0; | |
1626 | ||
1627 | displacement = entry_point - bfd_get_start_address (exec_bfd); | |
1628 | ||
1629 | /* Verify the DISPLACEMENT candidate complies with the required page | |
1630 | alignment. It is cheaper than the program headers comparison below. */ | |
1631 | ||
1632 | if (bfd_get_flavour (exec_bfd) == bfd_target_elf_flavour) | |
1633 | { | |
1634 | const struct elf_backend_data *elf = get_elf_backend_data (exec_bfd); | |
1635 | ||
1636 | /* p_align of PT_LOAD segments does not specify any alignment but | |
1637 | only congruency of addresses: | |
1638 | p_offset % p_align == p_vaddr % p_align | |
1639 | Kernel is free to load the executable with lower alignment. */ | |
1640 | ||
1641 | if ((displacement & (elf->minpagesize - 1)) != 0) | |
1642 | return 0; | |
1643 | } | |
1644 | ||
1645 | /* Verify that the auxilliary vector describes the same file as exec_bfd, by | |
1646 | comparing their program headers. If the program headers in the auxilliary | |
1647 | vector do not match the program headers in the executable, then we are | |
1648 | looking at a different file than the one used by the kernel - for | |
1649 | instance, "gdb program" connected to "gdbserver :PORT ld.so program". */ | |
1650 | ||
1651 | if (bfd_get_flavour (exec_bfd) == bfd_target_elf_flavour) | |
1652 | { | |
1653 | /* Be optimistic and clear OK only if GDB was able to verify the headers | |
1654 | really do not match. */ | |
1655 | int phdrs_size, phdrs2_size, ok = 1; | |
1656 | gdb_byte *buf, *buf2; | |
0a1e94c7 | 1657 | int arch_size; |
09919ac2 | 1658 | |
0a1e94c7 | 1659 | buf = read_program_header (-1, &phdrs_size, &arch_size); |
09919ac2 | 1660 | buf2 = read_program_headers_from_bfd (exec_bfd, &phdrs2_size); |
0a1e94c7 JK |
1661 | if (buf != NULL && buf2 != NULL) |
1662 | { | |
1663 | enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch); | |
1664 | ||
1665 | /* We are dealing with three different addresses. EXEC_BFD | |
1666 | represents current address in on-disk file. target memory content | |
1667 | may be different from EXEC_BFD as the file may have been prelinked | |
1668 | to a different address after the executable has been loaded. | |
1669 | Moreover the address of placement in target memory can be | |
3e43a32a MS |
1670 | different from what the program headers in target memory say - |
1671 | this is the goal of PIE. | |
0a1e94c7 JK |
1672 | |
1673 | Detected DISPLACEMENT covers both the offsets of PIE placement and | |
1674 | possible new prelink performed after start of the program. Here | |
1675 | relocate BUF and BUF2 just by the EXEC_BFD vs. target memory | |
1676 | content offset for the verification purpose. */ | |
1677 | ||
1678 | if (phdrs_size != phdrs2_size | |
1679 | || bfd_get_arch_size (exec_bfd) != arch_size) | |
1680 | ok = 0; | |
3e43a32a MS |
1681 | else if (arch_size == 32 |
1682 | && phdrs_size >= sizeof (Elf32_External_Phdr) | |
0a1e94c7 JK |
1683 | && phdrs_size % sizeof (Elf32_External_Phdr) == 0) |
1684 | { | |
1685 | Elf_Internal_Ehdr *ehdr2 = elf_tdata (exec_bfd)->elf_header; | |
1686 | Elf_Internal_Phdr *phdr2 = elf_tdata (exec_bfd)->phdr; | |
1687 | CORE_ADDR displacement = 0; | |
1688 | int i; | |
1689 | ||
1690 | /* DISPLACEMENT could be found more easily by the difference of | |
1691 | ehdr2->e_entry. But we haven't read the ehdr yet, and we | |
1692 | already have enough information to compute that displacement | |
1693 | with what we've read. */ | |
1694 | ||
1695 | for (i = 0; i < ehdr2->e_phnum; i++) | |
1696 | if (phdr2[i].p_type == PT_LOAD) | |
1697 | { | |
1698 | Elf32_External_Phdr *phdrp; | |
1699 | gdb_byte *buf_vaddr_p, *buf_paddr_p; | |
1700 | CORE_ADDR vaddr, paddr; | |
1701 | CORE_ADDR displacement_vaddr = 0; | |
1702 | CORE_ADDR displacement_paddr = 0; | |
1703 | ||
1704 | phdrp = &((Elf32_External_Phdr *) buf)[i]; | |
1705 | buf_vaddr_p = (gdb_byte *) &phdrp->p_vaddr; | |
1706 | buf_paddr_p = (gdb_byte *) &phdrp->p_paddr; | |
1707 | ||
1708 | vaddr = extract_unsigned_integer (buf_vaddr_p, 4, | |
1709 | byte_order); | |
1710 | displacement_vaddr = vaddr - phdr2[i].p_vaddr; | |
1711 | ||
1712 | paddr = extract_unsigned_integer (buf_paddr_p, 4, | |
1713 | byte_order); | |
1714 | displacement_paddr = paddr - phdr2[i].p_paddr; | |
1715 | ||
1716 | if (displacement_vaddr == displacement_paddr) | |
1717 | displacement = displacement_vaddr; | |
1718 | ||
1719 | break; | |
1720 | } | |
1721 | ||
1722 | /* Now compare BUF and BUF2 with optional DISPLACEMENT. */ | |
1723 | ||
1724 | for (i = 0; i < phdrs_size / sizeof (Elf32_External_Phdr); i++) | |
1725 | { | |
1726 | Elf32_External_Phdr *phdrp; | |
1727 | Elf32_External_Phdr *phdr2p; | |
1728 | gdb_byte *buf_vaddr_p, *buf_paddr_p; | |
1729 | CORE_ADDR vaddr, paddr; | |
43b8e241 | 1730 | asection *plt2_asect; |
0a1e94c7 JK |
1731 | |
1732 | phdrp = &((Elf32_External_Phdr *) buf)[i]; | |
1733 | buf_vaddr_p = (gdb_byte *) &phdrp->p_vaddr; | |
1734 | buf_paddr_p = (gdb_byte *) &phdrp->p_paddr; | |
1735 | phdr2p = &((Elf32_External_Phdr *) buf2)[i]; | |
1736 | ||
1737 | /* PT_GNU_STACK is an exception by being never relocated by | |
1738 | prelink as its addresses are always zero. */ | |
1739 | ||
1740 | if (memcmp (phdrp, phdr2p, sizeof (*phdrp)) == 0) | |
1741 | continue; | |
1742 | ||
1743 | /* Check also other adjustment combinations - PR 11786. */ | |
1744 | ||
3e43a32a MS |
1745 | vaddr = extract_unsigned_integer (buf_vaddr_p, 4, |
1746 | byte_order); | |
0a1e94c7 JK |
1747 | vaddr -= displacement; |
1748 | store_unsigned_integer (buf_vaddr_p, 4, byte_order, vaddr); | |
1749 | ||
3e43a32a MS |
1750 | paddr = extract_unsigned_integer (buf_paddr_p, 4, |
1751 | byte_order); | |
0a1e94c7 JK |
1752 | paddr -= displacement; |
1753 | store_unsigned_integer (buf_paddr_p, 4, byte_order, paddr); | |
1754 | ||
1755 | if (memcmp (phdrp, phdr2p, sizeof (*phdrp)) == 0) | |
1756 | continue; | |
1757 | ||
43b8e241 JK |
1758 | /* prelink can convert .plt SHT_NOBITS to SHT_PROGBITS. */ |
1759 | plt2_asect = bfd_get_section_by_name (exec_bfd, ".plt"); | |
1760 | if (plt2_asect) | |
1761 | { | |
1762 | int content2; | |
1763 | gdb_byte *buf_filesz_p = (gdb_byte *) &phdrp->p_filesz; | |
1764 | CORE_ADDR filesz; | |
1765 | ||
1766 | content2 = (bfd_get_section_flags (exec_bfd, plt2_asect) | |
1767 | & SEC_HAS_CONTENTS) != 0; | |
1768 | ||
1769 | filesz = extract_unsigned_integer (buf_filesz_p, 4, | |
1770 | byte_order); | |
1771 | ||
1772 | /* PLT2_ASECT is from on-disk file (exec_bfd) while | |
1773 | FILESZ is from the in-memory image. */ | |
1774 | if (content2) | |
1775 | filesz += bfd_get_section_size (plt2_asect); | |
1776 | else | |
1777 | filesz -= bfd_get_section_size (plt2_asect); | |
1778 | ||
1779 | store_unsigned_integer (buf_filesz_p, 4, byte_order, | |
1780 | filesz); | |
1781 | ||
1782 | if (memcmp (phdrp, phdr2p, sizeof (*phdrp)) == 0) | |
1783 | continue; | |
1784 | } | |
1785 | ||
0a1e94c7 JK |
1786 | ok = 0; |
1787 | break; | |
1788 | } | |
1789 | } | |
3e43a32a MS |
1790 | else if (arch_size == 64 |
1791 | && phdrs_size >= sizeof (Elf64_External_Phdr) | |
0a1e94c7 JK |
1792 | && phdrs_size % sizeof (Elf64_External_Phdr) == 0) |
1793 | { | |
1794 | Elf_Internal_Ehdr *ehdr2 = elf_tdata (exec_bfd)->elf_header; | |
1795 | Elf_Internal_Phdr *phdr2 = elf_tdata (exec_bfd)->phdr; | |
1796 | CORE_ADDR displacement = 0; | |
1797 | int i; | |
1798 | ||
1799 | /* DISPLACEMENT could be found more easily by the difference of | |
1800 | ehdr2->e_entry. But we haven't read the ehdr yet, and we | |
1801 | already have enough information to compute that displacement | |
1802 | with what we've read. */ | |
1803 | ||
1804 | for (i = 0; i < ehdr2->e_phnum; i++) | |
1805 | if (phdr2[i].p_type == PT_LOAD) | |
1806 | { | |
1807 | Elf64_External_Phdr *phdrp; | |
1808 | gdb_byte *buf_vaddr_p, *buf_paddr_p; | |
1809 | CORE_ADDR vaddr, paddr; | |
1810 | CORE_ADDR displacement_vaddr = 0; | |
1811 | CORE_ADDR displacement_paddr = 0; | |
1812 | ||
1813 | phdrp = &((Elf64_External_Phdr *) buf)[i]; | |
1814 | buf_vaddr_p = (gdb_byte *) &phdrp->p_vaddr; | |
1815 | buf_paddr_p = (gdb_byte *) &phdrp->p_paddr; | |
1816 | ||
1817 | vaddr = extract_unsigned_integer (buf_vaddr_p, 8, | |
1818 | byte_order); | |
1819 | displacement_vaddr = vaddr - phdr2[i].p_vaddr; | |
1820 | ||
1821 | paddr = extract_unsigned_integer (buf_paddr_p, 8, | |
1822 | byte_order); | |
1823 | displacement_paddr = paddr - phdr2[i].p_paddr; | |
1824 | ||
1825 | if (displacement_vaddr == displacement_paddr) | |
1826 | displacement = displacement_vaddr; | |
1827 | ||
1828 | break; | |
1829 | } | |
1830 | ||
1831 | /* Now compare BUF and BUF2 with optional DISPLACEMENT. */ | |
1832 | ||
1833 | for (i = 0; i < phdrs_size / sizeof (Elf64_External_Phdr); i++) | |
1834 | { | |
1835 | Elf64_External_Phdr *phdrp; | |
1836 | Elf64_External_Phdr *phdr2p; | |
1837 | gdb_byte *buf_vaddr_p, *buf_paddr_p; | |
1838 | CORE_ADDR vaddr, paddr; | |
43b8e241 | 1839 | asection *plt2_asect; |
0a1e94c7 JK |
1840 | |
1841 | phdrp = &((Elf64_External_Phdr *) buf)[i]; | |
1842 | buf_vaddr_p = (gdb_byte *) &phdrp->p_vaddr; | |
1843 | buf_paddr_p = (gdb_byte *) &phdrp->p_paddr; | |
1844 | phdr2p = &((Elf64_External_Phdr *) buf2)[i]; | |
1845 | ||
1846 | /* PT_GNU_STACK is an exception by being never relocated by | |
1847 | prelink as its addresses are always zero. */ | |
1848 | ||
1849 | if (memcmp (phdrp, phdr2p, sizeof (*phdrp)) == 0) | |
1850 | continue; | |
1851 | ||
1852 | /* Check also other adjustment combinations - PR 11786. */ | |
1853 | ||
3e43a32a MS |
1854 | vaddr = extract_unsigned_integer (buf_vaddr_p, 8, |
1855 | byte_order); | |
0a1e94c7 JK |
1856 | vaddr -= displacement; |
1857 | store_unsigned_integer (buf_vaddr_p, 8, byte_order, vaddr); | |
1858 | ||
3e43a32a MS |
1859 | paddr = extract_unsigned_integer (buf_paddr_p, 8, |
1860 | byte_order); | |
0a1e94c7 JK |
1861 | paddr -= displacement; |
1862 | store_unsigned_integer (buf_paddr_p, 8, byte_order, paddr); | |
1863 | ||
1864 | if (memcmp (phdrp, phdr2p, sizeof (*phdrp)) == 0) | |
1865 | continue; | |
1866 | ||
43b8e241 JK |
1867 | /* prelink can convert .plt SHT_NOBITS to SHT_PROGBITS. */ |
1868 | plt2_asect = bfd_get_section_by_name (exec_bfd, ".plt"); | |
1869 | if (plt2_asect) | |
1870 | { | |
1871 | int content2; | |
1872 | gdb_byte *buf_filesz_p = (gdb_byte *) &phdrp->p_filesz; | |
1873 | CORE_ADDR filesz; | |
1874 | ||
1875 | content2 = (bfd_get_section_flags (exec_bfd, plt2_asect) | |
1876 | & SEC_HAS_CONTENTS) != 0; | |
1877 | ||
1878 | filesz = extract_unsigned_integer (buf_filesz_p, 8, | |
1879 | byte_order); | |
1880 | ||
1881 | /* PLT2_ASECT is from on-disk file (exec_bfd) while | |
1882 | FILESZ is from the in-memory image. */ | |
1883 | if (content2) | |
1884 | filesz += bfd_get_section_size (plt2_asect); | |
1885 | else | |
1886 | filesz -= bfd_get_section_size (plt2_asect); | |
1887 | ||
1888 | store_unsigned_integer (buf_filesz_p, 8, byte_order, | |
1889 | filesz); | |
1890 | ||
1891 | if (memcmp (phdrp, phdr2p, sizeof (*phdrp)) == 0) | |
1892 | continue; | |
1893 | } | |
1894 | ||
0a1e94c7 JK |
1895 | ok = 0; |
1896 | break; | |
1897 | } | |
1898 | } | |
1899 | else | |
1900 | ok = 0; | |
1901 | } | |
09919ac2 JK |
1902 | |
1903 | xfree (buf); | |
1904 | xfree (buf2); | |
1905 | ||
1906 | if (!ok) | |
1907 | return 0; | |
1908 | } | |
b8040f19 | 1909 | |
ccf26247 JK |
1910 | if (info_verbose) |
1911 | { | |
1912 | /* It can be printed repeatedly as there is no easy way to check | |
1913 | the executable symbols/file has been already relocated to | |
1914 | displacement. */ | |
1915 | ||
1916 | printf_unfiltered (_("Using PIE (Position Independent Executable) " | |
1917 | "displacement %s for \"%s\".\n"), | |
1918 | paddress (target_gdbarch, displacement), | |
1919 | bfd_get_filename (exec_bfd)); | |
1920 | } | |
1921 | ||
01c30d6e JK |
1922 | *displacementp = displacement; |
1923 | return 1; | |
b8040f19 JK |
1924 | } |
1925 | ||
1926 | /* Relocate the main executable. This function should be called upon | |
c378eb4e | 1927 | stopping the inferior process at the entry point to the program. |
b8040f19 JK |
1928 | The entry point from BFD is compared to the AT_ENTRY of AUXV and if they are |
1929 | different, the main executable is relocated by the proper amount. */ | |
1930 | ||
1931 | static void | |
1932 | svr4_relocate_main_executable (void) | |
1933 | { | |
01c30d6e JK |
1934 | CORE_ADDR displacement; |
1935 | ||
4e5799b6 JK |
1936 | /* If we are re-running this executable, SYMFILE_OBJFILE->SECTION_OFFSETS |
1937 | probably contains the offsets computed using the PIE displacement | |
1938 | from the previous run, which of course are irrelevant for this run. | |
1939 | So we need to determine the new PIE displacement and recompute the | |
1940 | section offsets accordingly, even if SYMFILE_OBJFILE->SECTION_OFFSETS | |
1941 | already contains pre-computed offsets. | |
01c30d6e | 1942 | |
4e5799b6 | 1943 | If we cannot compute the PIE displacement, either: |
01c30d6e | 1944 | |
4e5799b6 JK |
1945 | - The executable is not PIE. |
1946 | ||
1947 | - SYMFILE_OBJFILE does not match the executable started in the target. | |
1948 | This can happen for main executable symbols loaded at the host while | |
1949 | `ld.so --ld-args main-executable' is loaded in the target. | |
1950 | ||
1951 | Then we leave the section offsets untouched and use them as is for | |
1952 | this run. Either: | |
1953 | ||
1954 | - These section offsets were properly reset earlier, and thus | |
1955 | already contain the correct values. This can happen for instance | |
1956 | when reconnecting via the remote protocol to a target that supports | |
1957 | the `qOffsets' packet. | |
1958 | ||
1959 | - The section offsets were not reset earlier, and the best we can | |
c378eb4e | 1960 | hope is that the old offsets are still applicable to the new run. */ |
01c30d6e JK |
1961 | |
1962 | if (! svr4_exec_displacement (&displacement)) | |
1963 | return; | |
b8040f19 | 1964 | |
01c30d6e JK |
1965 | /* Even DISPLACEMENT 0 is a valid new difference of in-memory vs. in-file |
1966 | addresses. */ | |
b8040f19 JK |
1967 | |
1968 | if (symfile_objfile) | |
e2a44558 | 1969 | { |
e2a44558 | 1970 | struct section_offsets *new_offsets; |
b8040f19 | 1971 | int i; |
e2a44558 | 1972 | |
b8040f19 JK |
1973 | new_offsets = alloca (symfile_objfile->num_sections |
1974 | * sizeof (*new_offsets)); | |
e2a44558 | 1975 | |
b8040f19 JK |
1976 | for (i = 0; i < symfile_objfile->num_sections; i++) |
1977 | new_offsets->offsets[i] = displacement; | |
e2a44558 | 1978 | |
b8040f19 | 1979 | objfile_relocate (symfile_objfile, new_offsets); |
e2a44558 | 1980 | } |
51bee8e9 JK |
1981 | else if (exec_bfd) |
1982 | { | |
1983 | asection *asect; | |
1984 | ||
1985 | for (asect = exec_bfd->sections; asect != NULL; asect = asect->next) | |
1986 | exec_set_section_address (bfd_get_filename (exec_bfd), asect->index, | |
1987 | (bfd_section_vma (exec_bfd, asect) | |
1988 | + displacement)); | |
1989 | } | |
e2a44558 KB |
1990 | } |
1991 | ||
7f86f058 | 1992 | /* Implement the "create_inferior_hook" target_solib_ops method. |
13437d4b KB |
1993 | |
1994 | For SVR4 executables, this first instruction is either the first | |
1995 | instruction in the dynamic linker (for dynamically linked | |
1996 | executables) or the instruction at "start" for statically linked | |
1997 | executables. For dynamically linked executables, the system | |
1998 | first exec's /lib/libc.so.N, which contains the dynamic linker, | |
1999 | and starts it running. The dynamic linker maps in any needed | |
2000 | shared libraries, maps in the actual user executable, and then | |
2001 | jumps to "start" in the user executable. | |
2002 | ||
7f86f058 PA |
2003 | We can arrange to cooperate with the dynamic linker to discover the |
2004 | names of shared libraries that are dynamically linked, and the base | |
2005 | addresses to which they are linked. | |
13437d4b KB |
2006 | |
2007 | This function is responsible for discovering those names and | |
2008 | addresses, and saving sufficient information about them to allow | |
2009 | their symbols to be read at a later time. | |
2010 | ||
2011 | FIXME | |
2012 | ||
2013 | Between enable_break() and disable_break(), this code does not | |
2014 | properly handle hitting breakpoints which the user might have | |
2015 | set in the startup code or in the dynamic linker itself. Proper | |
2016 | handling will probably have to wait until the implementation is | |
2017 | changed to use the "breakpoint handler function" method. | |
2018 | ||
7f86f058 | 2019 | Also, what if child has exit()ed? Must exit loop somehow. */ |
13437d4b | 2020 | |
e2a44558 | 2021 | static void |
268a4a75 | 2022 | svr4_solib_create_inferior_hook (int from_tty) |
13437d4b | 2023 | { |
1cd337a5 | 2024 | #if defined(_SCO_DS) |
d6b48e9c | 2025 | struct inferior *inf; |
2020b7ab | 2026 | struct thread_info *tp; |
1cd337a5 | 2027 | #endif /* defined(_SCO_DS) */ |
1a816a87 PA |
2028 | struct svr4_info *info; |
2029 | ||
6c95b8df | 2030 | info = get_svr4_info (); |
2020b7ab | 2031 | |
e2a44558 | 2032 | /* Relocate the main executable if necessary. */ |
86e4bafc | 2033 | svr4_relocate_main_executable (); |
e2a44558 | 2034 | |
c91c8c16 PA |
2035 | /* No point setting a breakpoint in the dynamic linker if we can't |
2036 | hit it (e.g., a core file, or a trace file). */ | |
2037 | if (!target_has_execution) | |
2038 | return; | |
2039 | ||
d5a921c9 | 2040 | if (!svr4_have_link_map_offsets ()) |
513f5903 | 2041 | return; |
d5a921c9 | 2042 | |
268a4a75 | 2043 | if (!enable_break (info, from_tty)) |
542c95c2 | 2044 | return; |
13437d4b | 2045 | |
ab31aa69 KB |
2046 | #if defined(_SCO_DS) |
2047 | /* SCO needs the loop below, other systems should be using the | |
13437d4b KB |
2048 | special shared library breakpoints and the shared library breakpoint |
2049 | service routine. | |
2050 | ||
2051 | Now run the target. It will eventually hit the breakpoint, at | |
2052 | which point all of the libraries will have been mapped in and we | |
2053 | can go groveling around in the dynamic linker structures to find | |
c378eb4e | 2054 | out what we need to know about them. */ |
13437d4b | 2055 | |
d6b48e9c | 2056 | inf = current_inferior (); |
2020b7ab PA |
2057 | tp = inferior_thread (); |
2058 | ||
13437d4b | 2059 | clear_proceed_status (); |
16c381f0 JK |
2060 | inf->control.stop_soon = STOP_QUIETLY; |
2061 | tp->suspend.stop_signal = TARGET_SIGNAL_0; | |
13437d4b KB |
2062 | do |
2063 | { | |
16c381f0 | 2064 | target_resume (pid_to_ptid (-1), 0, tp->suspend.stop_signal); |
e4c8541f | 2065 | wait_for_inferior (); |
13437d4b | 2066 | } |
16c381f0 JK |
2067 | while (tp->suspend.stop_signal != TARGET_SIGNAL_TRAP); |
2068 | inf->control.stop_soon = NO_STOP_QUIETLY; | |
ab31aa69 | 2069 | #endif /* defined(_SCO_DS) */ |
13437d4b KB |
2070 | } |
2071 | ||
2072 | static void | |
2073 | svr4_clear_solib (void) | |
2074 | { | |
6c95b8df PA |
2075 | struct svr4_info *info; |
2076 | ||
2077 | info = get_svr4_info (); | |
2078 | info->debug_base = 0; | |
2079 | info->debug_loader_offset_p = 0; | |
2080 | info->debug_loader_offset = 0; | |
2081 | xfree (info->debug_loader_name); | |
2082 | info->debug_loader_name = NULL; | |
13437d4b KB |
2083 | } |
2084 | ||
6bb7be43 JB |
2085 | /* Clear any bits of ADDR that wouldn't fit in a target-format |
2086 | data pointer. "Data pointer" here refers to whatever sort of | |
2087 | address the dynamic linker uses to manage its sections. At the | |
2088 | moment, we don't support shared libraries on any processors where | |
2089 | code and data pointers are different sizes. | |
2090 | ||
2091 | This isn't really the right solution. What we really need here is | |
2092 | a way to do arithmetic on CORE_ADDR values that respects the | |
2093 | natural pointer/address correspondence. (For example, on the MIPS, | |
2094 | converting a 32-bit pointer to a 64-bit CORE_ADDR requires you to | |
2095 | sign-extend the value. There, simply truncating the bits above | |
819844ad | 2096 | gdbarch_ptr_bit, as we do below, is no good.) This should probably |
6bb7be43 JB |
2097 | be a new gdbarch method or something. */ |
2098 | static CORE_ADDR | |
2099 | svr4_truncate_ptr (CORE_ADDR addr) | |
2100 | { | |
1cf3db46 | 2101 | if (gdbarch_ptr_bit (target_gdbarch) == sizeof (CORE_ADDR) * 8) |
6bb7be43 JB |
2102 | /* We don't need to truncate anything, and the bit twiddling below |
2103 | will fail due to overflow problems. */ | |
2104 | return addr; | |
2105 | else | |
1cf3db46 | 2106 | return addr & (((CORE_ADDR) 1 << gdbarch_ptr_bit (target_gdbarch)) - 1); |
6bb7be43 JB |
2107 | } |
2108 | ||
2109 | ||
749499cb KB |
2110 | static void |
2111 | svr4_relocate_section_addresses (struct so_list *so, | |
0542c86d | 2112 | struct target_section *sec) |
749499cb | 2113 | { |
b23518f0 | 2114 | sec->addr = svr4_truncate_ptr (sec->addr + lm_addr_check (so, |
cc10cae3 | 2115 | sec->bfd)); |
b23518f0 | 2116 | sec->endaddr = svr4_truncate_ptr (sec->endaddr + lm_addr_check (so, |
cc10cae3 | 2117 | sec->bfd)); |
749499cb | 2118 | } |
4b188b9f | 2119 | \f |
749499cb | 2120 | |
4b188b9f | 2121 | /* Architecture-specific operations. */ |
6bb7be43 | 2122 | |
4b188b9f MK |
2123 | /* Per-architecture data key. */ |
2124 | static struct gdbarch_data *solib_svr4_data; | |
e5e2b9ff | 2125 | |
4b188b9f | 2126 | struct solib_svr4_ops |
e5e2b9ff | 2127 | { |
4b188b9f MK |
2128 | /* Return a description of the layout of `struct link_map'. */ |
2129 | struct link_map_offsets *(*fetch_link_map_offsets)(void); | |
2130 | }; | |
e5e2b9ff | 2131 | |
4b188b9f | 2132 | /* Return a default for the architecture-specific operations. */ |
e5e2b9ff | 2133 | |
4b188b9f MK |
2134 | static void * |
2135 | solib_svr4_init (struct obstack *obstack) | |
e5e2b9ff | 2136 | { |
4b188b9f | 2137 | struct solib_svr4_ops *ops; |
e5e2b9ff | 2138 | |
4b188b9f | 2139 | ops = OBSTACK_ZALLOC (obstack, struct solib_svr4_ops); |
8d005789 | 2140 | ops->fetch_link_map_offsets = NULL; |
4b188b9f | 2141 | return ops; |
e5e2b9ff KB |
2142 | } |
2143 | ||
4b188b9f | 2144 | /* Set the architecture-specific `struct link_map_offsets' fetcher for |
7e3cb44c | 2145 | GDBARCH to FLMO. Also, install SVR4 solib_ops into GDBARCH. */ |
1c4dcb57 | 2146 | |
21479ded | 2147 | void |
e5e2b9ff KB |
2148 | set_solib_svr4_fetch_link_map_offsets (struct gdbarch *gdbarch, |
2149 | struct link_map_offsets *(*flmo) (void)) | |
21479ded | 2150 | { |
4b188b9f MK |
2151 | struct solib_svr4_ops *ops = gdbarch_data (gdbarch, solib_svr4_data); |
2152 | ||
2153 | ops->fetch_link_map_offsets = flmo; | |
7e3cb44c UW |
2154 | |
2155 | set_solib_ops (gdbarch, &svr4_so_ops); | |
21479ded KB |
2156 | } |
2157 | ||
4b188b9f MK |
2158 | /* Fetch a link_map_offsets structure using the architecture-specific |
2159 | `struct link_map_offsets' fetcher. */ | |
1c4dcb57 | 2160 | |
4b188b9f MK |
2161 | static struct link_map_offsets * |
2162 | svr4_fetch_link_map_offsets (void) | |
21479ded | 2163 | { |
1cf3db46 | 2164 | struct solib_svr4_ops *ops = gdbarch_data (target_gdbarch, solib_svr4_data); |
4b188b9f MK |
2165 | |
2166 | gdb_assert (ops->fetch_link_map_offsets); | |
2167 | return ops->fetch_link_map_offsets (); | |
21479ded KB |
2168 | } |
2169 | ||
4b188b9f MK |
2170 | /* Return 1 if a link map offset fetcher has been defined, 0 otherwise. */ |
2171 | ||
2172 | static int | |
2173 | svr4_have_link_map_offsets (void) | |
2174 | { | |
1cf3db46 | 2175 | struct solib_svr4_ops *ops = gdbarch_data (target_gdbarch, solib_svr4_data); |
433759f7 | 2176 | |
4b188b9f MK |
2177 | return (ops->fetch_link_map_offsets != NULL); |
2178 | } | |
2179 | \f | |
2180 | ||
e4bbbda8 MK |
2181 | /* Most OS'es that have SVR4-style ELF dynamic libraries define a |
2182 | `struct r_debug' and a `struct link_map' that are binary compatible | |
2183 | with the origional SVR4 implementation. */ | |
2184 | ||
2185 | /* Fetch (and possibly build) an appropriate `struct link_map_offsets' | |
2186 | for an ILP32 SVR4 system. */ | |
d989b283 | 2187 | |
e4bbbda8 MK |
2188 | struct link_map_offsets * |
2189 | svr4_ilp32_fetch_link_map_offsets (void) | |
2190 | { | |
2191 | static struct link_map_offsets lmo; | |
2192 | static struct link_map_offsets *lmp = NULL; | |
2193 | ||
2194 | if (lmp == NULL) | |
2195 | { | |
2196 | lmp = &lmo; | |
2197 | ||
e4cd0d6a MK |
2198 | lmo.r_version_offset = 0; |
2199 | lmo.r_version_size = 4; | |
e4bbbda8 | 2200 | lmo.r_map_offset = 4; |
7cd25cfc | 2201 | lmo.r_brk_offset = 8; |
e4cd0d6a | 2202 | lmo.r_ldsomap_offset = 20; |
e4bbbda8 MK |
2203 | |
2204 | /* Everything we need is in the first 20 bytes. */ | |
2205 | lmo.link_map_size = 20; | |
2206 | lmo.l_addr_offset = 0; | |
e4bbbda8 | 2207 | lmo.l_name_offset = 4; |
cc10cae3 | 2208 | lmo.l_ld_offset = 8; |
e4bbbda8 | 2209 | lmo.l_next_offset = 12; |
e4bbbda8 | 2210 | lmo.l_prev_offset = 16; |
e4bbbda8 MK |
2211 | } |
2212 | ||
2213 | return lmp; | |
2214 | } | |
2215 | ||
2216 | /* Fetch (and possibly build) an appropriate `struct link_map_offsets' | |
2217 | for an LP64 SVR4 system. */ | |
d989b283 | 2218 | |
e4bbbda8 MK |
2219 | struct link_map_offsets * |
2220 | svr4_lp64_fetch_link_map_offsets (void) | |
2221 | { | |
2222 | static struct link_map_offsets lmo; | |
2223 | static struct link_map_offsets *lmp = NULL; | |
2224 | ||
2225 | if (lmp == NULL) | |
2226 | { | |
2227 | lmp = &lmo; | |
2228 | ||
e4cd0d6a MK |
2229 | lmo.r_version_offset = 0; |
2230 | lmo.r_version_size = 4; | |
e4bbbda8 | 2231 | lmo.r_map_offset = 8; |
7cd25cfc | 2232 | lmo.r_brk_offset = 16; |
e4cd0d6a | 2233 | lmo.r_ldsomap_offset = 40; |
e4bbbda8 MK |
2234 | |
2235 | /* Everything we need is in the first 40 bytes. */ | |
2236 | lmo.link_map_size = 40; | |
2237 | lmo.l_addr_offset = 0; | |
e4bbbda8 | 2238 | lmo.l_name_offset = 8; |
cc10cae3 | 2239 | lmo.l_ld_offset = 16; |
e4bbbda8 | 2240 | lmo.l_next_offset = 24; |
e4bbbda8 | 2241 | lmo.l_prev_offset = 32; |
e4bbbda8 MK |
2242 | } |
2243 | ||
2244 | return lmp; | |
2245 | } | |
2246 | \f | |
2247 | ||
7d522c90 | 2248 | struct target_so_ops svr4_so_ops; |
13437d4b | 2249 | |
c378eb4e | 2250 | /* Lookup global symbol for ELF DSOs linked with -Bsymbolic. Those DSOs have a |
3a40aaa0 UW |
2251 | different rule for symbol lookup. The lookup begins here in the DSO, not in |
2252 | the main executable. */ | |
2253 | ||
2254 | static struct symbol * | |
2255 | elf_lookup_lib_symbol (const struct objfile *objfile, | |
2256 | const char *name, | |
21b556f4 | 2257 | const domain_enum domain) |
3a40aaa0 | 2258 | { |
61f0d762 JK |
2259 | bfd *abfd; |
2260 | ||
2261 | if (objfile == symfile_objfile) | |
2262 | abfd = exec_bfd; | |
2263 | else | |
2264 | { | |
2265 | /* OBJFILE should have been passed as the non-debug one. */ | |
2266 | gdb_assert (objfile->separate_debug_objfile_backlink == NULL); | |
2267 | ||
2268 | abfd = objfile->obfd; | |
2269 | } | |
2270 | ||
2271 | if (abfd == NULL || scan_dyntag (DT_SYMBOLIC, abfd, NULL) != 1) | |
3a40aaa0 UW |
2272 | return NULL; |
2273 | ||
94af9270 | 2274 | return lookup_global_symbol_from_objfile (objfile, name, domain); |
3a40aaa0 UW |
2275 | } |
2276 | ||
a78f21af AC |
2277 | extern initialize_file_ftype _initialize_svr4_solib; /* -Wmissing-prototypes */ |
2278 | ||
13437d4b KB |
2279 | void |
2280 | _initialize_svr4_solib (void) | |
2281 | { | |
4b188b9f | 2282 | solib_svr4_data = gdbarch_data_register_pre_init (solib_svr4_init); |
6c95b8df PA |
2283 | solib_svr4_pspace_data |
2284 | = register_program_space_data_with_cleanup (svr4_pspace_data_cleanup); | |
4b188b9f | 2285 | |
749499cb | 2286 | svr4_so_ops.relocate_section_addresses = svr4_relocate_section_addresses; |
13437d4b KB |
2287 | svr4_so_ops.free_so = svr4_free_so; |
2288 | svr4_so_ops.clear_solib = svr4_clear_solib; | |
2289 | svr4_so_ops.solib_create_inferior_hook = svr4_solib_create_inferior_hook; | |
2290 | svr4_so_ops.special_symbol_handling = svr4_special_symbol_handling; | |
2291 | svr4_so_ops.current_sos = svr4_current_sos; | |
2292 | svr4_so_ops.open_symbol_file_object = open_symbol_file_object; | |
d7fa2ae2 | 2293 | svr4_so_ops.in_dynsym_resolve_code = svr4_in_dynsym_resolve_code; |
831a0c44 | 2294 | svr4_so_ops.bfd_open = solib_bfd_open; |
3a40aaa0 | 2295 | svr4_so_ops.lookup_lib_global_symbol = elf_lookup_lib_symbol; |
a7c02bc8 | 2296 | svr4_so_ops.same = svr4_same; |
de18c1d8 | 2297 | svr4_so_ops.keep_data_in_core = svr4_keep_data_in_core; |
13437d4b | 2298 | } |