Executable.cs 4.9 KB

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