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