Skip to content

Commit e44a502

Browse files
committed
Fix GNU import library for i386 architecture
1 parent 490aef9 commit e44a502

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

src/gnu.rs

+19-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::io::{Error, ErrorKind, Seek, Write};
22

33
use object::pe::*;
4-
use object::write::{Object, Relocation, Symbol, SymbolId, SymbolSection};
4+
use object::write::{Mangling, Object, Relocation, Symbol, SymbolId, SymbolSection};
55
use object::{
66
BinaryFormat, Endianness, SectionFlags, SectionKind, SymbolFlags, SymbolKind, SymbolScope,
77
};
@@ -179,9 +179,8 @@ impl<'a> ObjectFactory<'a> {
179179

180180
let import_name = self.import_name.replace('.', "_");
181181

182-
let head_sym_name = format!("_head_{}", import_name);
183182
let head_sym = Symbol {
184-
name: head_sym_name.as_bytes().to_vec(),
183+
name: format!("_head_{}", import_name).into_bytes(),
185184
value: 0,
186185
size: 0,
187186
kind: SymbolKind::Data,
@@ -190,7 +189,8 @@ impl<'a> ObjectFactory<'a> {
190189
section: SymbolSection::Section(id2),
191190
flags: SymbolFlags::None,
192191
};
193-
obj.add_symbol(head_sym);
192+
let head_sym_id = obj.add_symbol(head_sym);
193+
let head_sym_name = obj.symbol(head_sym_id).name.clone();
194194

195195
let iname_sym = Symbol {
196196
name: format!("{}_iname", import_name).into_bytes(),
@@ -212,7 +212,7 @@ impl<'a> ObjectFactory<'a> {
212212
data: obj
213213
.write()
214214
.map_err(|e| Error::new(ErrorKind::Other, e.to_string()))?,
215-
symbols: vec![head_sym_name],
215+
symbols: vec![String::from_utf8(head_sym_name).unwrap()],
216216
})
217217
}
218218

@@ -271,9 +271,8 @@ impl<'a> ObjectFactory<'a> {
271271
obj.add_file_symbol(b"fake".to_vec());
272272

273273
let import_name = self.import_name.replace('.', "_");
274-
let iname_sym_name = format!("{}_iname", import_name);
275274
let iname_sym = Symbol {
276-
name: iname_sym_name.as_bytes().to_vec(),
275+
name: format!("{}_iname", import_name).into_bytes(),
277276
value: 0,
278277
size: 0,
279278
kind: SymbolKind::Data,
@@ -282,7 +281,8 @@ impl<'a> ObjectFactory<'a> {
282281
section: SymbolSection::Section(id7),
283282
flags: SymbolFlags::None,
284283
};
285-
obj.add_symbol(iname_sym);
284+
let iname_sym_id = obj.add_symbol(iname_sym);
285+
let iname_sym_name = obj.symbol(iname_sym_id).name.clone();
286286

287287
obj.append_section_data(id4, &[0; 8], 4);
288288
obj.append_section_data(id5, &[0; 8], 4);
@@ -296,7 +296,7 @@ impl<'a> ObjectFactory<'a> {
296296
data: obj
297297
.write()
298298
.map_err(|e| Error::new(ErrorKind::Other, e.to_string()))?,
299-
symbols: vec![iname_sym_name],
299+
symbols: vec![String::from_utf8(iname_sym_name).unwrap()],
300300
})
301301
}
302302

@@ -368,6 +368,9 @@ impl<'a> ObjectFactory<'a> {
368368
};
369369
let head_sym = obj.add_symbol(head_sym);
370370

371+
// All subsequent symbols should be added unmangled.
372+
obj.mangling = Mangling::None;
373+
371374
let mut archive_symbols = Vec::new();
372375
if !export.data {
373376
let exp_sym = Symbol {
@@ -455,12 +458,17 @@ impl<'a> ObjectFactory<'a> {
455458
obj.append_section_data(id4, &id4_data, 4);
456459

457460
if !export.no_name {
458-
let len = 2 + export.name.len() + 1;
461+
// Remove i386 mangling added by the def parser.
462+
let export_name = match self.machine {
463+
MachineType::I386 => export.name.strip_prefix("_").unwrap(),
464+
_ => &export.name,
465+
};
466+
let len = 2 + export_name.len() + 1;
459467
let mut id6_data = vec![0; len];
460468
let ord = export.ordinal;
461469
id6_data[0] = ord as u8;
462470
id6_data[1] = (ord >> 8) as u8;
463-
id6_data[2..len - 1].copy_from_slice(export.name.as_bytes());
471+
id6_data[2..len - 1].copy_from_slice(export_name.as_bytes());
464472
obj.append_section_data(id6, &id6_data, 2);
465473
}
466474

0 commit comments

Comments
 (0)