Executable.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 IElf 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(IElf NsoData, AMemory Memory, long ImageBase)
  15. {
  16. this.NsoData = NsoData;
  17. this.Memory = Memory;
  18. this.ImageBase = ImageBase;
  19. this.ImageEnd = ImageBase;
  20. WriteData(ImageBase + NsoData.TextOffset, NsoData.Text, MemoryType.CodeStatic, AMemoryPerm.RX);
  21. WriteData(ImageBase + NsoData.ROOffset, NsoData.RO, MemoryType.Normal, AMemoryPerm.Read);
  22. WriteData(ImageBase + NsoData.DataOffset, NsoData.Data, MemoryType.Normal, AMemoryPerm.RW);
  23. if (NsoData.Text.Count == 0)
  24. {
  25. return;
  26. }
  27. long Mod0Offset = ImageBase + NsoData.Mod0Offset;
  28. int Mod0Magic = Memory.ReadInt32(Mod0Offset + 0x0);
  29. long DynamicOffset = Memory.ReadInt32(Mod0Offset + 0x4) + Mod0Offset;
  30. long BssStartOffset = Memory.ReadInt32(Mod0Offset + 0x8) + Mod0Offset;
  31. long BssEndOffset = Memory.ReadInt32(Mod0Offset + 0xc) + Mod0Offset;
  32. long EhHdrStartOffset = Memory.ReadInt32(Mod0Offset + 0x10) + Mod0Offset;
  33. long EhHdrEndOffset = Memory.ReadInt32(Mod0Offset + 0x14) + Mod0Offset;
  34. long ModObjOffset = Memory.ReadInt32(Mod0Offset + 0x18) + Mod0Offset;
  35. long BssSize = BssEndOffset - BssStartOffset;
  36. Memory.Manager.MapPhys(BssStartOffset, BssSize, (int)MemoryType.Normal, AMemoryPerm.RW);
  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 ElfRel GetRelocation(long Position)
  66. {
  67. long Offset = Memory.ReadInt64(Position + 0);
  68. long Info = Memory.ReadInt64(Position + 8);
  69. long Addend = Memory.ReadInt64(Position + 16);
  70. int RelType = (int)(Info >> 0);
  71. int SymIdx = (int)(Info >> 32);
  72. ElfSym Symbol = GetSymbol(SymIdx);
  73. return new ElfRel(Offset, Addend, Symbol, (ElfRelType)RelType);
  74. }
  75. private ElfSym GetSymbol(int Index)
  76. {
  77. long StrTblAddr = ImageBase + GetFirstValue(ElfDynTag.DT_STRTAB);
  78. long SymTblAddr = ImageBase + GetFirstValue(ElfDynTag.DT_SYMTAB);
  79. long SymEntSize = GetFirstValue(ElfDynTag.DT_SYMENT);
  80. long Position = SymTblAddr + Index * SymEntSize;
  81. return GetSymbol(Position, StrTblAddr);
  82. }
  83. private ElfSym GetSymbol(long Position, long StrTblAddr)
  84. {
  85. int NameIndex = Memory.ReadInt32(Position + 0);
  86. int Info = Memory.ReadByte(Position + 4);
  87. int Other = Memory.ReadByte(Position + 5);
  88. int SHIdx = Memory.ReadInt16(Position + 6);
  89. long Value = Memory.ReadInt64(Position + 8);
  90. long Size = Memory.ReadInt64(Position + 16);
  91. string Name = string.Empty;
  92. for (int Chr; (Chr = Memory.ReadByte(StrTblAddr + NameIndex++)) != 0;)
  93. {
  94. Name += (char)Chr;
  95. }
  96. return new ElfSym(Name, Info, Other, SHIdx, ImageBase, Value, Size);
  97. }
  98. private long GetFirstValue(ElfDynTag Tag)
  99. {
  100. foreach (ElfDyn Entry in Dynamic)
  101. {
  102. if (Entry.Tag == Tag)
  103. {
  104. return Entry.Value;
  105. }
  106. }
  107. return 0;
  108. }
  109. }
  110. }