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 * pcom: Print Comment
277c478bd9Sstevel@tonic-gate *
287c478bd9Sstevel@tonic-gate * This program demonstrates the use of the libelf interface to
297c478bd9Sstevel@tonic-gate * read an ELF file. pcom will open an ELF file using
307c478bd9Sstevel@tonic-gate * elf_begin(ELF_C_READ) and examine search the ELF file
317c478bd9Sstevel@tonic-gate * for a .comment section. If a .comment section is found it's
327c478bd9Sstevel@tonic-gate * contents will be displayed on stdout.
337c478bd9Sstevel@tonic-gate */
347c478bd9Sstevel@tonic-gate
357c478bd9Sstevel@tonic-gate #include <stdio.h>
367c478bd9Sstevel@tonic-gate #include <libelf.h>
377c478bd9Sstevel@tonic-gate #include <gelf.h>
387c478bd9Sstevel@tonic-gate #include <fcntl.h>
397c478bd9Sstevel@tonic-gate #include <unistd.h>
407c478bd9Sstevel@tonic-gate #include <stdlib.h>
417c478bd9Sstevel@tonic-gate #include <string.h>
427c478bd9Sstevel@tonic-gate
437c478bd9Sstevel@tonic-gate
447c478bd9Sstevel@tonic-gate static const char *CommentStr = ".comment";
457c478bd9Sstevel@tonic-gate
467c478bd9Sstevel@tonic-gate static void
print_comment(Elf * elf,const char * file)477c478bd9Sstevel@tonic-gate print_comment(Elf *elf, const char *file)
487c478bd9Sstevel@tonic-gate {
49*20c1c355SRod Evans Elf_Scn *scn = NULL;
507c478bd9Sstevel@tonic-gate GElf_Shdr shdr;
517c478bd9Sstevel@tonic-gate Elf_Data *data;
527c478bd9Sstevel@tonic-gate size_t shstrndx;
537c478bd9Sstevel@tonic-gate
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gate (void) printf("%s .comment:\n", file);
567c478bd9Sstevel@tonic-gate
5762b628a6SAli Bahrami if (elf_getshdrstrndx(elf, &shstrndx) == -1) {
5862b628a6SAli Bahrami (void) fprintf(stderr, "%s: elf_getshdrstrndx() failed: %s\n",
597c478bd9Sstevel@tonic-gate file, elf_errmsg(0));
607c478bd9Sstevel@tonic-gate return;
617c478bd9Sstevel@tonic-gate }
627c478bd9Sstevel@tonic-gate
63*20c1c355SRod Evans while ((scn = elf_nextscn(elf, scn)) != NULL) {
647c478bd9Sstevel@tonic-gate /*
657c478bd9Sstevel@tonic-gate * Do a string compare to examine each section header
667c478bd9Sstevel@tonic-gate * to see if it is a ".comment" section. If it is then
677c478bd9Sstevel@tonic-gate * this is the section we want to process.
687c478bd9Sstevel@tonic-gate */
69*20c1c355SRod Evans if (gelf_getshdr(scn, &shdr) == NULL) {
7062b628a6SAli Bahrami (void) fprintf(stderr, "%s: elf_getshdr() failed: %s\n",
717c478bd9Sstevel@tonic-gate file, elf_errmsg(0));
727c478bd9Sstevel@tonic-gate return;
737c478bd9Sstevel@tonic-gate }
747c478bd9Sstevel@tonic-gate if (strcmp(CommentStr, elf_strptr(elf, shstrndx,
757c478bd9Sstevel@tonic-gate shdr.sh_name)) == 0) {
767c478bd9Sstevel@tonic-gate int i;
777c478bd9Sstevel@tonic-gate char *ptr;
787c478bd9Sstevel@tonic-gate
797c478bd9Sstevel@tonic-gate /*
807c478bd9Sstevel@tonic-gate * Get the data associated with the .comment
817c478bd9Sstevel@tonic-gate * section.
827c478bd9Sstevel@tonic-gate */
83*20c1c355SRod Evans if ((data = elf_getdata(scn, NULL)) == NULL) {
847c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
857c478bd9Sstevel@tonic-gate "%s: elf_getdata() failed: %s\n",
867c478bd9Sstevel@tonic-gate file, elf_errmsg(0));
877c478bd9Sstevel@tonic-gate return;
887c478bd9Sstevel@tonic-gate }
89*20c1c355SRod Evans
907c478bd9Sstevel@tonic-gate /*
917c478bd9Sstevel@tonic-gate * Data in a .comment section is a list of 'null'
927c478bd9Sstevel@tonic-gate * terminated strings. The following will print
937c478bd9Sstevel@tonic-gate * one string per line.
947c478bd9Sstevel@tonic-gate */
957c478bd9Sstevel@tonic-gate for (i = 0, ptr = (char *)data->d_buf;
967c478bd9Sstevel@tonic-gate i < data->d_size; i++)
977c478bd9Sstevel@tonic-gate if (ptr[i]) {
987c478bd9Sstevel@tonic-gate (void) puts(&ptr[i]);
997c478bd9Sstevel@tonic-gate i += strlen(&ptr[i]);
1007c478bd9Sstevel@tonic-gate }
1017c478bd9Sstevel@tonic-gate (void) putchar('\n');
1027c478bd9Sstevel@tonic-gate }
1037c478bd9Sstevel@tonic-gate }
1047c478bd9Sstevel@tonic-gate
1057c478bd9Sstevel@tonic-gate }
1067c478bd9Sstevel@tonic-gate
1077c478bd9Sstevel@tonic-gate static void
process_elf(Elf * elf,char * file,int fd,int member)1087c478bd9Sstevel@tonic-gate process_elf(Elf *elf, char *file, int fd, int member)
1097c478bd9Sstevel@tonic-gate {
1107c478bd9Sstevel@tonic-gate Elf_Cmd cmd;
1117c478bd9Sstevel@tonic-gate Elf *_elf;
1127c478bd9Sstevel@tonic-gate
1137c478bd9Sstevel@tonic-gate switch (elf_kind(elf)) {
1147c478bd9Sstevel@tonic-gate case ELF_K_ELF:
1157c478bd9Sstevel@tonic-gate /*
1167c478bd9Sstevel@tonic-gate * This is an ELF file, now attempt to find it's
1177c478bd9Sstevel@tonic-gate * .comment section and to display it.
1187c478bd9Sstevel@tonic-gate */
1197c478bd9Sstevel@tonic-gate print_comment(elf, file);
1207c478bd9Sstevel@tonic-gate break;
1217c478bd9Sstevel@tonic-gate case ELF_K_AR:
1227c478bd9Sstevel@tonic-gate /*
1237c478bd9Sstevel@tonic-gate * Archives contain multiple ELF files, which can each
1247c478bd9Sstevel@tonic-gate * in turn be examined with libelf.
1257c478bd9Sstevel@tonic-gate *
1267c478bd9Sstevel@tonic-gate * The below loop will iterate over each member of the
127*20c1c355SRod Evans * archive and recursively call process_elf().
1287c478bd9Sstevel@tonic-gate */
1297c478bd9Sstevel@tonic-gate cmd = ELF_C_READ;
130*20c1c355SRod Evans while ((_elf = elf_begin(fd, cmd, elf)) != NULL) {
1317c478bd9Sstevel@tonic-gate Elf_Arhdr *arhdr;
1327c478bd9Sstevel@tonic-gate char buffer[1024];
1337c478bd9Sstevel@tonic-gate
1347c478bd9Sstevel@tonic-gate arhdr = elf_getarhdr(_elf);
1357c478bd9Sstevel@tonic-gate
1367c478bd9Sstevel@tonic-gate /*
1377c478bd9Sstevel@tonic-gate * Build up file names based off of
1387c478bd9Sstevel@tonic-gate * 'archivename(membername)'.
1397c478bd9Sstevel@tonic-gate */
1407c478bd9Sstevel@tonic-gate (void) sprintf(buffer, "%s(%s)", file, arhdr->ar_name);
1417c478bd9Sstevel@tonic-gate
1427c478bd9Sstevel@tonic-gate /*
143*20c1c355SRod Evans * Recursively process the ELF members.
1447c478bd9Sstevel@tonic-gate */
1457c478bd9Sstevel@tonic-gate process_elf(_elf, buffer, fd, 1);
1467c478bd9Sstevel@tonic-gate cmd = elf_next(_elf);
1477c478bd9Sstevel@tonic-gate (void) elf_end(_elf);
1487c478bd9Sstevel@tonic-gate }
1497c478bd9Sstevel@tonic-gate break;
1507c478bd9Sstevel@tonic-gate default:
1517c478bd9Sstevel@tonic-gate if (!member)
1527c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
1537c478bd9Sstevel@tonic-gate "%s: unexpected elf_kind(): 0x%x\n",
1547c478bd9Sstevel@tonic-gate file, elf_kind(elf));
1557c478bd9Sstevel@tonic-gate return;
1567c478bd9Sstevel@tonic-gate }
1577c478bd9Sstevel@tonic-gate }
1587c478bd9Sstevel@tonic-gate
1597c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)1607c478bd9Sstevel@tonic-gate main(int argc, char **argv)
1617c478bd9Sstevel@tonic-gate {
1627c478bd9Sstevel@tonic-gate int i;
1637c478bd9Sstevel@tonic-gate
1647c478bd9Sstevel@tonic-gate if (argc < 2) {
1657c478bd9Sstevel@tonic-gate (void) printf("usage: %s elf_file ...\n", argv[0]);
1667c478bd9Sstevel@tonic-gate return (1);
1677c478bd9Sstevel@tonic-gate }
1687c478bd9Sstevel@tonic-gate
1697c478bd9Sstevel@tonic-gate /*
1707c478bd9Sstevel@tonic-gate * Initialize the elf library, must be called before elf_begin()
1717c478bd9Sstevel@tonic-gate * can be called.
1727c478bd9Sstevel@tonic-gate */
1737c478bd9Sstevel@tonic-gate if (elf_version(EV_CURRENT) == EV_NONE) {
1747c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
1757c478bd9Sstevel@tonic-gate "elf_version() failed: %s\n", elf_errmsg(0));
1767c478bd9Sstevel@tonic-gate return (1);
1777c478bd9Sstevel@tonic-gate }
1787c478bd9Sstevel@tonic-gate
1797c478bd9Sstevel@tonic-gate for (i = 1; i < argc; i++) {
1807c478bd9Sstevel@tonic-gate int fd;
1817c478bd9Sstevel@tonic-gate Elf *elf;
1827c478bd9Sstevel@tonic-gate char *elf_fname;
1837c478bd9Sstevel@tonic-gate
1847c478bd9Sstevel@tonic-gate elf_fname = argv[i];
1857c478bd9Sstevel@tonic-gate if ((fd = open(elf_fname, O_RDONLY)) == -1) {
1867c478bd9Sstevel@tonic-gate perror("open");
1877c478bd9Sstevel@tonic-gate continue;
1887c478bd9Sstevel@tonic-gate }
1897c478bd9Sstevel@tonic-gate
1907c478bd9Sstevel@tonic-gate /*
1917c478bd9Sstevel@tonic-gate * Attempt to open an Elf descriptor Read/Write
1927c478bd9Sstevel@tonic-gate * for each file.
1937c478bd9Sstevel@tonic-gate */
1947c478bd9Sstevel@tonic-gate if ((elf = elf_begin(fd, ELF_C_READ, 0)) == NULL) {
1957c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "elf_begin() failed: %s\n",
1967c478bd9Sstevel@tonic-gate elf_errmsg(0));
1977c478bd9Sstevel@tonic-gate (void) close(fd);
1987c478bd9Sstevel@tonic-gate continue;
1997c478bd9Sstevel@tonic-gate }
2007c478bd9Sstevel@tonic-gate
2017c478bd9Sstevel@tonic-gate /*
2027c478bd9Sstevel@tonic-gate * Process each elf descriptor.
2037c478bd9Sstevel@tonic-gate */
2047c478bd9Sstevel@tonic-gate process_elf(elf, elf_fname, fd, 0);
2057c478bd9Sstevel@tonic-gate (void) elf_end(elf);
2067c478bd9Sstevel@tonic-gate (void) close(fd);
2077c478bd9Sstevel@tonic-gate }
2087c478bd9Sstevel@tonic-gate
2097c478bd9Sstevel@tonic-gate return (0);
2107c478bd9Sstevel@tonic-gate }
211