Executable.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using ChocolArm64.Memory;
  2. using Ryujinx.Core.Loaders.Executables;
  3. using Ryujinx.Core.OsHle;
  4. using System.Collections.Generic;
  5. namespace Ryujinx.Core.Loaders
  6. {
  7. class Executable
  8. {
  9. private AMemory Memory;
  10. private ElfDyn[] Dynamic;
  11. public long ImageBase { get; private set; }
  12. public long ImageEnd { get; private set; }
  13. public Extensions Extension { get; private set; }
  14. public Executable(IExecutable Exe, AMemory Memory, long ImageBase)
  15. {
  16. this.Memory = Memory;
  17. this.ImageBase = ImageBase;
  18. this.ImageEnd = ImageBase;
  19. WriteData(ImageBase + Exe.TextOffset, Exe.Text, MemoryType.CodeStatic, AMemoryPerm.RX);
  20. WriteData(ImageBase + Exe.ROOffset, Exe.RO, MemoryType.Normal, AMemoryPerm.Read);
  21. WriteData(ImageBase + Exe.DataOffset, Exe.Data, MemoryType.Normal, AMemoryPerm.RW);
  22. if (Exe.Mod0Offset == 0)
  23. {
  24. int BssOffset = Exe.DataOffset + Exe.Data.Count;
  25. int BssSize = Exe.BssSize;
  26. MapBss(ImageBase + BssOffset, BssSize);
  27. ImageEnd = ImageBase + BssOffset + BssSize;
  28. return;
  29. }
  30. long Mod0Offset = ImageBase + Exe.Mod0Offset;
  31. int Mod0Magic = Memory.ReadInt32(Mod0Offset + 0x0);
  32. long DynamicOffset = Memory.ReadInt32(Mod0Offset + 0x4) + Mod0Offset;
  33. long BssStartOffset = Memory.ReadInt32(Mod0Offset + 0x8) + Mod0Offset;
  34. long BssEndOffset = Memory.ReadInt32(Mod0Offset + 0xc) + Mod0Offset;
  35. long EhHdrStartOffset = Memory.ReadInt32(Mod0Offset + 0x10) + Mod0Offset;
  36. long EhHdrEndOffset = Memory.ReadInt32(Mod0Offset + 0x14) + Mod0Offset;
  37. long ModObjOffset = Memory.ReadInt32(Mod0Offset + 0x18) + Mod0Offset;
  38. Extension = Exe.Extension;
  39. MapBss(BssStartOffset, BssEndOffset - BssStartOffset);
  40. ImageEnd = BssEndOffset;
  41. List<ElfDyn> Dynamic = new List<ElfDyn>();
  42. while (true)
  43. {
  44. long TagVal = Memory.ReadInt64(DynamicOffset + 0);
  45. long Value = Memory.ReadInt64(DynamicOffset + 8);
  46. DynamicOffset += 0x10;
  47. ElfDynTag Tag = (ElfDynTag)TagVal;
  48. if (Tag == ElfDynTag.DT_NULL)
  49. {
  50. break;
  51. }
  52. Dynamic.Add(new ElfDyn(Tag, Value));
  53. }
  54. this.Dynamic = Dynamic.ToArray();
  55. }
  56. private void WriteData(
  57. long Position,
  58. IList<byte> Data,
  59. MemoryType Type,
  60. AMemoryPerm Perm)
  61. {
  62. Memory.Manager.MapPhys(Position, Data.Count, (int)Type, AMemoryPerm.Write);
  63. for (int Index = 0; Index < Data.Count; Index++)
  64. {
  65. Memory.WriteByte(Position + Index, Data[Index]);
  66. }
  67. Memory.Manager.Reprotect(Position, Data.Count, Perm);
  68. }
  69. private void MapBss(long Position, long Size)
  70. {
  71. Memory.Manager.MapPhys(Position, Size, (int)MemoryType.Normal, AMemoryPerm.RW);
  72. }
  73. private ElfRel GetRelocation(long Position)
  74. {
  75. long Offset = Memory.ReadInt64(Position + 0);
  76. long Info = Memory.ReadInt64(Position + 8);
  77. long Addend = Memory.ReadInt64(Position + 16);
  78. int RelType = (int)(Info >> 0);
  79. int SymIdx = (int)(Info >> 32);
  80. ElfSym Symbol = GetSymbol(SymIdx);
  81. return new ElfRel(Offset, Addend, Symbol, (ElfRelType)RelType);
  82. }
  83. private ElfSym GetSymbol(int Index)
  84. {
  85. long StrTblAddr = ImageBase + GetFirstValue(ElfDynTag.DT_STRTAB);
  86. long SymTblAddr = ImageBase + GetFirstValue(ElfDynTag.DT_SYMTAB);
  87. long SymEntSize = GetFirstValue(ElfDynTag.DT_SYMENT);
  88. long Position = SymTblAddr + Index * SymEntSize;
  89. return GetSymbol(Position, StrTblAddr);
  90. }
  91. private ElfSym GetSymbol(long Position, long StrTblAddr)
  92. {
  93. int NameIndex = Memory.ReadInt32(Position + 0);
  94. int Info = Memory.ReadByte(Position + 4);
  95. int Other = Memory.ReadByte(Position + 5);
  96. int SHIdx = Memory.ReadInt16(Position + 6);
  97. long Value = Memory.ReadInt64(Position + 8);
  98. long Size = Memory.ReadInt64(Position + 16);
  99. string Name = string.Empty;
  100. for (int Chr; (Chr = Memory.ReadByte(StrTblAddr + NameIndex++)) != 0;)
  101. {
  102. Name += (char)Chr;
  103. }
  104. return new ElfSym(Name, Info, Other, SHIdx, ImageBase, Value, Size);
  105. }
  106. private long GetFirstValue(ElfDynTag Tag)
  107. {
  108. foreach (ElfDyn Entry in Dynamic)
  109. {
  110. if (Entry.Tag == Tag)
  111. {
  112. return Entry.Value;
  113. }
  114. }
  115. return 0;
  116. }
  117. }
  118. }