Date: Sun, 3 Feb 2013 21:16:34 +0000 (UTC) From: "Pedro F. Giffuni" <pfg@FreeBSD.org> To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r246298 - head/usr.sbin/crunch/crunchide Message-ID: <201302032116.r13LGYKQ044901@svn.freebsd.org>
next in thread | raw e-mail | index | archive | help
Author: pfg Date: Sun Feb 3 21:16:33 2013 New Revision: 246298 URL: http://svnweb.freebsd.org/changeset/base/246298 Log: crunchide(1): Put e_shnum into a local variable. This simplifies the code a bit. Submitted by: Cristoph Mallon MFC after: 2 weeks Modified: head/usr.sbin/crunch/crunchide/exec_elf32.c Modified: head/usr.sbin/crunch/crunchide/exec_elf32.c ============================================================================== --- head/usr.sbin/crunch/crunchide/exec_elf32.c Sun Feb 3 20:40:41 2013 (r246297) +++ head/usr.sbin/crunch/crunchide/exec_elf32.c Sun Feb 3 21:16:33 2013 (r246298) @@ -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); }
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201302032116.r13LGYKQ044901>