authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-09-03 15:07:30+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-09-04 13:34:26+02:00
logfca92fd7c0a2e1431338ef62440751870563a0e3
tree062d7177d6f84e75e9b10e6f053c2e1313bb58e8
parent5cdad186fe1cb83c6d79642433bfbe330436914a

elf: copy existing data when allocating other alloc sections in relocatable mode


1 files changed, 29 insertions(+), 1 deletions(-)

src/link/Elf/relocatable.zig+29-1
......@@ -407,7 +407,7 @@ fn updateComdatGroupsSizes(elf_file: *Elf) void {
407407
408408/// Allocates alloc sections when merging relocatable objects files together.
409409fn allocateAllocSections(elf_file: *Elf) !void {
410 for (elf_file.sections.items(.shdr)) |*shdr| {
410 for (elf_file.sections.items(.shdr), 0..) |*shdr, shndx| {
411411 if (shdr.sh_type == elf.SHT_NULL) continue;
412412 if (shdr.sh_flags & elf.SHF_ALLOC == 0) continue;
413413 if (shdr.sh_type == elf.SHT_NOBITS) {
......@@ -418,6 +418,34 @@ fn allocateAllocSections(elf_file: *Elf) !void {
418418 if (needed_size > elf_file.allocatedSize(shdr.sh_offset)) {
419419 shdr.sh_size = 0;
420420 const new_offset = try elf_file.findFreeSpace(needed_size, shdr.sh_addralign);
421
422 if (elf_file.zigObjectPtr()) |zo| blk: {
423 const existing_size = for ([_]?Symbol.Index{
424 zo.text_index,
425 zo.rodata_index,
426 zo.data_relro_index,
427 zo.data_index,
428 zo.tdata_index,
429 zo.eh_frame_index,
430 }) |maybe_sym_index| {
431 const sect_sym_index = maybe_sym_index orelse continue;
432 const sect_atom_ptr = zo.symbol(sect_sym_index).atom(elf_file).?;
433 if (sect_atom_ptr.output_section_index == shndx) break sect_atom_ptr.size;
434 } else break :blk;
435 log.debug("moving {s} from 0x{x} to 0x{x}", .{
436 elf_file.getShString(shdr.sh_name),
437 shdr.sh_offset,
438 new_offset,
439 });
440 const amt = try elf_file.base.file.?.copyRangeAll(
441 shdr.sh_offset,
442 elf_file.base.file.?,
443 new_offset,
444 existing_size,
445 );
446 if (amt != existing_size) return error.InputOutput;
447 }
448
421449 shdr.sh_offset = new_offset;
422450 shdr.sh_size = needed_size;
423451 }