| 1 |
use alloc::vec::Vec; |
| 2 |
|
| 3 |
use crate::pe as coff; |
| 4 |
use crate::write::coff::writer; |
| 5 |
use crate::write::util::*; |
| 6 |
use crate::write::*; |
| 7 |
|
| 8 |
#[derive(Default, Clone, Copy)] |
| 9 |
struct SectionOffsets { |
| 10 |
name: writer::Name, |
| 11 |
offset: u32, |
| 12 |
reloc_offset: u32, |
| 13 |
selection: u8, |
| 14 |
associative_section: u32, |
| 15 |
} |
| 16 |
|
| 17 |
#[derive(Default, Clone, Copy)] |
| 18 |
struct SymbolOffsets { |
| 19 |
name: writer::Name, |
| 20 |
index: u32, |
| 21 |
aux_count: u8, |
| 22 |
} |
| 23 |
|
| 24 |
/// Internal format to use for the `.drectve` section containing linker |
| 25 |
/// directives for symbol exports. |
| 26 |
#[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 27 |
pub enum CoffExportStyle { |
| 28 |
/// MSVC format supported by link.exe and LLD. |
| 29 |
Msvc, |
| 30 |
/// Gnu format supported by GNU LD and LLD. |
| 31 |
Gnu, |
| 32 |
} |
| 33 |
|
| 34 |
impl<'a> Object<'a> { |
| 35 |
pub(crate) fn coff_section_info( |
| 36 |
&self, |
| 37 |
section: StandardSection, |
| 38 |
) -> (&'static [u8], &'static [u8], SectionKind, SectionFlags) { |
| 39 |
match section { |
| 40 |
StandardSection::Text => (&[], &b".text"[..], SectionKind::Text, SectionFlags::None), |
| 41 |
StandardSection::Data => (&[], &b".data"[..], SectionKind::Data, SectionFlags::None), |
| 42 |
StandardSection::ReadOnlyData |
| 43 |
| StandardSection::ReadOnlyDataWithRel |
| 44 |
| StandardSection::ReadOnlyString => ( |
| 45 |
&[], |
| 46 |
&b".rdata"[..], |
| 47 |
SectionKind::ReadOnlyData, |
| 48 |
SectionFlags::None, |
| 49 |
), |
| 50 |
StandardSection::UninitializedData => ( |
| 51 |
&[], |
| 52 |
&b".bss"[..], |
| 53 |
SectionKind::UninitializedData, |
| 54 |
SectionFlags::None, |
| 55 |
), |
| 56 |
// TLS sections are data sections with a special name. |
| 57 |
StandardSection::Tls => (&[], &b".tls$"[..], SectionKind::Data, SectionFlags::None), |
| 58 |
StandardSection::UninitializedTls => { |
| 59 |
// Unsupported section. |
| 60 |
(&[], &[], SectionKind::UninitializedTls, SectionFlags::None) |
| 61 |
} |
| 62 |
StandardSection::TlsVariables => { |
| 63 |
// Unsupported section. |
| 64 |
(&[], &[], SectionKind::TlsVariables, SectionFlags::None) |
| 65 |
} |
| 66 |
StandardSection::Common => { |
| 67 |
// Unsupported section. |
| 68 |
(&[], &[], SectionKind::Common, SectionFlags::None) |
| 69 |
} |
| 70 |
StandardSection::GnuProperty => { |
| 71 |
// Unsupported section. |
| 72 |
(&[], &[], SectionKind::Note, SectionFlags::None) |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
pub(crate) fn coff_subsection_name(&self, section: &[u8], value: &[u8]) -> Vec<u8> { |
| 78 |
let mut name = section.to_vec(); |
| 79 |
name.push(b'$'); |
| 80 |
name.extend_from_slice(value); |
| 81 |
name |
| 82 |
} |
| 83 |
|
| 84 |
pub(crate) fn coff_translate_relocation(&mut self, reloc: &mut Relocation) -> Result<()> { |
| 85 |
let (mut kind, _encoding, size) = if let RelocationFlags::Generic { |
| 86 |
kind, |
| 87 |
encoding, |
| 88 |
size, |
| 89 |
} = reloc.flags |
| 90 |
{ |
| 91 |
(kind, encoding, size) |
| 92 |
} else { |
| 93 |
return Ok(()); |
| 94 |
}; |
| 95 |
if kind == RelocationKind::GotRelative { |
| 96 |
// Use a stub symbol for the relocation instead. |
| 97 |
// This isn't really a GOT, but it's a similar purpose. |
| 98 |
// TODO: need to handle DLL imports differently? |
| 99 |
kind = RelocationKind::Relative; |
| 100 |
reloc.symbol = self.coff_add_stub_symbol(reloc.symbol)?; |
| 101 |
} else if kind == RelocationKind::PltRelative { |
| 102 |
// Windows doesn't need a separate relocation type for |
| 103 |
// references to functions in import libraries. |
| 104 |
// For convenience, treat this the same as Relative. |
| 105 |
kind = RelocationKind::Relative; |
| 106 |
} |
| 107 |
|
| 108 |
let typ = match self.architecture { |
| 109 |
Architecture::I386 => match (kind, size) { |
| 110 |
(RelocationKind::Absolute, 16) => coff::IMAGE_REL_I386_DIR16, |
| 111 |
(RelocationKind::Relative, 16) => coff::IMAGE_REL_I386_REL16, |
| 112 |
(RelocationKind::Absolute, 32) => coff::IMAGE_REL_I386_DIR32, |
| 113 |
(RelocationKind::ImageOffset, 32) => coff::IMAGE_REL_I386_DIR32NB, |
| 114 |
(RelocationKind::SectionIndex, 16) => coff::IMAGE_REL_I386_SECTION, |
| 115 |
(RelocationKind::SectionOffset, 32) => coff::IMAGE_REL_I386_SECREL, |
| 116 |
(RelocationKind::SectionOffset, 7) => coff::IMAGE_REL_I386_SECREL7, |
| 117 |
(RelocationKind::Relative, 32) => coff::IMAGE_REL_I386_REL32, |
| 118 |
_ => { |
| 119 |
return Err(Error(format!("unimplemented relocation {:?}", reloc))); |
| 120 |
} |
| 121 |
}, |
| 122 |
Architecture::X86_64 => match (kind, size) { |
| 123 |
(RelocationKind::Absolute, 64) => coff::IMAGE_REL_AMD64_ADDR64, |
| 124 |
(RelocationKind::Absolute, 32) => coff::IMAGE_REL_AMD64_ADDR32, |
| 125 |
(RelocationKind::ImageOffset, 32) => coff::IMAGE_REL_AMD64_ADDR32NB, |
| 126 |
(RelocationKind::Relative, 32) => match reloc.addend { |
| 127 |
-5 => coff::IMAGE_REL_AMD64_REL32_1, |
| 128 |
-6 => coff::IMAGE_REL_AMD64_REL32_2, |
| 129 |
-7 => coff::IMAGE_REL_AMD64_REL32_3, |
| 130 |
-8 => coff::IMAGE_REL_AMD64_REL32_4, |
| 131 |
-9 => coff::IMAGE_REL_AMD64_REL32_5, |
| 132 |
_ => coff::IMAGE_REL_AMD64_REL32, |
| 133 |
}, |
| 134 |
(RelocationKind::SectionIndex, 16) => coff::IMAGE_REL_AMD64_SECTION, |
| 135 |
(RelocationKind::SectionOffset, 32) => coff::IMAGE_REL_AMD64_SECREL, |
| 136 |
(RelocationKind::SectionOffset, 7) => coff::IMAGE_REL_AMD64_SECREL7, |
| 137 |
_ => { |
| 138 |
return Err(Error(format!("unimplemented relocation {:?}", reloc))); |
| 139 |
} |
| 140 |
}, |
| 141 |
Architecture::Arm => match (kind, size) { |
| 142 |
(RelocationKind::Absolute, 32) => coff::IMAGE_REL_ARM_ADDR32, |
| 143 |
(RelocationKind::ImageOffset, 32) => coff::IMAGE_REL_ARM_ADDR32NB, |
| 144 |
(RelocationKind::Relative, 32) => coff::IMAGE_REL_ARM_REL32, |
| 145 |
(RelocationKind::SectionIndex, 16) => coff::IMAGE_REL_ARM_SECTION, |
| 146 |
(RelocationKind::SectionOffset, 32) => coff::IMAGE_REL_ARM_SECREL, |
| 147 |
_ => { |
| 148 |
return Err(Error(format!("unimplemented relocation {:?}", reloc))); |
| 149 |
} |
| 150 |
}, |
| 151 |
Architecture::Aarch64 => match (kind, size) { |
| 152 |
(RelocationKind::Absolute, 32) => coff::IMAGE_REL_ARM64_ADDR32, |
| 153 |
(RelocationKind::ImageOffset, 32) => coff::IMAGE_REL_ARM64_ADDR32NB, |
| 154 |
(RelocationKind::SectionIndex, 16) => coff::IMAGE_REL_ARM64_SECTION, |
| 155 |
(RelocationKind::SectionOffset, 32) => coff::IMAGE_REL_ARM64_SECREL, |
| 156 |
(RelocationKind::Absolute, 64) => coff::IMAGE_REL_ARM64_ADDR64, |
| 157 |
(RelocationKind::Relative, 32) => coff::IMAGE_REL_ARM64_REL32, |
| 158 |
_ => { |
| 159 |
return Err(Error(format!("unimplemented relocation {:?}", reloc))); |
| 160 |
} |
| 161 |
}, |
| 162 |
_ => { |
| 163 |
return Err(Error(format!( |
| 164 |
"unimplemented architecture {:?}", |
| 165 |
self.architecture |
| 166 |
))); |
| 167 |
} |
| 168 |
}; |
| 169 |
reloc.flags = RelocationFlags::Coff { typ }; |
| 170 |
Ok(()) |
| 171 |
} |
| 172 |
|
| 173 |
pub(crate) fn coff_adjust_addend(&self, relocation: &mut Relocation) -> Result<bool> { |
| 174 |
let typ = if let RelocationFlags::Coff { typ } = relocation.flags { |
| 175 |
typ |
| 176 |
} else { |
| 177 |
return Err(Error(format!("invalid relocation flags {:?}", relocation))); |
| 178 |
}; |
| 179 |
let offset = match self.architecture { |
| 180 |
Architecture::Arm => { |
| 181 |
if typ == coff::IMAGE_REL_ARM_REL32 { |
| 182 |
4 |
| 183 |
} else { |
| 184 |
0 |
| 185 |
} |
| 186 |
} |
| 187 |
Architecture::Aarch64 => { |
| 188 |
if typ == coff::IMAGE_REL_ARM64_REL32 { |
| 189 |
4 |
| 190 |
} else { |
| 191 |
0 |
| 192 |
} |
| 193 |
} |
| 194 |
Architecture::I386 => { |
| 195 |
if typ == coff::IMAGE_REL_I386_REL32 { |
| 196 |
4 |
| 197 |
} else { |
| 198 |
0 |
| 199 |
} |
| 200 |
} |
| 201 |
Architecture::X86_64 => match typ { |
| 202 |
coff::IMAGE_REL_AMD64_REL32 => 4, |
| 203 |
coff::IMAGE_REL_AMD64_REL32_1 => 5, |
| 204 |
coff::IMAGE_REL_AMD64_REL32_2 => 6, |
| 205 |
coff::IMAGE_REL_AMD64_REL32_3 => 7, |
| 206 |
coff::IMAGE_REL_AMD64_REL32_4 => 8, |
| 207 |
coff::IMAGE_REL_AMD64_REL32_5 => 9, |
| 208 |
_ => 0, |
| 209 |
}, |
| 210 |
_ => return Err(Error(format!("unimplemented relocation {:?}", relocation))), |
| 211 |
}; |
| 212 |
relocation.addend += offset; |
| 213 |
Ok(true) |
| 214 |
} |
| 215 |
|
| 216 |
pub(crate) fn coff_relocation_size(&self, reloc: &Relocation) -> Result<u8> { |
| 217 |
let typ = if let RelocationFlags::Coff { typ } = reloc.flags { |
| 218 |
typ |
| 219 |
} else { |
| 220 |
return Err(Error(format!("unexpected relocation for size {:?}", reloc))); |
| 221 |
}; |
| 222 |
let size = match self.architecture { |
| 223 |
Architecture::I386 => match typ { |
| 224 |
coff::IMAGE_REL_I386_DIR16 |
| 225 |
| coff::IMAGE_REL_I386_REL16 |
| 226 |
| coff::IMAGE_REL_I386_SECTION => Some(16), |
| 227 |
coff::IMAGE_REL_I386_DIR32 |
| 228 |
| coff::IMAGE_REL_I386_DIR32NB |
| 229 |
| coff::IMAGE_REL_I386_SECREL |
| 230 |
| coff::IMAGE_REL_I386_TOKEN |
| 231 |
| coff::IMAGE_REL_I386_REL32 => Some(32), |
| 232 |
_ => None, |
| 233 |
}, |
| 234 |
Architecture::X86_64 => match typ { |
| 235 |
coff::IMAGE_REL_AMD64_SECTION => Some(16), |
| 236 |
coff::IMAGE_REL_AMD64_ADDR32 |
| 237 |
| coff::IMAGE_REL_AMD64_ADDR32NB |
| 238 |
| coff::IMAGE_REL_AMD64_REL32 |
| 239 |
| coff::IMAGE_REL_AMD64_REL32_1 |
| 240 |
| coff::IMAGE_REL_AMD64_REL32_2 |
| 241 |
| coff::IMAGE_REL_AMD64_REL32_3 |
| 242 |
| coff::IMAGE_REL_AMD64_REL32_4 |
| 243 |
| coff::IMAGE_REL_AMD64_REL32_5 |
| 244 |
| coff::IMAGE_REL_AMD64_SECREL |
| 245 |
| coff::IMAGE_REL_AMD64_TOKEN => Some(32), |
| 246 |
coff::IMAGE_REL_AMD64_ADDR64 => Some(64), |
| 247 |
_ => None, |
| 248 |
}, |
| 249 |
Architecture::Arm => match typ { |
| 250 |
coff::IMAGE_REL_ARM_SECTION => Some(16), |
| 251 |
coff::IMAGE_REL_ARM_ADDR32 |
| 252 |
| coff::IMAGE_REL_ARM_ADDR32NB |
| 253 |
| coff::IMAGE_REL_ARM_TOKEN |
| 254 |
| coff::IMAGE_REL_ARM_REL32 |
| 255 |
| coff::IMAGE_REL_ARM_SECREL => Some(32), |
| 256 |
_ => None, |
| 257 |
}, |
| 258 |
Architecture::Aarch64 => match typ { |
| 259 |
coff::IMAGE_REL_ARM64_SECTION => Some(16), |
| 260 |
coff::IMAGE_REL_ARM64_ADDR32 |
| 261 |
| coff::IMAGE_REL_ARM64_ADDR32NB |
| 262 |
| coff::IMAGE_REL_ARM64_SECREL |
| 263 |
| coff::IMAGE_REL_ARM64_TOKEN |
| 264 |
| coff::IMAGE_REL_ARM64_REL32 => Some(32), |
| 265 |
coff::IMAGE_REL_ARM64_ADDR64 => Some(64), |
| 266 |
_ => None, |
| 267 |
}, |
| 268 |
_ => None, |
| 269 |
}; |
| 270 |
size.ok_or_else(|| Error(format!("unsupported relocation for size {:?}", reloc))) |
| 271 |
} |
| 272 |
|
| 273 |
fn coff_add_stub_symbol(&mut self, symbol_id: SymbolId) -> Result<SymbolId> { |
| 274 |
if let Some(stub_id) = self.stub_symbols.get(&symbol_id) { |
| 275 |
return Ok(*stub_id); |
| 276 |
} |
| 277 |
let stub_size = self.architecture.address_size().unwrap().bytes(); |
| 278 |
|
| 279 |
let name = b".rdata$.refptr".to_vec(); |
| 280 |
let section_id = self.add_section(Vec::new(), name, SectionKind::ReadOnlyData); |
| 281 |
let section = self.section_mut(section_id); |
| 282 |
section.set_data(vec![0; stub_size as usize], u64::from(stub_size)); |
| 283 |
self.add_relocation( |
| 284 |
section_id, |
| 285 |
Relocation { |
| 286 |
offset: 0, |
| 287 |
symbol: symbol_id, |
| 288 |
addend: 0, |
| 289 |
flags: RelocationFlags::Generic { |
| 290 |
kind: RelocationKind::Absolute, |
| 291 |
encoding: RelocationEncoding::Generic, |
| 292 |
size: stub_size * 8, |
| 293 |
}, |
| 294 |
}, |
| 295 |
)?; |
| 296 |
|
| 297 |
let mut name = b".refptr.".to_vec(); |
| 298 |
name.extend_from_slice(&self.symbol(symbol_id).name); |
| 299 |
let stub_id = self.add_raw_symbol(Symbol { |
| 300 |
name, |
| 301 |
value: 0, |
| 302 |
size: u64::from(stub_size), |
| 303 |
kind: SymbolKind::Data, |
| 304 |
scope: SymbolScope::Compilation, |
| 305 |
weak: false, |
| 306 |
section: SymbolSection::Section(section_id), |
| 307 |
flags: SymbolFlags::None, |
| 308 |
}); |
| 309 |
self.stub_symbols.insert(symbol_id, stub_id); |
| 310 |
|
| 311 |
Ok(stub_id) |
| 312 |
} |
| 313 |
|
| 314 |
/// Appends linker directives to the `.drectve` section to tell the linker |
| 315 |
/// to export all symbols with `SymbolScope::Dynamic`. |
| 316 |
/// |
| 317 |
/// This must be called after all symbols have been defined. |
| 318 |
pub fn add_coff_exports(&mut self, style: CoffExportStyle) { |
| 319 |
assert_eq!(self.format, BinaryFormat::Coff); |
| 320 |
|
| 321 |
let mut directives = vec![]; |
| 322 |
for symbol in &self.symbols { |
| 323 |
if symbol.scope == SymbolScope::Dynamic { |
| 324 |
match style { |
| 325 |
CoffExportStyle::Msvc => directives.extend(b" /EXPORT:\""), |
| 326 |
CoffExportStyle::Gnu => directives.extend(b" -export:\""), |
| 327 |
} |
| 328 |
directives.extend(&symbol.name); |
| 329 |
directives.extend(b"\""); |
| 330 |
if symbol.kind != SymbolKind::Text { |
| 331 |
match style { |
| 332 |
CoffExportStyle::Msvc => directives.extend(b",DATA"), |
| 333 |
CoffExportStyle::Gnu => directives.extend(b",data"), |
| 334 |
} |
| 335 |
} |
| 336 |
} |
| 337 |
} |
| 338 |
let drectve = self.add_section(vec![], b".drectve".to_vec(), SectionKind::Linker); |
| 339 |
self.append_section_data(drectve, &directives, 1); |
| 340 |
} |
| 341 |
|
| 342 |
pub(crate) fn coff_write(&self, buffer: &mut dyn WritableBuffer) -> Result<()> { |
| 343 |
let mut writer = writer::Writer::new(buffer); |
| 344 |
|
| 345 |
// Add section strings to strtab. |
| 346 |
let mut section_offsets = vec![SectionOffsets::default(); self.sections.len()]; |
| 347 |
for (index, section) in self.sections.iter().enumerate() { |
| 348 |
section_offsets[index].name = writer.add_name(§ion.name); |
| 349 |
} |
| 350 |
|
| 351 |
// Set COMDAT flags. |
| 352 |
for comdat in &self.comdats { |
| 353 |
let symbol = &self.symbols[comdat.symbol.0]; |
| 354 |
let comdat_section = match symbol.section { |
| 355 |
SymbolSection::Section(id) => id.0, |
| 356 |
_ => { |
| 357 |
return Err(Error(format!( |
| 358 |
"unsupported COMDAT symbol `{}` section {:?}", |
| 359 |
symbol.name().unwrap_or(""), |
| 360 |
symbol.section |
| 361 |
))); |
| 362 |
} |
| 363 |
}; |
| 364 |
section_offsets[comdat_section].selection = match comdat.kind { |
| 365 |
ComdatKind::NoDuplicates => coff::IMAGE_COMDAT_SELECT_NODUPLICATES, |
| 366 |
ComdatKind::Any => coff::IMAGE_COMDAT_SELECT_ANY, |
| 367 |
ComdatKind::SameSize => coff::IMAGE_COMDAT_SELECT_SAME_SIZE, |
| 368 |
ComdatKind::ExactMatch => coff::IMAGE_COMDAT_SELECT_EXACT_MATCH, |
| 369 |
ComdatKind::Largest => coff::IMAGE_COMDAT_SELECT_LARGEST, |
| 370 |
ComdatKind::Newest => coff::IMAGE_COMDAT_SELECT_NEWEST, |
| 371 |
ComdatKind::Unknown => { |
| 372 |
return Err(Error(format!( |
| 373 |
"unsupported COMDAT symbol `{}` kind {:?}", |
| 374 |
symbol.name().unwrap_or(""), |
| 375 |
comdat.kind |
| 376 |
))); |
| 377 |
} |
| 378 |
}; |
| 379 |
for id in &comdat.sections { |
| 380 |
let section = &self.sections[id.0]; |
| 381 |
if section.symbol.is_none() { |
| 382 |
return Err(Error(format!( |
| 383 |
"missing symbol for COMDAT section `{}`", |
| 384 |
section.name().unwrap_or(""), |
| 385 |
))); |
| 386 |
} |
| 387 |
if id.0 != comdat_section { |
| 388 |
section_offsets[id.0].selection = coff::IMAGE_COMDAT_SELECT_ASSOCIATIVE; |
| 389 |
section_offsets[id.0].associative_section = comdat_section as u32 + 1; |
| 390 |
} |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
// Reserve symbol indices and add symbol strings to strtab. |
| 395 |
let mut symbol_offsets = vec![SymbolOffsets::default(); self.symbols.len()]; |
| 396 |
for (index, symbol) in self.symbols.iter().enumerate() { |
| 397 |
symbol_offsets[index].index = writer.reserve_symbol_index(); |
| 398 |
let mut name = &*symbol.name; |
| 399 |
match symbol.kind { |
| 400 |
SymbolKind::File => { |
| 401 |
// Name goes in auxiliary symbol records. |
| 402 |
symbol_offsets[index].aux_count = writer.reserve_aux_file_name(&symbol.name); |
| 403 |
name = b".file"; |
| 404 |
} |
| 405 |
SymbolKind::Section if symbol.section.id().is_some() => { |
| 406 |
symbol_offsets[index].aux_count = writer.reserve_aux_section(); |
| 407 |
} |
| 408 |
_ => {} |
| 409 |
}; |
| 410 |
symbol_offsets[index].name = writer.add_name(name); |
| 411 |
} |
| 412 |
|
| 413 |
// Reserve file ranges. |
| 414 |
writer.reserve_file_header(); |
| 415 |
writer.reserve_section_headers(self.sections.len() as u16); |
| 416 |
for (index, section) in self.sections.iter().enumerate() { |
| 417 |
section_offsets[index].offset = writer.reserve_section(section.data.len()); |
| 418 |
section_offsets[index].reloc_offset = |
| 419 |
writer.reserve_relocations(section.relocations.len()); |
| 420 |
} |
| 421 |
writer.reserve_symtab_strtab(); |
| 422 |
|
| 423 |
// Start writing. |
| 424 |
writer.write_file_header(writer::FileHeader { |
| 425 |
machine: match (self.architecture, self.sub_architecture) { |
| 426 |
(Architecture::Arm, None) => coff::IMAGE_FILE_MACHINE_ARMNT, |
| 427 |
(Architecture::Aarch64, None) => coff::IMAGE_FILE_MACHINE_ARM64, |
| 428 |
(Architecture::Aarch64, Some(SubArchitecture::Arm64EC)) => { |
| 429 |
coff::IMAGE_FILE_MACHINE_ARM64EC |
| 430 |
} |
| 431 |
(Architecture::I386, None) => coff::IMAGE_FILE_MACHINE_I386, |
| 432 |
(Architecture::X86_64, None) => coff::IMAGE_FILE_MACHINE_AMD64, |
| 433 |
_ => { |
| 434 |
return Err(Error(format!( |
| 435 |
"unimplemented architecture {:?} with sub-architecture {:?}", |
| 436 |
self.architecture, self.sub_architecture |
| 437 |
))); |
| 438 |
} |
| 439 |
}, |
| 440 |
time_date_stamp: 0, |
| 441 |
characteristics: match self.flags { |
| 442 |
FileFlags::Coff { characteristics } => characteristics, |
| 443 |
_ => 0, |
| 444 |
}, |
| 445 |
})?; |
| 446 |
|
| 447 |
// Write section headers. |
| 448 |
for (index, section) in self.sections.iter().enumerate() { |
| 449 |
let mut characteristics = if let SectionFlags::Coff { |
| 450 |
characteristics, .. |
| 451 |
} = section.flags |
| 452 |
{ |
| 453 |
characteristics |
| 454 |
} else { |
| 455 |
match section.kind { |
| 456 |
SectionKind::Text => { |
| 457 |
coff::IMAGE_SCN_CNT_CODE |
| 458 |
| coff::IMAGE_SCN_MEM_EXECUTE |
| 459 |
| coff::IMAGE_SCN_MEM_READ |
| 460 |
} |
| 461 |
SectionKind::Data => { |
| 462 |
coff::IMAGE_SCN_CNT_INITIALIZED_DATA |
| 463 |
| coff::IMAGE_SCN_MEM_READ |
| 464 |
| coff::IMAGE_SCN_MEM_WRITE |
| 465 |
} |
| 466 |
SectionKind::UninitializedData => { |
| 467 |
coff::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
| 468 |
| coff::IMAGE_SCN_MEM_READ |
| 469 |
| coff::IMAGE_SCN_MEM_WRITE |
| 470 |
} |
| 471 |
SectionKind::ReadOnlyData |
| 472 |
| SectionKind::ReadOnlyDataWithRel |
| 473 |
| SectionKind::ReadOnlyString => { |
| 474 |
coff::IMAGE_SCN_CNT_INITIALIZED_DATA | coff::IMAGE_SCN_MEM_READ |
| 475 |
} |
| 476 |
SectionKind::Debug |
| 477 |
| SectionKind::DebugString |
| 478 |
| SectionKind::Other |
| 479 |
| SectionKind::OtherString => { |
| 480 |
coff::IMAGE_SCN_CNT_INITIALIZED_DATA |
| 481 |
| coff::IMAGE_SCN_MEM_READ |
| 482 |
| coff::IMAGE_SCN_MEM_DISCARDABLE |
| 483 |
} |
| 484 |
SectionKind::Linker => coff::IMAGE_SCN_LNK_INFO | coff::IMAGE_SCN_LNK_REMOVE, |
| 485 |
SectionKind::Common |
| 486 |
| SectionKind::Tls |
| 487 |
| SectionKind::UninitializedTls |
| 488 |
| SectionKind::TlsVariables |
| 489 |
| SectionKind::Note |
| 490 |
| SectionKind::Unknown |
| 491 |
| SectionKind::Metadata |
| 492 |
| SectionKind::Elf(_) => { |
| 493 |
return Err(Error(format!( |
| 494 |
"unimplemented section `{}` kind {:?}", |
| 495 |
section.name().unwrap_or(""), |
| 496 |
section.kind |
| 497 |
))); |
| 498 |
} |
| 499 |
} |
| 500 |
}; |
| 501 |
if section_offsets[index].selection != 0 { |
| 502 |
characteristics |= coff::IMAGE_SCN_LNK_COMDAT; |
| 503 |
}; |
| 504 |
if section.relocations.len() > 0xffff { |
| 505 |
characteristics |= coff::IMAGE_SCN_LNK_NRELOC_OVFL; |
| 506 |
} |
| 507 |
characteristics |= match section.align { |
| 508 |
1 => coff::IMAGE_SCN_ALIGN_1BYTES, |
| 509 |
2 => coff::IMAGE_SCN_ALIGN_2BYTES, |
| 510 |
4 => coff::IMAGE_SCN_ALIGN_4BYTES, |
| 511 |
8 => coff::IMAGE_SCN_ALIGN_8BYTES, |
| 512 |
16 => coff::IMAGE_SCN_ALIGN_16BYTES, |
| 513 |
32 => coff::IMAGE_SCN_ALIGN_32BYTES, |
| 514 |
64 => coff::IMAGE_SCN_ALIGN_64BYTES, |
| 515 |
128 => coff::IMAGE_SCN_ALIGN_128BYTES, |
| 516 |
256 => coff::IMAGE_SCN_ALIGN_256BYTES, |
| 517 |
512 => coff::IMAGE_SCN_ALIGN_512BYTES, |
| 518 |
1024 => coff::IMAGE_SCN_ALIGN_1024BYTES, |
| 519 |
2048 => coff::IMAGE_SCN_ALIGN_2048BYTES, |
| 520 |
4096 => coff::IMAGE_SCN_ALIGN_4096BYTES, |
| 521 |
8192 => coff::IMAGE_SCN_ALIGN_8192BYTES, |
| 522 |
_ => { |
| 523 |
return Err(Error(format!( |
| 524 |
"unimplemented section `{}` align {}", |
| 525 |
section.name().unwrap_or(""), |
| 526 |
section.align |
| 527 |
))); |
| 528 |
} |
| 529 |
}; |
| 530 |
writer.write_section_header(writer::SectionHeader { |
| 531 |
name: section_offsets[index].name, |
| 532 |
size_of_raw_data: section.size as u32, |
| 533 |
pointer_to_raw_data: section_offsets[index].offset, |
| 534 |
pointer_to_relocations: section_offsets[index].reloc_offset, |
| 535 |
pointer_to_linenumbers: 0, |
| 536 |
number_of_relocations: section.relocations.len() as u32, |
| 537 |
number_of_linenumbers: 0, |
| 538 |
characteristics, |
| 539 |
}); |
| 540 |
} |
| 541 |
|
| 542 |
// Write section data and relocations. |
| 543 |
for section in &self.sections { |
| 544 |
writer.write_section(§ion.data); |
| 545 |
|
| 546 |
if !section.relocations.is_empty() { |
| 547 |
//debug_assert_eq!(section_offsets[index].reloc_offset, buffer.len()); |
| 548 |
writer.write_relocations_count(section.relocations.len()); |
| 549 |
for reloc in §ion.relocations { |
| 550 |
let typ = if let RelocationFlags::Coff { typ } = reloc.flags { |
| 551 |
typ |
| 552 |
} else { |
| 553 |
return Err(Error("invalid relocation flags".into())); |
| 554 |
}; |
| 555 |
writer.write_relocation(writer::Relocation { |
| 556 |
virtual_address: reloc.offset as u32, |
| 557 |
symbol: symbol_offsets[reloc.symbol.0].index, |
| 558 |
typ, |
| 559 |
}); |
| 560 |
} |
| 561 |
} |
| 562 |
} |
| 563 |
|
| 564 |
// Write symbols. |
| 565 |
for (index, symbol) in self.symbols.iter().enumerate() { |
| 566 |
let section_number = match symbol.section { |
| 567 |
SymbolSection::None => { |
| 568 |
debug_assert_eq!(symbol.kind, SymbolKind::File); |
| 569 |
coff::IMAGE_SYM_DEBUG as u16 |
| 570 |
} |
| 571 |
SymbolSection::Undefined => coff::IMAGE_SYM_UNDEFINED as u16, |
| 572 |
SymbolSection::Absolute => coff::IMAGE_SYM_ABSOLUTE as u16, |
| 573 |
SymbolSection::Common => coff::IMAGE_SYM_UNDEFINED as u16, |
| 574 |
SymbolSection::Section(id) => id.0 as u16 + 1, |
| 575 |
}; |
| 576 |
let typ = if symbol.kind == SymbolKind::Text { |
| 577 |
coff::IMAGE_SYM_DTYPE_FUNCTION << coff::IMAGE_SYM_DTYPE_SHIFT |
| 578 |
} else { |
| 579 |
coff::IMAGE_SYM_TYPE_NULL |
| 580 |
}; |
| 581 |
let storage_class = match symbol.kind { |
| 582 |
SymbolKind::File => coff::IMAGE_SYM_CLASS_FILE, |
| 583 |
SymbolKind::Section => { |
| 584 |
if symbol.section.id().is_some() { |
| 585 |
coff::IMAGE_SYM_CLASS_STATIC |
| 586 |
} else { |
| 587 |
coff::IMAGE_SYM_CLASS_SECTION |
| 588 |
} |
| 589 |
} |
| 590 |
SymbolKind::Label => coff::IMAGE_SYM_CLASS_LABEL, |
| 591 |
SymbolKind::Text | SymbolKind::Data | SymbolKind::Tls => { |
| 592 |
match symbol.section { |
| 593 |
SymbolSection::None => { |
| 594 |
return Err(Error(format!( |
| 595 |
"missing section for symbol `{}`", |
| 596 |
symbol.name().unwrap_or("") |
| 597 |
))); |
| 598 |
} |
| 599 |
SymbolSection::Undefined | SymbolSection::Common => { |
| 600 |
coff::IMAGE_SYM_CLASS_EXTERNAL |
| 601 |
} |
| 602 |
SymbolSection::Absolute | SymbolSection::Section(_) => { |
| 603 |
match symbol.scope { |
| 604 |
// TODO: does this need aux symbol records too? |
| 605 |
_ if symbol.weak => coff::IMAGE_SYM_CLASS_WEAK_EXTERNAL, |
| 606 |
SymbolScope::Unknown => { |
| 607 |
return Err(Error(format!( |
| 608 |
"unimplemented symbol `{}` scope {:?}", |
| 609 |
symbol.name().unwrap_or(""), |
| 610 |
symbol.scope |
| 611 |
))); |
| 612 |
} |
| 613 |
SymbolScope::Compilation => coff::IMAGE_SYM_CLASS_STATIC, |
| 614 |
SymbolScope::Linkage | SymbolScope::Dynamic => { |
| 615 |
coff::IMAGE_SYM_CLASS_EXTERNAL |
| 616 |
} |
| 617 |
} |
| 618 |
} |
| 619 |
} |
| 620 |
} |
| 621 |
SymbolKind::Unknown => { |
| 622 |
return Err(Error(format!( |
| 623 |
"unimplemented symbol `{}` kind {:?}", |
| 624 |
symbol.name().unwrap_or(""), |
| 625 |
symbol.kind |
| 626 |
))); |
| 627 |
} |
| 628 |
}; |
| 629 |
let number_of_aux_symbols = symbol_offsets[index].aux_count; |
| 630 |
let value = if symbol.section == SymbolSection::Common { |
| 631 |
symbol.size as u32 |
| 632 |
} else { |
| 633 |
symbol.value as u32 |
| 634 |
}; |
| 635 |
writer.write_symbol(writer::Symbol { |
| 636 |
name: symbol_offsets[index].name, |
| 637 |
value, |
| 638 |
section_number, |
| 639 |
typ, |
| 640 |
storage_class, |
| 641 |
number_of_aux_symbols, |
| 642 |
}); |
| 643 |
|
| 644 |
// Write auxiliary symbols. |
| 645 |
match symbol.kind { |
| 646 |
SymbolKind::File => { |
| 647 |
writer.write_aux_file_name(&symbol.name, number_of_aux_symbols); |
| 648 |
} |
| 649 |
SymbolKind::Section if symbol.section.id().is_some() => { |
| 650 |
debug_assert_eq!(number_of_aux_symbols, 1); |
| 651 |
let section_index = symbol.section.id().unwrap().0; |
| 652 |
let section = &self.sections[section_index]; |
| 653 |
writer.write_aux_section(writer::AuxSymbolSection { |
| 654 |
length: section.size as u32, |
| 655 |
number_of_relocations: section.relocations.len() as u32, |
| 656 |
number_of_linenumbers: 0, |
| 657 |
check_sum: if section.is_bss() { |
| 658 |
0 |
| 659 |
} else { |
| 660 |
checksum(section.data()) |
| 661 |
}, |
| 662 |
number: section_offsets[section_index].associative_section, |
| 663 |
selection: section_offsets[section_index].selection, |
| 664 |
}); |
| 665 |
} |
| 666 |
_ => { |
| 667 |
debug_assert_eq!(number_of_aux_symbols, 0); |
| 668 |
} |
| 669 |
} |
| 670 |
} |
| 671 |
|
| 672 |
writer.write_strtab(); |
| 673 |
|
| 674 |
debug_assert_eq!(writer.reserved_len(), writer.len()); |
| 675 |
|
| 676 |
Ok(()) |
| 677 |
} |
| 678 |
} |
| 679 |
|
| 680 |
// JamCRC |
| 681 |
fn checksum(data: &[u8]) -> u32 { |
| 682 |
let mut hasher = crc32fast::Hasher::new_with_initial(0xffff_ffff); |
| 683 |
hasher.update(data); |
| 684 |
!hasher.finalize() |
| 685 |
} |