1 /* $NetBSD: value-labels.c,v 1.1.1.3 2019/12/22 12:34:06 skrll Exp $ */
2
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4 /*
5 * libfdt - Flat Device Tree manipulation
6 * Test labels within values
7 * Copyright (C) 2008 David Gibson, IBM Corporation.
8 */
9
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <stdint.h>
14 #include <errno.h>
15
16 #include <dlfcn.h>
17
18 #include <libfdt.h>
19
20 #include "tests.h"
21 #include "testdata.h"
22
23 struct val_label {
24 const char *labelname;
25 int propoff;
26 };
27
28 static struct val_label labels1[] = {
29 { "start1", 0 },
30 { "mid1", 2 },
31 { "end1", -1 },
32 };
33
34 static struct val_label labels2[] = {
35 { "start2", 0 },
36 { "innerstart2", 0 },
37 { "innermid2", 4 },
38 { "innerend2", -1 },
39 { "end2", -1 },
40 };
41
42 static struct val_label labels3[] = {
43 { "start3", 0 },
44 { "innerstart3", 0 },
45 { "innermid3", 1 },
46 { "innerend3", -1 },
47 { "end3", -1 },
48 };
49
check_prop_labels(void * sohandle,void * fdt,const char * name,const struct val_label * labels,int n)50 static void check_prop_labels(void *sohandle, void *fdt, const char *name,
51 const struct val_label* labels, int n)
52 {
53 const struct fdt_property *prop;
54 const char *p;
55 int len;
56 int i;
57
58 prop = fdt_get_property(fdt, 0, name, &len);
59 if (!prop)
60 FAIL("Couldn't locate property \"%s\"", name);
61
62 p = dlsym(sohandle, name);
63 if (!p)
64 FAIL("Couldn't locate label symbol \"%s\"", name);
65
66 if (p != (const char *)prop)
67 FAIL("Label \"%s\" does not point to correct property", name);
68
69 for (i = 0; i < n; i++) {
70 int off = labels[i].propoff;
71
72 if (off == -1)
73 off = len;
74
75 p = dlsym(sohandle, labels[i].labelname);
76 if (!p)
77 FAIL("Couldn't locate label symbol \"%s\"", name);
78
79 if ((p - prop->data) != off)
80 FAIL("Label \"%s\" points to offset %ld instead of %d"
81 "in property \"%s\"", labels[i].labelname,
82 (long)(p - prop->data), off, name);
83 }
84 }
85
main(int argc,char * argv[])86 int main(int argc, char *argv[])
87 {
88 void *sohandle;
89 void *fdt;
90 int err;
91
92 test_init(argc, argv);
93 if (argc != 2)
94 CONFIG("Usage: %s <so file>", argv[0]);
95
96 sohandle = dlopen(argv[1], RTLD_NOW);
97 if (!sohandle)
98 FAIL("Couldn't dlopen() %s", argv[1]);
99
100 fdt = dlsym(sohandle, "dt_blob_start");
101 if (!fdt)
102 FAIL("Couldn't locate \"dt_blob_start\" symbol in %s",
103 argv[1]);
104
105 err = fdt_check_header(fdt);
106 if (err != 0)
107 FAIL("%s contains invalid tree: %s", argv[1],
108 fdt_strerror(err));
109
110
111 check_prop_labels(sohandle, fdt, "prop1", labels1, ARRAY_SIZE(labels1));
112 check_prop_labels(sohandle, fdt, "prop2", labels2, ARRAY_SIZE(labels2));
113 check_prop_labels(sohandle, fdt, "prop3", labels3, ARRAY_SIZE(labels3));
114
115 PASS();
116 }
117