1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/compiler.h>
3 #include <linux/string.h>
16 struct machine
*machine
;
17 struct thread
*thread
;
20 static int init_test_info(struct test_info
*ti
)
22 ti
->machine
= machine__new_host();
24 pr_debug("machine__new_host() failed!\n");
28 /* Create a dummy thread */
29 ti
->thread
= machine__findnew_thread(ti
->machine
, 100, 100);
31 pr_debug("machine__findnew_thread() failed!\n");
38 static void exit_test_info(struct test_info
*ti
)
40 thread__put(ti
->thread
);
41 machine__delete(ti
->machine
);
49 static int find_map_cb(struct map
*map
, void *d
)
51 struct dso_map
*data
= d
;
53 if (map__dso(map
) != data
->dso
)
59 static struct map
*find_module_map(struct machine
*machine
, struct dso
*dso
)
61 struct dso_map data
= { .dso
= dso
};
63 machine__for_each_kernel_map(machine
, find_map_cb
, &data
);
68 static void get_test_dso_filename(char *filename
, size_t max_sz
)
71 strlcpy(filename
, dso_to_test
, max_sz
);
73 perf_exe(filename
, max_sz
);
76 static int create_map(struct test_info
*ti
, char *filename
, struct map
**map_p
)
78 struct dso
*dso
= machine__findnew_dso(ti
->machine
, filename
);
81 * If 'filename' matches a current kernel module, must use a kernel
82 * map. Find the one that already exists.
84 if (dso
&& dso__kernel(dso
) != DSO_SPACE__USER
) {
85 *map_p
= find_module_map(ti
->machine
, dso
);
88 pr_debug("Failed to find map for current kernel module %s",
98 /* Create a dummy map at 0x100000 */
99 *map_p
= map__new(ti
->machine
, 0x100000, 0xffffffff, 0, NULL
,
100 PROT_EXEC
, 0, NULL
, filename
, ti
->thread
);
102 pr_debug("Failed to create map!");
109 static int test_dso(struct dso
*dso
)
111 struct symbol
*last_sym
= NULL
;
115 /* dso__fprintf() prints all the symbols */
117 dso__fprintf(dso
, stderr
);
119 for (nd
= rb_first_cached(dso__symbols(dso
)); nd
; nd
= rb_next(nd
)) {
120 struct symbol
*sym
= rb_entry(nd
, struct symbol
, rb_node
);
122 if (sym
->type
!= STT_FUNC
&& sym
->type
!= STT_GNU_IFUNC
)
125 /* Check for overlapping function symbols */
126 if (last_sym
&& sym
->start
< last_sym
->end
) {
127 pr_debug("Overlapping symbols:\n");
128 symbol__fprintf(last_sym
, stderr
);
129 symbol__fprintf(sym
, stderr
);
132 /* Check for zero-length function symbol */
133 if (sym
->start
== sym
->end
) {
134 pr_debug("Zero-length symbol:\n");
135 symbol__fprintf(sym
, stderr
);
144 static int subdivided_dso_cb(struct dso
*dso
, struct machine
*machine __maybe_unused
, void *d
)
146 struct dso
*text_dso
= d
;
148 if (dso
!= text_dso
&& strstarts(dso__short_name(dso
), dso__short_name(text_dso
)))
149 if (test_dso(dso
) != TEST_OK
)
155 static int process_subdivided_dso(struct machine
*machine
, struct dso
*dso
)
159 ret
= machine__for_each_dso(machine
, subdivided_dso_cb
, dso
);
161 return ret
< 0 ? TEST_FAIL
: TEST_OK
;
164 static int test_file(struct test_info
*ti
, char *filename
)
166 struct map
*map
= NULL
;
170 pr_debug("Testing %s\n", filename
);
172 ret
= create_map(ti
, filename
, &map
);
177 nr
= dso__load(dso
, map
);
179 pr_debug("dso__load() failed!\n");
185 pr_debug("DSO has no symbols!\n");
192 /* Module dso is split into many dsos by section */
193 if (ret
== TEST_OK
&& dso__kernel(dso
) != DSO_SPACE__USER
)
194 ret
= process_subdivided_dso(ti
->machine
, dso
);
201 static int test__symbols(struct test_suite
*test __maybe_unused
, int subtest __maybe_unused
)
203 char filename
[PATH_MAX
];
207 ret
= init_test_info(&ti
);
211 get_test_dso_filename(filename
, sizeof(filename
));
213 ret
= test_file(&ti
, filename
);
220 DEFINE_SUITE("Symbols", symbols
);