2004-12-01 Paul Brook <paul@codesourcery.com>
[binutils.git] / bfd / elf-eh-frame.c
blob0c0b32c36d5cb3e0096f0a1a08b8cc5e871a197d
1 /* .eh_frame section optimization.
2 Copyright 2001, 2002, 2003, 2004 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 /* Helper function for reading uleb128 encoded data. */
31 static bfd_vma
32 read_unsigned_leb128 (bfd *abfd ATTRIBUTE_UNUSED,
33 char *buf,
34 unsigned int *bytes_read_ptr)
36 bfd_vma result;
37 unsigned int num_read;
38 int shift;
39 unsigned char byte;
41 result = 0;
42 shift = 0;
43 num_read = 0;
46 byte = bfd_get_8 (abfd, (bfd_byte *) buf);
47 buf++;
48 num_read++;
49 result |= (((bfd_vma) byte & 0x7f) << shift);
50 shift += 7;
52 while (byte & 0x80);
53 *bytes_read_ptr = num_read;
54 return result;
57 /* Helper function for reading sleb128 encoded data. */
59 static bfd_signed_vma
60 read_signed_leb128 (bfd *abfd ATTRIBUTE_UNUSED,
61 char *buf,
62 unsigned int * bytes_read_ptr)
64 bfd_vma result;
65 int shift;
66 int num_read;
67 unsigned char byte;
69 result = 0;
70 shift = 0;
71 num_read = 0;
74 byte = bfd_get_8 (abfd, (bfd_byte *) buf);
75 buf ++;
76 num_read ++;
77 result |= (((bfd_vma) byte & 0x7f) << shift);
78 shift += 7;
80 while (byte & 0x80);
81 if (byte & 0x40)
82 result |= (((bfd_vma) -1) << (shift - 7)) << 7;
83 *bytes_read_ptr = num_read;
84 return result;
87 #define read_uleb128(VAR, BUF) \
88 do \
89 { \
90 (VAR) = read_unsigned_leb128 (abfd, buf, &leb128_tmp); \
91 (BUF) += leb128_tmp; \
92 } \
93 while (0)
95 #define read_sleb128(VAR, BUF) \
96 do \
97 { \
98 (VAR) = read_signed_leb128 (abfd, buf, &leb128_tmp); \
99 (BUF) += leb128_tmp; \
101 while (0)
103 /* Return 0 if either encoding is variable width, or not yet known to bfd. */
105 static
106 int get_DW_EH_PE_width (int encoding, int ptr_size)
108 /* DW_EH_PE_ values of 0x60 and 0x70 weren't defined at the time .eh_frame
109 was added to bfd. */
110 if ((encoding & 0x60) == 0x60)
111 return 0;
113 switch (encoding & 7)
115 case DW_EH_PE_udata2: return 2;
116 case DW_EH_PE_udata4: return 4;
117 case DW_EH_PE_udata8: return 8;
118 case DW_EH_PE_absptr: return ptr_size;
119 default:
120 break;
123 return 0;
126 #define get_DW_EH_PE_signed(encoding) (((encoding) & DW_EH_PE_signed) != 0)
128 /* Read a width sized value from memory. */
130 static bfd_vma
131 read_value (bfd *abfd, bfd_byte *buf, int width, int is_signed)
133 bfd_vma value;
135 switch (width)
137 case 2:
138 if (is_signed)
139 value = bfd_get_signed_16 (abfd, buf);
140 else
141 value = bfd_get_16 (abfd, buf);
142 break;
143 case 4:
144 if (is_signed)
145 value = bfd_get_signed_32 (abfd, buf);
146 else
147 value = bfd_get_32 (abfd, buf);
148 break;
149 case 8:
150 if (is_signed)
151 value = bfd_get_signed_64 (abfd, buf);
152 else
153 value = bfd_get_64 (abfd, buf);
154 break;
155 default:
156 BFD_FAIL ();
157 return 0;
160 return value;
163 /* Store a width sized value to memory. */
165 static void
166 write_value (bfd *abfd, bfd_byte *buf, bfd_vma value, int width)
168 switch (width)
170 case 2: bfd_put_16 (abfd, value, buf); break;
171 case 4: bfd_put_32 (abfd, value, buf); break;
172 case 8: bfd_put_64 (abfd, value, buf); break;
173 default: BFD_FAIL ();
177 /* Return zero if C1 and C2 CIEs can be merged. */
179 static
180 int cie_compare (struct cie *c1, struct cie *c2)
182 if (c1->hdr.length == c2->hdr.length
183 && c1->version == c2->version
184 && strcmp (c1->augmentation, c2->augmentation) == 0
185 && strcmp (c1->augmentation, "eh") != 0
186 && c1->code_align == c2->code_align
187 && c1->data_align == c2->data_align
188 && c1->ra_column == c2->ra_column
189 && c1->augmentation_size == c2->augmentation_size
190 && c1->personality == c2->personality
191 && c1->per_encoding == c2->per_encoding
192 && c1->lsda_encoding == c2->lsda_encoding
193 && c1->fde_encoding == c2->fde_encoding
194 && c1->initial_insn_length == c2->initial_insn_length
195 && memcmp (c1->initial_instructions,
196 c2->initial_instructions,
197 c1->initial_insn_length) == 0)
198 return 0;
200 return 1;
203 /* Return the number of extra bytes that we'll be inserting into
204 ENTRY's augmentation string. */
206 static INLINE unsigned int
207 extra_augmentation_string_bytes (struct eh_cie_fde *entry)
209 unsigned int size = 0;
210 if (entry->cie)
212 if (entry->add_augmentation_size)
213 size++;
214 if (entry->add_fde_encoding)
215 size++;
217 return size;
220 /* Likewise ENTRY's augmentation data. */
222 static INLINE unsigned int
223 extra_augmentation_data_bytes (struct eh_cie_fde *entry)
225 unsigned int size = 0;
226 if (entry->cie)
228 if (entry->add_augmentation_size)
229 size++;
230 if (entry->add_fde_encoding)
231 size++;
233 else
235 if (entry->cie_inf->add_augmentation_size)
236 size++;
238 return size;
241 /* Return the size that ENTRY will have in the output. ALIGNMENT is the
242 required alignment of ENTRY in bytes. */
244 static unsigned int
245 size_of_output_cie_fde (struct eh_cie_fde *entry, unsigned int alignment)
247 if (entry->removed)
248 return 0;
249 if (entry->size == 4)
250 return 4;
251 return (entry->size
252 + extra_augmentation_string_bytes (entry)
253 + extra_augmentation_data_bytes (entry)
254 + alignment - 1) & -alignment;
257 /* This function is called for each input file before the .eh_frame
258 section is relocated. It discards duplicate CIEs and FDEs for discarded
259 functions. The function returns TRUE iff any entries have been
260 deleted. */
262 bfd_boolean
263 _bfd_elf_discard_section_eh_frame
264 (bfd *abfd, struct bfd_link_info *info, asection *sec,
265 bfd_boolean (*reloc_symbol_deleted_p) (bfd_vma, void *),
266 struct elf_reloc_cookie *cookie)
268 bfd_byte *ehbuf = NULL, *buf;
269 bfd_byte *last_cie, *last_fde;
270 struct eh_cie_fde *ent, *last_cie_inf, *this_inf;
271 struct cie_header hdr;
272 struct cie cie;
273 struct elf_link_hash_table *htab;
274 struct eh_frame_hdr_info *hdr_info;
275 struct eh_frame_sec_info *sec_info = NULL;
276 unsigned int leb128_tmp;
277 unsigned int cie_usage_count, offset;
278 unsigned int ptr_size;
280 if (sec->size == 0)
282 /* This file does not contain .eh_frame information. */
283 return FALSE;
286 if ((sec->output_section != NULL
287 && bfd_is_abs_section (sec->output_section)))
289 /* At least one of the sections is being discarded from the
290 link, so we should just ignore them. */
291 return FALSE;
294 htab = elf_hash_table (info);
295 hdr_info = &htab->eh_info;
297 /* Read the frame unwind information from abfd. */
299 if (!bfd_malloc_and_get_section (abfd, sec, &ehbuf))
300 goto free_no_table;
302 if (sec->size >= 4
303 && bfd_get_32 (abfd, ehbuf) == 0
304 && cookie->rel == cookie->relend)
306 /* Empty .eh_frame section. */
307 free (ehbuf);
308 return FALSE;
311 /* If .eh_frame section size doesn't fit into int, we cannot handle
312 it (it would need to use 64-bit .eh_frame format anyway). */
313 if (sec->size != (unsigned int) sec->size)
314 goto free_no_table;
316 ptr_size = (elf_elfheader (abfd)->e_ident[EI_CLASS]
317 == ELFCLASS64) ? 8 : 4;
318 buf = ehbuf;
319 last_cie = NULL;
320 last_cie_inf = NULL;
321 memset (&cie, 0, sizeof (cie));
322 cie_usage_count = 0;
323 sec_info = bfd_zmalloc (sizeof (struct eh_frame_sec_info)
324 + 99 * sizeof (struct eh_cie_fde));
325 if (sec_info == NULL)
326 goto free_no_table;
328 sec_info->alloced = 100;
330 #define ENSURE_NO_RELOCS(buf) \
331 if (cookie->rel < cookie->relend \
332 && (cookie->rel->r_offset \
333 < (bfd_size_type) ((buf) - ehbuf)) \
334 && cookie->rel->r_info != 0) \
335 goto free_no_table
337 #define SKIP_RELOCS(buf) \
338 while (cookie->rel < cookie->relend \
339 && (cookie->rel->r_offset \
340 < (bfd_size_type) ((buf) - ehbuf))) \
341 cookie->rel++
343 #define GET_RELOC(buf) \
344 ((cookie->rel < cookie->relend \
345 && (cookie->rel->r_offset \
346 == (bfd_size_type) ((buf) - ehbuf))) \
347 ? cookie->rel : NULL)
349 for (;;)
351 unsigned char *aug;
353 if (sec_info->count == sec_info->alloced)
355 struct eh_cie_fde *old_entry = sec_info->entry;
356 sec_info = bfd_realloc (sec_info,
357 sizeof (struct eh_frame_sec_info)
358 + ((sec_info->alloced + 99)
359 * sizeof (struct eh_cie_fde)));
360 if (sec_info == NULL)
361 goto free_no_table;
363 memset (&sec_info->entry[sec_info->alloced], 0,
364 100 * sizeof (struct eh_cie_fde));
365 sec_info->alloced += 100;
367 /* Now fix any pointers into the array. */
368 if (last_cie_inf >= old_entry
369 && last_cie_inf < old_entry + sec_info->count)
370 last_cie_inf = sec_info->entry + (last_cie_inf - old_entry);
373 this_inf = sec_info->entry + sec_info->count;
374 last_fde = buf;
375 /* If we are at the end of the section, we still need to decide
376 on whether to output or discard last encountered CIE (if any). */
377 if ((bfd_size_type) (buf - ehbuf) == sec->size)
378 hdr.id = (unsigned int) -1;
379 else
381 if ((bfd_size_type) (buf + 4 - ehbuf) > sec->size)
382 /* No space for CIE/FDE header length. */
383 goto free_no_table;
385 hdr.length = bfd_get_32 (abfd, buf);
386 if (hdr.length == 0xffffffff)
387 /* 64-bit .eh_frame is not supported. */
388 goto free_no_table;
389 buf += 4;
390 if ((bfd_size_type) (buf - ehbuf) + hdr.length > sec->size)
391 /* CIE/FDE not contained fully in this .eh_frame input section. */
392 goto free_no_table;
394 this_inf->offset = last_fde - ehbuf;
395 this_inf->size = 4 + hdr.length;
397 if (hdr.length == 0)
399 /* CIE with length 0 must be only the last in the section. */
400 if ((bfd_size_type) (buf - ehbuf) < sec->size)
401 goto free_no_table;
402 ENSURE_NO_RELOCS (buf);
403 sec_info->count++;
404 /* Now just finish last encountered CIE processing and break
405 the loop. */
406 hdr.id = (unsigned int) -1;
408 else
410 hdr.id = bfd_get_32 (abfd, buf);
411 buf += 4;
412 if (hdr.id == (unsigned int) -1)
413 goto free_no_table;
417 if (hdr.id == 0 || hdr.id == (unsigned int) -1)
419 unsigned int initial_insn_length;
421 /* CIE */
422 if (last_cie != NULL)
424 /* Now check if this CIE is identical to the last CIE,
425 in which case we can remove it provided we adjust
426 all FDEs. Also, it can be removed if we have removed
427 all FDEs using it. */
428 if ((!info->relocatable
429 && hdr_info->last_cie_sec
430 && (sec->output_section
431 == hdr_info->last_cie_sec->output_section)
432 && cie_compare (&cie, &hdr_info->last_cie) == 0)
433 || cie_usage_count == 0)
434 last_cie_inf->removed = 1;
435 else
437 hdr_info->last_cie = cie;
438 hdr_info->last_cie_sec = sec;
439 last_cie_inf->make_relative = cie.make_relative;
440 last_cie_inf->make_lsda_relative = cie.make_lsda_relative;
441 last_cie_inf->per_encoding_relative
442 = (cie.per_encoding & 0x70) == DW_EH_PE_pcrel;
446 if (hdr.id == (unsigned int) -1)
447 break;
449 last_cie_inf = this_inf;
450 this_inf->cie = 1;
452 cie_usage_count = 0;
453 memset (&cie, 0, sizeof (cie));
454 cie.hdr = hdr;
455 cie.version = *buf++;
457 /* Cannot handle unknown versions. */
458 if (cie.version != 1 && cie.version != 3)
459 goto free_no_table;
460 if (strlen (buf) > sizeof (cie.augmentation) - 1)
461 goto free_no_table;
463 strcpy (cie.augmentation, buf);
464 buf = strchr (buf, '\0') + 1;
465 ENSURE_NO_RELOCS (buf);
466 if (buf[0] == 'e' && buf[1] == 'h')
468 /* GCC < 3.0 .eh_frame CIE */
469 /* We cannot merge "eh" CIEs because __EXCEPTION_TABLE__
470 is private to each CIE, so we don't need it for anything.
471 Just skip it. */
472 buf += ptr_size;
473 SKIP_RELOCS (buf);
475 read_uleb128 (cie.code_align, buf);
476 read_sleb128 (cie.data_align, buf);
477 if (cie.version == 1)
478 cie.ra_column = *buf++;
479 else
480 read_uleb128 (cie.ra_column, buf);
481 ENSURE_NO_RELOCS (buf);
482 cie.lsda_encoding = DW_EH_PE_omit;
483 cie.fde_encoding = DW_EH_PE_omit;
484 cie.per_encoding = DW_EH_PE_omit;
485 aug = cie.augmentation;
486 if (aug[0] != 'e' || aug[1] != 'h')
488 if (*aug == 'z')
490 aug++;
491 read_uleb128 (cie.augmentation_size, buf);
492 ENSURE_NO_RELOCS (buf);
495 while (*aug != '\0')
496 switch (*aug++)
498 case 'L':
499 cie.lsda_encoding = *buf++;
500 ENSURE_NO_RELOCS (buf);
501 if (get_DW_EH_PE_width (cie.lsda_encoding, ptr_size) == 0)
502 goto free_no_table;
503 break;
504 case 'R':
505 cie.fde_encoding = *buf++;
506 ENSURE_NO_RELOCS (buf);
507 if (get_DW_EH_PE_width (cie.fde_encoding, ptr_size) == 0)
508 goto free_no_table;
509 break;
510 case 'P':
512 int per_width;
514 cie.per_encoding = *buf++;
515 per_width = get_DW_EH_PE_width (cie.per_encoding,
516 ptr_size);
517 if (per_width == 0)
518 goto free_no_table;
519 if ((cie.per_encoding & 0xf0) == DW_EH_PE_aligned)
520 buf = (ehbuf
521 + ((buf - ehbuf + per_width - 1)
522 & ~((bfd_size_type) per_width - 1)));
523 ENSURE_NO_RELOCS (buf);
524 /* Ensure we have a reloc here, against
525 a global symbol. */
526 if (GET_RELOC (buf) != NULL)
528 unsigned long r_symndx;
530 #ifdef BFD64
531 if (ptr_size == 8)
532 r_symndx = ELF64_R_SYM (cookie->rel->r_info);
533 else
534 #endif
535 r_symndx = ELF32_R_SYM (cookie->rel->r_info);
536 if (r_symndx >= cookie->locsymcount)
538 struct elf_link_hash_entry *h;
540 r_symndx -= cookie->extsymoff;
541 h = cookie->sym_hashes[r_symndx];
543 while (h->root.type == bfd_link_hash_indirect
544 || h->root.type == bfd_link_hash_warning)
545 h = (struct elf_link_hash_entry *)
546 h->root.u.i.link;
548 cie.personality = h;
550 /* Cope with MIPS-style composite relocations. */
552 cookie->rel++;
553 while (GET_RELOC (buf) != NULL);
555 buf += per_width;
557 break;
558 default:
559 /* Unrecognized augmentation. Better bail out. */
560 goto free_no_table;
564 /* For shared libraries, try to get rid of as many RELATIVE relocs
565 as possible. */
566 if (info->shared
567 && (get_elf_backend_data (abfd)
568 ->elf_backend_can_make_relative_eh_frame
569 (abfd, info, sec)))
571 if ((cie.fde_encoding & 0xf0) == DW_EH_PE_absptr)
572 cie.make_relative = 1;
573 /* If the CIE doesn't already have an 'R' entry, it's fairly
574 easy to add one, provided that there's no aligned data
575 after the augmentation string. */
576 else if (cie.fde_encoding == DW_EH_PE_omit
577 && (cie.per_encoding & 0xf0) != DW_EH_PE_aligned)
579 if (*cie.augmentation == 0)
580 this_inf->add_augmentation_size = 1;
581 this_inf->add_fde_encoding = 1;
582 cie.make_relative = 1;
586 if (info->shared
587 && (get_elf_backend_data (abfd)
588 ->elf_backend_can_make_lsda_relative_eh_frame
589 (abfd, info, sec))
590 && (cie.lsda_encoding & 0xf0) == DW_EH_PE_absptr)
591 cie.make_lsda_relative = 1;
593 /* If FDE encoding was not specified, it defaults to
594 DW_EH_absptr. */
595 if (cie.fde_encoding == DW_EH_PE_omit)
596 cie.fde_encoding = DW_EH_PE_absptr;
598 initial_insn_length = cie.hdr.length - (buf - last_fde - 4);
599 if (initial_insn_length <= 50)
601 cie.initial_insn_length = initial_insn_length;
602 memcpy (cie.initial_instructions, buf, initial_insn_length);
604 buf += initial_insn_length;
605 ENSURE_NO_RELOCS (buf);
606 last_cie = last_fde;
608 else
610 /* Ensure this FDE uses the last CIE encountered. */
611 if (last_cie == NULL
612 || hdr.id != (unsigned int) (buf - 4 - last_cie))
613 goto free_no_table;
615 ENSURE_NO_RELOCS (buf);
616 if (GET_RELOC (buf) == NULL)
617 /* This should not happen. */
618 goto free_no_table;
620 if ((*reloc_symbol_deleted_p) (buf - ehbuf, cookie))
621 /* This is a FDE against a discarded section. It should
622 be deleted. */
623 this_inf->removed = 1;
624 else
626 if (info->shared
627 && (((cie.fde_encoding & 0xf0) == DW_EH_PE_absptr
628 && cie.make_relative == 0)
629 || (cie.fde_encoding & 0xf0) == DW_EH_PE_aligned))
631 /* If a shared library uses absolute pointers
632 which we cannot turn into PC relative,
633 don't create the binary search table,
634 since it is affected by runtime relocations. */
635 hdr_info->table = FALSE;
637 cie_usage_count++;
638 hdr_info->fde_count++;
640 if (cie.lsda_encoding != DW_EH_PE_omit)
642 unsigned int dummy;
644 aug = buf;
645 buf += 2 * get_DW_EH_PE_width (cie.fde_encoding, ptr_size);
646 if (cie.augmentation[0] == 'z')
647 read_uleb128 (dummy, buf);
648 /* If some new augmentation data is added before LSDA
649 in FDE augmentation area, this need to be adjusted. */
650 this_inf->lsda_offset = (buf - aug);
652 buf = last_fde + 4 + hdr.length;
653 SKIP_RELOCS (buf);
656 this_inf->fde_encoding = cie.fde_encoding;
657 this_inf->lsda_encoding = cie.lsda_encoding;
658 sec_info->count++;
661 elf_section_data (sec)->sec_info = sec_info;
662 sec->sec_info_type = ELF_INFO_TYPE_EH_FRAME;
664 /* Ok, now we can assign new offsets. */
665 offset = 0;
666 last_cie_inf = hdr_info->last_cie_inf;
667 for (ent = sec_info->entry; ent < sec_info->entry + sec_info->count; ++ent)
668 if (!ent->removed)
670 if (ent->cie)
671 last_cie_inf = ent;
672 else
673 ent->cie_inf = last_cie_inf;
674 ent->new_offset = offset;
675 offset += size_of_output_cie_fde (ent, ptr_size);
677 hdr_info->last_cie_inf = last_cie_inf;
679 /* Resize the sec as needed. */
680 sec->rawsize = sec->size;
681 sec->size = offset;
682 if (sec->size == 0)
683 sec->flags |= SEC_EXCLUDE;
685 free (ehbuf);
686 return offset != sec->rawsize;
688 free_no_table:
689 if (ehbuf)
690 free (ehbuf);
691 if (sec_info)
692 free (sec_info);
693 hdr_info->table = FALSE;
694 hdr_info->last_cie.hdr.length = 0;
695 return FALSE;
698 /* This function is called for .eh_frame_hdr section after
699 _bfd_elf_discard_section_eh_frame has been called on all .eh_frame
700 input sections. It finalizes the size of .eh_frame_hdr section. */
702 bfd_boolean
703 _bfd_elf_discard_section_eh_frame_hdr (bfd *abfd, struct bfd_link_info *info)
705 struct elf_link_hash_table *htab;
706 struct eh_frame_hdr_info *hdr_info;
707 asection *sec;
709 htab = elf_hash_table (info);
710 hdr_info = &htab->eh_info;
711 sec = hdr_info->hdr_sec;
712 if (sec == NULL)
713 return FALSE;
715 sec->size = EH_FRAME_HDR_SIZE;
716 if (hdr_info->table)
717 sec->size += 4 + hdr_info->fde_count * 8;
719 /* Request program headers to be recalculated. */
720 elf_tdata (abfd)->program_header_size = 0;
721 elf_tdata (abfd)->eh_frame_hdr = sec;
722 return TRUE;
725 /* This function is called from size_dynamic_sections.
726 It needs to decide whether .eh_frame_hdr should be output or not,
727 because later on it is too late for calling _bfd_strip_section_from_output,
728 since dynamic symbol table has been sized. */
730 bfd_boolean
731 _bfd_elf_maybe_strip_eh_frame_hdr (struct bfd_link_info *info)
733 asection *o;
734 bfd *abfd;
735 struct elf_link_hash_table *htab;
736 struct eh_frame_hdr_info *hdr_info;
738 htab = elf_hash_table (info);
739 hdr_info = &htab->eh_info;
740 if (hdr_info->hdr_sec == NULL)
741 return TRUE;
743 if (bfd_is_abs_section (hdr_info->hdr_sec->output_section))
745 hdr_info->hdr_sec = NULL;
746 return TRUE;
749 abfd = NULL;
750 if (info->eh_frame_hdr)
751 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
753 /* Count only sections which have at least a single CIE or FDE.
754 There cannot be any CIE or FDE <= 8 bytes. */
755 o = bfd_get_section_by_name (abfd, ".eh_frame");
756 if (o && o->size > 8 && !bfd_is_abs_section (o->output_section))
757 break;
760 if (abfd == NULL)
762 _bfd_strip_section_from_output (info, hdr_info->hdr_sec);
763 hdr_info->hdr_sec = NULL;
764 return TRUE;
767 hdr_info->table = TRUE;
768 return TRUE;
771 /* Adjust an address in the .eh_frame section. Given OFFSET within
772 SEC, this returns the new offset in the adjusted .eh_frame section,
773 or -1 if the address refers to a CIE/FDE which has been removed
774 or to offset with dynamic relocation which is no longer needed. */
776 bfd_vma
777 _bfd_elf_eh_frame_section_offset (bfd *output_bfd ATTRIBUTE_UNUSED,
778 struct bfd_link_info *info,
779 asection *sec,
780 bfd_vma offset)
782 struct eh_frame_sec_info *sec_info;
783 struct elf_link_hash_table *htab;
784 struct eh_frame_hdr_info *hdr_info;
785 unsigned int lo, hi, mid;
787 if (sec->sec_info_type != ELF_INFO_TYPE_EH_FRAME)
788 return offset;
789 sec_info = elf_section_data (sec)->sec_info;
791 if (offset >= sec->rawsize)
792 return offset - sec->rawsize + sec->size;
794 htab = elf_hash_table (info);
795 hdr_info = &htab->eh_info;
796 if (hdr_info->offsets_adjusted)
797 offset += sec->output_offset;
799 lo = 0;
800 hi = sec_info->count;
801 mid = 0;
802 while (lo < hi)
804 mid = (lo + hi) / 2;
805 if (offset < sec_info->entry[mid].offset)
806 hi = mid;
807 else if (offset
808 >= sec_info->entry[mid].offset + sec_info->entry[mid].size)
809 lo = mid + 1;
810 else
811 break;
814 BFD_ASSERT (lo < hi);
816 /* FDE or CIE was removed. */
817 if (sec_info->entry[mid].removed)
818 return (bfd_vma) -1;
820 /* If converting to DW_EH_PE_pcrel, there will be no need for run-time
821 relocation against FDE's initial_location field. */
822 if (!sec_info->entry[mid].cie
823 && sec_info->entry[mid].cie_inf->make_relative
824 && offset == sec_info->entry[mid].offset + 8)
825 return (bfd_vma) -2;
827 /* If converting LSDA pointers to DW_EH_PE_pcrel, there will be no need
828 for run-time relocation against LSDA field. */
829 if (!sec_info->entry[mid].cie
830 && sec_info->entry[mid].cie_inf->make_lsda_relative
831 && (offset == (sec_info->entry[mid].offset + 8
832 + sec_info->entry[mid].lsda_offset))
833 && (sec_info->entry[mid].cie_inf->need_lsda_relative
834 || !hdr_info->offsets_adjusted))
836 sec_info->entry[mid].cie_inf->need_lsda_relative = 1;
837 return (bfd_vma) -2;
840 if (hdr_info->offsets_adjusted)
841 offset -= sec->output_offset;
842 /* Any new augmentation bytes go before the first relocation. */
843 return (offset + sec_info->entry[mid].new_offset
844 - sec_info->entry[mid].offset
845 + extra_augmentation_string_bytes (sec_info->entry + mid)
846 + extra_augmentation_data_bytes (sec_info->entry + mid));
849 /* Write out .eh_frame section. This is called with the relocated
850 contents. */
852 bfd_boolean
853 _bfd_elf_write_section_eh_frame (bfd *abfd,
854 struct bfd_link_info *info,
855 asection *sec,
856 bfd_byte *contents)
858 struct eh_frame_sec_info *sec_info;
859 struct elf_link_hash_table *htab;
860 struct eh_frame_hdr_info *hdr_info;
861 unsigned int leb128_tmp;
862 unsigned int ptr_size;
863 struct eh_cie_fde *ent;
865 ptr_size = (elf_elfheader (sec->owner)->e_ident[EI_CLASS]
866 == ELFCLASS64) ? 8 : 4;
868 if (sec->sec_info_type != ELF_INFO_TYPE_EH_FRAME)
869 return bfd_set_section_contents (abfd, sec->output_section, contents,
870 sec->output_offset, sec->size);
871 sec_info = elf_section_data (sec)->sec_info;
872 htab = elf_hash_table (info);
873 hdr_info = &htab->eh_info;
875 /* First convert all offsets to output section offsets, so that a
876 CIE offset is valid if the CIE is used by a FDE from some other
877 section. This can happen when duplicate CIEs are deleted in
878 _bfd_elf_discard_section_eh_frame. We do all sections here because
879 this function might not be called on sections in the same order as
880 _bfd_elf_discard_section_eh_frame. */
881 if (!hdr_info->offsets_adjusted)
883 bfd *ibfd;
884 asection *eh;
885 struct eh_frame_sec_info *eh_inf;
887 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
889 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
890 || (ibfd->flags & DYNAMIC) != 0)
891 continue;
893 eh = bfd_get_section_by_name (ibfd, ".eh_frame");
894 if (eh == NULL || eh->sec_info_type != ELF_INFO_TYPE_EH_FRAME)
895 continue;
897 eh_inf = elf_section_data (eh)->sec_info;
898 for (ent = eh_inf->entry; ent < eh_inf->entry + eh_inf->count; ++ent)
900 ent->offset += eh->output_offset;
901 ent->new_offset += eh->output_offset;
904 hdr_info->offsets_adjusted = TRUE;
907 if (hdr_info->table && hdr_info->array == NULL)
908 hdr_info->array
909 = bfd_malloc (hdr_info->fde_count * sizeof(*hdr_info->array));
910 if (hdr_info->array == NULL)
911 hdr_info = NULL;
913 /* The new offsets can be bigger or smaller than the original offsets.
914 We therefore need to make two passes over the section: one backward
915 pass to move entries up and one forward pass to move entries down.
916 The two passes won't interfere with each other because entries are
917 not reordered */
918 for (ent = sec_info->entry + sec_info->count; ent-- != sec_info->entry;)
919 if (!ent->removed && ent->new_offset > ent->offset)
920 memmove (contents + ent->new_offset - sec->output_offset,
921 contents + ent->offset - sec->output_offset, ent->size);
923 for (ent = sec_info->entry; ent < sec_info->entry + sec_info->count; ++ent)
924 if (!ent->removed && ent->new_offset < ent->offset)
925 memmove (contents + ent->new_offset - sec->output_offset,
926 contents + ent->offset - sec->output_offset, ent->size);
928 for (ent = sec_info->entry; ent < sec_info->entry + sec_info->count; ++ent)
930 unsigned char *buf, *end;
931 unsigned int new_size;
933 if (ent->removed)
934 continue;
936 if (ent->size == 4)
938 /* Any terminating FDE must be at the end of the section. */
939 BFD_ASSERT (ent == sec_info->entry + sec_info->count - 1);
940 continue;
943 buf = contents + ent->new_offset - sec->output_offset;
944 end = buf + ent->size;
945 new_size = size_of_output_cie_fde (ent, ptr_size);
947 /* Install the new size, filling the extra bytes with DW_CFA_nops. */
948 if (new_size != ent->size)
950 memset (end, 0, new_size - ent->size);
951 bfd_put_32 (abfd, new_size - 4, buf);
954 if (ent->cie)
956 /* CIE */
957 if (ent->make_relative
958 || ent->need_lsda_relative
959 || ent->per_encoding_relative)
961 unsigned char *aug;
962 unsigned int action, extra_string, extra_data;
963 unsigned int dummy, per_width, per_encoding;
965 /* Need to find 'R' or 'L' augmentation's argument and modify
966 DW_EH_PE_* value. */
967 action = ((ent->make_relative ? 1 : 0)
968 | (ent->need_lsda_relative ? 2 : 0)
969 | (ent->per_encoding_relative ? 4 : 0));
970 extra_string = extra_augmentation_string_bytes (ent);
971 extra_data = extra_augmentation_data_bytes (ent);
973 /* Skip length, id and version. */
974 buf += 9;
975 aug = buf;
976 buf = strchr (buf, '\0') + 1;
977 read_uleb128 (dummy, buf);
978 read_sleb128 (dummy, buf);
979 read_uleb128 (dummy, buf);
980 if (*aug == 'z')
982 /* The uleb128 will always be a single byte for the kind
983 of augmentation strings that we're prepared to handle. */
984 *buf++ += extra_data;
985 aug++;
988 /* Make room for the new augmentation string and data bytes. */
989 memmove (buf + extra_string + extra_data, buf, end - buf);
990 memmove (aug + extra_string, aug, buf - aug);
991 buf += extra_string;
993 if (ent->add_augmentation_size)
995 *aug++ = 'z';
996 *buf++ = extra_data - 1;
998 if (ent->add_fde_encoding)
1000 BFD_ASSERT (action & 1);
1001 *aug++ = 'R';
1002 *buf++ = DW_EH_PE_pcrel;
1003 action &= ~1;
1006 while (action)
1007 switch (*aug++)
1009 case 'L':
1010 if (action & 2)
1012 BFD_ASSERT (*buf == ent->lsda_encoding);
1013 *buf |= DW_EH_PE_pcrel;
1014 action &= ~2;
1016 buf++;
1017 break;
1018 case 'P':
1019 per_encoding = *buf++;
1020 per_width = get_DW_EH_PE_width (per_encoding, ptr_size);
1021 BFD_ASSERT (per_width != 0);
1022 BFD_ASSERT (((per_encoding & 0x70) == DW_EH_PE_pcrel)
1023 == ent->per_encoding_relative);
1024 if ((per_encoding & 0xf0) == DW_EH_PE_aligned)
1025 buf = (contents
1026 + ((buf - contents + per_width - 1)
1027 & ~((bfd_size_type) per_width - 1)));
1028 if (action & 4)
1030 bfd_vma val;
1032 val = read_value (abfd, buf, per_width,
1033 get_DW_EH_PE_signed (per_encoding));
1034 val += ent->offset - ent->new_offset;
1035 val -= extra_string + extra_data;
1036 write_value (abfd, buf, val, per_width);
1037 action &= ~4;
1039 buf += per_width;
1040 break;
1041 case 'R':
1042 if (action & 1)
1044 BFD_ASSERT (*buf == ent->fde_encoding);
1045 *buf |= DW_EH_PE_pcrel;
1046 action &= ~1;
1048 buf++;
1049 break;
1050 default:
1051 BFD_FAIL ();
1055 else
1057 /* FDE */
1058 bfd_vma value, address;
1059 unsigned int width;
1061 /* Skip length. */
1062 buf += 4;
1063 value = ent->new_offset + 4 - ent->cie_inf->new_offset;
1064 bfd_put_32 (abfd, value, buf);
1065 buf += 4;
1066 width = get_DW_EH_PE_width (ent->fde_encoding, ptr_size);
1067 value = read_value (abfd, buf, width,
1068 get_DW_EH_PE_signed (ent->fde_encoding));
1069 address = value;
1070 if (value)
1072 switch (ent->fde_encoding & 0xf0)
1074 case DW_EH_PE_indirect:
1075 case DW_EH_PE_textrel:
1076 BFD_ASSERT (hdr_info == NULL);
1077 break;
1078 case DW_EH_PE_datarel:
1080 asection *got = bfd_get_section_by_name (abfd, ".got");
1082 BFD_ASSERT (got != NULL);
1083 address += got->vma;
1085 break;
1086 case DW_EH_PE_pcrel:
1087 value += ent->offset - ent->new_offset;
1088 address += sec->output_section->vma + ent->offset + 8;
1089 break;
1091 if (ent->cie_inf->make_relative)
1092 value -= sec->output_section->vma + ent->new_offset + 8;
1093 write_value (abfd, buf, value, width);
1096 if (hdr_info)
1098 hdr_info->array[hdr_info->array_count].initial_loc = address;
1099 hdr_info->array[hdr_info->array_count++].fde
1100 = sec->output_section->vma + ent->new_offset;
1103 if ((ent->lsda_encoding & 0xf0) == DW_EH_PE_pcrel
1104 || ent->cie_inf->need_lsda_relative)
1106 buf += ent->lsda_offset;
1107 width = get_DW_EH_PE_width (ent->lsda_encoding, ptr_size);
1108 value = read_value (abfd, buf, width,
1109 get_DW_EH_PE_signed (ent->lsda_encoding));
1110 if (value)
1112 if ((ent->lsda_encoding & 0xf0) == DW_EH_PE_pcrel)
1113 value += ent->offset - ent->new_offset;
1114 else if (ent->cie_inf->need_lsda_relative)
1115 value -= (sec->output_section->vma + ent->new_offset + 8
1116 + ent->lsda_offset);
1117 write_value (abfd, buf, value, width);
1120 else if (ent->cie_inf->add_augmentation_size)
1122 /* Skip the PC and length and insert a zero byte for the
1123 augmentation size. */
1124 buf += width * 2;
1125 memmove (buf + 1, buf, end - buf);
1126 *buf = 0;
1132 unsigned int alignment = 1 << sec->alignment_power;
1133 unsigned int pad = sec->size % alignment;
1135 /* Don't pad beyond the raw size of the output section. It
1136 can happen at the last input section. */
1137 if (pad
1138 && ((sec->output_offset + sec->size + pad)
1139 <= sec->output_section->size))
1141 bfd_byte *buf;
1142 unsigned int new_size;
1144 /* Find the last CIE/FDE. */
1145 ent = sec_info->entry + sec_info->count;
1146 while (--ent != sec_info->entry)
1147 if (!ent->removed)
1148 break;
1150 /* The size of the last CIE/FDE must be at least 4. */
1151 if (ent->removed || ent->size < 4)
1152 abort ();
1154 pad = alignment - pad;
1155 buf = contents + ent->new_offset - sec->output_offset;
1156 new_size = size_of_output_cie_fde (ent, ptr_size);
1158 /* Pad it with DW_CFA_nop */
1159 memset (buf + new_size, 0, pad);
1160 bfd_put_32 (abfd, new_size + pad - 4, buf);
1162 sec->size += pad;
1166 return bfd_set_section_contents (abfd, sec->output_section,
1167 contents, (file_ptr) sec->output_offset,
1168 sec->size);
1171 /* Helper function used to sort .eh_frame_hdr search table by increasing
1172 VMA of FDE initial location. */
1174 static int
1175 vma_compare (const void *a, const void *b)
1177 const struct eh_frame_array_ent *p = a;
1178 const struct eh_frame_array_ent *q = b;
1179 if (p->initial_loc > q->initial_loc)
1180 return 1;
1181 if (p->initial_loc < q->initial_loc)
1182 return -1;
1183 return 0;
1186 /* Write out .eh_frame_hdr section. This must be called after
1187 _bfd_elf_write_section_eh_frame has been called on all input
1188 .eh_frame sections.
1189 .eh_frame_hdr format:
1190 ubyte version (currently 1)
1191 ubyte eh_frame_ptr_enc (DW_EH_PE_* encoding of pointer to start of
1192 .eh_frame section)
1193 ubyte fde_count_enc (DW_EH_PE_* encoding of total FDE count
1194 number (or DW_EH_PE_omit if there is no
1195 binary search table computed))
1196 ubyte table_enc (DW_EH_PE_* encoding of binary search table,
1197 or DW_EH_PE_omit if not present.
1198 DW_EH_PE_datarel is using address of
1199 .eh_frame_hdr section start as base)
1200 [encoded] eh_frame_ptr (pointer to start of .eh_frame section)
1201 optionally followed by:
1202 [encoded] fde_count (total number of FDEs in .eh_frame section)
1203 fde_count x [encoded] initial_loc, fde
1204 (array of encoded pairs containing
1205 FDE initial_location field and FDE address,
1206 sorted by increasing initial_loc). */
1208 bfd_boolean
1209 _bfd_elf_write_section_eh_frame_hdr (bfd *abfd, struct bfd_link_info *info)
1211 struct elf_link_hash_table *htab;
1212 struct eh_frame_hdr_info *hdr_info;
1213 asection *sec;
1214 bfd_byte *contents;
1215 asection *eh_frame_sec;
1216 bfd_size_type size;
1217 bfd_boolean retval;
1218 bfd_vma encoded_eh_frame;
1220 htab = elf_hash_table (info);
1221 hdr_info = &htab->eh_info;
1222 sec = hdr_info->hdr_sec;
1223 if (sec == NULL)
1224 return TRUE;
1226 size = EH_FRAME_HDR_SIZE;
1227 if (hdr_info->array && hdr_info->array_count == hdr_info->fde_count)
1228 size += 4 + hdr_info->fde_count * 8;
1229 contents = bfd_malloc (size);
1230 if (contents == NULL)
1231 return FALSE;
1233 eh_frame_sec = bfd_get_section_by_name (abfd, ".eh_frame");
1234 if (eh_frame_sec == NULL)
1236 free (contents);
1237 return FALSE;
1240 memset (contents, 0, EH_FRAME_HDR_SIZE);
1241 contents[0] = 1; /* Version. */
1242 contents[1] = get_elf_backend_data (abfd)->elf_backend_encode_eh_address
1243 (abfd, info, eh_frame_sec, 0, sec, 4,
1244 &encoded_eh_frame); /* .eh_frame offset. */
1246 if (hdr_info->array && hdr_info->array_count == hdr_info->fde_count)
1248 contents[2] = DW_EH_PE_udata4; /* FDE count encoding. */
1249 contents[3] = DW_EH_PE_datarel | DW_EH_PE_sdata4; /* Search table enc. */
1251 else
1253 contents[2] = DW_EH_PE_omit;
1254 contents[3] = DW_EH_PE_omit;
1256 bfd_put_32 (abfd, encoded_eh_frame, contents + 4);
1258 if (contents[2] != DW_EH_PE_omit)
1260 unsigned int i;
1262 bfd_put_32 (abfd, hdr_info->fde_count, contents + EH_FRAME_HDR_SIZE);
1263 qsort (hdr_info->array, hdr_info->fde_count, sizeof (*hdr_info->array),
1264 vma_compare);
1265 for (i = 0; i < hdr_info->fde_count; i++)
1267 bfd_put_32 (abfd,
1268 hdr_info->array[i].initial_loc
1269 - sec->output_section->vma,
1270 contents + EH_FRAME_HDR_SIZE + i * 8 + 4);
1271 bfd_put_32 (abfd,
1272 hdr_info->array[i].fde - sec->output_section->vma,
1273 contents + EH_FRAME_HDR_SIZE + i * 8 + 8);
1277 retval = bfd_set_section_contents (abfd, sec->output_section,
1278 contents, (file_ptr) sec->output_offset,
1279 sec->size);
1280 free (contents);
1281 return retval;
1284 /* Decide whether we can use a PC-relative encoding within the given
1285 EH frame section. This is the default implementation. */
1287 bfd_boolean
1288 _bfd_elf_can_make_relative (bfd *input_bfd ATTRIBUTE_UNUSED,
1289 struct bfd_link_info *info ATTRIBUTE_UNUSED,
1290 asection *eh_frame_section ATTRIBUTE_UNUSED)
1292 return TRUE;
1295 /* Select an encoding for the given address. Preference is given to
1296 PC-relative addressing modes. */
1298 bfd_byte
1299 _bfd_elf_encode_eh_address (bfd *abfd ATTRIBUTE_UNUSED,
1300 struct bfd_link_info *info ATTRIBUTE_UNUSED,
1301 asection *osec, bfd_vma offset,
1302 asection *loc_sec, bfd_vma loc_offset,
1303 bfd_vma *encoded)
1305 *encoded = osec->vma + offset -
1306 (loc_sec->output_section->vma + loc_sec->output_offset + loc_offset);
1307 return DW_EH_PE_pcrel | DW_EH_PE_sdata4;