@@ -2635,6 +2635,7 @@ fn linkWithZld(wasm: *Wasm, comp: *Compilation, prog_node: *std.Progress.Node) l
2635
2635
try man .addOptionalFile (compiler_rt_path );
2636
2636
man .hash .addOptionalBytes (options .entry );
2637
2637
man .hash .addOptional (options .stack_size_override );
2638
+ man .hash .add (wasm .base .options .build_id );
2638
2639
man .hash .add (options .import_memory );
2639
2640
man .hash .add (options .import_table );
2640
2641
man .hash .add (options .export_table );
@@ -3225,6 +3226,12 @@ fn writeToFile(
3225
3226
}
3226
3227
3227
3228
if (! wasm .base .options .strip ) {
3229
+ // The build id must be computed on the main sections only,
3230
+ // so we have to do it now, before the debug sections.
3231
+ if (wasm .base .options .build_id ) {
3232
+ try emitBuildIdSection (& binary_bytes );
3233
+ }
3234
+
3228
3235
// if (wasm.dwarf) |*dwarf| {
3229
3236
// const mod = wasm.base.options.module.?;
3230
3237
// try dwarf.writeDbgAbbrev();
@@ -3363,6 +3370,33 @@ fn emitProducerSection(binary_bytes: *std.ArrayList(u8)) !void {
3363
3370
);
3364
3371
}
3365
3372
3373
+ fn emitBuildIdSection (binary_bytes : * std .ArrayList (u8 )) ! void {
3374
+ const header_offset = try reserveCustomSectionHeader (binary_bytes );
3375
+
3376
+ const writer = binary_bytes .writer ();
3377
+ const build_id = "build_id" ;
3378
+ try leb .writeULEB128 (writer , @intCast (u32 , build_id .len ));
3379
+ try writer .writeAll (build_id );
3380
+
3381
+ var id : [16 ]u8 = undefined ;
3382
+ std .crypto .hash .sha3 .TurboShake128 (null ).hash (binary_bytes .items , & id , .{});
3383
+ var uuid : [36 ]u8 = undefined ;
3384
+ _ = try std .fmt .bufPrint (& uuid , "{s}-{s}-{s}-{s}-{s}" , .{
3385
+ std .fmt .fmtSliceHexLower (id [0.. 4]), std .fmt .fmtSliceHexLower (id [4.. 6]), std .fmt .fmtSliceHexLower (id [6.. 8]),
3386
+ std .fmt .fmtSliceHexLower (id [8.. 10]), std .fmt .fmtSliceHexLower (id [10.. ]),
3387
+ });
3388
+
3389
+ try leb .writeULEB128 (writer , @as (u32 , 1 ));
3390
+ try leb .writeULEB128 (writer , @as (u32 , uuid .len ));
3391
+ try writer .writeAll (& uuid );
3392
+
3393
+ try writeCustomSectionHeader (
3394
+ binary_bytes .items ,
3395
+ header_offset ,
3396
+ @intCast (u32 , binary_bytes .items .len - header_offset - 6 ),
3397
+ );
3398
+ }
3399
+
3366
3400
fn emitFeaturesSection (binary_bytes : * std .ArrayList (u8 ), enabled_features : []const bool , features_count : u32 ) ! void {
3367
3401
const header_offset = try reserveCustomSectionHeader (binary_bytes );
3368
3402
@@ -3594,6 +3628,7 @@ fn linkWithLLD(wasm: *Wasm, comp: *Compilation, prog_node: *std.Progress.Node) !
3594
3628
try man .addOptionalFile (compiler_rt_path );
3595
3629
man .hash .addOptionalBytes (wasm .base .options .entry );
3596
3630
man .hash .addOptional (wasm .base .options .stack_size_override );
3631
+ man .hash .add (wasm .base .options .build_id );
3597
3632
man .hash .add (wasm .base .options .import_memory );
3598
3633
man .hash .add (wasm .base .options .import_table );
3599
3634
man .hash .add (wasm .base .options .export_table );
@@ -3760,6 +3795,12 @@ fn linkWithLLD(wasm: *Wasm, comp: *Compilation, prog_node: *std.Progress.Node) !
3760
3795
if (wasm .base .options .import_symbols ) {
3761
3796
try argv .append ("--allow-undefined" );
3762
3797
}
3798
+
3799
+ // XXX - TODO: add when wasm-ld supports --build-id.
3800
+ // if (wasm.base.options.build_id) {
3801
+ // try argv.append("--build-id=tree");
3802
+ // }
3803
+
3763
3804
try argv .appendSlice (&.{ "-o" , full_out_path });
3764
3805
3765
3806
if (target .cpu .arch == .wasm64 ) {
0 commit comments