public override void DoCommand(IBinaryFile output, IList <IBinaryFile> inputs, LinkerScriptState state) { binary_library.elf.ElfFile ef = new binary_library.elf.ElfFile(); ef.Filename = f; ef.Read(); inputs.Add(ef); }
static void LoadELFFile(string name, Trie t) { binary_library.IBinaryFile file = new binary_library.elf.ElfFile(); file.Filename = name; file.Read(); int sym_count = file.GetSymbolCount(); for (int i = 0; i < sym_count; i++) { binary_library.ISymbol sym = file.GetSymbol(i); if ((sym.Name != null) && (sym.Name != "")) { t.AddSymbol(sym.Name, sym.Offset); } } }
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(); }
static void Main(string[] args) { if (!ParseArgs(args)) { return; } // Ensure the input file is readable System.IO.FileInfo input_fi = new System.IO.FileInfo(input); if (!input_fi.Exists) { System.Console.WriteLine("Unable to open: " + input); return; } // Determine the output filename if not specified if (output == null) { output = input_fi.FullName + ".hash"; } // Load up the input file binary_library.IBinaryFile f = new binary_library.elf.ElfFile(); f.Filename = input; f.Read(); // Determine the bitness if not specified if (bitness == -1) { switch (f.Bitness) { case binary_library.Bitness.Bits32: bitness = 0; break; case binary_library.Bitness.Bits64: bitness = 1; break; case binary_library.Bitness.BitsUnknown: System.Console.WriteLine("Warning: unable to determine bitness of " + input + " - defaulting to 32 bits"); bitness = 0; break; default: System.Console.WriteLine("Unsupported bitness of " + input + " - " + f.Bitness.ToString()); return; } } // Write the output file if (ver == 2) { // output copy of the original ELF file with a .hash section var hs = f.CreateSection(); hs.Name = ".hash"; hs.AddrAlign = 0x1000; hs.IsAlloc = true; hs.IsWriteable = false; hs.IsExecutable = false; hs.HasData = true; // locate it somewhere after the last offset and virtual address in the file if (f.IsExecutable) { long last_offset = 0; ulong last_vaddr = 0; for (int i = 0; i < f.GetSectionCount(); i++) { var s = f.GetSection(i); if (s != null && s.IsAlloc) { if (s.LoadAddress + (ulong)s.Length > last_vaddr) { last_vaddr = s.LoadAddress + (ulong)s.Length; } if (s.FileOffset + s.Length > last_offset) { last_offset = s.FileOffset + s.Length; } } } if ((last_vaddr & 0xfff) != 0) { last_vaddr = (last_vaddr + 0x1000UL) & ~0xfffUL; } if ((last_offset & 0xfff) != 0) { last_offset = (last_offset + 0x1000L) & ~0xfffL; } hs.LoadAddress = last_vaddr; hs.FileOffset = last_offset; // Create start symbol binary_library.ISymbol hss = null; // see if one exists first foreach (var sym in f.GetSymbols()) { if (sym.Name == hash_sym) { hss = sym; break; } } if (hss == null) { hss = f.CreateSymbol(); hss.Name = hash_sym; hs.AddSymbol(hss); } hss.Type = binary_library.SymbolType.Global; hss.ObjectType = binary_library.SymbolObjectType.Object; hss.Offset = hs.LoadAddress; // Update the value of update_sym if it exists if (update_sym != null) { foreach (var sym in f.GetSymbols()) { if (sym.Name == update_sym) { // get offset var offset = sym.Offset - sym.DefinedIn.LoadAddress; // write as lsb depending on bitness byte[] v; switch (f.Bitness) { case binary_library.Bitness.Bits32: v = BitConverter.GetBytes((uint)hs.LoadAddress); break; case binary_library.Bitness.Bits64: v = BitConverter.GetBytes((ulong)hs.LoadAddress); break; default: throw new NotSupportedException(); } for (int i = 0; i < v.Length; i++) { sym.DefinedIn.Data[(int)offset + i] = v[i]; } } } } } f.AddSection(hs); f.Filename = output; ((binary_library.elf.ElfFile)f).CreateHashSection = true; ((binary_library.elf.ElfFile)f).SortSymbols = true; f.Write(); } else { // generate a separate hash file Hash h = new Hash(); System.IO.FileStream fs = new System.IO.FileStream(output, System.IO.FileMode.Create); System.IO.BinaryWriter bw = new System.IO.BinaryWriter(fs); h.Write(bw, f, ver, 0, bitness); } }
public override void DoCommand(IBinaryFile output, IList<IBinaryFile> inputs, LinkerScriptState state) { binary_library.elf.ElfFile ef = new binary_library.elf.ElfFile(); ef.Filename = f; ef.Read(); inputs.Add(ef); }
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(); }