xref: /freebsd/sbin/ldconfig/ldconfig.c (revision b828161d123bec894bc5a320ef26d6afc9b13ae8)
11de7b4b8SPedro F. Giffuni /*-
21de7b4b8SPedro F. Giffuni  * SPDX-License-Identifier: BSD-4-Clause
31de7b4b8SPedro F. Giffuni  *
40f6b2cb3SPaul Traina  * Copyright (c) 1993,1995 Paul Kranenburg
5b9ae52e3SPaul Richards  * All rights reserved.
6b9ae52e3SPaul Richards  *
7b9ae52e3SPaul Richards  * Redistribution and use in source and binary forms, with or without
8b9ae52e3SPaul Richards  * modification, are permitted provided that the following conditions
9b9ae52e3SPaul Richards  * are met:
10b9ae52e3SPaul Richards  * 1. Redistributions of source code must retain the above copyright
11b9ae52e3SPaul Richards  *    notice, this list of conditions and the following disclaimer.
12b9ae52e3SPaul Richards  * 2. Redistributions in binary form must reproduce the above copyright
13b9ae52e3SPaul Richards  *    notice, this list of conditions and the following disclaimer in the
14b9ae52e3SPaul Richards  *    documentation and/or other materials provided with the distribution.
15b9ae52e3SPaul Richards  * 3. All advertising materials mentioning features or use of this software
16b9ae52e3SPaul Richards  *    must display the following acknowledgement:
17b9ae52e3SPaul Richards  *      This product includes software developed by Paul Kranenburg.
18b9ae52e3SPaul Richards  * 4. The name of the author may not be used to endorse or promote products
1909e3d49dSJordan K. Hubbard  *    derived from this software without specific prior written permission
20b9ae52e3SPaul Richards  *
21b9ae52e3SPaul Richards  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22b9ae52e3SPaul Richards  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23b9ae52e3SPaul Richards  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24b9ae52e3SPaul Richards  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25b9ae52e3SPaul Richards  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26b9ae52e3SPaul Richards  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27b9ae52e3SPaul Richards  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28b9ae52e3SPaul Richards  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29b9ae52e3SPaul Richards  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30b9ae52e3SPaul Richards  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31b9ae52e3SPaul Richards  */
32b9ae52e3SPaul Richards 
33b9ae52e3SPaul Richards #include <sys/param.h>
34b9ae52e3SPaul Richards #include <sys/types.h>
35b9ae52e3SPaul Richards #include <sys/stat.h>
36b9ae52e3SPaul Richards #include <sys/mman.h>
37b9ae52e3SPaul Richards #include <a.out.h>
38b50d7faeSPhilippe Charnier #include <ctype.h>
39b50d7faeSPhilippe Charnier #include <dirent.h>
405e6220d9SDavid E. O'Brien #include <elf-hints.h>
41b50d7faeSPhilippe Charnier #include <err.h>
42b50d7faeSPhilippe Charnier #include <errno.h>
43b50d7faeSPhilippe Charnier #include <fcntl.h>
443f2c6f55SKonstantin Belousov #include <stdbool.h>
45699e1b82SRich Murphey #include <stdio.h>
46699e1b82SRich Murphey #include <stdlib.h>
47b9ae52e3SPaul Richards #include <string.h>
48699e1b82SRich Murphey #include <unistd.h>
49b9ae52e3SPaul Richards 
50a565ca59SJohn Polstra #include "ldconfig.h"
5180c71499SPeter Wemm 
52c905e45dSPeter Wemm #define	_PATH_LD32_HINTS	"/var/run/ld32.so.hints"
53c905e45dSPeter Wemm #define	_PATH_ELF32_HINTS	"/var/run/ld-elf32.so.hints"
544153c211SWarner Losh #define	_PATH_ELFSOFT_HINTS	"/var/run/ld-elf-soft.so.hints"
55c905e45dSPeter Wemm 
5685429990SWarner Losh static void usage(void);
57b9ae52e3SPaul Richards 
58b9ae52e3SPaul Richards int
5906eda379SXin LI main(int argc, char **argv)
60b9ae52e3SPaul Richards {
613f2c6f55SKonstantin Belousov 	const char *hints_file;
6250a40d09SEd Maste 	int c;
63*b828161dSKonstantin Belousov 	bool is_32, is_soft, justread, merge, rescan, verbose;
643f2c6f55SKonstantin Belousov 
65*b828161dSKonstantin Belousov 	is_32 = is_soft = justread = merge = rescan = verbose = false;
66b9ae52e3SPaul Richards 
67c905e45dSPeter Wemm 	while (argc > 1) {
68c905e45dSPeter Wemm 		if (strcmp(argv[1], "-aout") == 0) {
6950a40d09SEd Maste 			errx(1, "aout is not supported");
70c905e45dSPeter Wemm 		} else if (strcmp(argv[1], "-elf") == 0) {
7166422f5bSPeter Wemm 			argc--;
7266422f5bSPeter Wemm 			argv++;
73c905e45dSPeter Wemm 		} else if (strcmp(argv[1], "-32") == 0) {
743f2c6f55SKonstantin Belousov 			is_32 = true;
75c905e45dSPeter Wemm 			argc--;
76c905e45dSPeter Wemm 			argv++;
774153c211SWarner Losh 		} else if (strcmp(argv[1], "-soft") == 0) {
783f2c6f55SKonstantin Belousov 			is_soft = true;
794153c211SWarner Losh 			argc--;
804153c211SWarner Losh 			argv++;
81c905e45dSPeter Wemm 		} else {
82c905e45dSPeter Wemm 			break;
83c905e45dSPeter Wemm 		}
8466422f5bSPeter Wemm 	}
85cfa4d739SJohn Polstra 
864153c211SWarner Losh 	if (is_soft)
8783511ce5SKonstantin Belousov 		hints_file = _PATH_ELFSOFT_HINTS;
884153c211SWarner Losh 	else if (is_32)
8950a40d09SEd Maste 		hints_file = _PATH_ELF32_HINTS;
90c905e45dSPeter Wemm 	else
9150a40d09SEd Maste 		hints_file = _PATH_ELF_HINTS;
9297333b9eSJohn Polstra 	if (argc == 1)
933f2c6f55SKonstantin Belousov 		rescan = true;
94643dcf40SJohn Polstra 	else while((c = getopt(argc, argv, "Rf:imrsv")) != -1) {
95b9ae52e3SPaul Richards 		switch (c) {
96d4ba5766SPeter Wemm 		case 'R':
973f2c6f55SKonstantin Belousov 			rescan = true;
98d4ba5766SPeter Wemm 			break;
997c6da7dcSJohn Polstra 		case 'f':
1007c6da7dcSJohn Polstra 			hints_file = optarg;
1017c6da7dcSJohn Polstra 			break;
102643dcf40SJohn Polstra 		case 'i':
1033f2c6f55SKonstantin Belousov 			insecure = true;
104643dcf40SJohn Polstra 			break;
105f606c848SSatoshi Asami 		case 'm':
1063f2c6f55SKonstantin Belousov 			merge = true;
107b9ae52e3SPaul Richards 			break;
108b9ae52e3SPaul Richards 		case 'r':
1093f2c6f55SKonstantin Belousov 			justread = true;
110b9ae52e3SPaul Richards 			break;
111f606c848SSatoshi Asami 		case 's':
112*b828161dSKonstantin Belousov 			/* was nostd */
113f606c848SSatoshi Asami 			break;
114f606c848SSatoshi Asami 		case 'v':
1153f2c6f55SKonstantin Belousov 			verbose = true;
116f606c848SSatoshi Asami 			break;
117b9ae52e3SPaul Richards 		default:
118b50d7faeSPhilippe Charnier 			usage();
119b9ae52e3SPaul Richards 			break;
120b9ae52e3SPaul Richards 		}
121b9ae52e3SPaul Richards 	}
122b9ae52e3SPaul Richards 
123a565ca59SJohn Polstra 	if (justread)
124a565ca59SJohn Polstra 		list_elf_hints(hints_file);
125a565ca59SJohn Polstra 	else
126a565ca59SJohn Polstra 		update_elf_hints(hints_file, argc - optind,
127a565ca59SJohn Polstra 		    argv + optind, merge || rescan);
1283f2c6f55SKonstantin Belousov 	exit(0);
129a565ca59SJohn Polstra }
130a565ca59SJohn Polstra 
131b50d7faeSPhilippe Charnier static void
132f709df34SEd Schouten usage(void)
133b50d7faeSPhilippe Charnier {
134b50d7faeSPhilippe Charnier 	fprintf(stderr,
135*b828161dSKonstantin Belousov 	    "usage: ldconfig [-32] [-elf] [-Rimrv] [-f hints_file] "
1363f2c6f55SKonstantin Belousov 	    "[directory | file ...]\n");
137b50d7faeSPhilippe Charnier 	exit(1);
138b50d7faeSPhilippe Charnier }
139