Beispiel #1
0
        static void Main(string[] args)
        {
            if (ParseArgs(args) == false)
            {
                DispUsage();
                return;
            }

            // Initialize the default linker scripts
            DefaultScripts.InitScripts();

            // Load the input files
            if(ifiles.Count == 0)
            {
                Console.WriteLine("No input files!");
                return;
            }
            List<binary_library.IBinaryFile> inputs = new List<binary_library.IBinaryFile>();
            foreach (string ifile in ifiles)
            {
                binary_library.elf.ElfFile ef = new binary_library.elf.ElfFile();
                ef.Filename = ifile;
                ef.Read();
                inputs.Add(ef);
            }

            // Determine the architecture
            string cur_arch = null;
            if (options["arch"].Set)
                cur_arch = options["arch"].String;
            foreach (binary_library.IBinaryFile ifile in inputs)
            {
                if (cur_arch == null)
                    cur_arch = ifile.NameTriple;
            }

            string[] ntriple = cur_arch.Split('-');
            if(ntriple.Length != 3)
            {
                Console.WriteLine("Error: invalid architecture: " + cur_arch);
                throw new Exception();
            }

            // Check input files are of the appropriate architecture
            foreach (var ifile in inputs)
            {
                if (ntriple[0] != ifile.Architecture)
                {
                    Console.WriteLine("Error: " + ifile.Filename + " is not built for architecture " + ntriple[0]);
                    throw new Exception();
                }
            }

            // Create an output file
            binary_library.IBinaryFile of = null;
            string oformat = null;
            if (options["oformat"].Set)
                oformat = options["oformat"].String.ToLower();
            else
                oformat = "elf";

            if (oformat == "elf")
                of = new binary_library.elf.ElfFile();
            else if (oformat == "binary")
                of = new binary_library.binary.FlatBinaryFile();
            else if (oformat == "hex")
                of = new binary_library.binary.HexFile();
            else
                throw new Exception("Unsupported output format: " + oformat);

            if (output_file == null)
                output_file = "a.out";
            
            of.Init();
            of.Architecture = ntriple[0];
            of.BinaryType = ntriple[1];
            of.OS = ntriple[2];

            // Get the linker script
            LinkerScript script = DefaultScripts.GetScript(ntriple);
            
            // Run the script
            script.RunScript(of, inputs);

            of.Filename = output_file;
            of.Write();
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            if (ParseArgs(args) == false)
            {
                DispUsage();
                return;
            }

            InitMaps();

            // Initialize the default linker scripts
            DefaultScripts.InitScripts();

            // Load the input files
            if (ifiles.Count == 0)
            {
                Console.WriteLine("No input files!");
                return;
            }
            List <binary_library.IBinaryFile> inputs = new List <binary_library.IBinaryFile>();

            foreach (string ifile in ifiles)
            {
                binary_library.elf.ElfFile ef = new binary_library.elf.ElfFile();
                ef.Filename = ifile;
                ef.Read();
                inputs.Add(ef);
            }

            // Determine the architecture
            string cur_arch = null;

            if (options["arch"].Set)
            {
                cur_arch = options["arch"].String;
            }
            foreach (binary_library.IBinaryFile ifile in inputs)
            {
                if (cur_arch == null)
                {
                    cur_arch = ifile.NameTriple;
                }
            }

            string[] ntriple = cur_arch.Split('-');
            if (ntriple.Length != 3)
            {
                Console.WriteLine("Error: invalid architecture: " + cur_arch);
                throw new Exception();
            }

            // Check input files are of the appropriate architecture
            foreach (var ifile in inputs)
            {
                if (ntriple[0] != ifile.Architecture)
                {
                    Console.WriteLine("Error: " + ifile.Filename + " is not built for architecture " + ntriple[0]);
                    throw new Exception();
                }
            }

            // Determine output file type
            string oformat = null;

            binary_library.Bitness bn = binary_library.Bitness.BitsUnknown;
            if (options["oformat"].Set)
            {
                oformat = options["oformat"].String.ToLower();
                if (bitness_map.ContainsKey(oformat))
                {
                    bn = bitness_map[oformat];
                }
                if (oformat == "elf32" || oformat == "elf64")
                {
                    oformat = "elf";
                }
            }
            else if (oformat_map.ContainsKey(ntriple[1]))
            {
                oformat = oformat_map[ntriple[1]];
            }
            else
            {
                oformat = "elf";
            }

            if (bn == binary_library.Bitness.BitsUnknown && bitness_map.ContainsKey(ntriple[0]))
            {
                bn = bitness_map[ntriple[0]];
            }

            // Create an output file
            binary_library.IBinaryFile of = null;

            if (oformat == "elf")
            {
                of = new binary_library.elf.ElfFile(bn);
            }
            else if (oformat == "binary")
            {
                of = new binary_library.binary.FlatBinaryFile();
            }
            else if (oformat == "hex")
            {
                of = new binary_library.binary.HexFile();
            }
            else
            {
                throw new Exception("Unsupported output format: " + oformat);
            }

            if (output_file == null)
            {
                output_file = "a.out";
            }

            of.Init();
            of.Architecture = ntriple[0];
            if (ntriple[1] == "elf" && bn == binary_library.Bitness.Bits64)
            {
                of.BinaryType = "elf64";
            }
            else
            {
                of.BinaryType = ntriple[1];
            }
            of.OS = ntriple[2];

            // Get the linker script
            LinkerScript script = DefaultScripts.GetScript(ntriple);

            // Fill out comment section
            comment += "triple: " + of.Architecture + "-" + of.BinaryType + "-" + of.OS + nl;
            comment += "arch: " + of.Architecture + nl;
            comment += "binarytype: " + of.Architecture + nl;
            comment += "os: " + of.OS + nl;
            comment += "script: " + script.Name + nl;
            comment += "comp-date: " + DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString() + nl;
            comment += "endtl" + nl + "\0";

            // Run the script
            script.RunScript(of, inputs);

            // ELF specific options
            if (of is binary_library.elf.ElfFile)
            {
                var ef = of as binary_library.elf.ElfFile;
                ef.CreateHashSection = gen_hash;
            }

            of.Filename = output_file;
            of.Write();
        }