RISC-V: Don't report warnings when linking different privileged spec objects.
[binutils-gdb.git] / gdb / solib-darwin.c
blob6c7d9065a658a6957ac502037a2becb30d462c43
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/>. */
21 #include "bfd.h"
22 #include "extract-store-integer.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 owning_intrusive_list<solib>
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 owning_intrusive_list<solib> 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_vector 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.data (), image_info_size))
250 break;
252 load_addr = extract_typed_address (buf.data (), ptr_type);
253 path_addr = extract_typed_address (buf.data () + 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 solib element. */
275 auto &newobj = sos.emplace_back ();
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);
286 return sos;
289 /* Check LOAD_ADDR points to a Mach-O executable header. Return LOAD_ADDR
290 in case of success, 0 in case of failure. */
292 static CORE_ADDR
293 darwin_validate_exec_header (CORE_ADDR load_addr)
295 bfd_endian byte_order = gdbarch_byte_order (current_inferior ()->arch ());
296 struct mach_o_header_external hdr;
297 unsigned long hdr_val;
299 /* Read Mach-O header from memory. */
300 if (target_read_memory (load_addr, (gdb_byte *) &hdr, sizeof (hdr) - 4))
301 return 0;
303 /* Discard wrong magic numbers. Shouldn't happen. */
304 hdr_val = extract_unsigned_integer
305 (hdr.magic, sizeof (hdr.magic), byte_order);
306 if (hdr_val != BFD_MACH_O_MH_MAGIC && hdr_val != BFD_MACH_O_MH_MAGIC_64)
307 return 0;
309 /* Check executable. */
310 hdr_val = extract_unsigned_integer
311 (hdr.filetype, sizeof (hdr.filetype), byte_order);
312 if (hdr_val == BFD_MACH_O_MH_EXECUTE)
313 return load_addr;
315 return 0;
318 /* Get the load address of the executable using dyld list of images.
319 We assume that the dyld info are correct (which is wrong if the target
320 is stopped at the first instruction). */
322 static CORE_ADDR
323 darwin_read_exec_load_addr_from_dyld (struct darwin_info *info)
325 type *ptr_type
326 = builtin_type (current_inferior ()->arch ())->builtin_data_ptr;
327 int ptr_len = ptr_type->length ();
328 unsigned int image_info_size = ptr_len * 3;
329 int i;
331 /* Read infos for each solib. One of them should be the executable. */
332 for (i = 0; i < info->all_image.count; i++)
334 CORE_ADDR iinfo = info->all_image.info + i * image_info_size;
335 gdb::byte_vector buf (image_info_size);
336 CORE_ADDR load_addr;
338 /* Read image info from inferior. */
339 if (target_read_memory (iinfo, buf.data (), image_info_size))
340 break;
342 load_addr = extract_typed_address (buf.data (), ptr_type);
343 if (darwin_validate_exec_header (load_addr) == load_addr)
344 return load_addr;
347 return 0;
350 /* Get the load address of the executable when the PC is at the dyld
351 entry point using parameter passed by the kernel (at SP). */
353 static CORE_ADDR
354 darwin_read_exec_load_addr_at_init (struct darwin_info *info)
356 gdbarch *gdbarch = current_inferior ()->arch ();
357 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
358 int addr_size = gdbarch_addr_bit (gdbarch) / 8;
359 ULONGEST load_ptr_addr;
360 ULONGEST load_addr;
361 gdb_byte buf[8];
363 /* Get SP. */
364 if (regcache_cooked_read_unsigned (get_thread_regcache (inferior_thread ()),
365 gdbarch_sp_regnum (gdbarch),
366 &load_ptr_addr) != REG_VALID)
367 return 0;
369 /* Read value at SP (image load address). */
370 if (target_read_memory (load_ptr_addr, buf, addr_size))
371 return 0;
373 load_addr = extract_unsigned_integer (buf, addr_size, byte_order);
375 return darwin_validate_exec_header (load_addr);
378 /* Return 1 if PC lies in the dynamic symbol resolution code of the
379 run time loader. */
381 static int
382 darwin_in_dynsym_resolve_code (CORE_ADDR pc)
384 return 0;
387 /* A wrapper for bfd_mach_o_fat_extract that handles reference
388 counting properly. This will either return NULL, or return a new
389 reference to a BFD. */
391 static gdb_bfd_ref_ptr
392 gdb_bfd_mach_o_fat_extract (bfd *abfd, bfd_format format,
393 const bfd_arch_info_type *arch)
395 bfd *result = bfd_mach_o_fat_extract (abfd, format, arch);
397 if (result == NULL)
398 return NULL;
400 if (result == abfd)
401 gdb_bfd_ref (result);
402 else
403 gdb_bfd_mark_parent (result, abfd);
405 return gdb_bfd_ref_ptr (result);
408 /* Return the BFD for the program interpreter. */
410 static gdb_bfd_ref_ptr
411 darwin_get_dyld_bfd ()
413 char *interp_name;
415 /* This method doesn't work with an attached process. */
416 if (current_inferior ()->attach_flag)
417 return NULL;
419 /* Find the program interpreter. */
420 interp_name = find_program_interpreter ();
421 if (!interp_name)
422 return NULL;
424 /* Create a bfd for the interpreter. */
425 gdb_bfd_ref_ptr dyld_bfd (gdb_bfd_open (interp_name, gnutarget));
426 if (dyld_bfd != NULL)
428 gdb_bfd_ref_ptr sub
429 (gdb_bfd_mach_o_fat_extract
430 (dyld_bfd.get (), bfd_object,
431 gdbarch_bfd_arch_info (current_inferior ()->arch ())));
432 dyld_bfd = sub;
434 return dyld_bfd;
437 /* Extract dyld_all_image_addr when the process was just created, assuming the
438 current PC is at the entry of the dynamic linker. */
440 static void
441 darwin_solib_get_all_image_info_addr_at_init (struct darwin_info *info)
443 CORE_ADDR load_addr = 0;
444 gdb_bfd_ref_ptr dyld_bfd = darwin_get_dyld_bfd ();
446 if (dyld_bfd == NULL)
447 return;
449 /* We find the dynamic linker's base address by examining
450 the current pc (which should point at the entry point for the
451 dynamic linker) and subtracting the offset of the entry point. */
452 load_addr = (regcache_read_pc (get_thread_regcache (inferior_thread ()))
453 - bfd_get_start_address (dyld_bfd.get ()));
455 /* Now try to set a breakpoint in the dynamic linker. */
456 info->all_image_addr =
457 lookup_symbol_from_bfd (dyld_bfd.get (), "_dyld_all_image_infos");
459 if (info->all_image_addr == 0)
460 return;
462 info->all_image_addr += load_addr;
465 /* Extract dyld_all_image_addr reading it from
466 TARGET_OBJECT_DARWIN_DYLD_INFO. */
468 static void
469 darwin_solib_read_all_image_info_addr (struct darwin_info *info)
471 gdb_byte buf[8];
472 LONGEST len;
473 type *ptr_type
474 = builtin_type (current_inferior ()->arch ())->builtin_data_ptr;
476 /* Sanity check. */
477 if (ptr_type->length () > sizeof (buf))
478 return;
480 len = target_read (current_inferior ()->top_target (),
481 TARGET_OBJECT_DARWIN_DYLD_INFO,
482 NULL, buf, 0, ptr_type->length ());
483 if (len <= 0)
484 return;
486 /* The use of BIG endian is intended, as BUF is a raw stream of bytes. This
487 makes the support of remote protocol easier. */
488 info->all_image_addr = extract_unsigned_integer (buf, len, BFD_ENDIAN_BIG);
491 /* Shared library startup support. See documentation in solib-svr4.c. */
493 static void
494 darwin_solib_create_inferior_hook (int from_tty)
496 /* Everything below only makes sense if we have a running inferior. */
497 if (!target_has_execution ())
498 return;
500 darwin_info *info = get_darwin_info (current_program_space);
501 CORE_ADDR load_addr;
503 info->all_image_addr = 0;
505 darwin_solib_read_all_image_info_addr (info);
507 if (info->all_image_addr == 0)
508 darwin_solib_get_all_image_info_addr_at_init (info);
510 if (info->all_image_addr == 0)
511 return;
513 darwin_load_image_infos (info);
515 if (!darwin_dyld_version_ok (info))
517 warning (_("unhandled dyld version (%d)"), info->all_image.version);
518 return;
521 if (info->all_image.count != 0)
523 /* Possible relocate the main executable (PIE). */
524 load_addr = darwin_read_exec_load_addr_from_dyld (info);
526 else
528 /* Possible issue:
529 Do not break on the notifier if dyld is not initialized (deduced from
530 count == 0). In that case, dyld hasn't relocated itself and the
531 notifier may point to a wrong address. */
533 load_addr = darwin_read_exec_load_addr_at_init (info);
536 if (load_addr != 0 && current_program_space->symfile_object_file != NULL)
538 CORE_ADDR vmaddr;
540 /* Find the base address of the executable. */
541 vmaddr = bfd_mach_o_get_base_address (current_program_space->exec_bfd ());
543 /* Relocate. */
544 if (vmaddr != load_addr)
545 objfile_rebase (current_program_space->symfile_object_file,
546 load_addr - vmaddr);
549 /* Set solib notifier (to reload list of shared libraries). */
550 CORE_ADDR notifier = info->all_image.notifier;
552 if (info->all_image.count == 0)
554 /* Dyld hasn't yet relocated itself, so the notifier address may
555 be incorrect (as it has to be relocated). */
556 CORE_ADDR start
557 = bfd_get_start_address (current_program_space->exec_bfd ());
558 if (start == 0)
559 notifier = 0;
560 else
562 gdb_bfd_ref_ptr dyld_bfd = darwin_get_dyld_bfd ();
563 if (dyld_bfd != NULL)
565 CORE_ADDR dyld_bfd_start_address;
566 CORE_ADDR dyld_relocated_base_address;
567 CORE_ADDR pc;
569 dyld_bfd_start_address = bfd_get_start_address (dyld_bfd.get());
571 /* We find the dynamic linker's base address by examining
572 the current pc (which should point at the entry point
573 for the dynamic linker) and subtracting the offset of
574 the entry point. */
576 pc = regcache_read_pc (get_thread_regcache (inferior_thread ()));
577 dyld_relocated_base_address = pc - dyld_bfd_start_address;
579 /* We get the proper notifier relocated address by
580 adding the dyld relocated base address to the current
581 notifier offset value. */
583 notifier += dyld_relocated_base_address;
588 /* Add the breakpoint which is hit by dyld when the list of solib is
589 modified. */
590 if (notifier != 0)
591 create_solib_event_breakpoint (current_inferior ()->arch (), notifier);
594 static void
595 darwin_clear_solib (program_space *pspace)
597 darwin_info *info = get_darwin_info (pspace);
599 info->all_image_addr = 0;
600 info->all_image.version = 0;
603 /* The section table is built from bfd sections using bfd VMAs.
604 Relocate these VMAs according to solib info. */
606 static void
607 darwin_relocate_section_addresses (solib &so, target_section *sec)
609 auto *li = gdb::checked_static_cast<lm_info_darwin *> (so.lm_info.get ());
611 sec->addr += li->lm_addr;
612 sec->endaddr += li->lm_addr;
614 /* Best effort to set addr_high/addr_low. This is used only by
615 'info sharedlibary'. */
616 if (so.addr_high == 0)
618 so.addr_low = sec->addr;
619 so.addr_high = sec->endaddr;
621 if (sec->endaddr > so.addr_high)
622 so.addr_high = sec->endaddr;
623 if (sec->addr < so.addr_low)
624 so.addr_low = sec->addr;
627 static gdb_bfd_ref_ptr
628 darwin_bfd_open (const char *pathname)
630 int found_file;
632 /* Search for shared library file. */
633 gdb::unique_xmalloc_ptr<char> found_pathname
634 = solib_find (pathname, &found_file);
635 if (found_pathname == NULL)
636 perror_with_name (pathname);
638 /* Open bfd for shared library. */
639 gdb_bfd_ref_ptr abfd (solib_bfd_fopen (found_pathname.get (), found_file));
641 gdb_bfd_ref_ptr res
642 (gdb_bfd_mach_o_fat_extract
643 (abfd.get (), bfd_object,
644 gdbarch_bfd_arch_info (current_inferior ()->arch ())));
645 if (res == NULL)
646 error (_("`%s': not a shared-library: %s"),
647 bfd_get_filename (abfd.get ()), bfd_errmsg (bfd_get_error ()));
649 /* The current filename for fat-binary BFDs is a name generated
650 by BFD, usually a string containing the name of the architecture.
651 Reset its value to the actual filename. */
652 bfd_set_filename (res.get (), pathname);
654 return res;
657 const solib_ops darwin_so_ops =
659 darwin_relocate_section_addresses,
660 nullptr,
661 darwin_clear_solib,
662 darwin_solib_create_inferior_hook,
663 darwin_current_sos,
664 open_symbol_file_object,
665 darwin_in_dynsym_resolve_code,
666 darwin_bfd_open,
667 nullptr,
668 nullptr,
669 nullptr,
670 nullptr,
671 default_find_solib_addr,