]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/solib-darwin.c
gdb: remove target_gdbarch
[thirdparty/binutils-gdb.git] / gdb / solib-darwin.c
1 /* Handle Darwin shared libraries for GDB, the GNU Debugger.
2
3 Copyright (C) 2009-2023 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21
22 #include "bfd.h"
23 #include "objfiles.h"
24 #include "gdbcore.h"
25 #include "target.h"
26 #include "inferior.h"
27 #include "regcache.h"
28 #include "gdb_bfd.h"
29
30 #include "solist.h"
31 #include "solib-darwin.h"
32
33 #include "mach-o.h"
34 #include "mach-o/external.h"
35
36 struct gdb_dyld_image_info
37 {
38 /* Base address (which corresponds to the Mach-O header). */
39 CORE_ADDR mach_header;
40 /* Image file path. */
41 CORE_ADDR file_path;
42 /* st.m_time of image file. */
43 unsigned long mtime;
44 };
45
46 /* Content of inferior dyld_all_image_infos structure.
47 See /usr/include/mach-o/dyld_images.h for the documentation. */
48 struct gdb_dyld_all_image_infos
49 {
50 /* Version (1). */
51 unsigned int version;
52 /* Number of images. */
53 unsigned int count;
54 /* Image description. */
55 CORE_ADDR info;
56 /* Notifier (function called when a library is added or removed). */
57 CORE_ADDR notifier;
58 };
59
60 /* Current all_image_infos version. */
61 #define DYLD_VERSION_MIN 1
62 #define DYLD_VERSION_MAX 15
63
64 /* Per PSPACE specific data. */
65 struct darwin_info
66 {
67 /* Address of structure dyld_all_image_infos in inferior. */
68 CORE_ADDR all_image_addr = 0;
69
70 /* Gdb copy of dyld_all_info_infos. */
71 struct gdb_dyld_all_image_infos all_image {};
72 };
73
74 /* Per-program-space data key. */
75 static const registry<program_space>::key<darwin_info>
76 solib_darwin_pspace_data;
77
78 /* Get the current darwin data. If none is found yet, add it now. This
79 function always returns a valid object. */
80
81 static struct darwin_info *
82 get_darwin_info (void)
83 {
84 struct darwin_info *info;
85
86 info = solib_darwin_pspace_data.get (current_program_space);
87 if (info != NULL)
88 return info;
89
90 return solib_darwin_pspace_data.emplace (current_program_space);
91 }
92
93 /* Return non-zero if the version in dyld_all_image is known. */
94
95 static int
96 darwin_dyld_version_ok (const struct darwin_info *info)
97 {
98 return info->all_image.version >= DYLD_VERSION_MIN
99 && info->all_image.version <= DYLD_VERSION_MAX;
100 }
101
102 /* Read dyld_all_image from inferior. */
103
104 static void
105 darwin_load_image_infos (struct darwin_info *info)
106 {
107 gdb_byte buf[24];
108 bfd_endian byte_order = gdbarch_byte_order (current_inferior ()->arch ());
109 type *ptr_type
110 = builtin_type (current_inferior ()->arch ())->builtin_data_ptr;
111 int len;
112
113 /* If the structure address is not known, don't continue. */
114 if (info->all_image_addr == 0)
115 return;
116
117 /* The structure has 4 fields: version (4 bytes), count (4 bytes),
118 info (pointer) and notifier (pointer). */
119 len = 4 + 4 + 2 * ptr_type->length ();
120 gdb_assert (len <= sizeof (buf));
121 memset (&info->all_image, 0, sizeof (info->all_image));
122
123 /* Read structure raw bytes from target. */
124 if (target_read_memory (info->all_image_addr, buf, len))
125 return;
126
127 /* Extract the fields. */
128 info->all_image.version = extract_unsigned_integer (buf, 4, byte_order);
129 if (!darwin_dyld_version_ok (info))
130 return;
131
132 info->all_image.count = extract_unsigned_integer (buf + 4, 4, byte_order);
133 info->all_image.info = extract_typed_address (buf + 8, ptr_type);
134 info->all_image.notifier = extract_typed_address
135 (buf + 8 + ptr_type->length (), ptr_type);
136 }
137
138 /* Link map info to include in an allocated so_list entry. */
139
140 struct lm_info_darwin : public lm_info_base
141 {
142 /* The target location of lm. */
143 CORE_ADDR lm_addr = 0;
144 };
145
146 /* Lookup the value for a specific symbol. */
147
148 static CORE_ADDR
149 lookup_symbol_from_bfd (bfd *abfd, const char *symname)
150 {
151 long storage_needed;
152 asymbol **symbol_table;
153 unsigned int number_of_symbols;
154 unsigned int i;
155 CORE_ADDR symaddr = 0;
156
157 storage_needed = bfd_get_symtab_upper_bound (abfd);
158
159 if (storage_needed <= 0)
160 return 0;
161
162 symbol_table = (asymbol **) xmalloc (storage_needed);
163 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
164
165 for (i = 0; i < number_of_symbols; i++)
166 {
167 asymbol *sym = symbol_table[i];
168
169 if (strcmp (sym->name, symname) == 0
170 && (sym->section->flags & (SEC_CODE | SEC_DATA)) != 0)
171 {
172 /* BFD symbols are section relative. */
173 symaddr = sym->value + sym->section->vma;
174 break;
175 }
176 }
177 xfree (symbol_table);
178
179 return symaddr;
180 }
181
182 /* Return program interpreter string. */
183
184 static char *
185 find_program_interpreter (void)
186 {
187 char *buf = NULL;
188
189 /* If we have an current exec_bfd, get the interpreter from the load
190 commands. */
191 if (current_program_space->exec_bfd ())
192 {
193 bfd_mach_o_load_command *cmd;
194
195 if (bfd_mach_o_lookup_command (current_program_space->exec_bfd (),
196 BFD_MACH_O_LC_LOAD_DYLINKER, &cmd) == 1)
197 return cmd->command.dylinker.name_str;
198 }
199
200 /* If we didn't find it, read from memory.
201 FIXME: todo. */
202 return buf;
203 }
204
205 /* Not used. I don't see how the main symbol file can be found: the
206 interpreter name is needed and it is known from the executable file.
207 Note that darwin-nat.c implements pid_to_exec_file. */
208
209 static int
210 open_symbol_file_object (int from_tty)
211 {
212 return 0;
213 }
214
215 /* Build a list of currently loaded shared objects. See solib-svr4.c. */
216
217 static struct so_list *
218 darwin_current_sos (void)
219 {
220 type *ptr_type
221 = builtin_type (current_inferior ()->arch ())->builtin_data_ptr;
222 enum bfd_endian byte_order = type_byte_order (ptr_type);
223 int ptr_len = ptr_type->length ();
224 unsigned int image_info_size;
225 struct so_list *head = NULL;
226 struct so_list *tail = NULL;
227 int i;
228 struct darwin_info *info = get_darwin_info ();
229
230 /* Be sure image infos are loaded. */
231 darwin_load_image_infos (info);
232
233 if (!darwin_dyld_version_ok (info))
234 return NULL;
235
236 image_info_size = ptr_len * 3;
237
238 /* Read infos for each solib.
239 The first entry was rumored to be the executable itself, but this is not
240 true when a large number of shared libraries are used (table expanded ?).
241 We now check all entries, but discard executable images. */
242 for (i = 0; i < info->all_image.count; i++)
243 {
244 CORE_ADDR iinfo = info->all_image.info + i * image_info_size;
245 gdb_byte buf[image_info_size];
246 CORE_ADDR load_addr;
247 CORE_ADDR path_addr;
248 struct mach_o_header_external hdr;
249 unsigned long hdr_val;
250
251 /* Read image info from inferior. */
252 if (target_read_memory (iinfo, buf, image_info_size))
253 break;
254
255 load_addr = extract_typed_address (buf, ptr_type);
256 path_addr = extract_typed_address (buf + ptr_len, ptr_type);
257
258 /* Read Mach-O header from memory. */
259 if (target_read_memory (load_addr, (gdb_byte *) &hdr, sizeof (hdr) - 4))
260 break;
261 /* Discard wrong magic numbers. Shouldn't happen. */
262 hdr_val = extract_unsigned_integer
263 (hdr.magic, sizeof (hdr.magic), byte_order);
264 if (hdr_val != BFD_MACH_O_MH_MAGIC && hdr_val != BFD_MACH_O_MH_MAGIC_64)
265 continue;
266 /* Discard executable. Should happen only once. */
267 hdr_val = extract_unsigned_integer
268 (hdr.filetype, sizeof (hdr.filetype), byte_order);
269 if (hdr_val == BFD_MACH_O_MH_EXECUTE)
270 continue;
271
272 gdb::unique_xmalloc_ptr<char> file_path
273 = target_read_string (path_addr, SO_NAME_MAX_PATH_SIZE - 1);
274 if (file_path == nullptr)
275 break;
276
277 /* Create and fill the new so_list element. */
278 gdb::unique_xmalloc_ptr<struct so_list> newobj (XCNEW (struct so_list));
279
280 lm_info_darwin *li = new lm_info_darwin;
281 newobj->lm_info = li;
282
283 strncpy (newobj->so_name, file_path.get (), SO_NAME_MAX_PATH_SIZE - 1);
284 newobj->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
285 strcpy (newobj->so_original_name, newobj->so_name);
286 li->lm_addr = load_addr;
287
288 if (head == NULL)
289 head = newobj.get ();
290 else
291 tail->next = newobj.get ();
292 tail = newobj.release ();
293 }
294
295 return head;
296 }
297
298 /* Check LOAD_ADDR points to a Mach-O executable header. Return LOAD_ADDR
299 in case of success, 0 in case of failure. */
300
301 static CORE_ADDR
302 darwin_validate_exec_header (CORE_ADDR load_addr)
303 {
304 bfd_endian byte_order = gdbarch_byte_order (current_inferior ()->arch ());
305 struct mach_o_header_external hdr;
306 unsigned long hdr_val;
307
308 /* Read Mach-O header from memory. */
309 if (target_read_memory (load_addr, (gdb_byte *) &hdr, sizeof (hdr) - 4))
310 return 0;
311
312 /* Discard wrong magic numbers. Shouldn't happen. */
313 hdr_val = extract_unsigned_integer
314 (hdr.magic, sizeof (hdr.magic), byte_order);
315 if (hdr_val != BFD_MACH_O_MH_MAGIC && hdr_val != BFD_MACH_O_MH_MAGIC_64)
316 return 0;
317
318 /* Check executable. */
319 hdr_val = extract_unsigned_integer
320 (hdr.filetype, sizeof (hdr.filetype), byte_order);
321 if (hdr_val == BFD_MACH_O_MH_EXECUTE)
322 return load_addr;
323
324 return 0;
325 }
326
327 /* Get the load address of the executable using dyld list of images.
328 We assume that the dyld info are correct (which is wrong if the target
329 is stopped at the first instruction). */
330
331 static CORE_ADDR
332 darwin_read_exec_load_addr_from_dyld (struct darwin_info *info)
333 {
334 type *ptr_type
335 = builtin_type (current_inferior ()->arch ())->builtin_data_ptr;
336 int ptr_len = ptr_type->length ();
337 unsigned int image_info_size = ptr_len * 3;
338 int i;
339
340 /* Read infos for each solib. One of them should be the executable. */
341 for (i = 0; i < info->all_image.count; i++)
342 {
343 CORE_ADDR iinfo = info->all_image.info + i * image_info_size;
344 gdb_byte buf[image_info_size];
345 CORE_ADDR load_addr;
346
347 /* Read image info from inferior. */
348 if (target_read_memory (iinfo, buf, image_info_size))
349 break;
350
351 load_addr = extract_typed_address (buf, ptr_type);
352 if (darwin_validate_exec_header (load_addr) == load_addr)
353 return load_addr;
354 }
355
356 return 0;
357 }
358
359 /* Get the load address of the executable when the PC is at the dyld
360 entry point using parameter passed by the kernel (at SP). */
361
362 static CORE_ADDR
363 darwin_read_exec_load_addr_at_init (struct darwin_info *info)
364 {
365 gdbarch *gdbarch = current_inferior ()->arch ();
366 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
367 int addr_size = gdbarch_addr_bit (gdbarch) / 8;
368 ULONGEST load_ptr_addr;
369 ULONGEST load_addr;
370 gdb_byte buf[8];
371
372 /* Get SP. */
373 if (regcache_cooked_read_unsigned (get_current_regcache (),
374 gdbarch_sp_regnum (gdbarch),
375 &load_ptr_addr) != REG_VALID)
376 return 0;
377
378 /* Read value at SP (image load address). */
379 if (target_read_memory (load_ptr_addr, buf, addr_size))
380 return 0;
381
382 load_addr = extract_unsigned_integer (buf, addr_size, byte_order);
383
384 return darwin_validate_exec_header (load_addr);
385 }
386
387 /* Return 1 if PC lies in the dynamic symbol resolution code of the
388 run time loader. */
389
390 static int
391 darwin_in_dynsym_resolve_code (CORE_ADDR pc)
392 {
393 return 0;
394 }
395
396 /* A wrapper for bfd_mach_o_fat_extract that handles reference
397 counting properly. This will either return NULL, or return a new
398 reference to a BFD. */
399
400 static gdb_bfd_ref_ptr
401 gdb_bfd_mach_o_fat_extract (bfd *abfd, bfd_format format,
402 const bfd_arch_info_type *arch)
403 {
404 bfd *result = bfd_mach_o_fat_extract (abfd, format, arch);
405
406 if (result == NULL)
407 return NULL;
408
409 if (result == abfd)
410 gdb_bfd_ref (result);
411 else
412 gdb_bfd_mark_parent (result, abfd);
413
414 return gdb_bfd_ref_ptr (result);
415 }
416
417 /* Return the BFD for the program interpreter. */
418
419 static gdb_bfd_ref_ptr
420 darwin_get_dyld_bfd ()
421 {
422 char *interp_name;
423
424 /* This method doesn't work with an attached process. */
425 if (current_inferior ()->attach_flag)
426 return NULL;
427
428 /* Find the program interpreter. */
429 interp_name = find_program_interpreter ();
430 if (!interp_name)
431 return NULL;
432
433 /* Create a bfd for the interpreter. */
434 gdb_bfd_ref_ptr dyld_bfd (gdb_bfd_open (interp_name, gnutarget));
435 if (dyld_bfd != NULL)
436 {
437 gdb_bfd_ref_ptr sub
438 (gdb_bfd_mach_o_fat_extract
439 (dyld_bfd.get (), bfd_object,
440 gdbarch_bfd_arch_info (current_inferior ()->arch ())));
441 dyld_bfd = sub;
442 }
443 return dyld_bfd;
444 }
445
446 /* Extract dyld_all_image_addr when the process was just created, assuming the
447 current PC is at the entry of the dynamic linker. */
448
449 static void
450 darwin_solib_get_all_image_info_addr_at_init (struct darwin_info *info)
451 {
452 CORE_ADDR load_addr = 0;
453 gdb_bfd_ref_ptr dyld_bfd = darwin_get_dyld_bfd ();
454
455 if (dyld_bfd == NULL)
456 return;
457
458 /* We find the dynamic linker's base address by examining
459 the current pc (which should point at the entry point for the
460 dynamic linker) and subtracting the offset of the entry point. */
461 load_addr = (regcache_read_pc (get_current_regcache ())
462 - bfd_get_start_address (dyld_bfd.get ()));
463
464 /* Now try to set a breakpoint in the dynamic linker. */
465 info->all_image_addr =
466 lookup_symbol_from_bfd (dyld_bfd.get (), "_dyld_all_image_infos");
467
468 if (info->all_image_addr == 0)
469 return;
470
471 info->all_image_addr += load_addr;
472 }
473
474 /* Extract dyld_all_image_addr reading it from
475 TARGET_OBJECT_DARWIN_DYLD_INFO. */
476
477 static void
478 darwin_solib_read_all_image_info_addr (struct darwin_info *info)
479 {
480 gdb_byte buf[8];
481 LONGEST len;
482 type *ptr_type
483 = builtin_type (current_inferior ()->arch ())->builtin_data_ptr;
484
485 /* Sanity check. */
486 if (ptr_type->length () > sizeof (buf))
487 return;
488
489 len = target_read (current_inferior ()->top_target (),
490 TARGET_OBJECT_DARWIN_DYLD_INFO,
491 NULL, buf, 0, ptr_type->length ());
492 if (len <= 0)
493 return;
494
495 /* The use of BIG endian is intended, as BUF is a raw stream of bytes. This
496 makes the support of remote protocol easier. */
497 info->all_image_addr = extract_unsigned_integer (buf, len, BFD_ENDIAN_BIG);
498 }
499
500 /* Shared library startup support. See documentation in solib-svr4.c. */
501
502 static void
503 darwin_solib_create_inferior_hook (int from_tty)
504 {
505 /* Everything below only makes sense if we have a running inferior. */
506 if (!target_has_execution ())
507 return;
508
509 struct darwin_info *info = get_darwin_info ();
510 CORE_ADDR load_addr;
511
512 info->all_image_addr = 0;
513
514 darwin_solib_read_all_image_info_addr (info);
515
516 if (info->all_image_addr == 0)
517 darwin_solib_get_all_image_info_addr_at_init (info);
518
519 if (info->all_image_addr == 0)
520 return;
521
522 darwin_load_image_infos (info);
523
524 if (!darwin_dyld_version_ok (info))
525 {
526 warning (_("unhandled dyld version (%d)"), info->all_image.version);
527 return;
528 }
529
530 if (info->all_image.count != 0)
531 {
532 /* Possible relocate the main executable (PIE). */
533 load_addr = darwin_read_exec_load_addr_from_dyld (info);
534 }
535 else
536 {
537 /* Possible issue:
538 Do not break on the notifier if dyld is not initialized (deduced from
539 count == 0). In that case, dyld hasn't relocated itself and the
540 notifier may point to a wrong address. */
541
542 load_addr = darwin_read_exec_load_addr_at_init (info);
543 }
544
545 if (load_addr != 0 && current_program_space->symfile_object_file != NULL)
546 {
547 CORE_ADDR vmaddr;
548
549 /* Find the base address of the executable. */
550 vmaddr = bfd_mach_o_get_base_address (current_program_space->exec_bfd ());
551
552 /* Relocate. */
553 if (vmaddr != load_addr)
554 objfile_rebase (current_program_space->symfile_object_file,
555 load_addr - vmaddr);
556 }
557
558 /* Set solib notifier (to reload list of shared libraries). */
559 CORE_ADDR notifier = info->all_image.notifier;
560
561 if (info->all_image.count == 0)
562 {
563 /* Dyld hasn't yet relocated itself, so the notifier address may
564 be incorrect (as it has to be relocated). */
565 CORE_ADDR start
566 = bfd_get_start_address (current_program_space->exec_bfd ());
567 if (start == 0)
568 notifier = 0;
569 else
570 {
571 gdb_bfd_ref_ptr dyld_bfd = darwin_get_dyld_bfd ();
572 if (dyld_bfd != NULL)
573 {
574 CORE_ADDR dyld_bfd_start_address;
575 CORE_ADDR dyld_relocated_base_address;
576 CORE_ADDR pc;
577
578 dyld_bfd_start_address = bfd_get_start_address (dyld_bfd.get());
579
580 /* We find the dynamic linker's base address by examining
581 the current pc (which should point at the entry point
582 for the dynamic linker) and subtracting the offset of
583 the entry point. */
584
585 pc = regcache_read_pc (get_current_regcache ());
586 dyld_relocated_base_address = pc - dyld_bfd_start_address;
587
588 /* We get the proper notifier relocated address by
589 adding the dyld relocated base address to the current
590 notifier offset value. */
591
592 notifier += dyld_relocated_base_address;
593 }
594 }
595 }
596
597 /* Add the breakpoint which is hit by dyld when the list of solib is
598 modified. */
599 if (notifier != 0)
600 create_solib_event_breakpoint (current_inferior ()->arch (), notifier);
601 }
602
603 static void
604 darwin_clear_solib (void)
605 {
606 struct darwin_info *info = get_darwin_info ();
607
608 info->all_image_addr = 0;
609 info->all_image.version = 0;
610 }
611
612 static void
613 darwin_free_so (struct so_list *so)
614 {
615 lm_info_darwin *li = (lm_info_darwin *) so->lm_info;
616
617 delete li;
618 }
619
620 /* The section table is built from bfd sections using bfd VMAs.
621 Relocate these VMAs according to solib info. */
622
623 static void
624 darwin_relocate_section_addresses (struct so_list *so,
625 struct target_section *sec)
626 {
627 lm_info_darwin *li = (lm_info_darwin *) so->lm_info;
628
629 sec->addr += li->lm_addr;
630 sec->endaddr += li->lm_addr;
631
632 /* Best effort to set addr_high/addr_low. This is used only by
633 'info sharedlibary'. */
634 if (so->addr_high == 0)
635 {
636 so->addr_low = sec->addr;
637 so->addr_high = sec->endaddr;
638 }
639 if (sec->endaddr > so->addr_high)
640 so->addr_high = sec->endaddr;
641 if (sec->addr < so->addr_low)
642 so->addr_low = sec->addr;
643 }
644 \f
645 static gdb_bfd_ref_ptr
646 darwin_bfd_open (const char *pathname)
647 {
648 int found_file;
649
650 /* Search for shared library file. */
651 gdb::unique_xmalloc_ptr<char> found_pathname
652 = solib_find (pathname, &found_file);
653 if (found_pathname == NULL)
654 perror_with_name (pathname);
655
656 /* Open bfd for shared library. */
657 gdb_bfd_ref_ptr abfd (solib_bfd_fopen (found_pathname.get (), found_file));
658
659 gdb_bfd_ref_ptr res
660 (gdb_bfd_mach_o_fat_extract
661 (abfd.get (), bfd_object,
662 gdbarch_bfd_arch_info (current_inferior ()->arch ())));
663 if (res == NULL)
664 error (_("`%s': not a shared-library: %s"),
665 bfd_get_filename (abfd.get ()), bfd_errmsg (bfd_get_error ()));
666
667 /* The current filename for fat-binary BFDs is a name generated
668 by BFD, usually a string containing the name of the architecture.
669 Reset its value to the actual filename. */
670 bfd_set_filename (res.get (), pathname);
671
672 return res;
673 }
674
675 const struct target_so_ops darwin_so_ops =
676 {
677 darwin_relocate_section_addresses,
678 darwin_free_so,
679 nullptr,
680 darwin_clear_solib,
681 darwin_solib_create_inferior_hook,
682 darwin_current_sos,
683 open_symbol_file_object,
684 darwin_in_dynsym_resolve_code,
685 darwin_bfd_open,
686 };