s/boolean/bfd_boolean/ s/true/TRUE/ s/false/FALSE/. Simplify
[external/binutils.git] / bfd / elf-eh-frame.c
1 /* .eh_frame section optimization.
2    Copyright 2001, 2002 Free Software Foundation, Inc.
3    Written by Jakub Jelinek <jakub@redhat.com>.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20
21 #include "bfd.h"
22 #include "sysdep.h"
23 #include "libbfd.h"
24 #include "elf-bfd.h"
25 #include "elf/dwarf2.h"
26
27 #define EH_FRAME_HDR_SIZE 8
28
29 static bfd_vma read_unsigned_leb128
30   PARAMS ((bfd *, char *, unsigned int *));
31 static bfd_signed_vma read_signed_leb128
32   PARAMS ((bfd *, char *, unsigned int *));
33 static int get_DW_EH_PE_width
34   PARAMS ((int, int));
35 static bfd_vma read_value
36   PARAMS ((bfd *, bfd_byte *, int));
37 static void write_value
38   PARAMS ((bfd *, bfd_byte *, bfd_vma, int));
39 static int cie_compare
40   PARAMS ((struct cie *, struct cie *));
41 static int vma_compare
42   PARAMS ((const PTR a, const PTR b));
43
44 /* Helper function for reading uleb128 encoded data.  */
45
46 static bfd_vma
47 read_unsigned_leb128 (abfd, buf, bytes_read_ptr)
48      bfd *abfd ATTRIBUTE_UNUSED;
49      char *buf;
50      unsigned int *bytes_read_ptr;
51 {
52   bfd_vma  result;
53   unsigned int  num_read;
54   int           shift;
55   unsigned char byte;
56
57   result   = 0;
58   shift    = 0;
59   num_read = 0;
60   do
61     {
62       byte = bfd_get_8 (abfd, (bfd_byte *) buf);
63       buf ++;
64       num_read ++;
65       result |= (((bfd_vma) byte & 0x7f) << shift);
66       shift += 7;
67     }
68   while (byte & 0x80);
69   * bytes_read_ptr = num_read;
70   return result;
71 }
72
73 /* Helper function for reading sleb128 encoded data.  */
74
75 static bfd_signed_vma
76 read_signed_leb128 (abfd, buf, bytes_read_ptr)
77      bfd *abfd ATTRIBUTE_UNUSED;
78      char *buf;
79      unsigned int * bytes_read_ptr;
80 {
81   bfd_vma       result;
82   int           shift;
83   int           num_read;
84   unsigned char byte;
85
86   result = 0;
87   shift = 0;
88   num_read = 0;
89   do
90     {
91       byte = bfd_get_8 (abfd, (bfd_byte *) buf);
92       buf ++;
93       num_read ++;
94       result |= (((bfd_vma) byte & 0x7f) << shift);
95       shift += 7;
96     }
97   while (byte & 0x80);
98   if (byte & 0x40)
99     result |= (((bfd_vma) -1) << (shift - 7)) << 7;
100   * bytes_read_ptr = num_read;
101   return result;
102 }
103
104 #define read_uleb128(VAR, BUF)                                  \
105 do                                                              \
106   {                                                             \
107     (VAR) = read_unsigned_leb128 (abfd, buf, &leb128_tmp);      \
108     (BUF) += leb128_tmp;                                        \
109   }                                                             \
110 while (0)
111
112 #define read_sleb128(VAR, BUF)                                  \
113 do                                                              \
114   {                                                             \
115     (VAR) = read_signed_leb128 (abfd, buf, &leb128_tmp);        \
116     (BUF) += leb128_tmp;                                        \
117   }                                                             \
118 while (0)
119
120 /* Return 0 if either encoding is variable width, or not yet known to bfd.  */
121
122 static
123 int get_DW_EH_PE_width (encoding, ptr_size)
124      int encoding, ptr_size;
125 {
126   /* DW_EH_PE_ values of 0x60 and 0x70 weren't defined at the time .eh_frame
127      was added to bfd.  */
128   if ((encoding & 0x60) == 0x60)
129     return 0;
130
131   switch (encoding & 7)
132     {
133     case DW_EH_PE_udata2: return 2;
134     case DW_EH_PE_udata4: return 4;
135     case DW_EH_PE_udata8: return 8;
136     case DW_EH_PE_absptr: return ptr_size;
137     default:
138       break;
139     }
140
141   return 0;
142 }
143
144 /* Read a width sized value from memory.  */
145
146 static bfd_vma
147 read_value (abfd, buf, width)
148      bfd *abfd;
149      bfd_byte *buf;
150      int width;
151 {
152   bfd_vma value;
153
154   switch (width)
155     {
156     case 2: value = bfd_get_16 (abfd, buf); break;
157     case 4: value = bfd_get_32 (abfd, buf); break;
158     case 8: value = bfd_get_64 (abfd, buf); break;
159     default: BFD_FAIL (); return 0;
160     }
161
162   return value;
163 }
164
165 /* Store a width sized value to memory.  */
166
167 static void
168 write_value (abfd, buf, value, width)
169      bfd *abfd;
170      bfd_byte *buf;
171      bfd_vma value;
172      int width;
173 {
174   switch (width)
175     {
176     case 2: bfd_put_16 (abfd, value, buf); break;
177     case 4: bfd_put_32 (abfd, value, buf); break;
178     case 8: bfd_put_64 (abfd, value, buf); break;
179     default: BFD_FAIL ();
180     }
181 }
182
183 /* Return zero if C1 and C2 CIEs can be merged.  */
184
185 static
186 int cie_compare (c1, c2)
187      struct cie *c1, *c2;
188 {
189   if (c1->hdr.length == c2->hdr.length
190       && c1->version == c2->version
191       && strcmp (c1->augmentation, c2->augmentation) == 0
192       && strcmp (c1->augmentation, "eh") != 0
193       && c1->code_align == c2->code_align
194       && c1->data_align == c2->data_align
195       && c1->ra_column == c2->ra_column
196       && c1->augmentation_size == c2->augmentation_size
197       && c1->personality == c2->personality
198       && c1->per_encoding == c2->per_encoding
199       && c1->lsda_encoding == c2->lsda_encoding
200       && c1->fde_encoding == c2->fde_encoding
201       && (c1->initial_insn_length
202           == c2->initial_insn_length)
203       && memcmp (c1->initial_instructions,
204                  c2->initial_instructions,
205                  c1->initial_insn_length) == 0)
206     return 0;
207
208   return 1;
209 }
210
211 /* This function is called for each input file before the .eh_frame
212    section is relocated.  It discards duplicate CIEs and FDEs for discarded
213    functions.  The function returns TRUE iff any entries have been
214    deleted.  */
215
216 bfd_boolean
217 _bfd_elf_discard_section_eh_frame (abfd, info, sec,
218                                    reloc_symbol_deleted_p, cookie)
219      bfd *abfd;
220      struct bfd_link_info *info;
221      asection *sec;
222      bfd_boolean (*reloc_symbol_deleted_p) PARAMS ((bfd_vma, PTR));
223      struct elf_reloc_cookie *cookie;
224 {
225   bfd_byte *ehbuf = NULL, *buf;
226   bfd_byte *last_cie, *last_fde;
227   struct cie_header hdr;
228   struct cie cie;
229   struct elf_link_hash_table *htab;
230   struct eh_frame_hdr_info *hdr_info;
231   struct eh_frame_sec_info *sec_info = NULL;
232   unsigned int leb128_tmp;
233   unsigned int cie_usage_count, last_cie_ndx, i, offset;
234   unsigned int make_relative, make_lsda_relative;
235   bfd_size_type new_size;
236   unsigned int ptr_size;
237
238   if (sec->_raw_size == 0)
239     {
240       /* This file does not contain .eh_frame information.  */
241       return FALSE;
242     }
243
244   if ((sec->output_section != NULL
245        && bfd_is_abs_section (sec->output_section)))
246     {
247       /* At least one of the sections is being discarded from the
248          link, so we should just ignore them.  */
249       return FALSE;
250     }
251
252   htab = elf_hash_table (info);
253   hdr_info = &htab->eh_info;
254
255   /* Read the frame unwind information from abfd.  */
256
257   ehbuf = (bfd_byte *) bfd_malloc (sec->_raw_size);
258   if (ehbuf == NULL)
259     goto free_no_table;
260
261   if (! bfd_get_section_contents (abfd, sec, ehbuf, (bfd_vma) 0,
262                                   sec->_raw_size))
263     goto free_no_table;
264
265   if (sec->_raw_size >= 4
266       && bfd_get_32 (abfd, ehbuf) == 0
267       && cookie->rel == cookie->relend)
268     {
269       /* Empty .eh_frame section.  */
270       free (ehbuf);
271       return FALSE;
272     }
273
274   /* If .eh_frame section size doesn't fit into int, we cannot handle
275      it (it would need to use 64-bit .eh_frame format anyway).  */
276   if (sec->_raw_size != (unsigned int) sec->_raw_size)
277     goto free_no_table;
278
279   ptr_size = (elf_elfheader (abfd)->e_ident[EI_CLASS]
280               == ELFCLASS64) ? 8 : 4;
281   buf = ehbuf;
282   last_cie = NULL;
283   last_cie_ndx = 0;
284   memset (&cie, 0, sizeof (cie));
285   cie_usage_count = 0;
286   new_size = sec->_raw_size;
287   make_relative = hdr_info->last_cie.make_relative;
288   make_lsda_relative = hdr_info->last_cie.make_lsda_relative;
289   sec_info = bfd_zmalloc (sizeof (struct eh_frame_sec_info)
290                           + 99 * sizeof (struct eh_cie_fde));
291   if (sec_info == NULL)
292     goto free_no_table;
293   sec_info->alloced = 100;
294
295 #define ENSURE_NO_RELOCS(buf)                           \
296   if (cookie->rel < cookie->relend                      \
297       && (cookie->rel->r_offset                         \
298           < (bfd_size_type) ((buf) - ehbuf))            \
299       && cookie->rel->r_info != 0)                      \
300     goto free_no_table
301
302 #define SKIP_RELOCS(buf)                                \
303   while (cookie->rel < cookie->relend                   \
304          && (cookie->rel->r_offset                      \
305              < (bfd_size_type) ((buf) - ehbuf)))        \
306     cookie->rel++
307
308 #define GET_RELOC(buf)                                  \
309   ((cookie->rel < cookie->relend                        \
310     && (cookie->rel->r_offset                           \
311         == (bfd_size_type) ((buf) - ehbuf)))            \
312    ? cookie->rel : NULL)
313
314   for (;;)
315     {
316       unsigned char *aug;
317
318       if (sec_info->count == sec_info->alloced)
319         {
320           sec_info = bfd_realloc (sec_info,
321                                   sizeof (struct eh_frame_sec_info)
322                                   + (sec_info->alloced + 99)
323                                      * sizeof (struct eh_cie_fde));
324           if (sec_info == NULL)
325             goto free_no_table;
326
327           memset (&sec_info->entry[sec_info->alloced], 0,
328                   100 * sizeof (struct eh_cie_fde));
329           sec_info->alloced += 100;
330         }
331
332       last_fde = buf;
333       /* If we are at the end of the section, we still need to decide
334          on whether to output or discard last encountered CIE (if any).  */
335       if ((bfd_size_type) (buf - ehbuf) == sec->_raw_size)
336         hdr.id = (unsigned int) -1;
337       else
338         {
339           if ((bfd_size_type) (buf + 4 - ehbuf) > sec->_raw_size)
340             /* No space for CIE/FDE header length.  */
341             goto free_no_table;
342
343           hdr.length = bfd_get_32 (abfd, buf);
344           if (hdr.length == 0xffffffff)
345             /* 64-bit .eh_frame is not supported.  */
346             goto free_no_table;
347           buf += 4;
348           if ((bfd_size_type) (buf - ehbuf) + hdr.length > sec->_raw_size)
349             /* CIE/FDE not contained fully in this .eh_frame input section.  */
350             goto free_no_table;
351
352           sec_info->entry[sec_info->count].offset = last_fde - ehbuf;
353           sec_info->entry[sec_info->count].size = 4 + hdr.length;
354
355           if (hdr.length == 0)
356             {
357               /* CIE with length 0 must be only the last in the section.  */
358               if ((bfd_size_type) (buf - ehbuf) < sec->_raw_size)
359                 goto free_no_table;
360               ENSURE_NO_RELOCS (buf);
361               sec_info->count++;
362               /* Now just finish last encountered CIE processing and break
363                  the loop.  */
364               hdr.id = (unsigned int) -1;
365             }
366           else
367             {
368               hdr.id = bfd_get_32 (abfd, buf);
369               buf += 4;
370               if (hdr.id == (unsigned int) -1)
371                 goto free_no_table;
372             }
373         }
374
375       if (hdr.id == 0 || hdr.id == (unsigned int) -1)
376         {
377           unsigned int initial_insn_length;
378
379           /* CIE  */
380           if (last_cie != NULL)
381             {
382               /* Now check if this CIE is identical to the last CIE,
383                  in which case we can remove it provided we adjust
384                  all FDEs.  Also, it can be removed if we have removed
385                  all FDEs using it.  */
386               if ((!info->relocateable
387                    && cie_compare (&cie, &hdr_info->last_cie) == 0)
388                   || cie_usage_count == 0)
389                 {
390                   new_size -= cie.hdr.length + 4;
391                   sec_info->entry[last_cie_ndx].removed = 1;
392                   sec_info->entry[last_cie_ndx].sec = hdr_info->last_cie_sec;
393                   sec_info->entry[last_cie_ndx].new_offset
394                     = hdr_info->last_cie_offset;
395                 }
396               else
397                 {
398                   hdr_info->last_cie = cie;
399                   hdr_info->last_cie_sec = sec;
400                   hdr_info->last_cie_offset = last_cie - ehbuf;
401                   sec_info->entry[last_cie_ndx].make_relative
402                     = cie.make_relative;
403                   sec_info->entry[last_cie_ndx].make_lsda_relative
404                     = cie.make_lsda_relative;
405                   sec_info->entry[last_cie_ndx].per_encoding_relative
406                     = (cie.per_encoding & 0x70) == DW_EH_PE_pcrel;
407                 }
408             }
409
410           if (hdr.id == (unsigned int) -1)
411             break;
412
413           last_cie_ndx = sec_info->count;
414           sec_info->entry[sec_info->count].cie = 1;
415
416           cie_usage_count = 0;
417           memset (&cie, 0, sizeof (cie));
418           cie.hdr = hdr;
419           cie.version = *buf++;
420
421           /* Cannot handle unknown versions.  */
422           if (cie.version != 1)
423             goto free_no_table;
424           if (strlen (buf) > sizeof (cie.augmentation) - 1)
425             goto free_no_table;
426
427           strcpy (cie.augmentation, buf);
428           buf = strchr (buf, '\0') + 1;
429           ENSURE_NO_RELOCS (buf);
430           if (buf[0] == 'e' && buf[1] == 'h')
431             {
432               /* GCC < 3.0 .eh_frame CIE */
433               /* We cannot merge "eh" CIEs because __EXCEPTION_TABLE__
434                  is private to each CIE, so we don't need it for anything.
435                  Just skip it.  */
436               buf += ptr_size;
437               SKIP_RELOCS (buf);
438             }
439           read_uleb128 (cie.code_align, buf);
440           read_sleb128 (cie.data_align, buf);
441           /* Note - in DWARF2 the return address column is an unsigned byte.
442              In DWARF3 it is a ULEB128.  We are following DWARF3.  For most
443              ports this will not matter as the value will be less than 128.
444              For the others (eg FRV, SH, MMIX, IA64) they need a fixed GCC
445              which conforms to the DWARF3 standard.  */
446           read_uleb128 (cie.ra_column, buf);
447           ENSURE_NO_RELOCS (buf);
448           cie.lsda_encoding = DW_EH_PE_omit;
449           cie.fde_encoding = DW_EH_PE_omit;
450           cie.per_encoding = DW_EH_PE_omit;
451           aug = cie.augmentation;
452           if (aug[0] != 'e' || aug[1] != 'h')
453             {
454               if (*aug == 'z')
455                 {
456                   aug++;
457                   read_uleb128 (cie.augmentation_size, buf);
458                   ENSURE_NO_RELOCS (buf);
459                 }
460
461               while (*aug != '\0')
462                 switch (*aug++)
463                   {
464                   case 'L':
465                     cie.lsda_encoding = *buf++;
466                     ENSURE_NO_RELOCS (buf);
467                     if (get_DW_EH_PE_width (cie.lsda_encoding, ptr_size) == 0)
468                       goto free_no_table;
469                     break;
470                   case 'R':
471                     cie.fde_encoding = *buf++;
472                     ENSURE_NO_RELOCS (buf);
473                     if (get_DW_EH_PE_width (cie.fde_encoding, ptr_size) == 0)
474                       goto free_no_table;
475                     break;
476                   case 'P':
477                     {
478                       int per_width;
479
480                       cie.per_encoding = *buf++;
481                       per_width = get_DW_EH_PE_width (cie.per_encoding,
482                                                       ptr_size);
483                       if (per_width == 0)
484                         goto free_no_table;
485                       if ((cie.per_encoding & 0xf0) == DW_EH_PE_aligned)
486                         buf = (ehbuf
487                                + ((buf - ehbuf + per_width - 1)
488                                   & ~((bfd_size_type) per_width - 1)));
489                       ENSURE_NO_RELOCS (buf);
490                       /* Ensure we have a reloc here, against
491                          a global symbol.  */
492                       if (GET_RELOC (buf) != NULL)
493                         {
494                           unsigned long r_symndx;
495
496 #ifdef BFD64
497                           if (ptr_size == 8)
498                             r_symndx = ELF64_R_SYM (cookie->rel->r_info);
499                           else
500 #endif
501                             r_symndx = ELF32_R_SYM (cookie->rel->r_info);
502                           if (r_symndx >= cookie->locsymcount)
503                             {
504                               struct elf_link_hash_entry *h;
505
506                               r_symndx -= cookie->extsymoff;
507                               h = cookie->sym_hashes[r_symndx];
508
509                               while (h->root.type == bfd_link_hash_indirect
510                                      || h->root.type == bfd_link_hash_warning)
511                                 h = (struct elf_link_hash_entry *)
512                                     h->root.u.i.link;
513
514                               cie.personality = h;
515                             }
516                           cookie->rel++;
517                         }
518                       buf += per_width;
519                     }
520                     break;
521                   default:
522                     /* Unrecognized augmentation. Better bail out.  */
523                     goto free_no_table;
524                   }
525             }
526
527           /* For shared libraries, try to get rid of as many RELATIVE relocs
528              as possible.  */
529           if (info->shared
530               && (cie.fde_encoding & 0xf0) == DW_EH_PE_absptr)
531             cie.make_relative = 1;
532
533           if (info->shared
534               && (cie.lsda_encoding & 0xf0) == DW_EH_PE_absptr)
535             cie.make_lsda_relative = 1;
536
537           /* If FDE encoding was not specified, it defaults to
538              DW_EH_absptr.  */
539           if (cie.fde_encoding == DW_EH_PE_omit)
540             cie.fde_encoding = DW_EH_PE_absptr;
541
542           initial_insn_length = cie.hdr.length - (buf - last_fde - 4);
543           if (initial_insn_length <= 50)
544             {
545               cie.initial_insn_length = initial_insn_length;
546               memcpy (cie.initial_instructions, buf, initial_insn_length);
547             }
548           buf += initial_insn_length;
549           ENSURE_NO_RELOCS (buf);
550           last_cie = last_fde;
551         }
552       else
553         {
554           /* Ensure this FDE uses the last CIE encountered.  */
555           if (last_cie == NULL
556               || hdr.id != (unsigned int) (buf - 4 - last_cie))
557             goto free_no_table;
558
559           ENSURE_NO_RELOCS (buf);
560           if (GET_RELOC (buf) == NULL)
561             /* This should not happen.  */
562             goto free_no_table;
563           if ((*reloc_symbol_deleted_p) (buf - ehbuf, cookie))
564             {
565               /* This is a FDE against a discarded section.  It should
566                  be deleted.  */
567               new_size -= hdr.length + 4;
568               sec_info->entry[sec_info->count].removed = 1;
569             }
570           else
571             {
572               if (info->shared
573                   && (((cie.fde_encoding & 0xf0) == DW_EH_PE_absptr
574                        && cie.make_relative == 0)
575                       || (cie.fde_encoding & 0xf0) == DW_EH_PE_aligned))
576                 {
577                   /* If a shared library uses absolute pointers
578                      which we cannot turn into PC relative,
579                      don't create the binary search table,
580                      since it is affected by runtime relocations.  */
581                   hdr_info->table = FALSE;
582                 }
583               cie_usage_count++;
584               hdr_info->fde_count++;
585             }
586           if (cie.lsda_encoding != DW_EH_PE_omit)
587             {
588               unsigned int dummy;
589
590               aug = buf;
591               buf += 2 * get_DW_EH_PE_width (cie.fde_encoding, ptr_size);
592               if (cie.augmentation[0] == 'z')
593                 read_uleb128 (dummy, buf);
594               /* If some new augmentation data is added before LSDA
595                  in FDE augmentation area, this need to be adjusted.  */
596               sec_info->entry[sec_info->count].lsda_offset = (buf - aug);
597             }
598           buf = last_fde + 4 + hdr.length;
599           SKIP_RELOCS (buf);
600         }
601
602       sec_info->entry[sec_info->count].fde_encoding = cie.fde_encoding;
603       sec_info->entry[sec_info->count].lsda_encoding = cie.lsda_encoding;
604       sec_info->count++;
605     }
606
607   elf_section_data (sec)->sec_info = sec_info;
608   elf_section_data (sec)->sec_info_type = ELF_INFO_TYPE_EH_FRAME;
609
610   /* Ok, now we can assign new offsets.  */
611   offset = 0;
612   last_cie_ndx = 0;
613   for (i = 0; i < sec_info->count; i++)
614     {
615       if (! sec_info->entry[i].removed)
616         {
617           sec_info->entry[i].new_offset = offset;
618           offset += sec_info->entry[i].size;
619           if (sec_info->entry[i].cie)
620             {
621               last_cie_ndx = i;
622               make_relative = sec_info->entry[i].make_relative;
623               make_lsda_relative = sec_info->entry[i].make_lsda_relative;
624             }
625           else
626             {
627               sec_info->entry[i].make_relative = make_relative;
628               sec_info->entry[i].make_lsda_relative = make_lsda_relative;
629               sec_info->entry[i].per_encoding_relative = 0;
630             }
631         }
632       else if (sec_info->entry[i].cie && sec_info->entry[i].sec == sec)
633         {
634           /* Need to adjust new_offset too.  */
635           BFD_ASSERT (sec_info->entry[last_cie_ndx].offset
636                       == sec_info->entry[i].new_offset);
637           sec_info->entry[i].new_offset
638             = sec_info->entry[last_cie_ndx].new_offset;
639         }
640     }
641   if (hdr_info->last_cie_sec == sec)
642     {
643       BFD_ASSERT (sec_info->entry[last_cie_ndx].offset
644                   == hdr_info->last_cie_offset);
645       hdr_info->last_cie_offset = sec_info->entry[last_cie_ndx].new_offset;
646     }
647
648   /* FIXME: Currently it is not possible to shrink sections to zero size at
649      this point, so build a fake minimal CIE.  */
650   if (new_size == 0)
651     new_size = 16;
652
653   /* Shrink the sec as needed.  */
654   sec->_cooked_size = new_size;
655   if (sec->_cooked_size == 0)
656     sec->flags |= SEC_EXCLUDE;
657
658   free (ehbuf);
659   return new_size != sec->_raw_size;
660
661 free_no_table:
662   if (ehbuf)
663     free (ehbuf);
664   if (sec_info)
665     free (sec_info);
666   hdr_info->table = FALSE;
667   hdr_info->last_cie.hdr.length = 0;
668   return FALSE;
669 }
670
671 /* This function is called for .eh_frame_hdr section after
672    _bfd_elf_discard_section_eh_frame has been called on all .eh_frame
673    input sections.  It finalizes the size of .eh_frame_hdr section.  */
674
675 bfd_boolean
676 _bfd_elf_discard_section_eh_frame_hdr (abfd, info)
677      bfd *abfd;
678      struct bfd_link_info *info;
679 {
680   struct elf_link_hash_table *htab;
681   struct eh_frame_hdr_info *hdr_info;
682   asection *sec;
683
684   htab = elf_hash_table (info);
685   hdr_info = &htab->eh_info;
686   sec = hdr_info->hdr_sec;
687   if (sec == NULL)
688     return FALSE;
689
690   sec->_cooked_size = EH_FRAME_HDR_SIZE;
691   if (hdr_info->table)
692     sec->_cooked_size += 4 + hdr_info->fde_count * 8;
693
694   /* Request program headers to be recalculated.  */
695   elf_tdata (abfd)->program_header_size = 0;
696   elf_tdata (abfd)->eh_frame_hdr = sec;
697   return TRUE;
698 }
699
700 /* This function is called from size_dynamic_sections.
701    It needs to decide whether .eh_frame_hdr should be output or not,
702    because later on it is too late for calling _bfd_strip_section_from_output,
703    since dynamic symbol table has been sized.  */
704
705 bfd_boolean
706 _bfd_elf_maybe_strip_eh_frame_hdr (info)
707      struct bfd_link_info *info;
708 {
709   asection *o;
710   bfd *abfd;
711   struct elf_link_hash_table *htab;
712   struct eh_frame_hdr_info *hdr_info;
713
714   htab = elf_hash_table (info);
715   hdr_info = &htab->eh_info;
716   if (hdr_info->hdr_sec == NULL)
717     return TRUE;
718
719   if (bfd_is_abs_section (hdr_info->hdr_sec->output_section))
720     {
721       hdr_info->hdr_sec = NULL;
722       return TRUE;
723     }
724
725   abfd = NULL;
726   if (info->eh_frame_hdr)
727     for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
728       {
729         /* Count only sections which have at least a single CIE or FDE.
730            There cannot be any CIE or FDE <= 8 bytes.  */
731         o = bfd_get_section_by_name (abfd, ".eh_frame");
732         if (o && o->_raw_size > 8 && !bfd_is_abs_section (o->output_section))
733           break;
734       }
735
736   if (abfd == NULL)
737     {
738       _bfd_strip_section_from_output (info, hdr_info->hdr_sec);
739       hdr_info->hdr_sec = NULL;
740       return TRUE;
741     }
742
743   hdr_info->table = TRUE;
744   return TRUE;
745 }
746
747 /* Adjust an address in the .eh_frame section.  Given OFFSET within
748    SEC, this returns the new offset in the adjusted .eh_frame section,
749    or -1 if the address refers to a CIE/FDE which has been removed
750    or to offset with dynamic relocation which is no longer needed.  */
751
752 bfd_vma
753 _bfd_elf_eh_frame_section_offset (output_bfd, sec, offset)
754      bfd *output_bfd ATTRIBUTE_UNUSED;
755      asection *sec;
756      bfd_vma offset;
757 {
758   struct eh_frame_sec_info *sec_info;
759   unsigned int lo, hi, mid;
760
761   if (elf_section_data (sec)->sec_info_type != ELF_INFO_TYPE_EH_FRAME)
762     return offset;
763   sec_info = (struct eh_frame_sec_info *)
764              elf_section_data (sec)->sec_info;
765
766   if (offset >= sec->_raw_size)
767     return offset - (sec->_cooked_size - sec->_raw_size);
768
769   lo = 0;
770   hi = sec_info->count;
771   mid = 0;
772   while (lo < hi)
773     {
774       mid = (lo + hi) / 2;
775       if (offset < sec_info->entry[mid].offset)
776         hi = mid;
777       else if (offset
778                >= sec_info->entry[mid].offset + sec_info->entry[mid].size)
779         lo = mid + 1;
780       else
781         break;
782     }
783
784   BFD_ASSERT (lo < hi);
785
786   /* FDE or CIE was removed.  */
787   if (sec_info->entry[mid].removed)
788     return (bfd_vma) -1;
789
790   /* If converting to DW_EH_PE_pcrel, there will be no need for run-time
791      relocation against FDE's initial_location field.  */
792   if (sec_info->entry[mid].make_relative
793       && ! sec_info->entry[mid].cie
794       && offset == sec_info->entry[mid].offset + 8)
795     return (bfd_vma) -2;
796
797   /* If converting LSDA pointers to DW_EH_PE_pcrel, there will be no need
798      for run-time relocation against LSDA field.  */
799   if (sec_info->entry[mid].make_lsda_relative
800       && ! sec_info->entry[mid].cie
801       && (offset == (sec_info->entry[mid].offset + 8
802                      + sec_info->entry[mid].lsda_offset)))
803     return (bfd_vma) -2;
804
805   return (offset + sec_info->entry[mid].new_offset
806           - sec_info->entry[mid].offset);
807 }
808
809 /* Write out .eh_frame section.  This is called with the relocated
810    contents.  */
811
812 bfd_boolean
813 _bfd_elf_write_section_eh_frame (abfd, info, sec, contents)
814      bfd *abfd;
815      struct bfd_link_info *info;
816      asection *sec;
817      bfd_byte *contents;
818 {
819   struct eh_frame_sec_info *sec_info;
820   struct elf_link_hash_table *htab;
821   struct eh_frame_hdr_info *hdr_info;
822   unsigned int i;
823   bfd_byte *p, *buf;
824   unsigned int leb128_tmp;
825   unsigned int cie_offset = 0;
826   unsigned int ptr_size;
827
828   ptr_size = (elf_elfheader (sec->owner)->e_ident[EI_CLASS]
829               == ELFCLASS64) ? 8 : 4;
830
831   if (elf_section_data (sec)->sec_info_type != ELF_INFO_TYPE_EH_FRAME)
832     return bfd_set_section_contents (abfd, sec->output_section,
833                                      contents,
834                                      (file_ptr) sec->output_offset,
835                                      sec->_raw_size);
836   sec_info = (struct eh_frame_sec_info *)
837              elf_section_data (sec)->sec_info;
838   htab = elf_hash_table (info);
839   hdr_info = &htab->eh_info;
840   if (hdr_info->table && hdr_info->array == NULL)
841     hdr_info->array
842       = bfd_malloc (hdr_info->fde_count * sizeof(*hdr_info->array));
843   if (hdr_info->array == NULL)
844     hdr_info = NULL;
845
846   p = contents;
847   for (i = 0; i < sec_info->count; ++i)
848     {
849       if (sec_info->entry[i].removed)
850         {
851           if (sec_info->entry[i].cie)
852             {
853               /* If CIE is removed due to no remaining FDEs referencing it
854                  and there were no CIEs kept before it, sec_info->entry[i].sec
855                  will be zero.  */
856               if (sec_info->entry[i].sec == NULL)
857                 cie_offset = 0;
858               else
859                 {
860                   cie_offset = sec_info->entry[i].new_offset;
861                   cie_offset += (sec_info->entry[i].sec->output_section->vma
862                                  + sec_info->entry[i].sec->output_offset
863                                  - sec->output_section->vma
864                                  - sec->output_offset);
865                 }
866             }
867           continue;
868         }
869
870       if (sec_info->entry[i].cie)
871         {
872           /* CIE */
873           cie_offset = sec_info->entry[i].new_offset;
874           if (sec_info->entry[i].make_relative
875               || sec_info->entry[i].make_lsda_relative
876               || sec_info->entry[i].per_encoding_relative)
877             {
878               unsigned char *aug;
879               unsigned int action;
880               unsigned int dummy, per_width, per_encoding;
881
882               /* Need to find 'R' or 'L' augmentation's argument and modify
883                  DW_EH_PE_* value.  */
884               action = (sec_info->entry[i].make_relative ? 1 : 0)
885                        | (sec_info->entry[i].make_lsda_relative ? 2 : 0)
886                        | (sec_info->entry[i].per_encoding_relative ? 4 : 0);
887               buf = contents + sec_info->entry[i].offset;
888               /* Skip length, id and version.  */
889               buf += 9;
890               aug = buf;
891               buf = strchr (buf, '\0') + 1;
892               read_uleb128 (dummy, buf);
893               read_sleb128 (dummy, buf);
894               read_uleb128 (dummy, buf);
895               if (*aug == 'z')
896                 {
897                   read_uleb128 (dummy, buf);
898                   aug++;
899                 }
900
901               while (action)
902                 switch (*aug++)
903                   {
904                   case 'L':
905                     if (action & 2)
906                       {
907                         BFD_ASSERT (*buf == sec_info->entry[i].lsda_encoding);
908                         *buf |= DW_EH_PE_pcrel;
909                         action &= ~2;
910                       }
911                     buf++;
912                     break;
913                   case 'P':
914                     per_encoding = *buf++;
915                     per_width = get_DW_EH_PE_width (per_encoding,
916                                                     ptr_size);
917                     BFD_ASSERT (per_width != 0);
918                     BFD_ASSERT (((per_encoding & 0x70) == DW_EH_PE_pcrel)
919                                 == sec_info->entry[i].per_encoding_relative);
920                     if ((per_encoding & 0xf0) == DW_EH_PE_aligned)
921                       buf = (contents
922                              + ((buf - contents + per_width - 1)
923                                 & ~((bfd_size_type) per_width - 1)));
924                     if (action & 4)
925                       {
926                         bfd_vma value;
927
928                         value = read_value (abfd, buf, per_width);
929                         value += (sec_info->entry[i].offset
930                                   - sec_info->entry[i].new_offset);
931                         write_value (abfd, buf, value, per_width);
932                         action &= ~4;
933                       }
934                     buf += per_width;
935                     break;
936                   case 'R':
937                     if (action & 1)
938                       {
939                         BFD_ASSERT (*buf == sec_info->entry[i].fde_encoding);
940                         *buf |= DW_EH_PE_pcrel;
941                         action &= ~1;
942                       }
943                     buf++;
944                     break;
945                   default:
946                     BFD_FAIL ();
947                   }
948             }
949         }
950       else if (sec_info->entry[i].size > 4)
951         {
952           /* FDE */
953           bfd_vma value = 0, address;
954           unsigned int width;
955
956           buf = contents + sec_info->entry[i].offset;
957           /* Skip length.  */
958           buf += 4;
959           bfd_put_32 (abfd,
960                       sec_info->entry[i].new_offset + 4 - cie_offset, buf);
961           buf += 4;
962           width = get_DW_EH_PE_width (sec_info->entry[i].fde_encoding,
963                                       ptr_size);
964           address = value = read_value (abfd, buf, width);
965           if (value)
966             {
967               switch (sec_info->entry[i].fde_encoding & 0xf0)
968                 {
969                 case DW_EH_PE_indirect:
970                 case DW_EH_PE_textrel:
971                   BFD_ASSERT (hdr_info == NULL);
972                   break;
973                 case DW_EH_PE_datarel:
974                   {
975                     asection *got = bfd_get_section_by_name (abfd, ".got");
976
977                     BFD_ASSERT (got != NULL);
978                     address += got->vma;
979                   }
980                   break;
981                 case DW_EH_PE_pcrel:
982                   value += (sec_info->entry[i].offset
983                             - sec_info->entry[i].new_offset);
984                   address += (sec->output_section->vma + sec->output_offset
985                               + sec_info->entry[i].offset + 8);
986                   break;
987                 }
988               if (sec_info->entry[i].make_relative)
989                 value -= (sec->output_section->vma + sec->output_offset
990                           + sec_info->entry[i].new_offset + 8);
991               write_value (abfd, buf, value, width);
992             }
993
994           if (hdr_info)
995             {
996               hdr_info->array[hdr_info->array_count].initial_loc = address;
997               hdr_info->array[hdr_info->array_count++].fde
998                 = (sec->output_section->vma + sec->output_offset
999                    + sec_info->entry[i].new_offset);
1000             }
1001
1002           if ((sec_info->entry[i].lsda_encoding & 0xf0) == DW_EH_PE_pcrel
1003               || sec_info->entry[i].make_lsda_relative)
1004             {
1005               buf += sec_info->entry[i].lsda_offset;
1006               width = get_DW_EH_PE_width (sec_info->entry[i].lsda_encoding,
1007                                           ptr_size);
1008               value = read_value (abfd, buf, width);
1009               if (value)
1010                 {
1011                   if ((sec_info->entry[i].lsda_encoding & 0xf0)
1012                       == DW_EH_PE_pcrel)
1013                     value += (sec_info->entry[i].offset
1014                               - sec_info->entry[i].new_offset);
1015                   else if (sec_info->entry[i].make_lsda_relative)
1016                     value -= (sec->output_section->vma + sec->output_offset
1017                               + sec_info->entry[i].new_offset + 8
1018                               + sec_info->entry[i].lsda_offset);
1019                   write_value (abfd, buf, value, width);
1020                 }
1021             }
1022         }
1023       else
1024         /* Terminating FDE must be at the end of .eh_frame section only.  */
1025         BFD_ASSERT (i == sec_info->count - 1);
1026
1027       BFD_ASSERT (p == contents + sec_info->entry[i].new_offset);
1028       memmove (p, contents + sec_info->entry[i].offset,
1029                sec_info->entry[i].size);
1030       p += sec_info->entry[i].size;
1031     }
1032
1033   /* FIXME: Once _bfd_elf_discard_section_eh_frame will be able to
1034      shrink sections to zero size, this won't be needed any more.  */
1035   if (p == contents && sec->_cooked_size == 16)
1036     {
1037       bfd_put_32 (abfd, 12, p);         /* Fake CIE length */
1038       bfd_put_32 (abfd, 0, p + 4);      /* Fake CIE id */
1039       p[8] = 1;                         /* Fake CIE version */
1040       memset (p + 9, 0, 7);             /* Fake CIE augmentation, 3xleb128
1041                                            and 3xDW_CFA_nop as pad  */
1042       p += 16;
1043     }
1044
1045   BFD_ASSERT ((bfd_size_type) (p - contents) == sec->_cooked_size);
1046
1047   return bfd_set_section_contents (abfd, sec->output_section,
1048                                    contents, (file_ptr) sec->output_offset,
1049                                    sec->_cooked_size);
1050 }
1051
1052 /* Helper function used to sort .eh_frame_hdr search table by increasing
1053    VMA of FDE initial location.  */
1054
1055 static int
1056 vma_compare (a, b)
1057      const PTR a;
1058      const PTR b;
1059 {
1060   struct eh_frame_array_ent *p = (struct eh_frame_array_ent *) a;
1061   struct eh_frame_array_ent *q = (struct eh_frame_array_ent *) b;
1062   if (p->initial_loc > q->initial_loc)
1063     return 1;
1064   if (p->initial_loc < q->initial_loc)
1065     return -1;
1066   return 0;
1067 }
1068
1069 /* Write out .eh_frame_hdr section.  This must be called after
1070    _bfd_elf_write_section_eh_frame has been called on all input
1071    .eh_frame sections.
1072    .eh_frame_hdr format:
1073    ubyte version                (currently 1)
1074    ubyte eh_frame_ptr_enc       (DW_EH_PE_* encoding of pointer to start of
1075                                  .eh_frame section)
1076    ubyte fde_count_enc          (DW_EH_PE_* encoding of total FDE count
1077                                  number (or DW_EH_PE_omit if there is no
1078                                  binary search table computed))
1079    ubyte table_enc              (DW_EH_PE_* encoding of binary search table,
1080                                  or DW_EH_PE_omit if not present.
1081                                  DW_EH_PE_datarel is using address of
1082                                  .eh_frame_hdr section start as base)
1083    [encoded] eh_frame_ptr       (pointer to start of .eh_frame section)
1084    optionally followed by:
1085    [encoded] fde_count          (total number of FDEs in .eh_frame section)
1086    fde_count x [encoded] initial_loc, fde
1087                                 (array of encoded pairs containing
1088                                  FDE initial_location field and FDE address,
1089                                  sorted by increasing initial_loc)  */
1090
1091 bfd_boolean
1092 _bfd_elf_write_section_eh_frame_hdr (abfd, info)
1093      bfd *abfd;
1094      struct bfd_link_info *info;
1095 {
1096   struct elf_link_hash_table *htab;
1097   struct eh_frame_hdr_info *hdr_info;
1098   asection *sec;
1099   bfd_byte *contents;
1100   asection *eh_frame_sec;
1101   bfd_size_type size;
1102
1103   htab = elf_hash_table (info);
1104   hdr_info = &htab->eh_info;
1105   sec = hdr_info->hdr_sec;
1106   if (sec == NULL)
1107     return TRUE;
1108
1109   size = EH_FRAME_HDR_SIZE;
1110   if (hdr_info->array && hdr_info->array_count == hdr_info->fde_count)
1111     size += 4 + hdr_info->fde_count * 8;
1112   contents = bfd_malloc (size);
1113   if (contents == NULL)
1114     return FALSE;
1115
1116   eh_frame_sec = bfd_get_section_by_name (abfd, ".eh_frame");
1117   if (eh_frame_sec == NULL)
1118     return FALSE;
1119
1120   memset (contents, 0, EH_FRAME_HDR_SIZE);
1121   contents[0] = 1;                              /* Version  */
1122   contents[1] = DW_EH_PE_pcrel | DW_EH_PE_sdata4; /* .eh_frame offset  */
1123   if (hdr_info->array && hdr_info->array_count == hdr_info->fde_count)
1124     {
1125       contents[2] = DW_EH_PE_udata4;            /* FDE count encoding  */
1126       contents[3] = DW_EH_PE_datarel | DW_EH_PE_sdata4; /* search table enc  */
1127     }
1128   else
1129     {
1130       contents[2] = DW_EH_PE_omit;
1131       contents[3] = DW_EH_PE_omit;
1132     }
1133   bfd_put_32 (abfd, eh_frame_sec->vma - sec->output_section->vma - 4,
1134               contents + 4);
1135   if (contents[2] != DW_EH_PE_omit)
1136     {
1137       unsigned int i;
1138
1139       bfd_put_32 (abfd, hdr_info->fde_count, contents + EH_FRAME_HDR_SIZE);
1140       qsort (hdr_info->array, hdr_info->fde_count, sizeof (*hdr_info->array),
1141              vma_compare);
1142       for (i = 0; i < hdr_info->fde_count; i++)
1143         {
1144           bfd_put_32 (abfd,
1145                       hdr_info->array[i].initial_loc
1146                       - sec->output_section->vma,
1147                       contents + EH_FRAME_HDR_SIZE + i * 8 + 4);
1148           bfd_put_32 (abfd,
1149                       hdr_info->array[i].fde - sec->output_section->vma,
1150                       contents + EH_FRAME_HDR_SIZE + i * 8 + 8);
1151         }
1152     }
1153
1154   return bfd_set_section_contents (abfd, sec->output_section,
1155                                    contents, (file_ptr) sec->output_offset,
1156                                    sec->_cooked_size);
1157 }