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