Skip to content
Merged
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
3 changes: 2 additions & 1 deletion src/coreclr/tools/Common/Compiler/CompilationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using ILCompiler.DependencyAnalysisFramework;

using Internal.IL;
using Internal.Text;

namespace ILCompiler
{
Expand Down Expand Up @@ -51,7 +52,7 @@ public CompilationBuilder UseParallelism(int parallelism)

public CompilationBuilder UseCompilationUnitPrefix(string prefix)
{
_nameMangler.CompilationUnitPrefix = prefix;
_nameMangler.CompilationUnitPrefix = new Utf8String(prefix);
return this;
}

Expand Down
26 changes: 18 additions & 8 deletions src/coreclr/tools/Common/Compiler/NativeAotNameMangler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,11 @@ private Utf8String SanitizeNameWithHash(Utf8String literal)
hash = SHA256.HashData(literal.AsSpan());
}

mangledName += "_" + Convert.ToHexString(hash);
mangledName = new Utf8StringBuilder()
.Append(mangledName)
.Append('_')
.AppendAscii(Convert.ToHexString(hash))
.ToUtf8String();
}

return mangledName;
Expand Down Expand Up @@ -293,7 +297,7 @@ private Utf8String ComputeMangledTypeName(TypeDesc type)
// This problem needs a better fix.
if (isSystemPrivate)
assemblyName = string.Concat("S.P.", assemblyName.AsSpan(15));
Utf8String prependAssemblyName = SanitizeName(assemblyName);
Utf8String prependAssemblyName = new Utf8String(SanitizeName(assemblyName));

var deduplicator = new HashSet<Utf8String>();

Expand Down Expand Up @@ -378,17 +382,17 @@ static void AppendTypeName(Utf8StringBuilder sb, MetadataType t)
break;
case TypeFlags.SzArray:
mangledName = new Utf8StringBuilder().Append("__Array"u8)
.Append(NestMangledName(GetMangledTypeName(((ArrayType)type).ElementType))).ToUtf8String();
.Append(EnterNameScopeSequence).Append(GetMangledTypeName(((ArrayType)type).ElementType)).Append(ExitNameScopeSequence).ToUtf8String();
break;
case TypeFlags.ByRef:
mangledName = new Utf8StringBuilder()
.Append(GetMangledTypeName(((ByRefType)type).ParameterType))
.Append(NestMangledName("ByRef")).ToUtf8String();
.Append(EnterNameScopeSequence).Append("ByRef"u8).Append(ExitNameScopeSequence).ToUtf8String();
break;
case TypeFlags.Pointer:
mangledName = new Utf8StringBuilder()
.Append(GetMangledTypeName(((PointerType)type).ParameterType))
.Append(NestMangledName("Pointer")).ToUtf8String();
.Append(EnterNameScopeSequence).Append("Pointer"u8).Append(ExitNameScopeSequence).ToUtf8String();
break;
case TypeFlags.FunctionPointer:
{
Expand Down Expand Up @@ -433,17 +437,23 @@ static void AppendTypeName(Utf8StringBuilder sb, MetadataType t)
}
else if (type is IPrefixMangledMethod)
{
mangledName = GetPrefixMangledMethodName((IPrefixMangledMethod)type).ToString();
mangledName = GetPrefixMangledMethodName((IPrefixMangledMethod)type);
}
else if (type is IPrefixMangledType)
{
mangledName = GetPrefixMangledTypeName((IPrefixMangledType)type).ToString();
mangledName = GetPrefixMangledTypeName((IPrefixMangledType)type);
}
else
{
// This is a type definition. Since we didn't fall in the `is EcmaType` case above,
// it's likely a compiler-generated type.
mangledName = SanitizeName(((DefType)type).GetFullName());
var defType = (DefType)type;
var sb = new Utf8StringBuilder();
if (defType.Namespace.Length > 0)
sb.Append(SanitizeName(defType.Namespace)).Append('_');

sb.Append(SanitizeName(defType.Name));
mangledName = sb.ToUtf8String();
}
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ private protected override void CreateSection(ObjectNodeSection section, Utf8Str
_sectionNumberToComdatAuxRecord[_sections.Count] = auxRecord;
_symbols.Add(new CoffSymbol
{
Name = sectionHeader.Name,
Name = new Utf8String(sectionHeader.Name),
Value = 0,
SectionIndex = coffSectionIndex,
StorageClass = CoffSymbolClass.IMAGE_SYM_CLASS_STATIC,
Expand Down Expand Up @@ -266,7 +266,7 @@ private protected override void EmitSymbolTable(
// Emit the feat.00 symbol that controls various linker behaviors
_symbols.Add(new CoffSymbol
{
Name = "@feat.00",
Name = new Utf8String("@feat.00"u8),
StorageClass = CoffSymbolClass.IMAGE_SYM_CLASS_STATIC,
SectionIndex = uint.MaxValue, // IMAGE_SYM_ABSOLUTE
Value = (uint)feat00Flags,
Expand Down Expand Up @@ -613,7 +613,7 @@ public void Write(Stream stream, CoffStringTable stringTable)
{
buffer.Clear();
buffer[0] = (byte)'/';
uint offset = stringTable.GetStringOffset(Name);
uint offset = stringTable.GetStringOffset(new Utf8String(Name));
if (offset <= 9999999)
{
Span<char> charBuffer = stackalloc char[16];
Expand Down
40 changes: 22 additions & 18 deletions src/coreclr/tools/Common/Compiler/ObjectWriter/ElfObjectWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
using System.Buffers.Binary;
using System.Numerics;
using System.Reflection;

using ILCompiler.DependencyAnalysis;
using ILCompiler.DependencyAnalysisFramework;

using Internal.Text;
using Internal.TypeSystem;

using static ILCompiler.DependencyAnalysis.RelocType;
using static ILCompiler.ObjectWriter.EabiNative;
using static ILCompiler.ObjectWriter.ElfNative;
Expand Down Expand Up @@ -155,9 +158,10 @@ private protected override void CreateSection(ObjectNodeSection section, Utf8Str
});

// Emit section symbol into symbol table (for COMDAT the defining symbol is section symbol)
Utf8String sectionNameUtf8 = new(sectionName);
if (comdatName.IsNull)
{
_symbolNameToIndex[sectionName] = (uint)_symbols.Count;
_symbolNameToIndex[sectionNameUtf8] = (uint)_symbols.Count;
_symbols.Add(new ElfSymbol
{
Section = _sections[sectionIndex],
Expand All @@ -171,11 +175,11 @@ private protected override void CreateSection(ObjectNodeSection section, Utf8Str
{
Section = _sections[sectionIndex],
Info = STT_NOTYPE,
Name = $"$t.{sectionIndex}"
Name = new Utf8StringBuilder().Append("$t."u8).Append(sectionIndex).ToUtf8String()
});
}

base.CreateSection(section, comdatName, symbolName.IsNull ? sectionName : symbolName, sectionIndex, sectionStream);
base.CreateSection(section, comdatName, symbolName.IsNull ? sectionNameUtf8 : symbolName, sectionIndex, sectionStream);
}

protected internal override void UpdateSectionAlignment(int sectionIndex, int alignment)
Expand Down Expand Up @@ -256,7 +260,7 @@ protected internal override unsafe void EmitRelocation(
// ld reg, reg, pcrel_lo12(.Lpcrel_hi) # note that this points at the label for 'auipc'
//
// Add a symbol for the auipc instruction so it can be pointed at by the LO12 relocation
string name = GetRiscV64SymbolNameForPcrelRelocation(sectionIndex, offset);
Utf8String name = new(GetRiscV64SymbolNameForPcrelRelocation(sectionIndex, offset));
EmitSymbolDefinition(sectionIndex, name, offset, 2 * 4);
}

Expand Down Expand Up @@ -305,7 +309,7 @@ private protected override void EmitSymbolTable(
{
sortedSymbols.Add(new ElfSymbol
{
Name = $"{externSymbol}$thunk",
Name = Utf8String.Concat(externSymbol.AsSpan(), "$thunk"u8),
Value = (ulong)((thunkSymbolsIndex * 4) | 1u),
Size = 4u,
Section = _sections[thunkSectionIndex],
Expand Down Expand Up @@ -340,7 +344,7 @@ private protected override void EmitSymbolTable(
_sections[thunkSectionWriter.SectionIndex].RelocationStream = relocationStream;
foreach (Utf8String externSymbol in undefinedSymbols)
{
if (_symbolNameToIndex.TryGetValue($"{externSymbol}$thunk", out uint thunkSymbolIndex))
if (_symbolNameToIndex.TryGetValue(Utf8String.Concat(externSymbol.AsSpan(), "$thunk"u8), out uint thunkSymbolIndex))
{
// Write the relocation to external symbol for the thunk
BinaryPrimitives.WriteUInt32LittleEndian(relocationEntry, (uint)thunkSectionWriter.Position);
Expand Down Expand Up @@ -587,7 +591,7 @@ private void EmitRelocationsRiscV64(int sectionIndex, List<SymbolicRelocation> r
if (symbolicRelocation.Type is IMAGE_REL_BASED_RISCV64_PCREL_I or IMAGE_REL_BASED_RISCV64_PCREL_S)
{
// Emit another relocation which points at the previous instruction
string symbolName = GetRiscV64SymbolNameForPcrelRelocation(sectionIndex, symbolicRelocation.Offset);
Utf8String symbolName = new(GetRiscV64SymbolNameForPcrelRelocation(sectionIndex, symbolicRelocation.Offset));
symbolIndex = _symbolNameToIndex[symbolName];
type = symbolicRelocation.Type is IMAGE_REL_BASED_RISCV64_PCREL_I
? R_RISCV_PCREL_LO12_I
Expand Down Expand Up @@ -680,7 +684,7 @@ private void EmitObjectFile<TSize>(Stream outputFileStream)
ulong currentOffset = (ulong)ElfHeader.GetSize<TSize>();
foreach (var section in _sections)
{
_stringTable.ReserveString(section.Name);
_stringTable.ReserveString(new Utf8String(section.Name));

if (section.SectionHeader.Alignment > 1)
{
Expand All @@ -704,7 +708,7 @@ private void EmitObjectFile<TSize>(Stream outputFileStream)

if (section.RelocationStream != Stream.Null)
{
_stringTable.ReserveString((_useInlineRelocationAddends ? ".rel" : ".rela") + section.Name);
_stringTable.ReserveString(new Utf8String((_useInlineRelocationAddends ? ".rel" : ".rela") + section.Name));
sectionCount++;
}

Expand All @@ -718,11 +722,11 @@ private void EmitObjectFile<TSize>(Stream outputFileStream)
}

// Reserve names for the predefined sections
_stringTable.ReserveString(".strtab");
_stringTable.ReserveString(".symtab");
_stringTable.ReserveString(new Utf8String(".strtab"u8));
_stringTable.ReserveString(new Utf8String(".symtab"u8));
if (sectionCount >= SHN_LORESERVE)
{
_stringTable.ReserveString(".symtab_shndx");
_stringTable.ReserveString(new Utf8String(".symtab_shndx"u8));
hasSymTabExtendedIndices = true;
}

Expand Down Expand Up @@ -828,15 +832,15 @@ private void EmitObjectFile<TSize>(Stream outputFileStream)
{
section.SectionHeader.Link = section.LinkSection.SectionIndex;
}
section.SectionHeader.NameIndex = _stringTable.GetStringOffset(section.Name);
section.SectionHeader.NameIndex = _stringTable.GetStringOffset(new Utf8String(section.Name));
section.SectionHeader.Write<TSize>(outputFileStream);

if (section.SectionHeader.Type != SHT_NOBITS &&
section.RelocationStream != Stream.Null)
{
ElfSectionHeader relaSectionHeader = new ElfSectionHeader
{
NameIndex = _stringTable.GetStringOffset((_useInlineRelocationAddends ? ".rel" : ".rela") + section.Name),
NameIndex = _stringTable.GetStringOffset(new Utf8String((_useInlineRelocationAddends ? ".rel" : ".rela") + section.Name)),
Type = _useInlineRelocationAddends ? SHT_REL : SHT_RELA,
Flags = (section.GroupSection is not null ? SHF_GROUP : 0u) | SHF_INFO_LINK,
Address = 0u,
Expand All @@ -854,7 +858,7 @@ private void EmitObjectFile<TSize>(Stream outputFileStream)
// String table section
ElfSectionHeader stringTableSectionHeader = new ElfSectionHeader
{
NameIndex = _stringTable.GetStringOffset(".strtab"),
NameIndex = _stringTable.GetStringOffset(new Utf8String(".strtab"u8)),
Type = SHT_STRTAB,
Flags = 0u,
Address = 0u,
Expand All @@ -870,7 +874,7 @@ private void EmitObjectFile<TSize>(Stream outputFileStream)
// Symbol table section
ElfSectionHeader symbolTableSectionHeader = new ElfSectionHeader
{
NameIndex = _stringTable.GetStringOffset(".symtab"),
NameIndex = _stringTable.GetStringOffset(new Utf8String(".symtab"u8)),
Type = SHT_SYMTAB,
Flags = 0u,
Address = 0u,
Expand All @@ -890,7 +894,7 @@ private void EmitObjectFile<TSize>(Stream outputFileStream)
{
ElfSectionHeader sectionHeader = new ElfSectionHeader
{
NameIndex = _stringTable.GetStringOffset(".symtab_shndx"),
NameIndex = _stringTable.GetStringOffset(new Utf8String(".symtab_shndx"u8)),
Type = SHT_SYMTAB_SHNDX,
Flags = 0u,
Address = 0u,
Expand Down Expand Up @@ -1090,7 +1094,7 @@ private sealed class ElfStringTable : StringTableBuilder
public ElfStringTable()
{
// Always start the table with empty string
GetStringOffset("");
GetStringOffset(Utf8String.Empty);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ internal sealed partial class MachObjectWriter : UnixObjectWriter
/// <summary>
/// Base symbol to use for <see cref="RelocType.IMAGE_REL_BASED_ADDR32NB"/> relocations.
/// </summary>
private readonly string _baseSymbolName;
private readonly Utf8String? _baseSymbolName;

public MachObjectWriter(NodeFactory factory, ObjectWritingOptions options, OutputInfoBuilder outputInfoBuilder = null)
: base(factory, options, outputInfoBuilder)
Expand Down Expand Up @@ -102,7 +102,7 @@ public MachObjectWriter(NodeFactory factory, ObjectWritingOptions options, Outpu
public MachObjectWriter(NodeFactory factory, ObjectWritingOptions options, OutputInfoBuilder outputInfoBuilder, string baseSymbolName)
: this(factory, options, outputInfoBuilder)
{
_baseSymbolName = baseSymbolName;
_baseSymbolName = baseSymbolName is not null ? new Utf8String(baseSymbolName) : null;
}

private protected override bool UsesSubsectionsViaSymbols => true;
Expand All @@ -121,7 +121,7 @@ private protected override void EmitSectionsAndLayout()
{
var machSymbol = new MachSymbol
{
Name = $"lsection{sectionIndex}",
Name = new Utf8StringBuilder().Append("lsection"u8).Append(sectionIndex).ToUtf8String(),
Section = section,
Value = section.VirtualAddress,
Descriptor = N_NO_DEAD_STRIP,
Expand Down Expand Up @@ -375,7 +375,7 @@ private protected override void CreateSection(ObjectNodeSection section, Utf8Str

_sections.Add(machSection);

base.CreateSection(section, comdatName, symbolName.IsNull ? $"lsection{sectionIndex}" : symbolName, sectionIndex, sectionStream);
base.CreateSection(section, comdatName, symbolName.IsNull ? new Utf8StringBuilder().Append("lsection"u8).Append(sectionIndex).ToUtf8String() : symbolName, sectionIndex, sectionStream);
}

protected internal override void UpdateSectionAlignment(int sectionIndex, int alignment)
Expand Down Expand Up @@ -547,7 +547,7 @@ private protected override void EmitSymbolTable(
// Add the base symbol as an undefined symbol.
if (_baseSymbolName is not null)
{
undefinedSymbols.Add(_baseSymbolName);
undefinedSymbols.Add(_baseSymbolName.Value);
}

foreach (Utf8String externSymbol in undefinedSymbols)
Expand Down Expand Up @@ -686,14 +686,14 @@ private void EmitRelocationsX64(int sectionIndex, List<SymbolicRelocation> reloc
throw new NotSupportedException("A base symbol name must be provided for IMAGE_REL_BASED_ADDR32NB relocations.");
}

Debug.Assert(_symbolNameToIndex.ContainsKey(_baseSymbolName));
Debug.Assert(_symbolNameToIndex.ContainsKey(_baseSymbolName.Value));

// Represent as X86_64_RELOC_SUBTRACTOR + X86_64_RELOC_UNSIGNED against the base symbol.
sectionRelocations.Add(
new MachRelocation
{
Address = (int)symbolicRelocation.Offset,
SymbolOrSectionIndex = _symbolNameToIndex[_baseSymbolName],
SymbolOrSectionIndex = _symbolNameToIndex[_baseSymbolName.Value],
Length = 4,
RelocationType = X86_64_RELOC_SUBTRACTOR,
IsExternal = true,
Expand Down Expand Up @@ -849,14 +849,14 @@ private void EmitRelocationsArm64(int sectionIndex, List<SymbolicRelocation> rel
throw new NotSupportedException("A base symbol name must be provided for IMAGE_REL_BASED_ADDR32NB relocations.");
}

Debug.Assert(_symbolNameToIndex.ContainsKey(_baseSymbolName));
Debug.Assert(_symbolNameToIndex.ContainsKey(_baseSymbolName.Value));

// Represent as ARM64_RELOC_SUBTRACTOR + ARM64_RELOC_UNSIGNED against the base symbol.
sectionRelocations.Add(
new MachRelocation
{
Address = (int)symbolicRelocation.Offset,
SymbolOrSectionIndex = _symbolNameToIndex[_baseSymbolName],
SymbolOrSectionIndex = _symbolNameToIndex[_baseSymbolName.Value],
Length = 4,
RelocationType = ARM64_RELOC_SUBTRACTOR,
IsExternal = true,
Expand Down Expand Up @@ -1034,7 +1034,7 @@ public void Write(Stream stream)

private sealed class MachSymbol
{
public Utf8String Name { get; init; } = string.Empty;
public Utf8String Name { get; init; }
public byte Type { get; init; }
public MachSection Section { get; init; }
public ushort Descriptor { get; init; }
Expand Down Expand Up @@ -1159,7 +1159,7 @@ private sealed class MachStringTable : StringTableBuilder
public MachStringTable()
{
// Always start the table with empty string
GetStringOffset("");
GetStringOffset(Utf8String.Empty);
}
}
}
Expand Down
Loading
Loading