[DWARF][RISCV] Add support for RISC-V relocations needed for debug info
When code relaxation is enabled many RISC-V fixups are not resolved but
instead relocations are emitted. This happens even for DWARF debug
sections. Therefore, to properly support the parsing of DWARF debug info
we need to be able to resolve RISC-V relocations. This patch adds:
* Support for RISC-V relocations in RelocationResolver
* DWARF support for two relocations per object file offset
* DWARF changes to support relocations in more DIE fields
The two relocations per offset change is needed because some RISC-V
relocations (used for label differences) come in pairs.
Relocations can also be emitted for DWARF fields where relocations were
not yet evaluated. Adding relocation support for some of these fields is
essencial. On the other hand, LLVM currently emits RISC-V relocations
for fixups that could be safely evaluated, since they can never be
affected by code relaxations. This patch also adds relocation support
for the fields affected by those extraneous relocations (the DWARF unit
entry Length, and the DWARF debug line entry TotalLength and
PrologueLength), for testing purposes.
Differential Revision: https://reviews.llvm.org/D62062
Patch by Luís Marques.
llvm-svn: 366402
diff --git a/llvm/lib/Object/RelocationResolver.cpp b/llvm/lib/Object/RelocationResolver.cpp
index 414165c..0a243f3 100644
--- a/llvm/lib/Object/RelocationResolver.cpp
+++ b/llvm/lib/Object/RelocationResolver.cpp
@@ -330,6 +330,55 @@
llvm_unreachable("Invalid relocation type");
}
+static bool supportsRISCV(uint64_t Type) {
+ switch (Type) {
+ case ELF::R_RISCV_NONE:
+ case ELF::R_RISCV_32:
+ case ELF::R_RISCV_64:
+ case ELF::R_RISCV_ADD8:
+ case ELF::R_RISCV_SUB8:
+ case ELF::R_RISCV_ADD16:
+ case ELF::R_RISCV_SUB16:
+ case ELF::R_RISCV_ADD32:
+ case ELF::R_RISCV_SUB32:
+ case ELF::R_RISCV_ADD64:
+ case ELF::R_RISCV_SUB64:
+ return true;
+ default:
+ return false;
+ }
+}
+
+static uint64_t resolveRISCV(RelocationRef R, uint64_t S, uint64_t A) {
+ int64_t RA = getELFAddend(R);
+ switch (R.getType()) {
+ case ELF::R_RISCV_NONE:
+ return A;
+ case ELF::R_RISCV_32:
+ return (S + RA) & 0xFFFFFFFF;
+ case ELF::R_RISCV_64:
+ return S + RA;
+ case ELF::R_RISCV_ADD8:
+ return (A + (S + RA)) & 0xFF;
+ case ELF::R_RISCV_SUB8:
+ return (A - (S + RA)) & 0xFF;
+ case ELF::R_RISCV_ADD16:
+ return (A + (S + RA)) & 0xFFFF;
+ case ELF::R_RISCV_SUB16:
+ return (A - (S + RA)) & 0xFFFF;
+ case ELF::R_RISCV_ADD32:
+ return (A + (S + RA)) & 0xFFFFFFFF;
+ case ELF::R_RISCV_SUB32:
+ return (A - (S + RA)) & 0xFFFFFFFF;
+ case ELF::R_RISCV_ADD64:
+ return (A + (S + RA));
+ case ELF::R_RISCV_SUB64:
+ return (A - (S + RA));
+ default:
+ llvm_unreachable("Invalid relocation type");
+ }
+}
+
static bool supportsCOFFX86(uint64_t Type) {
switch (Type) {
case COFF::IMAGE_REL_I386_SECREL:
@@ -449,6 +498,8 @@
return {supportsSparc64, resolveSparc64};
case Triple::amdgcn:
return {supportsAmdgpu, resolveAmdgpu};
+ case Triple::riscv64:
+ return {supportsRISCV, resolveRISCV};
default:
return {nullptr, nullptr};
}
@@ -477,6 +528,8 @@
return {supportsSparc32, resolveSparc32};
case Triple::hexagon:
return {supportsHexagon, resolveHexagon};
+ case Triple::riscv32:
+ return {supportsRISCV, resolveRISCV};
default:
return {nullptr, nullptr};
}