Skip to content

Commit

Permalink
Merge branch 'radareorg:master' into fix_atomic
Browse files Browse the repository at this point in the history
  • Loading branch information
bsekisser authored Jan 16, 2025
2 parents e140ee5 + d6cd906 commit 6086307
Show file tree
Hide file tree
Showing 18 changed files with 101 additions and 99 deletions.
9 changes: 6 additions & 3 deletions libr/bin/bobj.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ static void object_delete_items(RBinObject *o) {
r_list_free (o->strings);
ht_up_free (o->strings_db);

if (!RVecRBinImport_empty (&o->imports_vec)) {
RVecRBinImport_fini (&o->imports_vec);
}
if (!RVecRBinSymbol_empty (&o->symbols_vec)) {
RVecRBinSymbol_fini (&o->symbols_vec);
if (o->symbols) {
Expand Down Expand Up @@ -422,12 +425,12 @@ R_API int r_bin_object_set_items(RBinFile *bf, RBinObject *bo) {
}
if (bin->filter_rules & (R_BIN_REQ_RELOCS | R_BIN_REQ_IMPORTS)) {
if (p->relocs) {
const RList *l = p->relocs (bf); // XXX this is an internal list (should be a vector), and shouldnt be freed by the caller
RList *l = (RList *)p->relocs (bf); // XXX this is an internal list (should be a vector), and shouldnt be freed by the caller
if (l) {
REBASE_PADDR (bo, l, RBinReloc);
bo->relocs = list2rbtree ((RList*)l);
// l->free = NULL;
// r_list_free (l);
l->free = NULL;
r_list_free (l);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions libr/bin/format/bflt/bflt.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ static bool r_bin_bflt_init(RBinBfltObj *obj, RBuffer *buf) {

R_IPI void r_bin_bflt_free(RBinBfltObj *o) {
if (o) {
r_list_free (o->relocs_list);
R_FREE (o->hdr);
r_buf_free (o->b);
free (o);
Expand Down
1 change: 1 addition & 0 deletions libr/bin/format/bflt/bflt.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ typedef struct r_bin_bflt_obj {
RBinBfltHeader *hdr;
RBinBfltReloc *reloc_table;
RBinBfltReloc *got_table;
RList *relocs_list;
RBuffer *b;
ut8 endian;
size_t size;
Expand Down
2 changes: 1 addition & 1 deletion libr/bin/format/coff/coff.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2008-2024 pancake, inisider */
/* radare - LGPL - Copyright 2008-2025 pancake, inisider */

#include <r_util.h>
#include "coff.h"
Expand Down
17 changes: 15 additions & 2 deletions libr/bin/format/elf/elf.c
Original file line number Diff line number Diff line change
Expand Up @@ -4919,6 +4919,19 @@ void Elf_(free)(ELFOBJ* eo) {
if (!eo) {
return;
}
r_list_free (eo->relocs_list);
if (eo->imports_by_ord) {
int i;
for (i = 0; i < eo->imports_by_ord_size; i++) {
RBinImport *imp = eo->imports_by_ord[i];
if (imp) {
r_bin_import_free (eo->imports_by_ord[i]);
eo->imports_by_ord[i] = NULL;
}
}
eo->imports_by_ord_size = 0;
R_FREE (eo->imports_by_ord);
}
free (eo->osabi);
free (eo->phdr);
free (eo->shdr);
Expand Down Expand Up @@ -4947,8 +4960,8 @@ void Elf_(free)(ELFOBJ* eo) {
eo->phdr_symbols_vec = NULL;
}
// causes double free in g_symbols_vec.free() 2 lines below
// RVecRBinElfSymbol_free (eo->phdr_symbols_vec);
// RVecRBinElfSymbol_free (eo->phdr_imports_vec);
RVecRBinElfSymbol_free (eo->phdr_symbols_vec);
RVecRBinElfSymbol_free (eo->phdr_imports_vec);
RVecRBinElfSymbol_free (eo->g_symbols_vec);
RVecRBinElfSymbol_free (eo->g_imports_vec);
#if 0
Expand Down
1 change: 1 addition & 0 deletions libr/bin/format/elf/elf.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ struct Elf_(obj_t) {
ut32 g_reloc_num;
bool relocs_loaded;
RVector g_relocs; // RBinElfReloc
RList *relocs_list;
bool sections_loaded;
bool sections_cached;
#if R2_590
Expand Down
6 changes: 4 additions & 2 deletions libr/bin/format/pyc/marshal.c
Original file line number Diff line number Diff line change
Expand Up @@ -1174,8 +1174,10 @@ static bool extract_sections_symbols(pyc_object *obj, RList *sections, RList *sy
free (section);
fail2:
free (prefix);
free (symbol->name);
free (symbol);
if (symbol) {
free (symbol->name);
free (symbol);
}
return false;
}

Expand Down
4 changes: 4 additions & 0 deletions libr/bin/p/bin_bflt.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,11 @@ static ut32 get_ngot_entries(struct r_bin_bflt_obj *obj) {

static RList *relocs(RBinFile *bf) {
struct r_bin_bflt_obj *obj = (struct r_bin_bflt_obj *) bf->bo->bin_obj;
if (obj->relocs_list) {
return r_list_clone (obj->relocs_list, NULL);
}
RList *list = r_list_newf ((RListFree) free);
obj->relocs_list = list;
ut32 i, len, n_got, amount;
if (!list || !obj) {
r_list_free (list);
Expand Down
54 changes: 22 additions & 32 deletions libr/bin/p/bin_coff.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2014-2024 - Fedor Sakharov */
/* radare - LGPL - Copyright 2014-2025 - Fedor Sakharov */

#include <r_bin.h>
#include <sdb/ht_uu.h>
Expand Down Expand Up @@ -499,70 +499,60 @@ static ut16 _read_le16(RBin *rbin, ut64 addr) {
return r_read_le16 (data);
}

#define BYTES_PER_IMP_RELOC 8
#define BYTES_PER_IMP_RELOC 8

static RList *_relocs_list(RBin *rbin, struct r_bin_coff_obj *bin, bool patch, ut64 imp_map) {
R_RETURN_VAL_IF_FAIL (bin, NULL);
if (!bin->scn_hdrs) {
static RList *_relocs_list(RBin *rbin, struct r_bin_coff_obj *co, bool patch, ut64 imp_map) {
R_RETURN_VAL_IF_FAIL (rbin && co, NULL);
if (!co->scn_hdrs) {
return NULL;
}

RBinReloc *reloc;
struct coff_reloc *rel;
int j, i = 0;
ut32 f_nscns = bin->type == COFF_TYPE_BIGOBJ? bin->bigobj_hdr.f_nscns: bin->hdr.f_nscns;
RList *list_rel = r_list_newf (free);
if (!list_rel) {
return NULL;
}
ut32 f_nscns = (co->type == COFF_TYPE_BIGOBJ)
? co->bigobj_hdr.f_nscns: co->hdr.f_nscns;
const bool patch_imports = patch && (imp_map != UT64_MAX);
HtUU *imp_vaddr_ht = patch_imports? ht_uu_new0 (): NULL;
if (patch_imports && !imp_vaddr_ht) {
r_list_free (list_rel);
return NULL;
}
RList *list_rel = r_list_newf (free); // r_bin_reloc_free
for (i = 0; i < f_nscns; i++) {
if (!bin->scn_hdrs[i].s_nreloc) {
if (!co->scn_hdrs[i].s_nreloc) {
continue;
}
int len = 0, size = bin->scn_hdrs[i].s_nreloc * sizeof (struct coff_reloc);
int len = 0, size = co->scn_hdrs[i].s_nreloc * sizeof (struct coff_reloc);
if (size < 0) {
break;
}
rel = calloc (1, size + sizeof (struct coff_reloc));
struct coff_reloc *rel = calloc (1, size + sizeof (struct coff_reloc));
if (!rel) {
break;
}
if (bin->scn_hdrs[i].s_relptr > bin->size ||
bin->scn_hdrs[i].s_relptr + size > bin->size) {
if (co->scn_hdrs[i].s_relptr > co->size \
|| co->scn_hdrs[i].s_relptr + size > co->size) {
free (rel);
break;
}
len = r_buf_read_at (bin->b, bin->scn_hdrs[i].s_relptr, (ut8*)rel, size);
len = r_buf_read_at (co->b, co->scn_hdrs[i].s_relptr, (ut8*)rel, size);
if (len != size) {
free (rel);
break;
}
for (j = 0; j < bin->scn_hdrs[i].s_nreloc; j++) {
RBinSymbol *symbol = (RBinSymbol *)ht_up_find (bin->sym_ht, (ut64)rel[j].r_symndx, NULL);
for (j = 0; j < co->scn_hdrs[i].s_nreloc; j++) {
RBinSymbol *symbol = (RBinSymbol *)ht_up_find (co->sym_ht, (ut64)rel[j].r_symndx, NULL);
if (!symbol) {
continue;
}
reloc = R_NEW0 (RBinReloc);
if (!reloc) {
continue;
}

RBinReloc *reloc = R_NEW0 (RBinReloc);
reloc->symbol = symbol;
reloc->paddr = bin->scn_hdrs[i].s_scnptr + rel[j].r_vaddr;
if (bin->scn_va) {
reloc->vaddr = bin->scn_va[i] + rel[j].r_vaddr;
reloc->paddr = co->scn_hdrs[i].s_scnptr + rel[j].r_vaddr;
if (co->scn_va) {
reloc->vaddr = co->scn_va[i] + rel[j].r_vaddr;
}
reloc->type = rel[j].r_type;

ut64 sym_vaddr = symbol->vaddr;
if (symbol->is_imported) {
reloc->import = (RBinImport *)ht_up_find (bin->imp_ht, (ut64)rel[j].r_symndx, NULL);
reloc->import = (RBinImport *)ht_up_find (co->imp_ht, (ut64)rel[j].r_symndx, NULL);
if (patch_imports) {
bool found;
sym_vaddr = ht_uu_find (imp_vaddr_ht, (ut64)rel[j].r_symndx, &found);
Expand All @@ -579,7 +569,7 @@ static RList *_relocs_list(RBin *rbin, struct r_bin_coff_obj *bin, bool patch, u
if (sym_vaddr) {
int plen = 0;
ut8 patch_buf[8];
ut16 magic = bin->type == COFF_TYPE_BIGOBJ? bin->bigobj_hdr.f_magic: bin->hdr.f_magic;
ut16 magic = co->type == COFF_TYPE_BIGOBJ? co->bigobj_hdr.f_magic: co->hdr.f_magic;
switch (magic) {
case COFF_FILE_MACHINE_I386:
switch (rel[j].r_type) {
Expand Down
28 changes: 10 additions & 18 deletions libr/bin/p/bin_elf.inc.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2009-2024 - nibble, pancake, luctielen */
/* radare - LGPL - Copyright 2009-2025 - nibble, pancake, luctielen */

#define R_LOG_ORIGIN "bin.elf"

Expand Down Expand Up @@ -61,20 +61,7 @@ static bool load(RBinFile *bf, RBuffer *buf, ut64 loadaddr) {
}

static void destroy(RBinFile *bf) {
ELFOBJ* eo = bf->bo->bin_obj;
if (eo && eo->imports_by_ord) {
int i;
for (i = 0; i < eo->imports_by_ord_size; i++) {
RBinImport *imp = eo->imports_by_ord[i];
if (imp) {
r_bin_import_free (eo->imports_by_ord[i]);
eo->imports_by_ord[i] = NULL;
}
}
R_FREE (eo->imports_by_ord);
}
RVecRBinElfSymbol_free (eo->phdr_imports_vec);
Elf_(free) (eo);
Elf_(free) ((ELFOBJ*)bf->bo->bin_obj);
}

static ut64 baddr(RBinFile *bf) {
Expand Down Expand Up @@ -721,9 +708,12 @@ static RBinReloc *reloc_convert(ELFOBJ* eo, RBinElfReloc *rel, ut64 got_addr) {

static RList* relocs(RBinFile *bf) {
R_RETURN_VAL_IF_FAIL (bf && bf->bo && bf->bo->bin_obj, NULL);
RList *ret = NULL;
ELFOBJ *eo = bf->bo->bin_obj;
if (!(ret = r_list_newf (free))) {
if (eo->relocs_list) {
return eo->relocs_list;
}
RList *ret = r_list_newf (free);
if (!ret) {
return NULL;
}

Expand Down Expand Up @@ -771,7 +761,9 @@ static RList* relocs(RBinFile *bf) {
}
}
ht_up_free (reloc_ht);
return ret;
eo->relocs_list = ret;
ret->free = NULL; // already freed in the hashtable
return r_list_clone (eo->relocs_list, NULL);
}

static void _patch_reloc(ELFOBJ *bo, ut16 e_machine, RIOBind *iob, RBinElfReloc *rel, ut64 S, ut64 B, ut64 L) {
Expand Down
21 changes: 9 additions & 12 deletions libr/bin/p/bin_mach0.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2009-2023 - pancake */
/* radare - LGPL - Copyright 2009-2025 - pancake */

#include <r_core.h>
#include "../i/private.h"
Expand Down Expand Up @@ -262,17 +262,14 @@ static void _r_bin_reloc_free(RBinReloc *reloc) {
}

static RList *relocs(RBinFile *bf) {
RList *ret = NULL;
RBinObject *obj = bf ? bf->bo : NULL;
struct MACH0_(obj_t) *bin = (bf && bf->bo)? bf->bo->bin_obj: NULL;
if (!obj || !obj->bin_obj || !(ret = r_list_newf ((RListFree)_r_bin_reloc_free))) {
return NULL;
}
ret->free = free;
R_RETURN_VAL_IF_FAIL (bf && bf->bo && bf->bo->bin_obj, NULL);
struct MACH0_(obj_t) *mo = bf->bo->bin_obj;
const RSkipList *relocs = MACH0_(load_relocs) (bf->bo->bin_obj);
if (!relocs) {
return ret;
return NULL;
}
RList *ret = r_list_newf ((RListFree)_r_bin_reloc_free);
// ret->free = free;

RSkipListNode *it;
struct reloc_t *reloc;
Expand All @@ -289,13 +286,13 @@ static RList *relocs(RBinFile *bf) {
ptr->additive = 0;
if (reloc->name[0]) {
RBinImport *imp;
if (!(imp = import_from_name (bf->rbin, (char*) reloc->name, bin->imports_by_name))) {
if (!(imp = import_from_name (bf->rbin, (char*) reloc->name, mo->imports_by_name))) {
free (ptr);
break;
}
ptr->import = imp;
} else if (reloc->ord >= 0 && bin->imports_by_ord && reloc->ord < bin->imports_by_ord_size) {
ptr->import = bin->imports_by_ord[reloc->ord];
} else if (reloc->ord >= 0 && mo->imports_by_ord && reloc->ord < mo->imports_by_ord_size) {
ptr->import = mo->imports_by_ord[reloc->ord];
} else {
ptr->import = NULL;
}
Expand Down
15 changes: 4 additions & 11 deletions libr/bin/p/bin_mz.c
Original file line number Diff line number Diff line change
Expand Up @@ -193,26 +193,19 @@ static void header(RBinFile *bf) {
}

static RList *relocs(RBinFile *bf) {
RList *ret = NULL;
RBinReloc *rel = NULL;
R_RETURN_VAL_IF_FAIL (bf && bf->bo && bf->bo->bin_obj, NULL);
const struct r_bin_mz_reloc_t *relocs = NULL;
int i;

if (!bf || !bf->bo || !bf->bo->bin_obj) {
return NULL;
}
if (!(ret = r_list_newf (free))) {
RList *ret = r_list_newf (free);
if (!ret) {
return NULL;
}
if (!(relocs = r_bin_mz_get_relocs (bf->bo->bin_obj))) {
return ret;
}
for (i = 0; !relocs[i].last; i++) {
if (!(rel = R_NEW0 (RBinReloc))) {
free ((void *)relocs);
r_list_free (ret);
return NULL;
}
RBinReloc *rel = R_NEW0 (RBinReloc);
rel->type = R_BIN_RELOC_16;
rel->vaddr = relocs[i].vaddr;
rel->paddr = relocs[i].paddr;
Expand Down
2 changes: 1 addition & 1 deletion libr/bin/p/bin_pe.c
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ RBinPlugin r_bin_plugin_pe = {
.header = &header,
.fields = &fields,
.libs = &libs,
.relocs = &relocs,
.relocs = relocs,
.minstrlen = 4,
.create = &create,
.get_vaddr = &get_vaddr,
Expand Down
9 changes: 6 additions & 3 deletions libr/bin/p/bin_pe.inc.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2009-2023 - nibble, pancake, alvarofe */
/* radare - LGPL - Copyright 2009-2025 - nibble, pancake, alvarofe */

#include <r_bin.h>
#include "../i/private.h"
Expand Down Expand Up @@ -197,7 +197,7 @@ static RList* symbols(RBinFile *bf) {
struct r_bin_pe_import_t *imports = NULL;
int i;

if (!(ret = r_list_newf (free))) {
if (!(ret = r_list_newf (r_bin_symbol_free))) {
return NULL;
}
RBinPEObj *pe = PE_(get) (bf);
Expand Down Expand Up @@ -322,7 +322,10 @@ static RList* imports(RBinFile *bf) {

static RList* relocs(RBinFile *bf) {
RBinPEObj *pe = PE_(get) (bf);
return pe? pe->relocs: NULL;
if (pe && pe->relocs) {
return r_list_clone (pe->relocs, NULL);
}
return NULL;
}

static RList* libs(RBinFile *bf) {
Expand Down
5 changes: 4 additions & 1 deletion libr/bin/p/bin_qnx.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,10 @@ static RBinInfo *info(RBinFile *bf) {
static RList *relocs(RBinFile *bf) {
R_RETURN_VAL_IF_FAIL (bf && bf->bo, NULL);
QnxObj *qo = bf->bo->bin_obj;
return r_list_clone (qo->fixups, NULL);
if (qo && qo->fixups) {
return r_list_clone (qo->fixups, NULL);
}
return NULL;
}

static void header(RBinFile *bf) {
Expand Down
Loading

0 comments on commit 6086307

Please sign in to comment.