2 Copyright (C) 2009-2010 Red Hat, Inc.
3 This file is part of elfutils.
5 This file is free software; you can redistribute it and/or modify
6 it under the terms of either
8 * the GNU Lesser General Public License as published by the Free
9 Software Foundation; either version 3 of the License, or (at
10 your option) any later version
14 * the GNU General Public License as published by the Free
15 Software Foundation; either version 2 of the License, or (at
16 your option) any later version
18 or both in parallel, as here.
20 elfutils is distributed in the hope that it will be useful, but
21 WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 General Public License for more details.
25 You should have received copies of the GNU General Public License and
26 the GNU Lesser General Public License along with this program. If
27 not, see <http://www.gnu.org/licenses/>. */
34 #include "encoded-value.h"
41 compare_cie (const void *a, const void *b)
43 const struct dwarf_cie *cie1 = a;
44 const struct dwarf_cie *cie2 = b;
45 if (cie1->offset < cie2->offset)
47 if (cie1->offset > cie2->offset)
52 /* There is no CIE at OFFSET in the tree. Add it. */
53 static struct dwarf_cie *
54 intern_new_cie (Dwarf_CFI *cache, Dwarf_Off offset, const Dwarf_CIE *info)
56 struct dwarf_cie *cie = malloc (sizeof (struct dwarf_cie));
59 __libdw_seterrno (DWARF_E_NOMEM);
64 cie->code_alignment_factor = info->code_alignment_factor;
65 cie->data_alignment_factor = info->data_alignment_factor;
66 cie->return_address_register = info->return_address_register;
68 cie->fde_augmentation_data_size = 0;
69 cie->sized_augmentation_data = false;
70 cie->signal_frame = false;
72 cie->fde_encoding = DW_EH_PE_absptr;
73 cie->lsda_encoding = DW_EH_PE_omit;
75 /* Grok the augmentation string and its data. */
76 const uint8_t *data = info->augmentation_data;
77 for (const char *ap = info->augmentation; *ap != '\0'; ++ap)
83 cie->sized_augmentation_data = true;
87 cie->signal_frame = true;
90 case 'L': /* LSDA pointer encoding byte. */
91 cie->lsda_encoding = *data++;
92 if (!cie->sized_augmentation_data)
93 cie->fde_augmentation_data_size
94 += encoded_value_size (&cache->data->d, cache->e_ident,
95 cie->lsda_encoding, NULL);
98 case 'R': /* FDE address encoding byte. */
99 cie->fde_encoding = *data++;
102 case 'P': /* Skip personality routine. */
104 data += encoded_value_size (&cache->data->d, cache->e_ident,
109 /* Unknown augmentation string. If we have 'z' we can ignore it,
110 otherwise we must bail out. */
111 if (cie->sized_augmentation_data)
114 /* We only get here when we need to bail out. */
118 if ((cie->fde_encoding & 0x0f) == DW_EH_PE_absptr)
120 /* Canonicalize encoding to a specific size. */
121 assert (DW_EH_PE_absptr == 0);
123 /* XXX should get from dwarf_next_cfi with v4 header. */
124 uint_fast8_t address_size
125 = cache->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
126 switch (address_size)
129 cie->fde_encoding |= DW_EH_PE_udata8;
132 cie->fde_encoding |= DW_EH_PE_udata4;
136 __libdw_seterrno (DWARF_E_INVALID_DWARF);
141 /* Save the initial instructions to be played out into initial state. */
142 cie->initial_instructions = info->initial_instructions;
143 cie->initial_instructions_end = info->initial_instructions_end;
144 cie->initial_state = NULL;
146 /* Add the new entry to the search tree. */
147 if (tsearch (cie, &cache->cie_tree, &compare_cie) == NULL)
150 __libdw_seterrno (DWARF_E_NOMEM);
157 /* Look up a CIE_pointer for random access. */
160 __libdw_find_cie (Dwarf_CFI *cache, Dwarf_Off offset)
162 const struct dwarf_cie cie_key = { .offset = offset };
163 struct dwarf_cie **found = tfind (&cie_key, &cache->cie_tree, &compare_cie);
167 /* We have not read this CIE yet. Go find it. */
168 Dwarf_Off next_offset = offset;
169 Dwarf_CFI_Entry entry;
170 int result = INTUSE(dwarf_next_cfi) (cache->e_ident,
171 &cache->data->d, CFI_IS_EH (cache),
172 offset, &next_offset, &entry);
173 if (result != 0 || entry.cie.CIE_id != DW_CIE_ID_64)
175 __libdw_seterrno (DWARF_E_INVALID_DWARF);
179 /* If this happened to be what we would have read next, notice it. */
180 if (cache->next_offset == offset)
181 cache->next_offset = next_offset;
183 return intern_new_cie (cache, offset, &entry.cie);
186 /* Enter a CIE encountered while reading through for FDEs. */
189 __libdw_intern_cie (Dwarf_CFI *cache, Dwarf_Off offset, const Dwarf_CIE *info)
191 const struct dwarf_cie cie_key = { .offset = offset };
192 struct dwarf_cie **found = tfind (&cie_key, &cache->cie_tree, &compare_cie);
194 /* We have not read this CIE yet. Enter it. */
195 (void) intern_new_cie (cache, offset, info);