Skip to content

objcopy: -O elf --set-section-flags panics #22544

@jacobly0

Description

@jacobly0
$ cat hello.zig
const std = @import("std");

pub fn main() !void {
    const stdout = std.io.getStdOut().writer();
    try stdout.print("Hello, {s}!\n", .{"world"});
}
$ zig build-exe hello.zig
$ ZIG_DEBUG_CMD=1 zig objcopy -O elf hello hello2 --set-section-flags .bss=alloc,contents
thread 18714 panic: attempt to use null value
/home/jacob/prg/zig/3/lib/compiler/objcopy.zig:1299:98: 0x10bd3b2 in emit (objcopy)
                    const section_name = std.mem.span(@as([*:0]const u8, @ptrCast(&strtab.payload.?[section.sh_name])));
                                                                                                 ^
/home/jacob/prg/zig/3/lib/compiler/objcopy.zig:811:30: 0x10c5532 in stripElf (objcopy)
            try elf_file.emit(allocator, out_file, in_file, .{
                             ^
/home/jacob/prg/zig/3/lib/compiler/objcopy.zig:211:25: 0x10cfa6f in cmdObjCopy (objcopy)
            try stripElf(arena, in_file, out_file, elf_hdr, .{
                        ^
/home/jacob/prg/zig/3/lib/compiler/objcopy.zig:22:22: 0x10d15c4 in main (objcopy)
    return cmdObjCopy(gpa, arena, args[1..]);
                     ^
/home/jacob/prg/zig/3/lib/std/start.zig:656:37: 0x10ae572 in posixCallMainAndExit (objcopy)
            const result = root.main() catch |err| {
                                    ^
/home/jacob/prg/zig/3/lib/std/start.zig:271:5: 0x10ae15d in _start (objcopy)
    asm volatile (switch (native_arch) {
    ^
???:?:?: 0x0 in ??? (???)
Aborted (core dumped)

Potential fix:

--- a/lib/compiler/objcopy.zig
+++ b/lib/compiler/objcopy.zig
@@ -1279,9 +1279,10 @@ fn ElfFile(comptime is_64: bool) type {
                 if (self.raw_elf_header.e_shstrndx == elf.SHN_UNDEF)
                     fatal("zig objcopy: no strtab, cannot add the user section", .{}); // TODO add the section if needed?
 
-                const strtab = &sections_update[self.raw_elf_header.e_shstrndx];
+                const strtab = sections_update[self.raw_elf_header.e_shstrndx].payload orelse
+                    self.sections[self.raw_elf_header.e_shstrndx].payload;
                 for (updated_section_header) |*section| {
-                    const section_name = std.mem.span(@as([*:0]const u8, @ptrCast(&strtab.payload.?[section.sh_name])));
+                    const section_name = std.mem.span(@as([*:0]const u8, @ptrCast(&strtab.?[section.sh_name])));
                     if (std.mem.eql(u8, section_name, set_align.section_name)) {
                         section.sh_addralign = set_align.alignment;
                         break;
@@ -1294,9 +1295,10 @@ fn ElfFile(comptime is_64: bool) type {
                 if (self.raw_elf_header.e_shstrndx == elf.SHN_UNDEF)
                     fatal("zig objcopy: no strtab, cannot add the user section", .{}); // TODO add the section if needed?
 
-                const strtab = &sections_update[self.raw_elf_header.e_shstrndx];
+                const strtab = sections_update[self.raw_elf_header.e_shstrndx].payload orelse
+                    self.sections[self.raw_elf_header.e_shstrndx].payload;
                 for (updated_section_header) |*section| {
-                    const section_name = std.mem.span(@as([*:0]const u8, @ptrCast(&strtab.payload.?[section.sh_name])));
+                    const section_name = std.mem.span(@as([*:0]const u8, @ptrCast(&strtab.?[section.sh_name])));
                     if (std.mem.eql(u8, section_name, set_flags.section_name)) {
                         section.sh_flags = std.elf.SHF_WRITE; // default is writable cleared by "readonly"
                         const f = set_flags.flags;

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behaviorcontributor friendlyThis issue is limited in scope and/or knowledge of Zig internals.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions