Fix Boot Setup example:
[dragonfly/netmp.git] / contrib / binutils-2.17 / bfd / elf-eh-frame.c
blob5100e72938d93b946a68630960f651f264eccc80
1 /* .eh_frame section optimization.
2 Copyright 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
3 Written by Jakub Jelinek <jakub@redhat.com>.
5 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
21 #include "bfd.h"
22 #include "sysdep.h"
23 #include "libbfd.h"
24 #include "elf-bfd.h"
25 #include "elf/dwarf2.h"
27 #define EH_FRAME_HDR_SIZE 8
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. */
32 static inline bfd_boolean
33 read_byte (bfd_byte **iter, bfd_byte *end, unsigned char *result)
35 if (*iter >= end)
36 return FALSE;
37 *result = *((*iter)++);
38 return TRUE;
41 /* Move *ITER over LENGTH bytes, or up to END, whichever is closer.
42 Return true it was possible to move LENGTH bytes. */
44 static inline bfd_boolean
45 skip_bytes (bfd_byte **iter, bfd_byte *end, bfd_size_type length)
47 if ((bfd_size_type) (end - *iter) < length)
49 *iter = end;
50 return FALSE;
52 *iter += length;
53 return TRUE;
56 /* Move *ITER over an leb128, stopping at END. Return true if the end
57 of the leb128 was found. */
59 static bfd_boolean
60 skip_leb128 (bfd_byte **iter, bfd_byte *end)
62 unsigned char byte;
64 if (!read_byte (iter, end, &byte))
65 return FALSE;
66 while (byte & 0x80);
67 return TRUE;
70 /* Like skip_leb128, but treat the leb128 as an unsigned value and
71 store it in *VALUE. */
73 static bfd_boolean
74 read_uleb128 (bfd_byte **iter, bfd_byte *end, bfd_vma *value)
76 bfd_byte *start, *p;
78 start = *iter;
79 if (!skip_leb128 (iter, end))
80 return FALSE;
82 p = *iter;
83 *value = *--p;
84 while (p > start)
85 *value = (*value << 7) | (*--p & 0x7f);
87 return TRUE;
90 /* Like read_uleb128, but for signed values. */
92 static bfd_boolean
93 read_sleb128 (bfd_byte **iter, bfd_byte *end, bfd_signed_vma *value)
95 bfd_byte *start, *p;
97 start = *iter;
98 if (!skip_leb128 (iter, end))
99 return FALSE;
101 p = *iter;
102 *value = ((*--p & 0x7f) ^ 0x40) - 0x40;
103 while (p > start)
104 *value = (*value << 7) | (*--p & 0x7f);
106 return TRUE;
109 /* Return 0 if either encoding is variable width, or not yet known to bfd. */
111 static
112 int get_DW_EH_PE_width (int encoding, int ptr_size)
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;
119 switch (encoding & 7)
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;
129 return 0;
132 #define get_DW_EH_PE_signed(encoding) (((encoding) & DW_EH_PE_signed) != 0)
134 /* Read a width sized value from memory. */
136 static bfd_vma
137 read_value (bfd *abfd, bfd_byte *buf, int width, int is_signed)
139 bfd_vma value;
141 switch (width)
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;
166 return value;
169 /* Store a width sized value to memory. */
171 static void
172 write_value (bfd *abfd, bfd_byte *buf, bfd_vma value, int width)
174 switch (width)
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 ();
183 /* Return zero if C1 and C2 CIEs can be merged. */
185 static
186 int cie_compare (struct cie *c1, struct cie *c2)
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;
206 return 1;
209 /* Return the number of extra bytes that we'll be inserting into
210 ENTRY's augmentation string. */
212 static INLINE unsigned int
213 extra_augmentation_string_bytes (struct eh_cie_fde *entry)
215 unsigned int size = 0;
216 if (entry->cie)
218 if (entry->add_augmentation_size)
219 size++;
220 if (entry->add_fde_encoding)
221 size++;
223 return size;
226 /* Likewise ENTRY's augmentation data. */
228 static INLINE unsigned int
229 extra_augmentation_data_bytes (struct eh_cie_fde *entry)
231 unsigned int size = 0;
232 if (entry->cie)
234 if (entry->add_augmentation_size)
235 size++;
236 if (entry->add_fde_encoding)
237 size++;
239 else
241 if (entry->cie_inf->add_augmentation_size)
242 size++;
244 return size;
247 /* Return the size that ENTRY will have in the output. ALIGNMENT is the
248 required alignment of ENTRY in bytes. */
250 static unsigned int
251 size_of_output_cie_fde (struct eh_cie_fde *entry, unsigned int alignment)
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;
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. */
267 static bfd_boolean
268 skip_cfa_op (bfd_byte **iter, bfd_byte *end, unsigned int encoded_ptr_width)
270 bfd_byte op;
271 bfd_vma length;
273 if (!read_byte (iter, end, &op))
274 return FALSE;
276 switch (op & 0x80 ? op & 0xc0 : op)
278 case DW_CFA_nop:
279 case DW_CFA_advance_loc:
280 case DW_CFA_restore:
281 /* No arguments. */
282 return TRUE;
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);
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));
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));
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));
316 case DW_CFA_set_loc:
317 return skip_bytes (iter, end, encoded_ptr_width);
319 case DW_CFA_advance_loc1:
320 return skip_bytes (iter, end, 1);
322 case DW_CFA_advance_loc2:
323 return skip_bytes (iter, end, 2);
325 case DW_CFA_advance_loc4:
326 return skip_bytes (iter, end, 4);
328 case DW_CFA_MIPS_advance_loc8:
329 return skip_bytes (iter, end, 8);
331 default:
332 return FALSE;
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. */
341 static bfd_byte *
342 skip_non_nops (bfd_byte *buf, bfd_byte *end, unsigned int encoded_ptr_width)
344 bfd_byte *last;
346 last = buf;
347 while (buf < end)
348 if (*buf == DW_CFA_nop)
349 buf++;
350 else
352 if (!skip_cfa_op (&buf, end, encoded_ptr_width))
353 return 0;
354 last = buf;
356 return last;
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. */
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)
370 #define REQUIRE(COND) \
371 do \
372 if (!(COND)) \
373 goto free_no_table; \
374 while (0)
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;
387 if (sec->size == 0)
389 /* This file does not contain .eh_frame information. */
390 return FALSE;
393 if ((sec->output_section != NULL
394 && bfd_is_abs_section (sec->output_section)))
396 /* At least one of the sections is being discarded from the
397 link, so we should just ignore them. */
398 return FALSE;
401 htab = elf_hash_table (info);
402 hdr_info = &htab->eh_info;
404 /* Read the frame unwind information from abfd. */
406 REQUIRE (bfd_malloc_and_get_section (abfd, sec, &ehbuf));
408 if (sec->size >= 4
409 && bfd_get_32 (abfd, ehbuf) == 0
410 && cookie->rel == cookie->relend)
412 /* Empty .eh_frame section. */
413 free (ehbuf);
414 return FALSE;
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);
421 ptr_size = (get_elf_backend_data (abfd)
422 ->elf_backend_eh_frame_address_size (abfd, sec));
423 REQUIRE (ptr_size != 0);
425 buf = ehbuf;
426 last_cie = NULL;
427 last_cie_inf = NULL;
428 memset (&cie, 0, sizeof (cie));
429 cie_usage_count = 0;
430 sec_info = bfd_zmalloc (sizeof (struct eh_frame_sec_info)
431 + 99 * sizeof (struct eh_cie_fde));
432 REQUIRE (sec_info);
434 sec_info->alloced = 100;
436 #define ENSURE_NO_RELOCS(buf) \
437 REQUIRE (!(cookie->rel < cookie->relend \
438 && (cookie->rel->r_offset \
439 < (bfd_size_type) ((buf) - ehbuf)) \
440 && cookie->rel->r_info != 0))
442 #define SKIP_RELOCS(buf) \
443 while (cookie->rel < cookie->relend \
444 && (cookie->rel->r_offset \
445 < (bfd_size_type) ((buf) - ehbuf))) \
446 cookie->rel++
448 #define GET_RELOC(buf) \
449 ((cookie->rel < cookie->relend \
450 && (cookie->rel->r_offset \
451 == (bfd_size_type) ((buf) - ehbuf))) \
452 ? cookie->rel : NULL)
454 for (;;)
456 char *aug;
457 bfd_byte *start, *end, *insns;
458 bfd_size_type length;
460 if (sec_info->count == sec_info->alloced)
462 struct eh_cie_fde *old_entry = sec_info->entry;
463 sec_info = bfd_realloc (sec_info,
464 sizeof (struct eh_frame_sec_info)
465 + ((sec_info->alloced + 99)
466 * sizeof (struct eh_cie_fde)));
467 REQUIRE (sec_info);
469 memset (&sec_info->entry[sec_info->alloced], 0,
470 100 * sizeof (struct eh_cie_fde));
471 sec_info->alloced += 100;
473 /* Now fix any pointers into the array. */
474 if (last_cie_inf >= old_entry
475 && last_cie_inf < old_entry + sec_info->count)
476 last_cie_inf = sec_info->entry + (last_cie_inf - old_entry);
479 this_inf = sec_info->entry + sec_info->count;
480 last_fde = buf;
481 /* If we are at the end of the section, we still need to decide
482 on whether to output or discard last encountered CIE (if any). */
483 if ((bfd_size_type) (buf - ehbuf) == sec->size)
485 hdr.length = 0;
486 hdr.id = (unsigned int) -1;
487 end = buf;
489 else
491 /* Read the length of the entry. */
492 REQUIRE (skip_bytes (&buf, ehbuf + sec->size, 4));
493 hdr.length = bfd_get_32 (abfd, buf - 4);
495 /* 64-bit .eh_frame is not supported. */
496 REQUIRE (hdr.length != 0xffffffff);
498 /* The CIE/FDE must be fully contained in this input section. */
499 REQUIRE ((bfd_size_type) (buf - ehbuf) + hdr.length <= sec->size);
500 end = buf + hdr.length;
502 this_inf->offset = last_fde - ehbuf;
503 this_inf->size = 4 + hdr.length;
505 if (hdr.length == 0)
507 /* A zero-length CIE should only be found at the end of
508 the section. */
509 REQUIRE ((bfd_size_type) (buf - ehbuf) == sec->size);
510 ENSURE_NO_RELOCS (buf);
511 sec_info->count++;
512 /* Now just finish last encountered CIE processing and break
513 the loop. */
514 hdr.id = (unsigned int) -1;
516 else
518 REQUIRE (skip_bytes (&buf, end, 4));
519 hdr.id = bfd_get_32 (abfd, buf - 4);
520 REQUIRE (hdr.id != (unsigned int) -1);
524 if (hdr.id == 0 || hdr.id == (unsigned int) -1)
526 unsigned int initial_insn_length;
528 /* CIE */
529 if (last_cie != NULL)
531 /* Now check if this CIE is identical to the last CIE,
532 in which case we can remove it provided we adjust
533 all FDEs. Also, it can be removed if we have removed
534 all FDEs using it. */
535 if ((!info->relocatable
536 && hdr_info->last_cie_sec
537 && (sec->output_section
538 == hdr_info->last_cie_sec->output_section)
539 && cie_compare (&cie, &hdr_info->last_cie) == 0)
540 || cie_usage_count == 0)
541 last_cie_inf->removed = 1;
542 else
544 hdr_info->last_cie = cie;
545 hdr_info->last_cie_sec = sec;
546 last_cie_inf->make_relative = cie.make_relative;
547 last_cie_inf->make_lsda_relative = cie.make_lsda_relative;
548 last_cie_inf->per_encoding_relative
549 = (cie.per_encoding & 0x70) == DW_EH_PE_pcrel;
553 if (hdr.id == (unsigned int) -1)
554 break;
556 last_cie_inf = this_inf;
557 this_inf->cie = 1;
559 cie_usage_count = 0;
560 memset (&cie, 0, sizeof (cie));
561 cie.hdr = hdr;
562 REQUIRE (read_byte (&buf, end, &cie.version));
564 /* Cannot handle unknown versions. */
565 REQUIRE (cie.version == 1 || cie.version == 3);
566 REQUIRE (strlen ((char *) buf) < sizeof (cie.augmentation));
568 strcpy (cie.augmentation, (char *) buf);
569 buf = (bfd_byte *) strchr ((char *) buf, '\0') + 1;
570 ENSURE_NO_RELOCS (buf);
571 if (buf[0] == 'e' && buf[1] == 'h')
573 /* GCC < 3.0 .eh_frame CIE */
574 /* We cannot merge "eh" CIEs because __EXCEPTION_TABLE__
575 is private to each CIE, so we don't need it for anything.
576 Just skip it. */
577 REQUIRE (skip_bytes (&buf, end, ptr_size));
578 SKIP_RELOCS (buf);
580 REQUIRE (read_uleb128 (&buf, end, &cie.code_align));
581 REQUIRE (read_sleb128 (&buf, end, &cie.data_align));
582 if (cie.version == 1)
584 REQUIRE (buf < end);
585 cie.ra_column = *buf++;
587 else
588 REQUIRE (read_uleb128 (&buf, end, &cie.ra_column));
589 ENSURE_NO_RELOCS (buf);
590 cie.lsda_encoding = DW_EH_PE_omit;
591 cie.fde_encoding = DW_EH_PE_omit;
592 cie.per_encoding = DW_EH_PE_omit;
593 aug = cie.augmentation;
594 if (aug[0] != 'e' || aug[1] != 'h')
596 if (*aug == 'z')
598 aug++;
599 REQUIRE (read_uleb128 (&buf, end, &cie.augmentation_size));
600 ENSURE_NO_RELOCS (buf);
603 while (*aug != '\0')
604 switch (*aug++)
606 case 'L':
607 REQUIRE (read_byte (&buf, end, &cie.lsda_encoding));
608 ENSURE_NO_RELOCS (buf);
609 REQUIRE (get_DW_EH_PE_width (cie.lsda_encoding, ptr_size));
610 break;
611 case 'R':
612 REQUIRE (read_byte (&buf, end, &cie.fde_encoding));
613 ENSURE_NO_RELOCS (buf);
614 REQUIRE (get_DW_EH_PE_width (cie.fde_encoding, ptr_size));
615 break;
616 case 'S':
617 break;
618 case 'P':
620 int per_width;
622 REQUIRE (read_byte (&buf, end, &cie.per_encoding));
623 per_width = get_DW_EH_PE_width (cie.per_encoding,
624 ptr_size);
625 REQUIRE (per_width);
626 if ((cie.per_encoding & 0xf0) == DW_EH_PE_aligned)
628 length = -(buf - ehbuf) & (per_width - 1);
629 REQUIRE (skip_bytes (&buf, end, length));
631 ENSURE_NO_RELOCS (buf);
632 /* Ensure we have a reloc here, against
633 a global symbol. */
634 if (GET_RELOC (buf) != NULL)
636 unsigned long r_symndx;
638 #ifdef BFD64
639 if (ptr_size == 8)
640 r_symndx = ELF64_R_SYM (cookie->rel->r_info);
641 else
642 #endif
643 r_symndx = ELF32_R_SYM (cookie->rel->r_info);
644 if (r_symndx >= cookie->locsymcount)
646 struct elf_link_hash_entry *h;
648 r_symndx -= cookie->extsymoff;
649 h = cookie->sym_hashes[r_symndx];
651 while (h->root.type == bfd_link_hash_indirect
652 || h->root.type == bfd_link_hash_warning)
653 h = (struct elf_link_hash_entry *)
654 h->root.u.i.link;
656 cie.personality = h;
658 /* Cope with MIPS-style composite relocations. */
660 cookie->rel++;
661 while (GET_RELOC (buf) != NULL);
663 REQUIRE (skip_bytes (&buf, end, per_width));
665 break;
666 default:
667 /* Unrecognized augmentation. Better bail out. */
668 goto free_no_table;
672 /* For shared libraries, try to get rid of as many RELATIVE relocs
673 as possible. */
674 if (info->shared
675 && (get_elf_backend_data (abfd)
676 ->elf_backend_can_make_relative_eh_frame
677 (abfd, info, sec)))
679 if ((cie.fde_encoding & 0xf0) == DW_EH_PE_absptr)
680 cie.make_relative = 1;
681 /* If the CIE doesn't already have an 'R' entry, it's fairly
682 easy to add one, provided that there's no aligned data
683 after the augmentation string. */
684 else if (cie.fde_encoding == DW_EH_PE_omit
685 && (cie.per_encoding & 0xf0) != DW_EH_PE_aligned)
687 if (*cie.augmentation == 0)
688 this_inf->add_augmentation_size = 1;
689 this_inf->add_fde_encoding = 1;
690 cie.make_relative = 1;
694 if (info->shared
695 && (get_elf_backend_data (abfd)
696 ->elf_backend_can_make_lsda_relative_eh_frame
697 (abfd, info, sec))
698 && (cie.lsda_encoding & 0xf0) == DW_EH_PE_absptr)
699 cie.make_lsda_relative = 1;
701 /* If FDE encoding was not specified, it defaults to
702 DW_EH_absptr. */
703 if (cie.fde_encoding == DW_EH_PE_omit)
704 cie.fde_encoding = DW_EH_PE_absptr;
706 initial_insn_length = end - buf;
707 if (initial_insn_length <= 50)
709 cie.initial_insn_length = initial_insn_length;
710 memcpy (cie.initial_instructions, buf, initial_insn_length);
712 insns = buf;
713 buf += initial_insn_length;
714 ENSURE_NO_RELOCS (buf);
715 last_cie = last_fde;
717 else
719 /* Ensure this FDE uses the last CIE encountered. */
720 REQUIRE (last_cie);
721 REQUIRE (hdr.id == (unsigned int) (buf - 4 - last_cie));
723 ENSURE_NO_RELOCS (buf);
724 REQUIRE (GET_RELOC (buf));
726 if ((*reloc_symbol_deleted_p) (buf - ehbuf, cookie))
727 /* This is a FDE against a discarded section. It should
728 be deleted. */
729 this_inf->removed = 1;
730 else
732 if (info->shared
733 && (((cie.fde_encoding & 0xf0) == DW_EH_PE_absptr
734 && cie.make_relative == 0)
735 || (cie.fde_encoding & 0xf0) == DW_EH_PE_aligned))
737 /* If a shared library uses absolute pointers
738 which we cannot turn into PC relative,
739 don't create the binary search table,
740 since it is affected by runtime relocations. */
741 hdr_info->table = FALSE;
743 cie_usage_count++;
744 hdr_info->fde_count++;
746 /* Skip the initial location and address range. */
747 start = buf;
748 length = get_DW_EH_PE_width (cie.fde_encoding, ptr_size);
749 REQUIRE (skip_bytes (&buf, end, 2 * length));
751 /* Skip the augmentation size, if present. */
752 if (cie.augmentation[0] == 'z')
753 REQUIRE (read_uleb128 (&buf, end, &length));
754 else
755 length = 0;
757 /* Of the supported augmentation characters above, only 'L'
758 adds augmentation data to the FDE. This code would need to
759 be adjusted if any future augmentations do the same thing. */
760 if (cie.lsda_encoding != DW_EH_PE_omit)
762 this_inf->lsda_offset = buf - start;
763 /* If there's no 'z' augmentation, we don't know where the
764 CFA insns begin. Assume no padding. */
765 if (cie.augmentation[0] != 'z')
766 length = end - buf;
769 /* Skip over the augmentation data. */
770 REQUIRE (skip_bytes (&buf, end, length));
771 insns = buf;
773 buf = last_fde + 4 + hdr.length;
774 SKIP_RELOCS (buf);
777 /* Try to interpret the CFA instructions and find the first
778 padding nop. Shrink this_inf's size so that it doesn't
779 including the padding. */
780 length = get_DW_EH_PE_width (cie.fde_encoding, ptr_size);
781 insns = skip_non_nops (insns, end, length);
782 if (insns != 0)
783 this_inf->size -= end - insns;
785 this_inf->fde_encoding = cie.fde_encoding;
786 this_inf->lsda_encoding = cie.lsda_encoding;
787 sec_info->count++;
790 elf_section_data (sec)->sec_info = sec_info;
791 sec->sec_info_type = ELF_INFO_TYPE_EH_FRAME;
793 /* Ok, now we can assign new offsets. */
794 offset = 0;
795 last_cie_inf = hdr_info->last_cie_inf;
796 for (ent = sec_info->entry; ent < sec_info->entry + sec_info->count; ++ent)
797 if (!ent->removed)
799 if (ent->cie)
800 last_cie_inf = ent;
801 else
802 ent->cie_inf = last_cie_inf;
803 ent->new_offset = offset;
804 offset += size_of_output_cie_fde (ent, ptr_size);
806 hdr_info->last_cie_inf = last_cie_inf;
808 /* Resize the sec as needed. */
809 sec->rawsize = sec->size;
810 sec->size = offset;
811 if (sec->size == 0)
812 sec->flags |= SEC_EXCLUDE;
814 free (ehbuf);
815 return offset != sec->rawsize;
817 free_no_table:
818 if (ehbuf)
819 free (ehbuf);
820 if (sec_info)
821 free (sec_info);
822 hdr_info->table = FALSE;
823 hdr_info->last_cie.hdr.length = 0;
824 return FALSE;
826 #undef REQUIRE
829 /* This function is called for .eh_frame_hdr section after
830 _bfd_elf_discard_section_eh_frame has been called on all .eh_frame
831 input sections. It finalizes the size of .eh_frame_hdr section. */
833 bfd_boolean
834 _bfd_elf_discard_section_eh_frame_hdr (bfd *abfd, struct bfd_link_info *info)
836 struct elf_link_hash_table *htab;
837 struct eh_frame_hdr_info *hdr_info;
838 asection *sec;
840 htab = elf_hash_table (info);
841 hdr_info = &htab->eh_info;
842 sec = hdr_info->hdr_sec;
843 if (sec == NULL)
844 return FALSE;
846 sec->size = EH_FRAME_HDR_SIZE;
847 if (hdr_info->table)
848 sec->size += 4 + hdr_info->fde_count * 8;
850 /* Request program headers to be recalculated. */
851 elf_tdata (abfd)->program_header_size = 0;
852 elf_tdata (abfd)->eh_frame_hdr = sec;
853 return TRUE;
856 /* This function is called from size_dynamic_sections.
857 It needs to decide whether .eh_frame_hdr should be output or not,
858 because when the dynamic symbol table has been sized it is too late
859 to strip sections. */
861 bfd_boolean
862 _bfd_elf_maybe_strip_eh_frame_hdr (struct bfd_link_info *info)
864 asection *o;
865 bfd *abfd;
866 struct elf_link_hash_table *htab;
867 struct eh_frame_hdr_info *hdr_info;
869 htab = elf_hash_table (info);
870 hdr_info = &htab->eh_info;
871 if (hdr_info->hdr_sec == NULL)
872 return TRUE;
874 if (bfd_is_abs_section (hdr_info->hdr_sec->output_section))
876 hdr_info->hdr_sec = NULL;
877 return TRUE;
880 abfd = NULL;
881 if (info->eh_frame_hdr)
882 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
884 /* Count only sections which have at least a single CIE or FDE.
885 There cannot be any CIE or FDE <= 8 bytes. */
886 o = bfd_get_section_by_name (abfd, ".eh_frame");
887 if (o && o->size > 8 && !bfd_is_abs_section (o->output_section))
888 break;
891 if (abfd == NULL)
893 hdr_info->hdr_sec->flags |= SEC_EXCLUDE;
894 hdr_info->hdr_sec = NULL;
895 return TRUE;
898 hdr_info->table = TRUE;
899 return TRUE;
902 /* Adjust an address in the .eh_frame section. Given OFFSET within
903 SEC, this returns the new offset in the adjusted .eh_frame section,
904 or -1 if the address refers to a CIE/FDE which has been removed
905 or to offset with dynamic relocation which is no longer needed. */
907 bfd_vma
908 _bfd_elf_eh_frame_section_offset (bfd *output_bfd ATTRIBUTE_UNUSED,
909 struct bfd_link_info *info,
910 asection *sec,
911 bfd_vma offset)
913 struct eh_frame_sec_info *sec_info;
914 struct elf_link_hash_table *htab;
915 struct eh_frame_hdr_info *hdr_info;
916 unsigned int lo, hi, mid;
918 if (sec->sec_info_type != ELF_INFO_TYPE_EH_FRAME)
919 return offset;
920 sec_info = elf_section_data (sec)->sec_info;
922 if (offset >= sec->rawsize)
923 return offset - sec->rawsize + sec->size;
925 htab = elf_hash_table (info);
926 hdr_info = &htab->eh_info;
927 if (hdr_info->offsets_adjusted)
928 offset += sec->output_offset;
930 lo = 0;
931 hi = sec_info->count;
932 mid = 0;
933 while (lo < hi)
935 mid = (lo + hi) / 2;
936 if (offset < sec_info->entry[mid].offset)
937 hi = mid;
938 else if (offset
939 >= sec_info->entry[mid].offset + sec_info->entry[mid].size)
940 lo = mid + 1;
941 else
942 break;
945 BFD_ASSERT (lo < hi);
947 /* FDE or CIE was removed. */
948 if (sec_info->entry[mid].removed)
949 return (bfd_vma) -1;
951 /* If converting to DW_EH_PE_pcrel, there will be no need for run-time
952 relocation against FDE's initial_location field. */
953 if (!sec_info->entry[mid].cie
954 && sec_info->entry[mid].cie_inf->make_relative
955 && offset == sec_info->entry[mid].offset + 8)
956 return (bfd_vma) -2;
958 /* If converting LSDA pointers to DW_EH_PE_pcrel, there will be no need
959 for run-time relocation against LSDA field. */
960 if (!sec_info->entry[mid].cie
961 && sec_info->entry[mid].cie_inf->make_lsda_relative
962 && (offset == (sec_info->entry[mid].offset + 8
963 + sec_info->entry[mid].lsda_offset))
964 && (sec_info->entry[mid].cie_inf->need_lsda_relative
965 || !hdr_info->offsets_adjusted))
967 sec_info->entry[mid].cie_inf->need_lsda_relative = 1;
968 return (bfd_vma) -2;
971 if (hdr_info->offsets_adjusted)
972 offset -= sec->output_offset;
973 /* Any new augmentation bytes go before the first relocation. */
974 return (offset + sec_info->entry[mid].new_offset
975 - sec_info->entry[mid].offset
976 + extra_augmentation_string_bytes (sec_info->entry + mid)
977 + extra_augmentation_data_bytes (sec_info->entry + mid));
980 /* Write out .eh_frame section. This is called with the relocated
981 contents. */
983 bfd_boolean
984 _bfd_elf_write_section_eh_frame (bfd *abfd,
985 struct bfd_link_info *info,
986 asection *sec,
987 bfd_byte *contents)
989 struct eh_frame_sec_info *sec_info;
990 struct elf_link_hash_table *htab;
991 struct eh_frame_hdr_info *hdr_info;
992 unsigned int ptr_size;
993 struct eh_cie_fde *ent;
995 if (sec->sec_info_type != ELF_INFO_TYPE_EH_FRAME)
996 return bfd_set_section_contents (abfd, sec->output_section, contents,
997 sec->output_offset, sec->size);
999 ptr_size = (get_elf_backend_data (abfd)
1000 ->elf_backend_eh_frame_address_size (abfd, sec));
1001 BFD_ASSERT (ptr_size != 0);
1003 sec_info = elf_section_data (sec)->sec_info;
1004 htab = elf_hash_table (info);
1005 hdr_info = &htab->eh_info;
1007 /* First convert all offsets to output section offsets, so that a
1008 CIE offset is valid if the CIE is used by a FDE from some other
1009 section. This can happen when duplicate CIEs are deleted in
1010 _bfd_elf_discard_section_eh_frame. We do all sections here because
1011 this function might not be called on sections in the same order as
1012 _bfd_elf_discard_section_eh_frame. */
1013 if (!hdr_info->offsets_adjusted)
1015 bfd *ibfd;
1016 asection *eh;
1017 struct eh_frame_sec_info *eh_inf;
1019 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
1021 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1022 || (ibfd->flags & DYNAMIC) != 0)
1023 continue;
1025 eh = bfd_get_section_by_name (ibfd, ".eh_frame");
1026 if (eh == NULL || eh->sec_info_type != ELF_INFO_TYPE_EH_FRAME)
1027 continue;
1029 eh_inf = elf_section_data (eh)->sec_info;
1030 for (ent = eh_inf->entry; ent < eh_inf->entry + eh_inf->count; ++ent)
1032 ent->offset += eh->output_offset;
1033 ent->new_offset += eh->output_offset;
1036 hdr_info->offsets_adjusted = TRUE;
1039 if (hdr_info->table && hdr_info->array == NULL)
1040 hdr_info->array
1041 = bfd_malloc (hdr_info->fde_count * sizeof(*hdr_info->array));
1042 if (hdr_info->array == NULL)
1043 hdr_info = NULL;
1045 /* The new offsets can be bigger or smaller than the original offsets.
1046 We therefore need to make two passes over the section: one backward
1047 pass to move entries up and one forward pass to move entries down.
1048 The two passes won't interfere with each other because entries are
1049 not reordered */
1050 for (ent = sec_info->entry + sec_info->count; ent-- != sec_info->entry;)
1051 if (!ent->removed && ent->new_offset > ent->offset)
1052 memmove (contents + ent->new_offset - sec->output_offset,
1053 contents + ent->offset - sec->output_offset, ent->size);
1055 for (ent = sec_info->entry; ent < sec_info->entry + sec_info->count; ++ent)
1056 if (!ent->removed && ent->new_offset < ent->offset)
1057 memmove (contents + ent->new_offset - sec->output_offset,
1058 contents + ent->offset - sec->output_offset, ent->size);
1060 for (ent = sec_info->entry; ent < sec_info->entry + sec_info->count; ++ent)
1062 unsigned char *buf, *end;
1063 unsigned int new_size;
1065 if (ent->removed)
1066 continue;
1068 if (ent->size == 4)
1070 /* Any terminating FDE must be at the end of the section. */
1071 BFD_ASSERT (ent == sec_info->entry + sec_info->count - 1);
1072 continue;
1075 buf = contents + ent->new_offset - sec->output_offset;
1076 end = buf + ent->size;
1077 new_size = size_of_output_cie_fde (ent, ptr_size);
1079 /* Update the size. It may be shrinked. */
1080 bfd_put_32 (abfd, new_size - 4, buf);
1082 /* Filling the extra bytes with DW_CFA_nops. */
1083 if (new_size != ent->size)
1084 memset (end, 0, new_size - ent->size);
1086 if (ent->cie)
1088 /* CIE */
1089 if (ent->make_relative
1090 || ent->need_lsda_relative
1091 || ent->per_encoding_relative)
1093 char *aug;
1094 unsigned int action, extra_string, extra_data;
1095 unsigned int per_width, per_encoding;
1097 /* Need to find 'R' or 'L' augmentation's argument and modify
1098 DW_EH_PE_* value. */
1099 action = ((ent->make_relative ? 1 : 0)
1100 | (ent->need_lsda_relative ? 2 : 0)
1101 | (ent->per_encoding_relative ? 4 : 0));
1102 extra_string = extra_augmentation_string_bytes (ent);
1103 extra_data = extra_augmentation_data_bytes (ent);
1105 /* Skip length, id and version. */
1106 buf += 9;
1107 aug = (char *) buf;
1108 buf += strlen (aug) + 1;
1109 skip_leb128 (&buf, end);
1110 skip_leb128 (&buf, end);
1111 skip_leb128 (&buf, end);
1112 if (*aug == 'z')
1114 /* The uleb128 will always be a single byte for the kind
1115 of augmentation strings that we're prepared to handle. */
1116 *buf++ += extra_data;
1117 aug++;
1120 /* Make room for the new augmentation string and data bytes. */
1121 memmove (buf + extra_string + extra_data, buf, end - buf);
1122 memmove (aug + extra_string, aug, buf - (bfd_byte *) aug);
1123 buf += extra_string;
1124 end += extra_string + extra_data;
1126 if (ent->add_augmentation_size)
1128 *aug++ = 'z';
1129 *buf++ = extra_data - 1;
1131 if (ent->add_fde_encoding)
1133 BFD_ASSERT (action & 1);
1134 *aug++ = 'R';
1135 *buf++ = DW_EH_PE_pcrel;
1136 action &= ~1;
1139 while (action)
1140 switch (*aug++)
1142 case 'L':
1143 if (action & 2)
1145 BFD_ASSERT (*buf == ent->lsda_encoding);
1146 *buf |= DW_EH_PE_pcrel;
1147 action &= ~2;
1149 buf++;
1150 break;
1151 case 'P':
1152 per_encoding = *buf++;
1153 per_width = get_DW_EH_PE_width (per_encoding, ptr_size);
1154 BFD_ASSERT (per_width != 0);
1155 BFD_ASSERT (((per_encoding & 0x70) == DW_EH_PE_pcrel)
1156 == ent->per_encoding_relative);
1157 if ((per_encoding & 0xf0) == DW_EH_PE_aligned)
1158 buf = (contents
1159 + ((buf - contents + per_width - 1)
1160 & ~((bfd_size_type) per_width - 1)));
1161 if (action & 4)
1163 bfd_vma val;
1165 val = read_value (abfd, buf, per_width,
1166 get_DW_EH_PE_signed (per_encoding));
1167 val += ent->offset - ent->new_offset;
1168 val -= extra_string + extra_data;
1169 write_value (abfd, buf, val, per_width);
1170 action &= ~4;
1172 buf += per_width;
1173 break;
1174 case 'R':
1175 if (action & 1)
1177 BFD_ASSERT (*buf == ent->fde_encoding);
1178 *buf |= DW_EH_PE_pcrel;
1179 action &= ~1;
1181 buf++;
1182 break;
1183 case 'S':
1184 break;
1185 default:
1186 BFD_FAIL ();
1190 else
1192 /* FDE */
1193 bfd_vma value, address;
1194 unsigned int width;
1196 /* Skip length. */
1197 buf += 4;
1198 value = ent->new_offset + 4 - ent->cie_inf->new_offset;
1199 bfd_put_32 (abfd, value, buf);
1200 buf += 4;
1201 width = get_DW_EH_PE_width (ent->fde_encoding, ptr_size);
1202 value = read_value (abfd, buf, width,
1203 get_DW_EH_PE_signed (ent->fde_encoding));
1204 address = value;
1205 if (value)
1207 switch (ent->fde_encoding & 0xf0)
1209 case DW_EH_PE_indirect:
1210 case DW_EH_PE_textrel:
1211 BFD_ASSERT (hdr_info == NULL);
1212 break;
1213 case DW_EH_PE_datarel:
1215 asection *got = bfd_get_section_by_name (abfd, ".got");
1217 BFD_ASSERT (got != NULL);
1218 address += got->vma;
1220 break;
1221 case DW_EH_PE_pcrel:
1222 value += ent->offset - ent->new_offset;
1223 address += sec->output_section->vma + ent->offset + 8;
1224 break;
1226 if (ent->cie_inf->make_relative)
1227 value -= sec->output_section->vma + ent->new_offset + 8;
1228 write_value (abfd, buf, value, width);
1231 if (hdr_info)
1233 hdr_info->array[hdr_info->array_count].initial_loc = address;
1234 hdr_info->array[hdr_info->array_count++].fde
1235 = sec->output_section->vma + ent->new_offset;
1238 if ((ent->lsda_encoding & 0xf0) == DW_EH_PE_pcrel
1239 || ent->cie_inf->need_lsda_relative)
1241 buf += ent->lsda_offset;
1242 width = get_DW_EH_PE_width (ent->lsda_encoding, ptr_size);
1243 value = read_value (abfd, buf, width,
1244 get_DW_EH_PE_signed (ent->lsda_encoding));
1245 if (value)
1247 if ((ent->lsda_encoding & 0xf0) == DW_EH_PE_pcrel)
1248 value += ent->offset - ent->new_offset;
1249 else if (ent->cie_inf->need_lsda_relative)
1250 value -= (sec->output_section->vma + ent->new_offset + 8
1251 + ent->lsda_offset);
1252 write_value (abfd, buf, value, width);
1255 else if (ent->cie_inf->add_augmentation_size)
1257 /* Skip the PC and length and insert a zero byte for the
1258 augmentation size. */
1259 buf += width * 2;
1260 memmove (buf + 1, buf, end - buf);
1261 *buf = 0;
1266 /* We don't align the section to its section alignment since the
1267 runtime library only expects all CIE/FDE records aligned at
1268 the pointer size. _bfd_elf_discard_section_eh_frame should
1269 have padded CIE/FDE records to multiple of pointer size with
1270 size_of_output_cie_fde. */
1271 if ((sec->size % ptr_size) != 0)
1272 abort ();
1274 return bfd_set_section_contents (abfd, sec->output_section,
1275 contents, (file_ptr) sec->output_offset,
1276 sec->size);
1279 /* Helper function used to sort .eh_frame_hdr search table by increasing
1280 VMA of FDE initial location. */
1282 static int
1283 vma_compare (const void *a, const void *b)
1285 const struct eh_frame_array_ent *p = a;
1286 const struct eh_frame_array_ent *q = b;
1287 if (p->initial_loc > q->initial_loc)
1288 return 1;
1289 if (p->initial_loc < q->initial_loc)
1290 return -1;
1291 return 0;
1294 /* Write out .eh_frame_hdr section. This must be called after
1295 _bfd_elf_write_section_eh_frame has been called on all input
1296 .eh_frame sections.
1297 .eh_frame_hdr format:
1298 ubyte version (currently 1)
1299 ubyte eh_frame_ptr_enc (DW_EH_PE_* encoding of pointer to start of
1300 .eh_frame section)
1301 ubyte fde_count_enc (DW_EH_PE_* encoding of total FDE count
1302 number (or DW_EH_PE_omit if there is no
1303 binary search table computed))
1304 ubyte table_enc (DW_EH_PE_* encoding of binary search table,
1305 or DW_EH_PE_omit if not present.
1306 DW_EH_PE_datarel is using address of
1307 .eh_frame_hdr section start as base)
1308 [encoded] eh_frame_ptr (pointer to start of .eh_frame section)
1309 optionally followed by:
1310 [encoded] fde_count (total number of FDEs in .eh_frame section)
1311 fde_count x [encoded] initial_loc, fde
1312 (array of encoded pairs containing
1313 FDE initial_location field and FDE address,
1314 sorted by increasing initial_loc). */
1316 bfd_boolean
1317 _bfd_elf_write_section_eh_frame_hdr (bfd *abfd, struct bfd_link_info *info)
1319 struct elf_link_hash_table *htab;
1320 struct eh_frame_hdr_info *hdr_info;
1321 asection *sec;
1322 bfd_byte *contents;
1323 asection *eh_frame_sec;
1324 bfd_size_type size;
1325 bfd_boolean retval;
1326 bfd_vma encoded_eh_frame;
1328 htab = elf_hash_table (info);
1329 hdr_info = &htab->eh_info;
1330 sec = hdr_info->hdr_sec;
1331 if (sec == NULL)
1332 return TRUE;
1334 size = EH_FRAME_HDR_SIZE;
1335 if (hdr_info->array && hdr_info->array_count == hdr_info->fde_count)
1336 size += 4 + hdr_info->fde_count * 8;
1337 contents = bfd_malloc (size);
1338 if (contents == NULL)
1339 return FALSE;
1341 eh_frame_sec = bfd_get_section_by_name (abfd, ".eh_frame");
1342 if (eh_frame_sec == NULL)
1344 free (contents);
1345 return FALSE;
1348 memset (contents, 0, EH_FRAME_HDR_SIZE);
1349 contents[0] = 1; /* Version. */
1350 contents[1] = get_elf_backend_data (abfd)->elf_backend_encode_eh_address
1351 (abfd, info, eh_frame_sec, 0, sec, 4,
1352 &encoded_eh_frame); /* .eh_frame offset. */
1354 if (hdr_info->array && hdr_info->array_count == hdr_info->fde_count)
1356 contents[2] = DW_EH_PE_udata4; /* FDE count encoding. */
1357 contents[3] = DW_EH_PE_datarel | DW_EH_PE_sdata4; /* Search table enc. */
1359 else
1361 contents[2] = DW_EH_PE_omit;
1362 contents[3] = DW_EH_PE_omit;
1364 bfd_put_32 (abfd, encoded_eh_frame, contents + 4);
1366 if (contents[2] != DW_EH_PE_omit)
1368 unsigned int i;
1370 bfd_put_32 (abfd, hdr_info->fde_count, contents + EH_FRAME_HDR_SIZE);
1371 qsort (hdr_info->array, hdr_info->fde_count, sizeof (*hdr_info->array),
1372 vma_compare);
1373 for (i = 0; i < hdr_info->fde_count; i++)
1375 bfd_put_32 (abfd,
1376 hdr_info->array[i].initial_loc
1377 - sec->output_section->vma,
1378 contents + EH_FRAME_HDR_SIZE + i * 8 + 4);
1379 bfd_put_32 (abfd,
1380 hdr_info->array[i].fde - sec->output_section->vma,
1381 contents + EH_FRAME_HDR_SIZE + i * 8 + 8);
1385 retval = bfd_set_section_contents (abfd, sec->output_section,
1386 contents, (file_ptr) sec->output_offset,
1387 sec->size);
1388 free (contents);
1389 return retval;
1392 /* Return the width of FDE addresses. This is the default implementation. */
1394 unsigned int
1395 _bfd_elf_eh_frame_address_size (bfd *abfd, asection *sec ATTRIBUTE_UNUSED)
1397 return elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64 ? 8 : 4;
1400 /* Decide whether we can use a PC-relative encoding within the given
1401 EH frame section. This is the default implementation. */
1403 bfd_boolean
1404 _bfd_elf_can_make_relative (bfd *input_bfd ATTRIBUTE_UNUSED,
1405 struct bfd_link_info *info ATTRIBUTE_UNUSED,
1406 asection *eh_frame_section ATTRIBUTE_UNUSED)
1408 return TRUE;
1411 /* Select an encoding for the given address. Preference is given to
1412 PC-relative addressing modes. */
1414 bfd_byte
1415 _bfd_elf_encode_eh_address (bfd *abfd ATTRIBUTE_UNUSED,
1416 struct bfd_link_info *info ATTRIBUTE_UNUSED,
1417 asection *osec, bfd_vma offset,
1418 asection *loc_sec, bfd_vma loc_offset,
1419 bfd_vma *encoded)
1421 *encoded = osec->vma + offset -
1422 (loc_sec->output_section->vma + loc_sec->output_offset + loc_offset);
1423 return DW_EH_PE_pcrel | DW_EH_PE_sdata4;