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