ziglang / zig

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
https://ziglang.org
MIT License
35.03k stars 2.56k forks source link

zig objcopy: support `--add-section` #19009

Closed leecannon closed 1 month ago

leecannon commented 9 months ago

I currently fork out to the system objcopy to insert a new section into a binary.

It would be nice if zig objcopy/std.Build.Step.ObjCopy supported this use case.

Status quo:

const run_objcopy = b.addSystemCommand(&.{"objcopy"});
run_objcopy.addArg("--add-section");
run_objcopy.addPrefixedFileArg(".sdf=", sdf_data_path);
run_objcopy.addArgs(&.{
    "--set-section-alignment", ".sdf=8",
});
run_objcopy.addArgs(&.{
    "--set-section-flags", ".sdf=contents,alloc,load,readonly,data",
});
run_objcopy.addFileArg(exe.getOutput());
const exe_with_extra_section = run_objcopy.addOutputFileArg(
    b.fmt("{s}", .{exe.out_filename}),
);
matklad commented 2 months ago

Would use this in TigerBeetle as well!

andrewrk commented 2 months ago

This is a contributor friendly issue, you don't even need to rebuild the compiler to work on it. Just edit lib/compiler/objcopy.zig and you're off to the races.

patrickwick commented 1 month ago

I should add here that I did not remove the pre-existing restriction that zig objdump currently only supports ELF input files with sections that are ordered by their increasing file offset, see: https://github.com/ziglang/zig/blob/73de620ad50121bacb0b9b376efd50b5444485b8/lib/compiler/objcopy.zig#L1125-L1135

Probably related #14717. I may create a PR to allow this some time.