xref: /titanic_41/usr/src/cmd/boot/symdef/symdef.c (revision 2654012f83cec5dc15b61dfe3e4a4915f186e7a6)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <stdio.h>
30 #include <string.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <fcntl.h>
34 #include <unistd.h>
35 #include <libelf.h>
36 #include <gelf.h>
37 #include <errno.h>
38 
39 /*
40  * symdef is a very simplified version of nm.  It is used by upgrade and
41  * create_ramdisk in situations where we can't guarantee that nm will be around.
42  *
43  * Two arguments are expected: a binary and a symbol name.  If the symbol is
44  * found in the name table of the binary, 0 is returned.  If it is not found,
45  * 1 is returned.  If an error occurs, a message is printed to stderr and -1
46  * is returned.
47  */
48 
49 
50 static void
51 usage(void)
52 {
53 	(void) fprintf(stderr, "USAGE: symdef file_name symbol\n");
54 }
55 
56 int
57 main(int argc, char *argv[])
58 {
59 	int	fd = 0;
60 	int	rv = 1;
61 	uint_t	cnt, symcnt;
62 	Elf	*elfp = NULL;
63 	Elf_Scn	*scn = NULL;
64 	size_t	shstrndx;
65 	GElf_Ehdr	ehdr;
66 	GElf_Shdr	shdr;
67 	GElf_Sym	sym;
68 	Elf32_Word	shndx;
69 	Elf_Data	*symdata, *shndxdata;
70 
71 	if (argc != 3) {
72 		usage();
73 		return (-1);
74 	}
75 
76 	fd = open(argv[1], O_RDONLY);
77 	if (fd == -1) {
78 		(void) fprintf(stderr, "%s\n", strerror(errno));
79 		rv = -1;
80 		goto done;
81 	}
82 	if (elf_version(EV_CURRENT) == EV_NONE) {
83 		(void) fprintf(stderr, "Elf library version out of date\n");
84 		rv = -1;
85 		goto done;
86 	}
87 	elfp = elf_begin(fd, ELF_C_READ, NULL);
88 	if ((elfp == NULL) || (elf_kind(elfp) != ELF_K_ELF) ||
89 	    ((gelf_getehdr(elfp, &ehdr)) == NULL) ||
90 	    (elf_getshstrndx(elfp, &shstrndx) == 0))
91 		goto done;
92 
93 	while ((scn = elf_nextscn(elfp, scn)) != NULL) {
94 		if ((gelf_getshdr(scn, &shdr) == NULL) ||
95 		    ((shdr.sh_type != SHT_SYMTAB) &&
96 		    (shdr.sh_type != SHT_DYNSYM)) ||
97 		    ((symdata = elf_getdata(scn, NULL)) == NULL))
98 			continue;
99 		symcnt = shdr.sh_size / shdr.sh_entsize;
100 		shndxdata = NULL;
101 		for (cnt = 0; cnt < symcnt; cnt++) {
102 			if ((gelf_getsymshndx(symdata, shndxdata, cnt,
103 			    &sym, &shndx) != NULL) &&
104 			    (strcmp(argv[2], elf_strptr(elfp, shdr.sh_link,
105 			    sym.st_name)) == 0)) {
106 				rv = 0;
107 				goto done;
108 			}
109 		}
110 	}
111 done:
112 	if (elfp)
113 		(void) elf_end(elfp);
114 	if (fd != -1)
115 		(void) close(fd);
116 	return (rv);
117 }
118