| ... | ... | @@ -939,12 +939,7 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node |
| 939 | 939 | } else null; |
| 940 | 940 | const gc_sections = self.base.options.gc_sections orelse false; |
| 941 | 941 | |
| 942 | | if (self.isRelocatable() and self.zig_object_index == null) { |
| 943 | | if (self.isStaticLib()) { |
| 944 | | var err = try self.addErrorWithNotes(0); |
| 945 | | try err.addMsg(self, "fatal linker error: emitting static libs unimplemented", .{}); |
| 946 | | return; |
| 947 | | } |
| 942 | if (self.isObject() and self.zig_object_index == null) { |
| 948 | 943 | // TODO this will become -r route I guess. For now, just copy the object file. |
| 949 | 944 | assert(self.base.file == null); // TODO uncomment once we implement -r |
| 950 | 945 | const the_object_path = blk: { |
| ... | ... | @@ -1555,15 +1550,18 @@ pub fn flushStaticLib(self: *Elf, comp: *Compilation) link.File.FlushError!void |
| 1555 | 1550 | try self.writeElfHeader(); |
| 1556 | 1551 | } |
| 1557 | 1552 | |
| 1558 | | // TODO parse positionals that we want to make part of the archive |
| 1559 | | |
| 1560 | | // TODO update ar symtab from parsed positionals |
| 1553 | var files = std.ArrayList(File.Index).init(gpa); |
| 1554 | defer files.deinit(); |
| 1555 | try files.ensureTotalCapacityPrecise(self.objects.items.len + 1); |
| 1556 | if (self.zigObjectPtr()) |zig_object| files.appendAssumeCapacity(zig_object.index); |
| 1557 | for (self.objects.items) |index| files.appendAssumeCapacity(index); |
| 1561 | 1558 | |
| 1559 | // Update ar symtab from parsed objects |
| 1562 | 1560 | var ar_symtab: Archive.ArSymtab = .{}; |
| 1563 | 1561 | defer ar_symtab.deinit(gpa); |
| 1564 | 1562 | |
| 1565 | | if (self.zigObjectPtr()) |zig_object| { |
| 1566 | | try zig_object.updateArSymtab(&ar_symtab, self); |
| 1563 | for (files.items) |index| { |
| 1564 | try self.file(index).?.updateArSymtab(&ar_symtab, self); |
| 1567 | 1565 | } |
| 1568 | 1566 | |
| 1569 | 1567 | ar_symtab.sort(); |
| ... | ... | @@ -1572,9 +1570,10 @@ pub fn flushStaticLib(self: *Elf, comp: *Compilation) link.File.FlushError!void |
| 1572 | 1570 | var ar_strtab: Archive.ArStrtab = .{}; |
| 1573 | 1571 | defer ar_strtab.deinit(gpa); |
| 1574 | 1572 | |
| 1575 | | if (self.zigObjectPtr()) |zig_object| { |
| 1576 | | try zig_object.updateArStrtab(gpa, &ar_strtab); |
| 1577 | | zig_object.updateArSize(self); |
| 1573 | for (files.items) |index| { |
| 1574 | const file_ptr = self.file(index).?; |
| 1575 | try file_ptr.updateArStrtab(gpa, &ar_strtab); |
| 1576 | file_ptr.updateArSize(self); |
| 1578 | 1577 | } |
| 1579 | 1578 | |
| 1580 | 1579 | // Update file offsets of contributing objects. |
| ... | ... | @@ -1587,10 +1586,16 @@ pub fn flushStaticLib(self: *Elf, comp: *Compilation) link.File.FlushError!void |
| 1587 | 1586 | pos += @sizeOf(Archive.ar_hdr) + ar_strtab.size(); |
| 1588 | 1587 | } |
| 1589 | 1588 | |
| 1590 | | if (self.zigObjectPtr()) |zig_object| { |
| 1589 | for (files.items) |index| { |
| 1590 | const file_ptr = self.file(index).?; |
| 1591 | const state = switch (file_ptr) { |
| 1592 | .zig_object => |x| &x.output_ar_state, |
| 1593 | .object => |x| &x.output_ar_state, |
| 1594 | else => unreachable, |
| 1595 | }; |
| 1591 | 1596 | pos = mem.alignForward(usize, pos, 2); |
| 1592 | | zig_object.output_ar_state.file_off = pos; |
| 1593 | | pos += @sizeOf(Archive.ar_hdr) + (math.cast(usize, zig_object.output_ar_state.size) orelse return error.Overflow); |
| 1597 | state.file_off = pos; |
| 1598 | pos += @sizeOf(Archive.ar_hdr) + (math.cast(usize, state.size) orelse return error.Overflow); |
| 1594 | 1599 | } |
| 1595 | 1600 | |
| 1596 | 1601 | break :blk pos; |
| ... | ... | @@ -1618,9 +1623,9 @@ pub fn flushStaticLib(self: *Elf, comp: *Compilation) link.File.FlushError!void |
| 1618 | 1623 | } |
| 1619 | 1624 | |
| 1620 | 1625 | // Write object files |
| 1621 | | if (self.zigObjectPtr()) |zig_object| { |
| 1626 | for (files.items) |index| { |
| 1622 | 1627 | if (!mem.isAligned(buffer.items.len, 2)) try buffer.writer().writeByte(0); |
| 1623 | | try zig_object.writeAr(self, buffer.writer()); |
| 1628 | try self.file(index).?.writeAr(self, buffer.writer()); |
| 1624 | 1629 | } |
| 1625 | 1630 | |
| 1626 | 1631 | assert(buffer.items.len == total_size); |