Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Diagnostics;
using System.Runtime.Serialization;
using ILCompiler.ObjectWriter;

Comment on lines 4 to 8
namespace ILCompiler.DependencyAnalysis
Expand Down Expand Up @@ -696,6 +697,31 @@ public static unsafe void WriteValue(RelocType relocType, void* location, long v
}
}

public static unsafe int WriteVariableLengthValue(RelocType relocType, byte* location, long value)
{
Debug.Assert(IsVariableLength(relocType));
switch (relocType)
{
case RelocType.WASM_TYPE_INDEX_LEB:
case RelocType.WASM_GLOBAL_INDEX_LEB:
case RelocType.WASM_FUNCTION_INDEX_LEB:
case RelocType.WASM_MEMORY_ADDR_LEB:
case RelocType.WASM_MEMORY_ADDR_REL_LEB:
case RelocType.WASM_CLR_RESTORE_CONTEXT_EXCEPTION_TAG_LEB:
DwarfHelper.WriteULEB128(new Span<byte>((byte*)location, WASM_PADDED_RELOC_SIZE_32), checked((ulong)value));
return (int)DwarfHelper.SizeOfULEB128((ulong)value);

case RelocType.WASM_TABLE_INDEX_SLEB:
case RelocType.WASM_MEMORY_ADDR_SLEB:
case RelocType.WASM_MEMORY_ADDR_REL_SLEB:
DwarfHelper.WriteSLEB128(new Span<byte>((byte*)location, WASM_PADDED_RELOC_SIZE_32), value);
return (int)DwarfHelper.SizeOfSLEB128(value);
default:
Debug.Fail("Invalid variable-length RelocType: " + relocType);
return 0;
}
}

public static readonly int MaxSize = 8;
// Note: Please update the above field if the max size
// changes when adding a new case to this method.
Expand Down Expand Up @@ -742,6 +768,45 @@ public static int GetSize(RelocType relocType)
};
}

public static bool IsVariableLength(RelocType relocType)
{
return relocType switch
{
RelocType.WASM_FUNCTION_INDEX_LEB or
RelocType.WASM_TABLE_INDEX_SLEB or
RelocType.WASM_TYPE_INDEX_LEB or
RelocType.WASM_GLOBAL_INDEX_LEB or
RelocType.WASM_MEMORY_ADDR_LEB or
RelocType.WASM_MEMORY_ADDR_SLEB or
RelocType.WASM_MEMORY_ADDR_REL_LEB or
RelocType.WASM_MEMORY_ADDR_REL_SLEB or
RelocType.WASM_CLR_RESTORE_CONTEXT_EXCEPTION_TAG_LEB => true,
_ => false,
};
}

public static int ActualSize(RelocType relocType, long resolvedValue)
{
Debug.Assert(IsVariableLength(relocType));
switch (relocType)
{
case RelocType.WASM_FUNCTION_INDEX_LEB:
case RelocType.WASM_TYPE_INDEX_LEB:
case RelocType.WASM_GLOBAL_INDEX_LEB:
case RelocType.WASM_MEMORY_ADDR_LEB:
case RelocType.WASM_MEMORY_ADDR_REL_LEB:
case RelocType.WASM_CLR_RESTORE_CONTEXT_EXCEPTION_TAG_LEB:
return (int)DwarfHelper.SizeOfULEB128((ulong)resolvedValue);
case RelocType.WASM_TABLE_INDEX_SLEB:
case RelocType.WASM_MEMORY_ADDR_SLEB:
case RelocType.WASM_MEMORY_ADDR_REL_SLEB:
return (int)DwarfHelper.SizeOfSLEB128(resolvedValue);
default:
Debug.Fail("Invalid reloc type");
return 0;
}
}

public static unsafe long ReadValue(RelocType relocType, void* location)
{
switch (relocType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Buffers;
using System.IO;
using System.Numerics;

namespace ILCompiler.ObjectWriter
Expand Down Expand Up @@ -122,6 +123,31 @@ public static ulong ReadULEB128(ReadOnlySpan<byte> buffer, out int bytesRead)
return value;
}

public static ulong? ReadULEB128(Stream source, out int bytesRead)
{
ulong value = 0;
byte @byte;
int shift = 0;
long startPos = source.Position;

do
{
int b = source.ReadByte();
if (b < 0)
{
bytesRead = (int)(source.Position - startPos);
return null;
}

@byte = (byte)b;
value |= ((ulong)@byte & 0x7f) << shift;
shift += 7;
} while ((@byte & 0x80) != 0);

bytesRead = (int)(source.Position - startPos);
return value;
}
Comment on lines +126 to +149

public static long ReadSLEB128(ReadOnlySpan<byte> buffer) => ReadSLEB128(buffer, out _);

public static long ReadSLEB128(ReadOnlySpan<byte> buffer, out int bytesRead)
Expand Down
Loading
Loading