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
359c6f9240SPeter Wemm #include <sys/param.h>
36493b2041SWarner Losh #include <sys/endian.h>
379c6f9240SPeter Wemm #include <sys/queue.h>
389c6f9240SPeter Wemm #include <sys/stat.h>
399c6f9240SPeter Wemm #include <sys/module.h>
40e2d0802cSEd Maste
410299afdfSJohn Baldwin #include <assert.h>
42517a1827SWarner Losh #include <ctype.h>
439c6f9240SPeter Wemm #include <err.h>
44e2d0802cSEd Maste #include <errno.h>
459c6f9240SPeter Wemm #include <fts.h>
460299afdfSJohn Baldwin #include <gelf.h>
470299afdfSJohn Baldwin #include <libelf.h>
48e2d0802cSEd Maste #include <stdbool.h>
499c6f9240SPeter Wemm #include <stdio.h>
509c6f9240SPeter Wemm #include <stdlib.h>
51e2d0802cSEd Maste #include <string.h>
529c6f9240SPeter Wemm #include <unistd.h>
539c6f9240SPeter Wemm
54*968bcca2SKa Ho Ng #include <kldelf.h>
559c6f9240SPeter Wemm
56493b2041SWarner Losh #define MAXRECSIZE (64 << 10) /* 64k */
579c6f9240SPeter Wemm #define check(val) if ((error = (val)) != 0) break
589c6f9240SPeter Wemm
59e2d0802cSEd Maste static bool dflag; /* do not create a hint file, only write on stdout */
609cb138bbSLuigi Rizzo static int verbose;
619c6f9240SPeter Wemm
629cb138bbSLuigi Rizzo static FILE *fxref; /* current hints file */
630299afdfSJohn Baldwin static int byte_order;
640299afdfSJohn Baldwin static GElf_Ehdr ehdr;
650299afdfSJohn Baldwin static char *ehdr_filename;
669c6f9240SPeter Wemm
6787e5cd7cSMike Heffner static const char *xref_file = "linker.hints";
689c6f9240SPeter Wemm
699cb138bbSLuigi Rizzo /*
709cb138bbSLuigi Rizzo * A record is stored in the static buffer recbuf before going to disk.
719cb138bbSLuigi Rizzo */
729c6f9240SPeter Wemm static char recbuf[MAXRECSIZE];
739cb138bbSLuigi Rizzo static int recpos; /* current write position */
749cb138bbSLuigi Rizzo static int reccnt; /* total record written to this file so far */
759c6f9240SPeter Wemm
769c6f9240SPeter Wemm static void
intalign(void)779c6f9240SPeter Wemm intalign(void)
789c6f9240SPeter Wemm {
79e2d0802cSEd Maste
80bf6911cdSMarcelo Araujo recpos = roundup2(recpos, sizeof(int));
819c6f9240SPeter Wemm }
829c6f9240SPeter Wemm
839c6f9240SPeter Wemm static void
write_int(int val)840299afdfSJohn Baldwin write_int(int val)
850299afdfSJohn Baldwin {
860299afdfSJohn Baldwin char buf[4];
870299afdfSJohn Baldwin
880299afdfSJohn Baldwin assert(byte_order != ELFDATANONE);
890299afdfSJohn Baldwin if (byte_order == ELFDATA2LSB)
900299afdfSJohn Baldwin le32enc(buf, val);
910299afdfSJohn Baldwin else
920299afdfSJohn Baldwin be32enc(buf, val);
930299afdfSJohn Baldwin fwrite(buf, sizeof(buf), 1, fxref);
940299afdfSJohn Baldwin }
950299afdfSJohn Baldwin
960299afdfSJohn Baldwin static void
record_start(void)979c6f9240SPeter Wemm record_start(void)
989c6f9240SPeter Wemm {
99e2d0802cSEd Maste
1009c6f9240SPeter Wemm recpos = 0;
1019c6f9240SPeter Wemm memset(recbuf, 0, MAXRECSIZE);
1029c6f9240SPeter Wemm }
1039c6f9240SPeter Wemm
1049c6f9240SPeter Wemm static int
record_end(void)1059c6f9240SPeter Wemm record_end(void)
1069c6f9240SPeter Wemm {
107e2d0802cSEd Maste
1080299afdfSJohn Baldwin if (recpos == 0) {
1090299afdfSJohn Baldwin /*
1100299afdfSJohn Baldwin * Pretend to have written a record in debug mode so
1110299afdfSJohn Baldwin * the architecture check works.
1120299afdfSJohn Baldwin */
1130299afdfSJohn Baldwin if (dflag)
1140299afdfSJohn Baldwin reccnt++;
115e2d0802cSEd Maste return (0);
1160299afdfSJohn Baldwin }
1170299afdfSJohn Baldwin
1180299afdfSJohn Baldwin if (reccnt == 0) {
1190299afdfSJohn Baldwin /* File version record. */
1200299afdfSJohn Baldwin write_int(1);
1210299afdfSJohn Baldwin }
1220299afdfSJohn Baldwin
1239c6f9240SPeter Wemm reccnt++;
1249c6f9240SPeter Wemm intalign();
1250299afdfSJohn Baldwin write_int(recpos);
126e2d0802cSEd Maste return (fwrite(recbuf, recpos, 1, fxref) != 1 ? errno : 0);
1279c6f9240SPeter Wemm }
1289c6f9240SPeter Wemm
1299c6f9240SPeter Wemm static int
record_buf(const void * buf,size_t size)130e2d0802cSEd Maste record_buf(const void *buf, size_t size)
1319c6f9240SPeter Wemm {
132e2d0802cSEd Maste
1339c6f9240SPeter Wemm if (MAXRECSIZE - recpos < size)
1349c6f9240SPeter Wemm errx(1, "record buffer overflow");
1359c6f9240SPeter Wemm memcpy(recbuf + recpos, buf, size);
1369c6f9240SPeter Wemm recpos += size;
137e2d0802cSEd Maste return (0);
1389c6f9240SPeter Wemm }
1399c6f9240SPeter Wemm
1409cb138bbSLuigi Rizzo /*
1410299afdfSJohn Baldwin * An int is stored in target byte order and aligned
1429cb138bbSLuigi Rizzo */
1439c6f9240SPeter Wemm static int
record_int(int val)1449c6f9240SPeter Wemm record_int(int val)
1459c6f9240SPeter Wemm {
1460299afdfSJohn Baldwin char buf[4];
1470299afdfSJohn Baldwin
1480299afdfSJohn Baldwin assert(byte_order != ELFDATANONE);
1490299afdfSJohn Baldwin if (byte_order == ELFDATA2LSB)
1500299afdfSJohn Baldwin le32enc(buf, val);
1510299afdfSJohn Baldwin else
1520299afdfSJohn Baldwin be32enc(buf, val);
153e2d0802cSEd Maste
1549c6f9240SPeter Wemm intalign();
1550299afdfSJohn Baldwin return (record_buf(buf, sizeof(buf)));
1569c6f9240SPeter Wemm }
1579c6f9240SPeter Wemm
1589cb138bbSLuigi Rizzo /*
1599cb138bbSLuigi Rizzo * A string is stored as 1-byte length plus data, no padding
1609cb138bbSLuigi Rizzo */
1619c6f9240SPeter Wemm static int
record_string(const char * str)1629c6f9240SPeter Wemm record_string(const char *str)
1639c6f9240SPeter Wemm {
164e2d0802cSEd Maste int error;
165e2d0802cSEd Maste size_t len;
1669cb138bbSLuigi Rizzo u_char val;
1679c6f9240SPeter Wemm
1689c6f9240SPeter Wemm if (dflag)
169e2d0802cSEd Maste return (0);
1709cb138bbSLuigi Rizzo val = len = strlen(str);
1719cb138bbSLuigi Rizzo if (len > 255)
1729cb138bbSLuigi Rizzo errx(1, "string %s too long", str);
1739cb138bbSLuigi Rizzo error = record_buf(&val, sizeof(val));
174e2d0802cSEd Maste if (error != 0)
175e2d0802cSEd Maste return (error);
176e2d0802cSEd Maste return (record_buf(str, len));
1779c6f9240SPeter Wemm }
1789c6f9240SPeter Wemm
179493b2041SWarner Losh /* From sys/isa/pnp.c */
180493b2041SWarner Losh static char *
pnp_eisaformat(uint32_t id)181493b2041SWarner Losh pnp_eisaformat(uint32_t id)
182493b2041SWarner Losh {
183493b2041SWarner Losh uint8_t *data;
184493b2041SWarner Losh static char idbuf[8];
185493b2041SWarner Losh const char hextoascii[] = "0123456789abcdef";
186493b2041SWarner Losh
187493b2041SWarner Losh id = htole32(id);
188493b2041SWarner Losh data = (uint8_t *)&id;
189493b2041SWarner Losh idbuf[0] = '@' + ((data[0] & 0x7c) >> 2);
190493b2041SWarner Losh idbuf[1] = '@' + (((data[0] & 0x3) << 3) + ((data[1] & 0xe0) >> 5));
191493b2041SWarner Losh idbuf[2] = '@' + (data[1] & 0x1f);
192493b2041SWarner Losh idbuf[3] = hextoascii[(data[2] >> 4)];
193493b2041SWarner Losh idbuf[4] = hextoascii[(data[2] & 0xf)];
194493b2041SWarner Losh idbuf[5] = hextoascii[(data[3] >> 4)];
195493b2041SWarner Losh idbuf[6] = hextoascii[(data[3] & 0xf)];
196493b2041SWarner Losh idbuf[7] = 0;
197493b2041SWarner Losh return (idbuf);
198493b2041SWarner Losh }
199493b2041SWarner Losh
200493b2041SWarner Losh struct pnp_elt
201493b2041SWarner Losh {
202493b2041SWarner Losh int pe_kind; /* What kind of entry */
203493b2041SWarner Losh #define TYPE_SZ_MASK 0x0f
204493b2041SWarner Losh #define TYPE_FLAGGED 0x10 /* all f's is a wildcard */
205493b2041SWarner Losh #define TYPE_INT 0x20 /* Is a number */
206493b2041SWarner Losh #define TYPE_PAIRED 0x40
207493b2041SWarner Losh #define TYPE_LE 0x80 /* Matches <= this value */
208493b2041SWarner Losh #define TYPE_GE 0x100 /* Matches >= this value */
209493b2041SWarner Losh #define TYPE_MASK 0x200 /* Specifies a mask to follow */
210493b2041SWarner Losh #define TYPE_U8 (1 | TYPE_INT)
211493b2041SWarner Losh #define TYPE_V8 (1 | TYPE_INT | TYPE_FLAGGED)
212493b2041SWarner Losh #define TYPE_G16 (2 | TYPE_INT | TYPE_GE)
213493b2041SWarner Losh #define TYPE_L16 (2 | TYPE_INT | TYPE_LE)
214493b2041SWarner Losh #define TYPE_M16 (2 | TYPE_INT | TYPE_MASK)
215493b2041SWarner Losh #define TYPE_U16 (2 | TYPE_INT)
216493b2041SWarner Losh #define TYPE_V16 (2 | TYPE_INT | TYPE_FLAGGED)
217493b2041SWarner Losh #define TYPE_U32 (4 | TYPE_INT)
218493b2041SWarner Losh #define TYPE_V32 (4 | TYPE_INT | TYPE_FLAGGED)
219493b2041SWarner Losh #define TYPE_W32 (4 | TYPE_INT | TYPE_PAIRED)
220493b2041SWarner Losh #define TYPE_D 7
221493b2041SWarner Losh #define TYPE_Z 8
222493b2041SWarner Losh #define TYPE_P 9
223493b2041SWarner Losh #define TYPE_E 10
224493b2041SWarner Losh #define TYPE_T 11
225493b2041SWarner Losh int pe_offset; /* Offset within the element */
226493b2041SWarner Losh char * pe_key; /* pnp key name */
227493b2041SWarner Losh TAILQ_ENTRY(pnp_elt) next; /* Link */
228493b2041SWarner Losh };
229493b2041SWarner Losh typedef TAILQ_HEAD(pnp_head, pnp_elt) pnp_list;
230493b2041SWarner Losh
231493b2041SWarner Losh /*
232493b2041SWarner Losh * this function finds the data from the pnp table, as described by the
233ec8a394dSElyes Haouas * description and creates a new output (new_desc). This output table
234493b2041SWarner Losh * is a form that's easier for the agent that's automatically loading the
235493b2041SWarner Losh * modules.
236493b2041SWarner Losh *
237493b2041SWarner Losh * The format output is the simplified string from this routine in the
238493b2041SWarner Losh * same basic format as the pnp string, as documented in sys/module.h.
239493b2041SWarner Losh * First a string describing the format is output, the a count of the
240493b2041SWarner Losh * number of records, then each record. The format string also describes
241493b2041SWarner Losh * the length of each entry (though it isn't a fixed length when strings
242493b2041SWarner Losh * are present).
243493b2041SWarner Losh *
244493b2041SWarner Losh * type Output Meaning
245493b2041SWarner Losh * I uint32_t Integer equality comparison
246493b2041SWarner Losh * J uint32_t Pair of uint16_t fields converted to native
247a35ddacaSWarner Losh * byte order. The two fields both must match.
248493b2041SWarner Losh * G uint32_t Greater than or equal to
249493b2041SWarner Losh * L uint32_t Less than or equal to
250493b2041SWarner Losh * M uint32_t Mask of which fields to test. Fields that
251a35ddacaSWarner Losh * take up space increment the count. This
252a35ddacaSWarner Losh * field must be first, and resets the count.
253493b2041SWarner Losh * D string Description of the device this pnp info is for
254493b2041SWarner Losh * Z string pnp string must match this
255493b2041SWarner Losh * T nothing T fields set pnp values that must be true for
256493b2041SWarner Losh * the entire table.
257493b2041SWarner Losh * Values are packed the same way that other values are packed in this file.
258493b2041SWarner Losh * Strings and int32_t's start on a 32-bit boundary and are padded with 0
259493b2041SWarner Losh * bytes. Objects that are smaller than uint32_t are converted, without
260493b2041SWarner Losh * sign extension to uint32_t to simplify parsing downstream.
261493b2041SWarner Losh */
262493b2041SWarner Losh static int
parse_pnp_list(struct elf_file * ef,const char * desc,char ** new_desc,pnp_list * list)2630299afdfSJohn Baldwin parse_pnp_list(struct elf_file *ef, const char *desc, char **new_desc,
2640299afdfSJohn Baldwin pnp_list *list)
265493b2041SWarner Losh {
266e2d0802cSEd Maste const char *walker, *ep;
267493b2041SWarner Losh const char *colon, *semi;
268493b2041SWarner Losh struct pnp_elt *elt;
269493b2041SWarner Losh char type[8], key[32];
270493b2041SWarner Losh int off;
271a98fa52eSJessica Clarke size_t new_desc_size;
272a98fa52eSJessica Clarke FILE *fp;
273493b2041SWarner Losh
274c40fa3dcSJohn Baldwin TAILQ_INIT(list);
275e2d0802cSEd Maste walker = desc;
276e2d0802cSEd Maste ep = desc + strlen(desc);
277493b2041SWarner Losh off = 0;
278a98fa52eSJessica Clarke fp = open_memstream(new_desc, &new_desc_size);
279a98fa52eSJessica Clarke if (fp == NULL)
280a98fa52eSJessica Clarke err(1, "Could not open new memory stream");
281493b2041SWarner Losh if (verbose > 1)
282493b2041SWarner Losh printf("Converting %s into a list\n", desc);
283493b2041SWarner Losh while (walker < ep) {
284493b2041SWarner Losh colon = strchr(walker, ':');
285493b2041SWarner Losh semi = strchr(walker, ';');
286493b2041SWarner Losh if (semi != NULL && semi < colon)
287493b2041SWarner Losh goto err;
288493b2041SWarner Losh if (colon - walker > sizeof(type))
289493b2041SWarner Losh goto err;
290493b2041SWarner Losh strncpy(type, walker, colon - walker);
291493b2041SWarner Losh type[colon - walker] = '\0';
292e2d0802cSEd Maste if (semi != NULL) {
293493b2041SWarner Losh if (semi - colon >= sizeof(key))
294493b2041SWarner Losh goto err;
295493b2041SWarner Losh strncpy(key, colon + 1, semi - colon - 1);
296493b2041SWarner Losh key[semi - colon - 1] = '\0';
297493b2041SWarner Losh walker = semi + 1;
298517a1827SWarner Losh /* Fail safe if we have spaces after ; */
299517a1827SWarner Losh while (walker < ep && isspace(*walker))
300517a1827SWarner Losh walker++;
301493b2041SWarner Losh } else {
302493b2041SWarner Losh if (strlen(colon + 1) >= sizeof(key))
303493b2041SWarner Losh goto err;
304493b2041SWarner Losh strcpy(key, colon + 1);
305493b2041SWarner Losh walker = ep;
306493b2041SWarner Losh }
307493b2041SWarner Losh if (verbose > 1)
308493b2041SWarner Losh printf("Found type %s for name %s\n", type, key);
309493b2041SWarner Losh /* Skip pointer place holders */
310493b2041SWarner Losh if (strcmp(type, "P") == 0) {
3110299afdfSJohn Baldwin off += elf_pointer_size(ef);
312493b2041SWarner Losh continue;
313493b2041SWarner Losh }
314493b2041SWarner Losh
315493b2041SWarner Losh /*
316493b2041SWarner Losh * Add a node of the appropriate type
317493b2041SWarner Losh */
318493b2041SWarner Losh elt = malloc(sizeof(struct pnp_elt) + strlen(key) + 1);
319493b2041SWarner Losh TAILQ_INSERT_TAIL(list, elt, next);
320493b2041SWarner Losh elt->pe_key = (char *)(elt + 1);
321493b2041SWarner Losh elt->pe_offset = off;
322493b2041SWarner Losh if (strcmp(type, "U8") == 0)
323493b2041SWarner Losh elt->pe_kind = TYPE_U8;
324493b2041SWarner Losh else if (strcmp(type, "V8") == 0)
325493b2041SWarner Losh elt->pe_kind = TYPE_V8;
326493b2041SWarner Losh else if (strcmp(type, "G16") == 0)
327493b2041SWarner Losh elt->pe_kind = TYPE_G16;
328493b2041SWarner Losh else if (strcmp(type, "L16") == 0)
329493b2041SWarner Losh elt->pe_kind = TYPE_L16;
330493b2041SWarner Losh else if (strcmp(type, "M16") == 0)
331493b2041SWarner Losh elt->pe_kind = TYPE_M16;
332493b2041SWarner Losh else if (strcmp(type, "U16") == 0)
333493b2041SWarner Losh elt->pe_kind = TYPE_U16;
334493b2041SWarner Losh else if (strcmp(type, "V16") == 0)
335493b2041SWarner Losh elt->pe_kind = TYPE_V16;
336493b2041SWarner Losh else if (strcmp(type, "U32") == 0)
337493b2041SWarner Losh elt->pe_kind = TYPE_U32;
338493b2041SWarner Losh else if (strcmp(type, "V32") == 0)
339493b2041SWarner Losh elt->pe_kind = TYPE_V32;
340493b2041SWarner Losh else if (strcmp(type, "W32") == 0)
341493b2041SWarner Losh elt->pe_kind = TYPE_W32;
342493b2041SWarner Losh else if (strcmp(type, "D") == 0) /* description char * */
343493b2041SWarner Losh elt->pe_kind = TYPE_D;
344493b2041SWarner Losh else if (strcmp(type, "Z") == 0) /* char * to match */
345493b2041SWarner Losh elt->pe_kind = TYPE_Z;
346493b2041SWarner Losh else if (strcmp(type, "P") == 0) /* Pointer -- ignored */
347493b2041SWarner Losh elt->pe_kind = TYPE_P;
348493b2041SWarner Losh else if (strcmp(type, "E") == 0) /* EISA PNP ID, as uint32_t */
349493b2041SWarner Losh elt->pe_kind = TYPE_E;
350493b2041SWarner Losh else if (strcmp(type, "T") == 0)
351493b2041SWarner Losh elt->pe_kind = TYPE_T;
352493b2041SWarner Losh else
353493b2041SWarner Losh goto err;
354493b2041SWarner Losh /*
355493b2041SWarner Losh * Maybe the rounding here needs to be more nuanced and/or somehow
356493b2041SWarner Losh * architecture specific. Fortunately, most tables in the system
357493b2041SWarner Losh * have sane ordering of types.
358493b2041SWarner Losh */
359493b2041SWarner Losh if (elt->pe_kind & TYPE_INT) {
360493b2041SWarner Losh elt->pe_offset = roundup2(elt->pe_offset, elt->pe_kind & TYPE_SZ_MASK);
361493b2041SWarner Losh off = elt->pe_offset + (elt->pe_kind & TYPE_SZ_MASK);
362493b2041SWarner Losh } else if (elt->pe_kind == TYPE_E) {
363493b2041SWarner Losh /* Type E stored as Int, displays as string */
364493b2041SWarner Losh elt->pe_offset = roundup2(elt->pe_offset, sizeof(uint32_t));
365493b2041SWarner Losh off = elt->pe_offset + sizeof(uint32_t);
366493b2041SWarner Losh } else if (elt->pe_kind == TYPE_T) {
367493b2041SWarner Losh /* doesn't actually consume space in the table */
368493b2041SWarner Losh off = elt->pe_offset;
369493b2041SWarner Losh } else {
3700299afdfSJohn Baldwin elt->pe_offset = roundup2(elt->pe_offset, elf_pointer_size(ef));
3710299afdfSJohn Baldwin off = elt->pe_offset + elf_pointer_size(ef);
372493b2041SWarner Losh }
373493b2041SWarner Losh if (elt->pe_kind & TYPE_PAIRED) {
374a98fa52eSJessica Clarke char *word, *ctx, newtype;
375493b2041SWarner Losh
376493b2041SWarner Losh for (word = strtok_r(key, "/", &ctx);
377493b2041SWarner Losh word; word = strtok_r(NULL, "/", &ctx)) {
378a98fa52eSJessica Clarke newtype = elt->pe_kind & TYPE_FLAGGED ? 'J' : 'I';
379a98fa52eSJessica Clarke fprintf(fp, "%c:%s;", newtype, word);
380493b2041SWarner Losh }
381493b2041SWarner Losh }
382493b2041SWarner Losh else {
383a98fa52eSJessica Clarke char newtype;
384a98fa52eSJessica Clarke
385493b2041SWarner Losh if (elt->pe_kind & TYPE_FLAGGED)
386a98fa52eSJessica Clarke newtype = 'J';
387493b2041SWarner Losh else if (elt->pe_kind & TYPE_GE)
388a98fa52eSJessica Clarke newtype = 'G';
389493b2041SWarner Losh else if (elt->pe_kind & TYPE_LE)
390a98fa52eSJessica Clarke newtype = 'L';
391493b2041SWarner Losh else if (elt->pe_kind & TYPE_MASK)
392a98fa52eSJessica Clarke newtype = 'M';
393493b2041SWarner Losh else if (elt->pe_kind & TYPE_INT)
394a98fa52eSJessica Clarke newtype = 'I';
395493b2041SWarner Losh else if (elt->pe_kind == TYPE_D)
396a98fa52eSJessica Clarke newtype = 'D';
397493b2041SWarner Losh else if (elt->pe_kind == TYPE_Z || elt->pe_kind == TYPE_E)
398a98fa52eSJessica Clarke newtype = 'Z';
399493b2041SWarner Losh else if (elt->pe_kind == TYPE_T)
400a98fa52eSJessica Clarke newtype = 'T';
401493b2041SWarner Losh else
402493b2041SWarner Losh errx(1, "Impossible type %x\n", elt->pe_kind);
403a98fa52eSJessica Clarke fprintf(fp, "%c:%s;", newtype, key);
404493b2041SWarner Losh }
405493b2041SWarner Losh }
406a98fa52eSJessica Clarke if (ferror(fp) != 0) {
407a98fa52eSJessica Clarke fclose(fp);
408a98fa52eSJessica Clarke errx(1, "Exhausted space converting description %s", desc);
409a98fa52eSJessica Clarke }
410a98fa52eSJessica Clarke if (fclose(fp) != 0)
411a98fa52eSJessica Clarke errx(1, "Failed to close memory stream");
412e2d0802cSEd Maste return (0);
413493b2041SWarner Losh err:
414493b2041SWarner Losh errx(1, "Parse error of description string %s", desc);
415493b2041SWarner Losh }
416493b2041SWarner Losh
417c40fa3dcSJohn Baldwin static void
free_pnp_list(char * new_desc,pnp_list * list)418c40fa3dcSJohn Baldwin free_pnp_list(char *new_desc, pnp_list *list)
4199c6f9240SPeter Wemm {
420493b2041SWarner Losh struct pnp_elt *elt, *elt_tmp;
421493b2041SWarner Losh
422c40fa3dcSJohn Baldwin TAILQ_FOREACH_SAFE(elt, list, next, elt_tmp) {
423c40fa3dcSJohn Baldwin TAILQ_REMOVE(list, elt, next);
424c40fa3dcSJohn Baldwin free(elt);
425c40fa3dcSJohn Baldwin }
426c40fa3dcSJohn Baldwin free(new_desc);
427c40fa3dcSJohn Baldwin }
428493b2041SWarner Losh
4290299afdfSJohn Baldwin static uint16_t
parse_16(const void * p)4300299afdfSJohn Baldwin parse_16(const void *p)
4310299afdfSJohn Baldwin {
4320299afdfSJohn Baldwin if (byte_order == ELFDATA2LSB)
4330299afdfSJohn Baldwin return (le16dec(p));
4340299afdfSJohn Baldwin else
4350299afdfSJohn Baldwin return (be16dec(p));
4360299afdfSJohn Baldwin }
4370299afdfSJohn Baldwin
4380299afdfSJohn Baldwin static uint32_t
parse_32(const void * p)4390299afdfSJohn Baldwin parse_32(const void *p)
4400299afdfSJohn Baldwin {
4410299afdfSJohn Baldwin if (byte_order == ELFDATA2LSB)
4420299afdfSJohn Baldwin return (le32dec(p));
4430299afdfSJohn Baldwin else
4440299afdfSJohn Baldwin return (be32dec(p));
4450299afdfSJohn Baldwin }
4460299afdfSJohn Baldwin
447c40fa3dcSJohn Baldwin static void
parse_pnp_entry(struct elf_file * ef,struct pnp_elt * elt,const char * walker)448c40fa3dcSJohn Baldwin parse_pnp_entry(struct elf_file *ef, struct pnp_elt *elt, const char *walker)
449c40fa3dcSJohn Baldwin {
450493b2041SWarner Losh uint8_t v1;
451493b2041SWarner Losh uint16_t v2;
452493b2041SWarner Losh uint32_t v4;
453493b2041SWarner Losh int value;
454493b2041SWarner Losh char buffer[1024];
455493b2041SWarner Losh
456493b2041SWarner Losh if (elt->pe_kind == TYPE_W32) {
4570299afdfSJohn Baldwin v4 = parse_32(walker + elt->pe_offset);
458493b2041SWarner Losh value = v4 & 0xffff;
459493b2041SWarner Losh record_int(value);
460493b2041SWarner Losh if (verbose > 1)
461493b2041SWarner Losh printf("W32:%#x", value);
462493b2041SWarner Losh value = (v4 >> 16) & 0xffff;
463493b2041SWarner Losh record_int(value);
464493b2041SWarner Losh if (verbose > 1)
465493b2041SWarner Losh printf(":%#x;", value);
466493b2041SWarner Losh } else if (elt->pe_kind & TYPE_INT) {
467493b2041SWarner Losh switch (elt->pe_kind & TYPE_SZ_MASK) {
468493b2041SWarner Losh case 1:
469493b2041SWarner Losh memcpy(&v1, walker + elt->pe_offset, sizeof(v1));
470493b2041SWarner Losh if ((elt->pe_kind & TYPE_FLAGGED) && v1 == 0xff)
471493b2041SWarner Losh value = -1;
472493b2041SWarner Losh else
473493b2041SWarner Losh value = v1;
474493b2041SWarner Losh break;
475493b2041SWarner Losh case 2:
4760299afdfSJohn Baldwin v2 = parse_16(walker + elt->pe_offset);
477493b2041SWarner Losh if ((elt->pe_kind & TYPE_FLAGGED) && v2 == 0xffff)
478493b2041SWarner Losh value = -1;
479493b2041SWarner Losh else
480493b2041SWarner Losh value = v2;
481493b2041SWarner Losh break;
482493b2041SWarner Losh case 4:
4830299afdfSJohn Baldwin v4 = parse_32(walker + elt->pe_offset);
484493b2041SWarner Losh if ((elt->pe_kind & TYPE_FLAGGED) && v4 == 0xffffffff)
485493b2041SWarner Losh value = -1;
486493b2041SWarner Losh else
487493b2041SWarner Losh value = v4;
488493b2041SWarner Losh break;
489493b2041SWarner Losh default:
490493b2041SWarner Losh errx(1, "Invalid size somehow %#x", elt->pe_kind);
491b03747e9SWarner Losh }
492493b2041SWarner Losh if (verbose > 1)
493493b2041SWarner Losh printf("I:%#x;", value);
494493b2041SWarner Losh record_int(value);
495493b2041SWarner Losh } else if (elt->pe_kind == TYPE_T) {
496493b2041SWarner Losh /* Do nothing */
497493b2041SWarner Losh } else { /* E, Z or D -- P already filtered */
498493b2041SWarner Losh if (elt->pe_kind == TYPE_E) {
4990299afdfSJohn Baldwin v4 = parse_32(walker + elt->pe_offset);
500493b2041SWarner Losh strcpy(buffer, pnp_eisaformat(v4));
501493b2041SWarner Losh } else {
5020299afdfSJohn Baldwin GElf_Addr address;
503493b2041SWarner Losh
5040299afdfSJohn Baldwin address = elf_address_from_pointer(ef, walker +
5050299afdfSJohn Baldwin elt->pe_offset);
506493b2041SWarner Losh buffer[0] = '\0';
5070299afdfSJohn Baldwin if (address != 0) {
5080299afdfSJohn Baldwin elf_read_string(ef, address, buffer,
5090299afdfSJohn Baldwin sizeof(buffer));
510493b2041SWarner Losh buffer[sizeof(buffer) - 1] = '\0';
511493b2041SWarner Losh }
512493b2041SWarner Losh }
513493b2041SWarner Losh if (verbose > 1)
514c40fa3dcSJohn Baldwin printf("%c:%s;", elt->pe_kind == TYPE_E ? 'E' :
515c40fa3dcSJohn Baldwin (elt->pe_kind == TYPE_Z ? 'Z' : 'D'), buffer);
516493b2041SWarner Losh record_string(buffer);
517493b2041SWarner Losh }
518493b2041SWarner Losh }
519c40fa3dcSJohn Baldwin
520c40fa3dcSJohn Baldwin static void
record_pnp_info(struct elf_file * ef,const char * cval,struct Gmod_pnp_match_info * pnp,const char * descr)521c40fa3dcSJohn Baldwin record_pnp_info(struct elf_file *ef, const char *cval,
5220299afdfSJohn Baldwin struct Gmod_pnp_match_info *pnp, const char *descr)
523c40fa3dcSJohn Baldwin {
524c40fa3dcSJohn Baldwin pnp_list list;
525c40fa3dcSJohn Baldwin struct pnp_elt *elt;
526c40fa3dcSJohn Baldwin char *new_descr, *walker;
527c40fa3dcSJohn Baldwin void *table;
528c40fa3dcSJohn Baldwin size_t len;
529c40fa3dcSJohn Baldwin int error, i;
530c40fa3dcSJohn Baldwin
531c40fa3dcSJohn Baldwin if (verbose > 1)
532c40fa3dcSJohn Baldwin printf(" pnp info for bus %s format %s %d entries of %d bytes\n",
533c40fa3dcSJohn Baldwin cval, descr, pnp->num_entry, pnp->entry_len);
534c40fa3dcSJohn Baldwin
535c40fa3dcSJohn Baldwin /*
536c40fa3dcSJohn Baldwin * Parse descr to weed out the chaff and to create a list
537c40fa3dcSJohn Baldwin * of offsets to output.
538c40fa3dcSJohn Baldwin */
5390299afdfSJohn Baldwin parse_pnp_list(ef, descr, &new_descr, &list);
540c40fa3dcSJohn Baldwin record_int(MDT_PNP_INFO);
541c40fa3dcSJohn Baldwin record_string(cval);
542c40fa3dcSJohn Baldwin record_string(new_descr);
543c40fa3dcSJohn Baldwin record_int(pnp->num_entry);
544c40fa3dcSJohn Baldwin len = pnp->num_entry * pnp->entry_len;
5450299afdfSJohn Baldwin error = elf_read_relocated_data(ef, pnp->table, len, &table);
546c40fa3dcSJohn Baldwin if (error != 0) {
547c40fa3dcSJohn Baldwin free_pnp_list(new_descr, &list);
548c40fa3dcSJohn Baldwin return;
549c40fa3dcSJohn Baldwin }
550c40fa3dcSJohn Baldwin
551c40fa3dcSJohn Baldwin /*
552c40fa3dcSJohn Baldwin * Walk the list and output things. We've collapsed all the
553c40fa3dcSJohn Baldwin * variant forms of the table down to just ints and strings.
554c40fa3dcSJohn Baldwin */
555c40fa3dcSJohn Baldwin walker = table;
556c40fa3dcSJohn Baldwin for (i = 0; i < pnp->num_entry; i++) {
557c40fa3dcSJohn Baldwin TAILQ_FOREACH(elt, &list, next) {
558c40fa3dcSJohn Baldwin parse_pnp_entry(ef, elt, walker);
559c40fa3dcSJohn Baldwin }
560493b2041SWarner Losh if (verbose > 1)
561493b2041SWarner Losh printf("\n");
562c40fa3dcSJohn Baldwin walker += pnp->entry_len;
563493b2041SWarner Losh }
564c40fa3dcSJohn Baldwin
565493b2041SWarner Losh /* Now free it */
566c40fa3dcSJohn Baldwin free_pnp_list(new_descr, &list);
567493b2041SWarner Losh free(table);
568493b2041SWarner Losh }
569c40fa3dcSJohn Baldwin
570c40fa3dcSJohn Baldwin static int
parse_entry(struct Gmod_metadata * md,const char * cval,struct elf_file * ef,const char * kldname)5710299afdfSJohn Baldwin parse_entry(struct Gmod_metadata *md, const char *cval,
572c40fa3dcSJohn Baldwin struct elf_file *ef, const char *kldname)
573c40fa3dcSJohn Baldwin {
5740299afdfSJohn Baldwin struct Gmod_depend mdp;
5750299afdfSJohn Baldwin struct Gmod_version mdv;
5760299afdfSJohn Baldwin struct Gmod_pnp_match_info pnp;
577c40fa3dcSJohn Baldwin char descr[1024];
5780299afdfSJohn Baldwin GElf_Addr data;
579c40fa3dcSJohn Baldwin int error;
580c40fa3dcSJohn Baldwin
5810299afdfSJohn Baldwin data = md->md_data;
582c40fa3dcSJohn Baldwin error = 0;
583c40fa3dcSJohn Baldwin record_start();
584c40fa3dcSJohn Baldwin switch (md->md_type) {
585c40fa3dcSJohn Baldwin case MDT_DEPEND:
586c40fa3dcSJohn Baldwin if (!dflag)
587c40fa3dcSJohn Baldwin break;
5880299afdfSJohn Baldwin check(elf_read_mod_depend(ef, data, &mdp));
589c40fa3dcSJohn Baldwin printf(" depends on %s.%d (%d,%d)\n", cval,
590c40fa3dcSJohn Baldwin mdp.md_ver_preferred, mdp.md_ver_minimum, mdp.md_ver_maximum);
591c40fa3dcSJohn Baldwin break;
592c40fa3dcSJohn Baldwin case MDT_VERSION:
5930299afdfSJohn Baldwin check(elf_read_mod_version(ef, data, &mdv));
594c40fa3dcSJohn Baldwin if (dflag) {
595c40fa3dcSJohn Baldwin printf(" interface %s.%d\n", cval, mdv.mv_version);
596c40fa3dcSJohn Baldwin } else {
597c40fa3dcSJohn Baldwin record_int(MDT_VERSION);
598c40fa3dcSJohn Baldwin record_string(cval);
599c40fa3dcSJohn Baldwin record_int(mdv.mv_version);
600c40fa3dcSJohn Baldwin record_string(kldname);
601c40fa3dcSJohn Baldwin }
602c40fa3dcSJohn Baldwin break;
603c40fa3dcSJohn Baldwin case MDT_MODULE:
604c40fa3dcSJohn Baldwin if (dflag) {
605c40fa3dcSJohn Baldwin printf(" module %s\n", cval);
606c40fa3dcSJohn Baldwin } else {
607c40fa3dcSJohn Baldwin record_int(MDT_MODULE);
608c40fa3dcSJohn Baldwin record_string(cval);
609c40fa3dcSJohn Baldwin record_string(kldname);
610c40fa3dcSJohn Baldwin }
611c40fa3dcSJohn Baldwin break;
612c40fa3dcSJohn Baldwin case MDT_PNP_INFO:
6130299afdfSJohn Baldwin check(elf_read_mod_pnp_match_info(ef, data, &pnp));
6140299afdfSJohn Baldwin check(elf_read_string(ef, pnp.descr, descr, sizeof(descr)));
615c40fa3dcSJohn Baldwin if (dflag) {
616c40fa3dcSJohn Baldwin printf(" pnp info for bus %s format %s %d entries of %d bytes\n",
617c40fa3dcSJohn Baldwin cval, descr, pnp.num_entry, pnp.entry_len);
618c40fa3dcSJohn Baldwin } else {
619c40fa3dcSJohn Baldwin record_pnp_info(ef, cval, &pnp, descr);
620c40fa3dcSJohn Baldwin }
621493b2041SWarner Losh break;
6229c6f9240SPeter Wemm default:
6239f5529b4SRuslan Ermilov warnx("unknown metadata record %d in file %s", md->md_type, kldname);
6249c6f9240SPeter Wemm }
6259c6f9240SPeter Wemm if (!error)
6269c6f9240SPeter Wemm record_end();
627e2d0802cSEd Maste return (error);
6289c6f9240SPeter Wemm }
6299c6f9240SPeter Wemm
6309c6f9240SPeter Wemm static int
read_kld(char * filename,char * kldname)6319c6f9240SPeter Wemm read_kld(char *filename, char *kldname)
6329c6f9240SPeter Wemm {
6330299afdfSJohn Baldwin struct Gmod_metadata md;
6349c6f9240SPeter Wemm struct elf_file ef;
6350299afdfSJohn Baldwin GElf_Addr *p;
6360299afdfSJohn Baldwin int error;
6370299afdfSJohn Baldwin long entries, i;
638e2d0802cSEd Maste char cval[MAXMODNAME + 1];
6399c6f9240SPeter Wemm
6409c6f9240SPeter Wemm if (verbose || dflag)
6419c6f9240SPeter Wemm printf("%s\n", filename);
6420299afdfSJohn Baldwin
6430299afdfSJohn Baldwin error = elf_open_file(&ef, filename, verbose);
6440299afdfSJohn Baldwin if (error != 0)
645e2d0802cSEd Maste return (error);
6460299afdfSJohn Baldwin
6470299afdfSJohn Baldwin if (reccnt == 0) {
6480299afdfSJohn Baldwin ehdr = ef.ef_hdr;
6490299afdfSJohn Baldwin byte_order = elf_encoding(&ef);
6500299afdfSJohn Baldwin free(ehdr_filename);
6510299afdfSJohn Baldwin ehdr_filename = strdup(filename);
6520299afdfSJohn Baldwin } else if (!elf_compatible(&ef, &ehdr)) {
6530299afdfSJohn Baldwin warnx("%s does not match architecture of %s",
6540299afdfSJohn Baldwin filename, ehdr_filename);
6550299afdfSJohn Baldwin elf_close_file(&ef);
6560299afdfSJohn Baldwin return (EINVAL);
6579772dc2aSIan Dowse }
6580299afdfSJohn Baldwin
6599c6f9240SPeter Wemm do {
6600299afdfSJohn Baldwin check(elf_read_linker_set(&ef, MDT_SETNAME, &p, &entries));
6610299afdfSJohn Baldwin
6629c1fa7a4SConrad Meyer /*
6639c1fa7a4SConrad Meyer * Do a first pass to find MDT_MODULE. It is required to be
6649c1fa7a4SConrad Meyer * ordered first in the output linker.hints stream because it
6659c1fa7a4SConrad Meyer * serves as an implicit record boundary between distinct klds
6669c1fa7a4SConrad Meyer * in the stream. Other MDTs only make sense in the context of
6679c1fa7a4SConrad Meyer * a specific MDT_MODULE.
6689c1fa7a4SConrad Meyer *
6699c1fa7a4SConrad Meyer * Some compilers (e.g., GCC 6.4.0 xtoolchain) or binutils
6709c1fa7a4SConrad Meyer * (e.g., GNU binutils 2.32 objcopy/ld.bfd) can reorder
6719c1fa7a4SConrad Meyer * MODULE_METADATA set entries relative to the source ordering.
6729c1fa7a4SConrad Meyer * This is permitted by the C standard; memory layout of
6739c1fa7a4SConrad Meyer * file-scope objects is left implementation-defined. There is
6749c1fa7a4SConrad Meyer * no requirement that source code ordering is retained.
6759c1fa7a4SConrad Meyer *
6769c1fa7a4SConrad Meyer * Handle that here by taking two passes to ensure MDT_MODULE
6779c1fa7a4SConrad Meyer * records are emitted to linker.hints before other MDT records
6789c1fa7a4SConrad Meyer * in the same kld.
6799c1fa7a4SConrad Meyer */
6809c1fa7a4SConrad Meyer for (i = 0; i < entries; i++) {
6810299afdfSJohn Baldwin check(elf_read_mod_metadata(&ef, p[i], &md));
6820299afdfSJohn Baldwin check(elf_read_string(&ef, md.md_cval, cval,
6830299afdfSJohn Baldwin sizeof(cval)));
6849c1fa7a4SConrad Meyer if (md.md_type == MDT_MODULE) {
6859c1fa7a4SConrad Meyer parse_entry(&md, cval, &ef, kldname);
6869c1fa7a4SConrad Meyer break;
6879c1fa7a4SConrad Meyer }
6889c1fa7a4SConrad Meyer }
6899c1fa7a4SConrad Meyer if (error != 0) {
6900299afdfSJohn Baldwin free(p);
6919c1fa7a4SConrad Meyer warnc(error, "error while reading %s", filename);
6929c1fa7a4SConrad Meyer break;
6939c1fa7a4SConrad Meyer }
6949c1fa7a4SConrad Meyer
6959c1fa7a4SConrad Meyer /*
6969c1fa7a4SConrad Meyer * Second pass for all !MDT_MODULE entries.
6979c1fa7a4SConrad Meyer */
6989c1fa7a4SConrad Meyer for (i = 0; i < entries; i++) {
6990299afdfSJohn Baldwin check(elf_read_mod_metadata(&ef, p[i], &md));
7000299afdfSJohn Baldwin check(elf_read_string(&ef, md.md_cval, cval,
7010299afdfSJohn Baldwin sizeof(cval)));
7029c1fa7a4SConrad Meyer if (md.md_type != MDT_MODULE)
7039c6f9240SPeter Wemm parse_entry(&md, cval, &ef, kldname);
7049c6f9240SPeter Wemm }
705e2d0802cSEd Maste if (error != 0)
7069c6f9240SPeter Wemm warnc(error, "error while reading %s", filename);
7079c1fa7a4SConrad Meyer free(p);
7089c6f9240SPeter Wemm } while(0);
7090299afdfSJohn Baldwin elf_close_file(&ef);
710e2d0802cSEd Maste return (error);
7119c6f9240SPeter Wemm }
7129c6f9240SPeter Wemm
7139cb138bbSLuigi Rizzo /*
7149cb138bbSLuigi Rizzo * Create a temp file in directory root, make sure we don't
7159cb138bbSLuigi Rizzo * overflow the buffer for the destination name
7169cb138bbSLuigi Rizzo */
7179cb138bbSLuigi Rizzo static FILE *
maketempfile(char * dest,const char * root)7189c6f9240SPeter Wemm maketempfile(char *dest, const char *root)
7199c6f9240SPeter Wemm {
720f4613af4SMina Galić int fd;
7219c6f9240SPeter Wemm
722f4613af4SMina Galić if (snprintf(dest, MAXPATHLEN, "%s/lhint.XXXXXX", root) >=
7239cb138bbSLuigi Rizzo MAXPATHLEN) {
7249cb138bbSLuigi Rizzo errno = ENAMETOOLONG;
725e2d0802cSEd Maste return (NULL);
7269cb138bbSLuigi Rizzo }
7279c6f9240SPeter Wemm
7289ceddbd5SMarcel Moolenaar fd = mkstemp(dest);
7299cb138bbSLuigi Rizzo if (fd < 0)
730e2d0802cSEd Maste return (NULL);
731ddce5818SLuigi Rizzo fchmod(fd, 0644); /* nothing secret in the file */
732e2d0802cSEd Maste return (fdopen(fd, "w+"));
7339c6f9240SPeter Wemm }
7349c6f9240SPeter Wemm
7359c6f9240SPeter Wemm static char xrefname[MAXPATHLEN], tempname[MAXPATHLEN];
7369c6f9240SPeter Wemm
7379cb138bbSLuigi Rizzo static void
usage(void)7389cb138bbSLuigi Rizzo usage(void)
7399cb138bbSLuigi Rizzo {
7409cb138bbSLuigi Rizzo
7419cb138bbSLuigi Rizzo fprintf(stderr, "%s\n",
7429cb138bbSLuigi Rizzo "usage: kldxref [-Rdv] [-f hintsfile] path ..."
7439cb138bbSLuigi Rizzo );
7449cb138bbSLuigi Rizzo exit(1);
7459cb138bbSLuigi Rizzo }
7469cb138bbSLuigi Rizzo
7475d452ceaSJilles Tjoelker static int
748364c014dSWarner Losh #if defined(__linux__) || defined(__APPLE__)
compare(const FTSENT ** a,const FTSENT ** b)74930887c7dSAlex Richardson compare(const FTSENT **a, const FTSENT **b)
75030887c7dSAlex Richardson #else
7516d9cb20bSJilles Tjoelker compare(const FTSENT *const *a, const FTSENT *const *b)
75230887c7dSAlex Richardson #endif
7536d9cb20bSJilles Tjoelker {
754e2d0802cSEd Maste
7556d9cb20bSJilles Tjoelker if ((*a)->fts_info == FTS_D && (*b)->fts_info != FTS_D)
756e2d0802cSEd Maste return (1);
7576d9cb20bSJilles Tjoelker if ((*a)->fts_info != FTS_D && (*b)->fts_info == FTS_D)
758e2d0802cSEd Maste return (-1);
759e2d0802cSEd Maste return (strcmp((*a)->fts_name, (*b)->fts_name));
7606d9cb20bSJilles Tjoelker }
7616d9cb20bSJilles Tjoelker
7626d9cb20bSJilles Tjoelker int
main(int argc,char * argv[])7639c6f9240SPeter Wemm main(int argc, char *argv[])
7649c6f9240SPeter Wemm {
7659c6f9240SPeter Wemm FTS *ftsp;
7669c6f9240SPeter Wemm FTSENT *p;
767773c13c6SMina Galić char *dot = NULL;
7680299afdfSJohn Baldwin int opt, fts_options;
769b7abc67eSMaxim Sobolev struct stat sb;
7709c6f9240SPeter Wemm
7719c6f9240SPeter Wemm fts_options = FTS_PHYSICAL;
7729c6f9240SPeter Wemm
7739c6f9240SPeter Wemm while ((opt = getopt(argc, argv, "Rdf:v")) != -1) {
7749c6f9240SPeter Wemm switch (opt) {
7759cb138bbSLuigi Rizzo case 'd': /* no hint file, only print on stdout */
776e2d0802cSEd Maste dflag = true;
7779c6f9240SPeter Wemm break;
7789cb138bbSLuigi Rizzo case 'f': /* use this name instead of linker.hints */
7799c6f9240SPeter Wemm xref_file = optarg;
7809c6f9240SPeter Wemm break;
7819c6f9240SPeter Wemm case 'v':
7829c6f9240SPeter Wemm verbose++;
7839c6f9240SPeter Wemm break;
7849cb138bbSLuigi Rizzo case 'R': /* recurse on directories */
7859c6f9240SPeter Wemm fts_options |= FTS_COMFOLLOW;
7869c6f9240SPeter Wemm break;
7879c6f9240SPeter Wemm default:
7889c6f9240SPeter Wemm usage();
7899c6f9240SPeter Wemm /* NOTREACHED */
7909c6f9240SPeter Wemm }
7919c6f9240SPeter Wemm }
7929c6f9240SPeter Wemm if (argc - optind < 1)
7939c6f9240SPeter Wemm usage();
7949c6f9240SPeter Wemm argc -= optind;
7959c6f9240SPeter Wemm argv += optind;
7969c6f9240SPeter Wemm
797b7abc67eSMaxim Sobolev if (stat(argv[0], &sb) != 0)
798b7abc67eSMaxim Sobolev err(1, "%s", argv[0]);
799d7751071SKonstantin Belousov if ((sb.st_mode & S_IFDIR) == 0 && !dflag) {
800b7abc67eSMaxim Sobolev errno = ENOTDIR;
801b7abc67eSMaxim Sobolev err(1, "%s", argv[0]);
802b7abc67eSMaxim Sobolev }
803b7abc67eSMaxim Sobolev
8040299afdfSJohn Baldwin if (elf_version(EV_CURRENT) == EV_NONE)
8050299afdfSJohn Baldwin errx(1, "unsupported libelf");
8060299afdfSJohn Baldwin
8076d9cb20bSJilles Tjoelker ftsp = fts_open(argv, fts_options, compare);
8089c6f9240SPeter Wemm if (ftsp == NULL)
8099c6f9240SPeter Wemm exit(1);
8109c6f9240SPeter Wemm
8119c6f9240SPeter Wemm for (;;) {
8129c6f9240SPeter Wemm p = fts_read(ftsp);
8139cb138bbSLuigi Rizzo if ((p == NULL || p->fts_info == FTS_D) && fxref) {
8149cb138bbSLuigi Rizzo /* close and rename the current hint file */
8159c6f9240SPeter Wemm fclose(fxref);
8169ceddbd5SMarcel Moolenaar fxref = NULL;
817e2d0802cSEd Maste if (reccnt != 0) {
8189c6f9240SPeter Wemm rename(tempname, xrefname);
8199c6f9240SPeter Wemm } else {
8209cb138bbSLuigi Rizzo /* didn't find any entry, ignore this file */
8219c6f9240SPeter Wemm unlink(tempname);
8229c6f9240SPeter Wemm unlink(xrefname);
8239c6f9240SPeter Wemm }
8249c6f9240SPeter Wemm }
8259c6f9240SPeter Wemm if (p == NULL)
8269c6f9240SPeter Wemm break;
8279cb138bbSLuigi Rizzo if (p->fts_info == FTS_D && !dflag) {
8289cb138bbSLuigi Rizzo /* visiting a new directory, create a new hint file */
8299c6f9240SPeter Wemm snprintf(xrefname, sizeof(xrefname), "%s/%s",
8309c6f9240SPeter Wemm ftsp->fts_path, xref_file);
8319ceddbd5SMarcel Moolenaar fxref = maketempfile(tempname, ftsp->fts_path);
8329c6f9240SPeter Wemm if (fxref == NULL)
8339c6f9240SPeter Wemm err(1, "can't create %s", tempname);
8340299afdfSJohn Baldwin byte_order = ELFDATANONE;
8359c6f9240SPeter Wemm reccnt = 0;
8369c6f9240SPeter Wemm }
837773c13c6SMina Galić /* skip non-files.. */
8389c6f9240SPeter Wemm if (p->fts_info != FTS_F)
8399c6f9240SPeter Wemm continue;
840773c13c6SMina Galić /*
841773c13c6SMina Galić * Skip files that generate errors like .debug, .symbol and .pkgsave
84297c31cc8SJessica Clarke * by generally skipping all files which neither end with ".ko"
84397c31cc8SJessica Clarke * nor have no dots in the name (like kernel).
844773c13c6SMina Galić */
8452b92b754SWarner Losh dot = strrchr(p->fts_name, '.');
8462b92b754SWarner Losh if (dot != NULL && strcmp(dot, ".ko") != 0)
8479f5529b4SRuslan Ermilov continue;
8489c6f9240SPeter Wemm read_kld(p->fts_path, p->fts_name);
8499c6f9240SPeter Wemm }
8509c6f9240SPeter Wemm fts_close(ftsp);
851e2d0802cSEd Maste return (0);
8529c6f9240SPeter Wemm }
853