OLD | NEW |
1 /* Handle Darwin shared libraries for GDB, the GNU Debugger. | 1 /* Handle Darwin shared libraries for GDB, the GNU Debugger. |
2 | 2 |
3 Copyright (C) 2009-2012 Free Software Foundation, Inc. | 3 Copyright (C) 2009-2012 Free Software Foundation, Inc. |
4 | 4 |
5 This file is part of GDB. | 5 This file is part of GDB. |
6 | 6 |
7 This program is free software; you can redistribute it and/or modify | 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 | 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 | 9 the Free Software Foundation; either version 3 of the License, or |
10 (at your option) any later version. | 10 (at your option) any later version. |
(...skipping 23 matching lines...) Expand all Loading... |
34 #include "solist.h" | 34 #include "solist.h" |
35 #include "solib.h" | 35 #include "solib.h" |
36 #include "solib-svr4.h" | 36 #include "solib-svr4.h" |
37 | 37 |
38 #include "bfd-target.h" | 38 #include "bfd-target.h" |
39 #include "elf-bfd.h" | 39 #include "elf-bfd.h" |
40 #include "exec.h" | 40 #include "exec.h" |
41 #include "auxv.h" | 41 #include "auxv.h" |
42 #include "exceptions.h" | 42 #include "exceptions.h" |
43 #include "mach-o.h" | 43 #include "mach-o.h" |
| 44 #include "mach-o/external.h" |
44 | 45 |
45 struct gdb_dyld_image_info | 46 struct gdb_dyld_image_info |
46 { | 47 { |
47 /* Base address (which corresponds to the Mach-O header). */ | 48 /* Base address (which corresponds to the Mach-O header). */ |
48 CORE_ADDR mach_header; | 49 CORE_ADDR mach_header; |
49 /* Image file path. */ | 50 /* Image file path. */ |
50 CORE_ADDR file_path; | 51 CORE_ADDR file_path; |
51 /* st.m_time of image file. */ | 52 /* st.m_time of image file. */ |
52 unsigned long mtime; | 53 unsigned long mtime; |
53 }; | 54 }; |
54 | 55 |
55 /* Content of inferior dyld_all_image_infos structure. | 56 /* Content of inferior dyld_all_image_infos structure. |
56 See /usr/include/mach-o/dyld_images.h for the documentation. */ | 57 See /usr/include/mach-o/dyld_images.h for the documentation. */ |
57 struct gdb_dyld_all_image_infos | 58 struct gdb_dyld_all_image_infos |
58 { | 59 { |
59 /* Version (1). */ | 60 /* Version (1). */ |
60 unsigned int version; | 61 unsigned int version; |
61 /* Number of images. */ | 62 /* Number of images. */ |
62 unsigned int count; | 63 unsigned int count; |
63 /* Image description. */ | 64 /* Image description. */ |
64 CORE_ADDR info; | 65 CORE_ADDR info; |
65 /* Notifier (function called when a library is added or removed). */ | 66 /* Notifier (function called when a library is added or removed). */ |
66 CORE_ADDR notifier; | 67 CORE_ADDR notifier; |
67 }; | 68 }; |
68 | 69 |
69 /* Current all_image_infos version. */ | 70 /* Current all_image_infos version. */ |
70 #define DYLD_VERSION_MIN 1 | 71 #define DYLD_VERSION_MIN 1 |
71 #define DYLD_VERSION_MAX 12 | 72 #define DYLD_VERSION_MAX 12 |
72 | 73 |
73 /* Address of structure dyld_all_image_infos in inferior. */ | 74 /* Per PSPACE specific data. */ |
74 static CORE_ADDR dyld_all_image_addr; | 75 struct darwin_info |
| 76 { |
| 77 /* Address of structure dyld_all_image_infos in inferior. */ |
| 78 CORE_ADDR all_image_addr; |
75 | 79 |
76 /* Gdb copy of dyld_all_info_infos. */ | 80 /* Gdb copy of dyld_all_info_infos. */ |
77 static struct gdb_dyld_all_image_infos dyld_all_image; | 81 struct gdb_dyld_all_image_infos all_image; |
| 82 }; |
| 83 |
| 84 /* Per-program-space data key. */ |
| 85 static const struct program_space_data *solib_darwin_pspace_data; |
| 86 |
| 87 static void |
| 88 darwin_pspace_data_cleanup (struct program_space *pspace, void *arg) |
| 89 { |
| 90 struct darwin_info *info; |
| 91 |
| 92 info = program_space_data (pspace, solib_darwin_pspace_data); |
| 93 xfree (info); |
| 94 } |
| 95 |
| 96 /* Get the current darwin data. If none is found yet, add it now. This |
| 97 function always returns a valid object. */ |
| 98 |
| 99 static struct darwin_info * |
| 100 get_darwin_info (void) |
| 101 { |
| 102 struct darwin_info *info; |
| 103 |
| 104 info = program_space_data (current_program_space, solib_darwin_pspace_data); |
| 105 if (info != NULL) |
| 106 return info; |
| 107 |
| 108 info = XZALLOC (struct darwin_info); |
| 109 set_program_space_data (current_program_space, |
| 110 » » » solib_darwin_pspace_data, info); |
| 111 return info; |
| 112 } |
78 | 113 |
79 /* Return non-zero if the version in dyld_all_image is known. */ | 114 /* Return non-zero if the version in dyld_all_image is known. */ |
80 | 115 |
81 static int | 116 static int |
82 darwin_dyld_version_ok (void) | 117 darwin_dyld_version_ok (const struct darwin_info *info) |
83 { | 118 { |
84 return dyld_all_image.version >= DYLD_VERSION_MIN | 119 return info->all_image.version >= DYLD_VERSION_MIN |
85 && dyld_all_image.version <= DYLD_VERSION_MAX; | 120 && info->all_image.version <= DYLD_VERSION_MAX; |
86 } | 121 } |
87 | 122 |
88 /* Read dyld_all_image from inferior. */ | 123 /* Read dyld_all_image from inferior. */ |
89 | 124 |
90 static void | 125 static void |
91 darwin_load_image_infos (void) | 126 darwin_load_image_infos (struct darwin_info *info) |
92 { | 127 { |
93 gdb_byte buf[24]; | 128 gdb_byte buf[24]; |
94 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch); | 129 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch); |
95 struct type *ptr_type = builtin_type (target_gdbarch)->builtin_data_ptr; | 130 struct type *ptr_type = builtin_type (target_gdbarch)->builtin_data_ptr; |
96 int len; | 131 int len; |
97 | 132 |
98 /* If the structure address is not known, don't continue. */ | 133 /* If the structure address is not known, don't continue. */ |
99 if (dyld_all_image_addr == 0) | 134 if (info->all_image_addr == 0) |
100 return; | 135 return; |
101 | 136 |
102 /* The structure has 4 fields: version (4 bytes), count (4 bytes), | 137 /* The structure has 4 fields: version (4 bytes), count (4 bytes), |
103 info (pointer) and notifier (pointer). */ | 138 info (pointer) and notifier (pointer). */ |
104 len = 4 + 4 + 2 * ptr_type->length; | 139 len = 4 + 4 + 2 * ptr_type->length; |
105 gdb_assert (len <= sizeof (buf)); | 140 gdb_assert (len <= sizeof (buf)); |
106 memset (&dyld_all_image, 0, sizeof (dyld_all_image)); | 141 memset (&info->all_image, 0, sizeof (info->all_image)); |
107 | 142 |
108 /* Read structure raw bytes from target. */ | 143 /* Read structure raw bytes from target. */ |
109 if (target_read_memory (dyld_all_image_addr, buf, len)) | 144 if (target_read_memory (info->all_image_addr, buf, len)) |
110 return; | 145 return; |
111 | 146 |
112 /* Extract the fields. */ | 147 /* Extract the fields. */ |
113 dyld_all_image.version = extract_unsigned_integer (buf, 4, byte_order); | 148 info->all_image.version = extract_unsigned_integer (buf, 4, byte_order); |
114 if (!darwin_dyld_version_ok ()) | 149 if (!darwin_dyld_version_ok (info)) |
115 return; | 150 return; |
116 | 151 |
117 dyld_all_image.count = extract_unsigned_integer (buf + 4, 4, byte_order); | 152 info->all_image.count = extract_unsigned_integer (buf + 4, 4, byte_order); |
118 dyld_all_image.info = extract_typed_address (buf + 8, ptr_type); | 153 info->all_image.info = extract_typed_address (buf + 8, ptr_type); |
119 dyld_all_image.notifier = extract_typed_address | 154 info->all_image.notifier = extract_typed_address |
120 (buf + 8 + ptr_type->length, ptr_type); | 155 (buf + 8 + ptr_type->length, ptr_type); |
121 } | 156 } |
122 | 157 |
123 /* Link map info to include in an allocated so_list entry. */ | 158 /* Link map info to include in an allocated so_list entry. */ |
124 | 159 |
125 struct lm_info | 160 struct lm_info |
126 { | 161 { |
127 /* The target location of lm. */ | 162 /* The target location of lm. */ |
128 CORE_ADDR lm_addr; | 163 CORE_ADDR lm_addr; |
129 }; | 164 }; |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 { | 238 { |
204 return 0; | 239 return 0; |
205 } | 240 } |
206 | 241 |
207 /* Build a list of currently loaded shared objects. See solib-svr4.c. */ | 242 /* Build a list of currently loaded shared objects. See solib-svr4.c. */ |
208 | 243 |
209 static struct so_list * | 244 static struct so_list * |
210 darwin_current_sos (void) | 245 darwin_current_sos (void) |
211 { | 246 { |
212 struct type *ptr_type = builtin_type (target_gdbarch)->builtin_data_ptr; | 247 struct type *ptr_type = builtin_type (target_gdbarch)->builtin_data_ptr; |
| 248 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch); |
213 int ptr_len = TYPE_LENGTH (ptr_type); | 249 int ptr_len = TYPE_LENGTH (ptr_type); |
214 unsigned int image_info_size; | 250 unsigned int image_info_size; |
215 CORE_ADDR lm; | |
216 struct so_list *head = NULL; | 251 struct so_list *head = NULL; |
217 struct so_list *tail = NULL; | 252 struct so_list *tail = NULL; |
218 int i; | 253 int i; |
| 254 struct darwin_info *info = get_darwin_info (); |
219 | 255 |
220 /* Be sure image infos are loaded. */ | 256 /* Be sure image infos are loaded. */ |
221 darwin_load_image_infos (); | 257 darwin_load_image_infos (info); |
222 | 258 |
223 if (!darwin_dyld_version_ok ()) | 259 if (!darwin_dyld_version_ok (info)) |
224 return NULL; | 260 return NULL; |
225 | 261 |
226 image_info_size = ptr_len * 3; | 262 image_info_size = ptr_len * 3; |
227 | 263 |
228 /* Read infos for each solib. | 264 /* Read infos for each solib. |
229 This first entry is ignored as this is the executable itself. */ | 265 The first entry was rumored to be the executable itself, but this is not |
230 for (i = 1; i < dyld_all_image.count; i++) | 266 true when a large number of shared libraries are used (table expanded ?). |
| 267 We now check all entries, but discard executable images. */ |
| 268 for (i = 0; i < info->all_image.count; i++) |
231 { | 269 { |
232 CORE_ADDR info = dyld_all_image.info + i * image_info_size; | 270 CORE_ADDR iinfo = info->all_image.info + i * image_info_size; |
233 char buf[image_info_size]; | 271 char buf[image_info_size]; |
234 CORE_ADDR load_addr; | 272 CORE_ADDR load_addr; |
235 CORE_ADDR path_addr; | 273 CORE_ADDR path_addr; |
| 274 struct mach_o_header_external hdr; |
| 275 unsigned long hdr_val; |
236 char *file_path; | 276 char *file_path; |
237 int errcode; | 277 int errcode; |
238 struct darwin_so_list *dnew; | 278 struct darwin_so_list *dnew; |
239 struct so_list *new; | 279 struct so_list *new; |
240 struct cleanup *old_chain; | 280 struct cleanup *old_chain; |
241 | 281 |
242 /* Read image info from inferior. */ | 282 /* Read image info from inferior. */ |
243 if (target_read_memory (info, buf, image_info_size)) | 283 if (target_read_memory (iinfo, buf, image_info_size)) |
244 break; | 284 break; |
245 | 285 |
246 load_addr = extract_typed_address (buf, ptr_type); | 286 load_addr = extract_typed_address (buf, ptr_type); |
247 path_addr = extract_typed_address (buf + ptr_len, ptr_type); | 287 path_addr = extract_typed_address (buf + ptr_len, ptr_type); |
248 | 288 |
| 289 /* Read Mach-O header from memory. */ |
| 290 if (target_read_memory (load_addr, (char *) &hdr, sizeof (hdr) - 4)) |
| 291 break; |
| 292 /* Discard wrong magic numbers. Shouldn't happen. */ |
| 293 hdr_val = extract_unsigned_integer |
| 294 (hdr.magic, sizeof (hdr.magic), byte_order); |
| 295 if (hdr_val != BFD_MACH_O_MH_MAGIC && hdr_val != BFD_MACH_O_MH_MAGIC_64) |
| 296 continue; |
| 297 /* Discard executable. Should happen only once. */ |
| 298 hdr_val = extract_unsigned_integer |
| 299 (hdr.filetype, sizeof (hdr.filetype), byte_order); |
| 300 if (hdr_val == BFD_MACH_O_MH_EXECUTE) |
| 301 continue; |
| 302 |
249 target_read_string (path_addr, &file_path, | 303 target_read_string (path_addr, &file_path, |
250 SO_NAME_MAX_PATH_SIZE - 1, &errcode); | 304 SO_NAME_MAX_PATH_SIZE - 1, &errcode); |
251 if (errcode) | 305 if (errcode) |
252 break; | 306 break; |
253 | 307 |
254 /* Create and fill the new so_list element. */ | 308 /* Create and fill the new so_list element. */ |
255 dnew = XZALLOC (struct darwin_so_list); | 309 dnew = XZALLOC (struct darwin_so_list); |
256 new = &dnew->sl; | 310 new = &dnew->sl; |
257 old_chain = make_cleanup (xfree, dnew); | 311 old_chain = make_cleanup (xfree, dnew); |
258 | 312 |
(...skipping 13 matching lines...) Expand all Loading... |
272 | 326 |
273 discard_cleanups (old_chain); | 327 discard_cleanups (old_chain); |
274 } | 328 } |
275 | 329 |
276 return head; | 330 return head; |
277 } | 331 } |
278 | 332 |
279 /* Return 1 if PC lies in the dynamic symbol resolution code of the | 333 /* Return 1 if PC lies in the dynamic symbol resolution code of the |
280 run time loader. */ | 334 run time loader. */ |
281 | 335 |
282 int | 336 static int |
283 darwin_in_dynsym_resolve_code (CORE_ADDR pc) | 337 darwin_in_dynsym_resolve_code (CORE_ADDR pc) |
284 { | 338 { |
285 return 0; | 339 return 0; |
286 } | 340 } |
287 | 341 |
288 | 342 |
289 /* No special symbol handling. */ | 343 /* No special symbol handling. */ |
290 | 344 |
291 static void | 345 static void |
292 darwin_special_symbol_handling (void) | 346 darwin_special_symbol_handling (void) |
293 { | 347 { |
294 } | 348 } |
295 | 349 |
296 /* Extract dyld_all_image_addr when the process was just created, assuming the | 350 /* Extract dyld_all_image_addr when the process was just created, assuming the |
297 current PC is at the entry of the dynamic linker. */ | 351 current PC is at the entry of the dynamic linker. */ |
298 | 352 |
299 static void | 353 static void |
300 darwin_solib_get_all_image_info_addr_at_init (void) | 354 darwin_solib_get_all_image_info_addr_at_init (struct darwin_info *info) |
301 { | 355 { |
302 gdb_byte *interp_name; | 356 gdb_byte *interp_name; |
303 CORE_ADDR load_addr = 0; | 357 CORE_ADDR load_addr = 0; |
304 bfd *dyld_bfd = NULL; | 358 bfd *dyld_bfd = NULL; |
305 | 359 |
306 /* This method doesn't work with an attached process. */ | 360 /* This method doesn't work with an attached process. */ |
307 if (current_inferior ()->attach_flag) | 361 if (current_inferior ()->attach_flag) |
308 return; | 362 return; |
309 | 363 |
310 /* Find the program interpreter. */ | 364 /* Find the program interpreter. */ |
(...skipping 20 matching lines...) Expand all Loading... |
331 if (!dyld_bfd) | 385 if (!dyld_bfd) |
332 return; | 386 return; |
333 | 387 |
334 /* We find the dynamic linker's base address by examining | 388 /* We find the dynamic linker's base address by examining |
335 the current pc (which should point at the entry point for the | 389 the current pc (which should point at the entry point for the |
336 dynamic linker) and subtracting the offset of the entry point. */ | 390 dynamic linker) and subtracting the offset of the entry point. */ |
337 load_addr = (regcache_read_pc (get_current_regcache ()) | 391 load_addr = (regcache_read_pc (get_current_regcache ()) |
338 - bfd_get_start_address (dyld_bfd)); | 392 - bfd_get_start_address (dyld_bfd)); |
339 | 393 |
340 /* Now try to set a breakpoint in the dynamic linker. */ | 394 /* Now try to set a breakpoint in the dynamic linker. */ |
341 dyld_all_image_addr = | 395 info->all_image_addr = |
342 lookup_symbol_from_bfd (dyld_bfd, "_dyld_all_image_infos"); | 396 lookup_symbol_from_bfd (dyld_bfd, "_dyld_all_image_infos"); |
343 | 397 |
344 bfd_close (dyld_bfd); | 398 bfd_close (dyld_bfd); |
345 | 399 |
346 if (dyld_all_image_addr == 0) | 400 if (info->all_image_addr == 0) |
347 return; | 401 return; |
348 | 402 |
349 dyld_all_image_addr += load_addr; | 403 info->all_image_addr += load_addr; |
350 } | 404 } |
351 | 405 |
352 /* Extract dyld_all_image_addr reading it from | 406 /* Extract dyld_all_image_addr reading it from |
353 TARGET_OBJECT_DARWIN_DYLD_INFO. */ | 407 TARGET_OBJECT_DARWIN_DYLD_INFO. */ |
354 | 408 |
355 static void | 409 static void |
356 darwin_solib_read_all_image_info_addr (void) | 410 darwin_solib_read_all_image_info_addr (struct darwin_info *info) |
357 { | 411 { |
358 gdb_byte buf[8 + 8 + 4]; | 412 gdb_byte buf[8 + 8 + 4]; |
359 LONGEST len; | 413 LONGEST len; |
360 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch); | 414 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch); |
361 | 415 |
362 len = target_read (¤t_target, TARGET_OBJECT_DARWIN_DYLD_INFO, NULL, | 416 len = target_read (¤t_target, TARGET_OBJECT_DARWIN_DYLD_INFO, NULL, |
363 buf, 0, sizeof (buf)); | 417 buf, 0, sizeof (buf)); |
364 if (len != sizeof (buf)) | 418 if (len != sizeof (buf)) |
365 return; | 419 return; |
366 | 420 |
367 dyld_all_image_addr = extract_unsigned_integer (buf, 8, byte_order); | 421 info->all_image_addr = extract_unsigned_integer (buf, 8, byte_order); |
368 } | 422 } |
369 | 423 |
370 /* Shared library startup support. See documentation in solib-svr4.c. */ | 424 /* Shared library startup support. See documentation in solib-svr4.c. */ |
371 | 425 |
372 static void | 426 static void |
373 darwin_solib_create_inferior_hook (int from_tty) | 427 darwin_solib_create_inferior_hook (int from_tty) |
374 { | 428 { |
375 dyld_all_image_addr = 0; | 429 struct darwin_info *info = get_darwin_info (); |
376 | 430 |
377 darwin_solib_read_all_image_info_addr (); | 431 info->all_image_addr = 0; |
378 | 432 |
379 if (dyld_all_image_addr == 0) | 433 darwin_solib_read_all_image_info_addr (info); |
380 darwin_solib_get_all_image_info_addr_at_init (); | |
381 | 434 |
382 if (dyld_all_image_addr == 0) | 435 if (info->all_image_addr == 0) |
| 436 darwin_solib_get_all_image_info_addr_at_init (info); |
| 437 |
| 438 if (info->all_image_addr == 0) |
383 return; | 439 return; |
384 | 440 |
385 darwin_load_image_infos (); | 441 darwin_load_image_infos (info); |
386 | 442 |
387 if (darwin_dyld_version_ok ()) | 443 if (darwin_dyld_version_ok (info)) |
388 create_solib_event_breakpoint (target_gdbarch, dyld_all_image.notifier); | 444 create_solib_event_breakpoint (target_gdbarch, info->all_image.notifier); |
389 } | 445 } |
390 | 446 |
391 static void | 447 static void |
392 darwin_clear_solib (void) | 448 darwin_clear_solib (void) |
393 { | 449 { |
394 dyld_all_image_addr = 0; | 450 struct darwin_info *info = get_darwin_info (); |
395 dyld_all_image.version = 0; | 451 |
| 452 info->all_image_addr = 0; |
| 453 info->all_image.version = 0; |
396 } | 454 } |
397 | 455 |
398 static void | 456 static void |
399 darwin_free_so (struct so_list *so) | 457 darwin_free_so (struct so_list *so) |
400 { | 458 { |
401 } | 459 } |
402 | 460 |
403 /* The section table is built from bfd sections using bfd VMAs. | 461 /* The section table is built from bfd sections using bfd VMAs. |
404 Relocate these VMAs according to solib info. */ | 462 Relocate these VMAs according to solib info. */ |
405 | 463 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
446 perror_with_name (pathname); | 504 perror_with_name (pathname); |
447 | 505 |
448 /* Open bfd for shared library. */ | 506 /* Open bfd for shared library. */ |
449 abfd = solib_bfd_fopen (found_pathname, found_file); | 507 abfd = solib_bfd_fopen (found_pathname, found_file); |
450 | 508 |
451 res = bfd_mach_o_fat_extract (abfd, bfd_object, | 509 res = bfd_mach_o_fat_extract (abfd, bfd_object, |
452 gdbarch_bfd_arch_info (target_gdbarch)); | 510 gdbarch_bfd_arch_info (target_gdbarch)); |
453 if (!res) | 511 if (!res) |
454 { | 512 { |
455 bfd_close (abfd); | 513 bfd_close (abfd); |
error: old chunk mismatch |
None
OLD | NEW |