Skip to content
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

write/xcoff: add support. #482

Merged
merged 15 commits into from
Mar 27, 2023
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "object"
version = "0.30.0"
edition = "2018"
exclude = ["/.github", "/testfiles"]
keywords = ["object", "elf", "mach-o", "pe", "coff"]
keywords = ["object", "elf", "mach-o", "pe", "coff", "xcoff"]
license = "Apache-2.0 OR MIT"
repository = "https://github.com/gimli-rs/object"
description = "A unified interface for reading and writing object file formats."
Expand Down Expand Up @@ -38,7 +38,7 @@ write_core = ["crc32fast", "indexmap", "hashbrown"]
# Core write support with libstd features. You will need to enable some file formats too.
write_std = ["write_core", "std", "indexmap/std", "crc32fast/std"]
# Write support for all file formats, including libstd features.
write = ["write_std", "coff", "elf", "macho", "pe"]
write = ["write_std", "coff", "elf", "macho", "pe", "xcoff"]

#=======================================
# Misc features.
Expand Down
30 changes: 17 additions & 13 deletions src/read/xcoff/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,19 +313,23 @@ impl<'data, 'file, Xcoff: FileHeader, R: ReadRef<'data>> ObjectSymbol<'data>
}

fn kind(&self) -> SymbolKind {
match self.symbol.n_sclass() {
xcoff::C_FILE => SymbolKind::File,
xcoff::C_NULL => SymbolKind::Null,
_ => self
.file
.section_by_index(SectionIndex((self.symbol.n_scnum() - 1) as usize))
.map(|section| match section.kind() {
SectionKind::Data | SectionKind::UninitializedData => SymbolKind::Data,
SectionKind::UninitializedTls | SectionKind::Tls => SymbolKind::Tls,
SectionKind::Text => SymbolKind::Text,
_ => SymbolKind::Unknown,
})
.unwrap_or(SymbolKind::Unknown),
if self.symbol.has_aux_csect() {
SymbolKind::Section
} else {
match self.symbol.n_sclass() {
xcoff::C_FILE => SymbolKind::File,
xcoff::C_NULL => SymbolKind::Null,
_ => self
.file
.section_by_index(SectionIndex((self.symbol.n_scnum() - 1) as usize))
.map(|section| match section.kind() {
SectionKind::Data | SectionKind::UninitializedData => SymbolKind::Data,
SectionKind::UninitializedTls | SectionKind::Tls => SymbolKind::Tls,
SectionKind::Text => SymbolKind::Text,
_ => SymbolKind::Unknown,
})
.unwrap_or(SymbolKind::Unknown),
}
}
}

Expand Down
16 changes: 14 additions & 2 deletions src/write/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ mod macho;
#[cfg(feature = "pe")]
pub mod pe;

#[cfg(feature = "xcoff")]
mod xcoff;

mod string;
pub use string::StringId;

Expand Down Expand Up @@ -218,6 +221,8 @@ impl<'a> Object<'a> {
BinaryFormat::Elf => self.elf_section_info(section),
#[cfg(feature = "macho")]
BinaryFormat::MachO => self.macho_section_info(section),
#[cfg(feature = "xcoff")]
BinaryFormat::Xcoff => self.xcoff_section_info(section),
_ => unimplemented!(),
}
}
Expand All @@ -243,7 +248,7 @@ impl<'a> Object<'a> {

fn has_subsections_via_symbols(&self) -> bool {
match self.format {
BinaryFormat::Coff | BinaryFormat::Elf => false,
BinaryFormat::Coff | BinaryFormat::Elf | BinaryFormat::Xcoff => false,
BinaryFormat::MachO => true,
_ => unimplemented!(),
}
Expand Down Expand Up @@ -506,6 +511,8 @@ impl<'a> Object<'a> {
BinaryFormat::Elf => self.elf_fixup_relocation(&mut relocation)?,
#[cfg(feature = "macho")]
BinaryFormat::MachO => self.macho_fixup_relocation(&mut relocation),
#[cfg(feature = "xcoff")]
BinaryFormat::Xcoff => self.xcoff_fixup_relocation(&mut relocation),
_ => unimplemented!(),
};
if addend != 0 {
Expand Down Expand Up @@ -574,6 +581,8 @@ impl<'a> Object<'a> {
BinaryFormat::Elf => self.elf_write(buffer),
#[cfg(feature = "macho")]
BinaryFormat::MachO => self.macho_write(buffer),
#[cfg(feature = "xcoff")]
BinaryFormat::Xcoff => self.xcoff_write(buffer),
_ => unimplemented!(),
}
}
Expand Down Expand Up @@ -894,6 +903,8 @@ pub enum Mangling {
Elf,
/// Mach-O symbol mangling.
MachO,
/// Xcoff symbol mangling.
Xcoff,
}

impl Mangling {
Expand All @@ -904,14 +915,15 @@ impl Mangling {
(BinaryFormat::Coff, _) => Mangling::Coff,
(BinaryFormat::Elf, _) => Mangling::Elf,
(BinaryFormat::MachO, _) => Mangling::MachO,
(BinaryFormat::Xcoff, _) => Mangling::Xcoff,
_ => Mangling::None,
}
}

/// Return the prefix to use for global symbols.
pub fn global_prefix(self) -> Option<u8> {
match self {
Mangling::None | Mangling::Elf | Mangling::Coff => None,
Mangling::None | Mangling::Elf | Mangling::Coff | Mangling::Xcoff => None,
Mangling::CoffI386 | Mangling::MachO => Some(b'_'),
}
}
Expand Down
Loading