2018-02-05 00:08:20 +01:00
|
|
|
using ChocolArm64.Memory;
|
2018-08-17 01:47:36 +02:00
|
|
|
using Ryujinx.HLE.HOS;
|
|
|
|
using Ryujinx.HLE.HOS.Kernel;
|
2018-06-11 02:46:42 +02:00
|
|
|
using Ryujinx.HLE.Loaders.Executables;
|
2018-08-17 01:47:36 +02:00
|
|
|
using Ryujinx.HLE.Utilities;
|
2018-09-19 22:07:56 +02:00
|
|
|
using System;
|
2018-02-05 00:08:20 +01:00
|
|
|
using System.Collections.Generic;
|
2018-09-19 22:07:56 +02:00
|
|
|
using System.Collections.ObjectModel;
|
2018-07-17 21:14:27 +02:00
|
|
|
using System.IO;
|
2018-09-19 22:07:56 +02:00
|
|
|
using System.Linq;
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-06-11 02:46:42 +02:00
|
|
|
namespace Ryujinx.HLE.Loaders
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
|
|
|
class Executable
|
|
|
|
{
|
2018-09-19 22:07:56 +02:00
|
|
|
private AMemory Memory;
|
2018-02-26 02:14:58 +01:00
|
|
|
|
2018-09-19 22:07:56 +02:00
|
|
|
private List<ElfDyn> Dynamic;
|
2018-02-26 02:14:58 +01:00
|
|
|
|
2018-09-19 22:07:56 +02:00
|
|
|
public ReadOnlyCollection<ElfSym> SymbolTable;
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-04-22 06:21:49 +02:00
|
|
|
public string Name { get; private set; }
|
|
|
|
|
2018-07-17 21:14:27 +02:00
|
|
|
public string FilePath { get; private set; }
|
|
|
|
|
2018-02-05 00:08:20 +01:00
|
|
|
public long ImageBase { get; private set; }
|
|
|
|
public long ImageEnd { get; private set; }
|
|
|
|
|
2018-08-15 20:59:51 +02:00
|
|
|
private KMemoryManager MemoryManager;
|
|
|
|
|
|
|
|
public Executable(IExecutable Exe, KMemoryManager MemoryManager, AMemory Memory, long ImageBase)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-02-26 02:14:58 +01:00
|
|
|
Dynamic = new List<ElfDyn>();
|
|
|
|
|
2018-07-17 21:14:27 +02:00
|
|
|
FilePath = Exe.FilePath;
|
2018-04-22 06:21:49 +02:00
|
|
|
|
2018-07-17 22:28:41 +02:00
|
|
|
if (FilePath != null)
|
|
|
|
{
|
|
|
|
Name = Path.GetFileNameWithoutExtension(FilePath.Replace(Homebrew.TemporaryNroSuffix, ""));
|
|
|
|
}
|
|
|
|
|
2018-08-15 20:59:51 +02:00
|
|
|
this.Memory = Memory;
|
|
|
|
this.MemoryManager = MemoryManager;
|
|
|
|
this.ImageBase = ImageBase;
|
|
|
|
this.ImageEnd = ImageBase;
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-08-15 20:59:51 +02:00
|
|
|
long TextPosition = ImageBase + (uint)Exe.TextOffset;
|
|
|
|
long ROPosition = ImageBase + (uint)Exe.ROOffset;
|
|
|
|
long DataPosition = ImageBase + (uint)Exe.DataOffset;
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-08-15 20:59:51 +02:00
|
|
|
long TextSize = (uint)IntUtils.AlignUp(Exe.Text.Length, KMemoryManager.PageSize);
|
2018-10-10 01:01:49 +02:00
|
|
|
long ROSize = (uint)IntUtils.AlignUp(Exe.RO.Length, KMemoryManager.PageSize);
|
2018-08-15 20:59:51 +02:00
|
|
|
long DataSize = (uint)IntUtils.AlignUp(Exe.Data.Length, KMemoryManager.PageSize);
|
2018-10-10 01:01:49 +02:00
|
|
|
long BssSize = (uint)IntUtils.AlignUp(Exe.BssSize, KMemoryManager.PageSize);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-10-10 01:01:49 +02:00
|
|
|
long DataAndBssSize = BssSize + DataSize;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
|
|
|
ImageEnd = DataPosition + DataAndBssSize;
|
2018-02-21 22:56:52 +01:00
|
|
|
|
2018-10-10 01:01:49 +02:00
|
|
|
if (Exe.SourceAddress == 0)
|
|
|
|
{
|
|
|
|
MemoryManager.HleMapProcessCode(TextPosition, TextSize + ROSize + DataAndBssSize);
|
|
|
|
|
|
|
|
MemoryManager.SetProcessMemoryPermission(ROPosition, ROSize, MemoryPermission.Read);
|
|
|
|
MemoryManager.SetProcessMemoryPermission(DataPosition, DataAndBssSize, MemoryPermission.ReadAndWrite);
|
|
|
|
|
|
|
|
Memory.WriteBytes(TextPosition, Exe.Text);
|
|
|
|
Memory.WriteBytes(ROPosition, Exe.RO);
|
|
|
|
Memory.WriteBytes(DataPosition, Exe.Data);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
long Result = MemoryManager.MapProcessCodeMemory(TextPosition, Exe.SourceAddress, TextSize + ROSize + DataSize);
|
2018-02-21 22:56:52 +01:00
|
|
|
|
2018-10-10 01:01:49 +02:00
|
|
|
if (Result != 0)
|
|
|
|
{
|
|
|
|
throw new InvalidOperationException();
|
|
|
|
}
|
2018-02-07 00:28:32 +01:00
|
|
|
|
2018-10-10 01:01:49 +02:00
|
|
|
MemoryManager.SetProcessMemoryPermission(ROPosition, ROSize, MemoryPermission.Read);
|
|
|
|
MemoryManager.SetProcessMemoryPermission(DataPosition, DataSize, MemoryPermission.ReadAndWrite);
|
|
|
|
|
|
|
|
if (Exe.BssAddress != 0 && Exe.BssSize != 0)
|
|
|
|
{
|
|
|
|
Result = MemoryManager.MapProcessCodeMemory(DataPosition + DataSize, Exe.BssAddress, BssSize);
|
|
|
|
|
|
|
|
if (Result != 0)
|
|
|
|
{
|
|
|
|
throw new InvalidOperationException();
|
|
|
|
}
|
|
|
|
|
|
|
|
MemoryManager.SetProcessMemoryPermission(DataPosition + DataSize, BssSize, MemoryPermission.ReadAndWrite);
|
|
|
|
}
|
|
|
|
}
|
2018-08-15 20:59:51 +02:00
|
|
|
|
|
|
|
if (Exe.Mod0Offset == 0)
|
|
|
|
{
|
2018-02-05 00:08:20 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-07 00:28:32 +01:00
|
|
|
long Mod0Offset = ImageBase + Exe.Mod0Offset;
|
2018-02-05 00:08:20 +01:00
|
|
|
|
|
|
|
int Mod0Magic = Memory.ReadInt32(Mod0Offset + 0x0);
|
|
|
|
long DynamicOffset = Memory.ReadInt32(Mod0Offset + 0x4) + Mod0Offset;
|
|
|
|
long BssStartOffset = Memory.ReadInt32(Mod0Offset + 0x8) + Mod0Offset;
|
|
|
|
long BssEndOffset = Memory.ReadInt32(Mod0Offset + 0xc) + Mod0Offset;
|
|
|
|
long EhHdrStartOffset = Memory.ReadInt32(Mod0Offset + 0x10) + Mod0Offset;
|
|
|
|
long EhHdrEndOffset = Memory.ReadInt32(Mod0Offset + 0x14) + Mod0Offset;
|
|
|
|
long ModObjOffset = Memory.ReadInt32(Mod0Offset + 0x18) + Mod0Offset;
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
long TagVal = Memory.ReadInt64(DynamicOffset + 0);
|
|
|
|
long Value = Memory.ReadInt64(DynamicOffset + 8);
|
|
|
|
|
|
|
|
DynamicOffset += 0x10;
|
|
|
|
|
|
|
|
ElfDynTag Tag = (ElfDynTag)TagVal;
|
|
|
|
|
|
|
|
if (Tag == ElfDynTag.DT_NULL)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
Dynamic.Add(new ElfDyn(Tag, Value));
|
|
|
|
}
|
|
|
|
|
2018-02-26 02:14:58 +01:00
|
|
|
long StrTblAddr = ImageBase + GetFirstValue(ElfDynTag.DT_STRTAB);
|
|
|
|
long SymTblAddr = ImageBase + GetFirstValue(ElfDynTag.DT_SYMTAB);
|
|
|
|
|
|
|
|
long SymEntSize = GetFirstValue(ElfDynTag.DT_SYMENT);
|
|
|
|
|
2018-09-19 22:07:56 +02:00
|
|
|
List<ElfSym> Symbols = new List<ElfSym>();
|
|
|
|
|
2018-02-26 02:14:58 +01:00
|
|
|
while ((ulong)SymTblAddr < (ulong)StrTblAddr)
|
|
|
|
{
|
|
|
|
ElfSym Sym = GetSymbol(SymTblAddr, StrTblAddr);
|
|
|
|
|
2018-09-19 22:07:56 +02:00
|
|
|
Symbols.Add(Sym);
|
2018-02-26 02:14:58 +01:00
|
|
|
|
|
|
|
SymTblAddr += SymEntSize;
|
|
|
|
}
|
2018-09-19 22:07:56 +02:00
|
|
|
|
|
|
|
SymbolTable = Array.AsReadOnly(Symbols.OrderBy(x => x.Value).ToArray());
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private ElfRel GetRelocation(long Position)
|
|
|
|
{
|
|
|
|
long Offset = Memory.ReadInt64(Position + 0);
|
|
|
|
long Info = Memory.ReadInt64(Position + 8);
|
|
|
|
long Addend = Memory.ReadInt64(Position + 16);
|
|
|
|
|
|
|
|
int RelType = (int)(Info >> 0);
|
|
|
|
int SymIdx = (int)(Info >> 32);
|
|
|
|
|
|
|
|
ElfSym Symbol = GetSymbol(SymIdx);
|
|
|
|
|
|
|
|
return new ElfRel(Offset, Addend, Symbol, (ElfRelType)RelType);
|
|
|
|
}
|
|
|
|
|
|
|
|
private ElfSym GetSymbol(int Index)
|
|
|
|
{
|
|
|
|
long StrTblAddr = ImageBase + GetFirstValue(ElfDynTag.DT_STRTAB);
|
|
|
|
long SymTblAddr = ImageBase + GetFirstValue(ElfDynTag.DT_SYMTAB);
|
|
|
|
|
|
|
|
long SymEntSize = GetFirstValue(ElfDynTag.DT_SYMENT);
|
|
|
|
|
|
|
|
long Position = SymTblAddr + Index * SymEntSize;
|
|
|
|
|
|
|
|
return GetSymbol(Position, StrTblAddr);
|
|
|
|
}
|
|
|
|
|
|
|
|
private ElfSym GetSymbol(long Position, long StrTblAddr)
|
|
|
|
{
|
|
|
|
int NameIndex = Memory.ReadInt32(Position + 0);
|
|
|
|
int Info = Memory.ReadByte(Position + 4);
|
|
|
|
int Other = Memory.ReadByte(Position + 5);
|
|
|
|
int SHIdx = Memory.ReadInt16(Position + 6);
|
|
|
|
long Value = Memory.ReadInt64(Position + 8);
|
|
|
|
long Size = Memory.ReadInt64(Position + 16);
|
|
|
|
|
|
|
|
string Name = string.Empty;
|
|
|
|
|
|
|
|
for (int Chr; (Chr = Memory.ReadByte(StrTblAddr + NameIndex++)) != 0;)
|
|
|
|
{
|
|
|
|
Name += (char)Chr;
|
|
|
|
}
|
|
|
|
|
2018-02-26 02:14:58 +01:00
|
|
|
return new ElfSym(Name, Info, Other, SHIdx, Value, Size);
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private long GetFirstValue(ElfDynTag Tag)
|
|
|
|
{
|
|
|
|
foreach (ElfDyn Entry in Dynamic)
|
|
|
|
{
|
|
|
|
if (Entry.Tag == Tag)
|
|
|
|
{
|
|
|
|
return Entry.Value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|