2017-11-18 14:26:50 +00:00
|
|
|
/*-
|
|
|
|
* SPDX-License-Identifier: BSD-4-Clause
|
|
|
|
*
|
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>
|
2015-12-11 05:27:55 +00:00
|
|
|
#include <sys/endian.h>
|
2001-09-11 01:13:15 +00:00
|
|
|
#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
|
2018-01-17 19:59:43 +00:00
|
|
|
|
2018-09-26 19:41:06 +00:00
|
|
|
#include <ctype.h>
|
2001-09-11 01:13:15 +00:00
|
|
|
#include <err.h>
|
2018-01-17 19:59:43 +00:00
|
|
|
#include <errno.h>
|
2001-09-11 01:13:15 +00:00
|
|
|
#include <fts.h>
|
2018-01-17 19:59:43 +00:00
|
|
|
#include <stdbool.h>
|
2001-09-11 01:13:15 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2018-01-17 19:59:43 +00:00
|
|
|
#include <string.h>
|
2001-09-11 01:13:15 +00:00
|
|
|
#include <unistd.h>
|
2018-01-17 19:59:43 +00:00
|
|
|
#include <machine/elf.h>
|
2001-09-11 01:13:15 +00:00
|
|
|
|
|
|
|
#include "ef.h"
|
|
|
|
|
2015-12-11 05:27:55 +00:00
|
|
|
#define MAXRECSIZE (64 << 10) /* 64k */
|
2001-09-11 01:13:15 +00:00
|
|
|
#define check(val) if ((error = (val)) != 0) break
|
|
|
|
|
2018-01-17 19:59:43 +00:00
|
|
|
static bool dflag; /* do not create a hint file, only write on stdout */
|
2009-01-06 14:23:38 +00:00
|
|
|
static int verbose;
|
2001-09-11 01:13:15 +00:00
|
|
|
|
2009-01-06 14:23:38 +00:00
|
|
|
static FILE *fxref; /* current hints file */
|
2001-09-11 01:13:15 +00:00
|
|
|
|
2001-12-09 07:22:26 +00:00
|
|
|
static const char *xref_file = "linker.hints";
|
2001-09-11 01:13:15 +00:00
|
|
|
|
2009-01-06 14:23:38 +00:00
|
|
|
/*
|
|
|
|
* A record is stored in the static buffer recbuf before going to disk.
|
|
|
|
*/
|
2001-09-11 01:13:15 +00:00
|
|
|
static char recbuf[MAXRECSIZE];
|
2009-01-06 14:23:38 +00:00
|
|
|
static int recpos; /* current write position */
|
|
|
|
static int reccnt; /* total record written to this file so far */
|
2001-09-11 01:13:15 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
intalign(void)
|
|
|
|
{
|
2018-01-17 19:59:43 +00:00
|
|
|
|
2016-08-23 13:43:43 +00:00
|
|
|
recpos = roundup2(recpos, sizeof(int));
|
2001-09-11 01:13:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
record_start(void)
|
|
|
|
{
|
2018-01-17 19:59:43 +00:00
|
|
|
|
2001-09-11 01:13:15 +00:00
|
|
|
recpos = 0;
|
|
|
|
memset(recbuf, 0, MAXRECSIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
record_end(void)
|
|
|
|
{
|
2018-01-17 19:59:43 +00:00
|
|
|
|
2009-01-06 14:23:38 +00:00
|
|
|
if (recpos == 0)
|
2018-01-17 19:59:43 +00:00
|
|
|
return (0);
|
2001-09-11 01:13:15 +00:00
|
|
|
reccnt++;
|
|
|
|
intalign();
|
|
|
|
fwrite(&recpos, sizeof(recpos), 1, fxref);
|
2018-01-17 19:59:43 +00:00
|
|
|
return (fwrite(recbuf, recpos, 1, fxref) != 1 ? errno : 0);
|
2001-09-11 01:13:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2018-01-17 19:59:43 +00:00
|
|
|
record_buf(const void *buf, size_t size)
|
2001-09-11 01:13:15 +00:00
|
|
|
{
|
2018-01-17 19:59:43 +00:00
|
|
|
|
2001-09-11 01:13:15 +00:00
|
|
|
if (MAXRECSIZE - recpos < size)
|
|
|
|
errx(1, "record buffer overflow");
|
|
|
|
memcpy(recbuf + recpos, buf, size);
|
|
|
|
recpos += size;
|
2018-01-17 19:59:43 +00:00
|
|
|
return (0);
|
2001-09-11 01:13:15 +00:00
|
|
|
}
|
|
|
|
|
2009-01-06 14:23:38 +00:00
|
|
|
/*
|
|
|
|
* An int is stored in host order and aligned
|
|
|
|
*/
|
2001-09-11 01:13:15 +00:00
|
|
|
static int
|
|
|
|
record_int(int val)
|
|
|
|
{
|
2018-01-17 19:59:43 +00:00
|
|
|
|
2001-09-11 01:13:15 +00:00
|
|
|
intalign();
|
2018-01-17 19:59:43 +00:00
|
|
|
return (record_buf(&val, sizeof(val)));
|
2001-09-11 01:13:15 +00:00
|
|
|
}
|
|
|
|
|
2009-01-06 14:23:38 +00:00
|
|
|
/*
|
|
|
|
* A string is stored as 1-byte length plus data, no padding
|
|
|
|
*/
|
2001-09-11 01:13:15 +00:00
|
|
|
static int
|
|
|
|
record_string(const char *str)
|
|
|
|
{
|
2018-01-17 19:59:43 +00:00
|
|
|
int error;
|
|
|
|
size_t len;
|
2009-01-06 14:23:38 +00:00
|
|
|
u_char val;
|
|
|
|
|
2001-09-11 01:13:15 +00:00
|
|
|
if (dflag)
|
2018-01-17 19:59:43 +00:00
|
|
|
return (0);
|
2009-01-06 14:23:38 +00:00
|
|
|
val = len = strlen(str);
|
|
|
|
if (len > 255)
|
|
|
|
errx(1, "string %s too long", str);
|
|
|
|
error = record_buf(&val, sizeof(val));
|
2018-01-17 19:59:43 +00:00
|
|
|
if (error != 0)
|
|
|
|
return (error);
|
|
|
|
return (record_buf(str, len));
|
2001-09-11 01:13:15 +00:00
|
|
|
}
|
|
|
|
|
2015-12-11 05:27:55 +00:00
|
|
|
/* From sys/isa/pnp.c */
|
|
|
|
static char *
|
|
|
|
pnp_eisaformat(uint32_t id)
|
|
|
|
{
|
|
|
|
uint8_t *data;
|
|
|
|
static char idbuf[8];
|
|
|
|
const char hextoascii[] = "0123456789abcdef";
|
|
|
|
|
|
|
|
id = htole32(id);
|
|
|
|
data = (uint8_t *)&id;
|
|
|
|
idbuf[0] = '@' + ((data[0] & 0x7c) >> 2);
|
|
|
|
idbuf[1] = '@' + (((data[0] & 0x3) << 3) + ((data[1] & 0xe0) >> 5));
|
|
|
|
idbuf[2] = '@' + (data[1] & 0x1f);
|
|
|
|
idbuf[3] = hextoascii[(data[2] >> 4)];
|
|
|
|
idbuf[4] = hextoascii[(data[2] & 0xf)];
|
|
|
|
idbuf[5] = hextoascii[(data[3] >> 4)];
|
|
|
|
idbuf[6] = hextoascii[(data[3] & 0xf)];
|
|
|
|
idbuf[7] = 0;
|
2018-01-17 19:59:43 +00:00
|
|
|
return (idbuf);
|
2015-12-11 05:27:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct pnp_elt
|
|
|
|
{
|
|
|
|
int pe_kind; /* What kind of entry */
|
|
|
|
#define TYPE_SZ_MASK 0x0f
|
|
|
|
#define TYPE_FLAGGED 0x10 /* all f's is a wildcard */
|
|
|
|
#define TYPE_INT 0x20 /* Is a number */
|
|
|
|
#define TYPE_PAIRED 0x40
|
|
|
|
#define TYPE_LE 0x80 /* Matches <= this value */
|
|
|
|
#define TYPE_GE 0x100 /* Matches >= this value */
|
|
|
|
#define TYPE_MASK 0x200 /* Specifies a mask to follow */
|
|
|
|
#define TYPE_U8 (1 | TYPE_INT)
|
|
|
|
#define TYPE_V8 (1 | TYPE_INT | TYPE_FLAGGED)
|
|
|
|
#define TYPE_G16 (2 | TYPE_INT | TYPE_GE)
|
|
|
|
#define TYPE_L16 (2 | TYPE_INT | TYPE_LE)
|
|
|
|
#define TYPE_M16 (2 | TYPE_INT | TYPE_MASK)
|
|
|
|
#define TYPE_U16 (2 | TYPE_INT)
|
|
|
|
#define TYPE_V16 (2 | TYPE_INT | TYPE_FLAGGED)
|
|
|
|
#define TYPE_U32 (4 | TYPE_INT)
|
|
|
|
#define TYPE_V32 (4 | TYPE_INT | TYPE_FLAGGED)
|
|
|
|
#define TYPE_W32 (4 | TYPE_INT | TYPE_PAIRED)
|
|
|
|
#define TYPE_D 7
|
|
|
|
#define TYPE_Z 8
|
|
|
|
#define TYPE_P 9
|
|
|
|
#define TYPE_E 10
|
|
|
|
#define TYPE_T 11
|
|
|
|
int pe_offset; /* Offset within the element */
|
|
|
|
char * pe_key; /* pnp key name */
|
|
|
|
TAILQ_ENTRY(pnp_elt) next; /* Link */
|
|
|
|
};
|
|
|
|
typedef TAILQ_HEAD(pnp_head, pnp_elt) pnp_list;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* this function finds the data from the pnp table, as described by the
|
|
|
|
* the description and creates a new output (new_desc). This output table
|
|
|
|
* is a form that's easier for the agent that's automatically loading the
|
|
|
|
* modules.
|
|
|
|
*
|
|
|
|
* The format output is the simplified string from this routine in the
|
|
|
|
* same basic format as the pnp string, as documented in sys/module.h.
|
|
|
|
* First a string describing the format is output, the a count of the
|
|
|
|
* number of records, then each record. The format string also describes
|
|
|
|
* the length of each entry (though it isn't a fixed length when strings
|
|
|
|
* are present).
|
|
|
|
*
|
|
|
|
* type Output Meaning
|
|
|
|
* I uint32_t Integer equality comparison
|
|
|
|
* J uint32_t Pair of uint16_t fields converted to native
|
2018-02-17 06:57:03 +00:00
|
|
|
* byte order. The two fields both must match.
|
2015-12-11 05:27:55 +00:00
|
|
|
* G uint32_t Greater than or equal to
|
|
|
|
* L uint32_t Less than or equal to
|
|
|
|
* M uint32_t Mask of which fields to test. Fields that
|
2018-02-17 06:57:03 +00:00
|
|
|
* take up space increment the count. This
|
|
|
|
* field must be first, and resets the count.
|
2015-12-11 05:27:55 +00:00
|
|
|
* D string Description of the device this pnp info is for
|
|
|
|
* Z string pnp string must match this
|
|
|
|
* T nothing T fields set pnp values that must be true for
|
|
|
|
* the entire table.
|
|
|
|
* Values are packed the same way that other values are packed in this file.
|
|
|
|
* Strings and int32_t's start on a 32-bit boundary and are padded with 0
|
|
|
|
* bytes. Objects that are smaller than uint32_t are converted, without
|
|
|
|
* sign extension to uint32_t to simplify parsing downstream.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
parse_pnp_list(const char *desc, char **new_desc, pnp_list *list)
|
|
|
|
{
|
2018-01-17 19:59:43 +00:00
|
|
|
const char *walker, *ep;
|
2015-12-11 05:27:55 +00:00
|
|
|
const char *colon, *semi;
|
|
|
|
struct pnp_elt *elt;
|
|
|
|
char type[8], key[32];
|
|
|
|
int off;
|
2020-10-15 18:03:14 +00:00
|
|
|
size_t new_desc_size;
|
|
|
|
FILE *fp;
|
2015-12-11 05:27:55 +00:00
|
|
|
|
2018-01-17 19:59:43 +00:00
|
|
|
walker = desc;
|
|
|
|
ep = desc + strlen(desc);
|
2015-12-11 05:27:55 +00:00
|
|
|
off = 0;
|
2020-10-15 18:03:14 +00:00
|
|
|
fp = open_memstream(new_desc, &new_desc_size);
|
|
|
|
if (fp == NULL)
|
|
|
|
err(1, "Could not open new memory stream");
|
2015-12-11 05:27:55 +00:00
|
|
|
if (verbose > 1)
|
|
|
|
printf("Converting %s into a list\n", desc);
|
|
|
|
while (walker < ep) {
|
|
|
|
colon = strchr(walker, ':');
|
|
|
|
semi = strchr(walker, ';');
|
|
|
|
if (semi != NULL && semi < colon)
|
|
|
|
goto err;
|
|
|
|
if (colon - walker > sizeof(type))
|
|
|
|
goto err;
|
|
|
|
strncpy(type, walker, colon - walker);
|
|
|
|
type[colon - walker] = '\0';
|
2018-01-17 19:59:43 +00:00
|
|
|
if (semi != NULL) {
|
2015-12-11 05:27:55 +00:00
|
|
|
if (semi - colon >= sizeof(key))
|
|
|
|
goto err;
|
|
|
|
strncpy(key, colon + 1, semi - colon - 1);
|
|
|
|
key[semi - colon - 1] = '\0';
|
|
|
|
walker = semi + 1;
|
2018-09-26 19:41:06 +00:00
|
|
|
/* Fail safe if we have spaces after ; */
|
|
|
|
while (walker < ep && isspace(*walker))
|
|
|
|
walker++;
|
2015-12-11 05:27:55 +00:00
|
|
|
} else {
|
|
|
|
if (strlen(colon + 1) >= sizeof(key))
|
|
|
|
goto err;
|
|
|
|
strcpy(key, colon + 1);
|
|
|
|
walker = ep;
|
|
|
|
}
|
|
|
|
if (verbose > 1)
|
|
|
|
printf("Found type %s for name %s\n", type, key);
|
|
|
|
/* Skip pointer place holders */
|
|
|
|
if (strcmp(type, "P") == 0) {
|
|
|
|
off += sizeof(void *);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add a node of the appropriate type
|
|
|
|
*/
|
|
|
|
elt = malloc(sizeof(struct pnp_elt) + strlen(key) + 1);
|
|
|
|
TAILQ_INSERT_TAIL(list, elt, next);
|
|
|
|
elt->pe_key = (char *)(elt + 1);
|
|
|
|
elt->pe_offset = off;
|
|
|
|
if (strcmp(type, "U8") == 0)
|
|
|
|
elt->pe_kind = TYPE_U8;
|
|
|
|
else if (strcmp(type, "V8") == 0)
|
|
|
|
elt->pe_kind = TYPE_V8;
|
|
|
|
else if (strcmp(type, "G16") == 0)
|
|
|
|
elt->pe_kind = TYPE_G16;
|
|
|
|
else if (strcmp(type, "L16") == 0)
|
|
|
|
elt->pe_kind = TYPE_L16;
|
|
|
|
else if (strcmp(type, "M16") == 0)
|
|
|
|
elt->pe_kind = TYPE_M16;
|
|
|
|
else if (strcmp(type, "U16") == 0)
|
|
|
|
elt->pe_kind = TYPE_U16;
|
|
|
|
else if (strcmp(type, "V16") == 0)
|
|
|
|
elt->pe_kind = TYPE_V16;
|
|
|
|
else if (strcmp(type, "U32") == 0)
|
|
|
|
elt->pe_kind = TYPE_U32;
|
|
|
|
else if (strcmp(type, "V32") == 0)
|
|
|
|
elt->pe_kind = TYPE_V32;
|
|
|
|
else if (strcmp(type, "W32") == 0)
|
|
|
|
elt->pe_kind = TYPE_W32;
|
|
|
|
else if (strcmp(type, "D") == 0) /* description char * */
|
|
|
|
elt->pe_kind = TYPE_D;
|
|
|
|
else if (strcmp(type, "Z") == 0) /* char * to match */
|
|
|
|
elt->pe_kind = TYPE_Z;
|
|
|
|
else if (strcmp(type, "P") == 0) /* Pointer -- ignored */
|
|
|
|
elt->pe_kind = TYPE_P;
|
|
|
|
else if (strcmp(type, "E") == 0) /* EISA PNP ID, as uint32_t */
|
|
|
|
elt->pe_kind = TYPE_E;
|
|
|
|
else if (strcmp(type, "T") == 0)
|
|
|
|
elt->pe_kind = TYPE_T;
|
|
|
|
else
|
|
|
|
goto err;
|
|
|
|
/*
|
|
|
|
* Maybe the rounding here needs to be more nuanced and/or somehow
|
|
|
|
* architecture specific. Fortunately, most tables in the system
|
|
|
|
* have sane ordering of types.
|
|
|
|
*/
|
|
|
|
if (elt->pe_kind & TYPE_INT) {
|
|
|
|
elt->pe_offset = roundup2(elt->pe_offset, elt->pe_kind & TYPE_SZ_MASK);
|
|
|
|
off = elt->pe_offset + (elt->pe_kind & TYPE_SZ_MASK);
|
|
|
|
} else if (elt->pe_kind == TYPE_E) {
|
|
|
|
/* Type E stored as Int, displays as string */
|
|
|
|
elt->pe_offset = roundup2(elt->pe_offset, sizeof(uint32_t));
|
|
|
|
off = elt->pe_offset + sizeof(uint32_t);
|
|
|
|
} else if (elt->pe_kind == TYPE_T) {
|
|
|
|
/* doesn't actually consume space in the table */
|
|
|
|
off = elt->pe_offset;
|
|
|
|
} else {
|
|
|
|
elt->pe_offset = roundup2(elt->pe_offset, sizeof(void *));
|
|
|
|
off = elt->pe_offset + sizeof(void *);
|
|
|
|
}
|
|
|
|
if (elt->pe_kind & TYPE_PAIRED) {
|
2020-10-15 18:03:14 +00:00
|
|
|
char *word, *ctx, newtype;
|
2015-12-11 05:27:55 +00:00
|
|
|
|
|
|
|
for (word = strtok_r(key, "/", &ctx);
|
|
|
|
word; word = strtok_r(NULL, "/", &ctx)) {
|
2020-10-15 18:03:14 +00:00
|
|
|
newtype = elt->pe_kind & TYPE_FLAGGED ? 'J' : 'I';
|
|
|
|
fprintf(fp, "%c:%s;", newtype, word);
|
2015-12-11 05:27:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2020-10-15 18:03:14 +00:00
|
|
|
char newtype;
|
|
|
|
|
2015-12-11 05:27:55 +00:00
|
|
|
if (elt->pe_kind & TYPE_FLAGGED)
|
2020-10-15 18:03:14 +00:00
|
|
|
newtype = 'J';
|
2015-12-11 05:27:55 +00:00
|
|
|
else if (elt->pe_kind & TYPE_GE)
|
2020-10-15 18:03:14 +00:00
|
|
|
newtype = 'G';
|
2015-12-11 05:27:55 +00:00
|
|
|
else if (elt->pe_kind & TYPE_LE)
|
2020-10-15 18:03:14 +00:00
|
|
|
newtype = 'L';
|
2015-12-11 05:27:55 +00:00
|
|
|
else if (elt->pe_kind & TYPE_MASK)
|
2020-10-15 18:03:14 +00:00
|
|
|
newtype = 'M';
|
2015-12-11 05:27:55 +00:00
|
|
|
else if (elt->pe_kind & TYPE_INT)
|
2020-10-15 18:03:14 +00:00
|
|
|
newtype = 'I';
|
2015-12-11 05:27:55 +00:00
|
|
|
else if (elt->pe_kind == TYPE_D)
|
2020-10-15 18:03:14 +00:00
|
|
|
newtype = 'D';
|
2015-12-11 05:27:55 +00:00
|
|
|
else if (elt->pe_kind == TYPE_Z || elt->pe_kind == TYPE_E)
|
2020-10-15 18:03:14 +00:00
|
|
|
newtype = 'Z';
|
2015-12-11 05:27:55 +00:00
|
|
|
else if (elt->pe_kind == TYPE_T)
|
2020-10-15 18:03:14 +00:00
|
|
|
newtype = 'T';
|
2015-12-11 05:27:55 +00:00
|
|
|
else
|
|
|
|
errx(1, "Impossible type %x\n", elt->pe_kind);
|
2020-10-15 18:03:14 +00:00
|
|
|
fprintf(fp, "%c:%s;", newtype, key);
|
2015-12-11 05:27:55 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-15 18:03:14 +00:00
|
|
|
if (ferror(fp) != 0) {
|
|
|
|
fclose(fp);
|
|
|
|
errx(1, "Exhausted space converting description %s", desc);
|
|
|
|
}
|
|
|
|
if (fclose(fp) != 0)
|
|
|
|
errx(1, "Failed to close memory stream");
|
2018-01-17 19:59:43 +00:00
|
|
|
return (0);
|
2015-12-11 05:27:55 +00:00
|
|
|
err:
|
|
|
|
errx(1, "Parse error of description string %s", desc);
|
|
|
|
}
|
|
|
|
|
2001-09-11 01:13:15 +00:00
|
|
|
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;
|
2015-12-11 05:27:55 +00:00
|
|
|
struct mod_pnp_match_info pnp;
|
|
|
|
char descr[1024];
|
2018-01-17 19:59:43 +00:00
|
|
|
Elf_Off data;
|
|
|
|
int error, i;
|
|
|
|
size_t len;
|
2015-12-11 05:27:55 +00:00
|
|
|
char *walker;
|
|
|
|
void *table;
|
2001-09-11 01:13:15 +00:00
|
|
|
|
2018-01-17 19:59:43 +00:00
|
|
|
data = (Elf_Off)md->md_data;
|
|
|
|
error = 0;
|
2001-09-11 01:13:15 +00:00
|
|
|
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));
|
2009-01-06 14:23:38 +00:00
|
|
|
if (dflag) {
|
|
|
|
printf(" interface %s.%d\n", cval, mdv.mv_version);
|
|
|
|
} else {
|
|
|
|
record_int(MDT_VERSION);
|
|
|
|
record_string(cval);
|
|
|
|
record_int(mdv.mv_version);
|
|
|
|
record_string(kldname);
|
|
|
|
}
|
2001-09-11 01:13:15 +00:00
|
|
|
break;
|
|
|
|
case MDT_MODULE:
|
2009-01-06 14:23:38 +00:00
|
|
|
if (dflag) {
|
|
|
|
printf(" module %s\n", cval);
|
|
|
|
} else {
|
|
|
|
record_int(MDT_MODULE);
|
|
|
|
record_string(cval);
|
|
|
|
record_string(kldname);
|
|
|
|
}
|
2001-09-11 01:13:15 +00:00
|
|
|
break;
|
2015-01-15 00:46:30 +00:00
|
|
|
case MDT_PNP_INFO:
|
2015-12-11 05:27:55 +00:00
|
|
|
check(EF_SEG_READ_REL(ef, data, sizeof(pnp), &pnp));
|
2018-07-15 05:29:39 +00:00
|
|
|
check(EF_SEG_READ_STRING(ef, (Elf_Off)pnp.descr, sizeof(descr), descr));
|
2015-12-11 05:27:55 +00:00
|
|
|
descr[sizeof(descr) - 1] = '\0';
|
2015-01-15 00:46:30 +00:00
|
|
|
if (dflag) {
|
2015-12-11 05:27:55 +00:00
|
|
|
printf(" pnp info for bus %s format %s %d entries of %d bytes\n",
|
|
|
|
cval, descr, pnp.num_entry, pnp.entry_len);
|
|
|
|
} else {
|
|
|
|
pnp_list list;
|
|
|
|
struct pnp_elt *elt, *elt_tmp;
|
|
|
|
char *new_descr;
|
|
|
|
|
|
|
|
if (verbose > 1)
|
|
|
|
printf(" pnp info for bus %s format %s %d entries of %d bytes\n",
|
|
|
|
cval, descr, pnp.num_entry, pnp.entry_len);
|
|
|
|
/*
|
|
|
|
* Parse descr to weed out the chaff and to create a list
|
|
|
|
* of offsets to output.
|
|
|
|
*/
|
|
|
|
TAILQ_INIT(&list);
|
|
|
|
parse_pnp_list(descr, &new_descr, &list);
|
|
|
|
record_int(MDT_PNP_INFO);
|
|
|
|
record_string(cval);
|
|
|
|
record_string(new_descr);
|
|
|
|
record_int(pnp.num_entry);
|
|
|
|
len = pnp.num_entry * pnp.entry_len;
|
|
|
|
walker = table = malloc(len);
|
|
|
|
check(EF_SEG_READ_REL(ef, (Elf_Off)pnp.table, len, table));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Walk the list and output things. We've collapsed all the
|
|
|
|
* variant forms of the table down to just ints and strings.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < pnp.num_entry; i++) {
|
|
|
|
TAILQ_FOREACH(elt, &list, next) {
|
|
|
|
uint8_t v1;
|
|
|
|
uint16_t v2;
|
|
|
|
uint32_t v4;
|
|
|
|
int value;
|
|
|
|
char buffer[1024];
|
|
|
|
|
|
|
|
if (elt->pe_kind == TYPE_W32) {
|
|
|
|
memcpy(&v4, walker + elt->pe_offset, sizeof(v4));
|
|
|
|
value = v4 & 0xffff;
|
|
|
|
record_int(value);
|
|
|
|
if (verbose > 1)
|
|
|
|
printf("W32:%#x", value);
|
|
|
|
value = (v4 >> 16) & 0xffff;
|
|
|
|
record_int(value);
|
|
|
|
if (verbose > 1)
|
|
|
|
printf(":%#x;", value);
|
|
|
|
} else if (elt->pe_kind & TYPE_INT) {
|
|
|
|
switch (elt->pe_kind & TYPE_SZ_MASK) {
|
|
|
|
case 1:
|
|
|
|
memcpy(&v1, walker + elt->pe_offset, sizeof(v1));
|
|
|
|
if ((elt->pe_kind & TYPE_FLAGGED) && v1 == 0xff)
|
|
|
|
value = -1;
|
|
|
|
else
|
|
|
|
value = v1;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
memcpy(&v2, walker + elt->pe_offset, sizeof(v2));
|
|
|
|
if ((elt->pe_kind & TYPE_FLAGGED) && v2 == 0xffff)
|
|
|
|
value = -1;
|
|
|
|
else
|
|
|
|
value = v2;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
memcpy(&v4, walker + elt->pe_offset, sizeof(v4));
|
|
|
|
if ((elt->pe_kind & TYPE_FLAGGED) && v4 == 0xffffffff)
|
|
|
|
value = -1;
|
|
|
|
else
|
|
|
|
value = v4;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
errx(1, "Invalid size somehow %#x", elt->pe_kind);
|
|
|
|
}
|
|
|
|
if (verbose > 1)
|
|
|
|
printf("I:%#x;", value);
|
|
|
|
record_int(value);
|
|
|
|
} else if (elt->pe_kind == TYPE_T) {
|
|
|
|
/* Do nothing */
|
|
|
|
} else { /* E, Z or D -- P already filtered */
|
|
|
|
if (elt->pe_kind == TYPE_E) {
|
|
|
|
memcpy(&v4, walker + elt->pe_offset, sizeof(v4));
|
|
|
|
strcpy(buffer, pnp_eisaformat(v4));
|
|
|
|
} else {
|
|
|
|
char *ptr;
|
|
|
|
|
|
|
|
ptr = *(char **)(walker + elt->pe_offset);
|
|
|
|
buffer[0] = '\0';
|
2016-05-16 01:12:56 +00:00
|
|
|
if (ptr != NULL) {
|
2018-07-15 05:29:39 +00:00
|
|
|
EF_SEG_READ_STRING(ef, (Elf_Off)ptr,
|
2015-12-11 05:27:55 +00:00
|
|
|
sizeof(buffer), buffer);
|
|
|
|
buffer[sizeof(buffer) - 1] = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (verbose > 1)
|
|
|
|
printf("%c:%s;", elt->pe_kind == TYPE_E ? 'E' : (elt->pe_kind == TYPE_Z ? 'Z' : 'D'), buffer);
|
|
|
|
record_string(buffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (verbose > 1)
|
|
|
|
printf("\n");
|
|
|
|
walker += pnp.entry_len;
|
|
|
|
}
|
|
|
|
/* Now free it */
|
|
|
|
TAILQ_FOREACH_SAFE(elt, &list, next, elt_tmp) {
|
|
|
|
TAILQ_REMOVE(&list, elt, next);
|
|
|
|
free(elt);
|
|
|
|
}
|
|
|
|
free(table);
|
2015-01-15 00:46:30 +00:00
|
|
|
}
|
2015-12-11 05:27:55 +00:00
|
|
|
break;
|
2001-09-11 01:13:15 +00:00
|
|
|
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();
|
2018-01-17 19:59:43 +00:00
|
|
|
return (error);
|
2001-09-11 01:13:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
read_kld(char *filename, char *kldname)
|
|
|
|
{
|
|
|
|
struct mod_metadata md;
|
|
|
|
struct elf_file ef;
|
2019-05-27 17:33:20 +00:00
|
|
|
void **p;
|
2018-01-17 19:59:43 +00:00
|
|
|
int error, eftype;
|
2019-05-27 17:33:20 +00:00
|
|
|
long start, finish, entries, i;
|
2018-01-17 19:59:43 +00:00
|
|
|
char cval[MAXMODNAME + 1];
|
2001-09-11 01:13:15 +00:00
|
|
|
|
|
|
|
if (verbose || dflag)
|
|
|
|
printf("%s\n", filename);
|
|
|
|
error = ef_open(filename, &ef, verbose);
|
2018-01-17 19:59:43 +00:00
|
|
|
if (error != 0) {
|
2004-08-27 00:58:40 +00:00
|
|
|
error = ef_obj_open(filename, &ef, verbose);
|
2018-01-17 19:59:43 +00:00
|
|
|
if (error != 0) {
|
2004-08-27 00:58:40 +00:00
|
|
|
if (verbose)
|
|
|
|
warnc(error, "elf_open(%s)", filename);
|
2018-01-17 19:59:43 +00:00
|
|
|
return (error);
|
2004-08-27 00:58:40 +00:00
|
|
|
}
|
2004-08-27 00:51:21 +00:00
|
|
|
}
|
|
|
|
eftype = EF_GET_TYPE(&ef);
|
|
|
|
if (eftype != EFT_KLD && eftype != EFT_KERNEL) {
|
|
|
|
EF_CLOSE(&ef);
|
2018-01-17 19:59:43 +00:00
|
|
|
return (0);
|
2001-09-11 01:13:15 +00:00
|
|
|
}
|
|
|
|
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));
|
2019-05-27 17:33:20 +00:00
|
|
|
/*
|
|
|
|
* Do a first pass to find MDT_MODULE. It is required to be
|
|
|
|
* ordered first in the output linker.hints stream because it
|
|
|
|
* serves as an implicit record boundary between distinct klds
|
|
|
|
* in the stream. Other MDTs only make sense in the context of
|
|
|
|
* a specific MDT_MODULE.
|
|
|
|
*
|
|
|
|
* Some compilers (e.g., GCC 6.4.0 xtoolchain) or binutils
|
|
|
|
* (e.g., GNU binutils 2.32 objcopy/ld.bfd) can reorder
|
|
|
|
* MODULE_METADATA set entries relative to the source ordering.
|
|
|
|
* This is permitted by the C standard; memory layout of
|
|
|
|
* file-scope objects is left implementation-defined. There is
|
|
|
|
* no requirement that source code ordering is retained.
|
|
|
|
*
|
|
|
|
* Handle that here by taking two passes to ensure MDT_MODULE
|
|
|
|
* records are emitted to linker.hints before other MDT records
|
|
|
|
* in the same kld.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < entries; i++) {
|
|
|
|
check(EF_SEG_READ_REL(&ef, (Elf_Off)p[i], sizeof(md),
|
|
|
|
&md));
|
|
|
|
check(EF_SEG_READ_STRING(&ef, (Elf_Off)md.md_cval,
|
|
|
|
sizeof(cval), cval));
|
|
|
|
if (md.md_type == MDT_MODULE) {
|
|
|
|
parse_entry(&md, cval, &ef, kldname);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (error != 0) {
|
|
|
|
warnc(error, "error while reading %s", filename);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Second pass for all !MDT_MODULE entries.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < entries; i++) {
|
|
|
|
check(EF_SEG_READ_REL(&ef, (Elf_Off)p[i], sizeof(md),
|
2004-08-27 00:51:21 +00:00
|
|
|
&md));
|
2018-01-16 18:20:12 +00:00
|
|
|
check(EF_SEG_READ_STRING(&ef, (Elf_Off)md.md_cval,
|
2004-08-27 00:51:21 +00:00
|
|
|
sizeof(cval), cval));
|
2019-05-27 17:33:20 +00:00
|
|
|
if (md.md_type != MDT_MODULE)
|
|
|
|
parse_entry(&md, cval, &ef, kldname);
|
2001-09-11 01:13:15 +00:00
|
|
|
}
|
2018-01-17 19:59:43 +00:00
|
|
|
if (error != 0)
|
2001-09-11 01:13:15 +00:00
|
|
|
warnc(error, "error while reading %s", filename);
|
2019-05-27 17:33:20 +00:00
|
|
|
free(p);
|
2001-09-11 01:13:15 +00:00
|
|
|
} while(0);
|
2004-08-27 00:51:21 +00:00
|
|
|
EF_CLOSE(&ef);
|
2018-01-17 19:59:43 +00:00
|
|
|
return (error);
|
2001-09-11 01:13:15 +00:00
|
|
|
}
|
|
|
|
|
2009-01-06 14:23:38 +00:00
|
|
|
/*
|
|
|
|
* Create a temp file in directory root, make sure we don't
|
|
|
|
* overflow the buffer for the destination name
|
|
|
|
*/
|
|
|
|
static FILE *
|
2001-09-11 01:13:15 +00:00
|
|
|
maketempfile(char *dest, const char *root)
|
|
|
|
{
|
|
|
|
char *p;
|
2009-01-06 14:23:38 +00:00
|
|
|
int n, fd;
|
|
|
|
|
|
|
|
p = strrchr(root, '/');
|
|
|
|
n = p != NULL ? p - root + 1 : 0;
|
|
|
|
if (snprintf(dest, MAXPATHLEN, "%.*slhint.XXXXXX", n, root) >=
|
|
|
|
MAXPATHLEN) {
|
|
|
|
errno = ENAMETOOLONG;
|
2018-01-17 19:59:43 +00:00
|
|
|
return (NULL);
|
2009-01-06 14:23:38 +00:00
|
|
|
}
|
2001-09-11 01:13:15 +00:00
|
|
|
|
2006-07-29 19:39:03 +00:00
|
|
|
fd = mkstemp(dest);
|
2009-01-06 14:23:38 +00:00
|
|
|
if (fd < 0)
|
2018-01-17 19:59:43 +00:00
|
|
|
return (NULL);
|
2009-01-06 14:23:38 +00:00
|
|
|
fchmod(fd, 0644); /* nothing secret in the file */
|
2018-01-17 19:59:43 +00:00
|
|
|
return (fdopen(fd, "w+"));
|
2001-09-11 01:13:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static char xrefname[MAXPATHLEN], tempname[MAXPATHLEN];
|
|
|
|
|
2009-01-06 14:23:38 +00:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
fprintf(stderr, "%s\n",
|
|
|
|
"usage: kldxref [-Rdv] [-f hintsfile] path ..."
|
|
|
|
);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2013-10-16 20:04:06 +00:00
|
|
|
static int
|
2013-10-04 21:25:55 +00:00
|
|
|
compare(const FTSENT *const *a, const FTSENT *const *b)
|
|
|
|
{
|
2018-01-17 19:59:43 +00:00
|
|
|
|
2013-10-04 21:25:55 +00:00
|
|
|
if ((*a)->fts_info == FTS_D && (*b)->fts_info != FTS_D)
|
2018-01-17 19:59:43 +00:00
|
|
|
return (1);
|
2013-10-04 21:25:55 +00:00
|
|
|
if ((*a)->fts_info != FTS_D && (*b)->fts_info == FTS_D)
|
2018-01-17 19:59:43 +00:00
|
|
|
return (-1);
|
|
|
|
return (strcmp((*a)->fts_name, (*b)->fts_name));
|
2013-10-04 21:25:55 +00:00
|
|
|
}
|
|
|
|
|
2001-09-11 01:13:15 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
while ((opt = getopt(argc, argv, "Rdf:v")) != -1) {
|
|
|
|
switch (opt) {
|
2009-01-06 14:23:38 +00:00
|
|
|
case 'd': /* no hint file, only print on stdout */
|
2018-01-17 19:59:43 +00:00
|
|
|
dflag = true;
|
2001-09-11 01:13:15 +00:00
|
|
|
break;
|
2009-01-06 14:23:38 +00:00
|
|
|
case 'f': /* use this name instead of linker.hints */
|
2001-09-11 01:13:15 +00:00
|
|
|
xref_file = optarg;
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
verbose++;
|
|
|
|
break;
|
2009-01-06 14:23:38 +00:00
|
|
|
case 'R': /* recurse on directories */
|
2001-09-11 01:13:15 +00:00
|
|
|
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]);
|
2021-05-20 19:12:11 +00:00
|
|
|
if ((sb.st_mode & S_IFDIR) == 0 && !dflag) {
|
2004-04-30 00:20:58 +00:00
|
|
|
errno = ENOTDIR;
|
|
|
|
err(1, "%s", argv[0]);
|
|
|
|
}
|
|
|
|
|
2013-10-04 21:25:55 +00:00
|
|
|
ftsp = fts_open(argv, fts_options, compare);
|
2001-09-11 01:13:15 +00:00
|
|
|
if (ftsp == NULL)
|
|
|
|
exit(1);
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
p = fts_read(ftsp);
|
2009-01-06 14:23:38 +00:00
|
|
|
if ((p == NULL || p->fts_info == FTS_D) && fxref) {
|
|
|
|
/* close and rename the current hint file */
|
2001-09-11 01:13:15 +00:00
|
|
|
fclose(fxref);
|
2006-07-29 19:39:03 +00:00
|
|
|
fxref = NULL;
|
2018-01-17 19:59:43 +00:00
|
|
|
if (reccnt != 0) {
|
2001-09-11 01:13:15 +00:00
|
|
|
rename(tempname, xrefname);
|
|
|
|
} else {
|
2009-01-06 14:23:38 +00:00
|
|
|
/* didn't find any entry, ignore this file */
|
2001-09-11 01:13:15 +00:00
|
|
|
unlink(tempname);
|
|
|
|
unlink(xrefname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (p == NULL)
|
|
|
|
break;
|
2009-01-06 14:23:38 +00:00
|
|
|
if (p->fts_info == FTS_D && !dflag) {
|
|
|
|
/* visiting a new directory, create a new hint file */
|
2001-09-11 01:13:15 +00:00
|
|
|
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;
|
|
|
|
}
|
2015-10-02 13:16:06 +00:00
|
|
|
/* skip non-files and separate debug files */
|
2001-09-11 01:13:15 +00:00
|
|
|
if (p->fts_info != FTS_F)
|
|
|
|
continue;
|
2015-10-02 13:16:06 +00:00
|
|
|
if (p->fts_namelen >= 6 &&
|
|
|
|
strcmp(p->fts_name + p->fts_namelen - 6, ".debug") == 0)
|
|
|
|
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);
|
2018-01-17 19:59:43 +00:00
|
|
|
return (0);
|
2001-09-11 01:13:15 +00:00
|
|
|
}
|