19c6f9240SPeter Wemm /* 29c6f9240SPeter Wemm * Copyright (c) 2000, Boris Popov 39c6f9240SPeter Wemm * All rights reserved. 49c6f9240SPeter Wemm * 59c6f9240SPeter Wemm * Redistribution and use in source and binary forms, with or without 69c6f9240SPeter Wemm * modification, are permitted provided that the following conditions 79c6f9240SPeter Wemm * are met: 89c6f9240SPeter Wemm * 1. Redistributions of source code must retain the above copyright 99c6f9240SPeter Wemm * notice, this list of conditions and the following disclaimer. 109c6f9240SPeter Wemm * 2. Redistributions in binary form must reproduce the above copyright 119c6f9240SPeter Wemm * notice, this list of conditions and the following disclaimer in the 129c6f9240SPeter Wemm * documentation and/or other materials provided with the distribution. 139c6f9240SPeter Wemm * 3. All advertising materials mentioning features or use of this software 149c6f9240SPeter Wemm * must display the following acknowledgement: 159c6f9240SPeter Wemm * This product includes software developed by Boris Popov. 169c6f9240SPeter Wemm * 4. Neither the name of the author nor the names of any co-contributors 179c6f9240SPeter Wemm * may be used to endorse or promote products derived from this software 189c6f9240SPeter Wemm * without specific prior written permission. 199c6f9240SPeter Wemm * 209c6f9240SPeter Wemm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 219c6f9240SPeter Wemm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 229c6f9240SPeter Wemm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 239c6f9240SPeter Wemm * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 249c6f9240SPeter Wemm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 259c6f9240SPeter Wemm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 269c6f9240SPeter Wemm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 279c6f9240SPeter Wemm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 289c6f9240SPeter Wemm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 299c6f9240SPeter Wemm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 309c6f9240SPeter Wemm * SUCH DAMAGE. 319c6f9240SPeter Wemm * 329c6f9240SPeter Wemm * $FreeBSD$ 339c6f9240SPeter Wemm */ 349c6f9240SPeter Wemm 35b7abc67eSMaxim Sobolev #include <sys/types.h> 369c6f9240SPeter Wemm #include <sys/param.h> 37*493b2041SWarner Losh #include <sys/endian.h> 389c6f9240SPeter Wemm #include <sys/exec.h> 399c6f9240SPeter Wemm #include <sys/queue.h> 409c6f9240SPeter Wemm #include <sys/kernel.h> 419c6f9240SPeter Wemm #include <sys/reboot.h> 429c6f9240SPeter Wemm #include <sys/linker.h> 439c6f9240SPeter Wemm #include <sys/stat.h> 449c6f9240SPeter Wemm #include <sys/module.h> 459c6f9240SPeter Wemm #define FREEBSD_ELF 469c6f9240SPeter Wemm #include <err.h> 479c6f9240SPeter Wemm #include <fts.h> 489c6f9240SPeter Wemm #include <string.h> 499c6f9240SPeter Wemm #include <machine/elf.h> 509c6f9240SPeter Wemm #include <stdio.h> 519c6f9240SPeter Wemm #include <stdlib.h> 529c6f9240SPeter Wemm #include <unistd.h> 539c6f9240SPeter Wemm #include <errno.h> 549c6f9240SPeter Wemm 559c6f9240SPeter Wemm #include "ef.h" 569c6f9240SPeter Wemm 57*493b2041SWarner Losh #define MAXRECSIZE (64 << 10) /* 64k */ 589c6f9240SPeter Wemm #define check(val) if ((error = (val)) != 0) break 599c6f9240SPeter Wemm 609cb138bbSLuigi Rizzo static int dflag; /* do not create a hint file, only write on stdout */ 619cb138bbSLuigi Rizzo static int verbose; 629c6f9240SPeter Wemm 639cb138bbSLuigi Rizzo static FILE *fxref; /* current hints file */ 649c6f9240SPeter Wemm 6587e5cd7cSMike Heffner static const char *xref_file = "linker.hints"; 669c6f9240SPeter Wemm 679cb138bbSLuigi Rizzo /* 689cb138bbSLuigi Rizzo * A record is stored in the static buffer recbuf before going to disk. 699cb138bbSLuigi Rizzo */ 709c6f9240SPeter Wemm static char recbuf[MAXRECSIZE]; 719cb138bbSLuigi Rizzo static int recpos; /* current write position */ 729cb138bbSLuigi Rizzo static int reccnt; /* total record written to this file so far */ 739c6f9240SPeter Wemm 749c6f9240SPeter Wemm static void 759c6f9240SPeter Wemm intalign(void) 769c6f9240SPeter Wemm { 779c6f9240SPeter Wemm recpos = (recpos + sizeof(int) - 1) & ~(sizeof(int) - 1); 789c6f9240SPeter Wemm } 799c6f9240SPeter Wemm 809c6f9240SPeter Wemm static void 819c6f9240SPeter Wemm record_start(void) 829c6f9240SPeter Wemm { 839c6f9240SPeter Wemm recpos = 0; 849c6f9240SPeter Wemm memset(recbuf, 0, MAXRECSIZE); 859c6f9240SPeter Wemm } 869c6f9240SPeter Wemm 879c6f9240SPeter Wemm static int 889c6f9240SPeter Wemm record_end(void) 899c6f9240SPeter Wemm { 909cb138bbSLuigi Rizzo if (recpos == 0) 919c6f9240SPeter Wemm return 0; 929c6f9240SPeter Wemm reccnt++; 939c6f9240SPeter Wemm intalign(); 949c6f9240SPeter Wemm fwrite(&recpos, sizeof(recpos), 1, fxref); 959c6f9240SPeter Wemm return fwrite(recbuf, recpos, 1, fxref) != 1 ? errno : 0; 969c6f9240SPeter Wemm } 979c6f9240SPeter Wemm 989c6f9240SPeter Wemm static int 999c6f9240SPeter Wemm record_buf(const void *buf, int size) 1009c6f9240SPeter Wemm { 1019c6f9240SPeter Wemm if (MAXRECSIZE - recpos < size) 1029c6f9240SPeter Wemm errx(1, "record buffer overflow"); 1039c6f9240SPeter Wemm memcpy(recbuf + recpos, buf, size); 1049c6f9240SPeter Wemm recpos += size; 1059c6f9240SPeter Wemm return 0; 1069c6f9240SPeter Wemm } 1079c6f9240SPeter Wemm 1089cb138bbSLuigi Rizzo /* 1099cb138bbSLuigi Rizzo * An int is stored in host order and aligned 1109cb138bbSLuigi Rizzo */ 1119c6f9240SPeter Wemm static int 1129c6f9240SPeter Wemm record_int(int val) 1139c6f9240SPeter Wemm { 1149c6f9240SPeter Wemm intalign(); 1159c6f9240SPeter Wemm return record_buf(&val, sizeof(val)); 1169c6f9240SPeter Wemm } 1179c6f9240SPeter Wemm 1189cb138bbSLuigi Rizzo /* 1199cb138bbSLuigi Rizzo * A string is stored as 1-byte length plus data, no padding 1209cb138bbSLuigi Rizzo */ 1219c6f9240SPeter Wemm static int 1229c6f9240SPeter Wemm record_string(const char *str) 1239c6f9240SPeter Wemm { 1249cb138bbSLuigi Rizzo int len, error; 1259cb138bbSLuigi Rizzo u_char val; 1269c6f9240SPeter Wemm 1279c6f9240SPeter Wemm if (dflag) 1289c6f9240SPeter Wemm return 0; 1299cb138bbSLuigi Rizzo val = len = strlen(str); 1309cb138bbSLuigi Rizzo if (len > 255) 1319cb138bbSLuigi Rizzo errx(1, "string %s too long", str); 1329cb138bbSLuigi Rizzo error = record_buf(&val, sizeof(val)); 1339c6f9240SPeter Wemm if (error) 1349c6f9240SPeter Wemm return error; 1359c6f9240SPeter Wemm return record_buf(str, len); 1369c6f9240SPeter Wemm } 1379c6f9240SPeter Wemm 138*493b2041SWarner Losh /* From sys/isa/pnp.c */ 139*493b2041SWarner Losh static char * 140*493b2041SWarner Losh pnp_eisaformat(uint32_t id) 141*493b2041SWarner Losh { 142*493b2041SWarner Losh uint8_t *data; 143*493b2041SWarner Losh static char idbuf[8]; 144*493b2041SWarner Losh const char hextoascii[] = "0123456789abcdef"; 145*493b2041SWarner Losh 146*493b2041SWarner Losh id = htole32(id); 147*493b2041SWarner Losh data = (uint8_t *)&id; 148*493b2041SWarner Losh idbuf[0] = '@' + ((data[0] & 0x7c) >> 2); 149*493b2041SWarner Losh idbuf[1] = '@' + (((data[0] & 0x3) << 3) + ((data[1] & 0xe0) >> 5)); 150*493b2041SWarner Losh idbuf[2] = '@' + (data[1] & 0x1f); 151*493b2041SWarner Losh idbuf[3] = hextoascii[(data[2] >> 4)]; 152*493b2041SWarner Losh idbuf[4] = hextoascii[(data[2] & 0xf)]; 153*493b2041SWarner Losh idbuf[5] = hextoascii[(data[3] >> 4)]; 154*493b2041SWarner Losh idbuf[6] = hextoascii[(data[3] & 0xf)]; 155*493b2041SWarner Losh idbuf[7] = 0; 156*493b2041SWarner Losh return(idbuf); 157*493b2041SWarner Losh } 158*493b2041SWarner Losh 159*493b2041SWarner Losh struct pnp_elt 160*493b2041SWarner Losh { 161*493b2041SWarner Losh int pe_kind; /* What kind of entry */ 162*493b2041SWarner Losh #define TYPE_SZ_MASK 0x0f 163*493b2041SWarner Losh #define TYPE_FLAGGED 0x10 /* all f's is a wildcard */ 164*493b2041SWarner Losh #define TYPE_INT 0x20 /* Is a number */ 165*493b2041SWarner Losh #define TYPE_PAIRED 0x40 166*493b2041SWarner Losh #define TYPE_LE 0x80 /* Matches <= this value */ 167*493b2041SWarner Losh #define TYPE_GE 0x100 /* Matches >= this value */ 168*493b2041SWarner Losh #define TYPE_MASK 0x200 /* Specifies a mask to follow */ 169*493b2041SWarner Losh #define TYPE_U8 (1 | TYPE_INT) 170*493b2041SWarner Losh #define TYPE_V8 (1 | TYPE_INT | TYPE_FLAGGED) 171*493b2041SWarner Losh #define TYPE_G16 (2 | TYPE_INT | TYPE_GE) 172*493b2041SWarner Losh #define TYPE_L16 (2 | TYPE_INT | TYPE_LE) 173*493b2041SWarner Losh #define TYPE_M16 (2 | TYPE_INT | TYPE_MASK) 174*493b2041SWarner Losh #define TYPE_U16 (2 | TYPE_INT) 175*493b2041SWarner Losh #define TYPE_V16 (2 | TYPE_INT | TYPE_FLAGGED) 176*493b2041SWarner Losh #define TYPE_U32 (4 | TYPE_INT) 177*493b2041SWarner Losh #define TYPE_V32 (4 | TYPE_INT | TYPE_FLAGGED) 178*493b2041SWarner Losh #define TYPE_W32 (4 | TYPE_INT | TYPE_PAIRED) 179*493b2041SWarner Losh #define TYPE_D 7 180*493b2041SWarner Losh #define TYPE_Z 8 181*493b2041SWarner Losh #define TYPE_P 9 182*493b2041SWarner Losh #define TYPE_E 10 183*493b2041SWarner Losh #define TYPE_T 11 184*493b2041SWarner Losh int pe_offset; /* Offset within the element */ 185*493b2041SWarner Losh char * pe_key; /* pnp key name */ 186*493b2041SWarner Losh TAILQ_ENTRY(pnp_elt) next; /* Link */ 187*493b2041SWarner Losh }; 188*493b2041SWarner Losh typedef TAILQ_HEAD(pnp_head, pnp_elt) pnp_list; 189*493b2041SWarner Losh 190*493b2041SWarner Losh /* 191*493b2041SWarner Losh * this function finds the data from the pnp table, as described by the 192*493b2041SWarner Losh * the description and creates a new output (new_desc). This output table 193*493b2041SWarner Losh * is a form that's easier for the agent that's automatically loading the 194*493b2041SWarner Losh * modules. 195*493b2041SWarner Losh * 196*493b2041SWarner Losh * The format output is the simplified string from this routine in the 197*493b2041SWarner Losh * same basic format as the pnp string, as documented in sys/module.h. 198*493b2041SWarner Losh * First a string describing the format is output, the a count of the 199*493b2041SWarner Losh * number of records, then each record. The format string also describes 200*493b2041SWarner Losh * the length of each entry (though it isn't a fixed length when strings 201*493b2041SWarner Losh * are present). 202*493b2041SWarner Losh * 203*493b2041SWarner Losh * type Output Meaning 204*493b2041SWarner Losh * I uint32_t Integer equality comparison 205*493b2041SWarner Losh * J uint32_t Pair of uint16_t fields converted to native 206*493b2041SWarner Losh byte order. The two fields both must match. 207*493b2041SWarner Losh * G uint32_t Greater than or equal to 208*493b2041SWarner Losh * L uint32_t Less than or equal to 209*493b2041SWarner Losh * M uint32_t Mask of which fields to test. Fields that 210*493b2041SWarner Losh take up space increment the count. This 211*493b2041SWarner Losh field must be first, and resets the count. 212*493b2041SWarner Losh * D string Description of the device this pnp info is for 213*493b2041SWarner Losh * Z string pnp string must match this 214*493b2041SWarner Losh * T nothing T fields set pnp values that must be true for 215*493b2041SWarner Losh * the entire table. 216*493b2041SWarner Losh * Values are packed the same way that other values are packed in this file. 217*493b2041SWarner Losh * Strings and int32_t's start on a 32-bit boundary and are padded with 0 218*493b2041SWarner Losh * bytes. Objects that are smaller than uint32_t are converted, without 219*493b2041SWarner Losh * sign extension to uint32_t to simplify parsing downstream. 220*493b2041SWarner Losh */ 221*493b2041SWarner Losh static int 222*493b2041SWarner Losh parse_pnp_list(const char *desc, char **new_desc, pnp_list *list) 223*493b2041SWarner Losh { 224*493b2041SWarner Losh const char *walker = desc, *ep = desc + strlen(desc); 225*493b2041SWarner Losh const char *colon, *semi; 226*493b2041SWarner Losh struct pnp_elt *elt; 227*493b2041SWarner Losh char *nd; 228*493b2041SWarner Losh char type[8], key[32]; 229*493b2041SWarner Losh int off; 230*493b2041SWarner Losh 231*493b2041SWarner Losh off = 0; 232*493b2041SWarner Losh nd = *new_desc = malloc(strlen(desc) + 1); 233*493b2041SWarner Losh if (verbose > 1) 234*493b2041SWarner Losh printf("Converting %s into a list\n", desc); 235*493b2041SWarner Losh while (walker < ep) { 236*493b2041SWarner Losh colon = strchr(walker, ':'); 237*493b2041SWarner Losh semi = strchr(walker, ';'); 238*493b2041SWarner Losh if (semi != NULL && semi < colon) 239*493b2041SWarner Losh goto err; 240*493b2041SWarner Losh if (colon - walker > sizeof(type)) 241*493b2041SWarner Losh goto err; 242*493b2041SWarner Losh strncpy(type, walker, colon - walker); 243*493b2041SWarner Losh type[colon - walker] = '\0'; 244*493b2041SWarner Losh if (semi) { 245*493b2041SWarner Losh if (semi - colon >= sizeof(key)) 246*493b2041SWarner Losh goto err; 247*493b2041SWarner Losh strncpy(key, colon + 1, semi - colon - 1); 248*493b2041SWarner Losh key[semi - colon - 1] = '\0'; 249*493b2041SWarner Losh walker = semi + 1; 250*493b2041SWarner Losh } else { 251*493b2041SWarner Losh if (strlen(colon + 1) >= sizeof(key)) 252*493b2041SWarner Losh goto err; 253*493b2041SWarner Losh strcpy(key, colon + 1); 254*493b2041SWarner Losh walker = ep; 255*493b2041SWarner Losh } 256*493b2041SWarner Losh if (verbose > 1) 257*493b2041SWarner Losh printf("Found type %s for name %s\n", type, key); 258*493b2041SWarner Losh /* Skip pointer place holders */ 259*493b2041SWarner Losh if (strcmp(type, "P") == 0) { 260*493b2041SWarner Losh off += sizeof(void *); 261*493b2041SWarner Losh continue; 262*493b2041SWarner Losh } 263*493b2041SWarner Losh 264*493b2041SWarner Losh /* 265*493b2041SWarner Losh * Add a node of the appropriate type 266*493b2041SWarner Losh */ 267*493b2041SWarner Losh elt = malloc(sizeof(struct pnp_elt) + strlen(key) + 1); 268*493b2041SWarner Losh TAILQ_INSERT_TAIL(list, elt, next); 269*493b2041SWarner Losh elt->pe_key = (char *)(elt + 1); 270*493b2041SWarner Losh elt->pe_offset = off; 271*493b2041SWarner Losh if (strcmp(type, "U8") == 0) 272*493b2041SWarner Losh elt->pe_kind = TYPE_U8; 273*493b2041SWarner Losh else if (strcmp(type, "V8") == 0) 274*493b2041SWarner Losh elt->pe_kind = TYPE_V8; 275*493b2041SWarner Losh else if (strcmp(type, "G16") == 0) 276*493b2041SWarner Losh elt->pe_kind = TYPE_G16; 277*493b2041SWarner Losh else if (strcmp(type, "L16") == 0) 278*493b2041SWarner Losh elt->pe_kind = TYPE_L16; 279*493b2041SWarner Losh else if (strcmp(type, "M16") == 0) 280*493b2041SWarner Losh elt->pe_kind = TYPE_M16; 281*493b2041SWarner Losh else if (strcmp(type, "U16") == 0) 282*493b2041SWarner Losh elt->pe_kind = TYPE_U16; 283*493b2041SWarner Losh else if (strcmp(type, "V16") == 0) 284*493b2041SWarner Losh elt->pe_kind = TYPE_V16; 285*493b2041SWarner Losh else if (strcmp(type, "U32") == 0) 286*493b2041SWarner Losh elt->pe_kind = TYPE_U32; 287*493b2041SWarner Losh else if (strcmp(type, "V32") == 0) 288*493b2041SWarner Losh elt->pe_kind = TYPE_V32; 289*493b2041SWarner Losh else if (strcmp(type, "W32") == 0) 290*493b2041SWarner Losh elt->pe_kind = TYPE_W32; 291*493b2041SWarner Losh else if (strcmp(type, "D") == 0) /* description char * */ 292*493b2041SWarner Losh elt->pe_kind = TYPE_D; 293*493b2041SWarner Losh else if (strcmp(type, "Z") == 0) /* char * to match */ 294*493b2041SWarner Losh elt->pe_kind = TYPE_Z; 295*493b2041SWarner Losh else if (strcmp(type, "P") == 0) /* Pointer -- ignored */ 296*493b2041SWarner Losh elt->pe_kind = TYPE_P; 297*493b2041SWarner Losh else if (strcmp(type, "E") == 0) /* EISA PNP ID, as uint32_t */ 298*493b2041SWarner Losh elt->pe_kind = TYPE_E; 299*493b2041SWarner Losh else if (strcmp(type, "T") == 0) 300*493b2041SWarner Losh elt->pe_kind = TYPE_T; 301*493b2041SWarner Losh else 302*493b2041SWarner Losh goto err; 303*493b2041SWarner Losh /* 304*493b2041SWarner Losh * Maybe the rounding here needs to be more nuanced and/or somehow 305*493b2041SWarner Losh * architecture specific. Fortunately, most tables in the system 306*493b2041SWarner Losh * have sane ordering of types. 307*493b2041SWarner Losh */ 308*493b2041SWarner Losh if (elt->pe_kind & TYPE_INT) { 309*493b2041SWarner Losh elt->pe_offset = roundup2(elt->pe_offset, elt->pe_kind & TYPE_SZ_MASK); 310*493b2041SWarner Losh off = elt->pe_offset + (elt->pe_kind & TYPE_SZ_MASK); 311*493b2041SWarner Losh } else if (elt->pe_kind == TYPE_E) { 312*493b2041SWarner Losh /* Type E stored as Int, displays as string */ 313*493b2041SWarner Losh elt->pe_offset = roundup2(elt->pe_offset, sizeof(uint32_t)); 314*493b2041SWarner Losh off = elt->pe_offset + sizeof(uint32_t); 315*493b2041SWarner Losh } else if (elt->pe_kind == TYPE_T) { 316*493b2041SWarner Losh /* doesn't actually consume space in the table */ 317*493b2041SWarner Losh off = elt->pe_offset; 318*493b2041SWarner Losh } else { 319*493b2041SWarner Losh elt->pe_offset = roundup2(elt->pe_offset, sizeof(void *)); 320*493b2041SWarner Losh off = elt->pe_offset + sizeof(void *); 321*493b2041SWarner Losh } 322*493b2041SWarner Losh if (elt->pe_kind & TYPE_PAIRED) { 323*493b2041SWarner Losh char *word, *ctx; 324*493b2041SWarner Losh 325*493b2041SWarner Losh for (word = strtok_r(key, "/", &ctx); 326*493b2041SWarner Losh word; word = strtok_r(NULL, "/", &ctx)) { 327*493b2041SWarner Losh sprintf(nd, "%c:%s;", elt->pe_kind & TYPE_FLAGGED ? 'J' : 'I', 328*493b2041SWarner Losh word); 329*493b2041SWarner Losh nd += strlen(nd); 330*493b2041SWarner Losh } 331*493b2041SWarner Losh 332*493b2041SWarner Losh } 333*493b2041SWarner Losh else { 334*493b2041SWarner Losh if (elt->pe_kind & TYPE_FLAGGED) 335*493b2041SWarner Losh *nd++ = 'J'; 336*493b2041SWarner Losh else if (elt->pe_kind & TYPE_GE) 337*493b2041SWarner Losh *nd++ = 'G'; 338*493b2041SWarner Losh else if (elt->pe_kind & TYPE_LE) 339*493b2041SWarner Losh *nd++ = 'L'; 340*493b2041SWarner Losh else if (elt->pe_kind & TYPE_MASK) 341*493b2041SWarner Losh *nd++ = 'M'; 342*493b2041SWarner Losh else if (elt->pe_kind & TYPE_INT) 343*493b2041SWarner Losh *nd++ = 'I'; 344*493b2041SWarner Losh else if (elt->pe_kind == TYPE_D) 345*493b2041SWarner Losh *nd++ = 'D'; 346*493b2041SWarner Losh else if (elt->pe_kind == TYPE_Z || elt->pe_kind == TYPE_E) 347*493b2041SWarner Losh *nd++ = 'Z'; 348*493b2041SWarner Losh else if (elt->pe_kind == TYPE_T) 349*493b2041SWarner Losh *nd++ = 'T'; 350*493b2041SWarner Losh else 351*493b2041SWarner Losh errx(1, "Impossible type %x\n", elt->pe_kind); 352*493b2041SWarner Losh *nd++ = ':'; 353*493b2041SWarner Losh strcpy(nd, key); 354*493b2041SWarner Losh nd += strlen(nd); 355*493b2041SWarner Losh *nd++ = ';'; 356*493b2041SWarner Losh } 357*493b2041SWarner Losh } 358*493b2041SWarner Losh *nd++ = '\0'; 359*493b2041SWarner Losh return 0; 360*493b2041SWarner Losh err: 361*493b2041SWarner Losh errx(1, "Parse error of description string %s", desc); 362*493b2041SWarner Losh } 363*493b2041SWarner Losh 3649c6f9240SPeter Wemm static int 3659c6f9240SPeter Wemm parse_entry(struct mod_metadata *md, const char *cval, 3669c6f9240SPeter Wemm struct elf_file *ef, const char *kldname) 3679c6f9240SPeter Wemm { 3689c6f9240SPeter Wemm struct mod_depend mdp; 3699c6f9240SPeter Wemm struct mod_version mdv; 370*493b2041SWarner Losh struct mod_pnp_match_info pnp; 371*493b2041SWarner Losh char descr[1024]; 3729c6f9240SPeter Wemm Elf_Off data = (Elf_Off)md->md_data; 373*493b2041SWarner Losh int error = 0, i, len; 374*493b2041SWarner Losh char *walker; 375*493b2041SWarner Losh void *table; 3769c6f9240SPeter Wemm 3779c6f9240SPeter Wemm record_start(); 3789c6f9240SPeter Wemm switch (md->md_type) { 3799c6f9240SPeter Wemm case MDT_DEPEND: 3809c6f9240SPeter Wemm if (!dflag) 3819c6f9240SPeter Wemm break; 3829772dc2aSIan Dowse check(EF_SEG_READ(ef, data, sizeof(mdp), &mdp)); 3839c6f9240SPeter Wemm printf(" depends on %s.%d (%d,%d)\n", cval, 3849c6f9240SPeter Wemm mdp.md_ver_preferred, mdp.md_ver_minimum, mdp.md_ver_maximum); 3859c6f9240SPeter Wemm break; 3869c6f9240SPeter Wemm case MDT_VERSION: 3879772dc2aSIan Dowse check(EF_SEG_READ(ef, data, sizeof(mdv), &mdv)); 3889cb138bbSLuigi Rizzo if (dflag) { 3899cb138bbSLuigi Rizzo printf(" interface %s.%d\n", cval, mdv.mv_version); 3909cb138bbSLuigi Rizzo } else { 3919c6f9240SPeter Wemm record_int(MDT_VERSION); 3929c6f9240SPeter Wemm record_string(cval); 3939c6f9240SPeter Wemm record_int(mdv.mv_version); 3949c6f9240SPeter Wemm record_string(kldname); 3959cb138bbSLuigi Rizzo } 3969c6f9240SPeter Wemm break; 3979c6f9240SPeter Wemm case MDT_MODULE: 3989cb138bbSLuigi Rizzo if (dflag) { 3999cb138bbSLuigi Rizzo printf(" module %s\n", cval); 4009cb138bbSLuigi Rizzo } else { 4019c6f9240SPeter Wemm record_int(MDT_MODULE); 4029c6f9240SPeter Wemm record_string(cval); 4039c6f9240SPeter Wemm record_string(kldname); 4049cb138bbSLuigi Rizzo } 4059c6f9240SPeter Wemm break; 406b03747e9SWarner Losh case MDT_PNP_INFO: 407*493b2041SWarner Losh check(EF_SEG_READ_REL(ef, data, sizeof(pnp), &pnp)); 408*493b2041SWarner Losh check(EF_SEG_READ(ef, (Elf_Off)pnp.descr, sizeof(descr), descr)); 409*493b2041SWarner Losh descr[sizeof(descr) - 1] = '\0'; 410b03747e9SWarner Losh if (dflag) { 411*493b2041SWarner Losh printf(" pnp info for bus %s format %s %d entries of %d bytes\n", 412*493b2041SWarner Losh cval, descr, pnp.num_entry, pnp.entry_len); 413*493b2041SWarner Losh } else { 414*493b2041SWarner Losh pnp_list list; 415*493b2041SWarner Losh struct pnp_elt *elt, *elt_tmp; 416*493b2041SWarner Losh char *new_descr; 417*493b2041SWarner Losh 418*493b2041SWarner Losh if (verbose > 1) 419*493b2041SWarner Losh printf(" pnp info for bus %s format %s %d entries of %d bytes\n", 420*493b2041SWarner Losh cval, descr, pnp.num_entry, pnp.entry_len); 421*493b2041SWarner Losh /* 422*493b2041SWarner Losh * Parse descr to weed out the chaff and to create a list 423*493b2041SWarner Losh * of offsets to output. 424*493b2041SWarner Losh */ 425*493b2041SWarner Losh TAILQ_INIT(&list); 426*493b2041SWarner Losh parse_pnp_list(descr, &new_descr, &list); 427*493b2041SWarner Losh record_int(MDT_PNP_INFO); 428*493b2041SWarner Losh record_string(cval); 429*493b2041SWarner Losh record_string(new_descr); 430*493b2041SWarner Losh record_int(pnp.num_entry); 431*493b2041SWarner Losh len = pnp.num_entry * pnp.entry_len; 432*493b2041SWarner Losh walker = table = malloc(len); 433*493b2041SWarner Losh check(EF_SEG_READ_REL(ef, (Elf_Off)pnp.table, len, table)); 434*493b2041SWarner Losh 435*493b2041SWarner Losh /* 436*493b2041SWarner Losh * Walk the list and output things. We've collapsed all the 437*493b2041SWarner Losh * variant forms of the table down to just ints and strings. 438*493b2041SWarner Losh */ 439*493b2041SWarner Losh for (i = 0; i < pnp.num_entry; i++) { 440*493b2041SWarner Losh TAILQ_FOREACH(elt, &list, next) { 441*493b2041SWarner Losh uint8_t v1; 442*493b2041SWarner Losh uint16_t v2; 443*493b2041SWarner Losh uint32_t v4; 444*493b2041SWarner Losh int value; 445*493b2041SWarner Losh char buffer[1024]; 446*493b2041SWarner Losh 447*493b2041SWarner Losh if (elt->pe_kind == TYPE_W32) { 448*493b2041SWarner Losh memcpy(&v4, walker + elt->pe_offset, sizeof(v4)); 449*493b2041SWarner Losh value = v4 & 0xffff; 450*493b2041SWarner Losh record_int(value); 451*493b2041SWarner Losh if (verbose > 1) 452*493b2041SWarner Losh printf("W32:%#x", value); 453*493b2041SWarner Losh value = (v4 >> 16) & 0xffff; 454*493b2041SWarner Losh record_int(value); 455*493b2041SWarner Losh if (verbose > 1) 456*493b2041SWarner Losh printf(":%#x;", value); 457*493b2041SWarner Losh } else if (elt->pe_kind & TYPE_INT) { 458*493b2041SWarner Losh switch (elt->pe_kind & TYPE_SZ_MASK) { 459*493b2041SWarner Losh case 1: 460*493b2041SWarner Losh memcpy(&v1, walker + elt->pe_offset, sizeof(v1)); 461*493b2041SWarner Losh if ((elt->pe_kind & TYPE_FLAGGED) && v1 == 0xff) 462*493b2041SWarner Losh value = -1; 463*493b2041SWarner Losh else 464*493b2041SWarner Losh value = v1; 465*493b2041SWarner Losh break; 466*493b2041SWarner Losh case 2: 467*493b2041SWarner Losh memcpy(&v2, walker + elt->pe_offset, sizeof(v2)); 468*493b2041SWarner Losh if ((elt->pe_kind & TYPE_FLAGGED) && v2 == 0xffff) 469*493b2041SWarner Losh value = -1; 470*493b2041SWarner Losh else 471*493b2041SWarner Losh value = v2; 472*493b2041SWarner Losh break; 473*493b2041SWarner Losh case 4: 474*493b2041SWarner Losh memcpy(&v4, walker + elt->pe_offset, sizeof(v4)); 475*493b2041SWarner Losh if ((elt->pe_kind & TYPE_FLAGGED) && v4 == 0xffffffff) 476*493b2041SWarner Losh value = -1; 477*493b2041SWarner Losh else 478*493b2041SWarner Losh value = v4; 479*493b2041SWarner Losh break; 480*493b2041SWarner Losh default: 481*493b2041SWarner Losh errx(1, "Invalid size somehow %#x", elt->pe_kind); 482b03747e9SWarner Losh } 483*493b2041SWarner Losh if (verbose > 1) 484*493b2041SWarner Losh printf("I:%#x;", value); 485*493b2041SWarner Losh record_int(value); 486*493b2041SWarner Losh } else if (elt->pe_kind == TYPE_T) { 487*493b2041SWarner Losh /* Do nothing */ 488*493b2041SWarner Losh } else { /* E, Z or D -- P already filtered */ 489*493b2041SWarner Losh if (elt->pe_kind == TYPE_E) { 490*493b2041SWarner Losh memcpy(&v4, walker + elt->pe_offset, sizeof(v4)); 491*493b2041SWarner Losh strcpy(buffer, pnp_eisaformat(v4)); 492*493b2041SWarner Losh } else { 493*493b2041SWarner Losh char *ptr; 494*493b2041SWarner Losh 495*493b2041SWarner Losh ptr = *(char **)(walker + elt->pe_offset); 496*493b2041SWarner Losh buffer[0] = '\0'; 497*493b2041SWarner Losh if (ptr != 0) { 498*493b2041SWarner Losh EF_SEG_READ(ef, (Elf_Off)ptr, 499*493b2041SWarner Losh sizeof(buffer), buffer); 500*493b2041SWarner Losh buffer[sizeof(buffer) - 1] = '\0'; 501*493b2041SWarner Losh } 502*493b2041SWarner Losh } 503*493b2041SWarner Losh if (verbose > 1) 504*493b2041SWarner Losh printf("%c:%s;", elt->pe_kind == TYPE_E ? 'E' : (elt->pe_kind == TYPE_Z ? 'Z' : 'D'), buffer); 505*493b2041SWarner Losh record_string(buffer); 506*493b2041SWarner Losh } 507*493b2041SWarner Losh } 508*493b2041SWarner Losh if (verbose > 1) 509*493b2041SWarner Losh printf("\n"); 510*493b2041SWarner Losh walker += pnp.entry_len; 511*493b2041SWarner Losh } 512*493b2041SWarner Losh /* Now free it */ 513*493b2041SWarner Losh TAILQ_FOREACH_SAFE(elt, &list, next, elt_tmp) { 514*493b2041SWarner Losh TAILQ_REMOVE(&list, elt, next); 515*493b2041SWarner Losh free(elt); 516*493b2041SWarner Losh } 517*493b2041SWarner Losh free(table); 518*493b2041SWarner Losh } 519*493b2041SWarner Losh break; 5209c6f9240SPeter Wemm default: 5219f5529b4SRuslan Ermilov warnx("unknown metadata record %d in file %s", md->md_type, kldname); 5229c6f9240SPeter Wemm } 5239c6f9240SPeter Wemm if (!error) 5249c6f9240SPeter Wemm record_end(); 5259c6f9240SPeter Wemm return error; 5269c6f9240SPeter Wemm } 5279c6f9240SPeter Wemm 5289c6f9240SPeter Wemm static int 5299c6f9240SPeter Wemm read_kld(char *filename, char *kldname) 5309c6f9240SPeter Wemm { 5319c6f9240SPeter Wemm struct mod_metadata md; 5329c6f9240SPeter Wemm struct elf_file ef; 5339c6f9240SPeter Wemm void **p, **orgp; 5349772dc2aSIan Dowse int error, eftype, nmlen; 5359c6f9240SPeter Wemm long start, finish, entries; 5369c6f9240SPeter Wemm char kldmodname[MAXMODNAME + 1], cval[MAXMODNAME + 1], *cp; 5379c6f9240SPeter Wemm 5389c6f9240SPeter Wemm if (verbose || dflag) 5399c6f9240SPeter Wemm printf("%s\n", filename); 5409c6f9240SPeter Wemm error = ef_open(filename, &ef, verbose); 5419772dc2aSIan Dowse if (error) { 5424a8b7e33SIan Dowse error = ef_obj_open(filename, &ef, verbose); 5434a8b7e33SIan Dowse if (error) { 5449772dc2aSIan Dowse if (verbose) 5459772dc2aSIan Dowse warnc(error, "elf_open(%s)", filename); 5469c6f9240SPeter Wemm return error; 5479772dc2aSIan Dowse } 5484a8b7e33SIan Dowse } 5499772dc2aSIan Dowse eftype = EF_GET_TYPE(&ef); 5509772dc2aSIan Dowse if (eftype != EFT_KLD && eftype != EFT_KERNEL) { 5519772dc2aSIan Dowse EF_CLOSE(&ef); 5529c6f9240SPeter Wemm return 0; 5539c6f9240SPeter Wemm } 5549c6f9240SPeter Wemm if (!dflag) { 5559c6f9240SPeter Wemm cp = strrchr(kldname, '.'); 5569cb138bbSLuigi Rizzo nmlen = (cp != NULL) ? cp - kldname : (int)strlen(kldname); 5579cb138bbSLuigi Rizzo if (nmlen > MAXMODNAME) 5589cb138bbSLuigi Rizzo nmlen = MAXMODNAME; 5592fdfd0feSWarner Losh strlcpy(kldmodname, kldname, nmlen); 5609c6f9240SPeter Wemm /* fprintf(fxref, "%s:%s:%d\n", kldmodname, kldname, 0);*/ 5619c6f9240SPeter Wemm } 5629c6f9240SPeter Wemm do { 5639772dc2aSIan Dowse check(EF_LOOKUP_SET(&ef, MDT_SETNAME, &start, &finish, 5649772dc2aSIan Dowse &entries)); 5659772dc2aSIan Dowse check(EF_SEG_READ_ENTRY_REL(&ef, start, sizeof(*p) * entries, 56676798703SDag-Erling Smørgrav (void *)&p)); 5679c6f9240SPeter Wemm orgp = p; 5689c6f9240SPeter Wemm while(entries--) { 5699772dc2aSIan Dowse check(EF_SEG_READ_REL(&ef, (Elf_Off)*p, sizeof(md), 5709772dc2aSIan Dowse &md)); 5719c6f9240SPeter Wemm p++; 5729772dc2aSIan Dowse check(EF_SEG_READ(&ef, (Elf_Off)md.md_cval, 5739772dc2aSIan Dowse sizeof(cval), cval)); 5749c6f9240SPeter Wemm cval[MAXMODNAME] = '\0'; 5759c6f9240SPeter Wemm parse_entry(&md, cval, &ef, kldname); 5769c6f9240SPeter Wemm } 5779c6f9240SPeter Wemm if (error) 5789c6f9240SPeter Wemm warnc(error, "error while reading %s", filename); 5799c6f9240SPeter Wemm free(orgp); 5809c6f9240SPeter Wemm } while(0); 5819772dc2aSIan Dowse EF_CLOSE(&ef); 5829c6f9240SPeter Wemm return error; 5839c6f9240SPeter Wemm } 5849c6f9240SPeter Wemm 5859cb138bbSLuigi Rizzo /* 5869cb138bbSLuigi Rizzo * Create a temp file in directory root, make sure we don't 5879cb138bbSLuigi Rizzo * overflow the buffer for the destination name 5889cb138bbSLuigi Rizzo */ 5899cb138bbSLuigi Rizzo static FILE * 5909c6f9240SPeter Wemm maketempfile(char *dest, const char *root) 5919c6f9240SPeter Wemm { 5929c6f9240SPeter Wemm char *p; 5939cb138bbSLuigi Rizzo int n, fd; 5949c6f9240SPeter Wemm 5959cb138bbSLuigi Rizzo p = strrchr(root, '/'); 5969cb138bbSLuigi Rizzo n = p != NULL ? p - root + 1 : 0; 5979cb138bbSLuigi Rizzo if (snprintf(dest, MAXPATHLEN, "%.*slhint.XXXXXX", n, root) >= 5989cb138bbSLuigi Rizzo MAXPATHLEN) { 5999cb138bbSLuigi Rizzo errno = ENAMETOOLONG; 6009cb138bbSLuigi Rizzo return NULL; 6019cb138bbSLuigi Rizzo } 6029c6f9240SPeter Wemm 6039ceddbd5SMarcel Moolenaar fd = mkstemp(dest); 6049cb138bbSLuigi Rizzo if (fd < 0) 6059cb138bbSLuigi Rizzo return NULL; 606ddce5818SLuigi Rizzo fchmod(fd, 0644); /* nothing secret in the file */ 6079cb138bbSLuigi Rizzo return fdopen(fd, "w+"); 6089c6f9240SPeter Wemm } 6099c6f9240SPeter Wemm 6109c6f9240SPeter Wemm static char xrefname[MAXPATHLEN], tempname[MAXPATHLEN]; 6119c6f9240SPeter Wemm 6129cb138bbSLuigi Rizzo static void 6139cb138bbSLuigi Rizzo usage(void) 6149cb138bbSLuigi Rizzo { 6159cb138bbSLuigi Rizzo 6169cb138bbSLuigi Rizzo fprintf(stderr, "%s\n", 6179cb138bbSLuigi Rizzo "usage: kldxref [-Rdv] [-f hintsfile] path ..." 6189cb138bbSLuigi Rizzo ); 6199cb138bbSLuigi Rizzo exit(1); 6209cb138bbSLuigi Rizzo } 6219cb138bbSLuigi Rizzo 6225d452ceaSJilles Tjoelker static int 6236d9cb20bSJilles Tjoelker compare(const FTSENT *const *a, const FTSENT *const *b) 6246d9cb20bSJilles Tjoelker { 6256d9cb20bSJilles Tjoelker if ((*a)->fts_info == FTS_D && (*b)->fts_info != FTS_D) 6266d9cb20bSJilles Tjoelker return 1; 6276d9cb20bSJilles Tjoelker if ((*a)->fts_info != FTS_D && (*b)->fts_info == FTS_D) 6286d9cb20bSJilles Tjoelker return -1; 6296d9cb20bSJilles Tjoelker return strcmp((*a)->fts_name, (*b)->fts_name); 6306d9cb20bSJilles Tjoelker } 6316d9cb20bSJilles Tjoelker 6326d9cb20bSJilles Tjoelker int 6339c6f9240SPeter Wemm main(int argc, char *argv[]) 6349c6f9240SPeter Wemm { 6359c6f9240SPeter Wemm FTS *ftsp; 6369c6f9240SPeter Wemm FTSENT *p; 6379c6f9240SPeter Wemm int opt, fts_options, ival; 638b7abc67eSMaxim Sobolev struct stat sb; 6399c6f9240SPeter Wemm 6409c6f9240SPeter Wemm fts_options = FTS_PHYSICAL; 6419c6f9240SPeter Wemm 6429c6f9240SPeter Wemm while ((opt = getopt(argc, argv, "Rdf:v")) != -1) { 6439c6f9240SPeter Wemm switch (opt) { 6449cb138bbSLuigi Rizzo case 'd': /* no hint file, only print on stdout */ 6459c6f9240SPeter Wemm dflag = 1; 6469c6f9240SPeter Wemm break; 6479cb138bbSLuigi Rizzo case 'f': /* use this name instead of linker.hints */ 6489c6f9240SPeter Wemm xref_file = optarg; 6499c6f9240SPeter Wemm break; 6509c6f9240SPeter Wemm case 'v': 6519c6f9240SPeter Wemm verbose++; 6529c6f9240SPeter Wemm break; 6539cb138bbSLuigi Rizzo case 'R': /* recurse on directories */ 6549c6f9240SPeter Wemm fts_options |= FTS_COMFOLLOW; 6559c6f9240SPeter Wemm break; 6569c6f9240SPeter Wemm default: 6579c6f9240SPeter Wemm usage(); 6589c6f9240SPeter Wemm /* NOTREACHED */ 6599c6f9240SPeter Wemm } 6609c6f9240SPeter Wemm } 6619c6f9240SPeter Wemm if (argc - optind < 1) 6629c6f9240SPeter Wemm usage(); 6639c6f9240SPeter Wemm argc -= optind; 6649c6f9240SPeter Wemm argv += optind; 6659c6f9240SPeter Wemm 666b7abc67eSMaxim Sobolev if (stat(argv[0], &sb) != 0) 667b7abc67eSMaxim Sobolev err(1, "%s", argv[0]); 668b7abc67eSMaxim Sobolev if ((sb.st_mode & S_IFDIR) == 0) { 669b7abc67eSMaxim Sobolev errno = ENOTDIR; 670b7abc67eSMaxim Sobolev err(1, "%s", argv[0]); 671b7abc67eSMaxim Sobolev } 672b7abc67eSMaxim Sobolev 6736d9cb20bSJilles Tjoelker ftsp = fts_open(argv, fts_options, compare); 6749c6f9240SPeter Wemm if (ftsp == NULL) 6759c6f9240SPeter Wemm exit(1); 6769c6f9240SPeter Wemm 6779c6f9240SPeter Wemm for (;;) { 6789c6f9240SPeter Wemm p = fts_read(ftsp); 6799cb138bbSLuigi Rizzo if ((p == NULL || p->fts_info == FTS_D) && fxref) { 6809cb138bbSLuigi Rizzo /* close and rename the current hint file */ 6819c6f9240SPeter Wemm fclose(fxref); 6829ceddbd5SMarcel Moolenaar fxref = NULL; 6839c6f9240SPeter Wemm if (reccnt) { 6849c6f9240SPeter Wemm rename(tempname, xrefname); 6859c6f9240SPeter Wemm } else { 6869cb138bbSLuigi Rizzo /* didn't find any entry, ignore this file */ 6879c6f9240SPeter Wemm unlink(tempname); 6889c6f9240SPeter Wemm unlink(xrefname); 6899c6f9240SPeter Wemm } 6909c6f9240SPeter Wemm } 6919c6f9240SPeter Wemm if (p == NULL) 6929c6f9240SPeter Wemm break; 6939cb138bbSLuigi Rizzo if (p->fts_info == FTS_D && !dflag) { 6949cb138bbSLuigi Rizzo /* visiting a new directory, create a new hint file */ 6959c6f9240SPeter Wemm snprintf(xrefname, sizeof(xrefname), "%s/%s", 6969c6f9240SPeter Wemm ftsp->fts_path, xref_file); 6979ceddbd5SMarcel Moolenaar fxref = maketempfile(tempname, ftsp->fts_path); 6989c6f9240SPeter Wemm if (fxref == NULL) 6999c6f9240SPeter Wemm err(1, "can't create %s", tempname); 7009c6f9240SPeter Wemm ival = 1; 7019c6f9240SPeter Wemm fwrite(&ival, sizeof(ival), 1, fxref); 7029c6f9240SPeter Wemm reccnt = 0; 7039c6f9240SPeter Wemm } 704113a1a21SEd Maste /* skip non-files and separate debug files */ 7059c6f9240SPeter Wemm if (p->fts_info != FTS_F) 7069c6f9240SPeter Wemm continue; 707113a1a21SEd Maste if (p->fts_namelen >= 6 && 708113a1a21SEd Maste strcmp(p->fts_name + p->fts_namelen - 6, ".debug") == 0) 709113a1a21SEd Maste continue; 7109f5529b4SRuslan Ermilov if (p->fts_namelen >= 8 && 7119f5529b4SRuslan Ermilov strcmp(p->fts_name + p->fts_namelen - 8, ".symbols") == 0) 7129f5529b4SRuslan Ermilov continue; 7139c6f9240SPeter Wemm read_kld(p->fts_path, p->fts_name); 7149c6f9240SPeter Wemm } 7159c6f9240SPeter Wemm fts_close(ftsp); 7169c6f9240SPeter Wemm return 0; 7179c6f9240SPeter Wemm } 718