Skip to content

Commit

Permalink
Don't emit empty DWARF sections
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda committed Nov 29, 2024
1 parent 27f6b62 commit ef9f500
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ harness = false

[dependencies]
anyhow = "1.0"
fallible-iterator = "0.2"
id-arena = "2.2.1"
leb128 = "0.2.4"
log = "0.4.8"
Expand Down
27 changes: 17 additions & 10 deletions src/module/debug/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod units;

use crate::emit::{Emit, EmitContext};
use crate::{CustomSection, Module, RawCustomSection};
use fallible_iterator::FallibleIterator;
use gimli::*;

use self::dwarf::{AddressSearchPreference, ConvertContext, DEAD_CODE};
Expand Down Expand Up @@ -42,6 +43,16 @@ impl Module {

impl Emit for ModuleDebugData {
fn emit(&self, cx: &mut EmitContext) {
let from_dwarf = cx
.module
.debug
.dwarf
.borrow(|sections| EndianSlice::new(sections.as_ref(), LittleEndian));

if let Ok(true) = from_dwarf.units().all(|unit| Ok(unit.unit_length() == 0)) {
return;
}

let address_generator = CodeAddressGenerator::new(&cx.module.funcs);
let address_converter = CodeAddressConverter::new(&cx.code_transform);

Expand All @@ -55,12 +66,6 @@ impl Emit for ModuleDebugData {
.map(write::Address::Constant)
};

let from_dwarf = cx
.module
.debug
.dwarf
.borrow(|sections| EndianSlice::new(sections.as_ref(), LittleEndian));

let mut dwarf = write::Dwarf::from(&from_dwarf, &|address| {
if address == 0 || address == DEAD_CODE {
Some(write::Address::Constant(address))
Expand Down Expand Up @@ -115,10 +120,12 @@ impl Emit for ModuleDebugData {
sections
.for_each(
|id: SectionId, data: &write::EndianVec<LittleEndian>| -> Result<()> {
cx.wasm_module.section(&wasm_encoder::CustomSection {
name: id.name().into(),
data: data.slice().into(),
});
if !data.slice().is_empty() {
cx.wasm_module.section(&wasm_encoder::CustomSection {
name: id.name().into(),
data: data.slice().into(),
});
}
Ok(())
},
)
Expand Down

0 comments on commit ef9f500

Please sign in to comment.