Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * Routines for manipulating tdesc and tdata structures
 */

#if HAVE_NBTOOL_CONFIG_H
# include "nbtool_config.h"
#endif

#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <pthread.h>

#include "ctftools.h"
#include "memory.h"
#include "traverse.h"

/*
 * The layout hash is used during the equivalency checking.  We have a node in
 * the child graph that may be equivalent to a node in the parent graph.  To
 * find the corresponding node (if any) in the parent, we need a quick way to
 * get to all nodes in the parent that look like the node in the child.  Since a
 * large number of nodes don't have names, we need to incorporate the layout of
 * the node into the hash.  If we don't, we'll end up with the vast majority of
 * nodes in bucket zero, with one or two nodes in each of the remaining buckets.
 *
 * There are a couple of constraints, both of which concern forward
 * declarations.  Recall that a forward declaration tdesc is equivalent to a
 * tdesc that actually defines the structure or union.  As such, we cannot
 * incorporate anything into the hash for a named struct or union node that
 * couldn't be found by looking at the forward, and vice versa.
 */
int
tdesc_layouthash(int nbuckets, void *node)
{
	tdesc_t *tdp = node;
	char *name = NULL;
	ulong_t h = 0;

	if (tdp->t_name)
		name = tdp->t_name;
	else {
		switch (tdp->t_type) {
		case POINTER:
		case TYPEDEF:
		case VOLATILE:
		case CONST:
		case RESTRICT:
			name = tdp->t_tdesc->t_name;
			break;
		case FUNCTION:
			h = tdp->t_fndef->fn_nargs +
			    tdp->t_fndef->fn_vargs;
			name = tdp->t_fndef->fn_ret->t_name;
			break;
		case ARRAY:
			h = tdp->t_ardef->ad_nelems;
			name = tdp->t_ardef->ad_contents->t_name;
			break;
		case STRUCT:
		case UNION:
			/*
			 * Unnamed structures, which cannot have forward
			 * declarations pointing to them.  We can therefore
			 * incorporate the name of the first member into
			 * the hash value, assuming there are any.
			 */
			if (tdp->t_members != NULL)
				name = tdp->t_members->ml_name;
			break;
		case ENUM:
			/* Use the first element in the hash value */
			name = tdp->t_emem->el_name;
			break;
		default:
			/*
			 * Intrinsics, forwards, and typedefs all have
			 * names.
			 */
			warning("Unexpected unnamed %d tdesc (ID %d)\n",
			    tdp->t_type, tdp->t_id);
		}
	}

	if (name)
		return (hash_name(nbuckets, name));

	return (h % nbuckets);
}

int
tdesc_layoutcmp(void *arg1, void *arg2)
{
	tdesc_t *tdp1 = arg1, *tdp2 = arg2;

	if (tdp1->t_name == NULL) {
		if (tdp2->t_name == NULL)
			return (0);
		else
			return (-1);
	} else if (tdp2->t_name == NULL)
		return (1);
	else
		return (strcmp(tdp1->t_name, tdp2->t_name));
}

int
tdesc_idhash(int nbuckets, void *data)
{
	tdesc_t *tdp = data;

	return (tdp->t_id % nbuckets);
}

int
tdesc_idcmp(void *arg1, void *arg2)
{
	tdesc_t *tdp1 = arg1, *tdp2 = arg2;

	if (tdp1->t_id == tdp2->t_id)
		return (0);
	else
		return (tdp1->t_id > tdp2->t_id ? 1 : -1);
}

int
tdesc_namehash(int nbuckets, void *data)
{
	tdesc_t *tdp = data;
	ulong_t h, g;
	char *c;

	if (tdp->t_name == NULL)
		return (0);

	for (h = 0, c = tdp->t_name; *c; c++) {
		h = (h << 4) + *c;
		if ((g = (h & 0xf0000000)) != 0) {
			h ^= (g >> 24);
			h ^= g;
		}
	}

	return (h % nbuckets);
}

int
tdesc_namecmp(void *arg1, void *arg2)
{
	tdesc_t *tdp1 = arg1, *tdp2 = arg2;

	return (!streq(tdp1->t_name, tdp2->t_name));
}

#ifdef illumos
/*ARGSUSED1*/
static int
tdesc_print(void *data, void *private __unused)
{
	tdesc_t *tdp = data;

	printf("%7d %s\n", tdp->t_id, tdesc_name(tdp));

	return (1);
}
#endif

static void
free_intr(tdesc_t *tdp)
{
	free(tdp->t_intr);
}

static void
free_ardef(tdesc_t *tdp)
{
	free(tdp->t_ardef);
}

static void
free_mlist(tdesc_t *tdp)
{
	mlist_t *ml = tdp->t_members;
	mlist_t *oml;

	while (ml) {
		oml = ml;
		ml = ml->ml_next;

		if (oml->ml_name)
			free(oml->ml_name);
		free(oml);
	}
}

static void
free_elist(tdesc_t *tdp)
{
	elist_t *el = tdp->t_emem;
	elist_t *oel;

	while (el) {
		oel = el;
		el = el->el_next;

		if (oel->el_name)
			free(oel->el_name);
		free(oel);
	}
}

static void (*free_cbs[])(tdesc_t *) = {
	NULL,
	free_intr,	/* intrinsic */
	NULL,		/* pointer */
	NULL,		/* reference */
	free_ardef,	/* array */
	NULL,		/* function */
	free_mlist,	/* struct */
	free_mlist,	/* union */
	free_mlist,	/* class */
	free_elist,	/* enum */
	NULL,		/* forward */
	NULL,		/* typedef */
	NULL,		/* typedef_unres */
	NULL,		/* volatile */
	NULL,		/* const */
	NULL		/* restrict */
};

/*ARGSUSED1*/
static void
tdesc_free_cb(void *arg, void *private __unused)
{
	tdesc_t *tdp = arg;
	if (tdp->t_name)
		free(tdp->t_name);
	if (free_cbs[tdp->t_type])
		free_cbs[tdp->t_type](tdp);
	free(tdp);

	return;
}

void
tdesc_free(tdesc_t *tdp)
{
	tdesc_free_cb(tdp, NULL);
}

static int
tdata_label_cmp(void *arg1, void *arg2)
{
	labelent_t *le1 = arg1;
	labelent_t *le2 = arg2;
	return (le1->le_idx - le2->le_idx);
}

void
tdata_label_add(tdata_t *td, const char *label, int idx)
{
	labelent_t *le = xmalloc(sizeof (*le));

	le->le_name = xstrdup(label);
	le->le_idx = (idx == -1 ? td->td_nextid - 1 : idx);

	slist_add(&td->td_labels, le, tdata_label_cmp);
}

static int
tdata_label_top_cb(void *data, void *arg)
{
	labelent_t *le = data;
	labelent_t **topp = arg;

	*topp = le;

	return (1);
}

labelent_t *
tdata_label_top(tdata_t *td)
{
	labelent_t *top = NULL;

	(void) list_iter(td->td_labels, tdata_label_top_cb, &top);

	return (top);
}

static int
tdata_label_find_cb(void *arg1, void *arg2)
{
	labelent_t *le = arg1;
	labelent_t *tmpl = arg2;
	return (streq(le->le_name, tmpl->le_name));
}

int
tdata_label_find(tdata_t *td, char *label)
{
	labelent_t let;
	labelent_t *ret;

	if (streq(label, "BASE")) {
		ret = (labelent_t *)list_first(td->td_labels);
		return (ret ? ret->le_idx : -1);
	}

	let.le_name = label;

	if (!(ret = (labelent_t *)list_find(td->td_labels, &let,
	    tdata_label_find_cb)))
		return (-1);

	return (ret->le_idx);
}

static int
tdata_label_newmax_cb(void *data, void *arg)
{
	labelent_t *le = data;
	int *newmaxp = arg;

	if (le->le_idx > *newmaxp) {
		le->le_idx = *newmaxp;
		return (1);
	}

	return (0);
}

void
tdata_label_newmax(tdata_t *td, int newmax)
{
	(void) list_iter(td->td_labels, tdata_label_newmax_cb, &newmax);
}

/*ARGSUSED1*/
static void
tdata_label_free_cb(void *arg, void *private __unused)
{
	labelent_t *le = arg;
	if (le->le_name)
		free(le->le_name);
	free(le);
}

void
tdata_label_free(tdata_t *td)
{
	list_free(td->td_labels, tdata_label_free_cb, NULL);
	td->td_labels = NULL;
}

tdata_t *
tdata_new(void)
{
	tdata_t *new = xcalloc(sizeof (tdata_t));

	new->td_layouthash = hash_new(TDATA_LAYOUT_HASH_SIZE, tdesc_layouthash,
	    tdesc_layoutcmp);
	new->td_idhash = hash_new(TDATA_ID_HASH_SIZE, tdesc_idhash,
	    tdesc_idcmp);
	/*
	 * This is also traversed as a list, but amortized O(1)
	 * lookup massively impacts part of the merge phase, so
	 * we store the iidescs as a hash.
	 */
	new->td_iihash = hash_new(IIDESC_HASH_SIZE, iidesc_hash, NULL);
	new->td_nextid = 1;
	new->td_curvgen = 1;

	pthread_mutex_init(&new->td_mergelock, NULL);

	return (new);
}

void
tdata_free(tdata_t *td)
{
	hash_free(td->td_iihash, iidesc_free, NULL);
	hash_free(td->td_layouthash, tdesc_free_cb, NULL);
	hash_free(td->td_idhash, NULL, NULL);
	list_free(td->td_fwdlist, NULL, NULL);

	tdata_label_free(td);

	free(td->td_parlabel);
	free(td->td_parname);

	pthread_mutex_destroy(&td->td_mergelock);

	free(td);
}

/*ARGSUSED1*/
static int
build_hashes(tdesc_t *ctdp, tdesc_t **ctdpp __unused, void *private)
{
	tdata_t *td = private;

	hash_add(td->td_idhash, ctdp);
	hash_add(td->td_layouthash, ctdp);

	return (1);
}

static tdtrav_cb_f build_hashes_cbs[] = {
	NULL,
	build_hashes,	/* intrinsic */
	build_hashes,	/* pointer */
	build_hashes,	/* reference */
	build_hashes,	/* array */
	build_hashes,	/* function */
	build_hashes,	/* struct */
	build_hashes,	/* union */
	build_hashes,	/* class */
	build_hashes,	/* enum */
	build_hashes,	/* forward */
	build_hashes,	/* typedef */
	tdtrav_assert,	/* typedef_unres */
	build_hashes,	/* volatile */
	build_hashes,	/* const */
	build_hashes	/* restrict */
};

static void
tdata_build_hashes_common(tdata_t *td, hash_t *hash)
{
	(void) iitraverse_hash(hash, &td->td_curvgen, NULL, NULL,
	    build_hashes_cbs, td);
}

void
tdata_build_hashes(tdata_t *td)
{
	tdata_build_hashes_common(td, td->td_iihash);
}

/* Merge td2 into td1.  td2 is destroyed by the merge */
void
tdata_merge(tdata_t *td1, tdata_t *td2)
{
	td1->td_curemark = MAX(td1->td_curemark, td2->td_curemark);
	td1->td_curvgen = MAX(td1->td_curvgen, td2->td_curvgen);
	td1->td_nextid = MAX(td1->td_nextid, td2->td_nextid);

	hash_merge(td1->td_iihash, td2->td_iihash);

	/* Add td2's type tree to the hashes */
	tdata_build_hashes_common(td1, td2->td_iihash);

	list_concat(&td1->td_fwdlist, td2->td_fwdlist);
	td2->td_fwdlist = NULL;

	slist_merge(&td1->td_labels, td2->td_labels,
	    tdata_label_cmp);
	td2->td_labels = NULL;

	/* free the td2 hashes (data is now part of td1) */

	hash_free(td2->td_layouthash, NULL, NULL);
	td2->td_layouthash = NULL;

	hash_free(td2->td_iihash, NULL, NULL);
	td2->td_iihash = NULL;

	tdata_free(td2);
}