Skip to content

Commit 6ee7638

Browse files
committed
coff: undefined symbols use IMAGE_SYM_CLASS_EXTERNAL
1 parent 267199e commit 6ee7638

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

src/read/coff/symbol.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ pub(crate) fn parse_symbol<'data>(
169169
};
170170
let section = match section_number {
171171
pe::IMAGE_SYM_UNDEFINED => {
172-
if symbol.storage_class == pe::IMAGE_SYM_CLASS_EXTERNAL {
172+
if symbol.storage_class == pe::IMAGE_SYM_CLASS_EXTERNAL && value != 0 {
173173
SymbolSection::Common
174174
} else {
175175
SymbolSection::Undefined
@@ -189,9 +189,7 @@ pub(crate) fn parse_symbol<'data>(
189189
let weak = symbol.storage_class == pe::IMAGE_SYM_CLASS_WEAK_EXTERNAL;
190190
let scope = match symbol.storage_class {
191191
_ if section == SymbolSection::Undefined => SymbolScope::Unknown,
192-
pe::IMAGE_SYM_CLASS_EXTERNAL
193-
| pe::IMAGE_SYM_CLASS_EXTERNAL_DEF
194-
| pe::IMAGE_SYM_CLASS_WEAK_EXTERNAL => {
192+
pe::IMAGE_SYM_CLASS_EXTERNAL | pe::IMAGE_SYM_CLASS_WEAK_EXTERNAL => {
195193
// TODO: determine if symbol is exported
196194
SymbolScope::Linkage
197195
}

src/write/coff.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -468,8 +468,9 @@ impl Object {
468468
symbol.name().unwrap_or("")
469469
)));
470470
}
471-
SymbolSection::Undefined => coff::IMAGE_SYM_CLASS_EXTERNAL_DEF,
472-
SymbolSection::Common => coff::IMAGE_SYM_CLASS_EXTERNAL,
471+
SymbolSection::Undefined | SymbolSection::Common => {
472+
coff::IMAGE_SYM_CLASS_EXTERNAL
473+
}
473474
SymbolSection::Absolute | SymbolSection::Section(_) => {
474475
match symbol.scope {
475476
// TODO: does this need aux symbol records too?

tests/round_trip/common.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,19 @@ fn coff_x86_64_common() {
3333
};
3434
object.add_common_symbol(symbol, 8, 8);
3535

36+
// Also check undefined symbols, which are very similar.
37+
let symbol = write::Symbol {
38+
name: b"v3".to_vec(),
39+
value: 0,
40+
size: 0,
41+
kind: SymbolKind::Data,
42+
scope: SymbolScope::Linkage,
43+
weak: false,
44+
section: write::SymbolSection::Undefined,
45+
flags: SymbolFlags::None,
46+
};
47+
object.add_symbol(symbol);
48+
3649
let bytes = object.write().unwrap();
3750

3851
//std::fs::write(&"common.o", &bytes).unwrap();
@@ -65,6 +78,17 @@ fn coff_x86_64_common() {
6578
assert_eq!(symbol.address(), 0);
6679
assert_eq!(symbol.size(), 8);
6780

81+
let (_, symbol) = symbols.next().unwrap();
82+
println!("{:?}", symbol);
83+
assert_eq!(symbol.name(), Some("v3"));
84+
assert_eq!(symbol.kind(), SymbolKind::Data);
85+
assert_eq!(symbol.section(), read::SymbolSection::Undefined);
86+
assert_eq!(symbol.scope(), SymbolScope::Unknown);
87+
assert_eq!(symbol.is_weak(), false);
88+
assert_eq!(symbol.is_undefined(), true);
89+
assert_eq!(symbol.address(), 0);
90+
assert_eq!(symbol.size(), 0);
91+
6892
let symbol = symbols.next();
6993
assert!(symbol.is_none(), format!("unexpected symbol {:?}", symbol));
7094
}

0 commit comments

Comments
 (0)