2008-01-08 H.J. Lu <hongjiu.lu@intel.com>
[deliverable/binutils-gdb.git] / gas / write.c
index b94099b8121a6b1817add524976f8f8d58ab13ba..03641dfa4b320618b2a37911b32eeb9572347ba4 100644 (file)
@@ -7,7 +7,7 @@
 
    GAS 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 2, or (at your option)
+   the Free Software Foundation; either version 3, or (at your option)
    any later version.
 
    GAS is distributed in the hope that it will be useful,
@@ -117,6 +117,9 @@ symbolS *abs_section_sym;
 /* Remember the value of dot when parsing expressions.  */
 addressT dot_value;
 
+/* Relocs generated by ".reloc" pseudo.  */
+struct reloc_list* reloc_list;
+
 void print_fixup (fixS *);
 
 /* We generally attach relocs to frag chains.  However, after we have
@@ -624,6 +627,86 @@ dump_section_relocs (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, FILE *stream)
 #define EMIT_SECTION_SYMBOLS 1
 #endif
 
+/* Resolve U.A.OFFSET_SYM and U.A.SYM fields of RELOC_LIST entries,
+   and check for validity.  Convert RELOC_LIST from using U.A fields
+   to U.B fields.  */
+static void
+resolve_reloc_expr_symbols (void)
+{
+  struct reloc_list *r;
+
+  for (r = reloc_list; r; r = r->next)
+    {
+      expressionS *symval;
+      symbolS *sym;
+      bfd_vma offset, addend;
+      asection *sec;
+      reloc_howto_type *howto;
+
+      resolve_symbol_value (r->u.a.offset_sym);
+      symval = symbol_get_value_expression (r->u.a.offset_sym);
+
+      offset = 0;
+      sym = NULL;
+      if (symval->X_op == O_constant)
+       sym = r->u.a.offset_sym;
+      else if (symval->X_op == O_symbol)
+       {
+         sym = symval->X_add_symbol;
+         offset = symval->X_add_number;
+         symval = symbol_get_value_expression (symval->X_add_symbol);
+       }
+      if (sym == NULL
+         || symval->X_op != O_constant
+         || (sec = S_GET_SEGMENT (sym)) == NULL
+         || !SEG_NORMAL (sec))
+       {
+         as_bad_where (r->file, r->line, _("invalid offset expression"));
+         sec = NULL;
+       }
+      else
+       offset += S_GET_VALUE (sym);
+
+      sym = NULL;
+      addend = r->u.a.addend;
+      if (r->u.a.sym != NULL)
+       {
+         resolve_symbol_value (r->u.a.sym);
+         symval = symbol_get_value_expression (r->u.a.sym);
+         if (symval->X_op == O_constant)
+           sym = r->u.a.sym;
+         else if (symval->X_op == O_symbol)
+           {
+             sym = symval->X_add_symbol;
+             addend += symval->X_add_number;
+             symval = symbol_get_value_expression (symval->X_add_symbol);
+           }
+         if (symval->X_op != O_constant)
+           {
+             as_bad_where (r->file, r->line, _("invalid reloc expression"));
+             sec = NULL;
+           }
+         else if (sym != NULL)
+           symbol_mark_used_in_reloc (sym);
+       }
+      if (sym == NULL)
+       {
+         if (abs_section_sym == NULL)
+           abs_section_sym = section_symbol (absolute_section);
+         sym = abs_section_sym;
+       }
+
+      howto = r->u.a.howto;
+
+      r->u.b.sec = sec;
+      r->u.b.s = symbol_get_bfdsym (sym);
+      r->u.b.r.sym_ptr_ptr = &r->u.b.s;
+      r->u.b.r.address = offset;
+      r->u.b.r.addend = addend;
+      r->u.b.r.howto = howto;
+    }
+}
+
 /* This pass over fixups decides whether symbols can be replaced with
    section symbols.  */
 
@@ -1027,6 +1110,7 @@ write_relocs (bfd *abfd, asection *sec, void *xxx ATTRIBUTE_UNUSED)
   segment_info_type *seginfo = seg_info (sec);
   unsigned int i;
   unsigned int n;
+  struct reloc_list *my_reloc_list, **rp, *r;
   arelent **relocs;
   fixS *fixp;
 
@@ -1044,6 +1128,22 @@ write_relocs (bfd *abfd, asection *sec, void *xxx ATTRIBUTE_UNUSED)
   n *= MAX_RELOC_EXPANSION;
 #endif
 
+  /* Extract relocs for this section from reloc_list.  */
+  rp = &reloc_list;
+  my_reloc_list = NULL;
+  while ((r = *rp) != NULL)
+    {
+      if (r->u.b.sec == sec)
+       {
+         *rp = r->next;
+         r->next = my_reloc_list;
+         my_reloc_list = r;
+         n++;
+       }
+      else
+       rp = &r->next;
+    }
+
   relocs = xcalloc (n, sizeof (arelent *));
 
   i = 0;
@@ -1107,6 +1207,23 @@ write_relocs (bfd *abfd, asection *sec, void *xxx ATTRIBUTE_UNUSED)
   }
 #endif
 
+  for (r = my_reloc_list; r != NULL; r = r->next)
+    {
+      fragS *f;
+      for (f = seginfo->frchainP->frch_root; f; f = f->fr_next)
+       if (f->fr_address <= r->u.b.r.address
+           && r->u.b.r.address < f->fr_address + f->fr_fix)
+         break;
+      if (f == NULL)
+       as_bad_where (r->file, r->line,
+                     _("reloc not within (fixed part of) section"));
+      else
+       {
+         relocs[n++] = &r->u.b.r;
+         install_reloc (sec, &r->u.b.r, f, r->file, r->line);
+       }
+    }
+
   if (n)
     {
       flagword flags = bfd_get_section_flags (abfd, sec);
@@ -1564,6 +1681,7 @@ write_object_file (void)
        resolve_symbol_value (symp);
     }
   resolve_local_symbol_values ();
+  resolve_reloc_expr_symbols ();
 
   PROGRESS (1);
 
@@ -1960,13 +2078,38 @@ relax_segment (struct frag *segment_frag_root, segT segment, int pass)
   /* Do relax().  */
   {
     unsigned long max_iterations;
-    offsetT stretch;   /* May be any size, 0 or negative.  */
-    /* Cumulative number of addresses we have relaxed this pass.
-       We may have relaxed more than one address.  */
-    int stretched;     /* Have we stretched on this pass?  */
-    /* This is 'cuz stretch may be zero, when, in fact some piece of code
-       grew, and another shrank.  If a branch instruction doesn't fit anymore,
-       we could be scrod.  */
+
+    /* Cumulative address adjustment.  */
+    offsetT stretch;
+
+    /* Have we made any adjustment this pass?  We can't just test
+       stretch because one piece of code may have grown and another
+       shrank.  */
+    int stretched;
+
+    /* Most horrible, but gcc may give us some exception data that
+       is impossible to assemble, of the form
+
+       .align 4
+       .byte 0, 0
+       .uleb128 end - start
+       start:
+       .space 128*128 - 1
+       .align 4
+       end:
+
+       If the leb128 is two bytes in size, then end-start is 128*128,
+       which requires a three byte leb128.  If the leb128 is three
+       bytes in size, then end-start is 128*128-1, which requires a
+       two byte leb128.  We work around this dilemma by inserting
+       an extra 4 bytes of alignment just after the .align.  This
+       works because the data after the align is accessed relative to
+       the end label.
+
+       This counter is used in a tiny state machine to detect
+       whether a leb128 followed by an align is impossible to
+       relax.  */
+    int rs_leb128_fudge = 0;
 
     /* We want to prevent going into an infinite loop where one frag grows
        depending upon the location of a symbol which is in turn moved by
@@ -2089,6 +2232,49 @@ relax_segment (struct frag *segment_frag_root, segT segment, int pass)
                    }
 
                  growth = newoff - oldoff;
+
+                 /* If this align happens to follow a leb128 and
+                    we have determined that the leb128 is bouncing
+                    in size, then break the cycle by inserting an
+                    extra alignment.  */
+                 if (growth < 0
+                     && (rs_leb128_fudge & 16) != 0
+                     && (rs_leb128_fudge & 15) >= 2)
+                   {
+                     segment_info_type *seginfo = seg_info (segment);
+                     struct obstack *ob = &seginfo->frchainP->frch_obstack;
+                     struct frag *newf;
+
+                     newf = frag_alloc (ob);
+                     obstack_blank_fast (ob, fragP->fr_var);
+                     obstack_finish (ob);
+                     memcpy (newf, fragP, SIZEOF_STRUCT_FRAG);
+                     memcpy (newf->fr_literal,
+                             fragP->fr_literal + fragP->fr_fix,
+                             fragP->fr_var);
+                     newf->fr_type = rs_fill;
+                     newf->fr_fix = 0;
+                     newf->fr_offset = (((offsetT) 1 << fragP->fr_offset)
+                                        / fragP->fr_var);
+                     if (newf->fr_offset * newf->fr_var
+                         != (offsetT) 1 << fragP->fr_offset)
+                       {
+                         newf->fr_offset = (offsetT) 1 << fragP->fr_offset;
+                         newf->fr_var = 1;
+                       }
+                     /* Include growth of new frag, because rs_fill
+                        frags don't normally grow.  */
+                     growth += newf->fr_offset * newf->fr_var;
+                     /* The new frag address is newoff.  Adjust this
+                        for the amount we'll add when we process the
+                        new frag.  */
+                     newf->fr_address = newoff - stretch - growth;
+                     newf->relax_marker ^= 1;
+                     fragP->fr_next = newf;
+#ifdef DEBUG
+                     as_warn (_("padding added"));
+#endif
+                   }
                }
                break;
 
@@ -2228,8 +2414,23 @@ relax_segment (struct frag *segment_frag_root, segT segment, int pass)
              {
                stretch += growth;
                stretched = 1;
+               if (fragP->fr_type == rs_leb128)
+                 rs_leb128_fudge += 16;
+               else if (fragP->fr_type == rs_align
+                        && (rs_leb128_fudge & 16) != 0
+                        && stretch == 0)
+                 rs_leb128_fudge += 16;
+               else
+                 rs_leb128_fudge = 0;
              }
          }
+
+       if (stretch == 0
+           && (rs_leb128_fudge & 16) == 0
+           && (rs_leb128_fudge & -16) != 0)
+         rs_leb128_fudge += 1;
+       else
+         rs_leb128_fudge = 0;
       }
     /* Until nothing further to relax.  */
     while (stretched && -- max_iterations);
@@ -2285,7 +2486,9 @@ void
 print_fixup (fixS *fixp)
 {
   indent_level = 1;
-  fprintf (stderr, "fix %lx %s:%d", (long) fixp, fixp->fx_file, fixp->fx_line);
+  fprintf (stderr, "fix ");
+  fprintf_vma (stderr, (bfd_vma)((bfd_hostptr_t) fixp));
+  fprintf (stderr, " %s:%d",fixp->fx_file, fixp->fx_line);
   if (fixp->fx_pcrel)
     fprintf (stderr, " pcrel");
   if (fixp->fx_pcrel_adjust)
@@ -2302,8 +2505,10 @@ print_fixup (fixS *fixp)
     fprintf (stderr, " tcbit");
   if (fixp->fx_done)
     fprintf (stderr, " done");
-  fprintf (stderr, "\n    size=%d frag=%lx where=%ld offset=%lx addnumber=%lx",
-          fixp->fx_size, (long) fixp->fx_frag, (long) fixp->fx_where,
+  fprintf (stderr, "\n    size=%d frag=", fixp->fx_size);
+  fprintf_vma (stderr, (bfd_vma) ((bfd_hostptr_t) fixp->fx_frag));
+  fprintf (stderr, " where=%ld offset=%lx addnumber=%lx",
+          (long) fixp->fx_where,
           (long) fixp->fx_offset, (long) fixp->fx_addnumber);
   fprintf (stderr, "\n    %s (%d)", bfd_get_reloc_code_name (fixp->fx_r_type),
           fixp->fx_r_type);
This page took 0.026596 seconds and 4 git commands to generate.