Skip to content
Open
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 @@ -291,19 +291,21 @@ private protected override void EmitRelocations(int sectionIndex, List<SymbolicR
// Write an overflow relocation with the real count of relocations
sectionHeader.NumberOfRelocations = ushort.MaxValue;
sectionHeader.SectionCharacteristics |= SectionCharacteristics.LinkerNRelocOvfl;
coffRelocations.Add(new CoffRelocation { VirtualAddress = (uint)(relocationList.Count + 1) });
coffRelocations.Add(new CoffRelocation(
(uint)(relocationList.Count + 1),
symbolTableIndex: 0,
type: 0));
Comment thread
awakecoding marked this conversation as resolved.
}

switch (_machine)
{
case Machine.I386:
foreach (var relocation in relocationList)
{
coffRelocations.Add(new CoffRelocation
{
VirtualAddress = (uint)relocation.Offset,
SymbolTableIndex = _symbolNameToIndex[relocation.SymbolName],
Type = relocation.Type switch
coffRelocations.Add(new CoffRelocation(
(uint)relocation.Offset,
_symbolNameToIndex[relocation.SymbolName],
relocation.Type switch
{
IMAGE_REL_BASED_ABSOLUTE => IMAGE_REL_I386_DIR32NB,
IMAGE_REL_BASED_ADDR32NB => IMAGE_REL_I386_DIR32NB,
Expand All @@ -313,19 +315,17 @@ private protected override void EmitRelocations(int sectionIndex, List<SymbolicR
IMAGE_REL_SECREL => IMAGE_REL_I386_SECREL,
IMAGE_REL_SECTION => IMAGE_REL_I386_SECTION,
_ => throw new NotSupportedException($"Unsupported relocation: {relocation.Type}")
},
});
}));
}
break;

case Machine.Amd64:
foreach (var relocation in relocationList)
{
coffRelocations.Add(new CoffRelocation
{
VirtualAddress = (uint)relocation.Offset,
SymbolTableIndex = _symbolNameToIndex[relocation.SymbolName],
Type = relocation.Type switch
coffRelocations.Add(new CoffRelocation(
(uint)relocation.Offset,
_symbolNameToIndex[relocation.SymbolName],
relocation.Type switch
{
IMAGE_REL_BASED_ABSOLUTE => IMAGE_REL_AMD64_ADDR32NB,
IMAGE_REL_BASED_ADDR32NB => IMAGE_REL_AMD64_ADDR32NB,
Expand All @@ -336,19 +336,17 @@ private protected override void EmitRelocations(int sectionIndex, List<SymbolicR
IMAGE_REL_SECREL => IMAGE_REL_AMD64_SECREL,
IMAGE_REL_SECTION => IMAGE_REL_AMD64_SECTION,
_ => throw new NotSupportedException($"Unsupported relocation: {relocation.Type}")
},
});
}));
}
break;

case Machine.Arm64:
foreach (var relocation in relocationList)
{
coffRelocations.Add(new CoffRelocation
{
VirtualAddress = (uint)relocation.Offset,
SymbolTableIndex = _symbolNameToIndex[relocation.SymbolName],
Type = relocation.Type switch
coffRelocations.Add(new CoffRelocation(
(uint)relocation.Offset,
_symbolNameToIndex[relocation.SymbolName],
relocation.Type switch
{
IMAGE_REL_BASED_ABSOLUTE => IMAGE_REL_ARM64_ADDR32NB,
IMAGE_REL_BASED_ADDR32NB => IMAGE_REL_ARM64_ADDR32NB,
Expand All @@ -365,8 +363,7 @@ private protected override void EmitRelocations(int sectionIndex, List<SymbolicR
IMAGE_REL_SECREL => IMAGE_REL_ARM64_SECREL,
IMAGE_REL_SECTION => IMAGE_REL_ARM64_SECTION,
_ => throw new NotSupportedException($"Unsupported relocation: {relocation.Type}")
},
});
}));
}
break;

Expand Down Expand Up @@ -454,7 +451,7 @@ private protected override void EmitObjectFile(Stream outputFileStream)

if (section.Relocations.Count > 0)
{
foreach (var relocation in section.Relocations)
foreach (ref readonly CoffRelocation relocation in CollectionsMarshal.AsSpan(section.Relocations))
{
relocation.Write(outputFileStream);
}
Expand Down Expand Up @@ -700,11 +697,18 @@ internal enum CoffRelocationType
IMAGE_REL_ARM64_REL32 = 17,
}

protected sealed class CoffRelocation
internal readonly struct CoffRelocation
{
public uint VirtualAddress { get; set; }
public uint SymbolTableIndex { get; set; }
public CoffRelocationType Type { get; set; }
public CoffRelocation(uint virtualAddress, uint symbolTableIndex, CoffRelocationType type)
{
VirtualAddress = virtualAddress;
SymbolTableIndex = symbolTableIndex;
Type = type;
}

public uint VirtualAddress { get; }
public uint SymbolTableIndex { get; }
public CoffRelocationType Type { get; }

public const int Size =
sizeof(uint) + // VirtualAddress
Comment thread
awakecoding marked this conversation as resolved.
Expand Down
Loading