diff options
author | 2020-01-22 13:58:41 -0500 | |
---|---|---|
committer | 2020-01-22 13:59:19 -0500 | |
commit | 492f428da578e0652dd5d0a621eb8111900d9cc9 (patch) | |
tree | 4ac47fe9f195b027da856f9a59cfd103e90aec54 /src/tools/unidasm.cpp | |
parent | a892c49d4aac224a14e213c200e815585c187b96 (diff) |
unidasm: Handle word endianness for address-shifted architectures correctly and safely
Diffstat (limited to 'src/tools/unidasm.cpp')
-rw-r--r-- | src/tools/unidasm.cpp | 139 |
1 files changed, 87 insertions, 52 deletions
diff --git a/src/tools/unidasm.cpp b/src/tools/unidasm.cpp index 648669f93e3..f45dafcc9b3 100644 --- a/src/tools/unidasm.cpp +++ b/src/tools/unidasm.cpp @@ -604,18 +604,17 @@ unidasm_data_buffer::unidasm_data_buffer(util::disasm_interface *_disasm, const switch(entry->pcshift) { case -1: lr8 = [](offs_t pc) -> u8 { throw std::logic_error("debug_disasm_buffer::debug_data_buffer: r8 access on 16-bits granularity bus\n"); }; - lr16 = [this](offs_t pc) -> u16 { - const u16 *src = get_ptr<u16>(pc); - return src[0]; - }; - switch(entry->endian) { case le: + lr16 = [this](offs_t pc) -> u16 { + const u16 *src = get_ptr<u16>(pc); + return little_endianize_int16(src[0]); + }; lr32 = [this, page_mask](offs_t pc) -> u32 { offs_t lpc = disasm->pc_real_to_linear(pc); u32 r = 0; for(int j=0; j != 2; j++) { - r |= get<u16>(disasm->pc_linear_to_real(lpc)) << (j*16); + r |= little_endianize_int16(get<u16>(disasm->pc_linear_to_real(lpc))) << (j*16); lpc = (lpc & ~page_mask) | ((lpc + 1) & page_mask); } return r; @@ -624,7 +623,7 @@ unidasm_data_buffer::unidasm_data_buffer(util::disasm_interface *_disasm, const offs_t lpc = disasm->pc_real_to_linear(pc); u64 r = 0; for(int j=0; j != 4; j++) { - r |= u64(get<u16>(disasm->pc_linear_to_real(lpc))) << (j*16); + r |= u64(little_endianize_int16(get<u16>(disasm->pc_linear_to_real(lpc)))) << (j*16); lpc = (lpc & ~page_mask) | ((lpc + 1) & page_mask); } return r; @@ -632,11 +631,15 @@ unidasm_data_buffer::unidasm_data_buffer(util::disasm_interface *_disasm, const break; case be: + lr16 = [this](offs_t pc) -> u16 { + const u16 *src = get_ptr<u16>(pc); + return big_endianize_int16(src[0]); + }; lr32 = [this, page_mask](offs_t pc) -> u32 { offs_t lpc = disasm->pc_real_to_linear(pc); u32 r = 0; for(int j=0; j != 2; j++) { - r |= get<u16>(disasm->pc_linear_to_real(lpc)) << ((1-j)*16); + r |= big_endianize_int16(get<u16>(disasm->pc_linear_to_real(lpc))) << ((1-j)*16); lpc = (lpc & ~page_mask) | ((lpc + 1) & page_mask); } return r; @@ -645,7 +648,7 @@ unidasm_data_buffer::unidasm_data_buffer(util::disasm_interface *_disasm, const offs_t lpc = disasm->pc_real_to_linear(pc); u64 r = 0; for(int j=0; j != 4; j++) { - r |= u64(get<u16>(disasm->pc_linear_to_real(lpc))) << ((3-j)*16); + r |= u64(big_endianize_int16(get<u16>(disasm->pc_linear_to_real(lpc)))) << ((3-j)*16); lpc = (lpc & ~page_mask) | ((lpc + 1) & page_mask); } return r; @@ -798,46 +801,52 @@ unidasm_data_buffer::unidasm_data_buffer(util::disasm_interface *_disasm, const case -1: lr8 = [](offs_t pc) -> u8 { abort(); }; - lr16 = [this](offs_t pc) -> u16 { - const u16 *p = get_ptr<u16>(pc); - return p ? - p[0] - : 0x0000; - }; switch(entry->endian) { case le: + lr16 = [this](offs_t pc) -> u16 { + const u16 *p = get_ptr<u16>(pc); + return p ? + little_endianize_int16(p[0]) + : 0x0000; + }; lr32 = [this](offs_t pc) -> u32 { const u16 *p = get_ptr<u16>(pc); return p ? - p[0] | - (p[1] << 16) + little_endianize_int16(p[0]) | + (little_endianize_int16(p[1]) << 16) : 0x00000000; }; lr64 = [this](offs_t pc) -> u64 { const u16 *p = get_ptr<u16>(pc); return p ? - p[0] | - (p[1] << 16) | - (u64(p[2]) << 32) | - (u64(p[3]) << 48) + little_endianize_int16(p[0]) | + (little_endianize_int16(p[1]) << 16) | + (u64(little_endianize_int16(p[2])) << 32) | + (u64(little_endianize_int16(p[3])) << 48) : 0x0000000000000000; }; break; case be: + lr16 = [this](offs_t pc) -> u16 { + const u16 *p = get_ptr<u16>(pc); + return p ? + big_endianize_int16(p[0]) + : 0x0000; + }; lr32 = [this](offs_t pc) -> u32 { const u16 *p = get_ptr<u16>(pc); return p ? - (p[0] << 16)| - p[1] + (big_endianize_int16(p[0]) << 16)| + big_endianize_int16(p[1]) : 0x00000000; }; lr64 = [this](offs_t pc) -> u64 { const u16 *p = get_ptr<u16>(pc); return p ? - (u64(p[0]) << 48) | - (u64(p[1]) << 32) | - (p[2] << 16) | - p[3] + (u64(big_endianize_int16(p[0])) << 48) | + (u64(big_endianize_int16(p[1])) << 32) | + (big_endianize_int16(p[2]) << 16) | + big_endianize_int16(p[3]) : 0x0000000000000000; }; break; @@ -847,28 +856,34 @@ unidasm_data_buffer::unidasm_data_buffer(util::disasm_interface *_disasm, const case -2: lr8 = [](offs_t pc) -> u8 { abort(); }; lr16 = [](offs_t pc) -> u16 { abort(); }; - lr32 = [this](offs_t pc) -> u32 { - const u32 *p = get_ptr<u32>(pc); - return p ? - p[0] - : 0x00000000; - }; switch(entry->endian) { case le: + lr32 = [this](offs_t pc) -> u32 { + const u32 *p = get_ptr<u32>(pc); + return p ? + little_endianize_int32(p[0]) + : 0x00000000; + }; lr64 = [this](offs_t pc) -> u64 { const u32 *p = get_ptr<u32>(pc); return p ? - p[0] | - (u64(p[1]) << 32) + little_endianize_int32(p[0]) | + (u64(little_endianize_int32(p[1])) << 32) : 0x0000000000000000; }; break; case be: + lr32 = [this](offs_t pc) -> u32 { + const u32 *p = get_ptr<u32>(pc); + return p ? + big_endianize_int32(p[0]) + : 0x00000000; + }; lr64 = [this](offs_t pc) -> u64 { const u32 *p = get_ptr<u32>(pc); return p ? - (u64(p[0]) << 32) | - p[1] + (u64(big_endianize_int32(p[0])) << 32) | + big_endianize_int32(p[1]) : 0x0000000000000000; }; break; @@ -879,26 +894,38 @@ unidasm_data_buffer::unidasm_data_buffer(util::disasm_interface *_disasm, const lr8 = [](offs_t pc) -> u8 { abort(); }; lr16 = [](offs_t pc) -> u16 { abort(); }; lr32 = [](offs_t pc) -> u32 { abort(); }; - lr64 = [this](offs_t pc) -> u64 { - const u64 *p = get_ptr<u64>(pc); - return p ? - p[0] - : 0x0000000000000000; + switch(entry->endian) { + case le: + lr64 = [this](offs_t pc) -> u64 { + const u64 *p = get_ptr<u64>(pc); + return p ? + little_endianize_int64(p[0]) + : 0x0000000000000000; + }; + break; + case be: + lr64 = [this](offs_t pc) -> u64 { + const u64 *p = get_ptr<u64>(pc); + return p ? + big_endianize_int64(p[0]) + : 0x0000000000000000; + }; + break; }; break; case 3: lr8 = [](offs_t pc) -> u8 { abort(); }; - lr16 = [this](offs_t pc) -> u16 { - if(pc < base_pc) - return 0x0000; - offs_t delta = (pc - base_pc) >> 3; - if(delta >= size) - return 0x0000; - return reinterpret_cast<const u16 *>(&data[delta])[0]; - }; switch(entry->endian) { case le: + lr16 = [this](offs_t pc) -> u16 { + if(pc < base_pc) + return 0x0000; + offs_t delta = (pc - base_pc) >> 3; + if(delta >= size) + return 0x0000; + return little_endianize_int16(reinterpret_cast<const u16 *>(&data[delta])[0]); + }; lr32 = [this](offs_t pc) -> u32 { if(pc < base_pc) return 0x00000000; @@ -906,10 +933,18 @@ unidasm_data_buffer::unidasm_data_buffer(util::disasm_interface *_disasm, const if(delta >= size + 2) return 0x00000000; auto p = reinterpret_cast<const u16 *>(&data[delta]); - return p[0] | (u32(p[1]) << 16); + return little_endianize_int16(p[0]) | (u32(little_endianize_int16(p[1])) << 16); }; break; case be: + lr16 = [this](offs_t pc) -> u16 { + if(pc < base_pc) + return 0x0000; + offs_t delta = (pc - base_pc) >> 3; + if(delta >= size) + return 0x0000; + return big_endianize_int16(reinterpret_cast<const u16 *>(&data[delta])[0]); + }; lr32 = [this](offs_t pc) -> u32 { if(pc < base_pc) return 0x00000000; @@ -917,7 +952,7 @@ unidasm_data_buffer::unidasm_data_buffer(util::disasm_interface *_disasm, const if(delta >= size + 2) return 0x00000000; auto p = reinterpret_cast<const u16 *>(&data[delta]); - return (u32(p[0]) << 16) | p[1]; + return (u32(big_endianize_int16(p[0])) << 16) | big_endianize_int16(p[1]); }; break; } |