blob: 93224aaed00f95d78ca7161fe8c39e22b9c52581 [file] [log] [blame]
/* Mach-O support for BFD.
Copyright (C) 1999-2024 Free Software Foundation, Inc.
This file is part of BFD, the Binary File Descriptor library.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
MA 02110-1301, USA. */
#include "sysdep.h"
#include <limits.h>
#include "bfd.h"
#include "libbfd.h"
#include "libiberty.h"
#include "mach-o.h"
#include "aout/stab_gnu.h"
#include "mach-o/reloc.h"
#include "mach-o/external.h"
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#define bfd_mach_o_object_p bfd_mach_o_gen_object_p
#define bfd_mach_o_core_p bfd_mach_o_gen_core_p
#define bfd_mach_o_mkobject bfd_mach_o_gen_mkobject
#define FILE_ALIGN(off, algn) \
(((off) + ((ufile_ptr) 1 << (algn)) - 1) & ((ufile_ptr) -1 << (algn)))
static bool
bfd_mach_o_read_dyld_content (bfd *abfd, bfd_mach_o_dyld_info_command *cmd);
unsigned int
bfd_mach_o_version (bfd *abfd)
{
bfd_mach_o_data_struct *mdata = NULL;
BFD_ASSERT (bfd_mach_o_valid (abfd));
mdata = bfd_mach_o_get_data (abfd);
return mdata->header.version;
}
bool
bfd_mach_o_valid (bfd *abfd)
{
if (abfd == NULL || abfd->xvec == NULL)
return false;
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 bool
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 bool
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_DATA | 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, 2},
{ 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},
{ ".debug_gdb_scripts", "__debug_gdb_scri",
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 }
};
static const char dsym_subdir[] = ".dSYM/Contents/Resources/DWARF";
/* 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;
size_t 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_section_name (sect);
const char *segname;
const char *dot;
size_t len;
size_t seglen;
size_t 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 <= BFD_MACH_O_SEGNAME_SIZE
&& seclen <= BFD_MACH_O_SECTNAME_SIZE)
{
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;
}
/* Return the size of an entry for section SEC.
Must be called only for symbol pointer section and symbol stubs
sections. */
unsigned int
bfd_mach_o_section_get_entry_size (bfd *abfd, bfd_mach_o_section *sec)
{
switch (sec->flags & BFD_MACH_O_SECTION_TYPE_MASK)
{
case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS:
case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS:
return bfd_mach_o_wide_p (abfd) ? 8 : 4;
case BFD_MACH_O_S_SYMBOL_STUBS:
return sec->reserved2;
default:
BFD_FAIL ();
return 0;
}
}
/* Return the number of indirect symbols for a section.
Must be called only for symbol pointer section and symbol stubs
sections. */
unsigned int
bfd_mach_o_section_get_nbr_indirect (bfd *abfd, bfd_mach_o_section *sec)
{
unsigned int elsz;
/* FIXME: This array is set by the assembler but does not seem to be
set anywhere for objcopy. Since bfd_mach_o_build_dysymtab will
not fill in output bfd_mach_o_dysymtab_command indirect_syms when
this array is NULL we may as well return zero for the size.
This is enough to stop objcopy allocating huge amounts of memory
for indirect symbols in fuzzed object files. */
if (sec->indirect_syms == NULL)
return 0;
elsz = bfd_mach_o_section_get_entry_size (abfd, sec);
if (elsz == 0)
return 0;
else
return sec->size / elsz;
}
/* Append command CMD to ABFD. Note that header.ncmds is not updated. */
static void
bfd_mach_o_append_command (bfd *abfd, bfd_mach_o_load_command *cmd)
{
bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
if (mdata->last_command != NULL)
mdata->last_command->next = cmd;
else
mdata->first_command = cmd;
mdata->last_command = cmd;
cmd->next = NULL;
}
/* Copy any private info we understand from the input symbol
to the output symbol. */
bool
bfd_mach_o_bfd_copy_private_symbol_data (bfd *ibfd ATTRIBUTE_UNUSED,
asymbol *isymbol,
bfd *obfd ATTRIBUTE_UNUSED,
asymbol *osymbol)
{
bfd_mach_o_asymbol *os, *is;
os = (bfd_mach_o_asymbol *)osymbol;
is = (bfd_mach_o_asymbol *)isymbol;
os->n_type = is->n_type;
os->n_sect = is->n_sect;
os->n_desc = is->n_desc;
os->symbol.udata.i = is->symbol.udata.i;
return true;
}
/* Copy any private info we understand from the input section
to the output section. */
bool
bfd_mach_o_bfd_copy_private_section_data (bfd *ibfd, asection *isection,
bfd *obfd, asection *osection)
{
bfd_mach_o_section *os = bfd_mach_o_get_mach_o_section (osection);
bfd_mach_o_section *is = bfd_mach_o_get_mach_o_section (isection);
if (ibfd->xvec->flavour != bfd_target_mach_o_flavour
|| obfd->xvec->flavour != bfd_target_mach_o_flavour)
return true;
BFD_ASSERT (is != NULL && os != NULL);
os->flags = is->flags;
os->reserved1 = is->reserved1;
os->reserved2 = is->reserved2;
os->reserved3 = is->reserved3;
return true;
}
static const char *
cputype (unsigned long value)
{
switch (value)
{
case BFD_MACH_O_CPU_TYPE_VAX: return "VAX";
case BFD_MACH_O_CPU_TYPE_MC680x0: return "MC68k";
case BFD_MACH_O_CPU_TYPE_I386: return "I386";
case BFD_MACH_O_CPU_TYPE_MIPS: return "MIPS";
case BFD_MACH_O_CPU_TYPE_MC98000: return "MC98k";
case BFD_MACH_O_CPU_TYPE_HPPA: return "HPPA";
case BFD_MACH_O_CPU_TYPE_ARM: return "ARM";
case BFD_MACH_O_CPU_TYPE_MC88000: return "MC88K";
case BFD_MACH_O_CPU_TYPE_SPARC: return "SPARC";
case BFD_MACH_O_CPU_TYPE_I860: return "I860";
case BFD_MACH_O_CPU_TYPE_ALPHA: return "ALPHA";
case BFD_MACH_O_CPU_TYPE_POWERPC: return "PPC";
case BFD_MACH_O_CPU_TYPE_POWERPC_64: return "PPC64";
case BFD_MACH_O_CPU_TYPE_X86_64: return "X86_64";
case BFD_MACH_O_CPU_TYPE_ARM64: return "ARM64";
default: return _("<unknown>");
}
}
static const char *
cpusubtype (unsigned long cpu_type, unsigned long cpu_subtype, char *buffer)
{
buffer[0] = 0;
switch (cpu_subtype & BFD_MACH_O_CPU_SUBTYPE_MASK)
{
case 0:
break;
case BFD_MACH_O_CPU_SUBTYPE_LIB64:
sprintf (buffer, " (LIB64)"); break;
default:
sprintf (buffer, _("<unknown mask flags>")); break;
}
cpu_subtype &= ~ BFD_MACH_O_CPU_SUBTYPE_MASK;
switch (cpu_type)
{
case BFD_MACH_O_CPU_TYPE_X86_64:
case BFD_MACH_O_CPU_TYPE_I386:
switch (cpu_subtype)
{
case BFD_MACH_O_CPU_SUBTYPE_X86_ALL:
return strcat (buffer, " (X86_ALL)");
default:
break;
}
break;
case BFD_MACH_O_CPU_TYPE_ARM:
switch (cpu_subtype)
{
case BFD_MACH_O_CPU_SUBTYPE_ARM_ALL:
return strcat (buffer, " (ARM_ALL)");
case BFD_MACH_O_CPU_SUBTYPE_ARM_V4T:
return strcat (buffer, " (ARM_V4T)");
case BFD_MACH_O_CPU_SUBTYPE_ARM_V6:
return strcat (buffer, " (ARM_V6)");
case BFD_MACH_O_CPU_SUBTYPE_ARM_V5TEJ:
return strcat (buffer, " (ARM_V5TEJ)");
case BFD_MACH_O_CPU_SUBTYPE_ARM_XSCALE:
return strcat (buffer, " (ARM_XSCALE)");
case BFD_MACH_O_CPU_SUBTYPE_ARM_V7:
return strcat (buffer, " (ARM_V7)");
default:
break;
}
break;
case BFD_MACH_O_CPU_TYPE_ARM64:
switch (cpu_subtype)
{
case BFD_MACH_O_CPU_SUBTYPE_ARM64_ALL:
return strcat (buffer, " (ARM64_ALL)");
case BFD_MACH_O_CPU_SUBTYPE_ARM64_V8:
return strcat (buffer, " (ARM64_V8)");
default:
break;
}
break;
default:
break;
}
if (cpu_subtype != 0)
return strcat (buffer, _(" (<unknown>)"));
return buffer;
}
bool
bfd_mach_o_bfd_print_private_bfd_data (bfd *abfd, void *ptr)
{
FILE * file = (FILE *) ptr;
bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
char buff[128];
fprintf (file, _(" MACH-O header:\n"));
fprintf (file, _(" magic: %#lx\n"), (long) mdata->header.magic);
fprintf (file, _(" cputype: %#lx (%s)\n"), (long) mdata->header.cputype,
cputype (mdata->header.cputype));
fprintf (file, _(" cpusubtype: %#lx%s\n"), (long) mdata->header.cpusubtype,
cpusubtype (mdata->header.cputype, mdata->header.cpusubtype, buff));
fprintf (file, _(" filetype: %#lx\n"), (long) mdata->header.filetype);
fprintf (file, _(" ncmds: %#lx\n"), (long) mdata->header.ncmds);
fprintf (file, _(" sizeocmds: %#lx\n"), (long) mdata->header.sizeofcmds);
fprintf (file, _(" flags: %#lx\n"), (long) mdata->header.flags);
fprintf (file, _(" version: %x\n"), mdata->header.version);
return true;
}
/* Copy any private info we understand from the input bfd
to the output bfd. */
bool
bfd_mach_o_bfd_copy_private_header_data (bfd *ibfd, bfd *obfd)
{
bfd_mach_o_data_struct *imdata;
bfd_mach_o_data_struct *omdata;
bfd_mach_o_load_command *icmd;
if (bfd_get_flavour (ibfd) != bfd_target_mach_o_flavour
|| bfd_get_flavour (obfd) != bfd_target_mach_o_flavour)
return true;
BFD_ASSERT (bfd_mach_o_valid (ibfd));
BFD_ASSERT (bfd_mach_o_valid (obfd));
imdata = bfd_mach_o_get_data (ibfd);
omdata = bfd_mach_o_get_data (obfd);
/* Copy header flags. */
omdata->header.flags = imdata->header.flags;
/* PR 23299. Copy the cputype. */
if (imdata->header.cputype != omdata->header.cputype)
{
if (omdata->header.cputype == 0)
omdata->header.cputype = imdata->header.cputype;
else if (imdata->header.cputype != 0)
/* Urg - what has happened ? */
_bfd_error_handler (_("incompatible cputypes in mach-o files: %ld vs %ld"),
(long) imdata->header.cputype,
(long) omdata->header.cputype);
}
/* Copy the cpusubtype. */
omdata->header.cpusubtype = imdata->header.cpusubtype;
/* Copy commands. */
for (icmd = imdata->first_command; icmd != NULL; icmd = icmd->next)
{
bfd_mach_o_load_command *ocmd;
switch (icmd->type)
{
case BFD_MACH_O_LC_LOAD_DYLIB:
case BFD_MACH_O_LC_LOAD_DYLINKER:
case BFD_MACH_O_LC_DYLD_INFO:
/* Command is copied. */
ocmd = bfd_alloc (obfd, sizeof (bfd_mach_o_load_command));
if (ocmd == NULL)
return false;
/* Copy common fields. */
ocmd->type = icmd->type;
ocmd->type_required = icmd->type_required;
ocmd->offset = 0;
ocmd->len = icmd->len;
break;
default:
/* Command is not copied. */
continue;
break;
}
switch (icmd->type)
{
case BFD_MACH_O_LC_LOAD_DYLIB:
{
bfd_mach_o_dylib_command *idy = &icmd->command.dylib;
bfd_mach_o_dylib_command *ody = &ocmd->command.dylib;
ody->name_offset = idy->name_offset;
ody->timestamp = idy->timestamp;
ody->current_version = idy->current_version;
ody->compatibility_version = idy->compatibility_version;
ody->name_str = idy->name_str;
}
break;
case BFD_MACH_O_LC_LOAD_DYLINKER:
{
bfd_mach_o_dylinker_command *idy = &icmd->command.dylinker;
bfd_mach_o_dylinker_command *ody = &ocmd->command.dylinker;
ody->name_offset = idy->name_offset;
ody->name_str = idy->name_str;
}
break;
case BFD_MACH_O_LC_DYLD_INFO:
{
bfd_mach_o_dyld_info_command *idy = &icmd->command.dyld_info;
bfd_mach_o_dyld_info_command *ody = &ocmd->command.dyld_info;
if (bfd_mach_o_read_dyld_content (ibfd, idy))
{
ody->rebase_size = idy->rebase_size;
ody->rebase_content = idy->rebase_content;
ody->bind_size = idy->bind_size;
ody->bind_content = idy->bind_content;
ody->weak_bind_size = idy->weak_bind_size;
ody->weak_bind_content = idy->weak_bind_content;
ody->lazy_bind_size = idy->lazy_bind_size;
ody->lazy_bind_content = idy->lazy_bind_content;
ody->export_size = idy->export_size;
ody->export_content = idy->export_content;
}
/* PR 17512L: file: 730e492d. */
else
{
ody->rebase_size =
ody->bind_size =
ody->weak_bind_size =
ody->lazy_bind_size =
ody->export_size = 0;
ody->rebase_content =
ody->bind_content =
ody->weak_bind_content =
ody->lazy_bind_content =
ody->export_content = NULL;
}
}
break;
default:
/* That command should be handled. */
abort ();
}
/* Insert command. */
bfd_mach_o_append_command (obfd, ocmd);
}
return true;
}
/* This allows us to set up to 32 bits of flags (unless we invent some
fiendish scheme to subdivide). For now, we'll just set the file flags
without error checking - just overwrite. */
bool
bfd_mach_o_bfd_set_private_flags (bfd *abfd, flagword flags)
{
bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
if (!mdata)
return false;
mdata->header.flags = flags;
return true;
}
/* Count the total number of symbols. */
static long
bfd_mach_o_count_symbols (bfd *abfd)
{
bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
if (mdata->symtab == NULL)
return 0;
return mdata->symtab->nsyms;
}
long
bfd_mach_o_get_symtab_upper_bound (bfd *abfd)
{
long nsyms = bfd_mach_o_count_symbols (abfd);
return ((nsyms + 1) * sizeof (asymbol *));
}
long
bfd_mach_o_canonicalize_symtab (bfd *abfd, asymbol **alocation)
{
bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
long nsyms = bfd_mach_o_count_symbols (abfd);
bfd_mach_o_symtab_command *sym = mdata->symtab;
unsigned long j;
if (nsyms < 0)
return nsyms;
if (nsyms == 0)
{
/* Do not try to read symbols if there are none. */
alocation[0] = NULL;
return 0;
}
if (!bfd_mach_o_read_symtab_symbols (abfd))
{
_bfd_error_handler
(_("bfd_mach_o_canonicalize_symtab: unable to load symbols"));
return -1;
}
BFD_ASSERT (sym->symbols != NULL);
for (j = 0; j < sym->nsyms; j++)
alocation[j] = &sym->symbols[j].symbol;
alocation[j] = NULL;
return nsyms;
}
/* Create synthetic symbols for indirect symbols. */
long
bfd_mach_o_get_synthetic_symtab (bfd *abfd,
long symcount ATTRIBUTE_UNUSED,
asymbol **syms ATTRIBUTE_UNUSED,
long dynsymcount ATTRIBUTE_UNUSED,
asymbol **dynsyms ATTRIBUTE_UNUSED,
asymbol **ret)
{
bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
bfd_mach_o_dysymtab_command *dysymtab = mdata->dysymtab;
bfd_mach_o_symtab_command *symtab = mdata->symtab;
asymbol *s;
char * s_start;
unsigned long count, i, j, n;
size_t size;
char *names;
const char stub [] = "$stub";
*ret = NULL;
/* Stop now if no symbols or no indirect symbols. */
if (dysymtab == NULL || dysymtab->nindirectsyms == 0
|| symtab == NULL || symtab->symbols == NULL)
return 0;
/* We need to allocate a bfd symbol for every indirect symbol and to
allocate the memory for its name. */
count = dysymtab->nindirectsyms;
size = 0;
for (j = 0; j < count; j++)
{
unsigned int isym = dysymtab->indirect_syms[j];
const char *str;
/* Some indirect symbols are anonymous. */
if (isym < symtab->nsyms
&& (str = symtab->symbols[isym].symbol.name) != NULL)
{
/* PR 17512: file: f5b8eeba. */
size += strnlen (str, symtab->strsize - (str - symtab->strtab));
size += sizeof (stub);
}
}
s_start = bfd_malloc (size + count * sizeof (asymbol));
s = *ret = (asymbol *) s_start;
if (s == NULL)
return -1;
names = (char *) (s + count);
n = 0;
for (i = 0; i < mdata->nsects; i++)
{
bfd_mach_o_section *sec = mdata->sections[i];
unsigned int first, last;
bfd_vma addr;
unsigned int entry_size;
switch (sec->flags & BFD_MACH_O_SECTION_TYPE_MASK)
{
case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS:
case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS:
case BFD_MACH_O_S_SYMBOL_STUBS:
/* Only these sections have indirect symbols. */
first = sec->reserved1;
last = first + bfd_mach_o_section_get_nbr_indirect (abfd, sec);
addr = sec->addr;
entry_size = bfd_mach_o_section_get_entry_size (abfd, sec);
/* PR 17512: file: 08e15eec. */
if (first >= count || last > count || first > last)
goto fail;
for (j = first; j < last; j++)
{
unsigned int isym = dysymtab->indirect_syms[j];
const char *str;
size_t len;
if (isym < symtab->nsyms
&& (str = symtab->symbols[isym].symbol.name) != NULL)
{
/* PR 17512: file: 04d64d9b. */
if (n >= count)
goto fail;
len = strnlen (str, symtab->strsize - (str - symtab->strtab));
/* PR 17512: file: 47dfd4d2, 18f340a4. */
if (size < len + sizeof (stub))
goto fail;
memcpy (names, str, len);
memcpy (names + len, stub, sizeof (stub));
s->name = names;
names += len + sizeof (stub);
size -= len + sizeof (stub);
s->the_bfd = symtab->symbols[isym].symbol.the_bfd;
s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
s->section = sec->bfdsection;
s->value = addr - sec->addr;
s->udata.p = NULL;
s++;
n++;
}
addr += entry_size;
}
break;
default:
break;
}
}
return n;
fail:
free (s_start);
* ret = NULL;
return -1;
}
void
bfd_mach_o_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
asymbol *symbol,
symbol_info *ret)
{
bfd_symbol_info (symbol, ret);
}
void
bfd_mach_o_print_symbol (bfd *abfd,
void * afile,
asymbol *symbol,
bfd_print_symbol_type how)
{
FILE *file = (FILE *) afile;
const char *name;
bfd_mach_o_asymbol *asym = (bfd_mach_o_asymbol *)symbol;
switch (how)
{
case bfd_print_symbol_name:
fprintf (file, "%s", symbol->name);
break;
default:
bfd_print_symbol_vandf (abfd, (void *) file, symbol);
if (asym->n_type & BFD_MACH_O_N_STAB)
name = bfd_get_stab_name (asym->n_type);
else
switch (asym->n_type & BFD_MACH_O_N_TYPE)
{
case BFD_MACH_O_N_UNDF:
if (symbol->value == 0)
name = "UND";
else
name = "COM";
break;
case BFD_MACH_O_N_ABS:
name = "ABS";
break;
case BFD_MACH_O_N_INDR:
name = "INDR";
break;
case BFD_MACH_O_N_PBUD:
name = "PBUD";
break;
case BFD_MACH_O_N_SECT:
name = "SECT";
break;
default:
name = "???";
break;
}
if (name == NULL)
name = "";
fprintf (file, " %02x %-6s %02x %04x",
asym->n_type, name, asym->n_sect, asym->n_desc);
if ((asym->n_type & BFD_MACH_O_N_STAB) == 0
&& (asym->n_type & BFD_MACH_O_N_TYPE) == BFD_MACH_O_N_SECT)
fprintf (file, " [%s]", symbol->section->name);
fprintf (file, " %s", symbol->name);
}
}
static void
bfd_mach_o_convert_architecture (bfd_mach_o_cpu_type mtype,
bfd_mach_o_cpu_subtype msubtype,
enum bfd_architecture *type,
unsigned long *subtype)
{
*subtype = bfd_arch_unknown;
switch (mtype)
{
case BFD_MACH_O_CPU_TYPE_VAX:
*type = bfd_arch_vax;
break;
case BFD_MACH_O_CPU_TYPE_MC680x0:
*type = bfd_arch_m68k;
break;
case BFD_MACH_O_CPU_TYPE_I386:
*type = bfd_arch_i386;
*subtype = bfd_mach_i386_i386;
break;
case BFD_MACH_O_CPU_TYPE_X86_64:
*type = bfd_arch_i386;
*subtype = bfd_mach_x86_64;
break;
case BFD_MACH_O_CPU_TYPE_MIPS:
*type = bfd_arch_mips;
break;
case BFD_MACH_O_CPU_TYPE_MC98000:
*type = bfd_arch_m98k;
break;
case BFD_MACH_O_CPU_TYPE_HPPA:
*type = bfd_arch_hppa;
break;
case BFD_MACH_O_CPU_TYPE_ARM:
*type = bfd_arch_arm;
switch (msubtype)
{
case BFD_MACH_O_CPU_SUBTYPE_ARM_V4T:
*subtype = bfd_mach_arm_4T;
break;
case BFD_MACH_O_CPU_SUBTYPE_ARM_V6:
*subtype = bfd_mach_arm_4T; /* Best fit ? */
break;
case BFD_MACH_O_CPU_SUBTYPE_ARM_V5TEJ:
*subtype = bfd_mach_arm_5TE;
break;
case BFD_MACH_O_CPU_SUBTYPE_ARM_XSCALE:
*subtype = bfd_mach_arm_XScale;
break;
case BFD_MACH_O_CPU_SUBTYPE_ARM_V7:
*subtype = bfd_mach_arm_5TE; /* Best fit ? */
break;
case BFD_MACH_O_CPU_SUBTYPE_ARM_ALL:
default:
break;
}
break;
case BFD_MACH_O_CPU_TYPE_SPARC:
*type = bfd_arch_sparc;
*subtype = bfd_mach_sparc;
break;
case BFD_MACH_O_CPU_TYPE_ALPHA:
*type = bfd_arch_alpha;
break;
case BFD_MACH_O_CPU_TYPE_POWERPC:
*type = bfd_arch_powerpc;
*subtype = bfd_mach_ppc;
break;
case BFD_MACH_O_CPU_TYPE_POWERPC_64:
*type = bfd_arch_powerpc;
*subtype = bfd_mach_ppc64;
break;
case BFD_MACH_O_CPU_TYPE_ARM64:
*type = bfd_arch_aarch64;
*subtype = bfd_mach_aarch64;
break;
default:
*type = bfd_arch_unknown;
break;
}
}
/* Write n NUL bytes to ABFD so that LEN + n is a multiple of 4. Return the
number of bytes written or -1 in case of error. */
static int
bfd_mach_o_pad4 (bfd *abfd, size_t len)
{
if (len % 4 != 0)
{
char pad[4] = {0,0,0,0};
unsigned int padlen = 4 - (len % 4);
if (bfd_write (pad, padlen, abfd) != padlen)
return -1;
return padlen;
}
else
return 0;
}
/* Likewise, but for a command. */
static int
bfd_mach_o_pad_command (bfd *abfd, size_t len)
{
size_t align = bfd_mach_o_wide_p (abfd) ? 8 : 4;
if (len % align != 0)
{
char pad[8] = {0};
size_t padlen = align - (len % align);
if (bfd_write (pad, padlen, abfd) != padlen)
return -1;
return padlen;
}
else
return 0;
}
static bool
bfd_mach_o_write_header (bfd *abfd, bfd_mach_o_header *header)
{
struct mach_o_header_external raw;
size_t size;
size = mach_o_wide_p (header) ?
BFD_MACH_O_HEADER_64_SIZE : BFD_MACH_O_HEADER_SIZE;
bfd_h_put_32 (abfd, header->magic, raw.magic);
bfd_h_put_32 (abfd, header->cputype, raw.cputype);
bfd_h_put_32 (abfd, header->cpusubtype, raw.cpusubtype);
bfd_h_put_32 (abfd, header->filetype, raw.filetype);
bfd_h_put_32 (abfd, header->ncmds, raw.ncmds);
bfd_h_put_32 (abfd, header->sizeofcmds, raw.sizeofcmds);
bfd_h_put_32 (abfd, header->flags, raw.flags);
if (mach_o_wide_p (header))
bfd_h_put_32 (abfd, header->reserved, raw.reserved);
if (bfd_seek (abfd, 0, SEEK_SET) != 0
|| bfd_write (&raw, size, abfd) != size)
return false;
return true;
}
static bool
bfd_mach_o_write_thread (bfd *abfd, bfd_mach_o_load_command *command)
{
bfd_mach_o_thread_command *cmd = &command->command.thread;
unsigned int i;
struct mach_o_thread_command_external raw;
size_t offset;
BFD_ASSERT ((command->type == BFD_MACH_O_LC_THREAD)
|| (command->type == BFD_MACH_O_LC_UNIXTHREAD));
offset = BFD_MACH_O_LC_SIZE;
for (i = 0; i < cmd->nflavours; i++)
{
BFD_ASSERT ((cmd->flavours[i].size % 4) == 0);
BFD_ASSERT (cmd->flavours[i].offset
== command->offset + offset + BFD_MACH_O_LC_SIZE);
bfd_h_put_32 (abfd, cmd->flavours[i].flavour, raw.flavour);
bfd_h_put_32 (abfd, (cmd->flavours[i].size / 4), raw.count);
if (bfd_seek (abfd, command->offset + offset, SEEK_SET) != 0
|| bfd_write (&raw, sizeof (raw), abfd) != sizeof (raw))
return false;
offset += cmd->flavours[i].size + sizeof (raw);
}
return true;
}
static bool
bfd_mach_o_write_dylinker (bfd *abfd, bfd_mach_o_load_command *command)
{
bfd_mach_o_dylinker_command *cmd = &command->command.dylinker;
struct mach_o_str_command_external raw;
size_t namelen;
bfd_h_put_32 (abfd, cmd->name_offset, raw.str);
if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
|| bfd_write (&raw, sizeof (raw), abfd) != sizeof (raw))
return false;
namelen = strlen (cmd->name_str) + 1;
if (bfd_write (cmd->name_str, namelen, abfd) != namelen)
return false;
if (bfd_mach_o_pad_command (abfd, namelen) < 0)
return false;
return true;
}
static bool
bfd_mach_o_write_dylib (bfd *abfd, bfd_mach_o_load_command *command)
{
bfd_mach_o_dylib_command *cmd = &command->command.dylib;
struct mach_o_dylib_command_external raw;
size_t namelen;
bfd_h_put_32 (abfd, cmd->name_offset, raw.name);
bfd_h_put_32 (abfd, cmd->timestamp, raw.timestamp);
bfd_h_put_32 (abfd, cmd->current_version, raw.current_version);
bfd_h_put_32 (abfd, cmd->compatibility_version, raw.compatibility_version);
if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
|| bfd_write (&raw, sizeof (raw), abfd) != sizeof (raw))
return false;
namelen = strlen (cmd->name_str) + 1;
if (bfd_write (cmd->name_str, namelen, abfd) != namelen)
return false;
if (bfd_mach_o_pad_command (abfd, namelen) < 0)
return false;
return true;
}
static bool
bfd_mach_o_write_main (bfd *abfd, bfd_mach_o_load_command *command)
{
bfd_mach_o_main_command *cmd = &command->command.main;
struct mach_o_entry_point_command_external raw;
bfd_h_put_64 (abfd, cmd->entryoff, raw.entryoff);
bfd_h_put_64 (abfd, cmd->stacksize, raw.stacksize);
if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
|| bfd_write (&raw, sizeof (raw), abfd) != sizeof (raw))
return false;
return true;
}
static bool
bfd_mach_o_write_dyld_info (bfd *abfd, bfd_mach_o_load_command *command)
{
bfd_mach_o_dyld_info_command *cmd = &command->command.dyld_info;
struct mach_o_dyld_info_command_external raw;
bfd_h_put_32 (abfd, cmd->rebase_off, raw.rebase_off);
bfd_h_put_32 (abfd, cmd->rebase_size, raw.rebase_size);
bfd_h_put_32 (abfd, cmd->bind_off, raw.bind_off);
bfd_h_put_32 (abfd, cmd->bind_size, raw.bind_size);
bfd_h_put_32 (abfd, cmd->weak_bind_off, raw.weak_bind_off);
bfd_h_put_32 (abfd, cmd->weak_bind_size, raw.weak_bind_size);
bfd_h_put_32 (abfd, cmd->lazy_bind_off, raw.lazy_bind_off);
bfd_h_put_32 (abfd, cmd->lazy_bind_size, raw.lazy_bind_size);
bfd_h_put_32 (abfd, cmd->export_off, raw.export_off);
bfd_h_put_32 (abfd, cmd->export_size, raw.export_size);
if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
|| bfd_write (&raw, sizeof (raw), abfd) != sizeof (raw))
return false;
if (cmd->rebase_size != 0)
if (bfd_seek (abfd, cmd->rebase_off, SEEK_SET) != 0
|| (bfd_write (cmd->rebase_content, cmd->rebase_size, abfd) !=
cmd->rebase_size))
return false;
if (cmd->bind_size != 0)
if (bfd_seek (abfd, cmd->bind_off, SEEK_SET) != 0
|| (bfd_write (cmd->bind_content, cmd->bind_size, abfd) !=
cmd->bind_size))
return false;
if (cmd->weak_bind_size != 0)
if (bfd_seek (abfd, cmd->weak_bind_off, SEEK_SET) != 0
|| (bfd_write (cmd->weak_bind_content, cmd->weak_bind_size, abfd) !=
cmd->weak_bind_size))
return false;
if (cmd->lazy_bind_size != 0)
if (bfd_seek (abfd, cmd->lazy_bind_off, SEEK_SET) != 0
|| (bfd_write (cmd->lazy_bind_content, cmd->lazy_bind_size, abfd) !=
cmd->lazy_bind_size))
return false;
if (cmd->export_size != 0)
if (bfd_seek (abfd, cmd->export_off, SEEK_SET) != 0
|| (bfd_write (cmd->export_content, cmd->export_size, abfd) !=
cmd->export_size))
return false;
return true;
}
long
bfd_mach_o_get_reloc_upper_bound (bfd *abfd, asection *asect)
{
size_t count, raw;
count = asect->reloc_count;
if (count >= LONG_MAX / sizeof (arelent *)
|| _bfd_mul_overflow (count, BFD_MACH_O_RELENT_SIZE, &raw))
{
bfd_set_error (bfd_error_file_too_big);
return -1;
}
if (!bfd_write_p (abfd))
{
ufile_ptr filesize = bfd_get_file_size (abfd);
if (filesize != 0 && raw > filesize)
{
bfd_set_error (bfd_error_file_truncated);
return -1;
}
}
return (count + 1) * sizeof (arelent *);
}
/* In addition to the need to byte-swap the symbol number, the bit positions
of the fields in the relocation information vary per target endian-ness. */
void
bfd_mach_o_swap_in_non_scattered_reloc (bfd *abfd, bfd_mach_o_reloc_info *rel,
unsigned char *fields)
{
unsigned char info = fields[3];
if (bfd_big_endian (abfd))
{
rel->r_value = (fields[0] << 16) | (fields[1] << 8) | fields[2];
rel->r_type = (info >> BFD_MACH_O_BE_TYPE_SHIFT) & BFD_MACH_O_TYPE_MASK;
rel->r_pcrel = (info & BFD_MACH_O_BE_PCREL) ? 1 : 0;
rel->r_length = (info >> BFD_MACH_O_BE_LENGTH_SHIFT)
& BFD_MACH_O_LENGTH_MASK;
rel->r_extern = (info & BFD_MACH_O_BE_EXTERN) ? 1 : 0;
}
else
{
rel->r_value = (fields[2] << 16) | (fields[1] << 8) | fields[0];
rel->r_type = (info >> BFD_MACH_O_LE_TYPE_SHIFT) & BFD_MACH_O_TYPE_MASK;
rel->r_pcrel = (info & BFD_MACH_O_LE_PCREL) ? 1 : 0;
rel->r_length = (info >> BFD_MACH_O_LE_LENGTH_SHIFT)
& BFD_MACH_O_LENGTH_MASK;
rel->r_extern = (info & BFD_MACH_O_LE_EXTERN) ? 1 : 0;
}
}
/* Set syms_ptr_ptr and addend of RES. */
bool
bfd_mach_o_canonicalize_non_scattered_reloc (bfd *abfd,
bfd_mach_o_reloc_info *reloc,
arelent *res, asymbol **syms)
{
bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
unsigned int num;
asymbol **sym;
/* Non-scattered relocation. */
reloc->r_scattered = 0;
res->addend = 0;
num = reloc->r_value;
if (reloc->r_extern)
{
/* PR 17512: file: 8396-1185-0.004. */
if (num >= (unsigned) bfd_mach_o_count_symbols (abfd))
sym = bfd_und_section_ptr->symbol_ptr_ptr;
else if (syms == NULL)
sym = bfd_und_section_ptr->symbol_ptr_ptr;
else
/* An external symbol number. */
sym = syms + num;
}
else if (num == 0x00ffffff || num == 0)
{
/* The 'symnum' in a non-scattered PAIR is 0x00ffffff. But as this
is generic code, we don't know wether this is really a PAIR.
This value is almost certainly not a valid section number, hence
this specific case to avoid an assertion failure.
Target specific swap_reloc_in routine should adjust that. */
sym = bfd_abs_section_ptr->symbol_ptr_ptr;
}
else
{
/* PR 17512: file: 006-2964-0.004. */
if (num > mdata->nsects)
{
_bfd_error_handler (_("\
malformed mach-o reloc: section index is greater than the number of sections"));
return false;
}
/* A section number. */
sym = mdata->sections[num - 1]->bfdsection->symbol_ptr_ptr;
/* For a symbol defined in section S, the addend (stored in the
binary) contains the address of the section. To comply with
bfd convention, subtract the section address.
Use the address from the header, so that the user can modify
the vma of the section. */
res->addend = -mdata->sections[num - 1]->addr;
}
/* Note: Pairs for PPC LO/HI/HA are not scattered, but contain the offset
in the lower 16bits of the address value. So we have to find the
'symbol' from the preceding reloc. We do this even though the
section symbol is probably not needed here, because NULL symbol
values cause an assert in generic BFD code. This must be done in
the PPC swap_reloc_in routine. */
res->sym_ptr_ptr = sym;
return true;
}
/* Do most of the work for canonicalize_relocs on RAW: create internal
representation RELOC and set most fields of RES using symbol table SYMS.
Each target still has to set the howto of RES and possibly adjust other
fields.
Previously the Mach-O hook point was simply swap_in, but some targets
(like arm64) don't follow the generic rules (symnum is a value for the
non-scattered relocation ADDEND). */
bool
bfd_mach_o_pre_canonicalize_one_reloc (bfd *abfd,
struct mach_o_reloc_info_external *raw,
bfd_mach_o_reloc_info *reloc,
arelent *res, asymbol **syms)
{
bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
bfd_vma addr;
addr = bfd_get_32 (abfd, raw->r_address);
res->sym_ptr_ptr = bfd_und_section_ptr->symbol_ptr_ptr;
res->addend = 0;
if (addr & BFD_MACH_O_SR_SCATTERED)
{
unsigned int j;
bfd_vma symnum = bfd_get_32 (abfd, raw->r_symbolnum);
/* Scattered relocation, can't be extern. */
reloc->r_scattered = 1;
reloc->r_extern = 0;
/* Extract section and offset from r_value (symnum). */
reloc->r_value = symnum;
/* FIXME: This breaks when a symbol in a reloc exactly follows the
end of the data for the section (e.g. in a calculation of section
data length). At present, the symbol will end up associated with
the following section or, if it falls within alignment padding, as
the undefined section symbol. */
for (j = 0; j < mdata->nsects; j++)
{
bfd_mach_o_section *sect = mdata->sections[j];
if (symnum >= sect->addr && symnum < sect->addr + sect->size)
{
res->sym_ptr_ptr = sect->bfdsection->symbol_ptr_ptr;
res->addend = symnum - sect->addr;
break;
}
}
/* Extract the info and address fields from r_address. */
reloc->r_type = BFD_MACH_O_GET_SR_TYPE (addr);
reloc->r_length = BFD_MACH_O_GET_SR_LENGTH (addr);
reloc->r_pcrel = addr & BFD_MACH_O_SR_PCREL;
reloc->r_address = BFD_MACH_O_GET_SR_TYPE (addr);
res->address = BFD_MACH_O_GET_SR_ADDRESS (addr);
}
else
{
/* Non-scattered relocation. */
reloc->r_scattered = 0;
reloc->r_address = addr;
res->address = addr;
/* The value and info fields have to be extracted dependent on target
endian-ness. */
bfd_mach_o_swap_in_non_scattered_reloc (abfd, reloc, raw->r_symbolnum);
if (!bfd_mach_o_canonicalize_non_scattered_reloc (abfd, reloc,
res, syms))
return false;
}
/* We have set up a reloc with all the information present, so the swapper
can modify address, value and addend fields, if necessary, to convey
information in the generic BFD reloc that is mach-o specific. */
return true;
}
static int
bfd_mach_o_canonicalize_relocs (bfd *abfd, unsigned long filepos,
unsigned long count,
arelent *res, asymbol **syms)
{
bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
unsigned long i;
struct mach_o_reloc_info_external *native_relocs = NULL;
size_t native_size;
/* Allocate and read relocs. */
if (_bfd_mul_overflow (count, BFD_MACH_O_RELENT_SIZE, &native_size))
/* PR 17512: file: 09477b57. */
goto err;
if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
return -1;
native_relocs = (struct mach_o_reloc_info_external *)
_bfd_malloc_and_read (abfd, native_size, native_size);
if (native_relocs == NULL)
return -1;
for (i = 0; i < count; i++)
{
if (!(*bed->_bfd_mach_o_canonicalize_one_reloc)(abfd, &native_relocs[i],
&res[i], syms, res))
goto err;
}
free (native_relocs);
return i;
err:
free (native_relocs);
if (bfd_get_error () == bfd_error_no_error)
bfd_set_error (bfd_error_invalid_operation);
return -1;
}
long
bfd_mach_o_canonicalize_reloc (bfd *abfd, asection *asect,
arelent **rels, asymbol **syms)
{
bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
unsigned long i;
arelent *res;
if (asect->reloc_count == 0)
return 0;
/* No need to go further if we don't know how to read relocs. */
if (bed->_bfd_mach_o_canonicalize_one_reloc == NULL)
return 0;
if (asect->relocation == NULL)
{
size_t amt;
if (_bfd_mul_overflow (asect->reloc_count, sizeof (arelent), &amt))
{
bfd_set_error (bfd_error_file_too_big);
return -1;
}
res = bfd_malloc (amt);
if (res == NULL)
return -1;
if (bfd_mach_o_canonicalize_relocs (abfd, asect->rel_filepos,
asect->reloc_count, res, syms) < 0)
{
free (res);
return -1;
}
asect->relocation = res;
}
res = asect->relocation;
for (i = 0; i < asect->reloc_count; i++)
rels[i] = &res[i];
rels[i] = NULL;
return i;
}
long
bfd_mach_o_get_dynamic_reloc_upper_bound (bfd *abfd)
{
bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
bfd_mach_o_dysymtab_command *dysymtab = mdata->dysymtab;
if (dysymtab == NULL)
return 1;
ufile_ptr filesize = bfd_get_file_size (abfd);
size_t amt;
if (filesize != 0)
{
if (dysymtab->extreloff > filesize
|| dysymtab->nextrel > ((filesize - dysymtab->extreloff)
/ BFD_MACH_O_RELENT_SIZE)
|| dysymtab->locreloff > filesize
|| dysymtab->nlocrel > ((filesize - dysymtab->locreloff)
/ BFD_MACH_O_RELENT_SIZE))
{
bfd_set_error (bfd_error_file_truncated);
return -1;
}
}
if (dysymtab->nextrel + dysymtab->nlocrel < dysymtab->nextrel
|| _bfd_mul_overflow (dysymtab->nextrel + dysymtab->nlocrel,
sizeof (arelent), &amt))
{
bfd_set_error (bfd_error_file_too_big);
return -1;
}
return (dysymtab->nextrel + dysymtab->nlocrel + 1) * sizeof (arelent *);
}
long
bfd_mach_o_canonicalize_dynamic_reloc (bfd *abfd, arelent **rels,
struct bfd_symbol **syms)
{
bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
bfd_mach_o_dysymtab_command *dysymtab = mdata->dysymtab;
bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
unsigned long i;
arelent *res;
if (dysymtab == NULL)
return 0;
if (dysymtab->nextrel == 0 && dysymtab->nlocrel == 0)
return 0;
/* No need to go further if we don't know how to read relocs. */
if (bed->_bfd_mach_o_canonicalize_one_reloc == NULL)
return 0;
if (mdata->dyn_reloc_cache == NULL)
{
size_t amt = (dysymtab->nextrel + dysymtab->nlocrel) * sizeof (arelent);
res = bfd_malloc (amt);
if (res == NULL)
return -1;
if (bfd_mach_o_canonicalize_relocs (abfd, dysymtab->extreloff,
dysymtab->nextrel, res, syms) < 0)
{
free (res);
return -1;
}
if (bfd_mach_o_canonicalize_relocs (abfd, dysymtab->locreloff,
dysymtab->nlocrel,
res + dysymtab->nextrel, syms) < 0)
{
free (res);
return -1;
}
mdata->dyn_reloc_cache = res;
}
res = mdata->dyn_reloc_cache;
for (i = 0; i < dysymtab->nextrel + dysymtab->nlocrel; i++)
rels[i] = &res[i];
rels[i] = NULL;
return i;
}
/* In addition to the need to byte-swap the symbol number, the bit positions
of the fields in the relocation information vary per target endian-ness. */
static void
bfd_mach_o_swap_out_non_scattered_reloc (bfd *abfd, unsigned char *fields,
bfd_mach_o_reloc_info *rel)
{
unsigned char info = 0;
BFD_ASSERT (rel->r_type <= 15);
BFD_ASSERT (rel->r_length <= 3);
if (bfd_big_endian (abfd))
{
fields[0] = (rel->r_value >> 16) & 0xff;
fields[1] = (rel->r_value >> 8) & 0xff;
fields[2] = rel->r_value & 0xff;
info |= rel->r_type << BFD_MACH_O_BE_TYPE_SHIFT;
info |= rel->r_pcrel ? BFD_MACH_O_BE_PCREL : 0;
info |= rel->r_length << BFD_MACH_O_BE_LENGTH_SHIFT;
info |= rel->r_extern ? BFD_MACH_O_BE_EXTERN : 0;
}
else
{
fields[2] = (rel->r_value >> 16) & 0xff;
fields[1] = (rel->r_value >> 8) & 0xff;
fields[0] = rel->r_value & 0xff;
info |= rel->r_type << BFD_MACH_O_LE_TYPE_SHIFT;
info |= rel->r_pcrel ? BFD_MACH_O_LE_PCREL : 0;
info |= rel->r_length << BFD_MACH_O_LE_LENGTH_SHIFT;
info |= rel->r_extern ? BFD_MACH_O_LE_EXTERN : 0;
}
fields[3] = info;
}
static bool
bfd_mach_o_write_relocs (bfd *abfd, bfd_mach_o_section *section)
{
unsigned int i;
arelent **entries;
asection *sec;
bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
sec = section->bfdsection;
if (sec->reloc_count == 0)
return true;
if (bed->_bfd_mach_o_swap_reloc_out == NULL)
return true;
if (bfd_seek (abfd, section->reloff, SEEK_SET) != 0)
return false;
/* Convert and write. */
entries = section->bfdsection->orelocation;
for (i = 0; i < section->nreloc; i++)
{
arelent *rel = entries[i];
struct mach_o_reloc_info_external raw;
bfd_mach_o_reloc_info info, *pinfo = &info;
/* Convert relocation to an intermediate representation. */
if (!(*bed->_bfd_mach_o_swap_reloc_out) (rel, pinfo))
return false;
/* Lower the relocation info. */
if (pinfo->r_scattered)
{
unsigned long v;
v = BFD_MACH_O_SR_SCATTERED
| (pinfo->r_pcrel ? BFD_MACH_O_SR_PCREL : 0)
| BFD_MACH_O_SET_SR_LENGTH (pinfo->r_length)
| BFD_MACH_O_SET_SR_TYPE (pinfo->r_type)
| BFD_MACH_O_SET_SR_ADDRESS (pinfo->r_address);
/* Note: scattered relocs have field in reverse order... */
bfd_put_32 (abfd, v, raw.r_address);
bfd_put_32 (abfd, pinfo->r_value, raw.r_symbolnum);
}
else
{
bfd_put_32 (abfd, pinfo->r_address, raw.r_address);
bfd_mach_o_swap_out_non_scattered_reloc (abfd, raw.r_symbolnum,
pinfo);
}
if (bfd_write (&raw, BFD_MACH_O_RELENT_SIZE, abfd)
!= BFD_MACH_O_RELENT_SIZE)
return false;
}
return true;
}
static bool
bfd_mach_o_write_section_32 (bfd *abfd, bfd_mach_o_section *section)
{
struct mach_o_section_32_external raw;
memcpy (raw.sectname, section->sectname, 16);
memcpy (raw.segname, section->segname, 16);
bfd_h_put_32 (abfd, section->addr, raw.addr);
bfd_h_put_32 (abfd, section->size, raw.size);
bfd_h_put_32 (abfd, section->offset, raw.offset);
bfd_h_put_32 (abfd, section->align, raw.align);
bfd_h_put_32 (abfd, section->reloff, raw.reloff);
bfd_h_put_32 (abfd, section->nreloc, raw.nreloc);
bfd_h_put_32 (abfd, section->flags, raw.flags);
bfd_h_put_32 (abfd, section->reserved1, raw.reserved1);
bfd_h_put_32 (abfd, section->reserved2, raw.reserved2);
if (bfd_write (&raw, BFD_MACH_O_SECTION_SIZE, abfd)
!= BFD_MACH_O_SECTION_SIZE)
return false;
return true;
}
static bool
bfd_mach_o_write_section_64 (bfd *abfd, bfd_mach_o_section *section)
{
struct mach_o_section_64_external raw;
memcpy (raw.sectname, section->sectname, 16);
memcpy (raw.segname, section->segname, 16);
bfd_h_put_64 (abfd, section->addr, raw.addr);
bfd_h_put_64 (abfd, section->size, raw.size);
bfd_h_put_32 (abfd, section->offset, raw.offset);
bfd_h_put_32 (abfd, section->align, raw.align);
bfd_h_put_32 (abfd, section->reloff, raw.reloff);
bfd_h_put_32 (abfd, section->nreloc, raw.nreloc);
bfd_h_put_32 (abfd, section->flags, raw.flags);
bfd_h_put_32 (abfd, section->reserved1, raw.reserved1);
bfd_h_put_32 (abfd, section->reserved2, raw.reserved2);
bfd_h_put_32 (abfd, section->reserved3, raw.reserved3);
if (bfd_write (&raw, BFD_MACH_O_SECTION_64_SIZE, abfd)
!= BFD_MACH_O_SECTION_64_SIZE)
return false;
return true;
}
static bool
bfd_mach_o_write_segment_32 (bfd *abfd, bfd_mach_o_load_command *command)
{
struct mach_o_segment_command_32_external raw;
bfd_mach_o_segment_command *seg = &command->command.segment;
bfd_mach_o_section *sec;
BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT);
for (sec = seg->sect_head; sec != NULL; sec = sec->next)
if (!bfd_mach_o_write_relocs (abfd, sec))
return false;
memcpy (raw.segname, seg->segname, 16);
bfd_h_put_32 (abfd, seg->vmaddr, raw.vmaddr);
bfd_h_put_32 (abfd, seg->vmsize, raw.vmsize);
bfd_h_put_32 (abfd, seg->fileoff, raw.fileoff);
bfd_h_put_32 (abfd, seg->filesize, raw.filesize);
bfd_h_put_32 (abfd, seg->maxprot, raw.maxprot);
bfd_h_put_32 (abfd, seg->initprot, raw.initprot);
bfd_h_put_32 (abfd, seg->nsects, raw.nsects);
bfd_h_put_32 (abfd, seg->flags, raw.flags);
if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
|| bfd_write (&raw, sizeof (raw), abfd) != sizeof (raw))
return false;
for (sec = seg->sect_head; sec != NULL; sec = sec->next)
if (!bfd_mach_o_write_section_32 (abfd, sec))
return false;
return true;
}
static bool
bfd_mach_o_write_segment_64 (bfd *abfd, bfd_mach_o_load_command *command)
{
struct mach_o_segment_command_64_external raw;
bfd_mach_o_segment_command *seg = &command->command.segment;
bfd_mach_o_section *sec;
BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT_64);
for (sec = seg->sect_head; sec != NULL; sec = sec->next)
if (!bfd_mach_o_write_relocs (abfd, sec))
return false;
memcpy (raw.segname, seg->segname, 16);
bfd_h_put_64 (abfd, seg->vmaddr, raw.vmaddr);
bfd_h_put_64 (abfd, seg->vmsize, raw.vmsize);
bfd_h_put_64 (abfd, seg->fileoff, raw.fileoff);
bfd_h_put_64 (abfd, seg->filesize, raw.filesize);
bfd_h_put_32 (abfd, seg->maxprot, raw.maxprot);
bfd_h_put_32 (abfd, seg->initprot, raw.initprot);
bfd_h_put_32 (abfd, seg->nsects, raw.nsects);
bfd_h_put_32 (abfd, seg->flags, raw.flags);
if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
|| bfd_write (&raw, sizeof (raw), abfd) != sizeof (raw))
return false;
for (sec = seg->sect_head; sec != NULL; sec = sec->next)
if (!bfd_mach_o_write_section_64 (abfd, sec))
return false;
return true;
}
static bool
bfd_mach_o_write_symtab_content (bfd *abfd, bfd_mach_o_symtab_command *sym)
{
bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
unsigned long i;
unsigned int wide = bfd_mach_o_wide_p (abfd);
struct bfd_strtab_hash *strtab;
asymbol **symbols = bfd_get_outsymbols (abfd);
int padlen;
/* Write the symbols first. */
if (bfd_seek (abfd, sym->symoff, SEEK_SET) != 0)
return false;
strtab = _bfd_stringtab_init ();
if (strtab == NULL)
return false;
if (sym->nsyms > 0)
/* Although we don't strictly need to do this, for compatibility with
Darwin system tools, actually output an empty string for the index
0 entry. */
_bfd_stringtab_add (strtab, "", true, false);
for (i = 0; i < sym->nsyms; i++)
{
bfd_size_type str_index;
bfd_mach_o_asymbol *s = (bfd_mach_o_asymbol *)symbols[i];
if (s->symbol.name == 0 || s->symbol.name[0] == '\0')
/* An index of 0 always means the empty string. */
str_index = 0;
else
{
str_index = _bfd_stringtab_add (strtab, s->symbol.name, true, false);
if (str_index == (bfd_size_type) -1)
goto err;
}
if (wide)
{
struct mach_o_nlist_64_external raw;
bfd_h_put_32 (abfd, str_index, raw.n_strx);
bfd_h_put_8 (abfd, s->n_type, raw.n_type);
bfd_h_put_8 (abfd, s->n_sect, raw.n_sect);
bfd_h_put_16 (abfd, s->n_desc, raw.n_desc);
bfd_h_put_64 (abfd, s->symbol.section->vma + s->symbol.value,
raw.n_value);
if (bfd_write (&raw, sizeof (raw), abfd) != sizeof (raw))
goto err;
}
else
{
struct mach_o_nlist_external raw;
bfd_h_put_32 (abfd, str_index, raw.n_strx);
bfd_h_put_8 (abfd, s->n_type, raw.n_type);
bfd_h_put_8 (abfd, s->n_sect, raw.n_sect);
bfd_h_put_16 (abfd, s->n_desc, raw.n_desc);
bfd_h_put_32 (abfd, s->symbol.section->vma + s->symbol.value,
raw.n_value);
if (bfd_write (&raw, sizeof (raw), abfd) != sizeof (raw))
goto err;
}
}
sym->strsize = _bfd_stringtab_size (strtab);
sym->stroff = mdata->filelen;
mdata->filelen += sym->strsize;
if (bfd_seek (abfd, sym->stroff, SEEK_SET) != 0)
goto err;
if (!_bfd_stringtab_emit (abfd, strtab))
goto err;
_bfd_stringtab_free (strtab);
/* Pad string table. */
padlen = bfd_mach_o_pad4 (abfd, sym->strsize);
if (padlen < 0)
return false;
mdata->filelen += padlen;
sym->strsize += padlen;
return true;
err:
_bfd_stringtab_free (strtab);
sym->strsize = 0;
return false;
}
static bool
bfd_mach_o_write_symtab (bfd *abfd, bfd_mach_o_load_command *command)
{
bfd_mach_o_symtab_command *sym = &command->command.symtab;
struct mach_o_symtab_command_external raw;
BFD_ASSERT (command->type == BFD_MACH_O_LC_SYMTAB);
/* The command. */
bfd_h_put_32 (abfd, sym->symoff, raw.symoff);
bfd_h_put_32 (abfd, sym->nsyms, raw.nsyms);
bfd_h_put_32 (abfd, sym->stroff, raw.stroff);
bfd_h_put_32 (abfd, sym->strsize, raw.strsize);
if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
|| bfd_write (&raw, sizeof (raw), abfd) != sizeof (raw))
return false;
return true;
}
/* Count the number of indirect symbols in the image.
Requires that the sections are in their final order. */
static unsigned int
bfd_mach_o_count_indirect_symbols (bfd *abfd, bfd_mach_o_data_struct *mdata)
{
unsigned int i;
unsigned int nisyms = 0;
for (i = 0; i < mdata->nsects; ++i)
{
bfd_mach_o_section *sec = mdata->sections[i];
switch (sec->flags & BFD_MACH_O_SECTION_TYPE_MASK)
{
case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS:
case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS:
case BFD_MACH_O_S_SYMBOL_STUBS:
nisyms += bfd_mach_o_section_get_nbr_indirect (abfd, sec);
break;
default:
break;
}
}
return nisyms;
}
/* Create the dysymtab. */
static bool
bfd_mach_o_build_dysymtab (bfd *abfd, bfd_mach_o_dysymtab_command *cmd)
{
bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
/* TODO:
We are not going to try and fill these in yet and, moreover, we are
going to bail if they are already set. */
if (cmd->nmodtab != 0
|| cmd->ntoc != 0
|| cmd->nextrefsyms != 0)
{
_bfd_error_handler (_("sorry: modtab, toc and extrefsyms are not yet"
" implemented for dysymtab commands."));
return false;
}
cmd->ilocalsym = 0;
if (bfd_get_symcount (abfd) > 0)
{
asymbol **symbols = bfd_get_outsymbols (abfd);
unsigned long i;
/* Count the number of each kind of symbol. */
for (i = 0; i < bfd_get_symcount (abfd); ++i)
{
bfd_mach_o_asymbol *s = (bfd_mach_o_asymbol *)symbols[i];
if (s->n_type & (BFD_MACH_O_N_EXT | BFD_MACH_O_N_PEXT))
break;
}
cmd->nlocalsym = i;
cmd->iextdefsym = i;
for (; i < bfd_get_symcount (abfd); ++i)
{
bfd_mach_o_asymbol *s = (bfd_mach_o_asymbol *)symbols[i];
if ((s->n_type & BFD_MACH_O_N_TYPE) == BFD_MACH_O_N_UNDF)
break;
}
cmd->nextdefsym = i - cmd->nlocalsym;
cmd->iundefsym = cmd->nextdefsym + cmd->iextdefsym;
cmd->nundefsym = bfd_get_symcount (abfd)
- cmd->nlocalsym
- cmd->nextdefsym;
}
else
{
cmd->nlocalsym = 0;
cmd->iextdefsym = 0;
cmd->nextdefsym = 0;
cmd->iundefsym = 0;
cmd->nundefsym = 0;
}
cmd->nindirectsyms = bfd_mach_o_count_indirect_symbols (abfd, mdata);
if (cmd->nindirectsyms > 0)
{
unsigned i;
unsigned n;
size_t amt;
mdata->filelen = FILE_ALIGN (mdata->filelen, 2);
cmd->indirectsymoff = mdata->filelen;
if (_bfd_mul_overflow (cmd->nindirectsyms, 4, &amt))
return false;
mdata->filelen += amt;
cmd->indirect_syms = bfd_zalloc (abfd, amt);
if (cmd->indirect_syms == NULL)
return false;
n = 0;
for (i = 0; i < mdata->nsects; ++i)
{
bfd_mach_o_section *sec = mdata->sections[i];
switch (sec->flags & BFD_MACH_O_SECTION_TYPE_MASK)
{
case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS:
case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS:
case BFD_MACH_O_S_SYMBOL_STUBS:
{
unsigned j, num;
bfd_mach_o_asymbol **isyms = sec->indirect_syms;
num = bfd_mach_o_section_get_nbr_indirect (abfd, sec);
if (isyms == NULL || num == 0)
break;
/* Record the starting index in the reserved1 field. */
sec->reserved1 = n;
for (j = 0; j < num; j++, n++)
{
if (isyms[j] == NULL)
cmd->indirect_syms[n] = BFD_MACH_O_INDIRECT_SYM_LOCAL;
else if (isyms[j]->symbol.section == bfd_abs_section_ptr
&& ! (isyms[j]->n_type & BFD_MACH_O_N_EXT))
cmd->indirect_syms[n] = BFD_MACH_O_INDIRECT_SYM_LOCAL
| BFD_MACH_O_INDIRECT_SYM_ABS;
else
cmd->indirect_syms[n] = isyms[j]->symbol.udata.i;
}
}
break;
default:
break;
}
}
}
return true;
}
/* Write a dysymtab command.
TODO: Possibly coalesce writes of smaller objects. */
static bool
bfd_mach_o_write_dysymtab (bfd *abfd, bfd_mach_o_load_command *command)
{
bfd_mach_o_dysymtab_command *cmd = &command->command.dysymtab;
BFD_ASSERT (command->type == BFD_MACH_O_LC_DYSYMTAB);
if (cmd->nmodtab != 0)
{
unsigned int i;
if (bfd_seek (abfd, cmd->modtaboff, SEEK_SET) != 0)
return false;
for (i = 0; i < cmd->nmodtab; i++)
{
bfd_mach_o_dylib_module *module = &cmd->dylib_module[i];
unsigned int iinit;
unsigned int ninit;
iinit = module->iinit & 0xffff;
iinit |= ((module->iterm & 0xffff) << 16);
ninit = module->ninit & 0xffff;
ninit |= ((module->nterm & 0xffff) << 16);
if (bfd_mach_o_wide_p (abfd))
{
struct mach_o_dylib_module_64_external w;
bfd_h_put_32 (abfd, module->module_name_idx, &w.module_name);
bfd_h_put_32 (abfd, module->iextdefsym, &w.iextdefsym);
bfd_h_put_32 (abfd, module->nextdefsym, &w.nextdefsym);
bfd_h_put_32 (abfd, module->irefsym, &w.irefsym);
bfd_h_put_32 (abfd, module->nrefsym, &w.nrefsym);
bfd_h_put_32 (abfd, module->ilocalsym, &w.ilocalsym);
bfd_h_put_32 (abfd, module->nlocalsym, &w.nlocalsym);
bfd_h_put_32 (abfd, module->iextrel, &w.iextrel);
bfd_h_put_32 (abfd, module->nextrel, &w.nextrel);
bfd_h_put_32 (abfd, iinit, &w.iinit_iterm);
bfd_h_put_32 (abfd, ninit, &w.ninit_nterm);
bfd_h_put_64 (abfd, module->objc_module_info_addr,
&w.objc_module_info_addr);
bfd_h_put_32 (abfd, module->objc_module_info_size,
&w.objc_module_info_size);
if (bfd_write (&w, sizeof (w), abfd) != sizeof (w))
return false;
}
else
{
struct mach_o_dylib_module_external n;
bfd_h_put_32 (abfd, module->module_name_idx, &n.module_name);
bfd_h_put_32 (abfd, module->iextdefsym, &n.iextdefsym);
bfd_h_put_32 (abfd, module->nextdefsym, &n.nextdefsym);
bfd_h_put_32 (abfd, module->irefsym, &n.irefsym);
bfd_h_put_32 (abfd, module->nrefsym, &n.nrefsym);
bfd_h_put_32 (abfd, module->ilocalsym, &n.ilocalsym);
bfd_h_put_32 (abfd, module->nlocalsym, &n.nlocalsym);
bfd_h_put_32 (abfd, module->iextrel, &n.iextrel);
bfd_h_put_32 (abfd, module->nextrel, &n.nextrel);
bfd_h_put_32 (abfd, iinit, &n.iinit_iterm);
bfd_h_put_32 (abfd, ninit, &n.ninit_nterm);
bfd_h_put_32 (abfd, module->objc_module_info_addr,
&n.objc_module_info_addr);
bfd_h_put_32 (abfd, module->objc_module_info_size,
&n.objc_module_info_size);
if (bfd_write (&n, sizeof (n), abfd) != sizeof (n))
return false;
}
}
}
if (cmd->ntoc != 0)
{
unsigned int i;
if (bfd_seek (abfd, cmd->tocoff, SEEK_SET) != 0)
return false;
for (i = 0; i < cmd->ntoc; i++)
{
struct mach_o_dylib_table_of_contents_external raw;
bfd_mach_o_dylib_table_of_content *toc = &cmd->dylib_toc[i];
bfd_h_put_32 (abfd, toc->symbol_index, &raw.symbol_index);
bfd_h_put_32 (abfd, toc->module_index, &raw.module_index);
if (bfd_write (&raw, sizeof (raw), abfd) != sizeof (raw))
return false;
}
}
if (cmd->nindirectsyms > 0)
{
unsigned int i;
if (bfd_seek (abfd, cmd->indirectsymoff, SEEK_SET) != 0)
return false;
for (i = 0; i < cmd->nindirectsyms; ++i)
{
unsigned char raw[4];
bfd_h_put_32 (abfd, cmd->indirect_syms[i], &raw);
if (bfd_write (raw, sizeof (raw), abfd) != sizeof (raw))
return false;
}
}
if (cmd->nextrefsyms != 0)
{
unsigned int i;
if (bfd_seek (abfd, cmd->extrefsymoff, SEEK_SET) != 0)
return false;
for (i = 0; i < cmd->nextrefsyms; i++)
{
unsigned long v;
unsigned char raw[4];
bfd_mach_o_dylib_reference *ref = &cmd->ext_refs[i];
/* Fields isym and flags are written as bit-fields, thus we need
a specific processing for endianness. */
if (bfd_big_endian (abfd))
{
v = ((ref->isym & 0xffffff) << 8);
v |= ref->flags & 0xff;
}
else
{
v = ref->isym & 0xffffff;
v |= ((ref->flags & 0xff) << 24);
}
bfd_h_put_32 (abfd, v, raw);
if (bfd_write (raw, sizeof (raw), abfd) != sizeof (raw))
return false;
}
}
/* The command. */
if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0)
return false;
else
{
struct mach_o_dysymtab_command_external raw;
bfd_h_put_32 (abfd, cmd->ilocalsym, &raw.ilocalsym);
bfd_h_put_32 (abfd, cmd->nlocalsym, &raw.nlocalsym);
bfd_h_put_32 (abfd, cmd->iextdefsym, &raw.iextdefsym);
bfd_h_put_32 (abfd, cmd->nextdefsym, &raw.nextdefsym);
bfd_h_put_32 (abfd, cmd->iundefsym, &raw.iundefsym);
bfd_h_put_32 (abfd, cmd->nundefsym, &raw.nundefsym);
bfd_h_put_32 (abfd, cmd->tocoff, &raw.tocoff);
bfd_h_put_32 (abfd, cmd->ntoc, &raw.ntoc);
bfd_h_put_32 (abfd, cmd->modtaboff, &raw.modtaboff);
bfd_h_put_32 (abfd, cmd->nmodtab, &raw.nmodtab);
bfd_h_put_32 (abfd, cmd->extrefsymoff, &raw.extrefsymoff);
bfd_h_put_32 (abfd, cmd->nextrefsyms, &raw.nextrefsyms);
bfd_h_put_32 (abfd, cmd->indirectsymoff, &raw.indirectsymoff);
bfd_h_put_32 (abfd, cmd->nindirectsyms, &raw.nindirectsyms);
bfd_h_put_32 (abfd, cmd->extreloff, &raw.extreloff);
bfd_h_put_32 (abfd, cmd->nextrel, &raw.nextrel);
bfd_h_put_32 (abfd, cmd->locreloff, &raw.locreloff);
bfd_h_put_32 (abfd, cmd->nlocrel, &raw.nlocrel);
if (bfd_write (&raw, sizeof (raw), abfd) != sizeof (raw))
return false;
}
return true;
}
static unsigned
bfd_mach_o_primary_symbol_sort_key (bfd_mach_o_asymbol *s)
{
unsigned mtyp = s->n_type & BFD_MACH_O_N_TYPE;
/* Just leave debug symbols where they are (pretend they are local, and
then they will just be sorted on position). */
if (s->n_type & BFD_MACH_O_N_STAB)
return 0;
/* Local (we should never see an undefined local AFAICT). */
if (! (s->n_type & (BFD_MACH_O_N_EXT | BFD_MACH_O_N_PEXT)))
return 0;
/* Common symbols look like undefined externs. */
if (mtyp == BFD_MACH_O_N_UNDF)
return 2;
/* A defined non-local, non-debug symbol. */
return 1;
}
static int
bfd_mach_o_cf_symbols (const void *a, const void *b)
{
bfd_mach_o_asymbol *sa = *(bfd_mach_o_asymbol **) a;
bfd_mach_o_asymbol *sb = *(bfd_mach_o_asymbol **) b;
unsigned int soa, sob;
soa = bfd_mach_o_primary_symbol_sort_key (sa);
sob = bfd_mach_o_primary_symbol_sort_key (sb);
if (soa < sob)
return -1;
if (soa > sob)
return 1;
/* If it's local or stab, just preserve the input order. */
if (soa == 0)
{
if (sa->symbol.udata.i < sb->symbol.udata.i)
return -1;
if (sa->symbol.udata.i > sb->symbol.udata.i)
return 1;
/* This is probably an error. */
return 0;
}
/* The second sort key is name. */
return strcmp (sa->symbol.name, sb->symbol.name);
}
/* Process the symbols.
This should be OK for single-module files - but it is not likely to work
for multi-module shared libraries.
(a) If the application has not filled in the relevant mach-o fields, make
an estimate.
(b) Order them, like this:
( i) local.
(unsorted)
( ii) external defined
(by name)
(iii) external undefined/common
(by name)
( iv) common
(by name)
*/
static bool
bfd_mach_o_mangle_symbols (bfd *abfd)
{
unsigned long i;
asymbol **symbols = bfd_get_outsymbols (abfd);
if (symbols == NULL || bfd_get_symcount (abfd) == 0)
return true;
for (i = 0; i < bfd_get_symcount (abfd); i++)
{
bfd_mach_o_asymbol *s = (bfd_mach_o_asymbol *)symbols[i];
/* We use this value, which is out-of-range as a symbol index, to signal
that the mach-o-specific data are not filled in and need to be created
from the bfd values. It is much preferable for the application to do
this, since more meaningful diagnostics can be made that way. */
if (s->symbol.udata.i == SYM_MACHO_FIELDS_UNSET)
{
/* No symbol information has been set - therefore determine
it from the bfd symbol flags/info. */
if (s->symbol.section == bfd_abs_section_ptr)
s->n_type = BFD_MACH_O_N_ABS;
else if (s->symbol.section == bfd_und_section_ptr)
{
s->n_type = BFD_MACH_O_N_UNDF;
if (s->symbol.flags & BSF_WEAK)
s->n_desc |= BFD_MACH_O_N_WEAK_REF;
/* mach-o automatically makes undefined symbols extern. */
s->n_type |= BFD_MACH_O_N_EXT;
s->symbol.flags |= BSF_GLOBAL;
}
else if (s->symbol.section == bfd_com_section_ptr)
{
s->n_type = BFD_MACH_O_N_UNDF | BFD_MACH_O_N_EXT;
s->symbol.flags |= BSF_GLOBAL;
}
else
s->n_type = BFD_MACH_O_N_SECT;
}
/* Update external symbol bit in case objcopy changed it. */
if (s->symbol.flags & BSF_GLOBAL)
s->n_type |= BFD_MACH_O_N_EXT;
else
s->n_type &= ~BFD_MACH_O_N_EXT;
/* Put the section index in, where required. */
if ((s->symbol.section != bfd_abs_section_ptr
&& s->symbol.section != bfd_und_section_ptr
&& s->symbol.section != bfd_com_section_ptr)
|| ((s->n_type & BFD_MACH_O_N_STAB) != 0
&& s->symbol.name == NULL))
s->n_sect = s->symbol.section->output_section->target_index;
/* Number to preserve order for local and debug syms. */
s->symbol.udata.i = i;
}
/* Sort the symbols. */
qsort ((void *) symbols, (size_t) bfd_get_symcount (abfd),
sizeof (asymbol *), bfd_mach_o_cf_symbols);
for (i = 0; i < bfd_get_symcount (abfd); ++i)
{
bfd_mach_o_asymbol *s = (bfd_mach_o_asymbol *)symbols[i];
s->symbol.udata.i = i; /* renumber. */
}
return true;
}
/* We build a flat table of sections, which can be re-ordered if necessary.
Fill in the section number and other mach-o-specific data. */
static bool
bfd_mach_o_mangle_sections (bfd *abfd, bfd_mach_o_data_struct *mdata)
{
asection *sec;
unsigned target_index;
unsigned nsect;
size_t amt;
nsect = bfd_count_sections (abfd);
/* Don't do it if it's already set - assume the application knows what it's
doing. */
if (mdata->nsects == nsect
&& (mdata->nsects == 0 || mdata->sections != NULL))
return true;
/* We need to check that this can be done... */
if (nsect > 255)
{
_bfd_error_handler (_("mach-o: there are too many sections (%u)"
" maximum is 255,\n"), nsect);
return false;
}
mdata->nsects = nsect;
amt = mdata->nsects * sizeof (bfd_mach_o_section *);
mdata->sections = bfd_alloc (abfd, amt);
if (mdata->sections == NULL)
return false;
/* Create Mach-O sections.
Section type, attribute and align should have been set when the
section was created - either read in or specified. */
target_index = 0;
for (sec = abfd->sections; sec; sec = sec->next)
{
unsigned bfd_align = bfd_section_alignment (sec);
bfd_mach_o_section *msect = bfd_mach_o_get_mach_o_section (sec);
mdata->sections[target_index] = msect;
msect->addr = bfd_section_vma (sec);
msect->size = bfd_section_size (sec);
/* Use the largest alignment set, in case it was bumped after the
section was created. */
msect->align = msect->align > bfd_align ? msect->align : bfd_align;
msect->offset = 0;
sec->target_index = ++target_index;
}
return true;
}
bool
bfd_mach_o_write_contents (bfd *abfd)
{
bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
bfd_mach_o_load_command *cmd;
bfd_mach_o_symtab_command *symtab = NULL;
bfd_mach_o_dysymtab_command *dysymtab = NULL;
bfd_mach_o_segment_command *linkedit = NULL;
/* Make the commands, if not already present. */
if (!abfd->output_has_begun && !bfd_mach_o_build_commands (abfd))
return false;
abfd->output_has_begun = true;
/* Write the header. */
if (!bfd_mach_o_write_header (abfd, &mdata->header))
return false;
/* First pass: allocate the linkedit segment. */
for (cmd = mdata->first_command; cmd != NULL; cmd = cmd->next)
switch (cmd->type)
{
case BFD_MACH_O_LC_SEGMENT_64:
case BFD_MACH_O_LC_SEGMENT:
if (strcmp (cmd->command.segment.segname, "__LINKEDIT") == 0)
linkedit = &cmd->command.segment;
break;
case BFD_MACH_O_LC_SYMTAB:
symtab = &cmd->command.symtab;
break;
case BFD_MACH_O_LC_DYSYMTAB:
dysymtab = &cmd->command.dysymtab;
break;
case BFD_MACH_O_LC_DYLD_INFO:
{
bfd_mach_o_dyld_info_command *di = &cmd->command.dyld_info;
di->rebase_off = di->rebase_size != 0 ? mdata->filelen : 0;
mdata->filelen += di->rebase_size;
di->bind_off = di->bind_size != 0 ? mdata->filelen : 0;
mdata->filelen += di->bind_size;
di->weak_bind_off = di->weak_bind_size != 0 ? mdata->filelen : 0;
mdata->filelen += di->weak_bind_size;
di->lazy_bind_off = di->lazy_bind_size != 0 ? mdata->filelen : 0;
mdata->filelen += di->lazy_bind_size;
di->export_off = di->export_size != 0 ? mdata->filelen : 0;
mdata->filelen += di->export_size;
}
break;
case BFD_MACH_O_LC_LOAD_DYLIB:
case BFD_MACH_O_LC_LOAD_DYLINKER:
case BFD_MACH_O_LC_MAIN:
/* Nothing to do. */
break;
default:
_bfd_error_handler
(_("unable to allocate data for load command %#x"),
cmd->type);
break;
}
/* Specially handle symtab and dysymtab. */
/* Pre-allocate the symbol table (but not the string table). The reason
is that the dysymtab is after the symbol table but before the string
table (required by the native strip tool). */
if (symtab != NULL)
{
unsigned int symlen;
unsigned int wide = bfd_mach_o_wide_p (abfd);
symlen = wide ? BFD_MACH_O_NLIST_64_SIZE : BFD_MACH_O_NLIST_SIZE;
/* Align for symbols. */
mdata->filelen = FILE_ALIGN (mdata->filelen, wide ? 3 : 2);
symtab->symoff = mdata->filelen;
symtab->nsyms = bfd_get_symcount (abfd);
mdata->filelen += symtab->nsyms * symlen;
}
/* Build the dysymtab. */
if (dysymtab != NULL)
if (!bfd_mach_o_build_dysymtab (abfd, dysymtab))
return false;
/* Write symtab and strtab. */
if (symtab != NULL)
if (!bfd_mach_o_write_symtab_content (abfd, symtab))
return false;
/* Adjust linkedit size. */
if (linkedit != NULL)
{
/* bfd_vma pagemask = bfd_mach_o_get_backend_data (abfd)->page_size - 1; */
linkedit->vmsize = mdata->filelen - linkedit->fileoff;
/* linkedit->vmsize = (linkedit->vmsize + pagemask) & ~pagemask; */
linkedit->filesize = mdata->filelen - linkedit->fileoff;
linkedit->initprot = BFD_MACH_O_PROT_READ;
linkedit->maxprot = BFD_MACH_O_PROT_READ | BFD_MACH_O_PROT_WRITE
| BFD_MACH_O_PROT_EXECUTE;
}
/* Second pass: write commands. */
for (cmd = mdata->first_command; cmd != NULL; cmd = cmd->next)
{
struct mach_o_load_command_external raw;
unsigned long typeflag;
typeflag = cmd->type | (cmd->type_required ? BFD_MACH_O_LC_REQ_DYLD : 0);
bfd_h_put_32 (abfd, typeflag, raw.cmd);
bfd_h_put_32 (abfd, cmd->len, raw.cmdsize);
if (bfd_seek (abfd, cmd->offset, SEEK_SET) != 0
|| bfd_write (&raw, BFD_MACH_O_LC_SIZE, abfd) != 8)
return false;
switch (cmd->type)
{
case BFD_MACH_O_LC_SEGMENT:
if (!bfd_mach_o_write_segment_32 (abfd, cmd))
return false;
break;
case BFD_MACH_O_LC_SEGMENT_64:
if (!bfd_mach_o_write_segment_64 (abfd, cmd))
return false;
break;
case BFD_MACH_O_LC_SYMTAB:
if (!bfd_mach_o_write_symtab (abfd, cmd))
return false;
break;
case BFD_MACH_O_LC_DYSYMTAB:
if (!bfd_mach_o_write_dysymtab (abfd, cmd))
return false;
break;
case BFD_MACH_O_LC_THREAD:
case BFD_MACH_O_LC_UNIXTHREAD:
if (!bfd_mach_o_write_thread (abfd, cmd))
return false;
break;
case BFD_MACH_O_LC_LOAD_DYLIB:
if (!bfd_mach_o_write_dylib (abfd, cmd))
return false;
break;
case BFD_MACH_O_LC_LOAD_DYLINKER:
if (!bfd_mach_o_write_dylinker (abfd, cmd))
return false;
break;
case BFD_MACH_O_LC_MAIN:
if (!bfd_mach_o_write_main (abfd, cmd))
return false;
break;
case BFD_MACH_O_LC_DYLD_INFO:
if (!bfd_mach_o_write_dyld_info (abfd, cmd))
return false;
break;
default:
_bfd_error_handler
(_("unable to write unknown load command %#x"),
cmd->type);
return false;
}
}
return true;
}
static void
bfd_mach_o_append_section_to_segment (bfd_mach_o_segment_command *seg,
bfd_mach_o_section *s)
{
if (seg->sect_head == NULL)
seg->sect_head = s;
else
seg->sect_tail->next = s;
seg->sect_tail = s;
}
/* Create section Mach-O flags from BFD flags. */
static void
bfd_mach_o_set_section_flags_from_bfd (bfd *abfd ATTRIBUTE_UNUSED,
asection *sec)
{
flagword bfd_flags;
bfd_mach_o_section *s = bfd_mach_o_get_mach_o_section (sec);
/* Create default flags. */
bfd_flags = bfd_section_flags (sec);
if ((bfd_flags & SEC_CODE) == SEC_CODE)
s->flags = BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS
| BFD_MACH_O_S_ATTR_SOME_INSTRUCTIONS
| BFD_MACH_O_S_REGULAR;
else if ((bfd_flags & (SEC_ALLOC | SEC_LOAD)) == SEC_ALLOC)
s->flags = BFD_MACH_O_S_ZEROFILL;
else if (bfd_flags & SEC_DEBUGGING)
s->flags = BFD_MACH_O_S_REGULAR | BFD_MACH_O_S_ATTR_DEBUG;
else
s->flags = BFD_MACH_O_S_REGULAR;
}
static bool
bfd_mach_o_build_obj_seg_command (bfd *abfd, bfd_mach_o_segment_command *seg)
{
bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
unsigned int i, j;
seg->vmaddr = 0;
seg->fileoff = mdata->filelen;
seg->initprot = BFD_MACH_O_PROT_READ | BFD_MACH_O_PROT_WRITE
| BFD_MACH_O_PROT_EXECUTE;
seg->maxprot = seg->initprot;
/* Append sections to the segment.
This is a little tedious, we have to honor the need to account zerofill
sections after all the rest. This forces us to do the calculation of
total vmsize in three passes so that any alignment increments are
properly accounted. */
for (i = 0; i < mdata->nsects; ++i)
{
bfd_mach_o_section *s = mdata->sections[i];
asection *sec = s->bfdsection;
/* Although we account for zerofill section sizes in vm order, they are
placed in the file in source sequence. */
bfd_mach_o_append_section_to_segment (seg, s);
s->offset = 0;
/* Zerofill sections have zero file size & offset, the only content
written to the file is the symbols. */
if ((s->flags & BFD_MACH_O_SECTION_TYPE_MASK) == BFD_MACH_O_S_ZEROFILL
|| ((s->flags & BFD_MACH_O_SECTION_TYPE_MASK)
== BFD_MACH_O_S_GB_ZEROFILL))
continue;
/* The Darwin system tools (in MH_OBJECT files, at least) always account
sections, even those with zero size. */
if (s->size > 0)
{
seg->vmsize = FILE_ALIGN (seg->vmsize, s->align);
seg->vmsize += s->size;
/* MH_OBJECT files have unaligned content. */
if (1)
{
seg->filesize = FILE_ALIGN (seg->filesize, s->align);
mdata->filelen = FILE_ALIGN (mdata->filelen, s->align);
}
seg->filesize += s->size;
/* The system tools write even zero-sized sections with an offset
field set to the current file position. */
s->offset = mdata->filelen;
}
sec->filepos = s->offset;
mdata->filelen += s->size;
}
/* Now pass through again, for zerofill, only now we just update the
vmsize, and then for zerofill_GB. */
for (j = 0; j < 2; j++)
{
unsigned int stype;
if (j == 0)
stype = BFD_MACH_O_S_ZEROFILL;
else
stype = BFD_MACH_O_S_GB_ZEROFILL;
for (i = 0; i < mdata->nsects; ++i)
{
bfd_mach_o_section *s = mdata->sections[i];
if ((s->flags & BFD_MACH_O_SECTION_TYPE_MASK) != stype)
continue;
if (s->size > 0)
{
seg->vmsize = FILE_ALIGN (seg->vmsize, s->align);
seg->vmsize += s->size;
}
}
}
/* Allocate space for the relocations. */
mdata->filelen = FILE_ALIGN (mdata->filelen, 2);