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