xref: /freebsd/sbin/ldconfig/ldconfig.c (revision af9115870670f508c11b3d173bcff5116d8ef320)
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"
51*af911587SKonstantin Belousov #include "rtld_paths.h"
5280c71499SPeter Wemm 
53c905e45dSPeter Wemm #define	_PATH_LD32_HINTS	"/var/run/ld32.so.hints"
54c905e45dSPeter Wemm #define	_PATH_ELF32_HINTS	"/var/run/ld-elf32.so.hints"
554153c211SWarner Losh #define	_PATH_ELFSOFT_HINTS	"/var/run/ld-elf-soft.so.hints"
56c905e45dSPeter Wemm 
5785429990SWarner Losh static void usage(void);
58b9ae52e3SPaul Richards 
59b9ae52e3SPaul Richards int
6006eda379SXin LI main(int argc, char **argv)
61b9ae52e3SPaul Richards {
623f2c6f55SKonstantin Belousov 	const char *hints_file;
6350a40d09SEd Maste 	int c;
64b828161dSKonstantin Belousov 	bool is_32, is_soft, justread, merge, rescan, verbose;
653f2c6f55SKonstantin Belousov 
66b828161dSKonstantin Belousov 	is_32 = is_soft = justread = merge = rescan = verbose = false;
67b9ae52e3SPaul Richards 
68c905e45dSPeter Wemm 	while (argc > 1) {
69c905e45dSPeter Wemm 		if (strcmp(argv[1], "-aout") == 0) {
7050a40d09SEd Maste 			errx(1, "aout is not supported");
71c905e45dSPeter Wemm 		} else if (strcmp(argv[1], "-elf") == 0) {
7266422f5bSPeter Wemm 			argc--;
7366422f5bSPeter Wemm 			argv++;
74c905e45dSPeter Wemm 		} else if (strcmp(argv[1], "-32") == 0) {
753f2c6f55SKonstantin Belousov 			is_32 = true;
76c905e45dSPeter Wemm 			argc--;
77c905e45dSPeter Wemm 			argv++;
784153c211SWarner Losh 		} else if (strcmp(argv[1], "-soft") == 0) {
793f2c6f55SKonstantin Belousov 			is_soft = true;
804153c211SWarner Losh 			argc--;
814153c211SWarner Losh 			argv++;
82c905e45dSPeter Wemm 		} else {
83c905e45dSPeter Wemm 			break;
84c905e45dSPeter Wemm 		}
8566422f5bSPeter Wemm 	}
86cfa4d739SJohn Polstra 
874153c211SWarner Losh 	if (is_soft)
88*af911587SKonstantin Belousov 		hints_file = _PATH_SOFT_ELF_HINTS;
894153c211SWarner Losh 	else if (is_32)
9050a40d09SEd Maste 		hints_file = _PATH_ELF32_HINTS;
91c905e45dSPeter Wemm 	else
9250a40d09SEd Maste 		hints_file = _PATH_ELF_HINTS;
9397333b9eSJohn Polstra 	if (argc == 1)
943f2c6f55SKonstantin Belousov 		rescan = true;
95643dcf40SJohn Polstra 	else while((c = getopt(argc, argv, "Rf:imrsv")) != -1) {
96b9ae52e3SPaul Richards 		switch (c) {
97d4ba5766SPeter Wemm 		case 'R':
983f2c6f55SKonstantin Belousov 			rescan = true;
99d4ba5766SPeter Wemm 			break;
1007c6da7dcSJohn Polstra 		case 'f':
1017c6da7dcSJohn Polstra 			hints_file = optarg;
1027c6da7dcSJohn Polstra 			break;
103643dcf40SJohn Polstra 		case 'i':
1043f2c6f55SKonstantin Belousov 			insecure = true;
105643dcf40SJohn Polstra 			break;
106f606c848SSatoshi Asami 		case 'm':
1073f2c6f55SKonstantin Belousov 			merge = true;
108b9ae52e3SPaul Richards 			break;
109b9ae52e3SPaul Richards 		case 'r':
1103f2c6f55SKonstantin Belousov 			justread = true;
111b9ae52e3SPaul Richards 			break;
112f606c848SSatoshi Asami 		case 's':
113b828161dSKonstantin Belousov 			/* was nostd */
114f606c848SSatoshi Asami 			break;
115f606c848SSatoshi Asami 		case 'v':
1163f2c6f55SKonstantin Belousov 			verbose = true;
117f606c848SSatoshi Asami 			break;
118b9ae52e3SPaul Richards 		default:
119b50d7faeSPhilippe Charnier 			usage();
120b9ae52e3SPaul Richards 			break;
121b9ae52e3SPaul Richards 		}
122b9ae52e3SPaul Richards 	}
123b9ae52e3SPaul Richards 
124a565ca59SJohn Polstra 	if (justread)
125a565ca59SJohn Polstra 		list_elf_hints(hints_file);
126a565ca59SJohn Polstra 	else
127a565ca59SJohn Polstra 		update_elf_hints(hints_file, argc - optind,
128a565ca59SJohn Polstra 		    argv + optind, merge || rescan);
1293f2c6f55SKonstantin Belousov 	exit(0);
130a565ca59SJohn Polstra }
131a565ca59SJohn Polstra 
132b50d7faeSPhilippe Charnier static void
133f709df34SEd Schouten usage(void)
134b50d7faeSPhilippe Charnier {
135b50d7faeSPhilippe Charnier 	fprintf(stderr,
136b828161dSKonstantin Belousov 	    "usage: ldconfig [-32] [-elf] [-Rimrv] [-f hints_file] "
1373f2c6f55SKonstantin Belousov 	    "[directory | file ...]\n");
138b50d7faeSPhilippe Charnier 	exit(1);
139b50d7faeSPhilippe Charnier }
140