| ... | ... | @@ -683,39 +683,40 @@ pub fn initMergeSections(self: *Object, elf_file: *Elf) !void { |
| 683 | 683 | |
| 684 | 684 | const data = try self.codeDecompressAlloc(elf_file, atom_index); |
| 685 | 685 | defer gpa.free(data); |
| 686 | | const sh_entsize: u32 = @intCast(shdr.sh_entsize); |
| 687 | 686 | |
| 688 | 687 | if (shdr.sh_flags & elf.SHF_STRINGS != 0) { |
| 689 | | var pos: u32 = 0; |
| 690 | | while (pos < data.len) switch (sh_entsize) { |
| 691 | | 0, 1 => { |
| 692 | | // According to mold's source code, GHC emits MS sections with sh_entsize = 0. |
| 693 | | // This actually can also happen for output created with `-r` mode. |
| 694 | | const string = mem.sliceTo(@as([*:0]const u8, @ptrCast(data.ptr + pos)), 0); |
| 695 | | if (pos + string.len == data.len) { |
| 696 | | var err = try elf_file.addErrorWithNotes(1); |
| 697 | | try err.addMsg(elf_file, "string not null terminated", .{}); |
| 698 | | try err.addNote(elf_file, "in {}:{s}", .{ self.fmtPath(), atom_ptr.name(elf_file) }); |
| 699 | | return error.MalformedObject; |
| 700 | | } |
| 701 | | try imsec.insertZ(gpa, string); |
| 702 | | try imsec.offsets.append(gpa, pos); |
| 703 | | pos += @as(u32, @intCast(string.len)) + 1; // account for null |
| 704 | | }, |
| 705 | | else => |entsize| { |
| 706 | | const string = data.ptr[pos..][0..entsize]; |
| 707 | | if (string[string.len - 1] != 0) { |
| 708 | | var err = try elf_file.addErrorWithNotes(1); |
| 709 | | try err.addMsg(elf_file, "string not null terminated", .{}); |
| 710 | | try err.addNote(elf_file, "in {}:{s}", .{ self.fmtPath(), atom_ptr.name(elf_file) }); |
| 711 | | return error.MalformedObject; |
| 712 | | } |
| 713 | | try imsec.insert(gpa, string); |
| 714 | | try imsec.offsets.append(gpa, pos); |
| 715 | | pos += @as(u32, @intCast(string.len)); |
| 716 | | }, |
| 688 | const sh_entsize: u32 = switch (shdr.sh_entsize) { |
| 689 | // According to mold's source code, GHC emits MS sections with sh_entsize = 0. |
| 690 | // This actually can also happen for output created with `-r` mode. |
| 691 | 0 => 1, |
| 692 | else => |x| @intCast(x), |
| 717 | 693 | }; |
| 694 | |
| 695 | const isNull = struct { |
| 696 | fn isNull(slice: []u8) bool { |
| 697 | for (slice) |x| if (x != 0) return false; |
| 698 | return true; |
| 699 | } |
| 700 | }.isNull; |
| 701 | |
| 702 | var start: u32 = 0; |
| 703 | while (start < data.len) { |
| 704 | var end = start; |
| 705 | while (end < data.len - sh_entsize and !isNull(data[end .. end + sh_entsize])) : (end += sh_entsize) {} |
| 706 | if (!isNull(data[end .. end + sh_entsize])) { |
| 707 | var err = try elf_file.addErrorWithNotes(1); |
| 708 | try err.addMsg(elf_file, "string not null terminated", .{}); |
| 709 | try err.addNote(elf_file, "in {}:{s}", .{ self.fmtPath(), atom_ptr.name(elf_file) }); |
| 710 | return error.MalformedObject; |
| 711 | } |
| 712 | end += sh_entsize; |
| 713 | const string = data[start..end]; |
| 714 | try imsec.insert(gpa, string); |
| 715 | try imsec.offsets.append(gpa, start); |
| 716 | start = end; |
| 717 | } |
| 718 | 718 | } else { |
| 719 | const sh_entsize: u32 = @intCast(shdr.sh_entsize); |
| 719 | 720 | if (sh_entsize == 0) continue; // Malformed, don't split but don't error out |
| 720 | 721 | if (shdr.sh_size % sh_entsize != 0) { |
| 721 | 722 | var err = try elf_file.addErrorWithNotes(1); |