1 /*
2 * Copyright (c) 2004 Marcel Moolenaar
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/param.h>
31 #include <sys/stat.h>
32 #include <fcntl.h>
33 #include <kvm.h>
34 #include <libgen.h>
35
36 #include <defs.h>
37 #include <command.h>
38 #include <completer.h>
39 #include <environ.h>
40 #include <exec.h>
41 #include <frame-unwind.h>
42 #include <inferior.h>
43 #include <objfiles.h>
44 #include <gdbcore.h>
45 #include <language.h>
46 #include <solist.h>
47
48 #include "kgdb.h"
49
50 struct lm_info {
51 CORE_ADDR base_address;
52 };
53
54 /* Offsets of fields in linker_file structure. */
55 static CORE_ADDR off_address, off_filename, off_pathname, off_next;
56
57 /* KVA of 'linker_path' which corresponds to the kern.module_path sysctl .*/
58 static CORE_ADDR module_path_addr;
59 static CORE_ADDR linker_files_addr;
60 static CORE_ADDR kernel_file_addr;
61
62 static struct target_so_ops kld_so_ops;
63
64 static int
kld_ok(char * path)65 kld_ok (char *path)
66 {
67 struct stat sb;
68
69 if (stat(path, &sb) == 0 && S_ISREG(sb.st_mode))
70 return (1);
71 return (0);
72 }
73
74 /*
75 * Look for a matching file checking for debug suffixes before the raw file:
76 * - filename + ".debug" (e.g. foo.ko.debug)
77 * - filename (e.g. foo.ko)
78 */
79 static const char *kld_suffixes[] = {
80 ".debug",
81 ".symbols",
82 "",
83 NULL
84 };
85
86 static int
check_kld_path(char * path,size_t path_size)87 check_kld_path (char *path, size_t path_size)
88 {
89 const char **suffix;
90 char *ep;
91
92 ep = path + strlen(path);
93 suffix = kld_suffixes;
94 while (*suffix != NULL) {
95 if (strlcat(path, *suffix, path_size) < path_size) {
96 if (kld_ok(path))
97 return (1);
98 }
99
100 /* Restore original path to remove suffix. */
101 *ep = '\0';
102 suffix++;
103 }
104 return (0);
105 }
106
107 /*
108 * Try to find the path for a kld by looking in the kernel's directory and
109 * in the various paths in the module path.
110 */
111 static int
find_kld_path(char * filename,char * path,size_t path_size)112 find_kld_path (char *filename, char *path, size_t path_size)
113 {
114 char *module_path;
115 char *kernel_dir, *module_dir, *cp;
116 int error;
117
118 if (exec_bfd) {
119 kernel_dir = dirname(bfd_get_filename(exec_bfd));
120 if (kernel_dir != NULL) {
121 snprintf(path, path_size, "%s/%s", kernel_dir,
122 filename);
123 if (check_kld_path(path, path_size))
124 return (1);
125 }
126 }
127 if (module_path_addr != 0) {
128 target_read_string(module_path_addr, &module_path, PATH_MAX,
129 &error);
130 if (error == 0) {
131 make_cleanup(xfree, module_path);
132 cp = module_path;
133 while ((module_dir = strsep(&cp, ";")) != NULL) {
134 snprintf(path, path_size, "%s/%s", module_dir,
135 filename);
136 if (check_kld_path(path, path_size))
137 return (1);
138 }
139 }
140 }
141 return (0);
142 }
143
144 /*
145 * Read a kernel pointer given a KVA in 'address'.
146 */
147 static CORE_ADDR
read_pointer(CORE_ADDR address)148 read_pointer (CORE_ADDR address)
149 {
150 CORE_ADDR value;
151
152 if (target_read_memory(address, (char *)&value, TARGET_PTR_BIT / 8) !=
153 0)
154 return (0);
155 return (extract_unsigned_integer(&value, TARGET_PTR_BIT / 8));
156 }
157
158 /*
159 * Try to find this kld in the kernel linker's list of linker files.
160 */
161 static int
find_kld_address(char * arg,CORE_ADDR * address)162 find_kld_address (char *arg, CORE_ADDR *address)
163 {
164 CORE_ADDR kld;
165 char *kld_filename;
166 char *filename;
167 int error;
168
169 if (linker_files_addr == 0 || off_address == 0 || off_filename == 0 ||
170 off_next == 0)
171 return (0);
172
173 filename = basename(arg);
174 for (kld = read_pointer(linker_files_addr); kld != 0;
175 kld = read_pointer(kld + off_next)) {
176 /* Try to read this linker file's filename. */
177 target_read_string(read_pointer(kld + off_filename),
178 &kld_filename, PATH_MAX, &error);
179 if (error)
180 continue;
181
182 /* Compare this kld's filename against our passed in name. */
183 if (strcmp(kld_filename, filename) != 0) {
184 xfree(kld_filename);
185 continue;
186 }
187 xfree(kld_filename);
188
189 /*
190 * We found a match, use its address as the base
191 * address if we can read it.
192 */
193 *address = read_pointer(kld + off_address);
194 if (*address == 0)
195 return (0);
196 return (1);
197 }
198 return (0);
199 }
200
201 static void
adjust_section_address(struct section_table * sec,CORE_ADDR * curr_base)202 adjust_section_address (struct section_table *sec, CORE_ADDR *curr_base)
203 {
204 struct bfd_section *asect = sec->the_bfd_section;
205 bfd *abfd = sec->bfd;
206
207 if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0) {
208 sec->addr += *curr_base;
209 sec->endaddr += *curr_base;
210 return;
211 }
212
213 *curr_base = align_power(*curr_base,
214 bfd_get_section_alignment(abfd, asect));
215 sec->addr = *curr_base;
216 sec->endaddr = sec->addr + bfd_section_size(abfd, asect);
217 *curr_base = sec->endaddr;
218 }
219
220 static void
load_kld(char * path,CORE_ADDR base_addr,int from_tty)221 load_kld (char *path, CORE_ADDR base_addr, int from_tty)
222 {
223 struct section_addr_info *sap;
224 struct section_table *sections = NULL, *sections_end = NULL, *s;
225 struct cleanup *cleanup;
226 bfd *bfd;
227 CORE_ADDR curr_addr;
228 int i;
229
230 /* Open the kld. */
231 bfd = bfd_openr(path, gnutarget);
232 if (bfd == NULL)
233 error("\"%s\": can't open: %s", path,
234 bfd_errmsg(bfd_get_error()));
235 cleanup = make_cleanup_bfd_close(bfd);
236
237 if (!bfd_check_format(bfd, bfd_object))
238 error("\%s\": not an object file", path);
239
240 /* Make sure we have a .text section. */
241 if (bfd_get_section_by_name (bfd, ".text") == NULL)
242 error("\"%s\": can't find text section", path);
243
244 /* Build a section table from the bfd and relocate the sections. */
245 if (build_section_table (bfd, §ions, §ions_end))
246 error("\"%s\": can't find file sections", path);
247 cleanup = make_cleanup(xfree, sections);
248 curr_addr = base_addr;
249 for (s = sections; s < sections_end; s++)
250 adjust_section_address(s, &curr_addr);
251
252 /* Build a section addr info to pass to symbol_file_add(). */
253 sap = build_section_addr_info_from_section_table (sections,
254 sections_end);
255 cleanup = make_cleanup((make_cleanup_ftype *)free_section_addr_info,
256 sap);
257
258 printf_unfiltered("add symbol table from file \"%s\" at\n", path);
259 for (i = 0; i < sap->num_sections; i++)
260 printf_unfiltered("\t%s_addr = %s\n", sap->other[i].name,
261 local_hex_string(sap->other[i].addr));
262
263 if (from_tty && (!query("%s", "")))
264 error("Not confirmed.");
265
266 symbol_file_add(path, from_tty, sap, 0, OBJF_USERLOADED);
267
268 do_cleanups(cleanup);
269 }
270
271 static void
kgdb_add_kld_cmd(char * arg,int from_tty)272 kgdb_add_kld_cmd (char *arg, int from_tty)
273 {
274 char path[PATH_MAX];
275 CORE_ADDR base_addr;
276
277 if (!exec_bfd)
278 error("No kernel symbol file");
279
280 /* Try to open the raw path to handle absolute paths first. */
281 snprintf(path, sizeof(path), "%s", arg);
282 if (!check_kld_path(path, sizeof(path))) {
283
284 /*
285 * If that didn't work, look in the various possible
286 * paths for the module.
287 */
288 if (!find_kld_path(arg, path, sizeof(path))) {
289 error("Unable to locate kld");
290 return;
291 }
292 }
293
294 if (!find_kld_address(arg, &base_addr)) {
295 error("Unable to find kld in kernel");
296 return;
297 }
298
299 load_kld(path, base_addr, from_tty);
300
301 reinit_frame_cache();
302 }
303
304 static void
kld_relocate_section_addresses(struct so_list * so,struct section_table * sec)305 kld_relocate_section_addresses (struct so_list *so, struct section_table *sec)
306 {
307 static CORE_ADDR curr_addr;
308
309 if (sec == so->sections)
310 curr_addr = so->lm_info->base_address;
311
312 adjust_section_address(sec, &curr_addr);
313 }
314
315 static void
kld_free_so(struct so_list * so)316 kld_free_so (struct so_list *so)
317 {
318
319 xfree(so->lm_info);
320 }
321
322 static void
kld_clear_solib(void)323 kld_clear_solib (void)
324 {
325 }
326
327 static void
kld_solib_create_inferior_hook(void)328 kld_solib_create_inferior_hook (void)
329 {
330 }
331
332 static void
kld_special_symbol_handling(void)333 kld_special_symbol_handling (void)
334 {
335 }
336
337 static struct so_list *
kld_current_sos(void)338 kld_current_sos (void)
339 {
340 struct so_list *head, **prev, *new;
341 CORE_ADDR kld, kernel;
342 char *path;
343 int error;
344
345 if (linker_files_addr == 0 || kernel_file_addr == 0 ||
346 off_address == 0 || off_filename == 0 || off_next == 0)
347 return (NULL);
348
349 head = NULL;
350 prev = &head;
351
352 /*
353 * Walk the list of linker files creating so_list entries for
354 * each non-kernel file.
355 */
356 kernel = read_pointer(kernel_file_addr);
357 for (kld = read_pointer(linker_files_addr); kld != 0;
358 kld = read_pointer(kld + off_next)) {
359 /* Skip the main kernel file. */
360 if (kld == kernel)
361 continue;
362
363 new = xmalloc(sizeof(*new));
364 memset(new, 0, sizeof(*new));
365
366 new->lm_info = xmalloc(sizeof(*new->lm_info));
367 new->lm_info->base_address = 0;
368
369 /* Read the base filename and store it in so_original_name. */
370 target_read_string(read_pointer(kld + off_filename),
371 &path, sizeof(new->so_original_name), &error);
372 if (error != 0) {
373 warning("kld_current_sos: Can't read filename: %s\n",
374 safe_strerror(error));
375 free_so(new);
376 continue;
377 }
378 strlcpy(new->so_original_name, path,
379 sizeof(new->so_original_name));
380 xfree(path);
381
382 /*
383 * Try to read the pathname (if it exists) and store
384 * it in so_name.
385 */
386 if (find_kld_path(new->so_original_name, new->so_name,
387 sizeof(new->so_name))) {
388 /* we found the kld */;
389 } else if (off_pathname != 0) {
390 target_read_string(read_pointer(kld + off_pathname),
391 &path, sizeof(new->so_name), &error);
392 if (error != 0) {
393 warning(
394 "kld_current_sos: Can't read pathname for \"%s\": %s\n",
395 new->so_original_name,
396 safe_strerror(error));
397 strlcpy(new->so_name, new->so_original_name,
398 sizeof(new->so_name));
399 } else {
400 strlcpy(new->so_name, path,
401 sizeof(new->so_name));
402 xfree(path);
403 }
404 } else
405 strlcpy(new->so_name, new->so_original_name,
406 sizeof(new->so_name));
407
408 /* Read this kld's base address. */
409 new->lm_info->base_address = read_pointer(kld + off_address);
410 if (new->lm_info->base_address == 0) {
411 warning(
412 "kld_current_sos: Invalid address for kld \"%s\"",
413 new->so_original_name);
414 free_so(new);
415 continue;
416 }
417
418 /* Append to the list. */
419 *prev = new;
420 prev = &new->next;
421 }
422
423 return (head);
424 }
425
426 static int
kld_open_symbol_file_object(void * from_ttyp)427 kld_open_symbol_file_object (void *from_ttyp)
428 {
429
430 return (0);
431 }
432
433 static int
kld_in_dynsym_resolve_code(CORE_ADDR pc)434 kld_in_dynsym_resolve_code (CORE_ADDR pc)
435 {
436
437 return (0);
438 }
439
440 static int
kld_find_and_open_solib(char * solib,unsigned o_flags,char ** temp_pathname)441 kld_find_and_open_solib (char *solib, unsigned o_flags, char **temp_pathname)
442 {
443 char path[PATH_MAX];
444 int fd;
445
446 *temp_pathname = NULL;
447 if (!find_kld_path(solib, path, sizeof(path))) {
448 errno = ENOENT;
449 return (-1);
450 }
451 fd = open(path, o_flags, 0);
452 if (fd >= 0)
453 *temp_pathname = xstrdup(path);
454 return (fd);
455 }
456
457 void
kld_new_objfile(struct objfile * objfile)458 kld_new_objfile (struct objfile *objfile)
459 {
460
461 if (!have_partial_symbols())
462 return;
463
464 /*
465 * Compute offsets of relevant members in struct linker_file
466 * and the addresses of global variables. Don't warn about
467 * kernels that don't have 'pathname' in the linker_file
468 * struct since 6.x kernels don't have it.
469 */
470 off_address = kgdb_parse("&((struct linker_file *)0)->address");
471 off_filename = kgdb_parse("&((struct linker_file *)0)->filename");
472 off_pathname = kgdb_parse_quiet("&((struct linker_file *)0)->pathname");
473 off_next = kgdb_parse("&((struct linker_file *)0)->link.tqe_next");
474 module_path_addr = kgdb_parse("linker_path");
475 linker_files_addr = kgdb_parse("&linker_files.tqh_first");
476 kernel_file_addr = kgdb_parse("&linker_kernel_file");
477 }
478
479 static int
load_klds_stub(void * arg)480 load_klds_stub (void *arg)
481 {
482
483 SOLIB_ADD(NULL, 1, ¤t_target, auto_solib_add);
484 return (0);
485 }
486
487 void
kld_init(void)488 kld_init (void)
489 {
490
491 catch_errors(load_klds_stub, NULL, NULL, RETURN_MASK_ALL);
492 }
493
494 void
initialize_kld_target(void)495 initialize_kld_target(void)
496 {
497 struct cmd_list_element *c;
498
499 kld_so_ops.relocate_section_addresses = kld_relocate_section_addresses;
500 kld_so_ops.free_so = kld_free_so;
501 kld_so_ops.clear_solib = kld_clear_solib;
502 kld_so_ops.solib_create_inferior_hook = kld_solib_create_inferior_hook;
503 kld_so_ops.special_symbol_handling = kld_special_symbol_handling;
504 kld_so_ops.current_sos = kld_current_sos;
505 kld_so_ops.open_symbol_file_object = kld_open_symbol_file_object;
506 kld_so_ops.in_dynsym_resolve_code = kld_in_dynsym_resolve_code;
507 kld_so_ops.find_and_open_solib = kld_find_and_open_solib;
508
509 current_target_so_ops = &kld_so_ops;
510
511 c = add_com("add-kld", class_files, kgdb_add_kld_cmd,
512 "Usage: add-kld FILE\n\
513 Load the symbols from the kernel loadable module FILE.");
514 set_cmd_completer(c, filename_completer);
515 }
516