2 * Copyright (c) 2004 Marcel Moolenaar
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
26 * $FreeBSD: src/gnu/usr.bin/gdb/kgdb/kld.c, svn 210424 2010/07/23 avg $
29 #include <sys/param.h>
37 #include <completer.h>
40 #include <frame-unwind.h>
46 #include <arch-utils.h>
48 #include <exceptions.h>
54 CORE_ADDR base_address;
57 /* Offsets of fields in linker_file structure. */
58 static CORE_ADDR off_address, off_filename, off_pathname, off_next;
60 /* KVA of 'linker_path' which corresponds to the kern.module_path sysctl .*/
61 static CORE_ADDR module_path_addr;
62 static CORE_ADDR linker_files_addr;
63 static CORE_ADDR kernel_file_addr;
65 static struct target_so_ops kld_so_ops;
72 if (stat(path, &sb) == 0 && S_ISREG(sb.st_mode))
78 * Look for a matching file checking for debug suffixes before the raw file:
79 * - filename + ".debug" (e.g. foo.ko.debug)
80 * - filename (e.g. foo.ko)
82 static const char *kld_suffixes[] = {
89 check_kld_path (char *path, size_t path_size)
94 ep = path + strlen(path);
95 suffix = kld_suffixes;
96 while (*suffix != NULL) {
97 if (strlcat(path, *suffix, path_size) < path_size) {
102 /* Restore original path to remove suffix. */
110 * Try to find the path for a kld by looking in the kernel's directory and
111 * in the various paths in the module path.
114 find_kld_path (char *filename, char *path, size_t path_size)
117 char *kernel_dir, *module_dir, *cp;
121 kernel_dir = dirname(bfd_get_filename(exec_bfd));
122 if (kernel_dir != NULL) {
123 snprintf(path, path_size, "%s/%s", kernel_dir,
125 if (check_kld_path(path, path_size))
129 if (module_path_addr != 0) {
130 target_read_string(module_path_addr, &module_path, PATH_MAX,
133 make_cleanup(xfree, module_path);
135 while ((module_dir = strsep(&cp, ";")) != NULL) {
136 snprintf(path, path_size, "%s/%s", module_dir,
138 if (check_kld_path(path, path_size))
147 * Read a kernel pointer given a KVA in 'address'.
150 read_pointer (CORE_ADDR address)
152 struct gdbarch *arch = get_current_arch();
153 enum bfd_endian byte_order = gdbarch_byte_order(arch);
154 gdb_byte buf[sizeof(ULONGEST)];
157 ptrsz = gdbarch_ptr_bit(arch) / 8;
159 if (target_read_memory(address, buf, ptrsz) != 0)
161 return (extract_unsigned_integer(buf, ptrsz, byte_order));
165 * Try to find this kld in the kernel linker's list of linker files.
168 find_kld_address (char *arg, CORE_ADDR *address)
175 if (linker_files_addr == 0 || off_address == 0 || off_filename == 0 ||
179 filename = basename(arg);
180 for (kld = read_pointer(linker_files_addr); kld != 0;
181 kld = read_pointer(kld + off_next)) {
182 /* Try to read this linker file's filename. */
183 target_read_string(read_pointer(kld + off_filename),
184 &kld_filename, PATH_MAX, &error);
188 /* Compare this kld's filename against our passed in name. */
189 if (strcmp(kld_filename, filename) != 0) {
196 * We found a match, use its address as the base
197 * address if we can read it.
199 *address = read_pointer(kld + off_address);
208 adjust_section_address (struct target_section *sec, CORE_ADDR *curr_base)
210 struct bfd_section *asect = sec->the_bfd_section;
211 bfd *abfd = sec->bfd;
213 if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0) {
214 sec->addr += *curr_base;
215 sec->endaddr += *curr_base;
219 *curr_base = align_power(*curr_base,
220 bfd_get_section_alignment(abfd, asect));
221 sec->addr = *curr_base;
222 sec->endaddr = sec->addr + bfd_section_size(abfd, asect);
223 *curr_base = sec->endaddr;
227 load_kld (char *path, CORE_ADDR base_addr, int from_tty)
229 struct section_addr_info *sap;
230 struct target_section *sections = NULL, *sections_end = NULL, *s;
231 struct cleanup *cleanup;
237 bfd = bfd_openr(path, gnutarget);
239 error("\"%s\": can't open: %s", path,
240 bfd_errmsg(bfd_get_error()));
241 cleanup = make_cleanup_bfd_close(bfd);
243 if (!bfd_check_format(bfd, bfd_object))
244 error("\%s\": not an object file", path);
246 /* Make sure we have a .text section. */
247 if (bfd_get_section_by_name (bfd, ".text") == NULL)
248 error("\"%s\": can't find text section", path);
250 /* Build a section table from the bfd and relocate the sections. */
251 if (build_section_table (bfd, §ions, §ions_end))
252 error("\"%s\": can't find file sections", path);
253 cleanup = make_cleanup(xfree, sections);
254 curr_addr = base_addr;
255 for (s = sections; s < sections_end; s++)
256 adjust_section_address(s, &curr_addr);
258 /* Build a section addr info to pass to symbol_file_add(). */
259 sap = build_section_addr_info_from_section_table (sections,
261 cleanup = make_cleanup((make_cleanup_ftype *)free_section_addr_info,
264 printf_unfiltered("add symbol table from file \"%s\" at\n", path);
265 for (i = 0; i < sap->num_sections; i++)
266 printf_unfiltered("\t%s_addr = %s\n", sap->other[i].name,
267 hex_string(sap->other[i].addr));
269 if (from_tty && (!query("%s", "")))
270 error("Not confirmed.");
272 symbol_file_add(path, from_tty ? SYMFILE_VERBOSE : 0, sap, OBJF_USERLOADED);
274 do_cleanups(cleanup);
278 kgdb_add_kld_cmd (char *arg, int from_tty)
284 error("No kernel symbol file");
286 /* Try to open the raw path to handle absolute paths first. */
287 snprintf(path, sizeof(path), "%s", arg);
288 if (!check_kld_path(path, sizeof(path))) {
291 * If that didn't work, look in the various possible
292 * paths for the module.
294 if (!find_kld_path(arg, path, sizeof(path))) {
295 error("Unable to locate kld");
300 if (!find_kld_address(arg, &base_addr)) {
301 error("Unable to find kld in kernel");
305 load_kld(path, base_addr, from_tty);
307 reinit_frame_cache();
311 kld_relocate_section_addresses (struct so_list *so, struct target_section *sec)
313 static CORE_ADDR curr_addr;
315 if (sec == so->sections)
316 curr_addr = so->lm_info->base_address;
318 adjust_section_address(sec, &curr_addr);
322 kld_free_so (struct so_list *so)
329 kld_clear_solib (void)
334 kld_solib_create_inferior_hook (int from_tty)
339 kld_special_symbol_handling (void)
343 static struct so_list *
344 kld_current_sos (void)
346 struct so_list *head, **prev, *new;
347 CORE_ADDR kld, kernel;
351 if (linker_files_addr == 0 || kernel_file_addr == 0 ||
352 off_address == 0 || off_filename == 0 || off_next == 0)
359 * Walk the list of linker files creating so_list entries for
360 * each non-kernel file.
362 kernel = read_pointer(kernel_file_addr);
363 for (kld = read_pointer(linker_files_addr); kld != 0;
364 kld = read_pointer(kld + off_next)) {
365 /* Skip the main kernel file. */
369 new = xmalloc(sizeof(*new));
370 memset(new, 0, sizeof(*new));
372 new->lm_info = xmalloc(sizeof(*new->lm_info));
373 new->lm_info->base_address = 0;
375 /* Read the base filename and store it in so_original_name. */
376 target_read_string(read_pointer(kld + off_filename),
377 &path, sizeof(new->so_original_name), &error);
379 warning("kld_current_sos: Can't read filename: %s\n",
380 safe_strerror(error));
384 strlcpy(new->so_original_name, path,
385 sizeof(new->so_original_name));
389 * Try to read the pathname (if it exists) and store
392 if (off_pathname != 0) {
393 target_read_string(read_pointer(kld + off_pathname),
394 &path, sizeof(new->so_name), &error);
397 "kld_current_sos: Can't read pathname for \"%s\": %s\n",
398 new->so_original_name,
399 safe_strerror(error));
400 strlcpy(new->so_name, new->so_original_name,
401 sizeof(new->so_name));
403 strlcpy(new->so_name, path,
404 sizeof(new->so_name));
408 strlcpy(new->so_name, new->so_original_name,
409 sizeof(new->so_name));
411 /* Read this kld's base address. */
412 new->lm_info->base_address = read_pointer(kld + off_address);
413 if (new->lm_info->base_address == 0) {
415 "kld_current_sos: Invalid address for kld \"%s\"",
416 new->so_original_name);
421 /* Append to the list. */
430 kld_open_symbol_file_object (void *from_ttyp)
437 kld_in_dynsym_resolve_code (CORE_ADDR pc)
444 kld_find_and_open_solib (char *solib, unsigned o_flags, char **temp_pathname)
449 *temp_pathname = NULL;
450 if (!find_kld_path(solib, path, sizeof(path))) {
454 fd = open(path, o_flags, 0);
456 *temp_pathname = xstrdup(path);
461 kld_new_objfile (struct objfile *objfile)
464 if (!have_partial_symbols())
468 * Compute offsets of relevant members in struct linker_file
469 * and the addresses of global variables. Don't warn about
470 * kernels that don't have 'pathname' in the linker_file
471 * struct since 6.x kernels don't have it.
473 off_address = kgdb_parse("&((struct linker_file *)0)->address");
474 off_filename = kgdb_parse("&((struct linker_file *)0)->filename");
475 off_pathname = kgdb_parse_quiet("&((struct linker_file *)0)->pathname");
476 off_next = kgdb_parse("&((struct linker_file *)0)->link.tqe_next");
477 module_path_addr = kgdb_parse("linker_path");
478 linker_files_addr = kgdb_parse("&linker_files.tqh_first");
479 kernel_file_addr = kgdb_parse("&linker_kernel_file");
483 load_klds_stub (void *arg)
486 solib_add(NULL, 1, ¤t_target, auto_solib_add);
493 /* XXX hack, needs to go into an abi init function */
494 set_solib_ops(get_current_arch(), &kld_so_ops);
496 kld_new_objfile(NULL);
497 catch_errors(load_klds_stub, NULL, NULL, RETURN_MASK_ALL);
498 observer_attach_new_objfile(kld_new_objfile);
502 initialize_kld_target(void)
504 struct cmd_list_element *c;
506 kld_so_ops.relocate_section_addresses = kld_relocate_section_addresses;
507 kld_so_ops.free_so = kld_free_so;
508 kld_so_ops.clear_solib = kld_clear_solib;
509 kld_so_ops.solib_create_inferior_hook = kld_solib_create_inferior_hook;
510 kld_so_ops.special_symbol_handling = kld_special_symbol_handling;
511 kld_so_ops.current_sos = kld_current_sos;
512 kld_so_ops.open_symbol_file_object = kld_open_symbol_file_object;
513 kld_so_ops.in_dynsym_resolve_code = kld_in_dynsym_resolve_code;
514 kld_so_ops.find_and_open_solib = kld_find_and_open_solib;
515 kld_so_ops.bfd_open = solib_bfd_open;
517 c = add_com("add-kld", class_files, kgdb_add_kld_cmd,
518 "Usage: add-kld FILE\n\
519 Load the symbols from the kernel loadable module FILE.");
520 set_cmd_completer(c, filename_completer);