Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jacobly0 opened this issue Jan 19, 2025 · 0 comments
Open

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

jacobly0 opened this issue Jan 19, 2025 · 0 comments
Labels
bug Observed behavior contradicts documented or intended behavior contributor friendly This issue is limited in scope and/or knowledge of Zig internals.
Milestone

Comments

@jacobly0
Copy link
Member

$ 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;
@jacobly0 jacobly0 added bug Observed behavior contradicts documented or intended behavior contributor friendly This issue is limited in scope and/or knowledge of Zig internals. labels Jan 19, 2025
@jacobly0 jacobly0 added this to the 0.15.0 milestone Jan 19, 2025
@jacobly0 jacobly0 changed the title objcopy: -O elf --set-section-flags does not work objcopy: -O elf --set-section-flags panics Jan 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior contributor friendly This issue is limited in scope and/or knowledge of Zig internals.
Projects
None yet
Development

No branches or pull requests

1 participant