]> granicus.if.org Git - llvm/commitdiff
[WebAssembly] Allow for signed relocation addends
authorSam Clegg <sbc@chromium.org>
Wed, 26 Apr 2017 00:02:31 +0000 (00:02 +0000)
committerSam Clegg <sbc@chromium.org>
Wed, 26 Apr 2017 00:02:31 +0000 (00:02 +0000)
Summary:
Addends are used as offsets to addresses of globals
and can be both positive and negative.  This change
prints libObject in line with the spec and the MC
layer.

Subscribers: jfb, dschuff

Differential Revision: https://reviews.llvm.org/D32507

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@301369 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/ObjectYAML/WasmYAML.h
lib/Object/WasmObjectFile.cpp
lib/ObjectYAML/WasmYAML.cpp
test/ObjectYAML/wasm/code_section.yaml
test/ObjectYAML/wasm/data_section.yaml

index b1af8bbdfa6e97c9b79b52b066973aed9c688c8e..dfeeb8589f829ba822daf3370f5361e0316d28b2 100644 (file)
@@ -88,7 +88,7 @@ struct Relocation {
   RelocType Type;
   uint32_t Index;
   yaml::Hex32 Offset;
-  yaml::Hex32 Addend;
+  int32_t Addend;
 };
 
 struct DataSegment {
index e1b3c4e1f39e54131e358e1dadbb7d51db5e639e..4e1f237e17c3769f9d63034c7c964211b6b981a0 100644 (file)
@@ -316,14 +316,15 @@ Error WasmObjectFile::parseRelocSection(StringRef Name, const uint8_t *Ptr,
     case wasm::R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
     case wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB:
     case wasm::R_WEBASSEMBLY_TABLE_INDEX_I32:
+    case wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB:
       break;
     case wasm::R_WEBASSEMBLY_GLOBAL_ADDR_LEB:
     case wasm::R_WEBASSEMBLY_GLOBAL_ADDR_SLEB:
     case wasm::R_WEBASSEMBLY_GLOBAL_ADDR_I32:
-      Reloc.Addend = readVaruint32(Ptr);
+      Reloc.Addend = readVarint32(Ptr);
       break;
     default:
-      return make_error<GenericBinaryError>("Bad relocation type",
+      return make_error<GenericBinaryError>("Bad relocation type: " + Twine(Reloc.Type),
                                             object_error::parse_failed);
     }
     Section->Relocations.push_back(Reloc);
index 1c7c07ae29e8bc23b12558479e66c763e21d21ce..9b1ff7e5dc1679266e69c178731086b86c5da334 100644 (file)
@@ -223,7 +223,7 @@ void MappingTraits<WasmYAML::Relocation>::mapping(
   IO.mapRequired("Type", Relocation.Type);
   IO.mapRequired("Index", Relocation.Index);
   IO.mapRequired("Offset", Relocation.Offset);
-  IO.mapRequired("Addend", Relocation.Addend);
+  IO.mapOptional("Addend", Relocation.Addend, 0);
 }
 
 void MappingTraits<WasmYAML::LocalDecl>::mapping(
index b75bf7e1cfd55bbd3b719714a7e56676fbee8893..5359d2a100ca2a87f9bdc170a5fb3060b3ccdbeb 100644 (file)
@@ -21,11 +21,9 @@ Sections:
       - Type:            R_WEBASSEMBLY_TABLE_INDEX_SLEB
         Index:           0
         Offset:          0x00000006
-        Addend:          0x00000000
       - Type:            R_WEBASSEMBLY_FUNCTION_INDEX_LEB
         Index:           1
         Offset:          0x00000025
-        Addend:          0x00000000
     Functions:
       - Locals:
          - Type:            I32
@@ -56,11 +54,9 @@ Sections:
 # CHECK:      - Type:            R_WEBASSEMBLY_TABLE_INDEX_SLEB
 # CHECK:        Index:           0
 # CHECK:        Offset:          0x00000006
-# CHECK:        Addend:          0x00000000
 # CHECK:      - Type:            R_WEBASSEMBLY_FUNCTION_INDEX_LEB
 # CHECK:        Index:           1
 # CHECK:        Offset:          0x00000025
-# CHECK:        Addend:          0x00000000
 # CHECK:    Functions:
 # CHECK:      - Locals:
 # CHECK:         - Type:            I32
index 542d0efe42f3d4598a75e73ee772ce5ab2b161c8..b8c65abbff9126fd41a57933caaf31443f16131a 100644 (file)
@@ -13,16 +13,34 @@ Sections:
           Opcode:          I32_CONST
           Value:           4
         Content:         '10001000'
+    Relocations:
+      - Type:            R_WEBASSEMBLY_GLOBAL_ADDR_I32
+        Index:           0
+        Offset:          0x00000006
+        Addend:          8
+      - Type:            R_WEBASSEMBLY_GLOBAL_ADDR_I32
+        Index:           0
+        Offset:          0x00000006
+        Addend:          -6
 ...
 # CHECK: --- !WASM
 # CHECK: FileHeader:
 # CHECK:   Version:           0x00000001
 # CHECK: Sections:
 # CHECK:   - Type:            DATA
-# CHECK:     Segments:
-# CHECK:       - Index:           0
-# CHECK:         Offset:
-# CHECK:           Opcode:          I32_CONST
-# CHECK:           Value:           4
-# CHECK:         Content:         '10001000'
-# CHECK: ...
+# CHECK-NEXT:     Relocations:
+# CHECK-NEXT:      - Type:            R_WEBASSEMBLY_GLOBAL_ADDR_I32
+# CHECK-NEXT:        Index:           0
+# CHECK-NEXT:        Offset:          0x00000006
+# CHECK-NEXT:        Addend:          8
+# CHECK-NEXT:      - Type:            R_WEBASSEMBLY_GLOBAL_ADDR_I32
+# CHECK-NEXT:        Index:           0
+# CHECK-NEXT:        Offset:          0x00000006
+# CHECK-NEXT:        Addend:          -6
+# CHECK-NEXT:     Segments:
+# CHECK-NEXT:       - Index:           0
+# CHECK-NEXT:         Offset:
+# CHECK-NEXT:           Opcode:          I32_CONST
+# CHECK-NEXT:           Value:           4
+# CHECK-NEXT:         Content:         '10001000'
+# CHECK-NEXT: ...