2 * Copyright (c) 2009,2010 Kai Wang
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include "_libdwarf.h"
29 ELFTC_VCSID("$Id: libdwarf_nametbl.c 2070 2011-10-27 03:05:32Z jkoshy $");
32 _dwarf_nametbl_cleanup(Dwarf_NameSec *nsp)
35 Dwarf_NameTbl nt, tnt;
36 Dwarf_NamePair np, tnp;
39 if ((ns = *nsp) == NULL)
42 STAILQ_FOREACH_SAFE(nt, &ns->ns_ntlist, nt_next, tnt) {
43 STAILQ_FOREACH_SAFE(np, &nt->nt_nplist, np_next, tnp) {
44 STAILQ_REMOVE(&nt->nt_nplist, np, _Dwarf_NamePair,
48 STAILQ_REMOVE(&ns->ns_ntlist, nt, _Dwarf_NameTbl, nt_next);
58 _dwarf_nametbl_init(Dwarf_Debug dbg, Dwarf_NameSec *namesec, Dwarf_Section *ds,
65 uint64_t offset, dwarf_size, length, cuoff;
69 assert(*namesec == NULL);
71 if ((ns = malloc(sizeof(struct _Dwarf_NameSec))) == NULL) {
72 DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
73 return (DW_DLE_MEMORY);
75 STAILQ_INIT(&ns->ns_ntlist);
80 while (offset < ds->ds_size) {
82 /* Allocate a new name table. */
83 if ((nt = malloc(sizeof(struct _Dwarf_NameTbl))) == NULL) {
85 DWARF_SET_ERROR(dbg, error, ret);
88 STAILQ_INIT(&nt->nt_nplist);
89 STAILQ_INSERT_TAIL(&ns->ns_ntlist, nt, nt_next);
91 /* Read in the table header. */
92 length = dbg->read(ds->ds_data, &offset, 4);
93 if (length == 0xffffffff) {
95 length = dbg->read(ds->ds_data, &offset, 8);
99 nt->nt_length = length;
100 /* FIXME: verify version */
101 nt->nt_version = dbg->read(ds->ds_data, &offset, 2);
102 nt->nt_cu_offset = dbg->read(ds->ds_data, &offset, dwarf_size);
103 nt->nt_cu_length = dbg->read(ds->ds_data, &offset, dwarf_size);
105 if (!dbg->dbg_info_loaded) {
106 ret = _dwarf_info_load(dbg, 1, error);
107 if (ret != DW_DLE_NONE)
111 /* Find the referenced CU. */
112 STAILQ_FOREACH(cu, &dbg->dbg_cu, cu_next) {
113 if (cu->cu_offset == nt->nt_cu_offset)
116 nt->nt_cu = cu; /* FIXME: Check if NULL here */
118 /* Add name pairs. */
119 while (offset < ds->ds_size) {
120 cuoff = dbg->read(ds->ds_data, &offset, dwarf_size);
123 if ((np = malloc(sizeof(struct _Dwarf_NamePair))) ==
126 DWARF_SET_ERROR(dbg, error, ret);
130 np->np_offset = cuoff;
131 p = (char *) ds->ds_data;
132 np->np_name = &p[offset];
133 while (p[offset++] != '\0')
135 STAILQ_INSERT_TAIL(&nt->nt_nplist, np, np_next);
140 /* Build array of name pairs from all tables. */
141 if (ns->ns_len > 0) {
142 if ((ns->ns_array = malloc(sizeof(Dwarf_NamePair) *
143 ns->ns_len)) == NULL) {
145 DWARF_SET_ERROR(dbg, error, ret);
150 STAILQ_FOREACH(nt, &ns->ns_ntlist, nt_next) {
151 STAILQ_FOREACH(np, &nt->nt_nplist, np_next)
152 ns->ns_array[i++] = np;
154 assert((Dwarf_Unsigned)i == ns->ns_len);
159 return (DW_DLE_NONE);
163 _dwarf_nametbl_cleanup(&ns);
169 _dwarf_nametbl_gen(Dwarf_P_Debug dbg, const char *name, Dwarf_NameTbl nt,
173 Dwarf_Rel_Section drs;
178 assert(dbg != NULL && name != NULL);
179 if (nt == NULL || STAILQ_EMPTY(&nt->nt_nplist))
180 return (DW_DLE_NONE);
184 nt->nt_cu = STAILQ_FIRST(&dbg->dbg_cu);
185 assert(nt->nt_cu != NULL);
186 nt->nt_cu_offset = nt->nt_cu->cu_offset;
187 nt->nt_cu_length = nt->nt_cu->cu_length;
189 /* Create name lookup section. */
190 if ((ret = _dwarf_section_init(dbg, &ds, name, 0, error)) !=
194 /* Create relocation section for the name lookup section. */
195 RCHECK(_dwarf_reloc_section_init(dbg, &drs, ds, error));
197 /* Write table header. */
198 RCHECK(WRITE_VALUE(nt->nt_length, 4));
199 RCHECK(WRITE_VALUE(nt->nt_version, 2));
200 RCHECK(_dwarf_reloc_entry_add(dbg, drs, ds, dwarf_drt_data_reloc, 4,
201 ds->ds_size, 0, nt->nt_cu_offset, ".debug_info", error));
202 RCHECK(WRITE_VALUE(nt->nt_cu_length, 4));
205 STAILQ_FOREACH(np, &nt->nt_nplist, np_next) {
206 assert(np->np_die != NULL);
207 np->np_offset = np->np_die->die_offset;
208 RCHECK(WRITE_VALUE(np->np_offset, 4));
209 RCHECK(WRITE_STRING(np->np_name));
211 RCHECK(WRITE_VALUE(0, 4));
213 /* Fill in the length field. */
214 nt->nt_length = ds->ds_size - 4;
216 dbg->write(ds->ds_data, &offset, nt->nt_length, 4);
218 /* Inform application the creation of name lookup ELF section. */
219 RCHECK(_dwarf_section_callback(dbg, ds, SHT_PROGBITS, 0, 0, 0, error));
221 /* Finalize relocation section for the name lookup section. */
222 RCHECK(_dwarf_reloc_section_finalize(dbg, drs, error));
224 return (DW_DLE_NONE);
227 _dwarf_reloc_section_free(dbg, &drs);
230 _dwarf_section_free(dbg, &ds);
236 _dwarf_nametbl_pro_cleanup(Dwarf_NameTbl *ntp)
239 Dwarf_NamePair np, tnp;
242 if ((nt = *ntp) == NULL)
245 STAILQ_FOREACH_SAFE(np, &nt->nt_nplist, np_next, tnp) {
246 STAILQ_REMOVE(&nt->nt_nplist, np, _Dwarf_NamePair, np_next);