17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 562b628a6SAli Bahrami * Common Development and Distribution License (the "License"). 662b628a6SAli Bahrami * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 22*20c1c355SRod Evans * Copyright (c) 1995, 2010, Oracle and/or its affiliates. All rights reserved. 237c478bd9Sstevel@tonic-gate */ 247c478bd9Sstevel@tonic-gate 257c478bd9Sstevel@tonic-gate /* 267c478bd9Sstevel@tonic-gate * tpcom: Threaded Print Comment 277c478bd9Sstevel@tonic-gate * 287c478bd9Sstevel@tonic-gate * tpcom is a threaded version of the pcom program. It will create 297c478bd9Sstevel@tonic-gate * a new thread for each new ELF descriptor that it examines. It 307c478bd9Sstevel@tonic-gate * will then examine each elf descriptor and print the .comment section 317c478bd9Sstevel@tonic-gate * if found. 327c478bd9Sstevel@tonic-gate * 337c478bd9Sstevel@tonic-gate * This program demonstrates that libelf is MT-Safe and the usage 347c478bd9Sstevel@tonic-gate * of elf_begin(ELF_C_READ). 357c478bd9Sstevel@tonic-gate */ 367c478bd9Sstevel@tonic-gate 377c478bd9Sstevel@tonic-gate #include <stdio.h> 387c478bd9Sstevel@tonic-gate #include <libelf.h> 397c478bd9Sstevel@tonic-gate #include <gelf.h> 407c478bd9Sstevel@tonic-gate #include <fcntl.h> 417c478bd9Sstevel@tonic-gate #include <unistd.h> 427c478bd9Sstevel@tonic-gate #include <stdlib.h> 437c478bd9Sstevel@tonic-gate #include <string.h> 447c478bd9Sstevel@tonic-gate #include <thread.h> 457c478bd9Sstevel@tonic-gate 467c478bd9Sstevel@tonic-gate 477c478bd9Sstevel@tonic-gate #define NUMLWPS 32 /* arbitrary number of LWPS */ 487c478bd9Sstevel@tonic-gate 497c478bd9Sstevel@tonic-gate static const char *CommentStr = ".comment"; 507c478bd9Sstevel@tonic-gate 517c478bd9Sstevel@tonic-gate /* 527c478bd9Sstevel@tonic-gate * arguments to be passed into process_elf(). 537c478bd9Sstevel@tonic-gate */ 547c478bd9Sstevel@tonic-gate typedef struct { 557c478bd9Sstevel@tonic-gate Elf *pe_elf; 567c478bd9Sstevel@tonic-gate char *pe_file; /* elf member name */ 577c478bd9Sstevel@tonic-gate int pe_fd; 587c478bd9Sstevel@tonic-gate short pe_member; /* is this an archive member? */ 597c478bd9Sstevel@tonic-gate } pe_args; 607c478bd9Sstevel@tonic-gate 617c478bd9Sstevel@tonic-gate 627c478bd9Sstevel@tonic-gate static mutex_t printlock = DEFAULTMUTEX; /* printlock used to */ 637c478bd9Sstevel@tonic-gate /* group output */ 647c478bd9Sstevel@tonic-gate /* of comment sections */ 657c478bd9Sstevel@tonic-gate 667c478bd9Sstevel@tonic-gate static void 677c478bd9Sstevel@tonic-gate print_comment(Elf *elf, const char *file) 687c478bd9Sstevel@tonic-gate { 69*20c1c355SRod Evans Elf_Scn *scn = NULL; 707c478bd9Sstevel@tonic-gate GElf_Shdr shdr; 717c478bd9Sstevel@tonic-gate Elf_Data *data; 727c478bd9Sstevel@tonic-gate size_t shstrndx; 737c478bd9Sstevel@tonic-gate 7462b628a6SAli Bahrami if (elf_getshdrstrndx(elf, &shstrndx) == -1) { 7562b628a6SAli Bahrami (void) fprintf(stderr, "%s: elf_getshdrstrndx() failed: %s\n", 767c478bd9Sstevel@tonic-gate file, elf_errmsg(0)); 777c478bd9Sstevel@tonic-gate return; 787c478bd9Sstevel@tonic-gate } 79*20c1c355SRod Evans 80*20c1c355SRod Evans while ((scn = elf_nextscn(elf, scn)) != NULL) { 817c478bd9Sstevel@tonic-gate /* 827c478bd9Sstevel@tonic-gate * Do a string compare to examine each section header 837c478bd9Sstevel@tonic-gate * to see if it is a ".comment" section. If it is then 847c478bd9Sstevel@tonic-gate * this is the section we want to process. 857c478bd9Sstevel@tonic-gate */ 86*20c1c355SRod Evans if (gelf_getshdr(scn, &shdr) == NULL) { 8762b628a6SAli Bahrami (void) fprintf(stderr, "%s: elf_getshdr() failed: %s\n", 887c478bd9Sstevel@tonic-gate file, elf_errmsg(0)); 897c478bd9Sstevel@tonic-gate return; 907c478bd9Sstevel@tonic-gate } 917c478bd9Sstevel@tonic-gate 927c478bd9Sstevel@tonic-gate if (strcmp(CommentStr, elf_strptr(elf, shstrndx, 937c478bd9Sstevel@tonic-gate shdr.sh_name)) == 0) { 947c478bd9Sstevel@tonic-gate int i; 957c478bd9Sstevel@tonic-gate char *ptr; 967c478bd9Sstevel@tonic-gate 97*20c1c355SRod Evans (void) mutex_lock(&printlock); 987c478bd9Sstevel@tonic-gate (void) printf("%s .comment:\n", file); 997c478bd9Sstevel@tonic-gate 1007c478bd9Sstevel@tonic-gate /* 1017c478bd9Sstevel@tonic-gate * Get the data associated with the .comment 1027c478bd9Sstevel@tonic-gate * section. 1037c478bd9Sstevel@tonic-gate */ 104*20c1c355SRod Evans if ((data = elf_getdata(scn, NULL)) == NULL) { 1057c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 1067c478bd9Sstevel@tonic-gate "%s: elf_getdata() failed: %s\n", 1077c478bd9Sstevel@tonic-gate file, elf_errmsg(0)); 108*20c1c355SRod Evans (void) mutex_unlock(&printlock); 1097c478bd9Sstevel@tonic-gate return; 1107c478bd9Sstevel@tonic-gate } 1117c478bd9Sstevel@tonic-gate /* 1127c478bd9Sstevel@tonic-gate * Data in a .comment section is a list of 'null' 1137c478bd9Sstevel@tonic-gate * terminated strings. The following will print 1147c478bd9Sstevel@tonic-gate * one string per line. 1157c478bd9Sstevel@tonic-gate */ 1167c478bd9Sstevel@tonic-gate for (i = 0, ptr = (char *)data->d_buf; 1177c478bd9Sstevel@tonic-gate i < data->d_size; i++) 1187c478bd9Sstevel@tonic-gate if (ptr[i]) { 1197c478bd9Sstevel@tonic-gate (void) puts(&ptr[i]); 1207c478bd9Sstevel@tonic-gate i += strlen(&ptr[i]); 1217c478bd9Sstevel@tonic-gate } 1227c478bd9Sstevel@tonic-gate (void) putchar('\n'); 123*20c1c355SRod Evans (void) mutex_unlock(&printlock); 1247c478bd9Sstevel@tonic-gate } 1257c478bd9Sstevel@tonic-gate } 1267c478bd9Sstevel@tonic-gate 1277c478bd9Sstevel@tonic-gate } 1287c478bd9Sstevel@tonic-gate 1297c478bd9Sstevel@tonic-gate static void 1307c478bd9Sstevel@tonic-gate process_elf(pe_args *pep) 1317c478bd9Sstevel@tonic-gate { 1327c478bd9Sstevel@tonic-gate Elf_Cmd cmd; 1337c478bd9Sstevel@tonic-gate Elf *_elf; 1347c478bd9Sstevel@tonic-gate 1357c478bd9Sstevel@tonic-gate switch (elf_kind(pep->pe_elf)) { 1367c478bd9Sstevel@tonic-gate case ELF_K_ELF: 1377c478bd9Sstevel@tonic-gate print_comment(pep->pe_elf, pep->pe_file); 1387c478bd9Sstevel@tonic-gate break; 1397c478bd9Sstevel@tonic-gate case ELF_K_AR: 1407c478bd9Sstevel@tonic-gate cmd = ELF_C_READ; 1417c478bd9Sstevel@tonic-gate while ((_elf = elf_begin(pep->pe_fd, cmd, 142*20c1c355SRod Evans pep->pe_elf)) != NULL) { 1437c478bd9Sstevel@tonic-gate Elf_Arhdr *arhdr; 1447c478bd9Sstevel@tonic-gate pe_args *_pep; 1457c478bd9Sstevel@tonic-gate int rc; 1467c478bd9Sstevel@tonic-gate 147*20c1c355SRod Evans if ((arhdr = elf_getarhdr(_elf)) == NULL) { 1487c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 1497c478bd9Sstevel@tonic-gate "%s: elf_getarhdr() failed: %s\n", 1507c478bd9Sstevel@tonic-gate pep->pe_file, elf_errmsg(0)); 1517c478bd9Sstevel@tonic-gate } 1527c478bd9Sstevel@tonic-gate cmd = elf_next(_elf); 1537c478bd9Sstevel@tonic-gate _pep = malloc(sizeof (pe_args)); 1547c478bd9Sstevel@tonic-gate _pep->pe_elf = _elf; 1557c478bd9Sstevel@tonic-gate _pep->pe_file = malloc(strlen(pep->pe_file) + 1567c478bd9Sstevel@tonic-gate strlen(arhdr->ar_name) + 5); 1577c478bd9Sstevel@tonic-gate (void) sprintf(_pep->pe_file, 1587c478bd9Sstevel@tonic-gate "%s(%s)", pep->pe_file, arhdr->ar_name); 1597c478bd9Sstevel@tonic-gate _pep->pe_fd = pep->pe_fd; 1607c478bd9Sstevel@tonic-gate _pep->pe_member = 1; 161*20c1c355SRod Evans 1627c478bd9Sstevel@tonic-gate if ((rc = thr_create(NULL, 0, 1637c478bd9Sstevel@tonic-gate (void *(*)(void *))process_elf, 1647c478bd9Sstevel@tonic-gate (void *)_pep, THR_DETACHED, 0)) != 0) { 1657c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 1667c478bd9Sstevel@tonic-gate "thr_create() failed, rc = %d\n", rc); 1677c478bd9Sstevel@tonic-gate } 1687c478bd9Sstevel@tonic-gate } 1697c478bd9Sstevel@tonic-gate break; 1707c478bd9Sstevel@tonic-gate default: 1717c478bd9Sstevel@tonic-gate if (!pep->pe_member) { 172*20c1c355SRod Evans (void) mutex_lock(&printlock); 1737c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 1747c478bd9Sstevel@tonic-gate "%s: unexpected elf_kind(): 0x%x\n", 1757c478bd9Sstevel@tonic-gate pep->pe_file, elf_kind(pep->pe_elf)); 176*20c1c355SRod Evans (void) mutex_unlock(&printlock); 1777c478bd9Sstevel@tonic-gate } 1787c478bd9Sstevel@tonic-gate } 1797c478bd9Sstevel@tonic-gate 1807c478bd9Sstevel@tonic-gate (void) elf_end(pep->pe_elf); 1817c478bd9Sstevel@tonic-gate if (pep->pe_member) 1827c478bd9Sstevel@tonic-gate free(pep->pe_file); 1837c478bd9Sstevel@tonic-gate free(pep); 1847c478bd9Sstevel@tonic-gate thr_exit(0); 1857c478bd9Sstevel@tonic-gate } 1867c478bd9Sstevel@tonic-gate 1877c478bd9Sstevel@tonic-gate int 1887c478bd9Sstevel@tonic-gate main(int argc, char ** argv) 1897c478bd9Sstevel@tonic-gate { 1907c478bd9Sstevel@tonic-gate int i; 1917c478bd9Sstevel@tonic-gate 1927c478bd9Sstevel@tonic-gate if (argc < 2) { 1937c478bd9Sstevel@tonic-gate (void) printf("usage: %s elf_file ...\n", argv[0]); 1947c478bd9Sstevel@tonic-gate return (1); 1957c478bd9Sstevel@tonic-gate } 1967c478bd9Sstevel@tonic-gate 1977c478bd9Sstevel@tonic-gate /* 1987c478bd9Sstevel@tonic-gate * Initialize the elf library, must be called before elf_begin() 1997c478bd9Sstevel@tonic-gate * can be called. 2007c478bd9Sstevel@tonic-gate */ 2017c478bd9Sstevel@tonic-gate if (elf_version(EV_CURRENT) == EV_NONE) { 2027c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 2037c478bd9Sstevel@tonic-gate "elf_version() failed: %s\n", elf_errmsg(0)); 2047c478bd9Sstevel@tonic-gate return (1); 2057c478bd9Sstevel@tonic-gate } 2067c478bd9Sstevel@tonic-gate 2077c478bd9Sstevel@tonic-gate /* 2087c478bd9Sstevel@tonic-gate * create an arbitrary number of LWP's to run the 2097c478bd9Sstevel@tonic-gate * threads that will be created. 2107c478bd9Sstevel@tonic-gate */ 2117c478bd9Sstevel@tonic-gate if (thr_setconcurrency(NUMLWPS) != 0) { 2127c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "thread setconcurrency failed\n"); 2137c478bd9Sstevel@tonic-gate return (1); 2147c478bd9Sstevel@tonic-gate } 2157c478bd9Sstevel@tonic-gate 2167c478bd9Sstevel@tonic-gate for (i = 1; i < argc; i++) { 2177c478bd9Sstevel@tonic-gate int fd; 2187c478bd9Sstevel@tonic-gate Elf *elf; 2197c478bd9Sstevel@tonic-gate pe_args *pep; 2207c478bd9Sstevel@tonic-gate int rc; 2217c478bd9Sstevel@tonic-gate char *elf_fname; 2227c478bd9Sstevel@tonic-gate 2237c478bd9Sstevel@tonic-gate elf_fname = argv[i]; 2247c478bd9Sstevel@tonic-gate 2257c478bd9Sstevel@tonic-gate if ((fd = open(elf_fname, O_RDONLY)) == -1) { 2267c478bd9Sstevel@tonic-gate perror("open"); 2277c478bd9Sstevel@tonic-gate continue; 2287c478bd9Sstevel@tonic-gate } 2297c478bd9Sstevel@tonic-gate 2307c478bd9Sstevel@tonic-gate /* 2317c478bd9Sstevel@tonic-gate * Attempt to open an Elf descriptor Read/Write 2327c478bd9Sstevel@tonic-gate * for each file. 2337c478bd9Sstevel@tonic-gate */ 2347c478bd9Sstevel@tonic-gate if ((elf = elf_begin(fd, ELF_C_READ, 0)) == NULL) { 235*20c1c355SRod Evans (void) mutex_lock(&printlock); 2367c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "elf_begin() failed: %s\n", 2377c478bd9Sstevel@tonic-gate elf_errmsg(0)); 238*20c1c355SRod Evans (void) mutex_unlock(&printlock); 2397c478bd9Sstevel@tonic-gate (void) close(fd); 2407c478bd9Sstevel@tonic-gate continue; 2417c478bd9Sstevel@tonic-gate } 2427c478bd9Sstevel@tonic-gate pep = malloc(sizeof (pe_args)); 2437c478bd9Sstevel@tonic-gate pep->pe_elf = elf; 2447c478bd9Sstevel@tonic-gate pep->pe_file = elf_fname; 2457c478bd9Sstevel@tonic-gate pep->pe_fd = fd; 2467c478bd9Sstevel@tonic-gate pep->pe_member = 0; 2477c478bd9Sstevel@tonic-gate if ((rc = thr_create(NULL, 0, (void *(*)(void *))process_elf, 2487c478bd9Sstevel@tonic-gate (void *)pep, THR_DETACHED, 0)) != 0) { 249*20c1c355SRod Evans (void) mutex_lock(&printlock); 2507c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 2517c478bd9Sstevel@tonic-gate "thr_create() failed with code: %d\n", rc); 252*20c1c355SRod Evans (void) mutex_unlock(&printlock); 2537c478bd9Sstevel@tonic-gate return (1); 2547c478bd9Sstevel@tonic-gate } 2557c478bd9Sstevel@tonic-gate } 2567c478bd9Sstevel@tonic-gate 2577c478bd9Sstevel@tonic-gate thr_exit(0); 2587c478bd9Sstevel@tonic-gate return (0); 2597c478bd9Sstevel@tonic-gate } 260