1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
//! Library to parse stack usage information ([`.stack_sizes`]) produced by LLVM
//!
//! [`.stack_sizes`]: https://llvm.org/docs/CodeGenerator.html#emitting-function-stack-size-information

#![deny(missing_docs)]
#![deny(warnings)]

extern crate byteorder;
extern crate either;
#[macro_use]
extern crate failure;
extern crate leb128;
#[cfg(feature = "tools")]
extern crate rustc_demangle;
extern crate xmas_elf;

use std::{collections::HashMap, io::Cursor, u32};
#[cfg(feature = "tools")]
use std::{fs::File, io::Read, path::Path};

use byteorder::{ReadBytesExt, LE};
use either::Either;
use xmas_elf::{
    sections::{SectionData, SectionHeader},
    symbol_table::{Entry, Type},
    ElfFile,
};

/// Information about a function
pub struct Function<'a, A> {
    address: A,
    name: &'a str,
    stack: u64,
}

impl<'a, A> Function<'a, A> {
    /// Returns the address of the function
    pub fn address(&self) -> A
    where
        A: Copy,
    {
        self.address
    }

    /// Returns the (mangled) name of the function
    pub fn name(&self) -> &'a str {
        self.name
    }

    /// Returns the stack usage of the function in bytes
    pub fn stack(&self) -> u64 {
        self.stack
    }
}

/// Parses an ELF file and returns a list of functions and their stack usage
pub fn analyze(
    elf: &[u8],
) -> Result<Either<Vec<Function<u32>>, Vec<Function<u64>>>, failure::Error> {
    let elf = ElfFile::new(elf).map_err(failure::err_msg)?;

    let mut names = HashMap::new();

    if let Some(section) = elf.find_section_by_name(".symtab") {
        match section.get_data(&elf).map_err(failure::err_msg)? {
            SectionData::SymbolTable32(entries) => {
                for entry in entries {
                    if entry.get_type() == Ok(Type::Func) {
                        names.insert(
                            entry.value(),
                            entry.get_name(&elf).map_err(failure::err_msg)?,
                        );
                    }
                }
            }
            SectionData::SymbolTable64(entries) => {
                for entry in entries {
                    if entry.get_type() == Ok(Type::Func) {
                        names.insert(
                            entry.value(),
                            entry.get_name(&elf).map_err(failure::err_msg)?,
                        );
                    }
                }
            }
            _ => bail!("malformed .symtab section"),
        }
    }

    let stack_sizes = elf
        .find_section_by_name(".stack_sizes")
        .ok_or_else(|| failure::err_msg(".stack_sizes section not found"))?;

    let data = stack_sizes.raw_data(&elf);
    let end = data.len() as u64;
    let mut cursor = Cursor::new(data);

    match stack_sizes {
        SectionHeader::Sh32(..) => {
            let mut funs = vec![];

            while cursor.position() < end {
                let address = cursor.read_u32::<LE>()?;
                // NOTE we also try the address plus one because this could be a function in Thumb
                // mode
                let name = names
                    .get(&(address as u64))
                    .or(names.get(&(address as u64 + 1)))
                    .map(|s| *s)
                    .unwrap_or("?");
                let stack = leb128::read::unsigned(&mut cursor)?;

                funs.push(Function {
                    address,
                    stack,
                    name,
                });
            }

            funs.sort_by(|a, b| b.stack().cmp(&a.stack()));

            Ok(Either::Left(funs))
        }
        SectionHeader::Sh64(..) => {
            let mut funs = vec![];

            while cursor.position() < end {
                let address = cursor.read_u64::<LE>()?;
                // NOTE we also try the address plus one because this could be a function in Thumb
                // mode
                let name = names
                    .get(&address)
                    .or(names.get(&(address + 1)))
                    .map(|s| *s)
                    .unwrap_or("?");
                let stack = leb128::read::unsigned(&mut cursor)?;

                funs.push(Function {
                    address,
                    stack,
                    name,
                });
            }

            funs.sort_by(|a, b| b.stack().cmp(&a.stack()));

            Ok(Either::Right(funs))
        }
    }
}

#[cfg(feature = "tools")]
#[doc(hidden)]
pub fn run<P>(path: P) -> Result<(), failure::Error>
where
    P: AsRef<Path>,
{
    let mut bytes = vec![];
    File::open(path)?.read_to_end(&mut bytes)?;

    let funs = analyze(&bytes)?;

    match funs {
        Either::Left(funs) => {
            // 32-bit address space
            println!("address\t\tstack\tname");

            for fun in funs {
                println!(
                    "{:#010x}\t{}\t{}",
                    fun.address(),
                    fun.stack(),
                    rustc_demangle::demangle(fun.name())
                );
            }
        }
        Either::Right(funs) => {
            // 64-bit address space
            println!("address\t\t\tstack\tname");

            for fun in funs {
                println!(
                    "{:#018x}\t{}\t{}",
                    fun.address(),
                    fun.stack(),
                    rustc_demangle::demangle(fun.name())
                );
            }
        }
    }

    Ok(())
}