- Add support for reading/removing rpath to r_bin_elf
  - Add r_bin_wr_rpath_del
  - Un-R_API r_bin_elf
  - Add rpathdel.c into bin/t/
This commit is contained in:
Nibble 2010-03-16 02:24:12 +01:00
parent 385abb8a68
commit 49f977a696
14 changed files with 182 additions and 38 deletions

View File

@ -11,6 +11,12 @@ R_API ut64 r_bin_wr_scn_resize(RBin *bin, const char *name, ut64 size) {
return R_FALSE;
}
R_API int r_bin_wr_rpath_del(RBin *bin) {
if (bin && bin->cur && bin->cur->write && bin->cur->write->rpath_del)
return bin->cur->write->rpath_del (bin);
return R_FALSE;
}
R_API int r_bin_wr_output(RBin *bin, const char *filename) {
return r_file_dump (filename, bin->buf->buf, bin->buf->length);
}

View File

@ -380,17 +380,12 @@ int Elf_(r_bin_elf_is_big_endian)(struct Elf_(r_bin_elf_obj_t) *bin) {
return (bin->ehdr.e_ident[EI_DATA] == ELFDATA2MSB);
}
/* XXX Init dt_strtab? */
char *Elf_(r_bin_elf_get_rpath)(struct Elf_(r_bin_elf_obj_t) *bin) {
/* TODO */
return NULL;
}
struct r_bin_elf_lib_t* Elf_(r_bin_elf_get_libs)(struct Elf_(r_bin_elf_obj_t) *bin)
{
struct r_bin_elf_lib_t *ret = NULL;
Elf_(Dyn) *dyn = NULL;
ut64 stroff;
int ndyn, i, j, k;
char *ret = NULL;
int ndyn, i, j, len;
for (i = 0; i < bin->ehdr.e_phnum; i++)
if (bin->phdr[i].p_type == PT_DYNAMIC) {
@ -398,12 +393,68 @@ struct r_bin_elf_lib_t* Elf_(r_bin_elf_get_libs)(struct Elf_(r_bin_elf_obj_t) *b
perror("malloc (dyn)");
return NULL;
}
if (r_buf_read_at (bin->b, bin->phdr[i].p_offset, (ut8*)dyn, bin->phdr[i].p_filesz) == -1) {
ndyn = (int)(bin->phdr[i].p_filesz / sizeof(Elf_(Dyn)));
#if R_BIN_ELF64
len = r_buf_fread_at(bin->b, bin->phdr[i].p_offset, (ut8*)dyn, bin->endian?"2L":"2l", ndyn);
#else
len = r_buf_fread_at(bin->b, bin->phdr[i].p_offset, (ut8*)dyn, bin->endian?"2I":"2i", ndyn);
#endif
if (len == -1) {
eprintf("Error: read (dyn)\n");
free (dyn);
return NULL;
}
for (j = 0; j < ndyn; j++)
if (dyn[j].d_tag == DT_STRTAB) {
stroff = (ut64)(dyn[j].d_un.d_ptr - bin->baddr);
break;
}
for (j = 0; j < ndyn; j++)
if (dyn[j].d_tag == DT_RPATH || dyn[j].d_tag == DT_RUNPATH) {
if ((ret = malloc (ELF_STRING_LENGTH)) == NULL) {
perror("malloc (rpath)");
free (dyn);
return NULL;
}
if (r_buf_read_at (bin->b, stroff + dyn[j].d_un.d_val,
(ut8*)ret, ELF_STRING_LENGTH) == -1) {
eprintf("Error: read (rpath)\n");
free (ret);
free (dyn);
return NULL;
}
break;
}
free (dyn);
break;
}
return ret;
}
struct r_bin_elf_lib_t* Elf_(r_bin_elf_get_libs)(struct Elf_(r_bin_elf_obj_t) *bin)
{
struct r_bin_elf_lib_t *ret = NULL;
Elf_(Dyn) *dyn = NULL;
ut64 stroff;
int ndyn, i, j, k, len;
for (i = 0; i < bin->ehdr.e_phnum; i++)
if (bin->phdr[i].p_type == PT_DYNAMIC) {
if (!(dyn = malloc (bin->phdr[i].p_filesz))) {
perror("malloc (dyn)");
return NULL;
}
ndyn = (int)(bin->phdr[i].p_filesz / sizeof(Elf_(Dyn)));
#if R_BIN_ELF64
len = r_buf_fread_at(bin->b, bin->phdr[i].p_offset, (ut8*)dyn, bin->endian?"2L":"2l", ndyn);
#else
len = r_buf_fread_at(bin->b, bin->phdr[i].p_offset, (ut8*)dyn, bin->endian?"2I":"2i", ndyn);
#endif
if (len == -1) {
eprintf("Error: read (dyn)\n");
free (dyn);
return NULL;
}
for (j = 0; j < ndyn; j++)
if (dyn[j].d_tag == DT_STRTAB) {
stroff = (ut64)(dyn[j].d_un.d_ptr - bin->baddr);

View File

@ -64,29 +64,27 @@ struct Elf_(r_bin_elf_obj_t) {
struct r_buf_t* b;
};
#ifdef R_API
R_API char *Elf_(r_bin_elf_get_rpath)(struct Elf_(r_bin_elf_obj_t) *bin);
R_API ut64 Elf_(r_bin_elf_get_baddr)(struct Elf_(r_bin_elf_obj_t) *bin);
R_API ut64 Elf_(r_bin_elf_get_entry_offset)(struct Elf_(r_bin_elf_obj_t) *bin);
R_API int Elf_(r_bin_elf_get_stripped)(struct Elf_(r_bin_elf_obj_t) *bin);
R_API int Elf_(r_bin_elf_get_static)(struct Elf_(r_bin_elf_obj_t) *bin);
R_API char* Elf_(r_bin_elf_get_data_encoding)(struct Elf_(r_bin_elf_obj_t) *bin);
R_API char* Elf_(r_bin_elf_get_arch)(struct Elf_(r_bin_elf_obj_t) *bin);
R_API char* Elf_(r_bin_elf_get_machine_name)(struct Elf_(r_bin_elf_obj_t) *bin);
R_API char* Elf_(r_bin_elf_get_file_type)(struct Elf_(r_bin_elf_obj_t) *bin);
R_API char* Elf_(r_bin_elf_get_elf_class)(struct Elf_(r_bin_elf_obj_t) *bin);
R_API int Elf_(r_bin_elf_get_bits)(struct Elf_(r_bin_elf_obj_t) *bin);
R_API char* Elf_(r_bin_elf_get_osabi_name)(struct Elf_(r_bin_elf_obj_t) *bin);
R_API int Elf_(r_bin_elf_is_big_endian)(struct Elf_(r_bin_elf_obj_t) *bin);
R_API struct r_bin_elf_lib_t* Elf_(r_bin_elf_get_libs)(struct Elf_(r_bin_elf_obj_t) *bin);
R_API struct r_bin_elf_section_t* Elf_(r_bin_elf_get_sections)(struct Elf_(r_bin_elf_obj_t) *bin);
R_API struct r_bin_elf_symbol_t* Elf_(r_bin_elf_get_symbols)(struct Elf_(r_bin_elf_obj_t) *bin, int type);
R_API struct r_bin_elf_field_t* Elf_(r_bin_elf_get_fields)(struct Elf_(r_bin_elf_obj_t) *bin);
R_API void* Elf_(r_bin_elf_free)(struct Elf_(r_bin_elf_obj_t)* bin);
R_API struct Elf_(r_bin_elf_obj_t)* Elf_(r_bin_elf_new)(const char* file);
R_API
R_API ut64 Elf_(r_bin_elf_resize_section)(struct Elf_(r_bin_elf_obj_t) *bin, const char *name, ut64 size);
#endif
ut64 Elf_(r_bin_elf_get_baddr)(struct Elf_(r_bin_elf_obj_t) *bin);
ut64 Elf_(r_bin_elf_get_entry_offset)(struct Elf_(r_bin_elf_obj_t) *bin);
int Elf_(r_bin_elf_get_stripped)(struct Elf_(r_bin_elf_obj_t) *bin);
int Elf_(r_bin_elf_get_static)(struct Elf_(r_bin_elf_obj_t) *bin);
char* Elf_(r_bin_elf_get_data_encoding)(struct Elf_(r_bin_elf_obj_t) *bin);
char* Elf_(r_bin_elf_get_arch)(struct Elf_(r_bin_elf_obj_t) *bin);
char* Elf_(r_bin_elf_get_machine_name)(struct Elf_(r_bin_elf_obj_t) *bin);
char* Elf_(r_bin_elf_get_file_type)(struct Elf_(r_bin_elf_obj_t) *bin);
char* Elf_(r_bin_elf_get_elf_class)(struct Elf_(r_bin_elf_obj_t) *bin);
int Elf_(r_bin_elf_get_bits)(struct Elf_(r_bin_elf_obj_t) *bin);
char* Elf_(r_bin_elf_get_osabi_name)(struct Elf_(r_bin_elf_obj_t) *bin);
int Elf_(r_bin_elf_is_big_endian)(struct Elf_(r_bin_elf_obj_t) *bin);
struct r_bin_elf_lib_t* Elf_(r_bin_elf_get_libs)(struct Elf_(r_bin_elf_obj_t) *bin);
struct r_bin_elf_section_t* Elf_(r_bin_elf_get_sections)(struct Elf_(r_bin_elf_obj_t) *bin);
struct r_bin_elf_symbol_t* Elf_(r_bin_elf_get_symbols)(struct Elf_(r_bin_elf_obj_t) *bin, int type);
struct r_bin_elf_field_t* Elf_(r_bin_elf_get_fields)(struct Elf_(r_bin_elf_obj_t) *bin);
char *Elf_(r_bin_elf_get_rpath)(struct Elf_(r_bin_elf_obj_t) *bin);
void* Elf_(r_bin_elf_free)(struct Elf_(r_bin_elf_obj_t)* bin);
struct Elf_(r_bin_elf_obj_t)* Elf_(r_bin_elf_new)(const char* file);
ut64 Elf_(r_bin_elf_resize_section)(struct Elf_(r_bin_elf_obj_t) *bin, const char *name, ut64 size);
int Elf_(r_bin_elf_del_rpath)(struct Elf_(r_bin_elf_obj_t) *bin);
#endif

View File

@ -167,3 +167,41 @@ ut64 Elf_(r_bin_elf_resize_section)(struct Elf_(r_bin_elf_obj_t) *bin, const cha
return delta;
}
/* XXX Endianness? */
int Elf_(r_bin_elf_del_rpath)(struct Elf_(r_bin_elf_obj_t) *bin) {
Elf_(Dyn) *dyn = NULL;
ut64 stroff;
int ndyn, i, j;
for (i = 0; i < bin->ehdr.e_phnum; i++)
if (bin->phdr[i].p_type == PT_DYNAMIC) {
if (!(dyn = malloc (bin->phdr[i].p_filesz))) {
perror("malloc (dyn)");
return R_FALSE;
}
if (r_buf_read_at (bin->b, bin->phdr[i].p_offset, (ut8*)dyn, bin->phdr[i].p_filesz) == -1) {
eprintf("Error: read (dyn)\n");
free (dyn);
return R_FALSE;
}
ndyn = (int)(bin->phdr[i].p_filesz / sizeof(Elf_(Dyn)));
for (j = 0; j < ndyn; j++)
if (dyn[j].d_tag == DT_STRTAB) {
stroff = (ut64)(dyn[j].d_un.d_ptr - bin->baddr);
break;
}
for (j = 0; j < ndyn; j++)
if (dyn[j].d_tag == DT_RPATH || dyn[j].d_tag == DT_RUNPATH) {
if (r_buf_write_at (bin->b, stroff + dyn[j].d_un.d_val,
(ut8*)"", 1) == -1) {
eprintf("Error: write (rpath)\n");
free (dyn);
return R_FALSE;
}
}
free (dyn);
break;
}
return R_TRUE;
}

View File

@ -155,9 +155,12 @@ static RBinInfo* info(RBin *bin)
if(!(ret = R_NEW (RBinInfo)))
return NULL;
ret->rpath = Elf_(r_bin_elf_get_rpath)(bin->bin_obj);
memset(ret, '\0', sizeof (RBinInfo));
strncpy (ret->file, bin->file, R_BIN_SIZEOF_STRINGS);
if ((str = Elf_(r_bin_elf_get_rpath)(bin->bin_obj))) {
strncpy (ret->rpath, str, R_BIN_SIZEOF_STRINGS);
free (str);
} else strncpy (ret->rpath, "NONE", R_BIN_SIZEOF_STRINGS);
if ((str = Elf_(r_bin_elf_get_file_type) (bin->bin_obj)) == NULL)
return NULL;
strncpy (ret->type, str, R_BIN_SIZEOF_STRINGS);

View File

@ -103,6 +103,7 @@ static RBinInfo* info(RBin *bin)
return NULL;
memset(ret, '\0', sizeof (RBinInfo));
strncpy (ret->file, bin->file, R_BIN_SIZEOF_STRINGS);
strncpy (ret->rpath, "NONE", R_BIN_SIZEOF_STRINGS);
strncpy (ret->type, "JAVA CLASS", R_BIN_SIZEOF_STRINGS);
version = r_bin_java_get_version (bin->bin_obj);
strncpy (ret->bclass, version, R_BIN_SIZEOF_STRINGS);

View File

@ -160,6 +160,7 @@ static RBinInfo* info(RBin *bin)
return NULL;
memset(ret, '\0', sizeof (RBinInfo));
strncpy (ret->file, bin->file, R_BIN_SIZEOF_STRINGS);
strncpy (ret->rpath, "NONE", R_BIN_SIZEOF_STRINGS);
if ((str = MACH0_(r_bin_mach0_get_class) (bin->bin_obj))) {
strncpy (ret->bclass, str, R_BIN_SIZEOF_STRINGS);
free (str);

View File

@ -167,6 +167,7 @@ static RBinInfo* info(RBin *bin)
return NULL;
memset(ret, '\0', sizeof (RBinInfo));
strncpy (ret->file, bin->file, R_BIN_SIZEOF_STRINGS);
strncpy (ret->rpath, "NONE", R_BIN_SIZEOF_STRINGS);
if ((str = PE_(r_bin_pe_get_class) (bin->bin_obj))) {
strncpy (ret->bclass, str, R_BIN_SIZEOF_STRINGS);
free (str);

View File

@ -8,9 +8,14 @@ static ut64 scn_resize(RBin *bin, const char *name, ut64 size) {
return Elf_(r_bin_elf_resize_section) (bin->bin_obj, name, size);
}
static int rpath_del(RBin *bin) {
return Elf_(r_bin_elf_del_rpath) (bin->bin_obj);
}
#if !R_BIN_ELF64
struct r_bin_write_t r_bin_write_elf = {
.scn_resize = &scn_resize,
.rpath_del = &rpath_del,
};
#endif

View File

@ -5,4 +5,5 @@
struct r_bin_write_t r_bin_write_elf64 = {
.scn_resize = &scn_resize,
.rpath_del = &rpath_del,
};

View File

@ -9,15 +9,18 @@ LIBS+=${DL_LIBS}
LDPATH=-L.. -L../../util
LDPATH+=-lr_util
all: ${BIN} test_meta${EXT_EXE} mach-ex${EXT_EXE}
all: ${BIN} test_meta${EXT_EXE} mach-ex${EXT_EXE} rpathdel${EXT_EXE}
test_meta${EXT_EXE}: test_meta.o
${CC} test_meta.o ${LDPATH} ${LDFLAGS} -o test_meta${EXT_EXE}
rpatdel${EXT_EXE}: rpathdel.o
${CC} rpathdel.o ${LDPATH} ${LDFLAGS} -o rpathdel${EXT_EXE}
mach-ex${EXT_EXE}:
${CC} -o mach-ex${EXT_EXE} mach-ex.c
myclean:
rm -f test_meta test_meta.o mach-ex
rm -f test_meta test_meta.o mach-ex rpathdel
include ../../rules.mk

View File

@ -329,7 +329,8 @@ static int rabin_show_info() {
"Static=%s\n"
"Line_nums=%s\n"
"Local_syms=%s\n"
"Relocs=%s\n",
"Relocs=%s\n"
"RPath=%s\n",
info->file, info->type, info->bclass,
info->arch, info->bits, info->machine, info->os,
info->subsystem, info->big_endian?"True":"False",
@ -337,7 +338,8 @@ static int rabin_show_info() {
R_BIN_DBG_STATIC (info->dbg_info)?"True":"False",
R_BIN_DBG_LINENUMS (info->dbg_info)?"True":"False",
R_BIN_DBG_SYMS (info->dbg_info)?"True":"False",
R_BIN_DBG_RELOCS (info->dbg_info)?"True":"False");
R_BIN_DBG_RELOCS (info->dbg_info)?"True":"False",
info->rpath);
return R_TRUE;
}

32
libr/bin/t/rpathdel.c Normal file
View File

@ -0,0 +1,32 @@
/* rpathdel.c - rooted 2010 - nibble<develsec.org> */
#include <stdio.h>
#include <r_bin.h>
int main(int argc, char **argv) {
RBin *bin;
char *input, *output;
if (argc != 3) {
fprintf (stderr, "Usage: %s <input file> <output file>\n", argv[0]);
return 1;
}
input = argv[1];
output = argv[2];
bin = r_bin_new ();
if (!r_bin_load (bin, input, NULL)) {
fprintf (stderr, "Error: Cannot open file '%s'\n", input);
return 1;
}
if (!r_bin_wr_rpath_del (bin)) {
fprintf (stderr, "Error: Cannot remove rpath\n");
return 1;
}
r_bin_wr_output (bin, output);
r_bin_free (bin);
return 0;
}

View File

@ -114,10 +114,10 @@ typedef struct r_bin_info_t {
char machine[R_BIN_SIZEOF_STRINGS];
char os[R_BIN_SIZEOF_STRINGS];
char subsystem[R_BIN_SIZEOF_STRINGS];
char rpath[R_BIN_SIZEOF_STRINGS];
int bits;
int big_endian;
ut64 dbg_info;
char *rpath;
} RBinInfo;
typedef struct r_bin_field_t {
@ -132,6 +132,7 @@ typedef struct r_bin_meta_t {
typedef struct r_bin_write_t {
ut64 (*scn_resize)(RBin *bin, const char *name, ut64 size);
int (*rpath_del)(RBin *bin);
} RBinWrite;
#ifdef R_API
@ -166,6 +167,7 @@ R_API int r_bin_meta_get_line(RBin *bin, ut64 addr, char *file, int len, int *li
/* bin_write.c */
R_API ut64 r_bin_wr_scn_resize(RBin *bin, const char *name, ut64 size);
R_API int r_bin_wr_rpath_del(RBin *bin);
R_API int r_bin_wr_output(RBin *bin, const char *filename);
#endif