| 1 | #[cfg(any(not(feature = "msvc"), not(feature = "gnu")))] |
| 2 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#; |
| 3 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::{Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#, Seekrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Seek#, Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#}; |
| 4 | |
| 5 | use objectrust-analyzer cargo object 0.39.1 crate/::perust-analyzer cargo object 0.39.1 pe/::*; |
| 6 | |
| 7 | /// Unix archiver writer |
| 8 | #[cfg(any(feature = "msvc", feature = "gnu"))] |
| 9 | mod arrust-analyzer cargo implib 0.3.3 ar/; |
| 10 | /// Parse .DEF file |
| 11 | pub mod defrust-analyzer cargo implib 0.3.3 def/; |
| 12 | /// GNU binutils flavored import library |
| 13 | #[cfg(feature = "gnu")] |
| 14 | mod gnurust-analyzer cargo implib 0.3.3 gnu/; |
| 15 | /// MSVC flavored import library |
| 16 | #[cfg(feature = "msvc")] |
| 17 | mod msvcrust-analyzer cargo implib 0.3.3 msvc/; |
| 18 | |
| 19 | #[cfg(feature = "gnu")] |
| 20 | use selfrust-analyzer cargo implib 0.3.3 crate/::gnurust-analyzer cargo implib 0.3.3 gnu/::GnuImportLibraryrust-analyzer cargo implib 0.3.3 gnu/GnuImportLibrary#; |
| 21 | #[cfg(feature = "msvc")] |
| 22 | use selfrust-analyzer cargo implib 0.3.3 crate/::msvcrust-analyzer cargo implib 0.3.3 msvc/::MsvcImportLibraryrust-analyzer cargo implib 0.3.3 msvc/MsvcImportLibrary#; |
| 23 | use craterust-analyzer cargo implib 0.3.3 crate/::defrust-analyzer cargo implib 0.3.3 def/::ModuleDefrust-analyzer cargo implib 0.3.3 def/ModuleDef#; |
| 24 | |
| 25 | /// Machine types |
| 26 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:)] |
| 27 | #[repr(u16)] |
| 28 | pub enum MachineTyperust-analyzer cargo implib 0.3.3 MachineType# { |
| 29 | /// Intel 386 |
| 30 | I386rust-analyzer cargo implib 0.3.3 MachineType#I386# = IMAGE_FILE_MACHINE_I386rust-analyzer cargo object 0.39.1 pe/IMAGE_FILE_MACHINE_I386., |
| 31 | /// ARM Thumb-2 Little-Endian |
| 32 | ARMNTrust-analyzer cargo implib 0.3.3 MachineType#ARMNT# = IMAGE_FILE_MACHINE_ARMNTrust-analyzer cargo object 0.39.1 pe/IMAGE_FILE_MACHINE_ARMNT., |
| 33 | /// AMD64 (K8) |
| 34 | AMD64rust-analyzer cargo implib 0.3.3 MachineType#AMD64# = IMAGE_FILE_MACHINE_AMD64rust-analyzer cargo object 0.39.1 pe/IMAGE_FILE_MACHINE_AMD64., |
| 35 | ARM64rust-analyzer cargo implib 0.3.3 MachineType#ARM64# = IMAGE_FILE_MACHINE_ARM64rust-analyzer cargo object 0.39.1 pe/IMAGE_FILE_MACHINE_ARM64., |
| 36 | } |
| 37 | |
| 38 | impl MachineTyperust-analyzer cargo implib 0.3.3 MachineType# { |
| 39 | fn img_rel_relocationrust-analyzer cargo implib 0.3.3 impl#[MachineType]img_rel_relocation().(&self) -> u16 { |
| 40 | match self { |
| 41 | Selfrust-analyzer cargo implib 0.3.3 impl#[MachineType]::AMD64rust-analyzer cargo implib 0.3.3 MachineType#AMD64# => IMAGE_REL_AMD64_ADDR32NBrust-analyzer cargo object 0.39.1 pe/IMAGE_REL_AMD64_ADDR32NB., |
| 42 | Selfrust-analyzer cargo implib 0.3.3 impl#[MachineType]::ARMNTrust-analyzer cargo implib 0.3.3 MachineType#ARMNT# => IMAGE_REL_ARM_ADDR32NBrust-analyzer cargo object 0.39.1 pe/IMAGE_REL_ARM_ADDR32NB., |
| 43 | Selfrust-analyzer cargo implib 0.3.3 impl#[MachineType]::ARM64rust-analyzer cargo implib 0.3.3 MachineType#ARM64# => IMAGE_REL_ARM64_ADDR32NBrust-analyzer cargo object 0.39.1 pe/IMAGE_REL_ARM64_ADDR32NB., |
| 44 | Selfrust-analyzer cargo implib 0.3.3 impl#[MachineType]::I386rust-analyzer cargo implib 0.3.3 MachineType#I386# => IMAGE_REL_I386_DIR32NBrust-analyzer cargo object 0.39.1 pe/IMAGE_REL_I386_DIR32NB., |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 50 | struct ArchiveMemberrust-analyzer cargo implib 0.3.3 ArchiveMember# { |
| 51 | namerust-analyzer cargo implib 0.3.3 ArchiveMember#name.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, |
| 52 | datarust-analyzer cargo implib 0.3.3 ArchiveMember#data.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>, |
| 53 | symbolsrust-analyzer cargo implib 0.3.3 ArchiveMember#symbols.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>, |
| 54 | } |
| 55 | |
| 56 | impl ArchiveMemberrust-analyzer cargo implib 0.3.3 ArchiveMember# { |
| 57 | #[cfg(any(feature = "msvc", feature = "gnu"))] |
| 58 | fn create_archive_entryrust-analyzer cargo implib 0.3.3 impl#[ArchiveMember]create_archive_entry().(self) -> (arrust-analyzer cargo implib 0.3.3 ar/::Headerrust-analyzer cargo implib 0.3.3 ar/Header#, ArchiveMemberrust-analyzer cargo implib 0.3.3 ArchiveMember#) { |
| 59 | let mut header = |
| 60 | arrust-analyzer cargo implib 0.3.3 ar/::Headerrust-analyzer cargo implib 0.3.3 ar/Header#::newrust-analyzer cargo implib 0.3.3 ar/impl#[Header]new().(self.namerust-analyzer cargo implib 0.3.3 ArchiveMember#name..to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().().into_bytesrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]into_bytes().(), self.datarust-analyzer cargo implib 0.3.3 ArchiveMember#data..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() as u64); |
| 61 | header.set_moderust-analyzer cargo implib 0.3.3 ar/impl#[Header]set_mode().(0o644); |
| 62 | (header, self) |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /// Import library flavor |
| 67 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:)] |
| 68 | pub enum Flavorrust-analyzer cargo implib 0.3.3 Flavor# { |
| 69 | /// MSVC short import library |
| 70 | Msvcrust-analyzer cargo implib 0.3.3 Flavor#Msvc#, |
| 71 | /// GNU(MinGW) import library |
| 72 | Gnurust-analyzer cargo implib 0.3.3 Flavor#Gnu#, |
| 73 | } |
| 74 | |
| 75 | /// Windows import library generator |
| 76 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:)] |
| 77 | pub struct ImportLibraryrust-analyzer cargo implib 0.3.3 ImportLibrary# { |
| 78 | defrust-analyzer cargo implib 0.3.3 ImportLibrary#def.: ModuleDefrust-analyzer cargo implib 0.3.3 def/ModuleDef#, |
| 79 | machinerust-analyzer cargo implib 0.3.3 ImportLibrary#machine.: MachineTyperust-analyzer cargo implib 0.3.3 MachineType#, |
| 80 | flavorrust-analyzer cargo implib 0.3.3 ImportLibrary#flavor.: Flavorrust-analyzer cargo implib 0.3.3 Flavor#, |
| 81 | } |
| 82 | |
| 83 | impl ImportLibraryrust-analyzer cargo implib 0.3.3 ImportLibrary# { |
| 84 | /// Create new import library generator from module definition text content |
| 85 | pub fn newrust-analyzer cargo implib 0.3.3 impl#[ImportLibrary]new().(def: &str, machine: MachineTyperust-analyzer cargo implib 0.3.3 MachineType#, flavor: Flavorrust-analyzer cargo implib 0.3.3 Flavor#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo implib 0.3.3 impl#[ImportLibrary], Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#> { |
| 86 | let def = ModuleDefrust-analyzer cargo implib 0.3.3 def/ModuleDef#::parserust-analyzer cargo implib 0.3.3 def/impl#[ModuleDef]parse().(def, machine)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 87 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Selfrust-analyzer cargo implib 0.3.3 impl#[ImportLibrary]::from_defrust-analyzer cargo implib 0.3.3 impl#[ImportLibrary]from_def().(def, machine, flavor)) |
| 88 | } |
| 89 | |
| 90 | /// Create new import library generator from `ModuleDef` |
| 91 | pub fn from_defrust-analyzer cargo implib 0.3.3 impl#[ImportLibrary]from_def().(mut def: ModuleDefrust-analyzer cargo implib 0.3.3 def/ModuleDef#, machine: MachineTyperust-analyzer cargo implib 0.3.3 MachineType#, flavor: Flavorrust-analyzer cargo implib 0.3.3 Flavor#) -> Selfrust-analyzer cargo implib 0.3.3 impl#[ImportLibrary] { |
| 92 | // If ext_name is set (if the "ext_name = name" syntax was used), overwrite |
| 93 | // name with ext_name and clear ext_name. When only creating an import |
| 94 | // library and not linking, the internal name is irrelevant. |
| 95 | for export in &mut def.exportsrust-analyzer cargo implib 0.3.3 def/ModuleDef#exports. { |
| 96 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(ext_name) = export.ext_namerust-analyzer cargo implib 0.3.3 def/ShortExport#ext_name..takerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]take().() { |
| 97 | export.namerust-analyzer cargo implib 0.3.3 def/ShortExport#name. = ext_name; |
| 98 | } |
| 99 | } |
| 100 | // Skipped i386 handling |
| 101 | // See https://github.com/llvm/llvm-project/blob/09c2b7c35af8c4bad39f03e9f60df8bd07323028/llvm/lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp#L197-L212 |
| 102 | ImportLibraryrust-analyzer cargo implib 0.3.3 ImportLibrary# { |
| 103 | defrust-analyzer cargo implib 0.3.3 ImportLibrary#def., |
| 104 | machinerust-analyzer cargo implib 0.3.3 ImportLibrary#machine., |
| 105 | flavorrust-analyzer cargo implib 0.3.3 ImportLibrary#flavor., |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | /// Get import library name |
| 110 | pub fn import_namerust-analyzer cargo implib 0.3.3 impl#[ImportLibrary]import_name().(&self) -> &str { |
| 111 | &self.defrust-analyzer cargo implib 0.3.3 ImportLibrary#def..import_namerust-analyzer cargo implib 0.3.3 def/ModuleDef#import_name. |
| 112 | } |
| 113 | |
| 114 | /// Write out the import library |
| 115 | pub fn write_torust-analyzer cargo implib 0.3.3 impl#[ImportLibrary]write_to().<W: Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write# + Seekrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Seek#>(self, writer: &mut W) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#> { |
| 116 | match self.flavorrust-analyzer cargo implib 0.3.3 ImportLibrary#flavor. { |
| 117 | #[cfg(feature = "msvc")] |
| 118 | Flavorrust-analyzer cargo implib 0.3.3 Flavor#::Msvcrust-analyzer cargo implib 0.3.3 Flavor#Msvc# => MsvcImportLibraryrust-analyzer cargo implib 0.3.3 msvc/MsvcImportLibrary#::newrust-analyzer cargo implib 0.3.3 msvc/impl#[MsvcImportLibrary]new().(self.defrust-analyzer cargo implib 0.3.3 ImportLibrary#def., self.machinerust-analyzer cargo implib 0.3.3 ImportLibrary#machine.).write_torust-analyzer cargo implib 0.3.3 msvc/impl#[MsvcImportLibrary]write_to().(writer), |
| 119 | #[cfg(not(feature = "msvc"))] |
| 120 | Flavorrust-analyzer cargo implib 0.3.3 Flavor#::Msvcrust-analyzer cargo implib 0.3.3 Flavor#Msvc# => Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::new( |
| 121 | ErrorKind::Unsupported, |
| 122 | "MSVC import library unsupported, enable 'msvc' feature to use it", |
| 123 | )), |
| 124 | #[cfg(feature = "gnu")] |
| 125 | Flavorrust-analyzer cargo implib 0.3.3 Flavor#::Gnurust-analyzer cargo implib 0.3.3 Flavor#Gnu# => GnuImportLibraryrust-analyzer cargo implib 0.3.3 gnu/GnuImportLibrary#::newrust-analyzer cargo implib 0.3.3 gnu/impl#[GnuImportLibrary]new().(self.defrust-analyzer cargo implib 0.3.3 ImportLibrary#def., self.machinerust-analyzer cargo implib 0.3.3 ImportLibrary#machine.).write_torust-analyzer cargo implib 0.3.3 gnu/impl#[GnuImportLibrary]write_to().(writer), |
| 126 | #[cfg(not(feature = "gnu"))] |
| 127 | Flavorrust-analyzer cargo implib 0.3.3 Flavor#::Gnurust-analyzer cargo implib 0.3.3 Flavor#Gnu# => Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::new( |
| 128 | ErrorKind::Unsupported, |
| 129 | "GNU import library unsupported, enable 'gnu' feature to use it", |
| 130 | )), |
| 131 | } |
| 132 | } |
| 133 | } |