authorgravatar for 35903594+patrickwick@users.noreply.github.comPatrick Wickenhaeuser <35903594+patrickwick@users.noreply.github.com> 2024-10-04 12:38:34+02:00
committergravatar for 35903594+patrickwick@users.noreply.github.comPatrick Wickenhaeuser <35903594+patrickwick@users.noreply.github.com> 2024-10-04 15:49:50+02:00
logcaa699fc68bcdc3c5e80eb089d8af62000e06757
treef881b66b3b4b72ed1d08fb3e066ba7421e9a041b
parentf72961ee306408cdd3ee4ef5a8bc5301f94fc7e5

19009: zig objcopy: implement --set-section-alignment


1 files changed, 34 insertions(+), 17 deletions(-)

lib/compiler/objcopy.zig+34-17
...@@ -1252,13 +1252,8 @@ fn ElfFile(comptime is_64: bool) type {...@@ -1252,13 +1252,8 @@ fn ElfFile(comptime is_64: bool) type {
12521252
1253 const max_size = std.math.maxInt(usize);1253 const max_size = std.math.maxInt(usize);
1254 const payload = try section_file.readToEndAlloc(arena.allocator(), max_size);1254 const payload = try section_file.readToEndAlloc(arena.allocator(), max_size);
1255 const flags = 0; // TODO: 19009: support --set-section-flags1255 const flags = 0;
1256 const alignment = align_bytes: {1256 const alignment = 4;
1257 if (options.set_section_alignment) |set_align| {
1258 if (std.mem.eql(u8, set_align.section_name, add_section.section_name)) break :align_bytes set_align.alignment;
1259 }
1260 break :align_bytes 4;
1261 };
12621257
1263 dest_sections[dest_section_idx] = Elf_Shdr{1258 dest_sections[dest_section_idx] = Elf_Shdr{
1264 .sh_name = user_section_name,1259 .sh_name = user_section_name,
...@@ -1278,20 +1273,42 @@ fn ElfFile(comptime is_64: bool) type {...@@ -1278,20 +1273,42 @@ fn ElfFile(comptime is_64: bool) type {
1278 eof_offset += @as(Elf_OffSize, @intCast(payload.len));1273 eof_offset += @as(Elf_OffSize, @intCast(payload.len));
1279 }1274 }
12801275
1281 // overwrite section alignment
1282 {
1283 // TODO: 19009: NYI
1284 }
1285
1286 // overwrite section flags
1287 {
1288 // TODO: 19009: NYI
1289 }
1290
1291 assert(dest_section_idx == new_shnum);1276 assert(dest_section_idx == new_shnum);
1292 break :blk dest_sections;1277 break :blk dest_sections;
1293 };1278 };
12941279
1280 // --set-section-alignment: overwrite alignment
1281 if (options.set_section_alignment) |set_align| {
1282 if (self.raw_elf_header.e_shstrndx == elf.SHN_UNDEF)
1283 fatal("zig objcopy: no strtab, cannot add the user section", .{}); // TODO add the section if needed?
1284
1285 const strtab = &sections_update[self.raw_elf_header.e_shstrndx];
1286 for (updated_section_header) |*section| {
1287 const section_name = std.mem.span(@as([*:0]const u8, @ptrCast(&strtab.payload.?[section.sh_name])));
1288 if (std.mem.eql(u8, section_name, set_align.section_name)) {
1289 section.sh_addralign = set_align.alignment;
1290 break;
1291 }
1292 } else std.log.warn("Skipping --set-section-alignment. Section '{s}' not found", .{set_align.section_name});
1293 }
1294
1295 // --set-section-flags: overwrite flags
1296 if (options.set_section_flags) |set_flags| {
1297 if (self.raw_elf_header.e_shstrndx == elf.SHN_UNDEF)
1298 fatal("zig objcopy: no strtab, cannot add the user section", .{}); // TODO add the section if needed?
1299
1300 const strtab = &sections_update[self.raw_elf_header.e_shstrndx];
1301 for (updated_section_header) |*section| {
1302 const section_name = std.mem.span(@as([*:0]const u8, @ptrCast(&strtab.payload.?[section.sh_name])));
1303 if (std.mem.eql(u8, section_name, set_flags.section_name)) {
1304 // TODO: 19009: map flags string to bitfield.
1305 // section.sh_flags = set_flags.flags;
1306 section.sh_flags = 0;
1307 break;
1308 }
1309 } else std.log.warn("Skipping --set-section-flags. Section '{s}' not found", .{set_flags.section_name});
1310 }
1311
1295 // write the section header at the tail1312 // write the section header at the tail
1296 {1313 {
1297 const offset = std.mem.alignForward(Elf_OffSize, eof_offset, @alignOf(Elf_Shdr));1314 const offset = std.mem.alignForward(Elf_OffSize, eof_offset, @alignOf(Elf_Shdr));