Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix relocs errors on riscv64 #111317

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/coreclr/jit/emitriscv64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1627,7 +1627,11 @@ unsigned emitter::emitOutputCall(const insGroup* ig, BYTE* dst, instrDesc* id, c
int reg2 = ((int)addr & 1) + 10;
addr = addr ^ 1;

assert(isValidSimm32(addr - (ssize_t)dst));
if (!emitComp->opts.compReloc)
{
assert(isValidSimm32(addr - (ssize_t)dst));
am11 marked this conversation as resolved.
Show resolved Hide resolved
}

assert((addr & 1) == 0);

dst += 4;
Expand All @@ -1642,7 +1646,7 @@ unsigned emitter::emitOutputCall(const insGroup* ig, BYTE* dst, instrDesc* id, c
#endif
emitOutput_Instr(dst, 0x00000067 | (REG_DEFAULT_HELPER_CALL_TARGET << 15) | reg2 << 7);

emitRecordRelocation(dst - 4, (BYTE*)addr, IMAGE_REL_RISCV64_PC);
emitRecordRelocation(dst - 4, (BYTE*)addr, IMAGE_REL_RISCV64_JALR);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ The .NET Foundation licenses this file to you under the MIT license.
<NativeLibrary Include="$(IlcFrameworkNativePath)libbrotlicommon.a" />
</ItemGroup>


<ItemGroup Condition="'$(StaticICULinking)' == 'true' and '$(NativeLib)' != 'Static' and '$(InvariantGlobalization)' != 'true'">
<NativeLibrary Include="$(IntermediateOutputPath)libs/System.Globalization.Native/build/libSystem.Globalization.Native.a" />
<DirectPInvoke Include="libSystem.Globalization.Native" />
Expand Down Expand Up @@ -222,6 +221,7 @@ The .NET Foundation licenses this file to you under the MIT license.
<NativeSystemLibrary Include="log" Condition="'$(_linuxLibcFlavor)' == 'bionic'" />
<NativeSystemLibrary Include="icucore" Condition="'$(_IsApplePlatform)' == 'true'" />
<NativeSystemLibrary Include="m" />
<NativeSystemLibrary Include="atomic" Condition="'$(_targetArchitecture)' == 'riscv64'" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@

// Normal case where a valid return address location is hijacked
sd a1, 0(a3)
tail ClearThreadState
tail LOCAL_LABEL(ClearThreadState)

LOCAL_LABEL(TailCallWasHijacked):

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@
mv a1, t1 // Second parameter to target function
jalr t0, t1, 0 // Jump to the function in t1

// We cannot make the label public as that tricks DIA stackwalker into thinking
// it's the beginning of a method. For this reason we export an auxiliary variable
// holding the address instead.
ALTERNATE_ENTRY ReturnFrom\FunctionName

// Restore the result address from t2
mv t2, a0 // Move result to t2

Expand Down Expand Up @@ -169,7 +174,6 @@

.endm


// To enable proper step-in behavior in the debugger, we need to have two instances
// of the thunk. For the first one, the debugger steps into the call in the function,
// for the other, it steps over it.
Expand Down
12 changes: 3 additions & 9 deletions src/coreclr/nativeaot/Runtime/unix/unixasmmacrosriscv64.inc
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,12 @@ C_FUNC(\Name):
.endm

.macro PREPARE_EXTERNAL_VAR Name, HelperReg
lui \HelperReg, %hi(C_FUNC(\Name))
addi \HelperReg, \HelperReg, %lo(C_FUNC(\Name))
.endm

.macro PREPARE_EXTERNAL_VAR_INDIRECT Name, HelperReg
lui \HelperReg, %hi(C_FUNC(\Name))
ld \HelperReg, %lo(C_FUNC(\Name))(\HelperReg)
la \HelperReg, C_FUNC(\Name) // Resolves the address in one step
.endm

.macro PREPARE_EXTERNAL_VAR_INDIRECT_W Name, HelperReg
lui \HelperReg, %hi(C_FUNC(\Name))
lw \HelperReg, %lo(C_FUNC(\Name))(\HelperReg)
la \HelperReg, C_FUNC(\Name)
lw \HelperReg, 0(\HelperReg)
.endm

.macro PROLOG_STACK_ALLOC Size
Expand Down
3 changes: 2 additions & 1 deletion src/coreclr/pal/inc/rt/ntimage.h
Original file line number Diff line number Diff line change
Expand Up @@ -1024,7 +1024,8 @@ typedef IMAGE_RELOCATION UNALIGNED *PIMAGE_RELOCATION;
//
// RISCV64 relocation types
//
#define IMAGE_REL_RISCV64_PC 0x0003
#define IMAGE_REL_RISCV64_PC 0x0002
#define IMAGE_REL_RISCV64_JALR 0x0004

//
// CEF relocation types.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ private static unsafe int GetRiscV64PC(uint* pCode)
private static unsafe void PutRiscV64PC(uint* pCode, long imm32)
{
// Verify that we got a valid offset
Debug.Assert((int)imm32 == imm32);
Debug.Assert((imm32 >= (long)-0x80000000 - 0x800) && (imm32 < (long)0x80000000 - 0x800));

int doff = (int)(imm32 & 0xfff);
uint auipcInstr = *pCode;
Expand Down Expand Up @@ -547,6 +547,7 @@ public static unsafe void WriteValue(RelocType relocType, void* location, long v
PutLoongArch64JIR((uint*)location, value);
break;
case RelocType.IMAGE_REL_BASED_RISCV64_PC:
case RelocType.IMAGE_REL_BASED_RISCV64_JALR:
PutRiscV64PC((uint*)location, value);
break;
default:
Expand Down Expand Up @@ -615,6 +616,7 @@ public static unsafe long ReadValue(RelocType relocType, void* location)
case RelocType.IMAGE_REL_BASED_LOONGARCH64_JIR:
return (long)GetLoongArch64JIR((uint*)location);
case RelocType.IMAGE_REL_BASED_RISCV64_PC:
case RelocType.IMAGE_REL_BASED_RISCV64_JALR:
return (long)GetRiscV64PC((uint*)location);
default:
Debug.Fail("Invalid RelocType: " + relocType);
Expand Down
5 changes: 4 additions & 1 deletion src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4026,12 +4026,15 @@ private static RelocType GetRelocType(TargetArchitecture targetArchitecture, ush
}
case TargetArchitecture.RiscV64:
{
const ushort IMAGE_REL_RISCV64_PC = 3;
const ushort IMAGE_REL_RISCV64_PC = 2;
const ushort IMAGE_REL_RISCV64_JALR = 4;

switch (fRelocType)
{
case IMAGE_REL_RISCV64_PC:
return RelocType.IMAGE_REL_BASED_RISCV64_PC;
case IMAGE_REL_RISCV64_JALR:
return RelocType.IMAGE_REL_BASED_RISCV64_JALR;
default:
Debug.Fail("Invalid RelocType: " + fRelocType);
return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -552,9 +552,8 @@ private void EmitRelocationsRiscV64(int sectionIndex, List<SymbolicRelocation> r
{
IMAGE_REL_BASED_DIR64 => R_RISCV_64,
IMAGE_REL_BASED_HIGHLOW => R_RISCV_32,
IMAGE_REL_BASED_RELPTR32 => R_RISCV_RELATIVE,
IMAGE_REL_BASED_RISCV64_PC => R_RISCV_PCREL_HI20,
IMAGE_REL_BASED_RISCV64_JALR => R_RISCV_CALL32,
IMAGE_REL_BASED_RELPTR32 => R_RISCV_64_LO12,
IMAGE_REL_BASED_RISCV64_PC or IMAGE_REL_BASED_RISCV64_JALR => R_RISCV_64_HI20,
_ => throw new NotSupportedException("Unknown relocation type: " + symbolicRelocation.Type)
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,12 @@ public void ProcessRelocation(RelocType relocationType, int sourceRVA, int targe
delta = targetRVA - sourceRVA;
break;
}
case RelocType.IMAGE_REL_BASED_RISCV64_JALR:
{
relocationLength = 8;
delta = targetRVA - sourceRVA;
break;
}

default:
throw new NotSupportedException();
Expand All @@ -257,7 +263,8 @@ public void ProcessRelocation(RelocType relocationType, int sourceRVA, int targe
(relocationType == RelocType.IMAGE_REL_BASED_ARM64_PAGEOFFSET_12A) ||
(relocationType == RelocType.IMAGE_REL_BASED_LOONGARCH64_PC) ||
(relocationType == RelocType.IMAGE_REL_BASED_LOONGARCH64_JIR) ||
(relocationType == RelocType.IMAGE_REL_BASED_RISCV64_PC)
(relocationType == RelocType.IMAGE_REL_BASED_RISCV64_PC) ||
(relocationType == RelocType.IMAGE_REL_BASED_RISCV64_JALR)
) && (value != 0))
{
throw new NotSupportedException();
Expand Down
Loading