pcom.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2004 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* pcom: Print Comment
*
* This program demonstrates the use of the libelf interface to
* read an ELF file. pcom will open an ELF file using
* elf_begin(ELF_C_READ) and examine search the ELF file
* for a .comment section. If a .comment section is found it's
* contents will be displayed on stdout.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <stdio.h>
#include <libelf.h>
#include <gelf.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
static const char *CommentStr = ".comment";
static void
{
file, elf_errmsg(0));
return;
}
/*
* Do a string compare to examine each section header
* to see if it is a ".comment" section. If it is then
* this is the section we want to process.
*/
"%s: elf_getshdr() failed: %s\n",
file, elf_errmsg(0));
return;
}
int i;
char *ptr;
/*
* Get the data associated with the .comment
* section.
*/
"%s: elf_getdata() failed: %s\n",
file, elf_errmsg(0));
return;
}
/*
* Data in a .comment section is a list of 'null'
* terminated strings. The following will print
* one string per line.
*/
if (ptr[i]) {
}
(void) putchar('\n');
}
}
}
static void
{
case ELF_K_ELF:
/*
* This is an ELF file, now attempt to find it's
* .comment section and to display it.
*/
break;
case ELF_K_AR:
/*
* Archives contain multiple ELF files, which can each
* in turn be examined with libelf.
*
* The below loop will iterate over each member of the
* archive and recursivly call process_elf() for processing.
*/
cmd = ELF_C_READ;
char buffer[1024];
/*
* Build up file names based off of
* 'archivename(membername)'.
*/
/*
* recursivly process the ELF members.
*/
}
break;
default:
if (!member)
"%s: unexpected elf_kind(): 0x%x\n",
return;
}
}
int
{
int i;
if (argc < 2) {
return (1);
}
/*
* Initialize the elf library, must be called before elf_begin()
* can be called.
*/
"elf_version() failed: %s\n", elf_errmsg(0));
return (1);
}
for (i = 1; i < argc; i++) {
int fd;
char *elf_fname;
perror("open");
continue;
}
/*
* for each file.
*/
elf_errmsg(0));
continue;
}
/*
* Process each elf descriptor.
*/
}
return (0);
}