示例#1
0
        /// <summary>
        /// Creates the elf32 file.
        /// </summary>
        /// <param name="compiler">The compiler.</param>
        private void CreateElf32File(CompilerFramework.AssemblyCompiler compiler)
        {
            using (System.IO.FileStream fs = new System.IO.FileStream(OutputFile, System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.None)) {
                Elf32Header header = new Elf32Header();
                header.Type = Elf32FileType.Executable;
                header.Machine = Elf32MachineType.Intel386;
                header.SectionHeaderNumber = (ushort)(Sections.Count + 2);
                header.SectionHeaderOffset = header.ElfHeaderSize;

                header.CreateIdent(Elf32IdentClass.Class32, Elf32IdentData.Data2LSB, null);

                // Calculate the concatenated size of all section's data
                uint offset = 0;
                foreach (Elf32Section section in Sections) {
                    offset += (uint)section.Length;
                }
                offset += (uint)_nullSection.Length;
                offset += (uint)_stringTableSection.Length;

                // Calculate offsets
                header.ProgramHeaderOffset = header.ElfHeaderSize + header.SectionHeaderEntrySize * (uint)header.SectionHeaderNumber + offset;
                header.ProgramHeaderNumber = 1;
                header.SectionHeaderStringIndex = 1;

                System.IO.BinaryWriter writer = new System.IO.BinaryWriter(fs);

                // Write the ELF Header
                header.Write(writer);

                // Overjump the Section Header Table and write the section's data first
                long tmp = fs.Position;
                writer.Seek((int)(tmp + header.SectionHeaderNumber * header.SectionHeaderEntrySize), System.IO.SeekOrigin.Begin);

                _nullSection.Write(writer);
                _stringTableSection.Write(writer);

                // Write the _sections
                foreach (Elf32Section section in Sections)
                    section.Write(writer);

                // Jump back to the Section Header Table
                writer.Seek((int)tmp, System.IO.SeekOrigin.Begin);

                _nullSection.WriteHeader(writer);
                _stringTableSection.WriteHeader(writer);

                // Write the section headers
                foreach (Elf32Section section in Sections)
                    section.WriteHeader(writer);

                Elf32ProgramHeader pheader = new Elf32ProgramHeader
                                                 {
                                                     Alignment = 0,
                                                     FileSize = (uint)GetSection(SectionKind.Text).Length
                                                 };
                pheader.MemorySize = pheader.FileSize;
                pheader.VirtualAddress = 0xFF0000;
                pheader.Flags = Elf32ProgramHeaderFlags.Execute | Elf32ProgramHeaderFlags.Read | Elf32ProgramHeaderFlags.Write;
                pheader.Offset = ((Elf32Section)GetSection(SectionKind.Text)).Header.Offset;
                pheader.Type = Elf32ProgramHeaderType.Load;

                writer.Seek((int)header.ProgramHeaderOffset, System.IO.SeekOrigin.Begin);
                pheader.Write(writer);
            }
        }
示例#2
0
        /// <summary>
        /// Performs stage specific processing on the compiler context.
        /// </summary>
        /// <param name="compiler">The compiler context to perform processing in.</param>
        public override void Run(CompilerFramework.AssemblyCompiler compiler)
        {
            if (String.IsNullOrEmpty(OutputFile))
                throw new ArgumentException(@"Invalid argument.", "compiler");

            // Layout the _sections in memory
            LayoutSections();

            // Resolve all symbols first
            base.Run(compiler);

            // Persist the Elf32 file now
            CreateElf32File(compiler);
        }