1df57947fSPedro F. Giffuni /*- 2df57947fSPedro F. Giffuni * SPDX-License-Identifier: BSD-4-Clause 3df57947fSPedro F. Giffuni * 49c6f9240SPeter Wemm * Copyright (c) 2000, Boris Popov 59c6f9240SPeter Wemm * All rights reserved. 69c6f9240SPeter Wemm * 79c6f9240SPeter Wemm * Redistribution and use in source and binary forms, with or without 89c6f9240SPeter Wemm * modification, are permitted provided that the following conditions 99c6f9240SPeter Wemm * are met: 109c6f9240SPeter Wemm * 1. Redistributions of source code must retain the above copyright 119c6f9240SPeter Wemm * notice, this list of conditions and the following disclaimer. 129c6f9240SPeter Wemm * 2. Redistributions in binary form must reproduce the above copyright 139c6f9240SPeter Wemm * notice, this list of conditions and the following disclaimer in the 149c6f9240SPeter Wemm * documentation and/or other materials provided with the distribution. 159c6f9240SPeter Wemm * 3. All advertising materials mentioning features or use of this software 169c6f9240SPeter Wemm * must display the following acknowledgement: 179c6f9240SPeter Wemm * This product includes software developed by Boris Popov. 189c6f9240SPeter Wemm * 4. Neither the name of the author nor the names of any co-contributors 199c6f9240SPeter Wemm * may be used to endorse or promote products derived from this software 209c6f9240SPeter Wemm * without specific prior written permission. 219c6f9240SPeter Wemm * 229c6f9240SPeter Wemm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 239c6f9240SPeter Wemm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 249c6f9240SPeter Wemm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 259c6f9240SPeter Wemm * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 269c6f9240SPeter Wemm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 279c6f9240SPeter Wemm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 289c6f9240SPeter Wemm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 299c6f9240SPeter Wemm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 309c6f9240SPeter Wemm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 319c6f9240SPeter Wemm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 329c6f9240SPeter Wemm * SUCH DAMAGE. 339c6f9240SPeter Wemm * 349c6f9240SPeter Wemm * $FreeBSD$ 359c6f9240SPeter Wemm */ 369c6f9240SPeter Wemm 37b7abc67eSMaxim Sobolev #include <sys/types.h> 389c6f9240SPeter Wemm #include <sys/param.h> 39493b2041SWarner Losh #include <sys/endian.h> 409c6f9240SPeter Wemm #include <sys/exec.h> 419c6f9240SPeter Wemm #include <sys/queue.h> 429c6f9240SPeter Wemm #include <sys/kernel.h> 439c6f9240SPeter Wemm #include <sys/reboot.h> 449c6f9240SPeter Wemm #include <sys/linker.h> 459c6f9240SPeter Wemm #include <sys/stat.h> 469c6f9240SPeter Wemm #include <sys/module.h> 479c6f9240SPeter Wemm #define FREEBSD_ELF 48e2d0802cSEd Maste 49*517a1827SWarner Losh #include <ctype.h> 509c6f9240SPeter Wemm #include <err.h> 51e2d0802cSEd Maste #include <errno.h> 529c6f9240SPeter Wemm #include <fts.h> 53e2d0802cSEd Maste #include <stdbool.h> 549c6f9240SPeter Wemm #include <stdio.h> 559c6f9240SPeter Wemm #include <stdlib.h> 56e2d0802cSEd Maste #include <string.h> 579c6f9240SPeter Wemm #include <unistd.h> 58e2d0802cSEd Maste #include <machine/elf.h> 599c6f9240SPeter Wemm 609c6f9240SPeter Wemm #include "ef.h" 619c6f9240SPeter Wemm 62493b2041SWarner Losh #define MAXRECSIZE (64 << 10) /* 64k */ 639c6f9240SPeter Wemm #define check(val) if ((error = (val)) != 0) break 649c6f9240SPeter Wemm 65e2d0802cSEd Maste static bool dflag; /* do not create a hint file, only write on stdout */ 669cb138bbSLuigi Rizzo static int verbose; 679c6f9240SPeter Wemm 689cb138bbSLuigi Rizzo static FILE *fxref; /* current hints file */ 699c6f9240SPeter Wemm 7087e5cd7cSMike Heffner static const char *xref_file = "linker.hints"; 719c6f9240SPeter Wemm 729cb138bbSLuigi Rizzo /* 739cb138bbSLuigi Rizzo * A record is stored in the static buffer recbuf before going to disk. 749cb138bbSLuigi Rizzo */ 759c6f9240SPeter Wemm static char recbuf[MAXRECSIZE]; 769cb138bbSLuigi Rizzo static int recpos; /* current write position */ 779cb138bbSLuigi Rizzo static int reccnt; /* total record written to this file so far */ 789c6f9240SPeter Wemm 799c6f9240SPeter Wemm static void 809c6f9240SPeter Wemm intalign(void) 819c6f9240SPeter Wemm { 82e2d0802cSEd Maste 83bf6911cdSMarcelo Araujo recpos = roundup2(recpos, sizeof(int)); 849c6f9240SPeter Wemm } 859c6f9240SPeter Wemm 869c6f9240SPeter Wemm static void 879c6f9240SPeter Wemm record_start(void) 889c6f9240SPeter Wemm { 89e2d0802cSEd Maste 909c6f9240SPeter Wemm recpos = 0; 919c6f9240SPeter Wemm memset(recbuf, 0, MAXRECSIZE); 929c6f9240SPeter Wemm } 939c6f9240SPeter Wemm 949c6f9240SPeter Wemm static int 959c6f9240SPeter Wemm record_end(void) 969c6f9240SPeter Wemm { 97e2d0802cSEd Maste 989cb138bbSLuigi Rizzo if (recpos == 0) 99e2d0802cSEd Maste return (0); 1009c6f9240SPeter Wemm reccnt++; 1019c6f9240SPeter Wemm intalign(); 1029c6f9240SPeter Wemm fwrite(&recpos, sizeof(recpos), 1, fxref); 103e2d0802cSEd Maste return (fwrite(recbuf, recpos, 1, fxref) != 1 ? errno : 0); 1049c6f9240SPeter Wemm } 1059c6f9240SPeter Wemm 1069c6f9240SPeter Wemm static int 107e2d0802cSEd Maste record_buf(const void *buf, size_t size) 1089c6f9240SPeter Wemm { 109e2d0802cSEd Maste 1109c6f9240SPeter Wemm if (MAXRECSIZE - recpos < size) 1119c6f9240SPeter Wemm errx(1, "record buffer overflow"); 1129c6f9240SPeter Wemm memcpy(recbuf + recpos, buf, size); 1139c6f9240SPeter Wemm recpos += size; 114e2d0802cSEd Maste return (0); 1159c6f9240SPeter Wemm } 1169c6f9240SPeter Wemm 1179cb138bbSLuigi Rizzo /* 1189cb138bbSLuigi Rizzo * An int is stored in host order and aligned 1199cb138bbSLuigi Rizzo */ 1209c6f9240SPeter Wemm static int 1219c6f9240SPeter Wemm record_int(int val) 1229c6f9240SPeter Wemm { 123e2d0802cSEd Maste 1249c6f9240SPeter Wemm intalign(); 125e2d0802cSEd Maste return (record_buf(&val, sizeof(val))); 1269c6f9240SPeter Wemm } 1279c6f9240SPeter Wemm 1289cb138bbSLuigi Rizzo /* 1299cb138bbSLuigi Rizzo * A string is stored as 1-byte length plus data, no padding 1309cb138bbSLuigi Rizzo */ 1319c6f9240SPeter Wemm static int 1329c6f9240SPeter Wemm record_string(const char *str) 1339c6f9240SPeter Wemm { 134e2d0802cSEd Maste int error; 135e2d0802cSEd Maste size_t len; 1369cb138bbSLuigi Rizzo u_char val; 1379c6f9240SPeter Wemm 1389c6f9240SPeter Wemm if (dflag) 139e2d0802cSEd Maste return (0); 1409cb138bbSLuigi Rizzo val = len = strlen(str); 1419cb138bbSLuigi Rizzo if (len > 255) 1429cb138bbSLuigi Rizzo errx(1, "string %s too long", str); 1439cb138bbSLuigi Rizzo error = record_buf(&val, sizeof(val)); 144e2d0802cSEd Maste if (error != 0) 145e2d0802cSEd Maste return (error); 146e2d0802cSEd Maste return (record_buf(str, len)); 1479c6f9240SPeter Wemm } 1489c6f9240SPeter Wemm 149493b2041SWarner Losh /* From sys/isa/pnp.c */ 150493b2041SWarner Losh static char * 151493b2041SWarner Losh pnp_eisaformat(uint32_t id) 152493b2041SWarner Losh { 153493b2041SWarner Losh uint8_t *data; 154493b2041SWarner Losh static char idbuf[8]; 155493b2041SWarner Losh const char hextoascii[] = "0123456789abcdef"; 156493b2041SWarner Losh 157493b2041SWarner Losh id = htole32(id); 158493b2041SWarner Losh data = (uint8_t *)&id; 159493b2041SWarner Losh idbuf[0] = '@' + ((data[0] & 0x7c) >> 2); 160493b2041SWarner Losh idbuf[1] = '@' + (((data[0] & 0x3) << 3) + ((data[1] & 0xe0) >> 5)); 161493b2041SWarner Losh idbuf[2] = '@' + (data[1] & 0x1f); 162493b2041SWarner Losh idbuf[3] = hextoascii[(data[2] >> 4)]; 163493b2041SWarner Losh idbuf[4] = hextoascii[(data[2] & 0xf)]; 164493b2041SWarner Losh idbuf[5] = hextoascii[(data[3] >> 4)]; 165493b2041SWarner Losh idbuf[6] = hextoascii[(data[3] & 0xf)]; 166493b2041SWarner Losh idbuf[7] = 0; 167493b2041SWarner Losh return (idbuf); 168493b2041SWarner Losh } 169493b2041SWarner Losh 170493b2041SWarner Losh struct pnp_elt 171493b2041SWarner Losh { 172493b2041SWarner Losh int pe_kind; /* What kind of entry */ 173493b2041SWarner Losh #define TYPE_SZ_MASK 0x0f 174493b2041SWarner Losh #define TYPE_FLAGGED 0x10 /* all f's is a wildcard */ 175493b2041SWarner Losh #define TYPE_INT 0x20 /* Is a number */ 176493b2041SWarner Losh #define TYPE_PAIRED 0x40 177493b2041SWarner Losh #define TYPE_LE 0x80 /* Matches <= this value */ 178493b2041SWarner Losh #define TYPE_GE 0x100 /* Matches >= this value */ 179493b2041SWarner Losh #define TYPE_MASK 0x200 /* Specifies a mask to follow */ 180493b2041SWarner Losh #define TYPE_U8 (1 | TYPE_INT) 181493b2041SWarner Losh #define TYPE_V8 (1 | TYPE_INT | TYPE_FLAGGED) 182493b2041SWarner Losh #define TYPE_G16 (2 | TYPE_INT | TYPE_GE) 183493b2041SWarner Losh #define TYPE_L16 (2 | TYPE_INT | TYPE_LE) 184493b2041SWarner Losh #define TYPE_M16 (2 | TYPE_INT | TYPE_MASK) 185493b2041SWarner Losh #define TYPE_U16 (2 | TYPE_INT) 186493b2041SWarner Losh #define TYPE_V16 (2 | TYPE_INT | TYPE_FLAGGED) 187493b2041SWarner Losh #define TYPE_U32 (4 | TYPE_INT) 188493b2041SWarner Losh #define TYPE_V32 (4 | TYPE_INT | TYPE_FLAGGED) 189493b2041SWarner Losh #define TYPE_W32 (4 | TYPE_INT | TYPE_PAIRED) 190493b2041SWarner Losh #define TYPE_D 7 191493b2041SWarner Losh #define TYPE_Z 8 192493b2041SWarner Losh #define TYPE_P 9 193493b2041SWarner Losh #define TYPE_E 10 194493b2041SWarner Losh #define TYPE_T 11 195493b2041SWarner Losh int pe_offset; /* Offset within the element */ 196493b2041SWarner Losh char * pe_key; /* pnp key name */ 197493b2041SWarner Losh TAILQ_ENTRY(pnp_elt) next; /* Link */ 198493b2041SWarner Losh }; 199493b2041SWarner Losh typedef TAILQ_HEAD(pnp_head, pnp_elt) pnp_list; 200493b2041SWarner Losh 201493b2041SWarner Losh /* 202493b2041SWarner Losh * this function finds the data from the pnp table, as described by the 203493b2041SWarner Losh * the description and creates a new output (new_desc). This output table 204493b2041SWarner Losh * is a form that's easier for the agent that's automatically loading the 205493b2041SWarner Losh * modules. 206493b2041SWarner Losh * 207493b2041SWarner Losh * The format output is the simplified string from this routine in the 208493b2041SWarner Losh * same basic format as the pnp string, as documented in sys/module.h. 209493b2041SWarner Losh * First a string describing the format is output, the a count of the 210493b2041SWarner Losh * number of records, then each record. The format string also describes 211493b2041SWarner Losh * the length of each entry (though it isn't a fixed length when strings 212493b2041SWarner Losh * are present). 213493b2041SWarner Losh * 214493b2041SWarner Losh * type Output Meaning 215493b2041SWarner Losh * I uint32_t Integer equality comparison 216493b2041SWarner Losh * J uint32_t Pair of uint16_t fields converted to native 217a35ddacaSWarner Losh * byte order. The two fields both must match. 218493b2041SWarner Losh * G uint32_t Greater than or equal to 219493b2041SWarner Losh * L uint32_t Less than or equal to 220493b2041SWarner Losh * M uint32_t Mask of which fields to test. Fields that 221a35ddacaSWarner Losh * take up space increment the count. This 222a35ddacaSWarner Losh * field must be first, and resets the count. 223493b2041SWarner Losh * D string Description of the device this pnp info is for 224493b2041SWarner Losh * Z string pnp string must match this 225493b2041SWarner Losh * T nothing T fields set pnp values that must be true for 226493b2041SWarner Losh * the entire table. 227493b2041SWarner Losh * Values are packed the same way that other values are packed in this file. 228493b2041SWarner Losh * Strings and int32_t's start on a 32-bit boundary and are padded with 0 229493b2041SWarner Losh * bytes. Objects that are smaller than uint32_t are converted, without 230493b2041SWarner Losh * sign extension to uint32_t to simplify parsing downstream. 231493b2041SWarner Losh */ 232493b2041SWarner Losh static int 233493b2041SWarner Losh parse_pnp_list(const char *desc, char **new_desc, pnp_list *list) 234493b2041SWarner Losh { 235e2d0802cSEd Maste const char *walker, *ep; 236493b2041SWarner Losh const char *colon, *semi; 237493b2041SWarner Losh struct pnp_elt *elt; 238493b2041SWarner Losh char *nd; 239493b2041SWarner Losh char type[8], key[32]; 240493b2041SWarner Losh int off; 241493b2041SWarner Losh 242e2d0802cSEd Maste walker = desc; 243e2d0802cSEd Maste ep = desc + strlen(desc); 244493b2041SWarner Losh off = 0; 245493b2041SWarner Losh nd = *new_desc = malloc(strlen(desc) + 1); 246493b2041SWarner Losh if (verbose > 1) 247493b2041SWarner Losh printf("Converting %s into a list\n", desc); 248493b2041SWarner Losh while (walker < ep) { 249493b2041SWarner Losh colon = strchr(walker, ':'); 250493b2041SWarner Losh semi = strchr(walker, ';'); 251493b2041SWarner Losh if (semi != NULL && semi < colon) 252493b2041SWarner Losh goto err; 253493b2041SWarner Losh if (colon - walker > sizeof(type)) 254493b2041SWarner Losh goto err; 255493b2041SWarner Losh strncpy(type, walker, colon - walker); 256493b2041SWarner Losh type[colon - walker] = '\0'; 257e2d0802cSEd Maste if (semi != NULL) { 258493b2041SWarner Losh if (semi - colon >= sizeof(key)) 259493b2041SWarner Losh goto err; 260493b2041SWarner Losh strncpy(key, colon + 1, semi - colon - 1); 261493b2041SWarner Losh key[semi - colon - 1] = '\0'; 262493b2041SWarner Losh walker = semi + 1; 263*517a1827SWarner Losh /* Fail safe if we have spaces after ; */ 264*517a1827SWarner Losh while (walker < ep && isspace(*walker)) 265*517a1827SWarner Losh walker++; 266493b2041SWarner Losh } else { 267493b2041SWarner Losh if (strlen(colon + 1) >= sizeof(key)) 268493b2041SWarner Losh goto err; 269493b2041SWarner Losh strcpy(key, colon + 1); 270493b2041SWarner Losh walker = ep; 271493b2041SWarner Losh } 272493b2041SWarner Losh if (verbose > 1) 273493b2041SWarner Losh printf("Found type %s for name %s\n", type, key); 274493b2041SWarner Losh /* Skip pointer place holders */ 275493b2041SWarner Losh if (strcmp(type, "P") == 0) { 276493b2041SWarner Losh off += sizeof(void *); 277493b2041SWarner Losh continue; 278493b2041SWarner Losh } 279493b2041SWarner Losh 280493b2041SWarner Losh /* 281493b2041SWarner Losh * Add a node of the appropriate type 282493b2041SWarner Losh */ 283493b2041SWarner Losh elt = malloc(sizeof(struct pnp_elt) + strlen(key) + 1); 284493b2041SWarner Losh TAILQ_INSERT_TAIL(list, elt, next); 285493b2041SWarner Losh elt->pe_key = (char *)(elt + 1); 286493b2041SWarner Losh elt->pe_offset = off; 287493b2041SWarner Losh if (strcmp(type, "U8") == 0) 288493b2041SWarner Losh elt->pe_kind = TYPE_U8; 289493b2041SWarner Losh else if (strcmp(type, "V8") == 0) 290493b2041SWarner Losh elt->pe_kind = TYPE_V8; 291493b2041SWarner Losh else if (strcmp(type, "G16") == 0) 292493b2041SWarner Losh elt->pe_kind = TYPE_G16; 293493b2041SWarner Losh else if (strcmp(type, "L16") == 0) 294493b2041SWarner Losh elt->pe_kind = TYPE_L16; 295493b2041SWarner Losh else if (strcmp(type, "M16") == 0) 296493b2041SWarner Losh elt->pe_kind = TYPE_M16; 297493b2041SWarner Losh else if (strcmp(type, "U16") == 0) 298493b2041SWarner Losh elt->pe_kind = TYPE_U16; 299493b2041SWarner Losh else if (strcmp(type, "V16") == 0) 300493b2041SWarner Losh elt->pe_kind = TYPE_V16; 301493b2041SWarner Losh else if (strcmp(type, "U32") == 0) 302493b2041SWarner Losh elt->pe_kind = TYPE_U32; 303493b2041SWarner Losh else if (strcmp(type, "V32") == 0) 304493b2041SWarner Losh elt->pe_kind = TYPE_V32; 305493b2041SWarner Losh else if (strcmp(type, "W32") == 0) 306493b2041SWarner Losh elt->pe_kind = TYPE_W32; 307493b2041SWarner Losh else if (strcmp(type, "D") == 0) /* description char * */ 308493b2041SWarner Losh elt->pe_kind = TYPE_D; 309493b2041SWarner Losh else if (strcmp(type, "Z") == 0) /* char * to match */ 310493b2041SWarner Losh elt->pe_kind = TYPE_Z; 311493b2041SWarner Losh else if (strcmp(type, "P") == 0) /* Pointer -- ignored */ 312493b2041SWarner Losh elt->pe_kind = TYPE_P; 313493b2041SWarner Losh else if (strcmp(type, "E") == 0) /* EISA PNP ID, as uint32_t */ 314493b2041SWarner Losh elt->pe_kind = TYPE_E; 315493b2041SWarner Losh else if (strcmp(type, "T") == 0) 316493b2041SWarner Losh elt->pe_kind = TYPE_T; 317493b2041SWarner Losh else 318493b2041SWarner Losh goto err; 319493b2041SWarner Losh /* 320493b2041SWarner Losh * Maybe the rounding here needs to be more nuanced and/or somehow 321493b2041SWarner Losh * architecture specific. Fortunately, most tables in the system 322493b2041SWarner Losh * have sane ordering of types. 323493b2041SWarner Losh */ 324493b2041SWarner Losh if (elt->pe_kind & TYPE_INT) { 325493b2041SWarner Losh elt->pe_offset = roundup2(elt->pe_offset, elt->pe_kind & TYPE_SZ_MASK); 326493b2041SWarner Losh off = elt->pe_offset + (elt->pe_kind & TYPE_SZ_MASK); 327493b2041SWarner Losh } else if (elt->pe_kind == TYPE_E) { 328493b2041SWarner Losh /* Type E stored as Int, displays as string */ 329493b2041SWarner Losh elt->pe_offset = roundup2(elt->pe_offset, sizeof(uint32_t)); 330493b2041SWarner Losh off = elt->pe_offset + sizeof(uint32_t); 331493b2041SWarner Losh } else if (elt->pe_kind == TYPE_T) { 332493b2041SWarner Losh /* doesn't actually consume space in the table */ 333493b2041SWarner Losh off = elt->pe_offset; 334493b2041SWarner Losh } else { 335493b2041SWarner Losh elt->pe_offset = roundup2(elt->pe_offset, sizeof(void *)); 336493b2041SWarner Losh off = elt->pe_offset + sizeof(void *); 337493b2041SWarner Losh } 338493b2041SWarner Losh if (elt->pe_kind & TYPE_PAIRED) { 339493b2041SWarner Losh char *word, *ctx; 340493b2041SWarner Losh 341493b2041SWarner Losh for (word = strtok_r(key, "/", &ctx); 342493b2041SWarner Losh word; word = strtok_r(NULL, "/", &ctx)) { 343493b2041SWarner Losh sprintf(nd, "%c:%s;", elt->pe_kind & TYPE_FLAGGED ? 'J' : 'I', 344493b2041SWarner Losh word); 345493b2041SWarner Losh nd += strlen(nd); 346493b2041SWarner Losh } 347493b2041SWarner Losh 348493b2041SWarner Losh } 349493b2041SWarner Losh else { 350493b2041SWarner Losh if (elt->pe_kind & TYPE_FLAGGED) 351493b2041SWarner Losh *nd++ = 'J'; 352493b2041SWarner Losh else if (elt->pe_kind & TYPE_GE) 353493b2041SWarner Losh *nd++ = 'G'; 354493b2041SWarner Losh else if (elt->pe_kind & TYPE_LE) 355493b2041SWarner Losh *nd++ = 'L'; 356493b2041SWarner Losh else if (elt->pe_kind & TYPE_MASK) 357493b2041SWarner Losh *nd++ = 'M'; 358493b2041SWarner Losh else if (elt->pe_kind & TYPE_INT) 359493b2041SWarner Losh *nd++ = 'I'; 360493b2041SWarner Losh else if (elt->pe_kind == TYPE_D) 361493b2041SWarner Losh *nd++ = 'D'; 362493b2041SWarner Losh else if (elt->pe_kind == TYPE_Z || elt->pe_kind == TYPE_E) 363493b2041SWarner Losh *nd++ = 'Z'; 364493b2041SWarner Losh else if (elt->pe_kind == TYPE_T) 365493b2041SWarner Losh *nd++ = 'T'; 366493b2041SWarner Losh else 367493b2041SWarner Losh errx(1, "Impossible type %x\n", elt->pe_kind); 368493b2041SWarner Losh *nd++ = ':'; 369493b2041SWarner Losh strcpy(nd, key); 370493b2041SWarner Losh nd += strlen(nd); 371493b2041SWarner Losh *nd++ = ';'; 372493b2041SWarner Losh } 373493b2041SWarner Losh } 374493b2041SWarner Losh *nd++ = '\0'; 375e2d0802cSEd Maste return (0); 376493b2041SWarner Losh err: 377493b2041SWarner Losh errx(1, "Parse error of description string %s", desc); 378493b2041SWarner Losh } 379493b2041SWarner Losh 3809c6f9240SPeter Wemm static int 3819c6f9240SPeter Wemm parse_entry(struct mod_metadata *md, const char *cval, 3829c6f9240SPeter Wemm struct elf_file *ef, const char *kldname) 3839c6f9240SPeter Wemm { 3849c6f9240SPeter Wemm struct mod_depend mdp; 3859c6f9240SPeter Wemm struct mod_version mdv; 386493b2041SWarner Losh struct mod_pnp_match_info pnp; 387493b2041SWarner Losh char descr[1024]; 388e2d0802cSEd Maste Elf_Off data; 389e2d0802cSEd Maste int error, i; 390e2d0802cSEd Maste size_t len; 391493b2041SWarner Losh char *walker; 392493b2041SWarner Losh void *table; 3939c6f9240SPeter Wemm 394e2d0802cSEd Maste data = (Elf_Off)md->md_data; 395e2d0802cSEd Maste error = 0; 3969c6f9240SPeter Wemm record_start(); 3979c6f9240SPeter Wemm switch (md->md_type) { 3989c6f9240SPeter Wemm case MDT_DEPEND: 3999c6f9240SPeter Wemm if (!dflag) 4009c6f9240SPeter Wemm break; 4019772dc2aSIan Dowse check(EF_SEG_READ(ef, data, sizeof(mdp), &mdp)); 4029c6f9240SPeter Wemm printf(" depends on %s.%d (%d,%d)\n", cval, 4039c6f9240SPeter Wemm mdp.md_ver_preferred, mdp.md_ver_minimum, mdp.md_ver_maximum); 4049c6f9240SPeter Wemm break; 4059c6f9240SPeter Wemm case MDT_VERSION: 4069772dc2aSIan Dowse check(EF_SEG_READ(ef, data, sizeof(mdv), &mdv)); 4079cb138bbSLuigi Rizzo if (dflag) { 4089cb138bbSLuigi Rizzo printf(" interface %s.%d\n", cval, mdv.mv_version); 4099cb138bbSLuigi Rizzo } else { 4109c6f9240SPeter Wemm record_int(MDT_VERSION); 4119c6f9240SPeter Wemm record_string(cval); 4129c6f9240SPeter Wemm record_int(mdv.mv_version); 4139c6f9240SPeter Wemm record_string(kldname); 4149cb138bbSLuigi Rizzo } 4159c6f9240SPeter Wemm break; 4169c6f9240SPeter Wemm case MDT_MODULE: 4179cb138bbSLuigi Rizzo if (dflag) { 4189cb138bbSLuigi Rizzo printf(" module %s\n", cval); 4199cb138bbSLuigi Rizzo } else { 4209c6f9240SPeter Wemm record_int(MDT_MODULE); 4219c6f9240SPeter Wemm record_string(cval); 4229c6f9240SPeter Wemm record_string(kldname); 4239cb138bbSLuigi Rizzo } 4249c6f9240SPeter Wemm break; 425b03747e9SWarner Losh case MDT_PNP_INFO: 426493b2041SWarner Losh check(EF_SEG_READ_REL(ef, data, sizeof(pnp), &pnp)); 427ee150a33SWarner Losh check(EF_SEG_READ_STRING(ef, (Elf_Off)pnp.descr, sizeof(descr), descr)); 428493b2041SWarner Losh descr[sizeof(descr) - 1] = '\0'; 429b03747e9SWarner Losh if (dflag) { 430493b2041SWarner Losh printf(" pnp info for bus %s format %s %d entries of %d bytes\n", 431493b2041SWarner Losh cval, descr, pnp.num_entry, pnp.entry_len); 432493b2041SWarner Losh } else { 433493b2041SWarner Losh pnp_list list; 434493b2041SWarner Losh struct pnp_elt *elt, *elt_tmp; 435493b2041SWarner Losh char *new_descr; 436493b2041SWarner Losh 437493b2041SWarner Losh if (verbose > 1) 438493b2041SWarner Losh printf(" pnp info for bus %s format %s %d entries of %d bytes\n", 439493b2041SWarner Losh cval, descr, pnp.num_entry, pnp.entry_len); 440493b2041SWarner Losh /* 441493b2041SWarner Losh * Parse descr to weed out the chaff and to create a list 442493b2041SWarner Losh * of offsets to output. 443493b2041SWarner Losh */ 444493b2041SWarner Losh TAILQ_INIT(&list); 445493b2041SWarner Losh parse_pnp_list(descr, &new_descr, &list); 446493b2041SWarner Losh record_int(MDT_PNP_INFO); 447493b2041SWarner Losh record_string(cval); 448493b2041SWarner Losh record_string(new_descr); 449493b2041SWarner Losh record_int(pnp.num_entry); 450493b2041SWarner Losh len = pnp.num_entry * pnp.entry_len; 451493b2041SWarner Losh walker = table = malloc(len); 452493b2041SWarner Losh check(EF_SEG_READ_REL(ef, (Elf_Off)pnp.table, len, table)); 453493b2041SWarner Losh 454493b2041SWarner Losh /* 455493b2041SWarner Losh * Walk the list and output things. We've collapsed all the 456493b2041SWarner Losh * variant forms of the table down to just ints and strings. 457493b2041SWarner Losh */ 458493b2041SWarner Losh for (i = 0; i < pnp.num_entry; i++) { 459493b2041SWarner Losh TAILQ_FOREACH(elt, &list, next) { 460493b2041SWarner Losh uint8_t v1; 461493b2041SWarner Losh uint16_t v2; 462493b2041SWarner Losh uint32_t v4; 463493b2041SWarner Losh int value; 464493b2041SWarner Losh char buffer[1024]; 465493b2041SWarner Losh 466493b2041SWarner Losh if (elt->pe_kind == TYPE_W32) { 467493b2041SWarner Losh memcpy(&v4, walker + elt->pe_offset, sizeof(v4)); 468493b2041SWarner Losh value = v4 & 0xffff; 469493b2041SWarner Losh record_int(value); 470493b2041SWarner Losh if (verbose > 1) 471493b2041SWarner Losh printf("W32:%#x", value); 472493b2041SWarner Losh value = (v4 >> 16) & 0xffff; 473493b2041SWarner Losh record_int(value); 474493b2041SWarner Losh if (verbose > 1) 475493b2041SWarner Losh printf(":%#x;", value); 476493b2041SWarner Losh } else if (elt->pe_kind & TYPE_INT) { 477493b2041SWarner Losh switch (elt->pe_kind & TYPE_SZ_MASK) { 478493b2041SWarner Losh case 1: 479493b2041SWarner Losh memcpy(&v1, walker + elt->pe_offset, sizeof(v1)); 480493b2041SWarner Losh if ((elt->pe_kind & TYPE_FLAGGED) && v1 == 0xff) 481493b2041SWarner Losh value = -1; 482493b2041SWarner Losh else 483493b2041SWarner Losh value = v1; 484493b2041SWarner Losh break; 485493b2041SWarner Losh case 2: 486493b2041SWarner Losh memcpy(&v2, walker + elt->pe_offset, sizeof(v2)); 487493b2041SWarner Losh if ((elt->pe_kind & TYPE_FLAGGED) && v2 == 0xffff) 488493b2041SWarner Losh value = -1; 489493b2041SWarner Losh else 490493b2041SWarner Losh value = v2; 491493b2041SWarner Losh break; 492493b2041SWarner Losh case 4: 493493b2041SWarner Losh memcpy(&v4, walker + elt->pe_offset, sizeof(v4)); 494493b2041SWarner Losh if ((elt->pe_kind & TYPE_FLAGGED) && v4 == 0xffffffff) 495493b2041SWarner Losh value = -1; 496493b2041SWarner Losh else 497493b2041SWarner Losh value = v4; 498493b2041SWarner Losh break; 499493b2041SWarner Losh default: 500493b2041SWarner Losh errx(1, "Invalid size somehow %#x", elt->pe_kind); 501b03747e9SWarner Losh } 502493b2041SWarner Losh if (verbose > 1) 503493b2041SWarner Losh printf("I:%#x;", value); 504493b2041SWarner Losh record_int(value); 505493b2041SWarner Losh } else if (elt->pe_kind == TYPE_T) { 506493b2041SWarner Losh /* Do nothing */ 507493b2041SWarner Losh } else { /* E, Z or D -- P already filtered */ 508493b2041SWarner Losh if (elt->pe_kind == TYPE_E) { 509493b2041SWarner Losh memcpy(&v4, walker + elt->pe_offset, sizeof(v4)); 510493b2041SWarner Losh strcpy(buffer, pnp_eisaformat(v4)); 511493b2041SWarner Losh } else { 512493b2041SWarner Losh char *ptr; 513493b2041SWarner Losh 514493b2041SWarner Losh ptr = *(char **)(walker + elt->pe_offset); 515493b2041SWarner Losh buffer[0] = '\0'; 5161dcace5bSMarcelo Araujo if (ptr != NULL) { 517ee150a33SWarner Losh EF_SEG_READ_STRING(ef, (Elf_Off)ptr, 518493b2041SWarner Losh sizeof(buffer), buffer); 519493b2041SWarner Losh buffer[sizeof(buffer) - 1] = '\0'; 520493b2041SWarner Losh } 521493b2041SWarner Losh } 522493b2041SWarner Losh if (verbose > 1) 523493b2041SWarner Losh printf("%c:%s;", elt->pe_kind == TYPE_E ? 'E' : (elt->pe_kind == TYPE_Z ? 'Z' : 'D'), buffer); 524493b2041SWarner Losh record_string(buffer); 525493b2041SWarner Losh } 526493b2041SWarner Losh } 527493b2041SWarner Losh if (verbose > 1) 528493b2041SWarner Losh printf("\n"); 529493b2041SWarner Losh walker += pnp.entry_len; 530493b2041SWarner Losh } 531493b2041SWarner Losh /* Now free it */ 532493b2041SWarner Losh TAILQ_FOREACH_SAFE(elt, &list, next, elt_tmp) { 533493b2041SWarner Losh TAILQ_REMOVE(&list, elt, next); 534493b2041SWarner Losh free(elt); 535493b2041SWarner Losh } 536493b2041SWarner Losh free(table); 537493b2041SWarner Losh } 538493b2041SWarner Losh break; 5399c6f9240SPeter Wemm default: 5409f5529b4SRuslan Ermilov warnx("unknown metadata record %d in file %s", md->md_type, kldname); 5419c6f9240SPeter Wemm } 5429c6f9240SPeter Wemm if (!error) 5439c6f9240SPeter Wemm record_end(); 544e2d0802cSEd Maste return (error); 5459c6f9240SPeter Wemm } 5469c6f9240SPeter Wemm 5479c6f9240SPeter Wemm static int 5489c6f9240SPeter Wemm read_kld(char *filename, char *kldname) 5499c6f9240SPeter Wemm { 5509c6f9240SPeter Wemm struct mod_metadata md; 5519c6f9240SPeter Wemm struct elf_file ef; 5529c6f9240SPeter Wemm void **p, **orgp; 553e2d0802cSEd Maste int error, eftype; 5549c6f9240SPeter Wemm long start, finish, entries; 555e2d0802cSEd Maste char cval[MAXMODNAME + 1]; 5569c6f9240SPeter Wemm 5579c6f9240SPeter Wemm if (verbose || dflag) 5589c6f9240SPeter Wemm printf("%s\n", filename); 5599c6f9240SPeter Wemm error = ef_open(filename, &ef, verbose); 560e2d0802cSEd Maste if (error != 0) { 5614a8b7e33SIan Dowse error = ef_obj_open(filename, &ef, verbose); 562e2d0802cSEd Maste if (error != 0) { 5639772dc2aSIan Dowse if (verbose) 5649772dc2aSIan Dowse warnc(error, "elf_open(%s)", filename); 565e2d0802cSEd Maste return (error); 5669772dc2aSIan Dowse } 5674a8b7e33SIan Dowse } 5689772dc2aSIan Dowse eftype = EF_GET_TYPE(&ef); 5699772dc2aSIan Dowse if (eftype != EFT_KLD && eftype != EFT_KERNEL) { 5709772dc2aSIan Dowse EF_CLOSE(&ef); 571e2d0802cSEd Maste return (0); 5729c6f9240SPeter Wemm } 5739c6f9240SPeter Wemm do { 5749772dc2aSIan Dowse check(EF_LOOKUP_SET(&ef, MDT_SETNAME, &start, &finish, 5759772dc2aSIan Dowse &entries)); 5769772dc2aSIan Dowse check(EF_SEG_READ_ENTRY_REL(&ef, start, sizeof(*p) * entries, 57776798703SDag-Erling Smørgrav (void *)&p)); 5789c6f9240SPeter Wemm orgp = p; 5799c6f9240SPeter Wemm while(entries--) { 5809772dc2aSIan Dowse check(EF_SEG_READ_REL(&ef, (Elf_Off)*p, sizeof(md), 5819772dc2aSIan Dowse &md)); 5829c6f9240SPeter Wemm p++; 583da67e6e6SEd Maste check(EF_SEG_READ_STRING(&ef, (Elf_Off)md.md_cval, 5849772dc2aSIan Dowse sizeof(cval), cval)); 5859c6f9240SPeter Wemm parse_entry(&md, cval, &ef, kldname); 5869c6f9240SPeter Wemm } 587e2d0802cSEd Maste if (error != 0) 5889c6f9240SPeter Wemm warnc(error, "error while reading %s", filename); 5899c6f9240SPeter Wemm free(orgp); 5909c6f9240SPeter Wemm } while(0); 5919772dc2aSIan Dowse EF_CLOSE(&ef); 592e2d0802cSEd Maste return (error); 5939c6f9240SPeter Wemm } 5949c6f9240SPeter Wemm 5959cb138bbSLuigi Rizzo /* 5969cb138bbSLuigi Rizzo * Create a temp file in directory root, make sure we don't 5979cb138bbSLuigi Rizzo * overflow the buffer for the destination name 5989cb138bbSLuigi Rizzo */ 5999cb138bbSLuigi Rizzo static FILE * 6009c6f9240SPeter Wemm maketempfile(char *dest, const char *root) 6019c6f9240SPeter Wemm { 6029c6f9240SPeter Wemm char *p; 6039cb138bbSLuigi Rizzo int n, fd; 6049c6f9240SPeter Wemm 6059cb138bbSLuigi Rizzo p = strrchr(root, '/'); 6069cb138bbSLuigi Rizzo n = p != NULL ? p - root + 1 : 0; 6079cb138bbSLuigi Rizzo if (snprintf(dest, MAXPATHLEN, "%.*slhint.XXXXXX", n, root) >= 6089cb138bbSLuigi Rizzo MAXPATHLEN) { 6099cb138bbSLuigi Rizzo errno = ENAMETOOLONG; 610e2d0802cSEd Maste return (NULL); 6119cb138bbSLuigi Rizzo } 6129c6f9240SPeter Wemm 6139ceddbd5SMarcel Moolenaar fd = mkstemp(dest); 6149cb138bbSLuigi Rizzo if (fd < 0) 615e2d0802cSEd Maste return (NULL); 616ddce5818SLuigi Rizzo fchmod(fd, 0644); /* nothing secret in the file */ 617e2d0802cSEd Maste return (fdopen(fd, "w+")); 6189c6f9240SPeter Wemm } 6199c6f9240SPeter Wemm 6209c6f9240SPeter Wemm static char xrefname[MAXPATHLEN], tempname[MAXPATHLEN]; 6219c6f9240SPeter Wemm 6229cb138bbSLuigi Rizzo static void 6239cb138bbSLuigi Rizzo usage(void) 6249cb138bbSLuigi Rizzo { 6259cb138bbSLuigi Rizzo 6269cb138bbSLuigi Rizzo fprintf(stderr, "%s\n", 6279cb138bbSLuigi Rizzo "usage: kldxref [-Rdv] [-f hintsfile] path ..." 6289cb138bbSLuigi Rizzo ); 6299cb138bbSLuigi Rizzo exit(1); 6309cb138bbSLuigi Rizzo } 6319cb138bbSLuigi Rizzo 6325d452ceaSJilles Tjoelker static int 6336d9cb20bSJilles Tjoelker compare(const FTSENT *const *a, const FTSENT *const *b) 6346d9cb20bSJilles Tjoelker { 635e2d0802cSEd Maste 6366d9cb20bSJilles Tjoelker if ((*a)->fts_info == FTS_D && (*b)->fts_info != FTS_D) 637e2d0802cSEd Maste return (1); 6386d9cb20bSJilles Tjoelker if ((*a)->fts_info != FTS_D && (*b)->fts_info == FTS_D) 639e2d0802cSEd Maste return (-1); 640e2d0802cSEd Maste return (strcmp((*a)->fts_name, (*b)->fts_name)); 6416d9cb20bSJilles Tjoelker } 6426d9cb20bSJilles Tjoelker 6436d9cb20bSJilles Tjoelker int 6449c6f9240SPeter Wemm main(int argc, char *argv[]) 6459c6f9240SPeter Wemm { 6469c6f9240SPeter Wemm FTS *ftsp; 6479c6f9240SPeter Wemm FTSENT *p; 6489c6f9240SPeter Wemm int opt, fts_options, ival; 649b7abc67eSMaxim Sobolev struct stat sb; 6509c6f9240SPeter Wemm 6519c6f9240SPeter Wemm fts_options = FTS_PHYSICAL; 6529c6f9240SPeter Wemm 6539c6f9240SPeter Wemm while ((opt = getopt(argc, argv, "Rdf:v")) != -1) { 6549c6f9240SPeter Wemm switch (opt) { 6559cb138bbSLuigi Rizzo case 'd': /* no hint file, only print on stdout */ 656e2d0802cSEd Maste dflag = true; 6579c6f9240SPeter Wemm break; 6589cb138bbSLuigi Rizzo case 'f': /* use this name instead of linker.hints */ 6599c6f9240SPeter Wemm xref_file = optarg; 6609c6f9240SPeter Wemm break; 6619c6f9240SPeter Wemm case 'v': 6629c6f9240SPeter Wemm verbose++; 6639c6f9240SPeter Wemm break; 6649cb138bbSLuigi Rizzo case 'R': /* recurse on directories */ 6659c6f9240SPeter Wemm fts_options |= FTS_COMFOLLOW; 6669c6f9240SPeter Wemm break; 6679c6f9240SPeter Wemm default: 6689c6f9240SPeter Wemm usage(); 6699c6f9240SPeter Wemm /* NOTREACHED */ 6709c6f9240SPeter Wemm } 6719c6f9240SPeter Wemm } 6729c6f9240SPeter Wemm if (argc - optind < 1) 6739c6f9240SPeter Wemm usage(); 6749c6f9240SPeter Wemm argc -= optind; 6759c6f9240SPeter Wemm argv += optind; 6769c6f9240SPeter Wemm 677b7abc67eSMaxim Sobolev if (stat(argv[0], &sb) != 0) 678b7abc67eSMaxim Sobolev err(1, "%s", argv[0]); 679b7abc67eSMaxim Sobolev if ((sb.st_mode & S_IFDIR) == 0) { 680b7abc67eSMaxim Sobolev errno = ENOTDIR; 681b7abc67eSMaxim Sobolev err(1, "%s", argv[0]); 682b7abc67eSMaxim Sobolev } 683b7abc67eSMaxim Sobolev 6846d9cb20bSJilles Tjoelker ftsp = fts_open(argv, fts_options, compare); 6859c6f9240SPeter Wemm if (ftsp == NULL) 6869c6f9240SPeter Wemm exit(1); 6879c6f9240SPeter Wemm 6889c6f9240SPeter Wemm for (;;) { 6899c6f9240SPeter Wemm p = fts_read(ftsp); 6909cb138bbSLuigi Rizzo if ((p == NULL || p->fts_info == FTS_D) && fxref) { 6919cb138bbSLuigi Rizzo /* close and rename the current hint file */ 6929c6f9240SPeter Wemm fclose(fxref); 6939ceddbd5SMarcel Moolenaar fxref = NULL; 694e2d0802cSEd Maste if (reccnt != 0) { 6959c6f9240SPeter Wemm rename(tempname, xrefname); 6969c6f9240SPeter Wemm } else { 6979cb138bbSLuigi Rizzo /* didn't find any entry, ignore this file */ 6989c6f9240SPeter Wemm unlink(tempname); 6999c6f9240SPeter Wemm unlink(xrefname); 7009c6f9240SPeter Wemm } 7019c6f9240SPeter Wemm } 7029c6f9240SPeter Wemm if (p == NULL) 7039c6f9240SPeter Wemm break; 7049cb138bbSLuigi Rizzo if (p->fts_info == FTS_D && !dflag) { 7059cb138bbSLuigi Rizzo /* visiting a new directory, create a new hint file */ 7069c6f9240SPeter Wemm snprintf(xrefname, sizeof(xrefname), "%s/%s", 7079c6f9240SPeter Wemm ftsp->fts_path, xref_file); 7089ceddbd5SMarcel Moolenaar fxref = maketempfile(tempname, ftsp->fts_path); 7099c6f9240SPeter Wemm if (fxref == NULL) 7109c6f9240SPeter Wemm err(1, "can't create %s", tempname); 7119c6f9240SPeter Wemm ival = 1; 7129c6f9240SPeter Wemm fwrite(&ival, sizeof(ival), 1, fxref); 7139c6f9240SPeter Wemm reccnt = 0; 7149c6f9240SPeter Wemm } 715113a1a21SEd Maste /* skip non-files and separate debug files */ 7169c6f9240SPeter Wemm if (p->fts_info != FTS_F) 7179c6f9240SPeter Wemm continue; 718113a1a21SEd Maste if (p->fts_namelen >= 6 && 719113a1a21SEd Maste strcmp(p->fts_name + p->fts_namelen - 6, ".debug") == 0) 720113a1a21SEd Maste continue; 7219f5529b4SRuslan Ermilov if (p->fts_namelen >= 8 && 7229f5529b4SRuslan Ermilov strcmp(p->fts_name + p->fts_namelen - 8, ".symbols") == 0) 7239f5529b4SRuslan Ermilov continue; 7249c6f9240SPeter Wemm read_kld(p->fts_path, p->fts_name); 7259c6f9240SPeter Wemm } 7269c6f9240SPeter Wemm fts_close(ftsp); 727e2d0802cSEd Maste return (0); 7289c6f9240SPeter Wemm } 729