Skip to content

Commit 144b79d

Browse files
committed
wip
1 parent 26fcb80 commit 144b79d

File tree

2 files changed

+46
-19
lines changed

2 files changed

+46
-19
lines changed

libr/bin/format/elf/elf.c

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5275,15 +5275,23 @@ static bool reloc_fill_local_address(ELFOBJ *eo) {
52755275
GotPltBounds ri = {0};
52765276
RBinElfSection *s;
52775277

5278+
ut64 di0 = UT64_MAX;
5279+
ut64 di1 = UT64_MAX;
52785280
// find got/plt section bounadries
52795281
r_vector_foreach (&eo->g_sections, s) {
52805282
if (!strcmp (s->name, ".got")) {
52815283
ri.got = true;
52825284
ri.got_min = s->offset;
52835285
ri.got_max = s->offset + s->size;
52845286
ri.got_va = s->rva;
5285-
}
5286-
if (!strcmp (s->name, ".plt")) {
5287+
#if 1
5288+
} else if (!strcmp (s->name, ".debug_info")) {
5289+
// di0 = s->rva;
5290+
// di1 = s->rva + s->size;
5291+
di0 = s->offset;
5292+
di1 = s->offset + s->size;
5293+
#endif
5294+
} else if (!strcmp (s->name, ".plt")) {
52875295
ri.plt_min = s->offset;
52885296
ri.plt_max = s->offset + s->size;
52895297
ri.plt_va = s->rva;
@@ -5294,7 +5302,9 @@ static bool reloc_fill_local_address(ELFOBJ *eo) {
52945302
}
52955303
}
52965304
if (!ri.got || !ri.plt) {
5297-
return false;
5305+
if (di0 == UT64_MAX) {
5306+
return false;
5307+
}
52985308
}
52995309
ut64 baddr = eo->user_baddr; // 0x10000;
53005310
if (baddr == UT64_MAX) {
@@ -5304,13 +5314,22 @@ static bool reloc_fill_local_address(ELFOBJ *eo) {
53045314
// resolve got and plt
53055315
r_vector_foreach (&eo->g_relocs, reloc) {
53065316
const ut64 raddr = reloc->offset;
5317+
if (!ri.got && !ri.plt) {
5318+
index++;
5319+
ut64 ra = baddr + di0 + (index * 4);
5320+
ra += 685182;
5321+
reloc->addend = 0; // index;
5322+
// reloc->rva = ra + baddr; // address to patch
5323+
reloc->laddr = ra;
5324+
continue;
5325+
}
53075326
if (raddr < ri.got_min || raddr >= ri.got_max) {
53085327
continue;
53095328
}
53105329
ut64 rvaddr = reloc->offset; // rva (eo, reloc->offset, reloc->rva);
53115330
ut64 pltptr = 0; // relocated buf tells the section to look at
53125331
#if R_BIN_ELF64
5313-
r_buf_read_at (eo->b, rvaddr, (ut8*)&pltptr, 8);
5332+
r_buf_read_at (eo->b, rvaddr, (ut8*)&pltptr, 8);
53145333
#else
53155334
ut32 n32 = 0;
53165335
r_buf_read_at (eo->b, rvaddr, (ut8*)&n32, 4);
@@ -5334,7 +5353,6 @@ static bool reloc_fill_local_address(ELFOBJ *eo) {
53345353
#else
53355354
index++;
53365355
#endif
5337-
// TODO: if (reloc->type == 22) { // on arm! // extra check of bounds
53385356
ut64 naddr = baddr + pltptr + (index * 12) + 0x20;
53395357
if (reloc->type == 1026) {
53405358
naddr = baddr + pltptr + (index * 16) + 64 - 16;
@@ -5345,6 +5363,10 @@ static bool reloc_fill_local_address(ELFOBJ *eo) {
53455363
} else {
53465364
R_LOG_DEBUG ("Cannot resolve reloc reference");
53475365
}
5366+
} else {
5367+
index++;
5368+
ut64 naddr = baddr + (index * 12) + 0x20;
5369+
reloc->laddr = naddr;
53485370
}
53495371
}
53505372
}

libr/bin/p/bin_elf.inc.c

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -567,18 +567,18 @@ static RBinReloc *reloc_convert(ELFOBJ* eo, RBinElfReloc *rel, ut64 got_addr) {
567567
case R_ARM_GOTOFF: ADD(32,-(st64)G); break;
568568
case R_ARM_GOTPC: ADD(32, G - P); break;
569569
case R_ARM_CALL: // ADD(24, got_addr -P);
570-
// eprintf ("CAL %llx\n", got_addr);
571-
// eprintf ("CAL %llx\n", P);
572-
// SET(24);
573-
// P = address of bl instruction to patch
574570
r->type = R_BIN_RELOC_24;
575571
if (G == UT64_MAX) {
576572
r->addend = B-P; // 171295;
577-
eprintf( "jeje 0x%x 0x%x\n", P, B);
573+
eprintf( "jeje 0x%x 0x%x\n", P, got_addr);
578574
} else {
575+
eprintf( "joje 0x%x 0x%x\n", P, got_addr);
579576
r->addend = got_addr -P;
580577
}
581-
rel->addend = r->addend;
578+
// r->addend = 0x08004dad;
579+
r->addend = 0x00004dad;
580+
// rel->laddr += 685182;
581+
rel->addend = r->addend + rel->laddr;
582582
// rel->addend = 685182 /4; // 171295
583583
r->additive = DT_RELA;
584584
return r;
@@ -746,7 +746,13 @@ static RList* relocs(RBinFile *bf) {
746746
if (got_addr == UT64_MAX && eo->ehdr.e_type == ET_REL) {
747747
got_addr = Elf_(get_section_addr) (eo, ".got.r2");
748748
}
749-
749+
#if 0
750+
if (got_addr == UT64_MAX) {
751+
// XXX
752+
got_addr = Elf_(get_section_addr) (eo, ".debug_info");
753+
// got_addr = 0x08001e60;
754+
}
755+
#endif
750756
const RVector *relocs = Elf_(load_relocs) (eo);
751757
if (!relocs) {
752758
return ret;
@@ -761,9 +767,9 @@ static RList* relocs(RBinFile *bf) {
761767
r_vector_foreach (relocs, reloc) {
762768
RBinReloc *already_inserted = ht_up_find (reloc_ht, reloc->rva, NULL);
763769
if (already_inserted) {
770+
R_LOG_DEBUG ("Reloc already inserted at 0x%08"PFMT64x, reloc->rva);
764771
continue;
765772
}
766-
767773
RBinReloc *ptr = reloc_convert (eo, reloc, got_addr);
768774
if (ptr && ptr->paddr != UT64_MAX) {
769775
r_list_append (ret, ptr);
@@ -795,23 +801,22 @@ static void _patch_reloc(ELFOBJ *bo, ut16 e_machine, RIOBind *iob, RBinElfReloc
795801
case EM_S390:
796802
switch (rel->type) {
797803
case R_390_GLOB_DAT: // globals
798-
iob->overlay_write_at (iob->io, rel->rva, buf, 8);
799-
break;
800-
case R_390_RELATIVE:
804+
case R_390_RELATIVE: // pic
801805
iob->overlay_write_at (iob->io, rel->rva, buf, 8);
802806
break;
803807
}
804808
break;
805809
case EM_ARM:
806810
if (rel->type == R_ARM_CALL) {
811+
eprintf ("one\n");
807812
// read original bytes of the "bl" instruction
808813
iob->read_at (iob->io, rel->rva, buf, 4);
809814
V = r_read_le32 (buf);
810815

811816
int delta = A;
812-
if (rel->rva == 0x08001ec8) {
817+
// if (rel->rva == 0x08001ec8) {
813818
eprintf ("DELTA = %llx\n",A);
814-
}
819+
// }
815820
delta &= 0xfffff;
816821
#if 0
817822
if (rel->rva == 0x08001ec8) {
@@ -995,7 +1000,7 @@ static void _patch_reloc(ELFOBJ *bo, ut16 e_machine, RIOBind *iob, RBinElfReloc
9951000
V = B + A;
9961001
break;
9971002
default:
998-
//eprintf ("relocation %d not handle at this time\n", rel->type);
1003+
R_LOG_WARN ("relocation %d not handle at this time", rel->type);
9991004
break;
10001005
}
10011006
switch (word) {

0 commit comments

Comments
 (0)