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