Home | History | Annotate | Line # | Download | only in bfd
      1   1.1     skrll /* .eh_frame section optimization.
      2  1.13  christos    Copyright (C) 2001-2026 Free Software Foundation, Inc.
      3   1.1     skrll    Written by Jakub Jelinek <jakub (at) redhat.com>.
      4   1.1     skrll 
      5   1.1     skrll    This file is part of BFD, the Binary File Descriptor library.
      6   1.1     skrll 
      7   1.1     skrll    This program is free software; you can redistribute it and/or modify
      8   1.1     skrll    it under the terms of the GNU General Public License as published by
      9   1.1     skrll    the Free Software Foundation; either version 3 of the License, or
     10   1.1     skrll    (at your option) any later version.
     11   1.1     skrll 
     12   1.1     skrll    This program is distributed in the hope that it will be useful,
     13   1.1     skrll    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14   1.1     skrll    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15   1.1     skrll    GNU General Public License for more details.
     16   1.1     skrll 
     17   1.1     skrll    You should have received a copy of the GNU General Public License
     18   1.1     skrll    along with this program; if not, write to the Free Software
     19   1.1     skrll    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
     20   1.1     skrll    MA 02110-1301, USA.  */
     21   1.1     skrll 
     22   1.1     skrll #include "sysdep.h"
     23   1.1     skrll #include "bfd.h"
     24   1.1     skrll #include "libbfd.h"
     25   1.1     skrll #include "elf-bfd.h"
     26   1.3  christos #include "dwarf2.h"
     27   1.1     skrll 
     28   1.1     skrll #define EH_FRAME_HDR_SIZE 8
     29   1.1     skrll 
     30   1.1     skrll struct cie
     31   1.1     skrll {
     32   1.1     skrll   unsigned int length;
     33   1.1     skrll   unsigned int hash;
     34   1.1     skrll   unsigned char version;
     35   1.1     skrll   unsigned char local_personality;
     36   1.1     skrll   char augmentation[20];
     37   1.1     skrll   bfd_vma code_align;
     38   1.1     skrll   bfd_signed_vma data_align;
     39   1.1     skrll   bfd_vma ra_column;
     40   1.1     skrll   bfd_vma augmentation_size;
     41   1.1     skrll   union {
     42   1.1     skrll     struct elf_link_hash_entry *h;
     43   1.5  christos     struct {
     44   1.5  christos       unsigned int bfd_id;
     45   1.5  christos       unsigned int index;
     46   1.5  christos     } sym;
     47   1.1     skrll     unsigned int reloc_index;
     48   1.1     skrll   } personality;
     49   1.1     skrll   struct eh_cie_fde *cie_inf;
     50   1.1     skrll   unsigned char per_encoding;
     51   1.1     skrll   unsigned char lsda_encoding;
     52   1.1     skrll   unsigned char fde_encoding;
     53   1.1     skrll   unsigned char initial_insn_length;
     54   1.1     skrll   unsigned char can_make_lsda_relative;
     55   1.1     skrll   unsigned char initial_instructions[50];
     56   1.1     skrll };
     57   1.1     skrll 
     58   1.1     skrll 
     59   1.1     skrll 
     60   1.1     skrll /* If *ITER hasn't reached END yet, read the next byte into *RESULT and
     61   1.1     skrll    move onto the next byte.  Return true on success.  */
     62   1.1     skrll 
     63  1.10  christos static inline bool
     64   1.1     skrll read_byte (bfd_byte **iter, bfd_byte *end, unsigned char *result)
     65   1.1     skrll {
     66   1.1     skrll   if (*iter >= end)
     67  1.10  christos     return false;
     68   1.1     skrll   *result = *((*iter)++);
     69  1.10  christos   return true;
     70   1.1     skrll }
     71   1.1     skrll 
     72   1.1     skrll /* Move *ITER over LENGTH bytes, or up to END, whichever is closer.
     73   1.1     skrll    Return true it was possible to move LENGTH bytes.  */
     74   1.1     skrll 
     75  1.10  christos static inline bool
     76   1.1     skrll skip_bytes (bfd_byte **iter, bfd_byte *end, bfd_size_type length)
     77   1.1     skrll {
     78   1.1     skrll   if ((bfd_size_type) (end - *iter) < length)
     79   1.1     skrll     {
     80   1.1     skrll       *iter = end;
     81  1.10  christos       return false;
     82   1.1     skrll     }
     83   1.1     skrll   *iter += length;
     84  1.10  christos   return true;
     85   1.1     skrll }
     86   1.1     skrll 
     87   1.1     skrll /* Move *ITER over an leb128, stopping at END.  Return true if the end
     88   1.1     skrll    of the leb128 was found.  */
     89   1.1     skrll 
     90  1.10  christos static bool
     91   1.1     skrll skip_leb128 (bfd_byte **iter, bfd_byte *end)
     92   1.1     skrll {
     93   1.1     skrll   unsigned char byte;
     94   1.1     skrll   do
     95   1.1     skrll     if (!read_byte (iter, end, &byte))
     96  1.10  christos       return false;
     97   1.1     skrll   while (byte & 0x80);
     98  1.10  christos   return true;
     99   1.1     skrll }
    100   1.1     skrll 
    101   1.1     skrll /* Like skip_leb128, but treat the leb128 as an unsigned value and
    102   1.1     skrll    store it in *VALUE.  */
    103   1.1     skrll 
    104  1.10  christos static bool
    105   1.1     skrll read_uleb128 (bfd_byte **iter, bfd_byte *end, bfd_vma *value)
    106   1.1     skrll {
    107   1.1     skrll   bfd_byte *start, *p;
    108   1.1     skrll 
    109   1.1     skrll   start = *iter;
    110   1.1     skrll   if (!skip_leb128 (iter, end))
    111  1.10  christos     return false;
    112   1.1     skrll 
    113   1.1     skrll   p = *iter;
    114   1.1     skrll   *value = *--p;
    115   1.1     skrll   while (p > start)
    116   1.1     skrll     *value = (*value << 7) | (*--p & 0x7f);
    117   1.1     skrll 
    118  1.10  christos   return true;
    119   1.1     skrll }
    120   1.1     skrll 
    121   1.1     skrll /* Like read_uleb128, but for signed values.  */
    122   1.1     skrll 
    123  1.10  christos static bool
    124   1.1     skrll read_sleb128 (bfd_byte **iter, bfd_byte *end, bfd_signed_vma *value)
    125   1.1     skrll {
    126   1.1     skrll   bfd_byte *start, *p;
    127   1.1     skrll 
    128   1.1     skrll   start = *iter;
    129   1.1     skrll   if (!skip_leb128 (iter, end))
    130  1.10  christos     return false;
    131   1.1     skrll 
    132   1.1     skrll   p = *iter;
    133   1.1     skrll   *value = ((*--p & 0x7f) ^ 0x40) - 0x40;
    134   1.1     skrll   while (p > start)
    135   1.1     skrll     *value = (*value << 7) | (*--p & 0x7f);
    136   1.1     skrll 
    137  1.10  christos   return true;
    138   1.1     skrll }
    139   1.1     skrll 
    140   1.1     skrll /* Return 0 if either encoding is variable width, or not yet known to bfd.  */
    141   1.1     skrll 
    142   1.1     skrll static
    143   1.1     skrll int get_DW_EH_PE_width (int encoding, int ptr_size)
    144   1.1     skrll {
    145   1.1     skrll   /* DW_EH_PE_ values of 0x60 and 0x70 weren't defined at the time .eh_frame
    146   1.1     skrll      was added to bfd.  */
    147   1.1     skrll   if ((encoding & 0x60) == 0x60)
    148   1.1     skrll     return 0;
    149   1.1     skrll 
    150   1.1     skrll   switch (encoding & 7)
    151   1.1     skrll     {
    152   1.1     skrll     case DW_EH_PE_udata2: return 2;
    153   1.1     skrll     case DW_EH_PE_udata4: return 4;
    154   1.1     skrll     case DW_EH_PE_udata8: return 8;
    155   1.1     skrll     case DW_EH_PE_absptr: return ptr_size;
    156   1.1     skrll     default:
    157   1.1     skrll       break;
    158   1.1     skrll     }
    159   1.1     skrll 
    160   1.1     skrll   return 0;
    161   1.1     skrll }
    162   1.1     skrll 
    163   1.1     skrll #define get_DW_EH_PE_signed(encoding) (((encoding) & DW_EH_PE_signed) != 0)
    164   1.1     skrll 
    165   1.1     skrll /* Read a width sized value from memory.  */
    166   1.1     skrll 
    167   1.1     skrll static bfd_vma
    168   1.1     skrll read_value (bfd *abfd, bfd_byte *buf, int width, int is_signed)
    169   1.1     skrll {
    170   1.1     skrll   bfd_vma value;
    171   1.1     skrll 
    172   1.1     skrll   switch (width)
    173   1.1     skrll     {
    174   1.1     skrll     case 2:
    175   1.1     skrll       if (is_signed)
    176   1.1     skrll 	value = bfd_get_signed_16 (abfd, buf);
    177   1.1     skrll       else
    178   1.1     skrll 	value = bfd_get_16 (abfd, buf);
    179   1.1     skrll       break;
    180   1.1     skrll     case 4:
    181   1.1     skrll       if (is_signed)
    182   1.1     skrll 	value = bfd_get_signed_32 (abfd, buf);
    183   1.1     skrll       else
    184   1.1     skrll 	value = bfd_get_32 (abfd, buf);
    185   1.1     skrll       break;
    186   1.1     skrll     case 8:
    187   1.1     skrll       if (is_signed)
    188   1.1     skrll 	value = bfd_get_signed_64 (abfd, buf);
    189   1.1     skrll       else
    190   1.1     skrll 	value = bfd_get_64 (abfd, buf);
    191   1.1     skrll       break;
    192   1.1     skrll     default:
    193   1.1     skrll       BFD_FAIL ();
    194   1.1     skrll       return 0;
    195   1.1     skrll     }
    196   1.1     skrll 
    197   1.1     skrll   return value;
    198   1.1     skrll }
    199   1.1     skrll 
    200   1.1     skrll /* Store a width sized value to memory.  */
    201   1.1     skrll 
    202   1.1     skrll static void
    203   1.1     skrll write_value (bfd *abfd, bfd_byte *buf, bfd_vma value, int width)
    204   1.1     skrll {
    205   1.1     skrll   switch (width)
    206   1.1     skrll     {
    207   1.1     skrll     case 2: bfd_put_16 (abfd, value, buf); break;
    208   1.1     skrll     case 4: bfd_put_32 (abfd, value, buf); break;
    209   1.1     skrll     case 8: bfd_put_64 (abfd, value, buf); break;
    210   1.1     skrll     default: BFD_FAIL ();
    211   1.1     skrll     }
    212   1.1     skrll }
    213   1.1     skrll 
    214   1.1     skrll /* Return one if C1 and C2 CIEs can be merged.  */
    215   1.1     skrll 
    216   1.1     skrll static int
    217   1.1     skrll cie_eq (const void *e1, const void *e2)
    218   1.1     skrll {
    219   1.3  christos   const struct cie *c1 = (const struct cie *) e1;
    220   1.3  christos   const struct cie *c2 = (const struct cie *) e2;
    221   1.1     skrll 
    222   1.1     skrll   if (c1->hash == c2->hash
    223   1.1     skrll       && c1->length == c2->length
    224   1.1     skrll       && c1->version == c2->version
    225   1.1     skrll       && c1->local_personality == c2->local_personality
    226   1.1     skrll       && strcmp (c1->augmentation, c2->augmentation) == 0
    227   1.1     skrll       && strcmp (c1->augmentation, "eh") != 0
    228   1.1     skrll       && c1->code_align == c2->code_align
    229   1.1     skrll       && c1->data_align == c2->data_align
    230   1.1     skrll       && c1->ra_column == c2->ra_column
    231   1.1     skrll       && c1->augmentation_size == c2->augmentation_size
    232   1.1     skrll       && memcmp (&c1->personality, &c2->personality,
    233   1.1     skrll 		 sizeof (c1->personality)) == 0
    234   1.5  christos       && (c1->cie_inf->u.cie.u.sec->output_section
    235   1.5  christos 	  == c2->cie_inf->u.cie.u.sec->output_section)
    236   1.1     skrll       && c1->per_encoding == c2->per_encoding
    237   1.1     skrll       && c1->lsda_encoding == c2->lsda_encoding
    238   1.1     skrll       && c1->fde_encoding == c2->fde_encoding
    239   1.1     skrll       && c1->initial_insn_length == c2->initial_insn_length
    240   1.5  christos       && c1->initial_insn_length <= sizeof (c1->initial_instructions)
    241   1.1     skrll       && memcmp (c1->initial_instructions,
    242   1.1     skrll 		 c2->initial_instructions,
    243   1.1     skrll 		 c1->initial_insn_length) == 0)
    244   1.1     skrll     return 1;
    245   1.1     skrll 
    246   1.1     skrll   return 0;
    247   1.1     skrll }
    248   1.1     skrll 
    249   1.1     skrll static hashval_t
    250   1.1     skrll cie_hash (const void *e)
    251   1.1     skrll {
    252   1.3  christos   const struct cie *c = (const struct cie *) e;
    253   1.1     skrll   return c->hash;
    254   1.1     skrll }
    255   1.1     skrll 
    256   1.1     skrll static hashval_t
    257   1.1     skrll cie_compute_hash (struct cie *c)
    258   1.1     skrll {
    259   1.1     skrll   hashval_t h = 0;
    260   1.5  christos   size_t len;
    261   1.1     skrll   h = iterative_hash_object (c->length, h);
    262   1.1     skrll   h = iterative_hash_object (c->version, h);
    263   1.1     skrll   h = iterative_hash (c->augmentation, strlen (c->augmentation) + 1, h);
    264   1.1     skrll   h = iterative_hash_object (c->code_align, h);
    265   1.1     skrll   h = iterative_hash_object (c->data_align, h);
    266   1.1     skrll   h = iterative_hash_object (c->ra_column, h);
    267   1.1     skrll   h = iterative_hash_object (c->augmentation_size, h);
    268   1.1     skrll   h = iterative_hash_object (c->personality, h);
    269   1.5  christos   h = iterative_hash_object (c->cie_inf->u.cie.u.sec->output_section, h);
    270   1.1     skrll   h = iterative_hash_object (c->per_encoding, h);
    271   1.1     skrll   h = iterative_hash_object (c->lsda_encoding, h);
    272   1.1     skrll   h = iterative_hash_object (c->fde_encoding, h);
    273   1.1     skrll   h = iterative_hash_object (c->initial_insn_length, h);
    274   1.5  christos   len = c->initial_insn_length;
    275   1.5  christos   if (len > sizeof (c->initial_instructions))
    276   1.5  christos     len = sizeof (c->initial_instructions);
    277   1.5  christos   h = iterative_hash (c->initial_instructions, len, h);
    278   1.1     skrll   c->hash = h;
    279   1.1     skrll   return h;
    280   1.1     skrll }
    281   1.1     skrll 
    282   1.1     skrll /* Return the number of extra bytes that we'll be inserting into
    283   1.1     skrll    ENTRY's augmentation string.  */
    284   1.1     skrll 
    285  1.10  christos static inline unsigned int
    286   1.1     skrll extra_augmentation_string_bytes (struct eh_cie_fde *entry)
    287   1.1     skrll {
    288   1.1     skrll   unsigned int size = 0;
    289   1.1     skrll   if (entry->cie)
    290   1.1     skrll     {
    291   1.1     skrll       if (entry->add_augmentation_size)
    292   1.1     skrll 	size++;
    293   1.1     skrll       if (entry->u.cie.add_fde_encoding)
    294   1.1     skrll 	size++;
    295   1.1     skrll     }
    296   1.1     skrll   return size;
    297   1.1     skrll }
    298   1.1     skrll 
    299   1.1     skrll /* Likewise ENTRY's augmentation data.  */
    300   1.1     skrll 
    301  1.10  christos static inline unsigned int
    302   1.1     skrll extra_augmentation_data_bytes (struct eh_cie_fde *entry)
    303   1.1     skrll {
    304   1.1     skrll   unsigned int size = 0;
    305   1.1     skrll   if (entry->add_augmentation_size)
    306   1.1     skrll     size++;
    307   1.1     skrll   if (entry->cie && entry->u.cie.add_fde_encoding)
    308   1.1     skrll     size++;
    309   1.1     skrll   return size;
    310   1.1     skrll }
    311   1.1     skrll 
    312   1.7  christos /* Return the size that ENTRY will have in the output.  */
    313   1.1     skrll 
    314   1.1     skrll static unsigned int
    315   1.7  christos size_of_output_cie_fde (struct eh_cie_fde *entry)
    316   1.1     skrll {
    317   1.1     skrll   if (entry->removed)
    318   1.1     skrll     return 0;
    319   1.1     skrll   if (entry->size == 4)
    320   1.1     skrll     return 4;
    321   1.1     skrll   return (entry->size
    322   1.1     skrll 	  + extra_augmentation_string_bytes (entry)
    323   1.7  christos 	  + extra_augmentation_data_bytes (entry));
    324   1.7  christos }
    325   1.7  christos 
    326   1.7  christos /* Return the offset of the FDE or CIE after ENT.  */
    327   1.7  christos 
    328   1.7  christos static unsigned int
    329   1.7  christos next_cie_fde_offset (const struct eh_cie_fde *ent,
    330   1.7  christos 		     const struct eh_cie_fde *last,
    331   1.7  christos 		     const asection *sec)
    332   1.7  christos {
    333   1.7  christos   while (++ent < last)
    334   1.7  christos     {
    335   1.7  christos       if (!ent->removed)
    336   1.7  christos 	return ent->new_offset;
    337   1.7  christos     }
    338   1.7  christos   return sec->size;
    339   1.1     skrll }
    340   1.1     skrll 
    341   1.1     skrll /* Assume that the bytes between *ITER and END are CFA instructions.
    342   1.1     skrll    Try to move *ITER past the first instruction and return true on
    343   1.1     skrll    success.  ENCODED_PTR_WIDTH gives the width of pointer entries.  */
    344   1.1     skrll 
    345  1.10  christos static bool
    346   1.1     skrll skip_cfa_op (bfd_byte **iter, bfd_byte *end, unsigned int encoded_ptr_width)
    347   1.1     skrll {
    348  1.12  christos   bfd_byte op = 0;
    349   1.1     skrll   bfd_vma length;
    350   1.1     skrll 
    351   1.1     skrll   if (!read_byte (iter, end, &op))
    352  1.10  christos     return false;
    353   1.1     skrll 
    354   1.1     skrll   switch (op & 0xc0 ? op & 0xc0 : op)
    355   1.1     skrll     {
    356   1.1     skrll     case DW_CFA_nop:
    357   1.1     skrll     case DW_CFA_advance_loc:
    358   1.1     skrll     case DW_CFA_restore:
    359   1.1     skrll     case DW_CFA_remember_state:
    360   1.1     skrll     case DW_CFA_restore_state:
    361   1.1     skrll     case DW_CFA_GNU_window_save:
    362  1.12  christos     case DW_CFA_AARCH64_negate_ra_state_with_pc:
    363   1.1     skrll       /* No arguments.  */
    364  1.10  christos       return true;
    365   1.1     skrll 
    366   1.1     skrll     case DW_CFA_offset:
    367   1.1     skrll     case DW_CFA_restore_extended:
    368   1.1     skrll     case DW_CFA_undefined:
    369   1.1     skrll     case DW_CFA_same_value:
    370   1.1     skrll     case DW_CFA_def_cfa_register:
    371   1.1     skrll     case DW_CFA_def_cfa_offset:
    372   1.1     skrll     case DW_CFA_def_cfa_offset_sf:
    373   1.1     skrll     case DW_CFA_GNU_args_size:
    374   1.1     skrll       /* One leb128 argument.  */
    375   1.1     skrll       return skip_leb128 (iter, end);
    376   1.1     skrll 
    377   1.1     skrll     case DW_CFA_val_offset:
    378   1.1     skrll     case DW_CFA_val_offset_sf:
    379   1.1     skrll     case DW_CFA_offset_extended:
    380   1.1     skrll     case DW_CFA_register:
    381   1.1     skrll     case DW_CFA_def_cfa:
    382   1.1     skrll     case DW_CFA_offset_extended_sf:
    383   1.1     skrll     case DW_CFA_GNU_negative_offset_extended:
    384   1.1     skrll     case DW_CFA_def_cfa_sf:
    385   1.1     skrll       /* Two leb128 arguments.  */
    386   1.1     skrll       return (skip_leb128 (iter, end)
    387   1.1     skrll 	      && skip_leb128 (iter, end));
    388   1.1     skrll 
    389   1.1     skrll     case DW_CFA_def_cfa_expression:
    390   1.1     skrll       /* A variable-length argument.  */
    391   1.1     skrll       return (read_uleb128 (iter, end, &length)
    392   1.1     skrll 	      && skip_bytes (iter, end, length));
    393   1.1     skrll 
    394   1.1     skrll     case DW_CFA_expression:
    395   1.1     skrll     case DW_CFA_val_expression:
    396   1.1     skrll       /* A leb128 followed by a variable-length argument.  */
    397   1.1     skrll       return (skip_leb128 (iter, end)
    398   1.1     skrll 	      && read_uleb128 (iter, end, &length)
    399   1.1     skrll 	      && skip_bytes (iter, end, length));
    400   1.1     skrll 
    401   1.1     skrll     case DW_CFA_set_loc:
    402   1.1     skrll       return skip_bytes (iter, end, encoded_ptr_width);
    403   1.1     skrll 
    404   1.1     skrll     case DW_CFA_advance_loc1:
    405   1.1     skrll       return skip_bytes (iter, end, 1);
    406   1.1     skrll 
    407   1.1     skrll     case DW_CFA_advance_loc2:
    408   1.1     skrll       return skip_bytes (iter, end, 2);
    409   1.1     skrll 
    410   1.1     skrll     case DW_CFA_advance_loc4:
    411   1.1     skrll       return skip_bytes (iter, end, 4);
    412   1.1     skrll 
    413   1.1     skrll     case DW_CFA_MIPS_advance_loc8:
    414   1.1     skrll       return skip_bytes (iter, end, 8);
    415   1.1     skrll 
    416   1.1     skrll     default:
    417  1.10  christos       return false;
    418   1.1     skrll     }
    419   1.1     skrll }
    420   1.1     skrll 
    421   1.1     skrll /* Try to interpret the bytes between BUF and END as CFA instructions.
    422   1.1     skrll    If every byte makes sense, return a pointer to the first DW_CFA_nop
    423   1.1     skrll    padding byte, or END if there is no padding.  Return null otherwise.
    424   1.1     skrll    ENCODED_PTR_WIDTH is as for skip_cfa_op.  */
    425   1.1     skrll 
    426   1.1     skrll static bfd_byte *
    427   1.1     skrll skip_non_nops (bfd_byte *buf, bfd_byte *end, unsigned int encoded_ptr_width,
    428   1.1     skrll 	       unsigned int *set_loc_count)
    429   1.1     skrll {
    430   1.1     skrll   bfd_byte *last;
    431   1.1     skrll 
    432   1.1     skrll   last = buf;
    433   1.1     skrll   while (buf < end)
    434   1.1     skrll     if (*buf == DW_CFA_nop)
    435   1.1     skrll       buf++;
    436   1.1     skrll     else
    437   1.1     skrll       {
    438   1.1     skrll 	if (*buf == DW_CFA_set_loc)
    439   1.1     skrll 	  ++*set_loc_count;
    440   1.1     skrll 	if (!skip_cfa_op (&buf, end, encoded_ptr_width))
    441   1.1     skrll 	  return 0;
    442   1.1     skrll 	last = buf;
    443   1.1     skrll       }
    444   1.1     skrll   return last;
    445   1.1     skrll }
    446   1.1     skrll 
    447   1.3  christos /* Convert absolute encoding ENCODING into PC-relative form.
    448   1.3  christos    SIZE is the size of a pointer.  */
    449   1.3  christos 
    450   1.3  christos static unsigned char
    451   1.3  christos make_pc_relative (unsigned char encoding, unsigned int ptr_size)
    452   1.3  christos {
    453   1.3  christos   if ((encoding & 0x7f) == DW_EH_PE_absptr)
    454   1.3  christos     switch (ptr_size)
    455   1.3  christos       {
    456   1.3  christos       case 2:
    457   1.3  christos 	encoding |= DW_EH_PE_sdata2;
    458   1.3  christos 	break;
    459   1.3  christos       case 4:
    460   1.3  christos 	encoding |= DW_EH_PE_sdata4;
    461   1.3  christos 	break;
    462   1.3  christos       case 8:
    463   1.3  christos 	encoding |= DW_EH_PE_sdata8;
    464   1.3  christos 	break;
    465   1.3  christos       }
    466   1.3  christos   return encoding | DW_EH_PE_pcrel;
    467   1.3  christos }
    468   1.3  christos 
    469   1.5  christos /*  Examine each .eh_frame_entry section and discard those
    470   1.5  christos     those that are marked SEC_EXCLUDE.  */
    471   1.1     skrll 
    472   1.5  christos static void
    473   1.5  christos bfd_elf_discard_eh_frame_entry (struct eh_frame_hdr_info *hdr_info)
    474   1.5  christos {
    475   1.5  christos   unsigned int i;
    476   1.5  christos   for (i = 0; i < hdr_info->array_count; i++)
    477   1.5  christos     {
    478   1.5  christos       if (hdr_info->u.compact.entries[i]->flags & SEC_EXCLUDE)
    479   1.5  christos 	{
    480   1.5  christos 	  unsigned int j;
    481   1.5  christos 	  for (j = i + 1; j < hdr_info->array_count; j++)
    482   1.5  christos 	    hdr_info->u.compact.entries[j-1] = hdr_info->u.compact.entries[j];
    483   1.5  christos 
    484   1.5  christos 	  hdr_info->array_count--;
    485   1.5  christos 	  hdr_info->u.compact.entries[hdr_info->array_count] = NULL;
    486   1.5  christos 	  i--;
    487   1.7  christos 	}
    488   1.5  christos     }
    489   1.5  christos }
    490   1.5  christos 
    491   1.5  christos /* Add a .eh_frame_entry section.  */
    492   1.5  christos 
    493   1.5  christos static void
    494   1.5  christos bfd_elf_record_eh_frame_entry (struct eh_frame_hdr_info *hdr_info,
    495   1.5  christos 				 asection *sec)
    496   1.5  christos {
    497   1.5  christos   if (hdr_info->array_count == hdr_info->u.compact.allocated_entries)
    498   1.5  christos     {
    499   1.5  christos       if (hdr_info->u.compact.allocated_entries == 0)
    500   1.5  christos 	{
    501  1.10  christos 	  hdr_info->frame_hdr_is_compact = true;
    502   1.5  christos 	  hdr_info->u.compact.allocated_entries = 2;
    503   1.5  christos 	  hdr_info->u.compact.entries =
    504   1.5  christos 	    bfd_malloc (hdr_info->u.compact.allocated_entries
    505   1.5  christos 			* sizeof (hdr_info->u.compact.entries[0]));
    506   1.5  christos 	}
    507   1.5  christos       else
    508   1.5  christos 	{
    509   1.5  christos 	  hdr_info->u.compact.allocated_entries *= 2;
    510   1.5  christos 	  hdr_info->u.compact.entries =
    511   1.5  christos 	    bfd_realloc (hdr_info->u.compact.entries,
    512   1.5  christos 			 hdr_info->u.compact.allocated_entries
    513   1.5  christos 			   * sizeof (hdr_info->u.compact.entries[0]));
    514   1.5  christos 	}
    515   1.5  christos 
    516   1.5  christos       BFD_ASSERT (hdr_info->u.compact.entries);
    517   1.5  christos     }
    518   1.5  christos 
    519   1.5  christos   hdr_info->u.compact.entries[hdr_info->array_count++] = sec;
    520   1.5  christos }
    521   1.5  christos 
    522   1.5  christos /* Parse a .eh_frame_entry section.  Figure out which text section it
    523   1.5  christos    references.  */
    524   1.5  christos 
    525  1.10  christos bool
    526   1.5  christos _bfd_elf_parse_eh_frame_entry (struct bfd_link_info *info,
    527   1.5  christos 			       asection *sec, struct elf_reloc_cookie *cookie)
    528   1.1     skrll {
    529   1.5  christos   struct elf_link_hash_table *htab;
    530   1.1     skrll   struct eh_frame_hdr_info *hdr_info;
    531   1.5  christos   unsigned long r_symndx;
    532   1.5  christos   asection *text_sec;
    533   1.5  christos 
    534   1.5  christos   htab = elf_hash_table (info);
    535   1.5  christos   hdr_info = &htab->eh_info;
    536   1.5  christos 
    537   1.5  christos   if (sec->size == 0
    538   1.5  christos       || sec->sec_info_type != SEC_INFO_TYPE_NONE)
    539   1.5  christos     {
    540  1.10  christos       return true;
    541   1.5  christos     }
    542   1.5  christos 
    543   1.5  christos   if (sec->output_section && bfd_is_abs_section (sec->output_section))
    544   1.5  christos     {
    545   1.5  christos       /* At least one of the sections is being discarded from the
    546   1.5  christos 	 link, so we should just ignore them.  */
    547  1.10  christos       return true;
    548   1.5  christos     }
    549   1.1     skrll 
    550   1.5  christos   if (cookie->rel == cookie->relend)
    551  1.10  christos     return false;
    552   1.5  christos 
    553   1.5  christos   /* The first relocation is the function start.  */
    554   1.5  christos   r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
    555   1.5  christos   if (r_symndx == STN_UNDEF)
    556  1.10  christos     return false;
    557   1.5  christos 
    558  1.13  christos   text_sec = _bfd_elf_section_for_symbol (cookie, r_symndx);
    559   1.5  christos 
    560   1.5  christos   if (text_sec == NULL)
    561  1.10  christos     return false;
    562   1.5  christos 
    563   1.5  christos   elf_section_eh_frame_entry (text_sec) = sec;
    564   1.5  christos   if (text_sec->output_section
    565   1.5  christos       && bfd_is_abs_section (text_sec->output_section))
    566   1.5  christos     sec->flags |= SEC_EXCLUDE;
    567   1.5  christos 
    568   1.5  christos   sec->sec_info_type = SEC_INFO_TYPE_EH_FRAME_ENTRY;
    569  1.13  christos   sec->sec_info = text_sec;
    570   1.5  christos   bfd_elf_record_eh_frame_entry (hdr_info, sec);
    571  1.10  christos   return true;
    572   1.1     skrll }
    573   1.1     skrll 
    574   1.1     skrll /* Try to parse .eh_frame section SEC, which belongs to ABFD.  Store the
    575   1.1     skrll    information in the section's sec_info field on success.  COOKIE
    576   1.1     skrll    describes the relocations in SEC.  */
    577   1.1     skrll 
    578   1.1     skrll void
    579   1.1     skrll _bfd_elf_parse_eh_frame (bfd *abfd, struct bfd_link_info *info,
    580   1.1     skrll 			 asection *sec, struct elf_reloc_cookie *cookie)
    581   1.1     skrll {
    582   1.1     skrll #define REQUIRE(COND)					\
    583   1.1     skrll   do							\
    584   1.1     skrll     if (!(COND))					\
    585   1.1     skrll       goto free_no_table;				\
    586   1.1     skrll   while (0)
    587   1.1     skrll 
    588   1.1     skrll   bfd_byte *ehbuf = NULL, *buf, *end;
    589   1.1     skrll   bfd_byte *last_fde;
    590   1.1     skrll   struct eh_cie_fde *this_inf;
    591   1.1     skrll   unsigned int hdr_length, hdr_id;
    592   1.1     skrll   unsigned int cie_count;
    593   1.1     skrll   struct cie *cie, *local_cies = NULL;
    594   1.1     skrll   struct elf_link_hash_table *htab;
    595   1.1     skrll   struct eh_frame_hdr_info *hdr_info;
    596   1.1     skrll   struct eh_frame_sec_info *sec_info = NULL;
    597   1.1     skrll   unsigned int ptr_size;
    598   1.1     skrll   unsigned int num_cies;
    599   1.1     skrll   unsigned int num_entries;
    600   1.1     skrll   elf_gc_mark_hook_fn gc_mark_hook;
    601   1.1     skrll 
    602   1.1     skrll   htab = elf_hash_table (info);
    603   1.1     skrll   hdr_info = &htab->eh_info;
    604   1.1     skrll 
    605   1.4  christos   if (sec->size == 0
    606  1.11  christos       || (sec->flags & SEC_HAS_CONTENTS) == 0
    607   1.4  christos       || sec->sec_info_type != SEC_INFO_TYPE_NONE)
    608   1.1     skrll     {
    609  1.12  christos       /* This file does not contain .eh_frame information or
    610  1.12  christos 	 .eh_frame has already been parsed, as can happen with
    611  1.12  christos 	 --gc-sections.  */
    612   1.1     skrll       return;
    613   1.1     skrll     }
    614   1.1     skrll 
    615   1.1     skrll   if (bfd_is_abs_section (sec->output_section))
    616   1.1     skrll     {
    617   1.1     skrll       /* At least one of the sections is being discarded from the
    618   1.1     skrll 	 link, so we should just ignore them.  */
    619   1.1     skrll       return;
    620   1.1     skrll     }
    621   1.1     skrll 
    622   1.1     skrll   /* Read the frame unwind information from abfd.  */
    623   1.1     skrll 
    624  1.12  christos   REQUIRE (_bfd_elf_mmap_section_contents (abfd, sec, &ehbuf));
    625   1.1     skrll 
    626   1.1     skrll   /* If .eh_frame section size doesn't fit into int, we cannot handle
    627   1.1     skrll      it (it would need to use 64-bit .eh_frame format anyway).  */
    628   1.1     skrll   REQUIRE (sec->size == (unsigned int) sec->size);
    629   1.1     skrll 
    630   1.1     skrll   ptr_size = (get_elf_backend_data (abfd)
    631   1.1     skrll 	      ->elf_backend_eh_frame_address_size (abfd, sec));
    632   1.1     skrll   REQUIRE (ptr_size != 0);
    633   1.1     skrll 
    634   1.1     skrll   /* Go through the section contents and work out how many FDEs and
    635   1.1     skrll      CIEs there are.  */
    636   1.1     skrll   buf = ehbuf;
    637   1.1     skrll   end = ehbuf + sec->size;
    638   1.1     skrll   num_cies = 0;
    639   1.1     skrll   num_entries = 0;
    640   1.1     skrll   while (buf != end)
    641   1.1     skrll     {
    642   1.1     skrll       num_entries++;
    643   1.1     skrll 
    644   1.1     skrll       /* Read the length of the entry.  */
    645   1.1     skrll       REQUIRE (skip_bytes (&buf, end, 4));
    646   1.1     skrll       hdr_length = bfd_get_32 (abfd, buf - 4);
    647   1.1     skrll 
    648   1.1     skrll       /* 64-bit .eh_frame is not supported.  */
    649   1.1     skrll       REQUIRE (hdr_length != 0xffffffff);
    650   1.1     skrll       if (hdr_length == 0)
    651   1.1     skrll 	break;
    652   1.1     skrll 
    653   1.1     skrll       REQUIRE (skip_bytes (&buf, end, 4));
    654   1.1     skrll       hdr_id = bfd_get_32 (abfd, buf - 4);
    655   1.1     skrll       if (hdr_id == 0)
    656   1.1     skrll 	num_cies++;
    657   1.1     skrll 
    658   1.1     skrll       REQUIRE (skip_bytes (&buf, end, hdr_length - 4));
    659   1.1     skrll     }
    660   1.1     skrll 
    661  1.12  christos   sec_info = bfd_zalloc (abfd,
    662  1.12  christos 			 (sizeof (struct eh_frame_sec_info)
    663  1.12  christos 			  + (num_entries - 1) * sizeof (struct eh_cie_fde)));
    664   1.1     skrll   REQUIRE (sec_info);
    665   1.1     skrll 
    666   1.1     skrll   /* We need to have a "struct cie" for each CIE in this section.  */
    667   1.7  christos   if (num_cies)
    668   1.7  christos     {
    669   1.7  christos       local_cies = (struct cie *) bfd_zmalloc (num_cies * sizeof (*local_cies));
    670   1.7  christos       REQUIRE (local_cies);
    671   1.7  christos     }
    672   1.1     skrll 
    673   1.3  christos   /* FIXME: octets_per_byte.  */
    674   1.1     skrll #define ENSURE_NO_RELOCS(buf)				\
    675   1.5  christos   while (cookie->rel < cookie->relend			\
    676   1.5  christos 	 && (cookie->rel->r_offset			\
    677   1.5  christos 	     < (bfd_size_type) ((buf) - ehbuf)))	\
    678   1.5  christos     {							\
    679   1.5  christos       REQUIRE (cookie->rel->r_info == 0);		\
    680   1.5  christos       cookie->rel++;					\
    681   1.5  christos     }
    682   1.1     skrll 
    683   1.3  christos   /* FIXME: octets_per_byte.  */
    684   1.1     skrll #define SKIP_RELOCS(buf)				\
    685   1.1     skrll   while (cookie->rel < cookie->relend			\
    686   1.1     skrll 	 && (cookie->rel->r_offset			\
    687   1.1     skrll 	     < (bfd_size_type) ((buf) - ehbuf)))	\
    688   1.1     skrll     cookie->rel++
    689   1.1     skrll 
    690   1.3  christos   /* FIXME: octets_per_byte.  */
    691   1.1     skrll #define GET_RELOC(buf)					\
    692   1.1     skrll   ((cookie->rel < cookie->relend			\
    693   1.1     skrll     && (cookie->rel->r_offset				\
    694   1.1     skrll 	== (bfd_size_type) ((buf) - ehbuf)))		\
    695   1.1     skrll    ? cookie->rel : NULL)
    696   1.1     skrll 
    697   1.1     skrll   buf = ehbuf;
    698   1.1     skrll   cie_count = 0;
    699   1.1     skrll   gc_mark_hook = get_elf_backend_data (abfd)->gc_mark_hook;
    700   1.1     skrll   while ((bfd_size_type) (buf - ehbuf) != sec->size)
    701   1.1     skrll     {
    702   1.1     skrll       char *aug;
    703   1.1     skrll       bfd_byte *start, *insns, *insns_end;
    704   1.1     skrll       bfd_size_type length;
    705   1.1     skrll       unsigned int set_loc_count;
    706   1.1     skrll 
    707   1.1     skrll       this_inf = sec_info->entry + sec_info->count;
    708   1.1     skrll       last_fde = buf;
    709   1.1     skrll 
    710   1.1     skrll       /* Read the length of the entry.  */
    711   1.1     skrll       REQUIRE (skip_bytes (&buf, ehbuf + sec->size, 4));
    712   1.1     skrll       hdr_length = bfd_get_32 (abfd, buf - 4);
    713   1.1     skrll 
    714   1.1     skrll       /* The CIE/FDE must be fully contained in this input section.  */
    715   1.1     skrll       REQUIRE ((bfd_size_type) (buf - ehbuf) + hdr_length <= sec->size);
    716   1.1     skrll       end = buf + hdr_length;
    717   1.1     skrll 
    718   1.1     skrll       this_inf->offset = last_fde - ehbuf;
    719   1.1     skrll       this_inf->size = 4 + hdr_length;
    720   1.1     skrll       this_inf->reloc_index = cookie->rel - cookie->rels;
    721   1.1     skrll 
    722   1.1     skrll       if (hdr_length == 0)
    723   1.1     skrll 	{
    724   1.1     skrll 	  /* A zero-length CIE should only be found at the end of
    725   1.7  christos 	     the section, but allow multiple terminators.  */
    726   1.7  christos 	  while (skip_bytes (&buf, ehbuf + sec->size, 4))
    727   1.7  christos 	    REQUIRE (bfd_get_32 (abfd, buf - 4) == 0);
    728   1.1     skrll 	  REQUIRE ((bfd_size_type) (buf - ehbuf) == sec->size);
    729   1.1     skrll 	  ENSURE_NO_RELOCS (buf);
    730   1.1     skrll 	  sec_info->count++;
    731   1.1     skrll 	  break;
    732   1.1     skrll 	}
    733   1.1     skrll 
    734   1.1     skrll       REQUIRE (skip_bytes (&buf, end, 4));
    735   1.1     skrll       hdr_id = bfd_get_32 (abfd, buf - 4);
    736   1.1     skrll 
    737   1.1     skrll       if (hdr_id == 0)
    738   1.1     skrll 	{
    739   1.1     skrll 	  unsigned int initial_insn_length;
    740  1.13  christos 	  char *null_byte;
    741   1.1     skrll 
    742   1.1     skrll 	  /* CIE  */
    743   1.1     skrll 	  this_inf->cie = 1;
    744   1.1     skrll 
    745   1.1     skrll 	  /* Point CIE to one of the section-local cie structures.  */
    746   1.1     skrll 	  cie = local_cies + cie_count++;
    747   1.1     skrll 
    748   1.1     skrll 	  cie->cie_inf = this_inf;
    749   1.1     skrll 	  cie->length = hdr_length;
    750   1.1     skrll 	  start = buf;
    751   1.1     skrll 	  REQUIRE (read_byte (&buf, end, &cie->version));
    752   1.1     skrll 
    753   1.1     skrll 	  /* Cannot handle unknown versions.  */
    754   1.3  christos 	  REQUIRE (cie->version == 1
    755   1.3  christos 		   || cie->version == 3
    756   1.3  christos 		   || cie->version == 4);
    757  1.13  christos 	  null_byte = memchr ((char *) buf, 0, end - buf);
    758  1.13  christos 	  REQUIRE (null_byte != NULL);
    759  1.13  christos 	  REQUIRE ((size_t) (null_byte - (char *) buf)
    760  1.13  christos 		   < sizeof (cie->augmentation));
    761   1.1     skrll 
    762   1.1     skrll 	  strcpy (cie->augmentation, (char *) buf);
    763  1.13  christos 	  buf = (bfd_byte *) null_byte + 1;
    764  1.13  christos 	  REQUIRE (buf + 1 < end);
    765   1.7  christos 	  this_inf->u.cie.aug_str_len = buf - start - 1;
    766   1.1     skrll 	  ENSURE_NO_RELOCS (buf);
    767   1.1     skrll 	  if (buf[0] == 'e' && buf[1] == 'h')
    768   1.1     skrll 	    {
    769   1.1     skrll 	      /* GCC < 3.0 .eh_frame CIE */
    770   1.1     skrll 	      /* We cannot merge "eh" CIEs because __EXCEPTION_TABLE__
    771   1.1     skrll 		 is private to each CIE, so we don't need it for anything.
    772   1.1     skrll 		 Just skip it.  */
    773   1.1     skrll 	      REQUIRE (skip_bytes (&buf, end, ptr_size));
    774   1.1     skrll 	      SKIP_RELOCS (buf);
    775   1.1     skrll 	    }
    776   1.3  christos 	  if (cie->version >= 4)
    777   1.3  christos 	    {
    778   1.3  christos 	      REQUIRE (buf + 1 < end);
    779   1.3  christos 	      REQUIRE (buf[0] == ptr_size);
    780   1.3  christos 	      REQUIRE (buf[1] == 0);
    781   1.3  christos 	      buf += 2;
    782   1.3  christos 	    }
    783   1.1     skrll 	  REQUIRE (read_uleb128 (&buf, end, &cie->code_align));
    784   1.1     skrll 	  REQUIRE (read_sleb128 (&buf, end, &cie->data_align));
    785   1.1     skrll 	  if (cie->version == 1)
    786   1.1     skrll 	    {
    787   1.1     skrll 	      REQUIRE (buf < end);
    788   1.1     skrll 	      cie->ra_column = *buf++;
    789   1.1     skrll 	    }
    790   1.1     skrll 	  else
    791   1.1     skrll 	    REQUIRE (read_uleb128 (&buf, end, &cie->ra_column));
    792   1.1     skrll 	  ENSURE_NO_RELOCS (buf);
    793   1.1     skrll 	  cie->lsda_encoding = DW_EH_PE_omit;
    794   1.1     skrll 	  cie->fde_encoding = DW_EH_PE_omit;
    795   1.1     skrll 	  cie->per_encoding = DW_EH_PE_omit;
    796   1.1     skrll 	  aug = cie->augmentation;
    797   1.1     skrll 	  if (aug[0] != 'e' || aug[1] != 'h')
    798   1.1     skrll 	    {
    799   1.1     skrll 	      if (*aug == 'z')
    800   1.1     skrll 		{
    801   1.1     skrll 		  aug++;
    802   1.1     skrll 		  REQUIRE (read_uleb128 (&buf, end, &cie->augmentation_size));
    803   1.7  christos 		  ENSURE_NO_RELOCS (buf);
    804   1.1     skrll 		}
    805   1.1     skrll 
    806   1.1     skrll 	      while (*aug != '\0')
    807   1.1     skrll 		switch (*aug++)
    808   1.1     skrll 		  {
    809   1.9  christos 		  case 'B':
    810  1.12  christos 		  case 'G':
    811  1.12  christos 		    if (abfd->arch_info->arch != bfd_arch_aarch64)
    812  1.12  christos 		      goto unrecognized;
    813   1.9  christos 		    break;
    814   1.1     skrll 		  case 'L':
    815   1.1     skrll 		    REQUIRE (read_byte (&buf, end, &cie->lsda_encoding));
    816   1.1     skrll 		    ENSURE_NO_RELOCS (buf);
    817   1.1     skrll 		    REQUIRE (get_DW_EH_PE_width (cie->lsda_encoding, ptr_size));
    818   1.1     skrll 		    break;
    819   1.1     skrll 		  case 'R':
    820   1.1     skrll 		    REQUIRE (read_byte (&buf, end, &cie->fde_encoding));
    821   1.1     skrll 		    ENSURE_NO_RELOCS (buf);
    822   1.1     skrll 		    REQUIRE (get_DW_EH_PE_width (cie->fde_encoding, ptr_size));
    823   1.1     skrll 		    break;
    824   1.1     skrll 		  case 'S':
    825   1.1     skrll 		    break;
    826   1.1     skrll 		  case 'P':
    827   1.1     skrll 		    {
    828   1.1     skrll 		      int per_width;
    829   1.1     skrll 
    830   1.1     skrll 		      REQUIRE (read_byte (&buf, end, &cie->per_encoding));
    831   1.1     skrll 		      per_width = get_DW_EH_PE_width (cie->per_encoding,
    832   1.1     skrll 						      ptr_size);
    833   1.1     skrll 		      REQUIRE (per_width);
    834   1.3  christos 		      if ((cie->per_encoding & 0x70) == DW_EH_PE_aligned)
    835   1.1     skrll 			{
    836   1.1     skrll 			  length = -(buf - ehbuf) & (per_width - 1);
    837   1.1     skrll 			  REQUIRE (skip_bytes (&buf, end, length));
    838   1.7  christos 			  if (per_width == 8)
    839   1.7  christos 			    this_inf->u.cie.per_encoding_aligned8 = 1;
    840   1.1     skrll 			}
    841   1.3  christos 		      this_inf->u.cie.personality_offset = buf - start;
    842   1.1     skrll 		      ENSURE_NO_RELOCS (buf);
    843   1.1     skrll 		      /* Ensure we have a reloc here.  */
    844   1.1     skrll 		      REQUIRE (GET_RELOC (buf));
    845   1.1     skrll 		      cie->personality.reloc_index
    846   1.1     skrll 			= cookie->rel - cookie->rels;
    847   1.1     skrll 		      /* Cope with MIPS-style composite relocations.  */
    848   1.1     skrll 		      do
    849   1.1     skrll 			cookie->rel++;
    850   1.1     skrll 		      while (GET_RELOC (buf) != NULL);
    851   1.1     skrll 		      REQUIRE (skip_bytes (&buf, end, per_width));
    852   1.1     skrll 		    }
    853   1.1     skrll 		    break;
    854  1.12  christos 		  unrecognized:
    855   1.1     skrll 		  default:
    856   1.1     skrll 		    /* Unrecognized augmentation. Better bail out.  */
    857   1.1     skrll 		    goto free_no_table;
    858   1.1     skrll 		  }
    859   1.1     skrll 	    }
    860   1.7  christos 	  this_inf->u.cie.aug_data_len
    861   1.7  christos 	    = buf - start - 1 - this_inf->u.cie.aug_str_len;
    862   1.1     skrll 
    863   1.1     skrll 	  /* For shared libraries, try to get rid of as many RELATIVE relocs
    864   1.1     skrll 	     as possible.  */
    865   1.5  christos 	  if (bfd_link_pic (info)
    866   1.1     skrll 	      && (get_elf_backend_data (abfd)
    867   1.1     skrll 		  ->elf_backend_can_make_relative_eh_frame
    868   1.1     skrll 		  (abfd, info, sec)))
    869   1.1     skrll 	    {
    870   1.3  christos 	      if ((cie->fde_encoding & 0x70) == DW_EH_PE_absptr)
    871   1.1     skrll 		this_inf->make_relative = 1;
    872   1.1     skrll 	      /* If the CIE doesn't already have an 'R' entry, it's fairly
    873   1.1     skrll 		 easy to add one, provided that there's no aligned data
    874   1.1     skrll 		 after the augmentation string.  */
    875   1.1     skrll 	      else if (cie->fde_encoding == DW_EH_PE_omit
    876   1.3  christos 		       && (cie->per_encoding & 0x70) != DW_EH_PE_aligned)
    877   1.1     skrll 		{
    878   1.1     skrll 		  if (*cie->augmentation == 0)
    879   1.1     skrll 		    this_inf->add_augmentation_size = 1;
    880   1.1     skrll 		  this_inf->u.cie.add_fde_encoding = 1;
    881   1.1     skrll 		  this_inf->make_relative = 1;
    882   1.1     skrll 		}
    883   1.3  christos 
    884   1.3  christos 	      if ((cie->lsda_encoding & 0x70) == DW_EH_PE_absptr)
    885   1.3  christos 		cie->can_make_lsda_relative = 1;
    886   1.1     skrll 	    }
    887   1.1     skrll 
    888   1.1     skrll 	  /* If FDE encoding was not specified, it defaults to
    889   1.1     skrll 	     DW_EH_absptr.  */
    890   1.1     skrll 	  if (cie->fde_encoding == DW_EH_PE_omit)
    891   1.1     skrll 	    cie->fde_encoding = DW_EH_PE_absptr;
    892   1.1     skrll 
    893   1.1     skrll 	  initial_insn_length = end - buf;
    894   1.5  christos 	  cie->initial_insn_length = initial_insn_length;
    895   1.5  christos 	  memcpy (cie->initial_instructions, buf,
    896   1.5  christos 		  initial_insn_length <= sizeof (cie->initial_instructions)
    897   1.5  christos 		  ? initial_insn_length : sizeof (cie->initial_instructions));
    898   1.1     skrll 	  insns = buf;
    899   1.1     skrll 	  buf += initial_insn_length;
    900   1.1     skrll 	  ENSURE_NO_RELOCS (buf);
    901   1.1     skrll 
    902   1.5  christos 	  if (!bfd_link_relocatable (info))
    903   1.5  christos 	    {
    904   1.5  christos 	      /* Keep info for merging cies.  */
    905   1.5  christos 	      this_inf->u.cie.u.full_cie = cie;
    906   1.5  christos 	      this_inf->u.cie.per_encoding_relative
    907   1.5  christos 		= (cie->per_encoding & 0x70) == DW_EH_PE_pcrel;
    908   1.5  christos 	    }
    909   1.1     skrll 	}
    910   1.1     skrll       else
    911   1.1     skrll 	{
    912   1.1     skrll 	  /* Find the corresponding CIE.  */
    913   1.1     skrll 	  unsigned int cie_offset = this_inf->offset + 4 - hdr_id;
    914   1.1     skrll 	  for (cie = local_cies; cie < local_cies + cie_count; cie++)
    915   1.1     skrll 	    if (cie_offset == cie->cie_inf->offset)
    916   1.1     skrll 	      break;
    917   1.1     skrll 
    918   1.1     skrll 	  /* Ensure this FDE references one of the CIEs in this input
    919   1.1     skrll 	     section.  */
    920   1.1     skrll 	  REQUIRE (cie != local_cies + cie_count);
    921   1.1     skrll 	  this_inf->u.fde.cie_inf = cie->cie_inf;
    922   1.1     skrll 	  this_inf->make_relative = cie->cie_inf->make_relative;
    923   1.1     skrll 	  this_inf->add_augmentation_size
    924   1.1     skrll 	    = cie->cie_inf->add_augmentation_size;
    925   1.1     skrll 
    926   1.1     skrll 	  ENSURE_NO_RELOCS (buf);
    927   1.4  christos 	  if ((sec->flags & SEC_LINKER_CREATED) == 0 || cookie->rels != NULL)
    928   1.4  christos 	    {
    929   1.4  christos 	      asection *rsec;
    930   1.4  christos 
    931   1.4  christos 	      REQUIRE (GET_RELOC (buf));
    932   1.1     skrll 
    933   1.4  christos 	      /* Chain together the FDEs for each section.  */
    934   1.5  christos 	      rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook,
    935   1.5  christos 					    cookie, NULL);
    936   1.4  christos 	      /* RSEC will be NULL if FDE was cleared out as it was belonging to
    937   1.4  christos 		 a discarded SHT_GROUP.  */
    938   1.4  christos 	      if (rsec)
    939   1.4  christos 		{
    940   1.4  christos 		  REQUIRE (rsec->owner == abfd);
    941   1.4  christos 		  this_inf->u.fde.next_for_section = elf_fde_list (rsec);
    942   1.4  christos 		  elf_fde_list (rsec) = this_inf;
    943   1.4  christos 		}
    944   1.1     skrll 	    }
    945   1.1     skrll 
    946   1.1     skrll 	  /* Skip the initial location and address range.  */
    947   1.1     skrll 	  start = buf;
    948   1.1     skrll 	  length = get_DW_EH_PE_width (cie->fde_encoding, ptr_size);
    949   1.1     skrll 	  REQUIRE (skip_bytes (&buf, end, 2 * length));
    950   1.1     skrll 
    951   1.5  christos 	  SKIP_RELOCS (buf - length);
    952   1.5  christos 	  if (!GET_RELOC (buf - length)
    953  1.10  christos 	      && read_value (abfd, buf - length, length, false) == 0)
    954   1.5  christos 	    {
    955   1.5  christos 	      (*info->callbacks->minfo)
    956   1.7  christos 		/* xgettext:c-format */
    957   1.8  christos 		(_("discarding zero address range FDE in %pB(%pA).\n"),
    958   1.5  christos 		 abfd, sec);
    959   1.5  christos 	      this_inf->u.fde.cie_inf = NULL;
    960   1.5  christos 	    }
    961   1.5  christos 
    962   1.1     skrll 	  /* Skip the augmentation size, if present.  */
    963   1.1     skrll 	  if (cie->augmentation[0] == 'z')
    964   1.1     skrll 	    REQUIRE (read_uleb128 (&buf, end, &length));
    965   1.1     skrll 	  else
    966   1.1     skrll 	    length = 0;
    967   1.1     skrll 
    968   1.1     skrll 	  /* Of the supported augmentation characters above, only 'L'
    969   1.1     skrll 	     adds augmentation data to the FDE.  This code would need to
    970   1.1     skrll 	     be adjusted if any future augmentations do the same thing.  */
    971   1.1     skrll 	  if (cie->lsda_encoding != DW_EH_PE_omit)
    972   1.1     skrll 	    {
    973   1.1     skrll 	      SKIP_RELOCS (buf);
    974   1.1     skrll 	      if (cie->can_make_lsda_relative && GET_RELOC (buf))
    975   1.1     skrll 		cie->cie_inf->u.cie.make_lsda_relative = 1;
    976   1.1     skrll 	      this_inf->lsda_offset = buf - start;
    977   1.1     skrll 	      /* If there's no 'z' augmentation, we don't know where the
    978   1.1     skrll 		 CFA insns begin.  Assume no padding.  */
    979   1.1     skrll 	      if (cie->augmentation[0] != 'z')
    980   1.1     skrll 		length = end - buf;
    981   1.1     skrll 	    }
    982   1.1     skrll 
    983   1.1     skrll 	  /* Skip over the augmentation data.  */
    984   1.1     skrll 	  REQUIRE (skip_bytes (&buf, end, length));
    985   1.1     skrll 	  insns = buf;
    986   1.1     skrll 
    987   1.1     skrll 	  buf = last_fde + 4 + hdr_length;
    988   1.1     skrll 
    989   1.2     skrll 	  /* For NULL RSEC (cleared FDE belonging to a discarded section)
    990   1.2     skrll 	     the relocations are commonly cleared.  We do not sanity check if
    991   1.2     skrll 	     all these relocations are cleared as (1) relocations to
    992   1.2     skrll 	     .gcc_except_table will remain uncleared (they will get dropped
    993   1.2     skrll 	     with the drop of this unused FDE) and (2) BFD already safely drops
    994   1.2     skrll 	     relocations of any type to .eh_frame by
    995   1.2     skrll 	     elf_section_ignore_discarded_relocs.
    996   1.2     skrll 	     TODO: The .gcc_except_table entries should be also filtered as
    997   1.2     skrll 	     .eh_frame entries; or GCC could rather use COMDAT for them.  */
    998   1.2     skrll 	  SKIP_RELOCS (buf);
    999   1.1     skrll 	}
   1000   1.1     skrll 
   1001   1.1     skrll       /* Try to interpret the CFA instructions and find the first
   1002   1.1     skrll 	 padding nop.  Shrink this_inf's size so that it doesn't
   1003   1.1     skrll 	 include the padding.  */
   1004   1.1     skrll       length = get_DW_EH_PE_width (cie->fde_encoding, ptr_size);
   1005   1.1     skrll       set_loc_count = 0;
   1006   1.1     skrll       insns_end = skip_non_nops (insns, end, length, &set_loc_count);
   1007   1.1     skrll       /* If we don't understand the CFA instructions, we can't know
   1008   1.1     skrll 	 what needs to be adjusted there.  */
   1009   1.1     skrll       if (insns_end == NULL
   1010   1.1     skrll 	  /* For the time being we don't support DW_CFA_set_loc in
   1011   1.1     skrll 	     CIE instructions.  */
   1012   1.1     skrll 	  || (set_loc_count && this_inf->cie))
   1013   1.1     skrll 	goto free_no_table;
   1014   1.1     skrll       this_inf->size -= end - insns_end;
   1015   1.1     skrll       if (insns_end != end && this_inf->cie)
   1016   1.1     skrll 	{
   1017   1.1     skrll 	  cie->initial_insn_length -= end - insns_end;
   1018   1.1     skrll 	  cie->length -= end - insns_end;
   1019   1.1     skrll 	}
   1020   1.1     skrll       if (set_loc_count
   1021   1.3  christos 	  && ((cie->fde_encoding & 0x70) == DW_EH_PE_pcrel
   1022   1.1     skrll 	      || this_inf->make_relative))
   1023   1.1     skrll 	{
   1024   1.1     skrll 	  unsigned int cnt;
   1025   1.1     skrll 	  bfd_byte *p;
   1026   1.1     skrll 
   1027  1.12  christos 	  this_inf->set_loc
   1028  1.12  christos 	    = bfd_alloc (abfd, (set_loc_count + 1) * sizeof (unsigned int));
   1029   1.1     skrll 	  REQUIRE (this_inf->set_loc);
   1030   1.1     skrll 	  this_inf->set_loc[0] = set_loc_count;
   1031   1.1     skrll 	  p = insns;
   1032   1.1     skrll 	  cnt = 0;
   1033   1.1     skrll 	  while (p < end)
   1034   1.1     skrll 	    {
   1035   1.1     skrll 	      if (*p == DW_CFA_set_loc)
   1036   1.1     skrll 		this_inf->set_loc[++cnt] = p + 1 - start;
   1037   1.1     skrll 	      REQUIRE (skip_cfa_op (&p, end, length));
   1038   1.1     skrll 	    }
   1039   1.1     skrll 	}
   1040   1.1     skrll 
   1041   1.1     skrll       this_inf->removed = 1;
   1042   1.1     skrll       this_inf->fde_encoding = cie->fde_encoding;
   1043   1.1     skrll       this_inf->lsda_encoding = cie->lsda_encoding;
   1044   1.1     skrll       sec_info->count++;
   1045   1.1     skrll     }
   1046   1.1     skrll   BFD_ASSERT (sec_info->count == num_entries);
   1047   1.1     skrll   BFD_ASSERT (cie_count == num_cies);
   1048   1.1     skrll 
   1049  1.13  christos   sec->sec_info = sec_info;
   1050   1.4  christos   sec->sec_info_type = SEC_INFO_TYPE_EH_FRAME;
   1051   1.5  christos   if (!bfd_link_relocatable (info))
   1052   1.1     skrll     {
   1053   1.5  christos       /* Keep info for merging cies.  */
   1054   1.1     skrll       sec_info->cies = local_cies;
   1055   1.1     skrll       local_cies = NULL;
   1056   1.1     skrll     }
   1057   1.1     skrll   goto success;
   1058   1.1     skrll 
   1059   1.1     skrll  free_no_table:
   1060   1.8  christos   _bfd_error_handler
   1061   1.7  christos     /* xgettext:c-format */
   1062   1.8  christos     (_("error in %pB(%pA); no .eh_frame_hdr table will be created"),
   1063   1.1     skrll      abfd, sec);
   1064  1.10  christos   hdr_info->u.dwarf.table = false;
   1065   1.1     skrll  success:
   1066  1.12  christos   _bfd_elf_munmap_section_contents (sec, ehbuf);
   1067  1.10  christos   free (local_cies);
   1068   1.1     skrll #undef REQUIRE
   1069   1.1     skrll }
   1070   1.1     skrll 
   1071   1.5  christos /* Order eh_frame_hdr entries by the VMA of their text section.  */
   1072   1.5  christos 
   1073   1.5  christos static int
   1074   1.5  christos cmp_eh_frame_hdr (const void *a, const void *b)
   1075   1.5  christos {
   1076   1.5  christos   bfd_vma text_a;
   1077   1.5  christos   bfd_vma text_b;
   1078   1.5  christos   asection *sec;
   1079   1.5  christos 
   1080   1.5  christos   sec = *(asection *const *)a;
   1081  1.13  christos   sec = sec->sec_info;
   1082   1.5  christos   text_a = sec->output_section->vma + sec->output_offset;
   1083   1.5  christos   sec = *(asection *const *)b;
   1084  1.13  christos   sec = sec->sec_info;
   1085   1.5  christos   text_b = sec->output_section->vma + sec->output_offset;
   1086   1.5  christos 
   1087   1.5  christos   if (text_a < text_b)
   1088   1.5  christos     return -1;
   1089   1.5  christos   return text_a > text_b;
   1090   1.5  christos 
   1091   1.5  christos }
   1092   1.5  christos 
   1093   1.5  christos /* Add space for a CANTUNWIND terminator to SEC if the text sections
   1094   1.5  christos    referenced by it and NEXT are not contiguous, or NEXT is NULL.  */
   1095   1.5  christos 
   1096   1.5  christos static void
   1097   1.5  christos add_eh_frame_hdr_terminator (asection *sec,
   1098   1.5  christos 			     asection *next)
   1099   1.5  christos {
   1100   1.5  christos   bfd_vma end;
   1101   1.5  christos   bfd_vma next_start;
   1102   1.5  christos   asection *text_sec;
   1103   1.5  christos 
   1104   1.5  christos   if (next)
   1105   1.5  christos     {
   1106   1.5  christos       /* See if there is a gap (presumably a text section without unwind info)
   1107   1.5  christos 	 between these two entries.  */
   1108  1.13  christos       text_sec = sec->sec_info;
   1109   1.5  christos       end = text_sec->output_section->vma + text_sec->output_offset
   1110   1.5  christos 	    + text_sec->size;
   1111  1.13  christos       text_sec = next->sec_info;
   1112   1.5  christos       next_start = text_sec->output_section->vma + text_sec->output_offset;
   1113   1.5  christos       if (end == next_start)
   1114   1.5  christos 	return;
   1115   1.5  christos     }
   1116   1.5  christos 
   1117   1.5  christos   /* Add space for a CANTUNWIND terminator.  */
   1118   1.5  christos   if (!sec->rawsize)
   1119   1.5  christos     sec->rawsize = sec->size;
   1120   1.5  christos 
   1121   1.9  christos   bfd_set_section_size (sec, sec->size + 8);
   1122   1.5  christos }
   1123   1.5  christos 
   1124   1.5  christos /* Finish a pass over all .eh_frame_entry sections.  */
   1125   1.1     skrll 
   1126  1.10  christos bool
   1127   1.1     skrll _bfd_elf_end_eh_frame_parsing (struct bfd_link_info *info)
   1128   1.1     skrll {
   1129   1.1     skrll   struct eh_frame_hdr_info *hdr_info;
   1130   1.5  christos   unsigned int i;
   1131   1.1     skrll 
   1132   1.1     skrll   hdr_info = &elf_hash_table (info)->eh_info;
   1133   1.5  christos 
   1134   1.5  christos   if (info->eh_frame_hdr_type != COMPACT_EH_HDR
   1135   1.5  christos       || hdr_info->array_count == 0)
   1136  1.10  christos     return false;
   1137   1.5  christos 
   1138   1.5  christos   bfd_elf_discard_eh_frame_entry (hdr_info);
   1139   1.5  christos 
   1140   1.5  christos   qsort (hdr_info->u.compact.entries, hdr_info->array_count,
   1141   1.5  christos 	 sizeof (asection *), cmp_eh_frame_hdr);
   1142   1.5  christos 
   1143   1.5  christos   for (i = 0; i < hdr_info->array_count - 1; i++)
   1144   1.5  christos     {
   1145   1.5  christos       add_eh_frame_hdr_terminator (hdr_info->u.compact.entries[i],
   1146   1.5  christos 				   hdr_info->u.compact.entries[i + 1]);
   1147   1.5  christos     }
   1148   1.5  christos 
   1149   1.5  christos   /* Add a CANTUNWIND terminator after the last entry.  */
   1150   1.5  christos   add_eh_frame_hdr_terminator (hdr_info->u.compact.entries[i], NULL);
   1151  1.10  christos   return true;
   1152   1.1     skrll }
   1153   1.1     skrll 
   1154   1.1     skrll /* Mark all relocations against CIE or FDE ENT, which occurs in
   1155   1.1     skrll    .eh_frame section SEC.  COOKIE describes the relocations in SEC;
   1156   1.1     skrll    its "rel" field can be changed freely.  */
   1157   1.1     skrll 
   1158  1.10  christos static bool
   1159   1.1     skrll mark_entry (struct bfd_link_info *info, asection *sec,
   1160   1.1     skrll 	    struct eh_cie_fde *ent, elf_gc_mark_hook_fn gc_mark_hook,
   1161   1.1     skrll 	    struct elf_reloc_cookie *cookie)
   1162   1.1     skrll {
   1163   1.3  christos   /* FIXME: octets_per_byte.  */
   1164   1.1     skrll   for (cookie->rel = cookie->rels + ent->reloc_index;
   1165   1.1     skrll        cookie->rel < cookie->relend
   1166   1.1     skrll 	 && cookie->rel->r_offset < ent->offset + ent->size;
   1167   1.1     skrll        cookie->rel++)
   1168   1.1     skrll     if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, cookie))
   1169  1.10  christos       return false;
   1170   1.1     skrll 
   1171  1.10  christos   return true;
   1172   1.1     skrll }
   1173   1.1     skrll 
   1174   1.1     skrll /* Mark all the relocations against FDEs that relate to code in input
   1175   1.1     skrll    section SEC.  The FDEs belong to .eh_frame section EH_FRAME, whose
   1176   1.1     skrll    relocations are described by COOKIE.  */
   1177   1.1     skrll 
   1178  1.10  christos bool
   1179   1.1     skrll _bfd_elf_gc_mark_fdes (struct bfd_link_info *info, asection *sec,
   1180   1.1     skrll 		       asection *eh_frame, elf_gc_mark_hook_fn gc_mark_hook,
   1181   1.1     skrll 		       struct elf_reloc_cookie *cookie)
   1182   1.1     skrll {
   1183   1.1     skrll   struct eh_cie_fde *fde, *cie;
   1184   1.1     skrll 
   1185   1.1     skrll   for (fde = elf_fde_list (sec); fde; fde = fde->u.fde.next_for_section)
   1186   1.1     skrll     {
   1187   1.1     skrll       if (!mark_entry (info, eh_frame, fde, gc_mark_hook, cookie))
   1188  1.10  christos 	return false;
   1189   1.1     skrll 
   1190   1.1     skrll       /* At this stage, all cie_inf fields point to local CIEs, so we
   1191   1.1     skrll 	 can use the same cookie to refer to them.  */
   1192   1.1     skrll       cie = fde->u.fde.cie_inf;
   1193   1.5  christos       if (cie != NULL && !cie->u.cie.gc_mark)
   1194   1.1     skrll 	{
   1195   1.1     skrll 	  cie->u.cie.gc_mark = 1;
   1196   1.1     skrll 	  if (!mark_entry (info, eh_frame, cie, gc_mark_hook, cookie))
   1197  1.10  christos 	    return false;
   1198   1.1     skrll 	}
   1199   1.1     skrll     }
   1200  1.10  christos   return true;
   1201   1.1     skrll }
   1202   1.1     skrll 
   1203   1.1     skrll /* Input section SEC of ABFD is an .eh_frame section that contains the
   1204   1.1     skrll    CIE described by CIE_INF.  Return a version of CIE_INF that is going
   1205   1.1     skrll    to be kept in the output, adding CIE_INF to the output if necessary.
   1206   1.1     skrll 
   1207   1.1     skrll    HDR_INFO is the .eh_frame_hdr information and COOKIE describes the
   1208   1.1     skrll    relocations in REL.  */
   1209   1.1     skrll 
   1210   1.1     skrll static struct eh_cie_fde *
   1211   1.3  christos find_merged_cie (bfd *abfd, struct bfd_link_info *info, asection *sec,
   1212   1.1     skrll 		 struct eh_frame_hdr_info *hdr_info,
   1213   1.1     skrll 		 struct elf_reloc_cookie *cookie,
   1214   1.1     skrll 		 struct eh_cie_fde *cie_inf)
   1215   1.1     skrll {
   1216   1.1     skrll   unsigned long r_symndx;
   1217   1.1     skrll   struct cie *cie, *new_cie;
   1218   1.1     skrll   Elf_Internal_Rela *rel;
   1219   1.1     skrll   void **loc;
   1220   1.1     skrll 
   1221   1.1     skrll   /* Use CIE_INF if we have already decided to keep it.  */
   1222   1.1     skrll   if (!cie_inf->removed)
   1223   1.1     skrll     return cie_inf;
   1224   1.1     skrll 
   1225   1.1     skrll   /* If we have merged CIE_INF with another CIE, use that CIE instead.  */
   1226   1.1     skrll   if (cie_inf->u.cie.merged)
   1227   1.1     skrll     return cie_inf->u.cie.u.merged_with;
   1228   1.1     skrll 
   1229   1.1     skrll   cie = cie_inf->u.cie.u.full_cie;
   1230   1.1     skrll 
   1231   1.1     skrll   /* Assume we will need to keep CIE_INF.  */
   1232   1.1     skrll   cie_inf->removed = 0;
   1233   1.1     skrll   cie_inf->u.cie.u.sec = sec;
   1234   1.1     skrll 
   1235   1.1     skrll   /* If we are not merging CIEs, use CIE_INF.  */
   1236   1.1     skrll   if (cie == NULL)
   1237   1.1     skrll     return cie_inf;
   1238   1.1     skrll 
   1239   1.1     skrll   if (cie->per_encoding != DW_EH_PE_omit)
   1240   1.1     skrll     {
   1241  1.13  christos       struct elf_link_hash_entry *h;
   1242  1.10  christos       bool per_binds_local;
   1243   1.3  christos 
   1244   1.5  christos       /* Work out the address of personality routine, or at least
   1245   1.5  christos 	 enough info that we could calculate the address had we made a
   1246   1.5  christos 	 final section layout.  The symbol on the reloc is enough,
   1247   1.5  christos 	 either the hash for a global, or (bfd id, index) pair for a
   1248   1.5  christos 	 local.  The assumption here is that no one uses addends on
   1249   1.5  christos 	 the reloc.  */
   1250   1.1     skrll       rel = cookie->rels + cie->personality.reloc_index;
   1251   1.1     skrll       memset (&cie->personality, 0, sizeof (cie->personality));
   1252   1.1     skrll #ifdef BFD64
   1253   1.1     skrll       if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
   1254   1.1     skrll 	r_symndx = ELF64_R_SYM (rel->r_info);
   1255   1.1     skrll       else
   1256   1.1     skrll #endif
   1257   1.1     skrll 	r_symndx = ELF32_R_SYM (rel->r_info);
   1258   1.1     skrll 
   1259  1.13  christos       if (r_symndx > cookie->num_sym)
   1260  1.13  christos 	return cie_inf;
   1261  1.13  christos       h = NULL;
   1262  1.13  christos       if (r_symndx >= cookie->extsymoff)
   1263  1.13  christos 	h = elf_sym_hashes (cookie->abfd)[r_symndx - cookie->extsymoff];
   1264   1.1     skrll 
   1265  1.13  christos       if (h != NULL)
   1266  1.13  christos 	{
   1267   1.1     skrll 	  while (h->root.type == bfd_link_hash_indirect
   1268   1.1     skrll 		 || h->root.type == bfd_link_hash_warning)
   1269   1.1     skrll 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
   1270   1.1     skrll 
   1271   1.1     skrll 	  cie->personality.h = h;
   1272   1.3  christos 	  per_binds_local = SYMBOL_REFERENCES_LOCAL (info, h);
   1273   1.1     skrll 	}
   1274   1.1     skrll       else
   1275   1.1     skrll 	{
   1276  1.13  christos 	  asection *sym_sec = _bfd_get_local_sym_section (cookie, r_symndx);
   1277   1.1     skrll 	  if (sym_sec == NULL)
   1278   1.1     skrll 	    return cie_inf;
   1279   1.1     skrll 
   1280   1.1     skrll 	  if (sym_sec->kept_section != NULL)
   1281   1.1     skrll 	    sym_sec = sym_sec->kept_section;
   1282   1.1     skrll 	  if (sym_sec->output_section == NULL)
   1283   1.1     skrll 	    return cie_inf;
   1284   1.1     skrll 
   1285   1.1     skrll 	  cie->local_personality = 1;
   1286   1.5  christos 	  cie->personality.sym.bfd_id = abfd->id;
   1287   1.5  christos 	  cie->personality.sym.index = r_symndx;
   1288  1.10  christos 	  per_binds_local = true;
   1289   1.3  christos 	}
   1290   1.3  christos 
   1291   1.3  christos       if (per_binds_local
   1292   1.5  christos 	  && bfd_link_pic (info)
   1293   1.3  christos 	  && (cie->per_encoding & 0x70) == DW_EH_PE_absptr
   1294   1.3  christos 	  && (get_elf_backend_data (abfd)
   1295   1.3  christos 	      ->elf_backend_can_make_relative_eh_frame (abfd, info, sec)))
   1296   1.3  christos 	{
   1297   1.3  christos 	  cie_inf->u.cie.make_per_encoding_relative = 1;
   1298   1.3  christos 	  cie_inf->u.cie.per_encoding_relative = 1;
   1299   1.1     skrll 	}
   1300   1.1     skrll     }
   1301   1.1     skrll 
   1302   1.1     skrll   /* See if we can merge this CIE with an earlier one.  */
   1303   1.1     skrll   cie_compute_hash (cie);
   1304   1.5  christos   if (hdr_info->u.dwarf.cies == NULL)
   1305   1.1     skrll     {
   1306   1.5  christos       hdr_info->u.dwarf.cies = htab_try_create (1, cie_hash, cie_eq, free);
   1307   1.5  christos       if (hdr_info->u.dwarf.cies == NULL)
   1308   1.1     skrll 	return cie_inf;
   1309   1.1     skrll     }
   1310   1.5  christos   loc = htab_find_slot_with_hash (hdr_info->u.dwarf.cies, cie,
   1311   1.5  christos 				  cie->hash, INSERT);
   1312   1.1     skrll   if (loc == NULL)
   1313   1.1     skrll     return cie_inf;
   1314   1.1     skrll 
   1315   1.1     skrll   new_cie = (struct cie *) *loc;
   1316   1.1     skrll   if (new_cie == NULL)
   1317   1.1     skrll     {
   1318   1.1     skrll       /* Keep CIE_INF and record it in the hash table.  */
   1319  1.12  christos       new_cie = bfd_malloc (sizeof (*new_cie));
   1320   1.1     skrll       if (new_cie == NULL)
   1321   1.1     skrll 	return cie_inf;
   1322   1.1     skrll 
   1323   1.1     skrll       memcpy (new_cie, cie, sizeof (struct cie));
   1324   1.1     skrll       *loc = new_cie;
   1325   1.1     skrll     }
   1326   1.1     skrll   else
   1327   1.1     skrll     {
   1328   1.1     skrll       /* Merge CIE_INF with NEW_CIE->CIE_INF.  */
   1329   1.1     skrll       cie_inf->removed = 1;
   1330   1.1     skrll       cie_inf->u.cie.merged = 1;
   1331   1.1     skrll       cie_inf->u.cie.u.merged_with = new_cie->cie_inf;
   1332   1.1     skrll       if (cie_inf->u.cie.make_lsda_relative)
   1333   1.1     skrll 	new_cie->cie_inf->u.cie.make_lsda_relative = 1;
   1334   1.1     skrll     }
   1335   1.1     skrll   return new_cie->cie_inf;
   1336   1.1     skrll }
   1337   1.1     skrll 
   1338   1.7  christos /* For a given OFFSET in SEC, return the delta to the new location
   1339   1.7  christos    after .eh_frame editing.  */
   1340   1.7  christos 
   1341   1.7  christos static bfd_signed_vma
   1342   1.7  christos offset_adjust (bfd_vma offset, const asection *sec)
   1343   1.7  christos {
   1344  1.13  christos   struct eh_frame_sec_info *sec_info = sec->sec_info;
   1345   1.7  christos   unsigned int lo, hi, mid;
   1346   1.7  christos   struct eh_cie_fde *ent = NULL;
   1347   1.7  christos   bfd_signed_vma delta;
   1348   1.7  christos 
   1349   1.7  christos   lo = 0;
   1350   1.7  christos   hi = sec_info->count;
   1351   1.7  christos   if (hi == 0)
   1352   1.7  christos     return 0;
   1353   1.7  christos 
   1354   1.7  christos   while (lo < hi)
   1355   1.7  christos     {
   1356   1.7  christos       mid = (lo + hi) / 2;
   1357   1.7  christos       ent = &sec_info->entry[mid];
   1358   1.7  christos       if (offset < ent->offset)
   1359   1.7  christos 	hi = mid;
   1360   1.7  christos       else if (mid + 1 >= hi)
   1361   1.7  christos 	break;
   1362   1.7  christos       else if (offset >= ent[1].offset)
   1363   1.7  christos 	lo = mid + 1;
   1364   1.7  christos       else
   1365   1.7  christos 	break;
   1366   1.7  christos     }
   1367   1.7  christos 
   1368   1.7  christos   if (!ent->removed)
   1369   1.7  christos     delta = (bfd_vma) ent->new_offset - (bfd_vma) ent->offset;
   1370   1.7  christos   else if (ent->cie && ent->u.cie.merged)
   1371   1.7  christos     {
   1372   1.7  christos       struct eh_cie_fde *cie = ent->u.cie.u.merged_with;
   1373   1.7  christos       delta = ((bfd_vma) cie->new_offset + cie->u.cie.u.sec->output_offset
   1374   1.7  christos 	       - (bfd_vma) ent->offset - sec->output_offset);
   1375   1.7  christos     }
   1376   1.7  christos   else
   1377   1.7  christos     {
   1378   1.7  christos       /* Is putting the symbol on the next entry best for a deleted
   1379   1.7  christos 	 CIE/FDE?  */
   1380   1.7  christos       struct eh_cie_fde *last = sec_info->entry + sec_info->count;
   1381   1.7  christos       delta = ((bfd_vma) next_cie_fde_offset (ent, last, sec)
   1382   1.7  christos 	       - (bfd_vma) ent->offset);
   1383   1.7  christos       return delta;
   1384   1.7  christos     }
   1385   1.7  christos 
   1386   1.7  christos   /* Account for editing within this CIE/FDE.  */
   1387   1.7  christos   offset -= ent->offset;
   1388   1.7  christos   if (ent->cie)
   1389   1.7  christos     {
   1390   1.7  christos       unsigned int extra
   1391   1.7  christos 	= ent->add_augmentation_size + ent->u.cie.add_fde_encoding;
   1392   1.7  christos       if (extra == 0
   1393   1.7  christos 	  || offset <= 9u + ent->u.cie.aug_str_len)
   1394   1.7  christos 	return delta;
   1395   1.7  christos       delta += extra;
   1396   1.7  christos       if (offset <= 9u + ent->u.cie.aug_str_len + ent->u.cie.aug_data_len)
   1397   1.7  christos 	return delta;
   1398   1.7  christos       delta += extra;
   1399   1.7  christos     }
   1400   1.7  christos   else
   1401   1.7  christos     {
   1402   1.7  christos       unsigned int ptr_size, width, extra = ent->add_augmentation_size;
   1403   1.7  christos       if (offset <= 12 || extra == 0)
   1404   1.7  christos 	return delta;
   1405   1.7  christos       ptr_size = (get_elf_backend_data (sec->owner)
   1406   1.7  christos 		  ->elf_backend_eh_frame_address_size (sec->owner, sec));
   1407   1.7  christos       width = get_DW_EH_PE_width (ent->fde_encoding, ptr_size);
   1408   1.7  christos       if (offset <= 8 + 2 * width)
   1409   1.7  christos 	return delta;
   1410   1.7  christos       delta += extra;
   1411   1.7  christos     }
   1412   1.7  christos 
   1413   1.7  christos   return delta;
   1414   1.7  christos }
   1415   1.7  christos 
   1416   1.7  christos /* Adjust a global symbol defined in .eh_frame, so that it stays
   1417   1.7  christos    relative to its original CIE/FDE.  It is assumed that a symbol
   1418   1.7  christos    defined at the beginning of a CIE/FDE belongs to that CIE/FDE
   1419   1.7  christos    rather than marking the end of the previous CIE/FDE.  This matters
   1420   1.7  christos    when a CIE is merged with a previous CIE, since the symbol is
   1421   1.7  christos    moved to the merged CIE.  */
   1422   1.7  christos 
   1423  1.10  christos bool
   1424   1.7  christos _bfd_elf_adjust_eh_frame_global_symbol (struct elf_link_hash_entry *h,
   1425   1.7  christos 					void *arg ATTRIBUTE_UNUSED)
   1426   1.7  christos {
   1427   1.7  christos   asection *sym_sec;
   1428   1.7  christos   bfd_signed_vma delta;
   1429   1.7  christos 
   1430   1.7  christos   if (h->root.type != bfd_link_hash_defined
   1431   1.7  christos       && h->root.type != bfd_link_hash_defweak)
   1432  1.10  christos     return true;
   1433   1.7  christos 
   1434   1.7  christos   sym_sec = h->root.u.def.section;
   1435   1.7  christos   if (sym_sec->sec_info_type != SEC_INFO_TYPE_EH_FRAME
   1436  1.13  christos       || sym_sec->sec_info == NULL)
   1437  1.10  christos     return true;
   1438   1.7  christos 
   1439   1.7  christos   delta = offset_adjust (h->root.u.def.value, sym_sec);
   1440   1.7  christos   h->root.u.def.value += delta;
   1441   1.7  christos 
   1442  1.10  christos   return true;
   1443   1.7  christos }
   1444   1.7  christos 
   1445  1.13  christos /* The same for all local symbols defined in .eh_frame.  Returns the
   1446  1.13  christos    local symbols if any symbol was changed.  */
   1447   1.7  christos 
   1448  1.13  christos static Elf_Internal_Sym *
   1449   1.7  christos adjust_eh_frame_local_symbols (const asection *sec,
   1450   1.7  christos 			       struct elf_reloc_cookie *cookie)
   1451   1.7  christos {
   1452  1.13  christos   bfd *abfd = cookie->abfd;
   1453  1.13  christos   unsigned int *loc_shndx = elf_loc_shndx (abfd);
   1454  1.13  christos   unsigned int shndx = elf_section_data (sec)->this_idx;
   1455   1.7  christos 
   1456  1.13  christos   if (loc_shndx != NULL)
   1457  1.10  christos     {
   1458  1.13  christos       unsigned int i;
   1459  1.13  christos 
   1460  1.13  christos       for (i = 1; i < cookie->locsymcount; i++)
   1461  1.13  christos 	if (loc_shndx[i] == shndx)
   1462  1.13  christos 	  break;
   1463  1.13  christos       if (i >= cookie->locsymcount)
   1464  1.13  christos 	return NULL;
   1465  1.13  christos     }
   1466  1.10  christos 
   1467  1.13  christos   Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (abfd);
   1468  1.13  christos   Elf_Internal_Sym *locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
   1469  1.13  christos 						    cookie->locsymcount, 0,
   1470  1.13  christos 						    NULL, NULL, NULL);
   1471  1.13  christos   if (locsyms == NULL)
   1472  1.13  christos     return NULL;
   1473  1.13  christos 
   1474  1.13  christos   bool adjusted = false;
   1475  1.13  christos   Elf_Internal_Sym *sym;
   1476  1.13  christos   Elf_Internal_Sym *end_sym = locsyms + cookie->locsymcount;
   1477  1.13  christos   for (sym = locsyms + 1; sym < end_sym; ++sym)
   1478  1.13  christos     if (sym->st_info <= ELF_ST_INFO (STB_LOCAL, STT_OBJECT)
   1479  1.13  christos 	&& sym->st_shndx == shndx)
   1480  1.13  christos       {
   1481  1.13  christos 	bfd_signed_vma delta = offset_adjust (sym->st_value, sec);
   1482  1.13  christos 
   1483  1.13  christos 	if (delta != 0)
   1484  1.10  christos 	  {
   1485  1.13  christos 	    adjusted = true;
   1486  1.13  christos 	    sym->st_value += delta;
   1487   1.7  christos 	  }
   1488  1.13  christos       }
   1489  1.13  christos   if (adjusted)
   1490  1.13  christos     return locsyms;
   1491  1.13  christos   free (locsyms);
   1492  1.13  christos   return NULL;
   1493   1.7  christos }
   1494   1.7  christos 
   1495   1.1     skrll /* This function is called for each input file before the .eh_frame
   1496  1.13  christos    section is relocated.  It discards duplicate CIEs and FDEs for
   1497  1.13  christos    discarded functions.  The function returns 0 when no changes are
   1498  1.13  christos    made, 1 when .eh_frame data has been edited and 2 when the editing
   1499  1.13  christos    results in a section size change.  */
   1500   1.1     skrll 
   1501  1.13  christos int
   1502   1.1     skrll _bfd_elf_discard_section_eh_frame
   1503   1.1     skrll    (bfd *abfd, struct bfd_link_info *info, asection *sec,
   1504  1.10  christos     bool (*reloc_symbol_deleted_p) (bfd_vma, void *),
   1505   1.1     skrll     struct elf_reloc_cookie *cookie)
   1506   1.1     skrll {
   1507   1.1     skrll   struct eh_cie_fde *ent;
   1508   1.1     skrll   struct eh_frame_sec_info *sec_info;
   1509   1.1     skrll   struct eh_frame_hdr_info *hdr_info;
   1510   1.7  christos   unsigned int ptr_size, offset, eh_alignment;
   1511   1.7  christos   int changed;
   1512   1.1     skrll 
   1513   1.4  christos   if (sec->sec_info_type != SEC_INFO_TYPE_EH_FRAME)
   1514  1.10  christos     return false;
   1515   1.4  christos 
   1516  1.13  christos   sec_info = sec->sec_info;
   1517   1.1     skrll   if (sec_info == NULL)
   1518  1.10  christos     return false;
   1519   1.1     skrll 
   1520   1.4  christos   ptr_size = (get_elf_backend_data (sec->owner)
   1521   1.4  christos 	      ->elf_backend_eh_frame_address_size (sec->owner, sec));
   1522   1.4  christos 
   1523   1.1     skrll   hdr_info = &elf_hash_table (info)->eh_info;
   1524   1.1     skrll   for (ent = sec_info->entry; ent < sec_info->entry + sec_info->count; ++ent)
   1525   1.1     skrll     if (ent->size == 4)
   1526   1.1     skrll       /* There should only be one zero terminator, on the last input
   1527   1.1     skrll 	 file supplying .eh_frame (crtend.o).  Remove any others.  */
   1528   1.1     skrll       ent->removed = sec->map_head.s != NULL;
   1529   1.5  christos     else if (!ent->cie && ent->u.fde.cie_inf != NULL)
   1530   1.1     skrll       {
   1531  1.10  christos 	bool keep;
   1532   1.4  christos 	if ((sec->flags & SEC_LINKER_CREATED) != 0 && cookie->rels == NULL)
   1533   1.4  christos 	  {
   1534   1.4  christos 	    unsigned int width
   1535   1.4  christos 	      = get_DW_EH_PE_width (ent->fde_encoding, ptr_size);
   1536   1.4  christos 	    bfd_vma value
   1537   1.4  christos 	      = read_value (abfd, sec->contents + ent->offset + 8 + width,
   1538   1.4  christos 			    width, get_DW_EH_PE_signed (ent->fde_encoding));
   1539   1.4  christos 	    keep = value != 0;
   1540   1.4  christos 	  }
   1541   1.4  christos 	else
   1542   1.4  christos 	  {
   1543   1.4  christos 	    cookie->rel = cookie->rels + ent->reloc_index;
   1544   1.4  christos 	    /* FIXME: octets_per_byte.  */
   1545   1.4  christos 	    BFD_ASSERT (cookie->rel < cookie->relend
   1546   1.4  christos 			&& cookie->rel->r_offset == ent->offset + 8);
   1547   1.4  christos 	    keep = !(*reloc_symbol_deleted_p) (ent->offset + 8, cookie);
   1548   1.4  christos 	  }
   1549   1.4  christos 	if (keep)
   1550   1.1     skrll 	  {
   1551   1.5  christos 	    if (bfd_link_pic (info)
   1552   1.3  christos 		&& (((ent->fde_encoding & 0x70) == DW_EH_PE_absptr
   1553   1.1     skrll 		     && ent->make_relative == 0)
   1554   1.3  christos 		    || (ent->fde_encoding & 0x70) == DW_EH_PE_aligned))
   1555   1.1     skrll 	      {
   1556   1.6  christos 		static int num_warnings_issued = 0;
   1557   1.6  christos 
   1558   1.1     skrll 		/* If a shared library uses absolute pointers
   1559   1.1     skrll 		   which we cannot turn into PC relative,
   1560   1.1     skrll 		   don't create the binary search table,
   1561   1.1     skrll 		   since it is affected by runtime relocations.  */
   1562  1.10  christos 		hdr_info->u.dwarf.table = false;
   1563   1.9  christos 		/* Only warn if --eh-frame-hdr was specified.  */
   1564   1.9  christos 		if (info->eh_frame_hdr_type != 0)
   1565   1.6  christos 		  {
   1566   1.9  christos 		    if (num_warnings_issued < 10)
   1567   1.9  christos 		      {
   1568   1.9  christos 			_bfd_error_handler
   1569   1.9  christos 			  /* xgettext:c-format */
   1570   1.9  christos 			  (_("FDE encoding in %pB(%pA) prevents .eh_frame_hdr"
   1571   1.9  christos 			     " table being created"), abfd, sec);
   1572   1.9  christos 			num_warnings_issued ++;
   1573   1.9  christos 		      }
   1574   1.9  christos 		    else if (num_warnings_issued == 10)
   1575   1.9  christos 		      {
   1576   1.9  christos 			_bfd_error_handler
   1577   1.9  christos 			  (_("further warnings about FDE encoding preventing .eh_frame_hdr generation dropped"));
   1578   1.9  christos 			num_warnings_issued ++;
   1579   1.9  christos 		      }
   1580   1.6  christos 		  }
   1581   1.1     skrll 	      }
   1582   1.1     skrll 	    ent->removed = 0;
   1583   1.5  christos 	    hdr_info->u.dwarf.fde_count++;
   1584   1.3  christos 	    ent->u.fde.cie_inf = find_merged_cie (abfd, info, sec, hdr_info,
   1585   1.3  christos 						  cookie, ent->u.fde.cie_inf);
   1586   1.1     skrll 	  }
   1587   1.1     skrll       }
   1588   1.1     skrll 
   1589  1.10  christos   free (sec_info->cies);
   1590  1.10  christos   sec_info->cies = NULL;
   1591   1.1     skrll 
   1592   1.7  christos   /* It may be that some .eh_frame input section has greater alignment
   1593   1.7  christos      than other .eh_frame sections.  In that case we run the risk of
   1594   1.7  christos      padding with zeros before that section, which would be seen as a
   1595   1.7  christos      zero terminator.  Alignment padding must be added *inside* the
   1596   1.7  christos      last FDE instead.  For other FDEs we align according to their
   1597   1.7  christos      encoding, in order to align FDE address range entries naturally.  */
   1598   1.1     skrll   offset = 0;
   1599   1.7  christos   changed = 0;
   1600   1.1     skrll   for (ent = sec_info->entry; ent < sec_info->entry + sec_info->count; ++ent)
   1601   1.1     skrll     if (!ent->removed)
   1602   1.1     skrll       {
   1603   1.7  christos 	eh_alignment = 4;
   1604   1.7  christos 	if (ent->size == 4)
   1605   1.7  christos 	  ;
   1606   1.7  christos 	else if (ent->cie)
   1607   1.7  christos 	  {
   1608   1.7  christos 	    if (ent->u.cie.per_encoding_aligned8)
   1609   1.7  christos 	      eh_alignment = 8;
   1610   1.7  christos 	  }
   1611   1.7  christos 	else
   1612   1.7  christos 	  {
   1613   1.7  christos 	    eh_alignment = get_DW_EH_PE_width (ent->fde_encoding, ptr_size);
   1614   1.7  christos 	    if (eh_alignment < 4)
   1615   1.7  christos 	      eh_alignment = 4;
   1616   1.7  christos 	  }
   1617   1.7  christos 	offset = (offset + eh_alignment - 1) & -eh_alignment;
   1618   1.1     skrll 	ent->new_offset = offset;
   1619   1.7  christos 	if (ent->new_offset != ent->offset)
   1620   1.7  christos 	  changed = 1;
   1621   1.7  christos 	offset += size_of_output_cie_fde (ent);
   1622   1.1     skrll       }
   1623   1.1     skrll 
   1624   1.7  christos   eh_alignment = 4;
   1625   1.7  christos   offset = (offset + eh_alignment - 1) & -eh_alignment;
   1626  1.13  christos   if (sec->rawsize == 0)
   1627  1.13  christos     sec->rawsize = sec->size;
   1628  1.13  christos   if (sec->size != offset)
   1629  1.13  christos     changed = 2;
   1630   1.1     skrll   sec->size = offset;
   1631   1.7  christos 
   1632  1.13  christos   if (changed)
   1633   1.7  christos     {
   1634  1.13  christos       Elf_Internal_Sym *locsyms = adjust_eh_frame_local_symbols (sec, cookie);
   1635  1.13  christos       if (locsyms != NULL)
   1636  1.13  christos 	{
   1637  1.13  christos 	  Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (abfd);
   1638  1.13  christos 	  symtab_hdr->contents = (unsigned char *) locsyms;
   1639  1.13  christos 	}
   1640   1.7  christos     }
   1641   1.7  christos   return changed;
   1642   1.1     skrll }
   1643   1.1     skrll 
   1644   1.1     skrll /* This function is called for .eh_frame_hdr section after
   1645   1.1     skrll    _bfd_elf_discard_section_eh_frame has been called on all .eh_frame
   1646   1.1     skrll    input sections.  It finalizes the size of .eh_frame_hdr section.  */
   1647   1.1     skrll 
   1648  1.10  christos bool
   1649  1.10  christos _bfd_elf_discard_section_eh_frame_hdr (struct bfd_link_info *info)
   1650   1.1     skrll {
   1651   1.1     skrll   struct elf_link_hash_table *htab;
   1652   1.1     skrll   struct eh_frame_hdr_info *hdr_info;
   1653   1.1     skrll   asection *sec;
   1654   1.1     skrll 
   1655   1.1     skrll   htab = elf_hash_table (info);
   1656   1.1     skrll   hdr_info = &htab->eh_info;
   1657   1.1     skrll 
   1658   1.5  christos   if (!hdr_info->frame_hdr_is_compact && hdr_info->u.dwarf.cies != NULL)
   1659   1.1     skrll     {
   1660   1.5  christos       htab_delete (hdr_info->u.dwarf.cies);
   1661   1.5  christos       hdr_info->u.dwarf.cies = NULL;
   1662   1.1     skrll     }
   1663   1.1     skrll 
   1664  1.12  christos   if (info->eh_frame_hdr_type == 0
   1665  1.12  christos       || bfd_link_relocatable (info))
   1666  1.12  christos     return false;
   1667  1.12  christos 
   1668   1.1     skrll   sec = hdr_info->hdr_sec;
   1669   1.1     skrll   if (sec == NULL)
   1670  1.10  christos     return false;
   1671   1.1     skrll 
   1672   1.5  christos   if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
   1673   1.5  christos     {
   1674   1.5  christos       /* For compact frames we only add the header.  The actual table comes
   1675   1.7  christos 	 from the .eh_frame_entry sections.  */
   1676   1.5  christos       sec->size = 8;
   1677   1.5  christos     }
   1678   1.5  christos   else
   1679   1.5  christos     {
   1680   1.5  christos       sec->size = EH_FRAME_HDR_SIZE;
   1681   1.5  christos       if (hdr_info->u.dwarf.table)
   1682   1.5  christos 	sec->size += 4 + hdr_info->u.dwarf.fde_count * 8;
   1683   1.5  christos     }
   1684   1.1     skrll 
   1685  1.10  christos   return true;
   1686   1.1     skrll }
   1687   1.1     skrll 
   1688   1.4  christos /* Return true if there is at least one non-empty .eh_frame section in
   1689   1.4  christos    input files.  Can only be called after ld has mapped input to
   1690   1.4  christos    output sections, and before sections are stripped.  */
   1691   1.5  christos 
   1692  1.10  christos bool
   1693   1.4  christos _bfd_elf_eh_frame_present (struct bfd_link_info *info)
   1694   1.4  christos {
   1695   1.4  christos   asection *eh = bfd_get_section_by_name (info->output_bfd, ".eh_frame");
   1696   1.4  christos 
   1697   1.4  christos   if (eh == NULL)
   1698  1.10  christos     return false;
   1699   1.4  christos 
   1700   1.4  christos   /* Count only sections which have at least a single CIE or FDE.
   1701   1.4  christos      There cannot be any CIE or FDE <= 8 bytes.  */
   1702   1.4  christos   for (eh = eh->map_head.s; eh != NULL; eh = eh->map_head.s)
   1703   1.4  christos     if (eh->size > 8)
   1704  1.10  christos       return true;
   1705   1.4  christos 
   1706  1.10  christos   return false;
   1707   1.4  christos }
   1708   1.4  christos 
   1709   1.5  christos /* Return true if there is at least one .eh_frame_entry section in
   1710   1.5  christos    input files.  */
   1711   1.5  christos 
   1712  1.10  christos bool
   1713   1.5  christos _bfd_elf_eh_frame_entry_present (struct bfd_link_info *info)
   1714   1.5  christos {
   1715   1.5  christos   asection *o;
   1716   1.5  christos   bfd *abfd;
   1717   1.5  christos 
   1718   1.5  christos   for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
   1719   1.5  christos     {
   1720   1.5  christos       for (o = abfd->sections; o; o = o->next)
   1721   1.5  christos 	{
   1722   1.9  christos 	  const char *name = bfd_section_name (o);
   1723   1.5  christos 
   1724   1.5  christos 	  if (strcmp (name, ".eh_frame_entry")
   1725   1.5  christos 	      && !bfd_is_abs_section (o->output_section))
   1726  1.10  christos 	    return true;
   1727   1.5  christos 	}
   1728   1.5  christos     }
   1729  1.10  christos   return false;
   1730   1.5  christos }
   1731   1.5  christos 
   1732   1.1     skrll /* This function is called from size_dynamic_sections.
   1733   1.1     skrll    It needs to decide whether .eh_frame_hdr should be output or not,
   1734   1.1     skrll    because when the dynamic symbol table has been sized it is too late
   1735   1.1     skrll    to strip sections.  */
   1736   1.1     skrll 
   1737  1.10  christos bool
   1738   1.1     skrll _bfd_elf_maybe_strip_eh_frame_hdr (struct bfd_link_info *info)
   1739   1.1     skrll {
   1740   1.1     skrll   struct elf_link_hash_table *htab;
   1741   1.1     skrll   struct eh_frame_hdr_info *hdr_info;
   1742   1.5  christos   struct bfd_link_hash_entry *bh = NULL;
   1743   1.5  christos   struct elf_link_hash_entry *h;
   1744   1.1     skrll 
   1745   1.1     skrll   htab = elf_hash_table (info);
   1746   1.1     skrll   hdr_info = &htab->eh_info;
   1747   1.1     skrll   if (hdr_info->hdr_sec == NULL)
   1748  1.10  christos     return true;
   1749   1.1     skrll 
   1750   1.4  christos   if (bfd_is_abs_section (hdr_info->hdr_sec->output_section)
   1751   1.5  christos       || info->eh_frame_hdr_type == 0
   1752   1.5  christos       || (info->eh_frame_hdr_type == DWARF2_EH_HDR
   1753   1.5  christos 	  && !_bfd_elf_eh_frame_present (info))
   1754   1.5  christos       || (info->eh_frame_hdr_type == COMPACT_EH_HDR
   1755   1.5  christos 	  && !_bfd_elf_eh_frame_entry_present (info)))
   1756   1.1     skrll     {
   1757   1.1     skrll       hdr_info->hdr_sec->flags |= SEC_EXCLUDE;
   1758   1.1     skrll       hdr_info->hdr_sec = NULL;
   1759  1.10  christos       return true;
   1760   1.1     skrll     }
   1761   1.1     skrll 
   1762   1.5  christos   /* Add a hidden symbol so that systems without access to PHDRs can
   1763   1.5  christos      find the table.  */
   1764   1.5  christos   if (! (_bfd_generic_link_add_one_symbol
   1765   1.5  christos 	 (info, info->output_bfd, "__GNU_EH_FRAME_HDR", BSF_LOCAL,
   1766  1.10  christos 	  hdr_info->hdr_sec, 0, NULL, false, false, &bh)))
   1767  1.10  christos     return false;
   1768   1.5  christos 
   1769   1.5  christos   h = (struct elf_link_hash_entry *) bh;
   1770   1.5  christos   h->def_regular = 1;
   1771   1.5  christos   h->other = STV_HIDDEN;
   1772   1.5  christos   get_elf_backend_data
   1773  1.10  christos     (info->output_bfd)->elf_backend_hide_symbol (info, h, true);
   1774   1.5  christos 
   1775   1.5  christos   if (!hdr_info->frame_hdr_is_compact)
   1776  1.10  christos     hdr_info->u.dwarf.table = true;
   1777  1.10  christos   return true;
   1778   1.1     skrll }
   1779   1.1     skrll 
   1780   1.1     skrll /* Adjust an address in the .eh_frame section.  Given OFFSET within
   1781   1.1     skrll    SEC, this returns the new offset in the adjusted .eh_frame section,
   1782   1.1     skrll    or -1 if the address refers to a CIE/FDE which has been removed
   1783   1.1     skrll    or to offset with dynamic relocation which is no longer needed.  */
   1784   1.1     skrll 
   1785   1.1     skrll bfd_vma
   1786   1.1     skrll _bfd_elf_eh_frame_section_offset (bfd *output_bfd ATTRIBUTE_UNUSED,
   1787   1.3  christos 				  struct bfd_link_info *info ATTRIBUTE_UNUSED,
   1788   1.1     skrll 				  asection *sec,
   1789   1.1     skrll 				  bfd_vma offset)
   1790   1.1     skrll {
   1791   1.1     skrll   struct eh_frame_sec_info *sec_info;
   1792   1.1     skrll   unsigned int lo, hi, mid;
   1793   1.1     skrll 
   1794   1.4  christos   if (sec->sec_info_type != SEC_INFO_TYPE_EH_FRAME)
   1795   1.1     skrll     return offset;
   1796  1.13  christos   sec_info = sec->sec_info;
   1797   1.1     skrll 
   1798   1.1     skrll   if (offset >= sec->rawsize)
   1799   1.1     skrll     return offset - sec->rawsize + sec->size;
   1800   1.1     skrll 
   1801   1.1     skrll   lo = 0;
   1802   1.1     skrll   hi = sec_info->count;
   1803   1.1     skrll   mid = 0;
   1804   1.1     skrll   while (lo < hi)
   1805   1.1     skrll     {
   1806   1.1     skrll       mid = (lo + hi) / 2;
   1807   1.1     skrll       if (offset < sec_info->entry[mid].offset)
   1808   1.1     skrll 	hi = mid;
   1809   1.1     skrll       else if (offset
   1810   1.1     skrll 	       >= sec_info->entry[mid].offset + sec_info->entry[mid].size)
   1811   1.1     skrll 	lo = mid + 1;
   1812   1.1     skrll       else
   1813   1.1     skrll 	break;
   1814   1.1     skrll     }
   1815   1.1     skrll 
   1816   1.1     skrll   BFD_ASSERT (lo < hi);
   1817   1.1     skrll 
   1818   1.1     skrll   /* FDE or CIE was removed.  */
   1819   1.1     skrll   if (sec_info->entry[mid].removed)
   1820   1.1     skrll     return (bfd_vma) -1;
   1821   1.1     skrll 
   1822   1.3  christos   /* If converting personality pointers to DW_EH_PE_pcrel, there will be
   1823   1.3  christos      no need for run-time relocation against the personality field.  */
   1824   1.3  christos   if (sec_info->entry[mid].cie
   1825   1.3  christos       && sec_info->entry[mid].u.cie.make_per_encoding_relative
   1826   1.3  christos       && offset == (sec_info->entry[mid].offset + 8
   1827   1.3  christos 		    + sec_info->entry[mid].u.cie.personality_offset))
   1828   1.3  christos     return (bfd_vma) -2;
   1829   1.3  christos 
   1830   1.1     skrll   /* If converting to DW_EH_PE_pcrel, there will be no need for run-time
   1831   1.1     skrll      relocation against FDE's initial_location field.  */
   1832   1.1     skrll   if (!sec_info->entry[mid].cie
   1833   1.1     skrll       && sec_info->entry[mid].make_relative
   1834   1.1     skrll       && offset == sec_info->entry[mid].offset + 8)
   1835   1.1     skrll     return (bfd_vma) -2;
   1836   1.1     skrll 
   1837   1.1     skrll   /* If converting LSDA pointers to DW_EH_PE_pcrel, there will be no need
   1838   1.1     skrll      for run-time relocation against LSDA field.  */
   1839   1.1     skrll   if (!sec_info->entry[mid].cie
   1840   1.1     skrll       && sec_info->entry[mid].u.fde.cie_inf->u.cie.make_lsda_relative
   1841   1.1     skrll       && offset == (sec_info->entry[mid].offset + 8
   1842   1.1     skrll 		    + sec_info->entry[mid].lsda_offset))
   1843   1.1     skrll     return (bfd_vma) -2;
   1844   1.1     skrll 
   1845   1.1     skrll   /* If converting to DW_EH_PE_pcrel, there will be no need for run-time
   1846   1.1     skrll      relocation against DW_CFA_set_loc's arguments.  */
   1847   1.1     skrll   if (sec_info->entry[mid].set_loc
   1848   1.1     skrll       && sec_info->entry[mid].make_relative
   1849   1.1     skrll       && (offset >= sec_info->entry[mid].offset + 8
   1850   1.1     skrll 		    + sec_info->entry[mid].set_loc[1]))
   1851   1.1     skrll     {
   1852   1.1     skrll       unsigned int cnt;
   1853   1.1     skrll 
   1854   1.1     skrll       for (cnt = 1; cnt <= sec_info->entry[mid].set_loc[0]; cnt++)
   1855   1.1     skrll 	if (offset == sec_info->entry[mid].offset + 8
   1856   1.1     skrll 		      + sec_info->entry[mid].set_loc[cnt])
   1857   1.1     skrll 	  return (bfd_vma) -2;
   1858   1.1     skrll     }
   1859   1.1     skrll 
   1860   1.1     skrll   /* Any new augmentation bytes go before the first relocation.  */
   1861   1.1     skrll   return (offset + sec_info->entry[mid].new_offset
   1862   1.1     skrll 	  - sec_info->entry[mid].offset
   1863   1.1     skrll 	  + extra_augmentation_string_bytes (sec_info->entry + mid)
   1864   1.1     skrll 	  + extra_augmentation_data_bytes (sec_info->entry + mid));
   1865   1.1     skrll }
   1866   1.1     skrll 
   1867   1.5  christos /* Write out .eh_frame_entry section.  Add CANTUNWIND terminator if needed.
   1868   1.5  christos    Also check that the contents look sane.  */
   1869   1.5  christos 
   1870  1.10  christos bool
   1871   1.5  christos _bfd_elf_write_section_eh_frame_entry (bfd *abfd, struct bfd_link_info *info,
   1872   1.5  christos 				       asection *sec, bfd_byte *contents)
   1873   1.5  christos {
   1874  1.13  christos   elf_backend_data *bed;
   1875   1.5  christos   bfd_byte cantunwind[8];
   1876   1.5  christos   bfd_vma addr;
   1877   1.5  christos   bfd_vma last_addr;
   1878   1.5  christos   bfd_vma offset;
   1879  1.13  christos   asection *text_sec = sec->sec_info;
   1880   1.5  christos 
   1881   1.5  christos   if (!sec->rawsize)
   1882   1.5  christos     sec->rawsize = sec->size;
   1883   1.5  christos 
   1884   1.5  christos   BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_EH_FRAME_ENTRY);
   1885   1.5  christos 
   1886   1.5  christos   /* Check to make sure that the text section corresponding to this eh_frame_entry
   1887   1.5  christos      section has not been excluded.  In particular, mips16 stub entries will be
   1888   1.5  christos      excluded outside of the normal process.  */
   1889   1.5  christos   if (sec->flags & SEC_EXCLUDE
   1890   1.5  christos       || text_sec->flags & SEC_EXCLUDE)
   1891  1.10  christos     return true;
   1892   1.5  christos 
   1893   1.5  christos   if (!bfd_set_section_contents (abfd, sec->output_section, contents,
   1894   1.5  christos 				 sec->output_offset, sec->rawsize))
   1895  1.10  christos       return false;
   1896   1.5  christos 
   1897   1.5  christos   last_addr = bfd_get_signed_32 (abfd, contents);
   1898   1.5  christos   /* Check that all the entries are in order.  */
   1899   1.5  christos   for (offset = 8; offset < sec->rawsize; offset += 8)
   1900   1.5  christos     {
   1901   1.5  christos       addr = bfd_get_signed_32 (abfd, contents + offset) + offset;
   1902   1.5  christos       if (addr <= last_addr)
   1903   1.5  christos 	{
   1904   1.7  christos 	  /* xgettext:c-format */
   1905   1.8  christos 	  _bfd_error_handler (_("%pB: %pA not in order"), sec->owner, sec);
   1906  1.10  christos 	  return false;
   1907   1.5  christos 	}
   1908   1.5  christos 
   1909   1.5  christos       last_addr = addr;
   1910   1.5  christos     }
   1911   1.5  christos 
   1912   1.5  christos   addr = text_sec->output_section->vma + text_sec->output_offset
   1913   1.5  christos 	 + text_sec->size;
   1914   1.5  christos   addr &= ~1;
   1915   1.5  christos   addr -= (sec->output_section->vma + sec->output_offset + sec->rawsize);
   1916   1.5  christos   if (addr & 1)
   1917   1.5  christos     {
   1918   1.7  christos       /* xgettext:c-format */
   1919   1.8  christos       _bfd_error_handler (_("%pB: %pA invalid input section size"),
   1920   1.7  christos 			  sec->owner, sec);
   1921   1.5  christos       bfd_set_error (bfd_error_bad_value);
   1922  1.10  christos       return false;
   1923   1.5  christos     }
   1924   1.5  christos   if (last_addr >= addr + sec->rawsize)
   1925   1.5  christos     {
   1926   1.7  christos       /* xgettext:c-format */
   1927   1.8  christos       _bfd_error_handler (_("%pB: %pA points past end of text section"),
   1928   1.7  christos 			  sec->owner, sec);
   1929   1.5  christos       bfd_set_error (bfd_error_bad_value);
   1930  1.10  christos       return false;
   1931   1.5  christos     }
   1932   1.5  christos 
   1933   1.5  christos   if (sec->size == sec->rawsize)
   1934  1.10  christos     return true;
   1935   1.5  christos 
   1936   1.5  christos   bed = get_elf_backend_data (abfd);
   1937   1.5  christos   BFD_ASSERT (sec->size == sec->rawsize + 8);
   1938   1.5  christos   BFD_ASSERT ((addr & 1) == 0);
   1939   1.5  christos   BFD_ASSERT (bed->cant_unwind_opcode);
   1940   1.5  christos 
   1941   1.5  christos   bfd_put_32 (abfd, addr, cantunwind);
   1942   1.5  christos   bfd_put_32 (abfd, (*bed->cant_unwind_opcode) (info), cantunwind + 4);
   1943   1.5  christos   return bfd_set_section_contents (abfd, sec->output_section, cantunwind,
   1944   1.5  christos 				   sec->output_offset + sec->rawsize, 8);
   1945   1.5  christos }
   1946   1.5  christos 
   1947   1.1     skrll /* Write out .eh_frame section.  This is called with the relocated
   1948   1.1     skrll    contents.  */
   1949   1.1     skrll 
   1950  1.10  christos bool
   1951   1.1     skrll _bfd_elf_write_section_eh_frame (bfd *abfd,
   1952   1.1     skrll 				 struct bfd_link_info *info,
   1953   1.1     skrll 				 asection *sec,
   1954   1.1     skrll 				 bfd_byte *contents)
   1955   1.1     skrll {
   1956   1.1     skrll   struct eh_frame_sec_info *sec_info;
   1957   1.1     skrll   struct elf_link_hash_table *htab;
   1958   1.1     skrll   struct eh_frame_hdr_info *hdr_info;
   1959   1.1     skrll   unsigned int ptr_size;
   1960   1.7  christos   struct eh_cie_fde *ent, *last_ent;
   1961   1.1     skrll 
   1962   1.4  christos   if (sec->sec_info_type != SEC_INFO_TYPE_EH_FRAME)
   1963   1.3  christos     /* FIXME: octets_per_byte.  */
   1964   1.1     skrll     return bfd_set_section_contents (abfd, sec->output_section, contents,
   1965   1.1     skrll 				     sec->output_offset, sec->size);
   1966   1.1     skrll 
   1967   1.1     skrll   ptr_size = (get_elf_backend_data (abfd)
   1968   1.1     skrll 	      ->elf_backend_eh_frame_address_size (abfd, sec));
   1969   1.1     skrll   BFD_ASSERT (ptr_size != 0);
   1970   1.1     skrll 
   1971  1.13  christos   sec_info = sec->sec_info;
   1972   1.1     skrll   htab = elf_hash_table (info);
   1973   1.1     skrll   hdr_info = &htab->eh_info;
   1974   1.1     skrll 
   1975   1.5  christos   if (hdr_info->u.dwarf.table && hdr_info->u.dwarf.array == NULL)
   1976   1.5  christos     {
   1977  1.10  christos       hdr_info->frame_hdr_is_compact = false;
   1978   1.5  christos       hdr_info->u.dwarf.array = (struct eh_frame_array_ent *)
   1979   1.7  christos 	bfd_malloc (hdr_info->u.dwarf.fde_count
   1980   1.5  christos 		    * sizeof (*hdr_info->u.dwarf.array));
   1981   1.5  christos     }
   1982   1.5  christos   if (hdr_info->u.dwarf.array == NULL)
   1983   1.1     skrll     hdr_info = NULL;
   1984   1.1     skrll 
   1985   1.1     skrll   /* The new offsets can be bigger or smaller than the original offsets.
   1986   1.1     skrll      We therefore need to make two passes over the section: one backward
   1987   1.1     skrll      pass to move entries up and one forward pass to move entries down.
   1988   1.1     skrll      The two passes won't interfere with each other because entries are
   1989   1.1     skrll      not reordered  */
   1990   1.1     skrll   for (ent = sec_info->entry + sec_info->count; ent-- != sec_info->entry;)
   1991   1.1     skrll     if (!ent->removed && ent->new_offset > ent->offset)
   1992   1.1     skrll       memmove (contents + ent->new_offset, contents + ent->offset, ent->size);
   1993   1.1     skrll 
   1994   1.1     skrll   for (ent = sec_info->entry; ent < sec_info->entry + sec_info->count; ++ent)
   1995   1.1     skrll     if (!ent->removed && ent->new_offset < ent->offset)
   1996   1.1     skrll       memmove (contents + ent->new_offset, contents + ent->offset, ent->size);
   1997   1.1     skrll 
   1998   1.7  christos   last_ent = sec_info->entry + sec_info->count;
   1999   1.7  christos   for (ent = sec_info->entry; ent < last_ent; ++ent)
   2000   1.1     skrll     {
   2001   1.1     skrll       unsigned char *buf, *end;
   2002   1.1     skrll       unsigned int new_size;
   2003   1.1     skrll 
   2004   1.1     skrll       if (ent->removed)
   2005   1.1     skrll 	continue;
   2006   1.1     skrll 
   2007   1.1     skrll       if (ent->size == 4)
   2008   1.1     skrll 	{
   2009   1.1     skrll 	  /* Any terminating FDE must be at the end of the section.  */
   2010   1.7  christos 	  BFD_ASSERT (ent == last_ent - 1);
   2011   1.1     skrll 	  continue;
   2012   1.1     skrll 	}
   2013   1.1     skrll 
   2014   1.1     skrll       buf = contents + ent->new_offset;
   2015   1.1     skrll       end = buf + ent->size;
   2016   1.7  christos       new_size = next_cie_fde_offset (ent, last_ent, sec) - ent->new_offset;
   2017   1.1     skrll 
   2018   1.1     skrll       /* Update the size.  It may be shrinked.  */
   2019   1.1     skrll       bfd_put_32 (abfd, new_size - 4, buf);
   2020   1.1     skrll 
   2021   1.1     skrll       /* Filling the extra bytes with DW_CFA_nops.  */
   2022   1.1     skrll       if (new_size != ent->size)
   2023   1.1     skrll 	memset (end, 0, new_size - ent->size);
   2024   1.1     skrll 
   2025   1.1     skrll       if (ent->cie)
   2026   1.1     skrll 	{
   2027   1.1     skrll 	  /* CIE */
   2028   1.1     skrll 	  if (ent->make_relative
   2029   1.1     skrll 	      || ent->u.cie.make_lsda_relative
   2030   1.1     skrll 	      || ent->u.cie.per_encoding_relative)
   2031   1.1     skrll 	    {
   2032   1.1     skrll 	      char *aug;
   2033   1.9  christos 	      unsigned int version, action, extra_string, extra_data;
   2034   1.1     skrll 	      unsigned int per_width, per_encoding;
   2035   1.1     skrll 
   2036   1.1     skrll 	      /* Need to find 'R' or 'L' augmentation's argument and modify
   2037   1.1     skrll 		 DW_EH_PE_* value.  */
   2038   1.1     skrll 	      action = ((ent->make_relative ? 1 : 0)
   2039   1.1     skrll 			| (ent->u.cie.make_lsda_relative ? 2 : 0)
   2040   1.1     skrll 			| (ent->u.cie.per_encoding_relative ? 4 : 0));
   2041   1.1     skrll 	      extra_string = extra_augmentation_string_bytes (ent);
   2042   1.1     skrll 	      extra_data = extra_augmentation_data_bytes (ent);
   2043   1.1     skrll 
   2044   1.9  christos 	      /* Skip length, id.  */
   2045   1.9  christos 	      buf += 8;
   2046   1.9  christos 	      version = *buf++;
   2047   1.1     skrll 	      aug = (char *) buf;
   2048   1.1     skrll 	      buf += strlen (aug) + 1;
   2049   1.1     skrll 	      skip_leb128 (&buf, end);
   2050   1.1     skrll 	      skip_leb128 (&buf, end);
   2051   1.9  christos 	      if (version == 1)
   2052   1.9  christos 		skip_bytes (&buf, end, 1);
   2053   1.9  christos 	      else
   2054   1.9  christos 		skip_leb128 (&buf, end);
   2055   1.1     skrll 	      if (*aug == 'z')
   2056   1.1     skrll 		{
   2057   1.1     skrll 		  /* The uleb128 will always be a single byte for the kind
   2058   1.1     skrll 		     of augmentation strings that we're prepared to handle.  */
   2059   1.1     skrll 		  *buf++ += extra_data;
   2060   1.1     skrll 		  aug++;
   2061   1.1     skrll 		}
   2062   1.1     skrll 
   2063   1.1     skrll 	      /* Make room for the new augmentation string and data bytes.  */
   2064   1.1     skrll 	      memmove (buf + extra_string + extra_data, buf, end - buf);
   2065   1.1     skrll 	      memmove (aug + extra_string, aug, buf - (bfd_byte *) aug);
   2066   1.1     skrll 	      buf += extra_string;
   2067   1.1     skrll 	      end += extra_string + extra_data;
   2068   1.1     skrll 
   2069   1.1     skrll 	      if (ent->add_augmentation_size)
   2070   1.1     skrll 		{
   2071   1.1     skrll 		  *aug++ = 'z';
   2072   1.1     skrll 		  *buf++ = extra_data - 1;
   2073   1.1     skrll 		}
   2074   1.1     skrll 	      if (ent->u.cie.add_fde_encoding)
   2075   1.1     skrll 		{
   2076   1.1     skrll 		  BFD_ASSERT (action & 1);
   2077   1.1     skrll 		  *aug++ = 'R';
   2078   1.3  christos 		  *buf++ = make_pc_relative (DW_EH_PE_absptr, ptr_size);
   2079   1.1     skrll 		  action &= ~1;
   2080   1.1     skrll 		}
   2081   1.1     skrll 
   2082   1.1     skrll 	      while (action)
   2083   1.1     skrll 		switch (*aug++)
   2084   1.1     skrll 		  {
   2085   1.1     skrll 		  case 'L':
   2086   1.1     skrll 		    if (action & 2)
   2087   1.1     skrll 		      {
   2088   1.1     skrll 			BFD_ASSERT (*buf == ent->lsda_encoding);
   2089   1.3  christos 			*buf = make_pc_relative (*buf, ptr_size);
   2090   1.1     skrll 			action &= ~2;
   2091   1.1     skrll 		      }
   2092   1.1     skrll 		    buf++;
   2093   1.1     skrll 		    break;
   2094   1.1     skrll 		  case 'P':
   2095   1.3  christos 		    if (ent->u.cie.make_per_encoding_relative)
   2096   1.3  christos 		      *buf = make_pc_relative (*buf, ptr_size);
   2097   1.1     skrll 		    per_encoding = *buf++;
   2098   1.1     skrll 		    per_width = get_DW_EH_PE_width (per_encoding, ptr_size);
   2099   1.1     skrll 		    BFD_ASSERT (per_width != 0);
   2100   1.1     skrll 		    BFD_ASSERT (((per_encoding & 0x70) == DW_EH_PE_pcrel)
   2101   1.1     skrll 				== ent->u.cie.per_encoding_relative);
   2102   1.3  christos 		    if ((per_encoding & 0x70) == DW_EH_PE_aligned)
   2103   1.1     skrll 		      buf = (contents
   2104   1.1     skrll 			     + ((buf - contents + per_width - 1)
   2105   1.1     skrll 				& ~((bfd_size_type) per_width - 1)));
   2106   1.1     skrll 		    if (action & 4)
   2107   1.1     skrll 		      {
   2108   1.1     skrll 			bfd_vma val;
   2109   1.1     skrll 
   2110   1.1     skrll 			val = read_value (abfd, buf, per_width,
   2111   1.1     skrll 					  get_DW_EH_PE_signed (per_encoding));
   2112   1.3  christos 			if (ent->u.cie.make_per_encoding_relative)
   2113   1.3  christos 			  val -= (sec->output_section->vma
   2114   1.3  christos 				  + sec->output_offset
   2115   1.3  christos 				  + (buf - contents));
   2116   1.3  christos 			else
   2117   1.3  christos 			  {
   2118   1.3  christos 			    val += (bfd_vma) ent->offset - ent->new_offset;
   2119   1.3  christos 			    val -= extra_string + extra_data;
   2120   1.3  christos 			  }
   2121   1.1     skrll 			write_value (abfd, buf, val, per_width);
   2122   1.1     skrll 			action &= ~4;
   2123   1.1     skrll 		      }
   2124   1.1     skrll 		    buf += per_width;
   2125   1.1     skrll 		    break;
   2126   1.1     skrll 		  case 'R':
   2127   1.1     skrll 		    if (action & 1)
   2128   1.1     skrll 		      {
   2129   1.1     skrll 			BFD_ASSERT (*buf == ent->fde_encoding);
   2130   1.3  christos 			*buf = make_pc_relative (*buf, ptr_size);
   2131   1.1     skrll 			action &= ~1;
   2132   1.1     skrll 		      }
   2133   1.1     skrll 		    buf++;
   2134   1.1     skrll 		    break;
   2135   1.1     skrll 		  case 'S':
   2136   1.1     skrll 		    break;
   2137   1.1     skrll 		  default:
   2138   1.1     skrll 		    BFD_FAIL ();
   2139   1.1     skrll 		  }
   2140   1.1     skrll 	    }
   2141   1.1     skrll 	}
   2142   1.1     skrll       else
   2143   1.1     skrll 	{
   2144   1.1     skrll 	  /* FDE */
   2145   1.1     skrll 	  bfd_vma value, address;
   2146   1.1     skrll 	  unsigned int width;
   2147   1.1     skrll 	  bfd_byte *start;
   2148   1.1     skrll 	  struct eh_cie_fde *cie;
   2149   1.1     skrll 
   2150   1.1     skrll 	  /* Skip length.  */
   2151   1.1     skrll 	  cie = ent->u.fde.cie_inf;
   2152   1.1     skrll 	  buf += 4;
   2153   1.1     skrll 	  value = ((ent->new_offset + sec->output_offset + 4)
   2154   1.1     skrll 		   - (cie->new_offset + cie->u.cie.u.sec->output_offset));
   2155   1.1     skrll 	  bfd_put_32 (abfd, value, buf);
   2156   1.5  christos 	  if (bfd_link_relocatable (info))
   2157   1.5  christos 	    continue;
   2158   1.1     skrll 	  buf += 4;
   2159   1.1     skrll 	  width = get_DW_EH_PE_width (ent->fde_encoding, ptr_size);
   2160   1.1     skrll 	  value = read_value (abfd, buf, width,
   2161   1.1     skrll 			      get_DW_EH_PE_signed (ent->fde_encoding));
   2162   1.1     skrll 	  address = value;
   2163   1.1     skrll 	  if (value)
   2164   1.1     skrll 	    {
   2165   1.3  christos 	      switch (ent->fde_encoding & 0x70)
   2166   1.1     skrll 		{
   2167   1.1     skrll 		case DW_EH_PE_textrel:
   2168   1.1     skrll 		  BFD_ASSERT (hdr_info == NULL);
   2169   1.1     skrll 		  break;
   2170   1.1     skrll 		case DW_EH_PE_datarel:
   2171   1.1     skrll 		  {
   2172   1.3  christos 		    switch (abfd->arch_info->arch)
   2173   1.3  christos 		      {
   2174   1.3  christos 		      case bfd_arch_ia64:
   2175   1.3  christos 			BFD_ASSERT (elf_gp (abfd) != 0);
   2176   1.3  christos 			address += elf_gp (abfd);
   2177   1.3  christos 			break;
   2178   1.3  christos 		      default:
   2179   1.8  christos 			_bfd_error_handler
   2180   1.8  christos 			  (_("DW_EH_PE_datarel unspecified"
   2181   1.8  christos 			     " for this architecture"));
   2182   1.3  christos 			/* Fall thru */
   2183   1.3  christos 		      case bfd_arch_frv:
   2184   1.3  christos 		      case bfd_arch_i386:
   2185   1.3  christos 			BFD_ASSERT (htab->hgot != NULL
   2186   1.3  christos 				    && ((htab->hgot->root.type
   2187   1.3  christos 					 == bfd_link_hash_defined)
   2188   1.3  christos 					|| (htab->hgot->root.type
   2189   1.3  christos 					    == bfd_link_hash_defweak)));
   2190   1.3  christos 			address
   2191   1.3  christos 			  += (htab->hgot->root.u.def.value
   2192   1.3  christos 			      + htab->hgot->root.u.def.section->output_offset
   2193   1.3  christos 			      + (htab->hgot->root.u.def.section->output_section
   2194   1.3  christos 				 ->vma));
   2195   1.3  christos 			break;
   2196   1.3  christos 		      }
   2197   1.1     skrll 		  }
   2198   1.1     skrll 		  break;
   2199   1.1     skrll 		case DW_EH_PE_pcrel:
   2200   1.1     skrll 		  value += (bfd_vma) ent->offset - ent->new_offset;
   2201   1.1     skrll 		  address += (sec->output_section->vma
   2202   1.1     skrll 			      + sec->output_offset
   2203   1.1     skrll 			      + ent->offset + 8);
   2204   1.1     skrll 		  break;
   2205   1.1     skrll 		}
   2206   1.1     skrll 	      if (ent->make_relative)
   2207   1.1     skrll 		value -= (sec->output_section->vma
   2208   1.1     skrll 			  + sec->output_offset
   2209   1.1     skrll 			  + ent->new_offset + 8);
   2210   1.1     skrll 	      write_value (abfd, buf, value, width);
   2211   1.1     skrll 	    }
   2212   1.1     skrll 
   2213   1.1     skrll 	  start = buf;
   2214   1.1     skrll 
   2215   1.1     skrll 	  if (hdr_info)
   2216   1.1     skrll 	    {
   2217   1.3  christos 	      /* The address calculation may overflow, giving us a
   2218   1.3  christos 		 value greater than 4G on a 32-bit target when
   2219   1.3  christos 		 dwarf_vma is 64-bit.  */
   2220   1.3  christos 	      if (sizeof (address) > 4 && ptr_size == 4)
   2221   1.3  christos 		address &= 0xffffffff;
   2222   1.5  christos 	      hdr_info->u.dwarf.array[hdr_info->array_count].initial_loc
   2223   1.5  christos 		= address;
   2224   1.5  christos 	      hdr_info->u.dwarf.array[hdr_info->array_count].range
   2225  1.10  christos 		= read_value (abfd, buf + width, width, false);
   2226   1.5  christos 	      hdr_info->u.dwarf.array[hdr_info->array_count++].fde
   2227   1.1     skrll 		= (sec->output_section->vma
   2228   1.1     skrll 		   + sec->output_offset
   2229   1.1     skrll 		   + ent->new_offset);
   2230   1.1     skrll 	    }
   2231   1.1     skrll 
   2232   1.3  christos 	  if ((ent->lsda_encoding & 0x70) == DW_EH_PE_pcrel
   2233   1.1     skrll 	      || cie->u.cie.make_lsda_relative)
   2234   1.1     skrll 	    {
   2235   1.1     skrll 	      buf += ent->lsda_offset;
   2236   1.1     skrll 	      width = get_DW_EH_PE_width (ent->lsda_encoding, ptr_size);
   2237   1.1     skrll 	      value = read_value (abfd, buf, width,
   2238   1.1     skrll 				  get_DW_EH_PE_signed (ent->lsda_encoding));
   2239   1.1     skrll 	      if (value)
   2240   1.1     skrll 		{
   2241   1.3  christos 		  if ((ent->lsda_encoding & 0x70) == DW_EH_PE_pcrel)
   2242   1.1     skrll 		    value += (bfd_vma) ent->offset - ent->new_offset;
   2243   1.1     skrll 		  else if (cie->u.cie.make_lsda_relative)
   2244   1.1     skrll 		    value -= (sec->output_section->vma
   2245   1.1     skrll 			      + sec->output_offset
   2246   1.1     skrll 			      + ent->new_offset + 8 + ent->lsda_offset);
   2247   1.1     skrll 		  write_value (abfd, buf, value, width);
   2248   1.1     skrll 		}
   2249   1.1     skrll 	    }
   2250   1.1     skrll 	  else if (ent->add_augmentation_size)
   2251   1.1     skrll 	    {
   2252   1.1     skrll 	      /* Skip the PC and length and insert a zero byte for the
   2253   1.1     skrll 		 augmentation size.  */
   2254   1.1     skrll 	      buf += width * 2;
   2255   1.1     skrll 	      memmove (buf + 1, buf, end - buf);
   2256   1.1     skrll 	      *buf = 0;
   2257   1.1     skrll 	    }
   2258   1.1     skrll 
   2259   1.1     skrll 	  if (ent->set_loc)
   2260   1.1     skrll 	    {
   2261   1.1     skrll 	      /* Adjust DW_CFA_set_loc.  */
   2262   1.3  christos 	      unsigned int cnt;
   2263   1.1     skrll 	      bfd_vma new_offset;
   2264   1.1     skrll 
   2265   1.1     skrll 	      width = get_DW_EH_PE_width (ent->fde_encoding, ptr_size);
   2266   1.1     skrll 	      new_offset = ent->new_offset + 8
   2267   1.1     skrll 			   + extra_augmentation_string_bytes (ent)
   2268   1.1     skrll 			   + extra_augmentation_data_bytes (ent);
   2269   1.1     skrll 
   2270   1.1     skrll 	      for (cnt = 1; cnt <= ent->set_loc[0]; cnt++)
   2271   1.1     skrll 		{
   2272   1.1     skrll 		  buf = start + ent->set_loc[cnt];
   2273   1.1     skrll 
   2274   1.1     skrll 		  value = read_value (abfd, buf, width,
   2275   1.1     skrll 				      get_DW_EH_PE_signed (ent->fde_encoding));
   2276   1.1     skrll 		  if (!value)
   2277   1.1     skrll 		    continue;
   2278   1.1     skrll 
   2279   1.3  christos 		  if ((ent->fde_encoding & 0x70) == DW_EH_PE_pcrel)
   2280   1.1     skrll 		    value += (bfd_vma) ent->offset + 8 - new_offset;
   2281   1.1     skrll 		  if (ent->make_relative)
   2282   1.1     skrll 		    value -= (sec->output_section->vma
   2283   1.1     skrll 			      + sec->output_offset
   2284   1.1     skrll 			      + new_offset + ent->set_loc[cnt]);
   2285   1.1     skrll 		  write_value (abfd, buf, value, width);
   2286   1.1     skrll 		}
   2287   1.1     skrll 	    }
   2288   1.1     skrll 	}
   2289   1.1     skrll     }
   2290   1.1     skrll 
   2291   1.3  christos   /* FIXME: octets_per_byte.  */
   2292   1.1     skrll   return bfd_set_section_contents (abfd, sec->output_section,
   2293   1.1     skrll 				   contents, (file_ptr) sec->output_offset,
   2294   1.1     skrll 				   sec->size);
   2295   1.1     skrll }
   2296   1.1     skrll 
   2297  1.13  christos /* A handy wrapper for writing linker generated .eh_frame sections
   2298  1.13  christos    with contents that may need to be extended beyond the initial size
   2299  1.13  christos    allocated.  */
   2300  1.13  christos 
   2301  1.13  christos bool
   2302  1.13  christos _bfd_elf_write_linker_section_eh_frame (bfd *obfd, struct bfd_link_info *info,
   2303  1.13  christos 					asection *sec, bfd_byte *bigbuf)
   2304  1.13  christos {
   2305  1.13  christos   bfd_size_type initial_size = sec->rawsize != 0 ? sec->rawsize : sec->size;
   2306  1.13  christos   memcpy (bigbuf, sec->contents, initial_size);
   2307  1.13  christos   if (!_bfd_elf_write_section_eh_frame (obfd, info, sec, bigbuf))
   2308  1.13  christos     return false;
   2309  1.13  christos   if (sec->size > initial_size)
   2310  1.13  christos     {
   2311  1.13  christos       if (sec->alloced)
   2312  1.13  christos 	sec->contents = bfd_alloc (sec->owner, sec->size);
   2313  1.13  christos       else
   2314  1.13  christos 	{
   2315  1.13  christos 	  free (sec->contents);
   2316  1.13  christos 	  sec->contents = bfd_malloc (sec->size);
   2317  1.13  christos 	}
   2318  1.13  christos       if (sec->contents == NULL)
   2319  1.13  christos 	return false;
   2320  1.13  christos     }
   2321  1.13  christos   memcpy (sec->contents, bigbuf, sec->size);
   2322  1.13  christos   return true;
   2323  1.13  christos }
   2324  1.13  christos 
   2325   1.1     skrll /* Helper function used to sort .eh_frame_hdr search table by increasing
   2326   1.1     skrll    VMA of FDE initial location.  */
   2327   1.1     skrll 
   2328   1.1     skrll static int
   2329   1.1     skrll vma_compare (const void *a, const void *b)
   2330   1.1     skrll {
   2331   1.3  christos   const struct eh_frame_array_ent *p = (const struct eh_frame_array_ent *) a;
   2332   1.3  christos   const struct eh_frame_array_ent *q = (const struct eh_frame_array_ent *) b;
   2333   1.1     skrll   if (p->initial_loc > q->initial_loc)
   2334   1.1     skrll     return 1;
   2335   1.1     skrll   if (p->initial_loc < q->initial_loc)
   2336   1.1     skrll     return -1;
   2337   1.5  christos   if (p->range > q->range)
   2338   1.5  christos     return 1;
   2339   1.5  christos   if (p->range < q->range)
   2340   1.5  christos     return -1;
   2341   1.1     skrll   return 0;
   2342   1.1     skrll }
   2343   1.1     skrll 
   2344   1.5  christos /* Reorder .eh_frame_entry sections to match the associated text sections.
   2345   1.5  christos    This routine is called during the final linking step, just before writing
   2346   1.5  christos    the contents.  At this stage, sections in the eh_frame_hdr_info are already
   2347   1.5  christos    sorted in order of increasing text section address and so we simply need
   2348   1.5  christos    to make the .eh_frame_entrys follow that same order.  Note that it is
   2349   1.5  christos    invalid for a linker script to try to force a particular order of
   2350   1.5  christos    .eh_frame_entry sections.  */
   2351   1.5  christos 
   2352  1.10  christos bool
   2353   1.5  christos _bfd_elf_fixup_eh_frame_hdr (struct bfd_link_info *info)
   2354   1.5  christos {
   2355   1.5  christos   asection *sec = NULL;
   2356   1.5  christos   asection *osec;
   2357   1.5  christos   struct eh_frame_hdr_info *hdr_info;
   2358   1.5  christos   unsigned int i;
   2359   1.5  christos   bfd_vma offset;
   2360   1.5  christos   struct bfd_link_order *p;
   2361   1.5  christos 
   2362   1.5  christos   hdr_info = &elf_hash_table (info)->eh_info;
   2363   1.5  christos 
   2364   1.5  christos   if (hdr_info->hdr_sec == NULL
   2365   1.5  christos       || info->eh_frame_hdr_type != COMPACT_EH_HDR
   2366   1.5  christos       || hdr_info->array_count == 0)
   2367  1.10  christos     return true;
   2368   1.5  christos 
   2369   1.5  christos   /* Change section output offsets to be in text section order.  */
   2370   1.5  christos   offset = 8;
   2371   1.5  christos   osec = hdr_info->u.compact.entries[0]->output_section;
   2372   1.5  christos   for (i = 0; i < hdr_info->array_count; i++)
   2373   1.5  christos     {
   2374   1.5  christos       sec = hdr_info->u.compact.entries[i];
   2375   1.5  christos       if (sec->output_section != osec)
   2376   1.5  christos 	{
   2377   1.7  christos 	  _bfd_error_handler
   2378   1.8  christos 	    (_("invalid output section for .eh_frame_entry: %pA"),
   2379   1.7  christos 	     sec->output_section);
   2380  1.10  christos 	  return false;
   2381   1.5  christos 	}
   2382   1.5  christos       sec->output_offset = offset;
   2383   1.5  christos       offset += sec->size;
   2384   1.5  christos     }
   2385   1.5  christos 
   2386   1.5  christos 
   2387   1.5  christos   /* Fix the link_order to match.  */
   2388   1.5  christos   for (p = sec->output_section->map_head.link_order; p != NULL; p = p->next)
   2389   1.5  christos     {
   2390   1.5  christos       if (p->type != bfd_indirect_link_order)
   2391   1.5  christos 	abort();
   2392   1.5  christos 
   2393   1.5  christos       p->offset = p->u.indirect.section->output_offset;
   2394   1.5  christos       if (p->next != NULL)
   2395   1.7  christos 	i--;
   2396   1.5  christos     }
   2397   1.5  christos 
   2398   1.5  christos   if (i != 0)
   2399   1.5  christos     {
   2400   1.7  christos       _bfd_error_handler
   2401   1.8  christos 	(_("invalid contents in %pA section"), osec);
   2402  1.10  christos       return false;
   2403   1.5  christos     }
   2404   1.5  christos 
   2405  1.10  christos   return true;
   2406   1.5  christos }
   2407   1.5  christos 
   2408   1.5  christos /* The .eh_frame_hdr format for Compact EH frames:
   2409   1.5  christos    ubyte version		(2)
   2410   1.5  christos    ubyte eh_ref_enc		(DW_EH_PE_* encoding of typinfo references)
   2411   1.5  christos    uint32_t count		(Number of entries in table)
   2412   1.5  christos    [array from .eh_frame_entry sections]  */
   2413   1.5  christos 
   2414  1.10  christos static bool
   2415   1.5  christos write_compact_eh_frame_hdr (bfd *abfd, struct bfd_link_info *info)
   2416   1.5  christos {
   2417   1.5  christos   struct elf_link_hash_table *htab;
   2418   1.5  christos   struct eh_frame_hdr_info *hdr_info;
   2419   1.5  christos   asection *sec;
   2420  1.13  christos   elf_backend_data *bed;
   2421   1.5  christos   bfd_vma count;
   2422   1.5  christos   bfd_byte contents[8];
   2423   1.5  christos   unsigned int i;
   2424   1.5  christos 
   2425   1.5  christos   htab = elf_hash_table (info);
   2426   1.5  christos   hdr_info = &htab->eh_info;
   2427   1.5  christos   sec = hdr_info->hdr_sec;
   2428   1.5  christos 
   2429   1.5  christos   if (sec->size != 8)
   2430   1.5  christos     abort();
   2431   1.5  christos 
   2432   1.5  christos   for (i = 0; i < sizeof (contents); i++)
   2433   1.5  christos     contents[i] = 0;
   2434   1.5  christos 
   2435   1.5  christos   contents[0] = COMPACT_EH_HDR;
   2436   1.5  christos   bed = get_elf_backend_data (abfd);
   2437   1.5  christos 
   2438   1.5  christos   BFD_ASSERT (bed->compact_eh_encoding);
   2439   1.5  christos   contents[1] = (*bed->compact_eh_encoding) (info);
   2440   1.5  christos 
   2441   1.5  christos   count = (sec->output_section->size - 8) / 8;
   2442   1.5  christos   bfd_put_32 (abfd, count, contents + 4);
   2443   1.5  christos   return bfd_set_section_contents (abfd, sec->output_section, contents,
   2444   1.5  christos 				   (file_ptr) sec->output_offset, sec->size);
   2445   1.5  christos }
   2446   1.5  christos 
   2447   1.5  christos /* The .eh_frame_hdr format for DWARF frames:
   2448   1.5  christos 
   2449   1.1     skrll    ubyte version		(currently 1)
   2450   1.7  christos    ubyte eh_frame_ptr_enc	(DW_EH_PE_* encoding of pointer to start of
   2451   1.1     skrll 				 .eh_frame section)
   2452   1.1     skrll    ubyte fde_count_enc		(DW_EH_PE_* encoding of total FDE count
   2453   1.1     skrll 				 number (or DW_EH_PE_omit if there is no
   2454   1.1     skrll 				 binary search table computed))
   2455   1.1     skrll    ubyte table_enc		(DW_EH_PE_* encoding of binary search table,
   2456   1.1     skrll 				 or DW_EH_PE_omit if not present.
   2457   1.1     skrll 				 DW_EH_PE_datarel is using address of
   2458   1.1     skrll 				 .eh_frame_hdr section start as base)
   2459   1.1     skrll    [encoded] eh_frame_ptr	(pointer to start of .eh_frame section)
   2460   1.1     skrll    optionally followed by:
   2461   1.1     skrll    [encoded] fde_count		(total number of FDEs in .eh_frame section)
   2462   1.1     skrll    fde_count x [encoded] initial_loc, fde
   2463   1.1     skrll 				(array of encoded pairs containing
   2464   1.1     skrll 				 FDE initial_location field and FDE address,
   2465   1.1     skrll 				 sorted by increasing initial_loc).  */
   2466   1.1     skrll 
   2467  1.10  christos static bool
   2468   1.5  christos write_dwarf_eh_frame_hdr (bfd *abfd, struct bfd_link_info *info)
   2469   1.1     skrll {
   2470   1.1     skrll   struct elf_link_hash_table *htab;
   2471   1.1     skrll   struct eh_frame_hdr_info *hdr_info;
   2472   1.1     skrll   asection *sec;
   2473  1.12  christos   bool retval = false;
   2474   1.5  christos 
   2475   1.5  christos   htab = elf_hash_table (info);
   2476   1.5  christos   hdr_info = &htab->eh_info;
   2477   1.5  christos   sec = hdr_info->hdr_sec;
   2478   1.1     skrll   bfd_byte *contents;
   2479   1.1     skrll   asection *eh_frame_sec;
   2480   1.1     skrll   bfd_size_type size;
   2481   1.1     skrll   bfd_vma encoded_eh_frame;
   2482   1.1     skrll 
   2483   1.1     skrll   size = EH_FRAME_HDR_SIZE;
   2484   1.5  christos   if (hdr_info->u.dwarf.array
   2485   1.5  christos       && hdr_info->array_count == hdr_info->u.dwarf.fde_count)
   2486   1.5  christos     size += 4 + hdr_info->u.dwarf.fde_count * 8;
   2487   1.3  christos   contents = (bfd_byte *) bfd_malloc (size);
   2488   1.1     skrll   if (contents == NULL)
   2489  1.12  christos     goto out;
   2490   1.1     skrll 
   2491   1.1     skrll   eh_frame_sec = bfd_get_section_by_name (abfd, ".eh_frame");
   2492   1.1     skrll   if (eh_frame_sec == NULL)
   2493  1.12  christos     goto out;
   2494   1.1     skrll 
   2495   1.1     skrll   memset (contents, 0, EH_FRAME_HDR_SIZE);
   2496   1.5  christos   /* Version.  */
   2497   1.5  christos   contents[0] = 1;
   2498   1.5  christos   /* .eh_frame offset.  */
   2499   1.1     skrll   contents[1] = get_elf_backend_data (abfd)->elf_backend_encode_eh_address
   2500   1.5  christos     (abfd, info, eh_frame_sec, 0, sec, 4, &encoded_eh_frame);
   2501   1.1     skrll 
   2502   1.5  christos   if (hdr_info->u.dwarf.array
   2503   1.5  christos       && hdr_info->array_count == hdr_info->u.dwarf.fde_count)
   2504   1.1     skrll     {
   2505   1.5  christos       /* FDE count encoding.  */
   2506   1.5  christos       contents[2] = DW_EH_PE_udata4;
   2507   1.5  christos       /* Search table encoding.  */
   2508   1.5  christos       contents[3] = DW_EH_PE_datarel | DW_EH_PE_sdata4;
   2509   1.1     skrll     }
   2510   1.1     skrll   else
   2511   1.1     skrll     {
   2512   1.1     skrll       contents[2] = DW_EH_PE_omit;
   2513   1.1     skrll       contents[3] = DW_EH_PE_omit;
   2514   1.1     skrll     }
   2515   1.1     skrll   bfd_put_32 (abfd, encoded_eh_frame, contents + 4);
   2516   1.1     skrll 
   2517  1.12  christos   retval = true;
   2518   1.1     skrll   if (contents[2] != DW_EH_PE_omit)
   2519   1.1     skrll     {
   2520   1.1     skrll       unsigned int i;
   2521  1.10  christos       bool overlap, overflow;
   2522   1.1     skrll 
   2523   1.5  christos       bfd_put_32 (abfd, hdr_info->u.dwarf.fde_count,
   2524   1.5  christos 		  contents + EH_FRAME_HDR_SIZE);
   2525   1.5  christos       qsort (hdr_info->u.dwarf.array, hdr_info->u.dwarf.fde_count,
   2526   1.5  christos 	     sizeof (*hdr_info->u.dwarf.array), vma_compare);
   2527  1.10  christos       overlap = false;
   2528  1.10  christos       overflow = false;
   2529   1.5  christos       for (i = 0; i < hdr_info->u.dwarf.fde_count; i++)
   2530   1.5  christos 	{
   2531   1.5  christos 	  bfd_vma val;
   2532   1.5  christos 
   2533   1.5  christos 	  val = hdr_info->u.dwarf.array[i].initial_loc
   2534   1.5  christos 	    - sec->output_section->vma;
   2535   1.5  christos 	  val = ((val & 0xffffffff) ^ 0x80000000) - 0x80000000;
   2536   1.5  christos 	  if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64
   2537   1.5  christos 	      && (hdr_info->u.dwarf.array[i].initial_loc
   2538   1.5  christos 		  != sec->output_section->vma + val))
   2539  1.10  christos 	    overflow = true;
   2540   1.5  christos 	  bfd_put_32 (abfd, val, contents + EH_FRAME_HDR_SIZE + i * 8 + 4);
   2541   1.5  christos 	  val = hdr_info->u.dwarf.array[i].fde - sec->output_section->vma;
   2542   1.5  christos 	  val = ((val & 0xffffffff) ^ 0x80000000) - 0x80000000;
   2543   1.5  christos 	  if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64
   2544   1.5  christos 	      && (hdr_info->u.dwarf.array[i].fde
   2545   1.5  christos 		  != sec->output_section->vma + val))
   2546  1.10  christos 	    overflow = true;
   2547   1.5  christos 	  bfd_put_32 (abfd, val, contents + EH_FRAME_HDR_SIZE + i * 8 + 8);
   2548   1.5  christos 	  if (i != 0
   2549   1.5  christos 	      && (hdr_info->u.dwarf.array[i].initial_loc
   2550   1.5  christos 		  < (hdr_info->u.dwarf.array[i - 1].initial_loc
   2551   1.5  christos 		     + hdr_info->u.dwarf.array[i - 1].range)))
   2552  1.10  christos 	    overlap = true;
   2553   1.5  christos 	}
   2554   1.5  christos       if (overflow)
   2555   1.8  christos 	_bfd_error_handler (_(".eh_frame_hdr entry overflow"));
   2556   1.5  christos       if (overlap)
   2557   1.8  christos 	_bfd_error_handler (_(".eh_frame_hdr refers to overlapping FDEs"));
   2558   1.5  christos       if (overflow || overlap)
   2559   1.1     skrll 	{
   2560   1.5  christos 	  bfd_set_error (bfd_error_bad_value);
   2561  1.10  christos 	  retval = false;
   2562   1.1     skrll 	}
   2563   1.1     skrll     }
   2564   1.1     skrll 
   2565   1.3  christos   /* FIXME: octets_per_byte.  */
   2566   1.5  christos   if (!bfd_set_section_contents (abfd, sec->output_section, contents,
   2567   1.5  christos 				 (file_ptr) sec->output_offset,
   2568  1.12  christos 				 size))
   2569  1.10  christos     retval = false;
   2570  1.12  christos  out:
   2571   1.1     skrll   free (contents);
   2572  1.10  christos   free (hdr_info->u.dwarf.array);
   2573  1.12  christos   hdr_info->u.dwarf.array = NULL;
   2574   1.1     skrll   return retval;
   2575   1.1     skrll }
   2576   1.1     skrll 
   2577   1.5  christos /* Write out .eh_frame_hdr section.  This must be called after
   2578   1.5  christos    _bfd_elf_write_section_eh_frame has been called on all input
   2579   1.5  christos    .eh_frame sections.  */
   2580   1.5  christos 
   2581  1.10  christos bool
   2582   1.5  christos _bfd_elf_write_section_eh_frame_hdr (bfd *abfd, struct bfd_link_info *info)
   2583   1.5  christos {
   2584   1.5  christos   struct elf_link_hash_table *htab;
   2585   1.5  christos   struct eh_frame_hdr_info *hdr_info;
   2586   1.5  christos   asection *sec;
   2587   1.5  christos 
   2588   1.5  christos   htab = elf_hash_table (info);
   2589   1.5  christos   hdr_info = &htab->eh_info;
   2590   1.5  christos   sec = hdr_info->hdr_sec;
   2591   1.5  christos 
   2592   1.5  christos   if (info->eh_frame_hdr_type == 0 || sec == NULL)
   2593  1.10  christos     return true;
   2594   1.5  christos 
   2595   1.5  christos   if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
   2596   1.5  christos     return write_compact_eh_frame_hdr (abfd, info);
   2597   1.5  christos   else
   2598   1.5  christos     return write_dwarf_eh_frame_hdr (abfd, info);
   2599   1.5  christos }
   2600   1.5  christos 
   2601   1.1     skrll /* Return the width of FDE addresses.  This is the default implementation.  */
   2602   1.1     skrll 
   2603   1.1     skrll unsigned int
   2604   1.7  christos _bfd_elf_eh_frame_address_size (bfd *abfd, const asection *sec ATTRIBUTE_UNUSED)
   2605   1.1     skrll {
   2606   1.1     skrll   return elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64 ? 8 : 4;
   2607   1.1     skrll }
   2608   1.1     skrll 
   2609   1.1     skrll /* Decide whether we can use a PC-relative encoding within the given
   2610   1.1     skrll    EH frame section.  This is the default implementation.  */
   2611   1.1     skrll 
   2612  1.10  christos bool
   2613   1.1     skrll _bfd_elf_can_make_relative (bfd *input_bfd ATTRIBUTE_UNUSED,
   2614   1.1     skrll 			    struct bfd_link_info *info ATTRIBUTE_UNUSED,
   2615   1.1     skrll 			    asection *eh_frame_section ATTRIBUTE_UNUSED)
   2616   1.1     skrll {
   2617  1.10  christos   return true;
   2618   1.1     skrll }
   2619   1.1     skrll 
   2620   1.1     skrll /* Select an encoding for the given address.  Preference is given to
   2621   1.1     skrll    PC-relative addressing modes.  */
   2622   1.1     skrll 
   2623   1.1     skrll bfd_byte
   2624   1.1     skrll _bfd_elf_encode_eh_address (bfd *abfd ATTRIBUTE_UNUSED,
   2625   1.1     skrll 			    struct bfd_link_info *info ATTRIBUTE_UNUSED,
   2626   1.1     skrll 			    asection *osec, bfd_vma offset,
   2627   1.1     skrll 			    asection *loc_sec, bfd_vma loc_offset,
   2628   1.1     skrll 			    bfd_vma *encoded)
   2629   1.1     skrll {
   2630   1.1     skrll   *encoded = osec->vma + offset -
   2631   1.1     skrll     (loc_sec->output_section->vma + loc_sec->output_offset + loc_offset);
   2632   1.1     skrll   return DW_EH_PE_pcrel | DW_EH_PE_sdata4;
   2633   1.1     skrll }
   2634