Home | History | Annotate | Line # | Download | only in bfd
elf-eh-frame.c revision 1.3
      1  1.1     skrll /* .eh_frame section optimization.
      2  1.3  christos    Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
      3  1.1     skrll    Free Software Foundation, Inc.
      4  1.1     skrll    Written by Jakub Jelinek <jakub (at) redhat.com>.
      5  1.1     skrll 
      6  1.1     skrll    This file is part of BFD, the Binary File Descriptor library.
      7  1.1     skrll 
      8  1.1     skrll    This program is free software; you can redistribute it and/or modify
      9  1.1     skrll    it under the terms of the GNU General Public License as published by
     10  1.1     skrll    the Free Software Foundation; either version 3 of the License, or
     11  1.1     skrll    (at your option) any later version.
     12  1.1     skrll 
     13  1.1     skrll    This program is distributed in the hope that it will be useful,
     14  1.1     skrll    but WITHOUT ANY WARRANTY; without even the implied warranty of
     15  1.1     skrll    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     16  1.1     skrll    GNU General Public License for more details.
     17  1.1     skrll 
     18  1.1     skrll    You should have received a copy of the GNU General Public License
     19  1.1     skrll    along with this program; if not, write to the Free Software
     20  1.1     skrll    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
     21  1.1     skrll    MA 02110-1301, USA.  */
     22  1.1     skrll 
     23  1.1     skrll #include "sysdep.h"
     24  1.1     skrll #include "bfd.h"
     25  1.1     skrll #include "libbfd.h"
     26  1.1     skrll #include "elf-bfd.h"
     27  1.3  christos #include "dwarf2.h"
     28  1.1     skrll 
     29  1.1     skrll #define EH_FRAME_HDR_SIZE 8
     30  1.1     skrll 
     31  1.1     skrll struct cie
     32  1.1     skrll {
     33  1.1     skrll   unsigned int length;
     34  1.1     skrll   unsigned int hash;
     35  1.1     skrll   unsigned char version;
     36  1.1     skrll   unsigned char local_personality;
     37  1.1     skrll   char augmentation[20];
     38  1.1     skrll   bfd_vma code_align;
     39  1.1     skrll   bfd_signed_vma data_align;
     40  1.1     skrll   bfd_vma ra_column;
     41  1.1     skrll   bfd_vma augmentation_size;
     42  1.1     skrll   union {
     43  1.1     skrll     struct elf_link_hash_entry *h;
     44  1.1     skrll     bfd_vma val;
     45  1.1     skrll     unsigned int reloc_index;
     46  1.1     skrll   } personality;
     47  1.1     skrll   asection *output_sec;
     48  1.1     skrll   struct eh_cie_fde *cie_inf;
     49  1.1     skrll   unsigned char per_encoding;
     50  1.1     skrll   unsigned char lsda_encoding;
     51  1.1     skrll   unsigned char fde_encoding;
     52  1.1     skrll   unsigned char initial_insn_length;
     53  1.1     skrll   unsigned char can_make_lsda_relative;
     54  1.1     skrll   unsigned char initial_instructions[50];
     55  1.1     skrll };
     56  1.1     skrll 
     57  1.1     skrll 
     58  1.1     skrll 
     59  1.1     skrll /* If *ITER hasn't reached END yet, read the next byte into *RESULT and
     60  1.1     skrll    move onto the next byte.  Return true on success.  */
     61  1.1     skrll 
     62  1.1     skrll static inline bfd_boolean
     63  1.1     skrll read_byte (bfd_byte **iter, bfd_byte *end, unsigned char *result)
     64  1.1     skrll {
     65  1.1     skrll   if (*iter >= end)
     66  1.1     skrll     return FALSE;
     67  1.1     skrll   *result = *((*iter)++);
     68  1.1     skrll   return TRUE;
     69  1.1     skrll }
     70  1.1     skrll 
     71  1.1     skrll /* Move *ITER over LENGTH bytes, or up to END, whichever is closer.
     72  1.1     skrll    Return true it was possible to move LENGTH bytes.  */
     73  1.1     skrll 
     74  1.1     skrll static inline bfd_boolean
     75  1.1     skrll skip_bytes (bfd_byte **iter, bfd_byte *end, bfd_size_type length)
     76  1.1     skrll {
     77  1.1     skrll   if ((bfd_size_type) (end - *iter) < length)
     78  1.1     skrll     {
     79  1.1     skrll       *iter = end;
     80  1.1     skrll       return FALSE;
     81  1.1     skrll     }
     82  1.1     skrll   *iter += length;
     83  1.1     skrll   return TRUE;
     84  1.1     skrll }
     85  1.1     skrll 
     86  1.1     skrll /* Move *ITER over an leb128, stopping at END.  Return true if the end
     87  1.1     skrll    of the leb128 was found.  */
     88  1.1     skrll 
     89  1.1     skrll static bfd_boolean
     90  1.1     skrll skip_leb128 (bfd_byte **iter, bfd_byte *end)
     91  1.1     skrll {
     92  1.1     skrll   unsigned char byte;
     93  1.1     skrll   do
     94  1.1     skrll     if (!read_byte (iter, end, &byte))
     95  1.1     skrll       return FALSE;
     96  1.1     skrll   while (byte & 0x80);
     97  1.1     skrll   return TRUE;
     98  1.1     skrll }
     99  1.1     skrll 
    100  1.1     skrll /* Like skip_leb128, but treat the leb128 as an unsigned value and
    101  1.1     skrll    store it in *VALUE.  */
    102  1.1     skrll 
    103  1.1     skrll static bfd_boolean
    104  1.1     skrll read_uleb128 (bfd_byte **iter, bfd_byte *end, bfd_vma *value)
    105  1.1     skrll {
    106  1.1     skrll   bfd_byte *start, *p;
    107  1.1     skrll 
    108  1.1     skrll   start = *iter;
    109  1.1     skrll   if (!skip_leb128 (iter, end))
    110  1.1     skrll     return FALSE;
    111  1.1     skrll 
    112  1.1     skrll   p = *iter;
    113  1.1     skrll   *value = *--p;
    114  1.1     skrll   while (p > start)
    115  1.1     skrll     *value = (*value << 7) | (*--p & 0x7f);
    116  1.1     skrll 
    117  1.1     skrll   return TRUE;
    118  1.1     skrll }
    119  1.1     skrll 
    120  1.1     skrll /* Like read_uleb128, but for signed values.  */
    121  1.1     skrll 
    122  1.1     skrll static bfd_boolean
    123  1.1     skrll read_sleb128 (bfd_byte **iter, bfd_byte *end, bfd_signed_vma *value)
    124  1.1     skrll {
    125  1.1     skrll   bfd_byte *start, *p;
    126  1.1     skrll 
    127  1.1     skrll   start = *iter;
    128  1.1     skrll   if (!skip_leb128 (iter, end))
    129  1.1     skrll     return FALSE;
    130  1.1     skrll 
    131  1.1     skrll   p = *iter;
    132  1.1     skrll   *value = ((*--p & 0x7f) ^ 0x40) - 0x40;
    133  1.1     skrll   while (p > start)
    134  1.1     skrll     *value = (*value << 7) | (*--p & 0x7f);
    135  1.1     skrll 
    136  1.1     skrll   return TRUE;
    137  1.1     skrll }
    138  1.1     skrll 
    139  1.1     skrll /* Return 0 if either encoding is variable width, or not yet known to bfd.  */
    140  1.1     skrll 
    141  1.1     skrll static
    142  1.1     skrll int get_DW_EH_PE_width (int encoding, int ptr_size)
    143  1.1     skrll {
    144  1.1     skrll   /* DW_EH_PE_ values of 0x60 and 0x70 weren't defined at the time .eh_frame
    145  1.1     skrll      was added to bfd.  */
    146  1.1     skrll   if ((encoding & 0x60) == 0x60)
    147  1.1     skrll     return 0;
    148  1.1     skrll 
    149  1.1     skrll   switch (encoding & 7)
    150  1.1     skrll     {
    151  1.1     skrll     case DW_EH_PE_udata2: return 2;
    152  1.1     skrll     case DW_EH_PE_udata4: return 4;
    153  1.1     skrll     case DW_EH_PE_udata8: return 8;
    154  1.1     skrll     case DW_EH_PE_absptr: return ptr_size;
    155  1.1     skrll     default:
    156  1.1     skrll       break;
    157  1.1     skrll     }
    158  1.1     skrll 
    159  1.1     skrll   return 0;
    160  1.1     skrll }
    161  1.1     skrll 
    162  1.1     skrll #define get_DW_EH_PE_signed(encoding) (((encoding) & DW_EH_PE_signed) != 0)
    163  1.1     skrll 
    164  1.1     skrll /* Read a width sized value from memory.  */
    165  1.1     skrll 
    166  1.1     skrll static bfd_vma
    167  1.1     skrll read_value (bfd *abfd, bfd_byte *buf, int width, int is_signed)
    168  1.1     skrll {
    169  1.1     skrll   bfd_vma value;
    170  1.1     skrll 
    171  1.1     skrll   switch (width)
    172  1.1     skrll     {
    173  1.1     skrll     case 2:
    174  1.1     skrll       if (is_signed)
    175  1.1     skrll 	value = bfd_get_signed_16 (abfd, buf);
    176  1.1     skrll       else
    177  1.1     skrll 	value = bfd_get_16 (abfd, buf);
    178  1.1     skrll       break;
    179  1.1     skrll     case 4:
    180  1.1     skrll       if (is_signed)
    181  1.1     skrll 	value = bfd_get_signed_32 (abfd, buf);
    182  1.1     skrll       else
    183  1.1     skrll 	value = bfd_get_32 (abfd, buf);
    184  1.1     skrll       break;
    185  1.1     skrll     case 8:
    186  1.1     skrll       if (is_signed)
    187  1.1     skrll 	value = bfd_get_signed_64 (abfd, buf);
    188  1.1     skrll       else
    189  1.1     skrll 	value = bfd_get_64 (abfd, buf);
    190  1.1     skrll       break;
    191  1.1     skrll     default:
    192  1.1     skrll       BFD_FAIL ();
    193  1.1     skrll       return 0;
    194  1.1     skrll     }
    195  1.1     skrll 
    196  1.1     skrll   return value;
    197  1.1     skrll }
    198  1.1     skrll 
    199  1.1     skrll /* Store a width sized value to memory.  */
    200  1.1     skrll 
    201  1.1     skrll static void
    202  1.1     skrll write_value (bfd *abfd, bfd_byte *buf, bfd_vma value, int width)
    203  1.1     skrll {
    204  1.1     skrll   switch (width)
    205  1.1     skrll     {
    206  1.1     skrll     case 2: bfd_put_16 (abfd, value, buf); break;
    207  1.1     skrll     case 4: bfd_put_32 (abfd, value, buf); break;
    208  1.1     skrll     case 8: bfd_put_64 (abfd, value, buf); break;
    209  1.1     skrll     default: BFD_FAIL ();
    210  1.1     skrll     }
    211  1.1     skrll }
    212  1.1     skrll 
    213  1.1     skrll /* Return one if C1 and C2 CIEs can be merged.  */
    214  1.1     skrll 
    215  1.1     skrll static int
    216  1.1     skrll cie_eq (const void *e1, const void *e2)
    217  1.1     skrll {
    218  1.3  christos   const struct cie *c1 = (const struct cie *) e1;
    219  1.3  christos   const struct cie *c2 = (const struct cie *) e2;
    220  1.1     skrll 
    221  1.1     skrll   if (c1->hash == c2->hash
    222  1.1     skrll       && c1->length == c2->length
    223  1.1     skrll       && c1->version == c2->version
    224  1.1     skrll       && c1->local_personality == c2->local_personality
    225  1.1     skrll       && strcmp (c1->augmentation, c2->augmentation) == 0
    226  1.1     skrll       && strcmp (c1->augmentation, "eh") != 0
    227  1.1     skrll       && c1->code_align == c2->code_align
    228  1.1     skrll       && c1->data_align == c2->data_align
    229  1.1     skrll       && c1->ra_column == c2->ra_column
    230  1.1     skrll       && c1->augmentation_size == c2->augmentation_size
    231  1.1     skrll       && memcmp (&c1->personality, &c2->personality,
    232  1.1     skrll 		 sizeof (c1->personality)) == 0
    233  1.1     skrll       && c1->output_sec == c2->output_sec
    234  1.1     skrll       && c1->per_encoding == c2->per_encoding
    235  1.1     skrll       && c1->lsda_encoding == c2->lsda_encoding
    236  1.1     skrll       && c1->fde_encoding == c2->fde_encoding
    237  1.1     skrll       && c1->initial_insn_length == c2->initial_insn_length
    238  1.1     skrll       && memcmp (c1->initial_instructions,
    239  1.1     skrll 		 c2->initial_instructions,
    240  1.1     skrll 		 c1->initial_insn_length) == 0)
    241  1.1     skrll     return 1;
    242  1.1     skrll 
    243  1.1     skrll   return 0;
    244  1.1     skrll }
    245  1.1     skrll 
    246  1.1     skrll static hashval_t
    247  1.1     skrll cie_hash (const void *e)
    248  1.1     skrll {
    249  1.3  christos   const struct cie *c = (const struct cie *) e;
    250  1.1     skrll   return c->hash;
    251  1.1     skrll }
    252  1.1     skrll 
    253  1.1     skrll static hashval_t
    254  1.1     skrll cie_compute_hash (struct cie *c)
    255  1.1     skrll {
    256  1.1     skrll   hashval_t h = 0;
    257  1.1     skrll   h = iterative_hash_object (c->length, h);
    258  1.1     skrll   h = iterative_hash_object (c->version, h);
    259  1.1     skrll   h = iterative_hash (c->augmentation, strlen (c->augmentation) + 1, h);
    260  1.1     skrll   h = iterative_hash_object (c->code_align, h);
    261  1.1     skrll   h = iterative_hash_object (c->data_align, h);
    262  1.1     skrll   h = iterative_hash_object (c->ra_column, h);
    263  1.1     skrll   h = iterative_hash_object (c->augmentation_size, h);
    264  1.1     skrll   h = iterative_hash_object (c->personality, h);
    265  1.1     skrll   h = iterative_hash_object (c->output_sec, h);
    266  1.1     skrll   h = iterative_hash_object (c->per_encoding, h);
    267  1.1     skrll   h = iterative_hash_object (c->lsda_encoding, h);
    268  1.1     skrll   h = iterative_hash_object (c->fde_encoding, h);
    269  1.1     skrll   h = iterative_hash_object (c->initial_insn_length, h);
    270  1.1     skrll   h = iterative_hash (c->initial_instructions, c->initial_insn_length, h);
    271  1.1     skrll   c->hash = h;
    272  1.1     skrll   return h;
    273  1.1     skrll }
    274  1.1     skrll 
    275  1.1     skrll /* Return the number of extra bytes that we'll be inserting into
    276  1.1     skrll    ENTRY's augmentation string.  */
    277  1.1     skrll 
    278  1.1     skrll static INLINE unsigned int
    279  1.1     skrll extra_augmentation_string_bytes (struct eh_cie_fde *entry)
    280  1.1     skrll {
    281  1.1     skrll   unsigned int size = 0;
    282  1.1     skrll   if (entry->cie)
    283  1.1     skrll     {
    284  1.1     skrll       if (entry->add_augmentation_size)
    285  1.1     skrll 	size++;
    286  1.1     skrll       if (entry->u.cie.add_fde_encoding)
    287  1.1     skrll 	size++;
    288  1.1     skrll     }
    289  1.1     skrll   return size;
    290  1.1     skrll }
    291  1.1     skrll 
    292  1.1     skrll /* Likewise ENTRY's augmentation data.  */
    293  1.1     skrll 
    294  1.1     skrll static INLINE unsigned int
    295  1.1     skrll extra_augmentation_data_bytes (struct eh_cie_fde *entry)
    296  1.1     skrll {
    297  1.1     skrll   unsigned int size = 0;
    298  1.1     skrll   if (entry->add_augmentation_size)
    299  1.1     skrll     size++;
    300  1.1     skrll   if (entry->cie && entry->u.cie.add_fde_encoding)
    301  1.1     skrll     size++;
    302  1.1     skrll   return size;
    303  1.1     skrll }
    304  1.1     skrll 
    305  1.1     skrll /* Return the size that ENTRY will have in the output.  ALIGNMENT is the
    306  1.1     skrll    required alignment of ENTRY in bytes.  */
    307  1.1     skrll 
    308  1.1     skrll static unsigned int
    309  1.1     skrll size_of_output_cie_fde (struct eh_cie_fde *entry, unsigned int alignment)
    310  1.1     skrll {
    311  1.1     skrll   if (entry->removed)
    312  1.1     skrll     return 0;
    313  1.1     skrll   if (entry->size == 4)
    314  1.1     skrll     return 4;
    315  1.1     skrll   return (entry->size
    316  1.1     skrll 	  + extra_augmentation_string_bytes (entry)
    317  1.1     skrll 	  + extra_augmentation_data_bytes (entry)
    318  1.1     skrll 	  + alignment - 1) & -alignment;
    319  1.1     skrll }
    320  1.1     skrll 
    321  1.1     skrll /* Assume that the bytes between *ITER and END are CFA instructions.
    322  1.1     skrll    Try to move *ITER past the first instruction and return true on
    323  1.1     skrll    success.  ENCODED_PTR_WIDTH gives the width of pointer entries.  */
    324  1.1     skrll 
    325  1.1     skrll static bfd_boolean
    326  1.1     skrll skip_cfa_op (bfd_byte **iter, bfd_byte *end, unsigned int encoded_ptr_width)
    327  1.1     skrll {
    328  1.1     skrll   bfd_byte op;
    329  1.1     skrll   bfd_vma length;
    330  1.1     skrll 
    331  1.1     skrll   if (!read_byte (iter, end, &op))
    332  1.1     skrll     return FALSE;
    333  1.1     skrll 
    334  1.1     skrll   switch (op & 0xc0 ? op & 0xc0 : op)
    335  1.1     skrll     {
    336  1.1     skrll     case DW_CFA_nop:
    337  1.1     skrll     case DW_CFA_advance_loc:
    338  1.1     skrll     case DW_CFA_restore:
    339  1.1     skrll     case DW_CFA_remember_state:
    340  1.1     skrll     case DW_CFA_restore_state:
    341  1.1     skrll     case DW_CFA_GNU_window_save:
    342  1.1     skrll       /* No arguments.  */
    343  1.1     skrll       return TRUE;
    344  1.1     skrll 
    345  1.1     skrll     case DW_CFA_offset:
    346  1.1     skrll     case DW_CFA_restore_extended:
    347  1.1     skrll     case DW_CFA_undefined:
    348  1.1     skrll     case DW_CFA_same_value:
    349  1.1     skrll     case DW_CFA_def_cfa_register:
    350  1.1     skrll     case DW_CFA_def_cfa_offset:
    351  1.1     skrll     case DW_CFA_def_cfa_offset_sf:
    352  1.1     skrll     case DW_CFA_GNU_args_size:
    353  1.1     skrll       /* One leb128 argument.  */
    354  1.1     skrll       return skip_leb128 (iter, end);
    355  1.1     skrll 
    356  1.1     skrll     case DW_CFA_val_offset:
    357  1.1     skrll     case DW_CFA_val_offset_sf:
    358  1.1     skrll     case DW_CFA_offset_extended:
    359  1.1     skrll     case DW_CFA_register:
    360  1.1     skrll     case DW_CFA_def_cfa:
    361  1.1     skrll     case DW_CFA_offset_extended_sf:
    362  1.1     skrll     case DW_CFA_GNU_negative_offset_extended:
    363  1.1     skrll     case DW_CFA_def_cfa_sf:
    364  1.1     skrll       /* Two leb128 arguments.  */
    365  1.1     skrll       return (skip_leb128 (iter, end)
    366  1.1     skrll 	      && skip_leb128 (iter, end));
    367  1.1     skrll 
    368  1.1     skrll     case DW_CFA_def_cfa_expression:
    369  1.1     skrll       /* A variable-length argument.  */
    370  1.1     skrll       return (read_uleb128 (iter, end, &length)
    371  1.1     skrll 	      && skip_bytes (iter, end, length));
    372  1.1     skrll 
    373  1.1     skrll     case DW_CFA_expression:
    374  1.1     skrll     case DW_CFA_val_expression:
    375  1.1     skrll       /* A leb128 followed by a variable-length argument.  */
    376  1.1     skrll       return (skip_leb128 (iter, end)
    377  1.1     skrll 	      && read_uleb128 (iter, end, &length)
    378  1.1     skrll 	      && skip_bytes (iter, end, length));
    379  1.1     skrll 
    380  1.1     skrll     case DW_CFA_set_loc:
    381  1.1     skrll       return skip_bytes (iter, end, encoded_ptr_width);
    382  1.1     skrll 
    383  1.1     skrll     case DW_CFA_advance_loc1:
    384  1.1     skrll       return skip_bytes (iter, end, 1);
    385  1.1     skrll 
    386  1.1     skrll     case DW_CFA_advance_loc2:
    387  1.1     skrll       return skip_bytes (iter, end, 2);
    388  1.1     skrll 
    389  1.1     skrll     case DW_CFA_advance_loc4:
    390  1.1     skrll       return skip_bytes (iter, end, 4);
    391  1.1     skrll 
    392  1.1     skrll     case DW_CFA_MIPS_advance_loc8:
    393  1.1     skrll       return skip_bytes (iter, end, 8);
    394  1.1     skrll 
    395  1.1     skrll     default:
    396  1.1     skrll       return FALSE;
    397  1.1     skrll     }
    398  1.1     skrll }
    399  1.1     skrll 
    400  1.1     skrll /* Try to interpret the bytes between BUF and END as CFA instructions.
    401  1.1     skrll    If every byte makes sense, return a pointer to the first DW_CFA_nop
    402  1.1     skrll    padding byte, or END if there is no padding.  Return null otherwise.
    403  1.1     skrll    ENCODED_PTR_WIDTH is as for skip_cfa_op.  */
    404  1.1     skrll 
    405  1.1     skrll static bfd_byte *
    406  1.1     skrll skip_non_nops (bfd_byte *buf, bfd_byte *end, unsigned int encoded_ptr_width,
    407  1.1     skrll 	       unsigned int *set_loc_count)
    408  1.1     skrll {
    409  1.1     skrll   bfd_byte *last;
    410  1.1     skrll 
    411  1.1     skrll   last = buf;
    412  1.1     skrll   while (buf < end)
    413  1.1     skrll     if (*buf == DW_CFA_nop)
    414  1.1     skrll       buf++;
    415  1.1     skrll     else
    416  1.1     skrll       {
    417  1.1     skrll 	if (*buf == DW_CFA_set_loc)
    418  1.1     skrll 	  ++*set_loc_count;
    419  1.1     skrll 	if (!skip_cfa_op (&buf, end, encoded_ptr_width))
    420  1.1     skrll 	  return 0;
    421  1.1     skrll 	last = buf;
    422  1.1     skrll       }
    423  1.1     skrll   return last;
    424  1.1     skrll }
    425  1.1     skrll 
    426  1.3  christos /* Convert absolute encoding ENCODING into PC-relative form.
    427  1.3  christos    SIZE is the size of a pointer.  */
    428  1.3  christos 
    429  1.3  christos static unsigned char
    430  1.3  christos make_pc_relative (unsigned char encoding, unsigned int ptr_size)
    431  1.3  christos {
    432  1.3  christos   if ((encoding & 0x7f) == DW_EH_PE_absptr)
    433  1.3  christos     switch (ptr_size)
    434  1.3  christos       {
    435  1.3  christos       case 2:
    436  1.3  christos 	encoding |= DW_EH_PE_sdata2;
    437  1.3  christos 	break;
    438  1.3  christos       case 4:
    439  1.3  christos 	encoding |= DW_EH_PE_sdata4;
    440  1.3  christos 	break;
    441  1.3  christos       case 8:
    442  1.3  christos 	encoding |= DW_EH_PE_sdata8;
    443  1.3  christos 	break;
    444  1.3  christos       }
    445  1.3  christos   return encoding | DW_EH_PE_pcrel;
    446  1.3  christos }
    447  1.3  christos 
    448  1.1     skrll /* Called before calling _bfd_elf_parse_eh_frame on every input bfd's
    449  1.1     skrll    .eh_frame section.  */
    450  1.1     skrll 
    451  1.1     skrll void
    452  1.1     skrll _bfd_elf_begin_eh_frame_parsing (struct bfd_link_info *info)
    453  1.1     skrll {
    454  1.1     skrll   struct eh_frame_hdr_info *hdr_info;
    455  1.1     skrll 
    456  1.1     skrll   hdr_info = &elf_hash_table (info)->eh_info;
    457  1.1     skrll   hdr_info->merge_cies = !info->relocatable;
    458  1.1     skrll }
    459  1.1     skrll 
    460  1.1     skrll /* Try to parse .eh_frame section SEC, which belongs to ABFD.  Store the
    461  1.1     skrll    information in the section's sec_info field on success.  COOKIE
    462  1.1     skrll    describes the relocations in SEC.  */
    463  1.1     skrll 
    464  1.1     skrll void
    465  1.1     skrll _bfd_elf_parse_eh_frame (bfd *abfd, struct bfd_link_info *info,
    466  1.1     skrll 			 asection *sec, struct elf_reloc_cookie *cookie)
    467  1.1     skrll {
    468  1.1     skrll #define REQUIRE(COND)					\
    469  1.1     skrll   do							\
    470  1.1     skrll     if (!(COND))					\
    471  1.1     skrll       goto free_no_table;				\
    472  1.1     skrll   while (0)
    473  1.1     skrll 
    474  1.1     skrll   bfd_byte *ehbuf = NULL, *buf, *end;
    475  1.1     skrll   bfd_byte *last_fde;
    476  1.1     skrll   struct eh_cie_fde *this_inf;
    477  1.1     skrll   unsigned int hdr_length, hdr_id;
    478  1.1     skrll   unsigned int cie_count;
    479  1.1     skrll   struct cie *cie, *local_cies = NULL;
    480  1.1     skrll   struct elf_link_hash_table *htab;
    481  1.1     skrll   struct eh_frame_hdr_info *hdr_info;
    482  1.1     skrll   struct eh_frame_sec_info *sec_info = NULL;
    483  1.1     skrll   unsigned int ptr_size;
    484  1.1     skrll   unsigned int num_cies;
    485  1.1     skrll   unsigned int num_entries;
    486  1.1     skrll   elf_gc_mark_hook_fn gc_mark_hook;
    487  1.1     skrll 
    488  1.1     skrll   htab = elf_hash_table (info);
    489  1.1     skrll   hdr_info = &htab->eh_info;
    490  1.1     skrll   if (hdr_info->parsed_eh_frames)
    491  1.1     skrll     return;
    492  1.1     skrll 
    493  1.1     skrll   if (sec->size == 0)
    494  1.1     skrll     {
    495  1.1     skrll       /* This file does not contain .eh_frame information.  */
    496  1.1     skrll       return;
    497  1.1     skrll     }
    498  1.1     skrll 
    499  1.1     skrll   if (bfd_is_abs_section (sec->output_section))
    500  1.1     skrll     {
    501  1.1     skrll       /* At least one of the sections is being discarded from the
    502  1.1     skrll 	 link, so we should just ignore them.  */
    503  1.1     skrll       return;
    504  1.1     skrll     }
    505  1.1     skrll 
    506  1.1     skrll   /* Read the frame unwind information from abfd.  */
    507  1.1     skrll 
    508  1.1     skrll   REQUIRE (bfd_malloc_and_get_section (abfd, sec, &ehbuf));
    509  1.1     skrll 
    510  1.1     skrll   if (sec->size >= 4
    511  1.1     skrll       && bfd_get_32 (abfd, ehbuf) == 0
    512  1.1     skrll       && cookie->rel == cookie->relend)
    513  1.1     skrll     {
    514  1.1     skrll       /* Empty .eh_frame section.  */
    515  1.1     skrll       free (ehbuf);
    516  1.1     skrll       return;
    517  1.1     skrll     }
    518  1.1     skrll 
    519  1.1     skrll   /* If .eh_frame section size doesn't fit into int, we cannot handle
    520  1.1     skrll      it (it would need to use 64-bit .eh_frame format anyway).  */
    521  1.1     skrll   REQUIRE (sec->size == (unsigned int) sec->size);
    522  1.1     skrll 
    523  1.1     skrll   ptr_size = (get_elf_backend_data (abfd)
    524  1.1     skrll 	      ->elf_backend_eh_frame_address_size (abfd, sec));
    525  1.1     skrll   REQUIRE (ptr_size != 0);
    526  1.1     skrll 
    527  1.1     skrll   /* Go through the section contents and work out how many FDEs and
    528  1.1     skrll      CIEs there are.  */
    529  1.1     skrll   buf = ehbuf;
    530  1.1     skrll   end = ehbuf + sec->size;
    531  1.1     skrll   num_cies = 0;
    532  1.1     skrll   num_entries = 0;
    533  1.1     skrll   while (buf != end)
    534  1.1     skrll     {
    535  1.1     skrll       num_entries++;
    536  1.1     skrll 
    537  1.1     skrll       /* Read the length of the entry.  */
    538  1.1     skrll       REQUIRE (skip_bytes (&buf, end, 4));
    539  1.1     skrll       hdr_length = bfd_get_32 (abfd, buf - 4);
    540  1.1     skrll 
    541  1.1     skrll       /* 64-bit .eh_frame is not supported.  */
    542  1.1     skrll       REQUIRE (hdr_length != 0xffffffff);
    543  1.1     skrll       if (hdr_length == 0)
    544  1.1     skrll 	break;
    545  1.1     skrll 
    546  1.1     skrll       REQUIRE (skip_bytes (&buf, end, 4));
    547  1.1     skrll       hdr_id = bfd_get_32 (abfd, buf - 4);
    548  1.1     skrll       if (hdr_id == 0)
    549  1.1     skrll 	num_cies++;
    550  1.1     skrll 
    551  1.1     skrll       REQUIRE (skip_bytes (&buf, end, hdr_length - 4));
    552  1.1     skrll     }
    553  1.1     skrll 
    554  1.3  christos   sec_info = (struct eh_frame_sec_info *)
    555  1.3  christos       bfd_zmalloc (sizeof (struct eh_frame_sec_info)
    556  1.3  christos                    + (num_entries - 1) * sizeof (struct eh_cie_fde));
    557  1.1     skrll   REQUIRE (sec_info);
    558  1.1     skrll 
    559  1.1     skrll   /* We need to have a "struct cie" for each CIE in this section.  */
    560  1.3  christos   local_cies = (struct cie *) bfd_zmalloc (num_cies * sizeof (*local_cies));
    561  1.1     skrll   REQUIRE (local_cies);
    562  1.1     skrll 
    563  1.3  christos   /* FIXME: octets_per_byte.  */
    564  1.1     skrll #define ENSURE_NO_RELOCS(buf)				\
    565  1.1     skrll   REQUIRE (!(cookie->rel < cookie->relend		\
    566  1.1     skrll 	     && (cookie->rel->r_offset			\
    567  1.1     skrll 		 < (bfd_size_type) ((buf) - ehbuf))	\
    568  1.1     skrll 	     && cookie->rel->r_info != 0))
    569  1.1     skrll 
    570  1.3  christos   /* FIXME: octets_per_byte.  */
    571  1.1     skrll #define SKIP_RELOCS(buf)				\
    572  1.1     skrll   while (cookie->rel < cookie->relend			\
    573  1.1     skrll 	 && (cookie->rel->r_offset			\
    574  1.1     skrll 	     < (bfd_size_type) ((buf) - ehbuf)))	\
    575  1.1     skrll     cookie->rel++
    576  1.1     skrll 
    577  1.3  christos   /* FIXME: octets_per_byte.  */
    578  1.1     skrll #define GET_RELOC(buf)					\
    579  1.1     skrll   ((cookie->rel < cookie->relend			\
    580  1.1     skrll     && (cookie->rel->r_offset				\
    581  1.1     skrll 	== (bfd_size_type) ((buf) - ehbuf)))		\
    582  1.1     skrll    ? cookie->rel : NULL)
    583  1.1     skrll 
    584  1.1     skrll   buf = ehbuf;
    585  1.1     skrll   cie_count = 0;
    586  1.1     skrll   gc_mark_hook = get_elf_backend_data (abfd)->gc_mark_hook;
    587  1.1     skrll   while ((bfd_size_type) (buf - ehbuf) != sec->size)
    588  1.1     skrll     {
    589  1.1     skrll       char *aug;
    590  1.1     skrll       bfd_byte *start, *insns, *insns_end;
    591  1.1     skrll       bfd_size_type length;
    592  1.1     skrll       unsigned int set_loc_count;
    593  1.1     skrll 
    594  1.1     skrll       this_inf = sec_info->entry + sec_info->count;
    595  1.1     skrll       last_fde = buf;
    596  1.1     skrll 
    597  1.1     skrll       /* Read the length of the entry.  */
    598  1.1     skrll       REQUIRE (skip_bytes (&buf, ehbuf + sec->size, 4));
    599  1.1     skrll       hdr_length = bfd_get_32 (abfd, buf - 4);
    600  1.1     skrll 
    601  1.1     skrll       /* The CIE/FDE must be fully contained in this input section.  */
    602  1.1     skrll       REQUIRE ((bfd_size_type) (buf - ehbuf) + hdr_length <= sec->size);
    603  1.1     skrll       end = buf + hdr_length;
    604  1.1     skrll 
    605  1.1     skrll       this_inf->offset = last_fde - ehbuf;
    606  1.1     skrll       this_inf->size = 4 + hdr_length;
    607  1.1     skrll       this_inf->reloc_index = cookie->rel - cookie->rels;
    608  1.1     skrll 
    609  1.1     skrll       if (hdr_length == 0)
    610  1.1     skrll 	{
    611  1.1     skrll 	  /* A zero-length CIE should only be found at the end of
    612  1.1     skrll 	     the section.  */
    613  1.1     skrll 	  REQUIRE ((bfd_size_type) (buf - ehbuf) == sec->size);
    614  1.1     skrll 	  ENSURE_NO_RELOCS (buf);
    615  1.1     skrll 	  sec_info->count++;
    616  1.1     skrll 	  break;
    617  1.1     skrll 	}
    618  1.1     skrll 
    619  1.1     skrll       REQUIRE (skip_bytes (&buf, end, 4));
    620  1.1     skrll       hdr_id = bfd_get_32 (abfd, buf - 4);
    621  1.1     skrll 
    622  1.1     skrll       if (hdr_id == 0)
    623  1.1     skrll 	{
    624  1.1     skrll 	  unsigned int initial_insn_length;
    625  1.1     skrll 
    626  1.1     skrll 	  /* CIE  */
    627  1.1     skrll 	  this_inf->cie = 1;
    628  1.1     skrll 
    629  1.1     skrll 	  /* Point CIE to one of the section-local cie structures.  */
    630  1.1     skrll 	  cie = local_cies + cie_count++;
    631  1.1     skrll 
    632  1.1     skrll 	  cie->cie_inf = this_inf;
    633  1.1     skrll 	  cie->length = hdr_length;
    634  1.1     skrll 	  cie->output_sec = sec->output_section;
    635  1.1     skrll 	  start = buf;
    636  1.1     skrll 	  REQUIRE (read_byte (&buf, end, &cie->version));
    637  1.1     skrll 
    638  1.1     skrll 	  /* Cannot handle unknown versions.  */
    639  1.3  christos 	  REQUIRE (cie->version == 1
    640  1.3  christos 		   || cie->version == 3
    641  1.3  christos 		   || cie->version == 4);
    642  1.1     skrll 	  REQUIRE (strlen ((char *) buf) < sizeof (cie->augmentation));
    643  1.1     skrll 
    644  1.1     skrll 	  strcpy (cie->augmentation, (char *) buf);
    645  1.1     skrll 	  buf = (bfd_byte *) strchr ((char *) buf, '\0') + 1;
    646  1.1     skrll 	  ENSURE_NO_RELOCS (buf);
    647  1.1     skrll 	  if (buf[0] == 'e' && buf[1] == 'h')
    648  1.1     skrll 	    {
    649  1.1     skrll 	      /* GCC < 3.0 .eh_frame CIE */
    650  1.1     skrll 	      /* We cannot merge "eh" CIEs because __EXCEPTION_TABLE__
    651  1.1     skrll 		 is private to each CIE, so we don't need it for anything.
    652  1.1     skrll 		 Just skip it.  */
    653  1.1     skrll 	      REQUIRE (skip_bytes (&buf, end, ptr_size));
    654  1.1     skrll 	      SKIP_RELOCS (buf);
    655  1.1     skrll 	    }
    656  1.3  christos 	  if (cie->version >= 4)
    657  1.3  christos 	    {
    658  1.3  christos 	      REQUIRE (buf + 1 < end);
    659  1.3  christos 	      REQUIRE (buf[0] == ptr_size);
    660  1.3  christos 	      REQUIRE (buf[1] == 0);
    661  1.3  christos 	      buf += 2;
    662  1.3  christos 	    }
    663  1.1     skrll 	  REQUIRE (read_uleb128 (&buf, end, &cie->code_align));
    664  1.1     skrll 	  REQUIRE (read_sleb128 (&buf, end, &cie->data_align));
    665  1.1     skrll 	  if (cie->version == 1)
    666  1.1     skrll 	    {
    667  1.1     skrll 	      REQUIRE (buf < end);
    668  1.1     skrll 	      cie->ra_column = *buf++;
    669  1.1     skrll 	    }
    670  1.1     skrll 	  else
    671  1.1     skrll 	    REQUIRE (read_uleb128 (&buf, end, &cie->ra_column));
    672  1.1     skrll 	  ENSURE_NO_RELOCS (buf);
    673  1.1     skrll 	  cie->lsda_encoding = DW_EH_PE_omit;
    674  1.1     skrll 	  cie->fde_encoding = DW_EH_PE_omit;
    675  1.1     skrll 	  cie->per_encoding = DW_EH_PE_omit;
    676  1.1     skrll 	  aug = cie->augmentation;
    677  1.1     skrll 	  if (aug[0] != 'e' || aug[1] != 'h')
    678  1.1     skrll 	    {
    679  1.1     skrll 	      if (*aug == 'z')
    680  1.1     skrll 		{
    681  1.1     skrll 		  aug++;
    682  1.1     skrll 		  REQUIRE (read_uleb128 (&buf, end, &cie->augmentation_size));
    683  1.1     skrll 	  	  ENSURE_NO_RELOCS (buf);
    684  1.1     skrll 		}
    685  1.1     skrll 
    686  1.1     skrll 	      while (*aug != '\0')
    687  1.1     skrll 		switch (*aug++)
    688  1.1     skrll 		  {
    689  1.1     skrll 		  case 'L':
    690  1.1     skrll 		    REQUIRE (read_byte (&buf, end, &cie->lsda_encoding));
    691  1.1     skrll 		    ENSURE_NO_RELOCS (buf);
    692  1.1     skrll 		    REQUIRE (get_DW_EH_PE_width (cie->lsda_encoding, ptr_size));
    693  1.1     skrll 		    break;
    694  1.1     skrll 		  case 'R':
    695  1.1     skrll 		    REQUIRE (read_byte (&buf, end, &cie->fde_encoding));
    696  1.1     skrll 		    ENSURE_NO_RELOCS (buf);
    697  1.1     skrll 		    REQUIRE (get_DW_EH_PE_width (cie->fde_encoding, ptr_size));
    698  1.1     skrll 		    break;
    699  1.1     skrll 		  case 'S':
    700  1.1     skrll 		    break;
    701  1.1     skrll 		  case 'P':
    702  1.1     skrll 		    {
    703  1.1     skrll 		      int per_width;
    704  1.1     skrll 
    705  1.1     skrll 		      REQUIRE (read_byte (&buf, end, &cie->per_encoding));
    706  1.1     skrll 		      per_width = get_DW_EH_PE_width (cie->per_encoding,
    707  1.1     skrll 						      ptr_size);
    708  1.1     skrll 		      REQUIRE (per_width);
    709  1.3  christos 		      if ((cie->per_encoding & 0x70) == DW_EH_PE_aligned)
    710  1.1     skrll 			{
    711  1.1     skrll 			  length = -(buf - ehbuf) & (per_width - 1);
    712  1.1     skrll 			  REQUIRE (skip_bytes (&buf, end, length));
    713  1.1     skrll 			}
    714  1.3  christos 		      this_inf->u.cie.personality_offset = buf - start;
    715  1.1     skrll 		      ENSURE_NO_RELOCS (buf);
    716  1.1     skrll 		      /* Ensure we have a reloc here.  */
    717  1.1     skrll 		      REQUIRE (GET_RELOC (buf));
    718  1.1     skrll 		      cie->personality.reloc_index
    719  1.1     skrll 			= cookie->rel - cookie->rels;
    720  1.1     skrll 		      /* Cope with MIPS-style composite relocations.  */
    721  1.1     skrll 		      do
    722  1.1     skrll 			cookie->rel++;
    723  1.1     skrll 		      while (GET_RELOC (buf) != NULL);
    724  1.1     skrll 		      REQUIRE (skip_bytes (&buf, end, per_width));
    725  1.1     skrll 		    }
    726  1.1     skrll 		    break;
    727  1.1     skrll 		  default:
    728  1.1     skrll 		    /* Unrecognized augmentation. Better bail out.  */
    729  1.1     skrll 		    goto free_no_table;
    730  1.1     skrll 		  }
    731  1.1     skrll 	    }
    732  1.1     skrll 
    733  1.1     skrll 	  /* For shared libraries, try to get rid of as many RELATIVE relocs
    734  1.1     skrll 	     as possible.  */
    735  1.1     skrll 	  if (info->shared
    736  1.1     skrll 	      && (get_elf_backend_data (abfd)
    737  1.1     skrll 		  ->elf_backend_can_make_relative_eh_frame
    738  1.1     skrll 		  (abfd, info, sec)))
    739  1.1     skrll 	    {
    740  1.3  christos 	      if ((cie->fde_encoding & 0x70) == DW_EH_PE_absptr)
    741  1.1     skrll 		this_inf->make_relative = 1;
    742  1.1     skrll 	      /* If the CIE doesn't already have an 'R' entry, it's fairly
    743  1.1     skrll 		 easy to add one, provided that there's no aligned data
    744  1.1     skrll 		 after the augmentation string.  */
    745  1.1     skrll 	      else if (cie->fde_encoding == DW_EH_PE_omit
    746  1.3  christos 		       && (cie->per_encoding & 0x70) != DW_EH_PE_aligned)
    747  1.1     skrll 		{
    748  1.1     skrll 		  if (*cie->augmentation == 0)
    749  1.1     skrll 		    this_inf->add_augmentation_size = 1;
    750  1.1     skrll 		  this_inf->u.cie.add_fde_encoding = 1;
    751  1.1     skrll 		  this_inf->make_relative = 1;
    752  1.1     skrll 		}
    753  1.3  christos 
    754  1.3  christos 	      if ((cie->lsda_encoding & 0x70) == DW_EH_PE_absptr)
    755  1.3  christos 		cie->can_make_lsda_relative = 1;
    756  1.1     skrll 	    }
    757  1.1     skrll 
    758  1.1     skrll 	  /* If FDE encoding was not specified, it defaults to
    759  1.1     skrll 	     DW_EH_absptr.  */
    760  1.1     skrll 	  if (cie->fde_encoding == DW_EH_PE_omit)
    761  1.1     skrll 	    cie->fde_encoding = DW_EH_PE_absptr;
    762  1.1     skrll 
    763  1.1     skrll 	  initial_insn_length = end - buf;
    764  1.1     skrll 	  if (initial_insn_length <= sizeof (cie->initial_instructions))
    765  1.1     skrll 	    {
    766  1.1     skrll 	      cie->initial_insn_length = initial_insn_length;
    767  1.1     skrll 	      memcpy (cie->initial_instructions, buf, initial_insn_length);
    768  1.1     skrll 	    }
    769  1.1     skrll 	  insns = buf;
    770  1.1     skrll 	  buf += initial_insn_length;
    771  1.1     skrll 	  ENSURE_NO_RELOCS (buf);
    772  1.1     skrll 
    773  1.1     skrll 	  if (hdr_info->merge_cies)
    774  1.1     skrll 	    this_inf->u.cie.u.full_cie = cie;
    775  1.1     skrll 	  this_inf->u.cie.per_encoding_relative
    776  1.1     skrll 	    = (cie->per_encoding & 0x70) == DW_EH_PE_pcrel;
    777  1.1     skrll 	}
    778  1.1     skrll       else
    779  1.1     skrll 	{
    780  1.1     skrll 	  asection *rsec;
    781  1.1     skrll 
    782  1.1     skrll 	  /* Find the corresponding CIE.  */
    783  1.1     skrll 	  unsigned int cie_offset = this_inf->offset + 4 - hdr_id;
    784  1.1     skrll 	  for (cie = local_cies; cie < local_cies + cie_count; cie++)
    785  1.1     skrll 	    if (cie_offset == cie->cie_inf->offset)
    786  1.1     skrll 	      break;
    787  1.1     skrll 
    788  1.1     skrll 	  /* Ensure this FDE references one of the CIEs in this input
    789  1.1     skrll 	     section.  */
    790  1.1     skrll 	  REQUIRE (cie != local_cies + cie_count);
    791  1.1     skrll 	  this_inf->u.fde.cie_inf = cie->cie_inf;
    792  1.1     skrll 	  this_inf->make_relative = cie->cie_inf->make_relative;
    793  1.1     skrll 	  this_inf->add_augmentation_size
    794  1.1     skrll 	    = cie->cie_inf->add_augmentation_size;
    795  1.1     skrll 
    796  1.1     skrll 	  ENSURE_NO_RELOCS (buf);
    797  1.1     skrll 	  REQUIRE (GET_RELOC (buf));
    798  1.1     skrll 
    799  1.1     skrll 	  /* Chain together the FDEs for each section.  */
    800  1.1     skrll 	  rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie);
    801  1.1     skrll 	  /* RSEC will be NULL if FDE was cleared out as it was belonging to
    802  1.1     skrll 	     a discarded SHT_GROUP.  */
    803  1.1     skrll 	  if (rsec)
    804  1.1     skrll 	    {
    805  1.1     skrll 	      REQUIRE (rsec->owner == abfd);
    806  1.1     skrll 	      this_inf->u.fde.next_for_section = elf_fde_list (rsec);
    807  1.1     skrll 	      elf_fde_list (rsec) = this_inf;
    808  1.1     skrll 	    }
    809  1.1     skrll 
    810  1.1     skrll 	  /* Skip the initial location and address range.  */
    811  1.1     skrll 	  start = buf;
    812  1.1     skrll 	  length = get_DW_EH_PE_width (cie->fde_encoding, ptr_size);
    813  1.1     skrll 	  REQUIRE (skip_bytes (&buf, end, 2 * length));
    814  1.1     skrll 
    815  1.1     skrll 	  /* Skip the augmentation size, if present.  */
    816  1.1     skrll 	  if (cie->augmentation[0] == 'z')
    817  1.1     skrll 	    REQUIRE (read_uleb128 (&buf, end, &length));
    818  1.1     skrll 	  else
    819  1.1     skrll 	    length = 0;
    820  1.1     skrll 
    821  1.1     skrll 	  /* Of the supported augmentation characters above, only 'L'
    822  1.1     skrll 	     adds augmentation data to the FDE.  This code would need to
    823  1.1     skrll 	     be adjusted if any future augmentations do the same thing.  */
    824  1.1     skrll 	  if (cie->lsda_encoding != DW_EH_PE_omit)
    825  1.1     skrll 	    {
    826  1.1     skrll 	      SKIP_RELOCS (buf);
    827  1.1     skrll 	      if (cie->can_make_lsda_relative && GET_RELOC (buf))
    828  1.1     skrll 		cie->cie_inf->u.cie.make_lsda_relative = 1;
    829  1.1     skrll 	      this_inf->lsda_offset = buf - start;
    830  1.1     skrll 	      /* If there's no 'z' augmentation, we don't know where the
    831  1.1     skrll 		 CFA insns begin.  Assume no padding.  */
    832  1.1     skrll 	      if (cie->augmentation[0] != 'z')
    833  1.1     skrll 		length = end - buf;
    834  1.1     skrll 	    }
    835  1.1     skrll 
    836  1.1     skrll 	  /* Skip over the augmentation data.  */
    837  1.1     skrll 	  REQUIRE (skip_bytes (&buf, end, length));
    838  1.1     skrll 	  insns = buf;
    839  1.1     skrll 
    840  1.1     skrll 	  buf = last_fde + 4 + hdr_length;
    841  1.1     skrll 
    842  1.2     skrll 	  /* For NULL RSEC (cleared FDE belonging to a discarded section)
    843  1.2     skrll 	     the relocations are commonly cleared.  We do not sanity check if
    844  1.2     skrll 	     all these relocations are cleared as (1) relocations to
    845  1.2     skrll 	     .gcc_except_table will remain uncleared (they will get dropped
    846  1.2     skrll 	     with the drop of this unused FDE) and (2) BFD already safely drops
    847  1.2     skrll 	     relocations of any type to .eh_frame by
    848  1.2     skrll 	     elf_section_ignore_discarded_relocs.
    849  1.2     skrll 	     TODO: The .gcc_except_table entries should be also filtered as
    850  1.2     skrll 	     .eh_frame entries; or GCC could rather use COMDAT for them.  */
    851  1.2     skrll 	  SKIP_RELOCS (buf);
    852  1.1     skrll 	}
    853  1.1     skrll 
    854  1.1     skrll       /* Try to interpret the CFA instructions and find the first
    855  1.1     skrll 	 padding nop.  Shrink this_inf's size so that it doesn't
    856  1.1     skrll 	 include the padding.  */
    857  1.1     skrll       length = get_DW_EH_PE_width (cie->fde_encoding, ptr_size);
    858  1.1     skrll       set_loc_count = 0;
    859  1.1     skrll       insns_end = skip_non_nops (insns, end, length, &set_loc_count);
    860  1.1     skrll       /* If we don't understand the CFA instructions, we can't know
    861  1.1     skrll 	 what needs to be adjusted there.  */
    862  1.1     skrll       if (insns_end == NULL
    863  1.1     skrll 	  /* For the time being we don't support DW_CFA_set_loc in
    864  1.1     skrll 	     CIE instructions.  */
    865  1.1     skrll 	  || (set_loc_count && this_inf->cie))
    866  1.1     skrll 	goto free_no_table;
    867  1.1     skrll       this_inf->size -= end - insns_end;
    868  1.1     skrll       if (insns_end != end && this_inf->cie)
    869  1.1     skrll 	{
    870  1.1     skrll 	  cie->initial_insn_length -= end - insns_end;
    871  1.1     skrll 	  cie->length -= end - insns_end;
    872  1.1     skrll 	}
    873  1.1     skrll       if (set_loc_count
    874  1.3  christos 	  && ((cie->fde_encoding & 0x70) == DW_EH_PE_pcrel
    875  1.1     skrll 	      || this_inf->make_relative))
    876  1.1     skrll 	{
    877  1.1     skrll 	  unsigned int cnt;
    878  1.1     skrll 	  bfd_byte *p;
    879  1.1     skrll 
    880  1.3  christos 	  this_inf->set_loc = (unsigned int *)
    881  1.3  christos               bfd_malloc ((set_loc_count + 1) * sizeof (unsigned int));
    882  1.1     skrll 	  REQUIRE (this_inf->set_loc);
    883  1.1     skrll 	  this_inf->set_loc[0] = set_loc_count;
    884  1.1     skrll 	  p = insns;
    885  1.1     skrll 	  cnt = 0;
    886  1.1     skrll 	  while (p < end)
    887  1.1     skrll 	    {
    888  1.1     skrll 	      if (*p == DW_CFA_set_loc)
    889  1.1     skrll 		this_inf->set_loc[++cnt] = p + 1 - start;
    890  1.1     skrll 	      REQUIRE (skip_cfa_op (&p, end, length));
    891  1.1     skrll 	    }
    892  1.1     skrll 	}
    893  1.1     skrll 
    894  1.1     skrll       this_inf->removed = 1;
    895  1.1     skrll       this_inf->fde_encoding = cie->fde_encoding;
    896  1.1     skrll       this_inf->lsda_encoding = cie->lsda_encoding;
    897  1.1     skrll       sec_info->count++;
    898  1.1     skrll     }
    899  1.1     skrll   BFD_ASSERT (sec_info->count == num_entries);
    900  1.1     skrll   BFD_ASSERT (cie_count == num_cies);
    901  1.1     skrll 
    902  1.1     skrll   elf_section_data (sec)->sec_info = sec_info;
    903  1.1     skrll   sec->sec_info_type = ELF_INFO_TYPE_EH_FRAME;
    904  1.1     skrll   if (hdr_info->merge_cies)
    905  1.1     skrll     {
    906  1.1     skrll       sec_info->cies = local_cies;
    907  1.1     skrll       local_cies = NULL;
    908  1.1     skrll     }
    909  1.1     skrll   goto success;
    910  1.1     skrll 
    911  1.1     skrll  free_no_table:
    912  1.1     skrll   (*info->callbacks->einfo)
    913  1.1     skrll     (_("%P: error in %B(%A); no .eh_frame_hdr table will be created.\n"),
    914  1.1     skrll      abfd, sec);
    915  1.1     skrll   hdr_info->table = FALSE;
    916  1.1     skrll   if (sec_info)
    917  1.1     skrll     free (sec_info);
    918  1.1     skrll  success:
    919  1.1     skrll   if (ehbuf)
    920  1.1     skrll     free (ehbuf);
    921  1.1     skrll   if (local_cies)
    922  1.1     skrll     free (local_cies);
    923  1.1     skrll #undef REQUIRE
    924  1.1     skrll }
    925  1.1     skrll 
    926  1.1     skrll /* Finish a pass over all .eh_frame sections.  */
    927  1.1     skrll 
    928  1.1     skrll void
    929  1.1     skrll _bfd_elf_end_eh_frame_parsing (struct bfd_link_info *info)
    930  1.1     skrll {
    931  1.1     skrll   struct eh_frame_hdr_info *hdr_info;
    932  1.1     skrll 
    933  1.1     skrll   hdr_info = &elf_hash_table (info)->eh_info;
    934  1.1     skrll   hdr_info->parsed_eh_frames = TRUE;
    935  1.1     skrll }
    936  1.1     skrll 
    937  1.1     skrll /* Mark all relocations against CIE or FDE ENT, which occurs in
    938  1.1     skrll    .eh_frame section SEC.  COOKIE describes the relocations in SEC;
    939  1.1     skrll    its "rel" field can be changed freely.  */
    940  1.1     skrll 
    941  1.1     skrll static bfd_boolean
    942  1.1     skrll mark_entry (struct bfd_link_info *info, asection *sec,
    943  1.1     skrll 	    struct eh_cie_fde *ent, elf_gc_mark_hook_fn gc_mark_hook,
    944  1.1     skrll 	    struct elf_reloc_cookie *cookie)
    945  1.1     skrll {
    946  1.3  christos   /* FIXME: octets_per_byte.  */
    947  1.1     skrll   for (cookie->rel = cookie->rels + ent->reloc_index;
    948  1.1     skrll        cookie->rel < cookie->relend
    949  1.1     skrll 	 && cookie->rel->r_offset < ent->offset + ent->size;
    950  1.1     skrll        cookie->rel++)
    951  1.1     skrll     if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, cookie))
    952  1.1     skrll       return FALSE;
    953  1.1     skrll 
    954  1.1     skrll   return TRUE;
    955  1.1     skrll }
    956  1.1     skrll 
    957  1.1     skrll /* Mark all the relocations against FDEs that relate to code in input
    958  1.1     skrll    section SEC.  The FDEs belong to .eh_frame section EH_FRAME, whose
    959  1.1     skrll    relocations are described by COOKIE.  */
    960  1.1     skrll 
    961  1.1     skrll bfd_boolean
    962  1.1     skrll _bfd_elf_gc_mark_fdes (struct bfd_link_info *info, asection *sec,
    963  1.1     skrll 		       asection *eh_frame, elf_gc_mark_hook_fn gc_mark_hook,
    964  1.1     skrll 		       struct elf_reloc_cookie *cookie)
    965  1.1     skrll {
    966  1.1     skrll   struct eh_cie_fde *fde, *cie;
    967  1.1     skrll 
    968  1.1     skrll   for (fde = elf_fde_list (sec); fde; fde = fde->u.fde.next_for_section)
    969  1.1     skrll     {
    970  1.1     skrll       if (!mark_entry (info, eh_frame, fde, gc_mark_hook, cookie))
    971  1.1     skrll 	return FALSE;
    972  1.1     skrll 
    973  1.1     skrll       /* At this stage, all cie_inf fields point to local CIEs, so we
    974  1.1     skrll 	 can use the same cookie to refer to them.  */
    975  1.1     skrll       cie = fde->u.fde.cie_inf;
    976  1.1     skrll       if (!cie->u.cie.gc_mark)
    977  1.1     skrll 	{
    978  1.1     skrll 	  cie->u.cie.gc_mark = 1;
    979  1.1     skrll 	  if (!mark_entry (info, eh_frame, cie, gc_mark_hook, cookie))
    980  1.1     skrll 	    return FALSE;
    981  1.1     skrll 	}
    982  1.1     skrll     }
    983  1.1     skrll   return TRUE;
    984  1.1     skrll }
    985  1.1     skrll 
    986  1.1     skrll /* Input section SEC of ABFD is an .eh_frame section that contains the
    987  1.1     skrll    CIE described by CIE_INF.  Return a version of CIE_INF that is going
    988  1.1     skrll    to be kept in the output, adding CIE_INF to the output if necessary.
    989  1.1     skrll 
    990  1.1     skrll    HDR_INFO is the .eh_frame_hdr information and COOKIE describes the
    991  1.1     skrll    relocations in REL.  */
    992  1.1     skrll 
    993  1.1     skrll static struct eh_cie_fde *
    994  1.3  christos find_merged_cie (bfd *abfd, struct bfd_link_info *info, asection *sec,
    995  1.1     skrll 		 struct eh_frame_hdr_info *hdr_info,
    996  1.1     skrll 		 struct elf_reloc_cookie *cookie,
    997  1.1     skrll 		 struct eh_cie_fde *cie_inf)
    998  1.1     skrll {
    999  1.1     skrll   unsigned long r_symndx;
   1000  1.1     skrll   struct cie *cie, *new_cie;
   1001  1.1     skrll   Elf_Internal_Rela *rel;
   1002  1.1     skrll   void **loc;
   1003  1.1     skrll 
   1004  1.1     skrll   /* Use CIE_INF if we have already decided to keep it.  */
   1005  1.1     skrll   if (!cie_inf->removed)
   1006  1.1     skrll     return cie_inf;
   1007  1.1     skrll 
   1008  1.1     skrll   /* If we have merged CIE_INF with another CIE, use that CIE instead.  */
   1009  1.1     skrll   if (cie_inf->u.cie.merged)
   1010  1.1     skrll     return cie_inf->u.cie.u.merged_with;
   1011  1.1     skrll 
   1012  1.1     skrll   cie = cie_inf->u.cie.u.full_cie;
   1013  1.1     skrll 
   1014  1.1     skrll   /* Assume we will need to keep CIE_INF.  */
   1015  1.1     skrll   cie_inf->removed = 0;
   1016  1.1     skrll   cie_inf->u.cie.u.sec = sec;
   1017  1.1     skrll 
   1018  1.1     skrll   /* If we are not merging CIEs, use CIE_INF.  */
   1019  1.1     skrll   if (cie == NULL)
   1020  1.1     skrll     return cie_inf;
   1021  1.1     skrll 
   1022  1.1     skrll   if (cie->per_encoding != DW_EH_PE_omit)
   1023  1.1     skrll     {
   1024  1.3  christos       bfd_boolean per_binds_local;
   1025  1.3  christos 
   1026  1.1     skrll       /* Work out the address of personality routine, either as an absolute
   1027  1.1     skrll 	 value or as a symbol.  */
   1028  1.1     skrll       rel = cookie->rels + cie->personality.reloc_index;
   1029  1.1     skrll       memset (&cie->personality, 0, sizeof (cie->personality));
   1030  1.1     skrll #ifdef BFD64
   1031  1.1     skrll       if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
   1032  1.1     skrll 	r_symndx = ELF64_R_SYM (rel->r_info);
   1033  1.1     skrll       else
   1034  1.1     skrll #endif
   1035  1.1     skrll 	r_symndx = ELF32_R_SYM (rel->r_info);
   1036  1.1     skrll       if (r_symndx >= cookie->locsymcount
   1037  1.1     skrll 	  || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
   1038  1.1     skrll 	{
   1039  1.1     skrll 	  struct elf_link_hash_entry *h;
   1040  1.1     skrll 
   1041  1.1     skrll 	  r_symndx -= cookie->extsymoff;
   1042  1.1     skrll 	  h = cookie->sym_hashes[r_symndx];
   1043  1.1     skrll 
   1044  1.1     skrll 	  while (h->root.type == bfd_link_hash_indirect
   1045  1.1     skrll 		 || h->root.type == bfd_link_hash_warning)
   1046  1.1     skrll 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
   1047  1.1     skrll 
   1048  1.1     skrll 	  cie->personality.h = h;
   1049  1.3  christos 	  per_binds_local = SYMBOL_REFERENCES_LOCAL (info, h);
   1050  1.1     skrll 	}
   1051  1.1     skrll       else
   1052  1.1     skrll 	{
   1053  1.1     skrll 	  Elf_Internal_Sym *sym;
   1054  1.1     skrll 	  asection *sym_sec;
   1055  1.1     skrll 
   1056  1.1     skrll 	  sym = &cookie->locsyms[r_symndx];
   1057  1.1     skrll 	  sym_sec = bfd_section_from_elf_index (abfd, sym->st_shndx);
   1058  1.1     skrll 	  if (sym_sec == NULL)
   1059  1.1     skrll 	    return cie_inf;
   1060  1.1     skrll 
   1061  1.1     skrll 	  if (sym_sec->kept_section != NULL)
   1062  1.1     skrll 	    sym_sec = sym_sec->kept_section;
   1063  1.1     skrll 	  if (sym_sec->output_section == NULL)
   1064  1.1     skrll 	    return cie_inf;
   1065  1.1     skrll 
   1066  1.1     skrll 	  cie->local_personality = 1;
   1067  1.1     skrll 	  cie->personality.val = (sym->st_value
   1068  1.1     skrll 				  + sym_sec->output_offset
   1069  1.1     skrll 				  + sym_sec->output_section->vma);
   1070  1.3  christos 	  per_binds_local = TRUE;
   1071  1.3  christos 	}
   1072  1.3  christos 
   1073  1.3  christos       if (per_binds_local
   1074  1.3  christos 	  && info->shared
   1075  1.3  christos 	  && (cie->per_encoding & 0x70) == DW_EH_PE_absptr
   1076  1.3  christos 	  && (get_elf_backend_data (abfd)
   1077  1.3  christos 	      ->elf_backend_can_make_relative_eh_frame (abfd, info, sec)))
   1078  1.3  christos 	{
   1079  1.3  christos 	  cie_inf->u.cie.make_per_encoding_relative = 1;
   1080  1.3  christos 	  cie_inf->u.cie.per_encoding_relative = 1;
   1081  1.1     skrll 	}
   1082  1.1     skrll     }
   1083  1.1     skrll 
   1084  1.1     skrll   /* See if we can merge this CIE with an earlier one.  */
   1085  1.1     skrll   cie->output_sec = sec->output_section;
   1086  1.1     skrll   cie_compute_hash (cie);
   1087  1.1     skrll   if (hdr_info->cies == NULL)
   1088  1.1     skrll     {
   1089  1.1     skrll       hdr_info->cies = htab_try_create (1, cie_hash, cie_eq, free);
   1090  1.1     skrll       if (hdr_info->cies == NULL)
   1091  1.1     skrll 	return cie_inf;
   1092  1.1     skrll     }
   1093  1.1     skrll   loc = htab_find_slot_with_hash (hdr_info->cies, cie, cie->hash, INSERT);
   1094  1.1     skrll   if (loc == NULL)
   1095  1.1     skrll     return cie_inf;
   1096  1.1     skrll 
   1097  1.1     skrll   new_cie = (struct cie *) *loc;
   1098  1.1     skrll   if (new_cie == NULL)
   1099  1.1     skrll     {
   1100  1.1     skrll       /* Keep CIE_INF and record it in the hash table.  */
   1101  1.3  christos       new_cie = (struct cie *) malloc (sizeof (struct cie));
   1102  1.1     skrll       if (new_cie == NULL)
   1103  1.1     skrll 	return cie_inf;
   1104  1.1     skrll 
   1105  1.1     skrll       memcpy (new_cie, cie, sizeof (struct cie));
   1106  1.1     skrll       *loc = new_cie;
   1107  1.1     skrll     }
   1108  1.1     skrll   else
   1109  1.1     skrll     {
   1110  1.1     skrll       /* Merge CIE_INF with NEW_CIE->CIE_INF.  */
   1111  1.1     skrll       cie_inf->removed = 1;
   1112  1.1     skrll       cie_inf->u.cie.merged = 1;
   1113  1.1     skrll       cie_inf->u.cie.u.merged_with = new_cie->cie_inf;
   1114  1.1     skrll       if (cie_inf->u.cie.make_lsda_relative)
   1115  1.1     skrll 	new_cie->cie_inf->u.cie.make_lsda_relative = 1;
   1116  1.1     skrll     }
   1117  1.1     skrll   return new_cie->cie_inf;
   1118  1.1     skrll }
   1119  1.1     skrll 
   1120  1.1     skrll /* This function is called for each input file before the .eh_frame
   1121  1.1     skrll    section is relocated.  It discards duplicate CIEs and FDEs for discarded
   1122  1.1     skrll    functions.  The function returns TRUE iff any entries have been
   1123  1.1     skrll    deleted.  */
   1124  1.1     skrll 
   1125  1.1     skrll bfd_boolean
   1126  1.1     skrll _bfd_elf_discard_section_eh_frame
   1127  1.1     skrll    (bfd *abfd, struct bfd_link_info *info, asection *sec,
   1128  1.1     skrll     bfd_boolean (*reloc_symbol_deleted_p) (bfd_vma, void *),
   1129  1.1     skrll     struct elf_reloc_cookie *cookie)
   1130  1.1     skrll {
   1131  1.1     skrll   struct eh_cie_fde *ent;
   1132  1.1     skrll   struct eh_frame_sec_info *sec_info;
   1133  1.1     skrll   struct eh_frame_hdr_info *hdr_info;
   1134  1.1     skrll   unsigned int ptr_size, offset;
   1135  1.1     skrll 
   1136  1.1     skrll   sec_info = (struct eh_frame_sec_info *) elf_section_data (sec)->sec_info;
   1137  1.1     skrll   if (sec_info == NULL)
   1138  1.1     skrll     return FALSE;
   1139  1.1     skrll 
   1140  1.1     skrll   hdr_info = &elf_hash_table (info)->eh_info;
   1141  1.1     skrll   for (ent = sec_info->entry; ent < sec_info->entry + sec_info->count; ++ent)
   1142  1.1     skrll     if (ent->size == 4)
   1143  1.1     skrll       /* There should only be one zero terminator, on the last input
   1144  1.1     skrll 	 file supplying .eh_frame (crtend.o).  Remove any others.  */
   1145  1.1     skrll       ent->removed = sec->map_head.s != NULL;
   1146  1.1     skrll     else if (!ent->cie)
   1147  1.1     skrll       {
   1148  1.1     skrll 	cookie->rel = cookie->rels + ent->reloc_index;
   1149  1.3  christos 	/* FIXME: octets_per_byte.  */
   1150  1.1     skrll 	BFD_ASSERT (cookie->rel < cookie->relend
   1151  1.1     skrll 		    && cookie->rel->r_offset == ent->offset + 8);
   1152  1.1     skrll 	if (!(*reloc_symbol_deleted_p) (ent->offset + 8, cookie))
   1153  1.1     skrll 	  {
   1154  1.1     skrll 	    if (info->shared
   1155  1.3  christos 		&& (((ent->fde_encoding & 0x70) == DW_EH_PE_absptr
   1156  1.1     skrll 		     && ent->make_relative == 0)
   1157  1.3  christos 		    || (ent->fde_encoding & 0x70) == DW_EH_PE_aligned))
   1158  1.1     skrll 	      {
   1159  1.1     skrll 		/* If a shared library uses absolute pointers
   1160  1.1     skrll 		   which we cannot turn into PC relative,
   1161  1.1     skrll 		   don't create the binary search table,
   1162  1.1     skrll 		   since it is affected by runtime relocations.  */
   1163  1.1     skrll 		hdr_info->table = FALSE;
   1164  1.1     skrll 		(*info->callbacks->einfo)
   1165  1.1     skrll 		  (_("%P: fde encoding in %B(%A) prevents .eh_frame_hdr"
   1166  1.1     skrll 		     " table being created.\n"), abfd, sec);
   1167  1.1     skrll 	      }
   1168  1.1     skrll 	    ent->removed = 0;
   1169  1.1     skrll 	    hdr_info->fde_count++;
   1170  1.3  christos 	    ent->u.fde.cie_inf = find_merged_cie (abfd, info, sec, hdr_info,
   1171  1.3  christos 						  cookie, ent->u.fde.cie_inf);
   1172  1.1     skrll 	  }
   1173  1.1     skrll       }
   1174  1.1     skrll 
   1175  1.1     skrll   if (sec_info->cies)
   1176  1.1     skrll     {
   1177  1.1     skrll       free (sec_info->cies);
   1178  1.1     skrll       sec_info->cies = NULL;
   1179  1.1     skrll     }
   1180  1.1     skrll 
   1181  1.1     skrll   ptr_size = (get_elf_backend_data (sec->owner)
   1182  1.1     skrll 	      ->elf_backend_eh_frame_address_size (sec->owner, sec));
   1183  1.1     skrll   offset = 0;
   1184  1.1     skrll   for (ent = sec_info->entry; ent < sec_info->entry + sec_info->count; ++ent)
   1185  1.1     skrll     if (!ent->removed)
   1186  1.1     skrll       {
   1187  1.1     skrll 	ent->new_offset = offset;
   1188  1.1     skrll 	offset += size_of_output_cie_fde (ent, ptr_size);
   1189  1.1     skrll       }
   1190  1.1     skrll 
   1191  1.1     skrll   sec->rawsize = sec->size;
   1192  1.1     skrll   sec->size = offset;
   1193  1.1     skrll   return offset != sec->rawsize;
   1194  1.1     skrll }
   1195  1.1     skrll 
   1196  1.1     skrll /* This function is called for .eh_frame_hdr section after
   1197  1.1     skrll    _bfd_elf_discard_section_eh_frame has been called on all .eh_frame
   1198  1.1     skrll    input sections.  It finalizes the size of .eh_frame_hdr section.  */
   1199  1.1     skrll 
   1200  1.1     skrll bfd_boolean
   1201  1.1     skrll _bfd_elf_discard_section_eh_frame_hdr (bfd *abfd, struct bfd_link_info *info)
   1202  1.1     skrll {
   1203  1.1     skrll   struct elf_link_hash_table *htab;
   1204  1.1     skrll   struct eh_frame_hdr_info *hdr_info;
   1205  1.1     skrll   asection *sec;
   1206  1.1     skrll 
   1207  1.1     skrll   htab = elf_hash_table (info);
   1208  1.1     skrll   hdr_info = &htab->eh_info;
   1209  1.1     skrll 
   1210  1.1     skrll   if (hdr_info->cies != NULL)
   1211  1.1     skrll     {
   1212  1.1     skrll       htab_delete (hdr_info->cies);
   1213  1.1     skrll       hdr_info->cies = NULL;
   1214  1.1     skrll     }
   1215  1.1     skrll 
   1216  1.1     skrll   sec = hdr_info->hdr_sec;
   1217  1.1     skrll   if (sec == NULL)
   1218  1.1     skrll     return FALSE;
   1219  1.1     skrll 
   1220  1.1     skrll   sec->size = EH_FRAME_HDR_SIZE;
   1221  1.1     skrll   if (hdr_info->table)
   1222  1.1     skrll     sec->size += 4 + hdr_info->fde_count * 8;
   1223  1.1     skrll 
   1224  1.1     skrll   elf_tdata (abfd)->eh_frame_hdr = sec;
   1225  1.1     skrll   return TRUE;
   1226  1.1     skrll }
   1227  1.1     skrll 
   1228  1.1     skrll /* This function is called from size_dynamic_sections.
   1229  1.1     skrll    It needs to decide whether .eh_frame_hdr should be output or not,
   1230  1.1     skrll    because when the dynamic symbol table has been sized it is too late
   1231  1.1     skrll    to strip sections.  */
   1232  1.1     skrll 
   1233  1.1     skrll bfd_boolean
   1234  1.1     skrll _bfd_elf_maybe_strip_eh_frame_hdr (struct bfd_link_info *info)
   1235  1.1     skrll {
   1236  1.1     skrll   asection *o;
   1237  1.1     skrll   bfd *abfd;
   1238  1.1     skrll   struct elf_link_hash_table *htab;
   1239  1.1     skrll   struct eh_frame_hdr_info *hdr_info;
   1240  1.1     skrll 
   1241  1.1     skrll   htab = elf_hash_table (info);
   1242  1.1     skrll   hdr_info = &htab->eh_info;
   1243  1.1     skrll   if (hdr_info->hdr_sec == NULL)
   1244  1.1     skrll     return TRUE;
   1245  1.1     skrll 
   1246  1.1     skrll   if (bfd_is_abs_section (hdr_info->hdr_sec->output_section))
   1247  1.1     skrll     {
   1248  1.1     skrll       hdr_info->hdr_sec = NULL;
   1249  1.1     skrll       return TRUE;
   1250  1.1     skrll     }
   1251  1.1     skrll 
   1252  1.1     skrll   abfd = NULL;
   1253  1.1     skrll   if (info->eh_frame_hdr)
   1254  1.1     skrll     for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
   1255  1.1     skrll       {
   1256  1.1     skrll 	/* Count only sections which have at least a single CIE or FDE.
   1257  1.1     skrll 	   There cannot be any CIE or FDE <= 8 bytes.  */
   1258  1.1     skrll 	o = bfd_get_section_by_name (abfd, ".eh_frame");
   1259  1.1     skrll 	if (o && o->size > 8 && !bfd_is_abs_section (o->output_section))
   1260  1.1     skrll 	  break;
   1261  1.1     skrll       }
   1262  1.1     skrll 
   1263  1.1     skrll   if (abfd == NULL)
   1264  1.1     skrll     {
   1265  1.1     skrll       hdr_info->hdr_sec->flags |= SEC_EXCLUDE;
   1266  1.1     skrll       hdr_info->hdr_sec = NULL;
   1267  1.1     skrll       return TRUE;
   1268  1.1     skrll     }
   1269  1.1     skrll 
   1270  1.1     skrll   hdr_info->table = TRUE;
   1271  1.1     skrll   return TRUE;
   1272  1.1     skrll }
   1273  1.1     skrll 
   1274  1.1     skrll /* Adjust an address in the .eh_frame section.  Given OFFSET within
   1275  1.1     skrll    SEC, this returns the new offset in the adjusted .eh_frame section,
   1276  1.1     skrll    or -1 if the address refers to a CIE/FDE which has been removed
   1277  1.1     skrll    or to offset with dynamic relocation which is no longer needed.  */
   1278  1.1     skrll 
   1279  1.1     skrll bfd_vma
   1280  1.1     skrll _bfd_elf_eh_frame_section_offset (bfd *output_bfd ATTRIBUTE_UNUSED,
   1281  1.3  christos 				  struct bfd_link_info *info ATTRIBUTE_UNUSED,
   1282  1.1     skrll 				  asection *sec,
   1283  1.1     skrll 				  bfd_vma offset)
   1284  1.1     skrll {
   1285  1.1     skrll   struct eh_frame_sec_info *sec_info;
   1286  1.1     skrll   unsigned int lo, hi, mid;
   1287  1.1     skrll 
   1288  1.1     skrll   if (sec->sec_info_type != ELF_INFO_TYPE_EH_FRAME)
   1289  1.1     skrll     return offset;
   1290  1.3  christos   sec_info = (struct eh_frame_sec_info *) elf_section_data (sec)->sec_info;
   1291  1.1     skrll 
   1292  1.1     skrll   if (offset >= sec->rawsize)
   1293  1.1     skrll     return offset - sec->rawsize + sec->size;
   1294  1.1     skrll 
   1295  1.1     skrll   lo = 0;
   1296  1.1     skrll   hi = sec_info->count;
   1297  1.1     skrll   mid = 0;
   1298  1.1     skrll   while (lo < hi)
   1299  1.1     skrll     {
   1300  1.1     skrll       mid = (lo + hi) / 2;
   1301  1.1     skrll       if (offset < sec_info->entry[mid].offset)
   1302  1.1     skrll 	hi = mid;
   1303  1.1     skrll       else if (offset
   1304  1.1     skrll 	       >= sec_info->entry[mid].offset + sec_info->entry[mid].size)
   1305  1.1     skrll 	lo = mid + 1;
   1306  1.1     skrll       else
   1307  1.1     skrll 	break;
   1308  1.1     skrll     }
   1309  1.1     skrll 
   1310  1.1     skrll   BFD_ASSERT (lo < hi);
   1311  1.1     skrll 
   1312  1.1     skrll   /* FDE or CIE was removed.  */
   1313  1.1     skrll   if (sec_info->entry[mid].removed)
   1314  1.1     skrll     return (bfd_vma) -1;
   1315  1.1     skrll 
   1316  1.3  christos   /* If converting personality pointers to DW_EH_PE_pcrel, there will be
   1317  1.3  christos      no need for run-time relocation against the personality field.  */
   1318  1.3  christos   if (sec_info->entry[mid].cie
   1319  1.3  christos       && sec_info->entry[mid].u.cie.make_per_encoding_relative
   1320  1.3  christos       && offset == (sec_info->entry[mid].offset + 8
   1321  1.3  christos 		    + sec_info->entry[mid].u.cie.personality_offset))
   1322  1.3  christos     return (bfd_vma) -2;
   1323  1.3  christos 
   1324  1.1     skrll   /* If converting to DW_EH_PE_pcrel, there will be no need for run-time
   1325  1.1     skrll      relocation against FDE's initial_location field.  */
   1326  1.1     skrll   if (!sec_info->entry[mid].cie
   1327  1.1     skrll       && sec_info->entry[mid].make_relative
   1328  1.1     skrll       && offset == sec_info->entry[mid].offset + 8)
   1329  1.1     skrll     return (bfd_vma) -2;
   1330  1.1     skrll 
   1331  1.1     skrll   /* If converting LSDA pointers to DW_EH_PE_pcrel, there will be no need
   1332  1.1     skrll      for run-time relocation against LSDA field.  */
   1333  1.1     skrll   if (!sec_info->entry[mid].cie
   1334  1.1     skrll       && sec_info->entry[mid].u.fde.cie_inf->u.cie.make_lsda_relative
   1335  1.1     skrll       && offset == (sec_info->entry[mid].offset + 8
   1336  1.1     skrll 		    + sec_info->entry[mid].lsda_offset))
   1337  1.1     skrll     return (bfd_vma) -2;
   1338  1.1     skrll 
   1339  1.1     skrll   /* If converting to DW_EH_PE_pcrel, there will be no need for run-time
   1340  1.1     skrll      relocation against DW_CFA_set_loc's arguments.  */
   1341  1.1     skrll   if (sec_info->entry[mid].set_loc
   1342  1.1     skrll       && sec_info->entry[mid].make_relative
   1343  1.1     skrll       && (offset >= sec_info->entry[mid].offset + 8
   1344  1.1     skrll 		    + sec_info->entry[mid].set_loc[1]))
   1345  1.1     skrll     {
   1346  1.1     skrll       unsigned int cnt;
   1347  1.1     skrll 
   1348  1.1     skrll       for (cnt = 1; cnt <= sec_info->entry[mid].set_loc[0]; cnt++)
   1349  1.1     skrll 	if (offset == sec_info->entry[mid].offset + 8
   1350  1.1     skrll 		      + sec_info->entry[mid].set_loc[cnt])
   1351  1.1     skrll 	  return (bfd_vma) -2;
   1352  1.1     skrll     }
   1353  1.1     skrll 
   1354  1.1     skrll   /* Any new augmentation bytes go before the first relocation.  */
   1355  1.1     skrll   return (offset + sec_info->entry[mid].new_offset
   1356  1.1     skrll 	  - sec_info->entry[mid].offset
   1357  1.1     skrll 	  + extra_augmentation_string_bytes (sec_info->entry + mid)
   1358  1.1     skrll 	  + extra_augmentation_data_bytes (sec_info->entry + mid));
   1359  1.1     skrll }
   1360  1.1     skrll 
   1361  1.1     skrll /* Write out .eh_frame section.  This is called with the relocated
   1362  1.1     skrll    contents.  */
   1363  1.1     skrll 
   1364  1.1     skrll bfd_boolean
   1365  1.1     skrll _bfd_elf_write_section_eh_frame (bfd *abfd,
   1366  1.1     skrll 				 struct bfd_link_info *info,
   1367  1.1     skrll 				 asection *sec,
   1368  1.1     skrll 				 bfd_byte *contents)
   1369  1.1     skrll {
   1370  1.1     skrll   struct eh_frame_sec_info *sec_info;
   1371  1.1     skrll   struct elf_link_hash_table *htab;
   1372  1.1     skrll   struct eh_frame_hdr_info *hdr_info;
   1373  1.1     skrll   unsigned int ptr_size;
   1374  1.1     skrll   struct eh_cie_fde *ent;
   1375  1.1     skrll 
   1376  1.1     skrll   if (sec->sec_info_type != ELF_INFO_TYPE_EH_FRAME)
   1377  1.3  christos     /* FIXME: octets_per_byte.  */
   1378  1.1     skrll     return bfd_set_section_contents (abfd, sec->output_section, contents,
   1379  1.1     skrll 				     sec->output_offset, sec->size);
   1380  1.1     skrll 
   1381  1.1     skrll   ptr_size = (get_elf_backend_data (abfd)
   1382  1.1     skrll 	      ->elf_backend_eh_frame_address_size (abfd, sec));
   1383  1.1     skrll   BFD_ASSERT (ptr_size != 0);
   1384  1.1     skrll 
   1385  1.3  christos   sec_info = (struct eh_frame_sec_info *) elf_section_data (sec)->sec_info;
   1386  1.1     skrll   htab = elf_hash_table (info);
   1387  1.1     skrll   hdr_info = &htab->eh_info;
   1388  1.1     skrll 
   1389  1.1     skrll   if (hdr_info->table && hdr_info->array == NULL)
   1390  1.3  christos     hdr_info->array = (struct eh_frame_array_ent *)
   1391  1.3  christos         bfd_malloc (hdr_info->fde_count * sizeof(*hdr_info->array));
   1392  1.1     skrll   if (hdr_info->array == NULL)
   1393  1.1     skrll     hdr_info = NULL;
   1394  1.1     skrll 
   1395  1.1     skrll   /* The new offsets can be bigger or smaller than the original offsets.
   1396  1.1     skrll      We therefore need to make two passes over the section: one backward
   1397  1.1     skrll      pass to move entries up and one forward pass to move entries down.
   1398  1.1     skrll      The two passes won't interfere with each other because entries are
   1399  1.1     skrll      not reordered  */
   1400  1.1     skrll   for (ent = sec_info->entry + sec_info->count; ent-- != sec_info->entry;)
   1401  1.1     skrll     if (!ent->removed && ent->new_offset > ent->offset)
   1402  1.1     skrll       memmove (contents + ent->new_offset, contents + ent->offset, ent->size);
   1403  1.1     skrll 
   1404  1.1     skrll   for (ent = sec_info->entry; ent < sec_info->entry + sec_info->count; ++ent)
   1405  1.1     skrll     if (!ent->removed && ent->new_offset < ent->offset)
   1406  1.1     skrll       memmove (contents + ent->new_offset, contents + ent->offset, ent->size);
   1407  1.1     skrll 
   1408  1.1     skrll   for (ent = sec_info->entry; ent < sec_info->entry + sec_info->count; ++ent)
   1409  1.1     skrll     {
   1410  1.1     skrll       unsigned char *buf, *end;
   1411  1.1     skrll       unsigned int new_size;
   1412  1.1     skrll 
   1413  1.1     skrll       if (ent->removed)
   1414  1.1     skrll 	continue;
   1415  1.1     skrll 
   1416  1.1     skrll       if (ent->size == 4)
   1417  1.1     skrll 	{
   1418  1.1     skrll 	  /* Any terminating FDE must be at the end of the section.  */
   1419  1.1     skrll 	  BFD_ASSERT (ent == sec_info->entry + sec_info->count - 1);
   1420  1.1     skrll 	  continue;
   1421  1.1     skrll 	}
   1422  1.1     skrll 
   1423  1.1     skrll       buf = contents + ent->new_offset;
   1424  1.1     skrll       end = buf + ent->size;
   1425  1.1     skrll       new_size = size_of_output_cie_fde (ent, ptr_size);
   1426  1.1     skrll 
   1427  1.1     skrll       /* Update the size.  It may be shrinked.  */
   1428  1.1     skrll       bfd_put_32 (abfd, new_size - 4, buf);
   1429  1.1     skrll 
   1430  1.1     skrll       /* Filling the extra bytes with DW_CFA_nops.  */
   1431  1.1     skrll       if (new_size != ent->size)
   1432  1.1     skrll 	memset (end, 0, new_size - ent->size);
   1433  1.1     skrll 
   1434  1.1     skrll       if (ent->cie)
   1435  1.1     skrll 	{
   1436  1.1     skrll 	  /* CIE */
   1437  1.1     skrll 	  if (ent->make_relative
   1438  1.1     skrll 	      || ent->u.cie.make_lsda_relative
   1439  1.1     skrll 	      || ent->u.cie.per_encoding_relative)
   1440  1.1     skrll 	    {
   1441  1.1     skrll 	      char *aug;
   1442  1.1     skrll 	      unsigned int action, extra_string, extra_data;
   1443  1.1     skrll 	      unsigned int per_width, per_encoding;
   1444  1.1     skrll 
   1445  1.1     skrll 	      /* Need to find 'R' or 'L' augmentation's argument and modify
   1446  1.1     skrll 		 DW_EH_PE_* value.  */
   1447  1.1     skrll 	      action = ((ent->make_relative ? 1 : 0)
   1448  1.1     skrll 			| (ent->u.cie.make_lsda_relative ? 2 : 0)
   1449  1.1     skrll 			| (ent->u.cie.per_encoding_relative ? 4 : 0));
   1450  1.1     skrll 	      extra_string = extra_augmentation_string_bytes (ent);
   1451  1.1     skrll 	      extra_data = extra_augmentation_data_bytes (ent);
   1452  1.1     skrll 
   1453  1.1     skrll 	      /* Skip length, id and version.  */
   1454  1.1     skrll 	      buf += 9;
   1455  1.1     skrll 	      aug = (char *) buf;
   1456  1.1     skrll 	      buf += strlen (aug) + 1;
   1457  1.1     skrll 	      skip_leb128 (&buf, end);
   1458  1.1     skrll 	      skip_leb128 (&buf, end);
   1459  1.1     skrll 	      skip_leb128 (&buf, end);
   1460  1.1     skrll 	      if (*aug == 'z')
   1461  1.1     skrll 		{
   1462  1.1     skrll 		  /* The uleb128 will always be a single byte for the kind
   1463  1.1     skrll 		     of augmentation strings that we're prepared to handle.  */
   1464  1.1     skrll 		  *buf++ += extra_data;
   1465  1.1     skrll 		  aug++;
   1466  1.1     skrll 		}
   1467  1.1     skrll 
   1468  1.1     skrll 	      /* Make room for the new augmentation string and data bytes.  */
   1469  1.1     skrll 	      memmove (buf + extra_string + extra_data, buf, end - buf);
   1470  1.1     skrll 	      memmove (aug + extra_string, aug, buf - (bfd_byte *) aug);
   1471  1.1     skrll 	      buf += extra_string;
   1472  1.1     skrll 	      end += extra_string + extra_data;
   1473  1.1     skrll 
   1474  1.1     skrll 	      if (ent->add_augmentation_size)
   1475  1.1     skrll 		{
   1476  1.1     skrll 		  *aug++ = 'z';
   1477  1.1     skrll 		  *buf++ = extra_data - 1;
   1478  1.1     skrll 		}
   1479  1.1     skrll 	      if (ent->u.cie.add_fde_encoding)
   1480  1.1     skrll 		{
   1481  1.1     skrll 		  BFD_ASSERT (action & 1);
   1482  1.1     skrll 		  *aug++ = 'R';
   1483  1.3  christos 		  *buf++ = make_pc_relative (DW_EH_PE_absptr, ptr_size);
   1484  1.1     skrll 		  action &= ~1;
   1485  1.1     skrll 		}
   1486  1.1     skrll 
   1487  1.1     skrll 	      while (action)
   1488  1.1     skrll 		switch (*aug++)
   1489  1.1     skrll 		  {
   1490  1.1     skrll 		  case 'L':
   1491  1.1     skrll 		    if (action & 2)
   1492  1.1     skrll 		      {
   1493  1.1     skrll 			BFD_ASSERT (*buf == ent->lsda_encoding);
   1494  1.3  christos 			*buf = make_pc_relative (*buf, ptr_size);
   1495  1.1     skrll 			action &= ~2;
   1496  1.1     skrll 		      }
   1497  1.1     skrll 		    buf++;
   1498  1.1     skrll 		    break;
   1499  1.1     skrll 		  case 'P':
   1500  1.3  christos 		    if (ent->u.cie.make_per_encoding_relative)
   1501  1.3  christos 		      *buf = make_pc_relative (*buf, ptr_size);
   1502  1.1     skrll 		    per_encoding = *buf++;
   1503  1.1     skrll 		    per_width = get_DW_EH_PE_width (per_encoding, ptr_size);
   1504  1.1     skrll 		    BFD_ASSERT (per_width != 0);
   1505  1.1     skrll 		    BFD_ASSERT (((per_encoding & 0x70) == DW_EH_PE_pcrel)
   1506  1.1     skrll 				== ent->u.cie.per_encoding_relative);
   1507  1.3  christos 		    if ((per_encoding & 0x70) == DW_EH_PE_aligned)
   1508  1.1     skrll 		      buf = (contents
   1509  1.1     skrll 			     + ((buf - contents + per_width - 1)
   1510  1.1     skrll 				& ~((bfd_size_type) per_width - 1)));
   1511  1.1     skrll 		    if (action & 4)
   1512  1.1     skrll 		      {
   1513  1.1     skrll 			bfd_vma val;
   1514  1.1     skrll 
   1515  1.1     skrll 			val = read_value (abfd, buf, per_width,
   1516  1.1     skrll 					  get_DW_EH_PE_signed (per_encoding));
   1517  1.3  christos 			if (ent->u.cie.make_per_encoding_relative)
   1518  1.3  christos 			  val -= (sec->output_section->vma
   1519  1.3  christos 				  + sec->output_offset
   1520  1.3  christos 				  + (buf - contents));
   1521  1.3  christos 			else
   1522  1.3  christos 			  {
   1523  1.3  christos 			    val += (bfd_vma) ent->offset - ent->new_offset;
   1524  1.3  christos 			    val -= extra_string + extra_data;
   1525  1.3  christos 			  }
   1526  1.1     skrll 			write_value (abfd, buf, val, per_width);
   1527  1.1     skrll 			action &= ~4;
   1528  1.1     skrll 		      }
   1529  1.1     skrll 		    buf += per_width;
   1530  1.1     skrll 		    break;
   1531  1.1     skrll 		  case 'R':
   1532  1.1     skrll 		    if (action & 1)
   1533  1.1     skrll 		      {
   1534  1.1     skrll 			BFD_ASSERT (*buf == ent->fde_encoding);
   1535  1.3  christos 			*buf = make_pc_relative (*buf, ptr_size);
   1536  1.1     skrll 			action &= ~1;
   1537  1.1     skrll 		      }
   1538  1.1     skrll 		    buf++;
   1539  1.1     skrll 		    break;
   1540  1.1     skrll 		  case 'S':
   1541  1.1     skrll 		    break;
   1542  1.1     skrll 		  default:
   1543  1.1     skrll 		    BFD_FAIL ();
   1544  1.1     skrll 		  }
   1545  1.1     skrll 	    }
   1546  1.1     skrll 	}
   1547  1.1     skrll       else
   1548  1.1     skrll 	{
   1549  1.1     skrll 	  /* FDE */
   1550  1.1     skrll 	  bfd_vma value, address;
   1551  1.1     skrll 	  unsigned int width;
   1552  1.1     skrll 	  bfd_byte *start;
   1553  1.1     skrll 	  struct eh_cie_fde *cie;
   1554  1.1     skrll 
   1555  1.1     skrll 	  /* Skip length.  */
   1556  1.1     skrll 	  cie = ent->u.fde.cie_inf;
   1557  1.1     skrll 	  buf += 4;
   1558  1.1     skrll 	  value = ((ent->new_offset + sec->output_offset + 4)
   1559  1.1     skrll 		   - (cie->new_offset + cie->u.cie.u.sec->output_offset));
   1560  1.1     skrll 	  bfd_put_32 (abfd, value, buf);
   1561  1.1     skrll 	  buf += 4;
   1562  1.1     skrll 	  width = get_DW_EH_PE_width (ent->fde_encoding, ptr_size);
   1563  1.1     skrll 	  value = read_value (abfd, buf, width,
   1564  1.1     skrll 			      get_DW_EH_PE_signed (ent->fde_encoding));
   1565  1.1     skrll 	  address = value;
   1566  1.1     skrll 	  if (value)
   1567  1.1     skrll 	    {
   1568  1.3  christos 	      switch (ent->fde_encoding & 0x70)
   1569  1.1     skrll 		{
   1570  1.1     skrll 		case DW_EH_PE_textrel:
   1571  1.1     skrll 		  BFD_ASSERT (hdr_info == NULL);
   1572  1.1     skrll 		  break;
   1573  1.1     skrll 		case DW_EH_PE_datarel:
   1574  1.1     skrll 		  {
   1575  1.3  christos 		    switch (abfd->arch_info->arch)
   1576  1.3  christos 		      {
   1577  1.3  christos 		      case bfd_arch_ia64:
   1578  1.3  christos 			BFD_ASSERT (elf_gp (abfd) != 0);
   1579  1.3  christos 			address += elf_gp (abfd);
   1580  1.3  christos 			break;
   1581  1.3  christos 		      default:
   1582  1.3  christos 			(*info->callbacks->einfo)
   1583  1.3  christos 			  (_("%P: DW_EH_PE_datarel unspecified"
   1584  1.3  christos 			     " for this architecture.\n"));
   1585  1.3  christos 			/* Fall thru */
   1586  1.3  christos 		      case bfd_arch_frv:
   1587  1.3  christos 		      case bfd_arch_i386:
   1588  1.3  christos 			BFD_ASSERT (htab->hgot != NULL
   1589  1.3  christos 				    && ((htab->hgot->root.type
   1590  1.3  christos 					 == bfd_link_hash_defined)
   1591  1.3  christos 					|| (htab->hgot->root.type
   1592  1.3  christos 					    == bfd_link_hash_defweak)));
   1593  1.3  christos 			address
   1594  1.3  christos 			  += (htab->hgot->root.u.def.value
   1595  1.3  christos 			      + htab->hgot->root.u.def.section->output_offset
   1596  1.3  christos 			      + (htab->hgot->root.u.def.section->output_section
   1597  1.3  christos 				 ->vma));
   1598  1.3  christos 			break;
   1599  1.3  christos 		      }
   1600  1.1     skrll 		  }
   1601  1.1     skrll 		  break;
   1602  1.1     skrll 		case DW_EH_PE_pcrel:
   1603  1.1     skrll 		  value += (bfd_vma) ent->offset - ent->new_offset;
   1604  1.1     skrll 		  address += (sec->output_section->vma
   1605  1.1     skrll 			      + sec->output_offset
   1606  1.1     skrll 			      + ent->offset + 8);
   1607  1.1     skrll 		  break;
   1608  1.1     skrll 		}
   1609  1.1     skrll 	      if (ent->make_relative)
   1610  1.1     skrll 		value -= (sec->output_section->vma
   1611  1.1     skrll 			  + sec->output_offset
   1612  1.1     skrll 			  + ent->new_offset + 8);
   1613  1.1     skrll 	      write_value (abfd, buf, value, width);
   1614  1.1     skrll 	    }
   1615  1.1     skrll 
   1616  1.1     skrll 	  start = buf;
   1617  1.1     skrll 
   1618  1.1     skrll 	  if (hdr_info)
   1619  1.1     skrll 	    {
   1620  1.3  christos 	      /* The address calculation may overflow, giving us a
   1621  1.3  christos 		 value greater than 4G on a 32-bit target when
   1622  1.3  christos 		 dwarf_vma is 64-bit.  */
   1623  1.3  christos 	      if (sizeof (address) > 4 && ptr_size == 4)
   1624  1.3  christos 		address &= 0xffffffff;
   1625  1.1     skrll 	      hdr_info->array[hdr_info->array_count].initial_loc = address;
   1626  1.1     skrll 	      hdr_info->array[hdr_info->array_count++].fde
   1627  1.1     skrll 		= (sec->output_section->vma
   1628  1.1     skrll 		   + sec->output_offset
   1629  1.1     skrll 		   + ent->new_offset);
   1630  1.1     skrll 	    }
   1631  1.1     skrll 
   1632  1.3  christos 	  if ((ent->lsda_encoding & 0x70) == DW_EH_PE_pcrel
   1633  1.1     skrll 	      || cie->u.cie.make_lsda_relative)
   1634  1.1     skrll 	    {
   1635  1.1     skrll 	      buf += ent->lsda_offset;
   1636  1.1     skrll 	      width = get_DW_EH_PE_width (ent->lsda_encoding, ptr_size);
   1637  1.1     skrll 	      value = read_value (abfd, buf, width,
   1638  1.1     skrll 				  get_DW_EH_PE_signed (ent->lsda_encoding));
   1639  1.1     skrll 	      if (value)
   1640  1.1     skrll 		{
   1641  1.3  christos 		  if ((ent->lsda_encoding & 0x70) == DW_EH_PE_pcrel)
   1642  1.1     skrll 		    value += (bfd_vma) ent->offset - ent->new_offset;
   1643  1.1     skrll 		  else if (cie->u.cie.make_lsda_relative)
   1644  1.1     skrll 		    value -= (sec->output_section->vma
   1645  1.1     skrll 			      + sec->output_offset
   1646  1.1     skrll 			      + ent->new_offset + 8 + ent->lsda_offset);
   1647  1.1     skrll 		  write_value (abfd, buf, value, width);
   1648  1.1     skrll 		}
   1649  1.1     skrll 	    }
   1650  1.1     skrll 	  else if (ent->add_augmentation_size)
   1651  1.1     skrll 	    {
   1652  1.1     skrll 	      /* Skip the PC and length and insert a zero byte for the
   1653  1.1     skrll 		 augmentation size.  */
   1654  1.1     skrll 	      buf += width * 2;
   1655  1.1     skrll 	      memmove (buf + 1, buf, end - buf);
   1656  1.1     skrll 	      *buf = 0;
   1657  1.1     skrll 	    }
   1658  1.1     skrll 
   1659  1.1     skrll 	  if (ent->set_loc)
   1660  1.1     skrll 	    {
   1661  1.1     skrll 	      /* Adjust DW_CFA_set_loc.  */
   1662  1.3  christos 	      unsigned int cnt;
   1663  1.1     skrll 	      bfd_vma new_offset;
   1664  1.1     skrll 
   1665  1.1     skrll 	      width = get_DW_EH_PE_width (ent->fde_encoding, ptr_size);
   1666  1.1     skrll 	      new_offset = ent->new_offset + 8
   1667  1.1     skrll 			   + extra_augmentation_string_bytes (ent)
   1668  1.1     skrll 			   + extra_augmentation_data_bytes (ent);
   1669  1.1     skrll 
   1670  1.1     skrll 	      for (cnt = 1; cnt <= ent->set_loc[0]; cnt++)
   1671  1.1     skrll 		{
   1672  1.1     skrll 		  buf = start + ent->set_loc[cnt];
   1673  1.1     skrll 
   1674  1.1     skrll 		  value = read_value (abfd, buf, width,
   1675  1.1     skrll 				      get_DW_EH_PE_signed (ent->fde_encoding));
   1676  1.1     skrll 		  if (!value)
   1677  1.1     skrll 		    continue;
   1678  1.1     skrll 
   1679  1.3  christos 		  if ((ent->fde_encoding & 0x70) == DW_EH_PE_pcrel)
   1680  1.1     skrll 		    value += (bfd_vma) ent->offset + 8 - new_offset;
   1681  1.1     skrll 		  if (ent->make_relative)
   1682  1.1     skrll 		    value -= (sec->output_section->vma
   1683  1.1     skrll 			      + sec->output_offset
   1684  1.1     skrll 			      + new_offset + ent->set_loc[cnt]);
   1685  1.1     skrll 		  write_value (abfd, buf, value, width);
   1686  1.1     skrll 		}
   1687  1.1     skrll 	    }
   1688  1.1     skrll 	}
   1689  1.1     skrll     }
   1690  1.1     skrll 
   1691  1.1     skrll   /* We don't align the section to its section alignment since the
   1692  1.1     skrll      runtime library only expects all CIE/FDE records aligned at
   1693  1.1     skrll      the pointer size. _bfd_elf_discard_section_eh_frame should
   1694  1.1     skrll      have padded CIE/FDE records to multiple of pointer size with
   1695  1.1     skrll      size_of_output_cie_fde.  */
   1696  1.1     skrll   if ((sec->size % ptr_size) != 0)
   1697  1.1     skrll     abort ();
   1698  1.1     skrll 
   1699  1.3  christos   /* FIXME: octets_per_byte.  */
   1700  1.1     skrll   return bfd_set_section_contents (abfd, sec->output_section,
   1701  1.1     skrll 				   contents, (file_ptr) sec->output_offset,
   1702  1.1     skrll 				   sec->size);
   1703  1.1     skrll }
   1704  1.1     skrll 
   1705  1.1     skrll /* Helper function used to sort .eh_frame_hdr search table by increasing
   1706  1.1     skrll    VMA of FDE initial location.  */
   1707  1.1     skrll 
   1708  1.1     skrll static int
   1709  1.1     skrll vma_compare (const void *a, const void *b)
   1710  1.1     skrll {
   1711  1.3  christos   const struct eh_frame_array_ent *p = (const struct eh_frame_array_ent *) a;
   1712  1.3  christos   const struct eh_frame_array_ent *q = (const struct eh_frame_array_ent *) b;
   1713  1.1     skrll   if (p->initial_loc > q->initial_loc)
   1714  1.1     skrll     return 1;
   1715  1.1     skrll   if (p->initial_loc < q->initial_loc)
   1716  1.1     skrll     return -1;
   1717  1.1     skrll   return 0;
   1718  1.1     skrll }
   1719  1.1     skrll 
   1720  1.1     skrll /* Write out .eh_frame_hdr section.  This must be called after
   1721  1.1     skrll    _bfd_elf_write_section_eh_frame has been called on all input
   1722  1.1     skrll    .eh_frame sections.
   1723  1.1     skrll    .eh_frame_hdr format:
   1724  1.1     skrll    ubyte version		(currently 1)
   1725  1.1     skrll    ubyte eh_frame_ptr_enc  	(DW_EH_PE_* encoding of pointer to start of
   1726  1.1     skrll 				 .eh_frame section)
   1727  1.1     skrll    ubyte fde_count_enc		(DW_EH_PE_* encoding of total FDE count
   1728  1.1     skrll 				 number (or DW_EH_PE_omit if there is no
   1729  1.1     skrll 				 binary search table computed))
   1730  1.1     skrll    ubyte table_enc		(DW_EH_PE_* encoding of binary search table,
   1731  1.1     skrll 				 or DW_EH_PE_omit if not present.
   1732  1.1     skrll 				 DW_EH_PE_datarel is using address of
   1733  1.1     skrll 				 .eh_frame_hdr section start as base)
   1734  1.1     skrll    [encoded] eh_frame_ptr	(pointer to start of .eh_frame section)
   1735  1.1     skrll    optionally followed by:
   1736  1.1     skrll    [encoded] fde_count		(total number of FDEs in .eh_frame section)
   1737  1.1     skrll    fde_count x [encoded] initial_loc, fde
   1738  1.1     skrll 				(array of encoded pairs containing
   1739  1.1     skrll 				 FDE initial_location field and FDE address,
   1740  1.1     skrll 				 sorted by increasing initial_loc).  */
   1741  1.1     skrll 
   1742  1.1     skrll bfd_boolean
   1743  1.1     skrll _bfd_elf_write_section_eh_frame_hdr (bfd *abfd, struct bfd_link_info *info)
   1744  1.1     skrll {
   1745  1.1     skrll   struct elf_link_hash_table *htab;
   1746  1.1     skrll   struct eh_frame_hdr_info *hdr_info;
   1747  1.1     skrll   asection *sec;
   1748  1.1     skrll   bfd_byte *contents;
   1749  1.1     skrll   asection *eh_frame_sec;
   1750  1.1     skrll   bfd_size_type size;
   1751  1.1     skrll   bfd_boolean retval;
   1752  1.1     skrll   bfd_vma encoded_eh_frame;
   1753  1.1     skrll 
   1754  1.1     skrll   htab = elf_hash_table (info);
   1755  1.1     skrll   hdr_info = &htab->eh_info;
   1756  1.1     skrll   sec = hdr_info->hdr_sec;
   1757  1.1     skrll   if (sec == NULL)
   1758  1.1     skrll     return TRUE;
   1759  1.1     skrll 
   1760  1.1     skrll   size = EH_FRAME_HDR_SIZE;
   1761  1.1     skrll   if (hdr_info->array && hdr_info->array_count == hdr_info->fde_count)
   1762  1.1     skrll     size += 4 + hdr_info->fde_count * 8;
   1763  1.3  christos   contents = (bfd_byte *) bfd_malloc (size);
   1764  1.1     skrll   if (contents == NULL)
   1765  1.1     skrll     return FALSE;
   1766  1.1     skrll 
   1767  1.1     skrll   eh_frame_sec = bfd_get_section_by_name (abfd, ".eh_frame");
   1768  1.1     skrll   if (eh_frame_sec == NULL)
   1769  1.1     skrll     {
   1770  1.1     skrll       free (contents);
   1771  1.1     skrll       return FALSE;
   1772  1.1     skrll     }
   1773  1.1     skrll 
   1774  1.1     skrll   memset (contents, 0, EH_FRAME_HDR_SIZE);
   1775  1.1     skrll   contents[0] = 1;				/* Version.  */
   1776  1.1     skrll   contents[1] = get_elf_backend_data (abfd)->elf_backend_encode_eh_address
   1777  1.1     skrll     (abfd, info, eh_frame_sec, 0, sec, 4,
   1778  1.1     skrll      &encoded_eh_frame);			/* .eh_frame offset.  */
   1779  1.1     skrll 
   1780  1.1     skrll   if (hdr_info->array && hdr_info->array_count == hdr_info->fde_count)
   1781  1.1     skrll     {
   1782  1.1     skrll       contents[2] = DW_EH_PE_udata4;		/* FDE count encoding.  */
   1783  1.1     skrll       contents[3] = DW_EH_PE_datarel | DW_EH_PE_sdata4; /* Search table enc.  */
   1784  1.1     skrll     }
   1785  1.1     skrll   else
   1786  1.1     skrll     {
   1787  1.1     skrll       contents[2] = DW_EH_PE_omit;
   1788  1.1     skrll       contents[3] = DW_EH_PE_omit;
   1789  1.1     skrll     }
   1790  1.1     skrll   bfd_put_32 (abfd, encoded_eh_frame, contents + 4);
   1791  1.1     skrll 
   1792  1.1     skrll   if (contents[2] != DW_EH_PE_omit)
   1793  1.1     skrll     {
   1794  1.1     skrll       unsigned int i;
   1795  1.1     skrll 
   1796  1.1     skrll       bfd_put_32 (abfd, hdr_info->fde_count, contents + EH_FRAME_HDR_SIZE);
   1797  1.1     skrll       qsort (hdr_info->array, hdr_info->fde_count, sizeof (*hdr_info->array),
   1798  1.1     skrll 	     vma_compare);
   1799  1.1     skrll       for (i = 0; i < hdr_info->fde_count; i++)
   1800  1.1     skrll 	{
   1801  1.1     skrll 	  bfd_put_32 (abfd,
   1802  1.1     skrll 		      hdr_info->array[i].initial_loc
   1803  1.1     skrll 		      - sec->output_section->vma,
   1804  1.1     skrll 		      contents + EH_FRAME_HDR_SIZE + i * 8 + 4);
   1805  1.1     skrll 	  bfd_put_32 (abfd,
   1806  1.1     skrll 		      hdr_info->array[i].fde - sec->output_section->vma,
   1807  1.1     skrll 		      contents + EH_FRAME_HDR_SIZE + i * 8 + 8);
   1808  1.1     skrll 	}
   1809  1.1     skrll     }
   1810  1.1     skrll 
   1811  1.3  christos   /* FIXME: octets_per_byte.  */
   1812  1.1     skrll   retval = bfd_set_section_contents (abfd, sec->output_section,
   1813  1.1     skrll 				     contents, (file_ptr) sec->output_offset,
   1814  1.1     skrll 				     sec->size);
   1815  1.1     skrll   free (contents);
   1816  1.1     skrll   return retval;
   1817  1.1     skrll }
   1818  1.1     skrll 
   1819  1.1     skrll /* Return the width of FDE addresses.  This is the default implementation.  */
   1820  1.1     skrll 
   1821  1.1     skrll unsigned int
   1822  1.1     skrll _bfd_elf_eh_frame_address_size (bfd *abfd, asection *sec ATTRIBUTE_UNUSED)
   1823  1.1     skrll {
   1824  1.1     skrll   return elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64 ? 8 : 4;
   1825  1.1     skrll }
   1826  1.1     skrll 
   1827  1.1     skrll /* Decide whether we can use a PC-relative encoding within the given
   1828  1.1     skrll    EH frame section.  This is the default implementation.  */
   1829  1.1     skrll 
   1830  1.1     skrll bfd_boolean
   1831  1.1     skrll _bfd_elf_can_make_relative (bfd *input_bfd ATTRIBUTE_UNUSED,
   1832  1.1     skrll 			    struct bfd_link_info *info ATTRIBUTE_UNUSED,
   1833  1.1     skrll 			    asection *eh_frame_section ATTRIBUTE_UNUSED)
   1834  1.1     skrll {
   1835  1.1     skrll   return TRUE;
   1836  1.1     skrll }
   1837  1.1     skrll 
   1838  1.1     skrll /* Select an encoding for the given address.  Preference is given to
   1839  1.1     skrll    PC-relative addressing modes.  */
   1840  1.1     skrll 
   1841  1.1     skrll bfd_byte
   1842  1.1     skrll _bfd_elf_encode_eh_address (bfd *abfd ATTRIBUTE_UNUSED,
   1843  1.1     skrll 			    struct bfd_link_info *info ATTRIBUTE_UNUSED,
   1844  1.1     skrll 			    asection *osec, bfd_vma offset,
   1845  1.1     skrll 			    asection *loc_sec, bfd_vma loc_offset,
   1846  1.1     skrll 			    bfd_vma *encoded)
   1847  1.1     skrll {
   1848  1.1     skrll   *encoded = osec->vma + offset -
   1849  1.1     skrll     (loc_sec->output_section->vma + loc_sec->output_offset + loc_offset);
   1850  1.1     skrll   return DW_EH_PE_pcrel | DW_EH_PE_sdata4;
   1851  1.1     skrll }
   1852