mirror of
				https://github.com/c64scene-ar/llvm-6502.git
				synced 2025-10-31 08:16:47 +00:00 
			
		
		
		
	Fix unaligned memory accesses when performing relocations in X86 JIT. There's
no cost to using memcpy here: the fixed code is optimized by LLVM to perfect machine code. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@162311 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
		| @@ -532,6 +532,15 @@ uintptr_t X86JITInfo::getPICJumpTableEntry(uintptr_t BB, uintptr_t Entry) { | |||||||
| #endif | #endif | ||||||
| } | } | ||||||
|  |  | ||||||
|  | template<typename T> void addUnaligned(void *Pos, T Delta) { | ||||||
|  |   T Value; | ||||||
|  |   std::memcpy(reinterpret_cast<char*>(&Value), reinterpret_cast<char*>(Pos), | ||||||
|  |               sizeof(T)); | ||||||
|  |   Value += Delta; | ||||||
|  |   std::memcpy(reinterpret_cast<char*>(Pos), reinterpret_cast<char*>(&Value), | ||||||
|  |               sizeof(T)); | ||||||
|  | } | ||||||
|  |  | ||||||
| /// relocate - Before the JIT can run a block of code that has been emitted, | /// relocate - Before the JIT can run a block of code that has been emitted, | ||||||
| /// it must rewrite the code to contain the actual addresses of any | /// it must rewrite the code to contain the actual addresses of any | ||||||
| /// referenced global symbols. | /// referenced global symbols. | ||||||
| @@ -545,24 +554,24 @@ void X86JITInfo::relocate(void *Function, MachineRelocation *MR, | |||||||
|       // PC relative relocation, add the relocated value to the value already in |       // PC relative relocation, add the relocated value to the value already in | ||||||
|       // memory, after we adjust it for where the PC is. |       // memory, after we adjust it for where the PC is. | ||||||
|       ResultPtr = ResultPtr -(intptr_t)RelocPos - 4 - MR->getConstantVal(); |       ResultPtr = ResultPtr -(intptr_t)RelocPos - 4 - MR->getConstantVal(); | ||||||
|       *((unsigned*)RelocPos) += (unsigned)ResultPtr; |       addUnaligned<unsigned>(RelocPos, ResultPtr); | ||||||
|       break; |       break; | ||||||
|     } |     } | ||||||
|     case X86::reloc_picrel_word: { |     case X86::reloc_picrel_word: { | ||||||
|       // PIC base relative relocation, add the relocated value to the value |       // PIC base relative relocation, add the relocated value to the value | ||||||
|       // already in memory, after we adjust it for where the PIC base is. |       // already in memory, after we adjust it for where the PIC base is. | ||||||
|       ResultPtr = ResultPtr - ((intptr_t)Function + MR->getConstantVal()); |       ResultPtr = ResultPtr - ((intptr_t)Function + MR->getConstantVal()); | ||||||
|       *((unsigned*)RelocPos) += (unsigned)ResultPtr; |       addUnaligned<unsigned>(RelocPos, ResultPtr); | ||||||
|       break; |       break; | ||||||
|     } |     } | ||||||
|     case X86::reloc_absolute_word: |     case X86::reloc_absolute_word: | ||||||
|     case X86::reloc_absolute_word_sext: |     case X86::reloc_absolute_word_sext: | ||||||
|       // Absolute relocation, just add the relocated value to the value already |       // Absolute relocation, just add the relocated value to the value already | ||||||
|       // in memory. |       // in memory. | ||||||
|       *((unsigned*)RelocPos) += (unsigned)ResultPtr; |       addUnaligned<unsigned>(RelocPos, ResultPtr); | ||||||
|       break; |       break; | ||||||
|     case X86::reloc_absolute_dword: |     case X86::reloc_absolute_dword: | ||||||
|       *((intptr_t*)RelocPos) += ResultPtr; |       addUnaligned<intptr_t>(RelocPos, ResultPtr); | ||||||
|       break; |       break; | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user