+/* Get the load address of the executable. We assume that the dyld info are
+ correct. */
+
+static CORE_ADDR
+darwin_read_exec_load_addr (struct darwin_info *info)
+{
+ struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
+ enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
+ int ptr_len = TYPE_LENGTH (ptr_type);
+ unsigned int image_info_size = ptr_len * 3;
+ int i;
+
+ /* Read infos for each solib. One of them should be the executable. */
+ for (i = 0; i < info->all_image.count; i++)
+ {
+ CORE_ADDR iinfo = info->all_image.info + i * image_info_size;
+ gdb_byte buf[image_info_size];
+ CORE_ADDR load_addr;
+ struct mach_o_header_external hdr;
+ unsigned long hdr_val;
+
+ /* Read image info from inferior. */
+ if (target_read_memory (iinfo, buf, image_info_size))
+ break;
+
+ load_addr = extract_typed_address (buf, ptr_type);
+
+ /* Read Mach-O header from memory. */
+ if (target_read_memory (load_addr, (gdb_byte *) &hdr, sizeof (hdr) - 4))
+ break;
+ /* Discard wrong magic numbers. Shouldn't happen. */
+ hdr_val = extract_unsigned_integer
+ (hdr.magic, sizeof (hdr.magic), byte_order);
+ if (hdr_val != BFD_MACH_O_MH_MAGIC && hdr_val != BFD_MACH_O_MH_MAGIC_64)
+ continue;
+ /* Check executable. */
+ hdr_val = extract_unsigned_integer
+ (hdr.filetype, sizeof (hdr.filetype), byte_order);
+ if (hdr_val == BFD_MACH_O_MH_EXECUTE)
+ return load_addr;
+ }
+
+ return 0;
+}
+