bfd/
[binutils-gdb.git] / bfd / elf-eh-frame.c
1 /* .eh_frame section optimization.
2 Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007
3 Free Software Foundation, Inc.
4 Written by Jakub Jelinek <jakub@redhat.com>.
5
6 This file is part of BFD, the Binary File Descriptor library.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
22
23 #include "sysdep.h"
24 #include "bfd.h"
25 #include "libbfd.h"
26 #include "elf-bfd.h"
27 #include "elf/dwarf2.h"
28
29 #define EH_FRAME_HDR_SIZE 8
30
31 struct cie
32 {
33 unsigned int length;
34 unsigned int hash;
35 unsigned char version;
36 unsigned char local_personality;
37 char augmentation[20];
38 bfd_vma code_align;
39 bfd_signed_vma data_align;
40 bfd_vma ra_column;
41 bfd_vma augmentation_size;
42 union {
43 struct elf_link_hash_entry *h;
44 bfd_vma val;
45 } personality;
46 asection *output_sec;
47 struct eh_cie_fde *cie_inf;
48 unsigned char per_encoding;
49 unsigned char lsda_encoding;
50 unsigned char fde_encoding;
51 unsigned char initial_insn_length;
52 unsigned char can_make_lsda_relative;
53 unsigned char initial_instructions[50];
54 };
55
56
57
58 /* If *ITER hasn't reached END yet, read the next byte into *RESULT and
59 move onto the next byte. Return true on success. */
60
61 static inline bfd_boolean
62 read_byte (bfd_byte **iter, bfd_byte *end, unsigned char *result)
63 {
64 if (*iter >= end)
65 return FALSE;
66 *result = *((*iter)++);
67 return TRUE;
68 }
69
70 /* Move *ITER over LENGTH bytes, or up to END, whichever is closer.
71 Return true it was possible to move LENGTH bytes. */
72
73 static inline bfd_boolean
74 skip_bytes (bfd_byte **iter, bfd_byte *end, bfd_size_type length)
75 {
76 if ((bfd_size_type) (end - *iter) < length)
77 {
78 *iter = end;
79 return FALSE;
80 }
81 *iter += length;
82 return TRUE;
83 }
84
85 /* Move *ITER over an leb128, stopping at END. Return true if the end
86 of the leb128 was found. */
87
88 static bfd_boolean
89 skip_leb128 (bfd_byte **iter, bfd_byte *end)
90 {
91 unsigned char byte;
92 do
93 if (!read_byte (iter, end, &byte))
94 return FALSE;
95 while (byte & 0x80);
96 return TRUE;
97 }
98
99 /* Like skip_leb128, but treat the leb128 as an unsigned value and
100 store it in *VALUE. */
101
102 static bfd_boolean
103 read_uleb128 (bfd_byte **iter, bfd_byte *end, bfd_vma *value)
104 {
105 bfd_byte *start, *p;
106
107 start = *iter;
108 if (!skip_leb128 (iter, end))
109 return FALSE;
110
111 p = *iter;
112 *value = *--p;
113 while (p > start)
114 *value = (*value << 7) | (*--p & 0x7f);
115
116 return TRUE;
117 }
118
119 /* Like read_uleb128, but for signed values. */
120
121 static bfd_boolean
122 read_sleb128 (bfd_byte **iter, bfd_byte *end, bfd_signed_vma *value)
123 {
124 bfd_byte *start, *p;
125
126 start = *iter;
127 if (!skip_leb128 (iter, end))
128 return FALSE;
129
130 p = *iter;
131 *value = ((*--p & 0x7f) ^ 0x40) - 0x40;
132 while (p > start)
133 *value = (*value << 7) | (*--p & 0x7f);
134
135 return TRUE;
136 }
137
138 /* Return 0 if either encoding is variable width, or not yet known to bfd. */
139
140 static
141 int get_DW_EH_PE_width (int encoding, int ptr_size)
142 {
143 /* DW_EH_PE_ values of 0x60 and 0x70 weren't defined at the time .eh_frame
144 was added to bfd. */
145 if ((encoding & 0x60) == 0x60)
146 return 0;
147
148 switch (encoding & 7)
149 {
150 case DW_EH_PE_udata2: return 2;
151 case DW_EH_PE_udata4: return 4;
152 case DW_EH_PE_udata8: return 8;
153 case DW_EH_PE_absptr: return ptr_size;
154 default:
155 break;
156 }
157
158 return 0;
159 }
160
161 #define get_DW_EH_PE_signed(encoding) (((encoding) & DW_EH_PE_signed) != 0)
162
163 /* Read a width sized value from memory. */
164
165 static bfd_vma
166 read_value (bfd *abfd, bfd_byte *buf, int width, int is_signed)
167 {
168 bfd_vma value;
169
170 switch (width)
171 {
172 case 2:
173 if (is_signed)
174 value = bfd_get_signed_16 (abfd, buf);
175 else
176 value = bfd_get_16 (abfd, buf);
177 break;
178 case 4:
179 if (is_signed)
180 value = bfd_get_signed_32 (abfd, buf);
181 else
182 value = bfd_get_32 (abfd, buf);
183 break;
184 case 8:
185 if (is_signed)
186 value = bfd_get_signed_64 (abfd, buf);
187 else
188 value = bfd_get_64 (abfd, buf);
189 break;
190 default:
191 BFD_FAIL ();
192 return 0;
193 }
194
195 return value;
196 }
197
198 /* Store a width sized value to memory. */
199
200 static void
201 write_value (bfd *abfd, bfd_byte *buf, bfd_vma value, int width)
202 {
203 switch (width)
204 {
205 case 2: bfd_put_16 (abfd, value, buf); break;
206 case 4: bfd_put_32 (abfd, value, buf); break;
207 case 8: bfd_put_64 (abfd, value, buf); break;
208 default: BFD_FAIL ();
209 }
210 }
211
212 /* Return one if C1 and C2 CIEs can be merged. */
213
214 static int
215 cie_eq (const void *e1, const void *e2)
216 {
217 const struct cie *c1 = e1;
218 const struct cie *c2 = e2;
219
220 if (c1->hash == c2->hash
221 && c1->length == c2->length
222 && c1->version == c2->version
223 && c1->local_personality == c2->local_personality
224 && strcmp (c1->augmentation, c2->augmentation) == 0
225 && strcmp (c1->augmentation, "eh") != 0
226 && c1->code_align == c2->code_align
227 && c1->data_align == c2->data_align
228 && c1->ra_column == c2->ra_column
229 && c1->augmentation_size == c2->augmentation_size
230 && memcmp (&c1->personality, &c2->personality,
231 sizeof (c1->personality)) == 0
232 && c1->output_sec == c2->output_sec
233 && c1->per_encoding == c2->per_encoding
234 && c1->lsda_encoding == c2->lsda_encoding
235 && c1->fde_encoding == c2->fde_encoding
236 && c1->initial_insn_length == c2->initial_insn_length
237 && memcmp (c1->initial_instructions,
238 c2->initial_instructions,
239 c1->initial_insn_length) == 0)
240 return 1;
241
242 return 0;
243 }
244
245 static hashval_t
246 cie_hash (const void *e)
247 {
248 const struct cie *c = e;
249 return c->hash;
250 }
251
252 static hashval_t
253 cie_compute_hash (struct cie *c)
254 {
255 hashval_t h = 0;
256 h = iterative_hash_object (c->length, h);
257 h = iterative_hash_object (c->version, h);
258 h = iterative_hash (c->augmentation, strlen (c->augmentation) + 1, h);
259 h = iterative_hash_object (c->code_align, h);
260 h = iterative_hash_object (c->data_align, h);
261 h = iterative_hash_object (c->ra_column, h);
262 h = iterative_hash_object (c->augmentation_size, h);
263 h = iterative_hash_object (c->personality, h);
264 h = iterative_hash_object (c->output_sec, h);
265 h = iterative_hash_object (c->per_encoding, h);
266 h = iterative_hash_object (c->lsda_encoding, h);
267 h = iterative_hash_object (c->fde_encoding, h);
268 h = iterative_hash_object (c->initial_insn_length, h);
269 h = iterative_hash (c->initial_instructions, c->initial_insn_length, h);
270 c->hash = h;
271 return h;
272 }
273
274 /* Return the number of extra bytes that we'll be inserting into
275 ENTRY's augmentation string. */
276
277 static INLINE unsigned int
278 extra_augmentation_string_bytes (struct eh_cie_fde *entry)
279 {
280 unsigned int size = 0;
281 if (entry->cie)
282 {
283 if (entry->add_augmentation_size)
284 size++;
285 if (entry->u.cie.add_fde_encoding)
286 size++;
287 }
288 return size;
289 }
290
291 /* Likewise ENTRY's augmentation data. */
292
293 static INLINE unsigned int
294 extra_augmentation_data_bytes (struct eh_cie_fde *entry)
295 {
296 unsigned int size = 0;
297 if (entry->add_augmentation_size)
298 size++;
299 if (entry->cie && entry->u.cie.add_fde_encoding)
300 size++;
301 return size;
302 }
303
304 /* Return the size that ENTRY will have in the output. ALIGNMENT is the
305 required alignment of ENTRY in bytes. */
306
307 static unsigned int
308 size_of_output_cie_fde (struct eh_cie_fde *entry, unsigned int alignment)
309 {
310 if (entry->removed)
311 return 0;
312 if (entry->size == 4)
313 return 4;
314 return (entry->size
315 + extra_augmentation_string_bytes (entry)
316 + extra_augmentation_data_bytes (entry)
317 + alignment - 1) & -alignment;
318 }
319
320 /* Assume that the bytes between *ITER and END are CFA instructions.
321 Try to move *ITER past the first instruction and return true on
322 success. ENCODED_PTR_WIDTH gives the width of pointer entries. */
323
324 static bfd_boolean
325 skip_cfa_op (bfd_byte **iter, bfd_byte *end, unsigned int encoded_ptr_width)
326 {
327 bfd_byte op;
328 bfd_vma length;
329
330 if (!read_byte (iter, end, &op))
331 return FALSE;
332
333 switch (op & 0xc0 ? op & 0xc0 : op)
334 {
335 case DW_CFA_nop:
336 case DW_CFA_advance_loc:
337 case DW_CFA_restore:
338 case DW_CFA_remember_state:
339 case DW_CFA_restore_state:
340 case DW_CFA_GNU_window_save:
341 /* No arguments. */
342 return TRUE;
343
344 case DW_CFA_offset:
345 case DW_CFA_restore_extended:
346 case DW_CFA_undefined:
347 case DW_CFA_same_value:
348 case DW_CFA_def_cfa_register:
349 case DW_CFA_def_cfa_offset:
350 case DW_CFA_def_cfa_offset_sf:
351 case DW_CFA_GNU_args_size:
352 /* One leb128 argument. */
353 return skip_leb128 (iter, end);
354
355 case DW_CFA_val_offset:
356 case DW_CFA_val_offset_sf:
357 case DW_CFA_offset_extended:
358 case DW_CFA_register:
359 case DW_CFA_def_cfa:
360 case DW_CFA_offset_extended_sf:
361 case DW_CFA_GNU_negative_offset_extended:
362 case DW_CFA_def_cfa_sf:
363 /* Two leb128 arguments. */
364 return (skip_leb128 (iter, end)
365 && skip_leb128 (iter, end));
366
367 case DW_CFA_def_cfa_expression:
368 /* A variable-length argument. */
369 return (read_uleb128 (iter, end, &length)
370 && skip_bytes (iter, end, length));
371
372 case DW_CFA_expression:
373 case DW_CFA_val_expression:
374 /* A leb128 followed by a variable-length argument. */
375 return (skip_leb128 (iter, end)
376 && read_uleb128 (iter, end, &length)
377 && skip_bytes (iter, end, length));
378
379 case DW_CFA_set_loc:
380 return skip_bytes (iter, end, encoded_ptr_width);
381
382 case DW_CFA_advance_loc1:
383 return skip_bytes (iter, end, 1);
384
385 case DW_CFA_advance_loc2:
386 return skip_bytes (iter, end, 2);
387
388 case DW_CFA_advance_loc4:
389 return skip_bytes (iter, end, 4);
390
391 case DW_CFA_MIPS_advance_loc8:
392 return skip_bytes (iter, end, 8);
393
394 default:
395 return FALSE;
396 }
397 }
398
399 /* Try to interpret the bytes between BUF and END as CFA instructions.
400 If every byte makes sense, return a pointer to the first DW_CFA_nop
401 padding byte, or END if there is no padding. Return null otherwise.
402 ENCODED_PTR_WIDTH is as for skip_cfa_op. */
403
404 static bfd_byte *
405 skip_non_nops (bfd_byte *buf, bfd_byte *end, unsigned int encoded_ptr_width,
406 unsigned int *set_loc_count)
407 {
408 bfd_byte *last;
409
410 last = buf;
411 while (buf < end)
412 if (*buf == DW_CFA_nop)
413 buf++;
414 else
415 {
416 if (*buf == DW_CFA_set_loc)
417 ++*set_loc_count;
418 if (!skip_cfa_op (&buf, end, encoded_ptr_width))
419 return 0;
420 last = buf;
421 }
422 return last;
423 }
424
425 /* Called before calling _bfd_elf_parse_eh_frame on every input bfd's
426 .eh_frame section. */
427
428 void
429 _bfd_elf_begin_eh_frame_parsing (struct bfd_link_info *info)
430 {
431 struct eh_frame_hdr_info *hdr_info;
432
433 hdr_info = &elf_hash_table (info)->eh_info;
434 if (!hdr_info->parsed_eh_frames && !info->relocatable)
435 hdr_info->cies = htab_try_create (1, cie_hash, cie_eq, free);
436 }
437
438 /* Try to parse .eh_frame section SEC, which belongs to ABFD. Store the
439 information in the section's sec_info field on success. COOKIE
440 describes the relocations in SEC. */
441
442 void
443 _bfd_elf_parse_eh_frame (bfd *abfd, struct bfd_link_info *info,
444 asection *sec, struct elf_reloc_cookie *cookie)
445 {
446 #define REQUIRE(COND) \
447 do \
448 if (!(COND)) \
449 goto free_no_table; \
450 while (0)
451
452 bfd_byte *ehbuf = NULL, *buf, *end;
453 bfd_byte *last_fde;
454 struct eh_cie_fde *this_inf;
455 unsigned int hdr_length, hdr_id;
456 struct extended_cie
457 {
458 struct cie *cie;
459 struct eh_cie_fde *local_cie;
460 } *ecies = NULL, *ecie;
461 unsigned int ecie_count;
462 struct cie *cie, *local_cies = NULL, tmp_cie;
463 struct elf_link_hash_table *htab;
464 struct eh_frame_hdr_info *hdr_info;
465 struct eh_frame_sec_info *sec_info = NULL;
466 unsigned int ptr_size;
467 unsigned int num_cies;
468 unsigned int num_entries;
469 elf_gc_mark_hook_fn gc_mark_hook;
470
471 htab = elf_hash_table (info);
472 hdr_info = &htab->eh_info;
473 if (hdr_info->parsed_eh_frames)
474 return;
475
476 if (sec->size == 0)
477 {
478 /* This file does not contain .eh_frame information. */
479 return;
480 }
481
482 if (bfd_is_abs_section (sec->output_section))
483 {
484 /* At least one of the sections is being discarded from the
485 link, so we should just ignore them. */
486 return;
487 }
488
489 /* Read the frame unwind information from abfd. */
490
491 REQUIRE (bfd_malloc_and_get_section (abfd, sec, &ehbuf));
492
493 if (sec->size >= 4
494 && bfd_get_32 (abfd, ehbuf) == 0
495 && cookie->rel == cookie->relend)
496 {
497 /* Empty .eh_frame section. */
498 free (ehbuf);
499 return;
500 }
501
502 /* If .eh_frame section size doesn't fit into int, we cannot handle
503 it (it would need to use 64-bit .eh_frame format anyway). */
504 REQUIRE (sec->size == (unsigned int) sec->size);
505
506 ptr_size = (get_elf_backend_data (abfd)
507 ->elf_backend_eh_frame_address_size (abfd, sec));
508 REQUIRE (ptr_size != 0);
509
510 /* Go through the section contents and work out how many FDEs and
511 CIEs there are. */
512 buf = ehbuf;
513 end = ehbuf + sec->size;
514 num_cies = 0;
515 num_entries = 0;
516 while (buf != end)
517 {
518 num_entries++;
519
520 /* Read the length of the entry. */
521 REQUIRE (skip_bytes (&buf, end, 4));
522 hdr_length = bfd_get_32 (abfd, buf - 4);
523
524 /* 64-bit .eh_frame is not supported. */
525 REQUIRE (hdr_length != 0xffffffff);
526 if (hdr_length == 0)
527 break;
528
529 REQUIRE (skip_bytes (&buf, end, 4));
530 hdr_id = bfd_get_32 (abfd, buf - 4);
531 if (hdr_id == 0)
532 num_cies++;
533
534 REQUIRE (skip_bytes (&buf, end, hdr_length - 4));
535 }
536
537 sec_info = bfd_zmalloc (sizeof (struct eh_frame_sec_info)
538 + (num_entries - 1) * sizeof (struct eh_cie_fde));
539 REQUIRE (sec_info);
540
541 ecies = bfd_zmalloc (num_cies * sizeof (*ecies));
542 REQUIRE (ecies);
543
544 /* If we're not merging CIE entries (such as for a relocatable link),
545 we need to have a "struct cie" for each CIE in this section. */
546 if (hdr_info->cies == NULL)
547 {
548 local_cies = bfd_zmalloc (num_cies * sizeof (*local_cies));
549 REQUIRE (local_cies);
550 }
551
552 #define ENSURE_NO_RELOCS(buf) \
553 REQUIRE (!(cookie->rel < cookie->relend \
554 && (cookie->rel->r_offset \
555 < (bfd_size_type) ((buf) - ehbuf)) \
556 && cookie->rel->r_info != 0))
557
558 #define SKIP_RELOCS(buf) \
559 while (cookie->rel < cookie->relend \
560 && (cookie->rel->r_offset \
561 < (bfd_size_type) ((buf) - ehbuf))) \
562 cookie->rel++
563
564 #define GET_RELOC(buf) \
565 ((cookie->rel < cookie->relend \
566 && (cookie->rel->r_offset \
567 == (bfd_size_type) ((buf) - ehbuf))) \
568 ? cookie->rel : NULL)
569
570 buf = ehbuf;
571 ecie_count = 0;
572 gc_mark_hook = get_elf_backend_data (abfd)->gc_mark_hook;
573 while ((bfd_size_type) (buf - ehbuf) != sec->size)
574 {
575 char *aug;
576 bfd_byte *start, *insns, *insns_end;
577 bfd_size_type length;
578 unsigned int set_loc_count;
579
580 this_inf = sec_info->entry + sec_info->count;
581 last_fde = buf;
582
583 /* Read the length of the entry. */
584 REQUIRE (skip_bytes (&buf, ehbuf + sec->size, 4));
585 hdr_length = bfd_get_32 (abfd, buf - 4);
586
587 /* The CIE/FDE must be fully contained in this input section. */
588 REQUIRE ((bfd_size_type) (buf - ehbuf) + hdr_length <= sec->size);
589 end = buf + hdr_length;
590
591 this_inf->offset = last_fde - ehbuf;
592 this_inf->size = 4 + hdr_length;
593 this_inf->reloc_index = cookie->rel - cookie->rels;
594
595 if (hdr_length == 0)
596 {
597 /* A zero-length CIE should only be found at the end of
598 the section. */
599 REQUIRE ((bfd_size_type) (buf - ehbuf) == sec->size);
600 ENSURE_NO_RELOCS (buf);
601 sec_info->count++;
602 break;
603 }
604
605 REQUIRE (skip_bytes (&buf, end, 4));
606 hdr_id = bfd_get_32 (abfd, buf - 4);
607
608 if (hdr_id == 0)
609 {
610 unsigned int initial_insn_length;
611
612 /* CIE */
613 this_inf->cie = 1;
614
615 /* If we're merging CIEs, construct the struct cie in TMP_CIE;
616 we'll enter it into the global pool later. Otherwise point
617 CIE to one of the section-local cie structures. */
618 if (local_cies)
619 cie = local_cies + ecie_count;
620 else
621 {
622 cie = &tmp_cie;
623 memset (cie, 0, sizeof (*cie));
624 }
625 cie->cie_inf = this_inf;
626 cie->length = hdr_length;
627 cie->output_sec = sec->output_section;
628 start = buf;
629 REQUIRE (read_byte (&buf, end, &cie->version));
630
631 /* Cannot handle unknown versions. */
632 REQUIRE (cie->version == 1 || cie->version == 3);
633 REQUIRE (strlen ((char *) buf) < sizeof (cie->augmentation));
634
635 strcpy (cie->augmentation, (char *) buf);
636 buf = (bfd_byte *) strchr ((char *) buf, '\0') + 1;
637 ENSURE_NO_RELOCS (buf);
638 if (buf[0] == 'e' && buf[1] == 'h')
639 {
640 /* GCC < 3.0 .eh_frame CIE */
641 /* We cannot merge "eh" CIEs because __EXCEPTION_TABLE__
642 is private to each CIE, so we don't need it for anything.
643 Just skip it. */
644 REQUIRE (skip_bytes (&buf, end, ptr_size));
645 SKIP_RELOCS (buf);
646 }
647 REQUIRE (read_uleb128 (&buf, end, &cie->code_align));
648 REQUIRE (read_sleb128 (&buf, end, &cie->data_align));
649 if (cie->version == 1)
650 {
651 REQUIRE (buf < end);
652 cie->ra_column = *buf++;
653 }
654 else
655 REQUIRE (read_uleb128 (&buf, end, &cie->ra_column));
656 ENSURE_NO_RELOCS (buf);
657 cie->lsda_encoding = DW_EH_PE_omit;
658 cie->fde_encoding = DW_EH_PE_omit;
659 cie->per_encoding = DW_EH_PE_omit;
660 aug = cie->augmentation;
661 if (aug[0] != 'e' || aug[1] != 'h')
662 {
663 if (*aug == 'z')
664 {
665 aug++;
666 REQUIRE (read_uleb128 (&buf, end, &cie->augmentation_size));
667 ENSURE_NO_RELOCS (buf);
668 }
669
670 while (*aug != '\0')
671 switch (*aug++)
672 {
673 case 'L':
674 REQUIRE (read_byte (&buf, end, &cie->lsda_encoding));
675 ENSURE_NO_RELOCS (buf);
676 REQUIRE (get_DW_EH_PE_width (cie->lsda_encoding, ptr_size));
677 break;
678 case 'R':
679 REQUIRE (read_byte (&buf, end, &cie->fde_encoding));
680 ENSURE_NO_RELOCS (buf);
681 REQUIRE (get_DW_EH_PE_width (cie->fde_encoding, ptr_size));
682 break;
683 case 'S':
684 break;
685 case 'P':
686 {
687 int per_width;
688
689 REQUIRE (read_byte (&buf, end, &cie->per_encoding));
690 per_width = get_DW_EH_PE_width (cie->per_encoding,
691 ptr_size);
692 REQUIRE (per_width);
693 if ((cie->per_encoding & 0xf0) == DW_EH_PE_aligned)
694 {
695 length = -(buf - ehbuf) & (per_width - 1);
696 REQUIRE (skip_bytes (&buf, end, length));
697 }
698 ENSURE_NO_RELOCS (buf);
699 /* Ensure we have a reloc here. */
700 if (GET_RELOC (buf) != NULL)
701 {
702 unsigned long r_symndx;
703
704 #ifdef BFD64
705 if (elf_elfheader (abfd)->e_ident[EI_CLASS]
706 == ELFCLASS64)
707 r_symndx = ELF64_R_SYM (cookie->rel->r_info);
708 else
709 #endif
710 r_symndx = ELF32_R_SYM (cookie->rel->r_info);
711 if (r_symndx >= cookie->locsymcount
712 || ELF_ST_BIND (cookie->locsyms[r_symndx]
713 .st_info) != STB_LOCAL)
714 {
715 struct elf_link_hash_entry *h;
716
717 r_symndx -= cookie->extsymoff;
718 h = cookie->sym_hashes[r_symndx];
719
720 while (h->root.type == bfd_link_hash_indirect
721 || h->root.type == bfd_link_hash_warning)
722 h = (struct elf_link_hash_entry *)
723 h->root.u.i.link;
724
725 cie->personality.h = h;
726 }
727 else
728 {
729 Elf_Internal_Sym *sym;
730 asection *sym_sec;
731 bfd_vma val;
732
733 sym = &cookie->locsyms[r_symndx];
734 sym_sec = (bfd_section_from_elf_index
735 (abfd, sym->st_shndx));
736 if (sym_sec != NULL)
737 {
738 if (sym_sec->kept_section != NULL)
739 sym_sec = sym_sec->kept_section;
740 if (sym_sec->output_section != NULL)
741 {
742 val = (sym->st_value
743 + sym_sec->output_offset
744 + sym_sec->output_section->vma);
745 cie->personality.val = val;
746 cie->local_personality = 1;
747 }
748 }
749 }
750
751 /* Cope with MIPS-style composite relocations. */
752 do
753 cookie->rel++;
754 while (GET_RELOC (buf) != NULL);
755 }
756 REQUIRE (skip_bytes (&buf, end, per_width));
757 REQUIRE (cie->local_personality || cie->personality.h);
758 }
759 break;
760 default:
761 /* Unrecognized augmentation. Better bail out. */
762 goto free_no_table;
763 }
764 }
765
766 /* For shared libraries, try to get rid of as many RELATIVE relocs
767 as possible. */
768 if (info->shared
769 && (get_elf_backend_data (abfd)
770 ->elf_backend_can_make_relative_eh_frame
771 (abfd, info, sec)))
772 {
773 if ((cie->fde_encoding & 0xf0) == DW_EH_PE_absptr)
774 this_inf->make_relative = 1;
775 /* If the CIE doesn't already have an 'R' entry, it's fairly
776 easy to add one, provided that there's no aligned data
777 after the augmentation string. */
778 else if (cie->fde_encoding == DW_EH_PE_omit
779 && (cie->per_encoding & 0xf0) != DW_EH_PE_aligned)
780 {
781 if (*cie->augmentation == 0)
782 this_inf->add_augmentation_size = 1;
783 this_inf->u.cie.add_fde_encoding = 1;
784 this_inf->make_relative = 1;
785 }
786 }
787
788 if (info->shared
789 && (get_elf_backend_data (abfd)
790 ->elf_backend_can_make_lsda_relative_eh_frame
791 (abfd, info, sec))
792 && (cie->lsda_encoding & 0xf0) == DW_EH_PE_absptr)
793 cie->can_make_lsda_relative = 1;
794
795 /* If FDE encoding was not specified, it defaults to
796 DW_EH_absptr. */
797 if (cie->fde_encoding == DW_EH_PE_omit)
798 cie->fde_encoding = DW_EH_PE_absptr;
799
800 initial_insn_length = end - buf;
801 if (initial_insn_length <= sizeof (cie->initial_instructions))
802 {
803 cie->initial_insn_length = initial_insn_length;
804 memcpy (cie->initial_instructions, buf, initial_insn_length);
805 }
806 insns = buf;
807 buf += initial_insn_length;
808 ENSURE_NO_RELOCS (buf);
809
810 this_inf->u.cie.per_encoding_relative
811 = (cie->per_encoding & 0x70) == DW_EH_PE_pcrel;
812 }
813 else
814 {
815 asection *rsec;
816
817 /* Find the corresponding CIE. */
818 unsigned int cie_offset = this_inf->offset + 4 - hdr_id;
819 for (ecie = ecies; ecie < ecies + ecie_count; ++ecie)
820 if (cie_offset == ecie->local_cie->offset)
821 break;
822
823 /* Ensure this FDE references one of the CIEs in this input
824 section. */
825 REQUIRE (ecie != ecies + ecie_count);
826 cie = ecie->cie;
827 this_inf->u.fde.cie_inf = ecie->local_cie;
828 this_inf->make_relative = ecie->local_cie->make_relative;
829 this_inf->add_augmentation_size
830 = ecie->local_cie->add_augmentation_size;
831
832 ENSURE_NO_RELOCS (buf);
833 REQUIRE (GET_RELOC (buf));
834
835 /* Chain together the FDEs for each section. */
836 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie);
837 REQUIRE (rsec && rsec->owner == abfd);
838 this_inf->u.fde.next_for_section = elf_fde_list (rsec);
839 elf_fde_list (rsec) = this_inf;
840
841 /* Skip the initial location and address range. */
842 start = buf;
843 length = get_DW_EH_PE_width (cie->fde_encoding, ptr_size);
844 REQUIRE (skip_bytes (&buf, end, 2 * length));
845
846 /* Skip the augmentation size, if present. */
847 if (cie->augmentation[0] == 'z')
848 REQUIRE (read_uleb128 (&buf, end, &length));
849 else
850 length = 0;
851
852 /* Of the supported augmentation characters above, only 'L'
853 adds augmentation data to the FDE. This code would need to
854 be adjusted if any future augmentations do the same thing. */
855 if (cie->lsda_encoding != DW_EH_PE_omit)
856 {
857 SKIP_RELOCS (buf);
858 if (cie->can_make_lsda_relative && GET_RELOC (buf))
859 cie->cie_inf->u.cie.make_lsda_relative = 1;
860 this_inf->lsda_offset = buf - start;
861 /* If there's no 'z' augmentation, we don't know where the
862 CFA insns begin. Assume no padding. */
863 if (cie->augmentation[0] != 'z')
864 length = end - buf;
865 }
866
867 /* Skip over the augmentation data. */
868 REQUIRE (skip_bytes (&buf, end, length));
869 insns = buf;
870
871 buf = last_fde + 4 + hdr_length;
872 SKIP_RELOCS (buf);
873 }
874
875 /* Try to interpret the CFA instructions and find the first
876 padding nop. Shrink this_inf's size so that it doesn't
877 include the padding. */
878 length = get_DW_EH_PE_width (cie->fde_encoding, ptr_size);
879 set_loc_count = 0;
880 insns_end = skip_non_nops (insns, end, length, &set_loc_count);
881 /* If we don't understand the CFA instructions, we can't know
882 what needs to be adjusted there. */
883 if (insns_end == NULL
884 /* For the time being we don't support DW_CFA_set_loc in
885 CIE instructions. */
886 || (set_loc_count && this_inf->cie))
887 goto free_no_table;
888 this_inf->size -= end - insns_end;
889 if (insns_end != end && this_inf->cie)
890 {
891 cie->initial_insn_length -= end - insns_end;
892 cie->length -= end - insns_end;
893 }
894 if (set_loc_count
895 && ((cie->fde_encoding & 0xf0) == DW_EH_PE_pcrel
896 || this_inf->make_relative))
897 {
898 unsigned int cnt;
899 bfd_byte *p;
900
901 this_inf->set_loc = bfd_malloc ((set_loc_count + 1)
902 * sizeof (unsigned int));
903 REQUIRE (this_inf->set_loc);
904 this_inf->set_loc[0] = set_loc_count;
905 p = insns;
906 cnt = 0;
907 while (p < end)
908 {
909 if (*p == DW_CFA_set_loc)
910 this_inf->set_loc[++cnt] = p + 1 - start;
911 REQUIRE (skip_cfa_op (&p, end, length));
912 }
913 }
914
915 this_inf->removed = 1;
916 this_inf->fde_encoding = cie->fde_encoding;
917 this_inf->lsda_encoding = cie->lsda_encoding;
918 if (this_inf->cie)
919 {
920 /* We have now finished constructing the struct cie. */
921 if (hdr_info->cies != NULL)
922 {
923 /* See if we can merge this CIE with an earlier one. */
924 void **loc;
925
926 cie_compute_hash (cie);
927 loc = htab_find_slot_with_hash (hdr_info->cies, cie,
928 cie->hash, INSERT);
929 REQUIRE (loc);
930 if (*loc == HTAB_EMPTY_ENTRY)
931 {
932 *loc = malloc (sizeof (struct cie));
933 REQUIRE (*loc);
934 memcpy (*loc, cie, sizeof (struct cie));
935 }
936 cie = (struct cie *) *loc;
937 }
938 this_inf->u.cie.u.merged = cie->cie_inf;
939 ecies[ecie_count].cie = cie;
940 ecies[ecie_count++].local_cie = this_inf;
941 }
942 sec_info->count++;
943 }
944 BFD_ASSERT (sec_info->count == num_entries);
945 BFD_ASSERT (ecie_count == num_cies);
946
947 elf_section_data (sec)->sec_info = sec_info;
948 sec->sec_info_type = ELF_INFO_TYPE_EH_FRAME;
949 goto success;
950
951 free_no_table:
952 (*info->callbacks->einfo)
953 (_("%P: error in %B(%A); no .eh_frame_hdr table will be created.\n"),
954 abfd, sec);
955 hdr_info->table = FALSE;
956 if (sec_info)
957 free (sec_info);
958 success:
959 if (ehbuf)
960 free (ehbuf);
961 if (ecies)
962 free (ecies);
963 if (local_cies)
964 free (local_cies);
965 #undef REQUIRE
966 }
967
968 /* Finish a pass over all .eh_frame sections. */
969
970 void
971 _bfd_elf_end_eh_frame_parsing (struct bfd_link_info *info)
972 {
973 struct eh_frame_hdr_info *hdr_info;
974
975 hdr_info = &elf_hash_table (info)->eh_info;
976 if (hdr_info->cies != NULL)
977 {
978 htab_delete (hdr_info->cies);
979 hdr_info->cies = NULL;
980 }
981 hdr_info->parsed_eh_frames = TRUE;
982 }
983
984 /* Mark all relocations against CIE or FDE ENT, which occurs in
985 .eh_frame section SEC. COOKIE describes the relocations in SEC;
986 its "rel" field can be changed freely. */
987
988 static bfd_boolean
989 mark_entry (struct bfd_link_info *info, asection *sec,
990 struct eh_cie_fde *ent, elf_gc_mark_hook_fn gc_mark_hook,
991 struct elf_reloc_cookie *cookie)
992 {
993 for (cookie->rel = cookie->rels + ent->reloc_index;
994 cookie->rel < cookie->relend
995 && cookie->rel->r_offset < ent->offset + ent->size;
996 cookie->rel++)
997 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, cookie))
998 return FALSE;
999
1000 return TRUE;
1001 }
1002
1003 /* Mark all the relocations against FDEs that relate to code in input
1004 section SEC. The FDEs belong to .eh_frame section EH_FRAME, whose
1005 relocations are described by COOKIE. */
1006
1007 bfd_boolean
1008 _bfd_elf_gc_mark_fdes (struct bfd_link_info *info, asection *sec,
1009 asection *eh_frame, elf_gc_mark_hook_fn gc_mark_hook,
1010 struct elf_reloc_cookie *cookie)
1011 {
1012 struct eh_cie_fde *fde, *cie, *merged;
1013
1014 for (fde = elf_fde_list (sec); fde; fde = fde->u.fde.next_for_section)
1015 {
1016 if (!mark_entry (info, eh_frame, fde, gc_mark_hook, cookie))
1017 return FALSE;
1018
1019 /* At this stage, all cie_inf fields point to local CIEs, so we
1020 can use the same cookie to refer to them. */
1021 cie = fde->u.fde.cie_inf;
1022 merged = cie->u.cie.u.merged;
1023 if (!merged->u.cie.gc_mark)
1024 {
1025 merged->u.cie.gc_mark = 1;
1026 if (!mark_entry (info, eh_frame, cie, gc_mark_hook, cookie))
1027 return FALSE;
1028 }
1029 }
1030 return TRUE;
1031 }
1032
1033 /* This function is called for each input file before the .eh_frame
1034 section is relocated. It discards duplicate CIEs and FDEs for discarded
1035 functions. The function returns TRUE iff any entries have been
1036 deleted. */
1037
1038 bfd_boolean
1039 _bfd_elf_discard_section_eh_frame
1040 (bfd *abfd, struct bfd_link_info *info, asection *sec,
1041 bfd_boolean (*reloc_symbol_deleted_p) (bfd_vma, void *),
1042 struct elf_reloc_cookie *cookie)
1043 {
1044 struct eh_cie_fde *ent, *cie, *merged;
1045 struct eh_frame_sec_info *sec_info;
1046 struct eh_frame_hdr_info *hdr_info;
1047 unsigned int ptr_size, offset;
1048
1049 sec_info = (struct eh_frame_sec_info *) elf_section_data (sec)->sec_info;
1050 if (sec_info == NULL)
1051 return FALSE;
1052
1053 hdr_info = &elf_hash_table (info)->eh_info;
1054 for (ent = sec_info->entry; ent < sec_info->entry + sec_info->count; ++ent)
1055 if (!ent->cie)
1056 {
1057 cookie->rel = cookie->rels + ent->reloc_index;
1058 BFD_ASSERT (cookie->rel < cookie->relend
1059 && cookie->rel->r_offset == ent->offset + 8);
1060 if (!(*reloc_symbol_deleted_p) (ent->offset + 8, cookie))
1061 {
1062 if (info->shared
1063 && (((ent->fde_encoding & 0xf0) == DW_EH_PE_absptr
1064 && ent->make_relative == 0)
1065 || (ent->fde_encoding & 0xf0) == DW_EH_PE_aligned))
1066 {
1067 /* If a shared library uses absolute pointers
1068 which we cannot turn into PC relative,
1069 don't create the binary search table,
1070 since it is affected by runtime relocations. */
1071 hdr_info->table = FALSE;
1072 (*info->callbacks->einfo)
1073 (_("%P: fde encoding in %B(%A) prevents .eh_frame_hdr"
1074 " table being created.\n"), abfd, sec);
1075 }
1076 ent->removed = 0;
1077 hdr_info->fde_count++;
1078
1079 cie = ent->u.fde.cie_inf;
1080 if (cie->removed)
1081 {
1082 merged = cie->u.cie.u.merged;
1083 if (!merged->removed)
1084 /* We have decided to keep the group representative. */
1085 ent->u.fde.cie_inf = merged;
1086 else if (merged->u.cie.u.merged != merged)
1087 /* We didn't keep the original group representative,
1088 but we did keep an alternative. */
1089 ent->u.fde.cie_inf = merged->u.cie.u.merged;
1090 else
1091 {
1092 /* Make the local CIE represent the merged group. */
1093 merged->u.cie.u.merged = cie;
1094 cie->removed = 0;
1095 cie->u.cie.u.sec = sec;
1096 cie->u.cie.make_lsda_relative
1097 = merged->u.cie.make_lsda_relative;
1098 }
1099 }
1100 }
1101 }
1102
1103 ptr_size = (get_elf_backend_data (sec->owner)
1104 ->elf_backend_eh_frame_address_size (sec->owner, sec));
1105 offset = 0;
1106 for (ent = sec_info->entry; ent < sec_info->entry + sec_info->count; ++ent)
1107 if (!ent->removed)
1108 {
1109 ent->new_offset = offset;
1110 offset += size_of_output_cie_fde (ent, ptr_size);
1111 }
1112
1113 sec->rawsize = sec->size;
1114 sec->size = offset;
1115 return offset != sec->rawsize;
1116 }
1117
1118 /* This function is called for .eh_frame_hdr section after
1119 _bfd_elf_discard_section_eh_frame has been called on all .eh_frame
1120 input sections. It finalizes the size of .eh_frame_hdr section. */
1121
1122 bfd_boolean
1123 _bfd_elf_discard_section_eh_frame_hdr (bfd *abfd, struct bfd_link_info *info)
1124 {
1125 struct elf_link_hash_table *htab;
1126 struct eh_frame_hdr_info *hdr_info;
1127 asection *sec;
1128
1129 htab = elf_hash_table (info);
1130 hdr_info = &htab->eh_info;
1131
1132 sec = hdr_info->hdr_sec;
1133 if (sec == NULL)
1134 return FALSE;
1135
1136 sec->size = EH_FRAME_HDR_SIZE;
1137 if (hdr_info->table)
1138 sec->size += 4 + hdr_info->fde_count * 8;
1139
1140 elf_tdata (abfd)->eh_frame_hdr = sec;
1141 return TRUE;
1142 }
1143
1144 /* This function is called from size_dynamic_sections.
1145 It needs to decide whether .eh_frame_hdr should be output or not,
1146 because when the dynamic symbol table has been sized it is too late
1147 to strip sections. */
1148
1149 bfd_boolean
1150 _bfd_elf_maybe_strip_eh_frame_hdr (struct bfd_link_info *info)
1151 {
1152 asection *o;
1153 bfd *abfd;
1154 struct elf_link_hash_table *htab;
1155 struct eh_frame_hdr_info *hdr_info;
1156
1157 htab = elf_hash_table (info);
1158 hdr_info = &htab->eh_info;
1159 if (hdr_info->hdr_sec == NULL)
1160 return TRUE;
1161
1162 if (bfd_is_abs_section (hdr_info->hdr_sec->output_section))
1163 {
1164 hdr_info->hdr_sec = NULL;
1165 return TRUE;
1166 }
1167
1168 abfd = NULL;
1169 if (info->eh_frame_hdr)
1170 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
1171 {
1172 /* Count only sections which have at least a single CIE or FDE.
1173 There cannot be any CIE or FDE <= 8 bytes. */
1174 o = bfd_get_section_by_name (abfd, ".eh_frame");
1175 if (o && o->size > 8 && !bfd_is_abs_section (o->output_section))
1176 break;
1177 }
1178
1179 if (abfd == NULL)
1180 {
1181 hdr_info->hdr_sec->flags |= SEC_EXCLUDE;
1182 hdr_info->hdr_sec = NULL;
1183 return TRUE;
1184 }
1185
1186 hdr_info->table = TRUE;
1187 return TRUE;
1188 }
1189
1190 /* Adjust an address in the .eh_frame section. Given OFFSET within
1191 SEC, this returns the new offset in the adjusted .eh_frame section,
1192 or -1 if the address refers to a CIE/FDE which has been removed
1193 or to offset with dynamic relocation which is no longer needed. */
1194
1195 bfd_vma
1196 _bfd_elf_eh_frame_section_offset (bfd *output_bfd ATTRIBUTE_UNUSED,
1197 struct bfd_link_info *info,
1198 asection *sec,
1199 bfd_vma offset)
1200 {
1201 struct eh_frame_sec_info *sec_info;
1202 struct elf_link_hash_table *htab;
1203 struct eh_frame_hdr_info *hdr_info;
1204 unsigned int lo, hi, mid;
1205
1206 if (sec->sec_info_type != ELF_INFO_TYPE_EH_FRAME)
1207 return offset;
1208 sec_info = elf_section_data (sec)->sec_info;
1209
1210 if (offset >= sec->rawsize)
1211 return offset - sec->rawsize + sec->size;
1212
1213 htab = elf_hash_table (info);
1214 hdr_info = &htab->eh_info;
1215
1216 lo = 0;
1217 hi = sec_info->count;
1218 mid = 0;
1219 while (lo < hi)
1220 {
1221 mid = (lo + hi) / 2;
1222 if (offset < sec_info->entry[mid].offset)
1223 hi = mid;
1224 else if (offset
1225 >= sec_info->entry[mid].offset + sec_info->entry[mid].size)
1226 lo = mid + 1;
1227 else
1228 break;
1229 }
1230
1231 BFD_ASSERT (lo < hi);
1232
1233 /* FDE or CIE was removed. */
1234 if (sec_info->entry[mid].removed)
1235 return (bfd_vma) -1;
1236
1237 /* If converting to DW_EH_PE_pcrel, there will be no need for run-time
1238 relocation against FDE's initial_location field. */
1239 if (!sec_info->entry[mid].cie
1240 && sec_info->entry[mid].make_relative
1241 && offset == sec_info->entry[mid].offset + 8)
1242 return (bfd_vma) -2;
1243
1244 /* If converting LSDA pointers to DW_EH_PE_pcrel, there will be no need
1245 for run-time relocation against LSDA field. */
1246 if (!sec_info->entry[mid].cie
1247 && sec_info->entry[mid].u.fde.cie_inf->u.cie.make_lsda_relative
1248 && offset == (sec_info->entry[mid].offset + 8
1249 + sec_info->entry[mid].lsda_offset))
1250 return (bfd_vma) -2;
1251
1252 /* If converting to DW_EH_PE_pcrel, there will be no need for run-time
1253 relocation against DW_CFA_set_loc's arguments. */
1254 if (sec_info->entry[mid].set_loc
1255 && sec_info->entry[mid].make_relative
1256 && (offset >= sec_info->entry[mid].offset + 8
1257 + sec_info->entry[mid].set_loc[1]))
1258 {
1259 unsigned int cnt;
1260
1261 for (cnt = 1; cnt <= sec_info->entry[mid].set_loc[0]; cnt++)
1262 if (offset == sec_info->entry[mid].offset + 8
1263 + sec_info->entry[mid].set_loc[cnt])
1264 return (bfd_vma) -2;
1265 }
1266
1267 /* Any new augmentation bytes go before the first relocation. */
1268 return (offset + sec_info->entry[mid].new_offset
1269 - sec_info->entry[mid].offset
1270 + extra_augmentation_string_bytes (sec_info->entry + mid)
1271 + extra_augmentation_data_bytes (sec_info->entry + mid));
1272 }
1273
1274 /* Write out .eh_frame section. This is called with the relocated
1275 contents. */
1276
1277 bfd_boolean
1278 _bfd_elf_write_section_eh_frame (bfd *abfd,
1279 struct bfd_link_info *info,
1280 asection *sec,
1281 bfd_byte *contents)
1282 {
1283 struct eh_frame_sec_info *sec_info;
1284 struct elf_link_hash_table *htab;
1285 struct eh_frame_hdr_info *hdr_info;
1286 unsigned int ptr_size;
1287 struct eh_cie_fde *ent;
1288
1289 if (sec->sec_info_type != ELF_INFO_TYPE_EH_FRAME)
1290 return bfd_set_section_contents (abfd, sec->output_section, contents,
1291 sec->output_offset, sec->size);
1292
1293 ptr_size = (get_elf_backend_data (abfd)
1294 ->elf_backend_eh_frame_address_size (abfd, sec));
1295 BFD_ASSERT (ptr_size != 0);
1296
1297 sec_info = elf_section_data (sec)->sec_info;
1298 htab = elf_hash_table (info);
1299 hdr_info = &htab->eh_info;
1300
1301 if (hdr_info->table && hdr_info->array == NULL)
1302 hdr_info->array
1303 = bfd_malloc (hdr_info->fde_count * sizeof(*hdr_info->array));
1304 if (hdr_info->array == NULL)
1305 hdr_info = NULL;
1306
1307 /* The new offsets can be bigger or smaller than the original offsets.
1308 We therefore need to make two passes over the section: one backward
1309 pass to move entries up and one forward pass to move entries down.
1310 The two passes won't interfere with each other because entries are
1311 not reordered */
1312 for (ent = sec_info->entry + sec_info->count; ent-- != sec_info->entry;)
1313 if (!ent->removed && ent->new_offset > ent->offset)
1314 memmove (contents + ent->new_offset, contents + ent->offset, ent->size);
1315
1316 for (ent = sec_info->entry; ent < sec_info->entry + sec_info->count; ++ent)
1317 if (!ent->removed && ent->new_offset < ent->offset)
1318 memmove (contents + ent->new_offset, contents + ent->offset, ent->size);
1319
1320 for (ent = sec_info->entry; ent < sec_info->entry + sec_info->count; ++ent)
1321 {
1322 unsigned char *buf, *end;
1323 unsigned int new_size;
1324
1325 if (ent->removed)
1326 continue;
1327
1328 if (ent->size == 4)
1329 {
1330 /* Any terminating FDE must be at the end of the section. */
1331 BFD_ASSERT (ent == sec_info->entry + sec_info->count - 1);
1332 continue;
1333 }
1334
1335 buf = contents + ent->new_offset;
1336 end = buf + ent->size;
1337 new_size = size_of_output_cie_fde (ent, ptr_size);
1338
1339 /* Update the size. It may be shrinked. */
1340 bfd_put_32 (abfd, new_size - 4, buf);
1341
1342 /* Filling the extra bytes with DW_CFA_nops. */
1343 if (new_size != ent->size)
1344 memset (end, 0, new_size - ent->size);
1345
1346 if (ent->cie)
1347 {
1348 /* CIE */
1349 if (ent->make_relative
1350 || ent->u.cie.make_lsda_relative
1351 || ent->u.cie.per_encoding_relative)
1352 {
1353 char *aug;
1354 unsigned int action, extra_string, extra_data;
1355 unsigned int per_width, per_encoding;
1356
1357 /* Need to find 'R' or 'L' augmentation's argument and modify
1358 DW_EH_PE_* value. */
1359 action = ((ent->make_relative ? 1 : 0)
1360 | (ent->u.cie.make_lsda_relative ? 2 : 0)
1361 | (ent->u.cie.per_encoding_relative ? 4 : 0));
1362 extra_string = extra_augmentation_string_bytes (ent);
1363 extra_data = extra_augmentation_data_bytes (ent);
1364
1365 /* Skip length, id and version. */
1366 buf += 9;
1367 aug = (char *) buf;
1368 buf += strlen (aug) + 1;
1369 skip_leb128 (&buf, end);
1370 skip_leb128 (&buf, end);
1371 skip_leb128 (&buf, end);
1372 if (*aug == 'z')
1373 {
1374 /* The uleb128 will always be a single byte for the kind
1375 of augmentation strings that we're prepared to handle. */
1376 *buf++ += extra_data;
1377 aug++;
1378 }
1379
1380 /* Make room for the new augmentation string and data bytes. */
1381 memmove (buf + extra_string + extra_data, buf, end - buf);
1382 memmove (aug + extra_string, aug, buf - (bfd_byte *) aug);
1383 buf += extra_string;
1384 end += extra_string + extra_data;
1385
1386 if (ent->add_augmentation_size)
1387 {
1388 *aug++ = 'z';
1389 *buf++ = extra_data - 1;
1390 }
1391 if (ent->u.cie.add_fde_encoding)
1392 {
1393 BFD_ASSERT (action & 1);
1394 *aug++ = 'R';
1395 *buf++ = DW_EH_PE_pcrel;
1396 action &= ~1;
1397 }
1398
1399 while (action)
1400 switch (*aug++)
1401 {
1402 case 'L':
1403 if (action & 2)
1404 {
1405 BFD_ASSERT (*buf == ent->lsda_encoding);
1406 *buf |= DW_EH_PE_pcrel;
1407 action &= ~2;
1408 }
1409 buf++;
1410 break;
1411 case 'P':
1412 per_encoding = *buf++;
1413 per_width = get_DW_EH_PE_width (per_encoding, ptr_size);
1414 BFD_ASSERT (per_width != 0);
1415 BFD_ASSERT (((per_encoding & 0x70) == DW_EH_PE_pcrel)
1416 == ent->u.cie.per_encoding_relative);
1417 if ((per_encoding & 0xf0) == DW_EH_PE_aligned)
1418 buf = (contents
1419 + ((buf - contents + per_width - 1)
1420 & ~((bfd_size_type) per_width - 1)));
1421 if (action & 4)
1422 {
1423 bfd_vma val;
1424
1425 val = read_value (abfd, buf, per_width,
1426 get_DW_EH_PE_signed (per_encoding));
1427 val += ent->offset - ent->new_offset;
1428 val -= extra_string + extra_data;
1429 write_value (abfd, buf, val, per_width);
1430 action &= ~4;
1431 }
1432 buf += per_width;
1433 break;
1434 case 'R':
1435 if (action & 1)
1436 {
1437 BFD_ASSERT (*buf == ent->fde_encoding);
1438 *buf |= DW_EH_PE_pcrel;
1439 action &= ~1;
1440 }
1441 buf++;
1442 break;
1443 case 'S':
1444 break;
1445 default:
1446 BFD_FAIL ();
1447 }
1448 }
1449 }
1450 else
1451 {
1452 /* FDE */
1453 bfd_vma value, address;
1454 unsigned int width;
1455 bfd_byte *start;
1456 struct eh_cie_fde *cie;
1457
1458 /* Skip length. */
1459 cie = ent->u.fde.cie_inf;
1460 buf += 4;
1461 value = ((ent->new_offset + sec->output_offset + 4)
1462 - (cie->new_offset + cie->u.cie.u.sec->output_offset));
1463 bfd_put_32 (abfd, value, buf);
1464 buf += 4;
1465 width = get_DW_EH_PE_width (ent->fde_encoding, ptr_size);
1466 value = read_value (abfd, buf, width,
1467 get_DW_EH_PE_signed (ent->fde_encoding));
1468 address = value;
1469 if (value)
1470 {
1471 switch (ent->fde_encoding & 0xf0)
1472 {
1473 case DW_EH_PE_indirect:
1474 case DW_EH_PE_textrel:
1475 BFD_ASSERT (hdr_info == NULL);
1476 break;
1477 case DW_EH_PE_datarel:
1478 {
1479 asection *got = bfd_get_section_by_name (abfd, ".got");
1480
1481 BFD_ASSERT (got != NULL);
1482 address += got->vma;
1483 }
1484 break;
1485 case DW_EH_PE_pcrel:
1486 value += ent->offset - ent->new_offset;
1487 address += (sec->output_section->vma
1488 + sec->output_offset
1489 + ent->offset + 8);
1490 break;
1491 }
1492 if (ent->make_relative)
1493 value -= (sec->output_section->vma
1494 + sec->output_offset
1495 + ent->new_offset + 8);
1496 write_value (abfd, buf, value, width);
1497 }
1498
1499 start = buf;
1500
1501 if (hdr_info)
1502 {
1503 hdr_info->array[hdr_info->array_count].initial_loc = address;
1504 hdr_info->array[hdr_info->array_count++].fde
1505 = (sec->output_section->vma
1506 + sec->output_offset
1507 + ent->new_offset);
1508 }
1509
1510 if ((ent->lsda_encoding & 0xf0) == DW_EH_PE_pcrel
1511 || cie->u.cie.make_lsda_relative)
1512 {
1513 buf += ent->lsda_offset;
1514 width = get_DW_EH_PE_width (ent->lsda_encoding, ptr_size);
1515 value = read_value (abfd, buf, width,
1516 get_DW_EH_PE_signed (ent->lsda_encoding));
1517 if (value)
1518 {
1519 if ((ent->lsda_encoding & 0xf0) == DW_EH_PE_pcrel)
1520 value += ent->offset - ent->new_offset;
1521 else if (cie->u.cie.make_lsda_relative)
1522 value -= (sec->output_section->vma
1523 + sec->output_offset
1524 + ent->new_offset + 8 + ent->lsda_offset);
1525 write_value (abfd, buf, value, width);
1526 }
1527 }
1528 else if (ent->add_augmentation_size)
1529 {
1530 /* Skip the PC and length and insert a zero byte for the
1531 augmentation size. */
1532 buf += width * 2;
1533 memmove (buf + 1, buf, end - buf);
1534 *buf = 0;
1535 }
1536
1537 if (ent->set_loc)
1538 {
1539 /* Adjust DW_CFA_set_loc. */
1540 unsigned int cnt, width;
1541 bfd_vma new_offset;
1542
1543 width = get_DW_EH_PE_width (ent->fde_encoding, ptr_size);
1544 new_offset = ent->new_offset + 8
1545 + extra_augmentation_string_bytes (ent)
1546 + extra_augmentation_data_bytes (ent);
1547
1548 for (cnt = 1; cnt <= ent->set_loc[0]; cnt++)
1549 {
1550 bfd_vma value;
1551 buf = start + ent->set_loc[cnt];
1552
1553 value = read_value (abfd, buf, width,
1554 get_DW_EH_PE_signed (ent->fde_encoding));
1555 if (!value)
1556 continue;
1557
1558 if ((ent->fde_encoding & 0xf0) == DW_EH_PE_pcrel)
1559 value += ent->offset + 8 - new_offset;
1560 if (ent->make_relative)
1561 value -= (sec->output_section->vma
1562 + sec->output_offset
1563 + new_offset + ent->set_loc[cnt]);
1564 write_value (abfd, buf, value, width);
1565 }
1566 }
1567 }
1568 }
1569
1570 /* We don't align the section to its section alignment since the
1571 runtime library only expects all CIE/FDE records aligned at
1572 the pointer size. _bfd_elf_discard_section_eh_frame should
1573 have padded CIE/FDE records to multiple of pointer size with
1574 size_of_output_cie_fde. */
1575 if ((sec->size % ptr_size) != 0)
1576 abort ();
1577
1578 return bfd_set_section_contents (abfd, sec->output_section,
1579 contents, (file_ptr) sec->output_offset,
1580 sec->size);
1581 }
1582
1583 /* Helper function used to sort .eh_frame_hdr search table by increasing
1584 VMA of FDE initial location. */
1585
1586 static int
1587 vma_compare (const void *a, const void *b)
1588 {
1589 const struct eh_frame_array_ent *p = a;
1590 const struct eh_frame_array_ent *q = b;
1591 if (p->initial_loc > q->initial_loc)
1592 return 1;
1593 if (p->initial_loc < q->initial_loc)
1594 return -1;
1595 return 0;
1596 }
1597
1598 /* Write out .eh_frame_hdr section. This must be called after
1599 _bfd_elf_write_section_eh_frame has been called on all input
1600 .eh_frame sections.
1601 .eh_frame_hdr format:
1602 ubyte version (currently 1)
1603 ubyte eh_frame_ptr_enc (DW_EH_PE_* encoding of pointer to start of
1604 .eh_frame section)
1605 ubyte fde_count_enc (DW_EH_PE_* encoding of total FDE count
1606 number (or DW_EH_PE_omit if there is no
1607 binary search table computed))
1608 ubyte table_enc (DW_EH_PE_* encoding of binary search table,
1609 or DW_EH_PE_omit if not present.
1610 DW_EH_PE_datarel is using address of
1611 .eh_frame_hdr section start as base)
1612 [encoded] eh_frame_ptr (pointer to start of .eh_frame section)
1613 optionally followed by:
1614 [encoded] fde_count (total number of FDEs in .eh_frame section)
1615 fde_count x [encoded] initial_loc, fde
1616 (array of encoded pairs containing
1617 FDE initial_location field and FDE address,
1618 sorted by increasing initial_loc). */
1619
1620 bfd_boolean
1621 _bfd_elf_write_section_eh_frame_hdr (bfd *abfd, struct bfd_link_info *info)
1622 {
1623 struct elf_link_hash_table *htab;
1624 struct eh_frame_hdr_info *hdr_info;
1625 asection *sec;
1626 bfd_byte *contents;
1627 asection *eh_frame_sec;
1628 bfd_size_type size;
1629 bfd_boolean retval;
1630 bfd_vma encoded_eh_frame;
1631
1632 htab = elf_hash_table (info);
1633 hdr_info = &htab->eh_info;
1634 sec = hdr_info->hdr_sec;
1635 if (sec == NULL)
1636 return TRUE;
1637
1638 size = EH_FRAME_HDR_SIZE;
1639 if (hdr_info->array && hdr_info->array_count == hdr_info->fde_count)
1640 size += 4 + hdr_info->fde_count * 8;
1641 contents = bfd_malloc (size);
1642 if (contents == NULL)
1643 return FALSE;
1644
1645 eh_frame_sec = bfd_get_section_by_name (abfd, ".eh_frame");
1646 if (eh_frame_sec == NULL)
1647 {
1648 free (contents);
1649 return FALSE;
1650 }
1651
1652 memset (contents, 0, EH_FRAME_HDR_SIZE);
1653 contents[0] = 1; /* Version. */
1654 contents[1] = get_elf_backend_data (abfd)->elf_backend_encode_eh_address
1655 (abfd, info, eh_frame_sec, 0, sec, 4,
1656 &encoded_eh_frame); /* .eh_frame offset. */
1657
1658 if (hdr_info->array && hdr_info->array_count == hdr_info->fde_count)
1659 {
1660 contents[2] = DW_EH_PE_udata4; /* FDE count encoding. */
1661 contents[3] = DW_EH_PE_datarel | DW_EH_PE_sdata4; /* Search table enc. */
1662 }
1663 else
1664 {
1665 contents[2] = DW_EH_PE_omit;
1666 contents[3] = DW_EH_PE_omit;
1667 }
1668 bfd_put_32 (abfd, encoded_eh_frame, contents + 4);
1669
1670 if (contents[2] != DW_EH_PE_omit)
1671 {
1672 unsigned int i;
1673
1674 bfd_put_32 (abfd, hdr_info->fde_count, contents + EH_FRAME_HDR_SIZE);
1675 qsort (hdr_info->array, hdr_info->fde_count, sizeof (*hdr_info->array),
1676 vma_compare);
1677 for (i = 0; i < hdr_info->fde_count; i++)
1678 {
1679 bfd_put_32 (abfd,
1680 hdr_info->array[i].initial_loc
1681 - sec->output_section->vma,
1682 contents + EH_FRAME_HDR_SIZE + i * 8 + 4);
1683 bfd_put_32 (abfd,
1684 hdr_info->array[i].fde - sec->output_section->vma,
1685 contents + EH_FRAME_HDR_SIZE + i * 8 + 8);
1686 }
1687 }
1688
1689 retval = bfd_set_section_contents (abfd, sec->output_section,
1690 contents, (file_ptr) sec->output_offset,
1691 sec->size);
1692 free (contents);
1693 return retval;
1694 }
1695
1696 /* Return the width of FDE addresses. This is the default implementation. */
1697
1698 unsigned int
1699 _bfd_elf_eh_frame_address_size (bfd *abfd, asection *sec ATTRIBUTE_UNUSED)
1700 {
1701 return elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64 ? 8 : 4;
1702 }
1703
1704 /* Decide whether we can use a PC-relative encoding within the given
1705 EH frame section. This is the default implementation. */
1706
1707 bfd_boolean
1708 _bfd_elf_can_make_relative (bfd *input_bfd ATTRIBUTE_UNUSED,
1709 struct bfd_link_info *info ATTRIBUTE_UNUSED,
1710 asection *eh_frame_section ATTRIBUTE_UNUSED)
1711 {
1712 return TRUE;
1713 }
1714
1715 /* Select an encoding for the given address. Preference is given to
1716 PC-relative addressing modes. */
1717
1718 bfd_byte
1719 _bfd_elf_encode_eh_address (bfd *abfd ATTRIBUTE_UNUSED,
1720 struct bfd_link_info *info ATTRIBUTE_UNUSED,
1721 asection *osec, bfd_vma offset,
1722 asection *loc_sec, bfd_vma loc_offset,
1723 bfd_vma *encoded)
1724 {
1725 *encoded = osec->vma + offset -
1726 (loc_sec->output_section->vma + loc_sec->output_offset + loc_offset);
1727 return DW_EH_PE_pcrel | DW_EH_PE_sdata4;
1728 }