2001-09-11 01:13:15 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2000, Boris Popov
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by Boris Popov.
|
|
|
|
* 4. Neither the name of the author nor the names of any co-contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* $FreeBSD$
|
|
|
|
*/
|
|
|
|
|
2004-04-30 00:20:58 +00:00
|
|
|
#include <sys/types.h>
|
2001-09-11 01:13:15 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/exec.h>
|
|
|
|
#include <sys/queue.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/reboot.h>
|
|
|
|
#include <sys/linker.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/module.h>
|
|
|
|
#define FREEBSD_ELF
|
|
|
|
#include <link.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <fts.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <machine/elf.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include "ef.h"
|
|
|
|
|
|
|
|
#define MAXRECSIZE 1024
|
|
|
|
#define check(val) if ((error = (val)) != 0) break
|
|
|
|
|
|
|
|
#ifndef min
|
|
|
|
#define min(a,b) (((a)<(b)) ? (a) : (b))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
struct mod_info {
|
|
|
|
char* mi_name;
|
|
|
|
int mi_ver;
|
|
|
|
SLIST_ENTRY(mod_info) mi_next;
|
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef notnow
|
|
|
|
struct kld_info {
|
|
|
|
char* k_filename;
|
|
|
|
SLIST_HEAD(mod_list_head, mod_info) k_modules;
|
|
|
|
SLIST_ENTRY(kld_info) k_next;
|
|
|
|
};
|
|
|
|
|
|
|
|
SLIST_HEAD(kld_list_head, kld_info) kldlist;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static int dflag, verbose;
|
|
|
|
|
|
|
|
FILE *fxref;
|
|
|
|
|
2001-12-09 07:22:26 +00:00
|
|
|
static const char *xref_file = "linker.hints";
|
2001-09-11 01:13:15 +00:00
|
|
|
|
|
|
|
static char recbuf[MAXRECSIZE];
|
|
|
|
static int recpos, reccnt;
|
|
|
|
|
2006-07-29 19:39:03 +00:00
|
|
|
FILE *maketempfile(char *, const char *);
|
2001-09-11 01:13:15 +00:00
|
|
|
static void usage(void);
|
|
|
|
|
|
|
|
static void
|
|
|
|
intalign(void)
|
|
|
|
{
|
|
|
|
recpos = (recpos + sizeof(int) - 1) & ~(sizeof(int) - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
record_start(void)
|
|
|
|
{
|
|
|
|
recpos = 0;
|
|
|
|
memset(recbuf, 0, MAXRECSIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
record_end(void)
|
|
|
|
{
|
|
|
|
if (dflag || recpos == 0)
|
|
|
|
return 0;
|
|
|
|
reccnt++;
|
|
|
|
intalign();
|
|
|
|
fwrite(&recpos, sizeof(recpos), 1, fxref);
|
|
|
|
return fwrite(recbuf, recpos, 1, fxref) != 1 ? errno : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
record_buf(const void *buf, int size)
|
|
|
|
{
|
|
|
|
if (MAXRECSIZE - recpos < size)
|
|
|
|
errx(1, "record buffer overflow");
|
|
|
|
memcpy(recbuf + recpos, buf, size);
|
|
|
|
recpos += size;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
record_int(int val)
|
|
|
|
{
|
|
|
|
intalign();
|
|
|
|
return record_buf(&val, sizeof(val));
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
record_byte(u_char val)
|
|
|
|
{
|
|
|
|
return record_buf(&val, sizeof(val));
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
record_string(const char *str)
|
|
|
|
{
|
|
|
|
int len = strlen(str);
|
|
|
|
int error;
|
|
|
|
|
|
|
|
if (dflag)
|
|
|
|
return 0;
|
|
|
|
error = record_byte(len);
|
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
return record_buf(str, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_entry(struct mod_metadata *md, const char *cval,
|
|
|
|
struct elf_file *ef, const char *kldname)
|
|
|
|
{
|
|
|
|
struct mod_depend mdp;
|
|
|
|
struct mod_version mdv;
|
|
|
|
Elf_Off data = (Elf_Off)md->md_data;
|
|
|
|
int error = 0;
|
|
|
|
|
|
|
|
record_start();
|
|
|
|
switch (md->md_type) {
|
|
|
|
case MDT_DEPEND:
|
|
|
|
if (!dflag)
|
|
|
|
break;
|
2004-08-27 00:51:21 +00:00
|
|
|
check(EF_SEG_READ(ef, data, sizeof(mdp), &mdp));
|
2001-09-11 01:13:15 +00:00
|
|
|
printf(" depends on %s.%d (%d,%d)\n", cval,
|
|
|
|
mdp.md_ver_preferred, mdp.md_ver_minimum, mdp.md_ver_maximum);
|
|
|
|
break;
|
|
|
|
case MDT_VERSION:
|
2004-08-27 00:51:21 +00:00
|
|
|
check(EF_SEG_READ(ef, data, sizeof(mdv), &mdv));
|
2001-09-11 01:13:15 +00:00
|
|
|
record_int(MDT_VERSION);
|
|
|
|
record_string(cval);
|
|
|
|
record_int(mdv.mv_version);
|
|
|
|
record_string(kldname);
|
|
|
|
if (!dflag)
|
|
|
|
break;
|
|
|
|
printf(" interface %s.%d\n", cval, mdv.mv_version);
|
|
|
|
break;
|
|
|
|
case MDT_MODULE:
|
|
|
|
record_int(MDT_MODULE);
|
|
|
|
record_string(cval);
|
|
|
|
record_string(kldname);
|
|
|
|
if (!dflag)
|
|
|
|
break;
|
|
|
|
printf(" module %s\n", cval);
|
|
|
|
break;
|
|
|
|
default:
|
2005-11-11 08:13:18 +00:00
|
|
|
warnx("unknown metadata record %d in file %s", md->md_type, kldname);
|
2001-09-11 01:13:15 +00:00
|
|
|
}
|
|
|
|
if (!error)
|
|
|
|
record_end();
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
read_kld(char *filename, char *kldname)
|
|
|
|
{
|
|
|
|
struct mod_metadata md;
|
|
|
|
struct elf_file ef;
|
|
|
|
/* struct kld_info *kip;
|
|
|
|
struct mod_info *mip;*/
|
|
|
|
void **p, **orgp;
|
2004-08-27 00:51:21 +00:00
|
|
|
int error, eftype, nmlen;
|
2001-09-11 01:13:15 +00:00
|
|
|
long start, finish, entries;
|
|
|
|
char kldmodname[MAXMODNAME + 1], cval[MAXMODNAME + 1], *cp;
|
|
|
|
|
|
|
|
if (verbose || dflag)
|
|
|
|
printf("%s\n", filename);
|
|
|
|
error = ef_open(filename, &ef, verbose);
|
2004-08-27 00:51:21 +00:00
|
|
|
if (error) {
|
2004-08-27 00:58:40 +00:00
|
|
|
error = ef_obj_open(filename, &ef, verbose);
|
|
|
|
if (error) {
|
|
|
|
if (verbose)
|
|
|
|
warnc(error, "elf_open(%s)", filename);
|
|
|
|
return error;
|
|
|
|
}
|
2004-08-27 00:51:21 +00:00
|
|
|
}
|
|
|
|
eftype = EF_GET_TYPE(&ef);
|
|
|
|
if (eftype != EFT_KLD && eftype != EFT_KERNEL) {
|
|
|
|
EF_CLOSE(&ef);
|
2001-09-11 01:13:15 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (!dflag) {
|
|
|
|
cp = strrchr(kldname, '.');
|
|
|
|
nmlen = cp ? min(MAXMODNAME, cp - kldname) :
|
|
|
|
min(MAXMODNAME, strlen(kldname));
|
2006-08-05 18:22:11 +00:00
|
|
|
strlcpy(kldmodname, kldname, nmlen);
|
2001-09-11 01:13:15 +00:00
|
|
|
/* fprintf(fxref, "%s:%s:%d\n", kldmodname, kldname, 0);*/
|
|
|
|
}
|
|
|
|
do {
|
2004-08-27 00:51:21 +00:00
|
|
|
check(EF_LOOKUP_SET(&ef, MDT_SETNAME, &start, &finish,
|
|
|
|
&entries));
|
|
|
|
check(EF_SEG_READ_ENTRY_REL(&ef, start, sizeof(*p) * entries,
|
2004-03-15 17:43:36 +00:00
|
|
|
(void *)&p));
|
2001-09-11 01:13:15 +00:00
|
|
|
orgp = p;
|
|
|
|
while(entries--) {
|
2004-08-27 00:51:21 +00:00
|
|
|
check(EF_SEG_READ_REL(&ef, (Elf_Off)*p, sizeof(md),
|
|
|
|
&md));
|
2001-09-11 01:13:15 +00:00
|
|
|
p++;
|
2004-08-27 00:51:21 +00:00
|
|
|
check(EF_SEG_READ(&ef, (Elf_Off)md.md_cval,
|
|
|
|
sizeof(cval), cval));
|
2001-09-11 01:13:15 +00:00
|
|
|
cval[MAXMODNAME] = '\0';
|
|
|
|
parse_entry(&md, cval, &ef, kldname);
|
|
|
|
}
|
|
|
|
if (error)
|
|
|
|
warnc(error, "error while reading %s", filename);
|
|
|
|
free(orgp);
|
|
|
|
} while(0);
|
2004-08-27 00:51:21 +00:00
|
|
|
EF_CLOSE(&ef);
|
2001-09-11 01:13:15 +00:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2006-07-29 19:39:03 +00:00
|
|
|
FILE *
|
2001-09-11 01:13:15 +00:00
|
|
|
maketempfile(char *dest, const char *root)
|
|
|
|
{
|
|
|
|
char *p;
|
2006-07-29 19:39:03 +00:00
|
|
|
int fd;
|
2001-09-11 01:13:15 +00:00
|
|
|
|
2006-08-05 18:22:11 +00:00
|
|
|
strlcpy(dest, root, MAXPATHLEN);
|
2001-09-11 01:13:15 +00:00
|
|
|
|
|
|
|
if ((p = strrchr(dest, '/')) != 0)
|
|
|
|
p++;
|
|
|
|
else
|
|
|
|
p = dest;
|
|
|
|
strcpy(p, "lhint.XXXXXX");
|
2006-07-29 19:39:03 +00:00
|
|
|
fd = mkstemp(dest);
|
2008-11-30 14:20:08 +00:00
|
|
|
if (fd >= 0)
|
|
|
|
fchmod(fd, 0644); /* nothing secret in the file */
|
2006-07-29 19:39:03 +00:00
|
|
|
return ((fd == -1) ? NULL : fdopen(fd, "w+"));
|
2001-09-11 01:13:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static char xrefname[MAXPATHLEN], tempname[MAXPATHLEN];
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
FTS *ftsp;
|
|
|
|
FTSENT *p;
|
|
|
|
int opt, fts_options, ival;
|
2004-04-30 00:20:58 +00:00
|
|
|
struct stat sb;
|
2001-09-11 01:13:15 +00:00
|
|
|
|
|
|
|
fts_options = FTS_PHYSICAL;
|
|
|
|
/* SLIST_INIT(&kldlist);*/
|
|
|
|
|
|
|
|
while ((opt = getopt(argc, argv, "Rdf:v")) != -1) {
|
|
|
|
switch (opt) {
|
|
|
|
case 'd':
|
|
|
|
dflag = 1;
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
xref_file = optarg;
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
verbose++;
|
|
|
|
break;
|
|
|
|
case 'R':
|
|
|
|
fts_options |= FTS_COMFOLLOW;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (argc - optind < 1)
|
|
|
|
usage();
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
2004-04-30 00:20:58 +00:00
|
|
|
if (stat(argv[0], &sb) != 0)
|
|
|
|
err(1, "%s", argv[0]);
|
|
|
|
if ((sb.st_mode & S_IFDIR) == 0) {
|
|
|
|
errno = ENOTDIR;
|
|
|
|
err(1, "%s", argv[0]);
|
|
|
|
}
|
|
|
|
|
2001-09-11 01:13:15 +00:00
|
|
|
ftsp = fts_open(argv, fts_options, 0);
|
|
|
|
if (ftsp == NULL)
|
|
|
|
exit(1);
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
p = fts_read(ftsp);
|
|
|
|
if ((p == NULL || p->fts_info == FTS_D) && !dflag && fxref) {
|
|
|
|
fclose(fxref);
|
2006-07-29 19:39:03 +00:00
|
|
|
fxref = NULL;
|
2001-09-11 01:13:15 +00:00
|
|
|
if (reccnt) {
|
|
|
|
rename(tempname, xrefname);
|
|
|
|
} else {
|
|
|
|
unlink(tempname);
|
|
|
|
unlink(xrefname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (p == NULL)
|
|
|
|
break;
|
|
|
|
if (p && p->fts_info == FTS_D && !dflag) {
|
|
|
|
snprintf(xrefname, sizeof(xrefname), "%s/%s",
|
|
|
|
ftsp->fts_path, xref_file);
|
2006-07-29 19:39:03 +00:00
|
|
|
fxref = maketempfile(tempname, ftsp->fts_path);
|
2001-09-11 01:13:15 +00:00
|
|
|
if (fxref == NULL)
|
|
|
|
err(1, "can't create %s", tempname);
|
|
|
|
ival = 1;
|
|
|
|
fwrite(&ival, sizeof(ival), 1, fxref);
|
|
|
|
reccnt = 0;
|
|
|
|
}
|
|
|
|
if (p->fts_info != FTS_F)
|
|
|
|
continue;
|
2005-11-11 08:13:18 +00:00
|
|
|
if (p->fts_namelen >= 8 &&
|
|
|
|
strcmp(p->fts_name + p->fts_namelen - 8, ".symbols") == 0)
|
|
|
|
continue;
|
2001-09-11 01:13:15 +00:00
|
|
|
read_kld(p->fts_path, p->fts_name);
|
|
|
|
}
|
|
|
|
fts_close(ftsp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
fprintf(stderr, "%s\n",
|
2004-06-01 09:34:04 +00:00
|
|
|
"usage: kldxref [-Rdv] [-f hintsfile] path ..."
|
2001-09-11 01:13:15 +00:00
|
|
|
);
|
|
|
|
exit(1);
|
|
|
|
}
|