xref: /freebsd/contrib/elftoolchain/libdwarf/libdwarf_nametbl.c (revision 6be3386466ab79a84b48429ae66244f21526d3df)
1 /*-
2  * Copyright (c) 2009,2010 Kai Wang
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  */
26 
27 #include "_libdwarf.h"
28 
29 ELFTC_VCSID("$Id: libdwarf_nametbl.c 3029 2014-04-21 23:26:02Z kaiwang27 $");
30 
31 void
32 _dwarf_nametbl_cleanup(Dwarf_NameSec *nsp)
33 {
34 	Dwarf_NameSec ns;
35 	Dwarf_NameTbl nt, tnt;
36 	Dwarf_NamePair np, tnp;
37 
38 	assert(nsp != NULL);
39 	if ((ns = *nsp) == NULL)
40 		return;
41 
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,
45 			    np_next);
46 			free(np);
47 		}
48 		STAILQ_REMOVE(&ns->ns_ntlist, nt, _Dwarf_NameTbl, nt_next);
49 		free(nt);
50 	}
51 	if (ns->ns_array)
52 		free(ns->ns_array);
53 	free(ns);
54 	*nsp = NULL;
55 }
56 
57 int
58 _dwarf_nametbl_init(Dwarf_Debug dbg, Dwarf_NameSec *namesec, Dwarf_Section *ds,
59     Dwarf_Error *error)
60 {
61 	Dwarf_CU cu;
62 	Dwarf_NameSec ns;
63 	Dwarf_NameTbl nt;
64 	Dwarf_NamePair np;
65 	uint64_t offset, dwarf_size, length, cuoff;
66 	char *p;
67 	int i, ret;
68 
69 	assert(*namesec == NULL);
70 
71 	if ((ns = malloc(sizeof(struct _Dwarf_NameSec))) == NULL) {
72 		DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
73 		return (DW_DLE_MEMORY);
74 	}
75 	STAILQ_INIT(&ns->ns_ntlist);
76 	ns->ns_array = NULL;
77 	ns->ns_len = 0;
78 
79 	offset = 0;
80 	while (offset < ds->ds_size) {
81 
82 		/* Allocate a new name table. */
83 		if ((nt = malloc(sizeof(struct _Dwarf_NameTbl))) == NULL) {
84 			ret = DW_DLE_MEMORY;
85 			DWARF_SET_ERROR(dbg, error, ret);
86 			goto fail_cleanup;
87 		}
88 		STAILQ_INIT(&nt->nt_nplist);
89 		STAILQ_INSERT_TAIL(&ns->ns_ntlist, nt, nt_next);
90 
91 		/* Read in the table header. */
92 		length = dbg->read(ds->ds_data, &offset, 4);
93 		if (length == 0xffffffff) {
94 			dwarf_size = 8;
95 			length = dbg->read(ds->ds_data, &offset, 8);
96 		} else
97 			dwarf_size = 4;
98 
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);
104 
105 		if (!dbg->dbg_info_loaded) {
106 			ret = _dwarf_info_load(dbg, 1, 1, error);
107 			if (ret != DW_DLE_NONE)
108 				goto fail_cleanup;
109 		}
110 
111 		/* Find the referenced CU. */
112 		STAILQ_FOREACH(cu, &dbg->dbg_cu, cu_next) {
113 			if (cu->cu_offset == nt->nt_cu_offset)
114 				break;
115 		}
116 		nt->nt_cu = cu;	/* FIXME: Check if NULL here */
117 
118 		/* Add name pairs. */
119 		while (offset < ds->ds_size) {
120 			cuoff = dbg->read(ds->ds_data, &offset, dwarf_size);
121 			if (cuoff == 0)
122 				break;
123 			if ((np = malloc(sizeof(struct _Dwarf_NamePair))) ==
124 			    NULL) {
125 				ret = DW_DLE_MEMORY;
126 				DWARF_SET_ERROR(dbg, error, ret);
127 				goto fail_cleanup;
128 			}
129 			np->np_nt = nt;
130 			np->np_offset = cuoff;
131 			p = (char *) ds->ds_data;
132 			np->np_name = &p[offset];
133 			while (p[offset++] != '\0')
134 				;
135 			STAILQ_INSERT_TAIL(&nt->nt_nplist, np, np_next);
136 			ns->ns_len++;
137 		}
138 	}
139 
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) {
144 			ret = DW_DLE_MEMORY;
145 			DWARF_SET_ERROR(dbg, error, ret);
146 			goto fail_cleanup;
147 		}
148 
149 		i = 0;
150 		STAILQ_FOREACH(nt, &ns->ns_ntlist, nt_next) {
151 			STAILQ_FOREACH(np, &nt->nt_nplist, np_next)
152 				ns->ns_array[i++] = np;
153 		}
154 		assert((Dwarf_Unsigned)i == ns->ns_len);
155 	}
156 
157 	*namesec = ns;
158 
159 	return (DW_DLE_NONE);
160 
161 fail_cleanup:
162 
163 	_dwarf_nametbl_cleanup(&ns);
164 
165 	return (ret);
166 }
167 
168 int
169 _dwarf_nametbl_gen(Dwarf_P_Debug dbg, const char *name, Dwarf_NameTbl nt,
170     Dwarf_Error *error)
171 {
172 	Dwarf_P_Section ds;
173 	Dwarf_Rel_Section drs;
174 	Dwarf_NamePair np;
175 	uint64_t offset;
176 	int ret;
177 
178 	assert(dbg != NULL && name != NULL);
179 	if (nt == NULL || STAILQ_EMPTY(&nt->nt_nplist))
180 		return (DW_DLE_NONE);
181 
182 	nt->nt_length = 0;
183 	nt->nt_version = 2;
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;
188 
189 	/* Create name lookup section. */
190 	if ((ret = _dwarf_section_init(dbg, &ds, name, 0, error)) !=
191 	    DW_DLE_NONE)
192 		goto gen_fail0;
193 
194 	/* Create relocation section for the name lookup section. */
195 	RCHECK(_dwarf_reloc_section_init(dbg, &drs, ds, error));
196 
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));
203 
204 	/* Write tuples. */
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));
210 	}
211 	RCHECK(WRITE_VALUE(0, 4));
212 
213 	/* Fill in the length field. */
214 	nt->nt_length = ds->ds_size - 4;
215 	offset = 0;
216 	dbg->write(ds->ds_data, &offset, nt->nt_length, 4);
217 
218 	/* Inform application the creation of name lookup ELF section. */
219 	RCHECK(_dwarf_section_callback(dbg, ds, SHT_PROGBITS, 0, 0, 0, error));
220 
221 	/* Finalize relocation section for the name lookup section. */
222 	RCHECK(_dwarf_reloc_section_finalize(dbg, drs, error));
223 
224 	return (DW_DLE_NONE);
225 
226 gen_fail:
227 	_dwarf_reloc_section_free(dbg, &drs);
228 
229 gen_fail0:
230 	_dwarf_section_free(dbg, &ds);
231 
232 	return (ret);
233 }
234 
235 void
236 _dwarf_nametbl_pro_cleanup(Dwarf_NameTbl *ntp)
237 {
238 	Dwarf_NameTbl nt;
239 	Dwarf_NamePair np, tnp;
240 
241 	assert(ntp != NULL);
242 	if ((nt = *ntp) == NULL)
243 		return;
244 
245 	STAILQ_FOREACH_SAFE(np, &nt->nt_nplist, np_next, tnp) {
246 		STAILQ_REMOVE(&nt->nt_nplist, np, _Dwarf_NamePair, np_next);
247 		if (np->np_name)
248 			free(np->np_name);
249 		free(np);
250 	}
251 	free(nt);
252 	*ntp = NULL;
253 }
254