| 1 |
#![cfg(all(feature = "read", feature = "write"))] |
| 2 |
|
| 3 |
use object::read::{Object, ObjectSection, ObjectSymbol}; |
| 4 |
use object::{read, write}; |
| 5 |
use object::{ |
| 6 |
Architecture, BinaryFormat, Endianness, RelocationEncoding, RelocationKind, SectionKind, |
| 7 |
SymbolFlags, SymbolKind, SymbolScope, |
| 8 |
}; |
| 9 |
|
| 10 |
#[test] |
| 11 |
fn coff_x86_64_tls() { |
| 12 |
let mut object = |
| 13 |
write::Object::new(BinaryFormat::Coff, Architecture::X86_64, Endianness::Little); |
| 14 |
|
| 15 |
let section = object.section_id(write::StandardSection::Tls); |
| 16 |
let symbol = object.add_symbol(write::Symbol { |
| 17 |
name: b"tls1".to_vec(), |
| 18 |
value: 0, |
| 19 |
size: 0, |
| 20 |
kind: SymbolKind::Tls, |
| 21 |
scope: SymbolScope::Linkage, |
| 22 |
weak: false, |
| 23 |
section: write::SymbolSection::Undefined, |
| 24 |
flags: SymbolFlags::None, |
| 25 |
}); |
| 26 |
object.add_symbol_data(symbol, section, &[1; 30], 4); |
| 27 |
|
| 28 |
let bytes = object.write().unwrap(); |
| 29 |
|
| 30 |
//std::fs::write(&"tls.o", &bytes).unwrap(); |
| 31 |
|
| 32 |
let object = read::File::parse(&*bytes).unwrap(); |
| 33 |
assert_eq!(object.format(), BinaryFormat::Coff); |
| 34 |
assert_eq!(object.architecture(), Architecture::X86_64); |
| 35 |
|
| 36 |
let mut sections = object.sections(); |
| 37 |
|
| 38 |
let section = sections.next().unwrap(); |
| 39 |
println!("{:?}", section); |
| 40 |
let tls_index = section.index(); |
| 41 |
assert_eq!(section.name(), Ok(".tls$")); |
| 42 |
assert_eq!(section.kind(), SectionKind::Data); |
| 43 |
assert_eq!(section.size(), 30); |
| 44 |
assert_eq!(section.data().unwrap(), &[1; 30]); |
| 45 |
|
| 46 |
let mut symbols = object.symbols(); |
| 47 |
|
| 48 |
let symbol = symbols.next().unwrap(); |
| 49 |
println!("{:?}", symbol); |
| 50 |
assert_eq!(symbol.name(), Ok("tls1")); |
| 51 |
assert_eq!(symbol.kind(), SymbolKind::Data); |
| 52 |
assert_eq!(symbol.section_index(), Some(tls_index)); |
| 53 |
assert_eq!(symbol.scope(), SymbolScope::Linkage); |
| 54 |
assert!(!symbol.is_weak()); |
| 55 |
assert!(!symbol.is_undefined()); |
| 56 |
} |
| 57 |
|
| 58 |
#[test] |
| 59 |
fn elf_x86_64_tls() { |
| 60 |
let mut object = |
| 61 |
write::Object::new(BinaryFormat::Elf, Architecture::X86_64, Endianness::Little); |
| 62 |
|
| 63 |
let section = object.section_id(write::StandardSection::Tls); |
| 64 |
let symbol = object.add_symbol(write::Symbol { |
| 65 |
name: b"tls1".to_vec(), |
| 66 |
value: 0, |
| 67 |
size: 0, |
| 68 |
kind: SymbolKind::Tls, |
| 69 |
scope: SymbolScope::Linkage, |
| 70 |
weak: false, |
| 71 |
section: write::SymbolSection::Undefined, |
| 72 |
flags: SymbolFlags::None, |
| 73 |
}); |
| 74 |
object.add_symbol_data(symbol, section, &[1; 30], 4); |
| 75 |
|
| 76 |
let section = object.section_id(write::StandardSection::UninitializedTls); |
| 77 |
let symbol = object.add_symbol(write::Symbol { |
| 78 |
name: b"tls2".to_vec(), |
| 79 |
value: 0, |
| 80 |
size: 0, |
| 81 |
kind: SymbolKind::Tls, |
| 82 |
scope: SymbolScope::Linkage, |
| 83 |
weak: false, |
| 84 |
section: write::SymbolSection::Undefined, |
| 85 |
flags: SymbolFlags::None, |
| 86 |
}); |
| 87 |
object.add_symbol_bss(symbol, section, 31, 4); |
| 88 |
|
| 89 |
let bytes = object.write().unwrap(); |
| 90 |
|
| 91 |
//std::fs::write(&"tls.o", &bytes).unwrap(); |
| 92 |
|
| 93 |
let object = read::File::parse(&*bytes).unwrap(); |
| 94 |
assert_eq!(object.format(), BinaryFormat::Elf); |
| 95 |
assert_eq!(object.architecture(), Architecture::X86_64); |
| 96 |
|
| 97 |
let mut sections = object.sections(); |
| 98 |
|
| 99 |
let section = sections.next().unwrap(); |
| 100 |
println!("{:?}", section); |
| 101 |
let tdata_index = section.index(); |
| 102 |
assert_eq!(section.name(), Ok(".tdata")); |
| 103 |
assert_eq!(section.kind(), SectionKind::Tls); |
| 104 |
assert_eq!(section.size(), 30); |
| 105 |
assert_eq!(section.data().unwrap(), &[1; 30]); |
| 106 |
|
| 107 |
let section = sections.next().unwrap(); |
| 108 |
println!("{:?}", section); |
| 109 |
let tbss_index = section.index(); |
| 110 |
assert_eq!(section.name(), Ok(".tbss")); |
| 111 |
assert_eq!(section.kind(), SectionKind::UninitializedTls); |
| 112 |
assert_eq!(section.size(), 31); |
| 113 |
assert_eq!(section.data().unwrap(), &[]); |
| 114 |
|
| 115 |
let mut symbols = object.symbols(); |
| 116 |
|
| 117 |
let symbol = symbols.next().unwrap(); |
| 118 |
println!("{:?}", symbol); |
| 119 |
assert_eq!(symbol.name(), Ok("tls1")); |
| 120 |
assert_eq!(symbol.kind(), SymbolKind::Tls); |
| 121 |
assert_eq!(symbol.section_index(), Some(tdata_index)); |
| 122 |
assert_eq!(symbol.scope(), SymbolScope::Linkage); |
| 123 |
assert!(!symbol.is_weak()); |
| 124 |
assert!(!symbol.is_undefined()); |
| 125 |
assert_eq!(symbol.size(), 30); |
| 126 |
|
| 127 |
let symbol = symbols.next().unwrap(); |
| 128 |
println!("{:?}", symbol); |
| 129 |
assert_eq!(symbol.name(), Ok("tls2")); |
| 130 |
assert_eq!(symbol.kind(), SymbolKind::Tls); |
| 131 |
assert_eq!(symbol.section_index(), Some(tbss_index)); |
| 132 |
assert_eq!(symbol.scope(), SymbolScope::Linkage); |
| 133 |
assert!(!symbol.is_weak()); |
| 134 |
assert!(!symbol.is_undefined()); |
| 135 |
assert_eq!(symbol.size(), 31); |
| 136 |
} |
| 137 |
|
| 138 |
#[test] |
| 139 |
fn macho_x86_64_tls() { |
| 140 |
let mut object = write::Object::new( |
| 141 |
BinaryFormat::MachO, |
| 142 |
Architecture::X86_64, |
| 143 |
Endianness::Little, |
| 144 |
); |
| 145 |
|
| 146 |
let section = object.section_id(write::StandardSection::Tls); |
| 147 |
let symbol = object.add_symbol(write::Symbol { |
| 148 |
name: b"tls1".to_vec(), |
| 149 |
value: 0, |
| 150 |
size: 0, |
| 151 |
kind: SymbolKind::Tls, |
| 152 |
scope: SymbolScope::Linkage, |
| 153 |
weak: false, |
| 154 |
section: write::SymbolSection::Undefined, |
| 155 |
flags: SymbolFlags::None, |
| 156 |
}); |
| 157 |
object.add_symbol_data(symbol, section, &[1; 30], 4); |
| 158 |
|
| 159 |
let section = object.section_id(write::StandardSection::UninitializedTls); |
| 160 |
let symbol = object.add_symbol(write::Symbol { |
| 161 |
name: b"tls2".to_vec(), |
| 162 |
value: 0, |
| 163 |
size: 0, |
| 164 |
kind: SymbolKind::Tls, |
| 165 |
scope: SymbolScope::Linkage, |
| 166 |
weak: false, |
| 167 |
section: write::SymbolSection::Undefined, |
| 168 |
flags: SymbolFlags::None, |
| 169 |
}); |
| 170 |
object.add_symbol_bss(symbol, section, 31, 4); |
| 171 |
|
| 172 |
let bytes = object.write().unwrap(); |
| 173 |
|
| 174 |
//std::fs::write(&"tls.o", &bytes).unwrap(); |
| 175 |
|
| 176 |
let object = read::File::parse(&*bytes).unwrap(); |
| 177 |
assert_eq!(object.format(), BinaryFormat::MachO); |
| 178 |
assert_eq!(object.architecture(), Architecture::X86_64); |
| 179 |
|
| 180 |
let mut sections = object.sections(); |
| 181 |
|
| 182 |
let thread_data = sections.next().unwrap(); |
| 183 |
println!("{:?}", thread_data); |
| 184 |
let thread_data_index = thread_data.index(); |
| 185 |
assert_eq!(thread_data.name(), Ok("__thread_data")); |
| 186 |
assert_eq!(thread_data.segment_name(), Ok(Some("__DATA"))); |
| 187 |
assert_eq!(thread_data.kind(), SectionKind::Tls); |
| 188 |
assert_eq!(thread_data.size(), 30); |
| 189 |
assert_eq!(thread_data.data().unwrap(), &[1; 30]); |
| 190 |
|
| 191 |
let thread_vars = sections.next().unwrap(); |
| 192 |
println!("{:?}", thread_vars); |
| 193 |
let thread_vars_index = thread_vars.index(); |
| 194 |
assert_eq!(thread_vars.name(), Ok("__thread_vars")); |
| 195 |
assert_eq!(thread_vars.segment_name(), Ok(Some("__DATA"))); |
| 196 |
assert_eq!(thread_vars.kind(), SectionKind::TlsVariables); |
| 197 |
assert_eq!(thread_vars.size(), 2 * 3 * 8); |
| 198 |
assert_eq!(thread_vars.data().unwrap(), &[0; 48]); |
| 199 |
|
| 200 |
let thread_bss = sections.next().unwrap(); |
| 201 |
println!("{:?}", thread_bss); |
| 202 |
let thread_bss_index = thread_bss.index(); |
| 203 |
assert_eq!(thread_bss.name(), Ok("__thread_bss")); |
| 204 |
assert_eq!(thread_bss.segment_name(), Ok(Some("__DATA"))); |
| 205 |
assert_eq!(thread_bss.kind(), SectionKind::UninitializedTls); |
| 206 |
assert_eq!(thread_bss.size(), 31); |
| 207 |
assert_eq!(thread_bss.data(), Ok(&[][..])); |
| 208 |
|
| 209 |
let mut symbols = object.symbols(); |
| 210 |
|
| 211 |
let symbol = symbols.next().unwrap(); |
| 212 |
println!("{:?}", symbol); |
| 213 |
let tls1_init_symbol = symbol.index(); |
| 214 |
assert_eq!(symbol.name(), Ok("_tls1$tlv$init")); |
| 215 |
assert_eq!(symbol.kind(), SymbolKind::Tls); |
| 216 |
assert_eq!(symbol.section_index(), Some(thread_data_index)); |
| 217 |
assert_eq!(symbol.scope(), SymbolScope::Compilation); |
| 218 |
assert!(!symbol.is_weak()); |
| 219 |
assert!(!symbol.is_undefined()); |
| 220 |
|
| 221 |
let symbol = symbols.next().unwrap(); |
| 222 |
println!("{:?}", symbol); |
| 223 |
let tls2_init_symbol = symbol.index(); |
| 224 |
assert_eq!(symbol.name(), Ok("_tls2$tlv$init")); |
| 225 |
assert_eq!(symbol.kind(), SymbolKind::Tls); |
| 226 |
assert_eq!(symbol.section_index(), Some(thread_bss_index)); |
| 227 |
assert_eq!(symbol.scope(), SymbolScope::Compilation); |
| 228 |
assert!(!symbol.is_weak()); |
| 229 |
assert!(!symbol.is_undefined()); |
| 230 |
|
| 231 |
let symbol = symbols.next().unwrap(); |
| 232 |
println!("{:?}", symbol); |
| 233 |
assert_eq!(symbol.name(), Ok("_tls1")); |
| 234 |
assert_eq!(symbol.kind(), SymbolKind::Tls); |
| 235 |
assert_eq!(symbol.section_index(), Some(thread_vars_index)); |
| 236 |
assert_eq!(symbol.scope(), SymbolScope::Linkage); |
| 237 |
assert!(!symbol.is_weak()); |
| 238 |
assert!(!symbol.is_undefined()); |
| 239 |
|
| 240 |
let symbol = symbols.next().unwrap(); |
| 241 |
println!("{:?}", symbol); |
| 242 |
assert_eq!(symbol.name(), Ok("_tls2")); |
| 243 |
assert_eq!(symbol.kind(), SymbolKind::Tls); |
| 244 |
assert_eq!(symbol.section_index(), Some(thread_vars_index)); |
| 245 |
assert_eq!(symbol.scope(), SymbolScope::Linkage); |
| 246 |
assert!(!symbol.is_weak()); |
| 247 |
assert!(!symbol.is_undefined()); |
| 248 |
|
| 249 |
let symbol = symbols.next().unwrap(); |
| 250 |
println!("{:?}", symbol); |
| 251 |
let tlv_bootstrap_symbol = symbol.index(); |
| 252 |
assert_eq!(symbol.name(), Ok("__tlv_bootstrap")); |
| 253 |
assert_eq!(symbol.kind(), SymbolKind::Unknown); |
| 254 |
assert_eq!(symbol.section_index(), None); |
| 255 |
assert_eq!(symbol.scope(), SymbolScope::Unknown); |
| 256 |
assert!(!symbol.is_weak()); |
| 257 |
assert!(symbol.is_undefined()); |
| 258 |
|
| 259 |
let mut relocations = thread_vars.relocations(); |
| 260 |
|
| 261 |
let (offset, relocation) = relocations.next().unwrap(); |
| 262 |
println!("{:?}", relocation); |
| 263 |
assert_eq!(offset, 40); |
| 264 |
assert_eq!(relocation.kind(), RelocationKind::Absolute); |
| 265 |
assert_eq!(relocation.encoding(), RelocationEncoding::Generic); |
| 266 |
assert_eq!(relocation.size(), 64); |
| 267 |
assert_eq!( |
| 268 |
relocation.target(), |
| 269 |
read::RelocationTarget::Symbol(tls2_init_symbol) |
| 270 |
); |
| 271 |
assert_eq!(relocation.addend(), 0); |
| 272 |
|
| 273 |
let (offset, relocation) = relocations.next().unwrap(); |
| 274 |
println!("{:?}", relocation); |
| 275 |
assert_eq!(offset, 24); |
| 276 |
assert_eq!(relocation.kind(), RelocationKind::Absolute); |
| 277 |
assert_eq!(relocation.encoding(), RelocationEncoding::Generic); |
| 278 |
assert_eq!(relocation.size(), 64); |
| 279 |
assert_eq!( |
| 280 |
relocation.target(), |
| 281 |
read::RelocationTarget::Symbol(tlv_bootstrap_symbol) |
| 282 |
); |
| 283 |
assert_eq!(relocation.addend(), 0); |
| 284 |
|
| 285 |
let (offset, relocation) = relocations.next().unwrap(); |
| 286 |
println!("{:?}", relocation); |
| 287 |
assert_eq!(offset, 16); |
| 288 |
assert_eq!(relocation.kind(), RelocationKind::Absolute); |
| 289 |
assert_eq!(relocation.encoding(), RelocationEncoding::Generic); |
| 290 |
assert_eq!(relocation.size(), 64); |
| 291 |
assert_eq!( |
| 292 |
relocation.target(), |
| 293 |
read::RelocationTarget::Symbol(tls1_init_symbol) |
| 294 |
); |
| 295 |
assert_eq!(relocation.addend(), 0); |
| 296 |
|
| 297 |
let (offset, relocation) = relocations.next().unwrap(); |
| 298 |
println!("{:?}", relocation); |
| 299 |
assert_eq!(offset, 0); |
| 300 |
assert_eq!(relocation.kind(), RelocationKind::Absolute); |
| 301 |
assert_eq!(relocation.encoding(), RelocationEncoding::Generic); |
| 302 |
assert_eq!(relocation.size(), 64); |
| 303 |
assert_eq!( |
| 304 |
relocation.target(), |
| 305 |
read::RelocationTarget::Symbol(tlv_bootstrap_symbol) |
| 306 |
); |
| 307 |
assert_eq!(relocation.addend(), 0); |
| 308 |
} |