Skip to content

Commit

Permalink
elfcopy: fix PE object section name corruption and crash
Browse files Browse the repository at this point in the history
Fixed a bug that the PE object section names are generated incorrectly
using the section name table found in the original input ELF object
instead of the intermediate ELF object.

Ticket:		freebsd#541

Do not try to copy section content from a NULL d_buf when creating
uninitialized data COFF section for PE object.

Ticket:		freebsd#540

Obtained from:	ELF Tool Chain r3507, r3508
MFC after:	1 week


git-svn-id: svn+ssh://svn.freebsd.org/base/head@310634 ccf9f872-aa2e-dd11-9fc8-001c23d0bc1f
  • Loading branch information
emaste committed Dec 27, 2016
1 parent 11a87d3 commit 72eba96
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions contrib/elftoolchain/elfcopy/pe.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ create_pe(struct elfcopy *ecp, int ifd, int ofd)
errx(EXIT_FAILURE, "gelf_getehdr() failed: %s",
elf_errmsg(-1));

if (elf_getshstrndx(ecp->ein, &indx) == 0)
if (elf_getshstrndx(e, &indx) == 0)
errx(EXIT_FAILURE, "elf_getshstrndx() failed: %s",
elf_errmsg(-1));

Expand Down Expand Up @@ -124,7 +124,7 @@ create_pe(struct elfcopy *ecp, int ifd, int ofd)
(void) elf_errno();
continue;
}
if ((name = elf_strptr(ecp->ein, indx, sh.sh_name)) ==
if ((name = elf_strptr(e, indx, sh.sh_name)) ==
NULL) {
warnx("elf_strptr() failed: %s", elf_errmsg(-1));
(void) elf_errno();
Expand Down Expand Up @@ -210,12 +210,14 @@ create_pe(struct elfcopy *ecp, int ifd, int ofd)
}
pb->pb_align = 1;
pb->pb_off = 0;
pb->pb_size = roundup(sh.sh_size, poh.oh_filealign);
if ((pb->pb_buf = calloc(1, pb->pb_size)) == NULL) {
warn("calloc failed");
continue;
if (sh.sh_type != SHT_NOBITS) {
pb->pb_size = roundup(sh.sh_size, poh.oh_filealign);
if ((pb->pb_buf = calloc(1, pb->pb_size)) == NULL) {
warn("calloc failed");
continue;
}
memcpy(pb->pb_buf, d->d_buf, sh.sh_size);
}
memcpy(pb->pb_buf, d->d_buf, sh.sh_size);
}
elferr = elf_errno();
if (elferr != 0)
Expand Down

0 comments on commit 72eba96

Please sign in to comment.