| 1 |
#![cfg(all(feature = "read", feature = "write"))] |
| 2 |
|
| 3 |
use object::read::{Object, ObjectSection, ObjectSymbol}; |
| 4 |
use object::{read, write, SectionIndex, SubArchitecture}; |
| 5 |
use object::{ |
| 6 |
Architecture, BinaryFormat, Endianness, RelocationEncoding, RelocationFlags, RelocationKind, |
| 7 |
SectionKind, SymbolFlags, SymbolKind, SymbolScope, SymbolSection, |
| 8 |
}; |
| 9 |
|
| 10 |
mod bss; |
| 11 |
mod coff; |
| 12 |
mod comdat; |
| 13 |
mod common; |
| 14 |
mod elf; |
| 15 |
mod macho; |
| 16 |
mod section_flags; |
| 17 |
mod tls; |
| 18 |
|
| 19 |
#[test] |
| 20 |
fn coff_any() { |
| 21 |
for (arch, sub_arch) in [ |
| 22 |
(Architecture::Aarch64, None), |
| 23 |
(Architecture::Aarch64, Some(SubArchitecture::Arm64EC)), |
| 24 |
(Architecture::Arm, None), |
| 25 |
(Architecture::I386, None), |
| 26 |
(Architecture::X86_64, None), |
| 27 |
] |
| 28 |
.iter() |
| 29 |
.copied() |
| 30 |
{ |
| 31 |
let mut object = write::Object::new(BinaryFormat::Coff, arch, Endianness::Little); |
| 32 |
object.set_sub_architecture(sub_arch); |
| 33 |
|
| 34 |
object.add_file_symbol(b"file.c".to_vec()); |
| 35 |
|
| 36 |
let text = object.section_id(write::StandardSection::Text); |
| 37 |
object.append_section_data(text, &[1; 30], 4); |
| 38 |
|
| 39 |
let func1_offset = object.append_section_data(text, &[1; 30], 4); |
| 40 |
assert_eq!(func1_offset, 32); |
| 41 |
let func1_symbol = object.add_symbol(write::Symbol { |
| 42 |
name: b"func1".to_vec(), |
| 43 |
value: func1_offset, |
| 44 |
size: 32, |
| 45 |
kind: SymbolKind::Text, |
| 46 |
scope: SymbolScope::Linkage, |
| 47 |
weak: false, |
| 48 |
section: write::SymbolSection::Section(text), |
| 49 |
flags: SymbolFlags::None, |
| 50 |
}); |
| 51 |
let func2_offset = object.append_section_data(text, &[1; 30], 4); |
| 52 |
assert_eq!(func2_offset, 64); |
| 53 |
object.add_symbol(write::Symbol { |
| 54 |
name: b"func2_long".to_vec(), |
| 55 |
value: func2_offset, |
| 56 |
size: 32, |
| 57 |
kind: SymbolKind::Text, |
| 58 |
scope: SymbolScope::Linkage, |
| 59 |
weak: false, |
| 60 |
section: write::SymbolSection::Section(text), |
| 61 |
flags: SymbolFlags::None, |
| 62 |
}); |
| 63 |
object |
| 64 |
.add_relocation( |
| 65 |
text, |
| 66 |
write::Relocation { |
| 67 |
offset: 8, |
| 68 |
symbol: func1_symbol, |
| 69 |
addend: 0, |
| 70 |
flags: RelocationFlags::Generic { |
| 71 |
kind: RelocationKind::Absolute, |
| 72 |
encoding: RelocationEncoding::Generic, |
| 73 |
size: arch.address_size().unwrap().bytes() * 8, |
| 74 |
}, |
| 75 |
}, |
| 76 |
) |
| 77 |
.unwrap(); |
| 78 |
|
| 79 |
let bytes = object.write().unwrap(); |
| 80 |
let object = read::File::parse(&*bytes).unwrap(); |
| 81 |
assert_eq!(object.format(), BinaryFormat::Coff); |
| 82 |
assert_eq!(object.architecture(), arch); |
| 83 |
assert_eq!(object.sub_architecture(), sub_arch); |
| 84 |
assert_eq!(object.endianness(), Endianness::Little); |
| 85 |
|
| 86 |
let mut sections = object.sections(); |
| 87 |
|
| 88 |
let text = sections.next().unwrap(); |
| 89 |
println!("{:?}", text); |
| 90 |
let text_index = text.index(); |
| 91 |
assert_eq!(text.name(), Ok(".text")); |
| 92 |
assert_eq!(text.kind(), SectionKind::Text); |
| 93 |
assert_eq!(text.address(), 0); |
| 94 |
assert_eq!(text.size(), 94); |
| 95 |
assert_eq!(&text.data().unwrap()[..30], &[1; 30]); |
| 96 |
assert_eq!(&text.data().unwrap()[32..62], &[1; 30]); |
| 97 |
|
| 98 |
let mut symbols = object.symbols(); |
| 99 |
|
| 100 |
let symbol = symbols.next().unwrap(); |
| 101 |
println!("{:?}", symbol); |
| 102 |
assert_eq!(symbol.name(), Ok("file.c")); |
| 103 |
assert_eq!(symbol.address(), 0); |
| 104 |
assert_eq!(symbol.kind(), SymbolKind::File); |
| 105 |
assert_eq!(symbol.section(), SymbolSection::None); |
| 106 |
assert_eq!(symbol.scope(), SymbolScope::Compilation); |
| 107 |
assert!(!symbol.is_weak()); |
| 108 |
|
| 109 |
let decorated_name = |name: &str| { |
| 110 |
if arch == Architecture::I386 { |
| 111 |
format!("_{name}") |
| 112 |
} else { |
| 113 |
name.to_owned() |
| 114 |
} |
| 115 |
}; |
| 116 |
|
| 117 |
let symbol = symbols.next().unwrap(); |
| 118 |
println!("{:?}", symbol); |
| 119 |
let func1_symbol = symbol.index(); |
| 120 |
assert_eq!(symbol.name(), Ok(decorated_name("func1").as_str())); |
| 121 |
assert_eq!(symbol.address(), func1_offset); |
| 122 |
assert_eq!(symbol.kind(), SymbolKind::Text); |
| 123 |
assert_eq!(symbol.section_index(), Some(text_index)); |
| 124 |
assert_eq!(symbol.scope(), SymbolScope::Linkage); |
| 125 |
assert!(!symbol.is_weak()); |
| 126 |
assert!(!symbol.is_undefined()); |
| 127 |
|
| 128 |
let symbol = symbols.next().unwrap(); |
| 129 |
println!("{:?}", symbol); |
| 130 |
assert_eq!(symbol.name(), Ok(decorated_name("func2_long").as_str())); |
| 131 |
assert_eq!(symbol.address(), func2_offset); |
| 132 |
assert_eq!(symbol.kind(), SymbolKind::Text); |
| 133 |
assert_eq!(symbol.section_index(), Some(text_index)); |
| 134 |
assert_eq!(symbol.scope(), SymbolScope::Linkage); |
| 135 |
assert!(!symbol.is_weak()); |
| 136 |
assert!(!symbol.is_undefined()); |
| 137 |
|
| 138 |
let mut relocations = text.relocations(); |
| 139 |
|
| 140 |
let (offset, relocation) = relocations.next().unwrap(); |
| 141 |
println!("{:?}", relocation); |
| 142 |
assert_eq!(offset, 8); |
| 143 |
assert_eq!(relocation.kind(), RelocationKind::Absolute); |
| 144 |
assert_eq!(relocation.encoding(), RelocationEncoding::Generic); |
| 145 |
assert_eq!(relocation.size(), arch.address_size().unwrap().bytes() * 8); |
| 146 |
assert_eq!( |
| 147 |
relocation.target(), |
| 148 |
read::RelocationTarget::Symbol(func1_symbol) |
| 149 |
); |
| 150 |
assert_eq!(relocation.addend(), 0); |
| 151 |
|
| 152 |
let map = object.symbol_map(); |
| 153 |
let symbol = map.get(func1_offset + 1).unwrap(); |
| 154 |
assert_eq!(symbol.address(), func1_offset); |
| 155 |
assert_eq!(symbol.name(), decorated_name("func1")); |
| 156 |
assert_eq!(map.get(func1_offset - 1), None); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
#[test] |
| 161 |
fn elf_x86_64() { |
| 162 |
let mut object = |
| 163 |
write::Object::new(BinaryFormat::Elf, Architecture::X86_64, Endianness::Little); |
| 164 |
|
| 165 |
object.add_file_symbol(b"file.c".to_vec()); |
| 166 |
|
| 167 |
let text = object.section_id(write::StandardSection::Text); |
| 168 |
object.append_section_data(text, &[1; 30], 4); |
| 169 |
|
| 170 |
let func1_offset = object.append_section_data(text, &[1; 30], 4); |
| 171 |
assert_eq!(func1_offset, 32); |
| 172 |
let func1_symbol = object.add_symbol(write::Symbol { |
| 173 |
name: b"func1".to_vec(), |
| 174 |
value: func1_offset, |
| 175 |
size: 32, |
| 176 |
kind: SymbolKind::Text, |
| 177 |
scope: SymbolScope::Linkage, |
| 178 |
weak: false, |
| 179 |
section: write::SymbolSection::Section(text), |
| 180 |
flags: SymbolFlags::None, |
| 181 |
}); |
| 182 |
object |
| 183 |
.add_relocation( |
| 184 |
text, |
| 185 |
write::Relocation { |
| 186 |
offset: 8, |
| 187 |
symbol: func1_symbol, |
| 188 |
addend: 0, |
| 189 |
flags: RelocationFlags::Generic { |
| 190 |
kind: RelocationKind::Absolute, |
| 191 |
encoding: RelocationEncoding::Generic, |
| 192 |
size: 64, |
| 193 |
}, |
| 194 |
}, |
| 195 |
) |
| 196 |
.unwrap(); |
| 197 |
|
| 198 |
let bytes = object.write().unwrap(); |
| 199 |
let object = read::File::parse(&*bytes).unwrap(); |
| 200 |
assert_eq!(object.format(), BinaryFormat::Elf); |
| 201 |
assert_eq!(object.architecture(), Architecture::X86_64); |
| 202 |
assert_eq!(object.endianness(), Endianness::Little); |
| 203 |
|
| 204 |
let mut sections = object.sections(); |
| 205 |
|
| 206 |
let text = sections.next().unwrap(); |
| 207 |
println!("{:?}", text); |
| 208 |
let text_index = text.index(); |
| 209 |
assert_eq!(text.name(), Ok(".text")); |
| 210 |
assert_eq!(text.kind(), SectionKind::Text); |
| 211 |
assert_eq!(text.address(), 0); |
| 212 |
assert_eq!(text.size(), 62); |
| 213 |
assert_eq!(&text.data().unwrap()[..30], &[1; 30]); |
| 214 |
assert_eq!(&text.data().unwrap()[32..62], &[1; 30]); |
| 215 |
|
| 216 |
let mut symbols = object.symbols(); |
| 217 |
|
| 218 |
let symbol = symbols.next().unwrap(); |
| 219 |
println!("{:?}", symbol); |
| 220 |
assert_eq!(symbol.name(), Ok("file.c")); |
| 221 |
assert_eq!(symbol.address(), 0); |
| 222 |
assert_eq!(symbol.kind(), SymbolKind::File); |
| 223 |
assert_eq!(symbol.section(), SymbolSection::None); |
| 224 |
assert_eq!(symbol.scope(), SymbolScope::Compilation); |
| 225 |
assert!(!symbol.is_weak()); |
| 226 |
|
| 227 |
let symbol = symbols.next().unwrap(); |
| 228 |
println!("{:?}", symbol); |
| 229 |
let func1_symbol = symbol.index(); |
| 230 |
assert_eq!(symbol.name(), Ok("func1")); |
| 231 |
assert_eq!(symbol.address(), func1_offset); |
| 232 |
assert_eq!(symbol.kind(), SymbolKind::Text); |
| 233 |
assert_eq!(symbol.section_index(), Some(text_index)); |
| 234 |
assert_eq!(symbol.scope(), SymbolScope::Linkage); |
| 235 |
assert!(!symbol.is_weak()); |
| 236 |
assert!(!symbol.is_undefined()); |
| 237 |
|
| 238 |
let mut relocations = text.relocations(); |
| 239 |
|
| 240 |
let (offset, relocation) = relocations.next().unwrap(); |
| 241 |
println!("{:?}", relocation); |
| 242 |
assert_eq!(offset, 8); |
| 243 |
assert_eq!(relocation.kind(), RelocationKind::Absolute); |
| 244 |
assert_eq!(relocation.encoding(), RelocationEncoding::Generic); |
| 245 |
assert_eq!(relocation.size(), 64); |
| 246 |
assert_eq!( |
| 247 |
relocation.target(), |
| 248 |
read::RelocationTarget::Symbol(func1_symbol) |
| 249 |
); |
| 250 |
assert_eq!(relocation.addend(), 0); |
| 251 |
|
| 252 |
let map = object.symbol_map(); |
| 253 |
let symbol = map.get(func1_offset + 1).unwrap(); |
| 254 |
assert_eq!(symbol.address(), func1_offset); |
| 255 |
assert_eq!(symbol.name(), "func1"); |
| 256 |
assert_eq!(map.get(func1_offset - 1), None); |
| 257 |
} |
| 258 |
|
| 259 |
#[test] |
| 260 |
fn elf_any() { |
| 261 |
for (arch, endian) in [ |
| 262 |
(Architecture::Aarch64, Endianness::Little), |
| 263 |
(Architecture::Aarch64_Ilp32, Endianness::Little), |
| 264 |
(Architecture::Arm, Endianness::Little), |
| 265 |
(Architecture::Avr, Endianness::Little), |
| 266 |
(Architecture::Bpf, Endianness::Little), |
| 267 |
(Architecture::Csky, Endianness::Little), |
| 268 |
(Architecture::I386, Endianness::Little), |
| 269 |
(Architecture::X86_64, Endianness::Little), |
| 270 |
(Architecture::X86_64_X32, Endianness::Little), |
| 271 |
(Architecture::Hexagon, Endianness::Little), |
| 272 |
(Architecture::LoongArch64, Endianness::Little), |
| 273 |
(Architecture::Mips, Endianness::Little), |
| 274 |
(Architecture::Mips64, Endianness::Little), |
| 275 |
(Architecture::Msp430, Endianness::Little), |
| 276 |
(Architecture::PowerPc, Endianness::Big), |
| 277 |
(Architecture::PowerPc64, Endianness::Big), |
| 278 |
(Architecture::Riscv32, Endianness::Little), |
| 279 |
(Architecture::Riscv64, Endianness::Little), |
| 280 |
(Architecture::S390x, Endianness::Big), |
| 281 |
(Architecture::Sbf, Endianness::Little), |
| 282 |
(Architecture::Sparc, Endianness::Big), |
| 283 |
(Architecture::Sparc32Plus, Endianness::Big), |
| 284 |
(Architecture::Sparc64, Endianness::Big), |
| 285 |
(Architecture::Xtensa, Endianness::Little), |
| 286 |
] |
| 287 |
.iter() |
| 288 |
.copied() |
| 289 |
{ |
| 290 |
let mut object = write::Object::new(BinaryFormat::Elf, arch, endian); |
| 291 |
|
| 292 |
let section = object.section_id(write::StandardSection::Data); |
| 293 |
object.append_section_data(section, &[1; 30], 4); |
| 294 |
let symbol = object.section_symbol(section); |
| 295 |
|
| 296 |
object |
| 297 |
.add_relocation( |
| 298 |
section, |
| 299 |
write::Relocation { |
| 300 |
offset: 8, |
| 301 |
symbol, |
| 302 |
addend: 0, |
| 303 |
flags: RelocationFlags::Generic { |
| 304 |
kind: RelocationKind::Absolute, |
| 305 |
encoding: RelocationEncoding::Generic, |
| 306 |
size: 32, |
| 307 |
}, |
| 308 |
}, |
| 309 |
) |
| 310 |
.unwrap(); |
| 311 |
if arch.address_size().unwrap().bytes() >= 8 { |
| 312 |
object |
| 313 |
.add_relocation( |
| 314 |
section, |
| 315 |
write::Relocation { |
| 316 |
offset: 16, |
| 317 |
symbol, |
| 318 |
addend: 0, |
| 319 |
flags: RelocationFlags::Generic { |
| 320 |
kind: RelocationKind::Absolute, |
| 321 |
encoding: RelocationEncoding::Generic, |
| 322 |
size: 64, |
| 323 |
}, |
| 324 |
}, |
| 325 |
) |
| 326 |
.unwrap(); |
| 327 |
} |
| 328 |
|
| 329 |
let bytes = object.write().unwrap(); |
| 330 |
let object = read::File::parse(&*bytes).unwrap(); |
| 331 |
println!("{:?}", object.architecture()); |
| 332 |
assert_eq!(object.format(), BinaryFormat::Elf); |
| 333 |
assert_eq!(object.architecture(), arch); |
| 334 |
assert_eq!(object.endianness(), endian); |
| 335 |
|
| 336 |
let mut sections = object.sections(); |
| 337 |
|
| 338 |
let data = sections.next().unwrap(); |
| 339 |
println!("{:?}", data); |
| 340 |
assert_eq!(data.name(), Ok(".data")); |
| 341 |
assert_eq!(data.kind(), SectionKind::Data); |
| 342 |
|
| 343 |
let mut relocations = data.relocations(); |
| 344 |
|
| 345 |
let (offset, relocation) = relocations.next().unwrap(); |
| 346 |
println!("{:?}", relocation); |
| 347 |
assert_eq!(offset, 8); |
| 348 |
assert_eq!(relocation.kind(), RelocationKind::Absolute); |
| 349 |
assert_eq!(relocation.encoding(), RelocationEncoding::Generic); |
| 350 |
assert_eq!(relocation.size(), 32); |
| 351 |
assert_eq!(relocation.addend(), 0); |
| 352 |
|
| 353 |
if arch.address_size().unwrap().bytes() >= 8 { |
| 354 |
let (offset, relocation) = relocations.next().unwrap(); |
| 355 |
println!("{:?}", relocation); |
| 356 |
assert_eq!(offset, 16); |
| 357 |
assert_eq!(relocation.kind(), RelocationKind::Absolute); |
| 358 |
assert_eq!(relocation.encoding(), RelocationEncoding::Generic); |
| 359 |
assert_eq!(relocation.size(), 64); |
| 360 |
assert_eq!(relocation.addend(), 0); |
| 361 |
} |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
#[test] |
| 366 |
fn macho_x86_64() { |
| 367 |
let mut object = write::Object::new( |
| 368 |
BinaryFormat::MachO, |
| 369 |
Architecture::X86_64, |
| 370 |
Endianness::Little, |
| 371 |
); |
| 372 |
|
| 373 |
object.add_file_symbol(b"file.c".to_vec()); |
| 374 |
|
| 375 |
let text = object.section_id(write::StandardSection::Text); |
| 376 |
object.append_section_data(text, &[1; 30], 4); |
| 377 |
|
| 378 |
let func1_offset = object.append_section_data(text, &[1; 30], 4); |
| 379 |
assert_eq!(func1_offset, 32); |
| 380 |
let func1_symbol = object.add_symbol(write::Symbol { |
| 381 |
name: b"func1".to_vec(), |
| 382 |
value: func1_offset, |
| 383 |
size: 32, |
| 384 |
kind: SymbolKind::Text, |
| 385 |
scope: SymbolScope::Linkage, |
| 386 |
weak: false, |
| 387 |
section: write::SymbolSection::Section(text), |
| 388 |
flags: SymbolFlags::None, |
| 389 |
}); |
| 390 |
object |
| 391 |
.add_relocation( |
| 392 |
text, |
| 393 |
write::Relocation { |
| 394 |
offset: 8, |
| 395 |
symbol: func1_symbol, |
| 396 |
addend: 0, |
| 397 |
flags: RelocationFlags::Generic { |
| 398 |
kind: RelocationKind::Absolute, |
| 399 |
encoding: RelocationEncoding::Generic, |
| 400 |
size: 64, |
| 401 |
}, |
| 402 |
}, |
| 403 |
) |
| 404 |
.unwrap(); |
| 405 |
object |
| 406 |
.add_relocation( |
| 407 |
text, |
| 408 |
write::Relocation { |
| 409 |
offset: 16, |
| 410 |
symbol: func1_symbol, |
| 411 |
addend: -4, |
| 412 |
flags: RelocationFlags::Generic { |
| 413 |
kind: RelocationKind::Relative, |
| 414 |
encoding: RelocationEncoding::Generic, |
| 415 |
size: 32, |
| 416 |
}, |
| 417 |
}, |
| 418 |
) |
| 419 |
.unwrap(); |
| 420 |
|
| 421 |
let bytes = object.write().unwrap(); |
| 422 |
let object = read::File::parse(&*bytes).unwrap(); |
| 423 |
assert_eq!(object.format(), BinaryFormat::MachO); |
| 424 |
assert_eq!(object.architecture(), Architecture::X86_64); |
| 425 |
assert_eq!(object.endianness(), Endianness::Little); |
| 426 |
|
| 427 |
let mut sections = object.sections(); |
| 428 |
|
| 429 |
let text = sections.next().unwrap(); |
| 430 |
println!("{:?}", text); |
| 431 |
let text_index = text.index(); |
| 432 |
assert_eq!(text.name(), Ok("__text")); |
| 433 |
assert_eq!(text.segment_name(), Ok(Some("__TEXT"))); |
| 434 |
assert_eq!(text.kind(), SectionKind::Text); |
| 435 |
assert_eq!(text.address(), 0); |
| 436 |
assert_eq!(text.size(), 62); |
| 437 |
assert_eq!(&text.data().unwrap()[..30], &[1; 30]); |
| 438 |
assert_eq!(&text.data().unwrap()[32..62], &[1; 30]); |
| 439 |
|
| 440 |
let mut symbols = object.symbols(); |
| 441 |
|
| 442 |
let symbol = symbols.next().unwrap(); |
| 443 |
println!("{:?}", symbol); |
| 444 |
let func1_symbol = symbol.index(); |
| 445 |
assert_eq!(symbol.name(), Ok("_func1")); |
| 446 |
assert_eq!(symbol.address(), func1_offset); |
| 447 |
assert_eq!(symbol.kind(), SymbolKind::Text); |
| 448 |
assert_eq!(symbol.section_index(), Some(text_index)); |
| 449 |
assert_eq!(symbol.scope(), SymbolScope::Linkage); |
| 450 |
assert!(!symbol.is_weak()); |
| 451 |
assert!(!symbol.is_undefined()); |
| 452 |
|
| 453 |
let mut relocations = text.relocations(); |
| 454 |
|
| 455 |
let (offset, relocation) = relocations.next().unwrap(); |
| 456 |
println!("{:?}", relocation); |
| 457 |
assert_eq!(offset, 16); |
| 458 |
assert_eq!(relocation.kind(), RelocationKind::Relative); |
| 459 |
assert_eq!(relocation.encoding(), RelocationEncoding::X86RipRelative); |
| 460 |
assert_eq!(relocation.size(), 32); |
| 461 |
assert_eq!( |
| 462 |
relocation.target(), |
| 463 |
read::RelocationTarget::Symbol(func1_symbol) |
| 464 |
); |
| 465 |
assert_eq!(relocation.addend(), -4); |
| 466 |
|
| 467 |
let (offset, relocation) = relocations.next().unwrap(); |
| 468 |
println!("{:?}", relocation); |
| 469 |
assert_eq!(offset, 8); |
| 470 |
assert_eq!(relocation.kind(), RelocationKind::Absolute); |
| 471 |
assert_eq!(relocation.encoding(), RelocationEncoding::Generic); |
| 472 |
assert_eq!(relocation.size(), 64); |
| 473 |
assert_eq!( |
| 474 |
relocation.target(), |
| 475 |
read::RelocationTarget::Symbol(func1_symbol) |
| 476 |
); |
| 477 |
assert_eq!(relocation.addend(), 0); |
| 478 |
|
| 479 |
let map = object.symbol_map(); |
| 480 |
let symbol = map.get(func1_offset + 1).unwrap(); |
| 481 |
assert_eq!(symbol.address(), func1_offset); |
| 482 |
assert_eq!(symbol.name(), "_func1"); |
| 483 |
assert_eq!(map.get(func1_offset - 1), None); |
| 484 |
} |
| 485 |
|
| 486 |
#[test] |
| 487 |
fn macho_any() { |
| 488 |
for (arch, subarch, endian) in [ |
| 489 |
(Architecture::Aarch64, None, Endianness::Little), |
| 490 |
( |
| 491 |
Architecture::Aarch64, |
| 492 |
Some(SubArchitecture::Arm64E), |
| 493 |
Endianness::Little, |
| 494 |
), |
| 495 |
(Architecture::Aarch64_Ilp32, None, Endianness::Little), |
| 496 |
/* TODO: |
| 497 |
(Architecture::Arm, None, Endianness::Little), |
| 498 |
*/ |
| 499 |
(Architecture::I386, None, Endianness::Little), |
| 500 |
(Architecture::X86_64, None, Endianness::Little), |
| 501 |
/* TODO: |
| 502 |
(Architecture::PowerPc, None, Endianness::Big), |
| 503 |
(Architecture::PowerPc64, None, Endianness::Big), |
| 504 |
*/ |
| 505 |
] |
| 506 |
.iter() |
| 507 |
.copied() |
| 508 |
{ |
| 509 |
let mut object = write::Object::new(BinaryFormat::MachO, arch, endian); |
| 510 |
object.set_sub_architecture(subarch); |
| 511 |
|
| 512 |
let section = object.section_id(write::StandardSection::Data); |
| 513 |
object.append_section_data(section, &[1; 30], 4); |
| 514 |
let symbol = object.section_symbol(section); |
| 515 |
|
| 516 |
object |
| 517 |
.add_relocation( |
| 518 |
section, |
| 519 |
write::Relocation { |
| 520 |
offset: 8, |
| 521 |
symbol, |
| 522 |
addend: 0, |
| 523 |
flags: RelocationFlags::Generic { |
| 524 |
kind: RelocationKind::Absolute, |
| 525 |
encoding: RelocationEncoding::Generic, |
| 526 |
size: 32, |
| 527 |
}, |
| 528 |
}, |
| 529 |
) |
| 530 |
.unwrap(); |
| 531 |
if arch.address_size().unwrap().bytes() >= 8 { |
| 532 |
object |
| 533 |
.add_relocation( |
| 534 |
section, |
| 535 |
write::Relocation { |
| 536 |
offset: 16, |
| 537 |
symbol, |
| 538 |
addend: 0, |
| 539 |
flags: RelocationFlags::Generic { |
| 540 |
kind: RelocationKind::Absolute, |
| 541 |
encoding: RelocationEncoding::Generic, |
| 542 |
size: 64, |
| 543 |
}, |
| 544 |
}, |
| 545 |
) |
| 546 |
.unwrap(); |
| 547 |
} |
| 548 |
|
| 549 |
let bytes = object.write().unwrap(); |
| 550 |
let object = read::File::parse(&*bytes).unwrap(); |
| 551 |
println!("{:?}", object.architecture()); |
| 552 |
assert_eq!(object.format(), BinaryFormat::MachO); |
| 553 |
assert_eq!(object.architecture(), arch); |
| 554 |
assert_eq!(object.sub_architecture(), subarch); |
| 555 |
assert_eq!(object.endianness(), endian); |
| 556 |
|
| 557 |
let mut sections = object.sections(); |
| 558 |
|
| 559 |
let data = sections.next().unwrap(); |
| 560 |
println!("{:?}", data); |
| 561 |
assert_eq!(data.segment_name(), Ok(Some("__DATA"))); |
| 562 |
assert_eq!(data.name(), Ok("__data")); |
| 563 |
assert_eq!(data.kind(), SectionKind::Data); |
| 564 |
|
| 565 |
let mut relocations = data.relocations(); |
| 566 |
|
| 567 |
if arch.address_size().unwrap().bytes() >= 8 { |
| 568 |
let (offset, relocation) = relocations.next().unwrap(); |
| 569 |
println!("{:?}", relocation); |
| 570 |
assert_eq!(offset, 16); |
| 571 |
assert_eq!(relocation.kind(), RelocationKind::Absolute); |
| 572 |
assert_eq!(relocation.encoding(), RelocationEncoding::Generic); |
| 573 |
assert_eq!(relocation.size(), 64); |
| 574 |
assert_eq!(relocation.addend(), 0); |
| 575 |
} |
| 576 |
|
| 577 |
let (offset, relocation) = relocations.next().unwrap(); |
| 578 |
println!("{:?}", relocation); |
| 579 |
assert_eq!(offset, 8); |
| 580 |
assert_eq!(relocation.kind(), RelocationKind::Absolute); |
| 581 |
assert_eq!(relocation.encoding(), RelocationEncoding::Generic); |
| 582 |
assert_eq!(relocation.size(), 32); |
| 583 |
assert_eq!(relocation.addend(), 0); |
| 584 |
} |
| 585 |
} |
| 586 |
|
| 587 |
#[cfg(feature = "xcoff")] |
| 588 |
#[test] |
| 589 |
fn xcoff_powerpc() { |
| 590 |
for arch in [Architecture::PowerPc, Architecture::PowerPc64] { |
| 591 |
let mut object = write::Object::new(BinaryFormat::Xcoff, arch, Endianness::Big); |
| 592 |
|
| 593 |
object.add_file_symbol(b"file.c".to_vec()); |
| 594 |
|
| 595 |
let text = object.section_id(write::StandardSection::Text); |
| 596 |
object.append_section_data(text, &[1; 30], 4); |
| 597 |
|
| 598 |
let func1_offset = object.append_section_data(text, &[1; 30], 4); |
| 599 |
assert_eq!(func1_offset, 32); |
| 600 |
let func1_symbol = object.add_symbol(write::Symbol { |
| 601 |
name: b"func1".to_vec(), |
| 602 |
value: func1_offset, |
| 603 |
size: 32, |
| 604 |
kind: SymbolKind::Text, |
| 605 |
scope: SymbolScope::Linkage, |
| 606 |
weak: false, |
| 607 |
section: write::SymbolSection::Section(text), |
| 608 |
flags: SymbolFlags::None, |
| 609 |
}); |
| 610 |
|
| 611 |
object |
| 612 |
.add_relocation( |
| 613 |
text, |
| 614 |
write::Relocation { |
| 615 |
offset: 8, |
| 616 |
symbol: func1_symbol, |
| 617 |
addend: 0, |
| 618 |
flags: RelocationFlags::Generic { |
| 619 |
kind: RelocationKind::Absolute, |
| 620 |
encoding: RelocationEncoding::Generic, |
| 621 |
size: 64, |
| 622 |
}, |
| 623 |
}, |
| 624 |
) |
| 625 |
.unwrap(); |
| 626 |
|
| 627 |
let bytes = object.write().unwrap(); |
| 628 |
let object = read::File::parse(&*bytes).unwrap(); |
| 629 |
assert_eq!(object.format(), BinaryFormat::Xcoff); |
| 630 |
assert_eq!(object.architecture(), arch); |
| 631 |
assert_eq!(object.endianness(), Endianness::Big); |
| 632 |
|
| 633 |
let mut sections = object.sections(); |
| 634 |
|
| 635 |
let text = sections.next().unwrap(); |
| 636 |
println!("{:?}", text); |
| 637 |
let text_index = text.index().0; |
| 638 |
assert_eq!(text.name(), Ok(".text")); |
| 639 |
assert_eq!(text.kind(), SectionKind::Text); |
| 640 |
assert_eq!(text.address(), 0); |
| 641 |
assert_eq!(text.size(), 62); |
| 642 |
assert_eq!(&text.data().unwrap()[..30], &[1; 30]); |
| 643 |
assert_eq!(&text.data().unwrap()[32..62], &[1; 30]); |
| 644 |
|
| 645 |
let mut symbols = object.symbols(); |
| 646 |
|
| 647 |
let mut symbol = symbols.next().unwrap(); |
| 648 |
println!("{:?}", symbol); |
| 649 |
assert_eq!(symbol.name(), Ok("file.c")); |
| 650 |
assert_eq!(symbol.address(), 0); |
| 651 |
assert_eq!(symbol.kind(), SymbolKind::File); |
| 652 |
assert_eq!(symbol.section_index(), None); |
| 653 |
assert_eq!(symbol.scope(), SymbolScope::Compilation); |
| 654 |
assert!(!symbol.is_weak()); |
| 655 |
assert!(!symbol.is_undefined()); |
| 656 |
|
| 657 |
symbol = symbols.next().unwrap(); |
| 658 |
println!("{:?}", symbol); |
| 659 |
let func1_symbol = symbol.index(); |
| 660 |
assert_eq!(symbol.name(), Ok("func1")); |
| 661 |
assert_eq!(symbol.address(), func1_offset); |
| 662 |
assert_eq!(symbol.kind(), SymbolKind::Text); |
| 663 |
assert_eq!(symbol.section_index(), Some(SectionIndex(text_index))); |
| 664 |
assert_eq!(symbol.scope(), SymbolScope::Linkage); |
| 665 |
assert!(!symbol.is_weak()); |
| 666 |
assert!(!symbol.is_undefined()); |
| 667 |
|
| 668 |
let mut relocations = text.relocations(); |
| 669 |
|
| 670 |
let (offset, relocation) = relocations.next().unwrap(); |
| 671 |
println!("{:?}", relocation); |
| 672 |
assert_eq!(offset, 8); |
| 673 |
assert_eq!(relocation.kind(), RelocationKind::Absolute); |
| 674 |
assert_eq!(relocation.encoding(), RelocationEncoding::Generic); |
| 675 |
assert_eq!(relocation.size(), 64); |
| 676 |
assert_eq!( |
| 677 |
relocation.target(), |
| 678 |
read::RelocationTarget::Symbol(func1_symbol) |
| 679 |
); |
| 680 |
assert_eq!(relocation.addend(), 0); |
| 681 |
} |
| 682 |
} |