From caa699fc68bcdc3c5e80eb089d8af62000e06757 Mon Sep 17 00:00:00 2001 From: Patrick Wickenhaeuser <35903594+patrickwick@users.noreply.github.com> Date: Fri, 4 Oct 2024 12:38:34 +0200 Subject: [PATCH] 19009: zig objcopy: implement --set-section-alignment --- lib/compiler/objcopy.zig | 51 ++++++++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 17 deletions(-) diff --git a/lib/compiler/objcopy.zig b/lib/compiler/objcopy.zig index 51bdab5b17..afa714d8db 100644 --- a/lib/compiler/objcopy.zig +++ b/lib/compiler/objcopy.zig @@ -1252,13 +1252,8 @@ fn ElfFile(comptime is_64: bool) type { const max_size = std.math.maxInt(usize); const payload = try section_file.readToEndAlloc(arena.allocator(), max_size); - const flags = 0; // TODO: 19009: support --set-section-flags - const alignment = align_bytes: { - if (options.set_section_alignment) |set_align| { - if (std.mem.eql(u8, set_align.section_name, add_section.section_name)) break :align_bytes set_align.alignment; - } - break :align_bytes 4; - }; + const flags = 0; + const alignment = 4; dest_sections[dest_section_idx] = Elf_Shdr{ .sh_name = user_section_name, @@ -1278,20 +1273,42 @@ fn ElfFile(comptime is_64: bool) type { eof_offset += @as(Elf_OffSize, @intCast(payload.len)); } - // overwrite section alignment - { - // TODO: 19009: NYI - } - - // overwrite section flags - { - // TODO: 19009: NYI - } - assert(dest_section_idx == new_shnum); break :blk dest_sections; }; + // --set-section-alignment: overwrite alignment + if (options.set_section_alignment) |set_align| { + 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 = §ions_update[self.raw_elf_header.e_shstrndx]; + for (updated_section_header) |*section| { + const section_name = std.mem.span(@as([*:0]const u8, @ptrCast(&strtab.payload.?[section.sh_name]))); + if (std.mem.eql(u8, section_name, set_align.section_name)) { + section.sh_addralign = set_align.alignment; + break; + } + } else std.log.warn("Skipping --set-section-alignment. Section '{s}' not found", .{set_align.section_name}); + } + + // --set-section-flags: overwrite flags + if (options.set_section_flags) |set_flags| { + 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 = §ions_update[self.raw_elf_header.e_shstrndx]; + for (updated_section_header) |*section| { + const section_name = std.mem.span(@as([*:0]const u8, @ptrCast(&strtab.payload.?[section.sh_name]))); + if (std.mem.eql(u8, section_name, set_flags.section_name)) { + // TODO: 19009: map flags string to bitfield. + // section.sh_flags = set_flags.flags; + section.sh_flags = 0; + break; + } + } else std.log.warn("Skipping --set-section-flags. Section '{s}' not found", .{set_flags.section_name}); + } + // write the section header at the tail { const offset = std.mem.alignForward(Elf_OffSize, eof_offset, @alignOf(Elf_Shdr));