+ if (abfd->xvec->flavour != bfd_target_mach_o_flavour)
+ return FALSE;
+
+ if (bfd_mach_o_get_data (abfd) == NULL)
+ return FALSE;
+ return TRUE;
+}
+
+static INLINE bfd_boolean
+mach_o_wide_p (bfd_mach_o_header *header)
+{
+ switch (header->version)
+ {
+ case 1:
+ return FALSE;
+ case 2:
+ return TRUE;
+ default:
+ BFD_FAIL ();
+ return FALSE;
+ }
+}
+
+static INLINE bfd_boolean
+bfd_mach_o_wide_p (bfd *abfd)
+{
+ return mach_o_wide_p (&bfd_mach_o_get_data (abfd)->header);
+}
+
+/* Tables to translate well known Mach-O segment/section names to bfd
+ names. Use of canonical names (such as .text or .debug_frame) is required
+ by gdb. */
+
+/* __TEXT Segment. */
+static const mach_o_section_name_xlat text_section_names_xlat[] =
+ {
+ { ".text", "__text",
+ SEC_CODE | SEC_LOAD, BFD_MACH_O_S_REGULAR,
+ BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS, 0},
+ { ".const", "__const",
+ SEC_READONLY | SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
+ BFD_MACH_O_S_ATTR_NONE, 0},
+ { ".static_const", "__static_const",
+ SEC_READONLY | SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
+ BFD_MACH_O_S_ATTR_NONE, 0},
+ { ".cstring", "__cstring",
+ SEC_READONLY | SEC_DATA | SEC_LOAD | SEC_MERGE | SEC_STRINGS,
+ BFD_MACH_O_S_CSTRING_LITERALS,
+ BFD_MACH_O_S_ATTR_NONE, 0},
+ { ".literal4", "__literal4",
+ SEC_READONLY | SEC_DATA | SEC_LOAD, BFD_MACH_O_S_4BYTE_LITERALS,
+ BFD_MACH_O_S_ATTR_NONE, 2},
+ { ".literal8", "__literal8",
+ SEC_READONLY | SEC_DATA | SEC_LOAD, BFD_MACH_O_S_8BYTE_LITERALS,
+ BFD_MACH_O_S_ATTR_NONE, 3},
+ { ".literal16", "__literal16",
+ SEC_READONLY | SEC_DATA | SEC_LOAD, BFD_MACH_O_S_16BYTE_LITERALS,
+ BFD_MACH_O_S_ATTR_NONE, 4},
+ { ".constructor", "__constructor",
+ SEC_CODE | SEC_LOAD, BFD_MACH_O_S_REGULAR,
+ BFD_MACH_O_S_ATTR_NONE, 0},
+ { ".destructor", "__destructor",
+ SEC_CODE | SEC_LOAD, BFD_MACH_O_S_REGULAR,
+ BFD_MACH_O_S_ATTR_NONE, 0},
+ { ".eh_frame", "__eh_frame",
+ SEC_READONLY | SEC_LOAD, BFD_MACH_O_S_COALESCED,
+ BFD_MACH_O_S_ATTR_LIVE_SUPPORT
+ | BFD_MACH_O_S_ATTR_STRIP_STATIC_SYMS
+ | BFD_MACH_O_S_ATTR_NO_TOC, 3},
+ { NULL, NULL, 0, 0, 0, 0}
+ };
+
+/* __DATA Segment. */
+static const mach_o_section_name_xlat data_section_names_xlat[] =
+ {
+ { ".data", "__data",
+ SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
+ BFD_MACH_O_S_ATTR_NONE, 0},
+ { ".bss", "__bss",
+ SEC_NO_FLAGS, BFD_MACH_O_S_ZEROFILL,
+ BFD_MACH_O_S_ATTR_NONE, 0},
+ { ".const_data", "__const",
+ SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
+ BFD_MACH_O_S_ATTR_NONE, 0},
+ { ".static_data", "__static_data",
+ SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
+ BFD_MACH_O_S_ATTR_NONE, 0},
+ { ".mod_init_func", "__mod_init_func",
+ SEC_DATA | SEC_LOAD, BFD_MACH_O_S_MOD_INIT_FUNC_POINTERS,
+ BFD_MACH_O_S_ATTR_NONE, 2},
+ { ".mod_term_func", "__mod_term_func",
+ SEC_DATA | SEC_LOAD, BFD_MACH_O_S_MOD_FINI_FUNC_POINTERS,
+ BFD_MACH_O_S_ATTR_NONE, 2},
+ { ".dyld", "__dyld",
+ SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
+ BFD_MACH_O_S_ATTR_NONE, 0},
+ { ".cfstring", "__cfstring",
+ SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
+ BFD_MACH_O_S_ATTR_NONE, 2},
+ { NULL, NULL, 0, 0, 0, 0}
+ };
+
+/* __DWARF Segment. */
+static const mach_o_section_name_xlat dwarf_section_names_xlat[] =
+ {
+ { ".debug_frame", "__debug_frame",
+ SEC_DEBUGGING, BFD_MACH_O_S_REGULAR,
+ BFD_MACH_O_S_ATTR_DEBUG, 0},
+ { ".debug_info", "__debug_info",
+ SEC_DEBUGGING, BFD_MACH_O_S_REGULAR,
+ BFD_MACH_O_S_ATTR_DEBUG, 0},
+ { ".debug_abbrev", "__debug_abbrev",
+ SEC_DEBUGGING, BFD_MACH_O_S_REGULAR,
+ BFD_MACH_O_S_ATTR_DEBUG, 0},
+ { ".debug_aranges", "__debug_aranges",
+ SEC_DEBUGGING, BFD_MACH_O_S_REGULAR,
+ BFD_MACH_O_S_ATTR_DEBUG, 0},
+ { ".debug_macinfo", "__debug_macinfo",
+ SEC_DEBUGGING, BFD_MACH_O_S_REGULAR,
+ BFD_MACH_O_S_ATTR_DEBUG, 0},
+ { ".debug_line", "__debug_line",
+ SEC_DEBUGGING, BFD_MACH_O_S_REGULAR,
+ BFD_MACH_O_S_ATTR_DEBUG, 0},
+ { ".debug_loc", "__debug_loc",
+ SEC_DEBUGGING, BFD_MACH_O_S_REGULAR,
+ BFD_MACH_O_S_ATTR_DEBUG, 0},
+ { ".debug_pubnames", "__debug_pubnames",
+ SEC_DEBUGGING, BFD_MACH_O_S_REGULAR,
+ BFD_MACH_O_S_ATTR_DEBUG, 0},
+ { ".debug_pubtypes", "__debug_pubtypes",
+ SEC_DEBUGGING, BFD_MACH_O_S_REGULAR,
+ BFD_MACH_O_S_ATTR_DEBUG, 0},
+ { ".debug_str", "__debug_str",
+ SEC_DEBUGGING, BFD_MACH_O_S_REGULAR,
+ BFD_MACH_O_S_ATTR_DEBUG, 0},
+ { ".debug_ranges", "__debug_ranges",
+ SEC_DEBUGGING, BFD_MACH_O_S_REGULAR,
+ BFD_MACH_O_S_ATTR_DEBUG, 0},
+ { ".debug_macro", "__debug_macro",
+ SEC_DEBUGGING, BFD_MACH_O_S_REGULAR,
+ BFD_MACH_O_S_ATTR_DEBUG, 0},
+ { NULL, NULL, 0, 0, 0, 0}
+ };
+
+/* __OBJC Segment. */
+static const mach_o_section_name_xlat objc_section_names_xlat[] =
+ {
+ { ".objc_class", "__class",
+ SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
+ BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
+ { ".objc_meta_class", "__meta_class",
+ SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
+ BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
+ { ".objc_cat_cls_meth", "__cat_cls_meth",
+ SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
+ BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
+ { ".objc_cat_inst_meth", "__cat_inst_meth",
+ SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
+ BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
+ { ".objc_protocol", "__protocol",
+ SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
+ BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
+ { ".objc_string_object", "__string_object",
+ SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
+ BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
+ { ".objc_cls_meth", "__cls_meth",
+ SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
+ BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
+ { ".objc_inst_meth", "__inst_meth",
+ SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
+ BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
+ { ".objc_cls_refs", "__cls_refs",
+ SEC_DATA | SEC_LOAD, BFD_MACH_O_S_LITERAL_POINTERS,
+ BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
+ { ".objc_message_refs", "__message_refs",
+ SEC_DATA | SEC_LOAD, BFD_MACH_O_S_LITERAL_POINTERS,
+ BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
+ { ".objc_symbols", "__symbols",
+ SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
+ BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
+ { ".objc_category", "__category",
+ SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
+ BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
+ { ".objc_class_vars", "__class_vars",
+ SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
+ BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
+ { ".objc_instance_vars", "__instance_vars",
+ SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
+ BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
+ { ".objc_module_info", "__module_info",
+ SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
+ BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
+ { ".objc_selector_strs", "__selector_strs",
+ SEC_DATA | SEC_LOAD, BFD_MACH_O_S_CSTRING_LITERALS,
+ BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
+ { ".objc_image_info", "__image_info",
+ SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
+ BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
+ { ".objc_selector_fixup", "__sel_fixup",
+ SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
+ BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
+ /* Objc V1 */
+ { ".objc1_class_ext", "__class_ext",
+ SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
+ BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
+ { ".objc1_property_list", "__property",
+ SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
+ BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
+ { ".objc1_protocol_ext", "__protocol_ext",
+ SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
+ BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
+ { NULL, NULL, 0, 0, 0, 0}
+ };
+
+static const mach_o_segment_name_xlat segsec_names_xlat[] =
+ {
+ { "__TEXT", text_section_names_xlat },
+ { "__DATA", data_section_names_xlat },
+ { "__DWARF", dwarf_section_names_xlat },
+ { "__OBJC", objc_section_names_xlat },
+ { NULL, NULL }
+ };
+
+/* For both cases bfd-name => mach-o name and vice versa, the specific target
+ is checked before the generic. This allows a target (e.g. ppc for cstring)
+ to override the generic definition with a more specific one. */
+
+/* Fetch the translation from a Mach-O section designation (segment, section)
+ as a bfd short name, if one exists. Otherwise return NULL.
+
+ Allow the segment and section names to be unterminated 16 byte arrays. */
+
+const mach_o_section_name_xlat *
+bfd_mach_o_section_data_for_mach_sect (bfd *abfd, const char *segname,
+ const char *sectname)
+{
+ const struct mach_o_segment_name_xlat *seg;
+ const mach_o_section_name_xlat *sec;
+ bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
+
+ /* First try any target-specific translations defined... */
+ if (bed->segsec_names_xlat)
+ for (seg = bed->segsec_names_xlat; seg->segname; seg++)
+ if (strncmp (seg->segname, segname, BFD_MACH_O_SEGNAME_SIZE) == 0)
+ for (sec = seg->sections; sec->mach_o_name; sec++)
+ if (strncmp (sec->mach_o_name, sectname,
+ BFD_MACH_O_SECTNAME_SIZE) == 0)
+ return sec;
+
+ /* ... and then the Mach-O generic ones. */
+ for (seg = segsec_names_xlat; seg->segname; seg++)
+ if (strncmp (seg->segname, segname, BFD_MACH_O_SEGNAME_SIZE) == 0)
+ for (sec = seg->sections; sec->mach_o_name; sec++)
+ if (strncmp (sec->mach_o_name, sectname,
+ BFD_MACH_O_SECTNAME_SIZE) == 0)
+ return sec;
+
+ return NULL;
+}
+
+/* If the bfd_name for this section is a 'canonical' form for which we
+ know the Mach-O data, return the segment name and the data for the
+ Mach-O equivalent. Otherwise return NULL. */
+
+const mach_o_section_name_xlat *
+bfd_mach_o_section_data_for_bfd_name (bfd *abfd, const char *bfd_name,
+ const char **segname)
+{
+ const struct mach_o_segment_name_xlat *seg;
+ const mach_o_section_name_xlat *sec;
+ bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
+ *segname = NULL;
+
+ if (bfd_name[0] != '.')
+ return NULL;
+
+ /* First try any target-specific translations defined... */
+ if (bed->segsec_names_xlat)
+ for (seg = bed->segsec_names_xlat; seg->segname; seg++)
+ for (sec = seg->sections; sec->bfd_name; sec++)
+ if (strcmp (bfd_name, sec->bfd_name) == 0)
+ {
+ *segname = seg->segname;
+ return sec;
+ }
+
+ /* ... and then the Mach-O generic ones. */
+ for (seg = segsec_names_xlat; seg->segname; seg++)
+ for (sec = seg->sections; sec->bfd_name; sec++)
+ if (strcmp (bfd_name, sec->bfd_name) == 0)
+ {
+ *segname = seg->segname;
+ return sec;
+ }
+
+ return NULL;
+}
+
+/* Convert Mach-O section name to BFD.
+
+ Try to use standard/canonical names, for which we have tables including
+ default flag settings - which are returned. Otherwise forge a new name
+ in the form "<segmentname>.<sectionname>" this will be prefixed with
+ LC_SEGMENT. if the segment name does not begin with an underscore.
+
+ SEGNAME and SECTNAME are 16 byte arrays (they do not need to be NUL-
+ terminated if the name length is exactly 16 bytes - but must be if the name
+ length is less than 16 characters). */
+
+void
+bfd_mach_o_convert_section_name_to_bfd (bfd *abfd, const char *segname,
+ const char *secname, const char **name,
+ flagword *flags)
+{
+ const mach_o_section_name_xlat *xlat;
+ char *res;
+ unsigned int len;
+ const char *pfx = "";
+
+ *name = NULL;
+ *flags = SEC_NO_FLAGS;
+
+ /* First search for a canonical name...
+ xlat will be non-null if there is an entry for segname, secname. */
+ xlat = bfd_mach_o_section_data_for_mach_sect (abfd, segname, secname);
+ if (xlat)
+ {
+ len = strlen (xlat->bfd_name);
+ res = bfd_alloc (abfd, len+1);
+ if (res == NULL)
+ return;
+ memcpy (res, xlat->bfd_name, len+1);
+ *name = res;
+ *flags = xlat->bfd_flags;
+ return;
+ }
+
+ /* ... else we make up a bfd name from the segment concatenated with the
+ section. */
+
+ len = 16 + 1 + 16 + 1;
+
+ /* Put "LC_SEGMENT." prefix if the segment name is weird (ie doesn't start
+ with an underscore. */
+ if (segname[0] != '_')
+ {
+ static const char seg_pfx[] = "LC_SEGMENT.";
+
+ pfx = seg_pfx;
+ len += sizeof (seg_pfx) - 1;
+ }
+
+ res = bfd_alloc (abfd, len);
+ if (res == NULL)
+ return;
+ snprintf (res, len, "%s%.16s.%.16s", pfx, segname, secname);
+ *name = res;
+}
+
+/* Convert a bfd section name to a Mach-O segment + section name.
+
+ If the name is a canonical one for which we have a Darwin match
+ return the translation table - which contains defaults for flags,
+ type, attribute and default alignment data.
+
+ Otherwise, expand the bfd_name (assumed to be in the form
+ "[LC_SEGMENT.]<segmentname>.<sectionname>") and return NULL. */
+
+static const mach_o_section_name_xlat *
+bfd_mach_o_convert_section_name_to_mach_o (bfd *abfd ATTRIBUTE_UNUSED,
+ asection *sect,
+ bfd_mach_o_section *section)
+{
+ const mach_o_section_name_xlat *xlat;
+ const char *name = bfd_get_section_name (abfd, sect);
+ const char *segname;
+ const char *dot;
+ unsigned int len;
+ unsigned int seglen;
+ unsigned int seclen;
+
+ memset (section->segname, 0, BFD_MACH_O_SEGNAME_SIZE + 1);
+ memset (section->sectname, 0, BFD_MACH_O_SECTNAME_SIZE + 1);
+
+ /* See if is a canonical name ... */
+ xlat = bfd_mach_o_section_data_for_bfd_name (abfd, name, &segname);
+ if (xlat)
+ {
+ strcpy (section->segname, segname);
+ strcpy (section->sectname, xlat->mach_o_name);
+ return xlat;
+ }
+
+ /* .. else we convert our constructed one back to Mach-O.
+ Strip LC_SEGMENT. prefix, if present. */
+ if (strncmp (name, "LC_SEGMENT.", 11) == 0)
+ name += 11;
+
+ /* Find a dot. */
+ dot = strchr (name, '.');
+ len = strlen (name);
+
+ /* Try to split name into segment and section names. */
+ if (dot && dot != name)
+ {
+ seglen = dot - name;
+ seclen = len - (dot + 1 - name);
+
+ if (seglen < 16 && seclen < 16)
+ {
+ memcpy (section->segname, name, seglen);
+ section->segname[seglen] = 0;
+ memcpy (section->sectname, dot + 1, seclen);
+ section->sectname[seclen] = 0;
+ return NULL;
+ }
+ }
+
+ /* The segment and section names are both missing - don't make them
+ into dots. */
+ if (dot && dot == name)
+ return NULL;
+
+ /* Just duplicate the name into both segment and section. */
+ if (len > 16)
+ len = 16;
+ memcpy (section->segname, name, len);
+ section->segname[len] = 0;
+ memcpy (section->sectname, name, len);
+ section->sectname[len] = 0;
+ return NULL;
+}