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) 1989, 2010, Oracle and/or its affiliates. All rights reserved. 237c478bd9Sstevel@tonic-gate */ 247c478bd9Sstevel@tonic-gate 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate /* 277c478bd9Sstevel@tonic-gate * dcom: Delete Comment 287c478bd9Sstevel@tonic-gate * 297c478bd9Sstevel@tonic-gate * This program demonstrates the use of libelf interface to 307c478bd9Sstevel@tonic-gate * copy the contents of one ELF file to create a new one. 317c478bd9Sstevel@tonic-gate * dcom creates a new ELF file using elf_begin(ELF_C_WRITE). 327c478bd9Sstevel@tonic-gate * 337c478bd9Sstevel@tonic-gate * In order to delete a section from an ELF file you must 347c478bd9Sstevel@tonic-gate * instead create a new ELF file and copy all but the 'selected' 357c478bd9Sstevel@tonic-gate * sections to the new ELF file. This is because libelf is 367c478bd9Sstevel@tonic-gate * unable to delete any sections from an ELF file, it can 377c478bd9Sstevel@tonic-gate * only add them. 387c478bd9Sstevel@tonic-gate * 397c478bd9Sstevel@tonic-gate * NOTE: While this program works fine for simple ELF objects, 407c478bd9Sstevel@tonic-gate * as they get more complex it may not properly update all of the 417c478bd9Sstevel@tonic-gate * fields required. This program is *only* an example of how 427c478bd9Sstevel@tonic-gate * to do this and not a complete program in itself. 437c478bd9Sstevel@tonic-gate */ 447c478bd9Sstevel@tonic-gate #include <stdio.h> 457c478bd9Sstevel@tonic-gate #include <libelf.h> 467c478bd9Sstevel@tonic-gate #include <gelf.h> 477c478bd9Sstevel@tonic-gate #include <fcntl.h> 487c478bd9Sstevel@tonic-gate #include <string.h> 497c478bd9Sstevel@tonic-gate #include <stdlib.h> 507c478bd9Sstevel@tonic-gate #include <unistd.h> 517c478bd9Sstevel@tonic-gate #include <sys/types.h> 527c478bd9Sstevel@tonic-gate #include <sys/stat.h> 537c478bd9Sstevel@tonic-gate #include <sys/param.h> 547c478bd9Sstevel@tonic-gate 557c478bd9Sstevel@tonic-gate 567c478bd9Sstevel@tonic-gate static const char *CommentStr = ".comment"; 577c478bd9Sstevel@tonic-gate 587c478bd9Sstevel@tonic-gate /* 597c478bd9Sstevel@tonic-gate * Build a temporary file name that is in the 607c478bd9Sstevel@tonic-gate * same directory as the elf file being processed. 617c478bd9Sstevel@tonic-gate */ 627c478bd9Sstevel@tonic-gate static char * 637c478bd9Sstevel@tonic-gate mkname(const char *bname) 647c478bd9Sstevel@tonic-gate { 657c478bd9Sstevel@tonic-gate char *ptr; 667c478bd9Sstevel@tonic-gate char buffer[MAXPATHLEN]; 677c478bd9Sstevel@tonic-gate 687c478bd9Sstevel@tonic-gate ptr = strcpy(buffer, bname); 697c478bd9Sstevel@tonic-gate ptr += strlen(buffer); 707c478bd9Sstevel@tonic-gate while (ptr >= buffer) { 717c478bd9Sstevel@tonic-gate if (*ptr == '/') { 727c478bd9Sstevel@tonic-gate *(ptr + 1) = '\0'; 737c478bd9Sstevel@tonic-gate break; 747c478bd9Sstevel@tonic-gate } 757c478bd9Sstevel@tonic-gate ptr--; 767c478bd9Sstevel@tonic-gate } 777c478bd9Sstevel@tonic-gate if (ptr < buffer) { 787c478bd9Sstevel@tonic-gate buffer[0] = '.'; 797c478bd9Sstevel@tonic-gate buffer[1] = '\0'; 807c478bd9Sstevel@tonic-gate } 817c478bd9Sstevel@tonic-gate return (tempnam(buffer, 0)); 827c478bd9Sstevel@tonic-gate } 837c478bd9Sstevel@tonic-gate 847c478bd9Sstevel@tonic-gate static void 857c478bd9Sstevel@tonic-gate delete_comment(Elf *elf, int fd, const char *file) 867c478bd9Sstevel@tonic-gate { 87*20c1c355SRod Evans Elf_Scn *scn = NULL; 887c478bd9Sstevel@tonic-gate char *tfile; 897c478bd9Sstevel@tonic-gate Elf *telf; 90*20c1c355SRod Evans GElf_Ehdr ehdr, tehdr; 91*20c1c355SRod Evans GElf_Phdr phdr, tphdr; 92*20c1c355SRod Evans size_t shstrndx, shnum, phnum; 93*20c1c355SRod Evans int tfd, *shndx, ndx = 1, off = 0; 947c478bd9Sstevel@tonic-gate struct stat sbuf; 957c478bd9Sstevel@tonic-gate 96*20c1c355SRod Evans if (gelf_getehdr(elf, &ehdr) == NULL) { 977c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: elf_getehdr() failed: %s\n", 987c478bd9Sstevel@tonic-gate file, elf_errmsg(0)); 997c478bd9Sstevel@tonic-gate return; 1007c478bd9Sstevel@tonic-gate } 1017c478bd9Sstevel@tonic-gate 10262b628a6SAli Bahrami if (elf_getshdrnum(elf, &shnum) == -1) { 10362b628a6SAli Bahrami (void) fprintf(stderr, "%s: elf_getshdrnum() failed: %s\n", 1047c478bd9Sstevel@tonic-gate file, elf_errmsg(0)); 1057c478bd9Sstevel@tonic-gate return; 1067c478bd9Sstevel@tonic-gate } 1077c478bd9Sstevel@tonic-gate 10862b628a6SAli Bahrami if (elf_getshdrstrndx(elf, &shstrndx) == -1) { 10962b628a6SAli Bahrami (void) fprintf(stderr, "%s: elf_getshdrstrndx() failed: %s\n", 1107c478bd9Sstevel@tonic-gate file, elf_errmsg(0)); 1117c478bd9Sstevel@tonic-gate return; 1127c478bd9Sstevel@tonic-gate } 1137c478bd9Sstevel@tonic-gate 11462b628a6SAli Bahrami if (elf_getphdrnum(elf, &phnum) == -1) { 11562b628a6SAli Bahrami (void) fprintf(stderr, "%s: elf_getphdrnum() failed: %s\n", 11630da1432Sahl file, elf_errmsg(0)); 11730da1432Sahl return; 11830da1432Sahl } 11930da1432Sahl 1207c478bd9Sstevel@tonic-gate /* 1217c478bd9Sstevel@tonic-gate * shndx is an array used to map the current section 1227c478bd9Sstevel@tonic-gate * indexes to the new section indexes. 1237c478bd9Sstevel@tonic-gate */ 1247c478bd9Sstevel@tonic-gate shndx = calloc(shnum, sizeof (int)); 1257c478bd9Sstevel@tonic-gate 126*20c1c355SRod Evans while ((scn = elf_nextscn(elf, scn)) != NULL) { 1277c478bd9Sstevel@tonic-gate GElf_Shdr shdr; 1287c478bd9Sstevel@tonic-gate 1297c478bd9Sstevel@tonic-gate /* 1307c478bd9Sstevel@tonic-gate * Do a string compare to examine each section header 1317c478bd9Sstevel@tonic-gate * to see if it is a ".comment" section. If it is then 1327c478bd9Sstevel@tonic-gate * this is the section we want to process. 1337c478bd9Sstevel@tonic-gate */ 134*20c1c355SRod Evans if (gelf_getshdr(scn, &shdr) == NULL) { 13562b628a6SAli Bahrami (void) fprintf(stderr, "%s: elf_getshdr() failed: %s\n", 1367c478bd9Sstevel@tonic-gate file, elf_errmsg(0)); 1377c478bd9Sstevel@tonic-gate free(shndx); 1387c478bd9Sstevel@tonic-gate return; 1397c478bd9Sstevel@tonic-gate } 1407c478bd9Sstevel@tonic-gate if (strcmp(CommentStr, elf_strptr(elf, shstrndx, 1417c478bd9Sstevel@tonic-gate shdr.sh_name)) == 0) { 1427c478bd9Sstevel@tonic-gate shndx[ndx] = -1; 1437c478bd9Sstevel@tonic-gate off++; 1447c478bd9Sstevel@tonic-gate 1457c478bd9Sstevel@tonic-gate /* 1467c478bd9Sstevel@tonic-gate * If the .comment section is part of a loadable 1477c478bd9Sstevel@tonic-gate * segment then it can not be delted from the 1487c478bd9Sstevel@tonic-gate * ELF file. 1497c478bd9Sstevel@tonic-gate */ 1507c478bd9Sstevel@tonic-gate if (shdr.sh_addr != 0) { 1517c478bd9Sstevel@tonic-gate (void) printf("%s: .comment section is " 1527c478bd9Sstevel@tonic-gate "part of a loadable segment, it " 1537c478bd9Sstevel@tonic-gate "cannot be deleted.\n", file); 1547c478bd9Sstevel@tonic-gate free(shndx); 1557c478bd9Sstevel@tonic-gate return; 1567c478bd9Sstevel@tonic-gate } 1577c478bd9Sstevel@tonic-gate } else 1587c478bd9Sstevel@tonic-gate shndx[ndx] = ndx - off; 1597c478bd9Sstevel@tonic-gate ndx++; 1607c478bd9Sstevel@tonic-gate } 1617c478bd9Sstevel@tonic-gate 1627c478bd9Sstevel@tonic-gate /* 1637c478bd9Sstevel@tonic-gate * obtain a unique file name and open a file descriptor 1647c478bd9Sstevel@tonic-gate * pointing to that file. 1657c478bd9Sstevel@tonic-gate */ 1667c478bd9Sstevel@tonic-gate tfile = mkname(file); 1677c478bd9Sstevel@tonic-gate if ((tfd = open(tfile, O_RDWR | O_CREAT, 0600)) == -1) { 1687c478bd9Sstevel@tonic-gate perror("temp open"); 1697c478bd9Sstevel@tonic-gate return; 1707c478bd9Sstevel@tonic-gate } 1717c478bd9Sstevel@tonic-gate 1727c478bd9Sstevel@tonic-gate /* 1737c478bd9Sstevel@tonic-gate * Create a new ELF to duplicate the ELF file into. 1747c478bd9Sstevel@tonic-gate */ 175*20c1c355SRod Evans if ((telf = elf_begin(tfd, ELF_C_WRITE, 0)) == NULL) { 1767c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "elf_begin(ELF_C_WRITE) failed: %s\n", 1777c478bd9Sstevel@tonic-gate elf_errmsg(0)); 1787c478bd9Sstevel@tonic-gate return; 1797c478bd9Sstevel@tonic-gate } 1807c478bd9Sstevel@tonic-gate 181*20c1c355SRod Evans if (gelf_newehdr(telf, gelf_getclass(elf)) == NULL) { 1827c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: elf_newehdr() failed: %s\n", 1837c478bd9Sstevel@tonic-gate file, elf_errmsg(0)); 1847c478bd9Sstevel@tonic-gate free(shndx); 1857c478bd9Sstevel@tonic-gate return; 1867c478bd9Sstevel@tonic-gate } 187*20c1c355SRod Evans if (gelf_getehdr(telf, &tehdr) == NULL) { 1887c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: elf_getehdr() failed: %s\n", 1897c478bd9Sstevel@tonic-gate file, elf_errmsg(0)); 1907c478bd9Sstevel@tonic-gate free(shndx); 1917c478bd9Sstevel@tonic-gate return; 1927c478bd9Sstevel@tonic-gate } 1937c478bd9Sstevel@tonic-gate 194*20c1c355SRod Evans scn = NULL; 1957c478bd9Sstevel@tonic-gate ndx = 1; 196*20c1c355SRod Evans while ((scn = elf_nextscn(elf, scn)) != NULL) { 1977c478bd9Sstevel@tonic-gate Elf_Scn *tscn; 198*20c1c355SRod Evans Elf_Data *data, *tdata; 199*20c1c355SRod Evans GElf_Shdr shdr, tshdr; 2007c478bd9Sstevel@tonic-gate 2017c478bd9Sstevel@tonic-gate if (shndx[ndx] == -1) { 2027c478bd9Sstevel@tonic-gate ndx++; 2037c478bd9Sstevel@tonic-gate continue; 2047c478bd9Sstevel@tonic-gate } 2057c478bd9Sstevel@tonic-gate 2067c478bd9Sstevel@tonic-gate /* 2077c478bd9Sstevel@tonic-gate * Duplicate all but the .comment section in the 2087c478bd9Sstevel@tonic-gate * new file. 2097c478bd9Sstevel@tonic-gate */ 210*20c1c355SRod Evans if (gelf_getshdr(scn, &shdr) == NULL) { 21162b628a6SAli Bahrami (void) fprintf(stderr, "%s: elf_getshdr() failed: %s\n", 2127c478bd9Sstevel@tonic-gate file, elf_errmsg(0)); 2137c478bd9Sstevel@tonic-gate free(shndx); 2147c478bd9Sstevel@tonic-gate return; 2157c478bd9Sstevel@tonic-gate } 216*20c1c355SRod Evans if ((tscn = elf_newscn(telf)) == NULL) { 21762b628a6SAli Bahrami (void) fprintf(stderr, "%s: elf_newscn() failed: %s\n", 2187c478bd9Sstevel@tonic-gate file, elf_errmsg(0)); 2197c478bd9Sstevel@tonic-gate free(shndx); 2207c478bd9Sstevel@tonic-gate return; 2217c478bd9Sstevel@tonic-gate } 222*20c1c355SRod Evans if (gelf_getshdr(tscn, &tshdr) == NULL) { 22362b628a6SAli Bahrami (void) fprintf(stderr, "%s: elf_getshdr() failed: %s\n", 2247c478bd9Sstevel@tonic-gate file, elf_errmsg(0)); 2257c478bd9Sstevel@tonic-gate free(shndx); 2267c478bd9Sstevel@tonic-gate return; 2277c478bd9Sstevel@tonic-gate } 2287c478bd9Sstevel@tonic-gate tshdr = shdr; 2297c478bd9Sstevel@tonic-gate tshdr.sh_link = shndx[shdr.sh_link]; 2307c478bd9Sstevel@tonic-gate 2317c478bd9Sstevel@tonic-gate /* 2327c478bd9Sstevel@tonic-gate * The relocation sections sh_info field also contains 2337c478bd9Sstevel@tonic-gate * a section index that needs to be adjusted. This is 2347c478bd9Sstevel@tonic-gate * the only section who's sh_info field contains 2357c478bd9Sstevel@tonic-gate * a section index according to the ABI. 2367c478bd9Sstevel@tonic-gate * 2377c478bd9Sstevel@tonic-gate * If their are non-ABI sections who's sh_info field 2387c478bd9Sstevel@tonic-gate * contains section indexes they will not properly 2397c478bd9Sstevel@tonic-gate * be updated by this routine. 2407c478bd9Sstevel@tonic-gate */ 2417c478bd9Sstevel@tonic-gate if (shdr.sh_type == SHT_REL) 2427c478bd9Sstevel@tonic-gate tshdr.sh_info = shndx[ndx]; 2437c478bd9Sstevel@tonic-gate 2447c478bd9Sstevel@tonic-gate /* 2457c478bd9Sstevel@tonic-gate * Flush the changes to the underlying elf32 or elf64 2467c478bd9Sstevel@tonic-gate * section header. 2477c478bd9Sstevel@tonic-gate */ 248*20c1c355SRod Evans (void) gelf_update_shdr(tscn, &tshdr); 2497c478bd9Sstevel@tonic-gate 250*20c1c355SRod Evans if ((data = elf_getdata(scn, 0)) == NULL) { 25162b628a6SAli Bahrami (void) fprintf(stderr, "%s: elf_getdata() failed: %s\n", 2527c478bd9Sstevel@tonic-gate file, elf_errmsg(0)); 2537c478bd9Sstevel@tonic-gate free(shndx); 2547c478bd9Sstevel@tonic-gate return; 2557c478bd9Sstevel@tonic-gate } 256*20c1c355SRod Evans if ((tdata = elf_newdata(tscn)) == NULL) { 25762b628a6SAli Bahrami (void) fprintf(stderr, "%s: elf_newdata() failed: %s\n", 2587c478bd9Sstevel@tonic-gate file, elf_errmsg(0)); 2597c478bd9Sstevel@tonic-gate free(shndx); 2607c478bd9Sstevel@tonic-gate return; 2617c478bd9Sstevel@tonic-gate } 2627c478bd9Sstevel@tonic-gate *tdata = *data; 2637c478bd9Sstevel@tonic-gate ndx++; 2647c478bd9Sstevel@tonic-gate } 2657c478bd9Sstevel@tonic-gate 2667c478bd9Sstevel@tonic-gate tehdr = ehdr; 2677c478bd9Sstevel@tonic-gate if (shndx[shstrndx] < SHN_LORESERVE) 2687c478bd9Sstevel@tonic-gate tehdr.e_shstrndx = shndx[shstrndx]; 2697c478bd9Sstevel@tonic-gate else { 2707c478bd9Sstevel@tonic-gate Elf_Scn *_scn; 2717c478bd9Sstevel@tonic-gate GElf_Shdr shdr0; 272*20c1c355SRod Evans 2737c478bd9Sstevel@tonic-gate /* 2747c478bd9Sstevel@tonic-gate * 'ELF Extended Sections' are enabled - we must 2757c478bd9Sstevel@tonic-gate * store the shstrndx in Shdr[0].sh_link 2767c478bd9Sstevel@tonic-gate */ 277*20c1c355SRod Evans if ((_scn = elf_getscn(telf, 0)) == NULL) { 27862b628a6SAli Bahrami (void) fprintf(stderr, "%s: elf_getscn() failed: %s\n", 2797c478bd9Sstevel@tonic-gate file, elf_errmsg(0)); 2807c478bd9Sstevel@tonic-gate free(shndx); 2817c478bd9Sstevel@tonic-gate return; 2827c478bd9Sstevel@tonic-gate } 283*20c1c355SRod Evans if (gelf_getshdr(_scn, &shdr0) == NULL) { 28462b628a6SAli Bahrami (void) fprintf(stderr, "%s: elf_getshdr() failed: %s\n", 2857c478bd9Sstevel@tonic-gate file, elf_errmsg(0)); 2867c478bd9Sstevel@tonic-gate free(shndx); 2877c478bd9Sstevel@tonic-gate return; 2887c478bd9Sstevel@tonic-gate } 2897c478bd9Sstevel@tonic-gate tehdr.e_shstrndx = SHN_XINDEX; 2907c478bd9Sstevel@tonic-gate shdr0.sh_link = shndx[shstrndx]; 291*20c1c355SRod Evans (void) gelf_update_shdr(_scn, &shdr0); 2927c478bd9Sstevel@tonic-gate } 293*20c1c355SRod Evans (void) gelf_update_ehdr(telf, &tehdr); 2947c478bd9Sstevel@tonic-gate 2957c478bd9Sstevel@tonic-gate free(shndx); 2967c478bd9Sstevel@tonic-gate 2977c478bd9Sstevel@tonic-gate /* 2987c478bd9Sstevel@tonic-gate * Duplicate all program headers contained in the ELF file. 2997c478bd9Sstevel@tonic-gate */ 30030da1432Sahl if (phnum != 0) { 301*20c1c355SRod Evans if (gelf_newphdr(telf, phnum) == NULL) { 30262b628a6SAli Bahrami (void) fprintf(stderr, "%s: elf_newphdr() failed: %s\n", 3037c478bd9Sstevel@tonic-gate file, elf_errmsg(0)); 3047c478bd9Sstevel@tonic-gate return; 3057c478bd9Sstevel@tonic-gate } 30630da1432Sahl for (ndx = 0; ndx < (int)phnum; ndx++) { 307*20c1c355SRod Evans if (gelf_getphdr(elf, ndx, &phdr) == NULL || 308*20c1c355SRod Evans gelf_getphdr(telf, ndx, &tphdr) == NULL) { 3097c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 3107c478bd9Sstevel@tonic-gate "%s: elf_getphdr() failed: %s\n", 3117c478bd9Sstevel@tonic-gate file, elf_errmsg(0)); 3127c478bd9Sstevel@tonic-gate return; 3137c478bd9Sstevel@tonic-gate } 3147c478bd9Sstevel@tonic-gate tphdr = phdr; 315*20c1c355SRod Evans (void) gelf_update_phdr(telf, ndx, &tphdr); 3167c478bd9Sstevel@tonic-gate } 3177c478bd9Sstevel@tonic-gate } 3187c478bd9Sstevel@tonic-gate 3197c478bd9Sstevel@tonic-gate /* 3207c478bd9Sstevel@tonic-gate * The new Elf file has now been fully described to libelf. 3217c478bd9Sstevel@tonic-gate * elf_update() will construct the new Elf file and write 3227c478bd9Sstevel@tonic-gate * it out to disk. 3237c478bd9Sstevel@tonic-gate */ 3247c478bd9Sstevel@tonic-gate if (elf_update(telf, ELF_C_WRITE) == -1) { 3257c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "elf_update() failed: %s\n", 3267c478bd9Sstevel@tonic-gate elf_errmsg(0)); 3277c478bd9Sstevel@tonic-gate (void) elf_end(telf); 3287c478bd9Sstevel@tonic-gate (void) close(tfd); 3297c478bd9Sstevel@tonic-gate return; 3307c478bd9Sstevel@tonic-gate } 3317c478bd9Sstevel@tonic-gate (void) elf_end(telf); 3327c478bd9Sstevel@tonic-gate 3337c478bd9Sstevel@tonic-gate /* 3347c478bd9Sstevel@tonic-gate * set new files permissions to the original files 3357c478bd9Sstevel@tonic-gate * permissions. 3367c478bd9Sstevel@tonic-gate */ 3377c478bd9Sstevel@tonic-gate (void) fstat(fd, &sbuf); 3387c478bd9Sstevel@tonic-gate (void) fchmod(tfd, sbuf.st_mode); 3397c478bd9Sstevel@tonic-gate 3407c478bd9Sstevel@tonic-gate (void) close(tfd); 3417c478bd9Sstevel@tonic-gate 3427c478bd9Sstevel@tonic-gate /* 3437c478bd9Sstevel@tonic-gate * delete the original file and rename the new file 3447c478bd9Sstevel@tonic-gate * to the orignal file. 3457c478bd9Sstevel@tonic-gate */ 3467c478bd9Sstevel@tonic-gate (void) rename(tfile, file); 3477c478bd9Sstevel@tonic-gate } 3487c478bd9Sstevel@tonic-gate 3497c478bd9Sstevel@tonic-gate int 3507c478bd9Sstevel@tonic-gate main(int argc, char ** argv) 3517c478bd9Sstevel@tonic-gate { 3527c478bd9Sstevel@tonic-gate int i; 3537c478bd9Sstevel@tonic-gate 3547c478bd9Sstevel@tonic-gate if (argc < 2) { 3557c478bd9Sstevel@tonic-gate (void) printf("usage: %s elf_file ...\n", argv[0]); 3567c478bd9Sstevel@tonic-gate return (1); 3577c478bd9Sstevel@tonic-gate } 3587c478bd9Sstevel@tonic-gate 3597c478bd9Sstevel@tonic-gate /* 3607c478bd9Sstevel@tonic-gate * Initialize the elf library, must be called before elf_begin() 3617c478bd9Sstevel@tonic-gate * can be called. 3627c478bd9Sstevel@tonic-gate */ 3637c478bd9Sstevel@tonic-gate if (elf_version(EV_CURRENT) == EV_NONE) { 3647c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "elf_version() failed: %s\n", 3657c478bd9Sstevel@tonic-gate elf_errmsg(0)); 3667c478bd9Sstevel@tonic-gate return (1); 3677c478bd9Sstevel@tonic-gate } 3687c478bd9Sstevel@tonic-gate 3697c478bd9Sstevel@tonic-gate for (i = 1; i < argc; i++) { 3707c478bd9Sstevel@tonic-gate int fd; 3717c478bd9Sstevel@tonic-gate Elf *elf; 3727c478bd9Sstevel@tonic-gate char *elf_fname; 3737c478bd9Sstevel@tonic-gate 3747c478bd9Sstevel@tonic-gate elf_fname = argv[i]; 3757c478bd9Sstevel@tonic-gate 3767c478bd9Sstevel@tonic-gate if ((fd = open(elf_fname, O_RDONLY)) == -1) { 3777c478bd9Sstevel@tonic-gate perror("open"); 3787c478bd9Sstevel@tonic-gate continue; 3797c478bd9Sstevel@tonic-gate } 3807c478bd9Sstevel@tonic-gate 3817c478bd9Sstevel@tonic-gate /* 3827c478bd9Sstevel@tonic-gate * Attempt to open an Elf descriptor Read/Write 3837c478bd9Sstevel@tonic-gate * for each file. 3847c478bd9Sstevel@tonic-gate */ 3857c478bd9Sstevel@tonic-gate if ((elf = elf_begin(fd, ELF_C_READ, 0)) == NULL) { 3867c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "elf_begin() failed: %s\n", 3877c478bd9Sstevel@tonic-gate elf_errmsg(0)); 3887c478bd9Sstevel@tonic-gate (void) close(fd); 3897c478bd9Sstevel@tonic-gate continue; 3907c478bd9Sstevel@tonic-gate } 3917c478bd9Sstevel@tonic-gate 3927c478bd9Sstevel@tonic-gate /* 3937c478bd9Sstevel@tonic-gate * Determine what kind of elf file this is: 3947c478bd9Sstevel@tonic-gate */ 3957c478bd9Sstevel@tonic-gate if (elf_kind(elf) != ELF_K_ELF) { 3967c478bd9Sstevel@tonic-gate /* 3977c478bd9Sstevel@tonic-gate * can only delete comment sections from 3987c478bd9Sstevel@tonic-gate * ELF files. 3997c478bd9Sstevel@tonic-gate */ 4007c478bd9Sstevel@tonic-gate (void) printf("%s not of type ELF_K_ELF. " 40162b628a6SAli Bahrami "elf_kind == %d\n", elf_fname, elf_kind(elf)); 4027c478bd9Sstevel@tonic-gate } else 4037c478bd9Sstevel@tonic-gate delete_comment(elf, fd, elf_fname); 4047c478bd9Sstevel@tonic-gate 4057c478bd9Sstevel@tonic-gate (void) elf_end(elf); 4067c478bd9Sstevel@tonic-gate (void) close(fd); 4077c478bd9Sstevel@tonic-gate } 4087c478bd9Sstevel@tonic-gate 4097c478bd9Sstevel@tonic-gate return (0); 4107c478bd9Sstevel@tonic-gate } 411