/src/main.rs

https://gitlab.com/juggle-tux/elf.rs · Rust · 91 lines · 81 code · 10 blank · 0 comment · 8 complexity · 1b7c380bcf4c0c706f3bc0aad0b76d6a MD5 · raw file

  1. #![cfg(feature="cli")]
  2. #[macro_use]
  3. extern crate clap;
  4. #[macro_use]
  5. extern crate log;
  6. extern crate env_logger;
  7. extern crate elf;
  8. use clap::ArgMatches;
  9. use elf::{Elf, ReadElf, Result};
  10. use elf::file::Headers;
  11. use elf::parser::Address;
  12. use env_logger::{LogBuilder, LogTarget};
  13. use log::LogLevelFilter;
  14. use std::fs::File;
  15. fn main() {
  16. let args = clap_app!(
  17. (crate_name!()) =>
  18. (version: (crate_version!()))
  19. (author: (crate_authors!()))
  20. (about: (crate_description!()))
  21. (@arg file: * "Path to elf file to use")
  22. (@arg print_header: -e --elf "Prints the elf header")
  23. (@arg print_sections: -s --sections "Prints the section headers")
  24. (@arg print_programs: -p --programs "Prints the program headers")
  25. (@arg verbose: -v --verbose ... "Sets the level of verbosity")
  26. (@arg quite: -q --quite "Be quite")
  27. ).get_matches();
  28. LogBuilder::new()
  29. .target(LogTarget::Stderr)
  30. .filter(None, {
  31. match args.occurrences_of("verbose") {
  32. 0 if args.is_present("quite") => LogLevelFilter::Off,
  33. 0 => LogLevelFilter::Error,
  34. 1 => LogLevelFilter::Info,
  35. 2 => LogLevelFilter::Debug,
  36. _ => LogLevelFilter::Trace,
  37. }
  38. })
  39. .init().unwrap();
  40. run(args).unwrap_or_else(|err| {
  41. error!("{}", err);
  42. ::std::process::exit(1)
  43. })
  44. }
  45. fn run(args: ArgMatches) -> Result<()> {
  46. info!("Running {} {}", crate_name!(), crate_version!());
  47. File::open(args.value_of("file").unwrap())
  48. .map_err(Into::into)
  49. .and_then(|mut file| file.read_elf())
  50. .map(|elf| {
  51. let e = args.is_present("print_header");
  52. let s = args.is_present("print_sections");
  53. let p = args.is_present("print_programms");
  54. match elf {
  55. Elf::File32(ref headers) => print_headers(headers, e, p, s),
  56. Elf::File64(ref headers) => print_headers(headers, e, p ,s),
  57. }
  58. })
  59. }
  60. fn print_headers<A: Address>(
  61. headers: &Headers<A>,
  62. elf: bool,
  63. program: bool,
  64. section: bool
  65. ) {
  66. if elf {
  67. println!("{}", &headers.elf_header);
  68. }
  69. if program {
  70. println!("program headers:");
  71. for (idx, ph) in headers.program_table.iter().enumerate() {
  72. println!("[{}]\n{}", idx, ph)
  73. }
  74. }
  75. if section {
  76. println!("section headers:");
  77. for (idx, (name, sh)) in headers.section_table.iter().enumerate() {
  78. println!("[{}]: {:?}\n{}", idx, name, sh)
  79. }
  80. }
  81. }