xref: /dragonfly/gnu/usr.bin/gdb/kgdb/kld.c (revision e5a92d33)
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  * $FreeBSD: src/gnu/usr.bin/gdb/kgdb/kld.c, svn 210424 2010/07/23 avg $
27  */
28 
29 #include <sys/param.h>
30 #include <sys/stat.h>
31 #include <fcntl.h>
32 #include <kvm.h>
33 #include <libgen.h>
34 
35 #include <defs.h>
36 #include <command.h>
37 #include <completer.h>
38 #include <environ.h>
39 #include <exec.h>
40 #include <frame-unwind.h>
41 #include <inferior.h>
42 #include <objfiles.h>
43 #include <gdbcore.h>
44 #include <language.h>
45 #include <solist.h>
46 #include <arch-utils.h>
47 #include <solib.h>
48 #include <exceptions.h>
49 #include <observer.h>
50 
51 #include "kgdb.h"
52 
53 struct lm_info {
54 	CORE_ADDR base_address;
55 };
56 
57 /* Offsets of fields in linker_file structure. */
58 static CORE_ADDR off_address, off_filename, off_pathname, off_next;
59 
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;
64 
65 static struct target_so_ops kld_so_ops;
66 
67 static int
68 kld_ok (char *path)
69 {
70 	struct stat sb;
71 
72 	if (stat(path, &sb) == 0 && S_ISREG(sb.st_mode))
73 		return (1);
74 	return (0);
75 }
76 
77 /*
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)
81  */
82 static const char *kld_suffixes[] = {
83 	".debug",
84 	"",
85 	NULL
86 };
87 
88 static int
89 check_kld_path (char *path, size_t path_size)
90 {
91 	const char **suffix;
92 	char *ep;
93 
94 	ep = path + strlen(path);
95 	suffix = kld_suffixes;
96 	while (*suffix != NULL) {
97 		if (strlcat(path, *suffix, path_size) < path_size) {
98 			if (kld_ok(path))
99 				return (1);
100 		}
101 
102 		/* Restore original path to remove suffix. */
103 		*ep = '\0';
104 		suffix++;
105 	}
106 	return (0);
107 }
108 
109 /*
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.
112  */
113 static int
114 find_kld_path (char *filename, char *path, size_t path_size)
115 {
116 	char *module_path;
117 	char *kernel_dir, *module_dir, *cp;
118 	int error;
119 
120 	if (exec_bfd) {
121 		kernel_dir = dirname(bfd_get_filename(exec_bfd));
122 		if (kernel_dir != NULL) {
123 			snprintf(path, path_size, "%s/%s", kernel_dir,
124 			    filename);
125 			if (check_kld_path(path, path_size))
126 				return (1);
127 		}
128 	}
129 	if (module_path_addr != 0) {
130 		target_read_string(module_path_addr, &module_path, PATH_MAX,
131 		    &error);
132 		if (error == 0) {
133 			make_cleanup(xfree, module_path);
134 			cp = module_path;
135 			while ((module_dir = strsep(&cp, ";")) != NULL) {
136 				snprintf(path, path_size, "%s/%s", module_dir,
137 				    filename);
138 				if (check_kld_path(path, path_size))
139 					return (1);
140 			}
141 		}
142 	}
143 	return (0);
144 }
145 
146 /*
147  * Read a kernel pointer given a KVA in 'address'.
148  */
149 static CORE_ADDR
150 read_pointer (CORE_ADDR address)
151 {
152 	struct gdbarch *arch = get_current_arch();
153 	enum bfd_endian byte_order = gdbarch_byte_order(arch);
154 	gdb_byte buf[sizeof(ULONGEST)];
155 	int ptrsz;
156 
157 	ptrsz = gdbarch_ptr_bit(arch) / 8;
158 
159 	if (target_read_memory(address, buf, ptrsz) != 0)
160 		return (0);
161 	return (extract_unsigned_integer(buf, ptrsz, byte_order));
162 }
163 
164 /*
165  * Try to find this kld in the kernel linker's list of linker files.
166  */
167 static int
168 find_kld_address (char *arg, CORE_ADDR *address)
169 {
170 	CORE_ADDR kld;
171 	char *kld_filename;
172 	char *filename;
173 	int error;
174 
175 	if (linker_files_addr == 0 || off_address == 0 || off_filename == 0 ||
176 	    off_next == 0)
177 		return (0);
178 
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);
185 		if (error)
186 			continue;
187 
188 		/* Compare this kld's filename against our passed in name. */
189 		if (strcmp(kld_filename, filename) != 0) {
190 			xfree(kld_filename);
191 			continue;
192 		}
193 		xfree(kld_filename);
194 
195 		/*
196 		 * We found a match, use its address as the base
197 		 * address if we can read it.
198 		 */
199 		*address = read_pointer(kld + off_address);
200 		if (*address == 0)
201 			return (0);
202 		return (1);
203 	}
204 	return (0);
205 }
206 
207 static void
208 adjust_section_address (struct target_section *sec, CORE_ADDR *curr_base)
209 {
210 	struct bfd_section *asect = sec->the_bfd_section;
211 	bfd *abfd = sec->bfd;
212 
213 	if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0) {
214 		sec->addr += *curr_base;
215 		sec->endaddr += *curr_base;
216 		return;
217 	}
218 
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;
224 }
225 
226 static void
227 load_kld (char *path, CORE_ADDR base_addr, int from_tty)
228 {
229 	struct section_addr_info *sap;
230 	struct target_section *sections = NULL, *sections_end = NULL, *s;
231 	struct cleanup *cleanup;
232 	bfd *bfd;
233 	CORE_ADDR curr_addr;
234 	int i;
235 
236 	/* Open the kld. */
237 	bfd = bfd_openr(path, gnutarget);
238 	if (bfd == NULL)
239 		error("\"%s\": can't open: %s", path,
240 		    bfd_errmsg(bfd_get_error()));
241 	cleanup = make_cleanup_bfd_unref(bfd);
242 
243 	if (!bfd_check_format(bfd, bfd_object))
244 		error("\"%s\": not an object file", path);
245 
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);
249 
250 	/* Build a section table from the bfd and relocate the sections. */
251 	if (build_section_table (bfd, &sections, &sections_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);
257 
258 	/* Build a section addr info to pass to symbol_file_add(). */
259 	sap = build_section_addr_info_from_section_table (sections,
260 	    sections_end);
261 	cleanup = make_cleanup((make_cleanup_ftype *)free_section_addr_info,
262 	    sap);
263 
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));
268 
269 	if (from_tty && (!query("%s", "")))
270 		error("Not confirmed.");
271 
272 	symbol_file_add(path, from_tty ? SYMFILE_VERBOSE : 0, sap, OBJF_USERLOADED);
273 
274 	do_cleanups(cleanup);
275 }
276 
277 static void
278 kgdb_add_kld_cmd (char *arg, int from_tty)
279 {
280 	char path[PATH_MAX];
281 	CORE_ADDR base_addr;
282 
283 	if (!exec_bfd)
284 		error("No kernel symbol file");
285 
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))) {
289 
290 		/*
291 		 * If that didn't work, look in the various possible
292 		 * paths for the module.
293 		 */
294 		if (!find_kld_path(arg, path, sizeof(path))) {
295 			error("Unable to locate kld");
296 			return;
297 		}
298 	}
299 
300 	if (!find_kld_address(arg, &base_addr)) {
301 		error("Unable to find kld in kernel");
302 		return;
303 	}
304 
305 	load_kld(path, base_addr, from_tty);
306 
307 	reinit_frame_cache();
308 }
309 
310 static void
311 kld_relocate_section_addresses (struct so_list *so, struct target_section *sec)
312 {
313 	static CORE_ADDR curr_addr;
314 
315 	if (sec == so->sections)
316 		curr_addr = so->lm_info->base_address;
317 
318 	adjust_section_address(sec, &curr_addr);
319 }
320 
321 static void
322 kld_free_so (struct so_list *so)
323 {
324 
325 	xfree(so->lm_info);
326 }
327 
328 static void
329 kld_clear_solib (void)
330 {
331 }
332 
333 static void
334 kld_solib_create_inferior_hook (int from_tty)
335 {
336 }
337 
338 static void
339 kld_special_symbol_handling (void)
340 {
341 }
342 
343 static struct so_list *
344 kld_current_sos (void)
345 {
346 	struct so_list *head, **prev, *new;
347 	CORE_ADDR kld, kernel;
348 	char *path;
349 	int error;
350 
351 	if (linker_files_addr == 0 || kernel_file_addr == 0 ||
352 	    off_address == 0 || off_filename == 0 || off_next == 0)
353 		return (NULL);
354 
355 	head = NULL;
356 	prev = &head;
357 
358 	/*
359 	 * Walk the list of linker files creating so_list entries for
360 	 * each non-kernel file.
361 	 */
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. */
366 		if (kld == kernel)
367 			continue;
368 
369 		new = xmalloc(sizeof(*new));
370 		memset(new, 0, sizeof(*new));
371 
372 		new->lm_info = xmalloc(sizeof(*new->lm_info));
373 		new->lm_info->base_address = 0;
374 
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);
378 		if (error != 0) {
379 			warning("kld_current_sos: Can't read filename: %s\n",
380 			    safe_strerror(error));
381 			free_so(new);
382 			continue;
383 		}
384 		strlcpy(new->so_original_name, path,
385 		    sizeof(new->so_original_name));
386 		xfree(path);
387 
388 		/*
389 		 * Try to read the pathname (if it exists) and store
390 		 * it in so_name.
391 		 */
392 		if (off_pathname != 0) {
393 			target_read_string(read_pointer(kld + off_pathname),
394 			    &path, sizeof(new->so_name), &error);
395 			if (error != 0) {
396 				warning(
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));
402 			} else {
403 				strlcpy(new->so_name, path,
404 				    sizeof(new->so_name));
405 				xfree(path);
406 			}
407 		} else
408 			strlcpy(new->so_name, new->so_original_name,
409 			    sizeof(new->so_name));
410 
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) {
414 			warning(
415 			    "kld_current_sos: Invalid address for kld \"%s\"",
416 			    new->so_original_name);
417 			free_so(new);
418 			continue;
419 		}
420 
421 		/* Append to the list. */
422 		*prev = new;
423 		prev = &new->next;
424 	}
425 
426 	return (head);
427 }
428 
429 static int
430 kld_open_symbol_file_object (void *from_ttyp)
431 {
432 
433 	return (0);
434 }
435 
436 static int
437 kld_in_dynsym_resolve_code (CORE_ADDR pc)
438 {
439 
440 	return (0);
441 }
442 
443 static int
444 kld_find_and_open_solib (char *solib, unsigned o_flags, char **temp_pathname)
445 {
446 	char path[PATH_MAX];
447 	int fd;
448 
449 	*temp_pathname = NULL;
450 	if (!find_kld_path(solib, path, sizeof(path))) {
451 		errno = ENOENT;
452 		return (-1);
453 	}
454 	fd = open(path, o_flags, 0);
455 	if (fd >= 0)
456 		*temp_pathname = xstrdup(path);
457 	return (fd);
458 }
459 
460 void
461 kld_new_objfile (struct objfile *objfile)
462 {
463 
464 	if (!have_partial_symbols())
465 		return;
466 
467 	/*
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.
472 	 */
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");
480 }
481 
482 static int
483 load_klds_stub (void *arg)
484 {
485 
486 	solib_add(NULL, 1, &current_target, auto_solib_add);
487 	return (0);
488 }
489 
490 void
491 kld_init (struct gdbarch *kgdbarch)
492 {
493 	/* XXX hack, needs to go into an abi init function */
494 	set_solib_ops(kgdbarch, &kld_so_ops);
495 
496 	kld_new_objfile(NULL);
497 	catch_errors(load_klds_stub, NULL, NULL, RETURN_MASK_ALL);
498 	observer_attach_new_objfile(kld_new_objfile);
499 }
500 
501 void
502 initialize_kld_target(void)
503 {
504 	struct cmd_list_element *c;
505 
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;
516 
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);
521 }
522