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