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