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