aboutsummaryrefslogtreecommitdiff
path: root/usr.sbin
diff options
context:
space:
mode:
authorPedro F. Giffuni <pfg@FreeBSD.org>2013-02-03 21:16:33 +0000
committerPedro F. Giffuni <pfg@FreeBSD.org>2013-02-03 21:16:33 +0000
commite8ee1a1ea5acd0d028bee431e229de0dca7955c6 (patch)
tree89a18c612500a50c04ca593a5b8493c39983cc81 /usr.sbin
parentccf9b6362b093b651693488103e6c167009be0e1 (diff)
downloadsrc-e8ee1a1ea5acd0d028bee431e229de0dca7955c6.tar.gz
src-e8ee1a1ea5acd0d028bee431e229de0dca7955c6.zip
crunchide(1): Put e_shnum into a local variable.
This simplifies the code a bit. Submitted by: Cristoph Mallon MFC after: 2 weeks
Notes
Notes: svn path=/head/; revision=246298
Diffstat (limited to 'usr.sbin')
-rw-r--r--usr.sbin/crunch/crunchide/exec_elf32.c23
1 files changed, 13 insertions, 10 deletions
diff --git a/usr.sbin/crunch/crunchide/exec_elf32.c b/usr.sbin/crunch/crunchide/exec_elf32.c
index 59ef41f37d30..752007f45f26 100644
--- a/usr.sbin/crunch/crunchide/exec_elf32.c
+++ b/usr.sbin/crunch/crunchide/exec_elf32.c
@@ -248,14 +248,16 @@ ELFNAMEEND(hide)(int fd, const char *fn)
unsigned char data;
const char *weirdreason = NULL;
void *buf;
+ Elf_Half shnum;
rv = 0;
if (xreadatoff(fd, &ehdr, 0, sizeof ehdr, fn) != sizeof ehdr)
goto bad;
data = ehdr.e_ident[EI_DATA];
+ shnum = xe16toh(ehdr.e_shnum);
- shdrsize = xe16toh(ehdr.e_shnum) * xe16toh(ehdr.e_shentsize);
+ shdrsize = shnum * xe16toh(ehdr.e_shentsize);
if ((shdrp = xmalloc(shdrsize, fn, "section header table")) == NULL)
goto bad;
if (xreadatoff(fd, shdrp, xewtoh(ehdr.e_shoff), shdrsize, fn) !=
@@ -264,7 +266,7 @@ ELFNAMEEND(hide)(int fd, const char *fn)
symtabshdr = strtabshdr = shstrtabshdr = NULL;
weird = 0;
- for (i = 0; i < xe16toh(ehdr.e_shnum); i++) {
+ for (i = 0; i < shnum; i++) {
switch (xe32toh(shdrp[i].sh_type)) {
case SHT_SYMTAB:
if (symtabshdr != NULL) {
@@ -300,7 +302,7 @@ ELFNAMEEND(hide)(int fd, const char *fn)
/*
* sort section layout table by offset
*/
- layoutp = xmalloc(sizeof(struct shlayout) * (xe16toh(ehdr.e_shnum) + 1),
+ layoutp = xmalloc((shnum + 1) * sizeof(struct shlayout),
fn, "layout table");
if (layoutp == NULL)
goto bad;
@@ -309,12 +311,12 @@ ELFNAMEEND(hide)(int fd, const char *fn)
shdrshdr.sh_offset = ehdr.e_shoff;
shdrshdr.sh_size = htoxew(shdrsize);
shdrshdr.sh_addralign = htoxew(ELFSIZE / 8);
- layoutp[xe16toh(ehdr.e_shnum)].shdr = &shdrshdr;
+ layoutp[shnum].shdr = &shdrshdr;
/* insert and sort normal section headers */
- for (i = xe16toh(ehdr.e_shnum) - 1; i >= 0; i--) {
+ for (i = shnum; i-- != 0;) {
l = i + 1;
- r = xe16toh(ehdr.e_shnum);
+ r = shnum;
while (l <= r) {
m = ( l + r) / 2;
if (xewtoh(shdrp[i].sh_offset) >
@@ -332,6 +334,7 @@ ELFNAMEEND(hide)(int fd, const char *fn)
layoutp[r].shdr = &shdrp[i];
layoutp[r].bufp = NULL;
}
+ ++shnum;
/*
* load up everything we need
@@ -347,7 +350,7 @@ ELFNAMEEND(hide)(int fd, const char *fn)
/* we need symtab, strtab, and everything behind strtab */
strtabidx = INT_MAX;
- for (i = 0; i < xe16toh(ehdr.e_shnum) + 1; i++) {
+ for (i = 0; i < shnum; i++) {
if (layoutp[i].shdr == &shdrshdr) {
/* not load section header again */
layoutp[i].bufp = shdrp;
@@ -430,7 +433,7 @@ ELFNAMEEND(hide)(int fd, const char *fn)
/*
* update section header table in ascending order of offset
*/
- for (i = strtabidx + 1; i < xe16toh(ehdr.e_shnum) + 1; i++) {
+ for (i = strtabidx + 1; i < shnum; i++) {
Elf_Off off, align;
off = xewtoh(layoutp[i - 1].shdr->sh_offset) +
xewtoh(layoutp[i - 1].shdr->sh_size);
@@ -442,7 +445,7 @@ ELFNAMEEND(hide)(int fd, const char *fn)
/*
* write data to the file in descending order of offset
*/
- for (i = xe16toh(ehdr.e_shnum); i >= 0; i--) {
+ for (i = shnum; i-- != 0;) {
if (layoutp[i].shdr == strtabshdr) {
/* new string table */
buf = nstrtabp;
@@ -477,7 +480,7 @@ ELFNAMEEND(hide)(int fd, const char *fn)
out:
if (layoutp != NULL) {
- for (i = 0; i < xe16toh(ehdr.e_shnum) + 1; i++) {
+ for (i = 0; i < shnum; i++) {
if (layoutp[i].bufp != NULL)
free(layoutp[i].bufp);
}