Add generated source files and fix thinko in aarch64-asm.c
[binutils-gdb.git] / gdb / solib-darwin.c
blob707e389988f63aa2d803ecb11f2de91f757f49ba
1 /* Handle Darwin shared libraries for GDB, the GNU Debugger.
3 Copyright (C) 2009-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "defs.h"
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"
30 #include "solist.h"
31 #include "solib-darwin.h"
33 #include "mach-o.h"
34 #include "mach-o/external.h"
36 struct gdb_dyld_image_info
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;
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
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;
60 /* Current all_image_infos version. */
61 #define DYLD_VERSION_MIN 1
62 #define DYLD_VERSION_MAX 15
64 /* Per PSPACE specific data. */
65 struct darwin_info
67 /* Address of structure dyld_all_image_infos in inferior. */
68 CORE_ADDR all_image_addr = 0;
70 /* Gdb copy of dyld_all_info_infos. */
71 struct gdb_dyld_all_image_infos all_image {};
74 /* Per-program-space data key. */
75 static const registry<program_space>::key<darwin_info>
76 solib_darwin_pspace_data;
78 /* Get the darwin solib data for PSPACE. If none is found yet, add it now. This
79 function always returns a valid object. */
81 static darwin_info *
82 get_darwin_info (program_space *pspace)
84 darwin_info *info = solib_darwin_pspace_data.get (pspace);
85 if (info != nullptr)
86 return info;
88 return solib_darwin_pspace_data.emplace (pspace);
91 /* Return non-zero if the version in dyld_all_image is known. */
93 static int
94 darwin_dyld_version_ok (const struct darwin_info *info)
96 return info->all_image.version >= DYLD_VERSION_MIN
97 && info->all_image.version <= DYLD_VERSION_MAX;
100 /* Read dyld_all_image from inferior. */
102 static void
103 darwin_load_image_infos (struct darwin_info *info)
105 gdb_byte buf[24];
106 bfd_endian byte_order = gdbarch_byte_order (current_inferior ()->arch ());
107 type *ptr_type
108 = builtin_type (current_inferior ()->arch ())->builtin_data_ptr;
109 int len;
111 /* If the structure address is not known, don't continue. */
112 if (info->all_image_addr == 0)
113 return;
115 /* The structure has 4 fields: version (4 bytes), count (4 bytes),
116 info (pointer) and notifier (pointer). */
117 len = 4 + 4 + 2 * ptr_type->length ();
118 gdb_assert (len <= sizeof (buf));
119 memset (&info->all_image, 0, sizeof (info->all_image));
121 /* Read structure raw bytes from target. */
122 if (target_read_memory (info->all_image_addr, buf, len))
123 return;
125 /* Extract the fields. */
126 info->all_image.version = extract_unsigned_integer (buf, 4, byte_order);
127 if (!darwin_dyld_version_ok (info))
128 return;
130 info->all_image.count = extract_unsigned_integer (buf + 4, 4, byte_order);
131 info->all_image.info = extract_typed_address (buf + 8, ptr_type);
132 info->all_image.notifier = extract_typed_address
133 (buf + 8 + ptr_type->length (), ptr_type);
136 /* Link map info to include in an allocated so_list entry. */
138 struct lm_info_darwin final : public lm_info
140 /* The target location of lm. */
141 CORE_ADDR lm_addr = 0;
144 /* Lookup the value for a specific symbol. */
146 static CORE_ADDR
147 lookup_symbol_from_bfd (bfd *abfd, const char *symname)
149 long storage_needed;
150 asymbol **symbol_table;
151 unsigned int number_of_symbols;
152 unsigned int i;
153 CORE_ADDR symaddr = 0;
155 storage_needed = bfd_get_symtab_upper_bound (abfd);
157 if (storage_needed <= 0)
158 return 0;
160 symbol_table = (asymbol **) xmalloc (storage_needed);
161 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
163 for (i = 0; i < number_of_symbols; i++)
165 asymbol *sym = symbol_table[i];
167 if (strcmp (sym->name, symname) == 0
168 && (sym->section->flags & (SEC_CODE | SEC_DATA)) != 0)
170 /* BFD symbols are section relative. */
171 symaddr = sym->value + sym->section->vma;
172 break;
175 xfree (symbol_table);
177 return symaddr;
180 /* Return program interpreter string. */
182 static char *
183 find_program_interpreter (void)
185 char *buf = NULL;
187 /* If we have an current exec_bfd, get the interpreter from the load
188 commands. */
189 if (current_program_space->exec_bfd ())
191 bfd_mach_o_load_command *cmd;
193 if (bfd_mach_o_lookup_command (current_program_space->exec_bfd (),
194 BFD_MACH_O_LC_LOAD_DYLINKER, &cmd) == 1)
195 return cmd->command.dylinker.name_str;
198 /* If we didn't find it, read from memory.
199 FIXME: todo. */
200 return buf;
203 /* Not used. I don't see how the main symbol file can be found: the
204 interpreter name is needed and it is known from the executable file.
205 Note that darwin-nat.c implements pid_to_exec_file. */
207 static int
208 open_symbol_file_object (int from_tty)
210 return 0;
213 /* Build a list of currently loaded shared objects. See solib-svr4.c. */
215 static intrusive_list<shobj>
216 darwin_current_sos ()
218 type *ptr_type
219 = builtin_type (current_inferior ()->arch ())->builtin_data_ptr;
220 enum bfd_endian byte_order = type_byte_order (ptr_type);
221 int ptr_len = ptr_type->length ();
222 unsigned int image_info_size;
223 darwin_info *info = get_darwin_info (current_program_space);
225 /* Be sure image infos are loaded. */
226 darwin_load_image_infos (info);
228 if (!darwin_dyld_version_ok (info))
229 return {};
231 image_info_size = ptr_len * 3;
233 intrusive_list<shobj> sos;
235 /* Read infos for each solib.
236 The first entry was rumored to be the executable itself, but this is not
237 true when a large number of shared libraries are used (table expanded ?).
238 We now check all entries, but discard executable images. */
239 for (int i = 0; i < info->all_image.count; i++)
241 CORE_ADDR iinfo = info->all_image.info + i * image_info_size;
242 gdb_byte buf[image_info_size];
243 CORE_ADDR load_addr;
244 CORE_ADDR path_addr;
245 struct mach_o_header_external hdr;
246 unsigned long hdr_val;
248 /* Read image info from inferior. */
249 if (target_read_memory (iinfo, buf, image_info_size))
250 break;
252 load_addr = extract_typed_address (buf, ptr_type);
253 path_addr = extract_typed_address (buf + ptr_len, ptr_type);
255 /* Read Mach-O header from memory. */
256 if (target_read_memory (load_addr, (gdb_byte *) &hdr, sizeof (hdr) - 4))
257 break;
258 /* Discard wrong magic numbers. Shouldn't happen. */
259 hdr_val = extract_unsigned_integer
260 (hdr.magic, sizeof (hdr.magic), byte_order);
261 if (hdr_val != BFD_MACH_O_MH_MAGIC && hdr_val != BFD_MACH_O_MH_MAGIC_64)
262 continue;
263 /* Discard executable. Should happen only once. */
264 hdr_val = extract_unsigned_integer
265 (hdr.filetype, sizeof (hdr.filetype), byte_order);
266 if (hdr_val == BFD_MACH_O_MH_EXECUTE)
267 continue;
269 gdb::unique_xmalloc_ptr<char> file_path
270 = target_read_string (path_addr, SO_NAME_MAX_PATH_SIZE - 1);
271 if (file_path == nullptr)
272 break;
274 /* Create and fill the new struct shobj element. */
275 shobj *newobj = new shobj;
277 auto li = std::make_unique<lm_info_darwin> ();
279 newobj->so_name = file_path.get ();
280 newobj->so_original_name = newobj->so_name;
281 li->lm_addr = load_addr;
283 newobj->lm_info = std::move (li);
284 sos.push_back (*newobj);
287 return sos;
290 /* Check LOAD_ADDR points to a Mach-O executable header. Return LOAD_ADDR
291 in case of success, 0 in case of failure. */
293 static CORE_ADDR
294 darwin_validate_exec_header (CORE_ADDR load_addr)
296 bfd_endian byte_order = gdbarch_byte_order (current_inferior ()->arch ());
297 struct mach_o_header_external hdr;
298 unsigned long hdr_val;
300 /* Read Mach-O header from memory. */
301 if (target_read_memory (load_addr, (gdb_byte *) &hdr, sizeof (hdr) - 4))
302 return 0;
304 /* Discard wrong magic numbers. Shouldn't happen. */
305 hdr_val = extract_unsigned_integer
306 (hdr.magic, sizeof (hdr.magic), byte_order);
307 if (hdr_val != BFD_MACH_O_MH_MAGIC && hdr_val != BFD_MACH_O_MH_MAGIC_64)
308 return 0;
310 /* Check executable. */
311 hdr_val = extract_unsigned_integer
312 (hdr.filetype, sizeof (hdr.filetype), byte_order);
313 if (hdr_val == BFD_MACH_O_MH_EXECUTE)
314 return load_addr;
316 return 0;
319 /* Get the load address of the executable using dyld list of images.
320 We assume that the dyld info are correct (which is wrong if the target
321 is stopped at the first instruction). */
323 static CORE_ADDR
324 darwin_read_exec_load_addr_from_dyld (struct darwin_info *info)
326 type *ptr_type
327 = builtin_type (current_inferior ()->arch ())->builtin_data_ptr;
328 int ptr_len = ptr_type->length ();
329 unsigned int image_info_size = ptr_len * 3;
330 int i;
332 /* Read infos for each solib. One of them should be the executable. */
333 for (i = 0; i < info->all_image.count; i++)
335 CORE_ADDR iinfo = info->all_image.info + i * image_info_size;
336 gdb_byte buf[image_info_size];
337 CORE_ADDR load_addr;
339 /* Read image info from inferior. */
340 if (target_read_memory (iinfo, buf, image_info_size))
341 break;
343 load_addr = extract_typed_address (buf, ptr_type);
344 if (darwin_validate_exec_header (load_addr) == load_addr)
345 return load_addr;
348 return 0;
351 /* Get the load address of the executable when the PC is at the dyld
352 entry point using parameter passed by the kernel (at SP). */
354 static CORE_ADDR
355 darwin_read_exec_load_addr_at_init (struct darwin_info *info)
357 gdbarch *gdbarch = current_inferior ()->arch ();
358 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
359 int addr_size = gdbarch_addr_bit (gdbarch) / 8;
360 ULONGEST load_ptr_addr;
361 ULONGEST load_addr;
362 gdb_byte buf[8];
364 /* Get SP. */
365 if (regcache_cooked_read_unsigned (get_thread_regcache (inferior_thread ()),
366 gdbarch_sp_regnum (gdbarch),
367 &load_ptr_addr) != REG_VALID)
368 return 0;
370 /* Read value at SP (image load address). */
371 if (target_read_memory (load_ptr_addr, buf, addr_size))
372 return 0;
374 load_addr = extract_unsigned_integer (buf, addr_size, byte_order);
376 return darwin_validate_exec_header (load_addr);
379 /* Return 1 if PC lies in the dynamic symbol resolution code of the
380 run time loader. */
382 static int
383 darwin_in_dynsym_resolve_code (CORE_ADDR pc)
385 return 0;
388 /* A wrapper for bfd_mach_o_fat_extract that handles reference
389 counting properly. This will either return NULL, or return a new
390 reference to a BFD. */
392 static gdb_bfd_ref_ptr
393 gdb_bfd_mach_o_fat_extract (bfd *abfd, bfd_format format,
394 const bfd_arch_info_type *arch)
396 bfd *result = bfd_mach_o_fat_extract (abfd, format, arch);
398 if (result == NULL)
399 return NULL;
401 if (result == abfd)
402 gdb_bfd_ref (result);
403 else
404 gdb_bfd_mark_parent (result, abfd);
406 return gdb_bfd_ref_ptr (result);
409 /* Return the BFD for the program interpreter. */
411 static gdb_bfd_ref_ptr
412 darwin_get_dyld_bfd ()
414 char *interp_name;
416 /* This method doesn't work with an attached process. */
417 if (current_inferior ()->attach_flag)
418 return NULL;
420 /* Find the program interpreter. */
421 interp_name = find_program_interpreter ();
422 if (!interp_name)
423 return NULL;
425 /* Create a bfd for the interpreter. */
426 gdb_bfd_ref_ptr dyld_bfd (gdb_bfd_open (interp_name, gnutarget));
427 if (dyld_bfd != NULL)
429 gdb_bfd_ref_ptr sub
430 (gdb_bfd_mach_o_fat_extract
431 (dyld_bfd.get (), bfd_object,
432 gdbarch_bfd_arch_info (current_inferior ()->arch ())));
433 dyld_bfd = sub;
435 return dyld_bfd;
438 /* Extract dyld_all_image_addr when the process was just created, assuming the
439 current PC is at the entry of the dynamic linker. */
441 static void
442 darwin_solib_get_all_image_info_addr_at_init (struct darwin_info *info)
444 CORE_ADDR load_addr = 0;
445 gdb_bfd_ref_ptr dyld_bfd = darwin_get_dyld_bfd ();
447 if (dyld_bfd == NULL)
448 return;
450 /* We find the dynamic linker's base address by examining
451 the current pc (which should point at the entry point for the
452 dynamic linker) and subtracting the offset of the entry point. */
453 load_addr = (regcache_read_pc (get_thread_regcache (inferior_thread ()))
454 - bfd_get_start_address (dyld_bfd.get ()));
456 /* Now try to set a breakpoint in the dynamic linker. */
457 info->all_image_addr =
458 lookup_symbol_from_bfd (dyld_bfd.get (), "_dyld_all_image_infos");
460 if (info->all_image_addr == 0)
461 return;
463 info->all_image_addr += load_addr;
466 /* Extract dyld_all_image_addr reading it from
467 TARGET_OBJECT_DARWIN_DYLD_INFO. */
469 static void
470 darwin_solib_read_all_image_info_addr (struct darwin_info *info)
472 gdb_byte buf[8];
473 LONGEST len;
474 type *ptr_type
475 = builtin_type (current_inferior ()->arch ())->builtin_data_ptr;
477 /* Sanity check. */
478 if (ptr_type->length () > sizeof (buf))
479 return;
481 len = target_read (current_inferior ()->top_target (),
482 TARGET_OBJECT_DARWIN_DYLD_INFO,
483 NULL, buf, 0, ptr_type->length ());
484 if (len <= 0)
485 return;
487 /* The use of BIG endian is intended, as BUF is a raw stream of bytes. This
488 makes the support of remote protocol easier. */
489 info->all_image_addr = extract_unsigned_integer (buf, len, BFD_ENDIAN_BIG);
492 /* Shared library startup support. See documentation in solib-svr4.c. */
494 static void
495 darwin_solib_create_inferior_hook (int from_tty)
497 /* Everything below only makes sense if we have a running inferior. */
498 if (!target_has_execution ())
499 return;
501 darwin_info *info = get_darwin_info (current_program_space);
502 CORE_ADDR load_addr;
504 info->all_image_addr = 0;
506 darwin_solib_read_all_image_info_addr (info);
508 if (info->all_image_addr == 0)
509 darwin_solib_get_all_image_info_addr_at_init (info);
511 if (info->all_image_addr == 0)
512 return;
514 darwin_load_image_infos (info);
516 if (!darwin_dyld_version_ok (info))
518 warning (_("unhandled dyld version (%d)"), info->all_image.version);
519 return;
522 if (info->all_image.count != 0)
524 /* Possible relocate the main executable (PIE). */
525 load_addr = darwin_read_exec_load_addr_from_dyld (info);
527 else
529 /* Possible issue:
530 Do not break on the notifier if dyld is not initialized (deduced from
531 count == 0). In that case, dyld hasn't relocated itself and the
532 notifier may point to a wrong address. */
534 load_addr = darwin_read_exec_load_addr_at_init (info);
537 if (load_addr != 0 && current_program_space->symfile_object_file != NULL)
539 CORE_ADDR vmaddr;
541 /* Find the base address of the executable. */
542 vmaddr = bfd_mach_o_get_base_address (current_program_space->exec_bfd ());
544 /* Relocate. */
545 if (vmaddr != load_addr)
546 objfile_rebase (current_program_space->symfile_object_file,
547 load_addr - vmaddr);
550 /* Set solib notifier (to reload list of shared libraries). */
551 CORE_ADDR notifier = info->all_image.notifier;
553 if (info->all_image.count == 0)
555 /* Dyld hasn't yet relocated itself, so the notifier address may
556 be incorrect (as it has to be relocated). */
557 CORE_ADDR start
558 = bfd_get_start_address (current_program_space->exec_bfd ());
559 if (start == 0)
560 notifier = 0;
561 else
563 gdb_bfd_ref_ptr dyld_bfd = darwin_get_dyld_bfd ();
564 if (dyld_bfd != NULL)
566 CORE_ADDR dyld_bfd_start_address;
567 CORE_ADDR dyld_relocated_base_address;
568 CORE_ADDR pc;
570 dyld_bfd_start_address = bfd_get_start_address (dyld_bfd.get());
572 /* We find the dynamic linker's base address by examining
573 the current pc (which should point at the entry point
574 for the dynamic linker) and subtracting the offset of
575 the entry point. */
577 pc = regcache_read_pc (get_thread_regcache (inferior_thread ()));
578 dyld_relocated_base_address = pc - dyld_bfd_start_address;
580 /* We get the proper notifier relocated address by
581 adding the dyld relocated base address to the current
582 notifier offset value. */
584 notifier += dyld_relocated_base_address;
589 /* Add the breakpoint which is hit by dyld when the list of solib is
590 modified. */
591 if (notifier != 0)
592 create_solib_event_breakpoint (current_inferior ()->arch (), notifier);
595 static void
596 darwin_clear_solib (program_space *pspace)
598 darwin_info *info = get_darwin_info (pspace);
600 info->all_image_addr = 0;
601 info->all_image.version = 0;
604 /* The section table is built from bfd sections using bfd VMAs.
605 Relocate these VMAs according to solib info. */
607 static void
608 darwin_relocate_section_addresses (shobj &so, target_section *sec)
610 auto *li = gdb::checked_static_cast<lm_info_darwin *> (so.lm_info.get ());
612 sec->addr += li->lm_addr;
613 sec->endaddr += li->lm_addr;
615 /* Best effort to set addr_high/addr_low. This is used only by
616 'info sharedlibary'. */
617 if (so.addr_high == 0)
619 so.addr_low = sec->addr;
620 so.addr_high = sec->endaddr;
622 if (sec->endaddr > so.addr_high)
623 so.addr_high = sec->endaddr;
624 if (sec->addr < so.addr_low)
625 so.addr_low = sec->addr;
628 static gdb_bfd_ref_ptr
629 darwin_bfd_open (const char *pathname)
631 int found_file;
633 /* Search for shared library file. */
634 gdb::unique_xmalloc_ptr<char> found_pathname
635 = solib_find (pathname, &found_file);
636 if (found_pathname == NULL)
637 perror_with_name (pathname);
639 /* Open bfd for shared library. */
640 gdb_bfd_ref_ptr abfd (solib_bfd_fopen (found_pathname.get (), found_file));
642 gdb_bfd_ref_ptr res
643 (gdb_bfd_mach_o_fat_extract
644 (abfd.get (), bfd_object,
645 gdbarch_bfd_arch_info (current_inferior ()->arch ())));
646 if (res == NULL)
647 error (_("`%s': not a shared-library: %s"),
648 bfd_get_filename (abfd.get ()), bfd_errmsg (bfd_get_error ()));
650 /* The current filename for fat-binary BFDs is a name generated
651 by BFD, usually a string containing the name of the architecture.
652 Reset its value to the actual filename. */
653 bfd_set_filename (res.get (), pathname);
655 return res;
658 const struct target_so_ops darwin_so_ops =
660 darwin_relocate_section_addresses,
661 nullptr,
662 darwin_clear_solib,
663 darwin_solib_create_inferior_hook,
664 darwin_current_sos,
665 open_symbol_file_object,
666 darwin_in_dynsym_resolve_code,
667 darwin_bfd_open,