From 8696993c4c3a92f2099e6160d516007a543f0116 Mon Sep 17 00:00:00 2001 From: Simon Hausmann Date: Thu, 4 May 2017 16:21:27 +0200 Subject: Fix crash in pre-cross-compiled ARMv7 code when host was 64-bit When encoding negative offsets for relative jumps, we must stay within signed 32-bit range to correctly perform all the different thumb offset encodings correctly. Task-number: QTBUG-60441 Change-Id: I0a7243debbcbc4d557710dddbd39cb97bd702da4 Reviewed-by: Lars Knoll --- src/3rdparty/masm/assembler/ARMv7Assembler.h | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) (limited to 'src/3rdparty') diff --git a/src/3rdparty/masm/assembler/ARMv7Assembler.h b/src/3rdparty/masm/assembler/ARMv7Assembler.h index 615c72fc15..d57e5a7c78 100644 --- a/src/3rdparty/masm/assembler/ARMv7Assembler.h +++ b/src/3rdparty/masm/assembler/ARMv7Assembler.h @@ -2531,12 +2531,18 @@ private: return (instruction[0] == OP_NOP_T2a) && (instruction[1] == OP_NOP_T2b); } + static int32_t makeRelative(const void *target, const void *source) + { + intptr_t difference = reinterpret_cast(target) - reinterpret_cast(source); + return static_cast(difference); + } + static bool canBeJumpT1(const uint16_t* instruction, const void* target) { ASSERT(!(reinterpret_cast(instruction) & 1)); ASSERT(!(reinterpret_cast(target) & 1)); - intptr_t relative = reinterpret_cast(target) - (reinterpret_cast(instruction)); + auto relative = makeRelative(target, instruction); // It does not appear to be documented in the ARM ARM (big surprise), but // for OP_B_T1 the branch displacement encoded in the instruction is 2 // less than the actual displacement. @@ -2549,7 +2555,7 @@ private: ASSERT(!(reinterpret_cast(instruction) & 1)); ASSERT(!(reinterpret_cast(target) & 1)); - intptr_t relative = reinterpret_cast(target) - (reinterpret_cast(instruction)); + auto relative = makeRelative(target, instruction); // It does not appear to be documented in the ARM ARM (big surprise), but // for OP_B_T2 the branch displacement encoded in the instruction is 2 // less than the actual displacement. @@ -2562,7 +2568,7 @@ private: ASSERT(!(reinterpret_cast(instruction) & 1)); ASSERT(!(reinterpret_cast(target) & 1)); - intptr_t relative = reinterpret_cast(target) - (reinterpret_cast(instruction)); + auto relative = makeRelative(target, instruction); return ((relative << 11) >> 11) == relative; } @@ -2571,7 +2577,7 @@ private: ASSERT(!(reinterpret_cast(instruction) & 1)); ASSERT(!(reinterpret_cast(target) & 1)); - intptr_t relative = reinterpret_cast(target) - (reinterpret_cast(instruction)); + auto relative = makeRelative(target, instruction); return ((relative << 7) >> 7) == relative; } @@ -2582,7 +2588,7 @@ private: ASSERT(!(reinterpret_cast(target) & 1)); ASSERT(canBeJumpT1(instruction, target)); - intptr_t relative = reinterpret_cast(target) - (reinterpret_cast(instruction)); + auto relative = makeRelative(target, instruction); // It does not appear to be documented in the ARM ARM (big surprise), but // for OP_B_T1 the branch displacement encoded in the instruction is 2 // less than the actual displacement. @@ -2600,7 +2606,7 @@ private: ASSERT(!(reinterpret_cast(target) & 1)); ASSERT(canBeJumpT2(instruction, target)); - intptr_t relative = reinterpret_cast(target) - (reinterpret_cast(instruction)); + auto relative = makeRelative(target, instruction); // It does not appear to be documented in the ARM ARM (big surprise), but // for OP_B_T2 the branch displacement encoded in the instruction is 2 // less than the actual displacement. @@ -2618,7 +2624,7 @@ private: ASSERT(!(reinterpret_cast(target) & 1)); ASSERT(canBeJumpT3(instruction, target)); - intptr_t relative = reinterpret_cast(target) - (reinterpret_cast(instruction)); + auto relative = makeRelative(target, instruction); // All branch offsets should be an even distance. ASSERT(!(relative & 1)); @@ -2633,7 +2639,7 @@ private: ASSERT(!(reinterpret_cast(target) & 1)); ASSERT(canBeJumpT4(instruction, target)); - intptr_t relative = reinterpret_cast(target) - (reinterpret_cast(instruction)); + auto relative = makeRelative(target, instruction); // ARM encoding for the top two bits below the sign bit is 'peculiar'. if (relative >= 0) relative ^= 0xC00000; -- cgit v1.2.3