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.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
38 #include <completer.h>
41 #include <frame-unwind.h>
51 CORE_ADDR base_address;
54 /* Offsets of fields in linker_file structure. */
55 static CORE_ADDR off_address, off_filename, off_pathname, off_next;
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;
62 static struct target_so_ops kld_so_ops;
69 if (stat(path, &sb) == 0 && S_ISREG(sb.st_mode))
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)
79 static const char *kld_suffixes[] = {
86 check_kld_path (char *path, size_t path_size)
91 ep = path + strlen(path);
92 suffix = kld_suffixes;
93 while (*suffix != NULL) {
94 if (strlcat(path, *suffix, path_size) < path_size) {
99 /* Restore original path to remove suffix. */
107 * Try to find the path for a kld by looking in the kernel's directory and
108 * in the various paths in the module path.
111 find_kld_path (char *filename, char *path, size_t path_size)
114 char *kernel_dir, *module_dir, *cp;
118 kernel_dir = dirname(bfd_get_filename(exec_bfd));
119 if (kernel_dir != NULL) {
120 snprintf(path, path_size, "%s/%s", kernel_dir,
122 if (check_kld_path(path, path_size))
126 if (module_path_addr != 0) {
127 target_read_string(module_path_addr, &module_path, PATH_MAX,
130 make_cleanup(xfree, module_path);
132 while ((module_dir = strsep(&cp, ";")) != NULL) {
133 snprintf(path, path_size, "%s/%s", module_dir,
135 if (check_kld_path(path, path_size))
144 * Read a kernel pointer given a KVA in 'address'.
147 read_pointer (CORE_ADDR address)
151 if (target_read_memory(address, (char *)&value, TARGET_PTR_BIT / 8) !=
154 return (extract_unsigned_integer(&value, TARGET_PTR_BIT / 8));
158 * Try to find this kld in the kernel linker's list of linker files.
161 find_kld_address (char *arg, CORE_ADDR *address)
168 if (linker_files_addr == 0 || off_address == 0 || off_filename == 0 ||
172 filename = basename(arg);
173 for (kld = read_pointer(linker_files_addr); kld != 0;
174 kld = read_pointer(kld + off_next)) {
175 /* Try to read this linker file's filename. */
176 target_read_string(read_pointer(kld + off_filename),
177 &kld_filename, PATH_MAX, &error);
181 /* Compare this kld's filename against our passed in name. */
182 if (strcmp(kld_filename, filename) != 0) {
189 * We found a match, use its address as the base
190 * address if we can read it.
192 *address = read_pointer(kld + off_address);
201 adjust_section_address (struct section_table *sec, CORE_ADDR *curr_base)
203 struct bfd_section *asect = sec->the_bfd_section;
204 bfd *abfd = sec->bfd;
206 if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0) {
207 sec->addr += *curr_base;
208 sec->endaddr += *curr_base;
212 *curr_base = align_power(*curr_base,
213 bfd_get_section_alignment(abfd, asect));
214 sec->addr = *curr_base;
215 sec->endaddr = sec->addr + bfd_section_size(abfd, asect);
216 *curr_base = sec->endaddr;
220 load_kld (char *path, CORE_ADDR base_addr, int from_tty)
222 struct section_addr_info *sap;
223 struct section_table *sections = NULL, *sections_end = NULL, *s;
224 struct cleanup *cleanup;
230 bfd = bfd_openr(path, gnutarget);
232 error("\"%s\": can't open: %s", path,
233 bfd_errmsg(bfd_get_error()));
234 cleanup = make_cleanup_bfd_close(bfd);
236 if (!bfd_check_format(bfd, bfd_object))
237 error("\%s\": not an object file", path);
239 /* Make sure we have a .text section. */
240 if (bfd_get_section_by_name (bfd, ".text") == NULL)
241 error("\"%s\": can't find text section", path);
243 /* Build a section table from the bfd and relocate the sections. */
244 if (build_section_table (bfd, §ions, §ions_end))
245 error("\"%s\": can't find file sections", path);
246 cleanup = make_cleanup(xfree, sections);
247 curr_addr = base_addr;
248 for (s = sections; s < sections_end; s++)
249 adjust_section_address(s, &curr_addr);
251 /* Build a section addr info to pass to symbol_file_add(). */
252 sap = build_section_addr_info_from_section_table (sections,
254 cleanup = make_cleanup((make_cleanup_ftype *)free_section_addr_info,
257 printf_unfiltered("add symbol table from file \"%s\" at\n", path);
258 for (i = 0; i < sap->num_sections; i++)
259 printf_unfiltered("\t%s_addr = %s\n", sap->other[i].name,
260 local_hex_string(sap->other[i].addr));
262 if (from_tty && (!query("%s", "")))
263 error("Not confirmed.");
265 symbol_file_add(path, from_tty, sap, 0, OBJF_USERLOADED);
267 do_cleanups(cleanup);
271 kgdb_add_kld_cmd (char *arg, int from_tty)
277 error("No kernel symbol file");
279 /* Try to open the raw path to handle absolute paths first. */
280 snprintf(path, sizeof(path), "%s", arg);
281 if (!check_kld_path(path, sizeof(path))) {
284 * If that didn't work, look in the various possible
285 * paths for the module.
287 if (!find_kld_path(arg, path, sizeof(path))) {
288 error("Unable to locate kld");
293 if (!find_kld_address(arg, &base_addr)) {
294 error("Unable to find kld in kernel");
298 load_kld(path, base_addr, from_tty);
300 reinit_frame_cache();
304 kld_relocate_section_addresses (struct so_list *so, struct section_table *sec)
306 static CORE_ADDR curr_addr;
308 if (sec == so->sections)
309 curr_addr = so->lm_info->base_address;
311 adjust_section_address(sec, &curr_addr);
315 kld_free_so (struct so_list *so)
322 kld_clear_solib (void)
327 kld_solib_create_inferior_hook (void)
332 kld_special_symbol_handling (void)
336 static struct so_list *
337 kld_current_sos (void)
339 struct so_list *head, **prev, *new;
340 CORE_ADDR kld, kernel;
344 if (linker_files_addr == 0 || kernel_file_addr == 0 ||
345 off_address == 0 || off_filename == 0 || off_next == 0)
352 * Walk the list of linker files creating so_list entries for
353 * each non-kernel file.
355 kernel = read_pointer(kernel_file_addr);
356 for (kld = read_pointer(linker_files_addr); kld != 0;
357 kld = read_pointer(kld + off_next)) {
358 /* Skip the main kernel file. */
362 new = xmalloc(sizeof(*new));
363 memset(new, 0, sizeof(*new));
365 new->lm_info = xmalloc(sizeof(*new->lm_info));
366 new->lm_info->base_address = 0;
368 /* Read the base filename and store it in so_original_name. */
369 target_read_string(read_pointer(kld + off_filename),
370 &path, sizeof(new->so_original_name), &error);
372 warning("kld_current_sos: Can't read filename: %s\n",
373 safe_strerror(error));
377 strlcpy(new->so_original_name, path,
378 sizeof(new->so_original_name));
382 * Try to read the pathname (if it exists) and store
385 if (off_pathname != 0) {
386 target_read_string(read_pointer(kld + off_pathname),
387 &path, sizeof(new->so_name), &error);
390 "kld_current_sos: Can't read pathname for \"%s\": %s\n",
391 new->so_original_name,
392 safe_strerror(error));
393 strlcpy(new->so_name, new->so_original_name,
394 sizeof(new->so_name));
396 strlcpy(new->so_name, path,
397 sizeof(new->so_name));
401 strlcpy(new->so_name, new->so_original_name,
402 sizeof(new->so_name));
404 /* Read this kld's base address. */
405 new->lm_info->base_address = read_pointer(kld + off_address);
406 if (new->lm_info->base_address == 0) {
408 "kld_current_sos: Invalid address for kld \"%s\"",
409 new->so_original_name);
414 /* Append to the list. */
423 kld_open_symbol_file_object (void *from_ttyp)
430 kld_in_dynsym_resolve_code (CORE_ADDR pc)
437 kld_find_and_open_solib (char *solib, unsigned o_flags, char **temp_pathname)
442 *temp_pathname = NULL;
443 if (!find_kld_path(solib, path, sizeof(path))) {
447 fd = open(path, o_flags, 0);
449 *temp_pathname = xstrdup(path);
454 kld_new_objfile (struct objfile *objfile)
457 if (!have_partial_symbols())
461 * Compute offsets of relevant members in struct linker_file
462 * and the addresses of global variables. Don't warn about
463 * kernels that don't have 'pathname' in the linker_file
464 * struct since 6.x kernels don't have it.
466 off_address = kgdb_parse("&((struct linker_file *)0)->address");
467 off_filename = kgdb_parse("&((struct linker_file *)0)->filename");
468 off_pathname = kgdb_parse_quiet("&((struct linker_file *)0)->pathname");
469 off_next = kgdb_parse("&((struct linker_file *)0)->link.tqe_next");
470 module_path_addr = kgdb_parse("linker_path");
471 linker_files_addr = kgdb_parse("&linker_files.tqh_first");
472 kernel_file_addr = kgdb_parse("&linker_kernel_file");
476 load_klds_stub (void *arg)
479 SOLIB_ADD(NULL, 1, ¤t_target, auto_solib_add);
487 catch_errors(load_klds_stub, NULL, NULL, RETURN_MASK_ALL);
491 initialize_kld_target(void)
493 struct cmd_list_element *c;
495 kld_so_ops.relocate_section_addresses = kld_relocate_section_addresses;
496 kld_so_ops.free_so = kld_free_so;
497 kld_so_ops.clear_solib = kld_clear_solib;
498 kld_so_ops.solib_create_inferior_hook = kld_solib_create_inferior_hook;
499 kld_so_ops.special_symbol_handling = kld_special_symbol_handling;
500 kld_so_ops.current_sos = kld_current_sos;
501 kld_so_ops.open_symbol_file_object = kld_open_symbol_file_object;
502 kld_so_ops.in_dynsym_resolve_code = kld_in_dynsym_resolve_code;
503 kld_so_ops.find_and_open_solib = kld_find_and_open_solib;
505 current_target_so_ops = &kld_so_ops;
507 c = add_com("add-kld", class_files, kgdb_add_kld_cmd,
508 "Usage: add-kld FILE\n\
509 Load the symbols from the kernel loadable module FILE.");
510 set_cmd_completer(c, filename_completer);