summaryrefslogtreecommitdiffstats
path: root/third_party/rust/object/tests/tls.rs
blob: 653b7e714f8d93e8bddc06da3acf846fe0ede7fc (plain)
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
#![cfg(all(feature = "read", feature = "write"))]

use object::read::{Object, ObjectSection};
use object::{read, write};
use object::{RelocationEncoding, RelocationKind, SectionKind, SymbolKind, SymbolScope};
use target_lexicon::{Architecture, BinaryFormat};

#[test]
fn macho_x86_64_tls() {
    let mut object = write::Object::new(BinaryFormat::Macho, Architecture::X86_64);

    let section = object.section_id(write::StandardSection::Tls);
    let symbol = object.add_symbol(write::Symbol {
        name: b"tls1".to_vec(),
        value: 0,
        size: 0,
        kind: SymbolKind::Tls,
        scope: SymbolScope::Linkage,
        weak: false,
        section: None,
    });
    object.add_symbol_data(symbol, section, &[1; 30], 4);

    let section = object.section_id(write::StandardSection::UninitializedTls);
    let symbol = object.add_symbol(write::Symbol {
        name: b"tls2".to_vec(),
        value: 0,
        size: 0,
        kind: SymbolKind::Tls,
        scope: SymbolScope::Linkage,
        weak: false,
        section: None,
    });
    object.add_symbol_bss(symbol, section, 31, 4);

    let bytes = object.write().unwrap();

    std::fs::write(&"tls.o", &bytes).unwrap();

    let object = read::File::parse(&bytes).unwrap();
    assert_eq!(object.format(), BinaryFormat::Macho);
    assert_eq!(object.architecture(), Architecture::X86_64);

    let mut sections = object.sections();

    let thread_data = sections.next().unwrap();
    println!("{:?}", section);
    let thread_data_index = thread_data.index();
    assert_eq!(thread_data.name(), Some("__thread_data"));
    assert_eq!(thread_data.segment_name(), Some("__DATA"));
    assert_eq!(thread_data.kind(), SectionKind::Tls);
    assert_eq!(thread_data.size(), 30);
    assert_eq!(&thread_data.data()[..], &[1; 30]);

    let thread_vars = sections.next().unwrap();
    println!("{:?}", section);
    let thread_vars_index = thread_vars.index();
    assert_eq!(thread_vars.name(), Some("__thread_vars"));
    assert_eq!(thread_vars.segment_name(), Some("__DATA"));
    assert_eq!(thread_vars.kind(), SectionKind::TlsVariables);
    assert_eq!(thread_vars.size(), 2 * 3 * 8);

    let thread_bss = sections.next().unwrap();
    println!("{:?}", section);
    let thread_bss_index = thread_bss.index();
    assert_eq!(thread_bss.name(), Some("__thread_bss"));
    assert_eq!(thread_bss.segment_name(), Some("__DATA"));
    assert_eq!(thread_bss.kind(), SectionKind::UninitializedTls);
    assert_eq!(thread_bss.size(), 31);

    let mut symbols = object.symbols();

    let (_, symbol) = symbols.next().unwrap();
    println!("{:?}", symbol);
    assert_eq!(symbol.name(), Some("_tls1"));
    assert_eq!(symbol.kind(), SymbolKind::Tls);
    assert_eq!(symbol.section_index(), Some(thread_vars_index));
    assert_eq!(symbol.scope(), SymbolScope::Linkage);
    assert_eq!(symbol.is_weak(), false);
    assert_eq!(symbol.is_undefined(), false);

    let (tls1_init_symbol, symbol) = symbols.next().unwrap();
    println!("{:?}", symbol);
    assert_eq!(symbol.name(), Some("_tls1$tlv$init"));
    assert_eq!(symbol.kind(), SymbolKind::Tls);
    assert_eq!(symbol.section_index(), Some(thread_data_index));
    assert_eq!(symbol.scope(), SymbolScope::Compilation);
    assert_eq!(symbol.is_weak(), false);
    assert_eq!(symbol.is_undefined(), false);

    let (tlv_bootstrap_symbol, symbol) = symbols.next().unwrap();
    println!("{:?}", symbol);
    assert_eq!(symbol.name(), Some("__tlv_bootstrap"));
    assert_eq!(symbol.kind(), SymbolKind::Unknown);
    assert_eq!(symbol.section_index(), None);
    assert_eq!(symbol.scope(), SymbolScope::Unknown);
    assert_eq!(symbol.is_weak(), false);
    assert_eq!(symbol.is_undefined(), true);

    let (_, symbol) = symbols.next().unwrap();
    println!("{:?}", symbol);
    assert_eq!(symbol.name(), Some("_tls2"));
    assert_eq!(symbol.kind(), SymbolKind::Tls);
    assert_eq!(symbol.section_index(), Some(thread_vars_index));
    assert_eq!(symbol.scope(), SymbolScope::Linkage);
    assert_eq!(symbol.is_weak(), false);
    assert_eq!(symbol.is_undefined(), false);

    let (tls2_init_symbol, symbol) = symbols.next().unwrap();
    println!("{:?}", symbol);
    assert_eq!(symbol.name(), Some("_tls2$tlv$init"));
    assert_eq!(symbol.kind(), SymbolKind::Tls);
    assert_eq!(symbol.section_index(), Some(thread_bss_index));
    assert_eq!(symbol.scope(), SymbolScope::Compilation);
    assert_eq!(symbol.is_weak(), false);
    assert_eq!(symbol.is_undefined(), false);

    let mut relocations = thread_vars.relocations();

    let (offset, relocation) = relocations.next().unwrap();
    println!("{:?}", relocation);
    assert_eq!(offset, 0);
    assert_eq!(relocation.kind(), RelocationKind::Absolute);
    assert_eq!(relocation.encoding(), RelocationEncoding::Generic);
    assert_eq!(relocation.size(), 64);
    assert_eq!(
        relocation.target(),
        read::RelocationTarget::Symbol(tlv_bootstrap_symbol)
    );
    assert_eq!(relocation.addend(), 0);

    let (offset, relocation) = relocations.next().unwrap();
    println!("{:?}", relocation);
    assert_eq!(offset, 16);
    assert_eq!(relocation.kind(), RelocationKind::Absolute);
    assert_eq!(relocation.encoding(), RelocationEncoding::Generic);
    assert_eq!(relocation.size(), 64);
    assert_eq!(
        relocation.target(),
        read::RelocationTarget::Symbol(tls1_init_symbol)
    );
    assert_eq!(relocation.addend(), 0);

    let (offset, relocation) = relocations.next().unwrap();
    println!("{:?}", relocation);
    assert_eq!(offset, 24);
    assert_eq!(relocation.kind(), RelocationKind::Absolute);
    assert_eq!(relocation.encoding(), RelocationEncoding::Generic);
    assert_eq!(relocation.size(), 64);
    assert_eq!(
        relocation.target(),
        read::RelocationTarget::Symbol(tlv_bootstrap_symbol)
    );
    assert_eq!(relocation.addend(), 0);

    let (offset, relocation) = relocations.next().unwrap();
    println!("{:?}", relocation);
    assert_eq!(offset, 40);
    assert_eq!(relocation.kind(), RelocationKind::Absolute);
    assert_eq!(relocation.encoding(), RelocationEncoding::Generic);
    assert_eq!(relocation.size(), 64);
    assert_eq!(
        relocation.target(),
        read::RelocationTarget::Symbol(tls2_init_symbol)
    );
    assert_eq!(relocation.addend(), 0);
}