Index: include/llvm/DebugInfo/DWARF/DWARFRelocMap.h =================================================================== --- include/llvm/DebugInfo/DWARF/DWARFRelocMap.h +++ include/llvm/DebugInfo/DWARF/DWARFRelocMap.h @@ -20,8 +20,10 @@ struct RelocAddrEntry { uint64_t SectionIndex; object::RelocationRef Reloc; - object::RelocationResolver Resolver; uint64_t SymbolValue; + Optional Reloc2; + uint64_t SymbolValue2; + object::RelocationResolver Resolver; }; /// In place of applying the relocations to the data we've read from disk we use Index: lib/DebugInfo/DWARF/DWARFContext.cpp =================================================================== --- lib/DebugInfo/DWARF/DWARFContext.cpp +++ lib/DebugInfo/DWARF/DWARFContext.cpp @@ -1525,9 +1525,25 @@ // // TODO Don't store Resolver in every RelocAddrEntry. if (Supports && Supports(Reloc.getType())) { - Map->try_emplace(Reloc.getOffset(), - RelocAddrEntry{SymInfoOrErr->SectionIndex, Reloc, - Resolver, SymInfoOrErr->Address}); + auto I = Map->try_emplace( + Reloc.getOffset(), + RelocAddrEntry{SymInfoOrErr->SectionIndex, Reloc, + SymInfoOrErr->Address, + Optional(), 0, Resolver}); + // If we didn't successfully insert that's because we already had a + // relocation for that offset. Store it as a second relocation in the + // same RelocAddrEntry instead. + if (!I.second) { + RelocAddrEntry &entry = I.first->getSecond(); + if (entry.Reloc2) { + ErrorPolicy EP = HandleError(createError( + "At most two relocations per offset are supported")); + if (EP == ErrorPolicy::Halt) + return; + } + entry.Reloc2 = Reloc; + entry.SymbolValue2 = SymInfoOrErr->Address; + } } else { SmallString<32> Type; Reloc.getTypeName(Type); Index: lib/DebugInfo/DWARF/DWARFDataExtractor.cpp =================================================================== --- lib/DebugInfo/DWARF/DWARFDataExtractor.cpp +++ lib/DebugInfo/DWARF/DWARFDataExtractor.cpp @@ -24,7 +24,10 @@ return A; if (SecNdx) *SecNdx = E->SectionIndex; - return E->Resolver(E->Reloc, E->SymbolValue, A); + uint64_t R = E->Resolver(E->Reloc, E->SymbolValue, A); + if (E->Reloc2) + R = E->Resolver(*E->Reloc2, E->SymbolValue2, R); + return R; } Optional Index: lib/DebugInfo/DWARF/DWARFDebugLine.cpp =================================================================== --- lib/DebugInfo/DWARF/DWARFDebugLine.cpp +++ lib/DebugInfo/DWARF/DWARFDebugLine.cpp @@ -280,7 +280,7 @@ const uint64_t PrologueOffset = *OffsetPtr; clear(); - TotalLength = DebugLineData.getU32(OffsetPtr); + TotalLength = DebugLineData.getRelocatedValue(4, OffsetPtr); if (TotalLength == UINT32_MAX) { FormParams.Format = dwarf::DWARF64; TotalLength = DebugLineData.getU64(OffsetPtr); @@ -305,7 +305,8 @@ SegSelectorSize = DebugLineData.getU8(OffsetPtr); } - PrologueLength = DebugLineData.getUnsigned(OffsetPtr, sizeofPrologueLength()); + PrologueLength = + DebugLineData.getRelocatedValue(sizeofPrologueLength(), OffsetPtr); const uint64_t EndPrologueOffset = PrologueLength + *OffsetPtr; MinInstLength = DebugLineData.getU8(OffsetPtr); if (getVersion() >= 4) @@ -734,7 +735,7 @@ // requires the use of DW_LNS_advance_pc. Such assemblers, however, // can use DW_LNS_fixed_advance_pc instead, sacrificing compression. { - uint16_t PCOffset = DebugLineData.getU16(OffsetPtr); + uint16_t PCOffset = DebugLineData.getRelocatedValue(2, OffsetPtr); State.Row.Address.Address += PCOffset; if (OS) *OS Index: lib/DebugInfo/DWARF/DWARFFormValue.cpp =================================================================== --- lib/DebugInfo/DWARF/DWARFFormValue.cpp +++ lib/DebugInfo/DWARF/DWARFFormValue.cpp @@ -299,7 +299,7 @@ case DW_FORM_data8: case DW_FORM_ref8: case DW_FORM_ref_sup8: - Value.uval = Data.getU64(OffsetPtr); + Value.uval = Data.getRelocatedValue(8, OffsetPtr); break; case DW_FORM_data16: // Treat this like a 16-byte block. Index: lib/DebugInfo/DWARF/DWARFUnit.cpp =================================================================== --- lib/DebugInfo/DWARF/DWARFUnit.cpp +++ lib/DebugInfo/DWARF/DWARFUnit.cpp @@ -241,7 +241,7 @@ IndexEntry = Entry; if (!IndexEntry && Index) IndexEntry = Index->getFromOffset(*offset_ptr); - Length = debug_info.getU32(offset_ptr); + Length = debug_info.getRelocatedValue(4, offset_ptr); // FIXME: Support DWARF64. unsigned SizeOfLength = 4; FormParams.Format = DWARF32; Index: lib/Object/RelocationResolver.cpp =================================================================== --- lib/Object/RelocationResolver.cpp +++ 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}; }