[lld] r277846 - COFF ARM: Apply an existing offset in MOV32T relocations

Saleem Abdulrasool via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 5 11:20:32 PDT 2016


Author: compnerd
Date: Fri Aug  5 13:20:31 2016
New Revision: 277846

URL: http://llvm.org/viewvc/llvm-project?rev=277846&view=rev
Log:
COFF ARM: Apply an existing offset in MOV32T relocations

Don't blindly OR in the new value, but clear the existing one, since it can be
nonzero. Read out the existing value before, and add into the desired offset.
(The add is done outside of the applyMOV, to handle potential overflow between
the two.)

Patch by Martin Storsjö!

Modified:
    lld/trunk/COFF/Chunks.cpp
    lld/trunk/test/COFF/reloc-arm.test

Modified: lld/trunk/COFF/Chunks.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Chunks.cpp?rev=277846&r1=277845&r2=277846&view=diff
==============================================================================
--- lld/trunk/COFF/Chunks.cpp (original)
+++ lld/trunk/COFF/Chunks.cpp Fri Aug  5 13:20:31 2016
@@ -81,11 +81,23 @@ void SectionChunk::applyRelX86(uint8_t *
 }
 
 static void applyMOV(uint8_t *Off, uint16_t V) {
-  or16(Off, ((V & 0x800) >> 1) | ((V >> 12) & 0xf));
-  or16(Off + 2, ((V & 0x700) << 4) | (V & 0xff));
+  write16le(Off, (read16le(Off) & 0xfbf0) | ((V & 0x800) >> 1) | ((V >> 12) & 0xf));
+  write16le(Off + 2, (read16le(Off + 2) & 0x8f00) | ((V & 0x700) << 4) | (V & 0xff));
+}
+
+static uint16_t readMOV(uint8_t *Off) {
+  uint16_t Opcode1 = read16le(Off);
+  uint16_t Opcode2 = read16le(Off + 2);
+  uint16_t Imm = (Opcode2 & 0x00ff) | ((Opcode2 >> 4) & 0x0700);
+  Imm |= ((Opcode1 << 1) & 0x0800) | ((Opcode1 & 0x000f) << 12);
+  return Imm;
 }
 
 static void applyMOV32T(uint8_t *Off, uint32_t V) {
+  uint16_t ImmW = readMOV(Off);     // read MOVW operand
+  uint16_t ImmT = readMOV(Off + 4); // read MOVT operand
+  uint32_t Imm = ImmW | (ImmT << 16);
+  V += Imm;                         // add the immediate offset
   applyMOV(Off, V);           // set MOVW operand
   applyMOV(Off + 4, V >> 16); // set MOVT operand
 }

Modified: lld/trunk/test/COFF/reloc-arm.test
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/COFF/reloc-arm.test?rev=277846&r1=277845&r2=277846&view=diff
==============================================================================
--- lld/trunk/test/COFF/reloc-arm.test (original)
+++ lld/trunk/test/COFF/reloc-arm.test Fri Aug  5 13:20:31 2016
@@ -5,7 +5,7 @@
 # CHECK: .text:
 # CHECK: 402000 01104000 00000000 00000000 00000000
 # CHECK: 402010 01100000 00000000 00000000 00000000
-# CHECK: 402020 01000100 00004000 00000000 00000000
+# CHECK: 402020 41f20009 c0f24009 00000000 00000000
 # CHECK: 402030 fe07e62f 00000000 00000000 00000000
 # CHECK: 402040 3e04de2f 00000000 00000000 00000000
 # CHECK: 402050 fe07d62f 00000000 00000000 00000000
@@ -23,7 +23,7 @@ sections:
   - Name:            .text
     Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_PURGEABLE, IMAGE_SCN_MEM_16BIT, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
     Alignment:       4096
-    SectionData:     00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000f000f8000000000000000000000000
+    SectionData:     00000000000000000000000000000000000000000000000000000000000000004ff6ff79cff6ff79000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000f000f8000000000000000000000000
     Relocations:
       - VirtualAddress:  0
         SymbolName:      foo




More information about the llvm-commits mailing list