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