| ... | ... | @@ -498,7 +498,7 @@ pub fn initMetadata(self: *Elf) !void { |
| 498 | 498 | |
| 499 | 499 | const fillSection = struct { |
| 500 | 500 | fn fillSection(elf_file: *Elf, shdr: *elf.Elf64_Shdr, size: u64, phndx: ?u16) void { |
| 501 | | if (elf_file.isObject()) { |
| 501 | if (elf_file.isRelocatable()) { |
| 502 | 502 | const off = elf_file.findFreeSpace(size, shdr.sh_addralign); |
| 503 | 503 | shdr.sh_offset = off; |
| 504 | 504 | shdr.sh_size = size; |
| ... | ... | @@ -513,7 +513,7 @@ pub fn initMetadata(self: *Elf) !void { |
| 513 | 513 | |
| 514 | 514 | comptime assert(number_of_zig_segments == 5); |
| 515 | 515 | |
| 516 | | if (!self.isObject()) { |
| 516 | if (!self.isRelocatable()) { |
| 517 | 517 | if (self.phdr_zig_load_re_index == null) { |
| 518 | 518 | const filesz = self.base.options.program_code_size_hint; |
| 519 | 519 | const off = self.findFreeSpace(filesz, self.page_size); |
| ... | ... | @@ -597,7 +597,7 @@ pub fn initMetadata(self: *Elf) !void { |
| 597 | 597 | }); |
| 598 | 598 | const shdr = &self.shdrs.items[self.zig_text_section_index.?]; |
| 599 | 599 | fillSection(self, shdr, self.base.options.program_code_size_hint, self.phdr_zig_load_re_index); |
| 600 | | if (self.isObject()) { |
| 600 | if (self.isRelocatable()) { |
| 601 | 601 | try zig_object.addSectionSymbol(self.zig_text_section_index.?, self); |
| 602 | 602 | self.zig_text_rela_section_index = try self.addRelaShdr( |
| 603 | 603 | ".rela.text.zig", |
| ... | ... | @@ -613,7 +613,7 @@ pub fn initMetadata(self: *Elf) !void { |
| 613 | 613 | try self.last_atom_and_free_list_table.putNoClobber(gpa, self.zig_text_section_index.?, .{}); |
| 614 | 614 | } |
| 615 | 615 | |
| 616 | | if (self.zig_got_section_index == null and !self.isObject()) { |
| 616 | if (self.zig_got_section_index == null and !self.isRelocatable()) { |
| 617 | 617 | self.zig_got_section_index = try self.addSection(.{ |
| 618 | 618 | .name = ".got.zig", |
| 619 | 619 | .type = elf.SHT_PROGBITS, |
| ... | ... | @@ -644,7 +644,7 @@ pub fn initMetadata(self: *Elf) !void { |
| 644 | 644 | }); |
| 645 | 645 | const shdr = &self.shdrs.items[self.zig_data_rel_ro_section_index.?]; |
| 646 | 646 | fillSection(self, shdr, 1024, self.phdr_zig_load_ro_index); |
| 647 | | if (self.isObject()) { |
| 647 | if (self.isRelocatable()) { |
| 648 | 648 | try zig_object.addSectionSymbol(self.zig_data_rel_ro_section_index.?, self); |
| 649 | 649 | self.zig_data_rel_ro_rela_section_index = try self.addRelaShdr( |
| 650 | 650 | ".rela.data.rel.ro.zig", |
| ... | ... | @@ -670,7 +670,7 @@ pub fn initMetadata(self: *Elf) !void { |
| 670 | 670 | }); |
| 671 | 671 | const shdr = &self.shdrs.items[self.zig_data_section_index.?]; |
| 672 | 672 | fillSection(self, shdr, 1024, self.phdr_zig_load_rw_index); |
| 673 | | if (self.isObject()) { |
| 673 | if (self.isRelocatable()) { |
| 674 | 674 | try zig_object.addSectionSymbol(self.zig_data_section_index.?, self); |
| 675 | 675 | self.zig_data_rela_section_index = try self.addRelaShdr( |
| 676 | 676 | ".rela.data.zig", |
| ... | ... | @@ -904,10 +904,6 @@ pub fn flush(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) link |
| 904 | 904 | if (use_lld) { |
| 905 | 905 | return self.linkWithLLD(comp, prog_node); |
| 906 | 906 | } |
| 907 | | if (self.base.options.output_mode == .Lib and self.isStatic()) { |
| 908 | | // TODO writing static library files |
| 909 | | return error.TODOImplementWritingLibFiles; |
| 910 | | } |
| 911 | 907 | try self.flushModule(comp, prog_node); |
| 912 | 908 | } |
| 913 | 909 | |
| ... | ... | @@ -943,7 +939,12 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node |
| 943 | 939 | } else null; |
| 944 | 940 | const gc_sections = self.base.options.gc_sections orelse false; |
| 945 | 941 | |
| 946 | | if (self.isObject() and self.zig_object_index == null) { |
| 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 | } |
| 947 | 948 | // TODO this will become -r route I guess. For now, just copy the object file. |
| 948 | 949 | assert(self.base.file == null); // TODO uncomment once we implement -r |
| 949 | 950 | const the_object_path = blk: { |
| ... | ... | @@ -1389,6 +1390,7 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node |
| 1389 | 1390 | } |
| 1390 | 1391 | |
| 1391 | 1392 | if (self.zigObjectPtr()) |zig_object| try zig_object.flushModule(self); |
| 1393 | if (self.isStaticLib()) return self.flushStaticLib(comp); |
| 1392 | 1394 | |
| 1393 | 1395 | // Dedup shared objects |
| 1394 | 1396 | { |
| ... | ... | @@ -1424,9 +1426,7 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node |
| 1424 | 1426 | self.resolveSymbols(); |
| 1425 | 1427 | self.markEhFrameAtomsDead(); |
| 1426 | 1428 | |
| 1427 | | if (self.isObject()) { |
| 1428 | | return self.flushObject(comp); |
| 1429 | | } |
| 1429 | if (self.isObject()) return self.flushObject(comp); |
| 1430 | 1430 | |
| 1431 | 1431 | try self.convertCommonSymbols(); |
| 1432 | 1432 | self.markImportsExports(); |
| ... | ... | @@ -1511,7 +1511,7 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node |
| 1511 | 1511 | try self.writeAtoms(); |
| 1512 | 1512 | try self.writeSyntheticSections(); |
| 1513 | 1513 | |
| 1514 | | if (self.entry_index == null and self.base.options.effectiveOutputMode() == .Exe) { |
| 1514 | if (self.entry_index == null and self.isExe()) { |
| 1515 | 1515 | log.debug("flushing. no_entry_point_found = true", .{}); |
| 1516 | 1516 | self.error_flags.no_entry_point_found = true; |
| 1517 | 1517 | } else { |
| ... | ... | @@ -1521,6 +1521,12 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node |
| 1521 | 1521 | } |
| 1522 | 1522 | } |
| 1523 | 1523 | |
| 1524 | pub fn flushStaticLib(self: *Elf, comp: *Compilation) link.File.FlushError!void { |
| 1525 | _ = comp; |
| 1526 | var err = try self.addErrorWithNotes(0); |
| 1527 | try err.addMsg(self, "fatal linker error: emitting static libs unimplemented", .{}); |
| 1528 | } |
| 1529 | |
| 1524 | 1530 | pub fn flushObject(self: *Elf, comp: *Compilation) link.File.FlushError!void { |
| 1525 | 1531 | _ = comp; |
| 1526 | 1532 | self.claimUnresolvedObject(); |
| ... | ... | @@ -2822,7 +2828,7 @@ fn writeHeader(self: *Elf) !void { |
| 2822 | 2828 | |
| 2823 | 2829 | assert(index == 16); |
| 2824 | 2830 | |
| 2825 | | const elf_type: elf.ET = switch (self.base.options.effectiveOutputMode()) { |
| 2831 | const elf_type: elf.ET = switch (self.base.options.output_mode) { |
| 2826 | 2832 | .Exe => if (self.base.options.pie) .DYN else .EXEC, |
| 2827 | 2833 | .Obj => .REL, |
| 2828 | 2834 | .Lib => switch (self.base.options.link_mode) { |
| ... | ... | @@ -3147,11 +3153,11 @@ fn initSections(self: *Elf) !void { |
| 3147 | 3153 | }; |
| 3148 | 3154 | const ptr_size = self.ptrWidthBytes(); |
| 3149 | 3155 | |
| 3150 | | for (self.objects.items) |index| { |
| 3156 | if (!self.isStaticLib()) for (self.objects.items) |index| { |
| 3151 | 3157 | try self.file(index).?.object.initOutputSections(self); |
| 3152 | | } |
| 3158 | }; |
| 3153 | 3159 | |
| 3154 | | const needs_eh_frame = for (self.objects.items) |index| { |
| 3160 | const needs_eh_frame = if (self.isStaticLib()) false else for (self.objects.items) |index| { |
| 3155 | 3161 | if (self.file(index).?.object.cies.items.len > 0) break true; |
| 3156 | 3162 | } else false; |
| 3157 | 3163 | if (needs_eh_frame) { |
| ... | ... | @@ -4954,15 +4960,23 @@ pub fn isStatic(self: Elf) bool { |
| 4954 | 4960 | } |
| 4955 | 4961 | |
| 4956 | 4962 | pub fn isObject(self: Elf) bool { |
| 4957 | | return self.base.options.effectiveOutputMode() == .Obj; |
| 4963 | return self.base.options.output_mode == .Obj; |
| 4958 | 4964 | } |
| 4959 | 4965 | |
| 4960 | 4966 | pub fn isExe(self: Elf) bool { |
| 4961 | | return self.base.options.effectiveOutputMode() == .Exe; |
| 4967 | return self.base.options.output_mode == .Exe; |
| 4968 | } |
| 4969 | |
| 4970 | pub fn isStaticLib(self: Elf) bool { |
| 4971 | return self.base.options.output_mode == .Lib and self.isStatic(); |
| 4972 | } |
| 4973 | |
| 4974 | pub fn isRelocatable(self: Elf) bool { |
| 4975 | return self.isObject() or self.isStaticLib(); |
| 4962 | 4976 | } |
| 4963 | 4977 | |
| 4964 | 4978 | pub fn isDynLib(self: Elf) bool { |
| 4965 | | return self.base.options.effectiveOutputMode() == .Lib and self.base.options.link_mode == .Dynamic; |
| 4979 | return self.base.options.output_mode == .Lib and !self.isStatic(); |
| 4966 | 4980 | } |
| 4967 | 4981 | |
| 4968 | 4982 | pub fn isZigSection(self: Elf, shndx: u16) bool { |