[PATCH] D80855: [lld-macho] Support non-pcrel section relocs

Jez Ng via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri May 29 21:46:29 PDT 2020


int3 created this revision.
int3 added reviewers: ruiu, pcc, MaskRay, smeenai, alexshap, gkm, Ktwu, christylee.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
int3 added a child revision: D80856: [lld-macho] Support X86_64_RELOC_GOT.

Depends on D80854 <https://reviews.llvm.org/D80854>.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D80855

Files:
  lld/MachO/InputFiles.cpp
  lld/test/MachO/relocations.s


Index: lld/test/MachO/relocations.s
===================================================================
--- lld/test/MachO/relocations.s
+++ lld/test/MachO/relocations.s
@@ -29,13 +29,19 @@
 _f:
   movl $0x2000004, %eax # write() syscall
   mov $1, %rdi # stdout
-  leaq _str(%rip), %rsi
+  leaq _str(%rip), %rsi # This generates a pcrel symbol relocation
   mov $21, %rdx # length of str
   syscall
 
   movl $0x2000004, %eax # write() syscall
   mov $1, %rdi # stdout
-  leaq L_.str(%rip), %rsi
+  leaq L_.str(%rip), %rsi # This generates a pcrel section relocation
+  mov $15, %rdx # length of str
+  syscall
+
+  movl $0x2000004, %eax # write() syscall
+  mov $1, %rdi # stdout
+  movq L_.ptr_to_str(%rip), %rsi
   mov $15, %rdx # length of str
   syscall
   ret
@@ -47,3 +53,8 @@
 ## References to this generate a section relocation
 L_.str:
   .asciz "Private symbol\n"
+
+.section __DATA,__const
+L_.ptr_to_str:
+## This generates a non-pcrel section relocation
+  .quad L_.str
Index: lld/MachO/InputFiles.cpp
===================================================================
--- lld/MachO/InputFiles.cpp
+++ lld/MachO/InputFiles.cpp
@@ -185,9 +185,6 @@
       r.target = symbols[rel.r_symbolnum];
       r.addend = rawAddend;
     } else {
-      if (!rel.r_pcrel)
-        fatal("TODO: Only pcrel section relocations are supported");
-
       if (rel.r_symbolnum == 0 || rel.r_symbolnum > subsections.size())
         fatal("invalid section index in relocation for offset " +
               std::to_string(r.offset) + " in section " + sec.sectname +
@@ -195,14 +192,18 @@
 
       SubsectionMap &targetSubsecMap = subsections[rel.r_symbolnum - 1];
       const section_64 &targetSec = sectionHeaders[rel.r_symbolnum - 1];
-      // The implicit addend for pcrel section relocations is the pcrel offset
-      // in terms of the addresses in the input file. Here we adjust it so that
-      // it describes the offset from the start of the target section.
-      // TODO: Figure out what to do for non-pcrel section relocations.
-      // TODO: The offset of 4 is probably not right for ARM64, nor for
-      //       relocations with r_length != 2.
-      uint32_t targetOffset =
-          sec.addr + rel.r_address + 4 + rawAddend - targetSec.addr;
+      uint32_t targetOffset;
+      if (rel.r_pcrel)
+        // The implicit addend for pcrel section relocations is the pcrel offset
+        // in terms of the addresses in the input file. Here we adjust it so
+        // that it describes the offset from the start of the target section.
+        // TODO: The offset of 4 is probably not right for ARM64, nor for
+        //       relocations with r_length != 2.
+        targetOffset =
+            sec.addr + rel.r_address + 4 + rawAddend - targetSec.addr;
+      else
+        // The addend for a non-pcrel relocation is its absolute address.
+        targetOffset = rawAddend - targetSec.addr;
       r.target = findContainingSubsection(targetSubsecMap, &targetOffset);
       r.addend = targetOffset;
     }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D80855.267430.patch
Type: text/x-patch
Size: 3029 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200530/2bf50923/attachment.bin>


More information about the llvm-commits mailing list