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