Example #1
0
        private void WriteDebugRedirect()
        {
            Region r = writer.AllocateRegion(".debug");

            r.WriteInt32(0);                                // Characteristics
            r.WriteInt32(0);                                // Timestamp
            r.WriteInt16(0);                                // Major version of debug format
            r.WriteInt16(0);                                // Minor
            r.WriteInt32(4);                                // type: IMAGE_DEBUG_TYPE_MISC
            IntToken    t             = r.InsertIntToken(); // SizeOfData;
            Placeholder startLocation = r.CurrentLocation.Increment(8);

            r.WritePlaceholderRelative(startLocation); //starts immediatly after this header
            r.WritePlaceholderFile(startLocation);     //starts immediatly after this header


            // /!\ windbg doesn't seem to allow unicode names form dbg files
            string dbgName = moduleName + ".dbg";
            int    len     = dbgName.Length + 1;
            int    fill    = 4 - (len % 4);

            if (fill == 4)
            {
                fill = 0;
            }
            len += fill;

            r.WriteInt32(1);                 // DataType: IMAGE_DEBUG_MISC_EXENAME
            r.WriteInt32(len);
            r.WriteByte(0);                  // Ascii
            r.Write(new byte[] { 0, 0, 0 }); // reserved
            r.WriteAsUtf8NullTerminated(dbgName);
            for (int i = 0; i < fill; ++i)
            {
                r.WriteByte(0);
            }
            t.SetValue((int)(r.CurrentLocation.MemoryDistanceFrom(startLocation)));
        }
Example #2
0
        public override void Close()
        {
            debugabbrev.WriteULEB(1);
            debugabbrev.WriteULEB(0x11); // compile_unit
            debugabbrev.WriteByte(0);    // no children
            debugabbrev.WriteULEB(0x10); // stmt_list
            debugabbrev.WriteULEB(0x6);
            debugabbrev.WriteULEB(0x12); // high_addr
            debugabbrev.WriteULEB(0x1);
            debugabbrev.WriteULEB(0x11); // low_addr
            debugabbrev.WriteULEB(0x1);
            debugabbrev.WriteULEB(0x3);  // name
            debugabbrev.WriteULEB(0x8);
            debugabbrev.WriteULEB(0x25); // producer
            debugabbrev.WriteULEB(0x8);
            debugabbrev.WriteByte(0x0);  // terminator
            debugabbrev.WriteByte(0x0);

            debugabbrev.WriteByte(0x0);
            foreach (string file in sourceLines.Keys)
            {
                List <SourceLine> list            = sourceLines[file];
                Placeholder       lowAddr         = list[0].placeholder;
                Placeholder       highAddr        = list[list.Count - 1].placeholder;
                IntToken          cu_length       = debuginfo.InsertIntToken(); // cu_length;
                Placeholder       cu_length_begin = debuginfo.CurrentLocation;
                debuginfo.WriteInt16(2);
                debuginfo.WriteInt32(0);
                debuginfo.WriteInt8(debuginfo.SizeOfWord);

                debuginfo.WriteULEB(1); // DW_TAG_compile_unit
                debuginfo.WriteInt32((int)debugline.CurrentLocation.Offset);
                debuginfo.WritePlaceholder(highAddr);
                debuginfo.WritePlaceholder(lowAddr);
                debuginfo.WriteAsUtf8NullTerminated(file);
                debuginfo.WriteAsUtf8NullTerminated("pluk plux0r 1.2.3");
                cu_length.SetValue((int)(debuginfo.CurrentLocation.MemoryDistanceFrom(cu_length_begin)));

                IntToken    total_length = debugline.InsertIntToken();
                Placeholder begin        = debugline.CurrentLocation;
                debugline.WriteInt16(2); //version
                IntToken    prologue_length = debugline.InsertIntToken();
                Placeholder prologue        = debugline.CurrentLocation;
                debugline.WriteByte(1); // minimum_instruction_length
                debugline.WriteByte(1); // default_is_stmt
                debugline.Write(new byte[] { 256 - 5, 14, 10, 0, 1, 1, 1, 1, 0, 0, 0, 1 });

                string directory = System.IO.Path.GetDirectoryName(file);
                string filename  = System.IO.Path.GetFileName(file);

                if (string.IsNullOrEmpty(directory))
                {
                    directory = ".";
                }
                debugline.WriteAsUtf8NullTerminated(directory);
                debugline.WriteByte(0);
                Require.NotEmpty(file);
                debugline.WriteAsUtf8NullTerminated(filename);
                debugline.Write(new byte[] { 1, 0, 0 });
                debugline.WriteByte(0);

                prologue_length.SetValue((int)(debugline.CurrentLocation.MemoryDistanceFrom(prologue)));

                int        line   = 1;
                int        column = 0;
                SourceLine prev   = new SourceLine();
                foreach (SourceLine x in list)
                {
                    if (SourceLine.Match(prev, x))
                    {
                        prev = x;
                        continue;
                    }
                    prev = x;
                    debugline.WriteByte(0); // DW_LNE_set_address
                    debugline.WriteULEB(1 + debugline.SizeOfWord);
                    debugline.WriteByte(2);
                    debugline.WritePlaceholder(x.placeholder);

                    int l = x.location.Line;
                    if (l < 0)
                    {
                        l = 0;
                    }
                    int delta = l - line;
                    line = l;
                    if (delta != 0)
                    {
                        debugline.WriteByte(3); // advance_line
                        debugline.WriteSLEB(delta);
                    }
                    if (x.location.Column != column)
                    {
                        debugline.WriteByte(5); // set_column
                        if (x.location.Column < 0)
                        {
                            column = 0;
                        }
                        else
                        {
                            column = x.location.Column;
                        }
                        debugline.WriteULEB(column);
                    }
                    if (x.mark == SourceMark.EndSequence)
                    {
                        debugline.Write(new byte[] { 0, 1, 1 }); // end_sequence
                        line   = 1;
                        column = 0;
                    }
                    else
                    {
                        debugline.WriteByte(1); // copy
                    }
                }
                debugline.Write(new byte[] { 0, 1, 1 }); // end_sequence
                total_length.SetValue((int)(debugline.CurrentLocation.MemoryDistanceFrom(begin)));
            }
        }
Example #3
0
        private void WriteVersionHeader()
        {
            Region resourceHeader = writer.AllocateRegion(".rsrc");

            // Resouce Directory Table
            resourceHeader.WriteInt32(0);          //Characteristics
            resourceHeader.WriteInt32(0);          // TimeDateStamp
            resourceHeader.WriteInt16(0);          // mayor version
            resourceHeader.WriteInt16(0);          // minor verson
            resourceHeader.WriteInt16(0);          // number of named entries
            resourceHeader.WriteInt16(1);          // number of id'ed entries
            // Resource Directory Entry
            resourceHeader.WriteInt32(0x10);       // ID (VERSION)
            resourceHeader.WriteInt32(0x80000018); //pointer to next directory
            // Resouce Directory Table
            resourceHeader.WriteInt32(0);          //Characteristics
            resourceHeader.WriteInt32(0);          // TimeDateStamp
            resourceHeader.WriteInt16(0);          // mayor version
            resourceHeader.WriteInt16(0);          // minor verson
            resourceHeader.WriteInt16(0);          // number of named entries
            resourceHeader.WriteInt16(1);          // number of id'ed entries
            // Resource Directory Entry
            resourceHeader.WriteInt32(1);          // ID (1)
            resourceHeader.WriteInt32(0x80000030); //pointer to next directory
            // Resouce Directory Table
            resourceHeader.WriteInt32(0);          //Characteristics
            resourceHeader.WriteInt32(0);          // TimeDateStamp
            resourceHeader.WriteInt16(0);          // mayor version
            resourceHeader.WriteInt16(0);          // minor verson
            resourceHeader.WriteInt16(0);          // number of named entries
            resourceHeader.WriteInt16(1);          // number of id'ed entries
            // Resource Directory Entry
            resourceHeader.WriteInt32(0);          // ID
            resourceHeader.WriteInt32(0x48);       //pointer to leaf
            // Resource Data Entry
            resourceHeader.WritePlaceholderRelative(resourceHeader.CurrentLocation.Increment(0x10));
            IntToken SizeOfResourceBody1 = resourceHeader.InsertIntToken();

            resourceHeader.WriteInt32(0); // codepage (default unicode)
            resourceHeader.WriteInt32(0); // reserved

            long RelativeSizeOfHeader = resourceHeader.CurrentLocation.Offset;

            // struct VS_VERSIONINFO {
            WordToken SizeOfResourceBody2 = resourceHeader.InsertWordToken(); // wLength

            resourceHeader.WriteInt16(0x34);                                  // wValueLength
            resourceHeader.WriteInt16(0);                                     // wType
            resourceHeader.WriteAsUtf16NullTerminated2("VS_VERSION_INFO");
            resourceHeader.WriteInt16(0);                                     // padding1

            // struct VS_FIXEDFILEINFO {
            resourceHeader.WriteInt32(0xFEEF04BD); // dwSignature
            resourceHeader.WriteInt32(0x00010000); // dwStrucVersion
            resourceHeader.WriteInt32(0x00010000); // dwFileVersionMS
            resourceHeader.WriteInt32(0x00000000); // dwFileVersionLS
            resourceHeader.WriteInt32(0x00010000); // dwProductVersionMS
            resourceHeader.WriteInt32(0x00000000); // dwProductVersionLS

            resourceHeader.WriteInt32(0x3f);       // dwFileFlagsMask;
            resourceHeader.WriteInt32(0);          // dwFileFlags;
            resourceHeader.WriteInt32(4);          // dwFileOS;
            resourceHeader.WriteInt32(1);          // dwFileType
            resourceHeader.WriteInt32(0);          // dwFileSubtype
            resourceHeader.WriteInt32(0);          // dwFileDateMS
            resourceHeader.WriteInt32(0);          // dwFileDateLS

            // rest of  //struct VS_VERSIONINFO {
            //  WORD  Padding2[];
            //WORD  Children[];

            WordToken varFileInfo = WriteEntry(resourceHeader, 0, 1, "VarFileInfo");
            WordToken translation = WriteEntry(resourceHeader, 4, 0, "Translation");

            resourceHeader.WriteInt32(0x04B00000); // codepage unicode
            translation.SetDistanceSinceTaken();
            varFileInfo.SetDistanceSinceTaken();
            WordToken varStringFileInfo = WriteEntry(resourceHeader, 0, 1, "StringFileInfo");
            WordToken varVersion        = WriteEntry(resourceHeader, 0, 1, "000004b0");

            WriteEntry2(resourceHeader, "FileDescription", "Something compiled by Bart's compiler.");
            WriteEntry2(resourceHeader, "FileVersion", "1.0.0.0");
            WriteEntry2(resourceHeader, "InternalName", moduleName + ".exe");
            WriteEntry2(resourceHeader, "LegalCopyright", " ");
            WriteEntry2(resourceHeader, "OriginalFilename", moduleName + ".exe");
            WriteEntry2(resourceHeader, "ProductName", moduleName);
            WriteEntry2(resourceHeader, "ProductVersion", "1.0.0.0");
            varVersion.SetDistanceSinceTaken();
            varStringFileInfo.SetDistanceSinceTaken();

            RelativeSizeOfHeader = resourceHeader.CurrentLocation.Offset - RelativeSizeOfHeader;
            SizeOfResourceBody1.SetValue((int)RelativeSizeOfHeader);
            SizeOfResourceBody2.SetValue((int)RelativeSizeOfHeader);
        }