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