2018-02-04 23:08:20 +00:00
|
|
|
using ChocolArm64.Memory;
|
2018-02-20 20:09:23 +00:00
|
|
|
using Ryujinx.Core.Loaders.Executables;
|
|
|
|
using Ryujinx.Core.OsHle;
|
2018-02-04 23:08:20 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
2018-02-20 20:09:23 +00:00
|
|
|
namespace Ryujinx.Core.Loaders
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
|
|
|
class Executable
|
|
|
|
{
|
2018-02-26 01:14:58 +00:00
|
|
|
private List<ElfDyn> Dynamic;
|
|
|
|
|
|
|
|
private Dictionary<long, string> m_SymbolTable;
|
|
|
|
|
|
|
|
public IReadOnlyDictionary<long, string> SymbolTable => m_SymbolTable;
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2018-04-22 04:21:49 +00:00
|
|
|
public string Name { get; private set; }
|
|
|
|
|
|
|
|
private AMemory Memory;
|
|
|
|
|
2018-02-04 23:08:20 +00:00
|
|
|
public long ImageBase { get; private set; }
|
|
|
|
public long ImageEnd { get; private set; }
|
|
|
|
|
2018-02-06 23:28:32 +00:00
|
|
|
public Executable(IExecutable Exe, AMemory Memory, long ImageBase)
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
2018-02-26 01:14:58 +00:00
|
|
|
Dynamic = new List<ElfDyn>();
|
|
|
|
|
|
|
|
m_SymbolTable = new Dictionary<long, string>();
|
|
|
|
|
2018-04-22 04:21:49 +00:00
|
|
|
Name = Exe.Name;
|
|
|
|
|
2018-02-04 23:08:20 +00:00
|
|
|
this.Memory = Memory;
|
|
|
|
this.ImageBase = ImageBase;
|
|
|
|
this.ImageEnd = ImageBase;
|
|
|
|
|
NvServices refactoring (#120)
* Initial implementation of NvMap/NvHostCtrl
* More work on NvHostCtrl
* Refactoring of nvservices, move GPU Vmm, make Vmm per-process, refactor most gpu devices, move Gpu to Core, fix CbBind
* Implement GetGpuTime, support CancelSynchronization, fix issue on InsertWaitingMutex, proper double buffering support (again, not working properly for commercial games, only hb)
* Try to fix perf regression reading/writing textures, moved syncpts and events to a UserCtx class, delete global state when the process exits, other minor tweaks
* Remove now unused code, add comment about probably wrong result codes
2018-05-07 18:53:23 +00:00
|
|
|
WriteData(ImageBase + Exe.TextOffset, Exe.Text, MemoryType.CodeStatic, AMemoryPerm.RX);
|
|
|
|
WriteData(ImageBase + Exe.ROOffset, Exe.RO, MemoryType.CodeMutable, AMemoryPerm.Read);
|
|
|
|
WriteData(ImageBase + Exe.DataOffset, Exe.Data, MemoryType.CodeMutable, AMemoryPerm.RW);
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2018-02-06 23:28:32 +00:00
|
|
|
if (Exe.Mod0Offset == 0)
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
2018-03-10 23:39:16 +00:00
|
|
|
int BssOffset = Exe.DataOffset + Exe.Data.Length;
|
2018-02-21 21:56:52 +00:00
|
|
|
int BssSize = Exe.BssSize;
|
|
|
|
|
|
|
|
MapBss(ImageBase + BssOffset, BssSize);
|
|
|
|
|
|
|
|
ImageEnd = ImageBase + BssOffset + BssSize;
|
2018-02-06 23:28:32 +00:00
|
|
|
|
2018-02-04 23:08:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-06 23:28:32 +00:00
|
|
|
long Mod0Offset = ImageBase + Exe.Mod0Offset;
|
2018-02-04 23:08:20 +00: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;
|
|
|
|
|
2018-02-06 23:28:32 +00:00
|
|
|
MapBss(BssStartOffset, BssEndOffset - BssStartOffset);
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2018-02-26 01:44:30 +00:00
|
|
|
ImageEnd = BssEndOffset;
|
2018-02-04 23:08:20 +00:00
|
|
|
|
|
|
|
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 01:14:58 +00:00
|
|
|
long StrTblAddr = ImageBase + GetFirstValue(ElfDynTag.DT_STRTAB);
|
|
|
|
long SymTblAddr = ImageBase + GetFirstValue(ElfDynTag.DT_SYMTAB);
|
|
|
|
|
|
|
|
long SymEntSize = GetFirstValue(ElfDynTag.DT_SYMENT);
|
|
|
|
|
|
|
|
while ((ulong)SymTblAddr < (ulong)StrTblAddr)
|
|
|
|
{
|
|
|
|
ElfSym Sym = GetSymbol(SymTblAddr, StrTblAddr);
|
|
|
|
|
|
|
|
m_SymbolTable.TryAdd(Sym.Value, Sym.Name);
|
|
|
|
|
|
|
|
SymTblAddr += SymEntSize;
|
|
|
|
}
|
2018-02-04 23:08:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void WriteData(
|
|
|
|
long Position,
|
2018-03-10 23:39:16 +00:00
|
|
|
byte[] Data,
|
2018-02-04 23:08:20 +00:00
|
|
|
MemoryType Type,
|
|
|
|
AMemoryPerm Perm)
|
|
|
|
{
|
2018-03-10 23:39:16 +00:00
|
|
|
Memory.Manager.Map(Position, Data.Length, (int)Type, AMemoryPerm.Write);
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2018-03-10 23:39:16 +00:00
|
|
|
AMemoryHelper.WriteBytes(Memory, Position, Data);
|
2018-02-09 03:26:20 +00:00
|
|
|
|
2018-03-10 23:39:16 +00:00
|
|
|
Memory.Manager.Reprotect(Position, Data.Length, Perm);
|
2018-02-04 23:08:20 +00:00
|
|
|
}
|
|
|
|
|
2018-02-06 23:28:32 +00:00
|
|
|
private void MapBss(long Position, long Size)
|
|
|
|
{
|
2018-02-27 23:45:07 +00:00
|
|
|
Memory.Manager.Map(Position, Size, (int)MemoryType.Normal, AMemoryPerm.RW);
|
2018-02-06 23:28:32 +00:00
|
|
|
}
|
|
|
|
|
2018-02-04 23:08:20 +00: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 01:14:58 +00:00
|
|
|
return new ElfSym(Name, Info, Other, SHIdx, Value, Size);
|
2018-02-04 23:08:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private long GetFirstValue(ElfDynTag Tag)
|
|
|
|
{
|
|
|
|
foreach (ElfDyn Entry in Dynamic)
|
|
|
|
{
|
|
|
|
if (Entry.Tag == Tag)
|
|
|
|
{
|
|
|
|
return Entry.Value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|