2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2006 The FreeBSD Project
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/types.h>
33 #include <sys/param.h>
34 #include <sys/queue.h> /* for <sys/linker.h> with _KERNEL defined */
45 #include <sys/linker.h>
50 /* Name of the head of the linker file list in /sys/kern/kern_linker.c */
51 #define LINKER_HEAD "linker_files"
54 * Get the list of linker files using kvm(3).
55 * Can work with a live kernel as well as with a crash dump.
58 asf_kvm(const char *kernfile, const char *corefile)
60 char errbuf[LINE_MAX];
64 struct linker_file lf;
65 linker_file_list_t linker_files;
69 kd = kvm_openfiles(kernfile, corefile, NULL, O_RDONLY, errbuf);
71 errx(2, "open kernel memory: %s", errbuf);
74 * Locate the head of the linker file list using kernel symbols.
76 strcpy(name, LINKER_HEAD);
77 nl[0].n_name = name; /* can't use LINKER_HEAD here because it's const */
78 nl[1].n_name = NULL; /* terminate the array for kvm_nlist() */
79 switch (kvm_nlist(kd, nl)) {
83 warnx("%s: %s", LINKER_HEAD, kvm_geterr(kd));
88 errx(2, "%s: symbol not found", LINKER_HEAD);
92 * Read the head of the linker file list from kernel memory.
94 n = kvm_read(kd, nl[0].n_value, &linker_files, sizeof(linker_files));
97 if (n != sizeof(linker_files)) {
99 errx(2, "%s: short read", LINKER_HEAD);
103 * Traverse the linker file list starting at its head.
105 for (kp = linker_files.tqh_first; kp; kp = lf.link.tqe_next) {
106 /* Read a linker file structure */
107 n = kvm_read(kd, (u_long)kp, &lf, sizeof(lf));
110 if (n != sizeof(lf)) {
112 errx(2, "kvm: short read");
114 /* Read the name of the file stored separately */
115 bzero(name, sizeof(name));
116 n = kvm_read(kd, (u_long)lf.filename, name, sizeof(name) - 1);
119 if (strcmp(name, KERNFILE) == 0)
121 /* Add this file to our list of linker files */
122 kfile_add(name, lf.address);
127 read_err: /* A common error case */
128 warnx("read kernel memory: %s", kvm_geterr(kd));