[lld] fcde378 - [lld-macho] Support non-pcrel section relocs
Jez Ng via llvm-commits
llvm-commits at lists.llvm.org
Wed Jun 17 20:41:52 PDT 2020
Author: Jez Ng
Date: 2020-06-17T20:41:28-07:00
New Revision: fcde378dcbdf2447e543d75d012bebae91a3d0c6
URL: https://github.com/llvm/llvm-project/commit/fcde378dcbdf2447e543d75d012bebae91a3d0c6
DIFF: https://github.com/llvm/llvm-project/commit/fcde378dcbdf2447e543d75d012bebae91a3d0c6.diff
LOG: [lld-macho] Support non-pcrel section relocs
Summary: Depends on D80854.
Reviewers: ruiu, pcc, MaskRay, smeenai, alexshap, gkm, Ktwu, christylee
Subscribers: llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D80855
Added:
Modified:
lld/MachO/InputFiles.cpp
lld/test/MachO/relocations.s
Removed:
################################################################################
diff --git a/lld/MachO/InputFiles.cpp b/lld/MachO/InputFiles.cpp
index ec46daf91f7e..071d7383949c 100644
--- a/lld/MachO/InputFiles.cpp
+++ b/lld/MachO/InputFiles.cpp
@@ -185,9 +185,6 @@ void InputFile::parseRelocations(const section_64 &sec,
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,19 @@ void InputFile::parseRelocations(const section_64 &sec,
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;
}
diff --git a/lld/test/MachO/relocations.s b/lld/test/MachO/relocations.s
index b1c828d4d469..edaa30893560 100644
--- a/lld/test/MachO/relocations.s
+++ b/lld/test/MachO/relocations.s
@@ -1,6 +1,6 @@
# REQUIRES: x86
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t.o
-# RUN: lld -flavor darwinnew -o %t %t.o
+# RUN: lld -flavor darwinnew -L%S/Inputs/MacOSX.sdk/usr/lib -lSystem -o %t %t.o
# RUN: llvm-objdump --section-headers --syms -d %t | FileCheck %s
# CHECK-LABEL: Sections:
@@ -19,23 +19,33 @@
# CHECK: leaq [[#%u, LSTR_OFF:]](%rip), %rsi
# CHECK-NEXT: [[#%x, CSTRING_ADDR + 22 - LSTR_OFF]]
+# RUN: llvm-objdump --section=__const --full-contents -d %t | FileCheck %s --check-prefix=NONPCREL
+# NONPCREL: Contents of section __const:
+# NONPCREL-NEXT: 100001000 b0030000 01000000 b0030000 01000000
+
.section __TEXT,__text
.globl _main, _f
_main:
- callq _f
+ callq _f # X86_64_RELOC_BRANCH
mov $0, %rax
ret
_f:
movl $0x2000004, %eax # write() syscall
mov $1, %rdi # stdout
- leaq _str(%rip), %rsi
+ leaq _str(%rip), %rsi # Generates a X86_64_RELOC_SIGNED 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 # Generates a X86_64_RELOC_SIGNED pcrel section relocation
+ mov $15, %rdx # length of str
+ syscall
+
+ movl $0x2000004, %eax # write() syscall
+ mov $1, %rdi # stdout
+ movq L_.ptr_1_to_str(%rip), %rsi
mov $15, %rdx # length of str
syscall
ret
@@ -47,3 +57,10 @@ _str:
## References to this generate a section relocation
L_.str:
.asciz "Private symbol\n"
+
+.section __DATA,__const
+## These generate X86_64_RELOC_UNSIGNED non-pcrel section relocations
+L_.ptr_1_to_str:
+ .quad L_.str
+L_.ptr_2_to_str:
+ .quad L_.str
More information about the llvm-commits
mailing list