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