Commit | Line | Data |
---|---|---|
65765700 | 1 | /* .eh_frame section optimization. |
4b95cf5c | 2 | Copyright (C) 2001-2014 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 | |
cd123cb7 | 9 | the Free Software Foundation; either version 3 of the License, or |
5ed6aba4 | 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 | |
cd123cb7 NC |
19 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, |
20 | MA 02110-1301, USA. */ | |
65765700 | 21 | |
65765700 | 22 | #include "sysdep.h" |
3db64b00 | 23 | #include "bfd.h" |
65765700 JJ |
24 | #include "libbfd.h" |
25 | #include "elf-bfd.h" | |
fa8f86ff | 26 | #include "dwarf2.h" |
65765700 JJ |
27 | |
28 | #define EH_FRAME_HDR_SIZE 8 | |
29 | ||
bce613b9 JJ |
30 | struct cie |
31 | { | |
32 | unsigned int length; | |
33 | unsigned int hash; | |
34 | unsigned char version; | |
f137a54e | 35 | unsigned char local_personality; |
bce613b9 JJ |
36 | char augmentation[20]; |
37 | bfd_vma code_align; | |
38 | bfd_signed_vma data_align; | |
39 | bfd_vma ra_column; | |
40 | bfd_vma augmentation_size; | |
f137a54e AM |
41 | union { |
42 | struct elf_link_hash_entry *h; | |
43 | bfd_vma val; | |
184d07da | 44 | unsigned int reloc_index; |
f137a54e | 45 | } personality; |
bce613b9 JJ |
46 | asection *output_sec; |
47 | struct eh_cie_fde *cie_inf; | |
48 | unsigned char per_encoding; | |
49 | unsigned char lsda_encoding; | |
50 | unsigned char fde_encoding; | |
51 | unsigned char initial_insn_length; | |
9f4b847e | 52 | unsigned char can_make_lsda_relative; |
bce613b9 JJ |
53 | unsigned char initial_instructions[50]; |
54 | }; | |
55 | ||
56 | ||
57 | ||
2c42be65 RS |
58 | /* If *ITER hasn't reached END yet, read the next byte into *RESULT and |
59 | move onto the next byte. Return true on success. */ | |
60 | ||
61 | static inline bfd_boolean | |
62 | read_byte (bfd_byte **iter, bfd_byte *end, unsigned char *result) | |
63 | { | |
64 | if (*iter >= end) | |
65 | return FALSE; | |
66 | *result = *((*iter)++); | |
67 | return TRUE; | |
68 | } | |
69 | ||
70 | /* Move *ITER over LENGTH bytes, or up to END, whichever is closer. | |
71 | Return true it was possible to move LENGTH bytes. */ | |
72 | ||
73 | static inline bfd_boolean | |
74 | skip_bytes (bfd_byte **iter, bfd_byte *end, bfd_size_type length) | |
75 | { | |
76 | if ((bfd_size_type) (end - *iter) < length) | |
77 | { | |
78 | *iter = end; | |
79 | return FALSE; | |
80 | } | |
81 | *iter += length; | |
82 | return TRUE; | |
83 | } | |
84 | ||
85 | /* Move *ITER over an leb128, stopping at END. Return true if the end | |
86 | of the leb128 was found. */ | |
87 | ||
88 | static bfd_boolean | |
89 | skip_leb128 (bfd_byte **iter, bfd_byte *end) | |
90 | { | |
91 | unsigned char byte; | |
92 | do | |
93 | if (!read_byte (iter, end, &byte)) | |
94 | return FALSE; | |
95 | while (byte & 0x80); | |
96 | return TRUE; | |
97 | } | |
98 | ||
99 | /* Like skip_leb128, but treat the leb128 as an unsigned value and | |
100 | store it in *VALUE. */ | |
101 | ||
102 | static bfd_boolean | |
103 | read_uleb128 (bfd_byte **iter, bfd_byte *end, bfd_vma *value) | |
104 | { | |
105 | bfd_byte *start, *p; | |
106 | ||
107 | start = *iter; | |
108 | if (!skip_leb128 (iter, end)) | |
109 | return FALSE; | |
110 | ||
111 | p = *iter; | |
112 | *value = *--p; | |
113 | while (p > start) | |
114 | *value = (*value << 7) | (*--p & 0x7f); | |
115 | ||
116 | return TRUE; | |
117 | } | |
118 | ||
119 | /* Like read_uleb128, but for signed values. */ | |
120 | ||
121 | static bfd_boolean | |
122 | read_sleb128 (bfd_byte **iter, bfd_byte *end, bfd_signed_vma *value) | |
123 | { | |
124 | bfd_byte *start, *p; | |
125 | ||
126 | start = *iter; | |
127 | if (!skip_leb128 (iter, end)) | |
128 | return FALSE; | |
129 | ||
130 | p = *iter; | |
131 | *value = ((*--p & 0x7f) ^ 0x40) - 0x40; | |
132 | while (p > start) | |
133 | *value = (*value << 7) | (*--p & 0x7f); | |
134 | ||
135 | return TRUE; | |
136 | } | |
65765700 JJ |
137 | |
138 | /* Return 0 if either encoding is variable width, or not yet known to bfd. */ | |
139 | ||
140 | static | |
c39a58e6 | 141 | int get_DW_EH_PE_width (int encoding, int ptr_size) |
65765700 JJ |
142 | { |
143 | /* DW_EH_PE_ values of 0x60 and 0x70 weren't defined at the time .eh_frame | |
144 | was added to bfd. */ | |
145 | if ((encoding & 0x60) == 0x60) | |
146 | return 0; | |
147 | ||
148 | switch (encoding & 7) | |
149 | { | |
150 | case DW_EH_PE_udata2: return 2; | |
151 | case DW_EH_PE_udata4: return 4; | |
152 | case DW_EH_PE_udata8: return 8; | |
153 | case DW_EH_PE_absptr: return ptr_size; | |
154 | default: | |
155 | break; | |
156 | } | |
157 | ||
158 | return 0; | |
159 | } | |
160 | ||
84f97cb6 AS |
161 | #define get_DW_EH_PE_signed(encoding) (((encoding) & DW_EH_PE_signed) != 0) |
162 | ||
9e2a4898 JJ |
163 | /* Read a width sized value from memory. */ |
164 | ||
165 | static bfd_vma | |
c39a58e6 | 166 | read_value (bfd *abfd, bfd_byte *buf, int width, int is_signed) |
9e2a4898 JJ |
167 | { |
168 | bfd_vma value; | |
169 | ||
170 | switch (width) | |
171 | { | |
84f97cb6 AS |
172 | case 2: |
173 | if (is_signed) | |
174 | value = bfd_get_signed_16 (abfd, buf); | |
175 | else | |
176 | value = bfd_get_16 (abfd, buf); | |
177 | break; | |
178 | case 4: | |
179 | if (is_signed) | |
180 | value = bfd_get_signed_32 (abfd, buf); | |
181 | else | |
182 | value = bfd_get_32 (abfd, buf); | |
183 | break; | |
184 | case 8: | |
185 | if (is_signed) | |
186 | value = bfd_get_signed_64 (abfd, buf); | |
187 | else | |
188 | value = bfd_get_64 (abfd, buf); | |
189 | break; | |
190 | default: | |
191 | BFD_FAIL (); | |
192 | return 0; | |
9e2a4898 JJ |
193 | } |
194 | ||
195 | return value; | |
196 | } | |
b34976b6 | 197 | |
9e2a4898 JJ |
198 | /* Store a width sized value to memory. */ |
199 | ||
200 | static void | |
c39a58e6 | 201 | write_value (bfd *abfd, bfd_byte *buf, bfd_vma value, int width) |
9e2a4898 JJ |
202 | { |
203 | switch (width) | |
204 | { | |
205 | case 2: bfd_put_16 (abfd, value, buf); break; | |
206 | case 4: bfd_put_32 (abfd, value, buf); break; | |
207 | case 8: bfd_put_64 (abfd, value, buf); break; | |
208 | default: BFD_FAIL (); | |
209 | } | |
210 | } | |
211 | ||
bce613b9 | 212 | /* Return one if C1 and C2 CIEs can be merged. */ |
65765700 | 213 | |
bce613b9 JJ |
214 | static int |
215 | cie_eq (const void *e1, const void *e2) | |
65765700 | 216 | { |
a50b1753 NC |
217 | const struct cie *c1 = (const struct cie *) e1; |
218 | const struct cie *c2 = (const struct cie *) e2; | |
bce613b9 JJ |
219 | |
220 | if (c1->hash == c2->hash | |
221 | && c1->length == c2->length | |
65765700 | 222 | && c1->version == c2->version |
f137a54e | 223 | && c1->local_personality == c2->local_personality |
65765700 JJ |
224 | && strcmp (c1->augmentation, c2->augmentation) == 0 |
225 | && strcmp (c1->augmentation, "eh") != 0 | |
226 | && c1->code_align == c2->code_align | |
227 | && c1->data_align == c2->data_align | |
228 | && c1->ra_column == c2->ra_column | |
229 | && c1->augmentation_size == c2->augmentation_size | |
f137a54e AM |
230 | && memcmp (&c1->personality, &c2->personality, |
231 | sizeof (c1->personality)) == 0 | |
bce613b9 | 232 | && c1->output_sec == c2->output_sec |
65765700 JJ |
233 | && c1->per_encoding == c2->per_encoding |
234 | && c1->lsda_encoding == c2->lsda_encoding | |
235 | && c1->fde_encoding == c2->fde_encoding | |
c39a58e6 | 236 | && c1->initial_insn_length == c2->initial_insn_length |
99d190fa | 237 | && c1->initial_insn_length <= sizeof (c1->initial_instructions) |
65765700 JJ |
238 | && memcmp (c1->initial_instructions, |
239 | c2->initial_instructions, | |
240 | c1->initial_insn_length) == 0) | |
bce613b9 | 241 | return 1; |
65765700 | 242 | |
bce613b9 JJ |
243 | return 0; |
244 | } | |
245 | ||
246 | static hashval_t | |
247 | cie_hash (const void *e) | |
248 | { | |
a50b1753 | 249 | const struct cie *c = (const struct cie *) e; |
bce613b9 JJ |
250 | return c->hash; |
251 | } | |
252 | ||
253 | static hashval_t | |
254 | cie_compute_hash (struct cie *c) | |
255 | { | |
256 | hashval_t h = 0; | |
99d190fa | 257 | size_t len; |
bce613b9 JJ |
258 | h = iterative_hash_object (c->length, h); |
259 | h = iterative_hash_object (c->version, h); | |
260 | h = iterative_hash (c->augmentation, strlen (c->augmentation) + 1, h); | |
261 | h = iterative_hash_object (c->code_align, h); | |
262 | h = iterative_hash_object (c->data_align, h); | |
263 | h = iterative_hash_object (c->ra_column, h); | |
264 | h = iterative_hash_object (c->augmentation_size, h); | |
265 | h = iterative_hash_object (c->personality, h); | |
266 | h = iterative_hash_object (c->output_sec, h); | |
267 | h = iterative_hash_object (c->per_encoding, h); | |
268 | h = iterative_hash_object (c->lsda_encoding, h); | |
269 | h = iterative_hash_object (c->fde_encoding, h); | |
270 | h = iterative_hash_object (c->initial_insn_length, h); | |
99d190fa AM |
271 | len = c->initial_insn_length; |
272 | if (len > sizeof (c->initial_instructions)) | |
273 | len = sizeof (c->initial_instructions); | |
274 | h = iterative_hash (c->initial_instructions, len, h); | |
bce613b9 JJ |
275 | c->hash = h; |
276 | return h; | |
65765700 JJ |
277 | } |
278 | ||
353057a5 RS |
279 | /* Return the number of extra bytes that we'll be inserting into |
280 | ENTRY's augmentation string. */ | |
281 | ||
282 | static INLINE unsigned int | |
283 | extra_augmentation_string_bytes (struct eh_cie_fde *entry) | |
284 | { | |
285 | unsigned int size = 0; | |
286 | if (entry->cie) | |
287 | { | |
288 | if (entry->add_augmentation_size) | |
289 | size++; | |
6b2cc140 | 290 | if (entry->u.cie.add_fde_encoding) |
353057a5 RS |
291 | size++; |
292 | } | |
293 | return size; | |
294 | } | |
295 | ||
296 | /* Likewise ENTRY's augmentation data. */ | |
297 | ||
298 | static INLINE unsigned int | |
299 | extra_augmentation_data_bytes (struct eh_cie_fde *entry) | |
300 | { | |
301 | unsigned int size = 0; | |
6b2cc140 RS |
302 | if (entry->add_augmentation_size) |
303 | size++; | |
304 | if (entry->cie && entry->u.cie.add_fde_encoding) | |
305 | size++; | |
353057a5 RS |
306 | return size; |
307 | } | |
308 | ||
309 | /* Return the size that ENTRY will have in the output. ALIGNMENT is the | |
310 | required alignment of ENTRY in bytes. */ | |
311 | ||
312 | static unsigned int | |
313 | size_of_output_cie_fde (struct eh_cie_fde *entry, unsigned int alignment) | |
314 | { | |
315 | if (entry->removed) | |
316 | return 0; | |
317 | if (entry->size == 4) | |
318 | return 4; | |
319 | return (entry->size | |
320 | + extra_augmentation_string_bytes (entry) | |
321 | + extra_augmentation_data_bytes (entry) | |
322 | + alignment - 1) & -alignment; | |
323 | } | |
324 | ||
dcf507a6 RS |
325 | /* Assume that the bytes between *ITER and END are CFA instructions. |
326 | Try to move *ITER past the first instruction and return true on | |
327 | success. ENCODED_PTR_WIDTH gives the width of pointer entries. */ | |
328 | ||
329 | static bfd_boolean | |
330 | skip_cfa_op (bfd_byte **iter, bfd_byte *end, unsigned int encoded_ptr_width) | |
331 | { | |
332 | bfd_byte op; | |
333 | bfd_vma length; | |
334 | ||
335 | if (!read_byte (iter, end, &op)) | |
336 | return FALSE; | |
337 | ||
ac685e6a | 338 | switch (op & 0xc0 ? op & 0xc0 : op) |
dcf507a6 RS |
339 | { |
340 | case DW_CFA_nop: | |
341 | case DW_CFA_advance_loc: | |
342 | case DW_CFA_restore: | |
ac685e6a JJ |
343 | case DW_CFA_remember_state: |
344 | case DW_CFA_restore_state: | |
345 | case DW_CFA_GNU_window_save: | |
dcf507a6 RS |
346 | /* No arguments. */ |
347 | return TRUE; | |
348 | ||
349 | case DW_CFA_offset: | |
350 | case DW_CFA_restore_extended: | |
351 | case DW_CFA_undefined: | |
352 | case DW_CFA_same_value: | |
353 | case DW_CFA_def_cfa_register: | |
354 | case DW_CFA_def_cfa_offset: | |
355 | case DW_CFA_def_cfa_offset_sf: | |
356 | case DW_CFA_GNU_args_size: | |
357 | /* One leb128 argument. */ | |
358 | return skip_leb128 (iter, end); | |
359 | ||
ac685e6a JJ |
360 | case DW_CFA_val_offset: |
361 | case DW_CFA_val_offset_sf: | |
dcf507a6 RS |
362 | case DW_CFA_offset_extended: |
363 | case DW_CFA_register: | |
364 | case DW_CFA_def_cfa: | |
365 | case DW_CFA_offset_extended_sf: | |
366 | case DW_CFA_GNU_negative_offset_extended: | |
367 | case DW_CFA_def_cfa_sf: | |
368 | /* Two leb128 arguments. */ | |
369 | return (skip_leb128 (iter, end) | |
370 | && skip_leb128 (iter, end)); | |
371 | ||
372 | case DW_CFA_def_cfa_expression: | |
373 | /* A variable-length argument. */ | |
374 | return (read_uleb128 (iter, end, &length) | |
375 | && skip_bytes (iter, end, length)); | |
376 | ||
377 | case DW_CFA_expression: | |
ac685e6a | 378 | case DW_CFA_val_expression: |
dcf507a6 RS |
379 | /* A leb128 followed by a variable-length argument. */ |
380 | return (skip_leb128 (iter, end) | |
381 | && read_uleb128 (iter, end, &length) | |
382 | && skip_bytes (iter, end, length)); | |
383 | ||
384 | case DW_CFA_set_loc: | |
385 | return skip_bytes (iter, end, encoded_ptr_width); | |
386 | ||
387 | case DW_CFA_advance_loc1: | |
388 | return skip_bytes (iter, end, 1); | |
389 | ||
390 | case DW_CFA_advance_loc2: | |
391 | return skip_bytes (iter, end, 2); | |
392 | ||
393 | case DW_CFA_advance_loc4: | |
394 | return skip_bytes (iter, end, 4); | |
395 | ||
396 | case DW_CFA_MIPS_advance_loc8: | |
397 | return skip_bytes (iter, end, 8); | |
398 | ||
399 | default: | |
400 | return FALSE; | |
401 | } | |
402 | } | |
403 | ||
404 | /* Try to interpret the bytes between BUF and END as CFA instructions. | |
405 | If every byte makes sense, return a pointer to the first DW_CFA_nop | |
406 | padding byte, or END if there is no padding. Return null otherwise. | |
407 | ENCODED_PTR_WIDTH is as for skip_cfa_op. */ | |
408 | ||
409 | static bfd_byte * | |
ac685e6a JJ |
410 | skip_non_nops (bfd_byte *buf, bfd_byte *end, unsigned int encoded_ptr_width, |
411 | unsigned int *set_loc_count) | |
dcf507a6 RS |
412 | { |
413 | bfd_byte *last; | |
414 | ||
415 | last = buf; | |
416 | while (buf < end) | |
417 | if (*buf == DW_CFA_nop) | |
418 | buf++; | |
419 | else | |
420 | { | |
ac685e6a JJ |
421 | if (*buf == DW_CFA_set_loc) |
422 | ++*set_loc_count; | |
dcf507a6 RS |
423 | if (!skip_cfa_op (&buf, end, encoded_ptr_width)) |
424 | return 0; | |
425 | last = buf; | |
426 | } | |
427 | return last; | |
428 | } | |
429 | ||
30af5962 RS |
430 | /* Convert absolute encoding ENCODING into PC-relative form. |
431 | SIZE is the size of a pointer. */ | |
432 | ||
433 | static unsigned char | |
434 | make_pc_relative (unsigned char encoding, unsigned int ptr_size) | |
435 | { | |
436 | if ((encoding & 0x7f) == DW_EH_PE_absptr) | |
437 | switch (ptr_size) | |
438 | { | |
439 | case 2: | |
440 | encoding |= DW_EH_PE_sdata2; | |
441 | break; | |
442 | case 4: | |
443 | encoding |= DW_EH_PE_sdata4; | |
444 | break; | |
445 | case 8: | |
446 | encoding |= DW_EH_PE_sdata8; | |
447 | break; | |
448 | } | |
449 | return encoding | DW_EH_PE_pcrel; | |
450 | } | |
451 | ||
ca92cecb RS |
452 | /* Called before calling _bfd_elf_parse_eh_frame on every input bfd's |
453 | .eh_frame section. */ | |
65765700 | 454 | |
ca92cecb RS |
455 | void |
456 | _bfd_elf_begin_eh_frame_parsing (struct bfd_link_info *info) | |
457 | { | |
458 | struct eh_frame_hdr_info *hdr_info; | |
459 | ||
460 | hdr_info = &elf_hash_table (info)->eh_info; | |
184d07da | 461 | hdr_info->merge_cies = !info->relocatable; |
ca92cecb RS |
462 | } |
463 | ||
464 | /* Try to parse .eh_frame section SEC, which belongs to ABFD. Store the | |
465 | information in the section's sec_info field on success. COOKIE | |
466 | describes the relocations in SEC. */ | |
467 | ||
468 | void | |
469 | _bfd_elf_parse_eh_frame (bfd *abfd, struct bfd_link_info *info, | |
470 | asection *sec, struct elf_reloc_cookie *cookie) | |
65765700 | 471 | { |
acfe5567 RS |
472 | #define REQUIRE(COND) \ |
473 | do \ | |
474 | if (!(COND)) \ | |
475 | goto free_no_table; \ | |
476 | while (0) | |
477 | ||
ca92cecb | 478 | bfd_byte *ehbuf = NULL, *buf, *end; |
bce613b9 | 479 | bfd_byte *last_fde; |
ca92cecb | 480 | struct eh_cie_fde *this_inf; |
bce613b9 | 481 | unsigned int hdr_length, hdr_id; |
184d07da RS |
482 | unsigned int cie_count; |
483 | struct cie *cie, *local_cies = NULL; | |
126495ed | 484 | struct elf_link_hash_table *htab; |
65765700 | 485 | struct eh_frame_hdr_info *hdr_info; |
68f69152 | 486 | struct eh_frame_sec_info *sec_info = NULL; |
65765700 | 487 | unsigned int ptr_size; |
ca92cecb RS |
488 | unsigned int num_cies; |
489 | unsigned int num_entries; | |
9d0a14d3 | 490 | elf_gc_mark_hook_fn gc_mark_hook; |
ca92cecb RS |
491 | |
492 | htab = elf_hash_table (info); | |
493 | hdr_info = &htab->eh_info; | |
494 | if (hdr_info->parsed_eh_frames) | |
495 | return; | |
65765700 | 496 | |
4d16d575 | 497 | if (sec->size == 0 |
dbaa2011 | 498 | || sec->sec_info_type != SEC_INFO_TYPE_NONE) |
65765700 JJ |
499 | { |
500 | /* This file does not contain .eh_frame information. */ | |
ca92cecb | 501 | return; |
65765700 JJ |
502 | } |
503 | ||
e460dd0d | 504 | if (bfd_is_abs_section (sec->output_section)) |
65765700 JJ |
505 | { |
506 | /* At least one of the sections is being discarded from the | |
3472e2e9 | 507 | link, so we should just ignore them. */ |
ca92cecb | 508 | return; |
65765700 JJ |
509 | } |
510 | ||
511 | /* Read the frame unwind information from abfd. */ | |
512 | ||
acfe5567 | 513 | REQUIRE (bfd_malloc_and_get_section (abfd, sec, &ehbuf)); |
68f69152 | 514 | |
eea6121a | 515 | if (sec->size >= 4 |
65765700 JJ |
516 | && bfd_get_32 (abfd, ehbuf) == 0 |
517 | && cookie->rel == cookie->relend) | |
518 | { | |
519 | /* Empty .eh_frame section. */ | |
520 | free (ehbuf); | |
ca92cecb | 521 | return; |
65765700 JJ |
522 | } |
523 | ||
65765700 JJ |
524 | /* If .eh_frame section size doesn't fit into int, we cannot handle |
525 | it (it would need to use 64-bit .eh_frame format anyway). */ | |
acfe5567 | 526 | REQUIRE (sec->size == (unsigned int) sec->size); |
65765700 | 527 | |
8c946ed5 RS |
528 | ptr_size = (get_elf_backend_data (abfd) |
529 | ->elf_backend_eh_frame_address_size (abfd, sec)); | |
530 | REQUIRE (ptr_size != 0); | |
531 | ||
ca92cecb RS |
532 | /* Go through the section contents and work out how many FDEs and |
533 | CIEs there are. */ | |
65765700 | 534 | buf = ehbuf; |
ca92cecb RS |
535 | end = ehbuf + sec->size; |
536 | num_cies = 0; | |
537 | num_entries = 0; | |
538 | while (buf != end) | |
539 | { | |
540 | num_entries++; | |
541 | ||
542 | /* Read the length of the entry. */ | |
543 | REQUIRE (skip_bytes (&buf, end, 4)); | |
544 | hdr_length = bfd_get_32 (abfd, buf - 4); | |
545 | ||
546 | /* 64-bit .eh_frame is not supported. */ | |
547 | REQUIRE (hdr_length != 0xffffffff); | |
548 | if (hdr_length == 0) | |
549 | break; | |
550 | ||
551 | REQUIRE (skip_bytes (&buf, end, 4)); | |
552 | hdr_id = bfd_get_32 (abfd, buf - 4); | |
553 | if (hdr_id == 0) | |
554 | num_cies++; | |
555 | ||
556 | REQUIRE (skip_bytes (&buf, end, hdr_length - 4)); | |
557 | } | |
558 | ||
a50b1753 NC |
559 | sec_info = (struct eh_frame_sec_info *) |
560 | bfd_zmalloc (sizeof (struct eh_frame_sec_info) | |
561 | + (num_entries - 1) * sizeof (struct eh_cie_fde)); | |
acfe5567 | 562 | REQUIRE (sec_info); |
eea6121a | 563 | |
184d07da | 564 | /* We need to have a "struct cie" for each CIE in this section. */ |
a50b1753 | 565 | local_cies = (struct cie *) bfd_zmalloc (num_cies * sizeof (*local_cies)); |
184d07da | 566 | REQUIRE (local_cies); |
65765700 | 567 | |
5dabe785 | 568 | /* FIXME: octets_per_byte. */ |
65765700 | 569 | #define ENSURE_NO_RELOCS(buf) \ |
acfe5567 RS |
570 | REQUIRE (!(cookie->rel < cookie->relend \ |
571 | && (cookie->rel->r_offset \ | |
572 | < (bfd_size_type) ((buf) - ehbuf)) \ | |
573 | && cookie->rel->r_info != 0)) | |
65765700 | 574 | |
5dabe785 | 575 | /* FIXME: octets_per_byte. */ |
65765700 JJ |
576 | #define SKIP_RELOCS(buf) \ |
577 | while (cookie->rel < cookie->relend \ | |
3472e2e9 | 578 | && (cookie->rel->r_offset \ |
65765700 JJ |
579 | < (bfd_size_type) ((buf) - ehbuf))) \ |
580 | cookie->rel++ | |
581 | ||
5dabe785 | 582 | /* FIXME: octets_per_byte. */ |
65765700 JJ |
583 | #define GET_RELOC(buf) \ |
584 | ((cookie->rel < cookie->relend \ | |
585 | && (cookie->rel->r_offset \ | |
3472e2e9 | 586 | == (bfd_size_type) ((buf) - ehbuf))) \ |
65765700 JJ |
587 | ? cookie->rel : NULL) |
588 | ||
ca92cecb | 589 | buf = ehbuf; |
184d07da | 590 | cie_count = 0; |
9d0a14d3 | 591 | gc_mark_hook = get_elf_backend_data (abfd)->gc_mark_hook; |
ca92cecb | 592 | while ((bfd_size_type) (buf - ehbuf) != sec->size) |
65765700 | 593 | { |
f075ee0c | 594 | char *aug; |
ca92cecb | 595 | bfd_byte *start, *insns, *insns_end; |
2c42be65 | 596 | bfd_size_type length; |
ac685e6a | 597 | unsigned int set_loc_count; |
65765700 | 598 | |
fda3ecf2 | 599 | this_inf = sec_info->entry + sec_info->count; |
65765700 | 600 | last_fde = buf; |
bce613b9 | 601 | |
bce613b9 JJ |
602 | /* Read the length of the entry. */ |
603 | REQUIRE (skip_bytes (&buf, ehbuf + sec->size, 4)); | |
604 | hdr_length = bfd_get_32 (abfd, buf - 4); | |
acfe5567 | 605 | |
bce613b9 JJ |
606 | /* The CIE/FDE must be fully contained in this input section. */ |
607 | REQUIRE ((bfd_size_type) (buf - ehbuf) + hdr_length <= sec->size); | |
608 | end = buf + hdr_length; | |
65765700 | 609 | |
bce613b9 JJ |
610 | this_inf->offset = last_fde - ehbuf; |
611 | this_inf->size = 4 + hdr_length; | |
155eaaa0 | 612 | this_inf->reloc_index = cookie->rel - cookie->rels; |
bce613b9 JJ |
613 | |
614 | if (hdr_length == 0) | |
615 | { | |
616 | /* A zero-length CIE should only be found at the end of | |
617 | the section. */ | |
618 | REQUIRE ((bfd_size_type) (buf - ehbuf) == sec->size); | |
619 | ENSURE_NO_RELOCS (buf); | |
620 | sec_info->count++; | |
621 | break; | |
65765700 JJ |
622 | } |
623 | ||
bce613b9 JJ |
624 | REQUIRE (skip_bytes (&buf, end, 4)); |
625 | hdr_id = bfd_get_32 (abfd, buf - 4); | |
626 | ||
627 | if (hdr_id == 0) | |
65765700 JJ |
628 | { |
629 | unsigned int initial_insn_length; | |
630 | ||
631 | /* CIE */ | |
bce613b9 JJ |
632 | this_inf->cie = 1; |
633 | ||
184d07da RS |
634 | /* Point CIE to one of the section-local cie structures. */ |
635 | cie = local_cies + cie_count++; | |
636 | ||
ca92cecb | 637 | cie->cie_inf = this_inf; |
bce613b9 | 638 | cie->length = hdr_length; |
ca92cecb | 639 | cie->output_sec = sec->output_section; |
ac685e6a | 640 | start = buf; |
bce613b9 | 641 | REQUIRE (read_byte (&buf, end, &cie->version)); |
65765700 JJ |
642 | |
643 | /* Cannot handle unknown versions. */ | |
604282a7 JJ |
644 | REQUIRE (cie->version == 1 |
645 | || cie->version == 3 | |
646 | || cie->version == 4); | |
bce613b9 | 647 | REQUIRE (strlen ((char *) buf) < sizeof (cie->augmentation)); |
65765700 | 648 | |
bce613b9 | 649 | strcpy (cie->augmentation, (char *) buf); |
f075ee0c | 650 | buf = (bfd_byte *) strchr ((char *) buf, '\0') + 1; |
65765700 JJ |
651 | ENSURE_NO_RELOCS (buf); |
652 | if (buf[0] == 'e' && buf[1] == 'h') | |
653 | { | |
654 | /* GCC < 3.0 .eh_frame CIE */ | |
655 | /* We cannot merge "eh" CIEs because __EXCEPTION_TABLE__ | |
656 | is private to each CIE, so we don't need it for anything. | |
657 | Just skip it. */ | |
2c42be65 | 658 | REQUIRE (skip_bytes (&buf, end, ptr_size)); |
65765700 JJ |
659 | SKIP_RELOCS (buf); |
660 | } | |
604282a7 JJ |
661 | if (cie->version >= 4) |
662 | { | |
663 | REQUIRE (buf + 1 < end); | |
664 | REQUIRE (buf[0] == ptr_size); | |
665 | REQUIRE (buf[1] == 0); | |
666 | buf += 2; | |
667 | } | |
bce613b9 JJ |
668 | REQUIRE (read_uleb128 (&buf, end, &cie->code_align)); |
669 | REQUIRE (read_sleb128 (&buf, end, &cie->data_align)); | |
670 | if (cie->version == 1) | |
2c42be65 RS |
671 | { |
672 | REQUIRE (buf < end); | |
bce613b9 | 673 | cie->ra_column = *buf++; |
2c42be65 | 674 | } |
0da76f83 | 675 | else |
bce613b9 | 676 | REQUIRE (read_uleb128 (&buf, end, &cie->ra_column)); |
65765700 | 677 | ENSURE_NO_RELOCS (buf); |
bce613b9 JJ |
678 | cie->lsda_encoding = DW_EH_PE_omit; |
679 | cie->fde_encoding = DW_EH_PE_omit; | |
680 | cie->per_encoding = DW_EH_PE_omit; | |
681 | aug = cie->augmentation; | |
65765700 JJ |
682 | if (aug[0] != 'e' || aug[1] != 'h') |
683 | { | |
684 | if (*aug == 'z') | |
685 | { | |
686 | aug++; | |
bce613b9 | 687 | REQUIRE (read_uleb128 (&buf, end, &cie->augmentation_size)); |
65765700 JJ |
688 | ENSURE_NO_RELOCS (buf); |
689 | } | |
690 | ||
691 | while (*aug != '\0') | |
692 | switch (*aug++) | |
693 | { | |
694 | case 'L': | |
bce613b9 | 695 | REQUIRE (read_byte (&buf, end, &cie->lsda_encoding)); |
65765700 | 696 | ENSURE_NO_RELOCS (buf); |
bce613b9 | 697 | REQUIRE (get_DW_EH_PE_width (cie->lsda_encoding, ptr_size)); |
65765700 JJ |
698 | break; |
699 | case 'R': | |
bce613b9 | 700 | REQUIRE (read_byte (&buf, end, &cie->fde_encoding)); |
65765700 | 701 | ENSURE_NO_RELOCS (buf); |
bce613b9 | 702 | REQUIRE (get_DW_EH_PE_width (cie->fde_encoding, ptr_size)); |
65765700 | 703 | break; |
63752a75 JJ |
704 | case 'S': |
705 | break; | |
65765700 JJ |
706 | case 'P': |
707 | { | |
708 | int per_width; | |
709 | ||
bce613b9 JJ |
710 | REQUIRE (read_byte (&buf, end, &cie->per_encoding)); |
711 | per_width = get_DW_EH_PE_width (cie->per_encoding, | |
65765700 | 712 | ptr_size); |
acfe5567 | 713 | REQUIRE (per_width); |
18e04883 | 714 | if ((cie->per_encoding & 0x70) == DW_EH_PE_aligned) |
2c42be65 RS |
715 | { |
716 | length = -(buf - ehbuf) & (per_width - 1); | |
717 | REQUIRE (skip_bytes (&buf, end, length)); | |
718 | } | |
18e04883 | 719 | this_inf->u.cie.personality_offset = buf - start; |
65765700 | 720 | ENSURE_NO_RELOCS (buf); |
f137a54e | 721 | /* Ensure we have a reloc here. */ |
184d07da RS |
722 | REQUIRE (GET_RELOC (buf)); |
723 | cie->personality.reloc_index | |
724 | = cookie->rel - cookie->rels; | |
725 | /* Cope with MIPS-style composite relocations. */ | |
726 | do | |
727 | cookie->rel++; | |
728 | while (GET_RELOC (buf) != NULL); | |
2c42be65 | 729 | REQUIRE (skip_bytes (&buf, end, per_width)); |
65765700 JJ |
730 | } |
731 | break; | |
732 | default: | |
733 | /* Unrecognized augmentation. Better bail out. */ | |
734 | goto free_no_table; | |
735 | } | |
736 | } | |
737 | ||
738 | /* For shared libraries, try to get rid of as many RELATIVE relocs | |
0bb2d96a | 739 | as possible. */ |
3472e2e9 | 740 | if (info->shared |
ec3391e7 AO |
741 | && (get_elf_backend_data (abfd) |
742 | ->elf_backend_can_make_relative_eh_frame | |
353057a5 RS |
743 | (abfd, info, sec))) |
744 | { | |
18e04883 | 745 | if ((cie->fde_encoding & 0x70) == DW_EH_PE_absptr) |
6b2cc140 | 746 | this_inf->make_relative = 1; |
353057a5 RS |
747 | /* If the CIE doesn't already have an 'R' entry, it's fairly |
748 | easy to add one, provided that there's no aligned data | |
749 | after the augmentation string. */ | |
bce613b9 | 750 | else if (cie->fde_encoding == DW_EH_PE_omit |
18e04883 | 751 | && (cie->per_encoding & 0x70) != DW_EH_PE_aligned) |
353057a5 | 752 | { |
bce613b9 | 753 | if (*cie->augmentation == 0) |
353057a5 | 754 | this_inf->add_augmentation_size = 1; |
6b2cc140 RS |
755 | this_inf->u.cie.add_fde_encoding = 1; |
756 | this_inf->make_relative = 1; | |
353057a5 | 757 | } |
65765700 | 758 | |
18e04883 RS |
759 | if ((cie->lsda_encoding & 0x70) == DW_EH_PE_absptr) |
760 | cie->can_make_lsda_relative = 1; | |
761 | } | |
9e2a4898 | 762 | |
65765700 JJ |
763 | /* If FDE encoding was not specified, it defaults to |
764 | DW_EH_absptr. */ | |
bce613b9 JJ |
765 | if (cie->fde_encoding == DW_EH_PE_omit) |
766 | cie->fde_encoding = DW_EH_PE_absptr; | |
65765700 | 767 | |
dcf507a6 | 768 | initial_insn_length = end - buf; |
99d190fa AM |
769 | cie->initial_insn_length = initial_insn_length; |
770 | memcpy (cie->initial_instructions, buf, | |
771 | initial_insn_length <= sizeof (cie->initial_instructions) | |
772 | ? initial_insn_length : sizeof (cie->initial_instructions)); | |
dcf507a6 | 773 | insns = buf; |
65765700 JJ |
774 | buf += initial_insn_length; |
775 | ENSURE_NO_RELOCS (buf); | |
ca92cecb | 776 | |
184d07da RS |
777 | if (hdr_info->merge_cies) |
778 | this_inf->u.cie.u.full_cie = cie; | |
6b2cc140 | 779 | this_inf->u.cie.per_encoding_relative |
ca92cecb | 780 | = (cie->per_encoding & 0x70) == DW_EH_PE_pcrel; |
65765700 JJ |
781 | } |
782 | else | |
783 | { | |
bce613b9 JJ |
784 | /* Find the corresponding CIE. */ |
785 | unsigned int cie_offset = this_inf->offset + 4 - hdr_id; | |
184d07da RS |
786 | for (cie = local_cies; cie < local_cies + cie_count; cie++) |
787 | if (cie_offset == cie->cie_inf->offset) | |
bce613b9 JJ |
788 | break; |
789 | ||
790 | /* Ensure this FDE references one of the CIEs in this input | |
791 | section. */ | |
184d07da RS |
792 | REQUIRE (cie != local_cies + cie_count); |
793 | this_inf->u.fde.cie_inf = cie->cie_inf; | |
794 | this_inf->make_relative = cie->cie_inf->make_relative; | |
6b2cc140 | 795 | this_inf->add_augmentation_size |
184d07da | 796 | = cie->cie_inf->add_augmentation_size; |
65765700 JJ |
797 | |
798 | ENSURE_NO_RELOCS (buf); | |
e41b3a13 | 799 | if ((sec->flags & SEC_LINKER_CREATED) == 0 || cookie->rels != NULL) |
2a7b2e88 | 800 | { |
e41b3a13 JJ |
801 | asection *rsec; |
802 | ||
803 | REQUIRE (GET_RELOC (buf)); | |
804 | ||
805 | /* Chain together the FDEs for each section. */ | |
806 | rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie); | |
807 | /* RSEC will be NULL if FDE was cleared out as it was belonging to | |
808 | a discarded SHT_GROUP. */ | |
809 | if (rsec) | |
810 | { | |
811 | REQUIRE (rsec->owner == abfd); | |
812 | this_inf->u.fde.next_for_section = elf_fde_list (rsec); | |
813 | elf_fde_list (rsec) = this_inf; | |
814 | } | |
2a7b2e88 | 815 | } |
9d0a14d3 | 816 | |
2c42be65 RS |
817 | /* Skip the initial location and address range. */ |
818 | start = buf; | |
bce613b9 | 819 | length = get_DW_EH_PE_width (cie->fde_encoding, ptr_size); |
2c42be65 RS |
820 | REQUIRE (skip_bytes (&buf, end, 2 * length)); |
821 | ||
822 | /* Skip the augmentation size, if present. */ | |
bce613b9 | 823 | if (cie->augmentation[0] == 'z') |
dcf507a6 RS |
824 | REQUIRE (read_uleb128 (&buf, end, &length)); |
825 | else | |
826 | length = 0; | |
2c42be65 RS |
827 | |
828 | /* Of the supported augmentation characters above, only 'L' | |
829 | adds augmentation data to the FDE. This code would need to | |
830 | be adjusted if any future augmentations do the same thing. */ | |
bce613b9 | 831 | if (cie->lsda_encoding != DW_EH_PE_omit) |
dcf507a6 | 832 | { |
9f4b847e RS |
833 | SKIP_RELOCS (buf); |
834 | if (cie->can_make_lsda_relative && GET_RELOC (buf)) | |
835 | cie->cie_inf->u.cie.make_lsda_relative = 1; | |
dcf507a6 RS |
836 | this_inf->lsda_offset = buf - start; |
837 | /* If there's no 'z' augmentation, we don't know where the | |
838 | CFA insns begin. Assume no padding. */ | |
bce613b9 | 839 | if (cie->augmentation[0] != 'z') |
dcf507a6 RS |
840 | length = end - buf; |
841 | } | |
842 | ||
843 | /* Skip over the augmentation data. */ | |
844 | REQUIRE (skip_bytes (&buf, end, length)); | |
845 | insns = buf; | |
9e2a4898 | 846 | |
bce613b9 | 847 | buf = last_fde + 4 + hdr_length; |
2a7b2e88 | 848 | |
273f4430 JK |
849 | /* For NULL RSEC (cleared FDE belonging to a discarded section) |
850 | the relocations are commonly cleared. We do not sanity check if | |
851 | all these relocations are cleared as (1) relocations to | |
852 | .gcc_except_table will remain uncleared (they will get dropped | |
853 | with the drop of this unused FDE) and (2) BFD already safely drops | |
854 | relocations of any type to .eh_frame by | |
855 | elf_section_ignore_discarded_relocs. | |
856 | TODO: The .gcc_except_table entries should be also filtered as | |
857 | .eh_frame entries; or GCC could rather use COMDAT for them. */ | |
858 | SKIP_RELOCS (buf); | |
65765700 JJ |
859 | } |
860 | ||
dcf507a6 RS |
861 | /* Try to interpret the CFA instructions and find the first |
862 | padding nop. Shrink this_inf's size so that it doesn't | |
ac685e6a | 863 | include the padding. */ |
bce613b9 | 864 | length = get_DW_EH_PE_width (cie->fde_encoding, ptr_size); |
ac685e6a JJ |
865 | set_loc_count = 0; |
866 | insns_end = skip_non_nops (insns, end, length, &set_loc_count); | |
867 | /* If we don't understand the CFA instructions, we can't know | |
868 | what needs to be adjusted there. */ | |
869 | if (insns_end == NULL | |
870 | /* For the time being we don't support DW_CFA_set_loc in | |
871 | CIE instructions. */ | |
872 | || (set_loc_count && this_inf->cie)) | |
873 | goto free_no_table; | |
874 | this_inf->size -= end - insns_end; | |
bce613b9 JJ |
875 | if (insns_end != end && this_inf->cie) |
876 | { | |
877 | cie->initial_insn_length -= end - insns_end; | |
878 | cie->length -= end - insns_end; | |
879 | } | |
ac685e6a | 880 | if (set_loc_count |
18e04883 | 881 | && ((cie->fde_encoding & 0x70) == DW_EH_PE_pcrel |
6b2cc140 | 882 | || this_inf->make_relative)) |
ac685e6a JJ |
883 | { |
884 | unsigned int cnt; | |
885 | bfd_byte *p; | |
886 | ||
a50b1753 NC |
887 | this_inf->set_loc = (unsigned int *) |
888 | bfd_malloc ((set_loc_count + 1) * sizeof (unsigned int)); | |
ac685e6a JJ |
889 | REQUIRE (this_inf->set_loc); |
890 | this_inf->set_loc[0] = set_loc_count; | |
891 | p = insns; | |
892 | cnt = 0; | |
893 | while (p < end) | |
894 | { | |
895 | if (*p == DW_CFA_set_loc) | |
896 | this_inf->set_loc[++cnt] = p + 1 - start; | |
897 | REQUIRE (skip_cfa_op (&p, end, length)); | |
898 | } | |
899 | } | |
dcf507a6 | 900 | |
ca92cecb | 901 | this_inf->removed = 1; |
bce613b9 JJ |
902 | this_inf->fde_encoding = cie->fde_encoding; |
903 | this_inf->lsda_encoding = cie->lsda_encoding; | |
65765700 JJ |
904 | sec_info->count++; |
905 | } | |
ca92cecb | 906 | BFD_ASSERT (sec_info->count == num_entries); |
184d07da | 907 | BFD_ASSERT (cie_count == num_cies); |
65765700 JJ |
908 | |
909 | elf_section_data (sec)->sec_info = sec_info; | |
dbaa2011 | 910 | sec->sec_info_type = SEC_INFO_TYPE_EH_FRAME; |
184d07da RS |
911 | if (hdr_info->merge_cies) |
912 | { | |
913 | sec_info->cies = local_cies; | |
914 | local_cies = NULL; | |
915 | } | |
ca92cecb | 916 | goto success; |
65765700 | 917 | |
ca92cecb RS |
918 | free_no_table: |
919 | (*info->callbacks->einfo) | |
920 | (_("%P: error in %B(%A); no .eh_frame_hdr table will be created.\n"), | |
921 | abfd, sec); | |
922 | hdr_info->table = FALSE; | |
923 | if (sec_info) | |
924 | free (sec_info); | |
925 | success: | |
926 | if (ehbuf) | |
927 | free (ehbuf); | |
ca92cecb RS |
928 | if (local_cies) |
929 | free (local_cies); | |
930 | #undef REQUIRE | |
931 | } | |
bce613b9 | 932 | |
ca92cecb RS |
933 | /* Finish a pass over all .eh_frame sections. */ |
934 | ||
935 | void | |
936 | _bfd_elf_end_eh_frame_parsing (struct bfd_link_info *info) | |
937 | { | |
938 | struct eh_frame_hdr_info *hdr_info; | |
939 | ||
940 | hdr_info = &elf_hash_table (info)->eh_info; | |
ca92cecb RS |
941 | hdr_info->parsed_eh_frames = TRUE; |
942 | } | |
bce613b9 | 943 | |
9d0a14d3 RS |
944 | /* Mark all relocations against CIE or FDE ENT, which occurs in |
945 | .eh_frame section SEC. COOKIE describes the relocations in SEC; | |
946 | its "rel" field can be changed freely. */ | |
947 | ||
948 | static bfd_boolean | |
949 | mark_entry (struct bfd_link_info *info, asection *sec, | |
950 | struct eh_cie_fde *ent, elf_gc_mark_hook_fn gc_mark_hook, | |
951 | struct elf_reloc_cookie *cookie) | |
952 | { | |
5dabe785 | 953 | /* FIXME: octets_per_byte. */ |
9d0a14d3 RS |
954 | for (cookie->rel = cookie->rels + ent->reloc_index; |
955 | cookie->rel < cookie->relend | |
956 | && cookie->rel->r_offset < ent->offset + ent->size; | |
957 | cookie->rel++) | |
958 | if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, cookie)) | |
959 | return FALSE; | |
960 | ||
961 | return TRUE; | |
962 | } | |
963 | ||
964 | /* Mark all the relocations against FDEs that relate to code in input | |
965 | section SEC. The FDEs belong to .eh_frame section EH_FRAME, whose | |
966 | relocations are described by COOKIE. */ | |
967 | ||
968 | bfd_boolean | |
969 | _bfd_elf_gc_mark_fdes (struct bfd_link_info *info, asection *sec, | |
970 | asection *eh_frame, elf_gc_mark_hook_fn gc_mark_hook, | |
971 | struct elf_reloc_cookie *cookie) | |
972 | { | |
184d07da | 973 | struct eh_cie_fde *fde, *cie; |
9d0a14d3 RS |
974 | |
975 | for (fde = elf_fde_list (sec); fde; fde = fde->u.fde.next_for_section) | |
976 | { | |
977 | if (!mark_entry (info, eh_frame, fde, gc_mark_hook, cookie)) | |
978 | return FALSE; | |
979 | ||
980 | /* At this stage, all cie_inf fields point to local CIEs, so we | |
981 | can use the same cookie to refer to them. */ | |
982 | cie = fde->u.fde.cie_inf; | |
184d07da | 983 | if (!cie->u.cie.gc_mark) |
9d0a14d3 | 984 | { |
184d07da | 985 | cie->u.cie.gc_mark = 1; |
9d0a14d3 RS |
986 | if (!mark_entry (info, eh_frame, cie, gc_mark_hook, cookie)) |
987 | return FALSE; | |
988 | } | |
989 | } | |
990 | return TRUE; | |
991 | } | |
992 | ||
184d07da RS |
993 | /* Input section SEC of ABFD is an .eh_frame section that contains the |
994 | CIE described by CIE_INF. Return a version of CIE_INF that is going | |
995 | to be kept in the output, adding CIE_INF to the output if necessary. | |
996 | ||
997 | HDR_INFO is the .eh_frame_hdr information and COOKIE describes the | |
998 | relocations in REL. */ | |
999 | ||
1000 | static struct eh_cie_fde * | |
18e04883 | 1001 | find_merged_cie (bfd *abfd, struct bfd_link_info *info, asection *sec, |
184d07da RS |
1002 | struct eh_frame_hdr_info *hdr_info, |
1003 | struct elf_reloc_cookie *cookie, | |
1004 | struct eh_cie_fde *cie_inf) | |
1005 | { | |
1006 | unsigned long r_symndx; | |
1007 | struct cie *cie, *new_cie; | |
1008 | Elf_Internal_Rela *rel; | |
1009 | void **loc; | |
1010 | ||
1011 | /* Use CIE_INF if we have already decided to keep it. */ | |
1012 | if (!cie_inf->removed) | |
1013 | return cie_inf; | |
1014 | ||
1015 | /* If we have merged CIE_INF with another CIE, use that CIE instead. */ | |
1016 | if (cie_inf->u.cie.merged) | |
1017 | return cie_inf->u.cie.u.merged_with; | |
1018 | ||
1019 | cie = cie_inf->u.cie.u.full_cie; | |
1020 | ||
1021 | /* Assume we will need to keep CIE_INF. */ | |
1022 | cie_inf->removed = 0; | |
1023 | cie_inf->u.cie.u.sec = sec; | |
1024 | ||
1025 | /* If we are not merging CIEs, use CIE_INF. */ | |
1026 | if (cie == NULL) | |
1027 | return cie_inf; | |
1028 | ||
1029 | if (cie->per_encoding != DW_EH_PE_omit) | |
1030 | { | |
18e04883 RS |
1031 | bfd_boolean per_binds_local; |
1032 | ||
184d07da RS |
1033 | /* Work out the address of personality routine, either as an absolute |
1034 | value or as a symbol. */ | |
1035 | rel = cookie->rels + cie->personality.reloc_index; | |
1036 | memset (&cie->personality, 0, sizeof (cie->personality)); | |
1037 | #ifdef BFD64 | |
1038 | if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64) | |
1039 | r_symndx = ELF64_R_SYM (rel->r_info); | |
1040 | else | |
1041 | #endif | |
1042 | r_symndx = ELF32_R_SYM (rel->r_info); | |
1043 | if (r_symndx >= cookie->locsymcount | |
1044 | || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL) | |
1045 | { | |
1046 | struct elf_link_hash_entry *h; | |
1047 | ||
1048 | r_symndx -= cookie->extsymoff; | |
1049 | h = cookie->sym_hashes[r_symndx]; | |
1050 | ||
1051 | while (h->root.type == bfd_link_hash_indirect | |
1052 | || h->root.type == bfd_link_hash_warning) | |
1053 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1054 | ||
1055 | cie->personality.h = h; | |
18e04883 | 1056 | per_binds_local = SYMBOL_REFERENCES_LOCAL (info, h); |
184d07da RS |
1057 | } |
1058 | else | |
1059 | { | |
1060 | Elf_Internal_Sym *sym; | |
1061 | asection *sym_sec; | |
1062 | ||
1063 | sym = &cookie->locsyms[r_symndx]; | |
1064 | sym_sec = bfd_section_from_elf_index (abfd, sym->st_shndx); | |
1065 | if (sym_sec == NULL) | |
1066 | return cie_inf; | |
1067 | ||
1068 | if (sym_sec->kept_section != NULL) | |
1069 | sym_sec = sym_sec->kept_section; | |
1070 | if (sym_sec->output_section == NULL) | |
1071 | return cie_inf; | |
1072 | ||
1073 | cie->local_personality = 1; | |
1074 | cie->personality.val = (sym->st_value | |
1075 | + sym_sec->output_offset | |
1076 | + sym_sec->output_section->vma); | |
18e04883 RS |
1077 | per_binds_local = TRUE; |
1078 | } | |
1079 | ||
1080 | if (per_binds_local | |
1081 | && info->shared | |
1082 | && (cie->per_encoding & 0x70) == DW_EH_PE_absptr | |
1083 | && (get_elf_backend_data (abfd) | |
1084 | ->elf_backend_can_make_relative_eh_frame (abfd, info, sec))) | |
1085 | { | |
1086 | cie_inf->u.cie.make_per_encoding_relative = 1; | |
1087 | cie_inf->u.cie.per_encoding_relative = 1; | |
184d07da RS |
1088 | } |
1089 | } | |
1090 | ||
1091 | /* See if we can merge this CIE with an earlier one. */ | |
1092 | cie->output_sec = sec->output_section; | |
1093 | cie_compute_hash (cie); | |
1094 | if (hdr_info->cies == NULL) | |
1095 | { | |
1096 | hdr_info->cies = htab_try_create (1, cie_hash, cie_eq, free); | |
1097 | if (hdr_info->cies == NULL) | |
1098 | return cie_inf; | |
1099 | } | |
1100 | loc = htab_find_slot_with_hash (hdr_info->cies, cie, cie->hash, INSERT); | |
1101 | if (loc == NULL) | |
1102 | return cie_inf; | |
1103 | ||
1104 | new_cie = (struct cie *) *loc; | |
1105 | if (new_cie == NULL) | |
1106 | { | |
1107 | /* Keep CIE_INF and record it in the hash table. */ | |
a50b1753 | 1108 | new_cie = (struct cie *) malloc (sizeof (struct cie)); |
184d07da RS |
1109 | if (new_cie == NULL) |
1110 | return cie_inf; | |
1111 | ||
1112 | memcpy (new_cie, cie, sizeof (struct cie)); | |
1113 | *loc = new_cie; | |
1114 | } | |
1115 | else | |
1116 | { | |
1117 | /* Merge CIE_INF with NEW_CIE->CIE_INF. */ | |
1118 | cie_inf->removed = 1; | |
1119 | cie_inf->u.cie.merged = 1; | |
1120 | cie_inf->u.cie.u.merged_with = new_cie->cie_inf; | |
1121 | if (cie_inf->u.cie.make_lsda_relative) | |
1122 | new_cie->cie_inf->u.cie.make_lsda_relative = 1; | |
1123 | } | |
1124 | return new_cie->cie_inf; | |
1125 | } | |
1126 | ||
ca92cecb RS |
1127 | /* This function is called for each input file before the .eh_frame |
1128 | section is relocated. It discards duplicate CIEs and FDEs for discarded | |
1129 | functions. The function returns TRUE iff any entries have been | |
1130 | deleted. */ | |
1131 | ||
1132 | bfd_boolean | |
1133 | _bfd_elf_discard_section_eh_frame | |
1134 | (bfd *abfd, struct bfd_link_info *info, asection *sec, | |
1135 | bfd_boolean (*reloc_symbol_deleted_p) (bfd_vma, void *), | |
1136 | struct elf_reloc_cookie *cookie) | |
1137 | { | |
184d07da | 1138 | struct eh_cie_fde *ent; |
ca92cecb RS |
1139 | struct eh_frame_sec_info *sec_info; |
1140 | struct eh_frame_hdr_info *hdr_info; | |
1141 | unsigned int ptr_size, offset; | |
1142 | ||
dbaa2011 | 1143 | if (sec->sec_info_type != SEC_INFO_TYPE_EH_FRAME) |
4d16d575 AM |
1144 | return FALSE; |
1145 | ||
ca92cecb RS |
1146 | sec_info = (struct eh_frame_sec_info *) elf_section_data (sec)->sec_info; |
1147 | if (sec_info == NULL) | |
1148 | return FALSE; | |
1149 | ||
e41b3a13 JJ |
1150 | ptr_size = (get_elf_backend_data (sec->owner) |
1151 | ->elf_backend_eh_frame_address_size (sec->owner, sec)); | |
1152 | ||
ca92cecb | 1153 | hdr_info = &elf_hash_table (info)->eh_info; |
fda3ecf2 | 1154 | for (ent = sec_info->entry; ent < sec_info->entry + sec_info->count; ++ent) |
f60e73e9 AM |
1155 | if (ent->size == 4) |
1156 | /* There should only be one zero terminator, on the last input | |
1157 | file supplying .eh_frame (crtend.o). Remove any others. */ | |
1158 | ent->removed = sec->map_head.s != NULL; | |
1159 | else if (!ent->cie) | |
fda3ecf2 | 1160 | { |
e41b3a13 JJ |
1161 | bfd_boolean keep; |
1162 | if ((sec->flags & SEC_LINKER_CREATED) != 0 && cookie->rels == NULL) | |
1163 | { | |
1164 | unsigned int width | |
1165 | = get_DW_EH_PE_width (ent->fde_encoding, ptr_size); | |
1166 | bfd_vma value | |
1167 | = read_value (abfd, sec->contents + ent->offset + 8 + width, | |
1168 | width, get_DW_EH_PE_signed (ent->fde_encoding)); | |
1169 | keep = value != 0; | |
1170 | } | |
1171 | else | |
1172 | { | |
1173 | cookie->rel = cookie->rels + ent->reloc_index; | |
1174 | /* FIXME: octets_per_byte. */ | |
1175 | BFD_ASSERT (cookie->rel < cookie->relend | |
1176 | && cookie->rel->r_offset == ent->offset + 8); | |
1177 | keep = !(*reloc_symbol_deleted_p) (ent->offset + 8, cookie); | |
1178 | } | |
1179 | if (keep) | |
bce613b9 | 1180 | { |
ca92cecb | 1181 | if (info->shared |
18e04883 | 1182 | && (((ent->fde_encoding & 0x70) == DW_EH_PE_absptr |
6b2cc140 | 1183 | && ent->make_relative == 0) |
18e04883 | 1184 | || (ent->fde_encoding & 0x70) == DW_EH_PE_aligned)) |
ca92cecb RS |
1185 | { |
1186 | /* If a shared library uses absolute pointers | |
1187 | which we cannot turn into PC relative, | |
1188 | don't create the binary search table, | |
1189 | since it is affected by runtime relocations. */ | |
1190 | hdr_info->table = FALSE; | |
1191 | (*info->callbacks->einfo) | |
1192 | (_("%P: fde encoding in %B(%A) prevents .eh_frame_hdr" | |
1193 | " table being created.\n"), abfd, sec); | |
1194 | } | |
1195 | ent->removed = 0; | |
1196 | hdr_info->fde_count++; | |
18e04883 RS |
1197 | ent->u.fde.cie_inf = find_merged_cie (abfd, info, sec, hdr_info, |
1198 | cookie, ent->u.fde.cie_inf); | |
bce613b9 | 1199 | } |
ca92cecb RS |
1200 | } |
1201 | ||
184d07da RS |
1202 | if (sec_info->cies) |
1203 | { | |
1204 | free (sec_info->cies); | |
1205 | sec_info->cies = NULL; | |
1206 | } | |
1207 | ||
ca92cecb RS |
1208 | offset = 0; |
1209 | for (ent = sec_info->entry; ent < sec_info->entry + sec_info->count; ++ent) | |
1210 | if (!ent->removed) | |
1211 | { | |
353057a5 RS |
1212 | ent->new_offset = offset; |
1213 | offset += size_of_output_cie_fde (ent, ptr_size); | |
fda3ecf2 | 1214 | } |
65765700 | 1215 | |
eea6121a | 1216 | sec->rawsize = sec->size; |
353057a5 | 1217 | sec->size = offset; |
353057a5 | 1218 | return offset != sec->rawsize; |
65765700 JJ |
1219 | } |
1220 | ||
1221 | /* This function is called for .eh_frame_hdr section after | |
1222 | _bfd_elf_discard_section_eh_frame has been called on all .eh_frame | |
1223 | input sections. It finalizes the size of .eh_frame_hdr section. */ | |
1224 | ||
b34976b6 | 1225 | bfd_boolean |
c39a58e6 | 1226 | _bfd_elf_discard_section_eh_frame_hdr (bfd *abfd, struct bfd_link_info *info) |
65765700 | 1227 | { |
126495ed | 1228 | struct elf_link_hash_table *htab; |
65765700 | 1229 | struct eh_frame_hdr_info *hdr_info; |
126495ed | 1230 | asection *sec; |
65765700 | 1231 | |
126495ed AM |
1232 | htab = elf_hash_table (info); |
1233 | hdr_info = &htab->eh_info; | |
bce613b9 | 1234 | |
184d07da RS |
1235 | if (hdr_info->cies != NULL) |
1236 | { | |
1237 | htab_delete (hdr_info->cies); | |
1238 | hdr_info->cies = NULL; | |
1239 | } | |
1240 | ||
126495ed AM |
1241 | sec = hdr_info->hdr_sec; |
1242 | if (sec == NULL) | |
b34976b6 | 1243 | return FALSE; |
126495ed | 1244 | |
eea6121a | 1245 | sec->size = EH_FRAME_HDR_SIZE; |
65765700 | 1246 | if (hdr_info->table) |
eea6121a | 1247 | sec->size += 4 + hdr_info->fde_count * 8; |
65765700 | 1248 | |
12bd6957 | 1249 | elf_eh_frame_hdr (abfd) = sec; |
b34976b6 | 1250 | return TRUE; |
65765700 JJ |
1251 | } |
1252 | ||
9a2a56cc AM |
1253 | /* Return true if there is at least one non-empty .eh_frame section in |
1254 | input files. Can only be called after ld has mapped input to | |
1255 | output sections, and before sections are stripped. */ | |
1256 | bfd_boolean | |
1257 | _bfd_elf_eh_frame_present (struct bfd_link_info *info) | |
1258 | { | |
1259 | asection *eh = bfd_get_section_by_name (info->output_bfd, ".eh_frame"); | |
1260 | ||
1261 | if (eh == NULL) | |
1262 | return FALSE; | |
1263 | ||
1264 | /* Count only sections which have at least a single CIE or FDE. | |
1265 | There cannot be any CIE or FDE <= 8 bytes. */ | |
1266 | for (eh = eh->map_head.s; eh != NULL; eh = eh->map_head.s) | |
1267 | if (eh->size > 8) | |
1268 | return TRUE; | |
1269 | ||
1270 | return FALSE; | |
1271 | } | |
1272 | ||
68f69152 JJ |
1273 | /* This function is called from size_dynamic_sections. |
1274 | It needs to decide whether .eh_frame_hdr should be output or not, | |
8423293d AM |
1275 | because when the dynamic symbol table has been sized it is too late |
1276 | to strip sections. */ | |
68f69152 | 1277 | |
b34976b6 | 1278 | bfd_boolean |
c39a58e6 | 1279 | _bfd_elf_maybe_strip_eh_frame_hdr (struct bfd_link_info *info) |
68f69152 | 1280 | { |
126495ed | 1281 | struct elf_link_hash_table *htab; |
68f69152 JJ |
1282 | struct eh_frame_hdr_info *hdr_info; |
1283 | ||
126495ed AM |
1284 | htab = elf_hash_table (info); |
1285 | hdr_info = &htab->eh_info; | |
1286 | if (hdr_info->hdr_sec == NULL) | |
b34976b6 | 1287 | return TRUE; |
68f69152 | 1288 | |
9a2a56cc AM |
1289 | if (bfd_is_abs_section (hdr_info->hdr_sec->output_section) |
1290 | || !info->eh_frame_hdr | |
1291 | || !_bfd_elf_eh_frame_present (info)) | |
68f69152 | 1292 | { |
8423293d | 1293 | hdr_info->hdr_sec->flags |= SEC_EXCLUDE; |
126495ed | 1294 | hdr_info->hdr_sec = NULL; |
b34976b6 | 1295 | return TRUE; |
68f69152 | 1296 | } |
126495ed | 1297 | |
b34976b6 AM |
1298 | hdr_info->table = TRUE; |
1299 | return TRUE; | |
68f69152 JJ |
1300 | } |
1301 | ||
65765700 JJ |
1302 | /* Adjust an address in the .eh_frame section. Given OFFSET within |
1303 | SEC, this returns the new offset in the adjusted .eh_frame section, | |
1304 | or -1 if the address refers to a CIE/FDE which has been removed | |
1305 | or to offset with dynamic relocation which is no longer needed. */ | |
1306 | ||
1307 | bfd_vma | |
c39a58e6 | 1308 | _bfd_elf_eh_frame_section_offset (bfd *output_bfd ATTRIBUTE_UNUSED, |
3d540e93 | 1309 | struct bfd_link_info *info ATTRIBUTE_UNUSED, |
c39a58e6 AM |
1310 | asection *sec, |
1311 | bfd_vma offset) | |
65765700 JJ |
1312 | { |
1313 | struct eh_frame_sec_info *sec_info; | |
1314 | unsigned int lo, hi, mid; | |
1315 | ||
dbaa2011 | 1316 | if (sec->sec_info_type != SEC_INFO_TYPE_EH_FRAME) |
65765700 | 1317 | return offset; |
a50b1753 | 1318 | sec_info = (struct eh_frame_sec_info *) elf_section_data (sec)->sec_info; |
65765700 | 1319 | |
eea6121a AM |
1320 | if (offset >= sec->rawsize) |
1321 | return offset - sec->rawsize + sec->size; | |
65765700 JJ |
1322 | |
1323 | lo = 0; | |
1324 | hi = sec_info->count; | |
1325 | mid = 0; | |
1326 | while (lo < hi) | |
1327 | { | |
1328 | mid = (lo + hi) / 2; | |
1329 | if (offset < sec_info->entry[mid].offset) | |
1330 | hi = mid; | |
1331 | else if (offset | |
1332 | >= sec_info->entry[mid].offset + sec_info->entry[mid].size) | |
1333 | lo = mid + 1; | |
1334 | else | |
1335 | break; | |
1336 | } | |
1337 | ||
1338 | BFD_ASSERT (lo < hi); | |
1339 | ||
1340 | /* FDE or CIE was removed. */ | |
1341 | if (sec_info->entry[mid].removed) | |
1342 | return (bfd_vma) -1; | |
1343 | ||
18e04883 RS |
1344 | /* If converting personality pointers to DW_EH_PE_pcrel, there will be |
1345 | no need for run-time relocation against the personality field. */ | |
1346 | if (sec_info->entry[mid].cie | |
1347 | && sec_info->entry[mid].u.cie.make_per_encoding_relative | |
1348 | && offset == (sec_info->entry[mid].offset + 8 | |
1349 | + sec_info->entry[mid].u.cie.personality_offset)) | |
1350 | return (bfd_vma) -2; | |
1351 | ||
65765700 JJ |
1352 | /* If converting to DW_EH_PE_pcrel, there will be no need for run-time |
1353 | relocation against FDE's initial_location field. */ | |
fda3ecf2 | 1354 | if (!sec_info->entry[mid].cie |
6b2cc140 | 1355 | && sec_info->entry[mid].make_relative |
353057a5 RS |
1356 | && offset == sec_info->entry[mid].offset + 8) |
1357 | return (bfd_vma) -2; | |
65765700 | 1358 | |
9e2a4898 JJ |
1359 | /* If converting LSDA pointers to DW_EH_PE_pcrel, there will be no need |
1360 | for run-time relocation against LSDA field. */ | |
fda3ecf2 | 1361 | if (!sec_info->entry[mid].cie |
9f4b847e RS |
1362 | && sec_info->entry[mid].u.fde.cie_inf->u.cie.make_lsda_relative |
1363 | && offset == (sec_info->entry[mid].offset + 8 | |
1364 | + sec_info->entry[mid].lsda_offset)) | |
1365 | return (bfd_vma) -2; | |
9e2a4898 | 1366 | |
ac685e6a JJ |
1367 | /* If converting to DW_EH_PE_pcrel, there will be no need for run-time |
1368 | relocation against DW_CFA_set_loc's arguments. */ | |
1369 | if (sec_info->entry[mid].set_loc | |
6b2cc140 | 1370 | && sec_info->entry[mid].make_relative |
ac685e6a JJ |
1371 | && (offset >= sec_info->entry[mid].offset + 8 |
1372 | + sec_info->entry[mid].set_loc[1])) | |
1373 | { | |
1374 | unsigned int cnt; | |
1375 | ||
1376 | for (cnt = 1; cnt <= sec_info->entry[mid].set_loc[0]; cnt++) | |
1377 | if (offset == sec_info->entry[mid].offset + 8 | |
1378 | + sec_info->entry[mid].set_loc[cnt]) | |
1379 | return (bfd_vma) -2; | |
1380 | } | |
1381 | ||
353057a5 | 1382 | /* Any new augmentation bytes go before the first relocation. */ |
c68836a9 | 1383 | return (offset + sec_info->entry[mid].new_offset |
353057a5 RS |
1384 | - sec_info->entry[mid].offset |
1385 | + extra_augmentation_string_bytes (sec_info->entry + mid) | |
1386 | + extra_augmentation_data_bytes (sec_info->entry + mid)); | |
65765700 JJ |
1387 | } |
1388 | ||
1389 | /* Write out .eh_frame section. This is called with the relocated | |
1390 | contents. */ | |
1391 | ||
b34976b6 | 1392 | bfd_boolean |
c39a58e6 AM |
1393 | _bfd_elf_write_section_eh_frame (bfd *abfd, |
1394 | struct bfd_link_info *info, | |
1395 | asection *sec, | |
1396 | bfd_byte *contents) | |
65765700 JJ |
1397 | { |
1398 | struct eh_frame_sec_info *sec_info; | |
126495ed | 1399 | struct elf_link_hash_table *htab; |
65765700 | 1400 | struct eh_frame_hdr_info *hdr_info; |
65765700 | 1401 | unsigned int ptr_size; |
fda3ecf2 | 1402 | struct eh_cie_fde *ent; |
65765700 | 1403 | |
dbaa2011 | 1404 | if (sec->sec_info_type != SEC_INFO_TYPE_EH_FRAME) |
5dabe785 | 1405 | /* FIXME: octets_per_byte. */ |
c39a58e6 | 1406 | return bfd_set_section_contents (abfd, sec->output_section, contents, |
eea6121a | 1407 | sec->output_offset, sec->size); |
8c946ed5 RS |
1408 | |
1409 | ptr_size = (get_elf_backend_data (abfd) | |
1410 | ->elf_backend_eh_frame_address_size (abfd, sec)); | |
1411 | BFD_ASSERT (ptr_size != 0); | |
1412 | ||
a50b1753 | 1413 | sec_info = (struct eh_frame_sec_info *) elf_section_data (sec)->sec_info; |
126495ed AM |
1414 | htab = elf_hash_table (info); |
1415 | hdr_info = &htab->eh_info; | |
3472e2e9 | 1416 | |
126495ed | 1417 | if (hdr_info->table && hdr_info->array == NULL) |
a50b1753 NC |
1418 | hdr_info->array = (struct eh_frame_array_ent *) |
1419 | bfd_malloc (hdr_info->fde_count * sizeof(*hdr_info->array)); | |
126495ed AM |
1420 | if (hdr_info->array == NULL) |
1421 | hdr_info = NULL; | |
65765700 | 1422 | |
353057a5 RS |
1423 | /* The new offsets can be bigger or smaller than the original offsets. |
1424 | We therefore need to make two passes over the section: one backward | |
1425 | pass to move entries up and one forward pass to move entries down. | |
1426 | The two passes won't interfere with each other because entries are | |
1427 | not reordered */ | |
1428 | for (ent = sec_info->entry + sec_info->count; ent-- != sec_info->entry;) | |
1429 | if (!ent->removed && ent->new_offset > ent->offset) | |
fc802241 | 1430 | memmove (contents + ent->new_offset, contents + ent->offset, ent->size); |
353057a5 RS |
1431 | |
1432 | for (ent = sec_info->entry; ent < sec_info->entry + sec_info->count; ++ent) | |
1433 | if (!ent->removed && ent->new_offset < ent->offset) | |
fc802241 | 1434 | memmove (contents + ent->new_offset, contents + ent->offset, ent->size); |
353057a5 | 1435 | |
fda3ecf2 | 1436 | for (ent = sec_info->entry; ent < sec_info->entry + sec_info->count; ++ent) |
65765700 | 1437 | { |
353057a5 RS |
1438 | unsigned char *buf, *end; |
1439 | unsigned int new_size; | |
1440 | ||
fda3ecf2 AM |
1441 | if (ent->removed) |
1442 | continue; | |
1443 | ||
353057a5 RS |
1444 | if (ent->size == 4) |
1445 | { | |
1446 | /* Any terminating FDE must be at the end of the section. */ | |
1447 | BFD_ASSERT (ent == sec_info->entry + sec_info->count - 1); | |
1448 | continue; | |
1449 | } | |
1450 | ||
fc802241 | 1451 | buf = contents + ent->new_offset; |
353057a5 RS |
1452 | end = buf + ent->size; |
1453 | new_size = size_of_output_cie_fde (ent, ptr_size); | |
1454 | ||
a34a056a L |
1455 | /* Update the size. It may be shrinked. */ |
1456 | bfd_put_32 (abfd, new_size - 4, buf); | |
1457 | ||
1458 | /* Filling the extra bytes with DW_CFA_nops. */ | |
353057a5 | 1459 | if (new_size != ent->size) |
a34a056a | 1460 | memset (end, 0, new_size - ent->size); |
353057a5 | 1461 | |
fda3ecf2 | 1462 | if (ent->cie) |
65765700 JJ |
1463 | { |
1464 | /* CIE */ | |
353057a5 | 1465 | if (ent->make_relative |
9f4b847e | 1466 | || ent->u.cie.make_lsda_relative |
6b2cc140 | 1467 | || ent->u.cie.per_encoding_relative) |
65765700 | 1468 | { |
f075ee0c | 1469 | char *aug; |
353057a5 | 1470 | unsigned int action, extra_string, extra_data; |
2c42be65 | 1471 | unsigned int per_width, per_encoding; |
65765700 | 1472 | |
9e2a4898 | 1473 | /* Need to find 'R' or 'L' augmentation's argument and modify |
65765700 | 1474 | DW_EH_PE_* value. */ |
353057a5 | 1475 | action = ((ent->make_relative ? 1 : 0) |
9f4b847e | 1476 | | (ent->u.cie.make_lsda_relative ? 2 : 0) |
6b2cc140 | 1477 | | (ent->u.cie.per_encoding_relative ? 4 : 0)); |
353057a5 RS |
1478 | extra_string = extra_augmentation_string_bytes (ent); |
1479 | extra_data = extra_augmentation_data_bytes (ent); | |
1480 | ||
65765700 JJ |
1481 | /* Skip length, id and version. */ |
1482 | buf += 9; | |
f075ee0c AM |
1483 | aug = (char *) buf; |
1484 | buf += strlen (aug) + 1; | |
2c42be65 RS |
1485 | skip_leb128 (&buf, end); |
1486 | skip_leb128 (&buf, end); | |
1487 | skip_leb128 (&buf, end); | |
65765700 JJ |
1488 | if (*aug == 'z') |
1489 | { | |
353057a5 RS |
1490 | /* The uleb128 will always be a single byte for the kind |
1491 | of augmentation strings that we're prepared to handle. */ | |
1492 | *buf++ += extra_data; | |
65765700 JJ |
1493 | aug++; |
1494 | } | |
1495 | ||
353057a5 RS |
1496 | /* Make room for the new augmentation string and data bytes. */ |
1497 | memmove (buf + extra_string + extra_data, buf, end - buf); | |
f075ee0c | 1498 | memmove (aug + extra_string, aug, buf - (bfd_byte *) aug); |
353057a5 | 1499 | buf += extra_string; |
2c42be65 | 1500 | end += extra_string + extra_data; |
353057a5 RS |
1501 | |
1502 | if (ent->add_augmentation_size) | |
1503 | { | |
1504 | *aug++ = 'z'; | |
1505 | *buf++ = extra_data - 1; | |
1506 | } | |
6b2cc140 | 1507 | if (ent->u.cie.add_fde_encoding) |
353057a5 RS |
1508 | { |
1509 | BFD_ASSERT (action & 1); | |
1510 | *aug++ = 'R'; | |
30af5962 | 1511 | *buf++ = make_pc_relative (DW_EH_PE_absptr, ptr_size); |
353057a5 RS |
1512 | action &= ~1; |
1513 | } | |
1514 | ||
9e2a4898 | 1515 | while (action) |
65765700 JJ |
1516 | switch (*aug++) |
1517 | { | |
1518 | case 'L': | |
9e2a4898 JJ |
1519 | if (action & 2) |
1520 | { | |
fda3ecf2 | 1521 | BFD_ASSERT (*buf == ent->lsda_encoding); |
30af5962 | 1522 | *buf = make_pc_relative (*buf, ptr_size); |
9e2a4898 JJ |
1523 | action &= ~2; |
1524 | } | |
65765700 JJ |
1525 | buf++; |
1526 | break; | |
1527 | case 'P': | |
18e04883 | 1528 | if (ent->u.cie.make_per_encoding_relative) |
a10917ef | 1529 | *buf = make_pc_relative (*buf, ptr_size); |
65765700 | 1530 | per_encoding = *buf++; |
3472e2e9 | 1531 | per_width = get_DW_EH_PE_width (per_encoding, ptr_size); |
65765700 | 1532 | BFD_ASSERT (per_width != 0); |
09ae86c2 | 1533 | BFD_ASSERT (((per_encoding & 0x70) == DW_EH_PE_pcrel) |
6b2cc140 | 1534 | == ent->u.cie.per_encoding_relative); |
18e04883 | 1535 | if ((per_encoding & 0x70) == DW_EH_PE_aligned) |
65765700 JJ |
1536 | buf = (contents |
1537 | + ((buf - contents + per_width - 1) | |
1538 | & ~((bfd_size_type) per_width - 1))); | |
09ae86c2 JJ |
1539 | if (action & 4) |
1540 | { | |
fda3ecf2 AM |
1541 | bfd_vma val; |
1542 | ||
1543 | val = read_value (abfd, buf, per_width, | |
1544 | get_DW_EH_PE_signed (per_encoding)); | |
18e04883 RS |
1545 | if (ent->u.cie.make_per_encoding_relative) |
1546 | val -= (sec->output_section->vma | |
1547 | + sec->output_offset | |
1548 | + (buf - contents)); | |
1549 | else | |
1550 | { | |
1551 | val += (bfd_vma) ent->offset - ent->new_offset; | |
1552 | val -= extra_string + extra_data; | |
1553 | } | |
fda3ecf2 | 1554 | write_value (abfd, buf, val, per_width); |
09ae86c2 JJ |
1555 | action &= ~4; |
1556 | } | |
65765700 JJ |
1557 | buf += per_width; |
1558 | break; | |
9e2a4898 JJ |
1559 | case 'R': |
1560 | if (action & 1) | |
1561 | { | |
fda3ecf2 | 1562 | BFD_ASSERT (*buf == ent->fde_encoding); |
30af5962 | 1563 | *buf = make_pc_relative (*buf, ptr_size); |
9e2a4898 JJ |
1564 | action &= ~1; |
1565 | } | |
1566 | buf++; | |
1567 | break; | |
63752a75 JJ |
1568 | case 'S': |
1569 | break; | |
65765700 JJ |
1570 | default: |
1571 | BFD_FAIL (); | |
1572 | } | |
65765700 JJ |
1573 | } |
1574 | } | |
353057a5 | 1575 | else |
65765700 JJ |
1576 | { |
1577 | /* FDE */ | |
fda3ecf2 | 1578 | bfd_vma value, address; |
9e2a4898 | 1579 | unsigned int width; |
ac685e6a | 1580 | bfd_byte *start; |
155eaaa0 | 1581 | struct eh_cie_fde *cie; |
65765700 | 1582 | |
b34976b6 | 1583 | /* Skip length. */ |
155eaaa0 | 1584 | cie = ent->u.fde.cie_inf; |
65765700 | 1585 | buf += 4; |
fc802241 RS |
1586 | value = ((ent->new_offset + sec->output_offset + 4) |
1587 | - (cie->new_offset + cie->u.cie.u.sec->output_offset)); | |
fda3ecf2 | 1588 | bfd_put_32 (abfd, value, buf); |
65765700 | 1589 | buf += 4; |
fda3ecf2 AM |
1590 | width = get_DW_EH_PE_width (ent->fde_encoding, ptr_size); |
1591 | value = read_value (abfd, buf, width, | |
1592 | get_DW_EH_PE_signed (ent->fde_encoding)); | |
1593 | address = value; | |
9e2a4898 | 1594 | if (value) |
65765700 | 1595 | { |
18e04883 | 1596 | switch (ent->fde_encoding & 0x70) |
9e2a4898 | 1597 | { |
9e2a4898 JJ |
1598 | case DW_EH_PE_textrel: |
1599 | BFD_ASSERT (hdr_info == NULL); | |
1600 | break; | |
1601 | case DW_EH_PE_datarel: | |
1602 | { | |
cd9e734e AM |
1603 | switch (abfd->arch_info->arch) |
1604 | { | |
1605 | case bfd_arch_ia64: | |
1606 | BFD_ASSERT (elf_gp (abfd) != 0); | |
1607 | address += elf_gp (abfd); | |
1608 | break; | |
1609 | default: | |
1610 | (*info->callbacks->einfo) | |
1611 | (_("%P: DW_EH_PE_datarel unspecified" | |
1612 | " for this architecture.\n")); | |
1613 | /* Fall thru */ | |
1614 | case bfd_arch_frv: | |
1615 | case bfd_arch_i386: | |
1616 | BFD_ASSERT (htab->hgot != NULL | |
1617 | && ((htab->hgot->root.type | |
1618 | == bfd_link_hash_defined) | |
1619 | || (htab->hgot->root.type | |
1620 | == bfd_link_hash_defweak))); | |
1621 | address | |
1622 | += (htab->hgot->root.u.def.value | |
1623 | + htab->hgot->root.u.def.section->output_offset | |
1624 | + (htab->hgot->root.u.def.section->output_section | |
1625 | ->vma)); | |
1626 | break; | |
1627 | } | |
9e2a4898 JJ |
1628 | } |
1629 | break; | |
1630 | case DW_EH_PE_pcrel: | |
9c47c4c1 | 1631 | value += (bfd_vma) ent->offset - ent->new_offset; |
fc802241 RS |
1632 | address += (sec->output_section->vma |
1633 | + sec->output_offset | |
1634 | + ent->offset + 8); | |
9e2a4898 JJ |
1635 | break; |
1636 | } | |
6b2cc140 | 1637 | if (ent->make_relative) |
fc802241 RS |
1638 | value -= (sec->output_section->vma |
1639 | + sec->output_offset | |
1640 | + ent->new_offset + 8); | |
9e2a4898 | 1641 | write_value (abfd, buf, value, width); |
65765700 JJ |
1642 | } |
1643 | ||
ac685e6a JJ |
1644 | start = buf; |
1645 | ||
65765700 JJ |
1646 | if (hdr_info) |
1647 | { | |
cd9e734e AM |
1648 | /* The address calculation may overflow, giving us a |
1649 | value greater than 4G on a 32-bit target when | |
1650 | dwarf_vma is 64-bit. */ | |
1651 | if (sizeof (address) > 4 && ptr_size == 4) | |
1652 | address &= 0xffffffff; | |
65765700 JJ |
1653 | hdr_info->array[hdr_info->array_count].initial_loc = address; |
1654 | hdr_info->array[hdr_info->array_count++].fde | |
fc802241 RS |
1655 | = (sec->output_section->vma |
1656 | + sec->output_offset | |
1657 | + ent->new_offset); | |
65765700 | 1658 | } |
9e2a4898 | 1659 | |
18e04883 | 1660 | if ((ent->lsda_encoding & 0x70) == DW_EH_PE_pcrel |
9f4b847e | 1661 | || cie->u.cie.make_lsda_relative) |
9e2a4898 | 1662 | { |
fda3ecf2 AM |
1663 | buf += ent->lsda_offset; |
1664 | width = get_DW_EH_PE_width (ent->lsda_encoding, ptr_size); | |
84f97cb6 | 1665 | value = read_value (abfd, buf, width, |
fda3ecf2 | 1666 | get_DW_EH_PE_signed (ent->lsda_encoding)); |
9e2a4898 JJ |
1667 | if (value) |
1668 | { | |
18e04883 | 1669 | if ((ent->lsda_encoding & 0x70) == DW_EH_PE_pcrel) |
9c47c4c1 | 1670 | value += (bfd_vma) ent->offset - ent->new_offset; |
9f4b847e | 1671 | else if (cie->u.cie.make_lsda_relative) |
fc802241 RS |
1672 | value -= (sec->output_section->vma |
1673 | + sec->output_offset | |
1674 | + ent->new_offset + 8 + ent->lsda_offset); | |
9e2a4898 JJ |
1675 | write_value (abfd, buf, value, width); |
1676 | } | |
1677 | } | |
6b2cc140 | 1678 | else if (ent->add_augmentation_size) |
353057a5 RS |
1679 | { |
1680 | /* Skip the PC and length and insert a zero byte for the | |
1681 | augmentation size. */ | |
1682 | buf += width * 2; | |
1683 | memmove (buf + 1, buf, end - buf); | |
1684 | *buf = 0; | |
1685 | } | |
ac685e6a JJ |
1686 | |
1687 | if (ent->set_loc) | |
1688 | { | |
1689 | /* Adjust DW_CFA_set_loc. */ | |
91d6fa6a | 1690 | unsigned int cnt; |
ac685e6a JJ |
1691 | bfd_vma new_offset; |
1692 | ||
1693 | width = get_DW_EH_PE_width (ent->fde_encoding, ptr_size); | |
1694 | new_offset = ent->new_offset + 8 | |
1695 | + extra_augmentation_string_bytes (ent) | |
1696 | + extra_augmentation_data_bytes (ent); | |
1697 | ||
1698 | for (cnt = 1; cnt <= ent->set_loc[0]; cnt++) | |
1699 | { | |
ac685e6a JJ |
1700 | buf = start + ent->set_loc[cnt]; |
1701 | ||
1702 | value = read_value (abfd, buf, width, | |
1703 | get_DW_EH_PE_signed (ent->fde_encoding)); | |
1704 | if (!value) | |
1705 | continue; | |
1706 | ||
18e04883 | 1707 | if ((ent->fde_encoding & 0x70) == DW_EH_PE_pcrel) |
9c47c4c1 | 1708 | value += (bfd_vma) ent->offset + 8 - new_offset; |
6b2cc140 | 1709 | if (ent->make_relative) |
fc802241 RS |
1710 | value -= (sec->output_section->vma |
1711 | + sec->output_offset | |
1712 | + new_offset + ent->set_loc[cnt]); | |
ac685e6a JJ |
1713 | write_value (abfd, buf, value, width); |
1714 | } | |
1715 | } | |
65765700 | 1716 | } |
65765700 JJ |
1717 | } |
1718 | ||
a34a056a L |
1719 | /* We don't align the section to its section alignment since the |
1720 | runtime library only expects all CIE/FDE records aligned at | |
4e591bc1 | 1721 | the pointer size. _bfd_elf_discard_section_eh_frame should |
a34a056a L |
1722 | have padded CIE/FDE records to multiple of pointer size with |
1723 | size_of_output_cie_fde. */ | |
1724 | if ((sec->size % ptr_size) != 0) | |
1725 | abort (); | |
a5eb27e6 | 1726 | |
5dabe785 | 1727 | /* FIXME: octets_per_byte. */ |
65765700 | 1728 | return bfd_set_section_contents (abfd, sec->output_section, |
3472e2e9 AM |
1729 | contents, (file_ptr) sec->output_offset, |
1730 | sec->size); | |
65765700 JJ |
1731 | } |
1732 | ||
1733 | /* Helper function used to sort .eh_frame_hdr search table by increasing | |
1734 | VMA of FDE initial location. */ | |
1735 | ||
1736 | static int | |
c39a58e6 | 1737 | vma_compare (const void *a, const void *b) |
65765700 | 1738 | { |
a50b1753 NC |
1739 | const struct eh_frame_array_ent *p = (const struct eh_frame_array_ent *) a; |
1740 | const struct eh_frame_array_ent *q = (const struct eh_frame_array_ent *) b; | |
65765700 JJ |
1741 | if (p->initial_loc > q->initial_loc) |
1742 | return 1; | |
1743 | if (p->initial_loc < q->initial_loc) | |
1744 | return -1; | |
1745 | return 0; | |
1746 | } | |
1747 | ||
1748 | /* Write out .eh_frame_hdr section. This must be called after | |
1749 | _bfd_elf_write_section_eh_frame has been called on all input | |
1750 | .eh_frame sections. | |
1751 | .eh_frame_hdr format: | |
1752 | ubyte version (currently 1) | |
1753 | ubyte eh_frame_ptr_enc (DW_EH_PE_* encoding of pointer to start of | |
1754 | .eh_frame section) | |
1755 | ubyte fde_count_enc (DW_EH_PE_* encoding of total FDE count | |
1756 | number (or DW_EH_PE_omit if there is no | |
1757 | binary search table computed)) | |
1758 | ubyte table_enc (DW_EH_PE_* encoding of binary search table, | |
1759 | or DW_EH_PE_omit if not present. | |
1760 | DW_EH_PE_datarel is using address of | |
1761 | .eh_frame_hdr section start as base) | |
1762 | [encoded] eh_frame_ptr (pointer to start of .eh_frame section) | |
1763 | optionally followed by: | |
1764 | [encoded] fde_count (total number of FDEs in .eh_frame section) | |
1765 | fde_count x [encoded] initial_loc, fde | |
1766 | (array of encoded pairs containing | |
1767 | FDE initial_location field and FDE address, | |
5ed6aba4 | 1768 | sorted by increasing initial_loc). */ |
65765700 | 1769 | |
b34976b6 | 1770 | bfd_boolean |
c39a58e6 | 1771 | _bfd_elf_write_section_eh_frame_hdr (bfd *abfd, struct bfd_link_info *info) |
65765700 | 1772 | { |
126495ed | 1773 | struct elf_link_hash_table *htab; |
65765700 | 1774 | struct eh_frame_hdr_info *hdr_info; |
126495ed | 1775 | asection *sec; |
9f7c3e5e | 1776 | bfd_boolean retval = TRUE; |
65765700 | 1777 | |
126495ed AM |
1778 | htab = elf_hash_table (info); |
1779 | hdr_info = &htab->eh_info; | |
1780 | sec = hdr_info->hdr_sec; | |
65765700 | 1781 | |
9f7c3e5e | 1782 | if (info->eh_frame_hdr && sec != NULL) |
5ed6aba4 | 1783 | { |
9f7c3e5e AM |
1784 | bfd_byte *contents; |
1785 | asection *eh_frame_sec; | |
1786 | bfd_size_type size; | |
1787 | bfd_vma encoded_eh_frame; | |
1788 | ||
1789 | size = EH_FRAME_HDR_SIZE; | |
1790 | if (hdr_info->array && hdr_info->array_count == hdr_info->fde_count) | |
1791 | size += 4 + hdr_info->fde_count * 8; | |
1792 | contents = (bfd_byte *) bfd_malloc (size); | |
1793 | if (contents == NULL) | |
1794 | return FALSE; | |
65765700 | 1795 | |
9f7c3e5e AM |
1796 | eh_frame_sec = bfd_get_section_by_name (abfd, ".eh_frame"); |
1797 | if (eh_frame_sec == NULL) | |
1798 | { | |
1799 | free (contents); | |
1800 | return FALSE; | |
1801 | } | |
ec3391e7 | 1802 | |
9f7c3e5e AM |
1803 | memset (contents, 0, EH_FRAME_HDR_SIZE); |
1804 | /* Version. */ | |
1805 | contents[0] = 1; | |
1806 | /* .eh_frame offset. */ | |
1807 | contents[1] = get_elf_backend_data (abfd)->elf_backend_encode_eh_address | |
1808 | (abfd, info, eh_frame_sec, 0, sec, 4, &encoded_eh_frame); | |
ec3391e7 | 1809 | |
9f7c3e5e AM |
1810 | if (hdr_info->array && hdr_info->array_count == hdr_info->fde_count) |
1811 | { | |
1812 | /* FDE count encoding. */ | |
1813 | contents[2] = DW_EH_PE_udata4; | |
1814 | /* Search table encoding. */ | |
1815 | contents[3] = DW_EH_PE_datarel | DW_EH_PE_sdata4; | |
1816 | } | |
1817 | else | |
1818 | { | |
1819 | contents[2] = DW_EH_PE_omit; | |
1820 | contents[3] = DW_EH_PE_omit; | |
1821 | } | |
1822 | bfd_put_32 (abfd, encoded_eh_frame, contents + 4); | |
65765700 | 1823 | |
9f7c3e5e | 1824 | if (contents[2] != DW_EH_PE_omit) |
65765700 | 1825 | { |
9f7c3e5e AM |
1826 | unsigned int i; |
1827 | ||
1828 | bfd_put_32 (abfd, hdr_info->fde_count, contents + EH_FRAME_HDR_SIZE); | |
1829 | qsort (hdr_info->array, hdr_info->fde_count, | |
1830 | sizeof (*hdr_info->array), vma_compare); | |
1831 | for (i = 0; i < hdr_info->fde_count; i++) | |
1832 | { | |
1833 | bfd_put_32 (abfd, | |
1834 | hdr_info->array[i].initial_loc | |
1835 | - sec->output_section->vma, | |
1836 | contents + EH_FRAME_HDR_SIZE + i * 8 + 4); | |
1837 | bfd_put_32 (abfd, | |
1838 | hdr_info->array[i].fde - sec->output_section->vma, | |
1839 | contents + EH_FRAME_HDR_SIZE + i * 8 + 8); | |
1840 | } | |
65765700 | 1841 | } |
65765700 | 1842 | |
9f7c3e5e AM |
1843 | /* FIXME: octets_per_byte. */ |
1844 | retval = bfd_set_section_contents (abfd, sec->output_section, contents, | |
1845 | (file_ptr) sec->output_offset, | |
1846 | sec->size); | |
1847 | free (contents); | |
1848 | } | |
1849 | if (hdr_info->array != NULL) | |
1850 | free (hdr_info->array); | |
5ed6aba4 | 1851 | return retval; |
65765700 | 1852 | } |
ec3391e7 | 1853 | |
8c946ed5 RS |
1854 | /* Return the width of FDE addresses. This is the default implementation. */ |
1855 | ||
1856 | unsigned int | |
1857 | _bfd_elf_eh_frame_address_size (bfd *abfd, asection *sec ATTRIBUTE_UNUSED) | |
1858 | { | |
1859 | return elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64 ? 8 : 4; | |
1860 | } | |
1861 | ||
ec3391e7 AO |
1862 | /* Decide whether we can use a PC-relative encoding within the given |
1863 | EH frame section. This is the default implementation. */ | |
1864 | ||
1865 | bfd_boolean | |
1866 | _bfd_elf_can_make_relative (bfd *input_bfd ATTRIBUTE_UNUSED, | |
1867 | struct bfd_link_info *info ATTRIBUTE_UNUSED, | |
1868 | asection *eh_frame_section ATTRIBUTE_UNUSED) | |
1869 | { | |
1870 | return TRUE; | |
1871 | } | |
1872 | ||
1873 | /* Select an encoding for the given address. Preference is given to | |
1874 | PC-relative addressing modes. */ | |
1875 | ||
1876 | bfd_byte | |
1877 | _bfd_elf_encode_eh_address (bfd *abfd ATTRIBUTE_UNUSED, | |
1878 | struct bfd_link_info *info ATTRIBUTE_UNUSED, | |
1879 | asection *osec, bfd_vma offset, | |
1880 | asection *loc_sec, bfd_vma loc_offset, | |
1881 | bfd_vma *encoded) | |
1882 | { | |
1883 | *encoded = osec->vma + offset - | |
1884 | (loc_sec->output_section->vma + loc_sec->output_offset + loc_offset); | |
1885 | return DW_EH_PE_pcrel | DW_EH_PE_sdata4; | |
1886 | } |