* elf-eh-frame.c (skip_cfa_op, skip_non_nops): New functions.
[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 = (elf_elfheader (abfd)->e_ident[EI_CLASS]
422               == ELFCLASS64) ? 8 : 4;
423   buf = ehbuf;
424   last_cie = NULL;
425   last_cie_inf = NULL;
426   memset (&cie, 0, sizeof (cie));
427   cie_usage_count = 0;
428   sec_info = bfd_zmalloc (sizeof (struct eh_frame_sec_info)
429                           + 99 * sizeof (struct eh_cie_fde));
430   REQUIRE (sec_info);
431
432   sec_info->alloced = 100;
433
434 #define ENSURE_NO_RELOCS(buf)                           \
435   REQUIRE (!(cookie->rel < cookie->relend               \
436              && (cookie->rel->r_offset                  \
437                  < (bfd_size_type) ((buf) - ehbuf))     \
438              && cookie->rel->r_info != 0))
439
440 #define SKIP_RELOCS(buf)                                \
441   while (cookie->rel < cookie->relend                   \
442          && (cookie->rel->r_offset                      \
443              < (bfd_size_type) ((buf) - ehbuf)))        \
444     cookie->rel++
445
446 #define GET_RELOC(buf)                                  \
447   ((cookie->rel < cookie->relend                        \
448     && (cookie->rel->r_offset                           \
449         == (bfd_size_type) ((buf) - ehbuf)))            \
450    ? cookie->rel : NULL)
451
452   for (;;)
453     {
454       unsigned char *aug;
455       bfd_byte *start, *end, *insns;
456       bfd_size_type length;
457
458       if (sec_info->count == sec_info->alloced)
459         {
460           struct eh_cie_fde *old_entry = sec_info->entry;
461           sec_info = bfd_realloc (sec_info,
462                                   sizeof (struct eh_frame_sec_info)
463                                   + ((sec_info->alloced + 99)
464                                      * sizeof (struct eh_cie_fde)));
465           REQUIRE (sec_info);
466
467           memset (&sec_info->entry[sec_info->alloced], 0,
468                   100 * sizeof (struct eh_cie_fde));
469           sec_info->alloced += 100;
470
471           /* Now fix any pointers into the array.  */
472           if (last_cie_inf >= old_entry
473               && last_cie_inf < old_entry + sec_info->count)
474             last_cie_inf = sec_info->entry + (last_cie_inf - old_entry);
475         }
476
477       this_inf = sec_info->entry + sec_info->count;
478       last_fde = buf;
479       /* If we are at the end of the section, we still need to decide
480          on whether to output or discard last encountered CIE (if any).  */
481       if ((bfd_size_type) (buf - ehbuf) == sec->size)
482         {
483           hdr.id = (unsigned int) -1;
484           end = buf;
485         }
486       else
487         {
488           /* Read the length of the entry.  */
489           REQUIRE (skip_bytes (&buf, ehbuf + sec->size, 4));
490           hdr.length = bfd_get_32 (abfd, buf - 4);
491
492           /* 64-bit .eh_frame is not supported.  */
493           REQUIRE (hdr.length != 0xffffffff);
494
495           /* The CIE/FDE must be fully contained in this input section.  */
496           REQUIRE ((bfd_size_type) (buf - ehbuf) + hdr.length <= sec->size);
497           end = buf + hdr.length;
498
499           this_inf->offset = last_fde - ehbuf;
500           this_inf->size = 4 + hdr.length;
501
502           if (hdr.length == 0)
503             {
504               /* A zero-length CIE should only be found at the end of
505                  the section.  */
506               REQUIRE ((bfd_size_type) (buf - ehbuf) == sec->size);
507               ENSURE_NO_RELOCS (buf);
508               sec_info->count++;
509               /* Now just finish last encountered CIE processing and break
510                  the loop.  */
511               hdr.id = (unsigned int) -1;
512             }
513           else
514             {
515               REQUIRE (skip_bytes (&buf, end, 4));
516               hdr.id = bfd_get_32 (abfd, buf - 4);
517               REQUIRE (hdr.id != (unsigned int) -1);
518             }
519         }
520
521       if (hdr.id == 0 || hdr.id == (unsigned int) -1)
522         {
523           unsigned int initial_insn_length;
524
525           /* CIE  */
526           if (last_cie != NULL)
527             {
528               /* Now check if this CIE is identical to the last CIE,
529                  in which case we can remove it provided we adjust
530                  all FDEs.  Also, it can be removed if we have removed
531                  all FDEs using it.  */
532               if ((!info->relocatable
533                    && hdr_info->last_cie_sec
534                    && (sec->output_section
535                        == hdr_info->last_cie_sec->output_section)
536                    && cie_compare (&cie, &hdr_info->last_cie) == 0)
537                   || cie_usage_count == 0)
538                 last_cie_inf->removed = 1;
539               else
540                 {
541                   hdr_info->last_cie = cie;
542                   hdr_info->last_cie_sec = sec;
543                   last_cie_inf->make_relative = cie.make_relative;
544                   last_cie_inf->make_lsda_relative = cie.make_lsda_relative;
545                   last_cie_inf->per_encoding_relative
546                     = (cie.per_encoding & 0x70) == DW_EH_PE_pcrel;
547                 }
548             }
549
550           if (hdr.id == (unsigned int) -1)
551             break;
552
553           last_cie_inf = this_inf;
554           this_inf->cie = 1;
555
556           cie_usage_count = 0;
557           memset (&cie, 0, sizeof (cie));
558           cie.hdr = hdr;
559           REQUIRE (read_byte (&buf, end, &cie.version));
560
561           /* Cannot handle unknown versions.  */
562           REQUIRE (cie.version == 1 || cie.version == 3);
563           REQUIRE (strlen (buf) < sizeof (cie.augmentation));
564
565           strcpy (cie.augmentation, buf);
566           buf = strchr (buf, '\0') + 1;
567           ENSURE_NO_RELOCS (buf);
568           if (buf[0] == 'e' && buf[1] == 'h')
569             {
570               /* GCC < 3.0 .eh_frame CIE */
571               /* We cannot merge "eh" CIEs because __EXCEPTION_TABLE__
572                  is private to each CIE, so we don't need it for anything.
573                  Just skip it.  */
574               REQUIRE (skip_bytes (&buf, end, ptr_size));
575               SKIP_RELOCS (buf);
576             }
577           REQUIRE (read_uleb128 (&buf, end, &cie.code_align));
578           REQUIRE (read_sleb128 (&buf, end, &cie.data_align));
579           if (cie.version == 1)
580             {
581               REQUIRE (buf < end);
582               cie.ra_column = *buf++;
583             }
584           else
585             REQUIRE (read_uleb128 (&buf, end, &cie.ra_column));
586           ENSURE_NO_RELOCS (buf);
587           cie.lsda_encoding = DW_EH_PE_omit;
588           cie.fde_encoding = DW_EH_PE_omit;
589           cie.per_encoding = DW_EH_PE_omit;
590           aug = cie.augmentation;
591           if (aug[0] != 'e' || aug[1] != 'h')
592             {
593               if (*aug == 'z')
594                 {
595                   aug++;
596                   REQUIRE (read_uleb128 (&buf, end, &cie.augmentation_size));
597                   ENSURE_NO_RELOCS (buf);
598                 }
599
600               while (*aug != '\0')
601                 switch (*aug++)
602                   {
603                   case 'L':
604                     REQUIRE (read_byte (&buf, end, &cie.lsda_encoding));
605                     ENSURE_NO_RELOCS (buf);
606                     REQUIRE (get_DW_EH_PE_width (cie.lsda_encoding, ptr_size));
607                     break;
608                   case 'R':
609                     REQUIRE (read_byte (&buf, end, &cie.fde_encoding));
610                     ENSURE_NO_RELOCS (buf);
611                     REQUIRE (get_DW_EH_PE_width (cie.fde_encoding, ptr_size));
612                     break;
613                   case 'P':
614                     {
615                       int per_width;
616
617                       REQUIRE (read_byte (&buf, end, &cie.per_encoding));
618                       per_width = get_DW_EH_PE_width (cie.per_encoding,
619                                                       ptr_size);
620                       REQUIRE (per_width);
621                       if ((cie.per_encoding & 0xf0) == DW_EH_PE_aligned)
622                         {
623                           length = -(buf - ehbuf) & (per_width - 1);
624                           REQUIRE (skip_bytes (&buf, end, length));
625                         }
626                       ENSURE_NO_RELOCS (buf);
627                       /* Ensure we have a reloc here, against
628                          a global symbol.  */
629                       if (GET_RELOC (buf) != NULL)
630                         {
631                           unsigned long r_symndx;
632
633 #ifdef BFD64
634                           if (ptr_size == 8)
635                             r_symndx = ELF64_R_SYM (cookie->rel->r_info);
636                           else
637 #endif
638                             r_symndx = ELF32_R_SYM (cookie->rel->r_info);
639                           if (r_symndx >= cookie->locsymcount)
640                             {
641                               struct elf_link_hash_entry *h;
642
643                               r_symndx -= cookie->extsymoff;
644                               h = cookie->sym_hashes[r_symndx];
645
646                               while (h->root.type == bfd_link_hash_indirect
647                                      || h->root.type == bfd_link_hash_warning)
648                                 h = (struct elf_link_hash_entry *)
649                                     h->root.u.i.link;
650
651                               cie.personality = h;
652                             }
653                           /* Cope with MIPS-style composite relocations.  */
654                           do
655                             cookie->rel++;
656                           while (GET_RELOC (buf) != NULL);
657                         }
658                       REQUIRE (skip_bytes (&buf, end, per_width));
659                     }
660                     break;
661                   default:
662                     /* Unrecognized augmentation. Better bail out.  */
663                     goto free_no_table;
664                   }
665             }
666
667           /* For shared libraries, try to get rid of as many RELATIVE relocs
668              as possible.  */
669           if (info->shared
670               && (get_elf_backend_data (abfd)
671                   ->elf_backend_can_make_relative_eh_frame
672                   (abfd, info, sec)))
673             {
674               if ((cie.fde_encoding & 0xf0) == DW_EH_PE_absptr)
675                 cie.make_relative = 1;
676               /* If the CIE doesn't already have an 'R' entry, it's fairly
677                  easy to add one, provided that there's no aligned data
678                  after the augmentation string.  */
679               else if (cie.fde_encoding == DW_EH_PE_omit
680                        && (cie.per_encoding & 0xf0) != DW_EH_PE_aligned)
681                 {
682                   if (*cie.augmentation == 0)
683                     this_inf->add_augmentation_size = 1;
684                   this_inf->add_fde_encoding = 1;
685                   cie.make_relative = 1;
686                 }
687             }
688
689           if (info->shared
690               && (get_elf_backend_data (abfd)
691                   ->elf_backend_can_make_lsda_relative_eh_frame
692                   (abfd, info, sec))
693               && (cie.lsda_encoding & 0xf0) == DW_EH_PE_absptr)
694             cie.make_lsda_relative = 1;
695
696           /* If FDE encoding was not specified, it defaults to
697              DW_EH_absptr.  */
698           if (cie.fde_encoding == DW_EH_PE_omit)
699             cie.fde_encoding = DW_EH_PE_absptr;
700
701           initial_insn_length = end - buf;
702           if (initial_insn_length <= 50)
703             {
704               cie.initial_insn_length = initial_insn_length;
705               memcpy (cie.initial_instructions, buf, initial_insn_length);
706             }
707           insns = buf;
708           buf += initial_insn_length;
709           ENSURE_NO_RELOCS (buf);
710           last_cie = last_fde;
711         }
712       else
713         {
714           /* Ensure this FDE uses the last CIE encountered.  */
715           REQUIRE (last_cie);
716           REQUIRE (hdr.id == (unsigned int) (buf - 4 - last_cie));
717
718           ENSURE_NO_RELOCS (buf);
719           REQUIRE (GET_RELOC (buf));
720
721           if ((*reloc_symbol_deleted_p) (buf - ehbuf, cookie))
722             /* This is a FDE against a discarded section.  It should
723                be deleted.  */
724             this_inf->removed = 1;
725           else
726             {
727               if (info->shared
728                   && (((cie.fde_encoding & 0xf0) == DW_EH_PE_absptr
729                        && cie.make_relative == 0)
730                       || (cie.fde_encoding & 0xf0) == DW_EH_PE_aligned))
731                 {
732                   /* If a shared library uses absolute pointers
733                      which we cannot turn into PC relative,
734                      don't create the binary search table,
735                      since it is affected by runtime relocations.  */
736                   hdr_info->table = FALSE;
737                 }
738               cie_usage_count++;
739               hdr_info->fde_count++;
740             }
741           /* Skip the initial location and address range.  */
742           start = buf;
743           length = get_DW_EH_PE_width (cie.fde_encoding, ptr_size);
744           REQUIRE (skip_bytes (&buf, end, 2 * length));
745
746           /* Skip the augmentation size, if present.  */
747           if (cie.augmentation[0] == 'z')
748             REQUIRE (read_uleb128 (&buf, end, &length));
749           else
750             length = 0;
751
752           /* Of the supported augmentation characters above, only 'L'
753              adds augmentation data to the FDE.  This code would need to
754              be adjusted if any future augmentations do the same thing.  */
755           if (cie.lsda_encoding != DW_EH_PE_omit)
756             {
757               this_inf->lsda_offset = buf - start;
758               /* If there's no 'z' augmentation, we don't know where the
759                  CFA insns begin.  Assume no padding.  */
760               if (cie.augmentation[0] != 'z')
761                 length = end - buf;
762             }
763
764           /* Skip over the augmentation data.  */
765           REQUIRE (skip_bytes (&buf, end, length));
766           insns = buf;
767
768           buf = last_fde + 4 + hdr.length;
769           SKIP_RELOCS (buf);
770         }
771
772       /* Try to interpret the CFA instructions and find the first
773          padding nop.  Shrink this_inf's size so that it doesn't
774          including the padding.  */
775       length = get_DW_EH_PE_width (cie.fde_encoding, ptr_size);
776       insns = skip_non_nops (insns, end, length);
777       if (insns != 0)
778         this_inf->size -= end - insns;
779
780       this_inf->fde_encoding = cie.fde_encoding;
781       this_inf->lsda_encoding = cie.lsda_encoding;
782       sec_info->count++;
783     }
784
785   elf_section_data (sec)->sec_info = sec_info;
786   sec->sec_info_type = ELF_INFO_TYPE_EH_FRAME;
787
788   /* Ok, now we can assign new offsets.  */
789   offset = 0;
790   last_cie_inf = hdr_info->last_cie_inf;
791   for (ent = sec_info->entry; ent < sec_info->entry + sec_info->count; ++ent)
792     if (!ent->removed)
793       {
794         if (ent->cie)
795           last_cie_inf = ent;
796         else
797           ent->cie_inf = last_cie_inf;
798         ent->new_offset = offset;
799         offset += size_of_output_cie_fde (ent, ptr_size);
800       }
801   hdr_info->last_cie_inf = last_cie_inf;
802
803   /* Resize the sec as needed.  */
804   sec->rawsize = sec->size;
805   sec->size = offset;
806   if (sec->size == 0)
807     sec->flags |= SEC_EXCLUDE;
808
809   free (ehbuf);
810   return offset != sec->rawsize;
811
812 free_no_table:
813   if (ehbuf)
814     free (ehbuf);
815   if (sec_info)
816     free (sec_info);
817   hdr_info->table = FALSE;
818   hdr_info->last_cie.hdr.length = 0;
819   return FALSE;
820
821 #undef REQUIRE
822 }
823
824 /* This function is called for .eh_frame_hdr section after
825    _bfd_elf_discard_section_eh_frame has been called on all .eh_frame
826    input sections.  It finalizes the size of .eh_frame_hdr section.  */
827
828 bfd_boolean
829 _bfd_elf_discard_section_eh_frame_hdr (bfd *abfd, struct bfd_link_info *info)
830 {
831   struct elf_link_hash_table *htab;
832   struct eh_frame_hdr_info *hdr_info;
833   asection *sec;
834
835   htab = elf_hash_table (info);
836   hdr_info = &htab->eh_info;
837   sec = hdr_info->hdr_sec;
838   if (sec == NULL)
839     return FALSE;
840
841   sec->size = EH_FRAME_HDR_SIZE;
842   if (hdr_info->table)
843     sec->size += 4 + hdr_info->fde_count * 8;
844
845   /* Request program headers to be recalculated.  */
846   elf_tdata (abfd)->program_header_size = 0;
847   elf_tdata (abfd)->eh_frame_hdr = sec;
848   return TRUE;
849 }
850
851 /* This function is called from size_dynamic_sections.
852    It needs to decide whether .eh_frame_hdr should be output or not,
853    because later on it is too late for calling _bfd_strip_section_from_output,
854    since dynamic symbol table has been sized.  */
855
856 bfd_boolean
857 _bfd_elf_maybe_strip_eh_frame_hdr (struct bfd_link_info *info)
858 {
859   asection *o;
860   bfd *abfd;
861   struct elf_link_hash_table *htab;
862   struct eh_frame_hdr_info *hdr_info;
863
864   htab = elf_hash_table (info);
865   hdr_info = &htab->eh_info;
866   if (hdr_info->hdr_sec == NULL)
867     return TRUE;
868
869   if (bfd_is_abs_section (hdr_info->hdr_sec->output_section))
870     {
871       hdr_info->hdr_sec = NULL;
872       return TRUE;
873     }
874
875   abfd = NULL;
876   if (info->eh_frame_hdr)
877     for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
878       {
879         /* Count only sections which have at least a single CIE or FDE.
880            There cannot be any CIE or FDE <= 8 bytes.  */
881         o = bfd_get_section_by_name (abfd, ".eh_frame");
882         if (o && o->size > 8 && !bfd_is_abs_section (o->output_section))
883           break;
884       }
885
886   if (abfd == NULL)
887     {
888       _bfd_strip_section_from_output (info, hdr_info->hdr_sec);
889       hdr_info->hdr_sec = NULL;
890       return TRUE;
891     }
892
893   hdr_info->table = TRUE;
894   return TRUE;
895 }
896
897 /* Adjust an address in the .eh_frame section.  Given OFFSET within
898    SEC, this returns the new offset in the adjusted .eh_frame section,
899    or -1 if the address refers to a CIE/FDE which has been removed
900    or to offset with dynamic relocation which is no longer needed.  */
901
902 bfd_vma
903 _bfd_elf_eh_frame_section_offset (bfd *output_bfd ATTRIBUTE_UNUSED,
904                                   struct bfd_link_info *info,
905                                   asection *sec,
906                                   bfd_vma offset)
907 {
908   struct eh_frame_sec_info *sec_info;
909   struct elf_link_hash_table *htab;
910   struct eh_frame_hdr_info *hdr_info;
911   unsigned int lo, hi, mid;
912
913   if (sec->sec_info_type != ELF_INFO_TYPE_EH_FRAME)
914     return offset;
915   sec_info = elf_section_data (sec)->sec_info;
916
917   if (offset >= sec->rawsize)
918     return offset - sec->rawsize + sec->size;
919
920   htab = elf_hash_table (info);
921   hdr_info = &htab->eh_info;
922   if (hdr_info->offsets_adjusted)
923     offset += sec->output_offset;
924
925   lo = 0;
926   hi = sec_info->count;
927   mid = 0;
928   while (lo < hi)
929     {
930       mid = (lo + hi) / 2;
931       if (offset < sec_info->entry[mid].offset)
932         hi = mid;
933       else if (offset
934                >= sec_info->entry[mid].offset + sec_info->entry[mid].size)
935         lo = mid + 1;
936       else
937         break;
938     }
939
940   BFD_ASSERT (lo < hi);
941
942   /* FDE or CIE was removed.  */
943   if (sec_info->entry[mid].removed)
944     return (bfd_vma) -1;
945
946   /* If converting to DW_EH_PE_pcrel, there will be no need for run-time
947      relocation against FDE's initial_location field.  */
948   if (!sec_info->entry[mid].cie
949       && sec_info->entry[mid].cie_inf->make_relative
950       && offset == sec_info->entry[mid].offset + 8)
951     return (bfd_vma) -2;
952
953   /* If converting LSDA pointers to DW_EH_PE_pcrel, there will be no need
954      for run-time relocation against LSDA field.  */
955   if (!sec_info->entry[mid].cie
956       && sec_info->entry[mid].cie_inf->make_lsda_relative
957       && (offset == (sec_info->entry[mid].offset + 8
958                      + sec_info->entry[mid].lsda_offset))
959       && (sec_info->entry[mid].cie_inf->need_lsda_relative
960           || !hdr_info->offsets_adjusted))
961     {
962       sec_info->entry[mid].cie_inf->need_lsda_relative = 1;
963       return (bfd_vma) -2;
964     }
965
966   if (hdr_info->offsets_adjusted)
967     offset -= sec->output_offset;
968   /* Any new augmentation bytes go before the first relocation.  */
969   return (offset + sec_info->entry[mid].new_offset
970           - sec_info->entry[mid].offset
971           + extra_augmentation_string_bytes (sec_info->entry + mid)
972           + extra_augmentation_data_bytes (sec_info->entry + mid));
973 }
974
975 /* Write out .eh_frame section.  This is called with the relocated
976    contents.  */
977
978 bfd_boolean
979 _bfd_elf_write_section_eh_frame (bfd *abfd,
980                                  struct bfd_link_info *info,
981                                  asection *sec,
982                                  bfd_byte *contents)
983 {
984   struct eh_frame_sec_info *sec_info;
985   struct elf_link_hash_table *htab;
986   struct eh_frame_hdr_info *hdr_info;
987   unsigned int ptr_size;
988   struct eh_cie_fde *ent;
989
990   ptr_size = (elf_elfheader (sec->owner)->e_ident[EI_CLASS]
991               == ELFCLASS64) ? 8 : 4;
992
993   if (sec->sec_info_type != ELF_INFO_TYPE_EH_FRAME)
994     return bfd_set_section_contents (abfd, sec->output_section, contents,
995                                      sec->output_offset, sec->size);
996   sec_info = elf_section_data (sec)->sec_info;
997   htab = elf_hash_table (info);
998   hdr_info = &htab->eh_info;
999
1000   /* First convert all offsets to output section offsets, so that a
1001      CIE offset is valid if the CIE is used by a FDE from some other
1002      section.  This can happen when duplicate CIEs are deleted in
1003      _bfd_elf_discard_section_eh_frame.  We do all sections here because
1004      this function might not be called on sections in the same order as
1005      _bfd_elf_discard_section_eh_frame.  */
1006   if (!hdr_info->offsets_adjusted)
1007     {
1008       bfd *ibfd;
1009       asection *eh;
1010       struct eh_frame_sec_info *eh_inf;
1011
1012       for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
1013         {
1014           if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1015               || (ibfd->flags & DYNAMIC) != 0)
1016             continue;
1017
1018           eh = bfd_get_section_by_name (ibfd, ".eh_frame");
1019           if (eh == NULL || eh->sec_info_type != ELF_INFO_TYPE_EH_FRAME)
1020             continue;
1021
1022           eh_inf = elf_section_data (eh)->sec_info;
1023           for (ent = eh_inf->entry; ent < eh_inf->entry + eh_inf->count; ++ent)
1024             {
1025               ent->offset += eh->output_offset;
1026               ent->new_offset += eh->output_offset;
1027             }
1028         }
1029       hdr_info->offsets_adjusted = TRUE;
1030     }
1031
1032   if (hdr_info->table && hdr_info->array == NULL)
1033     hdr_info->array
1034       = bfd_malloc (hdr_info->fde_count * sizeof(*hdr_info->array));
1035   if (hdr_info->array == NULL)
1036     hdr_info = NULL;
1037
1038   /* The new offsets can be bigger or smaller than the original offsets.
1039      We therefore need to make two passes over the section: one backward
1040      pass to move entries up and one forward pass to move entries down.
1041      The two passes won't interfere with each other because entries are
1042      not reordered  */
1043   for (ent = sec_info->entry + sec_info->count; ent-- != sec_info->entry;)
1044     if (!ent->removed && ent->new_offset > ent->offset)
1045       memmove (contents + ent->new_offset - sec->output_offset,
1046                contents + ent->offset - sec->output_offset, ent->size);
1047
1048   for (ent = sec_info->entry; ent < sec_info->entry + sec_info->count; ++ent)
1049     if (!ent->removed && ent->new_offset < ent->offset)
1050       memmove (contents + ent->new_offset - sec->output_offset,
1051                contents + ent->offset - sec->output_offset, ent->size);
1052
1053   for (ent = sec_info->entry; ent < sec_info->entry + sec_info->count; ++ent)
1054     {
1055       unsigned char *buf, *end;
1056       unsigned int new_size;
1057
1058       if (ent->removed)
1059         continue;
1060
1061       if (ent->size == 4)
1062         {
1063           /* Any terminating FDE must be at the end of the section.  */
1064           BFD_ASSERT (ent == sec_info->entry + sec_info->count - 1);
1065           continue;
1066         }
1067
1068       buf = contents + ent->new_offset - sec->output_offset;
1069       end = buf + ent->size;
1070       new_size = size_of_output_cie_fde (ent, ptr_size);
1071
1072       /* Install the new size, filling the extra bytes with DW_CFA_nops.  */
1073       if (new_size != ent->size)
1074         {
1075           memset (end, 0, new_size - ent->size);
1076           bfd_put_32 (abfd, new_size - 4, buf);
1077         }
1078
1079       if (ent->cie)
1080         {
1081           /* CIE */
1082           if (ent->make_relative
1083               || ent->need_lsda_relative
1084               || ent->per_encoding_relative)
1085             {
1086               unsigned char *aug;
1087               unsigned int action, extra_string, extra_data;
1088               unsigned int per_width, per_encoding;
1089
1090               /* Need to find 'R' or 'L' augmentation's argument and modify
1091                  DW_EH_PE_* value.  */
1092               action = ((ent->make_relative ? 1 : 0)
1093                         | (ent->need_lsda_relative ? 2 : 0)
1094                         | (ent->per_encoding_relative ? 4 : 0));
1095               extra_string = extra_augmentation_string_bytes (ent);
1096               extra_data = extra_augmentation_data_bytes (ent);
1097
1098               /* Skip length, id and version.  */
1099               buf += 9;
1100               aug = buf;
1101               buf = strchr (buf, '\0') + 1;
1102               skip_leb128 (&buf, end);
1103               skip_leb128 (&buf, end);
1104               skip_leb128 (&buf, end);
1105               if (*aug == 'z')
1106                 {
1107                   /* The uleb128 will always be a single byte for the kind
1108                      of augmentation strings that we're prepared to handle.  */
1109                   *buf++ += extra_data;
1110                   aug++;
1111                 }
1112
1113               /* Make room for the new augmentation string and data bytes.  */
1114               memmove (buf + extra_string + extra_data, buf, end - buf);
1115               memmove (aug + extra_string, aug, buf - aug);
1116               buf += extra_string;
1117               end += extra_string + extra_data;
1118
1119               if (ent->add_augmentation_size)
1120                 {
1121                   *aug++ = 'z';
1122                   *buf++ = extra_data - 1;
1123                 }
1124               if (ent->add_fde_encoding)
1125                 {
1126                   BFD_ASSERT (action & 1);
1127                   *aug++ = 'R';
1128                   *buf++ = DW_EH_PE_pcrel;
1129                   action &= ~1;
1130                 }
1131
1132               while (action)
1133                 switch (*aug++)
1134                   {
1135                   case 'L':
1136                     if (action & 2)
1137                       {
1138                         BFD_ASSERT (*buf == ent->lsda_encoding);
1139                         *buf |= DW_EH_PE_pcrel;
1140                         action &= ~2;
1141                       }
1142                     buf++;
1143                     break;
1144                   case 'P':
1145                     per_encoding = *buf++;
1146                     per_width = get_DW_EH_PE_width (per_encoding, ptr_size);
1147                     BFD_ASSERT (per_width != 0);
1148                     BFD_ASSERT (((per_encoding & 0x70) == DW_EH_PE_pcrel)
1149                                 == ent->per_encoding_relative);
1150                     if ((per_encoding & 0xf0) == DW_EH_PE_aligned)
1151                       buf = (contents
1152                              + ((buf - contents + per_width - 1)
1153                                 & ~((bfd_size_type) per_width - 1)));
1154                     if (action & 4)
1155                       {
1156                         bfd_vma val;
1157
1158                         val = read_value (abfd, buf, per_width,
1159                                           get_DW_EH_PE_signed (per_encoding));
1160                         val += ent->offset - ent->new_offset;
1161                         val -= extra_string + extra_data;
1162                         write_value (abfd, buf, val, per_width);
1163                         action &= ~4;
1164                       }
1165                     buf += per_width;
1166                     break;
1167                   case 'R':
1168                     if (action & 1)
1169                       {
1170                         BFD_ASSERT (*buf == ent->fde_encoding);
1171                         *buf |= DW_EH_PE_pcrel;
1172                         action &= ~1;
1173                       }
1174                     buf++;
1175                     break;
1176                   default:
1177                     BFD_FAIL ();
1178                   }
1179             }
1180         }
1181       else
1182         {
1183           /* FDE */
1184           bfd_vma value, address;
1185           unsigned int width;
1186
1187           /* Skip length.  */
1188           buf += 4;
1189           value = ent->new_offset + 4 - ent->cie_inf->new_offset;
1190           bfd_put_32 (abfd, value, buf);
1191           buf += 4;
1192           width = get_DW_EH_PE_width (ent->fde_encoding, ptr_size);
1193           value = read_value (abfd, buf, width,
1194                               get_DW_EH_PE_signed (ent->fde_encoding));
1195           address = value;
1196           if (value)
1197             {
1198               switch (ent->fde_encoding & 0xf0)
1199                 {
1200                 case DW_EH_PE_indirect:
1201                 case DW_EH_PE_textrel:
1202                   BFD_ASSERT (hdr_info == NULL);
1203                   break;
1204                 case DW_EH_PE_datarel:
1205                   {
1206                     asection *got = bfd_get_section_by_name (abfd, ".got");
1207
1208                     BFD_ASSERT (got != NULL);
1209                     address += got->vma;
1210                   }
1211                   break;
1212                 case DW_EH_PE_pcrel:
1213                   value += ent->offset - ent->new_offset;
1214                   address += sec->output_section->vma + ent->offset + 8;
1215                   break;
1216                 }
1217               if (ent->cie_inf->make_relative)
1218                 value -= sec->output_section->vma + ent->new_offset + 8;
1219               write_value (abfd, buf, value, width);
1220             }
1221
1222           if (hdr_info)
1223             {
1224               hdr_info->array[hdr_info->array_count].initial_loc = address;
1225               hdr_info->array[hdr_info->array_count++].fde
1226                 = sec->output_section->vma + ent->new_offset;
1227             }
1228
1229           if ((ent->lsda_encoding & 0xf0) == DW_EH_PE_pcrel
1230               || ent->cie_inf->need_lsda_relative)
1231             {
1232               buf += ent->lsda_offset;
1233               width = get_DW_EH_PE_width (ent->lsda_encoding, ptr_size);
1234               value = read_value (abfd, buf, width,
1235                                   get_DW_EH_PE_signed (ent->lsda_encoding));
1236               if (value)
1237                 {
1238                   if ((ent->lsda_encoding & 0xf0) == DW_EH_PE_pcrel)
1239                     value += ent->offset - ent->new_offset;
1240                   else if (ent->cie_inf->need_lsda_relative)
1241                     value -= (sec->output_section->vma + ent->new_offset + 8
1242                               + ent->lsda_offset);
1243                   write_value (abfd, buf, value, width);
1244                 }
1245             }
1246           else if (ent->cie_inf->add_augmentation_size)
1247             {
1248               /* Skip the PC and length and insert a zero byte for the
1249                  augmentation size.  */
1250               buf += width * 2;
1251               memmove (buf + 1, buf, end - buf);
1252               *buf = 0;
1253             }
1254         }
1255     }
1256
1257     {
1258       unsigned int alignment = 1 << sec->alignment_power;
1259       unsigned int pad = sec->size % alignment;
1260
1261       /* Don't pad beyond the raw size of the output section. It
1262          can happen at the last input section.  */
1263       if (pad
1264           && ((sec->output_offset + sec->size + pad)
1265               <= sec->output_section->size))
1266         {
1267           bfd_byte *buf;
1268           unsigned int new_size;
1269
1270           /* Find the last CIE/FDE.  */
1271           ent = sec_info->entry + sec_info->count;
1272           while (--ent != sec_info->entry)
1273             if (!ent->removed)
1274               break;
1275
1276           /* The size of the last CIE/FDE must be at least 4.  */
1277           if (ent->removed || ent->size < 4)
1278             abort ();
1279
1280           pad = alignment - pad;
1281           buf = contents + ent->new_offset - sec->output_offset;
1282           new_size = size_of_output_cie_fde (ent, ptr_size);
1283
1284           /* Pad it with DW_CFA_nop  */
1285           memset (buf + new_size, 0, pad);
1286           bfd_put_32 (abfd, new_size + pad - 4, buf);
1287
1288           sec->size += pad;
1289         }
1290     }
1291
1292   return bfd_set_section_contents (abfd, sec->output_section,
1293                                    contents, (file_ptr) sec->output_offset,
1294                                    sec->size);
1295 }
1296
1297 /* Helper function used to sort .eh_frame_hdr search table by increasing
1298    VMA of FDE initial location.  */
1299
1300 static int
1301 vma_compare (const void *a, const void *b)
1302 {
1303   const struct eh_frame_array_ent *p = a;
1304   const struct eh_frame_array_ent *q = b;
1305   if (p->initial_loc > q->initial_loc)
1306     return 1;
1307   if (p->initial_loc < q->initial_loc)
1308     return -1;
1309   return 0;
1310 }
1311
1312 /* Write out .eh_frame_hdr section.  This must be called after
1313    _bfd_elf_write_section_eh_frame has been called on all input
1314    .eh_frame sections.
1315    .eh_frame_hdr format:
1316    ubyte version                (currently 1)
1317    ubyte eh_frame_ptr_enc       (DW_EH_PE_* encoding of pointer to start of
1318                                  .eh_frame section)
1319    ubyte fde_count_enc          (DW_EH_PE_* encoding of total FDE count
1320                                  number (or DW_EH_PE_omit if there is no
1321                                  binary search table computed))
1322    ubyte table_enc              (DW_EH_PE_* encoding of binary search table,
1323                                  or DW_EH_PE_omit if not present.
1324                                  DW_EH_PE_datarel is using address of
1325                                  .eh_frame_hdr section start as base)
1326    [encoded] eh_frame_ptr       (pointer to start of .eh_frame section)
1327    optionally followed by:
1328    [encoded] fde_count          (total number of FDEs in .eh_frame section)
1329    fde_count x [encoded] initial_loc, fde
1330                                 (array of encoded pairs containing
1331                                  FDE initial_location field and FDE address,
1332                                  sorted by increasing initial_loc).  */
1333
1334 bfd_boolean
1335 _bfd_elf_write_section_eh_frame_hdr (bfd *abfd, struct bfd_link_info *info)
1336 {
1337   struct elf_link_hash_table *htab;
1338   struct eh_frame_hdr_info *hdr_info;
1339   asection *sec;
1340   bfd_byte *contents;
1341   asection *eh_frame_sec;
1342   bfd_size_type size;
1343   bfd_boolean retval;
1344   bfd_vma encoded_eh_frame;
1345
1346   htab = elf_hash_table (info);
1347   hdr_info = &htab->eh_info;
1348   sec = hdr_info->hdr_sec;
1349   if (sec == NULL)
1350     return TRUE;
1351
1352   size = EH_FRAME_HDR_SIZE;
1353   if (hdr_info->array && hdr_info->array_count == hdr_info->fde_count)
1354     size += 4 + hdr_info->fde_count * 8;
1355   contents = bfd_malloc (size);
1356   if (contents == NULL)
1357     return FALSE;
1358
1359   eh_frame_sec = bfd_get_section_by_name (abfd, ".eh_frame");
1360   if (eh_frame_sec == NULL)
1361     {
1362       free (contents);
1363       return FALSE;
1364     }
1365
1366   memset (contents, 0, EH_FRAME_HDR_SIZE);
1367   contents[0] = 1;                              /* Version.  */
1368   contents[1] = get_elf_backend_data (abfd)->elf_backend_encode_eh_address
1369     (abfd, info, eh_frame_sec, 0, sec, 4,
1370      &encoded_eh_frame);                        /* .eh_frame offset.  */
1371
1372   if (hdr_info->array && hdr_info->array_count == hdr_info->fde_count)
1373     {
1374       contents[2] = DW_EH_PE_udata4;            /* FDE count encoding.  */
1375       contents[3] = DW_EH_PE_datarel | DW_EH_PE_sdata4; /* Search table enc.  */
1376     }
1377   else
1378     {
1379       contents[2] = DW_EH_PE_omit;
1380       contents[3] = DW_EH_PE_omit;
1381     }
1382   bfd_put_32 (abfd, encoded_eh_frame, contents + 4);
1383
1384   if (contents[2] != DW_EH_PE_omit)
1385     {
1386       unsigned int i;
1387
1388       bfd_put_32 (abfd, hdr_info->fde_count, contents + EH_FRAME_HDR_SIZE);
1389       qsort (hdr_info->array, hdr_info->fde_count, sizeof (*hdr_info->array),
1390              vma_compare);
1391       for (i = 0; i < hdr_info->fde_count; i++)
1392         {
1393           bfd_put_32 (abfd,
1394                       hdr_info->array[i].initial_loc
1395                       - sec->output_section->vma,
1396                       contents + EH_FRAME_HDR_SIZE + i * 8 + 4);
1397           bfd_put_32 (abfd,
1398                       hdr_info->array[i].fde - sec->output_section->vma,
1399                       contents + EH_FRAME_HDR_SIZE + i * 8 + 8);
1400         }
1401     }
1402
1403   retval = bfd_set_section_contents (abfd, sec->output_section,
1404                                      contents, (file_ptr) sec->output_offset,
1405                                      sec->size);
1406   free (contents);
1407   return retval;
1408 }
1409
1410 /* Decide whether we can use a PC-relative encoding within the given
1411    EH frame section.  This is the default implementation.  */
1412
1413 bfd_boolean
1414 _bfd_elf_can_make_relative (bfd *input_bfd ATTRIBUTE_UNUSED,
1415                             struct bfd_link_info *info ATTRIBUTE_UNUSED,
1416                             asection *eh_frame_section ATTRIBUTE_UNUSED)
1417 {
1418   return TRUE;
1419 }
1420
1421 /* Select an encoding for the given address.  Preference is given to
1422    PC-relative addressing modes.  */
1423
1424 bfd_byte
1425 _bfd_elf_encode_eh_address (bfd *abfd ATTRIBUTE_UNUSED,
1426                             struct bfd_link_info *info ATTRIBUTE_UNUSED,
1427                             asection *osec, bfd_vma offset,
1428                             asection *loc_sec, bfd_vma loc_offset,
1429                             bfd_vma *encoded)
1430 {
1431   *encoded = osec->vma + offset -
1432     (loc_sec->output_section->vma + loc_sec->output_offset + loc_offset);
1433   return DW_EH_PE_pcrel | DW_EH_PE_sdata4;
1434 }