]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - bfd/elf-eh-frame.c
bfd/
[thirdparty/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.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.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.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.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.merged;
1090 else
1091 {
1092 /* Make the local CIE represent the merged group. */
1093 merged->u.cie.merged = cie;
1094 cie->removed = 0;
1095 cie->u.cie.make_lsda_relative
1096 = merged->u.cie.make_lsda_relative;
1097 }
1098 }
1099 }
1100 }
1101
1102 ptr_size = (get_elf_backend_data (sec->owner)
1103 ->elf_backend_eh_frame_address_size (sec->owner, sec));
1104 offset = 0;
1105 for (ent = sec_info->entry; ent < sec_info->entry + sec_info->count; ++ent)
1106 if (!ent->removed)
1107 {
1108 ent->new_offset = offset;
1109 offset += size_of_output_cie_fde (ent, ptr_size);
1110 }
1111
1112 sec->rawsize = sec->size;
1113 sec->size = offset;
1114 return offset != sec->rawsize;
1115 }
1116
1117 /* This function is called for .eh_frame_hdr section after
1118 _bfd_elf_discard_section_eh_frame has been called on all .eh_frame
1119 input sections. It finalizes the size of .eh_frame_hdr section. */
1120
1121 bfd_boolean
1122 _bfd_elf_discard_section_eh_frame_hdr (bfd *abfd, struct bfd_link_info *info)
1123 {
1124 struct elf_link_hash_table *htab;
1125 struct eh_frame_hdr_info *hdr_info;
1126 asection *sec;
1127
1128 htab = elf_hash_table (info);
1129 hdr_info = &htab->eh_info;
1130
1131 sec = hdr_info->hdr_sec;
1132 if (sec == NULL)
1133 return FALSE;
1134
1135 sec->size = EH_FRAME_HDR_SIZE;
1136 if (hdr_info->table)
1137 sec->size += 4 + hdr_info->fde_count * 8;
1138
1139 elf_tdata (abfd)->eh_frame_hdr = sec;
1140 return TRUE;
1141 }
1142
1143 /* This function is called from size_dynamic_sections.
1144 It needs to decide whether .eh_frame_hdr should be output or not,
1145 because when the dynamic symbol table has been sized it is too late
1146 to strip sections. */
1147
1148 bfd_boolean
1149 _bfd_elf_maybe_strip_eh_frame_hdr (struct bfd_link_info *info)
1150 {
1151 asection *o;
1152 bfd *abfd;
1153 struct elf_link_hash_table *htab;
1154 struct eh_frame_hdr_info *hdr_info;
1155
1156 htab = elf_hash_table (info);
1157 hdr_info = &htab->eh_info;
1158 if (hdr_info->hdr_sec == NULL)
1159 return TRUE;
1160
1161 if (bfd_is_abs_section (hdr_info->hdr_sec->output_section))
1162 {
1163 hdr_info->hdr_sec = NULL;
1164 return TRUE;
1165 }
1166
1167 abfd = NULL;
1168 if (info->eh_frame_hdr)
1169 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
1170 {
1171 /* Count only sections which have at least a single CIE or FDE.
1172 There cannot be any CIE or FDE <= 8 bytes. */
1173 o = bfd_get_section_by_name (abfd, ".eh_frame");
1174 if (o && o->size > 8 && !bfd_is_abs_section (o->output_section))
1175 break;
1176 }
1177
1178 if (abfd == NULL)
1179 {
1180 hdr_info->hdr_sec->flags |= SEC_EXCLUDE;
1181 hdr_info->hdr_sec = NULL;
1182 return TRUE;
1183 }
1184
1185 hdr_info->table = TRUE;
1186 return TRUE;
1187 }
1188
1189 /* Adjust an address in the .eh_frame section. Given OFFSET within
1190 SEC, this returns the new offset in the adjusted .eh_frame section,
1191 or -1 if the address refers to a CIE/FDE which has been removed
1192 or to offset with dynamic relocation which is no longer needed. */
1193
1194 bfd_vma
1195 _bfd_elf_eh_frame_section_offset (bfd *output_bfd ATTRIBUTE_UNUSED,
1196 struct bfd_link_info *info,
1197 asection *sec,
1198 bfd_vma offset)
1199 {
1200 struct eh_frame_sec_info *sec_info;
1201 struct elf_link_hash_table *htab;
1202 struct eh_frame_hdr_info *hdr_info;
1203 unsigned int lo, hi, mid;
1204
1205 if (sec->sec_info_type != ELF_INFO_TYPE_EH_FRAME)
1206 return offset;
1207 sec_info = elf_section_data (sec)->sec_info;
1208
1209 if (offset >= sec->rawsize)
1210 return offset - sec->rawsize + sec->size;
1211
1212 htab = elf_hash_table (info);
1213 hdr_info = &htab->eh_info;
1214 if (hdr_info->offsets_adjusted)
1215 offset += sec->output_offset;
1216
1217 lo = 0;
1218 hi = sec_info->count;
1219 mid = 0;
1220 while (lo < hi)
1221 {
1222 mid = (lo + hi) / 2;
1223 if (offset < sec_info->entry[mid].offset)
1224 hi = mid;
1225 else if (offset
1226 >= sec_info->entry[mid].offset + sec_info->entry[mid].size)
1227 lo = mid + 1;
1228 else
1229 break;
1230 }
1231
1232 BFD_ASSERT (lo < hi);
1233
1234 /* FDE or CIE was removed. */
1235 if (sec_info->entry[mid].removed)
1236 return (bfd_vma) -1;
1237
1238 /* If converting to DW_EH_PE_pcrel, there will be no need for run-time
1239 relocation against FDE's initial_location field. */
1240 if (!sec_info->entry[mid].cie
1241 && sec_info->entry[mid].make_relative
1242 && offset == sec_info->entry[mid].offset + 8)
1243 return (bfd_vma) -2;
1244
1245 /* If converting LSDA pointers to DW_EH_PE_pcrel, there will be no need
1246 for run-time relocation against LSDA field. */
1247 if (!sec_info->entry[mid].cie
1248 && sec_info->entry[mid].u.fde.cie_inf->u.cie.make_lsda_relative
1249 && offset == (sec_info->entry[mid].offset + 8
1250 + sec_info->entry[mid].lsda_offset))
1251 return (bfd_vma) -2;
1252
1253 /* If converting to DW_EH_PE_pcrel, there will be no need for run-time
1254 relocation against DW_CFA_set_loc's arguments. */
1255 if (sec_info->entry[mid].set_loc
1256 && sec_info->entry[mid].make_relative
1257 && (offset >= sec_info->entry[mid].offset + 8
1258 + sec_info->entry[mid].set_loc[1]))
1259 {
1260 unsigned int cnt;
1261
1262 for (cnt = 1; cnt <= sec_info->entry[mid].set_loc[0]; cnt++)
1263 if (offset == sec_info->entry[mid].offset + 8
1264 + sec_info->entry[mid].set_loc[cnt])
1265 return (bfd_vma) -2;
1266 }
1267
1268 if (hdr_info->offsets_adjusted)
1269 offset -= sec->output_offset;
1270 /* Any new augmentation bytes go before the first relocation. */
1271 return (offset + sec_info->entry[mid].new_offset
1272 - sec_info->entry[mid].offset
1273 + extra_augmentation_string_bytes (sec_info->entry + mid)
1274 + extra_augmentation_data_bytes (sec_info->entry + mid));
1275 }
1276
1277 /* Write out .eh_frame section. This is called with the relocated
1278 contents. */
1279
1280 bfd_boolean
1281 _bfd_elf_write_section_eh_frame (bfd *abfd,
1282 struct bfd_link_info *info,
1283 asection *sec,
1284 bfd_byte *contents)
1285 {
1286 struct eh_frame_sec_info *sec_info;
1287 struct elf_link_hash_table *htab;
1288 struct eh_frame_hdr_info *hdr_info;
1289 unsigned int ptr_size;
1290 struct eh_cie_fde *ent;
1291
1292 if (sec->sec_info_type != ELF_INFO_TYPE_EH_FRAME)
1293 return bfd_set_section_contents (abfd, sec->output_section, contents,
1294 sec->output_offset, sec->size);
1295
1296 ptr_size = (get_elf_backend_data (abfd)
1297 ->elf_backend_eh_frame_address_size (abfd, sec));
1298 BFD_ASSERT (ptr_size != 0);
1299
1300 sec_info = elf_section_data (sec)->sec_info;
1301 htab = elf_hash_table (info);
1302 hdr_info = &htab->eh_info;
1303
1304 /* First convert all offsets to output section offsets, so that a
1305 CIE offset is valid if the CIE is used by a FDE from some other
1306 section. This can happen when duplicate CIEs are deleted in
1307 _bfd_elf_discard_section_eh_frame. We do all sections here because
1308 this function might not be called on sections in the same order as
1309 _bfd_elf_discard_section_eh_frame. */
1310 if (!hdr_info->offsets_adjusted)
1311 {
1312 bfd *ibfd;
1313 asection *eh;
1314 struct eh_frame_sec_info *eh_inf;
1315
1316 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
1317 {
1318 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1319 || (ibfd->flags & DYNAMIC) != 0)
1320 continue;
1321
1322 eh = bfd_get_section_by_name (ibfd, ".eh_frame");
1323 if (eh == NULL || eh->sec_info_type != ELF_INFO_TYPE_EH_FRAME)
1324 continue;
1325
1326 eh_inf = elf_section_data (eh)->sec_info;
1327 for (ent = eh_inf->entry; ent < eh_inf->entry + eh_inf->count; ++ent)
1328 {
1329 ent->offset += eh->output_offset;
1330 ent->new_offset += eh->output_offset;
1331 }
1332 }
1333 hdr_info->offsets_adjusted = TRUE;
1334 }
1335
1336 if (hdr_info->table && hdr_info->array == NULL)
1337 hdr_info->array
1338 = bfd_malloc (hdr_info->fde_count * sizeof(*hdr_info->array));
1339 if (hdr_info->array == NULL)
1340 hdr_info = NULL;
1341
1342 /* The new offsets can be bigger or smaller than the original offsets.
1343 We therefore need to make two passes over the section: one backward
1344 pass to move entries up and one forward pass to move entries down.
1345 The two passes won't interfere with each other because entries are
1346 not reordered */
1347 for (ent = sec_info->entry + sec_info->count; ent-- != sec_info->entry;)
1348 if (!ent->removed && ent->new_offset > ent->offset)
1349 memmove (contents + ent->new_offset - sec->output_offset,
1350 contents + ent->offset - sec->output_offset, ent->size);
1351
1352 for (ent = sec_info->entry; ent < sec_info->entry + sec_info->count; ++ent)
1353 if (!ent->removed && ent->new_offset < ent->offset)
1354 memmove (contents + ent->new_offset - sec->output_offset,
1355 contents + ent->offset - sec->output_offset, ent->size);
1356
1357 for (ent = sec_info->entry; ent < sec_info->entry + sec_info->count; ++ent)
1358 {
1359 unsigned char *buf, *end;
1360 unsigned int new_size;
1361
1362 if (ent->removed)
1363 continue;
1364
1365 if (ent->size == 4)
1366 {
1367 /* Any terminating FDE must be at the end of the section. */
1368 BFD_ASSERT (ent == sec_info->entry + sec_info->count - 1);
1369 continue;
1370 }
1371
1372 buf = contents + ent->new_offset - sec->output_offset;
1373 end = buf + ent->size;
1374 new_size = size_of_output_cie_fde (ent, ptr_size);
1375
1376 /* Update the size. It may be shrinked. */
1377 bfd_put_32 (abfd, new_size - 4, buf);
1378
1379 /* Filling the extra bytes with DW_CFA_nops. */
1380 if (new_size != ent->size)
1381 memset (end, 0, new_size - ent->size);
1382
1383 if (ent->cie)
1384 {
1385 /* CIE */
1386 if (ent->make_relative
1387 || ent->u.cie.make_lsda_relative
1388 || ent->u.cie.per_encoding_relative)
1389 {
1390 char *aug;
1391 unsigned int action, extra_string, extra_data;
1392 unsigned int per_width, per_encoding;
1393
1394 /* Need to find 'R' or 'L' augmentation's argument and modify
1395 DW_EH_PE_* value. */
1396 action = ((ent->make_relative ? 1 : 0)
1397 | (ent->u.cie.make_lsda_relative ? 2 : 0)
1398 | (ent->u.cie.per_encoding_relative ? 4 : 0));
1399 extra_string = extra_augmentation_string_bytes (ent);
1400 extra_data = extra_augmentation_data_bytes (ent);
1401
1402 /* Skip length, id and version. */
1403 buf += 9;
1404 aug = (char *) buf;
1405 buf += strlen (aug) + 1;
1406 skip_leb128 (&buf, end);
1407 skip_leb128 (&buf, end);
1408 skip_leb128 (&buf, end);
1409 if (*aug == 'z')
1410 {
1411 /* The uleb128 will always be a single byte for the kind
1412 of augmentation strings that we're prepared to handle. */
1413 *buf++ += extra_data;
1414 aug++;
1415 }
1416
1417 /* Make room for the new augmentation string and data bytes. */
1418 memmove (buf + extra_string + extra_data, buf, end - buf);
1419 memmove (aug + extra_string, aug, buf - (bfd_byte *) aug);
1420 buf += extra_string;
1421 end += extra_string + extra_data;
1422
1423 if (ent->add_augmentation_size)
1424 {
1425 *aug++ = 'z';
1426 *buf++ = extra_data - 1;
1427 }
1428 if (ent->u.cie.add_fde_encoding)
1429 {
1430 BFD_ASSERT (action & 1);
1431 *aug++ = 'R';
1432 *buf++ = DW_EH_PE_pcrel;
1433 action &= ~1;
1434 }
1435
1436 while (action)
1437 switch (*aug++)
1438 {
1439 case 'L':
1440 if (action & 2)
1441 {
1442 BFD_ASSERT (*buf == ent->lsda_encoding);
1443 *buf |= DW_EH_PE_pcrel;
1444 action &= ~2;
1445 }
1446 buf++;
1447 break;
1448 case 'P':
1449 per_encoding = *buf++;
1450 per_width = get_DW_EH_PE_width (per_encoding, ptr_size);
1451 BFD_ASSERT (per_width != 0);
1452 BFD_ASSERT (((per_encoding & 0x70) == DW_EH_PE_pcrel)
1453 == ent->u.cie.per_encoding_relative);
1454 if ((per_encoding & 0xf0) == DW_EH_PE_aligned)
1455 buf = (contents
1456 + ((buf - contents + per_width - 1)
1457 & ~((bfd_size_type) per_width - 1)));
1458 if (action & 4)
1459 {
1460 bfd_vma val;
1461
1462 val = read_value (abfd, buf, per_width,
1463 get_DW_EH_PE_signed (per_encoding));
1464 val += ent->offset - ent->new_offset;
1465 val -= extra_string + extra_data;
1466 write_value (abfd, buf, val, per_width);
1467 action &= ~4;
1468 }
1469 buf += per_width;
1470 break;
1471 case 'R':
1472 if (action & 1)
1473 {
1474 BFD_ASSERT (*buf == ent->fde_encoding);
1475 *buf |= DW_EH_PE_pcrel;
1476 action &= ~1;
1477 }
1478 buf++;
1479 break;
1480 case 'S':
1481 break;
1482 default:
1483 BFD_FAIL ();
1484 }
1485 }
1486 }
1487 else
1488 {
1489 /* FDE */
1490 bfd_vma value, address;
1491 unsigned int width;
1492 bfd_byte *start;
1493 struct eh_cie_fde *cie;
1494
1495 /* Skip length. */
1496 cie = ent->u.fde.cie_inf;
1497 buf += 4;
1498 value = ent->new_offset + 4 - cie->new_offset;
1499 bfd_put_32 (abfd, value, buf);
1500 buf += 4;
1501 width = get_DW_EH_PE_width (ent->fde_encoding, ptr_size);
1502 value = read_value (abfd, buf, width,
1503 get_DW_EH_PE_signed (ent->fde_encoding));
1504 address = value;
1505 if (value)
1506 {
1507 switch (ent->fde_encoding & 0xf0)
1508 {
1509 case DW_EH_PE_indirect:
1510 case DW_EH_PE_textrel:
1511 BFD_ASSERT (hdr_info == NULL);
1512 break;
1513 case DW_EH_PE_datarel:
1514 {
1515 asection *got = bfd_get_section_by_name (abfd, ".got");
1516
1517 BFD_ASSERT (got != NULL);
1518 address += got->vma;
1519 }
1520 break;
1521 case DW_EH_PE_pcrel:
1522 value += ent->offset - ent->new_offset;
1523 address += sec->output_section->vma + ent->offset + 8;
1524 break;
1525 }
1526 if (ent->make_relative)
1527 value -= sec->output_section->vma + ent->new_offset + 8;
1528 write_value (abfd, buf, value, width);
1529 }
1530
1531 start = buf;
1532
1533 if (hdr_info)
1534 {
1535 hdr_info->array[hdr_info->array_count].initial_loc = address;
1536 hdr_info->array[hdr_info->array_count++].fde
1537 = sec->output_section->vma + ent->new_offset;
1538 }
1539
1540 if ((ent->lsda_encoding & 0xf0) == DW_EH_PE_pcrel
1541 || cie->u.cie.make_lsda_relative)
1542 {
1543 buf += ent->lsda_offset;
1544 width = get_DW_EH_PE_width (ent->lsda_encoding, ptr_size);
1545 value = read_value (abfd, buf, width,
1546 get_DW_EH_PE_signed (ent->lsda_encoding));
1547 if (value)
1548 {
1549 if ((ent->lsda_encoding & 0xf0) == DW_EH_PE_pcrel)
1550 value += ent->offset - ent->new_offset;
1551 else if (cie->u.cie.make_lsda_relative)
1552 value -= (sec->output_section->vma + ent->new_offset + 8
1553 + ent->lsda_offset);
1554 write_value (abfd, buf, value, width);
1555 }
1556 }
1557 else if (ent->add_augmentation_size)
1558 {
1559 /* Skip the PC and length and insert a zero byte for the
1560 augmentation size. */
1561 buf += width * 2;
1562 memmove (buf + 1, buf, end - buf);
1563 *buf = 0;
1564 }
1565
1566 if (ent->set_loc)
1567 {
1568 /* Adjust DW_CFA_set_loc. */
1569 unsigned int cnt, width;
1570 bfd_vma new_offset;
1571
1572 width = get_DW_EH_PE_width (ent->fde_encoding, ptr_size);
1573 new_offset = ent->new_offset + 8
1574 + extra_augmentation_string_bytes (ent)
1575 + extra_augmentation_data_bytes (ent);
1576
1577 for (cnt = 1; cnt <= ent->set_loc[0]; cnt++)
1578 {
1579 bfd_vma value;
1580 buf = start + ent->set_loc[cnt];
1581
1582 value = read_value (abfd, buf, width,
1583 get_DW_EH_PE_signed (ent->fde_encoding));
1584 if (!value)
1585 continue;
1586
1587 if ((ent->fde_encoding & 0xf0) == DW_EH_PE_pcrel)
1588 value += ent->offset + 8 - new_offset;
1589 if (ent->make_relative)
1590 value -= sec->output_section->vma + new_offset
1591 + ent->set_loc[cnt];
1592 write_value (abfd, buf, value, width);
1593 }
1594 }
1595 }
1596 }
1597
1598 /* We don't align the section to its section alignment since the
1599 runtime library only expects all CIE/FDE records aligned at
1600 the pointer size. _bfd_elf_discard_section_eh_frame should
1601 have padded CIE/FDE records to multiple of pointer size with
1602 size_of_output_cie_fde. */
1603 if ((sec->size % ptr_size) != 0)
1604 abort ();
1605
1606 return bfd_set_section_contents (abfd, sec->output_section,
1607 contents, (file_ptr) sec->output_offset,
1608 sec->size);
1609 }
1610
1611 /* Helper function used to sort .eh_frame_hdr search table by increasing
1612 VMA of FDE initial location. */
1613
1614 static int
1615 vma_compare (const void *a, const void *b)
1616 {
1617 const struct eh_frame_array_ent *p = a;
1618 const struct eh_frame_array_ent *q = b;
1619 if (p->initial_loc > q->initial_loc)
1620 return 1;
1621 if (p->initial_loc < q->initial_loc)
1622 return -1;
1623 return 0;
1624 }
1625
1626 /* Write out .eh_frame_hdr section. This must be called after
1627 _bfd_elf_write_section_eh_frame has been called on all input
1628 .eh_frame sections.
1629 .eh_frame_hdr format:
1630 ubyte version (currently 1)
1631 ubyte eh_frame_ptr_enc (DW_EH_PE_* encoding of pointer to start of
1632 .eh_frame section)
1633 ubyte fde_count_enc (DW_EH_PE_* encoding of total FDE count
1634 number (or DW_EH_PE_omit if there is no
1635 binary search table computed))
1636 ubyte table_enc (DW_EH_PE_* encoding of binary search table,
1637 or DW_EH_PE_omit if not present.
1638 DW_EH_PE_datarel is using address of
1639 .eh_frame_hdr section start as base)
1640 [encoded] eh_frame_ptr (pointer to start of .eh_frame section)
1641 optionally followed by:
1642 [encoded] fde_count (total number of FDEs in .eh_frame section)
1643 fde_count x [encoded] initial_loc, fde
1644 (array of encoded pairs containing
1645 FDE initial_location field and FDE address,
1646 sorted by increasing initial_loc). */
1647
1648 bfd_boolean
1649 _bfd_elf_write_section_eh_frame_hdr (bfd *abfd, struct bfd_link_info *info)
1650 {
1651 struct elf_link_hash_table *htab;
1652 struct eh_frame_hdr_info *hdr_info;
1653 asection *sec;
1654 bfd_byte *contents;
1655 asection *eh_frame_sec;
1656 bfd_size_type size;
1657 bfd_boolean retval;
1658 bfd_vma encoded_eh_frame;
1659
1660 htab = elf_hash_table (info);
1661 hdr_info = &htab->eh_info;
1662 sec = hdr_info->hdr_sec;
1663 if (sec == NULL)
1664 return TRUE;
1665
1666 size = EH_FRAME_HDR_SIZE;
1667 if (hdr_info->array && hdr_info->array_count == hdr_info->fde_count)
1668 size += 4 + hdr_info->fde_count * 8;
1669 contents = bfd_malloc (size);
1670 if (contents == NULL)
1671 return FALSE;
1672
1673 eh_frame_sec = bfd_get_section_by_name (abfd, ".eh_frame");
1674 if (eh_frame_sec == NULL)
1675 {
1676 free (contents);
1677 return FALSE;
1678 }
1679
1680 memset (contents, 0, EH_FRAME_HDR_SIZE);
1681 contents[0] = 1; /* Version. */
1682 contents[1] = get_elf_backend_data (abfd)->elf_backend_encode_eh_address
1683 (abfd, info, eh_frame_sec, 0, sec, 4,
1684 &encoded_eh_frame); /* .eh_frame offset. */
1685
1686 if (hdr_info->array && hdr_info->array_count == hdr_info->fde_count)
1687 {
1688 contents[2] = DW_EH_PE_udata4; /* FDE count encoding. */
1689 contents[3] = DW_EH_PE_datarel | DW_EH_PE_sdata4; /* Search table enc. */
1690 }
1691 else
1692 {
1693 contents[2] = DW_EH_PE_omit;
1694 contents[3] = DW_EH_PE_omit;
1695 }
1696 bfd_put_32 (abfd, encoded_eh_frame, contents + 4);
1697
1698 if (contents[2] != DW_EH_PE_omit)
1699 {
1700 unsigned int i;
1701
1702 bfd_put_32 (abfd, hdr_info->fde_count, contents + EH_FRAME_HDR_SIZE);
1703 qsort (hdr_info->array, hdr_info->fde_count, sizeof (*hdr_info->array),
1704 vma_compare);
1705 for (i = 0; i < hdr_info->fde_count; i++)
1706 {
1707 bfd_put_32 (abfd,
1708 hdr_info->array[i].initial_loc
1709 - sec->output_section->vma,
1710 contents + EH_FRAME_HDR_SIZE + i * 8 + 4);
1711 bfd_put_32 (abfd,
1712 hdr_info->array[i].fde - sec->output_section->vma,
1713 contents + EH_FRAME_HDR_SIZE + i * 8 + 8);
1714 }
1715 }
1716
1717 retval = bfd_set_section_contents (abfd, sec->output_section,
1718 contents, (file_ptr) sec->output_offset,
1719 sec->size);
1720 free (contents);
1721 return retval;
1722 }
1723
1724 /* Return the width of FDE addresses. This is the default implementation. */
1725
1726 unsigned int
1727 _bfd_elf_eh_frame_address_size (bfd *abfd, asection *sec ATTRIBUTE_UNUSED)
1728 {
1729 return elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64 ? 8 : 4;
1730 }
1731
1732 /* Decide whether we can use a PC-relative encoding within the given
1733 EH frame section. This is the default implementation. */
1734
1735 bfd_boolean
1736 _bfd_elf_can_make_relative (bfd *input_bfd ATTRIBUTE_UNUSED,
1737 struct bfd_link_info *info ATTRIBUTE_UNUSED,
1738 asection *eh_frame_section ATTRIBUTE_UNUSED)
1739 {
1740 return TRUE;
1741 }
1742
1743 /* Select an encoding for the given address. Preference is given to
1744 PC-relative addressing modes. */
1745
1746 bfd_byte
1747 _bfd_elf_encode_eh_address (bfd *abfd ATTRIBUTE_UNUSED,
1748 struct bfd_link_info *info ATTRIBUTE_UNUSED,
1749 asection *osec, bfd_vma offset,
1750 asection *loc_sec, bfd_vma loc_offset,
1751 bfd_vma *encoded)
1752 {
1753 *encoded = osec->vma + offset -
1754 (loc_sec->output_section->vma + loc_sec->output_offset + loc_offset);
1755 return DW_EH_PE_pcrel | DW_EH_PE_sdata4;
1756 }