Skip to content

Commit

Permalink
write: add add_coff_exports function to Object (gimli-rs#423)
Browse files Browse the repository at this point in the history
  • Loading branch information
Amanieu authored Jan 17, 2022
1 parent 4049c00 commit 2492ea5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
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

0 comments on commit 2492ea5

Please sign in to comment.