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