dcom.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.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* dcom: Delete Comment
*
* This program demonstrates the use of libelf interface to
* copy the contents of one ELF file to create a new one.
* dcom creates a new ELF file using elf_begin(ELF_C_WRITE).
*
* In order to delete a section from an ELF file you must
* instead create a new ELF file and copy all but the 'selected'
* sections to the new ELF file. This is because libelf is
* unable to delete any sections from an ELF file, it can
* only add them.
*
* NOTE: While this program works fine for simple ELF objects,
* as they get more complex it may not properly update all of the
* fields required. This program is *only* an example of how
* to do this and not a complete program in itself.
*/
#include <stdio.h>
#include <libelf.h>
#include <gelf.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
static const char *CommentStr = ".comment";
/*
* Build a temporary file name that is in the
* same directory as the elf file being processed.
*/
static char *
{
char *ptr;
char buffer[MAXPATHLEN];
if (*ptr == '/') {
break;
}
ptr--;
}
buffer[0] = '.';
}
}
static void
{
char *tfile;
int tfd;
int *shndx;
int ndx = 1;
int off = 0;
file, elf_errmsg(0));
return;
}
file, elf_errmsg(0));
return;
}
file, elf_errmsg(0));
return;
}
/*
* shndx is an array used to map the current section
* indexes to the new section indexes.
*/
/*
* 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;
}
off++;
/*
* If the .comment section is part of a loadable
* segment then it can not be delted from the
* ELF file.
*/
(void) printf("%s: .comment section is "
"part of a loadable segment, it "
"cannot be deleted.\n", file);
return;
}
} else
ndx++;
}
/*
* obtain a unique file name and open a file descriptor
* pointing to that file.
*/
perror("temp open");
return;
}
/*
* Create a new ELF to duplicate the ELF file into.
*/
elf_errmsg(0));
return;
}
file, elf_errmsg(0));
return;
}
file, elf_errmsg(0));
return;
}
scn = 0;
ndx = 1;
ndx++;
continue;
}
/*
* Duplicate all but the .comment section in the
* new file.
*/
"%s: elf_getshdr() failed: %s\n",
file, elf_errmsg(0));
return;
}
"%s: elf_newscn() failed: %s\n",
file, elf_errmsg(0));
return;
}
"%s: elf_getshdr() failed: %s\n",
file, elf_errmsg(0));
return;
}
/*
* The relocation sections sh_info field also contains
* a section index that needs to be adjusted. This is
* the only section who's sh_info field contains
* a section index according to the ABI.
*
* If their are non-ABI sections who's sh_info field
* contains section indexes they will not properly
* be updated by this routine.
*/
/*
* Flush the changes to the underlying elf32 or elf64
* section header.
*/
"%s: elf_getdata() failed: %s\n",
file, elf_errmsg(0));
return;
}
"%s: elf_newdata() failed: %s\n",
file, elf_errmsg(0));
return;
}
ndx++;
}
else {
/*
* 'ELF Extended Sections' are enabled - we must
* store the shstrndx in Shdr[0].sh_link
*/
"%s: elf_getscn() failed: %s\n",
file, elf_errmsg(0));
return;
}
"%s: elf_getshdr() failed: %s\n",
file, elf_errmsg(0));
return;
}
}
/*
* Duplicate all program headers contained in the ELF file.
*/
"%s: elf_newphdr() failed: %s\n",
file, elf_errmsg(0));
return;
}
"%s: elf_getphdr() failed: %s\n",
file, elf_errmsg(0));
return;
}
}
}
/*
* The new Elf file has now been fully described to libelf.
* elf_update() will construct the new Elf file and write
* it out to disk.
*/
elf_errmsg(0));
return;
}
/*
* set new files permissions to the original files
* permissions.
*/
/*
* delete the original file and rename the new file
* to the orignal file.
*/
}
int
{
int i;
if (argc < 2) {
return (1);
}
/*
* Initialize the elf library, must be called before elf_begin()
* can be called.
*/
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;
}
/*
* Determine what kind of elf file this is:
*/
/*
* can only delete comment sections from
* ELF files.
*/
(void) printf("%s not of type ELF_K_ELF. "
"elf_kind == %d\n",
} else
}
return (0);
}