Skip to content

Commit 7df6f41

Browse files
committed
rustc: Add a #[wasm_custom_section] attribute
This commit is an implementation of adding custom sections to wasm artifacts in rustc. The intention here is to expose the ability of the wasm binary format to contain custom sections with arbitrary user-defined data. Currently neither our version of LLVM nor LLD supports this so the implementation is currently custom to rustc itself. The implementation here is to attach a `#[wasm_custom_section = "foo"]` attribute to any `const` which has a type like `[u8; N]`. Other types of constants aren't supported yet but may be added one day! This should hopefully be enough to get off the ground with *some* custom section support. The current semantics are that any constant tagged with `#[wasm_custom_section]` section will be *appended* to the corresponding section in the final output wasm artifact (and this affects dependencies linked in as well, not just the final crate). This means that whatever is interpreting the contents must be able to interpret binary-concatenated sections (or each constant needs to be in its own custom section). To test this change the existing `run-make` test suite was moved to a `run-make-fulldeps` folder and a new `run-make` test suite was added which applies to all targets by default. This test suite currently only has one test which only runs for the wasm target (using a node.js script to use `WebAssembly` in JS to parse the wasm output).
1 parent 5092c6b commit 7df6f41

File tree

575 files changed

+522
-17
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

575 files changed

+522
-17
lines changed

src/bootstrap/builder.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ impl<'a> Builder<'a> {
313313
test::RunPassFullDepsPretty, test::RunFailFullDepsPretty,
314314
test::Crate, test::CrateLibrustc, test::CrateRustdoc, test::Linkcheck,
315315
test::Cargotest, test::Cargo, test::Rls, test::ErrorIndex, test::Distcheck,
316+
test::RunMakeFullDeps,
316317
test::Nomicon, test::Reference, test::RustdocBook, test::RustByExample,
317318
test::TheBook, test::UnstableBook,
318319
test::Rustfmt, test::Miri, test::Clippy, test::RustdocJS, test::RustdocTheme,

src/bootstrap/compile.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -915,7 +915,7 @@ impl Step for Assemble {
915915
}
916916
}
917917

918-
let lld_install = if build.config.lld_enabled && target_compiler.stage > 0 {
918+
let lld_install = if build.config.lld_enabled {
919919
Some(builder.ensure(native::Lld {
920920
target: target_compiler.host,
921921
}))

src/bootstrap/test.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -759,12 +759,18 @@ test!(RunFailFullDepsPretty {
759759
host: true
760760
});
761761

762-
host_test!(RunMake {
762+
default_test!(RunMake {
763763
path: "src/test/run-make",
764764
mode: "run-make",
765765
suite: "run-make"
766766
});
767767

768+
host_test!(RunMakeFullDeps {
769+
path: "src/test/run-make-fulldeps",
770+
mode: "run-make",
771+
suite: "run-make-fulldeps"
772+
});
773+
768774
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
769775
struct Compiletest {
770776
compiler: Compiler,
@@ -827,8 +833,7 @@ impl Step for Compiletest {
827833
// FIXME: Does pretty need librustc compiled? Note that there are
828834
// fulldeps test suites with mode = pretty as well.
829835
mode == "pretty" ||
830-
mode == "rustdoc" ||
831-
mode == "run-make" {
836+
mode == "rustdoc" {
832837
builder.ensure(compile::Rustc { compiler, target });
833838
}
834839

@@ -849,7 +854,7 @@ impl Step for Compiletest {
849854
cmd.arg("--rustc-path").arg(builder.rustc(compiler));
850855

851856
// Avoid depending on rustdoc when we don't need it.
852-
if mode == "rustdoc" || mode == "run-make" {
857+
if mode == "rustdoc" || (mode == "run-make" && suite.ends_with("fulldeps")) {
853858
cmd.arg("--rustdoc-path").arg(builder.rustdoc(compiler.host));
854859
}
855860

@@ -931,7 +936,7 @@ impl Step for Compiletest {
931936

932937
// Only pass correct values for these flags for the `run-make` suite as it
933938
// requires that a C++ compiler was configured which isn't always the case.
934-
if suite == "run-make" {
939+
if suite == "run-make-fulldeps" {
935940
let llvm_components = output(Command::new(&llvm_config).arg("--components"));
936941
let llvm_cxxflags = output(Command::new(&llvm_config).arg("--cxxflags"));
937942
cmd.arg("--cc").arg(build.cc(target))
@@ -944,12 +949,12 @@ impl Step for Compiletest {
944949
}
945950
}
946951
}
947-
if suite == "run-make" && !build.config.llvm_enabled {
952+
if suite == "run-make-fulldeps" && !build.config.llvm_enabled {
948953
println!("Ignoring run-make test suite as they generally don't work without LLVM");
949954
return;
950955
}
951956

952-
if suite != "run-make" {
957+
if suite != "run-make-fulldeps" {
953958
cmd.arg("--cc").arg("")
954959
.arg("--cxx").arg("")
955960
.arg("--cflags").arg("")

src/librustc/dep_graph/dep_node.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,8 @@ define_dep_nodes!( <'tcx>
650650

651651
[] GetSymbolExportLevel(DefId),
652652

653+
[] WasmCustomSections(CrateNum),
654+
653655
[input] Features,
654656

655657
[] ProgramClausesFor(DefId),

src/librustc/hir/check_attr.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ enum Target {
2525
Struct,
2626
Union,
2727
Enum,
28+
Const,
2829
Other,
2930
}
3031

@@ -35,6 +36,7 @@ impl Target {
3536
hir::ItemStruct(..) => Target::Struct,
3637
hir::ItemUnion(..) => Target::Union,
3738
hir::ItemEnum(..) => Target::Enum,
39+
hir::ItemConst(..) => Target::Const,
3840
_ => Target::Other,
3941
}
4042
}
@@ -60,6 +62,17 @@ impl<'a, 'tcx> CheckAttrVisitor<'a, 'tcx> {
6062
if name == "inline" {
6163
self.check_inline(attr, item, target)
6264
}
65+
66+
if name == "wasm_custom_section" {
67+
if target != Target::Const {
68+
self.tcx.sess.span_err(attr.span, "only allowed on consts");
69+
}
70+
71+
if attr.value_str().is_none() {
72+
self.tcx.sess.span_err(attr.span, "must be of the form \
73+
#[wasm_custom_section = \"foo\"]");
74+
}
75+
}
6376
}
6477
}
6578

src/librustc/middle/dead.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,11 @@ fn has_allow_dead_code_or_lang_attr(tcx: TyCtxt,
318318
return true;
319319
}
320320

321+
// These constants are special for wasm
322+
if attr::contains_name(attrs, "wasm_custom_section") {
323+
return true;
324+
}
325+
321326
tcx.lint_level_at_node(lint::builtin::DEAD_CODE, id).0 == lint::Allow
322327
}
323328

src/librustc/ty/maps/config.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,12 @@ impl<'tcx> QueryDescription<'tcx> for queries::instance_def_size_estimate<'tcx>
678678
}
679679
}
680680

681+
impl<'tcx> QueryDescription<'tcx> for queries::wasm_custom_sections<'tcx> {
682+
fn describe(_tcx: TyCtxt, _: CrateNum) -> String {
683+
format!("custom wasm sections for a crate")
684+
}
685+
}
686+
681687
impl<'tcx> QueryDescription<'tcx> for queries::generics_of<'tcx> {
682688
#[inline]
683689
fn cache_on_disk(def_id: Self::Key) -> bool {

src/librustc/ty/maps/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,8 @@ define_maps! { <'tcx>
424424
[] fn features_query: features_node(CrateNum) -> Lrc<feature_gate::Features>,
425425

426426
[] fn program_clauses_for: ProgramClausesFor(DefId) -> Lrc<Vec<Clause<'tcx>>>,
427+
428+
[] fn wasm_custom_sections: WasmCustomSections(CrateNum) -> Lrc<Vec<DefId>>,
427429
}
428430

429431
//////////////////////////////////////////////////////////////////////

src/librustc/ty/maps/plumbing.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -940,6 +940,7 @@ pub fn force_from_dep_node<'a, 'gcx, 'lcx>(tcx: TyCtxt<'a, 'gcx, 'lcx>,
940940
DepKind::Features => { force!(features_query, LOCAL_CRATE); }
941941

942942
DepKind::ProgramClausesFor => { force!(program_clauses_for, def_id!()); }
943+
DepKind::WasmCustomSections => { force!(wasm_custom_sections, krate!()); }
943944
}
944945

945946
true

src/librustc_metadata/cstore_impl.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,8 @@ provide! { <'tcx> tcx, def_id, other, cdata,
271271

272272
Arc::new(cdata.exported_symbols())
273273
}
274+
275+
wasm_custom_sections => { Lrc::new(cdata.wasm_custom_sections()) }
274276
}
275277

276278
pub fn provide<'tcx>(providers: &mut Providers<'tcx>) {

0 commit comments

Comments
 (0)