OSDN Git Service

2009-06-12 Tristan Gingold <gingold@adacore.com>
authorTristan Gingold <gingold@adacore.com>
Fri, 12 Jun 2009 09:15:45 +0000 (09:15 +0000)
committerTristan Gingold <gingold@adacore.com>
Fri, 12 Jun 2009 09:15:45 +0000 (09:15 +0000)
* reloc.c: Add BFD_RELOC_MACH_O_SECTDIFF and BFD_RELOC_MACH_O_PAIR.
* bfd-in2.h: Regenerated.
* libbfd.h: Regenerated.

* mach-o.c (FILE_ALIGN): New macro.
(bfd_mach_o_canonicalize_symtab): Adjust for bfd_mach_o_asymbol.
(bfd_mach_o_print_symbol): Ditto.
(bfd_mach_o_scan_write_thread): Adjust type of variable offset.
(bfd_mach_o_get_reloc_upper_bound): New function.
(bfd_mach_o_canonicalize_reloc): Ditto.
(bfd_mach_o_scan_write_relocs): Ditto.
(bfd_mach_o_scan_write_section_32): Remove offset argument.
(bfd_mach_o_scan_write_section_64): Ditto.
(bfd_mach_o_scan_write_segment_32): Write relocs.  Fix size of data
written for load command.  Do not compute section file offset.
(bfd_mach_o_scan_write_segment_64): Ditto.
(bfd_mach_o_scan_write_symtab): Compute file offset first.
Adjust for bfd_mach_o_asymbol.  Move code to convert from BFD to
Mach-O to ...
(bfd_mach_o_mangle_symbols): ... this new function.
(bfd_mach_o_write_contents): Be sure to habe load commands built.
Call bfd_mach_o_mangle_symbols.
(bfd_mach_o_build_commands): Adjust for filelen field.
Use FILE_ALIGN macro.  Clear section file offset if section is empty.
Do not set symtab file offset anymore here.
(bfd_mach_o_make_empty_symbol): Allocate a bfd_mach_o_asymbol.
(bfd_mach_o_make_bfd_section): Set SEC_RELOC if has reloc.
Set reloc_count and rel_filepos section fields.
(bfd_mach_o_scan_read_section_32): Type of argument offset is now
unsigned int.
(bfd_mach_o_scan_read_section_64): Ditto.
(bfd_mach_o_scan_read_section): Ditto.
(bfd_mach_o_scan_read_symtab_symbol): Typr of argument s is now
bfd_mach_o_asymbol.  Use unsigned int for file offset type.
(bfd_mach_o_scan_read_symtab_symbol): Adjust for bfd_mach_o_asymbol.
Handle weak-ref symbols.
(bfd_mach_o_scan_read_symtab_symbols): Adjust for bfd_mach_o_asymbol.
(bfd_mach_o_scan_read_dysymtab_symbol): Ditto.
(bfd_mach_o_scan): Remove assignment to removed field.
(bfd_mach_o_mkobject_init): Ditto.
(bfd_mach_o_bfd_print_private_bfd_data): Adjust printf formatter.
(bfd_mach_o_bfd_reloc_type_lookup): New macro.
(bfd_mach_o_bfd_reloc_name_lookup): Ditto.
(bfd_mach_o_swap_reloc_in): Ditto.
(bfd_mach_o_swap_reloc_out): Ditto.

* mach-o.h (BFD_MACH_O_REFERENCE_FLAG_UNDEFINED_NON_LAZY)
(BFD_MACH_O_REFERENCE_FLAG_UNDEFINED_LAZY)
(BFD_MACH_O_REFERENCE_FLAG_DEFINED)
(BFD_MACH_O_REFERENCE_FLAG_PRIVATE_DEFINED)
(BFD_MACH_O_REFERENCE_FLAG_PRIVATE_UNDEFINED_NON_LAZY)
(BFD_MACH_O_REFERENCE_FLAG_PRIVATE_UNDEFINED_LAZY)
(BFD_MACH_O_REFERENCED_DYNAMICALLY, BFD_MACH_O_N_DESC_DISCARDED)
(BFD_MACH_O_N_NO_DEAD_STRIP, BFD_MACH_O_N_WEAK_REF)
(BFD_MACH_O_N_WEAK_DEF): New macros.
Add comments.
(bfd_mach_o_section_type): Add new enumeration values.
(BFD_MACH_O_S_SELF_MODIFYING_CODE)
(BFD_MACH_O_S_ATTR_LIVE_SUPPORT)
(BFD_MACH_O_S_ATTR_NO_DEAD_STRIP)
(BFD_MACH_O_S_ATTR_STRIP_STATIC_SYMS, BFD_MACH_O_S_ATTR_NO_TOC): New
macros.
(BFD_MACH_O_GENERIC_RELOC_VANILLA)
(BFD_MACH_O_GENERIC_RELOC_PAIR)
(BFD_MACH_O_GENERIC_RELOC_SECTDIFF)
(BFD_MACH_O_GENERIC_RELOC_PB_LA_PTR)
(BFD_MACH_O_GENERIC_RELOC_LOCAL_SECTDIFF): Ditto.
(BFD_MACH_O_RELENT_SIZE): Ditto.
(BFD_MACH_O_R_PCREL, BFD_MACH_O_GET_R_LENGTH)
(BFD_MACH_O_R_EXTERN, BFD_MACH_O_GET_R_TYPE)
(BFD_MACH_O_GET_R_SYMBOLNUM, BFD_MACH_O_SET_R_LENGTH)
(BFD_MACH_O_SET_R_TYPE, BFD_MACH_O_SET_R_SYMBOLNUM): Ditto.
(BFD_MACH_O_SR_SCATTERED, BFD_MACH_O_SR_PCREL)
(BFD_MACH_O_GET_SR_LENGTH, BFD_MACH_O_GET_SR_TYPE)
(BFD_MACH_O_GET_SR_ADDRESS, BFD_MACH_O_SET_SR_LENGTH)
(BFD_MACH_O_SET_SR_TYPE, BFD_MACH_O_SET_SR_ADDRESS): Ditto.
(bfd_mach_o_reloc_info): New struct.
(bfd_mach_o_asymbol): New struct.
(bfd_mach_o_symtab_command): The symbols field is now of type
bfd_mach_o_asymbol.
(bfd_mach_o_load_command): The offset and len fields are now of type
unsigned int.
(mach_o_data_struct): Remove symbols, nsymbols and ibfd fields.
Add a filelen field.  Add comments.
(bfd_mach_o_backend_data): New struct.
(bfd_mach_o_get_backend_data): New macro.
(bfd_mach_o_scan_read_symtab_symbol): Parameter symbol now of type
bfd_mach_o_asymbol.
(bfd_mach_o_scan_read_dysymtab_symbol): Ditto.
(bfd_mach_o_get_reloc_upper_bound): New prototype.
(bfd_mach_o_canonicalize_reloc): Ditto.

* mach-o-target.c (bfd_mach_o_bfd_is_local_label_name): Remove
duplicated macro.  Now defined to _bfd_generic_is_local_label.
(TARGET_NAME_BACKEND): New macro...
(TARGET_NAME_BACKEND): ... used to name a new variable.
(TARGET_NAME): Define relocs table using bfd_mach_o, reference
Mach-O backend data.

* mach-o-i386.c (i386_howto_table): New variable.
(bfd_mach_o_i386_swap_reloc_in): New function.
(bfd_mach_o_i386_swap_reloc_out): Ditto.
(bfd_mach_o_i386_bfd_reloc_type_lookup): Ditto.
(bfd_mach_o_i386_bfd_reloc_name_lookup): Ditto.
(bfd_mach_o_swap_reloc_in, bfd_mach_o_swap_reloc_out): New macros.
(bfd_mach_o_bfd_reloc_type_lookup)
(bfd_mach_o_bfd_reloc_name_lookup): Ditto.

bfd/ChangeLog
bfd/bfd-in2.h
bfd/libbfd.h
bfd/mach-o-i386.c
bfd/mach-o-target.c
bfd/mach-o.c
bfd/mach-o.h
bfd/reloc.c

index e5e3d97..c0002d0 100644 (file)
@@ -1,3 +1,113 @@
+2009-06-12  Tristan Gingold  <gingold@adacore.com>
+
+       * reloc.c: Add BFD_RELOC_MACH_O_SECTDIFF and BFD_RELOC_MACH_O_PAIR.
+       * bfd-in2.h: Regenerated.
+       * libbfd.h: Regenerated.
+
+       * mach-o.c (FILE_ALIGN): New macro.
+       (bfd_mach_o_canonicalize_symtab): Adjust for bfd_mach_o_asymbol.
+       (bfd_mach_o_print_symbol): Ditto.
+       (bfd_mach_o_scan_write_thread): Adjust type of variable offset.
+       (bfd_mach_o_get_reloc_upper_bound): New function.
+       (bfd_mach_o_canonicalize_reloc): Ditto.
+       (bfd_mach_o_scan_write_relocs): Ditto.
+       (bfd_mach_o_scan_write_section_32): Remove offset argument.
+       (bfd_mach_o_scan_write_section_64): Ditto.
+       (bfd_mach_o_scan_write_segment_32): Write relocs.  Fix size of data
+       written for load command.  Do not compute section file offset.
+       (bfd_mach_o_scan_write_segment_64): Ditto.
+       (bfd_mach_o_scan_write_symtab): Compute file offset first.
+       Adjust for bfd_mach_o_asymbol.  Move code to convert from BFD to
+       Mach-O to ...
+       (bfd_mach_o_mangle_symbols): ... this new function.
+       (bfd_mach_o_write_contents): Be sure to habe load commands built.
+       Call bfd_mach_o_mangle_symbols.
+       (bfd_mach_o_build_commands): Adjust for filelen field.
+       Use FILE_ALIGN macro.  Clear section file offset if section is empty.
+       Do not set symtab file offset anymore here.
+       (bfd_mach_o_make_empty_symbol): Allocate a bfd_mach_o_asymbol.
+       (bfd_mach_o_make_bfd_section): Set SEC_RELOC if has reloc.
+       Set reloc_count and rel_filepos section fields.
+       (bfd_mach_o_scan_read_section_32): Type of argument offset is now
+       unsigned int.
+       (bfd_mach_o_scan_read_section_64): Ditto.
+       (bfd_mach_o_scan_read_section): Ditto.
+       (bfd_mach_o_scan_read_symtab_symbol): Typr of argument s is now
+       bfd_mach_o_asymbol.  Use unsigned int for file offset type.
+       (bfd_mach_o_scan_read_symtab_symbol): Adjust for bfd_mach_o_asymbol.
+       Handle weak-ref symbols.
+       (bfd_mach_o_scan_read_symtab_symbols): Adjust for bfd_mach_o_asymbol.
+       (bfd_mach_o_scan_read_dysymtab_symbol): Ditto.
+       (bfd_mach_o_scan): Remove assignment to removed field.
+       (bfd_mach_o_mkobject_init): Ditto.
+       (bfd_mach_o_bfd_print_private_bfd_data): Adjust printf formatter.
+       (bfd_mach_o_bfd_reloc_type_lookup): New macro.
+       (bfd_mach_o_bfd_reloc_name_lookup): Ditto.
+       (bfd_mach_o_swap_reloc_in): Ditto.
+       (bfd_mach_o_swap_reloc_out): Ditto.
+
+       * mach-o.h (BFD_MACH_O_REFERENCE_FLAG_UNDEFINED_NON_LAZY)
+       (BFD_MACH_O_REFERENCE_FLAG_UNDEFINED_LAZY)
+       (BFD_MACH_O_REFERENCE_FLAG_DEFINED)
+       (BFD_MACH_O_REFERENCE_FLAG_PRIVATE_DEFINED)
+       (BFD_MACH_O_REFERENCE_FLAG_PRIVATE_UNDEFINED_NON_LAZY)
+       (BFD_MACH_O_REFERENCE_FLAG_PRIVATE_UNDEFINED_LAZY)
+       (BFD_MACH_O_REFERENCED_DYNAMICALLY, BFD_MACH_O_N_DESC_DISCARDED)
+       (BFD_MACH_O_N_NO_DEAD_STRIP, BFD_MACH_O_N_WEAK_REF)
+       (BFD_MACH_O_N_WEAK_DEF): New macros.
+       Add comments.
+       (bfd_mach_o_section_type): Add new enumeration values.
+       (BFD_MACH_O_S_SELF_MODIFYING_CODE)
+       (BFD_MACH_O_S_ATTR_LIVE_SUPPORT)
+       (BFD_MACH_O_S_ATTR_NO_DEAD_STRIP)
+       (BFD_MACH_O_S_ATTR_STRIP_STATIC_SYMS, BFD_MACH_O_S_ATTR_NO_TOC): New
+       macros.
+       (BFD_MACH_O_GENERIC_RELOC_VANILLA)
+       (BFD_MACH_O_GENERIC_RELOC_PAIR)
+       (BFD_MACH_O_GENERIC_RELOC_SECTDIFF)
+       (BFD_MACH_O_GENERIC_RELOC_PB_LA_PTR)
+       (BFD_MACH_O_GENERIC_RELOC_LOCAL_SECTDIFF): Ditto.
+       (BFD_MACH_O_RELENT_SIZE): Ditto.
+       (BFD_MACH_O_R_PCREL, BFD_MACH_O_GET_R_LENGTH)
+       (BFD_MACH_O_R_EXTERN, BFD_MACH_O_GET_R_TYPE)
+       (BFD_MACH_O_GET_R_SYMBOLNUM, BFD_MACH_O_SET_R_LENGTH)
+       (BFD_MACH_O_SET_R_TYPE, BFD_MACH_O_SET_R_SYMBOLNUM): Ditto.
+       (BFD_MACH_O_SR_SCATTERED, BFD_MACH_O_SR_PCREL)
+       (BFD_MACH_O_GET_SR_LENGTH, BFD_MACH_O_GET_SR_TYPE)
+       (BFD_MACH_O_GET_SR_ADDRESS, BFD_MACH_O_SET_SR_LENGTH)
+       (BFD_MACH_O_SET_SR_TYPE, BFD_MACH_O_SET_SR_ADDRESS): Ditto.
+       (bfd_mach_o_reloc_info): New struct.
+       (bfd_mach_o_asymbol): New struct.
+       (bfd_mach_o_symtab_command): The symbols field is now of type
+       bfd_mach_o_asymbol.
+       (bfd_mach_o_load_command): The offset and len fields are now of type
+       unsigned int.
+       (mach_o_data_struct): Remove symbols, nsymbols and ibfd fields.
+       Add a filelen field.  Add comments.
+       (bfd_mach_o_backend_data): New struct.
+       (bfd_mach_o_get_backend_data): New macro.
+       (bfd_mach_o_scan_read_symtab_symbol): Parameter symbol now of type
+       bfd_mach_o_asymbol.
+       (bfd_mach_o_scan_read_dysymtab_symbol): Ditto.
+       (bfd_mach_o_get_reloc_upper_bound): New prototype.
+       (bfd_mach_o_canonicalize_reloc): Ditto.
+       
+       * mach-o-target.c (bfd_mach_o_bfd_is_local_label_name): Remove
+       duplicated macro.  Now defined to _bfd_generic_is_local_label.
+       (TARGET_NAME_BACKEND): New macro...
+       (TARGET_NAME_BACKEND): ... used to name a new variable.
+       (TARGET_NAME): Define relocs table using bfd_mach_o, reference
+       Mach-O backend data.
+
+       * mach-o-i386.c (i386_howto_table): New variable.
+       (bfd_mach_o_i386_swap_reloc_in): New function.
+       (bfd_mach_o_i386_swap_reloc_out): Ditto.
+       (bfd_mach_o_i386_bfd_reloc_type_lookup): Ditto.
+       (bfd_mach_o_i386_bfd_reloc_name_lookup): Ditto.
+       (bfd_mach_o_swap_reloc_in, bfd_mach_o_swap_reloc_out): New macros.
+       (bfd_mach_o_bfd_reloc_type_lookup)
+       (bfd_mach_o_bfd_reloc_name_lookup): Ditto.
+
 2009-06-11  Anthony Green  <green@moxielogic.com>
 
        * reloc.c: Add BFD_RELOC_MOXIE_10_PCREL.
index 4baa1a4..9b98f8c 100644 (file)
@@ -4499,6 +4499,13 @@ BFD_RELOC_XTENSA_ASM_EXPAND.  */
   BFD_RELOC_LM32_GLOB_DAT,
   BFD_RELOC_LM32_JMP_SLOT,
   BFD_RELOC_LM32_RELATIVE,
+
+/* Difference between two section addreses.  Must be followed by a
+BFD_RELOC_MACH_O_PAIR.  */
+  BFD_RELOC_MACH_O_SECTDIFF,
+
+/* Mach-O generic relocations.  */
+  BFD_RELOC_MACH_O_PAIR,
   BFD_RELOC_UNUSED };
 typedef enum bfd_reloc_code_real bfd_reloc_code_real_type;
 reloc_howto_type *bfd_reloc_type_lookup
index a3bb580..d0499e8 100644 (file)
@@ -2069,6 +2069,8 @@ static const char *const bfd_reloc_code_real_names[] = { "@@uninitialized@@",
   "BFD_RELOC_LM32_GLOB_DAT",
   "BFD_RELOC_LM32_JMP_SLOT",
   "BFD_RELOC_LM32_RELATIVE",
+  "BFD_RELOC_MACH_O_SECTDIFF",
+  "BFD_RELOC_MACH_O_PAIR",
  "@@overflow: BFD_RELOC_UNUSED@@",
 };
 #endif
index 7d389f5..f600c01 100644 (file)
@@ -60,9 +60,171 @@ bfd_mach_o_i386_mkobject (bfd *abfd)
   return TRUE;
 }
 
+static reloc_howto_type i386_howto_table[]=
+{
+  HOWTO(BFD_RELOC_32, 0, 2, 32, FALSE, 0,
+       complain_overflow_bitfield,
+       NULL, "32",
+       FALSE, 0xffffffff, 0xffffffff, FALSE),
+  HOWTO(BFD_RELOC_16, 0, 1, 16, FALSE, 0,
+       complain_overflow_bitfield,
+       NULL, "16",
+       FALSE, 0xffff, 0xffff, FALSE),
+  HOWTO(BFD_RELOC_8, 0, 0, 8, FALSE, 0,
+       complain_overflow_bitfield,
+       NULL, "8",
+       FALSE, 0xff, 0xff, FALSE),
+  HOWTO(BFD_RELOC_32_PCREL, 0, 2, 32, TRUE, 0,
+       complain_overflow_bitfield,
+       NULL, "DISP32",
+       FALSE, 0xffffffff, 0xffffffff, TRUE),
+  HOWTO(BFD_RELOC_16_PCREL, 0, 1, 16, TRUE, 0,
+       complain_overflow_bitfield,
+       NULL, "DISP16",
+       FALSE, 0xffff, 0xffff, TRUE),
+  HOWTO(BFD_RELOC_MACH_O_SECTDIFF, 0, 2, 32, FALSE, 0,
+       complain_overflow_bitfield,
+       NULL, "SECTDIFF_32",
+       FALSE, 0xffffffff, 0xffffffff, FALSE),
+  HOWTO(BFD_RELOC_MACH_O_PAIR, 0, 2, 32, FALSE, 0,
+       complain_overflow_bitfield,
+       NULL, "PAIR_32",
+       FALSE, 0xffffffff, 0xffffffff, FALSE),
+};
+
+static bfd_boolean
+bfd_mach_o_i386_swap_reloc_in (arelent *res, bfd_mach_o_reloc_info *reloc)
+{
+  if (reloc->r_scattered)
+    {
+      switch (reloc->r_type)
+        {
+        case BFD_MACH_O_GENERIC_RELOC_PAIR:
+          if (reloc->r_length != 2)
+            return FALSE;
+          res->howto = &i386_howto_table[3];
+          res->address = res[-1].address;
+          return TRUE;
+        case BFD_MACH_O_GENERIC_RELOC_SECTDIFF:
+        case BFD_MACH_O_GENERIC_RELOC_LOCAL_SECTDIFF:
+          if (reloc->r_length != 2)
+            return FALSE;
+          res->howto = &i386_howto_table[2];
+          return TRUE;
+        default:
+          return FALSE;
+        }
+    }
+  else
+    {
+      switch (reloc->r_type)
+        {
+        case BFD_MACH_O_GENERIC_RELOC_VANILLA:
+          switch ((reloc->r_length << 1) | reloc->r_pcrel)
+            {
+            case 0: /* len = 0, pcrel = 0  */
+              res->howto = &i386_howto_table[2];
+              return TRUE;
+            case 2: /* len = 1, pcrel = 0  */
+              res->howto = &i386_howto_table[1];
+              return TRUE;
+            case 3: /* len = 1, pcrel = 1  */
+              res->howto = &i386_howto_table[4];
+              return TRUE;
+            case 4: /* len = 2, pcrel = 0  */
+              res->howto = &i386_howto_table[0];
+              return TRUE;
+            case 5: /* len = 2, pcrel = 1  */
+              res->howto = &i386_howto_table[3];
+              return TRUE;
+            default:
+              return FALSE;
+            }
+          break;
+        default:
+          return FALSE;
+        }
+    }
+}
+
+static bfd_boolean
+bfd_mach_o_i386_swap_reloc_out (arelent *rel, bfd_mach_o_reloc_info *rinfo)
+{
+  rinfo->r_address = rel->address;
+  switch (rel->howto->type)
+    {
+    case BFD_RELOC_32:
+    case BFD_RELOC_32_PCREL:
+    case BFD_RELOC_16:
+    case BFD_RELOC_16_PCREL:
+    case BFD_RELOC_8:
+      rinfo->r_scattered = 0;
+      rinfo->r_type = BFD_MACH_O_GENERIC_RELOC_VANILLA;
+      rinfo->r_pcrel = rel->howto->pc_relative;
+      rinfo->r_length = rel->howto->size; /* Correct in practice.  */
+      if ((*rel->sym_ptr_ptr)->flags & BSF_SECTION_SYM)
+        {
+          rinfo->r_extern = 0;
+          rinfo->r_value = (*rel->sym_ptr_ptr)->section->target_index;
+        }
+      else
+        {
+          rinfo->r_extern = 1;
+          rinfo->r_value = (*rel->sym_ptr_ptr)->udata.i;
+        }
+      break;
+    case BFD_RELOC_MACH_O_SECTDIFF:
+      rinfo->r_scattered = 1;
+      rinfo->r_type = BFD_MACH_O_GENERIC_RELOC_SECTDIFF;
+      rinfo->r_pcrel = 0;
+      rinfo->r_length = 2;
+      rinfo->r_extern = 0;
+      rinfo->r_value = (*rel->sym_ptr_ptr)->value 
+        + (*rel->sym_ptr_ptr)->section->vma;
+      break;
+    case BFD_RELOC_MACH_O_PAIR:
+      rinfo->r_address = 0;
+      rinfo->r_scattered = 1;
+      rinfo->r_type = BFD_MACH_O_GENERIC_RELOC_PAIR;
+      rinfo->r_pcrel = 0;
+      rinfo->r_length = 2;
+      rinfo->r_extern = 0;
+      rinfo->r_value = (*rel->sym_ptr_ptr)->value 
+        + (*rel->sym_ptr_ptr)->section->vma;
+      break;
+    default:
+      return FALSE;
+    }
+  return TRUE;
+}
+
+static reloc_howto_type *
+bfd_mach_o_i386_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
+                                       bfd_reloc_code_real_type code)
+{
+  unsigned int i;
+
+  for (i = 0; i < sizeof (i386_howto_table) / sizeof (*i386_howto_table); i++)
+    if (code == i386_howto_table[i].type)
+      return &i386_howto_table[i];
+  return NULL;
+}
+
+static reloc_howto_type *
+bfd_mach_o_i386_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
+                                       const char *name ATTRIBUTE_UNUSED)
+{
+  return NULL;
+}
+
+#define bfd_mach_o_swap_reloc_in bfd_mach_o_i386_swap_reloc_in
+#define bfd_mach_o_swap_reloc_out bfd_mach_o_i386_swap_reloc_out
+
+#define bfd_mach_o_bfd_reloc_type_lookup bfd_mach_o_i386_bfd_reloc_type_lookup 
+#define bfd_mach_o_bfd_reloc_name_lookup bfd_mach_o_i386_bfd_reloc_name_lookup
+
 #define TARGET_NAME            mach_o_i386_vec
 #define TARGET_STRING          "mach-o-i386"
 #define TARGET_BIG_ENDIAN      0
 #define TARGET_ARCHIVE                 0
-
 #include "mach-o-target.c"
index bf68f93..f203382 100644 (file)
@@ -39,9 +39,8 @@
 #define bfd_mach_o_bfd_free_cached_info               _bfd_generic_bfd_free_cached_info
 #define bfd_mach_o_new_section_hook                   _bfd_generic_new_section_hook
 #define bfd_mach_o_get_section_contents_in_window     _bfd_generic_get_section_contents_in_window
-#define bfd_mach_o_bfd_is_local_label_name            _bfd_nosymbols_bfd_is_local_label_name
 #define bfd_mach_o_bfd_is_target_special_symbol       ((bfd_boolean (*) (bfd *, asymbol *)) bfd_false)
-#define bfd_mach_o_bfd_is_local_label_name            _bfd_nosymbols_bfd_is_local_label_name
+#define bfd_mach_o_bfd_is_local_label_name            bfd_generic_is_local_label_name
 #define bfd_mach_o_get_lineno                         _bfd_nosymbols_get_lineno
 #define bfd_mach_o_find_nearest_line                  _bfd_nosymbols_find_nearest_line
 #define bfd_mach_o_find_inliner_info                  _bfd_nosymbols_find_inliner_info
@@ -69,6 +68,8 @@
 #define bfd_mach_o_bfd_copy_private_header_data       _bfd_generic_bfd_copy_private_header_data
 #define bfd_mach_o_core_file_matches_executable_p     generic_core_file_matches_executable_p
 
+#define TARGET_NAME_BACKEND XCONCAT2(TARGET_NAME,_backend)
+
 #endif /* MACH_O_TARGET_COMMON_DEFINED */
 
 #ifndef TARGET_NAME
 #error Mach-O fat files must always be big-endian.
 #endif /* ((TARGET_ARCHIVE) && (! TARGET_BIG_ENDIAN)) */
 
+static const bfd_mach_o_backend_data TARGET_NAME_BACKEND =
+{
+  bfd_mach_o_swap_reloc_in,
+  bfd_mach_o_swap_reloc_out
+};
+
 const bfd_target TARGET_NAME =
 {
   TARGET_STRING,               /* Name.  */
@@ -162,13 +169,15 @@ const bfd_target TARGET_NAME =
   BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_bsd),
 #endif
   BFD_JUMP_TABLE_SYMBOLS (bfd_mach_o),
-  BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
+  BFD_JUMP_TABLE_RELOCS (bfd_mach_o),
   BFD_JUMP_TABLE_WRITE (bfd_mach_o),
   BFD_JUMP_TABLE_LINK (bfd_mach_o),
   BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
 
+  /* Alternative endian target.  */
   NULL,
 
-  NULL
+  /* Back-end data.  */
+  &TARGET_NAME_BACKEND
 };
 
index b01d785..d8e702e 100644 (file)
@@ -31,6 +31,9 @@
 #define bfd_mach_o_core_p bfd_mach_o_gen_core_p
 #define bfd_mach_o_mkobject bfd_false
 
+#define FILE_ALIGN(off, algn) \
+  (((off) + ((file_ptr) 1 << (algn)) - 1) & ((file_ptr) -1 << (algn)))
+
 unsigned int
 bfd_mach_o_version (bfd *abfd)
 {
@@ -351,7 +354,7 @@ bfd_mach_o_canonicalize_symtab (bfd *abfd, asymbol **alocation)
          for (j = 0; j < sym->nsyms; j++)
            {
              BFD_ASSERT (csym < (alocation + nsyms));
-             *csym++ = &sym->symbols[j];
+             *csym++ = &sym->symbols[j].symbol;
            }
        }
     }
@@ -376,10 +379,8 @@ bfd_mach_o_print_symbol (bfd *abfd,
                         bfd_print_symbol_type how)
 {
   FILE *file = (FILE *) afile;
-  unsigned char ntype;
-  unsigned char nsect;
-  unsigned int ndesc;
   const char *name;
+  bfd_mach_o_asymbol *asym = (bfd_mach_o_asymbol *)symbol;
 
   switch (how)
     {
@@ -388,13 +389,10 @@ bfd_mach_o_print_symbol (bfd *abfd,
       break;
     default:
       bfd_print_symbol_vandf (abfd, (PTR) file, symbol);
-      ntype = BFD_MACH_O_SYM_NTYPE (symbol);
-      nsect = BFD_MACH_O_SYM_NSECT (symbol);
-      ndesc = BFD_MACH_O_SYM_NDESC (symbol);
-      if (ntype & BFD_MACH_O_N_STAB)
-       name = bfd_get_stab_name (ntype);
+      if (asym->n_type & BFD_MACH_O_N_STAB)
+       name = bfd_get_stab_name (asym->n_type);
       else
-       switch (ntype & BFD_MACH_O_N_TYPE)
+       switch (asym->n_type & BFD_MACH_O_N_TYPE)
          {
          case BFD_MACH_O_N_UNDF:
            name = "UND";
@@ -417,9 +415,10 @@ bfd_mach_o_print_symbol (bfd *abfd,
          }
       if (name == NULL)
        name = "";
-      fprintf (file, " %02x %-6s %02x %04x", ntype, name, nsect, ndesc);
-      if ((ntype & BFD_MACH_O_N_STAB) == 0
-         && (ntype & BFD_MACH_O_N_TYPE) == BFD_MACH_O_N_SECT)
+      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, " %-5s", symbol->section->name);
       fprintf (file, " %s", symbol->name);
     }
@@ -503,7 +502,7 @@ bfd_mach_o_scan_write_thread (bfd *abfd, bfd_mach_o_load_command *command)
   bfd_mach_o_thread_command *cmd = &command->command.thread;
   unsigned int i;
   unsigned char buf[8];
-  bfd_vma offset;
+  unsigned int offset;
   unsigned int nflavours;
 
   BFD_ASSERT ((command->type == BFD_MACH_O_LC_THREAD)
@@ -529,10 +528,192 @@ bfd_mach_o_scan_write_thread (bfd *abfd, bfd_mach_o_load_command *command)
   return 0;
 }
 
+long
+bfd_mach_o_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
+                                  asection *asect)
+{
+  return (asect->reloc_count + 1) * sizeof (arelent *);
+}
+
+long
+bfd_mach_o_canonicalize_reloc (bfd *abfd, asection *asect,
+                               arelent **rels, asymbol **syms)
+{
+  bfd_mach_o_data_struct *mdata = abfd->tdata.mach_o_data;
+  bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
+  unsigned long i;
+  arelent *res;
+  char *native_relocs;
+  bfd_size_type native_size;
+
+  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_swap_reloc_in == NULL)
+    return 0;
+
+  /* Allocate and read relocs.  */
+  native_size = asect->reloc_count * BFD_MACH_O_RELENT_SIZE;
+  native_relocs = bfd_malloc (native_size);
+  if (native_relocs == NULL)
+    return -1;
+
+  if (bfd_seek (abfd, asect->rel_filepos, SEEK_SET) != 0
+      || bfd_bread (native_relocs, native_size, abfd) != native_size)
+    {
+      free (native_relocs);
+      return -1;
+    }
+
+  res = bfd_malloc (asect->reloc_count * sizeof (arelent));
+  if (res == NULL)
+    {
+      free (native_relocs);
+      return -1;
+    }
+
+  for (i = 0; i < asect->reloc_count; i++)
+    {
+      char *buf = native_relocs + BFD_MACH_O_RELENT_SIZE * i;
+      bfd_mach_o_reloc_info reloc;
+      bfd_vma addr;
+      bfd_vma symnum;
+      asymbol **sym;
+
+      addr = bfd_get_32 (abfd, buf + 0);
+      symnum = bfd_get_32 (abfd, buf + 4);
+
+      if (addr & BFD_MACH_O_SR_SCATTERED)
+       {
+          unsigned int j;
+
+          /* Scattered relocation.
+             Extract section and offset from r_value.  */
+         res[i].sym_ptr_ptr = NULL;
+         res[i].addend = 0;
+          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[i].sym_ptr_ptr = sect->bfdsection->symbol_ptr_ptr;
+                  res[i].addend = symnum - sect->addr;
+                  break;
+                }
+            }
+         res[i].address = BFD_MACH_O_GET_SR_ADDRESS (addr);
+         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_scattered = 1;
+       }
+      else
+       {
+         unsigned int num = BFD_MACH_O_GET_R_SYMBOLNUM (symnum);
+         res[i].addend = 0;
+         res[i].address = addr;
+         if (symnum & BFD_MACH_O_R_EXTERN)
+           sym = syms + num;
+         else
+            {
+              BFD_ASSERT (num != 0);
+              BFD_ASSERT (num <= mdata->nsects);
+              sym = mdata->sections[num - 1]->bfdsection->symbol_ptr_ptr;
+            }
+         res[i].sym_ptr_ptr = sym;
+         reloc.r_type = BFD_MACH_O_GET_R_TYPE (symnum);
+         reloc.r_length = BFD_MACH_O_GET_R_LENGTH (symnum);
+         reloc.r_pcrel = (symnum & BFD_MACH_O_R_PCREL) ? 1 : 0;
+          reloc.r_scattered = 0;
+       }
+
+      if (!(*bed->_bfd_mach_o_swap_reloc_in)(&res[i], &reloc))
+        {
+          free (res);
+          free (native_relocs);
+          return -1;
+        }
+      rels[i] = &res[i];
+    }
+  asect->relocation = res;
+  free (native_relocs);
+  return i;
+}
+
+static bfd_boolean
+bfd_mach_o_scan_write_relocs (bfd *abfd, bfd_mach_o_section *section)
+{
+  bfd_mach_o_data_struct *mdata = bfd_get_mach_o_data (abfd);
+  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;
+
+  /* Allocate relocation room.  */
+  mdata->filelen = FILE_ALIGN(mdata->filelen, 2);
+  section->nreloc = sec->reloc_count;
+  sec->rel_filepos = mdata->filelen;
+  section->reloff = sec->rel_filepos;
+  mdata->filelen += sec->reloc_count * BFD_MACH_O_RELENT_SIZE;
+
+  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];
+      char buf[8];
+      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);
+          bfd_put_32 (abfd, v, buf);
+          bfd_put_32 (abfd, pinfo->r_value, buf + 4);
+        }
+      else
+        {
+          unsigned long v;
+
+          bfd_put_32 (abfd, pinfo->r_address, buf);
+          v = BFD_MACH_O_SET_R_SYMBOLNUM (pinfo->r_value)
+            | (pinfo->r_pcrel ? BFD_MACH_O_R_PCREL : 0)
+            | BFD_MACH_O_SET_R_LENGTH (pinfo->r_length)
+            | (pinfo->r_extern ? BFD_MACH_O_R_EXTERN : 0)
+            | BFD_MACH_O_SET_R_TYPE (pinfo->r_type);
+          bfd_put_32 (abfd, v, buf + 4);
+        }
+
+      if (bfd_bwrite ((PTR) buf, BFD_MACH_O_RELENT_SIZE, abfd)
+          != BFD_MACH_O_RELENT_SIZE)
+        return FALSE;
+    }
+  return TRUE;
+}
+
 static int
-bfd_mach_o_scan_write_section_32 (bfd *abfd,
-                                 bfd_mach_o_section *section,
-                                 bfd_vma offset)
+bfd_mach_o_scan_write_section_32 (bfd *abfd, bfd_mach_o_section *section)
 {
   unsigned char buf[BFD_MACH_O_SECTION_SIZE];
 
@@ -548,18 +729,15 @@ bfd_mach_o_scan_write_section_32 (bfd *abfd,
   bfd_h_put_32 (abfd, section->reserved1, buf + 60);
   bfd_h_put_32 (abfd, section->reserved2, buf + 64);
 
-  if (bfd_seek (abfd, offset, SEEK_SET) != 0
-      || (bfd_bwrite ((PTR) buf, BFD_MACH_O_SECTION_SIZE, abfd)
-          != BFD_MACH_O_SECTION_SIZE))
+  if (bfd_bwrite ((PTR) buf, BFD_MACH_O_SECTION_SIZE, abfd)
+      != BFD_MACH_O_SECTION_SIZE)
     return -1;
 
   return 0;
 }
 
 static int
-bfd_mach_o_scan_write_section_64 (bfd *abfd,
-                                 bfd_mach_o_section *section,
-                                 bfd_vma offset)
+bfd_mach_o_scan_write_section_64 (bfd *abfd, bfd_mach_o_section *section)
 {
   unsigned char buf[BFD_MACH_O_SECTION_64_SIZE];
 
@@ -576,9 +754,8 @@ bfd_mach_o_scan_write_section_64 (bfd *abfd,
   bfd_h_put_32 (abfd, section->reserved2, buf + 72);
   bfd_h_put_32 (abfd, section->reserved3, buf + 76);
 
-  if (bfd_seek (abfd, offset, SEEK_SET) != 0
-      || (bfd_bwrite ((PTR) buf, BFD_MACH_O_SECTION_64_SIZE, abfd)
-          != BFD_MACH_O_SECTION_64_SIZE))
+  if (bfd_bwrite ((PTR) buf, BFD_MACH_O_SECTION_64_SIZE, abfd)
+      != BFD_MACH_O_SECTION_64_SIZE)
     return -1;
 
   return 0;
@@ -593,8 +770,11 @@ bfd_mach_o_scan_write_segment_32 (bfd *abfd, bfd_mach_o_load_command *command)
 
   BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT);
 
-  memcpy (buf, seg->segname, 16);
+  for (i = 0; i < seg->nsects; i++)
+    if (!bfd_mach_o_scan_write_relocs (abfd, &seg->sections[i]))
+      return -1;
 
+  memcpy (buf, seg->segname, 16);
   bfd_h_put_32 (abfd, seg->vmaddr, buf + 16);
   bfd_h_put_32 (abfd, seg->vmsize, buf + 20);
   bfd_h_put_32 (abfd, seg->fileoff, buf + 24);
@@ -605,18 +785,14 @@ bfd_mach_o_scan_write_segment_32 (bfd *abfd, bfd_mach_o_load_command *command)
   bfd_h_put_32 (abfd, seg->flags, buf + 44);
   
   if (bfd_seek (abfd, command->offset + 8, SEEK_SET) != 0
-      || (bfd_bwrite ((PTR) buf, BFD_MACH_O_LC_SEGMENT_SIZE, abfd) 
-          != BFD_MACH_O_LC_SEGMENT_SIZE))
+      || (bfd_bwrite ((PTR) buf, BFD_MACH_O_LC_SEGMENT_SIZE - 8, abfd) 
+          != BFD_MACH_O_LC_SEGMENT_SIZE - 8))
     return -1;
 
   for (i = 0; i < seg->nsects; i++)
-    {
-      bfd_vma segoff = command->offset + BFD_MACH_O_LC_SEGMENT_SIZE
-        + (i * BFD_MACH_O_SECTION_SIZE);
+    if (bfd_mach_o_scan_write_section_32 (abfd, &seg->sections[i]))
+      return -1;
 
-      if (bfd_mach_o_scan_write_section_32 (abfd, &seg->sections[i], segoff))
-       return -1;
-    }
   return 0;
 }
 
@@ -629,8 +805,11 @@ bfd_mach_o_scan_write_segment_64 (bfd *abfd, bfd_mach_o_load_command *command)
 
   BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT_64);
 
-  memcpy (buf, seg->segname, 16);
+  for (i = 0; i < seg->nsects; i++)
+    if (!bfd_mach_o_scan_write_relocs (abfd, &seg->sections[i]))
+      return -1;
 
+  memcpy (buf, seg->segname, 16);
   bfd_h_put_64 (abfd, seg->vmaddr, buf + 16);
   bfd_h_put_64 (abfd, seg->vmsize, buf + 24);
   bfd_h_put_64 (abfd, seg->fileoff, buf + 32);
@@ -641,24 +820,21 @@ bfd_mach_o_scan_write_segment_64 (bfd *abfd, bfd_mach_o_load_command *command)
   bfd_h_put_32 (abfd, seg->flags, buf + 60);
 
   if (bfd_seek (abfd, command->offset + 8, SEEK_SET) != 0
-      || (bfd_bwrite ((PTR) buf, BFD_MACH_O_LC_SEGMENT_64_SIZE, abfd)
-          != BFD_MACH_O_LC_SEGMENT_64_SIZE))
+      || (bfd_bwrite ((PTR) buf, BFD_MACH_O_LC_SEGMENT_64_SIZE - 8, abfd)
+          != BFD_MACH_O_LC_SEGMENT_64_SIZE - 8))
     return -1;
 
   for (i = 0; i < seg->nsects; i++)
-    {
-      bfd_vma segoff = command->offset + BFD_MACH_O_LC_SEGMENT_64_SIZE
-        + (i * BFD_MACH_O_SECTION_64_SIZE);
+    if (bfd_mach_o_scan_write_section_64 (abfd, &seg->sections[i]))
+      return -1;
 
-      if (bfd_mach_o_scan_write_section_64 (abfd, &seg->sections[i], segoff))
-       return -1;
-    }
   return 0;
 }
 
 static bfd_boolean
 bfd_mach_o_scan_write_symtab (bfd *abfd, bfd_mach_o_load_command *command)
 {
+  bfd_mach_o_data_struct *mdata = bfd_get_mach_o_data (abfd);
   bfd_mach_o_symtab_command *sym = &command->command.symtab;
   unsigned char buf[16];
   unsigned long i;
@@ -670,10 +846,13 @@ bfd_mach_o_scan_write_symtab (bfd *abfd, bfd_mach_o_load_command *command)
   BFD_ASSERT (command->type == BFD_MACH_O_LC_SYMTAB);
 
   /* Write the symbols first.  */
+  mdata->filelen = FILE_ALIGN(mdata->filelen, wide ? 3 : 2);
+  sym->symoff = mdata->filelen;
   if (bfd_seek (abfd, sym->symoff, SEEK_SET) != 0)
     return FALSE;
 
   sym->nsyms = bfd_get_symcount (abfd);
+  mdata->filelen += sym->nsyms * symlen;
 
   strtab = _bfd_stringtab_init ();
   if (strtab == NULL)
@@ -682,68 +861,34 @@ bfd_mach_o_scan_write_symtab (bfd *abfd, bfd_mach_o_load_command *command)
   for (i = 0; i < sym->nsyms; i++)
     {
       unsigned char buf[16];
-      unsigned char ntype;
-      unsigned char nsect;
-      short ndesc;
       bfd_size_type index;
-      asymbol *s = symbols[i];
+      bfd_mach_o_asymbol *s = (bfd_mach_o_asymbol *)symbols[i];
 
-      /* Compute index.  */
+      /* Compute name index.  */
       /* An index of 0 always means the empty string.  */
-      if (s->name == 0 || s->name[0] == '\0')
+      if (s->symbol.name == 0 || s->symbol.name[0] == '\0')
         index = 0;
       else
         {
-          index = _bfd_stringtab_add (strtab, s->name, TRUE, FALSE);
+          index = _bfd_stringtab_add (strtab, s->symbol.name, TRUE, FALSE);
           if (index == (bfd_size_type) -1)
             goto err;
         }
-
-      /* Get back-end specific values.  */
-      ntype = BFD_MACH_O_SYM_NTYPE (s);
-      nsect = BFD_MACH_O_SYM_NSECT (s);
-      ndesc = BFD_MACH_O_SYM_NDESC (s);
-
-      if (ntype == BFD_MACH_O_N_UNDF && !(s->flags & BSF_DEBUGGING))
-        {
-          /* As genuine Mach-O symbols type shouldn't be N_UNDF (undefined
-             symbols should be N_UNDEF | N_EXT), we suppose the back-end
-             values haven't been set.  */
-          if (s->flags & (BSF_LOCAL | BSF_GLOBAL))
-            {
-              if (s->section == bfd_abs_section_ptr)
-                ntype = BFD_MACH_O_N_ABS;
-              else if (s->section == bfd_und_section_ptr)
-                ntype = BFD_MACH_O_N_UNDF;
-              else if (s->section == bfd_com_section_ptr)
-                ntype = BFD_MACH_O_N_UNDF | BFD_MACH_O_N_EXT;
-              else
-                ntype = BFD_MACH_O_N_SECT;
-
-              ntype |= (s->flags & BSF_GLOBAL) ? BFD_MACH_O_N_EXT : 0;
-            }
-        }
-
-      /* Compute section index.  */
-      if (s->section != bfd_abs_section_ptr
-          && s->section != bfd_und_section_ptr
-          && s->section != bfd_com_section_ptr)
-        nsect = s->section->target_index;
-
       bfd_h_put_32 (abfd, index, buf);
-      bfd_h_put_8 (abfd, ntype, buf + 4);
-      bfd_h_put_8 (abfd, nsect, buf + 5);
-      bfd_h_put_16 (abfd, ndesc, buf + 6);
+      bfd_h_put_8 (abfd, s->n_type, buf + 4);
+      bfd_h_put_8 (abfd, s->n_sect, buf + 5);
+      bfd_h_put_16 (abfd, s->n_desc, buf + 6);
       if (wide)
-        bfd_h_put_64 (abfd, s->section->vma + s->value, buf + 8);
+        bfd_h_put_64 (abfd, s->symbol.section->vma + s->symbol.value, buf + 8);
       else
-        bfd_h_put_32 (abfd, s->section->vma + s->value, buf + 8);
+        bfd_h_put_32 (abfd, s->symbol.section->vma + s->symbol.value, buf + 8);
 
       if (bfd_bwrite ((PTR) buf, symlen, abfd) != symlen)
         goto err;
     }
   sym->strsize = _bfd_stringtab_size (strtab);
-  sym->stroff = sym->symoff + sym->nsyms * symlen;
+  sym->stroff = mdata->filelen;
+  mdata->filelen += sym->strsize;
 
   if (_bfd_stringtab_emit (abfd, strtab) != TRUE)
     goto err;
@@ -766,12 +911,63 @@ bfd_mach_o_scan_write_symtab (bfd *abfd, bfd_mach_o_load_command *command)
   return FALSE;
 }
 
+/* Process the symbols and generate Mach-O specific fields.
+   Number them.  */
+
+static bfd_boolean
+bfd_mach_o_mangle_symbols (bfd *abfd)
+{
+  unsigned long i;
+  asymbol **symbols = bfd_get_outsymbols (abfd);
+
+  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_UNDF && !(s->symbol.flags & BSF_DEBUGGING))
+        {
+          /* As genuine Mach-O symbols type shouldn't be N_UNDF (undefined
+             symbols should be N_UNDEF | N_EXT), we suppose the back-end
+             values haven't been set.  */
+          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;
+            }
+          else if (s->symbol.section == bfd_com_section_ptr)
+            s->n_type = BFD_MACH_O_N_UNDF | BFD_MACH_O_N_EXT;
+          else
+            s->n_type = BFD_MACH_O_N_SECT;
+          
+          if (s->symbol.flags & BSF_GLOBAL)
+            s->n_type |= BFD_MACH_O_N_EXT;
+        }
+
+      /* Compute section index.  */
+      if (s->symbol.section != bfd_abs_section_ptr
+          && s->symbol.section != bfd_und_section_ptr
+          && s->symbol.section != bfd_com_section_ptr)
+        s->n_sect = s->symbol.section->target_index;
+
+      /* Number symbols.  */
+      s->symbol.udata.i = i;
+    }
+  return TRUE;
+}
+
 bfd_boolean
 bfd_mach_o_write_contents (bfd *abfd)
 {
   unsigned int i;
   bfd_mach_o_data_struct *mdata = abfd->tdata.mach_o_data;
 
+  if (mdata->header.ncmds == 0)
+    if (!bfd_mach_o_build_commands (abfd))
+      return FALSE;
+
   /* Now write header information.  */
   if (mdata->header.filetype == 0)
     {
@@ -785,6 +981,10 @@ bfd_mach_o_write_contents (bfd *abfd)
   if (!bfd_mach_o_write_header (abfd, &mdata->header))
     return FALSE;
 
+  /* Assign a number to each symbols.  */
+  if (!bfd_mach_o_mangle_symbols (abfd))
+    return FALSE;
+
   for (i = 0; i < mdata->header.ncmds; i++)
     {
       unsigned char buf[8];
@@ -857,7 +1057,6 @@ bfd_mach_o_build_commands (bfd *abfd)
   bfd_mach_o_segment_command *seg;
   bfd_mach_o_section *sections;
   asection *sec;
-  file_ptr filepos;
   bfd_mach_o_load_command *cmd;
   bfd_mach_o_load_command *symtab_cmd;
   int target_index;
@@ -898,7 +1097,7 @@ bfd_mach_o_build_commands (bfd *abfd)
     }
   cmd->type_required = FALSE;
   mdata->header.sizeofcmds = cmd->len;
-  filepos = cmd->offset + cmd->len;
+  mdata->filelen = cmd->offset + cmd->len;
 
   /* Set symtab command.  */
   symtab_cmd = &mdata->commands[1];
@@ -909,12 +1108,12 @@ bfd_mach_o_build_commands (bfd *abfd)
   symtab_cmd->type_required = FALSE;
   
   mdata->header.sizeofcmds += symtab_cmd->len;
-  filepos += symtab_cmd->len;
+  mdata->filelen += symtab_cmd->len;
 
   /* Fill segment command.  */
   memset (seg->segname, 0, sizeof (seg->segname));
   seg->vmaddr = 0;
-  seg->fileoff = filepos;
+  seg->fileoff = mdata->filelen;
   seg->filesize = 0;
   seg->maxprot = BFD_MACH_O_PROT_READ | BFD_MACH_O_PROT_WRITE
     | BFD_MACH_O_PROT_EXECUTE;
@@ -931,30 +1130,28 @@ bfd_mach_o_build_commands (bfd *abfd)
       sections->size = bfd_get_section_size (sec);
       sections->align = bfd_get_section_alignment (abfd, sec);
 
-      filepos = (filepos + ((file_ptr) 1 << sections->align) - 1)
-        & ((file_ptr) -1 << sections->align);
-      sections->offset = filepos;
+      if (sections->size != 0)
+        {
+          mdata->filelen = FILE_ALIGN (mdata->filelen, sections->align);
+          sections->offset = mdata->filelen;
+        }
+      else
+        sections->offset = 0;
       sections->reloff = 0;
       sections->nreloc = 0;
       sections->reserved1 = 0;
       sections->reserved2 = 0;
       sections->reserved3 = 0;
 
-      sec->filepos = filepos;
+      sec->filepos = sections->offset;
       sec->target_index = ++target_index;
 
-      filepos += sections->size;
+      mdata->filelen += sections->size;
       sections++;
     }
-  seg->filesize = filepos - seg->fileoff;
+  seg->filesize = mdata->filelen - seg->fileoff;
   seg->vmsize = seg->filesize;
 
-  /* Fill symtab command.
-     Note: we don't know the number of symbols.
-     Also, symtab is at the end since the length of the symbol table (and
-     string table) is not known.  */
-  symtab_cmd->command.symtab.symoff = filepos;
-  
   return TRUE;
 }
 
@@ -1000,10 +1197,11 @@ bfd_mach_o_make_empty_symbol (bfd *abfd)
 {
   asymbol *new;
 
-  new = bfd_zalloc (abfd, sizeof (* new));
+  new = bfd_zalloc (abfd, sizeof (bfd_mach_o_asymbol));
   if (new == NULL)
     return new;
   new->the_bfd = abfd;
+  new->udata.i = 0;
   return new;
 }
 
@@ -1103,6 +1301,9 @@ bfd_mach_o_make_bfd_section (bfd *abfd, bfd_mach_o_section *section,
            flags |= SEC_READONLY;
        }
     }
+  if (section->nreloc != 0)
+    flags |= SEC_RELOC;
+
   bfdsec = bfd_make_section_anyway_with_flags (abfd, sname, flags);
   if (bfdsec == NULL)
     return NULL;
@@ -1113,6 +1314,8 @@ bfd_mach_o_make_bfd_section (bfd *abfd, bfd_mach_o_section *section,
   bfdsec->filepos = section->offset;
   bfdsec->alignment_power = section->align;
   bfdsec->segment_mark = 0;
+  bfdsec->reloc_count = section->nreloc;
+  bfdsec->rel_filepos = section->reloff;
 
   return bfdsec;
 }
@@ -1120,7 +1323,7 @@ bfd_mach_o_make_bfd_section (bfd *abfd, bfd_mach_o_section *section,
 static int
 bfd_mach_o_scan_read_section_32 (bfd *abfd,
                                 bfd_mach_o_section *section,
-                                bfd_vma offset,
+                                unsigned int offset,
                                 unsigned long prot)
 {
   unsigned char buf[BFD_MACH_O_SECTION_SIZE];
@@ -1155,7 +1358,7 @@ bfd_mach_o_scan_read_section_32 (bfd *abfd,
 static int
 bfd_mach_o_scan_read_section_64 (bfd *abfd,
                                 bfd_mach_o_section *section,
-                                bfd_vma offset,
+                                unsigned int offset,
                                 unsigned long prot)
 {
   unsigned char buf[BFD_MACH_O_SECTION_64_SIZE];
@@ -1190,7 +1393,7 @@ bfd_mach_o_scan_read_section_64 (bfd *abfd,
 static int
 bfd_mach_o_scan_read_section (bfd *abfd,
                              bfd_mach_o_section *section,
-                             bfd_vma offset,
+                             unsigned int offset,
                              unsigned long prot,
                              unsigned int wide)
 {
@@ -1203,13 +1406,13 @@ bfd_mach_o_scan_read_section (bfd *abfd,
 int
 bfd_mach_o_scan_read_symtab_symbol (bfd *abfd,
                                    bfd_mach_o_symtab_command *sym,
-                                   asymbol *s,
+                                   bfd_mach_o_asymbol *s,
                                    unsigned long i)
 {
   bfd_mach_o_data_struct *mdata = abfd->tdata.mach_o_data;
   unsigned int wide = mach_o_wide_p (&mdata->header);
   unsigned int symwidth = wide ? 16 : 12;
-  bfd_vma symoff = sym->symoff + (i * symwidth);
+  unsigned int symoff = sym->symoff + (i * symwidth);
   unsigned char buf[16];
   unsigned char type = -1;
   unsigned char section = -1;
@@ -1245,16 +1448,19 @@ bfd_mach_o_scan_read_symtab_symbol (bfd *abfd,
       return -1;
     }
 
-  s->the_bfd = abfd;
-  s->name = sym->strtab + stroff;
-  s->value = value;
-  s->udata.i = (type << 24) | (section << 16) | desc;
-  s->flags = 0x0;
+  s->symbol.the_bfd = abfd;
+  s->symbol.name = sym->strtab + stroff;
+  s->symbol.value = value;
+  s->symbol.flags = 0x0;
+  s->symbol.udata.i = 0;
+  s->n_type = type;
+  s->n_sect = section;
+  s->n_desc = desc;
 
   if (type & BFD_MACH_O_N_STAB)
     {
-      s->flags |= BSF_DEBUGGING;
-      s->section = bfd_und_section_ptr;
+      s->symbol.flags |= BSF_DEBUGGING;
+      s->symbol.section = bfd_und_section_ptr;
       switch (type)
        {
        case N_FUN:
@@ -1268,8 +1474,9 @@ bfd_mach_o_scan_read_symtab_symbol (bfd *abfd,
        case N_GSYM:
          if ((section > 0) && (section <= mdata->nsects))
            {
-             s->section = mdata->sections[section - 1]->bfdsection;
-             s->value = s->value - mdata->sections[section - 1]->addr;
+             s->symbol.section = mdata->sections[section - 1]->bfdsection;
+             s->symbol.value =
+                s->symbol.value - mdata->sections[section - 1]->addr;
            }
          break;
        }
@@ -1277,38 +1484,43 @@ bfd_mach_o_scan_read_symtab_symbol (bfd *abfd,
   else
     {
       if (type & BFD_MACH_O_N_PEXT)
-       s->flags |= BSF_GLOBAL;
+       s->symbol.flags |= BSF_GLOBAL;
 
       if (type & BFD_MACH_O_N_EXT)
-       s->flags |= BSF_GLOBAL;
+       s->symbol.flags |= BSF_GLOBAL;
 
       if (!(type & (BFD_MACH_O_N_PEXT | BFD_MACH_O_N_EXT)))
-       s->flags |= BSF_LOCAL;
+       s->symbol.flags |= BSF_LOCAL;
 
       switch (symtype)
        {
        case BFD_MACH_O_N_UNDF:
           if (type == (BFD_MACH_O_N_UNDF | BFD_MACH_O_N_EXT)
-              && s->value != 0)
+              && s->symbol.value != 0)
             {
               /* A common symbol.  */
-              s->section = bfd_com_section_ptr;
-              s->flags = BSF_NO_FLAGS;
+              s->symbol.section = bfd_com_section_ptr;
+              s->symbol.flags = BSF_NO_FLAGS;
             }
           else
-            s->section = bfd_und_section_ptr;
+            {
+              s->symbol.section = bfd_und_section_ptr;
+              if (s->n_desc & BFD_MACH_O_N_WEAK_REF)
+                s->symbol.flags |= BSF_WEAK;
+            }
          break;
        case BFD_MACH_O_N_PBUD:
-         s->section = bfd_und_section_ptr;
+         s->symbol.section = bfd_und_section_ptr;
          break;
        case BFD_MACH_O_N_ABS:
-         s->section = bfd_abs_section_ptr;
+         s->symbol.section = bfd_abs_section_ptr;
          break;
        case BFD_MACH_O_N_SECT:
          if ((section > 0) && (section <= mdata->nsects))
            {
-             s->section = mdata->sections[section - 1]->bfdsection;
-             s->value = s->value - mdata->sections[section - 1]->addr;
+             s->symbol.section = mdata->sections[section - 1]->bfdsection;
+             s->symbol.value =
+                s->symbol.value - mdata->sections[section - 1]->addr;
            }
          else
            {
@@ -1317,22 +1529,22 @@ bfd_mach_o_scan_read_symtab_symbol (bfd *abfd,
                {
                  fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbol: "
                           "symbol \"%s\" specified invalid section %d (max %lu): setting to undefined\n",
-                          s->name, section, mdata->nsects);
+                          s->symbol.name, section, mdata->nsects);
                }
-             s->section = bfd_und_section_ptr;
+             s->symbol.section = bfd_und_section_ptr;
            }
          break;
        case BFD_MACH_O_N_INDR:
          fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbol: "
                   "symbol \"%s\" is unsupported 'indirect' reference: setting to undefined\n",
-                  s->name);
-         s->section = bfd_und_section_ptr;
+                  s->symbol.name);
+         s->symbol.section = bfd_und_section_ptr;
          break;
        default:
          fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbol: "
                   "symbol \"%s\" specified invalid type field 0x%x: setting to undefined\n",
-                  s->name, symtype);
-         s->section = bfd_und_section_ptr;
+                  s->symbol.name, symtype);
+         s->symbol.section = bfd_und_section_ptr;
          break;
        }
     }
@@ -1368,8 +1580,7 @@ bfd_mach_o_scan_read_symtab_strtab (bfd *abfd,
   if (bfd_seek (abfd, sym->stroff, SEEK_SET) != 0
       || bfd_bread ((PTR) sym->strtab, sym->strsize, abfd) != sym->strsize)
     {
-      fprintf (stderr, "bfd_mach_o_scan_read_symtab_strtab: unable to read %lu bytes at %lu\n",
-              sym->strsize, sym->stroff);
+      bfd_set_error (bfd_error_file_truncated);
       return -1;
     }
 
@@ -1384,7 +1595,7 @@ bfd_mach_o_scan_read_symtab_symbols (bfd *abfd,
   int ret;
 
   BFD_ASSERT (sym->symbols == NULL);
-  sym->symbols = bfd_alloc (abfd, sym->nsyms * sizeof (asymbol));
+  sym->symbols = bfd_alloc (abfd, sym->nsyms * sizeof (bfd_mach_o_asymbol));
 
   if (sym->symbols == NULL)
     {
@@ -1410,7 +1621,7 @@ int
 bfd_mach_o_scan_read_dysymtab_symbol (bfd *abfd,
                                      bfd_mach_o_dysymtab_command *dysym,
                                      bfd_mach_o_symtab_command *sym,
-                                     asymbol *s,
+                                     bfd_mach_o_asymbol *s,
                                      unsigned long i)
 {
   unsigned long isymoff = dysym->indirectsymoff + (i * 4);
@@ -1587,7 +1798,7 @@ bfd_mach_o_scan_read_thread (bfd *abfd, bfd_mach_o_load_command *command)
   bfd_mach_o_data_struct *mdata = NULL;
   bfd_mach_o_thread_command *cmd = &command->command.thread;
   unsigned char buf[8];
-  bfd_vma offset;
+  unsigned int offset;
   unsigned int nflavours;
   unsigned int i;
 
@@ -2088,7 +2299,6 @@ bfd_mach_o_scan (bfd *abfd,
     BFD_MACH_O_HEADER_64_SIZE : BFD_MACH_O_HEADER_SIZE;
 
   mdata->header = *header;
-  mdata->symbols = NULL;
 
   abfd->flags = abfd->flags & BFD_IN_MEMORY;
   switch (header->filetype)
@@ -2167,11 +2377,8 @@ bfd_mach_o_mkobject_init (bfd *abfd)
   mdata->header.flags = 0;
   mdata->header.byteorder = BFD_ENDIAN_UNKNOWN;
   mdata->commands = NULL;
-  mdata->nsymbols = 0;
-  mdata->symbols = NULL;
   mdata->nsects = 0;
   mdata->sections = NULL;
-  mdata->ibfd = NULL;
 
   return TRUE;
 }
@@ -2685,7 +2892,7 @@ bfd_mach_o_bfd_print_private_bfd_data (bfd *abfd, PTR ptr)
          {
            bfd_mach_o_symtab_command *symtab = &cmd->command.symtab;
            fprintf (file, "\n"
-                    "LC_SYMTAB: nsyms: %lu, strsize: %lu\n",
+                    "LC_SYMTAB: nsyms: %u, strsize: %u\n",
                     symtab->nsyms, symtab->strsize);
            break;
          }
@@ -2831,11 +3038,16 @@ bfd_mach_o_core_file_failing_signal (bfd *abfd ATTRIBUTE_UNUSED)
   return 0;
 }
 
+#define bfd_mach_o_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup 
+#define bfd_mach_o_bfd_reloc_name_lookup _bfd_norelocs_bfd_reloc_name_lookup
+
+#define bfd_mach_o_swap_reloc_in NULL
+#define bfd_mach_o_swap_reloc_out NULL
+
 #define TARGET_NAME            mach_o_be_vec
 #define TARGET_STRING          "mach-o-be"
 #define TARGET_BIG_ENDIAN      1
 #define TARGET_ARCHIVE                 0
-
 #include "mach-o-target.c"
 
 #undef TARGET_NAME
index 5401587..766a68a 100644 (file)
@@ -24,6 +24,7 @@
 
 #include "bfd.h"
 
+/* Symbol n_type values.  */
 #define BFD_MACH_O_N_STAB  0xe0        /* If any of these bits set, a symbolic debugging entry.  */
 #define BFD_MACH_O_N_PEXT  0x10        /* Private external symbol bit.  */
 #define BFD_MACH_O_N_TYPE  0x0e        /* Mask for the type bits.  */
 #define BFD_MACH_O_N_PBUD  0x0c /* Prebound undefined (defined in a dylib).  */
 #define BFD_MACH_O_N_SECT  0x0e        /* Defined in section number n_sect.  */
 
-#define BFD_MACH_O_NO_SECT 0
-
-#define BFD_MACH_O_SYM_NTYPE(SYM) (((SYM)->udata.i >> 24) & 0xff)
-#define BFD_MACH_O_SYM_NSECT(SYM) (((SYM)->udata.i >> 16) & 0xff)
-#define BFD_MACH_O_SYM_NDESC(SYM) ((SYM)->udata.i & 0xffff)
+#define BFD_MACH_O_NO_SECT 0   /* Symbol not in any section of the image.  */
+
+/* Symbol n_desc flags.  */
+#define BFD_MACH_O_REFERENCE_FLAG_UNDEFINED_NON_LAZY           0x00
+#define BFD_MACH_O_REFERENCE_FLAG_UNDEFINED_LAZY               0x01
+#define BFD_MACH_O_REFERENCE_FLAG_DEFINED                      0x02
+#define BFD_MACH_O_REFERENCE_FLAG_PRIVATE_DEFINED              0x03
+#define BFD_MACH_O_REFERENCE_FLAG_PRIVATE_UNDEFINED_NON_LAZY   0x04
+#define BFD_MACH_O_REFERENCE_FLAG_PRIVATE_UNDEFINED_LAZY       0x05
+#define BFD_MACH_O_REFERENCED_DYNAMICALLY                      0x10
+#define BFD_MACH_O_N_DESC_DISCARDED                            0x20
+#define BFD_MACH_O_N_NO_DEAD_STRIP                             0x20
+#define BFD_MACH_O_N_WEAK_REF                                  0x40
+#define BFD_MACH_O_N_WEAK_DEF                                  0x80
 
 typedef enum bfd_mach_o_mach_header_magic
 {
@@ -203,11 +213,33 @@ typedef enum bfd_mach_o_section_type
   /* Section with only lazy symbol pointers.  */
   BFD_MACH_O_S_LAZY_SYMBOL_POINTERS = 0x7,
 
-  /* Section with only symbol stubs, byte size of stub in the reserved2 field.  */
+  /* Section with only symbol stubs, byte size of stub in the reserved2
+     field.  */
   BFD_MACH_O_S_SYMBOL_STUBS = 0x8,
 
   /* Section with only function pointers for initialization.  */
-  BFD_MACH_O_S_MOD_INIT_FUNC_POINTERS = 0x9
+  BFD_MACH_O_S_MOD_INIT_FUNC_POINTERS = 0x9,
+
+  /* Section with only function pointers for termination.  */
+  BFD_MACH_O_S_MOD_FINI_FUNC_POINTERS = 0xa,
+
+  /* Section contains symbols that are coalesced by the linkers.  */
+  BFD_MACH_O_S_COALESCED = 0xb,
+
+  /* Zero fill on demand section (possibly larger than 4 GB).  */
+  BFD_MACH_O_S_GB_ZEROFILL = 0xc,
+
+  /* Section with only pairs of function pointers for interposing.  */
+  BFD_MACH_O_S_INTERPOSING = 0xd,
+
+  /* Section with only 16 byte literals.  */
+  BFD_MACH_O_S_16BYTE_LITERALS = 0xe,
+
+  /* Section contains DTrace Object Format.  */
+  BFD_MACH_O_S_DTRACE_DOF = 0xf,
+
+  /* Section with only lazy symbol pointers to lazy loaded dylibs.  */
+  BFD_MACH_O_S_LAZY_DYLIB_SYMBOL_POINTERS = 0x10
 }
 bfd_mach_o_section_type;
 
@@ -233,10 +265,27 @@ bfd_mach_o_section_type;
 #define BFD_MACH_O_S_ATTR_EXT_RELOC         0x00000200
 
 /* Section contains some machine instructions.  */
-#define BFD_MACH_O_S_ATTR_SOME_INSTRUCTIONS 0x00004000
+#define BFD_MACH_O_S_ATTR_SOME_INSTRUCTIONS 0x00000400
 
+/* A debug section.  */
 #define BFD_MACH_O_S_ATTR_DEBUG             0x02000000
 
+/* Used with i386 stubs.  */
+#define BFD_MACH_O_S_SELF_MODIFYING_CODE    0x04000000
+
+/* Blocks are live if they reference live blocks.  */
+#define BFD_MACH_O_S_ATTR_LIVE_SUPPORT      0x08000000
+
+/* No dead stripping.  */
+#define BFD_MACH_O_S_ATTR_NO_DEAD_STRIP     0x10000000
+
+/* Section symbols can be stripped in files with MH_DYLDLINK flag.  */
+#define BFD_MACH_O_S_ATTR_STRIP_STATIC_SYMS 0x20000000
+
+/* Section contains coalesced symbols that are not to be in the TOC of an
+   archive.  */
+#define BFD_MACH_O_S_ATTR_NO_TOC            0x40000000
+
 /* Section contains only true machine instructions.  */
 #define BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS 0x80000000
 
@@ -302,13 +351,68 @@ bfd_mach_o_segment_command;
 #define BFD_MACH_O_PROT_WRITE   0x02
 #define BFD_MACH_O_PROT_EXECUTE 0x04
 
+/* Generic relocation types (used by i386).  */
+#define BFD_MACH_O_GENERIC_RELOC_VANILLA       0
+#define BFD_MACH_O_GENERIC_RELOC_PAIR          1
+#define BFD_MACH_O_GENERIC_RELOC_SECTDIFF      2
+#define BFD_MACH_O_GENERIC_RELOC_PB_LA_PTR     3
+#define BFD_MACH_O_GENERIC_RELOC_LOCAL_SECTDIFF        4
+
+/* Size of a relocation entry.  */
+#define BFD_MACH_O_RELENT_SIZE 8
+
+/* Fields for a normal (non-scattered) entry.  */
+#define BFD_MACH_O_R_PCREL             0x01000000
+#define BFD_MACH_O_GET_R_LENGTH(s)     (((s) >> 25) & 0x3)
+#define BFD_MACH_O_R_EXTERN            0x08000000
+#define BFD_MACH_O_GET_R_TYPE(s)       (((s) >> 28) & 0x0f)
+#define BFD_MACH_O_GET_R_SYMBOLNUM(s)  ((s) & 0x00ffffff)
+#define BFD_MACH_O_SET_R_LENGTH(l)     (((l) & 0x3) << 25)
+#define BFD_MACH_O_SET_R_TYPE(t)       (((t) & 0xf) << 28)
+#define BFD_MACH_O_SET_R_SYMBOLNUM(s)  ((s) & 0x00ffffff)
+
+/* Fields for a scattered entry.  */
+#define BFD_MACH_O_SR_SCATTERED                0x80000000
+#define BFD_MACH_O_SR_PCREL            0x40000000
+#define BFD_MACH_O_GET_SR_LENGTH(s)    (((s) >> 28) & 0x3)
+#define BFD_MACH_O_GET_SR_TYPE(s)      (((s) >> 24) & 0x0f)
+#define BFD_MACH_O_GET_SR_ADDRESS(s)   ((s) & 0x00ffffff)
+#define BFD_MACH_O_SET_SR_LENGTH(l)    (((l) & 0x3) << 28)
+#define BFD_MACH_O_SET_SR_TYPE(t)      (((t) & 0xf) << 24)
+#define BFD_MACH_O_SET_SR_ADDRESS(s)   ((s) & 0x00ffffff)
+
+/* Expanded internal representation of a relocation entry.  */
+typedef struct bfd_mach_o_reloc_info
+{
+  bfd_vma r_address;
+  bfd_vma r_value;
+  unsigned int r_scattered : 1;
+  unsigned int r_type : 4;
+  unsigned int r_pcrel : 1;
+  unsigned int r_length : 2;
+  unsigned int r_extern : 1;
+}
+bfd_mach_o_reloc_info;
+
+typedef struct bfd_mach_o_asymbol
+{
+  /* The actual symbol which the rest of BFD works with.  */
+  asymbol symbol;
+
+  /* Fields from Mach-O symbol.  */
+  unsigned char n_type;
+  unsigned char n_sect;
+  unsigned short n_desc;
+}
+bfd_mach_o_asymbol;
+
 typedef struct bfd_mach_o_symtab_command
 {
-  unsigned long symoff;
-  unsigned long nsyms;
-  unsigned long stroff;
-  unsigned long strsize;
-  asymbol *symbols;
+  unsigned int symoff;
+  unsigned int nsyms;
+  unsigned int stroff;
+  unsigned int strsize;
+  bfd_mach_o_asymbol *symbols;
   char *strtab;
 }
 bfd_mach_o_symtab_command;
@@ -525,8 +629,8 @@ typedef struct bfd_mach_o_load_command
 {
   bfd_mach_o_load_command_type type;
   bfd_boolean type_required;
-  bfd_vma offset;
-  bfd_vma len;
+  unsigned int offset;
+  unsigned int len;
   union
   {
     bfd_mach_o_segment_command segment;
@@ -544,25 +648,40 @@ bfd_mach_o_load_command;
 
 typedef struct mach_o_data_struct
 {
+  /* Mach-O header.  */
   bfd_mach_o_header header;
+  /* Array of load commands (length is given by header.ncmds).  */
   bfd_mach_o_load_command *commands;
-  unsigned long nsymbols;
-  asymbol *symbols;
+
+  /* Flatten array of sections.  The array is 0-based.  */
   unsigned long nsects;
   bfd_mach_o_section **sections;
-  bfd *ibfd;
+
+  /* Used while writting: current length of the output file.  This is used
+     to allocate space in the file.  */
+  ufile_ptr filelen;
 }
 mach_o_data_struct;
+typedef struct mach_o_data_struct bfd_mach_o_data_struct;
 
-#define bfd_get_mach_o_data(abfd) ((abfd)->tdata.mach_o_data)
+/* Target specific routines.  */
+typedef struct bfd_mach_o_backend_data
+{
+  bfd_boolean (*_bfd_mach_o_swap_reloc_in)(arelent *, bfd_mach_o_reloc_info *);
+  bfd_boolean (*_bfd_mach_o_swap_reloc_out)(arelent *, bfd_mach_o_reloc_info *);
+}
+bfd_mach_o_backend_data;
 
-typedef struct mach_o_data_struct bfd_mach_o_data_struct;
+#define bfd_get_mach_o_data(abfd) ((abfd)->tdata.mach_o_data)
+#define bfd_mach_o_get_backend_data(abfd) \
+  ((bfd_mach_o_backend_data*)(abfd)->xvec->backend_data)
 
 bfd_boolean bfd_mach_o_valid (bfd *);
-int bfd_mach_o_scan_read_symtab_symbol (bfd *, bfd_mach_o_symtab_command *, asymbol *, unsigned long);
+int bfd_mach_o_scan_read_symtab_symbol (bfd *, bfd_mach_o_symtab_command *, 
+                                        bfd_mach_o_asymbol *, unsigned long);
 int bfd_mach_o_scan_read_symtab_strtab (bfd *, bfd_mach_o_symtab_command *);
 int bfd_mach_o_scan_read_symtab_symbols (bfd *, bfd_mach_o_symtab_command *);
-int bfd_mach_o_scan_read_dysymtab_symbol (bfd *, bfd_mach_o_dysymtab_command *, bfd_mach_o_symtab_command *, asymbol *, unsigned long);
+int bfd_mach_o_scan_read_dysymtab_symbol (bfd *, bfd_mach_o_dysymtab_command *, bfd_mach_o_symtab_command *, bfd_mach_o_asymbol *, unsigned long);
 int bfd_mach_o_scan_start_address (bfd *);
 int bfd_mach_o_scan (bfd *, bfd_mach_o_header *, bfd_mach_o_data_struct *);
 bfd_boolean bfd_mach_o_mkobject_init (bfd *);
@@ -580,6 +699,10 @@ bfd_boolean bfd_mach_o_bfd_copy_private_section_data (bfd *, asection *,
 bfd_boolean bfd_mach_o_bfd_copy_private_bfd_data (bfd *, bfd *);
 long bfd_mach_o_get_symtab_upper_bound (bfd *);
 long bfd_mach_o_canonicalize_symtab (bfd *, asymbol **);
+long bfd_mach_o_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
+                                       asection *asect);
+long bfd_mach_o_canonicalize_reloc (bfd *abfd, asection *asect,
+                                    arelent **rels, asymbol **syms);
 asymbol *bfd_mach_o_make_empty_symbol (bfd *);
 void bfd_mach_o_get_symbol_info (bfd *, asymbol *, symbol_info *);
 void bfd_mach_o_print_symbol (bfd *, PTR, asymbol *, bfd_print_symbol_type);
index fd82c2d..cc20a87 100644 (file)
@@ -5180,6 +5180,16 @@ ENUMX
    BFD_RELOC_LM32_RELATIVE
 ENUMDOC
  Lattice Mico32 relocations.
+
+ENUM
+   BFD_RELOC_MACH_O_SECTDIFF
+ENUMDOC
+   Difference between two section addreses.  Must be followed by a
+   BFD_RELOC_MACH_O_PAIR.
+ENUM
+   BFD_RELOC_MACH_O_PAIR
+ENUMDOC
+ Mach-O generic relocations.
    
 ENDSENUM
   BFD_RELOC_UNUSED