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