Accept and ignore the R_BPF_64_NODLYD32 relocation.
[binutils-gdb.git] / gas / ehopt.c
index 033b22a1e858a7ce3469b4e0afc54b80d7e84352..9d6606adf3ef6a3904d12531392884be34db02f5 100644 (file)
@@ -1,5 +1,5 @@
 /* ehopt.c--optimize gcc exception frame information.
-   Copyright (C) 1998-2018 Free Software Foundation, Inc.
+   Copyright (C) 1998-2023 Free Software Foundation, Inc.
    Written by Ian Lance Taylor <ian@cygnus.com>.
 
    This file is part of GAS, the GNU Assembler.
@@ -21,7 +21,6 @@
 
 #include "as.h"
 #include "subsegs.h"
-#include "struc-symbol.h"
 
 /* We include this ELF file, even though we may not be assembling for
    ELF, since the exception frame information is always in a format
@@ -95,8 +94,6 @@ struct cie_info
   int z_augmentation;
 };
 
-static int get_cie_info (struct cie_info *);
-
 /* Extract information from the CIE.  */
 
 static int
@@ -104,7 +101,7 @@ get_cie_info (struct cie_info *info)
 {
   fragS *f;
   fixS *fix;
-  int offset;
+  unsigned int offset;
   char CIE_id;
   char augmentation[10];
   int iaug;
@@ -119,7 +116,7 @@ get_cie_info (struct cie_info *info)
 
   /* First make sure that the CIE Identifier Tag is 0/-1.  */
 
-  if (strncmp (segment_name (now_seg), ".debug_frame", 12) == 0)
+  if (startswith (segment_name (now_seg), ".debug_frame"))
     CIE_id = (char)0xff;
   else
     CIE_id = 0;
@@ -239,6 +236,27 @@ enum frame_state
   state_error,
 };
 
+struct frame_data
+{
+  enum frame_state state;
+
+  int cie_info_ok;
+  struct cie_info cie_info;
+
+  symbolS *size_end_sym;
+  fragS *loc4_frag;
+  int loc4_fix;
+
+  int aug_size;
+  int aug_shift;
+};
+
+static struct eh_state
+{
+  struct frame_data eh_data;
+  struct frame_data debug_data;
+} frame;
+
 /* This function is called from emit_expr.  It looks for cases which
    we can optimize.
 
@@ -255,23 +273,6 @@ enum frame_state
 int
 check_eh_frame (expressionS *exp, unsigned int *pnbytes)
 {
-  struct frame_data
-  {
-    enum frame_state state;
-
-    int cie_info_ok;
-    struct cie_info cie_info;
-
-    symbolS *size_end_sym;
-    fragS *loc4_frag;
-    int loc4_fix;
-
-    int aug_size;
-    int aug_shift;
-  };
-
-  static struct frame_data eh_frame_data;
-  static struct frame_data debug_frame_data;
   struct frame_data *d;
 
   /* Don't optimize.  */
@@ -284,11 +285,11 @@ check_eh_frame (expressionS *exp, unsigned int *pnbytes)
 #endif
 
   /* Select the proper section data.  */
-  if (strncmp (segment_name (now_seg), ".eh_frame", 9) == 0
+  if (startswith (segment_name (now_seg), ".eh_frame")
       && segment_name (now_seg)[9] != '_')
-    d = &eh_frame_data;
-  else if (strncmp (segment_name (now_seg), ".debug_frame", 12) == 0)
-    d = &debug_frame_data;
+    d = &frame.eh_data;
+  else if (startswith (segment_name (now_seg), ".debug_frame"))
+    d = &frame.debug_data;
   else
     return 0;
 
@@ -385,7 +386,7 @@ check_eh_frame (expressionS *exp, unsigned int *pnbytes)
        {
          /* This might be a DW_CFA_advance_loc4.  Record the frag and the
             position within the frag, so that we can change it later.  */
-         frag_grow (1);
+         frag_grow (1 + 4);
          d->state = state_saw_loc4;
          d->loc4_frag = frag_now;
          d->loc4_fix = frag_now_fix ();
@@ -434,23 +435,28 @@ check_eh_frame (expressionS *exp, unsigned int *pnbytes)
                || exp->X_op == O_right_shift)
               && d->cie_info.code_alignment > 1)
        {
-         if (exp->X_add_symbol->bsym
-             && exp->X_op_symbol->bsym
-             && exp->X_add_symbol->sy_value.X_op == O_subtract
-             && exp->X_op_symbol->sy_value.X_op == O_constant
+         if (symbol_symbolS (exp->X_add_symbol)
+             && symbol_constant_p (exp->X_op_symbol)
+             && S_GET_SEGMENT (exp->X_op_symbol) == absolute_section
              && ((exp->X_op == O_divide
-                  ? exp->X_op_symbol->sy_value.X_add_number
-                  : (offsetT) 1 << exp->X_op_symbol->sy_value.X_add_number)
+                  ? *symbol_X_add_number (exp->X_op_symbol)
+                  : (offsetT) 1 << *symbol_X_add_number (exp->X_op_symbol))
                  == (offsetT) d->cie_info.code_alignment))
            {
-             /* This is a case we can optimize as well.  The expression was
-                not reduced, so we can not finish the optimization until the
-                end of the assembly.  We set up a variant frag which we
-                handle later.  */
-             frag_var (rs_cfa, 4, 0, d->cie_info.code_alignment << 3,
-                       make_expr_symbol (&exp->X_add_symbol->sy_value),
-                       d->loc4_fix, (char *) d->loc4_frag);
-             return 1;
+             expressionS *symval;
+
+             symval = symbol_get_value_expression (exp->X_add_symbol);
+             if (symval->X_op == O_subtract)
+               {
+                 /* This is a case we can optimize as well.  The
+                    expression was not reduced, so we can not finish
+                    the optimization until the end of the assembly.
+                    We set up a variant frag which we handle later.  */
+                 frag_var (rs_cfa, 4, 0, d->cie_info.code_alignment << 3,
+                           make_expr_symbol (symval),
+                           d->loc4_fix, (char *) d->loc4_frag);
+                 return 1;
+               }
            }
        }
       break;
@@ -478,7 +484,9 @@ eh_frame_estimate_size_before_relax (fragS *frag)
 
   gas_assert (ca > 0);
   diff /= ca;
-  if (diff < 0x40)
+  if (diff == 0)
+    ret = -1;
+  else if (diff < 0x40)
     ret = 0;
   else if (diff < 0x100)
     ret = 1;
@@ -487,7 +495,7 @@ eh_frame_estimate_size_before_relax (fragS *frag)
   else
     ret = 4;
 
-  frag->fr_subtype = (frag->fr_subtype & ~7) | ret;
+  frag->fr_subtype = (frag->fr_subtype & ~7) | (ret & 7);
 
   return ret;
 }
@@ -502,6 +510,8 @@ eh_frame_relax_frag (fragS *frag)
   int oldsize, newsize;
 
   oldsize = frag->fr_subtype & 7;
+  if (oldsize == 7)
+    oldsize = -1;
   newsize = eh_frame_estimate_size_before_relax (frag);
   return newsize - oldsize;
 }
@@ -544,9 +554,17 @@ eh_frame_convert_frag (fragS *frag)
       md_number_to_chars (frag->fr_literal + frag->fr_fix, diff, 2);
       break;
 
-    default:
+    case 4:
       md_number_to_chars (frag->fr_literal + frag->fr_fix, diff, 4);
       break;
+
+    case 7:
+      gas_assert (diff == 0);
+      frag->fr_fix -= 8;
+      break;
+
+    default:
+      abort ();
     }
 
   frag->fr_fix += frag->fr_subtype & 7;
@@ -554,3 +572,9 @@ eh_frame_convert_frag (fragS *frag)
   frag->fr_subtype = 0;
   frag->fr_offset = 0;
 }
+
+void
+eh_begin (void)
+{
+  memset (&frame, 0, sizeof (frame));
+}