Skip to content

read/coff: fix has_aux_function for weak externals #772

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions crates/examples/src/readobj/pe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,8 @@ fn print_symbols<'data, Coff: CoffHeader>(
p.field_inline_string("Name", name);
});
}
} else if symbol.has_aux_function() {
}
if symbol.has_aux_function() {
if let Some(aux) = symbols.aux_function(index).print_err(p) {
p.group("ImageAuxSymbolFunction", |p| {
p.field("TagIndex", aux.tag_index.get(LE));
Expand All @@ -750,7 +751,8 @@ fn print_symbols<'data, Coff: CoffHeader>(
p.field("Unused", format!("{:X?}", aux.unused));
});
}
} else if symbol.has_aux_section() {
}
if symbol.has_aux_section() {
if let Some(aux) = symbols.aux_section(index).print_err(p) {
p.group("ImageAuxSymbolSection", |p| {
p.field_hex("Length", aux.length.get(LE));
Expand All @@ -764,8 +766,24 @@ fn print_symbols<'data, Coff: CoffHeader>(
});
}
}
if symbol.has_aux_weak_external() {
if let Some(aux) = symbols.aux_weak_external(index).print_err(p) {
p.group("ImageAuxWeak", |p| {
let index = aux.default_symbol();
let name = symbols
.symbol(index)
.and_then(|symbol| symbol.name(symbols.strings()))
.print_err(p);
p.field_string_option("DefaultSymbol", index.0, name);
p.field_enum(
"SearchType",
aux.weak_search_type.get(LE),
FLAGS_IMAGE_WEAK_EXTERN,
);
});
}
}
// TODO: ImageAuxSymbolFunctionBeginEnd
// TODO: ImageAuxSymbolWeak
});
}
}
Expand Down Expand Up @@ -1249,6 +1267,12 @@ const FLAGS_IMAGE_COMDAT_SELECT: &[Flag<u8>] = &flags!(
IMAGE_COMDAT_SELECT_LARGEST,
IMAGE_COMDAT_SELECT_NEWEST,
);
const FLAGS_IMAGE_WEAK_EXTERN: &[Flag<u32>] = &flags!(
IMAGE_WEAK_EXTERN_SEARCH_NOLIBRARY,
IMAGE_WEAK_EXTERN_SEARCH_LIBRARY,
IMAGE_WEAK_EXTERN_SEARCH_ALIAS,
IMAGE_WEAK_EXTERN_ANTI_DEPENDENCY,
);
const FLAGS_IMAGE_SUBSYSTEM: &[Flag<u16>] = &flags!(
IMAGE_SUBSYSTEM_UNKNOWN,
IMAGE_SUBSYSTEM_NATIVE,
Expand Down
39 changes: 39 additions & 0 deletions crates/examples/testfiles/pe/weak-extern.o.objdump
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
Format: Coff Little-endian 32-bit
Kind: Relocatable
Architecture: X86_64
Flags: Coff { characteristics: 4 }
Relative Address Base: 0
Entry Address: 0
Segment { name: ".text", address: 0, size: 0 }
Segment { name: ".data", address: 0, size: 0 }
Segment { name: ".bss", address: 0, size: 0 }
Segment { name: ".xdata", address: 0, size: 0 }
Segment { name: ".pdata", address: 0, size: 0 }
Segment { name: ".rdata$zzz", address: 0, size: 0 }
1: Section { name: ".text", address: 0, size: 10, align: 10, kind: Text, flags: Coff { characteristics: 60500020 } }
2: Section { name: ".data", address: 0, size: 0, align: 10, kind: Data, flags: Coff { characteristics: c0500040 } }
3: Section { name: ".bss", address: 0, size: 0, align: 10, kind: UninitializedData, flags: Coff { characteristics: c0500080 } }
4: Section { name: ".xdata", address: 0, size: 8, align: 4, kind: ReadOnlyData, flags: Coff { characteristics: 40300040 } }
5: Section { name: ".pdata", address: 0, size: c, align: 4, kind: ReadOnlyData, flags: Coff { characteristics: 40300040 } }
6: Section { name: ".rdata$zzz", address: 0, size: 40, align: 10, kind: ReadOnlyData, flags: Coff { characteristics: 40500040 } }

Symbols
0: Symbol { name: "weak-extern.c", address: 0, size: 0, kind: File, section: None, scope: Compilation, weak: false, flags: None }
2: Symbol { name: ".text", address: 0, size: 7, kind: Section, section: Section(SectionIndex(1)), scope: Compilation, weak: false, flags: CoffSection { selection: 0, associative_section: None } }
4: Symbol { name: ".data", address: 0, size: 0, kind: Section, section: Section(SectionIndex(2)), scope: Compilation, weak: false, flags: CoffSection { selection: 0, associative_section: None } }
6: Symbol { name: ".bss", address: 0, size: 0, kind: Section, section: Section(SectionIndex(3)), scope: Compilation, weak: false, flags: CoffSection { selection: 0, associative_section: None } }
8: Symbol { name: ".xdata", address: 0, size: 8, kind: Section, section: Section(SectionIndex(4)), scope: Compilation, weak: false, flags: CoffSection { selection: 0, associative_section: None } }
10: Symbol { name: ".pdata", address: 0, size: c, kind: Section, section: Section(SectionIndex(5)), scope: Compilation, weak: false, flags: CoffSection { selection: 0, associative_section: None } }
12: Symbol { name: ".rdata$zzz", address: 0, size: 38, kind: Section, section: Section(SectionIndex(6)), scope: Compilation, weak: false, flags: CoffSection { selection: 0, associative_section: None } }
14: Symbol { name: ".weak.weak_symbol.", address: 0, size: 0, kind: Data, section: Section(SectionIndex(1)), scope: Linkage, weak: false, flags: None }
15: Symbol { name: "weak_symbol", address: 0, size: 0, kind: Text, section: Unknown, scope: Linkage, weak: true, flags: None }

.pdata relocations
(0, Relocation { kind: ImageOffset, encoding: Generic, size: 20, target: Symbol(SymbolIndex(2)), addend: 0, implicit_addend: true, flags: Coff { typ: 3 } })
(4, Relocation { kind: ImageOffset, encoding: Generic, size: 20, target: Symbol(SymbolIndex(2)), addend: 0, implicit_addend: true, flags: Coff { typ: 3 } })
(8, Relocation { kind: ImageOffset, encoding: Generic, size: 20, target: Symbol(SymbolIndex(8)), addend: 0, implicit_addend: true, flags: Coff { typ: 3 } })

Dynamic symbols

Symbol map
0x0 ".weak.weak_symbol."
291 changes: 291 additions & 0 deletions crates/examples/testfiles/pe/weak-extern.o.readobj
Original file line number Diff line number Diff line change
@@ -0,0 +1,291 @@
Format: COFF
ImageFileHeader {
Machine: IMAGE_FILE_MACHINE_AMD64 (0x8664)
NumberOfSections: 6
TimeDateStamp: 0
PointerToSymbolTable: 0x186
NumberOfSymbols: 17
SizeOfOptionalHeader: 0x0
Characteristics: 0x4
IMAGE_FILE_LINE_NUMS_STRIPPED (0x4)
}
ImageSectionHeader {
Index: 1
Name: ".text"
VirtualSize: 0x0
VirtualAddress: 0x0
SizeOfRawData: 0x10
PointerToRawData: 0x104
PointerToRelocations: 0x0
PointerToLinenumbers: 0x0
NumberOfRelocations: 0
NumberOfLinenumbers: 0
Characteristics: 0x60500020
IMAGE_SCN_CNT_CODE (0x20)
IMAGE_SCN_MEM_EXECUTE (0x20000000)
IMAGE_SCN_MEM_READ (0x40000000)
IMAGE_SCN_ALIGN_16BYTES (0x500000)
}
ImageSectionHeader {
Index: 2
Name: ".data"
VirtualSize: 0x0
VirtualAddress: 0x0
SizeOfRawData: 0x0
PointerToRawData: 0x0
PointerToRelocations: 0x0
PointerToLinenumbers: 0x0
NumberOfRelocations: 0
NumberOfLinenumbers: 0
Characteristics: 0xC0500040
IMAGE_SCN_CNT_INITIALIZED_DATA (0x40)
IMAGE_SCN_MEM_READ (0x40000000)
IMAGE_SCN_MEM_WRITE (0x80000000)
IMAGE_SCN_ALIGN_16BYTES (0x500000)
}
ImageSectionHeader {
Index: 3
Name: ".bss"
VirtualSize: 0x0
VirtualAddress: 0x0
SizeOfRawData: 0x0
PointerToRawData: 0x0
PointerToRelocations: 0x0
PointerToLinenumbers: 0x0
NumberOfRelocations: 0
NumberOfLinenumbers: 0
Characteristics: 0xC0500080
IMAGE_SCN_CNT_UNINITIALIZED_DATA (0x80)
IMAGE_SCN_MEM_READ (0x40000000)
IMAGE_SCN_MEM_WRITE (0x80000000)
IMAGE_SCN_ALIGN_16BYTES (0x500000)
}
ImageSectionHeader {
Index: 4
Name: ".xdata"
VirtualSize: 0x0
VirtualAddress: 0x0
SizeOfRawData: 0x8
PointerToRawData: 0x114
PointerToRelocations: 0x0
PointerToLinenumbers: 0x0
NumberOfRelocations: 0
NumberOfLinenumbers: 0
Characteristics: 0x40300040
IMAGE_SCN_CNT_INITIALIZED_DATA (0x40)
IMAGE_SCN_MEM_READ (0x40000000)
IMAGE_SCN_ALIGN_4BYTES (0x300000)
}
ImageSectionHeader {
Index: 5
Name: ".pdata"
VirtualSize: 0x0
VirtualAddress: 0x0
SizeOfRawData: 0xC
PointerToRawData: 0x11C
PointerToRelocations: 0x168
PointerToLinenumbers: 0x0
NumberOfRelocations: 3
NumberOfLinenumbers: 0
Characteristics: 0x40300040
IMAGE_SCN_CNT_INITIALIZED_DATA (0x40)
IMAGE_SCN_MEM_READ (0x40000000)
IMAGE_SCN_ALIGN_4BYTES (0x300000)
ImageRelocation {
VirtualAddress: 0x0
Symbol: ".text" (0x2)
Type: IMAGE_REL_AMD64_ADDR32NB (0x3)
}
ImageRelocation {
VirtualAddress: 0x4
Symbol: ".text" (0x2)
Type: IMAGE_REL_AMD64_ADDR32NB (0x3)
}
ImageRelocation {
VirtualAddress: 0x8
Symbol: ".xdata" (0x8)
Type: IMAGE_REL_AMD64_ADDR32NB (0x3)
}
}
ImageSectionHeader {
Index: 6
Name: ".rdata$zzz"
VirtualSize: 0x0
VirtualAddress: 0x0
SizeOfRawData: 0x40
PointerToRawData: 0x128
PointerToRelocations: 0x0
PointerToLinenumbers: 0x0
NumberOfRelocations: 0
NumberOfLinenumbers: 0
Characteristics: 0x40500040
IMAGE_SCN_CNT_INITIALIZED_DATA (0x40)
IMAGE_SCN_MEM_READ (0x40000000)
IMAGE_SCN_ALIGN_16BYTES (0x500000)
}
ImageSymbol {
Index: 0
Name: ".file"
Value: 0x0
Section: IMAGE_SYM_DEBUG (-2)
Type: 0x0
BaseType: IMAGE_SYM_TYPE_NULL (0x0)
DerivedType: IMAGE_SYM_DTYPE_NULL (0x0)
StorageClass: IMAGE_SYM_CLASS_FILE (0x67)
NumberOfAuxSymbols: 0x1
ImageAuxSymbolFile {
Name: "weak-extern.c"
}
}
ImageSymbol {
Index: 2
Name: ".text"
Value: 0x0
Section: ".text" (0x1)
Type: 0x0
BaseType: IMAGE_SYM_TYPE_NULL (0x0)
DerivedType: IMAGE_SYM_DTYPE_NULL (0x0)
StorageClass: IMAGE_SYM_CLASS_STATIC (0x3)
NumberOfAuxSymbols: 0x1
ImageAuxSymbolSection {
Length: 0x7
NumberOfRelocations: 0
NumberOfLinenumbers: 0
CheckSum: 0x0
Number: 0
Selection: 0x0
Reserved: 0x0
HighNumber: 0
}
}
ImageSymbol {
Index: 4
Name: ".data"
Value: 0x0
Section: ".data" (0x2)
Type: 0x0
BaseType: IMAGE_SYM_TYPE_NULL (0x0)
DerivedType: IMAGE_SYM_DTYPE_NULL (0x0)
StorageClass: IMAGE_SYM_CLASS_STATIC (0x3)
NumberOfAuxSymbols: 0x1
ImageAuxSymbolSection {
Length: 0x0
NumberOfRelocations: 0
NumberOfLinenumbers: 0
CheckSum: 0x0
Number: 0
Selection: 0x0
Reserved: 0x0
HighNumber: 0
}
}
ImageSymbol {
Index: 6
Name: ".bss"
Value: 0x0
Section: ".bss" (0x3)
Type: 0x0
BaseType: IMAGE_SYM_TYPE_NULL (0x0)
DerivedType: IMAGE_SYM_DTYPE_NULL (0x0)
StorageClass: IMAGE_SYM_CLASS_STATIC (0x3)
NumberOfAuxSymbols: 0x1
ImageAuxSymbolSection {
Length: 0x0
NumberOfRelocations: 0
NumberOfLinenumbers: 0
CheckSum: 0x0
Number: 0
Selection: 0x0
Reserved: 0x0
HighNumber: 0
}
}
ImageSymbol {
Index: 8
Name: ".xdata"
Value: 0x0
Section: ".xdata" (0x4)
Type: 0x0
BaseType: IMAGE_SYM_TYPE_NULL (0x0)
DerivedType: IMAGE_SYM_DTYPE_NULL (0x0)
StorageClass: IMAGE_SYM_CLASS_STATIC (0x3)
NumberOfAuxSymbols: 0x1
ImageAuxSymbolSection {
Length: 0x8
NumberOfRelocations: 0
NumberOfLinenumbers: 0
CheckSum: 0x0
Number: 0
Selection: 0x0
Reserved: 0x0
HighNumber: 0
}
}
ImageSymbol {
Index: 10
Name: ".pdata"
Value: 0x0
Section: ".pdata" (0x5)
Type: 0x0
BaseType: IMAGE_SYM_TYPE_NULL (0x0)
DerivedType: IMAGE_SYM_DTYPE_NULL (0x0)
StorageClass: IMAGE_SYM_CLASS_STATIC (0x3)
NumberOfAuxSymbols: 0x1
ImageAuxSymbolSection {
Length: 0xC
NumberOfRelocations: 3
NumberOfLinenumbers: 0
CheckSum: 0x0
Number: 0
Selection: 0x0
Reserved: 0x0
HighNumber: 0
}
}
ImageSymbol {
Index: 12
Name: ".rdata$zzz"
Value: 0x0
Section: ".rdata$zzz" (0x6)
Type: 0x0
BaseType: IMAGE_SYM_TYPE_NULL (0x0)
DerivedType: IMAGE_SYM_DTYPE_NULL (0x0)
StorageClass: IMAGE_SYM_CLASS_STATIC (0x3)
NumberOfAuxSymbols: 0x1
ImageAuxSymbolSection {
Length: 0x38
NumberOfRelocations: 0
NumberOfLinenumbers: 0
CheckSum: 0x0
Number: 0
Selection: 0x0
Reserved: 0x0
HighNumber: 0
}
}
ImageSymbol {
Index: 14
Name: ".weak.weak_symbol."
Value: 0x0
Section: ".text" (0x1)
Type: 0x0
BaseType: IMAGE_SYM_TYPE_NULL (0x0)
DerivedType: IMAGE_SYM_DTYPE_NULL (0x0)
StorageClass: IMAGE_SYM_CLASS_EXTERNAL (0x2)
NumberOfAuxSymbols: 0x0
}
ImageSymbol {
Index: 15
Name: "weak_symbol"
Value: 0x0
Section: IMAGE_SYM_UNDEFINED (0)
Type: 0x20
BaseType: IMAGE_SYM_TYPE_NULL (0x0)
DerivedType: IMAGE_SYM_DTYPE_FUNCTION (0x2)
StorageClass: IMAGE_SYM_CLASS_WEAK_EXTERNAL (0x69)
NumberOfAuxSymbols: 0x1
ImageAuxWeak {
DefaultSymbol: ".weak.weak_symbol." (0xE)
SearchType: IMAGE_WEAK_EXTERN_SEARCH_NOLIBRARY (0x1)
}
}
Loading