File: rmake.rs

package info (click to toggle)
rustc 1.90.0%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid
  • size: 925,928 kB
  • sloc: xml: 158,148; javascript: 19,781; sh: 19,174; python: 15,732; ansic: 13,096; cpp: 7,181; asm: 4,376; makefile: 697; lisp: 176; sql: 15
file content (92 lines) | stat: -rw-r--r-- 3,667 bytes parent folder | download | duplicates (2)
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
//! `run_make_support::symbols` helpers self test.

// Only intended as a basic smoke test, does not try to account for platform or calling convention
// specific symbol decorations.
//@ only-x86_64-unknown-linux-gnu
//@ ignore-cross-compile

use std::collections::BTreeSet;

use object::{Object, ObjectSymbol};
use run_make_support::symbols::{
    ContainsAllSymbolSubstringsOutcome, ContainsAllSymbolsOutcome,
    object_contains_all_symbol_substring, object_contains_all_symbols, object_contains_any_symbol,
    object_contains_any_symbol_substring,
};
use run_make_support::{object, rfs, rust_lib_name, rustc};

fn main() {
    rustc().input("sample.rs").emit("obj").edition("2024").run();

    // `sample.rs` has two `no_mangle` functions, `eszett` and `beta`, in addition to `main`.
    //
    // These two symbol names and the test substrings used below are carefully picked to make sure
    // they do not overlap with `sample` and contain non-hex characters, to avoid accidentally
    // matching against CGU names like `sample.dad0f15d00c84e70-cgu.0`.

    let obj_filename = "sample.o";
    let blob = rfs::read(obj_filename);
    let obj = object::File::parse(&*blob).unwrap();
    eprintln!("found symbols:");
    for sym in obj.symbols() {
        eprintln!("symbol = {}", sym.name().unwrap());
    }

    // `hello` contains `hel`
    assert!(object_contains_any_symbol_substring(obj_filename, &["zett"]));
    assert!(object_contains_any_symbol_substring(obj_filename, &["zett", "does_not_exist"]));
    assert!(!object_contains_any_symbol_substring(obj_filename, &["does_not_exist"]));

    assert!(object_contains_any_symbol(obj_filename, &["eszett"]));
    assert!(object_contains_any_symbol(obj_filename, &["eszett", "beta"]));
    assert!(!object_contains_any_symbol(obj_filename, &["zett"]));
    assert!(!object_contains_any_symbol(obj_filename, &["does_not_exist"]));

    assert_eq!(
        object_contains_all_symbol_substring(obj_filename, &["zett"]),
        ContainsAllSymbolSubstringsOutcome::Ok
    );
    assert_eq!(
        object_contains_all_symbol_substring(obj_filename, &["zett", "bet"]),
        ContainsAllSymbolSubstringsOutcome::Ok
    );
    assert_eq!(
        object_contains_all_symbol_substring(obj_filename, &["does_not_exist"]),
        ContainsAllSymbolSubstringsOutcome::MissingSymbolSubstrings(BTreeSet::from([
            "does_not_exist"
        ]))
    );
    assert_eq!(
        object_contains_all_symbol_substring(obj_filename, &["zett", "does_not_exist"]),
        ContainsAllSymbolSubstringsOutcome::MissingSymbolSubstrings(BTreeSet::from([
            "does_not_exist"
        ]))
    );
    assert_eq!(
        object_contains_all_symbol_substring(obj_filename, &["zett", "bet", "does_not_exist"]),
        ContainsAllSymbolSubstringsOutcome::MissingSymbolSubstrings(BTreeSet::from([
            "does_not_exist"
        ]))
    );

    assert_eq!(
        object_contains_all_symbols(obj_filename, &["eszett"]),
        ContainsAllSymbolsOutcome::Ok
    );
    assert_eq!(
        object_contains_all_symbols(obj_filename, &["eszett", "beta"]),
        ContainsAllSymbolsOutcome::Ok
    );
    assert_eq!(
        object_contains_all_symbols(obj_filename, &["zett"]),
        ContainsAllSymbolsOutcome::MissingSymbols(BTreeSet::from(["zett"]))
    );
    assert_eq!(
        object_contains_all_symbols(obj_filename, &["zett", "beta"]),
        ContainsAllSymbolsOutcome::MissingSymbols(BTreeSet::from(["zett"]))
    );
    assert_eq!(
        object_contains_all_symbols(obj_filename, &["does_not_exist"]),
        ContainsAllSymbolsOutcome::MissingSymbols(BTreeSet::from(["does_not_exist"]))
    );
}