4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
31 #include "dis_target.h"
36 * List support functions.
38 * Support routines for managing lists of sections and functions. We first
39 * process the command line arguments into lists of strings. For each target,
40 * we resolve these strings against the set of available sections and/or
41 * functions to arrive at the set of objects to disassemble.
43 * We export two types of lists, namelists and resolvelists. The first is used
44 * to record names given as command line options. The latter is used to
45 * maintain the data objects specific to a given target.
48 typedef struct unresolved_name
{
49 const char *un_name
; /* name of function or object */
50 int un_value
; /* user-supplied data */
51 int un_mark
; /* internal counter */
52 uu_list_node_t un_node
; /* uulist node */
55 typedef struct resolved_name
{
56 void *rn_data
; /* section or function data */
57 int rn_value
; /* user-supplied data */
58 uu_list_node_t rn_node
; /* uulist node */
61 static uu_list_pool_t
*unresolved_pool
;
62 static uu_list_pool_t
*resolved_pool
;
63 static int current_mark
= 0;
66 initialize_pools(void)
68 unresolved_pool
= uu_list_pool_create(
69 "unresolved_pool", sizeof (unresolved_name_t
),
70 offsetof(unresolved_name_t
, un_node
), NULL
, 0);
71 resolved_pool
= uu_list_pool_create(
72 "resolved_pool", sizeof (resolved_name_t
),
73 offsetof(resolved_name_t
, rn_node
), NULL
, 0);
75 if (unresolved_pool
== NULL
||
76 resolved_pool
== NULL
)
81 * Returns an empty list of unresolved names.
84 dis_namelist_create(void)
89 * If this is the first request to create a list, initialize the list
92 if (unresolved_pool
== NULL
)
95 if ((listp
= uu_list_create(unresolved_pool
, NULL
, 0)) == NULL
)
102 * Adds the given name to the unresolved list. 'value' is an arbitrary value
103 * which is preserved for this entry, even when resolved against a target. This
104 * allows the caller to associate similar behavior (such as the difference
105 * between -d, -D, and -s) without having to create multiple lists.
108 dis_namelist_add(dis_namelist_t
*list
, const char *name
, int value
)
110 unresolved_name_t
*node
;
112 node
= safe_malloc(sizeof (unresolved_name_t
));
114 node
->un_name
= name
;
115 node
->un_value
= value
;
118 (void) uu_list_insert_before(list
, NULL
, node
);
122 * Internal callback structure used
124 typedef struct cb_data
{
126 uu_list_t
*cb_source
;
127 uu_list_t
*cb_resolved
;
131 * For each section, walk the list of unresolved names and resolve those that
132 * correspond to real functions. We mark functions as we see them, and re-walk
133 * the list a second time to warn about functions we didn't find.
135 * This is an O(n * m) algorithm, but we typically search for only a single
140 walk_sections(dis_tgt_t
*tgt
, dis_scn_t
*scn
, void *data
)
142 cb_data_t
*cb
= data
;
143 unresolved_name_t
*unp
;
144 uu_list_walk_t
*walk
;
146 if ((walk
= uu_list_walk_start(cb
->cb_source
, UU_DEFAULT
)) == NULL
)
147 die("out of memory");
149 while ((unp
= uu_list_walk_next(walk
)) != NULL
) {
150 if (strcmp(unp
->un_name
, dis_section_name(scn
)) == 0) {
151 resolved_name_t
*resolved
;
154 * Mark the current node as seen
156 unp
->un_mark
= cb
->cb_mark
;
159 * Add the data to the resolved list
161 resolved
= safe_malloc(sizeof (resolved_name_t
));
163 resolved
->rn_data
= dis_section_copy(scn
);
164 resolved
->rn_value
= unp
->un_value
;
166 (void) uu_list_insert_before(cb
->cb_resolved
, NULL
,
171 uu_list_walk_end(walk
);
175 * Take a list of unresolved names and create a resolved list of sections. We
176 * rely on walk_sections() to do the dirty work. After resolving the sections,
177 * we check for any unmarked names and warn the user about missing sections.
180 dis_namelist_resolve_sections(dis_namelist_t
*namelist
, dis_tgt_t
*tgt
)
184 unresolved_name_t
*unp
;
185 uu_list_walk_t
*walk
;
188 * Walk all sections in the target, calling walk_sections() for each
191 if ((listp
= uu_list_create(resolved_pool
, NULL
, UU_DEFAULT
)) == NULL
)
192 die("out of memory");
194 cb
.cb_mark
= ++current_mark
;
195 cb
.cb_source
= namelist
;
196 cb
.cb_resolved
= listp
;
198 dis_tgt_section_iter(tgt
, walk_sections
, &cb
);
201 * Walk all elements of the unresolved list, and report any that we
202 * didn't mark in the process.
204 if ((walk
= uu_list_walk_start(namelist
, UU_DEFAULT
)) == NULL
)
205 die("out of memory");
207 while ((unp
= uu_list_walk_next(walk
)) != NULL
) {
208 if (unp
->un_mark
!= current_mark
)
209 warn("failed to find section '%s' in '%s'",
210 unp
->un_name
, dis_tgt_name(tgt
));
213 uu_list_walk_end(walk
);
219 * Similar to walk_sections(), but for functions.
223 walk_functions(dis_tgt_t
*tgt
, dis_func_t
*func
, void *data
)
225 cb_data_t
*cb
= data
;
226 unresolved_name_t
*unp
;
227 uu_list_walk_t
*walk
;
229 if ((walk
= uu_list_walk_start(cb
->cb_source
, UU_DEFAULT
)) == NULL
)
230 die("out of memory");
232 while ((unp
= uu_list_walk_next(walk
)) != NULL
) {
233 if (strcmp(unp
->un_name
, dis_function_name(func
)) == 0) {
234 resolved_name_t
*resolved
;
236 unp
->un_mark
= cb
->cb_mark
;
238 resolved
= safe_malloc(sizeof (resolved_name_t
));
240 resolved
->rn_data
= dis_function_copy(func
);
241 resolved
->rn_value
= unp
->un_value
;
243 (void) uu_list_insert_before(cb
->cb_resolved
, NULL
,
248 uu_list_walk_end(walk
);
252 * Take a list of unresolved names and create a resolved list of functions. We
253 * rely on walk_functions() to do the dirty work. After resolving the
254 * functions, * we check for any unmarked names and warn the user about missing
258 dis_namelist_resolve_functions(dis_namelist_t
*namelist
, dis_tgt_t
*tgt
)
261 uu_list_walk_t
*walk
;
262 unresolved_name_t
*unp
;
265 if ((listp
= uu_list_create(resolved_pool
, NULL
, UU_DEFAULT
)) == NULL
)
266 die("out of memory");
268 cb
.cb_mark
= ++current_mark
;
269 cb
.cb_source
= namelist
;
270 cb
.cb_resolved
= listp
;
272 dis_tgt_function_iter(tgt
, walk_functions
, &cb
);
275 * Walk unresolved list and report any missing functions.
277 if ((walk
= uu_list_walk_start(namelist
, UU_DEFAULT
)) == NULL
)
278 die("out of memory");
280 while ((unp
= uu_list_walk_next(walk
)) != NULL
) {
281 if (unp
->un_mark
!= current_mark
)
282 warn("failed to find function '%s' in '%s'",
283 unp
->un_name
, dis_tgt_name(tgt
));
286 uu_list_walk_end(walk
);
292 * Returns true if the given list is empty.
295 dis_namelist_empty(dis_namelist_t
*list
)
297 return (uu_list_numnodes(list
) == 0);
301 free_list(uu_list_t
*list
)
303 uu_list_walk_t
*walk
;
306 if ((walk
= uu_list_walk_start(list
, UU_WALK_ROBUST
)) == NULL
)
307 die("out of memory");
309 while ((data
= uu_list_walk_next(walk
)) != NULL
) {
310 uu_list_remove(list
, data
);
314 uu_list_walk_end(walk
);
316 uu_list_destroy(list
);
320 * Destroy a list of sections. First, walk the list and free the associated
321 * section data. Pass the list onto to free_list() to clean up the rest of the
325 dis_scnlist_destroy(dis_scnlist_t
*list
)
327 uu_list_walk_t
*walk
;
328 resolved_name_t
*data
;
330 if ((walk
= uu_list_walk_start(list
, UU_DEFAULT
)) == NULL
)
331 die("out of memory");
333 while ((data
= uu_list_walk_next(walk
)) != NULL
)
334 dis_section_free(data
->rn_data
);
336 uu_list_walk_end(walk
);
342 * Destroy a list of functions. First, walk the list and free the associated
343 * function data. Pass the list onto to free_list() to clean up the rest of the
347 dis_funclist_destroy(dis_funclist_t
*list
)
349 uu_list_walk_t
*walk
;
350 resolved_name_t
*data
;
352 if ((walk
= uu_list_walk_start(list
, UU_DEFAULT
)) == NULL
)
353 die("out of memory");
355 while ((data
= uu_list_walk_next(walk
)) != NULL
)
356 dis_function_free(data
->rn_data
);
358 uu_list_walk_end(walk
);
364 * Destroy a lis tof unresolved names.
367 dis_namelist_destroy(dis_namelist_t
*list
)
373 * Iterate over a resolved list of sections.
376 dis_scnlist_iter(uu_list_t
*list
, void (*func
)(dis_scn_t
*, int, void *),
379 uu_list_walk_t
*walk
;
380 resolved_name_t
*data
;
382 if ((walk
= uu_list_walk_start(list
, UU_DEFAULT
)) == NULL
)
383 die("out of memory");
385 while ((data
= uu_list_walk_next(walk
)) != NULL
)
386 func(data
->rn_data
, data
->rn_value
, arg
);
388 uu_list_walk_end(walk
);
392 * Iterate over a resolved list of functions.
395 dis_funclist_iter(uu_list_t
*list
, void (*func
)(dis_func_t
*, int, void *),
398 uu_list_walk_t
*walk
;
399 resolved_name_t
*data
;
401 if ((walk
= uu_list_walk_start(list
, UU_DEFAULT
)) == NULL
)
402 die("out of memory");
404 while ((data
= uu_list_walk_next(walk
)) != NULL
)
405 func(data
->rn_data
, data
->rn_value
, arg
);
407 uu_list_walk_end(walk
);