Example #1
0
        public byte[] ReadExeFs(long addr, int size)
        {
            long pos = addr;

            byte[] fullBuffer = new byte[size];

            while (pos < addr + size)
            {
                var page = GetAddressPage(pos);
                if ((page.Permission & SegPerm.R) == SegPerm.R)
                {
                    Nso  curNso   = GetAddressNso(pos);
                    long baseAddr = GetNsoAddress((NsoTypeEnum)Nsos.ToList().IndexOf(curNso));
                    long off      = pos - page.Address;
                    int  curSize  = (int)Math.Min(page.Size - off, size);

                    byte[] sub = curNso.ReadNso((uint)(pos - baseAddr), curSize);
                    Buffer.BlockCopy(sub, 0, fullBuffer, (int)(pos - addr), sub.Length);
                    pos += curSize;
                }
                else
                {
                    pos += page.Size;
                }
            }

            return(fullBuffer);
        }
Example #2
0
        public ModHeader(Nso nso)
        {
            baseAddr = nso.GetModAddr();

            byte[] buffer = nso.ReadNso((uint)baseAddr, 0x1C);
            using (MemoryStream ms = new MemoryStream(buffer))
            {
                BinaryReader br = new BinaryReader(ms);

                string magic = Encoding.ASCII.GetString(br.ReadBytes(4));
                if (magic != HEADER_MAGIC)
                {
                    throw new NsoException("Invalid MOD0 header");
                }

                dynAddr      = br.ReadInt32();
                bssStart     = br.ReadInt32();
                bssEnd       = br.ReadInt32();
                ehFrameStart = br.ReadInt32();
                ehFrameEnd   = br.ReadInt32();
                moduleObj    = br.ReadInt32();

                DynInfo = new DynamicInfo(nso, DynamicAddr);
            }
        }
Example #3
0
        public void Rewrite(Nso nso, uint newAddr)
        {
            dynAddr      = (int)(DynamicAddr - newAddr);
            bssStart     = (int)(BssStartAddr - newAddr);
            bssEnd       = (int)(BssEndAddr - newAddr);
            ehFrameStart = (int)(EhStartAddr - newAddr);
            ehFrameEnd   = (int)(EhEndAddr - newAddr);
            moduleObj    = (int)(ModuleObjAddr - newAddr);

            Write(nso);
        }
Example #4
0
 public Elf64_Sym(Nso nso, uint addr)
 {
     byte[] buffer = nso.ReadNso(addr, ELF64_SYM_SIZE);
     using (MemoryStream ms = new MemoryStream(buffer))
     {
         BinaryReader br = new BinaryReader(ms);
         st_name  = br.ReadUInt32();
         st_info  = br.ReadByte();
         st_other = br.ReadByte();
         st_shndx = br.ReadUInt16();
         st_value = br.ReadUInt64();
         st_size  = br.ReadUInt64();
     }
 }
Example #5
0
        public static Nso CreateNso(byte[] text, byte[] rodata, byte[] rwdata, uint bssSize)
        {
            Nso nso = new Nso();

            nso.Flag = 0x1F;

            uint addr = 0;

            nso.segments[(int)NsoSegmentType.text] = new NsoSegment(addr, text, NsoSegmentType.text);
            addr += (uint)(((uint)text.Length + 0xFFF) & ~0xFFF);

            nso.segments[(int)NsoSegmentType.rodata] = new NsoSegment(addr, rodata, NsoSegmentType.rodata);
            addr += (uint)(((uint)rodata.Length + 0xFFF) & ~0xFFF);

            nso.segments[(int)NsoSegmentType.rwdata] = new NsoSegment(addr, rwdata, NsoSegmentType.rwdata, bssSize);
            return(nso);
        }
Example #6
0
        public static ModHeader CreateModHeader(Nso nso, long modBase, long dyn, long ehstart, long ehend, long modObj)
        {
            ModHeader mod = new ModHeader();

            mod.baseAddr = modBase;

            mod.DynamicAddr   = dyn;
            mod.BssStartAddr  = nso.RwdataSegment.Address + nso.RwdataSegment.DecompressedSize;
            mod.BssEndAddr    = mod.BssStartAddr + nso.RwdataSegment.BssOrAlign;
            mod.EhStartAddr   = ehstart;
            mod.EhEndAddr     = ehend;
            mod.ModuleObjAddr = modObj;

            mod.Write(nso);

            return(mod);
        }
Example #7
0
        public ExeFS(string dirpath)
        {
            for (int i = 0; i < MAX_NSO_COUNT; i++)
            {
                string filepath = $@"{dirpath}\{(NsoTypeEnum)i}";

                if (File.Exists(filepath))
                {
                    Nsos[i] = new Nso(filepath);
                }
            }

            if (File.Exists(dirpath + "\\" + NPDM_PATH))
            {
                Npdm = File.ReadAllBytes(dirpath + "\\" + NPDM_PATH);
            }
        }
Example #8
0
        public void Write(Nso nso)
        {
            byte[] raw = new byte[HEADER_SIZE];
            using (MemoryStream ms = new MemoryStream(raw))
            {
                BinaryWriter bw = new BinaryWriter(ms);
                bw.Write(Encoding.ASCII.GetBytes(HEADER_MAGIC));
                bw.Write(dynAddr);
                bw.Write(bssStart);
                bw.Write(bssEnd);
                bw.Write(ehFrameStart);
                bw.Write(ehFrameEnd);
                bw.Write(moduleObj);
            }

            nso.WriteNso(4, BitConverter.GetBytes((uint)baseAddr));
            nso.WriteNso((uint)baseAddr, raw);
        }
Example #9
0
        public static Nso CreateRwDataNso(byte[] rwdata)
        {
            Nso nso = new Nso();

            nso.Flag = 0x1F;

            byte[] text = new byte[0];
            byte[] rodata;

            //mod0
            using (MemoryStream ms = new MemoryStream())
            {
                BinaryWriter bw = new BinaryWriter(ms);
                bw.Write(Encoding.ASCII.GetBytes("MOD0"));
                bw.Write(0); //dyninfo
                bw.Write(0); //bss start
                bw.Write(0); //bss end
                bw.Write(0); //eh_frame_hdr start
                bw.Write(0); //eh_frame_hdr end

                rodata = ms.GetBuffer();
            }

            uint addr = 0;

            nso.segments[(int)NsoSegmentType.text] = new NsoSegment(addr, text, NsoSegmentType.text);
            addr += (uint)(((uint)text.Length + 0xFFF) & ~0xFFF);

            nso.segments[(int)NsoSegmentType.rodata] = new NsoSegment(addr, rodata, NsoSegmentType.rodata);
            addr += (uint)(((uint)rodata.Length + 0xFFF) & ~0xFFF);

            nso.segments[(int)NsoSegmentType.rwdata] = new NsoSegment(addr, rwdata, NsoSegmentType.rwdata, 0);

            try
            {
                nso.ModSection = new ModHeader(nso);
            }
            catch (NsoException)
            {
                nso.ModSection = null;
            }

            return(nso);
        }
Example #10
0
        public DynamicInfo(Nso nso, long addr)
        {
            byte[] buffer = nso.ReadNso((uint)addr, 0x200);
            using (MemoryStream ms = new MemoryStream(buffer))
            {
                BinaryReader br  = new BinaryReader(ms);
                Elf64_Dyn    dyn = new Elf64_Dyn(br);

                while (dyn.d_tag != DtTagEnum.DT_NULL)
                {
                    foreach (var item in fields)
                    {
                        if (dyn.d_tag == item.Item1)
                        {
                            this.GetType().GetProperty(item.Item2).SetValue(this, dyn.d_un);
                        }
                    }

                    dyn = new Elf64_Dyn(br);
                }
            }
        }
Example #11
0
        public void WriteExeFs(long addr, byte[] data)
        {
            var page = GetAddressPage(addr);

            if ((page.Permission & SegPerm.R) == SegPerm.R)
            {
                Nso curNso = GetAddressNso(addr);

                var baseAddr = GetNsoAddress(GetAddressNsoType(addr).Value);
                curNso.WriteNso((uint)(addr - baseAddr), data);
            }

            /*
             * long pos = addr;
             * int dataOff = 0;
             *
             * while (pos < addr + data.Length)
             * {
             *  var page = GetAddressPage(pos);
             *  if ((page.Permission & SegPerm.R) == SegPerm.R)
             *  {
             *      Nso curNso = GetAddressNso(pos);
             *      long baseAddr = GetNsoAddress((NsoType)Nsos.ToList().IndexOf(curNso));
             *      long off = pos - page.Address;
             *      int curSize = (int)Math.Min(page.Size - off, data.Length);
             *
             *      byte[] sub = curNso.ReadNso((uint)(pos - baseAddr), curSize);
             *      byte[] sub = new byte[curSize];
             *      curNso.WriteNso();
             *      Buffer.BlockCopy(sub, 0, fullBuffer, (int)(pos - addr), sub.Length);
             *      pos += curSize;
             *  }
             *  else pos += page.Size;
             * }
             */
        }
Example #12
0
 public NsoStream(Nso nso, bool ignorePermR = false) : base()
 {
     IgnoreReadPerm = ignorePermR;
     this.nso       = nso;
 }
Example #13
0
 public ExeFS(Nso main)
 {
     Nsos[(int)NsoTypeEnum.main] = main;
 }
Example #14
0
 public void SetNso(Nso nso, NsoTypeEnum type)
 {
     Nsos[(int)type] = nso;
 }