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