@@ -89,25 +89,81 @@ pub(crate) struct ImportObjectHeaderUnaligned {
89
89
/// - have no padding
90
90
unsafe impl object:: pod:: Pod for ImportObjectHeaderUnaligned { }
91
91
92
- pub ( crate ) fn write_short_import (
93
- data : & mut DataWriter ,
94
- dll_name : & str ,
95
- name : & & str ,
96
- ordinal_or_hint : Option < std:: primitive:: u16 > ,
97
- ) {
92
+ /// The IMPORT_OBJECT_* constants used to find the exported value in the DLL.
93
+ pub ( crate ) enum ImportNameType {
94
+ /// No import name, import by ordinal only.
95
+ Ordinal ,
96
+ /// Import name == public symbol name.
97
+ Name ,
98
+ /// Import name == public symbol name skipping leading ?, @, or optionally _.
99
+ NameNoPrefix ,
100
+ /// Import name == public symbol name skipping leading ?, @, or optionally _ and truncating at first @.
101
+ NameUndecorate ,
102
+ /// Import name == a name is explicitly provided after the DLL name.
103
+ NameExportAs { export_name : String } ,
104
+ }
105
+
106
+ impl ImportNameType {
107
+ fn as_u16 ( & self ) -> u16 {
108
+ match self {
109
+ ImportNameType :: Ordinal => IMPORT_OBJECT_ORDINAL ,
110
+ ImportNameType :: Name => IMPORT_OBJECT_NAME ,
111
+ ImportNameType :: NameNoPrefix => IMPORT_OBJECT_NAME_NO_PREFIX ,
112
+ ImportNameType :: NameUndecorate => IMPORT_OBJECT_NAME_UNDECORATE ,
113
+ ImportNameType :: NameExportAs { .. } => IMPORT_OBJECT_NAME_EXPORTAS ,
114
+ }
115
+ }
116
+ }
117
+
118
+ /// The IMPORT_OBJECT_* constants that defines how the import is linked, or in the words of the PE
119
+ /// documentation:
120
+ /// > These values are used to determine which section contributions must be generated by the tool
121
+ /// > that uses the library if it must access that data.
122
+ pub ( crate ) enum ImportType {
123
+ Code ,
124
+ Data ,
125
+ Const ,
126
+ }
127
+
128
+ impl ImportType {
129
+ fn as_u16 ( & self ) -> u16 {
130
+ match self {
131
+ ImportType :: Code => IMPORT_OBJECT_CODE ,
132
+ ImportType :: Data => IMPORT_OBJECT_DATA ,
133
+ ImportType :: Const => IMPORT_OBJECT_CONST ,
134
+ }
135
+ }
136
+ }
137
+
138
+ pub ( crate ) struct Import {
139
+ pub ( crate ) symbol_name : String ,
140
+ pub ( crate ) name_type : ImportNameType ,
141
+ pub ( crate ) import_type : ImportType ,
142
+ pub ( crate ) ordinal_or_hint : Option < std:: primitive:: u16 > ,
143
+ }
144
+
145
+ pub ( crate ) fn write_short_import ( data : & mut DataWriter , dll_name : & str , import : & Import ) {
146
+ let mut size_of_data = import. symbol_name . len ( ) + 1 + dll_name. len ( ) + 1 ;
147
+ if let ImportNameType :: NameExportAs { export_name } = & import. name_type {
148
+ size_of_data += export_name. len ( ) + 1 ;
149
+ }
150
+
98
151
data. write_pod ( & ImportObjectHeaderUnaligned {
99
152
sig1 : u16 ( IMAGE_FILE_MACHINE_UNKNOWN ) ,
100
153
sig2 : u16 ( IMPORT_OBJECT_HDR_SIG2 ) ,
101
154
version : u16 ( 0 ) ,
102
155
machine : u16 ( IMAGE_FILE_MACHINE_AMD64 ) ,
103
156
time_date_stamp : u32 ( 0 ) ,
104
- size_of_data : u32 ( ( name . len ( ) + 1 + dll_name . len ( ) + 1 ) as u32 ) ,
105
- ordinal_or_hint : u16 ( ordinal_or_hint. unwrap_or_default ( ) ) ,
106
- name_type : u16 ( IMPORT_OBJECT_CODE << IMPORT_OBJECT_TYPE_SHIFT
107
- | IMPORT_OBJECT_NAME << IMPORT_OBJECT_NAME_SHIFT ) ,
157
+ size_of_data : u32 ( size_of_data as u32 ) ,
158
+ ordinal_or_hint : u16 ( import . ordinal_or_hint . unwrap_or_default ( ) ) ,
159
+ name_type : u16 ( import . import_type . as_u16 ( ) << IMPORT_OBJECT_TYPE_SHIFT
160
+ | import . name_type . as_u16 ( ) << IMPORT_OBJECT_NAME_SHIFT ) ,
108
161
} ) ;
109
- data. write_c_str ( name ) ;
162
+ data. write_c_str ( & import . symbol_name ) ;
110
163
data. write_c_str ( dll_name) ;
164
+ if let ImportNameType :: NameExportAs { export_name } = & import. name_type {
165
+ data. write_c_str ( & export_name) ;
166
+ }
111
167
}
112
168
113
169
pub ( crate ) fn write_import_descriptor (
0 commit comments