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[] = {
87 check_kld_path (char *path, size_t path_size)
92 ep = path + strlen(path);
93 suffix = kld_suffixes;
94 while (*suffix != NULL) {
95 if (strlcat(path, *suffix, path_size) < path_size) {
100 /* Restore original path to remove suffix. */
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.
112 find_kld_path (char *filename, char *path, size_t path_size)
115 char *kernel_dir, *module_dir, *cp;
119 kernel_dir = dirname(bfd_get_filename(exec_bfd));
120 if (kernel_dir != NULL) {
121 snprintf(path, path_size, "%s/%s", kernel_dir,
123 if (check_kld_path(path, path_size))
127 if (module_path_addr != 0) {
128 target_read_string(module_path_addr, &module_path, PATH_MAX,
131 make_cleanup(xfree, module_path);
133 while ((module_dir = strsep(&cp, ";")) != NULL) {
134 snprintf(path, path_size, "%s/%s", module_dir,
136 if (check_kld_path(path, path_size))
145 * Read a kernel pointer given a KVA in 'address'.
148 read_pointer (CORE_ADDR address)
152 if (target_read_memory(address, (char *)&value, TARGET_PTR_BIT / 8) !=
155 return (extract_unsigned_integer(&value, TARGET_PTR_BIT / 8));
159 * Try to find this kld in the kernel linker's list of linker files.
162 find_kld_address (char *arg, CORE_ADDR *address)
169 if (linker_files_addr == 0 || off_address == 0 || off_filename == 0 ||
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);
182 /* Compare this kld's filename against our passed in name. */
183 if (strcmp(kld_filename, filename) != 0) {
190 * We found a match, use its address as the base
191 * address if we can read it.
193 *address = read_pointer(kld + off_address);
202 adjust_section_address (struct section_table *sec, CORE_ADDR *curr_base)
204 struct bfd_section *asect = sec->the_bfd_section;
205 bfd *abfd = sec->bfd;
207 if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0) {
208 sec->addr += *curr_base;
209 sec->endaddr += *curr_base;
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;
221 load_kld (char *path, CORE_ADDR base_addr, int from_tty)
223 struct section_addr_info *sap;
224 struct section_table *sections = NULL, *sections_end = NULL, *s;
225 struct cleanup *cleanup;
231 bfd = bfd_openr(path, gnutarget);
233 error("\"%s\": can't open: %s", path,
234 bfd_errmsg(bfd_get_error()));
235 cleanup = make_cleanup_bfd_close(bfd);
237 if (!bfd_check_format(bfd, bfd_object))
238 error("\%s\": not an object file", path);
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);
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);
252 /* Build a section addr info to pass to symbol_file_add(). */
253 sap = build_section_addr_info_from_section_table (sections,
255 cleanup = make_cleanup((make_cleanup_ftype *)free_section_addr_info,
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));
263 if (from_tty && (!query("%s", "")))
264 error("Not confirmed.");
266 symbol_file_add(path, from_tty, sap, 0, OBJF_USERLOADED);
268 do_cleanups(cleanup);
272 kgdb_add_kld_cmd (char *arg, int from_tty)
278 error("No kernel symbol file");
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))) {
285 * If that didn't work, look in the various possible
286 * paths for the module.
288 if (!find_kld_path(arg, path, sizeof(path))) {
289 error("Unable to locate kld");
294 if (!find_kld_address(arg, &base_addr)) {
295 error("Unable to find kld in kernel");
299 load_kld(path, base_addr, from_tty);
301 reinit_frame_cache();
305 kld_relocate_section_addresses (struct so_list *so, struct section_table *sec)
307 static CORE_ADDR curr_addr;
309 if (sec == so->sections)
310 curr_addr = so->lm_info->base_address;
312 adjust_section_address(sec, &curr_addr);
316 kld_free_so (struct so_list *so)
323 kld_clear_solib (void)
328 kld_solib_create_inferior_hook (void)
333 kld_special_symbol_handling (void)
337 static struct so_list *
338 kld_current_sos (void)
340 struct so_list *head, **prev, *new;
341 CORE_ADDR kld, kernel;
345 if (linker_files_addr == 0 || kernel_file_addr == 0 ||
346 off_address == 0 || off_filename == 0 || off_next == 0)
353 * Walk the list of linker files creating so_list entries for
354 * each non-kernel file.
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. */
363 new = xmalloc(sizeof(*new));
364 memset(new, 0, sizeof(*new));
366 new->lm_info = xmalloc(sizeof(*new->lm_info));
367 new->lm_info->base_address = 0;
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);
373 warning("kld_current_sos: Can't read filename: %s\n",
374 safe_strerror(error));
378 strlcpy(new->so_original_name, path,
379 sizeof(new->so_original_name));
383 * Try to read the pathname (if it exists) and store
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);
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));
400 strlcpy(new->so_name, path,
401 sizeof(new->so_name));
405 strlcpy(new->so_name, new->so_original_name,
406 sizeof(new->so_name));
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) {
412 "kld_current_sos: Invalid address for kld \"%s\"",
413 new->so_original_name);
418 /* Append to the list. */
427 kld_open_symbol_file_object (void *from_ttyp)
434 kld_in_dynsym_resolve_code (CORE_ADDR pc)
441 kld_find_and_open_solib (char *solib, unsigned o_flags, char **temp_pathname)
446 *temp_pathname = NULL;
447 if (!find_kld_path(solib, path, sizeof(path))) {
451 fd = open(path, o_flags, 0);
453 *temp_pathname = xstrdup(path);
458 kld_new_objfile (struct objfile *objfile)
461 if (!have_partial_symbols())
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.
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");
480 load_klds_stub (void *arg)
483 SOLIB_ADD(NULL, 1, ¤t_target, auto_solib_add);
491 catch_errors(load_klds_stub, NULL, NULL, RETURN_MASK_ALL);
495 initialize_kld_target(void)
497 struct cmd_list_element *c;
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;
509 current_target_so_ops = &kld_so_ops;
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);