From 6ddb5b347766df2dd0c88f7449d0adf4995e1f2a Mon Sep 17 00:00:00 2001 From: Yang Liu Date: Fri, 16 Aug 2024 02:14:33 +0800 Subject: [RV64,LA64_DYNAREC] Small change when handling long CC INT 3 opcode (#1736) --- src/dynarec/la64/dynarec_la64_00.c | 4 +++- src/dynarec/rv64/dynarec_rv64_00_3.c | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) (limited to 'src/dynarec') diff --git a/src/dynarec/la64/dynarec_la64_00.c b/src/dynarec/la64/dynarec_la64_00.c index 90824596..b2ceb004 100644 --- a/src/dynarec/la64/dynarec_la64_00.c +++ b/src/dynarec/la64/dynarec_la64_00.c @@ -1645,11 +1645,13 @@ uintptr_t dynarec64_00(dynarec_la64_t* dyn, uintptr_t addr, uintptr_t ip, int ni TABLE64(x1, (uintptr_t)my_context); MOV32w(x2, offsetof(box64context_t, signals[SIGTRAP])); LDX_D(x3, x1, x2); - CBZ_NEXT(x3); + BEQZ_MARK(x3); GETIP(addr); STORE_XEMU_CALL(); CALL(native_int3, -1); LOAD_XEMU_CALL(); + MARK; + jump_to_epilog(dyn, addr, 0, ninst); *need_epilog = 0; *ok = 0; } diff --git a/src/dynarec/rv64/dynarec_rv64_00_3.c b/src/dynarec/rv64/dynarec_rv64_00_3.c index f5fd7f9c..68a6f5f3 100644 --- a/src/dynarec/rv64/dynarec_rv64_00_3.c +++ b/src/dynarec/rv64/dynarec_rv64_00_3.c @@ -428,11 +428,13 @@ uintptr_t dynarec64_00_3(dynarec_rv64_t* dyn, uintptr_t addr, uintptr_t ip, int MOV32w(x2, offsetof(box64context_t, signals[SIGTRAP])); ADD(x2, x2, x1); LD(x3, x2, 0); - CBZ_NEXT(x3); + BEQZ_MARK(x3); GETIP(addr); STORE_XEMU_CALL(x3); CALL(native_int3, -1); LOAD_XEMU_CALL(); + MARK; + jump_to_epilog(dyn, addr, 0, ninst); *need_epilog = 0; *ok = 0; } -- cgit 1.4.1 From dda4f48397a1b44f1e23382a9418068d5630b1c7 Mon Sep 17 00:00:00 2001 From: Yang Liu Date: Fri, 16 Aug 2024 18:09:00 +0800 Subject: [RV64_DYNAREC] Added 66 0F 3A 63 PCMPISTRI opcode (#1735) --- src/dynarec/rv64/dynarec_rv64_660f.c | 39 +++++++++++++++++++++++++++++++----- src/dynarec/rv64/rv64_emitter.h | 10 ++++----- 2 files changed, 39 insertions(+), 10 deletions(-) (limited to 'src/dynarec') diff --git a/src/dynarec/rv64/dynarec_rv64_660f.c b/src/dynarec/rv64/dynarec_rv64_660f.c index fedcdd54..00c9c66c 100644 --- a/src/dynarec/rv64/dynarec_rv64_660f.c +++ b/src/dynarec/rv64/dynarec_rv64_660f.c @@ -927,10 +927,8 @@ uintptr_t dynarec64_660F(dynarec_rv64_t* dyn, uintptr_t addr, uintptr_t ip, int sse_reflect_reg(dyn, ninst, x6, ed); ADDI(x1, xEmu, offsetof(x64emu_t, xmm[ed])); } else { - addr = geted(dyn, addr, ninst, nextop, &wback, x1, x2, &fixedaddress, rex, NULL, 0, 1); - if (ed != x1) { - MV(x1, ed); - } + addr = geted(dyn, addr, ninst, nextop, &ed, x1, x2, &fixedaddress, rex, NULL, 0, 1); + if (ed != x1) MV(x1, ed); } // prepare rest arguments MV(x2, xRDX); @@ -948,7 +946,7 @@ uintptr_t dynarec64_660F(dynarec_rv64_t* dyn, uintptr_t addr, uintptr_t ip, int ADDI(x2, xZR, 31); SUB(xRCX, x2, xRCX); } else { - CTZxw(xRCX, xRCX, 0, x1, x2); + CTZxw(xRCX, x1, 0, x2, x3); } break; case 0xDB: @@ -1351,6 +1349,37 @@ uintptr_t dynarec64_660F(dynarec_rv64_t* dyn, uintptr_t addr, uintptr_t ip, int MOV32w(x4, u8); CALL(native_pclmul, -1); break; + case 0x63: + INST_NAME("PCMPISTRI Gx, Ex, Ib"); + SETFLAGS(X_ALL, SF_SET_DF); + nextop = F8; + GETG; + sse_reflect_reg(dyn, ninst, x6, gd); + ADDI(x2, xEmu, offsetof(x64emu_t, xmm[gd])); + if (MODREG) { + ed = (nextop & 7) + (rex.b << 3); + sse_reflect_reg(dyn, ninst, x6, ed); + ADDI(x1, xEmu, offsetof(x64emu_t, xmm[ed])); + } else { + addr = geted(dyn, addr, ninst, nextop, &ed, x1, x2, &fixedaddress, rex, NULL, 0, 1); + if (ed != x1) MV(x1, ed); + } + u8 = F8; + MOV32w(x3, u8); + CALL(sse42_compare_string_implicit_len, x1); + ZEROUP(x1); + BNEZ_MARK(x1); + MOV32w(xRCX, (u8 & 1) ? 8 : 16); + B_NEXT_nocond; + MARK; + if (u8 & 0b1000000) { + CLZxw(xRCX, x1, 0, x2, x3, x4); + ADDI(x2, xZR, 31); + SUB(xRCX, x2, xRCX); + } else { + CTZxw(xRCX, x1, 0, x2, x3); + } + break; case 0xDF: INST_NAME("AESKEYGENASSIST Gx, Ex, Ib"); // AES-NI nextop = F8; diff --git a/src/dynarec/rv64/rv64_emitter.h b/src/dynarec/rv64/rv64_emitter.h index abf718df..55384ed7 100644 --- a/src/dynarec/rv64/rv64_emitter.h +++ b/src/dynarec/rv64/rv64_emitter.h @@ -796,14 +796,14 @@ f28–31 ft8–11 FP temporaries Caller else \ CTZW(rd, rs); \ } else { \ - NEG(s2, ed); \ - AND(s2, s2, ed); \ - TABLE64(x3, 0x03f79d71b4ca8b09ULL); \ - MUL(s2, s2, x3); \ + NEG(s2, rs); \ + AND(s2, s2, rs); \ + TABLE64(s1, 0x03f79d71b4ca8b09ULL); \ + MUL(s2, s2, s1); \ SRLI(s2, s2, 64 - 6); \ TABLE64(s1, (uintptr_t) & deBruijn64tab); \ ADD(s1, s1, s2); \ - LBU(gd, s1, 0); \ + LBU(rd, s1, 0); \ } // Count set bits -- cgit 1.4.1 From bc0b5b118115d8b220fd16d03e2729d2fe3ebf2d Mon Sep 17 00:00:00 2001 From: Yang Liu Date: Fri, 16 Aug 2024 18:10:17 +0800 Subject: [LA64_DYNAREC] Port recent RV64 fixes (#1739) --- src/dynarec/la64/dynarec_la64_00.c | 9 ++++--- src/dynarec/la64/dynarec_la64_0f.c | 40 +++++++++++++++++++---------- src/dynarec/la64/dynarec_la64_emit_shift.c | 41 +++++++++++++----------------- src/dynarec/la64/dynarec_la64_helper.c | 6 ++--- src/dynarec/la64/dynarec_la64_helper.h | 2 +- 5 files changed, 55 insertions(+), 43 deletions(-) (limited to 'src/dynarec') diff --git a/src/dynarec/la64/dynarec_la64_00.c b/src/dynarec/la64/dynarec_la64_00.c index b2ceb004..c6145cf4 100644 --- a/src/dynarec/la64/dynarec_la64_00.c +++ b/src/dynarec/la64/dynarec_la64_00.c @@ -1664,16 +1664,17 @@ uintptr_t dynarec64_00(dynarec_la64_t* dyn, uintptr_t addr, uintptr_t ip, int ni case 5: if (opcode == 0xD0) { INST_NAME("SHR Eb, 1"); + GETEB(x1, 0); MOV32w(x2, 1); } else { INST_NAME("SHR Eb, CL"); + GETEB(x1, 0); ANDI(x2, xRCX, 0x1F); BEQ_NEXT(x2, xZR); } SETFLAGS(X_ALL, SF_SET_PENDING); // some flags are left undefined if (box64_dynarec_safeflags > 1) MAYSETFLAGS(); - GETEB(x1, 0); emit_shr8(dyn, ninst, x1, x2, x5, x4, x6); EBBACK(); break; @@ -1733,7 +1734,8 @@ uintptr_t dynarec64_00(dynarec_la64_t* dyn, uintptr_t addr, uintptr_t ip, int ni INST_NAME("ROL Ed, CL"); SETFLAGS(X_OF | X_CF, SF_SUBSET); GETED(0); - emit_rol32(dyn, ninst, rex, ed, xRCX, x3, x4); + ANDI(x6, xRCX, rex.w ? 0x3f : 0x1f); + emit_rol32(dyn, ninst, rex, ed, x6, x3, x4); WBACK; if (!wback && !rex.w) ZEROUP(ed); break; @@ -1741,7 +1743,8 @@ uintptr_t dynarec64_00(dynarec_la64_t* dyn, uintptr_t addr, uintptr_t ip, int ni INST_NAME("ROR Ed, CL"); SETFLAGS(X_OF | X_CF, SF_SUBSET); GETED(0); - emit_ror32(dyn, ninst, rex, ed, xRCX, x3, x4); + ANDI(x6, xRCX, rex.w ? 0x3f : 0x1f); + emit_ror32(dyn, ninst, rex, ed, x6, x3, x4); WBACK; if (!wback && !rex.w) ZEROUP(ed); break; diff --git a/src/dynarec/la64/dynarec_la64_0f.c b/src/dynarec/la64/dynarec_la64_0f.c index 0f795e61..7a0fbf7e 100644 --- a/src/dynarec/la64/dynarec_la64_0f.c +++ b/src/dynarec/la64/dynarec_la64_0f.c @@ -669,12 +669,17 @@ uintptr_t dynarec64_0F(dynarec_la64_t* dyn, uintptr_t addr, uintptr_t ip, int ni case 0xA4: nextop = F8; INST_NAME("SHLD Ed, Gd, Ib"); - SETFLAGS(X_ALL, SF_SET_PENDING); - GETED(1); - GETGD; - u8 = F8; - emit_shld32c(dyn, ninst, rex, ed, gd, u8, x3, x4); - WBACK; + if (geted_ib(dyn, addr, ninst, nextop)) { + SETFLAGS(X_ALL, SF_SET_PENDING); + GETED(1); + GETGD; + u8 = F8; + emit_shld32c(dyn, ninst, rex, ed, gd, u8, x3, x4); + WBACK; + } else { + FAKEED; + F8; + } break; case 0xAB: INST_NAME("BTS Ed, Gd"); @@ -716,13 +721,18 @@ uintptr_t dynarec64_0F(dynarec_la64_t* dyn, uintptr_t addr, uintptr_t ip, int ni case 0xAC: nextop = F8; INST_NAME("SHRD Ed, Gd, Ib"); - SETFLAGS(X_ALL, SF_SET_PENDING); - GETED(1); - GETGD; - u8 = F8; - u8 &= (rex.w ? 0x3f : 0x1f); - emit_shrd32c(dyn, ninst, rex, ed, gd, u8, x3, x4); - WBACK; + if (geted_ib(dyn, addr, ninst, nextop)) { + SETFLAGS(X_ALL, SF_SET_PENDING); + GETED(1); + GETGD; + u8 = F8; + u8 &= (rex.w ? 0x3f : 0x1f); + emit_shrd32c(dyn, ninst, rex, ed, gd, u8, x3, x4); + WBACK; + } else { + FAKEED; + F8; + } break; case 0xAE: nextop = F8; @@ -916,6 +926,8 @@ uintptr_t dynarec64_0F(dynarec_la64_t* dyn, uintptr_t addr, uintptr_t ip, int ni if (wback) { SDxw(ed, wback, fixedaddress); SMWRITE(); + } else if (!rex.w) { + ZEROUP(ed); } MARK; break; @@ -935,6 +947,8 @@ uintptr_t dynarec64_0F(dynarec_la64_t* dyn, uintptr_t addr, uintptr_t ip, int ni if (wback) { SDxw(ed, wback, fixedaddress); SMWRITE(); + } else if (!rex.w) { + ZEROUP(ed); } MARK; break; diff --git a/src/dynarec/la64/dynarec_la64_emit_shift.c b/src/dynarec/la64/dynarec_la64_emit_shift.c index 291674eb..2b83ccba 100644 --- a/src/dynarec/la64/dynarec_la64_emit_shift.c +++ b/src/dynarec/la64/dynarec_la64_emit_shift.c @@ -514,7 +514,7 @@ void emit_sar16(dynarec_la64_t* dyn, int ninst, int s1, int s2, int s3, int s4, IFX (X_PEND) { ST_H(s2, xEmu, offsetof(x64emu_t, op2)); ST_H(s1, xEmu, offsetof(x64emu_t, op1)); - SET_DF(s4, d_sar8); + SET_DF(s4, d_sar16); } else IFX (X_ALL) { SET_DFNONE(); } @@ -612,6 +612,8 @@ void emit_sar32c(dynarec_la64_t* dyn, int ninst, rex_t rex, int s1, uint32_t c, } OR(xFlags, xFlags, s3); } + // For the SAR instruction, the OF flag is cleared for all 1-bit shifts. + // OF nop if (rex.w) { SRAI_D(s1, s1, c); @@ -714,17 +716,11 @@ void emit_rol32(dynarec_la64_t* dyn, int ninst, rex_t rex, int s1, int s2, int s X64_ROTL_W(s1, s2); } - if (rex.w) { - ANDI(s4, s2, 0x3f); - } else { - ANDI(s4, s2, 0x1f); - } - if (!rex.w) ZEROUP(s1); - BEQ_NEXT(s4, xZR); + BEQ_NEXT(s2, xZR); - SLLxw(s3, s1, s4); - NEG_D(s4, s4); + SLLxw(s3, s1, s2); + NEG_D(s4, s2); ADDI_D(s4, s4, rex.w ? 64 : 32); SRLxw(s1, s1, s4); OR(s1, s3, s1); @@ -745,12 +741,13 @@ void emit_rol32(dynarec_la64_t* dyn, int ninst, rex_t rex, int s1, int s2, int s } IFX (X_OF) { // the OF flag is set to the exclusive OR of the CF bit (after the rotate) and the most-significant bit of the result. - ADDI_D(s3, xZR, 1); - BNE_NEXT(s2, s3); + ADDI_D(s3, s2, -1); + BNEZ_MARK(s3); SRLIxw(s3, s1, rex.w ? 63 : 31); XOR(s3, s3, s4); // s3: MSB, s4: CF bit SLLI_D(s3, s3, F_OF); OR(xFlags, xFlags, s3); + MARK; } } @@ -759,13 +756,10 @@ void emit_ror32(dynarec_la64_t* dyn, int ninst, rex_t rex, int s1, int s2, int s { int64_t j64; - if (rex.w) { - ANDI(s4, s2, 0x3f); - } else { - ANDI(s4, s2, 0x1f); + if (!rex.w) { ZEROUP(s1); } - BEQ_NEXT(s4, xZR); + BEQ_NEXT(s2, xZR); IFX (X_PEND) { SDxw(s2, xEmu, offsetof(x64emu_t, op2)); SET_DF(s4, rex.w ? d_ror64 : d_ror32); @@ -775,15 +769,15 @@ void emit_ror32(dynarec_la64_t* dyn, int ninst, rex_t rex, int s1, int s2, int s IFXA ((X_CF | X_OF), la64_lbt) { if (rex.w) - X64_ROTR_D(s1, s4); + X64_ROTR_D(s1, s2); else - X64_ROTR_W(s1, s4); + X64_ROTR_W(s1, s2); } if (rex.w) { - ROTR_D(s1, s1, s4); + ROTR_D(s1, s1, s2); } else { - ROTR_W(s1, s1, s4); + ROTR_W(s1, s1, s2); } IFX (X_PEND) { @@ -802,14 +796,15 @@ void emit_ror32(dynarec_la64_t* dyn, int ninst, rex_t rex, int s1, int s2, int s } IFX (X_OF) { // the OF flag is set to the exclusive OR of the two most-significant bits of the result - ADDI_D(s3, xZR, 1); - BNE_NEXT(s2, s3); + ADDI_D(s3, s2, -1); + BNEZ_MARK(s3); SRLIxw(s3, s1, rex.w ? 63 : 31); SRLIxw(s4, s1, rex.w ? 62 : 30); XOR(s3, s3, s4); ANDI(s3, s3, 1); SLLI_D(s3, s3, F_OF); OR(xFlags, xFlags, s3); + MARK; } } diff --git a/src/dynarec/la64/dynarec_la64_helper.c b/src/dynarec/la64/dynarec_la64_helper.c index b5b721fe..b597cfa6 100644 --- a/src/dynarec/la64/dynarec_la64_helper.c +++ b/src/dynarec/la64/dynarec_la64_helper.c @@ -133,7 +133,7 @@ uintptr_t geted(dynarec_la64_t* dyn, uintptr_t addr, int ninst, uint8_t nextop, i64 = F32S; else i64 = F8S; - if (i64 == 0 || ((i64 >= -2048) && (i64 <= 2047) && i12)) { + if (i64 == 0 || ((i64 >= -2048) && (i64 <= maxval) && i12)) { *fixaddress = i64; if ((nextop & 7) == 4) { if (sib_reg != 4) { @@ -265,7 +265,7 @@ static uintptr_t geted_32(dynarec_la64_t* dyn, uintptr_t addr, int ninst, uint8_ i32 = F32S; else i32 = F8S; - if (i32 == 0 || ((i32 >= -2048) && (i32 <= 2047) && i12)) { + if (i32 == 0 || ((i32 >= -2048) && (i32 <= maxval) && i12)) { *fixaddress = i32; if ((nextop & 7) == 4) { if (sib_reg != 4) { @@ -413,7 +413,7 @@ uintptr_t geted32(dynarec_la64_t* dyn, uintptr_t addr, int ninst, uint8_t nextop i64 = F32S; else i64 = F8S; - if (i64 == 0 || ((i64 >= -2048) && (i64 <= 2047) && i12)) { + if (i64 == 0 || ((i64 >= -2048) && (i64 <= maxval) && i12)) { *fixaddress = i64; if ((nextop & 7) == 4) { if (sib_reg != 4) { diff --git a/src/dynarec/la64/dynarec_la64_helper.h b/src/dynarec/la64/dynarec_la64_helper.h index 4e2d8653..9c1f6aa5 100644 --- a/src/dynarec/la64/dynarec_la64_helper.h +++ b/src/dynarec/la64/dynarec_la64_helper.h @@ -279,7 +279,7 @@ ed = i; \ } else { \ SMREAD(); \ - addr = geted(dyn, addr, ninst, nextop, &wback, x2, x3, &fixedaddress, rex, NULL, 1, D); \ + addr = geted(dyn, addr, ninst, nextop, &wback, x3, x2, &fixedaddress, rex, NULL, 1, D); \ LD_B(i, wback, fixedaddress); \ wb1 = 1; \ ed = i; \ -- cgit 1.4.1 From 0f1bce0f32a8429fc414f92cb6b85ff0d30b5d8f Mon Sep 17 00:00:00 2001 From: Yang Liu Date: Sat, 17 Aug 2024 00:16:20 +0800 Subject: [RV64_DYNAREC] Added more opcodes (#1740) * [RV64_DYNAREC] Added more opcodes * more opcodes * more --- src/dynarec/arm64/dynarec_arm64_660f.c | 4 +- src/dynarec/rv64/dynarec_rv64_0f.c | 33 +++++---- src/dynarec/rv64/dynarec_rv64_660f.c | 124 ++++++++++++++++++++++++--------- src/dynarec/rv64/dynarec_rv64_f20f.c | 31 +++++++++ src/emu/x64run660f.c | 4 +- 5 files changed, 147 insertions(+), 49 deletions(-) (limited to 'src/dynarec') diff --git a/src/dynarec/arm64/dynarec_arm64_660f.c b/src/dynarec/arm64/dynarec_arm64_660f.c index a8625e23..c967374d 100644 --- a/src/dynarec/arm64/dynarec_arm64_660f.c +++ b/src/dynarec/arm64/dynarec_arm64_660f.c @@ -1006,7 +1006,7 @@ uintptr_t dynarec64_660F(dynarec_arm_t* dyn, uintptr_t addr, uintptr_t ip, int n VMOVeD(q0, 0, v1, 0); break; case 0x0C: - INST_NAME("PBLENDPS Gx, Ex, Ib"); + INST_NAME("BLENDPS Gx, Ex, Ib"); nextop = F8; GETGX(q0, 1); GETEX(q1, 0, 1); @@ -1021,7 +1021,7 @@ uintptr_t dynarec64_660F(dynarec_arm_t* dyn, uintptr_t addr, uintptr_t ip, int n } break; case 0x0D: - INST_NAME("PBLENDPD Gx, Ex, Ib"); + INST_NAME("BLENDPD Gx, Ex, Ib"); nextop = F8; GETGX(q0, 1); GETEX(q1, 0, 1); diff --git a/src/dynarec/rv64/dynarec_rv64_0f.c b/src/dynarec/rv64/dynarec_rv64_0f.c index e3af9d94..0c691c05 100644 --- a/src/dynarec/rv64/dynarec_rv64_0f.c +++ b/src/dynarec/rv64/dynarec_rv64_0f.c @@ -66,21 +66,26 @@ uintptr_t dynarec64_0F(dynarec_rv64_t* dyn, uintptr_t addr, uintptr_t ip, int ni if(MODREG) { switch(nextop) { case 0xD0: - //TODO - DEFAULT; - /*INST_NAME("FAKE xgetbv"); - nextop = F8; - addr = fakeed(dyn, addr, ninst, nextop); - SETFLAGS(X_ALL, SF_SET_NODF); // Hack to set flags in "don't care" state - GETIP(ip); - STORE_XEMU_CALL(x3); - CALL(native_ud, -1); - LOAD_XEMU_CALL(); - jump_to_epilog(dyn, 0, xRIP, ninst); - *need_epilog = 0; - *ok = 0;*/ + INST_NAME("XGETBV"); + AND(x1, xRCX, xMASK); + BEQZ_MARK(x1); + UDF(); + MARK; + MOV32w(xRAX, 0b111); + MOV32w(xRDX, 0); + break; + case 0xE0: + case 0xE1: + case 0xE2: + case 0xE3: + case 0xE4: + case 0xE5: + case 0xE6: + case 0xE7: + INST_NAME("SMSW Ed"); + ed = xRAX + (nextop & 7) + (rex.b << 3); + MOV32w(ed, (1 << 0) | (1 << 4)); // only PE and ET set... break; - case 0xF9: INST_NAME("RDTSCP"); NOTEST(x1); diff --git a/src/dynarec/rv64/dynarec_rv64_660f.c b/src/dynarec/rv64/dynarec_rv64_660f.c index 00c9c66c..8fb7734d 100644 --- a/src/dynarec/rv64/dynarec_rv64_660f.c +++ b/src/dynarec/rv64/dynarec_rv64_660f.c @@ -684,6 +684,13 @@ uintptr_t dynarec64_660F(dynarec_rv64_t* dyn, uintptr_t addr, uintptr_t ip, int SD(x3, gback, gdoffset + i * 8); } break; + case 0x29: + INST_NAME("PCMPEQQ Gx, Ex"); // SSE4 opcode! + nextop = F8; + GETGX(); + GETEX(x2, 0, 8); + SSE_LOOP_Q(x3, x4, XOR(x3, x3, x4); SNEZ(x3, x3); ADDI(x3, x3, -1)); + break; case 0x2B: INST_NAME("PACKUSDW Gx, Ex"); nextop = F8; @@ -1055,37 +1062,6 @@ uintptr_t dynarec64_660F(dynarec_rv64_t* dyn, uintptr_t addr, uintptr_t ip, int case 0x3A: // these are some more SSSE3+ opcodes opcode = F8; switch (opcode) { - case 0x0B: - INST_NAME("ROUNDSD Gx, Ex, Ib"); - nextop = F8; - GETEXSD(d0, 1); - GETGXSD_empty(v0); - d1 = fpu_get_scratch(dyn); - v1 = fpu_get_scratch(dyn); - u8 = F8; - FEQD(x2, d0, d0); - BNEZ_MARK(x2); - if (v0 != d0) FMVD(v0, d0); - B_NEXT_nocond; - MARK; // d0 is not nan - FABSD(v1, d0); - MOV64x(x3, 1ULL << __DBL_MANT_DIG__); - FCVTDL(d1, x3, RD_RTZ); - FLTD(x3, v1, d1); - BNEZ_MARK2(x3); - if (v0 != d0) FMVD(v0, d0); - B_NEXT_nocond; - MARK2; - if (u8 & 4) { - u8 = sse_setround(dyn, ninst, x4, x2); - FCVTLD(x5, d0, RD_DYN); - FCVTDL(v0, x5, RD_RTZ); - x87_restoreround(dyn, ninst, u8); - } else { - FCVTLD(x5, d0, round_round[u8 & 3]); - FCVTDL(v0, x5, RD_RTZ); - } - break; case 0x09: INST_NAME("ROUNDPD Gx, Ex, Ib"); nextop = F8; @@ -1142,6 +1118,80 @@ uintptr_t dynarec64_660F(dynarec_rv64_t* dyn, uintptr_t addr, uintptr_t ip, int MARK2; FSD(d0, gback, gdoffset + 8); break; + case 0x0A: + INST_NAME("ROUNDSS Gx, Ex, Ib"); + nextop = F8; + GETEXSS(d0, 1); + GETGXSS_empty(v0); + d1 = fpu_get_scratch(dyn); + v1 = fpu_get_scratch(dyn); + u8 = F8; + FEQS(x2, d0, d0); + BNEZ_MARK(x2); + if (v0 != d0) FMVS(v0, d0); + B_NEXT_nocond; + MARK; // d0 is not nan + FABSS(v1, d0); + MOV64x(x3, 1ULL << __FLT_MANT_DIG__); + FCVTSW(d1, x3, RD_RTZ); + FLTS(x3, v1, d1); + BNEZ_MARK2(x3); + if (v0 != d0) FMVS(v0, d0); + B_NEXT_nocond; + MARK2; + if (u8 & 4) { + u8 = sse_setround(dyn, ninst, x4, x2); + FCVTWS(x5, d0, RD_DYN); + FCVTSW(v0, x5, RD_RTZ); + x87_restoreround(dyn, ninst, u8); + } else { + FCVTWS(x5, d0, round_round[u8 & 3]); + FCVTSW(v0, x5, RD_RTZ); + } + break; + case 0x0B: + INST_NAME("ROUNDSD Gx, Ex, Ib"); + nextop = F8; + GETEXSD(d0, 1); + GETGXSD_empty(v0); + d1 = fpu_get_scratch(dyn); + v1 = fpu_get_scratch(dyn); + u8 = F8; + FEQD(x2, d0, d0); + BNEZ_MARK(x2); + if (v0 != d0) FMVD(v0, d0); + B_NEXT_nocond; + MARK; // d0 is not nan + FABSD(v1, d0); + MOV64x(x3, 1ULL << __DBL_MANT_DIG__); + FCVTDL(d1, x3, RD_RTZ); + FLTD(x3, v1, d1); + BNEZ_MARK2(x3); + if (v0 != d0) FMVD(v0, d0); + B_NEXT_nocond; + MARK2; + if (u8 & 4) { + u8 = sse_setround(dyn, ninst, x4, x2); + FCVTLD(x5, d0, RD_DYN); + FCVTDL(v0, x5, RD_RTZ); + x87_restoreround(dyn, ninst, u8); + } else { + FCVTLD(x5, d0, round_round[u8 & 3]); + FCVTDL(v0, x5, RD_RTZ); + } + break; + case 0x0C: + INST_NAME("BLENDPS Gx, Ex, Ib"); + nextop = F8; + GETGX(); + GETEX(x2, 1, 12); + u8 = F8 & 0b1111; + for (int i = 0; i < 4; ++i) + if (u8 & (1 << i)) { + LWU(x1, wback, fixedaddress + i * 4); + SW(x1, gback, gdoffset + i * 4); + } + break; case 0x0E: INST_NAME("PBLENDW Gx, Ex, Ib"); nextop = F8; @@ -1265,6 +1315,18 @@ uintptr_t dynarec64_660F(dynarec_rv64_t* dyn, uintptr_t addr, uintptr_t ip, int SMWRITE2(); } break; + case 0x17: + INST_NAME("EXTRACTPS Ew, Gx, Ib"); + nextop = F8; + GETGX(); + GETED(1); + u8 = F8; + LWU(ed, gback, gdoffset + 4 * (u8 & 3)); + if (wback) { + SW(ed, wback, fixedaddress); + SMWRITE2(); + } + break; case 0x20: INST_NAME("PINSRB Gx, ED, Ib"); nextop = F8; diff --git a/src/dynarec/rv64/dynarec_rv64_f20f.c b/src/dynarec/rv64/dynarec_rv64_f20f.c index a7942c7e..73fea164 100644 --- a/src/dynarec/rv64/dynarec_rv64_f20f.c +++ b/src/dynarec/rv64/dynarec_rv64_f20f.c @@ -416,6 +416,37 @@ uintptr_t dynarec64_F20F(dynarec_rv64_t* dyn, uintptr_t addr, uintptr_t ip, int NEG(x2, x2); FMVDX(d0, x2); break; + case 0xD0: + INST_NAME("ADDSUBPS Gx, Ex"); + nextop = F8; + GETGX(); + GETEX(x2, 0, 12); + d0 = fpu_get_scratch(dyn); + d1 = fpu_get_scratch(dyn); + // GX->f[0] -= EX->f[0]; + FLW(d0, wback, fixedaddress + 0); + FLW(d1, gback, gdoffset + 0); + FSUBS(d1, d1, d0); + FSW(d1, gback, gdoffset + 0); + + // GX->f[1] += EX->f[1]; + FLW(d0, wback, fixedaddress + 4); + FLW(d1, gback, gdoffset + 4); + FADDS(d1, d1, d0); + FSW(d1, gback, gdoffset + 4); + + // GX->f[2] -= EX->f[2]; + FLW(d0, wback, fixedaddress + 8); + FLW(d1, gback, gdoffset + 8); + FSUBS(d1, d1, d0); + FSW(d1, gback, gdoffset + 8); + + // GX->f[3] += EX->f[3]; + FLW(d0, wback, fixedaddress + 12); + FLW(d1, gback, gdoffset + 12); + FADDS(d1, d1, d0); + FSW(d1, gback, gdoffset + 12); + break; case 0xE6: INST_NAME("CVTPD2DQ Gx, Ex"); nextop = F8; diff --git a/src/emu/x64run660f.c b/src/emu/x64run660f.c index 84ed4e25..35fd6eab 100644 --- a/src/emu/x64run660f.c +++ b/src/emu/x64run660f.c @@ -966,7 +966,7 @@ uintptr_t Run660F(x64emu_t *emu, rex_t rex, uintptr_t addr) } break; - case 0x0C: /* PBLENDPS Gx, Ex, Ib */ + case 0x0C: /* BLENDPS Gx, Ex, Ib */ nextop = F8; GETEX(1); GETGX; @@ -976,7 +976,7 @@ uintptr_t Run660F(x64emu_t *emu, rex_t rex, uintptr_t addr) GX->ud[i] = EX->ud[i]; } break; - case 0x0D: /* PBLENDPD Gx, Ex, Ib */ + case 0x0D: /* BLENDPD Gx, Ex, Ib */ nextop = F8; GETEX(1); GETGX; -- cgit 1.4.1 From fa87b0fcef3dff593a507b3200ef83d846101d64 Mon Sep 17 00:00:00 2001 From: ptitSeb Date: Sat, 17 Aug 2024 12:24:26 +0200 Subject: [BOX32] prepare elfheader_t structure for 32bits elfs --- src/dynarec/dynarec_native_functions.c | 2 +- src/elfs/elfhash.c | 119 ++++++---- src/elfs/elfload_dump.c | 96 ++++---- src/elfs/elfloader.c | 384 ++++++++++++++++++------------- src/elfs/elfloader_private.h | 54 +++-- src/elfs/elfparser.c | 78 +++---- src/elfs/elfparser64.c | 406 +++++++++++++++++++++++++++++++++ src/emu/x64int3.c | 6 +- src/include/box32.h | 112 +++++++++ src/include/elfload_dump.h | 23 +- src/include/elfloader.h | 2 +- src/include/x64run.h | 4 +- src/librarian/librarian.c | 8 +- src/mallochook.c | 48 ++-- 14 files changed, 995 insertions(+), 347 deletions(-) create mode 100644 src/elfs/elfparser64.c create mode 100644 src/include/box32.h (limited to 'src/dynarec') diff --git a/src/dynarec/dynarec_native_functions.c b/src/dynarec/dynarec_native_functions.c index 376541d1..c29e5083 100644 --- a/src/dynarec/dynarec_native_functions.c +++ b/src/dynarec/dynarec_native_functions.c @@ -608,7 +608,7 @@ int isNativeCall(dynarec_native_t* dyn, uintptr_t addr, uintptr_t* calladdress, if(!addr || !getProtection(addr)) return 0; onebridge_t *b = (onebridge_t*)(addr); - if(b->CC==0xCC && b->S=='S' && b->C=='C' && b->w!=(wrapper_t)0 && b->f!=(uintptr_t)PltResolver) { + if(b->CC==0xCC && b->S=='S' && b->C=='C' && b->w!=(wrapper_t)0 && b->f!=(uintptr_t)PltResolver64) { // found ! if(retn) *retn = (b->C3==0xC2)?b->N:0; if(calladdress) *calladdress = addr+1; diff --git a/src/elfs/elfhash.c b/src/elfs/elfhash.c index fefb0e7f..61d1af65 100644 --- a/src/elfs/elfhash.c +++ b/src/elfs/elfhash.c @@ -18,14 +18,15 @@ #include "elfload_dump.h" #include "elfloader_private.h" -const char* GetSymbolVersion(elfheader_t* h, int version) +const char* GetSymbolVersion32(elfheader_t* h, int version) { /* TODO */ return NULL; } +const char* GetSymbolVersion64(elfheader_t* h, int version) { if(version<2) return NULL; /*if(version==1) return "*";*/ - if(h->VerNeed) { - Elf64_Verneed *ver = (Elf64_Verneed*)((uintptr_t)h->VerNeed + h->delta); + if(h->VerNeed._64) { + Elf64_Verneed *ver = (Elf64_Verneed*)((uintptr_t)h->VerNeed._64 + h->delta); while(ver) { Elf64_Vernaux *aux = (Elf64_Vernaux*)((uintptr_t)ver + ver->vn_aux); for(int j=0; jvn_cnt; ++j) { @@ -38,12 +39,17 @@ const char* GetSymbolVersion(elfheader_t* h, int version) } return GetParentSymbolVersion(h, version); // if symbol is "internal", use Def table instead } +const char* GetSymbolVersion(elfheader_t* h, int version) +{ + return box64_is32bits?GetSymbolVersion32(h, version):GetSymbolVersion64(h, version); +} -const char* GetParentSymbolVersion(elfheader_t* h, int index) +const char* GetParentSymbolVersion32(elfheader_t* h, int index) { /* TODO */ return NULL; } +const char* GetParentSymbolVersion64(elfheader_t* h, int index) { - if(!h->VerDef || (index<1)) + if(!h->VerDef._64 || (index<1)) return NULL; - Elf64_Verdef *def = (Elf64_Verdef*)((uintptr_t)h->VerDef + h->delta); + Elf64_Verdef *def = (Elf64_Verdef*)((uintptr_t)h->VerDef._64 + h->delta); while(def) { if(def->vd_ndx==index) { if(def->vd_cnt<1) @@ -57,26 +63,37 @@ const char* GetParentSymbolVersion(elfheader_t* h, int index) } return NULL; } +const char* GetParentSymbolVersion(elfheader_t* h, int version) +{ + return box64_is32bits?GetParentSymbolVersion32(h, version):GetParentSymbolVersion64(h, version); +} -Elf64_Half GetParentSymbolVersionFlag(elfheader_t* h, int index) +uint16_t GetParentSymbolVersionFlag32(elfheader_t* h, int index) { /* TODO */ return (uint16_t)-1; } +uint16_t GetParentSymbolVersionFlag64(elfheader_t* h, int index) { - if(!h->VerDef || (index<1)) - return (Elf64_Half)-1; - Elf64_Verdef *def = (Elf64_Verdef*)((uintptr_t)h->VerDef + h->delta); + if(!h->VerDef._64 || (index<1)) + return (uint16_t)-1; + Elf64_Verdef *def = (Elf64_Verdef*)((uintptr_t)h->VerDef._64 + h->delta); while(def) { if(def->vd_ndx==index) { return def->vd_flags; } def = def->vd_next?((Elf64_Verdef*)((uintptr_t)def + def->vd_next)):NULL; } - return (Elf64_Half)-1; + return (uint16_t)-1; } -Elf64_Half GetSymbolVersionFlag(elfheader_t* h, int version) +uint16_t GetParentSymbolVersionFlag(elfheader_t* h, int index) +{ + return box64_is32bits?GetParentSymbolVersionFlag32(h, index):GetParentSymbolVersionFlag64(h, index); +} + +uint16_t GetSymbolVersionFlag32(elfheader_t* h, int version) { /* TODO */ return (uint16_t)-1; } +uint16_t GetSymbolVersionFlag64(elfheader_t* h, int version) { if(version<2) - return (Elf64_Half)-1; - if(h->VerNeed) { - Elf64_Verneed *ver = (Elf64_Verneed*)((uintptr_t)h->VerNeed + h->delta); + return (uint16_t)-1; + if(h->VerNeed._64) { + Elf64_Verneed *ver = (Elf64_Verneed*)((uintptr_t)h->VerNeed._64 + h->delta); while(ver) { Elf64_Vernaux *aux = (Elf64_Vernaux*)((uintptr_t)ver + ver->vn_aux); for(int j=0; jvn_cnt; ++j) { @@ -89,14 +106,18 @@ Elf64_Half GetSymbolVersionFlag(elfheader_t* h, int version) } return GetParentSymbolVersionFlag(h, version); // if symbol is "internal", use Def table instead } +uint16_t GetSymbolVersionFlag(elfheader_t* h, int index) { + return box64_is32bits?GetSymbolVersionFlag32(h, index):GetSymbolVersionFlag64(h, index); +} -int GetVersionIndice(elfheader_t* h, const char* vername) +int GetVersionIndice32(elfheader_t* h, const char* vername) { /* TODO */ return 0; } +int GetVersionIndice64(elfheader_t* h, const char* vername) { if(!vername) return 0; - if(h->VerDef) { - Elf64_Verdef *def = (Elf64_Verdef*)((uintptr_t)h->VerDef + h->delta); + if(h->VerDef._64) { + Elf64_Verdef *def = (Elf64_Verdef*)((uintptr_t)h->VerDef._64 + h->delta); while(def) { Elf64_Verdaux *aux = (Elf64_Verdaux*)((uintptr_t)def + def->vd_aux); if(!strcmp(h->DynStr+aux->vda_name, vername)) @@ -106,13 +127,18 @@ int GetVersionIndice(elfheader_t* h, const char* vername) } return 0; } +int GetVersionIndice(elfheader_t* h, const char* vername) +{ + return box64_is32bits?GetVersionIndice32(h, vername):GetVersionIndice64(h, vername); +} -int GetNeededVersionCnt(elfheader_t* h, const char* libname) +int GetNeededVersionCnt32(elfheader_t* h, const char* libname) { /* TODO */ return 0; } +int GetNeededVersionCnt64(elfheader_t* h, const char* libname) { if(!libname) return 0; - if(h->VerNeed) { - Elf64_Verneed *ver = (Elf64_Verneed*)((uintptr_t)h->VerNeed + h->delta); + if(h->VerNeed._64) { + Elf64_Verneed *ver = (Elf64_Verneed*)((uintptr_t)h->VerNeed._64 + h->delta); while(ver) { char *filename = h->DynStr + ver->vn_file; if(!strcmp(filename, libname)) @@ -122,13 +148,18 @@ int GetNeededVersionCnt(elfheader_t* h, const char* libname) } return 0; } +int GetNeededVersionCnt(elfheader_t* h, const char* libname) +{ + return box64_is32bits?GetNeededVersionCnt32(h, libname):GetNeededVersionCnt64(h, libname); +} -const char* GetNeededVersionString(elfheader_t* h, const char* libname, int idx) +const char* GetNeededVersionString32(elfheader_t* h, const char* libname, int idx) { /* TODO */ return NULL; } +const char* GetNeededVersionString64(elfheader_t* h, const char* libname, int idx) { if(!libname) - return 0; - if(h->VerNeed) { - Elf64_Verneed *ver = (Elf64_Verneed*)((uintptr_t)h->VerNeed + h->delta); + return NULL; + if(h->VerNeed._64) { + Elf64_Verneed *ver = (Elf64_Verneed*)((uintptr_t)h->VerNeed._64 + h->delta); while(ver) { char *filename = h->DynStr + ver->vn_file; Elf64_Vernaux *aux = (Elf64_Vernaux*)((uintptr_t)ver + ver->vn_aux); @@ -145,6 +176,10 @@ const char* GetNeededVersionString(elfheader_t* h, const char* libname, int idx) } return NULL; } +const char* GetNeededVersionString(elfheader_t* h, const char* libname, int idx) +{ + return box64_is32bits?GetNeededVersionString32(h, libname, idx):GetNeededVersionString64(h, libname, idx); +} int GetNeededVersionForLib(elfheader_t* h, const char* libname, const char* ver) { @@ -197,7 +232,7 @@ uint32_t old_elf_hash(const char* name) return h; } -Elf64_Sym* old_elf_lookup(elfheader_t* h, const char* symname, int ver, const char* vername, int local, int veropt) +static Elf64_Sym* old_elf_lookup(elfheader_t* h, const char* symname, int ver, const char* vername, int local, int veropt) { // Prepare hash table const uint32_t *hashtab = (uint32_t*)(h->hash + h->delta); @@ -209,16 +244,16 @@ Elf64_Sym* old_elf_lookup(elfheader_t* h, const char* symname, int ver, const ch const uint32_t hash = old_elf_hash(symname); // Search for it for (uint32_t i = buckets[hash % nbuckets]; i; i = chains[i]) { - const char* name = h->DynStr + h->DynSym[i].st_name; + const char* name = h->DynStr + h->DynSym._64[i].st_name; if (!strcmp(symname, name) && SymbolMatch(h, i, ver, vername, local, veropt)) { - return &h->DynSym[i]; + return &h->DynSym._64[i]; } } return NULL; } -void old_elf_hash_dump(elfheader_t* h) +static void old_elf_hash_dump(elfheader_t* h) { // Prepare hash table const uint32_t *hashtab = (uint32_t*)(h->hash + h->delta); @@ -229,7 +264,7 @@ void old_elf_hash_dump(elfheader_t* h) printf_log(LOG_NONE, "------------ Dump HASH from %s\n", h->name); printf_log(LOG_NONE, "Buckets[%d] = \n", nbuckets); for(uint32_t i=0; iDynStr + h->DynSym[buckets[i]].st_name; + const char* name = h->DynStr + h->DynSym._64[buckets[i]].st_name; printf_log(LOG_NONE, "%d: %s\n", buckets[i], name); } printf_log(LOG_NONE,"Chains[%d] = ", nchains); @@ -246,7 +281,7 @@ uint32_t new_elf_hash(const char *name) return h; } -Elf64_Sym* new_elf_lookup(elfheader_t* h, const char* symname, int ver, const char* vername, int local, int veropt) +static Elf64_Sym* new_elf_lookup(elfheader_t* h, const char* symname, int ver, const char* vername, int local, int veropt) { // Prepare hash table const uint32_t *hashtab = (uint32_t*)(h->gnu_hash + h->delta); @@ -272,10 +307,10 @@ Elf64_Sym* new_elf_lookup(elfheader_t* h, const char* symname, int ver, const ch if (symidx < symoffset) return NULL; while(1) { - const char* name = h->DynStr + h->DynSym[symidx].st_name; + const char* name = h->DynStr + h->DynSym._64[symidx].st_name; const uint32_t symhash = chains[symidx-symoffset]; if ((hash|1) == (symhash|1) && !strcmp(name, symname) && SymbolMatch(h, symidx, ver, vername, local, veropt)) { - return &h->DynSym[symidx]; + return &h->DynSym._64[symidx]; } if(symhash&1) return NULL; @@ -283,7 +318,7 @@ Elf64_Sym* new_elf_lookup(elfheader_t* h, const char* symname, int ver, const ch } } -void new_elf_hash_dump(elfheader_t* h) +static void new_elf_hash_dump(elfheader_t* h) { // Prepare hash table const uint32_t *hashtab = (uint32_t*)(h->gnu_hash + h->delta); @@ -301,7 +336,7 @@ void new_elf_hash_dump(elfheader_t* h) uint32_t symidx = buckets[i]; printf_log(LOG_NONE, "%d:", symidx); while(symidx>=symoffset) { - const char* name = h->DynStr + h->DynSym[symidx].st_name; + const char* name = h->DynStr + h->DynSym._64[symidx].st_name; const uint32_t hash = chains[symidx-symoffset]; if(hash&1) symidx=0; @@ -314,19 +349,19 @@ void new_elf_hash_dump(elfheader_t* h) printf_log(LOG_NONE, "\n===============\n"); } -Elf64_Sym* ElfLookup(elfheader_t* h, const char* symname, int ver, const char* vername, int local, int veropt) +Elf64_Sym* ElfLookup64(elfheader_t* h, const char* symname, int ver, const char* vername, int local, int veropt) { if(h->gnu_hash) return new_elf_lookup(h, symname, ver, vername, local, veropt); return old_elf_lookup(h, symname, ver, vername, local, veropt); } -Elf64_Sym* ElfSymTabLookup(elfheader_t* h, const char* symname) +Elf64_Sym* ElfSymTabLookup64(elfheader_t* h, const char* symname) { - if(!h->SymTab) + if(!h->SymTab._64) return 0; for(size_t i=0; inumSymTab; ++i) { - Elf64_Sym* sym = &h->SymTab[i]; + Elf64_Sym* sym = &h->SymTab._64[i]; int type = ELF64_ST_TYPE(sym->st_info); if(type==STT_FUNC || type==STT_TLS || type==STT_OBJECT) { const char * name = h->StrTab+sym->st_name; @@ -337,12 +372,12 @@ Elf64_Sym* ElfSymTabLookup(elfheader_t* h, const char* symname) return NULL; } -Elf64_Sym* ElfDynSymLookup(elfheader_t* h, const char* symname) +Elf64_Sym* ElfDynSymLookup64(elfheader_t* h, const char* symname) { - if(!h->DynSym) + if(!h->DynSym._64) return 0; for(size_t i=0; inumDynSym; ++i) { - Elf64_Sym* sym = &h->DynSym[i]; + Elf64_Sym* sym = &h->DynSym._64[i]; int type = ELF64_ST_TYPE(sym->st_info); if(type==STT_FUNC || type==STT_TLS || type==STT_OBJECT) { const char * name = h->DynStr+sym->st_name; diff --git a/src/elfs/elfload_dump.c b/src/elfs/elfload_dump.c index 62cedcf6..5c5f349a 100644 --- a/src/elfs/elfload_dump.c +++ b/src/elfs/elfload_dump.c @@ -13,7 +13,7 @@ #define SHT_CHECKSUM 0x6ffffff8 #endif -const char* DumpSection(Elf64_Shdr *s, char* SST) { +static const char* DumpSection(Elf64_Shdr *s, char* SST) { static char buff[400]; switch (s->sh_type) { case SHT_NULL: @@ -64,7 +64,7 @@ const char* DumpSection(Elf64_Shdr *s, char* SST) { return buff; } -const char* DumpDynamic(Elf64_Dyn *s) { +static const char* DumpDynamic(Elf64_Dyn *s) { static char buff[200]; switch (s->d_tag) { case DT_NULL: @@ -148,7 +148,7 @@ const char* DumpDynamic(Elf64_Dyn *s) { return buff; } -const char* DumpPHEntry(Elf64_Phdr *e) +static const char* DumpPHEntry(Elf64_Phdr *e) { static char buff[500]; memset(buff, 0, sizeof(buff)); @@ -175,7 +175,7 @@ const char* DumpPHEntry(Elf64_Phdr *e) return buff; } -const char* DumpRelType(int t) +const char* DumpRelType64(int t) { static char buff[50]; memset(buff, 0, sizeof(buff)); @@ -229,7 +229,7 @@ const char* DumpRelType(int t) return buff; } -const char* DumpSym(elfheader_t *h, Elf64_Sym* sym, int version) +static const char* DumpSym(elfheader_t *h, Elf64_Sym* sym, int version) { static char buff[4096]; const char* vername = (version==-1)?"(none)":((version==0)?"*local*":((version==1)?"*global*":GetSymbolVersion(h, version))); @@ -241,16 +241,16 @@ const char* DumpSym(elfheader_t *h, Elf64_Sym* sym, int version) return buff; } -const char* SymName(elfheader_t *h, Elf64_Sym* sym) +const char* SymName64(elfheader_t *h, Elf64_Sym* sym) { return h->DynStr+sym->st_name; } -const char* IdxSymName(elfheader_t *h, int sym) +static const char* IdxSymName(elfheader_t *h, int sym) { - return h->DynStr+h->DynSym[sym].st_name; + return h->DynStr+h->DynSym._64[sym].st_name; } -void DumpMainHeader(Elf64_Ehdr *header, elfheader_t *h) +void DumpMainHeader64(Elf64_Ehdr *header, elfheader_t *h) { if(box64_dump) { printf_dump(LOG_NEVER, "ELF Dump main header\n"); @@ -266,108 +266,116 @@ void DumpMainHeader(Elf64_Ehdr *header, elfheader_t *h) printf_dump(LOG_NEVER, "ELF Dump PEntries (%zu)\n", h->numPHEntries); for (size_t i=0; inumPHEntries; ++i) - printf_dump(LOG_NEVER, " PHEntry %04zu : %s\n", i, DumpPHEntry(h->PHEntries+i)); + printf_dump(LOG_NEVER, " PHEntry %04zu : %s\n", i, DumpPHEntry(h->PHEntries._64+i)); printf_dump(LOG_NEVER, "ELF Dump PEntries ====\n"); printf_dump(LOG_NEVER, "ELF Dump Sections (%zu)\n", h->numSHEntries); for (size_t i=0; inumSHEntries; ++i) - printf_dump(LOG_NEVER, " Section %04zu : %s\n", i, DumpSection(h->SHEntries+i, h->SHStrTab)); + printf_dump(LOG_NEVER, " Section %04zu : %s\n", i, DumpSection(h->SHEntries._64+i, h->SHStrTab)); printf_dump(LOG_NEVER, "ELF Dump Sections ====\n"); } } -void DumpSymTab(elfheader_t *h) +void DumpSymTab64(elfheader_t *h) { - if(box64_dump && h->SymTab) { + if(box64_dump && h->SymTab._64) { const char* name = ElfName(h); printf_dump(LOG_NEVER, "ELF Dump SymTab(%zu)=\n", h->numSymTab); for (size_t i=0; inumSymTab; ++i) printf_dump(LOG_NEVER, " %s:SymTab[%zu] = \"%s\", value=%p, size=%ld, info/other=%d/%d index=%d\n", name, - i, h->StrTab+h->SymTab[i].st_name, (void*)h->SymTab[i].st_value, h->SymTab[i].st_size, - h->SymTab[i].st_info, h->SymTab[i].st_other, h->SymTab[i].st_shndx); + i, h->StrTab+h->SymTab._64[i].st_name, (void*)h->SymTab._64[i].st_value, h->SymTab._64[i].st_size, + h->SymTab._64[i].st_info, h->SymTab._64[i].st_other, h->SymTab._64[i].st_shndx); printf_dump(LOG_NEVER, "ELF Dump SymTab=====\n"); } } -void DumpDynamicSections(elfheader_t *h) +void DumpDynamicSections64(elfheader_t *h) { - if(box64_dump && h->Dynamic) { + if(box64_dump && h->Dynamic._64) { printf_dump(LOG_NEVER, "ELF Dump Dynamic(%zu)=\n", h->numDynamic); for (size_t i=0; inumDynamic; ++i) - printf_dump(LOG_NEVER, " Dynamic %04zu : %s\n", i, DumpDynamic(h->Dynamic+i)); + printf_dump(LOG_NEVER, " Dynamic %04zu : %s\n", i, DumpDynamic(h->Dynamic._64+i)); printf_dump(LOG_NEVER, "ELF Dump Dynamic=====\n"); } } -void DumpDynSym(elfheader_t *h) +void DumpDynSym64(elfheader_t *h) { - if(box64_dump && h->DynSym) { + if(box64_dump && h->DynSym._64) { const char* name = ElfName(h); printf_dump(LOG_NEVER, "ELF Dump DynSym(%zu)=\n", h->numDynSym); for (size_t i=0; inumDynSym; ++i) { int version = h->VerSym?((Elf64_Half*)((uintptr_t)h->VerSym+h->delta))[i]:-1; - printf_dump(LOG_NEVER, " %s:DynSym[%zu] = %s\n", name, i, DumpSym(h, h->DynSym+i, version)); + printf_dump(LOG_NEVER, " %s:DynSym[%zu] = %s\n", name, i, DumpSym(h, h->DynSym._64+i, version)); } printf_dump(LOG_NEVER, "ELF Dump DynSym=====\n"); } } +void DumpDynamicNeeded32(elfheader_t *h) { /* TODO */} void DumpDynamicNeeded(elfheader_t *h) { - if(box64_dump && h->DynStrTab) { - printf_dump(LOG_NEVER, "ELF Dump DT_NEEDED=====\n"); - for (size_t i=0; inumDynamic; ++i) - if(h->Dynamic[i].d_tag==DT_NEEDED) { - printf_dump(LOG_NEVER, " Needed : %s\n", h->DynStrTab+h->Dynamic[i].d_un.d_val + h->delta); - } - printf_dump(LOG_NEVER, "ELF Dump DT_NEEDED=====\n"); - } + if(box64_is32bits) + DumpDynamicNeeded32(h); + else + if(box64_dump && h->DynStrTab) { + printf_dump(LOG_NEVER, "ELF Dump DT_NEEDED=====\n"); + for (size_t i=0; inumDynamic; ++i) + if(h->Dynamic._64[i].d_tag==DT_NEEDED) { + printf_dump(LOG_NEVER, " Needed : %s\n", h->DynStrTab+h->Dynamic._64[i].d_un.d_val + h->delta); + } + printf_dump(LOG_NEVER, "ELF Dump DT_NEEDED=====\n"); + } } +void DumpDynamicRPath32(elfheader_t *h) { /* TODO */} void DumpDynamicRPath(elfheader_t *h) { - if(box64_dump && h->DynStrTab) { - printf_dump(LOG_NEVER, "ELF Dump DT_RPATH/DT_RUNPATH=====\n"); - for (size_t i=0; inumDynamic; ++i) { - if(h->Dynamic[i].d_tag==DT_RPATH) { - printf_dump(LOG_NEVER, " RPATH : %s\n", h->DynStrTab+h->Dynamic[i].d_un.d_val + h->delta); - } - if(h->Dynamic[i].d_tag==DT_RUNPATH) { - printf_dump(LOG_NEVER, " RUNPATH : %s\n", h->DynStrTab+h->Dynamic[i].d_un.d_val + h->delta); + if(box64_is32bits) + DumpDynamicRPath32(h); + else + if(box64_dump && h->DynStrTab) { + printf_dump(LOG_NEVER, "ELF Dump DT_RPATH/DT_RUNPATH=====\n"); + for (size_t i=0; inumDynamic; ++i) { + if(h->Dynamic._64[i].d_tag==DT_RPATH) { + printf_dump(LOG_NEVER, " RPATH : %s\n", h->DynStrTab+h->Dynamic._64[i].d_un.d_val + h->delta); + } + if(h->Dynamic._64[i].d_tag==DT_RUNPATH) { + printf_dump(LOG_NEVER, " RUNPATH : %s\n", h->DynStrTab+h->Dynamic._64[i].d_un.d_val + h->delta); + } } + printf_dump(LOG_NEVER, "=====ELF Dump DT_RPATH/DT_RUNPATH\n"); } - printf_dump(LOG_NEVER, "=====ELF Dump DT_RPATH/DT_RUNPATH\n"); - } } -void DumpRelTable(elfheader_t *h, int cnt, Elf64_Rel *rel, const char* name) +void DumpRelTable64(elfheader_t *h, int cnt, Elf64_Rel *rel, const char* name) { if(box64_dump) { const char* elfname = ElfName(h); printf_dump(LOG_NEVER, "ELF Dump %s Table(%d) @%p\n", name, cnt, rel); for (int i = 0; irela) { const char* elfname = ElfName(h); printf_dump(LOG_NEVER, "ELF Dump %s Table(%d) @%p\n", name, cnt, rela); for (int i = 0; irelr) { const char* elfname = ElfName(h); diff --git a/src/elfs/elfloader.c b/src/elfs/elfloader.c index cd23e958..20fa13c0 100644 --- a/src/elfs/elfloader.c +++ b/src/elfs/elfloader.c @@ -80,14 +80,14 @@ void FreeElfHeader(elfheader_t** head) if(my_context) RemoveElfHeader(my_context, h); - box_free(h->PHEntries); - box_free(h->SHEntries); + box_free(h->PHEntries._64); //_64 or _32 doesn't mater for free, it's the same address + box_free(h->SHEntries._64); box_free(h->SHStrTab); box_free(h->StrTab); - box_free(h->Dynamic); + box_free(h->Dynamic._64); box_free(h->DynStr); - box_free(h->SymTab); - box_free(h->DynSym); + box_free(h->SymTab._64); + box_free(h->DynSym._64); FreeElfMemory(h); @@ -105,13 +105,23 @@ int CalcLoadAddr(elfheader_t* head) head->memsz = 0; head->paddr = head->vaddr = ~(uintptr_t)0; head->align = box64_pagesize; - for (size_t i=0; inumPHEntries; ++i) - if(head->PHEntries[i].p_type == PT_LOAD) { - if(head->paddr > (uintptr_t)head->PHEntries[i].p_paddr) - head->paddr = (uintptr_t)head->PHEntries[i].p_paddr; - if(head->vaddr > (uintptr_t)head->PHEntries[i].p_vaddr) - head->vaddr = (uintptr_t)head->PHEntries[i].p_vaddr; - } + if(box64_is32bits) { + for (size_t i=0; inumPHEntries; ++i) + if(head->PHEntries._32[i].p_type == PT_LOAD) { + if(head->paddr > (uintptr_t)head->PHEntries._32[i].p_paddr) + head->paddr = (uintptr_t)head->PHEntries._32[i].p_paddr; + if(head->vaddr > (uintptr_t)head->PHEntries._32[i].p_vaddr) + head->vaddr = (uintptr_t)head->PHEntries._32[i].p_vaddr; + } + } else { + for (size_t i=0; inumPHEntries; ++i) + if(head->PHEntries._64[i].p_type == PT_LOAD) { + if(head->paddr > (uintptr_t)head->PHEntries._64[i].p_paddr) + head->paddr = (uintptr_t)head->PHEntries._64[i].p_paddr; + if(head->vaddr > (uintptr_t)head->PHEntries._64[i].p_vaddr) + head->vaddr = (uintptr_t)head->PHEntries._64[i].p_vaddr; + } + } if(head->vaddr==~(uintptr_t)0 || head->paddr==~(uintptr_t)0) { printf_log(LOG_NONE, "Error: v/p Addr for Elf Load not set\n"); @@ -120,31 +130,58 @@ int CalcLoadAddr(elfheader_t* head) head->stacksz = 1024*1024; //1M stack size default? head->stackalign = 16; // default align for stack - for (size_t i=0; inumPHEntries; ++i) { - if(head->PHEntries[i].p_type == PT_LOAD) { - uintptr_t phend = head->PHEntries[i].p_vaddr - head->vaddr + head->PHEntries[i].p_memsz; - if(phend > head->memsz) - head->memsz = phend; - if(head->PHEntries[i].p_align > head->align) - head->align = head->PHEntries[i].p_align; - } - if(head->PHEntries[i].p_type == PT_GNU_STACK) { - if(head->stacksz < head->PHEntries[i].p_memsz) - head->stacksz = head->PHEntries[i].p_memsz; - if(head->stackalign < head->PHEntries[i].p_align) - head->stackalign = head->PHEntries[i].p_align; + if(box64_is32bits) + for (size_t i=0; inumPHEntries; ++i) { + if(head->PHEntries._32[i].p_type == PT_LOAD) { + uintptr_t phend = head->PHEntries._32[i].p_vaddr - head->vaddr + head->PHEntries._32[i].p_memsz; + if(phend > head->memsz) + head->memsz = phend; + if(head->PHEntries._32[i].p_align > head->align) + head->align = head->PHEntries._32[i].p_align; + } + if(head->PHEntries._32[i].p_type == PT_GNU_STACK) { + if(head->stacksz < head->PHEntries._32[i].p_memsz) + head->stacksz = head->PHEntries._32[i].p_memsz; + if(head->stackalign < head->PHEntries._32[i].p_align) + head->stackalign = head->PHEntries._32[i].p_align; + } + if(head->PHEntries._32[i].p_type == PT_TLS) { + head->tlsaddr = head->PHEntries._32[i].p_vaddr; + head->tlssize = head->PHEntries._32[i].p_memsz; + head->tlsfilesize = head->PHEntries._32[i].p_filesz; + head->tlsalign = head->PHEntries._32[i].p_align; + // force alignement... + if(head->tlsalign>1) + while(head->tlssize&(head->tlsalign-1)) + head->tlssize++; + } } - if(head->PHEntries[i].p_type == PT_TLS) { - head->tlsaddr = head->PHEntries[i].p_vaddr; - head->tlssize = head->PHEntries[i].p_memsz; - head->tlsfilesize = head->PHEntries[i].p_filesz; - head->tlsalign = head->PHEntries[i].p_align; - // force alignement... - if(head->tlsalign>1) - while(head->tlssize&(head->tlsalign-1)) - head->tlssize++; + else + for (size_t i=0; inumPHEntries; ++i) { + if(head->PHEntries._64[i].p_type == PT_LOAD) { + uintptr_t phend = head->PHEntries._64[i].p_vaddr - head->vaddr + head->PHEntries._64[i].p_memsz; + if(phend > head->memsz) + head->memsz = phend; + if(head->PHEntries._64[i].p_align > head->align) + head->align = head->PHEntries._64[i].p_align; + } + if(head->PHEntries._64[i].p_type == PT_GNU_STACK) { + if(head->stacksz < head->PHEntries._64[i].p_memsz) + head->stacksz = head->PHEntries._64[i].p_memsz; + if(head->stackalign < head->PHEntries._64[i].p_align) + head->stackalign = head->PHEntries._64[i].p_align; + } + if(head->PHEntries._64[i].p_type == PT_TLS) { + head->tlsaddr = head->PHEntries._64[i].p_vaddr; + head->tlssize = head->PHEntries._64[i].p_memsz; + head->tlsfilesize = head->PHEntries._64[i].p_filesz; + head->tlsalign = head->PHEntries._64[i].p_align; + // force alignement... + if(head->tlsalign>1) + while(head->tlssize&(head->tlsalign-1)) + head->tlssize++; + } } - } printf_log(LOG_DEBUG, "Elf Addr(v/p)=%p/%p Memsize=0x%zx (align=0x%zx)\n", (void*)head->vaddr, (void*)head->paddr, head->memsz, head->align); printf_log(LOG_DEBUG, "Elf Stack Memsize=%zu (align=%zu)\n", head->stacksz, head->stackalign); printf_log(LOG_DEBUG, "Elf TLS Memsize=%zu (align=%zu)\n", head->tlssize, head->tlsalign); @@ -165,8 +202,11 @@ const char* ElfPath(elfheader_t* head) return head->path; } +int AllocLoadElfMemory32(box64context_t* context, elfheader_t* head, int mainbin) { /*TODO*/ return 1; } int AllocLoadElfMemory(box64context_t* context, elfheader_t* head, int mainbin) { + if(box64_is32bits) + return AllocLoadElfMemory32(context, head, mainbin); uintptr_t offs = 0; loadProtectionFromMap(); int log_level = box64_load_addr?LOG_INFO:LOG_DEBUG; @@ -174,7 +214,7 @@ int AllocLoadElfMemory(box64context_t* context, elfheader_t* head, int mainbin) head->multiblock_n = 0; // count PHEntrie with LOAD uintptr_t max_align = head->align-1; for (size_t i=0; inumPHEntries; ++i) - if(head->PHEntries[i].p_type == PT_LOAD && head->PHEntries[i].p_flags) { + if(head->PHEntries._64[i].p_type == PT_LOAD && head->PHEntries._64[i].p_flags) { ++head->multiblock_n; } @@ -230,8 +270,8 @@ int AllocLoadElfMemory(box64context_t* context, elfheader_t* head, int mainbin) head->memory = (char*)0xffffffffffffffff; int n = 0; for (size_t i=0; inumPHEntries; ++i) { - if(head->PHEntries[i].p_type == PT_LOAD && head->PHEntries[i].p_flags) { - Elf64_Phdr * e = &head->PHEntries[i]; + if(head->PHEntries._64[i].p_type == PT_LOAD && head->PHEntries._64[i].p_flags) { + Elf64_Phdr * e = &head->PHEntries._64[i]; head->multiblocks[n].flags = e->p_flags; head->multiblocks[n].offs = e->p_offset; @@ -350,8 +390,8 @@ int AllocLoadElfMemory(box64context_t* context, elfheader_t* head, int mainbin) head->memory = (char*)head->multiblocks[n].p; ++n; } - if(head->PHEntries[i].p_type == PT_TLS) { - Elf64_Phdr * e = &head->PHEntries[i]; + if(head->PHEntries._64[i].p_type == PT_TLS) { + Elf64_Phdr * e = &head->PHEntries._64[i]; char* dest = (char*)(context->tlsdata+context->tlssize+head->tlsbase); printf_log(LOG_DEBUG, "Loading TLS block #%zu @%p (0x%zx/0x%zx)\n", i, dest, e->p_filesz, e->p_memsz); if(e->p_filesz) { @@ -400,7 +440,7 @@ int isElfHasNeededVer(elfheader_t* head, const char* libname, elfheader_t* verne { if(!verneeded || !head) return 1; - if(!head->VerDef || !verneeded->VerNeed) + if(!head->VerDef._64 || !verneeded->VerNeed._64) return 1; int cnt = GetNeededVersionCnt(verneeded, libname); for (int i=0; iSymTab && addr>=(uintptr_t)h->SymTab && addr<(uintptr_t)&h->SymTab[h->numSymTab]) - return 1; - if(h->DynSym && addr>=(uintptr_t)h->DynSym && addr<(uintptr_t)&h->DynSym[h->numDynSym]) - return 1; + if(box64_is32bits) { + if(h->SymTab._32 && addr>=(uintptr_t)h->SymTab._32 && addr<(uintptr_t)&h->SymTab._32[h->numSymTab]) + return 1; + if(h->DynSym._32 && addr>=(uintptr_t)h->DynSym._32 && addr<(uintptr_t)&h->DynSym._32[h->numDynSym]) + return 1; + } else { + if(h->SymTab._64 && addr>=(uintptr_t)h->SymTab._64 && addr<(uintptr_t)&h->SymTab._64[h->numSymTab]) + return 1; + if(h->DynSym._64 && addr>=(uintptr_t)h->DynSym._64 && addr<(uintptr_t)&h->DynSym._64[h->numDynSym]) + return 1; + } return 0; } static elfheader_t* FindElfSymbol(box64context_t *context, Elf64_Sym* sym) @@ -445,8 +492,8 @@ int FindR64COPYRel(elfheader_t* h, const char* name, uintptr_t *offs, uint64_t** int cnt = h->relasz / h->relaent; for (int i=0; iDynSym[ELF64_R_SYM(rela[i].r_info)]; - const char* symname = SymName(h, sym); + Elf64_Sym *sym = &h->DynSym._64[ELF64_R_SYM(rela[i].r_info)]; + const char* symname = SymName64(h, sym); if((t==R_X86_64_COPY) && symname && !strcmp(symname, name) && (sym->st_size==size)) { int version2 = h->VerSym?((Elf64_Half*)((uintptr_t)h->VerSym+h->delta))[ELF64_R_SYM(rela[i].r_info)]:-1; if(version2!=-1) version2 &= 0x7fff; @@ -464,7 +511,7 @@ int FindR64COPYRel(elfheader_t* h, const char* name, uintptr_t *offs, uint64_t** return 0; } -int RelocateElfREL(lib_t *maplib, lib_t *local_maplib, int bindnow, int deepbind, elfheader_t* head, int cnt, Elf64_Rel *rel) +static int RelocateElfREL(lib_t *maplib, lib_t *local_maplib, int bindnow, int deepbind, elfheader_t* head, int cnt, Elf64_Rel *rel) { printf_log(LOG_NONE, "Error: REL type of Relocation unsupported (only RELA)\n"); return 1; @@ -492,8 +539,8 @@ void GrabX64CopyMainElfReloc(elfheader_t* head) for (int i=0; iDynSym[ELF64_R_SYM(rela[i].r_info)]; - const char* symname = SymName(head, sym); + Elf64_Sym *sym = &head->DynSym._64[ELF64_R_SYM(rela[i].r_info)]; + const char* symname = SymName64(head, sym); int version = head->VerSym?((Elf64_Half*)((uintptr_t)head->VerSym+head->delta))[ELF64_R_SYM(rela[i].r_info)]:-1; if(version!=-1) version &=0x7fff; const char* vername = GetSymbolVersion(head, version); @@ -531,15 +578,15 @@ static elfheader_t* checkElfLib(elfheader_t* h, library_t* lib) return h; } -int RelocateElfRELA(lib_t *maplib, lib_t *local_maplib, int bindnow, int deepbind, elfheader_t* head, int cnt, Elf64_Rela *rela, int* need_resolv) +static int RelocateElfRELA(lib_t *maplib, lib_t *local_maplib, int bindnow, int deepbind, elfheader_t* head, int cnt, Elf64_Rela *rela, int* need_resolv) { int ret_ok = 0; for (int i=0; iDynSym[ELF64_R_SYM(rela[i].r_info)]; + Elf64_Sym *sym = &head->DynSym._64[ELF64_R_SYM(rela[i].r_info)]; int bind = ELF64_ST_BIND(sym->st_info); //uint64_t ndx = sym->st_shndx; - const char* symname = SymName(head, sym); + const char* symname = SymName64(head, sym); uint64_t *p = (uint64_t*)(rela[i].r_offset + head->delta); uintptr_t offs = 0; uintptr_t end = 0; @@ -554,7 +601,7 @@ int RelocateElfRELA(lib_t *maplib, lib_t *local_maplib, int bindnow, int deepbin Elf64_Sym* elfsym = NULL; int vis = ELF64_ST_VISIBILITY(sym->st_other); if(vis==STV_PROTECTED) { - elfsym = ElfDynSymLookup(head, symname); + elfsym = ElfDynSymLookup64(head, symname); printf_log(LOG_DEBUG, "Symbol %s from %s is PROTECTED\n", symname, head->name); } else { if(bind==STB_GNU_UNIQUE) { @@ -566,7 +613,7 @@ int RelocateElfRELA(lib_t *maplib, lib_t *local_maplib, int bindnow, int deepbin offs = sym->st_value + head->delta; end = offs + sym->st_size; } else { - elfsym = ElfDynSymLookup(head, symname); + elfsym = ElfDynSymLookup64(head, symname); if(elfsym && elfsym->st_shndx) { offs = elfsym->st_value + head->delta; end = offs + elfsym->st_size; @@ -602,14 +649,14 @@ int RelocateElfRELA(lib_t *maplib, lib_t *local_maplib, int bindnow, int deepbin // can be ignored break; case R_X86_64_RELATIVE: - printf_dump(LOG_NEVER, "Apply %s R_X86_64_RELATIVE @%p (%p -> %p)\n", BindSym64(bind), p, *(void**)p, (void*)(head->delta+ rela[i].r_addend)); + printf_dump(LOG_NEVER, "Apply %s R_X86_64_RELATIVE @%p (%p -> %p)\n", BindSym(bind), p, *(void**)p, (void*)(head->delta+ rela[i].r_addend)); *p = head->delta+ rela[i].r_addend; break; case R_X86_64_IRELATIVE: { x64emu_t* emu = thread_get_emu(); EmuCall(emu, head->delta+rela[i].r_addend); - printf_dump(LOG_NEVER, "Apply %s R_X86_64_IRELATIVE @%p (%p -> %p()=%p)\n", BindSym64(bind), p, *(void**)p, (void*)(head->delta+ rela[i].r_addend), (void*)(R_RAX)); + printf_dump(LOG_NEVER, "Apply %s R_X86_64_IRELATIVE @%p (%p -> %p()=%p)\n", BindSym(bind), p, *(void**)p, (void*)(head->delta+ rela[i].r_addend), (void*)(R_RAX)); *p = R_RAX; } break; @@ -639,7 +686,7 @@ int RelocateElfRELA(lib_t *maplib, lib_t *local_maplib, int bindnow, int deepbin if(GetSymbolStartEnd(my_context->globdata, symname, &globoffs, &globend, version, vername, 1, veropt)) { globp = (uint64_t*)globoffs; printf_dump(LOG_NEVER, "Apply %s R_X86_64_GLOB_DAT with R_X86_64_COPY @%p/%p (%p/%p -> %p/%p) size=%zd on sym=%s (%sver=%d/%s) \n", - BindSym64(bind), p, globp, (void*)(p?(*p):0), + BindSym(bind), p, globp, (void*)(p?(*p):0), (void*)(globp?(*globp):0), (void*)offs, (void*)globoffs, sym->st_size, symname, veropt?"opt":"", version, vername?vername:"(none)"); sym_elf = my_context->elfs[0]; *p = globoffs; @@ -648,7 +695,7 @@ int RelocateElfRELA(lib_t *maplib, lib_t *local_maplib, int bindnow, int deepbin if(strcmp(symname, "__gmon_start__") && strcmp(symname, "data_start") && strcmp(symname, "__data_start") && strcmp(symname, "collector_func_load")) printf_log((bind==STB_WEAK)?LOG_DEBUG:LOG_NONE, "%s: Global Symbol %s not found, cannot apply R_X86_64_GLOB_DAT @%p (%p) in %s\n", (bind==STB_WEAK)?"Warning":"Error", symname, p, *(void**)p, head->name); } else { - printf_dump(LOG_NEVER, "Apply %s R_X86_64_GLOB_DAT @%p (%p -> %p) on sym=%s (%sver=%d/%s, elf=%s)\n", BindSym64(bind), p, (void*)(p?(*p):0), (void*)(offs + rela[i].r_addend), symname, veropt?"opt":"", version, vername?vername:"(none)", sym_elf?sym_elf->name:"(native)"); + printf_dump(LOG_NEVER, "Apply %s R_X86_64_GLOB_DAT @%p (%p -> %p) on sym=%s (%sver=%d/%s, elf=%s)\n", BindSym(bind), p, (void*)(p?(*p):0), (void*)(offs + rela[i].r_addend), symname, veropt?"opt":"", version, vername?vername:"(none)", sym_elf?sym_elf->name:"(native)"); *p = offs + rela[i].r_addend; if(sym_elf && sym_elf!=last_elf && sym_elf!=head) last_elf = checkElfLib(head, sym_elf->lib); } @@ -676,7 +723,7 @@ int RelocateElfRELA(lib_t *maplib, lib_t *local_maplib, int bindnow, int deepbin } else { if(p) { printf_dump(LOG_NEVER, "Apply %s R_X86_64_JUMP_SLOT @%p with sym=%s (%p -> %p / %s (%sver=%d / %s))\n", - BindSym64(bind), p, symname, *(void**)p, (void*)(offs+rela[i].r_addend), sym_elf?sym_elf->name:"native", veropt?"opt":"", version, vername?vername:"(none)"); + BindSym(bind), p, symname, *(void**)p, (void*)(offs+rela[i].r_addend), sym_elf?sym_elf->name:"native", veropt?"opt":"", version, vername?vername:"(none)"); *p = offs + rela[i].r_addend; if(sym_elf && sym_elf!=last_elf && sym_elf!=head) last_elf = checkElfLib(head, sym_elf->lib); } else { @@ -685,7 +732,7 @@ int RelocateElfRELA(lib_t *maplib, lib_t *local_maplib, int bindnow, int deepbin } } else { printf_dump(LOG_NEVER, "Preparing (if needed) %s R_X86_64_JUMP_SLOT @%p (0x%lx->0x%0lx) with sym=%s to be apply later (addend=%ld)\n", - BindSym64(bind), p, *p, *p+head->delta, symname, rela[i].r_addend); + BindSym(bind), p, *p, *p+head->delta, symname, rela[i].r_addend); *p += head->delta; *need_resolv = 1; } @@ -695,7 +742,7 @@ int RelocateElfRELA(lib_t *maplib, lib_t *local_maplib, int bindnow, int deepbin if(offs!=globoffs) { offs = globoffs; sym_elf = my_context->elfs[0]; - elfsym = ElfDynSymLookup(sym_elf, symname); + elfsym = ElfDynSymLookup64(sym_elf, symname); } } if (!offs && !elfsym) { @@ -704,7 +751,7 @@ int RelocateElfRELA(lib_t *maplib, lib_t *local_maplib, int bindnow, int deepbin ret_ok = 1; } else { printf_dump(LOG_NEVER, "Apply %s R_X86_64_64 @%p with sym=%s (%sver=%d/%s) addend=0x%lx (%p -> %p)\n", - BindSym64(bind), p, symname, veropt?"opt":"", version, vername?vername:"(none)", rela[i].r_addend, *(void**)p, (void*)(offs+rela[i].r_addend/*+*(uint64_t*)p*/)); + BindSym(bind), p, symname, veropt?"opt":"", version, vername?vername:"(none)", rela[i].r_addend, *(void**)p, (void*)(offs+rela[i].r_addend/*+*(uint64_t*)p*/)); *p /*+*/= offs+rela[i].r_addend; if(sym_elf && sym_elf!=last_elf && sym_elf!=head) last_elf = checkElfLib(head, sym_elf->lib); } @@ -718,11 +765,11 @@ int RelocateElfRELA(lib_t *maplib, lib_t *local_maplib, int bindnow, int deepbin } if(sym_elf) { delta = *(int64_t*)p; - printf_dump(LOG_NEVER, "Applying %s %s on %s @%p (%ld -> %ld+%ld+%ld, size=%ld)\n", BindSym64(bind), DumpRelType(t), symname, p, delta, sym_elf->tlsbase, (int64_t)offs, rela[i].r_addend, end-offs); + printf_dump(LOG_NEVER, "Applying %s %s on %s @%p (%ld -> %ld+%ld+%ld, size=%ld)\n", BindSym(bind), DumpRelType64(t), symname, p, delta, sym_elf->tlsbase, (int64_t)offs, rela[i].r_addend, end-offs); *p = (uintptr_t)((int64_t)offs + rela[i].r_addend + sym_elf->tlsbase); if(sym_elf && sym_elf!=last_elf && sym_elf!=head) last_elf = checkElfLib(head, sym_elf->lib); } else { - printf_log(LOG_INFO, "Warning, cannot apply %s %s on %s @%p (%ld), no elf_header found\n", BindSym64(bind), DumpRelType(t), symname, p, (int64_t)offs); + printf_log(LOG_INFO, "Warning, cannot apply %s %s on %s @%p (%ld), no elf_header found\n", BindSym(bind), DumpRelType64(t), symname, p, (int64_t)offs); } } break; @@ -735,7 +782,7 @@ int RelocateElfRELA(lib_t *maplib, lib_t *local_maplib, int bindnow, int deepbin offs = getElfIndex(my_context, sym_elf); } if(p) { - printf_dump(LOG_NEVER, "Apply %s %s @%p with sym=%s (%p -> %p)\n", BindSym64(bind), "R_X86_64_DTPMOD64", p, symname, *(void**)p, (void*)offs); + printf_dump(LOG_NEVER, "Apply %s %s @%p with sym=%s (%p -> %p)\n", BindSym(bind), "R_X86_64_DTPMOD64", p, symname, *(void**)p, (void*)offs); *p = offs; if(sym_elf && sym_elf!=last_elf && sym_elf!=head) last_elf = checkElfLib(head, sym_elf->lib); } else { @@ -758,7 +805,7 @@ int RelocateElfRELA(lib_t *maplib, lib_t *local_maplib, int bindnow, int deepbin } if(p) { int64_t tlsoffset = (int64_t)offs; // it's not an offset in elf memory - printf_dump(LOG_NEVER, "Apply %s R_X86_64_DTPOFF64 @%p with sym=%s (%p -> %p)\n", BindSym64(bind), p, symname, (void*)tlsoffset, (void*)offs); + printf_dump(LOG_NEVER, "Apply %s R_X86_64_DTPOFF64 @%p with sym=%s (%p -> %p)\n", BindSym(bind), p, symname, (void*)tlsoffset, (void*)offs); *p = tlsoffset; if(sym_elf && sym_elf!=last_elf && sym_elf!=head) last_elf = checkElfLib(head, sym_elf->lib); } else { @@ -768,7 +815,7 @@ int RelocateElfRELA(lib_t *maplib, lib_t *local_maplib, int bindnow, int deepbin break; case R_X86_64_TLSDESC: if(!symname || !symname[0]) { - printf_dump(LOG_NEVER, "Apply %s R_X86_64_TLSDESC @%p with addend=%zu\n", BindSym64(bind), p, rela[i].r_addend); + printf_dump(LOG_NEVER, "Apply %s R_X86_64_TLSDESC @%p with addend=%zu\n", BindSym(bind), p, rela[i].r_addend); struct tlsdesc volatile *td = (struct tlsdesc volatile *)p; if(!tlsdescUndefweak) tlsdescUndefweak = AddBridge(my_context->system, pFE, my__dl_tlsdesc_undefweak, 0, "_dl_tlsdesc_undefweak"); @@ -779,13 +826,13 @@ int RelocateElfRELA(lib_t *maplib, lib_t *local_maplib, int bindnow, int deepbin } break; default: - printf_log(LOG_INFO, "Warning, don't know of to handle rela #%d %s on %s\n", i, DumpRelType(ELF64_R_TYPE(rela[i].r_info)), symname); + printf_log(LOG_INFO, "Warning, don't know of to handle rela #%d %s on %s\n", i, DumpRelType64(ELF64_R_TYPE(rela[i].r_info)), symname); } } return bindnow?ret_ok:0; } -int RelocateElfRELR(elfheader_t *head, int cnt, Elf64_Relr *relr) { +static int RelocateElfRELR(elfheader_t *head, int cnt, Elf64_Relr *relr) { Elf64_Addr *where = NULL; for (int i = 0; i < cnt; i++) { Elf64_Relr p = relr[i]; @@ -805,7 +852,8 @@ int RelocateElfRELR(elfheader_t *head, int cnt, Elf64_Relr *relr) { return 0; } -int RelocateElf(lib_t *maplib, lib_t *local_maplib, int bindnow, int deepbind, elfheader_t* head) +int RelocateElf32(lib_t *maplib, lib_t *local_maplib, int bindnow, int deepbind, elfheader_t* head) { /* TODO */ return -1; } +int RelocateElf64(lib_t *maplib, lib_t *local_maplib, int bindnow, int deepbind, elfheader_t* head) { if((head->flags&DF_BIND_NOW) && !bindnow) { bindnow = 1; @@ -813,29 +861,34 @@ int RelocateElf(lib_t *maplib, lib_t *local_maplib, int bindnow, int deepbind, e } if(head->relr) { int cnt = head->relrsz / head->relrent; - DumpRelRTable(head, cnt, (Elf64_Relr *)(head->relr + head->delta), "RelR"); + DumpRelRTable64(head, cnt, (Elf64_Relr *)(head->relr + head->delta), "RelR"); printf_dump(LOG_DEBUG, "Applying %d Relocation(s) without Addend for %s bindnow=%d, deepbind=%d\n", cnt, head->name, bindnow, deepbind); if(RelocateElfRELR(head, cnt, (Elf64_Relr *)(head->relr + head->delta))) return -1; } if(head->rel) { int cnt = head->relsz / head->relent; - DumpRelTable(head, cnt, (Elf64_Rel *)(head->rel + head->delta), "Rel"); + DumpRelTable64(head, cnt, (Elf64_Rel *)(head->rel + head->delta), "Rel"); printf_dump(LOG_DEBUG, "Applying %d Relocation(s) for %s bindnow=%d, deepbind=%d\n", cnt, head->name, bindnow, deepbind); if(RelocateElfREL(maplib, local_maplib, bindnow, deepbind, head, cnt, (Elf64_Rel *)(head->rel + head->delta))) return -1; } if(head->rela) { int cnt = head->relasz / head->relaent; - DumpRelATable(head, cnt, (Elf64_Rela *)(head->rela + head->delta), "RelA"); + DumpRelATable64(head, cnt, (Elf64_Rela *)(head->rela + head->delta), "RelA"); printf_dump(LOG_DEBUG, "Applying %d Relocation(s) with Addend for %s bindnow=%d, deepbind=%d\n", cnt, head->name, bindnow, deepbind); if(RelocateElfRELA(maplib, local_maplib, bindnow, deepbind, head, cnt, (Elf64_Rela *)(head->rela + head->delta), NULL)) return -1; } return 0; } +int RelocateElf(lib_t *maplib, lib_t *local_maplib, int bindnow, int deepbind, elfheader_t* head) +{ + return box64_is32bits?RelocateElf32(maplib, local_maplib, bindnow, deepbind, head):RelocateElf64(maplib, local_maplib, bindnow, deepbind, head); +} -int RelocateElfPlt(lib_t *maplib, lib_t *local_maplib, int bindnow, int deepbind, elfheader_t* head) +int RelocateElfPlt32(lib_t *maplib, lib_t *local_maplib, int bindnow, int deepbind, elfheader_t* head) { /* TODO */ return -1; } +int RelocateElfPlt64(lib_t *maplib, lib_t *local_maplib, int bindnow, int deepbind, elfheader_t* head) { int need_resolver = 0; if((head->flags&DF_BIND_NOW) && !bindnow) { @@ -845,26 +898,26 @@ int RelocateElfPlt(lib_t *maplib, lib_t *local_maplib, int bindnow, int deepbind if(head->pltrel) { int cnt = head->pltsz / head->pltent; if(head->pltrel==DT_REL) { - DumpRelTable(head, cnt, (Elf64_Rel *)(head->jmprel + head->delta), "PLT"); + DumpRelTable64(head, cnt, (Elf64_Rel *)(head->jmprel + head->delta), "PLT"); printf_dump(LOG_DEBUG, "Applying %d PLT Relocation(s) for %s bindnow=%d, deepbind=%d\n", cnt, head->name, bindnow, deepbind); if(RelocateElfREL(maplib, local_maplib, bindnow, deepbind, head, cnt, (Elf64_Rel *)(head->jmprel + head->delta))) return -1; } else if(head->pltrel==DT_RELA) { - DumpRelATable(head, cnt, (Elf64_Rela *)(head->jmprel + head->delta), "PLT"); + DumpRelATable64(head, cnt, (Elf64_Rela *)(head->jmprel + head->delta), "PLT"); printf_dump(LOG_DEBUG, "Applying %d PLT Relocation(s) with Addend for %s bindnow=%d, deepbind=%d\n", cnt, head->name, bindnow, deepbind); if(RelocateElfRELA(maplib, local_maplib, bindnow, deepbind, head, cnt, (Elf64_Rela *)(head->jmprel + head->delta), &need_resolver)) return -1; } if(need_resolver) { - if(pltResolver==(uintptr_t)-1) { - pltResolver = AddBridge(my_context->system, vFE, PltResolver, 0, "PltResolver"); + if(pltResolver64==(uintptr_t)-1) { + pltResolver64 = AddBridge(my_context->system, vFE, PltResolver64, 0, "PltResolver"); } if(head->pltgot) { - *(uintptr_t*)(head->pltgot+head->delta+16) = pltResolver; + *(uintptr_t*)(head->pltgot+head->delta+16) = pltResolver64; *(uintptr_t*)(head->pltgot+head->delta+8) = (uintptr_t)head; printf_dump(LOG_DEBUG, "PLT Resolver injected in plt.got at %p\n", (void*)(head->pltgot+head->delta+16)); } else if(head->got) { - *(uintptr_t*)(head->got+head->delta+16) = pltResolver; + *(uintptr_t*)(head->got+head->delta+16) = pltResolver64; *(uintptr_t*)(head->got+head->delta+8) = (uintptr_t)head; printf_dump(LOG_DEBUG, "PLT Resolver injected in got at %p\n", (void*)(head->got+head->delta+16)); } @@ -873,6 +926,10 @@ int RelocateElfPlt(lib_t *maplib, lib_t *local_maplib, int bindnow, int deepbind return 0; } +int RelocateElfPlt(lib_t *maplib, lib_t *local_maplib, int bindnow, int deepbind, elfheader_t* head) +{ + return box64_is32bits?RelocateElfPlt32(maplib, local_maplib, bindnow, deepbind, head):RelocateElfPlt64(maplib, local_maplib, bindnow, deepbind, head); +} void CalcStack(elfheader_t* elf, uint64_t* stacksz, size_t* stackalign) { @@ -882,43 +939,6 @@ void CalcStack(elfheader_t* elf, uint64_t* stacksz, size_t* stackalign) *stackalign = elf->stackalign; } -Elf64_Sym* GetFunction(elfheader_t* h, const char* name) -{ - // TODO: create a hash on named to avoid this loop - for (size_t i=0; inumSymTab; ++i) { - int type = ELF64_ST_TYPE(h->SymTab[i].st_info); - if(type==STT_FUNC) { - const char * symname = h->StrTab+h->SymTab[i].st_name; - if(strcmp(symname, name)==0) { - return h->SymTab+i; - } - } - } - return NULL; -} - -Elf64_Sym* GetElfObject(elfheader_t* h, const char* name) -{ - for (size_t i=0; inumSymTab; ++i) { - int type = ELF64_ST_TYPE(h->SymTab[i].st_info); - if(type==STT_OBJECT) { - const char * symname = h->StrTab+h->SymTab[i].st_name; - if(strcmp(symname, name)==0) { - return h->SymTab+i; - } - } - } - return NULL; -} - - -uintptr_t GetFunctionAddress(elfheader_t* h, const char* name) -{ - Elf64_Sym* sym = GetFunction(h, name); - if(sym) return sym->st_value; - return 0; -} - uintptr_t GetEntryPoint(lib_t* maplib, elfheader_t* h) { (void)maplib; @@ -945,13 +965,18 @@ uintptr_t GetLastByte(elfheader_t* h) #endif void checkHookedSymbols(elfheader_t* h); // in mallochook.c +void AddSymbols32(lib_t *maplib, elfheader_t* h) { /* TODO */ } void AddSymbols(lib_t *maplib, elfheader_t* h) { - //if(box64_dump && h->hash) old_elf_hash_dump(h); - //if(box64_dump && h->gnu_hash) new_elf_hash_dump(h); - if(box64_dump && h->DynSym) DumpDynSym(h); - if(h==my_context->elfs[0]) - GrabX64CopyMainElfReloc(h); + if(box64_is32bits) { + AddSymbols32(maplib, h); + } else { + //if(box64_dump && h->hash) old_elf_hash_dump(h); + //if(box64_dump && h->gnu_hash) new_elf_hash_dump(h); + if(box64_dump && h->DynSym._64) DumpDynSym64(h); + if(h==my_context->elfs[0]) + GrabX64CopyMainElfReloc(h); + } #ifndef STATICBUILD checkHookedSymbols(h); #endif @@ -977,9 +1002,10 @@ int LoadNeededLibs(elfheader_t* h, lib_t *maplib, int local, int bindnow, int de return 0; DumpDynamicRPath(h); // update RPATH first - for (size_t i=0; inumDynamic; ++i) - if(h->Dynamic[i].d_tag==DT_RPATH || h->Dynamic[i].d_tag==DT_RUNPATH) { - char *rpathref = h->DynStrTab+h->delta+h->Dynamic[i].d_un.d_val; + for (size_t i=0; inumDynamic; ++i) { + int tag = box64_is32bits?h->Dynamic._32[i].d_tag:h->Dynamic._64[i].d_tag; + if(tag==DT_RPATH || tag==DT_RUNPATH) { + char *rpathref = h->DynStrTab+h->delta+(box64_is32bits?h->Dynamic._32[i].d_un.d_val:h->Dynamic._64[i].d_un.d_val); char* rpath = rpathref; while(strstr(rpath, "$ORIGIN")) { char* origin = box_strdup(h->path); @@ -1032,23 +1058,24 @@ int LoadNeededLibs(elfheader_t* h, lib_t *maplib, int local, int bindnow, int de if(rpath!=rpathref) box_free(rpath); } - + } DumpDynamicNeeded(h); int cnt = 0; // count the number of needed libs, and also grab soname for (size_t i=0; inumDynamic; ++i) { - if(h->Dynamic[i].d_tag==DT_NEEDED) + int tag = box64_is32bits?h->Dynamic._32[i].d_tag:h->Dynamic._64[i].d_tag; + if(tag==DT_NEEDED) ++cnt; - if(h->Dynamic[i].d_tag==DT_SONAME) - h->soname = h->DynStrTab+h->delta+h->Dynamic[i].d_un.d_val; + if(tag==DT_SONAME) + h->soname = h->DynStrTab+h->delta+(box64_is32bits?h->Dynamic._32[i].d_un.d_val:h->Dynamic._64[i].d_un.d_val); } h->needed = new_neededlib(cnt); if(h == my_context->elfs[0]) my_context->neededlibs = h->needed; int j=0; for (size_t i=0; inumDynamic; ++i) - if(h->Dynamic[i].d_tag==DT_NEEDED) - h->needed->names[j++] = h->DynStrTab+h->delta+h->Dynamic[i].d_un.d_val; + if((box64_is32bits?h->Dynamic._32[i].d_tag:h->Dynamic._64[i].d_tag)==DT_NEEDED) + h->needed->names[j++] = h->DynStrTab+h->delta+(box64_is32bits?h->Dynamic._32[i].d_un.d_val:h->Dynamic._64[i].d_un.d_val); if(h==my_context->elfs[0] && box64_addlibs.size) { for(int i=0; iname); @@ -1069,8 +1096,8 @@ int ElfCheckIfUseTCMallocMinimal(elfheader_t* h) if(!h) return 0; for (size_t i=0; inumDynamic; ++i) - if(h->Dynamic[i].d_tag==DT_NEEDED) { - char *needed = h->DynStrTab+h->delta+h->Dynamic[i].d_un.d_val; + if((box64_is32bits?h->Dynamic._32[i].d_tag:h->Dynamic._64[i].d_tag)==DT_NEEDED) { + char *needed = h->DynStrTab+h->delta+(box64_is32bits?h->Dynamic._32[i].d_un.d_val:h->Dynamic._64[i].d_un.d_val); if(!strcmp(needed, "libtcmalloc_minimal.so.4")) // tcmalloc needs to be the 1st lib loaded return 1; else if(!strcmp(needed, "libtcmalloc_minimal.so.0")) // tcmalloc needs to be the 1st lib loaded @@ -1106,10 +1133,15 @@ void startMallocHook(); #else void startMallocHook() {} #endif +void RunElfInit32(elfheader_t* h, x64emu_t *emu) { /* TODO*/ } void RunElfInit(elfheader_t* h, x64emu_t *emu) { if(!h || h->init_done) return; + if(box64_is32bits) { + RunElfInit32(h, emu); + return; + } // reset Segs Cache memset(emu->segs_serial, 0, sizeof(emu->segs_serial)); uintptr_t p = h->initentry + h->delta; @@ -1171,10 +1203,15 @@ void RunDeferredElfInit(x64emu_t *emu) box_free(List); } +void RunElfFini32(elfheader_t* h, x64emu_t *emu) { /* TODO */ } void RunElfFini(elfheader_t* h, x64emu_t *emu) { if(!h || h->fini_done || !h->init_done) return; + if(box64_is32bits) { + RunElfFini32(h, emu); + return; + } h->fini_done = 1; // Call the registered cxa_atexit functions CallCleanup(emu, h); @@ -1274,28 +1311,28 @@ const char* FindNearestSymbolName(elfheader_t* h, void* p, uintptr_t* start, uin return ret; for (size_t i=0; inumSymTab && distance!=0; ++i) { - const char * symname = h->StrTab+h->SymTab[i].st_name; - uintptr_t offs = h->SymTab[i].st_value + h->delta; + const char * symname = box64_is32bits?(h->StrTab+h->SymTab._32[i].st_name):(h->StrTab+h->SymTab._64[i].st_name); + uintptr_t offs = (box64_is32bits?h->SymTab._32[i].st_value:h->SymTab._64[i].st_value) + h->delta; if(offs<=addr) { if(distance>addr-offs) { distance = addr-offs; ret = symname; s = offs; - size = h->SymTab[i].st_size; + size = box64_is32bits?h->SymTab._32[i].st_size:h->SymTab._64[i].st_size; } } } for (size_t i=0; inumDynSym && distance!=0; ++i) { - const char * symname = h->DynStr+h->DynSym[i].st_name; - uintptr_t offs = h->DynSym[i].st_value + h->delta; + const char * symname = h->DynStr+(box64_is32bits?h->DynSym._32[i].st_name:h->DynSym._64[i].st_name); + uintptr_t offs = (box64_is32bits?h->DynSym._32[i].st_value:h->DynSym._64[i].st_value) + h->delta; if(offs<=addr) { if(distance>addr-offs) { distance = addr-offs; ret = symname; s = offs; - size = h->DynSym[i].st_size; + size = box64_is32bits?h->DynSym._32[i].st_size:h->DynSym._64[i].st_size; } } } @@ -1376,7 +1413,7 @@ void* GetDynamicSection(elfheader_t* h) { if(!h) return NULL; - return h->Dynamic; + return box64_is32bits?((void*)h->Dynamic._32):((void*)h->Dynamic._64); } #ifdef DYNAREC @@ -1452,6 +1489,7 @@ static void* find_dl_iterate_phdr_Fct(void* fct) #undef SUPER EXPORT int my_dl_iterate_phdr(x64emu_t *emu, void* F, void *data) { + if(box64_is32bits) {printf_log(LOG_NONE, "Error, calling unsuppoeted dl_iterate_phdr in 32bits\n"); return 0; } printf_log(LOG_DEBUG, "Call to partially implemented dl_iterate_phdr(%p, %p)\n", F, data); box64context_t *context = GetEmuContext(emu); const char* empty = ""; @@ -1461,7 +1499,7 @@ EXPORT int my_dl_iterate_phdr(x64emu_t *emu, void* F, void *data) { my_dl_phdr_info_t info; info.dlpi_addr = GetElfDelta(context->elfs[idx]); info.dlpi_name = idx?context->elfs[idx]->name:empty; //1st elf is program, and this one doesn't get a name - info.dlpi_phdr = context->elfs[idx]->PHEntries; + info.dlpi_phdr = context->elfs[idx]->PHEntries._64; info.dlpi_phnum = context->elfs[idx]->numPHEntries; if((ret = dl_iterate_phdr_callback(emu, F, &info, sizeof(info), data))) { return ret; @@ -1473,12 +1511,17 @@ EXPORT int my_dl_iterate_phdr(x64emu_t *emu, void* F, void *data) { return ret; } +void ResetSpecialCaseMainElf32(elfheader_t* h) { /* TODO */ } void ResetSpecialCaseMainElf(elfheader_t* h) { + if(box64_is32bits) { + ResetSpecialCaseMainElf32(h); + return; + } Elf64_Sym *sym = NULL; for (size_t i=0; inumDynSym; ++i) { - if(h->DynSym[i].st_info == 17) { - sym = h->DynSym+i; + if(h->DynSym._64[i].st_info == 17) { + sym = h->DynSym._64+i; const char * symname = h->DynStr+sym->st_name; if(strcmp(symname, "_IO_2_1_stderr_")==0 && ((void*)sym->st_value+h->delta)) { memcpy((void*)sym->st_value+h->delta, stderr, sym->st_size); @@ -1564,12 +1607,12 @@ void ElfAttachLib(elfheader_t* head, library_t* lib) head->lib = lib; } -Elf64_Sym* ElfLocateSymbol(elfheader_t* head, uintptr_t *offs, uintptr_t *end, const char* symname, int* ver, const char** vername, int local, int* veropt) +static Elf64_Sym* ElfLocateSymbol(elfheader_t* head, uintptr_t *offs, uintptr_t *end, const char* symname, int* ver, const char** vername, int local, int* veropt) { - Elf64_Sym* sym = ElfLookup(head, symname, *ver, *vername, local, *veropt); + Elf64_Sym* sym = ElfLookup64(head, symname, *ver, *vername, local, *veropt); if(!sym) return NULL; if(head->VerSym && !*veropt) { - int idx = ((uintptr_t)sym - (uintptr_t)head->DynSym)/sizeof(Elf64_Sym); + int idx = ((uintptr_t)sym - (uintptr_t)head->DynSym._64)/sizeof(Elf64_Sym); int version = ((Elf64_Half*)((uintptr_t)head->VerSym+head->delta))[idx]; if(version!=-1) version &=0x7fff; const char* symvername = GetSymbolVersion(head, version); @@ -1589,7 +1632,8 @@ Elf64_Sym* ElfLocateSymbol(elfheader_t* head, uintptr_t *offs, uintptr_t *end, c return sym; } -void* ElfGetLocalSymbolStartEnd(elfheader_t* head, uintptr_t *offs, uintptr_t *end, const char* symname, int* ver, const char** vername, int local, int* veropt) +void* ElfGetLocalSymbolStartEnd32(elfheader_t* head, uintptr_t *offs, uintptr_t *end, const char* symname, int* ver, const char** vername, int local, int* veropt) { /* TOODO */ return NULL; } +void* ElfGetLocalSymbolStartEnd64(elfheader_t* head, uintptr_t *offs, uintptr_t *end, const char* symname, int* ver, const char** vername, int local, int* veropt) { Elf64_Sym* sym = ElfLocateSymbol(head, offs, end, symname, ver, vername, local, veropt); if(!sym) return NULL; @@ -1599,7 +1643,13 @@ void* ElfGetLocalSymbolStartEnd(elfheader_t* head, uintptr_t *offs, uintptr_t *e if(end) *end = sym->st_value + head->delta + sym->st_size; return sym; } -void* ElfGetGlobalSymbolStartEnd(elfheader_t* head, uintptr_t *offs, uintptr_t *end, const char* symname, int* ver, const char** vername, int local, int* veropt) +void* ElfGetLocalSymbolStartEnd(elfheader_t* head, uintptr_t *offs, uintptr_t *end, const char* symname, int* ver, const char** vername, int local, int* veropt) +{ + return box64_is32bits?ElfGetLocalSymbolStartEnd32(head, offs, end, symname, ver, vername, local, veropt):ElfGetLocalSymbolStartEnd64(head, offs, end, symname, ver, vername, local, veropt); +} + +void* ElfGetGlobalSymbolStartEnd32(elfheader_t* head, uintptr_t *offs, uintptr_t *end, const char* symname, int* ver, const char** vername, int local, int* veropt) { /*T ODO */ return NULL; } +void* ElfGetGlobalSymbolStartEnd64(elfheader_t* head, uintptr_t *offs, uintptr_t *end, const char* symname, int* ver, const char** vername, int local, int* veropt) { Elf64_Sym* sym = ElfLocateSymbol(head, offs, end, symname, ver, vername, local, veropt); if(!sym) return NULL; @@ -1609,7 +1659,13 @@ void* ElfGetGlobalSymbolStartEnd(elfheader_t* head, uintptr_t *offs, uintptr_t * if(end) *end = sym->st_value + head->delta + sym->st_size; return sym; } -void* ElfGetWeakSymbolStartEnd(elfheader_t* head, uintptr_t *offs, uintptr_t *end, const char* symname, int* ver, const char** vername, int local, int* veropt) +void* ElfGetGlobalSymbolStartEnd(elfheader_t* head, uintptr_t *offs, uintptr_t *end, const char* symname, int* ver, const char** vername, int local, int* veropt) +{ + return box64_is32bits?ElfGetGlobalSymbolStartEnd32(head, offs, end, symname, ver, vername, local, veropt):ElfGetGlobalSymbolStartEnd64(head, offs, end, symname, ver, vername, local, veropt); +} + +void* ElfGetWeakSymbolStartEnd32(elfheader_t* head, uintptr_t *offs, uintptr_t *end, const char* symname, int* ver, const char** vername, int local, int* veropt) { /* TODO */ return NULL; } +void* ElfGetWeakSymbolStartEnd64(elfheader_t* head, uintptr_t *offs, uintptr_t *end, const char* symname, int* ver, const char** vername, int local, int* veropt) { Elf64_Sym* sym = ElfLocateSymbol(head, offs, end, symname, ver, vername, local, veropt); if(!sym) return NULL; @@ -1619,9 +1675,15 @@ void* ElfGetWeakSymbolStartEnd(elfheader_t* head, uintptr_t *offs, uintptr_t *en if(end) *end = sym->st_value + head->delta + sym->st_size; return sym; } -int ElfGetSymTabStartEnd(elfheader_t* head, uintptr_t *offs, uintptr_t *end, const char* symname) +void* ElfGetWeakSymbolStartEnd(elfheader_t* head, uintptr_t *offs, uintptr_t *end, const char* symname, int* ver, const char** vername, int local, int* veropt) +{ + return box64_is32bits?ElfGetWeakSymbolStartEnd32(head, offs, end, symname, ver, vername, local, veropt):ElfGetWeakSymbolStartEnd64(head, offs, end, symname, ver, vername, local, veropt); +} + +int ElfGetSymTabStartEnd32(elfheader_t* head, uintptr_t *offs, uintptr_t *end, const char* symname) { /* TODO */ return 0; } +int ElfGetSymTabStartEnd64(elfheader_t* head, uintptr_t *offs, uintptr_t *end, const char* symname) { - Elf64_Sym* sym = ElfSymTabLookup(head, symname); + Elf64_Sym* sym = ElfSymTabLookup64(head, symname); if(!sym) return 0; if(!sym->st_shndx) return 0; if(!sym->st_size) return 0; //needed? @@ -1629,6 +1691,10 @@ int ElfGetSymTabStartEnd(elfheader_t* head, uintptr_t *offs, uintptr_t *end, con if(end) *end = sym->st_value + head->delta + sym->st_size; return 1; } +int ElfGetSymTabStartEnd(elfheader_t* head, uintptr_t *offs, uintptr_t *end, const char* symname) +{ + return box64_is32bits?ElfGetSymTabStartEnd32(head, offs, end, symname):ElfGetSymTabStartEnd64(head, offs, end, symname); +} typedef struct search_symbol_s{ const char* name; @@ -1701,8 +1767,8 @@ void* GetNativeSymbolUnversioned(void* lib, const char* name) return s.addr; } -uintptr_t pltResolver = ~0LL; -EXPORT void PltResolver(x64emu_t* emu) +uintptr_t pltResolver64 = ~0LL; +EXPORT void PltResolver64(x64emu_t* emu) { uintptr_t addr = Pop64(emu); int slot = (int)Pop64(emu); @@ -1710,13 +1776,13 @@ EXPORT void PltResolver(x64emu_t* emu) library_t* lib = h->lib; lib_t* local_maplib = GetMaplib(lib); int deepbind = GetDeepBind(lib); - printf_dump(LOG_DEBUG, "PltResolver: Addr=%p, Slot=%d Return=%p(%s): elf is %s (VerSym=%p, deepbind=%d, local_maplib=%p) func param: %p, %p...\n", (void*)addr, slot, *(void**)(R_RSP), getAddrFunctionName(*(uintptr_t*)R_RSP),h->name, h->VerSym, deepbind, local_maplib, (void*)R_RDI, (void*)R_RSI); + printf_dump(LOG_DEBUG, "PltResolver64: Addr=%p, Slot=%d Return=%p(%s): elf is %s (VerSym=%p, deepbind=%d, local_maplib=%p) func param: %p, %p...\n", (void*)addr, slot, *(void**)(R_RSP), getAddrFunctionName(*(uintptr_t*)R_RSP),h->name, h->VerSym, deepbind, local_maplib, (void*)R_RDI, (void*)R_RSI); Elf64_Rela * rel = (Elf64_Rela *)(h->jmprel + h->delta) + slot; - Elf64_Sym *sym = &h->DynSym[ELF64_R_SYM(rel->r_info)]; + Elf64_Sym *sym = &h->DynSym._64[ELF64_R_SYM(rel->r_info)]; int bind = ELF64_ST_BIND(sym->st_info); - const char* symname = SymName(h, sym); + const char* symname = SymName64(h, sym); int version = h->VerSym?((Elf64_Half*)((uintptr_t)h->VerSym+h->delta))[ELF64_R_SYM(rel->r_info)]:-1; if(version!=-1) version &= 0x7fff; const char* vername = GetSymbolVersion(h, version); @@ -1728,7 +1794,7 @@ EXPORT void PltResolver(x64emu_t* emu) Elf64_Sym *elfsym = NULL; if(bind==STB_LOCAL) { - elfsym = ElfDynSymLookup(h, symname); + elfsym = ElfDynSymLookup64(h, symname); if(elfsym && elfsym->st_shndx) { offs = elfsym->st_value + h->delta; end = offs + elfsym->st_size; @@ -1763,7 +1829,7 @@ EXPORT void PltResolver(x64emu_t* emu) offs = (uintptr_t)getAlternate((void*)offs); if(p) { - printf_dump(LOG_DEBUG, " Apply %s R_X86_64_JUMP_SLOT %p with sym=%s(%sver %d: %s%s%s) (%p -> %p / %s)\n", BindSym64(bind), p, symname, veropt?"opt":"", version, symname, vername?"@":"", vername?vername:"",*(void**)p, (void*)offs, ElfName(sym_elf)); + printf_dump(LOG_DEBUG, " Apply %s R_X86_64_JUMP_SLOT %p with sym=%s(%sver %d: %s%s%s) (%p -> %p / %s)\n", BindSym(bind), p, symname, veropt?"opt":"", version, symname, vername?"@":"", vername?vername:"",*(void**)p, (void*)offs, ElfName(sym_elf)); *p = offs; if(sym_elf && sym_elf!=h) checkElfLib(h, sym_elf->lib); } else { diff --git a/src/elfs/elfloader_private.h b/src/elfs/elfloader_private.h index 93750f49..d881149a 100644 --- a/src/elfs/elfloader_private.h +++ b/src/elfs/elfloader_private.h @@ -7,6 +7,7 @@ typedef struct cleanup_s cleanup_t; #include #include "elfloader.h" +#include "box32.h" typedef struct multiblock_s { void* p; @@ -22,27 +23,48 @@ typedef struct elfheader_s { char* name; char* path; // Resolved path to file char* soname; // soname of the elf - size_t numPHEntries; - Elf64_Phdr *PHEntries; - size_t numSHEntries; - Elf64_Shdr *SHEntries; + uint16_t numPHEntries; + union { + Elf64_Phdr* _64; + Elf32_Phdr* _32; + } PHEntries; + uint16_t numSHEntries; + union { + Elf64_Shdr* _64; + Elf32_Shdr* _32; + } SHEntries; size_t SHIdx; size_t numSST; char* SHStrTab; char* StrTab; - Elf64_Sym* SymTab; + union { + Elf64_Sym* _64; + Elf32_Sym* _32; + } SymTab; size_t numSymTab; char* DynStr; - Elf64_Sym* DynSym; + union { + Elf64_Sym* _64; + Elf32_Sym* _32; + } DynSym; size_t numDynSym; - Elf64_Dyn* Dynamic; + union { + Elf64_Dyn* _64; + Elf32_Dyn* _32; + } Dynamic; size_t numDynamic; char* DynStrTab; size_t szDynStrTab; - Elf64_Half* VerSym; - Elf64_Verneed* VerNeed; + uint16_t* VerSym; + union { + Elf64_Verneed* _64; + Elf32_Verneed* _32; + } VerNeed; int szVerNeed; - Elf64_Verdef* VerDef; + union { + Elf64_Verdef* _64; + Elf32_Verdef* _32; + } VerDef; int szVerDef; int e_type; uint32_t flags; @@ -178,19 +200,15 @@ typedef struct elfheader_s { elfheader_t* ParseElfHeader64(FILE* f, const char* name, int exec); -const char* BindSym64(int bind); +const char* BindSym(int bind); Elf64_Half GetSymbolVersionFlag(elfheader_t* h, int index); uint32_t old_elf_hash(const char* name); -Elf64_Sym* old_elf_lookup(elfheader_t* h, const char* symname, int ver, const char* vername, int local, int veropt); -void old_elf_hash_dump(elfheader_t* h); uint32_t new_elf_hash(const char *name); -Elf64_Sym* new_elf_lookup(elfheader_t* h, const char* symname, int ver, const char* vername, int local, int veropt); -void new_elf_hash_dump(elfheader_t* h); -Elf64_Sym* ElfLookup(elfheader_t* h, const char* symname, int ver, const char* vername, int local, int veropt); -Elf64_Sym* ElfSymTabLookup(elfheader_t* h, const char* symname); -Elf64_Sym* ElfDynSymLookup(elfheader_t* h, const char* symname); +Elf64_Sym* ElfLookup64(elfheader_t* h, const char* symname, int ver, const char* vername, int local, int veropt); +Elf64_Sym* ElfSymTabLookup64(elfheader_t* h, const char* symname); +Elf64_Sym* ElfDynSymLookup64(elfheader_t* h, const char* symname); #endif //__ELFLOADER_PRIVATE_H_ diff --git a/src/elfs/elfparser.c b/src/elfs/elfparser.c index 9a8f6bee..8f51e8c3 100644 --- a/src/elfs/elfparser.c +++ b/src/elfs/elfparser.c @@ -145,9 +145,9 @@ elfheader_t* ParseElfHeader64(FILE* f, const char* name, int exec) if(header.e_shentsize && h->numSHEntries) { // now read all section headers printf_dump(LOG_DEBUG, "Read %zu Section header\n", h->numSHEntries); - h->SHEntries = (Elf64_Shdr*)box_calloc(h->numSHEntries, sizeof(Elf64_Shdr)); + h->SHEntries._64 = (Elf64_Shdr*)box_calloc(h->numSHEntries, sizeof(Elf64_Shdr)); fseeko64(f, header.e_shoff ,SEEK_SET); - if(fread(h->SHEntries, sizeof(Elf64_Shdr), h->numSHEntries, f)!=h->numSHEntries) { + if(fread(h->SHEntries._64, sizeof(Elf64_Shdr), h->numSHEntries, f)!=h->numSHEntries) { FreeElfHeader(&h); printf_log(LOG_INFO, "Cannot read all Section Header\n"); return NULL; @@ -156,14 +156,14 @@ elfheader_t* ParseElfHeader64(FILE* f, const char* name, int exec) if(h->numPHEntries == PN_XNUM) { printf_dump(LOG_DEBUG, "Read number of Program Header in 1st Section\n"); // read 1st section header and grab actual number from here - h->numPHEntries = h->SHEntries[0].sh_info; + h->numPHEntries = h->SHEntries._64[0].sh_info; } } printf_dump(LOG_DEBUG, "Read %zu Program header\n", h->numPHEntries); - h->PHEntries = (Elf64_Phdr*)box_calloc(h->numPHEntries, sizeof(Elf64_Phdr)); + h->PHEntries._64 = (Elf64_Phdr*)box_calloc(h->numPHEntries, sizeof(Elf64_Phdr)); fseeko64(f, header.e_phoff ,SEEK_SET); - if(fread(h->PHEntries, sizeof(Elf64_Phdr), h->numPHEntries, f)!=h->numPHEntries) { + if(fread(h->PHEntries._64, sizeof(Elf64_Phdr), h->numPHEntries, f)!=h->numPHEntries) { FreeElfHeader(&h); printf_log(LOG_INFO, "Cannot read all Program Header\n"); return NULL; @@ -172,7 +172,7 @@ elfheader_t* ParseElfHeader64(FILE* f, const char* name, int exec) if(header.e_shentsize && header.e_shnum) { if(h->SHIdx == SHN_XINDEX) { printf_dump(LOG_DEBUG, "Read number of String Table in 1st Section\n"); - h->SHIdx = h->SHEntries[0].sh_link; + h->SHIdx = h->SHEntries._64[0].sh_link; } if(h->SHIdx > h->numSHEntries) { printf_log(LOG_INFO, "Incoherent Section String Table Index : %zu / %zu\n", h->SHIdx, h->numSHEntries); @@ -181,23 +181,23 @@ elfheader_t* ParseElfHeader64(FILE* f, const char* name, int exec) } // load Section table printf_dump(LOG_DEBUG, "Loading Sections Table String (idx = %zu)\n", h->SHIdx); - if(LoadSH(f, h->SHEntries+h->SHIdx, (void*)&h->SHStrTab, ".shstrtab", SHT_STRTAB)) { + if(LoadSH(f, h->SHEntries._64+h->SHIdx, (void*)&h->SHStrTab, ".shstrtab", SHT_STRTAB)) { FreeElfHeader(&h); return NULL; } - if(box64_dump) DumpMainHeader(&header, h); + if(box64_dump) DumpMainHeader64(&header, h); - LoadNamedSection(f, h->SHEntries, h->numSHEntries, h->SHStrTab, ".strtab", "SymTab Strings", SHT_STRTAB, (void**)&h->StrTab, NULL); - LoadNamedSection(f, h->SHEntries, h->numSHEntries, h->SHStrTab, ".symtab", "SymTab", SHT_SYMTAB, (void**)&h->SymTab, &h->numSymTab); - if(box64_dump && h->SymTab) DumpSymTab(h); + LoadNamedSection(f, h->SHEntries._64, h->numSHEntries, h->SHStrTab, ".strtab", "SymTab Strings", SHT_STRTAB, (void**)&h->StrTab, NULL); + LoadNamedSection(f, h->SHEntries._64, h->numSHEntries, h->SHStrTab, ".symtab", "SymTab", SHT_SYMTAB, (void**)&h->SymTab._64, &h->numSymTab); + if(box64_dump && h->SymTab._64) DumpSymTab64(h); - LoadNamedSection(f, h->SHEntries, h->numSHEntries, h->SHStrTab, ".dynamic", "Dynamic", SHT_DYNAMIC, (void**)&h->Dynamic, &h->numDynamic); - if(box64_dump && h->Dynamic) DumpDynamicSections(h); + LoadNamedSection(f, h->SHEntries._64, h->numSHEntries, h->SHStrTab, ".dynamic", "Dynamic", SHT_DYNAMIC, (void**)&h->Dynamic._64, &h->numDynamic); + if(box64_dump && h->Dynamic._64) DumpDynamicSections64(h); // grab DT_REL & DT_RELA stuffs // also grab the DT_STRTAB string table { for (size_t i=0; inumDynamic; ++i) { - Elf64_Dyn d = h->Dynamic[i]; + Elf64_Dyn d = h->Dynamic._64[i]; Elf64_Word val = d.d_un.d_val; Elf64_Addr ptr = d.d_un.d_ptr; switch (d.d_tag) { @@ -281,7 +281,7 @@ elfheader_t* ParseElfHeader64(FILE* f, const char* name, int exec) printf_dump(LOG_DEBUG, "The DT_VERNEEDNUM is %d\n", h->szVerNeed); break; case DT_VERNEED: - h->VerNeed = (Elf64_Verneed*)ptr; + h->VerNeed._64 = (Elf64_Verneed*)ptr; printf_dump(LOG_DEBUG, "The DT_VERNEED is at address %p\n", h->VerNeed); break; case DT_VERDEFNUM: @@ -289,7 +289,7 @@ elfheader_t* ParseElfHeader64(FILE* f, const char* name, int exec) printf_dump(LOG_DEBUG, "The DT_VERDEFNUM is %d\n", h->szVerDef); break; case DT_VERDEF: - h->VerDef = (Elf64_Verdef*)ptr; + h->VerDef._64 = (Elf64_Verdef*)ptr; printf_dump(LOG_DEBUG, "The DT_VERDEF is at address %p\n", h->VerDef); break; case DT_FLAGS: @@ -340,61 +340,61 @@ elfheader_t* ParseElfHeader64(FILE* f, const char* name, int exec) printf_dump(LOG_DEBUG, "PLT Table @%p (type=%ld 0x%zx/0x%0x)\n", (void*)h->jmprel, h->pltrel, h->pltsz, h->pltent); } if(h->DynStrTab && h->szDynStrTab) { - //DumpDynamicNeeded(h); cannot dump now, it's not loaded yet + //DumpDynamicNeeded64(h); cannot dump now, it's not loaded yet } } // look for PLT Offset - int ii = FindSection(h->SHEntries, h->numSHEntries, h->SHStrTab, ".got.plt"); + int ii = FindSection(h->SHEntries._64, h->numSHEntries, h->SHStrTab, ".got.plt"); if(ii) { - h->gotplt = h->SHEntries[ii].sh_addr; - h->gotplt_end = h->gotplt + h->SHEntries[ii].sh_size; + h->gotplt = h->SHEntries._64[ii].sh_addr; + h->gotplt_end = h->gotplt + h->SHEntries._64[ii].sh_size; printf_dump(LOG_DEBUG, "The GOT.PLT Table is at address %p\n", (void*)h->gotplt); } - ii = FindSection(h->SHEntries, h->numSHEntries, h->SHStrTab, ".got"); + ii = FindSection(h->SHEntries._64, h->numSHEntries, h->SHStrTab, ".got"); if(ii) { - h->got = h->SHEntries[ii].sh_addr; - h->got_end = h->got + h->SHEntries[ii].sh_size; + h->got = h->SHEntries._64[ii].sh_addr; + h->got_end = h->got + h->SHEntries._64[ii].sh_size; printf_dump(LOG_DEBUG, "The GOT Table is at address %p..%p\n", (void*)h->got, (void*)h->got_end); } - ii = FindSection(h->SHEntries, h->numSHEntries, h->SHStrTab, ".plt"); + ii = FindSection(h->SHEntries._64, h->numSHEntries, h->SHStrTab, ".plt"); if(ii) { - h->plt = h->SHEntries[ii].sh_addr; - h->plt_end = h->plt + h->SHEntries[ii].sh_size; + h->plt = h->SHEntries._64[ii].sh_addr; + h->plt_end = h->plt + h->SHEntries._64[ii].sh_size; printf_dump(LOG_DEBUG, "The PLT Table is at address %p..%p\n", (void*)h->plt, (void*)h->plt_end); } // grab version of symbols - ii = FindSection(h->SHEntries, h->numSHEntries, h->SHStrTab, ".gnu.version"); + ii = FindSection(h->SHEntries._64, h->numSHEntries, h->SHStrTab, ".gnu.version"); if(ii) { - h->VerSym = (Elf64_Half*)(h->SHEntries[ii].sh_addr); + h->VerSym = (Elf64_Half*)(h->SHEntries._64[ii].sh_addr); printf_dump(LOG_DEBUG, "The .gnu.version is at address %p\n", h->VerSym); } // grab .text for main code - ii = FindSection(h->SHEntries, h->numSHEntries, h->SHStrTab, ".text"); + ii = FindSection(h->SHEntries._64, h->numSHEntries, h->SHStrTab, ".text"); if(ii) { - h->text = (uintptr_t)(h->SHEntries[ii].sh_addr); - h->textsz = h->SHEntries[ii].sh_size; + h->text = (uintptr_t)(h->SHEntries._64[ii].sh_addr); + h->textsz = h->SHEntries._64[ii].sh_size; printf_dump(LOG_DEBUG, "The .text is at address %p, and is %zu big\n", (void*)h->text, h->textsz); } - ii = FindSection(h->SHEntries, h->numSHEntries, h->SHStrTab, ".eh_frame"); + ii = FindSection(h->SHEntries._64, h->numSHEntries, h->SHStrTab, ".eh_frame"); if(ii) { - h->ehframe = (uintptr_t)(h->SHEntries[ii].sh_addr); - h->ehframe_end = h->ehframe + h->SHEntries[ii].sh_size; + h->ehframe = (uintptr_t)(h->SHEntries._64[ii].sh_addr); + h->ehframe_end = h->ehframe + h->SHEntries._64[ii].sh_size; printf_dump(LOG_DEBUG, "The .eh_frame section is at address %p..%p\n", (void*)h->ehframe, (void*)h->ehframe_end); } - ii = FindSection(h->SHEntries, h->numSHEntries, h->SHStrTab, ".eh_frame_hdr"); + ii = FindSection(h->SHEntries._64, h->numSHEntries, h->SHStrTab, ".eh_frame_hdr"); if(ii) { - h->ehframehdr = (uintptr_t)(h->SHEntries[ii].sh_addr); + h->ehframehdr = (uintptr_t)(h->SHEntries._64[ii].sh_addr); printf_dump(LOG_DEBUG, "The .eh_frame_hdr section is at address %p\n", (void*)h->ehframehdr); } - LoadNamedSection(f, h->SHEntries, h->numSHEntries, h->SHStrTab, ".dynstr", "DynSym Strings", SHT_STRTAB, (void**)&h->DynStr, NULL); - LoadNamedSection(f, h->SHEntries, h->numSHEntries, h->SHStrTab, ".dynsym", "DynSym", SHT_DYNSYM, (void**)&h->DynSym, &h->numDynSym); + LoadNamedSection(f, h->SHEntries._64, h->numSHEntries, h->SHStrTab, ".dynstr", "DynSym Strings", SHT_STRTAB, (void**)&h->DynStr, NULL); + LoadNamedSection(f, h->SHEntries._64, h->numSHEntries, h->SHStrTab, ".dynsym", "DynSym", SHT_DYNSYM, (void**)&h->DynSym, &h->numDynSym); } return h; } -const char* BindSym64(int bind) +const char* BindSym(int bind) { switch(bind) { case STB_GLOBAL: return "STB_GLOBAL"; diff --git a/src/elfs/elfparser64.c b/src/elfs/elfparser64.c new file mode 100644 index 00000000..e496ca8b --- /dev/null +++ b/src/elfs/elfparser64.c @@ -0,0 +1,406 @@ +#define _GNU_SOURCE +#include +#include +#include +#include + +#include "box64version.h" +#include "elfloader.h" +#include "debug.h" +#include "elfload_dump.h" +#include "elfloader_private.h" + +#ifndef PN_XNUM +#define PN_XNUM (0xffff) +#endif +#ifndef DT_GNU_HASH +#define DT_GNU_HASH 0x6ffffef5 +#endif + +static int LoadSH(FILE *f, Elf64_Shdr *s, void** SH, const char* name, uint32_t type) +{ + if(type && (s->sh_type != type)) { + printf_log(LOG_INFO, "Section Header \"%s\" (off=%ld, size=%ld) has incorect type (%d != %d)\n", name, s->sh_offset, s->sh_size, s->sh_type, type); + return -1; + } + if (type==SHT_SYMTAB && s->sh_size%sizeof(Elf64_Sym)) { + printf_log(LOG_INFO, "Section Header \"%s\" (off=%ld, size=%ld) has size (not multiple of %ld)\n", name, s->sh_offset, s->sh_size, sizeof(Elf64_Sym)); + } + *SH = box_calloc(1, s->sh_size); + fseeko64(f, s->sh_offset ,SEEK_SET); + if(fread(*SH, s->sh_size, 1, f)!=1) { + printf_log(LOG_INFO, "Cannot read Section Header \"%s\" (off=%ld, size=%ld)\n", name, s->sh_offset, s->sh_size); + return -1; + } + + return 0; +} + +static int FindSection(Elf64_Shdr *s, int n, char* SHStrTab, const char* name) +{ + for (int i=0; i_64; + h->name = box_strdup(name); + h->entrypoint = header.e_entry; + h->numPHEntries = header.e_phnum; + h->numSHEntries = header.e_shnum; + h->SHIdx = header.e_shstrndx; + h->e_type = header.e_type; + // special cases for nums + if(header.e_shentsize && !h->numSHEntries) { + printf_dump(LOG_DEBUG, "Read number of Sections in 1st Section\n"); + // read 1st section header and grab actual number from here + fseeko64(f, header.e_shoff, SEEK_SET); + Elf64_Shdr section; + if(fread(§ion, sizeof(Elf64_Shdr), 1, f)!=1) { + box_free(h); + printf_log(LOG_INFO, "Cannot read Initial Section Header\n"); + return NULL; + } + h->numSHEntries = section.sh_size; + } + if(header.e_shentsize && h->numSHEntries) { + // now read all section headers + printf_dump(LOG_DEBUG, "Read %zu Section header\n", h->numSHEntries); + h->SHEntries = (Elf64_Shdr*)box_calloc(h->numSHEntries, sizeof(Elf64_Shdr)); + fseeko64(f, header.e_shoff ,SEEK_SET); + if(fread(h->SHEntries, sizeof(Elf64_Shdr), h->numSHEntries, f)!=h->numSHEntries) { + FreeElfHeader(&head); + printf_log(LOG_INFO, "Cannot read all Section Header\n"); + return NULL; + } + + if(h->numPHEntries == PN_XNUM) { + printf_dump(LOG_DEBUG, "Read number of Program Header in 1st Section\n"); + // read 1st section header and grab actual number from here + h->numPHEntries = h->SHEntries[0].sh_info; + } + } + + printf_dump(LOG_DEBUG, "Read %zu Program header\n", h->numPHEntries); + h->PHEntries = (Elf64_Phdr*)box_calloc(h->numPHEntries, sizeof(Elf64_Phdr)); + fseeko64(f, header.e_phoff ,SEEK_SET); + if(fread(h->PHEntries, sizeof(Elf64_Phdr), h->numPHEntries, f)!=h->numPHEntries) { + FreeElfHeader(&head); + printf_log(LOG_INFO, "Cannot read all Program Header\n"); + return NULL; + } + + if(header.e_shentsize && header.e_shnum) { + if(h->SHIdx == SHN_XINDEX) { + printf_dump(LOG_DEBUG, "Read number of String Table in 1st Section\n"); + h->SHIdx = h->SHEntries[0].sh_link; + } + if(h->SHIdx > h->numSHEntries) { + printf_log(LOG_INFO, "Incoherent Section String Table Index : %zu / %zu\n", h->SHIdx, h->numSHEntries); + FreeElfHeader(&head); + return NULL; + } + // load Section table + printf_dump(LOG_DEBUG, "Loading Sections Table String (idx = %zu)\n", h->SHIdx); + if(LoadSH(f, h->SHEntries+h->SHIdx, (void*)&h->SHStrTab, ".shstrtab", SHT_STRTAB)) { + FreeElfHeader(&head); + return NULL; + } + if(box64_dump) DumpMainHeader(&header, head); + + LoadNamedSection(f, h->SHEntries, h->numSHEntries, h->SHStrTab, ".strtab", "SymTab Strings", SHT_STRTAB, (void**)&h->StrTab, NULL); + LoadNamedSection(f, h->SHEntries, h->numSHEntries, h->SHStrTab, ".symtab", "SymTab", SHT_SYMTAB, (void**)&h->SymTab, &h->numSymTab); + if(box64_dump && h->SymTab) DumpSymTab64(head); + + LoadNamedSection(f, h->SHEntries, h->numSHEntries, h->SHStrTab, ".dynamic", "Dynamic", SHT_DYNAMIC, (void**)&h->Dynamic, &h->numDynamic); + if(box64_dump && h->Dynamic) DumpDynamicSections64(h); + // grab DT_REL & DT_RELA stuffs + // also grab the DT_STRTAB string table + { + for (size_t i=0; inumDynamic; ++i) { + Elf64_Dyn d = h->Dynamic[i]; + Elf64_Word val = d.d_un.d_val; + Elf64_Addr ptr = d.d_un.d_ptr; + switch (d.d_tag) { + case DT_REL: + h->rel = ptr; + break; + case DT_RELSZ: + h->relsz = val; + break; + case DT_RELENT: + h->relent = val; + break; + case DT_RELA: + h->rela = ptr; + break; + case DT_RELASZ: + h->relasz = val; + break; + case DT_RELAENT: + h->relaent = val; + break; + case DT_RELR: + h->relr = ptr; + break; + case DT_RELRSZ: + h->relrsz = val; + break; + case DT_RELRENT: + h->relrent = val; + break; + case DT_PLTGOT: + h->pltgot = ptr; + break; + case DT_PLTREL: + h->pltrel = val; + break; + case DT_PLTRELSZ: + h->pltsz = val; + break; + case DT_JMPREL: + h->jmprel = ptr; + break; + case DT_STRTAB: + h->DynStrTab = (char*)(ptr); + break; + case DT_STRSZ: + h->szDynStrTab = val; + break; + case DT_INIT: // Entry point + h->initentry = ptr; + printf_dump(LOG_DEBUG, "The DT_INIT is at address %p\n", (void*)h->initentry); + break; + case DT_INIT_ARRAY: + h->initarray = ptr; + printf_dump(LOG_DEBUG, "The DT_INIT_ARRAY is at address %p\n", (void*)h->initarray); + break; + case DT_INIT_ARRAYSZ: + h->initarray_sz = val / sizeof(Elf64_Addr); + printf_dump(LOG_DEBUG, "The DT_INIT_ARRAYSZ is %zu\n", h->initarray_sz); + break; + case DT_PREINIT_ARRAYSZ: + #ifndef ANDROID + if(val) + printf_log(LOG_NONE, "Warning, PreInit Array (size=%d) present and ignored!\n", val); + #endif + break; + case DT_FINI: // Exit hook + h->finientry = ptr; + printf_dump(LOG_DEBUG, "The DT_FINI is at address %p\n", (void*)h->finientry); + break; + case DT_FINI_ARRAY: + h->finiarray = ptr; + printf_dump(LOG_DEBUG, "The DT_FINI_ARRAY is at address %p\n", (void*)h->finiarray); + break; + case DT_FINI_ARRAYSZ: + h->finiarray_sz = val / sizeof(Elf64_Addr); + printf_dump(LOG_DEBUG, "The DT_FINI_ARRAYSZ is %zu\n", h->finiarray_sz); + break; + case DT_VERNEEDNUM: + h->szVerNeed = val; + printf_dump(LOG_DEBUG, "The DT_VERNEEDNUM is %d\n", h->szVerNeed); + break; + case DT_VERNEED: + h->VerNeed = (Elf64_Verneed*)ptr; + printf_dump(LOG_DEBUG, "The DT_VERNEED is at address %p\n", h->VerNeed); + break; + case DT_VERDEFNUM: + h->szVerDef = val; + printf_dump(LOG_DEBUG, "The DT_VERDEFNUM is %d\n", h->szVerDef); + break; + case DT_VERDEF: + h->VerDef = (Elf64_Verdef*)ptr; + printf_dump(LOG_DEBUG, "The DT_VERDEF is at address %p\n", h->VerDef); + break; + case DT_FLAGS: + h->flags = val; + printf_dump(LOG_DEBUG, "The DT_FLAGS is 0x%x\n", h->flags); + break; + case DT_HASH: + h->hash = ptr; + printf_dump(LOG_DEBUG, "The DT_HASH is at address %p\n", (void*)h->hash); + break; + case DT_GNU_HASH: + h->gnu_hash = ptr; + printf_dump(LOG_DEBUG, "The DT_GNU_HASH is at address %p\n", (void*)h->gnu_hash); + break; + } + } + if(h->rel) { + if(h->relent != sizeof(Elf64_Rel)) { + printf_log(LOG_NONE, "Rel Table Entry size invalid (0x%x should be 0x%zx)\n", h->relent, sizeof(Elf64_Rel)); + FreeElfHeader(&head); + return NULL; + } + printf_dump(LOG_DEBUG, "Rel Table @%p (0x%zx/0x%x)\n", (void*)h->rel, h->relsz, h->relent); + } + if(h->rela) { + if(h->relaent != sizeof(Elf64_Rela)) { + printf_log(LOG_NONE, "RelA Table Entry size invalid (0x%x should be 0x%zx)\n", h->relaent, sizeof(Elf64_Rela)); + FreeElfHeader(&head); + return NULL; + } + printf_dump(LOG_DEBUG, "RelA Table @%p (0x%zx/0x%x)\n", (void*)h->rela, h->relasz, h->relaent); + } + if(h->jmprel) { + if(h->pltrel == DT_REL) { + h->pltent = sizeof(Elf64_Rel); + } else if(h->pltrel == DT_RELA) { + h->pltent = sizeof(Elf64_Rela); + } else { + printf_log(LOG_NONE, "PLT Table type is unknown (size = 0x%zx, type=%ld)\n", h->pltsz, h->pltrel); + FreeElfHeader(&head); + return NULL; + } + if((h->pltsz / h->pltent)*h->pltent != h->pltsz) { + printf_log(LOG_NONE, "PLT Table Entry size invalid (0x%zx, ent=0x%x, type=%ld)\n", h->pltsz, h->pltent, h->pltrel); + FreeElfHeader(&head); + return NULL; + } + printf_dump(LOG_DEBUG, "PLT Table @%p (type=%ld 0x%zx/0x%0x)\n", (void*)h->jmprel, h->pltrel, h->pltsz, h->pltent); + } + } + // look for PLT Offset + int ii = FindSection(h->SHEntries, h->numSHEntries, h->SHStrTab, ".got.plt"); + if(ii) { + h->gotplt = h->SHEntries[ii].sh_addr; + h->gotplt_end = h->gotplt + h->SHEntries[ii].sh_size; + printf_dump(LOG_DEBUG, "The GOT.PLT Table is at address %p\n", (void*)h->gotplt); + } + ii = FindSection(h->SHEntries, h->numSHEntries, h->SHStrTab, ".got"); + if(ii) { + h->got = h->SHEntries[ii].sh_addr; + h->got_end = h->got + h->SHEntries[ii].sh_size; + printf_dump(LOG_DEBUG, "The GOT Table is at address %p..%p\n", (void*)h->got, (void*)h->got_end); + } + ii = FindSection(h->SHEntries, h->numSHEntries, h->SHStrTab, ".plt"); + if(ii) { + h->plt = h->SHEntries[ii].sh_addr; + h->plt_end = h->plt + h->SHEntries[ii].sh_size; + printf_dump(LOG_DEBUG, "The PLT Table is at address %p..%p\n", (void*)h->plt, (void*)h->plt_end); + } + // grab version of symbols + ii = FindSection(h->SHEntries, h->numSHEntries, h->SHStrTab, ".gnu.version"); + if(ii) { + h->VerSym = (Elf64_Half*)(h->SHEntries[ii].sh_addr); + printf_dump(LOG_DEBUG, "The .gnu.version is at address %p\n", h->VerSym); + } + // grab .text for main code + ii = FindSection(h->SHEntries, h->numSHEntries, h->SHStrTab, ".text"); + if(ii) { + h->text = (uintptr_t)(h->SHEntries[ii].sh_addr); + h->textsz = h->SHEntries[ii].sh_size; + printf_dump(LOG_DEBUG, "The .text is at address %p, and is %zu big\n", (void*)h->text, h->textsz); + } + ii = FindSection(h->SHEntries, h->numSHEntries, h->SHStrTab, ".eh_frame"); + if(ii) { + h->ehframe = (uintptr_t)(h->SHEntries[ii].sh_addr); + h->ehframe_end = h->ehframe + h->SHEntries[ii].sh_size; + printf_dump(LOG_DEBUG, "The .eh_frame section is at address %p..%p\n", (void*)h->ehframe, (void*)h->ehframe_end); + } + ii = FindSection(h->SHEntries, h->numSHEntries, h->SHStrTab, ".eh_frame_hdr"); + if(ii) { + h->ehframehdr = (uintptr_t)(h->SHEntries[ii].sh_addr); + printf_dump(LOG_DEBUG, "The .eh_frame_hdr section is at address %p\n", (void*)h->ehframehdr); + } + + LoadNamedSection(f, h->SHEntries, h->numSHEntries, h->SHStrTab, ".dynstr", "DynSym Strings", SHT_STRTAB, (void**)&h->DynStr, NULL); + LoadNamedSection(f, h->SHEntries, h->numSHEntries, h->SHStrTab, ".dynsym", "DynSym", SHT_DYNSYM, (void**)&h->DynSym, &h->numDynSym); + } + + return head; +} + +const char* BindSym64(int bind) +{ + switch(bind) { + case STB_GLOBAL: return "STB_GLOBAL"; + case STB_WEAK: return "STB_WEAK"; + case STB_LOCAL: return "STB_LOCAL"; + case STB_GNU_UNIQUE: return "STB_GNU_UNIQUE"; + } + static char tmp[50]; + sprintf(tmp, "??? 0x%x", bind); + return tmp; +} diff --git a/src/emu/x64int3.c b/src/emu/x64int3.c index 2b907c35..dd1b439c 100644 --- a/src/emu/x64int3.c +++ b/src/emu/x64int3.c @@ -127,14 +127,14 @@ void x64Int3(x64emu_t* emu, uintptr_t* addr) const char *s = bridge->name; if(!s) s = GetNativeName((void*)a); - if(a==(uintptr_t)PltResolver) { + if(a==(uintptr_t)PltResolver64) { if(cycle_log) { uintptr_t addr = *((uint64_t*)(R_RSP)); int slot = *((uint64_t*)(R_RSP+8)); elfheader_t *h = (elfheader_t*)addr; Elf64_Rela * rel = (Elf64_Rela *)(h->jmprel + h->delta) + slot; - Elf64_Sym *sym = &h->DynSym[ELF64_R_SYM(rel->r_info)]; - const char* symname = SymName(h, sym); + Elf64_Sym *sym = &h->DynSym._64[ELF64_R_SYM(rel->r_info)]; + const char* symname = SymName64(h, sym); snprintf(buff, 256, "%04d|PltResolver \"%s\"", tid, symname?symname:"???"); } else { snprintf(buff, 256, "%s", " ... "); diff --git a/src/include/box32.h b/src/include/box32.h new file mode 100644 index 00000000..ab8fa184 --- /dev/null +++ b/src/include/box32.h @@ -0,0 +1,112 @@ +#ifndef __BOX32_64__H_ +#define __BOX32_64__H_ + +#include +#include + +#ifndef BOX32_DEF +#define BOX32_DEF +typedef uint32_t ptr_t; +typedef int32_t long_t; +typedef uint32_t ulong_t; +#endif + +#define TEST32 +#define TEST_ABORT + +static inline uintptr_t from_ptr(ptr_t p) { + return (uintptr_t)p; +} +static inline void* from_ptrv(ptr_t p) { + return (void*)(uintptr_t)p; +} +static inline long from_long(long_t l) { + return (long)l; +} +static inline unsigned long from_ulong(ulong_t l) { + return (unsigned long)l; +} +uintptr_t from_hash(ulong_t l); +uintptr_t from_hash_d(ulong_t l); +#ifdef TEST32 +#include "debug.h" + +static inline ptr_t to_ptr(uintptr_t p) { + if(p!=0xffffffffffffffffLL && (p>>32)) { + printf_log(LOG_NONE, "Warning, pointer %p is not a 32bits value\n", (void*)p); + #ifdef TEST_ABORT + abort(); + #endif + } + return (ptr_t)p; +} +static inline ptr_t to_ptrv(void* p2) { + uintptr_t p = (uintptr_t)p2; + if(p!=0xffffffffffffffffLL && (p>>32)) { + printf_log(LOG_NONE, "Warning, pointer %p is not a 32bits value\n", (void*)p); + #ifdef TEST_ABORT + abort(); + #endif + } + return (ptr_t)p; +} +static inline long_t to_long(long l) { + long_t ret = (long_t)l; + if(l!=ret) + printf_log(LOG_NONE, "Warning, long %ld is not a 32bits value\n", l); + return ret; +} +static inline ulong_t to_ulong(unsigned long l) { + if(l!=0xffffffffffffffffLL && (l>>32)) + printf_log(LOG_NONE, "Warning, long 0x%p is not a 32bits value\n", (void*)l); + return (ulong_t)l; +} +#else //TEST32 +static inline ptr_t to_ptr(uintptr_t p) { + return (ptr_t)p; +} +static inline ptr_t to_ptrv(void* p) { + return (ptr_t)(uintptr_t)p; +} +static inline long_t to_long(long l) { + return (long_t)l; +} +static inline ulong_t to_ulong(unsigned long l) { + return (ulong_t)l; +} +#endif //TEST32 + +static inline ptr_t to_ptr_silent(uintptr_t p) { + return (ptr_t)p; +} +static inline ptr_t to_ptrv_silent(void* p) { + return (ptr_t)(uintptr_t)p; +} +// indirect l -> T +#define from_ptri(T, l) *(T*)from_ptr(l) +// indirect l -> void* +static inline void* from_ptriv(ptr_t l) { + return from_ptrv(from_ptri(ptr_t, l)); +} + +ulong_t to_hash(uintptr_t p); +ulong_t to_hash_d(uintptr_t p); +static inline ulong_t to_hashv(void* p) {return to_hash((uintptr_t)p);} +static inline ulong_t to_hashv_d(uintptr_t p) {return to_hash_d((uintptr_t)p);} + +void* from_locale(ptr_t l); +void* from_locale_d(ptr_t l); +ptr_t to_locale(void* p); +ptr_t to_locale_d(void* p); + +void init_hash_helper(); +void fini_hash_helper(); + +typedef struct x86emu_s x86emu_t; + +void* my_mmap(x86emu_t* emu, void* addr, unsigned long length, int prot, int flags, int fd, int offset); +void* my_mmap64(x86emu_t* emu, void *addr, unsigned long length, int prot, int flags, int fd, int64_t offset); +int my_munmap(x86emu_t* emu, void* addr, unsigned long length); +int my_mprotect(x86emu_t* emu, void *addr, unsigned long len, int prot); + +#endif //__BOX32_64__H_ diff --git a/src/include/elfload_dump.h b/src/include/elfload_dump.h index ad2ecd71..74906b5e 100644 --- a/src/include/elfload_dump.h +++ b/src/include/elfload_dump.h @@ -3,22 +3,17 @@ typedef struct elfheader_s elfheader_t; -const char* DumpSection(Elf64_Shdr *s, char* SST); -const char* DumpDynamic(Elf64_Dyn *s); -const char* DumpPHEntry(Elf64_Phdr *e); -const char* DumpSym(elfheader_t *h, Elf64_Sym* sym, int version); -const char* DumpRelType(int t); -const char* SymName(elfheader_t *h, Elf64_Sym* sym); -const char* IdxSymName(elfheader_t *h, int sym); -void DumpMainHeader(Elf64_Ehdr *header, elfheader_t *h); -void DumpSymTab(elfheader_t *h); -void DumpDynamicSections(elfheader_t *h); +const char* SymName64(elfheader_t *h, Elf64_Sym* sym); +const char* DumpRelType64(int t); +void DumpMainHeader64(Elf64_Ehdr *header, elfheader_t *h); +void DumpSymTab64(elfheader_t *h); +void DumpDynamicSections64(elfheader_t *h); void DumpDynamicNeeded(elfheader_t *h); void DumpDynamicRPath(elfheader_t *h); -void DumpDynSym(elfheader_t *h); -void DumpRelTable(elfheader_t *h, int cnt, Elf64_Rel *rel, const char* name); -void DumpRelATable(elfheader_t *h, int cnt, Elf64_Rela *rela, const char* name); -void DumpRelRTable(elfheader_t *h, int cnt, Elf64_Relr *relr, const char *name); +void DumpDynSym64(elfheader_t *h); +void DumpRelTable64(elfheader_t *h, int cnt, Elf64_Rel *rel, const char* name); +void DumpRelATable64(elfheader_t *h, int cnt, Elf64_Rela *rela, const char* name); +void DumpRelRTable64(elfheader_t *h, int cnt, Elf64_Relr *relr, const char *name); void DumpBinary(char* p, int sz); diff --git a/src/include/elfloader.h b/src/include/elfloader.h index 770709c8..2bce3c64 100644 --- a/src/include/elfloader.h +++ b/src/include/elfloader.h @@ -91,7 +91,7 @@ int GetNeededVersionForLib(elfheader_t* h, const char* libname, const char* ver) void* ElfGetLocalSymbolStartEnd(elfheader_t* head, uintptr_t *offs, uintptr_t *sz, const char* symname, int* ver, const char** vername, int local, int* veropt); void* ElfGetGlobalSymbolStartEnd(elfheader_t* head, uintptr_t *offs, uintptr_t *sz, const char* symname, int* ver, const char** vername, int local, int* veropt); void* ElfGetWeakSymbolStartEnd(elfheader_t* head, uintptr_t *offs, uintptr_t *sz, const char* symname, int* ver, const char** vername, int local, int* veropt); -int ElfGetSymTabStartEnd(elfheader_t* head, uintptr_t *offs, uintptr_t *end, const char* symname); +int ElfGetSymTabStartEnd64(elfheader_t* head, uintptr_t *offs, uintptr_t *end, const char* symname); void* GetNativeSymbolUnversioned(void* lib, const char* name); diff --git a/src/include/x64run.h b/src/include/x64run.h index b6c9e960..a5d4528e 100644 --- a/src/include/x64run.h +++ b/src/include/x64run.h @@ -9,8 +9,8 @@ int RunTest(x64test_t *test); void DynaRun(x64emu_t *emu); uint32_t LibSyscall(x64emu_t *emu); -void PltResolver(x64emu_t* emu); -extern uintptr_t pltResolver; +void PltResolver64(x64emu_t* emu); +extern uintptr_t pltResolver64; int GetTID(void); #endif //__X64RUN_H_ \ No newline at end of file diff --git a/src/librarian/librarian.c b/src/librarian/librarian.c index 56a0fffa..b836e4ca 100644 --- a/src/librarian/librarian.c +++ b/src/librarian/librarian.c @@ -709,8 +709,12 @@ int GetSymTabStartEnd(lib_t* maplib, const char* name, uintptr_t* start, uintptr return 0; for(int i=0; ilibsz; ++i) { elfheader_t* h = GetElf(maplib->libraries[i]); - if(h && ElfGetSymTabStartEnd(h, start, end, name)) - return 1; + if(box64_is32bits) { + /* TODO */ + } else { + if(h && ElfGetSymTabStartEnd64(h, start, end, name)) + return 1; + } } return 0; } diff --git a/src/mallochook.c b/src/mallochook.c index f63a85bd..abdadc8d 100644 --- a/src/mallochook.c +++ b/src/mallochook.c @@ -776,11 +776,15 @@ void checkHookedSymbols(elfheader_t* h) int hooked_symtab = 0; if(box64_malloc_hack==1) return; + if(box64_is32bits) { + /* TODO? */ + return; + } for (size_t i=0; inumSymTab; ++i) { - int type = ELF64_ST_TYPE(h->SymTab[i].st_info); - int sz = ELF64_ST_TYPE(h->SymTab[i].st_size); - if((type==STT_FUNC) && sz && (h->SymTab[i].st_shndx!=0 && h->SymTab[i].st_shndx<=65521)) { - const char * symname = h->StrTab+h->SymTab[i].st_name; + int type = ELF64_ST_TYPE(h->SymTab._64[i].st_info); + int sz = ELF64_ST_TYPE(h->SymTab._64[i].st_size); + if((type==STT_FUNC) && sz && (h->SymTab._64[i].st_shndx!=0 && h->SymTab._64[i].st_shndx<=65521)) { + const char * symname = h->StrTab+h->SymTab._64[i].st_name; #define GO(A, B) if(!strcmp(symname, #A)) ++hooked; else if(!strcmp(symname, "__libc_" #A)) ++hooked; #define GO2(A, B) SUPER() @@ -790,14 +794,14 @@ void checkHookedSymbols(elfheader_t* h) } if(hooked<2) { for (size_t i=0; inumDynSym && hooked<2; ++i) { - const char * symname = h->DynStr+h->DynSym[i].st_name; - int bind = ELF64_ST_BIND(h->DynSym[i].st_info); - int type = ELF64_ST_TYPE(h->DynSym[i].st_info); - int vis = h->DynSym[i].st_other&0x3; + const char * symname = h->DynStr+h->DynSym._64[i].st_name; + int bind = ELF64_ST_BIND(h->DynSym._64[i].st_info); + int type = ELF64_ST_TYPE(h->DynSym._64[i].st_info); + int vis = h->DynSym._64[i].st_other&0x3; if((type==STT_FUNC) - && (vis==STV_DEFAULT || vis==STV_PROTECTED) && (h->DynSym[i].st_shndx!=0 && h->DynSym[i].st_shndx<=65521)) { - uintptr_t offs = h->DynSym[i].st_value + h->delta; - size_t sz = h->DynSym[i].st_size; + && (vis==STV_DEFAULT || vis==STV_PROTECTED) && (h->DynSym._64[i].st_shndx!=0 && h->DynSym._64[i].st_shndx<=65521)) { + uintptr_t offs = h->DynSym._64[i].st_value + h->delta; + size_t sz = h->DynSym._64[i].st_size; if(bind!=STB_LOCAL && bind!=STB_WEAK && sz>=sizeof(reloc_jmp_t)) { #define GO(A, B) if(!strcmp(symname, #A)) ++hooked; else if(!strcmp(symname, "__libc_" #A)) ++hooked; #define GO2(A, B) @@ -814,11 +818,11 @@ void checkHookedSymbols(elfheader_t* h) printf_log(LOG_INFO, "Redirecting overridden malloc%s from %s function for %s\n", malloc_hack_2?" with hack":"", hooked_symtab?"symtab":"dynsym", ElfName(h)); if(hooked_symtab) { for (size_t i=0; inumSymTab; ++i) { - int type = ELF64_ST_TYPE(h->SymTab[i].st_info); + int type = ELF64_ST_TYPE(h->SymTab._64[i].st_info); if(type==STT_FUNC) { - const char * symname = h->StrTab+h->SymTab[i].st_name; - uintptr_t offs = h->SymTab[i].st_value + h->delta; - size_t sz = h->SymTab[i].st_size; + const char * symname = h->StrTab+h->SymTab._64[i].st_name; + uintptr_t offs = h->SymTab._64[i].st_value + h->delta; + size_t sz = h->SymTab._64[i].st_size; #define GO(A, B) if(!strcmp(symname, "__libc_" #A)) {uintptr_t alt = AddCheckBridge(my_context->system, B, A, 0, #A); printf_log(LOG_DEBUG, "Redirecting %s function from %p (%s)\n", symname, (void*)offs, ElfName(h)); addRelocJmp((void*)offs, (void*)alt, sz, "__libc_" #A, h, NULL);} #define GO2(A, B) SUPER() @@ -833,14 +837,14 @@ void checkHookedSymbols(elfheader_t* h) } } else { for (size_t i=0; inumDynSym; ++i) { - const char * symname = h->DynStr+h->DynSym[i].st_name; - int bind = ELF64_ST_BIND(h->DynSym[i].st_info); - int type = ELF64_ST_TYPE(h->DynSym[i].st_info); - int vis = h->DynSym[i].st_other&0x3; + const char * symname = h->DynStr+h->DynSym._64[i].st_name; + int bind = ELF64_ST_BIND(h->DynSym._64[i].st_info); + int type = ELF64_ST_TYPE(h->DynSym._64[i].st_info); + int vis = h->DynSym._64[i].st_other&0x3; if((type==STT_FUNC) - && (vis==STV_DEFAULT || vis==STV_PROTECTED) && (h->DynSym[i].st_shndx!=0 && h->DynSym[i].st_shndx<=65521)) { - uintptr_t offs = h->DynSym[i].st_value + h->delta; - size_t sz = h->DynSym[i].st_size; + && (vis==STV_DEFAULT || vis==STV_PROTECTED) && (h->DynSym._64[i].st_shndx!=0 && h->DynSym._64[i].st_shndx<=65521)) { + uintptr_t offs = h->DynSym._64[i].st_value + h->delta; + size_t sz = h->DynSym._64[i].st_size; if(bind!=STB_LOCAL && bind!=STB_WEAK) { #define GO(A, B) if(!strcmp(symname, "__libc_" #A)) {uintptr_t alt = AddCheckBridge(my_context->system, B, A, 0, #A); printf_log(LOG_DEBUG, "Redirecting %s function from %p (%s)\n", symname, (void*)offs, ElfName(h)); addRelocJmp((void*)offs, (void*)alt, sz, "__libc_" #A, h, NULL);} #define GO2(A, B) -- cgit 1.4.1 From 5b78159cb509ee46f5337300c37c15a3948a2287 Mon Sep 17 00:00:00 2001 From: Yang Liu Date: Sat, 17 Aug 2024 19:15:41 +0800 Subject: [RV64_DYNAREC] Added a warning on LOCK CMPXCHG16B opcode --- src/dynarec/rv64/dynarec_rv64_f0.c | 1 + 1 file changed, 1 insertion(+) (limited to 'src/dynarec') diff --git a/src/dynarec/rv64/dynarec_rv64_f0.c b/src/dynarec/rv64/dynarec_rv64_f0.c index 190ce101..533b14a3 100644 --- a/src/dynarec/rv64/dynarec_rv64_f0.c +++ b/src/dynarec/rv64/dynarec_rv64_f0.c @@ -287,6 +287,7 @@ uintptr_t dynarec64_F0(dynarec_rv64_t* dyn, uintptr_t addr, uintptr_t ip, int ni case 0: if (rex.w) { INST_NAME("LOCK CMPXCHG16B Gq, Eq"); + PASS3(dynarec_log(LOG_NONE, "Warning, LOCK CMPXCHG16B is not well supported on RISC-V and issues are expected.\n")); } else { INST_NAME("LOCK CMPXCHG8B Gq, Eq"); } -- cgit 1.4.1 From 769a9f65a93fa4af19161d65aab5510aab349f58 Mon Sep 17 00:00:00 2001 From: Yang Liu Date: Sat, 17 Aug 2024 20:08:11 +0800 Subject: Slightly improved LOCK CMPXCHG16B opcode --- src/dynarec/rv64/dynarec_rv64_f0.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'src/dynarec') diff --git a/src/dynarec/rv64/dynarec_rv64_f0.c b/src/dynarec/rv64/dynarec_rv64_f0.c index 533b14a3..98220915 100644 --- a/src/dynarec/rv64/dynarec_rv64_f0.c +++ b/src/dynarec/rv64/dynarec_rv64_f0.c @@ -300,17 +300,19 @@ uintptr_t dynarec64_F0(dynarec_rv64_t* dyn, uintptr_t addr, uintptr_t ip, int ni LD(x9, xEmu, offsetof(x64emu_t, context)); ADDI(x9, x9, offsetof(box64context_t, mutex_16b)); ADDI(x4, xZR, 1); - MARKLOCK; + MARK2; AMOSWAP_W(x4, x4, x9, 1, 1); // x4 == 1 if locked - BNEZ_MARKLOCK(x4); + BNEZ_MARK2(x4); SMDMB(); - LD(x2, wback, 0); LD(x3, wback, 8); + MARKLOCK; + LR_D(x2, wback, 1, 1); BNE_MARK(x2, xRAX); BNE_MARK(x3, xRDX); - SD(xRBX, wback, 0); + SC_D(x5, xRBX, wback, 1, 1); + BNEZ_MARKLOCK(x5); SD(xRCX, wback, 8); ORI(xFlags, xFlags, 1< Date: Sat, 17 Aug 2024 21:15:13 +0800 Subject: warn only once --- src/dynarec/rv64/dynarec_rv64_f0.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src/dynarec') diff --git a/src/dynarec/rv64/dynarec_rv64_f0.c b/src/dynarec/rv64/dynarec_rv64_f0.c index 98220915..30533be4 100644 --- a/src/dynarec/rv64/dynarec_rv64_f0.c +++ b/src/dynarec/rv64/dynarec_rv64_f0.c @@ -287,7 +287,9 @@ uintptr_t dynarec64_F0(dynarec_rv64_t* dyn, uintptr_t addr, uintptr_t ip, int ni case 0: if (rex.w) { INST_NAME("LOCK CMPXCHG16B Gq, Eq"); - PASS3(dynarec_log(LOG_NONE, "Warning, LOCK CMPXCHG16B is not well supported on RISC-V and issues are expected.\n")); + static int warned = 0; + PASS3(if (!warned) dynarec_log(LOG_NONE, "Warning, LOCK CMPXCHG16B is not well supported on RISC-V and issues are expected.\n")); + warned = 1; } else { INST_NAME("LOCK CMPXCHG8B Gq, Eq"); } -- cgit 1.4.1