4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
22 /* Copyright (c) 1988 AT&T */
23 /* All Rights Reserved */
27 * Copyright (c) 1998 by Sun Microsystems, Inc.
28 * All rights reserved.
39 #undef n_name /* This undef is to handle a #define in syms.h */
40 /* which conflicts with the member nlist->n_name */
41 /* as defined in nlist.h */
44 #define SPACE 100 /* number of symbols read at a time */
45 #define ISELF (strncmp(magic_buf, ELFMAG, SELFMAG) == 0)
49 end_elf_job(int fd
, Elf
* elfdes
)
51 (void) elf_end(elfdes
);
58 _elf_nlist(int fd
, struct nlist
* list
)
60 Elf
*elfdes
; /* ELF descriptor */
61 GElf_Ehdr ehdr
; /* ELF Ehdr */
62 GElf_Shdr s_buf
; /* buffer storing section header */
63 Elf_Data
*symdata
; /* buffer points to symbol table */
64 Elf_Scn
*secidx
= 0; /* index of the section header table */
65 GElf_Sym sym
; /* buffer storing one symbol information */
66 unsigned strtab
; /* index of symbol name in string table */
67 long count
; /* number of symbols */
68 long ii
; /* loop control */
70 if (elf_version(EV_CURRENT
) == EV_NONE
) {
74 elfdes
= elf_begin(fd
, ELF_C_READ
, (Elf
*)0);
75 if (gelf_getehdr(elfdes
, &ehdr
) == 0)
76 return (end_elf_job(fd
, elfdes
));
78 while ((secidx
= elf_nextscn(elfdes
, secidx
)) != 0) {
79 if ((gelf_getshdr(secidx
, &s_buf
)) == 0)
80 return (end_elf_job(fd
, elfdes
));
81 if (s_buf
.sh_type
!= SHT_SYMTAB
) /* not symbol table */
83 symdata
= elf_getdata(secidx
, (Elf_Data
*)0);
85 return (end_elf_job(fd
, elfdes
));
86 if (symdata
->d_size
== 0)
88 strtab
= s_buf
.sh_link
;
89 count
= symdata
->d_size
/ s_buf
.sh_entsize
;
90 for (ii
= 1; ii
< count
; ++ii
) {
94 (void) gelf_getsym(symdata
, (int)ii
, &sym
);
95 name
= elf_strptr(elfdes
, strtab
, (size_t)sym
.st_name
);
98 for (p
= list
; p
->n_name
&& p
->n_name
[0]; ++p
) {
99 if (strcmp(p
->n_name
, name
))
101 p
->n_value
= (long)sym
.st_value
;
102 p
->n_type
= GELF_ST_TYPE(sym
.st_info
);
103 p
->n_scnum
= sym
.st_shndx
;
109 * Currently there is only one symbol table section
110 * in an object file, but this restriction may be
111 * relaxed in the future.
114 (void) elf_end(elfdes
);
120 nlist(const char * name
, struct nlist
* list
)
122 register struct nlist
*p
;
123 char magic_buf
[EI_NIDENT
];
126 for (p
= list
; p
->n_name
&& p
->n_name
[0]; p
++) { /* n_name can be ptr */
134 if ((fd
= open(name
, O_RDONLY
)) < 0)
136 if (read(fd
, magic_buf
, EI_NIDENT
) == -1) {
141 if (lseek(fd
, 0L, 0) == -1L) { /* rewind to beginning of object file */
147 if (ISELF
&& (magic_buf
[EI_CLASS
] == ELFCLASS32
))
149 if (ISELF
) /* 64-bit case handles both Elf32 and Elf64 */
151 return (_elf_nlist(fd
, list
));