/* Create a dummy BFD. */ bfd * plugin_get_ir_dummy_bfd (const char *name, bfd *srctemplate) { bfd *abfd; bfd_use_reserved_id = 1; abfd = bfd_create (concat (name, IRONLY_SUFFIX, (const char *) NULL), srctemplate); if (abfd != NULL) { abfd->flags |= BFD_LINKER_CREATED | BFD_PLUGIN; bfd_set_arch_info (abfd, bfd_get_arch_info (srctemplate)); bfd_set_gp_size (abfd, bfd_get_gp_size (srctemplate)); if (bfd_make_writable (abfd) && bfd_copy_private_bfd_data (srctemplate, abfd)) { flagword flags; /* Create section to own the symbols. */ flags = (SEC_CODE | SEC_HAS_CONTENTS | SEC_READONLY | SEC_ALLOC | SEC_LOAD | SEC_KEEP | SEC_EXCLUDE); if (bfd_make_section_anyway_with_flags (abfd, ".text", flags)) return abfd; } } einfo (_("could not create dummy IR bfd: %F%E\n")); return NULL; }
static enum gdb_osabi arm_mach_o_osabi_sniffer_use_dyld_hint (bfd *abfd) { if (osabi_seen_in_attached_dyld == GDB_OSABI_UNKNOWN) return GDB_OSABI_UNKNOWN; bfd *nbfd = NULL; for (;;) { bfd_arch_info_type *arch_info; nbfd = bfd_openr_next_archived_file (abfd, nbfd); if (nbfd == NULL) break; if (!bfd_check_format (nbfd, bfd_object)) continue; arch_info = bfd_get_arch_info (nbfd); if (arch_info->arch == bfd_arch_arm) { if (arch_info->mach == bfd_mach_arm_4T && osabi_seen_in_attached_dyld == GDB_OSABI_DARWIN) return GDB_OSABI_DARWIN; if (arch_info->mach == bfd_mach_arm_6 && osabi_seen_in_attached_dyld == GDB_OSABI_DARWINV6) return GDB_OSABI_DARWINV6; } } return GDB_OSABI_UNKNOWN; }
void gdbarch_info_fill (struct gdbarch_info *info) { /* "(gdb) set architecture ...". */ if (info->bfd_arch_info == NULL && target_architecture_user) info->bfd_arch_info = target_architecture_user; /* From the file. */ if (info->bfd_arch_info == NULL && info->abfd != NULL && bfd_get_arch (info->abfd) != bfd_arch_unknown && bfd_get_arch (info->abfd) != bfd_arch_obscure) info->bfd_arch_info = bfd_get_arch_info (info->abfd); /* From the target. */ if (info->target_desc != NULL) info->bfd_arch_info = choose_architecture_for_target (info->target_desc, info->bfd_arch_info); /* From the default. */ if (info->bfd_arch_info == NULL) info->bfd_arch_info = default_bfd_arch; /* "(gdb) set byte-order ...". */ if (info->byte_order == BFD_ENDIAN_UNKNOWN && target_byte_order_user != BFD_ENDIAN_UNKNOWN) info->byte_order = target_byte_order_user; /* From the INFO struct. */ if (info->byte_order == BFD_ENDIAN_UNKNOWN && info->abfd != NULL) info->byte_order = (bfd_big_endian (info->abfd) ? BFD_ENDIAN_BIG : bfd_little_endian (info->abfd) ? BFD_ENDIAN_LITTLE : BFD_ENDIAN_UNKNOWN); /* From the default. */ if (info->byte_order == BFD_ENDIAN_UNKNOWN) info->byte_order = default_byte_order; info->byte_order_for_code = info->byte_order; /* "(gdb) set osabi ...". Handled by gdbarch_lookup_osabi. */ /* From the manual override, or from file. */ if (info->osabi == GDB_OSABI_UNINITIALIZED) info->osabi = gdbarch_lookup_osabi (info->abfd); /* From the target. */ if (info->osabi == GDB_OSABI_UNKNOWN && info->target_desc != NULL) info->osabi = tdesc_osabi (info->target_desc); /* From the configured default. */ #ifdef GDB_OSABI_DEFAULT if (info->osabi == GDB_OSABI_UNKNOWN) info->osabi = GDB_OSABI_DEFAULT; #endif /* Must have at least filled in the architecture. */ gdb_assert (info->bfd_arch_info != NULL); }
static void set_architecture_from_file (bfd *abfd) { const struct bfd_arch_info *wanted = bfd_get_arch_info (abfd); if (GDB_MULTI_ARCH) internal_error (__FILE__, __LINE__, "set_architecture_from_file: not multi-arched"); if (target_architecture_auto) { set_arch (wanted, set_arch_auto); } else if (wanted != target_architecture) { warning ("%s architecture file may be incompatible with %s target.", wanted->printable_name, target_architecture->printable_name); } }
void replace_hello(const char *input_path, const char *output_path) { bfd_init(); bfd *ibfd = bfd_openr(input_path, NULL); if (ibfd == NULL) errx(1, bfd_errmsg(bfd_get_error())); if (!bfd_check_format(ibfd, bfd_object)) { bfd_close_all_done(ibfd); errx(1, "Input file is not a valid object file."); } bfd *obfd = bfd_openw(output_path, bfd_get_target(ibfd)); if (obfd == NULL) { bfd_close_all_done(ibfd); errx(1, bfd_errmsg(bfd_get_error())); } if (!bfd_set_format(obfd, bfd_get_format(ibfd))) { bfd_close_all_done(ibfd); bfd_close_all_done(obfd); errx(1, "Setting obfd format failed: %s\n", bfd_errmsg(bfd_get_error())); } bfd_set_arch_info(obfd, bfd_get_arch_info(ibfd)); // Create sections for .data asection *section = bfd_get_section_by_name(ibfd, ".data"); while (section != NULL) { if (section->flags & SEC_HAS_CONTENTS) { char *section_contents = (char *)malloc(section->size); bfd_get_section_contents(ibfd, section, section_contents, 0, section->size); char *hello_pos = (char *)memmem(section_contents, section->size, "hello", 5); if (hello_pos != NULL) { if (bfd_make_section_anyway_with_flags(obfd, ".data", section->flags) == NULL) { free(section_contents); bfd_close_all_done(ibfd); bfd_close_all_done(obfd); errx(1, bfd_errmsg(bfd_get_error())); } } free(section_contents); } section = bfd_get_next_section_by_name(ibfd, section); } asection *comment_section = bfd_make_section_anyway_with_flags(obfd, ".comment.my_objcopy", SEC_HAS_CONTENTS); if (comment_section == NULL) { bfd_close_all_done(ibfd); bfd_close_all_done(obfd); errx(1, bfd_errmsg(bfd_get_error())); } if (!bfd_set_section_size(obfd, comment_section, 3)) { bfd_close_all_done(ibfd); bfd_close_all_done(obfd); errx(1, bfd_errmsg(bfd_get_error())); } if (!bfd_set_section_contents(obfd, comment_section, "moo", 0, 3)) { bfd_close_all_done(ibfd); bfd_close_all_done(obfd); errx(1, bfd_errmsg(bfd_get_error())); } // section = bfd_get_section_by_name(ibfd, ".data"); // asection *osection = bfd_get_section_by_name(obfd, ".data"); // if (osection == NULL) { // bfd_close_all_done(ibfd); // bfd_close_all_done(obfd); // errx(1, bfd_errmsg(bfd_get_error())); // } // while (section != NULL) { // if (section->flags & SEC_HAS_CONTENTS) { // char *section_contents = (char *)malloc(section->size); // bfd_get_section_contents(ibfd, section, section_contents, 0, section->size); // // char *hello_pos = (char *)memmem(section_contents, section->size, "hello", 5); // if (hello_pos != NULL) { // hello_pos[1] = 'o'; // hello_pos[4] = 'a'; // if (bfd_set_section_contents(obfd, osection, section_contents, 0, section->size)) { // free(section_contents); // bfd_close_all_done(ibfd); // bfd_close_all_done(obfd); // errx(1, bfd_errmsg(bfd_get_error())); // } // } // // free(section_contents); // osection = bfd_get_next_section_by_name(obfd, osection); // } // // section = bfd_get_next_section_by_name(ibfd, section); // } if (!bfd_close(obfd)) errx(1, "Closing obfd failed: %s\n", bfd_errmsg(bfd_get_error())); if (!bfd_close(ibfd)) errx(1, "Closing ibfd failed: %s\n", bfd_errmsg(bfd_get_error())); }
static enum gdb_osabi arm_mach_o_osabi_sniffer (bfd *abfd) { enum gdb_osabi ret; /* The way loading works on Darwin is that for FAT files, the fork corresponding to the host system will be the one used regardless of what the type of the main executable was. So the osabi has to be determined solely by the type of the host system. */ if (strcmp (bfd_get_target (abfd), "mach-o-le") == 0) { bfd_arch_info_type *arch_info = bfd_get_arch_info (abfd); if (arch_info->arch == bfd_arch_arm) { if (arch_info->mach == bfd_mach_arm_4T) return GDB_OSABI_DARWIN; else if (arch_info->mach == bfd_mach_arm_6) return GDB_OSABI_DARWINV6; } } /* However, if we are running a cross gdb, we won't know what the target architecture is. FIXME - going to have to determine this somehow. For now, fall back to looking at the binary. */ ret = arm_mach_o_osabi_sniffer_use_dyld_hint (abfd); if (ret == GDB_OSABI_DARWINV6 || ret == GDB_OSABI_DARWIN) return ret; if (bfd_check_format (abfd, bfd_archive)) { enum gdb_osabi best = GDB_OSABI_UNKNOWN; enum gdb_osabi cur = GDB_OSABI_UNKNOWN; bfd *nbfd = NULL; for (;;) { nbfd = bfd_openr_next_archived_file (abfd, nbfd); if (nbfd == NULL) break; if (!bfd_check_format (nbfd, bfd_object)) continue; cur = arm_mach_o_osabi_sniffer (nbfd); if (cur == GDB_OSABI_DARWINV6 && best != GDB_OSABI_DARWINV6 && arm_mach_o_query_v6 ()) best = cur; if (cur == GDB_OSABI_DARWIN && best != GDB_OSABI_DARWINV6 && best != GDB_OSABI_DARWIN) best = cur; } return best; } if (!bfd_check_format (abfd, bfd_object)) return GDB_OSABI_UNKNOWN; return GDB_OSABI_UNKNOWN; }