[PATCH] D23057: [COFF][ARM] Apply an existing offset in MOV32T relocations

Martin Storsjö via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 2 04:57:56 PDT 2016


mstorsjo created this revision.
mstorsjo added reviewers: peter.smith, zatrazz.
mstorsjo added a subscriber: llvm-commits.
Herald added subscribers: samparker, rengolin, aemerson.

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.)


https://reviews.llvm.org/D23057

Files:
  COFF/Chunks.cpp
  test/COFF/reloc-arm.test

Index: test/COFF/reloc-arm.test
===================================================================
--- test/COFF/reloc-arm.test
+++ test/COFF/reloc-arm.test
@@ -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 @@
   - 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
Index: COFF/Chunks.cpp
===================================================================
--- COFF/Chunks.cpp
+++ COFF/Chunks.cpp
@@ -81,11 +81,23 @@
 }
 
 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
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D23057.66459.patch
Type: text/x-patch
Size: 2477 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160802/8ec4a333/attachment.bin>


More information about the llvm-commits mailing list