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.
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.
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]
23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
31 #include <sys/types.h>
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.
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
53 (void) fprintf(stderr
, "USAGE: symdef file_name symbol\n");
57 main(int argc
, char *argv
[])
69 Elf_Data
*symdata
, *shndxdata
;
76 fd
= open(argv
[1], O_RDONLY
);
78 (void) fprintf(stderr
, "%s\n", strerror(errno
));
82 if (elf_version(EV_CURRENT
) == EV_NONE
) {
83 (void) fprintf(stderr
, "Elf library version out of date\n");
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))
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
))
99 symcnt
= shdr
.sh_size
/ shdr
.sh_entsize
;
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)) {
113 (void) elf_end(elfp
);