kld.c revision 248836
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: head/gnu/usr.bin/gdb/kgdb/kld.c 248836 2013-03-28 17:04:59Z will $");
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
50struct lm_info {
51	CORE_ADDR base_address;
52};
53
54/* Offsets of fields in linker_file structure. */
55static CORE_ADDR off_address, off_filename, off_pathname, off_next;
56
57/* KVA of 'linker_path' which corresponds to the kern.module_path sysctl .*/
58static CORE_ADDR module_path_addr;
59static CORE_ADDR linker_files_addr;
60static CORE_ADDR kernel_file_addr;
61
62static struct target_so_ops kld_so_ops;
63
64static int
65kld_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 */
79static const char *kld_suffixes[] = {
80	".debug",
81	"",
82	NULL
83};
84
85static int
86check_kld_path (char *path, size_t path_size)
87{
88	const char **suffix;
89	char *ep;
90
91	ep = path + strlen(path);
92	suffix = kld_suffixes;
93	while (*suffix != NULL) {
94		if (strlcat(path, *suffix, path_size) < path_size) {
95			if (kld_ok(path))
96				return (1);
97		}
98
99		/* Restore original path to remove suffix. */
100		*ep = '\0';
101		suffix++;
102	}
103	return (0);
104}
105
106/*
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.
109 */
110static int
111find_kld_path (char *filename, char *path, size_t path_size)
112{
113	char *module_path;
114	char *kernel_dir, *module_dir, *cp;
115	int error;
116
117	if (exec_bfd) {
118		kernel_dir = dirname(bfd_get_filename(exec_bfd));
119		if (kernel_dir != NULL) {
120			snprintf(path, path_size, "%s/%s", kernel_dir,
121			    filename);
122			if (check_kld_path(path, path_size))
123				return (1);
124		}
125	}
126	if (module_path_addr != 0) {
127		target_read_string(module_path_addr, &module_path, PATH_MAX,
128		    &error);
129		if (error == 0) {
130			make_cleanup(xfree, module_path);
131			cp = module_path;
132			while ((module_dir = strsep(&cp, ";")) != NULL) {
133				snprintf(path, path_size, "%s/%s", module_dir,
134				    filename);
135				if (check_kld_path(path, path_size))
136					return (1);
137			}
138		}
139	}
140	return (0);
141}
142
143/*
144 * Read a kernel pointer given a KVA in 'address'.
145 */
146static CORE_ADDR
147read_pointer (CORE_ADDR address)
148{
149	CORE_ADDR value;
150
151	if (target_read_memory(address, (char *)&value, TARGET_PTR_BIT / 8) !=
152	    0)
153		return (0);
154	return (extract_unsigned_integer(&value, TARGET_PTR_BIT / 8));
155}
156
157/*
158 * Try to find this kld in the kernel linker's list of linker files.
159 */
160static int
161find_kld_address (char *arg, CORE_ADDR *address)
162{
163	CORE_ADDR kld;
164	char *kld_filename;
165	char *filename;
166	int error;
167
168	if (linker_files_addr == 0 || off_address == 0 || off_filename == 0 ||
169	    off_next == 0)
170		return (0);
171
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);
178		if (error)
179			continue;
180
181		/* Compare this kld's filename against our passed in name. */
182		if (strcmp(kld_filename, filename) != 0) {
183			xfree(kld_filename);
184			continue;
185		}
186		xfree(kld_filename);
187
188		/*
189		 * We found a match, use its address as the base
190		 * address if we can read it.
191		 */
192		*address = read_pointer(kld + off_address);
193		if (*address == 0)
194			return (0);
195		return (1);
196	}
197	return (0);
198}
199
200static void
201adjust_section_address (struct section_table *sec, CORE_ADDR *curr_base)
202{
203	struct bfd_section *asect = sec->the_bfd_section;
204	bfd *abfd = sec->bfd;
205
206	if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0) {
207		sec->addr += *curr_base;
208		sec->endaddr += *curr_base;
209		return;
210	}
211
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;
217}
218
219static void
220load_kld (char *path, CORE_ADDR base_addr, int from_tty)
221{
222	struct section_addr_info *sap;
223	struct section_table *sections = NULL, *sections_end = NULL, *s;
224	struct cleanup *cleanup;
225	bfd *bfd;
226	CORE_ADDR curr_addr;
227	int i;
228
229	/* Open the kld. */
230	bfd = bfd_openr(path, gnutarget);
231	if (bfd == NULL)
232		error("\"%s\": can't open: %s", path,
233		    bfd_errmsg(bfd_get_error()));
234	cleanup = make_cleanup_bfd_close(bfd);
235
236	if (!bfd_check_format(bfd, bfd_object))
237		error("\%s\": not an object file", path);
238
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);
242
243	/* Build a section table from the bfd and relocate the sections. */
244	if (build_section_table (bfd, &sections, &sections_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);
250
251	/* Build a section addr info to pass to symbol_file_add(). */
252	sap = build_section_addr_info_from_section_table (sections,
253	    sections_end);
254	cleanup = make_cleanup((make_cleanup_ftype *)free_section_addr_info,
255	    sap);
256
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));
261
262	if (from_tty && (!query("%s", "")))
263		error("Not confirmed.");
264
265	symbol_file_add(path, from_tty, sap, 0, OBJF_USERLOADED);
266
267	do_cleanups(cleanup);
268}
269
270static void
271kgdb_add_kld_cmd (char *arg, int from_tty)
272{
273	char path[PATH_MAX];
274	CORE_ADDR base_addr;
275
276	if (!exec_bfd)
277		error("No kernel symbol file");
278
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))) {
282
283		/*
284		 * If that didn't work, look in the various possible
285		 * paths for the module.
286		 */
287		if (!find_kld_path(arg, path, sizeof(path))) {
288			error("Unable to locate kld");
289			return;
290		}
291	}
292
293	if (!find_kld_address(arg, &base_addr)) {
294		error("Unable to find kld in kernel");
295		return;
296	}
297
298	load_kld(path, base_addr, from_tty);
299
300	reinit_frame_cache();
301}
302
303static void
304kld_relocate_section_addresses (struct so_list *so, struct section_table *sec)
305{
306	static CORE_ADDR curr_addr;
307
308	if (sec == so->sections)
309		curr_addr = so->lm_info->base_address;
310
311	adjust_section_address(sec, &curr_addr);
312}
313
314static void
315kld_free_so (struct so_list *so)
316{
317
318	xfree(so->lm_info);
319}
320
321static void
322kld_clear_solib (void)
323{
324}
325
326static void
327kld_solib_create_inferior_hook (void)
328{
329}
330
331static void
332kld_special_symbol_handling (void)
333{
334}
335
336static struct so_list *
337kld_current_sos (void)
338{
339	struct so_list *head, **prev, *new;
340	CORE_ADDR kld, kernel;
341	char *path;
342	int error;
343
344	if (linker_files_addr == 0 || kernel_file_addr == 0 ||
345	    off_address == 0 || off_filename == 0 || off_next == 0)
346		return (NULL);
347
348	head = NULL;
349	prev = &head;
350
351	/*
352	 * Walk the list of linker files creating so_list entries for
353	 * each non-kernel file.
354	 */
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. */
359		if (kld == kernel)
360			continue;
361
362		new = xmalloc(sizeof(*new));
363		memset(new, 0, sizeof(*new));
364
365		new->lm_info = xmalloc(sizeof(*new->lm_info));
366		new->lm_info->base_address = 0;
367
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);
371		if (error != 0) {
372			warning("kld_current_sos: Can't read filename: %s\n",
373			    safe_strerror(error));
374			free_so(new);
375			continue;
376		}
377		strlcpy(new->so_original_name, path,
378		    sizeof(new->so_original_name));
379		xfree(path);
380
381		/*
382		 * Try to read the pathname (if it exists) and store
383		 * it in so_name.
384		 */
385		if (find_kld_path(new->so_original_name, new->so_name,
386		    sizeof(new->so_name))) {
387			/* we found the kld */;
388		} else if (off_pathname != 0) {
389			target_read_string(read_pointer(kld + off_pathname),
390			    &path, sizeof(new->so_name), &error);
391			if (error != 0) {
392				warning(
393		    "kld_current_sos: Can't read pathname for \"%s\": %s\n",
394				    new->so_original_name,
395				    safe_strerror(error));
396				strlcpy(new->so_name, new->so_original_name,
397				    sizeof(new->so_name));
398			} else {
399				strlcpy(new->so_name, path,
400				    sizeof(new->so_name));
401				xfree(path);
402			}
403		} else
404			strlcpy(new->so_name, new->so_original_name,
405			    sizeof(new->so_name));
406
407		/* Read this kld's base address. */
408		new->lm_info->base_address = read_pointer(kld + off_address);
409		if (new->lm_info->base_address == 0) {
410			warning(
411			    "kld_current_sos: Invalid address for kld \"%s\"",
412			    new->so_original_name);
413			free_so(new);
414			continue;
415		}
416
417		/* Append to the list. */
418		*prev = new;
419		prev = &new->next;
420	}
421
422	return (head);
423}
424
425static int
426kld_open_symbol_file_object (void *from_ttyp)
427{
428
429	return (0);
430}
431
432static int
433kld_in_dynsym_resolve_code (CORE_ADDR pc)
434{
435
436	return (0);
437}
438
439static int
440kld_find_and_open_solib (char *solib, unsigned o_flags, char **temp_pathname)
441{
442	char path[PATH_MAX];
443	int fd;
444
445	*temp_pathname = NULL;
446	if (!find_kld_path(solib, path, sizeof(path))) {
447		errno = ENOENT;
448		return (-1);
449	}
450	fd = open(path, o_flags, 0);
451	if (fd >= 0)
452		*temp_pathname = xstrdup(path);
453	return (fd);
454}
455
456void
457kld_new_objfile (struct objfile *objfile)
458{
459
460	if (!have_partial_symbols())
461		return;
462
463	/*
464	 * Compute offsets of relevant members in struct linker_file
465	 * and the addresses of global variables.  Don't warn about
466	 * kernels that don't have 'pathname' in the linker_file
467	 * struct since 6.x kernels don't have it.
468	 */
469	off_address = kgdb_parse("&((struct linker_file *)0)->address");
470	off_filename = kgdb_parse("&((struct linker_file *)0)->filename");
471	off_pathname = kgdb_parse_quiet("&((struct linker_file *)0)->pathname");
472	off_next = kgdb_parse("&((struct linker_file *)0)->link.tqe_next");
473	module_path_addr = kgdb_parse("linker_path");
474	linker_files_addr = kgdb_parse("&linker_files.tqh_first");
475	kernel_file_addr = kgdb_parse("&linker_kernel_file");
476}
477
478static int
479load_klds_stub (void *arg)
480{
481
482	SOLIB_ADD(NULL, 1, &current_target, auto_solib_add);
483	return (0);
484}
485
486void
487kld_init (void)
488{
489
490	catch_errors(load_klds_stub, NULL, NULL, RETURN_MASK_ALL);
491}
492
493void
494initialize_kld_target(void)
495{
496	struct cmd_list_element *c;
497
498	kld_so_ops.relocate_section_addresses = kld_relocate_section_addresses;
499	kld_so_ops.free_so = kld_free_so;
500	kld_so_ops.clear_solib = kld_clear_solib;
501	kld_so_ops.solib_create_inferior_hook = kld_solib_create_inferior_hook;
502	kld_so_ops.special_symbol_handling = kld_special_symbol_handling;
503	kld_so_ops.current_sos = kld_current_sos;
504	kld_so_ops.open_symbol_file_object = kld_open_symbol_file_object;
505	kld_so_ops.in_dynsym_resolve_code = kld_in_dynsym_resolve_code;
506	kld_so_ops.find_and_open_solib = kld_find_and_open_solib;
507
508	current_target_so_ops = &kld_so_ops;
509
510	c = add_com("add-kld", class_files, kgdb_add_kld_cmd,
511	   "Usage: add-kld FILE\n\
512Load the symbols from the kernel loadable module FILE.");
513	set_cmd_completer(c, filename_completer);
514}
515