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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
//! Data transformations involving the `resymgen` YAML format and other formats. Implements the
//! `gen` and `merge` commands.

use std::borrow::Cow;
use std::collections::BTreeSet;
use std::convert::AsRef;
use std::error::Error;
use std::fs::{self, File};
use std::path::{Path, PathBuf};

use tempfile::NamedTempFile;

use super::data_formats::symgen_yml::{IntFormat, LoadParams, Sort, Subregion, SymGen, Symbol};
use super::data_formats::{Generate, InFormat, OutFormat};
use super::util;

/// Forms the output file path from the base, version, and format.
fn output_file_name(base: &Path, version: &str, format: &OutFormat) -> PathBuf {
    let output_stem = match base.file_stem() {
        Some(s) => {
            let mut stem = s.to_os_string();
            if !version.is_empty() {
                // Append the version as a suffix if it exists
                stem.push("_");
                stem.push(version);
            }
            stem
        }
        None => {
            // path is empty or invalid; fall back to just the version
            if !version.is_empty() {
                version.into()
            } else {
                // no path or version; use the format extension as the output stem...
                format.extension().into()
            }
        }
    };
    base.with_file_name(output_stem)
        .with_extension(format.extension())
}

/// Generates symbol tables from a given SymGen struct for multiple different formats/versions.
fn generate_symbols<P: AsRef<Path>>(
    symgen: &SymGen,
    formats: &[OutFormat],
    versions: &[&str],
    output_base: P,
) -> Result<(), Box<dyn Error>> {
    for fmt in formats.iter() {
        for version in versions.iter() {
            // Write to a tempfile first, then persist atomically.
            let output_file = output_file_name(output_base.as_ref(), version, fmt);
            let f_gen = NamedTempFile::new()?;
            fmt.generate(&f_gen, symgen, version)?;
            // Make sure the parent directory exists first
            if let Some(parent) = output_file.parent() {
                fs::create_dir_all(parent)?;
            }
            util::persist_named_temp_file_safe(f_gen, output_file)?;
        }
    }
    Ok(())
}

/// Gets a list of all version names within a SymGen.
/// 1. If a version list is explicitly specified by blocks, use those.
/// 2. If a block does not explicitly specify a version list, infer it
/// based on the addresses it contains.
/// 3. If blocks with symbols exist but none has an explicit version, return
/// a vector containing a single empty string ("").
fn all_version_names(symgen: &SymGen) -> Vec<&str> {
    let mut vers = BTreeSet::new();
    let mut symgen_has_symbols: bool = false;
    let mut versions_inferred_from_symbols: bool = false;
    for b in symgen.blocks() {
        if let Some(v) = &b.versions {
            // Explicit version list
            for name in v.iter().map(|v| v.name()) {
                vers.insert(name);
            }
        } else {
            // Inferred version list
            for name in b.address.versions().map(|v| v.name()) {
                vers.insert(name);
            }
            for s in b.iter() {
                symgen_has_symbols = true;
                for name in s.address.versions().map(|v| v.name()) {
                    versions_inferred_from_symbols = true;
                    vers.insert(name);
                }
            }
        }
    }
    if symgen_has_symbols && vers.is_empty() {
        // Special case: symbols exist but all are Common
        return vec![""];
    } else if versions_inferred_from_symbols && !vers.is_empty() {
        // XXX: A non-empty version list was inferred from symbol contents. Unfortunately, this
        // won't actually give the desired generation output for blocks without an explicit
        // version list, since the version-realization step during generation won't work properly
        // without one. Warn the user of this, and tell them to use the --complete-version-list
        // check to enforce an explicit version list on all blocks.
        eprintln!(
            "Warning: Detected one or more blocks containing by-version symbol addresses, but \
            without an explicit version list. Proceeding with inferred version list, but the \
            generated output might be incomplete. Use `check --complete-version-list` to enforce \
            explicit version lists on all blocks."
        );
    }
    vers.into_iter().collect()
}

/// Generates symbol tables from a given `input_file` for multiple different `output_formats` and
/// `output_versions`.
///
/// Output is written to filepaths based on `output_base`. Both `output_formats` and
/// `output_versions` default to all formats/versions if `None`. If `sort_output` is true, the
/// function and data sections of the output symbol tables will each be sorted by symbol address.
///
/// # Examples
/// ```ignore
/// generate_symbol_tables(
///     "/path/to/symbols.yml",
///     Some([OutFormat::Ghidra]),
///     Some("v1"),
///     false,
///     "/path/to/out/symbols",
/// )
/// .expect("failed to generate symbol tables");
/// ```
pub fn generate_symbol_tables<'v, I, F, V, O>(
    input_file: I,
    output_formats: Option<F>,
    output_versions: Option<V>,
    sort_output: bool,
    output_base: O,
) -> Result<(), Box<dyn Error>>
where
    I: AsRef<Path>,
    F: AsRef<[OutFormat]>,
    V: AsRef<[&'v str]>,
    O: AsRef<Path>,
{
    let input_file = input_file.as_ref();
    let mut contents = {
        let file = File::open(input_file)?;
        SymGen::read(&file)?
    };
    contents.resolve_subregions(Subregion::subregion_dir(input_file), |p| File::open(p))?;
    contents.collapse_subregions();
    if sort_output {
        contents.sort();
    }

    let formats = match &output_formats {
        Some(f) => Cow::Borrowed(f.as_ref()),
        None => Cow::Owned(OutFormat::all().collect::<Vec<_>>()),
    };
    let versions = match &output_versions {
        Some(v) => Cow::Borrowed(v.as_ref()),
        None => Cow::Owned(all_version_names(&contents)),
    };

    generate_symbols(&contents, &formats, &versions, output_base)
}

/// Merges symbols from a collection of `input_files` of the format `input_format` into a given
/// `symgen_file`.
///
/// Additional configuration is specified with `merge_params`. Integers are written in `int_format`.
///
/// # Examples
/// ```ignore
/// let params = LoadParams {
///     default_block_name: None,
///     default_symbol_type: None,
///     default_version_name: Some("v1".into()),
/// };
/// merge_symbols(
///     "/path/to/symbols.yml",
///     ["/path/to/input.csv"],
///     InFormat::Csv,
///     &params,
///     IntFormat::Hexadecimal,
/// )
/// .expect("failed to merge symbols");
/// ```
pub fn merge_symbols<P, P2, I>(
    symgen_file: P,
    input_files: I,
    input_format: InFormat,
    merge_params: &LoadParams,
    int_format: IntFormat,
) -> Result<Vec<Vec<Symbol>>, Box<dyn Error>>
where
    P: AsRef<Path>,
    P2: AsRef<Path>,
    I: AsRef<[P2]>,
{
    let symgen_file = symgen_file.as_ref();
    let mut contents = {
        let file = File::open(symgen_file)?;
        SymGen::read(&file)?
    };
    contents.resolve_subregions(Subregion::subregion_dir(symgen_file), |p| File::open(p))?;

    let mut unmerged_symbols = Vec::with_capacity(input_files.as_ref().len());
    for input_name in input_files.as_ref() {
        let input = File::open(input_name)?;
        unmerged_symbols.push(input_format.merge(
            &mut contents,
            input,
            Some(input_name),
            merge_params,
        )?);
    }

    util::symgen_write_recursive(&contents, symgen_file, int_format)?;
    Ok(unmerged_symbols)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_output_file_name() {
        let cases = [
            (("out/", "NA", OutFormat::Ghidra), "out_NA.ghidra"),
            (("out/name", "EU", OutFormat::Sym), "out/name_EU.sym"),
            (
                ("/foo/bar/baz.yml", "JP", OutFormat::Sym),
                "/foo/bar/baz_JP.sym",
            ),
            (("", "NA", OutFormat::Sym), "NA.sym"),
            (("out/", "", OutFormat::Ghidra), "out.ghidra"),
            (("", "", OutFormat::Sym), "sym.sym"),
        ];
        for ((base, version, format), exp) in cases {
            assert_eq!(
                output_file_name(&Path::new(base), version, &format),
                Path::new(exp)
            );
        }
    }

    #[test]
    fn test_all_version_names() {
        let s = SymGen::read(
            r"
            main:
              versions:
                - v1
                - v3
              address: 0x2000000
              length: 0x1000
              functions: []
              data: []
            other:
              versions:
                - v2
              address: 0x2100000
              length: 0x1000
              functions: []
              data: []
            "
            .as_bytes(),
        )
        .expect("Read failed");

        assert_eq!(all_version_names(&s), vec!["v1", "v2", "v3"]);
    }

    #[test]
    fn test_all_version_names_inferred() {
        let s = SymGen::read(
            r"
            main:
              address:
                v0: 0x2000000
              length: 0x1000
              functions:
                - name: fn1
                  address:
                    v1: 0x2000000
                    v3: 0x2000000
              data: []
            other:
              address: 0x2100000
              length: 0x1000
              functions: []
              data:
                - name: data1
                  address:
                    v2: 0x2100000
            "
            .as_bytes(),
        )
        .expect("Read failed");

        assert_eq!(all_version_names(&s), vec!["v0", "v1", "v2", "v3"]);
    }

    #[test]
    fn test_all_version_names_common() {
        let s = SymGen::read(
            r"
            main:
              address: 0x2000000
              length: 0x1000
              functions:
                - name: fn1
                  address: 0x2000000
              data: []
            other:
              address: 0x2100000
              length: 0x1000
              functions: []
              data:
                - name: data1
                  address: 0x2100000
            "
            .as_bytes(),
        )
        .expect("Read failed");

        assert_eq!(all_version_names(&s), vec![""]);
    }

    #[test]
    fn test_all_version_names_empty() {
        let s = SymGen::read(
            r"
            main:
              address: 0x2000000
              length: 0x1000
              functions: []
              data: []
            "
            .as_bytes(),
        )
        .expect("Read failed");

        assert_eq!(all_version_names(&s), Vec::<&str>::new());
    }
}