Skip to content

Commit

Permalink
[ld] add --print-* for diagnostics
Browse files Browse the repository at this point in the history
This adds the following for passthrough to lld:
- `--print-gc-sections`
- `--print-icf-sections`
- `--print-map`

I am not adding these to the cache manifest, since it does not change
the produced artifacts.

Tested with an example from ziglang#11398: it successfully prints the resulting
map and the GC'd sections.
  • Loading branch information
motiejus committed Jun 26, 2022
1 parent 08459ff commit 9f313cd
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Compilation.zig
Expand Up @@ -845,6 +845,9 @@ pub const InitOptions = struct {
linker_shared_memory: bool = false,
linker_global_base: ?u64 = null,
linker_export_symbol_names: []const []const u8 = &.{},
linker_print_gc_sections: bool = false,
linker_print_icf_sections: bool = false,
linker_print_map: bool = false,
each_lib_rpath: ?bool = null,
build_id: ?bool = null,
disable_c_depfile: bool = false,
Expand Down Expand Up @@ -1694,6 +1697,9 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
.shared_memory = options.linker_shared_memory,
.global_base = options.linker_global_base,
.export_symbol_names = options.linker_export_symbol_names,
.print_gc_sections = options.linker_print_gc_sections,
.print_icf_sections = options.linker_print_icf_sections,
.print_map = options.linker_print_map,
.z_nodelete = options.linker_z_nodelete,
.z_notext = options.linker_z_notext,
.z_defs = options.linker_z_defs,
Expand Down
3 changes: 3 additions & 0 deletions src/link.zig
Expand Up @@ -159,6 +159,9 @@ pub const Options = struct {
version_script: ?[]const u8,
soname: ?[]const u8,
llvm_cpu_features: ?[*:0]const u8,
print_gc_sections: bool,
print_icf_sections: bool,
print_map: bool,

objects: []Compilation.LinkObject,
framework_dirs: []const []const u8,
Expand Down
12 changes: 12 additions & 0 deletions src/link/Elf.zig
Expand Up @@ -1475,6 +1475,18 @@ fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !v
try argv.append("--gc-sections");
}

if (self.base.options.print_gc_sections) {
try argv.append("--print-gc-sections");
}

if (self.base.options.print_icf_sections) {
try argv.append("--print-icf-sections");
}

if (self.base.options.print_map) {
try argv.append("--print-map");
}

if (self.base.options.eh_frame_hdr) {
try argv.append("--eh-frame-hdr");
}
Expand Down
12 changes: 12 additions & 0 deletions src/main.zig
Expand Up @@ -656,6 +656,9 @@ fn buildOutputType(
var linker_max_memory: ?u64 = null;
var linker_shared_memory: bool = false;
var linker_global_base: ?u64 = null;
var linker_print_gc_sections: bool = false;
var linker_print_icf_sections: bool = false;
var linker_print_map: bool = false;
var linker_z_nodelete = false;
var linker_z_notext = false;
var linker_z_defs = false;
Expand Down Expand Up @@ -1704,6 +1707,12 @@ fn buildOutputType(
linker_gc_sections = true;
} else if (mem.eql(u8, arg, "--no-gc-sections")) {
linker_gc_sections = false;
} else if (mem.eql(u8, arg, "--print-gc-sections")) {
linker_print_gc_sections = true;
} else if (mem.eql(u8, arg, "--print-icf-sections")) {
linker_print_icf_sections = true;
} else if (mem.eql(u8, arg, "--print-map")) {
linker_print_map = true;
} else if (mem.eql(u8, arg, "--allow-shlib-undefined") or
mem.eql(u8, arg, "-allow-shlib-undefined"))
{
Expand Down Expand Up @@ -2765,6 +2774,9 @@ fn buildOutputType(
.linker_initial_memory = linker_initial_memory,
.linker_max_memory = linker_max_memory,
.linker_shared_memory = linker_shared_memory,
.linker_print_gc_sections = linker_print_gc_sections,
.linker_print_icf_sections = linker_print_icf_sections,
.linker_print_map = linker_print_map,
.linker_global_base = linker_global_base,
.linker_export_symbol_names = linker_export_symbol_names.items,
.linker_z_nodelete = linker_z_nodelete,
Expand Down

0 comments on commit 9f313cd

Please sign in to comment.