From 656d9613ca6e295bdb7e516c5b74f2d962d70896 Mon Sep 17 00:00:00 2001 From: Nibble Date: Tue, 21 Apr 2009 12:11:07 +0200 Subject: [PATCH] * r_bin - Minor fixups in r_bin_elf * r_asm - Added multiline support to massemble - Fixed buf_hex generation in massemble --- libr/asm/asm.c | 2 +- libr/bin/format/elf/elf.c | 41 +++++++++++++++++++++++++++++++++++---- libr/include/r_util.h | 4 ++-- 3 files changed, 40 insertions(+), 7 deletions(-) diff --git a/libr/asm/asm.c b/libr/asm/asm.c index f6fcf20573..46b250f2f5 100644 --- a/libr/asm/asm.c +++ b/libr/asm/asm.c @@ -237,7 +237,7 @@ R_API int r_asm_massemble(struct r_asm_t *a, struct r_asm_aop_t *aop, char *buf) for (stage = 0; stage < 2; stage++) { if (stage == 0 && !labels) continue; - for (idx = ret = i = j = 0, label_offset = a->pc; + for (idx = ret = i = j = 0, label_offset = a->pc, buf_hex[0] = '\0'; i <= ctr; i++, idx += ret, label_offset += ret) { strncpy(buf_token, tokens[i], R_ASM_BUFSIZE); if (stage == 1) diff --git a/libr/bin/format/elf/elf.c b/libr/bin/format/elf/elf.c index 451c1bbaee..590095740c 100644 --- a/libr/bin/format/elf/elf.c +++ b/libr/bin/format/elf/elf.c @@ -705,6 +705,7 @@ int ELF_(r_bin_elf_is_big_endian)(ELF_(r_bin_elf_obj) *bin) /* TODO: Take care of endianess */ /* TODO: Real error handling */ /* TODO: Resize sections before .init */ +/* TODO: HUGE REFACTORING, CAUTION: VERY UGLY CODE */ u64 ELF_(r_bin_elf_resize_section)(ELF_(r_bin_elf_obj) *bin, const char *name, u64 size) { ELF_(Ehdr) *ehdr = &bin->ehdr; @@ -714,7 +715,7 @@ u64 ELF_(r_bin_elf_resize_section)(ELF_(r_bin_elf_obj) *bin, const char *name, u ELF_(Off) rsz_offset, new_offset; ELF_(Addr) new_addr, got_addr = 0; u64 off, got_offset, delta = 0; - u64 rsz_osize = 0, rsz_fsize, rsz_size = size; + u64 rsz_osize = 0, rsz_fsize, rsz_size = size, phdr_size; int i, j, done = 0; int elf64 = (bin->ehdr.e_ident[EI_CLASS] == ELFCLASS64); @@ -842,10 +843,41 @@ u64 ELF_(r_bin_elf_resize_section)(ELF_(r_bin_elf_obj) *bin, const char *name, u } /* rewrite program headers */ - for (i = 0, phdrp = phdr; i < ehdr->e_phnum; i++, phdrp++) + for (i = 0, phdrp = phdr; i < ehdr->e_phnum; i++, phdrp++) { +#if 0 + if (phdrp->p_offset < rsz_offset && phdrp->p_offset + phdrp->p_filesz > rsz_offset) { + if (elf64) + off = ehdr->e_phoff + i * sizeof(ELF_(Phdr)) + 2 * sizeof(ELF_(Word)) + sizeof(ELF_(Off)) + 2 * sizeof(ELF_(Addr)); + else off = ehdr->e_phoff + i * sizeof(ELF_(Phdr)) + sizeof(ELF_(Word)) + sizeof(ELF_(Off)) + 2 * sizeof(ELF_(Addr)); + + if (lseek(bin->fd, off, SEEK_SET) < 0) + perror("lseek"); + + if (elf64) { + phdr_size = phdrp->p_filesz + delta; + if (write(bin->fd, (ELF_(Xword)*)&phdr_size, sizeof(ELF_(Xword))) != sizeof(ELF_(Xword))) + perror("write (off)"); + phdr_size = phdrp->p_memsz + delta; + if (write(bin->fd, (ELF_(Xword)*)&phdr_size, sizeof(ELF_(Xword))) != sizeof(ELF_(Xword))) + perror("write (off)"); + } else { + phdr_size = phdrp->p_filesz + delta; + printf("FILESZ: %lli --> %lli\n", (u64)phdrp->p_filesz, (u64)phdr_size); + if (write(bin->fd, (ELF_(Word)*)&phdr_size, sizeof(ELF_(Word))) != sizeof(ELF_(Word))) + perror("write (off)"); + phdr_size = phdrp->p_memsz + delta; + printf("MEMSZ: %lli --> %lli\n", (u64)phdrp->p_memsz, (u64)phdr_size); + if (write(bin->fd, (ELF_(Word)*)&phdr_size, sizeof(ELF_(Word))) != sizeof(ELF_(Word))) + perror("write (off)"); + } + } +#endif + if (phdrp->p_offset >= rsz_offset + rsz_osize) { new_offset = (ELF_(Off)) (phdrp->p_offset + delta); - off = ehdr->e_phoff + i * sizeof(ELF_(Phdr)) + sizeof(ELF_(Word)); + if (elf64) + off = ehdr->e_phoff + i * sizeof(ELF_(Phdr)) + 2 * sizeof(ELF_(Word)); + else off = ehdr->e_phoff + i * sizeof(ELF_(Phdr)) + sizeof(ELF_(Word)); if (lseek(bin->fd, off, SEEK_SET) < 0) perror("lseek"); @@ -862,6 +894,7 @@ u64 ELF_(r_bin_elf_resize_section)(ELF_(r_bin_elf_obj) *bin, const char *name, u printf("-> program header (%08llx)\n", (u64) phdrp->p_offset); } + } /* rewrite other elf pointers (entrypoint, phoff, shoff) */ if (ehdr->e_entry - bin->base_addr >= rsz_offset + rsz_osize) { @@ -902,7 +935,7 @@ u64 ELF_(r_bin_elf_resize_section)(ELF_(r_bin_elf_obj) *bin, const char *name, u // XXX Check when delta is negative /* rewrite section contents */ { - u64 rest_size = rsz_fsize - rsz_offset; + u64 rest_size = rsz_fsize - (rsz_offset + rsz_osize); u8 *buf = (u8 *)malloc(rest_size); printf("COPY FROM 0x%08llx\n", (u64) rsz_offset+rsz_osize); lseek(bin->fd, rsz_offset+rsz_osize, SEEK_SET); diff --git a/libr/include/r_util.h b/libr/include/r_util.h index 2090b62a95..cc7312ae0a 100644 --- a/libr/include/r_util.h +++ b/libr/include/r_util.h @@ -61,8 +61,8 @@ void r_num_init(struct r_num_t *num); #define strnull(x) (!x||!*x) #define iswhitechar(x) (x==' '||x=='\t'||x=='\n'||x=='\r') #define iswhitespace(x) (x==' '||x=='\t') -#define isseparator(x) (x==' '||x=='\t'||x==','||x==';'||\ - x==':'||x=='['||x==']'||x=='('||x==')'||x=='{'||x=='}') +#define isseparator(x) (x==' '||x=='\t'||x=='\n'||x=='\r'||x==' '|| \ + x==','||x==';'||x==':'||x=='['||x==']'||x=='('||x==')'||x=='{'||x=='}') #define ishexchar(x) ((x>='0'&&x<='9') || (x>='a'&&x<='f') || (x>='A'&&x<='F')) { /* stabilized */