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]
22 * Copyright (c) 1995, 2010, Oracle and/or its affiliates. All rights reserved.
26 * tpcom: Threaded Print Comment
28 * tpcom is a threaded version of the pcom program. It will create
29 * a new thread for each new ELF descriptor that it examines. It
30 * will then examine each elf descriptor and print the .comment section
33 * This program demonstrates that libelf is MT-Safe and the usage
34 * of elf_begin(ELF_C_READ).
47 #define NUMLWPS 32 /* arbitrary number of LWPS */
49 static const char *CommentStr
= ".comment";
52 * arguments to be passed into process_elf().
56 char *pe_file
; /* elf member name */
58 short pe_member
; /* is this an archive member? */
62 static mutex_t printlock
= DEFAULTMUTEX
; /* printlock used to */
64 /* of comment sections */
67 print_comment(Elf
*elf
, const char *file
)
74 if (elf_getshdrstrndx(elf
, &shstrndx
) == -1) {
75 (void) fprintf(stderr
, "%s: elf_getshdrstrndx() failed: %s\n",
80 while ((scn
= elf_nextscn(elf
, scn
)) != NULL
) {
82 * Do a string compare to examine each section header
83 * to see if it is a ".comment" section. If it is then
84 * this is the section we want to process.
86 if (gelf_getshdr(scn
, &shdr
) == NULL
) {
87 (void) fprintf(stderr
, "%s: elf_getshdr() failed: %s\n",
92 if (strcmp(CommentStr
, elf_strptr(elf
, shstrndx
,
93 shdr
.sh_name
)) == 0) {
97 (void) mutex_lock(&printlock
);
98 (void) printf("%s .comment:\n", file
);
101 * Get the data associated with the .comment
104 if ((data
= elf_getdata(scn
, NULL
)) == NULL
) {
105 (void) fprintf(stderr
,
106 "%s: elf_getdata() failed: %s\n",
107 file
, elf_errmsg(0));
108 (void) mutex_unlock(&printlock
);
112 * Data in a .comment section is a list of 'null'
113 * terminated strings. The following will print
114 * one string per line.
116 for (i
= 0, ptr
= (char *)data
->d_buf
;
117 i
< data
->d_size
; i
++)
119 (void) puts(&ptr
[i
]);
120 i
+= strlen(&ptr
[i
]);
122 (void) putchar('\n');
123 (void) mutex_unlock(&printlock
);
130 process_elf(pe_args
*pep
)
135 switch (elf_kind(pep
->pe_elf
)) {
137 print_comment(pep
->pe_elf
, pep
->pe_file
);
141 while ((_elf
= elf_begin(pep
->pe_fd
, cmd
,
142 pep
->pe_elf
)) != NULL
) {
147 if ((arhdr
= elf_getarhdr(_elf
)) == NULL
) {
148 (void) fprintf(stderr
,
149 "%s: elf_getarhdr() failed: %s\n",
150 pep
->pe_file
, elf_errmsg(0));
152 cmd
= elf_next(_elf
);
153 _pep
= malloc(sizeof (pe_args
));
155 _pep
->pe_file
= malloc(strlen(pep
->pe_file
) +
156 strlen(arhdr
->ar_name
) + 5);
157 (void) sprintf(_pep
->pe_file
,
158 "%s(%s)", pep
->pe_file
, arhdr
->ar_name
);
159 _pep
->pe_fd
= pep
->pe_fd
;
162 if ((rc
= thr_create(NULL
, 0,
163 (void *(*)(void *))process_elf
,
164 (void *)_pep
, THR_DETACHED
, 0)) != 0) {
165 (void) fprintf(stderr
,
166 "thr_create() failed, rc = %d\n", rc
);
171 if (!pep
->pe_member
) {
172 (void) mutex_lock(&printlock
);
173 (void) fprintf(stderr
,
174 "%s: unexpected elf_kind(): 0x%x\n",
175 pep
->pe_file
, elf_kind(pep
->pe_elf
));
176 (void) mutex_unlock(&printlock
);
180 (void) elf_end(pep
->pe_elf
);
188 main(int argc
, char ** argv
)
193 (void) printf("usage: %s elf_file ...\n", argv
[0]);
198 * Initialize the elf library, must be called before elf_begin()
201 if (elf_version(EV_CURRENT
) == EV_NONE
) {
202 (void) fprintf(stderr
,
203 "elf_version() failed: %s\n", elf_errmsg(0));
208 * create an arbitrary number of LWP's to run the
209 * threads that will be created.
211 if (thr_setconcurrency(NUMLWPS
) != 0) {
212 (void) fprintf(stderr
, "thread setconcurrency failed\n");
216 for (i
= 1; i
< argc
; i
++) {
225 if ((fd
= open(elf_fname
, O_RDONLY
)) == -1) {
231 * Attempt to open an Elf descriptor Read/Write
234 if ((elf
= elf_begin(fd
, ELF_C_READ
, 0)) == NULL
) {
235 (void) mutex_lock(&printlock
);
236 (void) fprintf(stderr
, "elf_begin() failed: %s\n",
238 (void) mutex_unlock(&printlock
);
242 pep
= malloc(sizeof (pe_args
));
244 pep
->pe_file
= elf_fname
;
247 if ((rc
= thr_create(NULL
, 0, (void *(*)(void *))process_elf
,
248 (void *)pep
, THR_DETACHED
, 0)) != 0) {
249 (void) mutex_lock(&printlock
);
250 (void) fprintf(stderr
,
251 "thr_create() failed with code: %d\n", rc
);
252 (void) mutex_unlock(&printlock
);