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

Build a .drectve section in COFF objects with export directives #423

Merged
merged 1 commit into from
Jan 17, 2022
Merged
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
38 changes: 38 additions & 0 deletions src/write/coff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ struct SymbolOffsets {
aux_count: u8,
}

/// Internal format to use for the `.drectve` section containing linker
/// directives for symbol exports.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum CoffExportStyle {
/// MSVC format supported by link.exe and LLD.
Msvc,
/// Gnu format supported by GNU LD and LLD.
Gnu,
}

impl<'a> Object<'a> {
pub(crate) fn coff_section_info(
&self,
Expand Down Expand Up @@ -137,6 +147,34 @@ impl<'a> Object<'a> {
stub_id
}

/// Appends linker directives to the `.drectve` section to tell the linker
/// to export all symbols with `SymbolScope::Dynamic`.
///
/// This must be called after all symbols have been defined.
pub fn add_coff_exports(&mut self, style: CoffExportStyle) {
assert_eq!(self.format, BinaryFormat::Coff);

let mut directives = vec![];
for symbol in &self.symbols {
if symbol.scope == SymbolScope::Dynamic {
match style {
CoffExportStyle::Msvc => directives.extend(b" /EXPORT:\""),
CoffExportStyle::Gnu => directives.extend(b" -export:\""),
}
directives.extend(&symbol.name);
directives.extend(b"\"");
if symbol.kind != SymbolKind::Text {
match style {
CoffExportStyle::Msvc => directives.extend(b",DATA"),
CoffExportStyle::Gnu => directives.extend(b",data"),
}
}
}
}
let drectve = self.add_section(vec![], b".drectve".to_vec(), SectionKind::Linker);
self.append_section_data(drectve, &directives, 1);
}

pub(crate) fn coff_write(&self, buffer: &mut dyn WritableBuffer) -> Result<()> {
// Calculate offsets of everything, and build strtab.
let mut offset = 0;
Expand Down
2 changes: 2 additions & 0 deletions src/write/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ use crate::{

#[cfg(feature = "coff")]
mod coff;
#[cfg(feature = "coff")]
pub use coff::CoffExportStyle;

#[cfg(feature = "elf")]
pub mod elf;
Expand Down