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