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