[lld] [lld][ELF] Fix a corner case of elf::getLoongArchPageDelta (PR #71907)

Lu Weining via llvm-commits llvm-commits at lists.llvm.org
Sat Nov 18 08:18:40 PST 2023


SixWeining wrote:

> This special casing is really ugly and should not be needed, surely, if you just do the arithmetic in the right order and correctly. Can we not just do the same as mold?

How about using the approach proposed by @heiher in https://github.com/llvm/llvm-project/pull/71907#issuecomment-1812789284 ?
```
--- a/lld/ELF/Arch/LoongArch.cpp
+++ b/lld/ELF/Arch/LoongArch.cpp
@@ -156,16 +156,9 @@ uint64_t elf::getLoongArchPageDelta(uint64_t dest, uint64_t pc) {
   //     i = -0x10000'0000, j = 0, k = -0x1000
   //     result = page(dest) - page(pc) + 0x1000
   uint64_t result = getLoongArchPage(dest) - getLoongArchPage(pc);
-  bool negativeA = lo12(dest) > 0x7ff;
-  bool negativeB = (result & 0x8000'0000) != 0;
-
-  if (negativeA)
-    result += 0x1000;
-  if (negativeA && !negativeB)
-    result -= 0x10000'0000;
-  else if (!negativeA && negativeB)
-    result += 0x10000'0000;
-
+  result += ((dest & 0x800) << 1);
+  result += ((result & 0x8000'0000) << 1);
+  result -= ((dest & 0x800) << 21);
   return result;
 }
```

https://github.com/llvm/llvm-project/pull/71907


More information about the llvm-commits mailing list