1*fae6e9adSlinfeng // SPDX-License-Identifier: (Apache-2.0 OR MIT)
2*fae6e9adSlinfeng // Copyright 2017 6WIND S.A. <quentin.monnet@6wind.com>
3*fae6e9adSlinfeng
4*fae6e9adSlinfeng #[macro_use]
5*fae6e9adSlinfeng extern crate json;
6*fae6e9adSlinfeng
7*fae6e9adSlinfeng extern crate elf;
8*fae6e9adSlinfeng use std::path::PathBuf;
9*fae6e9adSlinfeng
10*fae6e9adSlinfeng extern crate rbpf;
11*fae6e9adSlinfeng use rbpf::disassembler;
12*fae6e9adSlinfeng
13*fae6e9adSlinfeng // Turn a program into a JSON string.
14*fae6e9adSlinfeng //
15*fae6e9adSlinfeng // Relies on `json` crate.
16*fae6e9adSlinfeng //
17*fae6e9adSlinfeng // You may copy this function and adapt it according to your needs. For instance, you may want to:
18*fae6e9adSlinfeng //
19*fae6e9adSlinfeng // * Remove the "desc" (description) attributes from the output.
20*fae6e9adSlinfeng // * Print integers as integers, and not as strings containing their hexadecimal representation
21*fae6e9adSlinfeng // (just replace the relevant `format!()` calls by the commented values.
to_json(prog: &[u8]) -> String22*fae6e9adSlinfeng fn to_json(prog: &[u8]) -> String {
23*fae6e9adSlinfeng // This call returns a high-level representation of the instructions, with the two parts of
24*fae6e9adSlinfeng // `LD_DW_IMM` instructions merged, and name and descriptions of the instructions.
25*fae6e9adSlinfeng // If you prefer to use a lower-level representation, use `ebpf::to_insn_vec()` function
26*fae6e9adSlinfeng // instead.
27*fae6e9adSlinfeng let insns = disassembler::to_insn_vec(prog);
28*fae6e9adSlinfeng let mut json_insns = vec![];
29*fae6e9adSlinfeng for insn in insns {
30*fae6e9adSlinfeng json_insns.push(object!(
31*fae6e9adSlinfeng "opc" => format!("{:#x}", insn.opc), // => insn.opc,
32*fae6e9adSlinfeng "dst" => format!("{:#x}", insn.dst), // => insn.dst,
33*fae6e9adSlinfeng "src" => format!("{:#x}", insn.src), // => insn.src,
34*fae6e9adSlinfeng "off" => format!("{:#x}", insn.off), // => insn.off,
35*fae6e9adSlinfeng // Warning: for imm we use a i64 instead of a i32 (to have correct values for
36*fae6e9adSlinfeng // `lddw` operation. If we print a number in the JSON this is not a problem, the
37*fae6e9adSlinfeng // internal i64 has the same value with extended sign on 32 most significant bytes.
38*fae6e9adSlinfeng // If we print the hexadecimal value as a string however, we want to cast as a i32
39*fae6e9adSlinfeng // to prevent all other instructions to print spurious `ffffffff` prefix if the
40*fae6e9adSlinfeng // number is negative. When values takes more than 32 bits with `lddw`, the cast
41*fae6e9adSlinfeng // has no effect and the complete value is printed anyway.
42*fae6e9adSlinfeng "imm" => format!("{:#x}", insn.imm as i32), // => insn.imm,
43*fae6e9adSlinfeng "desc" => insn.desc
44*fae6e9adSlinfeng ));
45*fae6e9adSlinfeng }
46*fae6e9adSlinfeng json::stringify_pretty(
47*fae6e9adSlinfeng object!(
48*fae6e9adSlinfeng "size" => json_insns.len(),
49*fae6e9adSlinfeng "insns" => json_insns
50*fae6e9adSlinfeng ),
51*fae6e9adSlinfeng 4,
52*fae6e9adSlinfeng )
53*fae6e9adSlinfeng }
54*fae6e9adSlinfeng
55*fae6e9adSlinfeng // Load a program from an object file, and prints it to standard output as a JSON string.
main()56*fae6e9adSlinfeng fn main() {
57*fae6e9adSlinfeng // Let's reuse this file from `load_elf/example`.
58*fae6e9adSlinfeng let filename = "examples/load_elf__block_a_port.elf";
59*fae6e9adSlinfeng
60*fae6e9adSlinfeng let path = PathBuf::from(filename);
61*fae6e9adSlinfeng let file = match elf::File::open_path(path) {
62*fae6e9adSlinfeng Ok(f) => f,
63*fae6e9adSlinfeng Err(e) => panic!("Error: {:?}", e),
64*fae6e9adSlinfeng };
65*fae6e9adSlinfeng
66*fae6e9adSlinfeng let text_scn = match file.get_section(".classifier") {
67*fae6e9adSlinfeng Some(s) => s,
68*fae6e9adSlinfeng None => panic!("Failed to look up .classifier section"),
69*fae6e9adSlinfeng };
70*fae6e9adSlinfeng
71*fae6e9adSlinfeng let prog = &text_scn.data;
72*fae6e9adSlinfeng
73*fae6e9adSlinfeng println!("{}", to_json(prog));
74*fae6e9adSlinfeng }
75