Executable.cs 4.8 KB

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