]> granicus.if.org Git - llvm/commitdiff
[lli/COFF] Set the correct alignment for common symbols
authorDavide Italiano <davide@freebsd.org>
Wed, 2 Nov 2016 17:32:19 +0000 (17:32 +0000)
committerDavide Italiano <davide@freebsd.org>
Wed, 2 Nov 2016 17:32:19 +0000 (17:32 +0000)
Otherwise we set it always to zero, which is not correct,
and we assert inside alignTo (Assertion failed:
Align != 0u && "Align can't be 0.").

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

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

include/llvm/Object/COFF.h
lib/Object/COFFObjectFile.cpp
test/ExecutionEngine/MCJIT/coff-alignment.ll [new file with mode: 0644]

index e2b6beeb6b7da2bc30eb2f45667bdf1d6e7fcbb0..95e496ed35f6ac0898387afcc0877f4a16ab59bf 100644 (file)
@@ -715,6 +715,7 @@ protected:
   void moveSymbolNext(DataRefImpl &Symb) const override;
   Expected<StringRef> getSymbolName(DataRefImpl Symb) const override;
   Expected<uint64_t> getSymbolAddress(DataRefImpl Symb) const override;
+  uint32_t getSymbolAlignment(DataRefImpl Symb) const override;
   uint64_t getSymbolValueImpl(DataRefImpl Symb) const override;
   uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
   uint32_t getSymbolFlags(DataRefImpl Symb) const override;
index 601a7faba32a74228585ce6ee30396b5b6478c37..c95037f4540bbb2a7ea967f02e6b592876c855b3 100644 (file)
@@ -157,6 +157,15 @@ uint64_t COFFObjectFile::getSymbolValueImpl(DataRefImpl Ref) const {
   return getCOFFSymbol(Ref).getValue();
 }
 
+uint32_t COFFObjectFile::getSymbolAlignment(DataRefImpl Ref) const {
+  // MSVC/link.exe seems to align symbols to the next-power-of-2
+  // up to 32 bytes.
+  COFFSymbolRef Symb = getCOFFSymbol(Ref);
+  uint32_t Value = Symb.getValue();
+  return std::min(uint64_t(32),
+                  isPowerOf2_64(Value) ? Value : NextPowerOf2(Value));
+}
+
 Expected<uint64_t> COFFObjectFile::getSymbolAddress(DataRefImpl Ref) const {
   uint64_t Result = getSymbolValue(Ref);
   COFFSymbolRef Symb = getCOFFSymbol(Ref);
diff --git a/test/ExecutionEngine/MCJIT/coff-alignment.ll b/test/ExecutionEngine/MCJIT/coff-alignment.ll
new file mode 100644 (file)
index 0000000..ed45d73
--- /dev/null
@@ -0,0 +1,8 @@
+; RUN: opt -mtriple=x86_64-pc-win32-coff %s -o - | lli
+
+@o = common global i32 0, align 4
+
+define i32 @main() {
+  %patatino = load i32, i32* @o, align 4
+  ret i32 %patatino
+}