[Patch gas/mach-o 2/2] implement mach-o subsections-via-symbols.

Iain Sandoe developer@sandoe-acoustics.co.uk
Fri Feb 17 17:21:00 GMT 2012
This is the bulk of the 'work' associated with the implementation.

I've re-named a couple of routines to keep to "obj_mach_o_xxx" and  
tried to ensure that the order of routines in the main file and header  
match up.  Our code is growing quite fast and maybe they got a little  
out of synch (new culpa, I'm sure ;)).

OK for trunk?
Iain

gas:

	* config/obj-macho.c (obj_mach_o_frob_colon): New.
	(obj_mach_o_frob_label): Record sub-section labels.
	(obj_mach_o_frob_symbol): Rename from obj_macho_frob_symbol.
	(obj_mach_o_set_subsections): New.
	(obj_mach_o_pre_relax_hook): New.
	(obj_mach_o_in_different_subsection): New.
	(obj_mach_o_force_reloc_sub_same): New.
	(obj_mach_o_force_reloc_sub_local): New.
	(obj_mach_o_force_reloc): New.
	* config/obj-macho.h (OBJ_SYMFIELD_TYPE): New.
	(obj_frob_colon): New Define.
	(obj_mach_o_frob_label): Renamed.
  	(obj_mach_o_frob_symbol): Renamed.
	(OBJ_FRAG_TYPE): New.
	(obj_mach_o_in_different_subsection, obj_mach_o_force_reloc,
	 obj_mach_o_force_reloc_sub_same,
	 obj_mach_o_force_reloc_sub_local): New declarations.

  gas/config/obj-macho.c |  199 +++++++++++++++++++++++++++++++++++++++ 
+--------
  gas/config/obj-macho.h |   49 ++++++++++---
  2 files changed, 205 insertions(+), 43 deletions(-)

diff --git a/gas/config/obj-macho.c b/gas/config/obj-macho.c
index 2a2e47d..632eafa 100644
--- a/gas/config/obj-macho.c
+++ b/gas/config/obj-macho.c
@@ -1331,6 +1331,18 @@ obj_mach_o_type_for_symbol (bfd_mach_o_asymbol  
*s)
      return BFD_MACH_O_N_SECT;
  }

+void
+obj_mach_o_frob_colon (const char *name)
+{
+  if (!bfd_is_local_label_name (stdoutput, name))
+    {
+      /* A non-local label will create a new subsection, so start a new
+         frag.  */
+      frag_wane (frag_now);
+      frag_new (0);
+    }
+}
+
  /* We need to check the correspondence between some kinds of symbols  
and their
     sections.  Common and BSS vars will seen via the obj_macho_comm()  
function.

@@ -1345,13 +1357,21 @@ obj_mach_o_type_for_symbol (bfd_mach_o_asymbol  
*s)
     are possibly incompatible with the section etc. that the symbol  
is defined
     in.  */

-void obj_macho_frob_label (struct symbol *sp)
+void obj_mach_o_frob_label (struct symbol *sp)
  {
    bfd_mach_o_asymbol *s;
    unsigned base_type;
    bfd_mach_o_section *sec;
    int sectype = -1;

+  if (!bfd_is_local_label_name (stdoutput, S_GET_NAME (sp)))
+    {
+      /* If this is a non-local label, it should have started a new  
sub-
+	 section.  */
+      gas_assert (frag_now->obj_frag_data.subsection == NULL);
+      frag_now->obj_frag_data.subsection = sp;
+    }
+
    /* Leave local symbols alone.  */

    if (S_IS_LOCAL (sp))
@@ -1392,7 +1412,7 @@ void obj_macho_frob_label (struct symbol *sp)
     (e.g. global + weak_def).  */

  int
-obj_macho_frob_symbol (struct symbol *sp)
+obj_mach_o_frob_symbol (struct symbol *sp)
  {
    bfd_mach_o_asymbol *s;
    unsigned base_type;
@@ -1483,6 +1503,95 @@ obj_macho_frob_symbol (struct symbol *sp)
    return 0;
  }

+/* Support stabs for mach-o.  */
+
+void
+obj_mach_o_process_stab (int what, const char *string,
+			 int type, int other, int desc)
+{
+  symbolS *symbolP;
+  bfd_mach_o_asymbol *s;
+
+  switch (what)
+    {
+      case 'd':
+	symbolP = symbol_new ("", now_seg, frag_now_fix (), frag_now);
+	/* Special stabd NULL name indicator.  */
+	S_SET_NAME (symbolP, NULL);
+	break;
+
+      case 'n':
+      case 's':
+	symbolP = symbol_new (string, undefined_section, (valueT) 0,
+			      &zero_address_frag);
+	pseudo_set (symbolP);
+	break;
+
+      default:
+	as_bad(_("unrecognized stab type '%c'"), (char)what);
+	abort ();
+	break;
+    }
+
+  s = (bfd_mach_o_asymbol *) symbol_get_bfdsym (symbolP);
+  s->n_type = type;
+  s->n_desc = desc;
+  /* For stabd, this will eventually get overwritten by the section  
number.  */
+  s->n_sect = other;
+
+  /* It's a debug symbol.  */
+  s->symbol.flags |= BSF_DEBUGGING;
+}
+
+/* Here we count up frags in each subsection (where a sub-section is  
defined
+   as starting with a non-local symbol).
+   Note that, if there are no non-local symbols in a section, all the  
frags will
+   be attached as one anonymous subsection.  */
+
+static void
+obj_mach_o_set_subsections (bfd *abfd ATTRIBUTE_UNUSED,
+                            asection *sec,
+                            void *unused ATTRIBUTE_UNUSED)
+{
+  segment_info_type *seginfo = seg_info (sec);
+  symbolS *cur_subsection = NULL;
+  struct obj_mach_o_symbol_data *cur_subsection_data = NULL;
+  fragS *frag;
+  frchainS *chain;
+
+  /* Protect against sections not created by gas.  */
+  if (seginfo == NULL)
+    return;
+
+  /* Attach every frag to a subsection.  */
+  for (chain = seginfo->frchainP; chain != NULL; chain = chain- 
 >frch_next)
+    for (frag = chain->frch_root; frag != NULL; frag = frag->fr_next)
+      {
+        if (frag->obj_frag_data.subsection == NULL)
+          frag->obj_frag_data.subsection = cur_subsection;
+        else
+          {
+            cur_subsection = frag->obj_frag_data.subsection;
+            cur_subsection_data = symbol_get_obj (cur_subsection);
+            cur_subsection_data->subsection_size = 0;
+          }
+        if (cur_subsection_data != NULL)
+          {
+            /* Update subsection size.  */
+            cur_subsection_data->subsection_size += frag->fr_fix;
+          }
+      }
+}
+
+/* Handle mach-o subsections-via-symbols counting up frags belonging  
to each
+   sub-section.  */
+
+void
+obj_mach_o_pre_relax_hook (void)
+{
+  bfd_map_over_sections (stdoutput, obj_mach_o_set_subsections, (char  
*) 0);
+}
+
  /* Zerofill and GB Zerofill sections must be sorted to follow all  
other
     sections in their segments.

@@ -1706,42 +1815,66 @@ obj_mach_o_reorder_section_relocs (asection  
*sec, arelent **rels, unsigned int n
    bfd_set_reloc (stdoutput, sec, rels, n);
  }

-/* Support stabs for mach-o.  */
-
-void
-obj_mach_o_process_stab (int what, const char *string,
-			 int type, int other, int desc)
+int
+obj_mach_o_in_different_subsection (symbolS *a, symbolS *b)
  {
-  symbolS *symbolP;
-  bfd_mach_o_asymbol *s;
+  fragS *fa;
+  fragS *fb;

-  switch (what)
+  if (S_GET_SEGMENT (a) != S_GET_SEGMENT (b)
+      || !S_IS_DEFINED (a)
+      || !S_IS_DEFINED (b))
      {
-      case 'd':
-	symbolP = symbol_new ("", now_seg, frag_now_fix (), frag_now);
-	/* Special stabd NULL name indicator.  */
-	S_SET_NAME (symbolP, NULL);
-	break;
-
-      case 'n':
-      case 's':
-	symbolP = symbol_new (string, undefined_section, (valueT) 0,
-			      &zero_address_frag);
-	pseudo_set (symbolP);
-	break;
+      /* Not in the same segment, or undefined symbol.  */
+      return 1;
+    }

-      default:
-	as_bad(_("unrecognized stab type '%c'"), (char)what);
-	abort ();
-	break;
+  fa = symbol_get_frag (a);
+  fb = symbol_get_frag (b);
+  if (fa == NULL || fb == NULL)
+    {
+      /* One of the symbols is not in a subsection.  */
+      return 1;
      }

-  s = (bfd_mach_o_asymbol *) symbol_get_bfdsym (symbolP);
-  s->n_type = type;
-  s->n_desc = desc;
-  /* For stabd, this will eventually get overwritten by the section  
number.  */
-  s->n_sect = other;
+  return fa->obj_frag_data.subsection != fb->obj_frag_data.subsection;
+}

-  /* It's a debug symbol.  */
-  s->symbol.flags |= BSF_DEBUGGING;
+int
+obj_mach_o_force_reloc_sub_same (fixS *fix, segT seg)
+{
+  if (! SEG_NORMAL (seg))
+    return 1;
+  return obj_mach_o_in_different_subsection (fix->fx_addsy, fix- 
 >fx_subsy);
+}
+
+int
+obj_mach_o_force_reloc_sub_local (fixS *fix, segT seg ATTRIBUTE_UNUSED)
+{
+  return obj_mach_o_in_different_subsection (fix->fx_addsy, fix- 
 >fx_subsy);
+}
+
+int
+obj_mach_o_force_reloc (fixS *fix)
+{
+  if (generic_force_reloc (fix))
+    return 1;
+
+  /* Force a reloc if the target is not in the same subsection.
+     FIXME: handle (a - b) where a and b belongs to the same  
subsection ?  */
+  if (fix->fx_addsy != NULL)
+    {
+      symbolS *subsec = fix->fx_frag->obj_frag_data.subsection;
+      symbolS *targ = fix->fx_addsy;
+
+      /* There might be no subsections at all.  */
+      if (subsec == NULL)
+        return 0;
+
+      if (S_GET_SEGMENT (targ) == absolute_section)
+        return 0;
+
+      return obj_mach_o_in_different_subsection (targ, subsec);
+    }
+  return 0;
  }
diff --git a/gas/config/obj-macho.h b/gas/config/obj-macho.h
index bb8daf9..b49038c 100644
--- a/gas/config/obj-macho.h
+++ b/gas/config/obj-macho.h
@@ -56,11 +56,36 @@ extern const pseudo_typeS mach_o_pseudo_table[];
  #define obj_read_begin_hook()	{;}
  #define obj_symbol_new_hook(s)	{;}

-#define obj_frob_label(s) obj_macho_frob_label(s)
-extern void obj_macho_frob_label (struct symbol *);
+#define EMIT_SECTION_SYMBOLS		0
+
+struct obj_mach_o_symbol_data
+{
+  /* If the symbol represents a subsection, this is the size of the  
subsection.
+     This is used to check whether a local symbol belongs to a  
subsection.  */
+  valueT subsection_size;
+};
+#define OBJ_SYMFIELD_TYPE struct obj_mach_o_symbol_data
+
+#define obj_frob_colon obj_mach_o_frob_colon
+extern void obj_mach_o_frob_colon (const char *);
+
+/* Called when a label is defined.  Mach-O uses this to create  
subsections.  */
+#define obj_frob_label obj_mach_o_frob_label
+extern void obj_mach_o_frob_label (symbolS *);

-#define obj_frob_symbol(s, punt) punt = obj_macho_frob_symbol(s)
-extern int obj_macho_frob_symbol (struct symbol *);
+#define obj_frob_symbol(s, punt) punt = obj_mach_o_frob_symbol(s)
+extern int obj_mach_o_frob_symbol (struct symbol *);
+
+struct obj_mach_o_frag_data
+{
+  /* Symbol that corresponds to the subsection.  */
+  symbolS *subsection;
+};
+
+#define OBJ_FRAG_TYPE struct obj_mach_o_frag_data
+
+#define md_pre_relax_hook obj_mach_o_pre_relax_hook()
+extern void obj_mach_o_pre_relax_hook (void);

  #define md_post_relax_hook obj_mach_o_post_relax_hook()
  void obj_mach_o_post_relax_hook (void);
@@ -68,14 +93,18 @@ void obj_mach_o_post_relax_hook (void);
  #define obj_frob_file_after_relocs obj_mach_o_frob_file_after_relocs
  extern void obj_mach_o_frob_file_after_relocs (void);

-void obj_mach_o_reorder_section_relocs (asection *, arelent **,  
unsigned int);
+#define OBJ_PROCESS_STAB(SEG,W,S,T,O,D)	 
obj_mach_o_process_stab(W,S,T,O,D)
+extern void obj_mach_o_process_stab (int, const char *,int, int, int);

  #define SET_SECTION_RELOCS(sec, relocs, n) \
    obj_mach_o_reorder_section_relocs (sec, relocs, n)
-
-#define EMIT_SECTION_SYMBOLS		0
-
-#define OBJ_PROCESS_STAB(SEG,W,S,T,O,D)	 
obj_mach_o_process_stab(W,S,T,O,D)
-extern void obj_mach_o_process_stab (int, const char *,int, int, int);
+extern void obj_mach_o_reorder_section_relocs (asection *, arelent **,
+					       unsigned int);
+
+struct fix;
+int obj_mach_o_in_different_subsection (symbolS *a, symbolS *b);
+int obj_mach_o_force_reloc (struct fix *fix);
+int obj_mach_o_force_reloc_sub_same (struct fix *fix, segT seg);
+int obj_mach_o_force_reloc_sub_local (struct fix *fix, segT seg);

  #endif /* _OBJ_MACH_O_H */



More information about the Binutils mailing list