| ... | ... | @@ -233,7 +233,15 @@ pub fn link(self: *Zld, files: []const []const u8, output: Output, args: LinkArg |
| 233 | 233 | try self.parseLibs(args.libs, args.syslibroot); |
| 234 | 234 | try self.resolveSymbols(); |
| 235 | 235 | try self.parseTextBlocks(); |
| 236 | | // try self.sortSections(); |
| 236 | try self.sortSections(); |
| 237 | try self.addRpaths(args.rpaths); |
| 238 | try self.addDataInCodeLC(); |
| 239 | try self.addCodeSignatureLC(); |
| 240 | try self.allocateTextSegment(); |
| 241 | try self.allocateDataConstSegment(); |
| 242 | try self.allocateDataSegment(); |
| 243 | self.allocateLinkeditSegment(); |
| 244 | try self.allocateTextBlocks(); |
| 237 | 245 | |
| 238 | 246 | log.warn("locals", .{}); |
| 239 | 247 | for (self.locals.items) |sym, id| { |
| ... | ... | @@ -289,14 +297,6 @@ pub fn link(self: *Zld, files: []const []const u8, output: Output, args: LinkArg |
| 289 | 297 | } |
| 290 | 298 | |
| 291 | 299 | return error.TODO; |
| 292 | | // try self.addRpaths(args.rpaths); |
| 293 | | // try self.addDataInCodeLC(); |
| 294 | | // try self.addCodeSignatureLC(); |
| 295 | | // try self.allocateTextSegment(); |
| 296 | | // try self.allocateDataConstSegment(); |
| 297 | | // try self.allocateDataSegment(); |
| 298 | | // self.allocateLinkeditSegment(); |
| 299 | | // try self.allocateTextBlocks(); |
| 300 | 300 | |
| 301 | 301 | // try self.flush(); |
| 302 | 302 | } |
| ... | ... | @@ -883,23 +883,11 @@ fn sortSections(self: *Zld) !void { |
| 883 | 883 | self.blocks.deinit(self.allocator); |
| 884 | 884 | self.blocks = transient; |
| 885 | 885 | } |
| 886 | | |
| 887 | | for (self.locals.items) |sym, i| { |
| 888 | | if (i == 0) continue; // skip the null symbol |
| 889 | | assert(sym.payload == .regular); |
| 890 | | const reg = &sym.payload.regular; |
| 891 | | reg.section_id = if (reg.segment_id == self.text_segment_cmd_index.?) |
| 892 | | text_index_mapping.get(reg.section_id).? |
| 893 | | else if (reg.segment_id == self.data_const_segment_cmd_index.?) |
| 894 | | data_const_index_mapping.get(reg.section_id).? |
| 895 | | else |
| 896 | | data_index_mapping.get(reg.section_id).?; |
| 897 | | } |
| 898 | 886 | } |
| 899 | 887 | |
| 900 | 888 | fn allocateTextSegment(self: *Zld) !void { |
| 901 | 889 | const seg = &self.load_commands.items[self.text_segment_cmd_index.?].Segment; |
| 902 | | const nstubs = @intCast(u32, self.stubs.items.len); |
| 890 | const nstubs = @intCast(u32, self.stubs.count()); |
| 903 | 891 | |
| 904 | 892 | const base_vmaddr = self.load_commands.items[self.pagezero_segment_cmd_index.?].Segment.inner.vmsize; |
| 905 | 893 | seg.inner.fileoff = 0; |
| ... | ... | @@ -950,7 +938,7 @@ fn allocateTextSegment(self: *Zld) !void { |
| 950 | 938 | |
| 951 | 939 | fn allocateDataConstSegment(self: *Zld) !void { |
| 952 | 940 | const seg = &self.load_commands.items[self.data_const_segment_cmd_index.?].Segment; |
| 953 | | const nentries = @intCast(u32, self.got_entries.items.len); |
| 941 | const nentries = @intCast(u32, self.got_entries.count()); |
| 954 | 942 | |
| 955 | 943 | const text_seg = self.load_commands.items[self.text_segment_cmd_index.?].Segment; |
| 956 | 944 | seg.inner.fileoff = text_seg.inner.fileoff + text_seg.inner.filesize; |
| ... | ... | @@ -965,7 +953,7 @@ fn allocateDataConstSegment(self: *Zld) !void { |
| 965 | 953 | |
| 966 | 954 | fn allocateDataSegment(self: *Zld) !void { |
| 967 | 955 | const seg = &self.load_commands.items[self.data_segment_cmd_index.?].Segment; |
| 968 | | const nstubs = @intCast(u32, self.stubs.items.len); |
| 956 | const nstubs = @intCast(u32, self.stubs.count()); |
| 969 | 957 | |
| 970 | 958 | const data_const_seg = self.load_commands.items[self.data_const_segment_cmd_index.?].Segment; |
| 971 | 959 | seg.inner.fileoff = data_const_seg.inner.fileoff + data_const_seg.inner.filesize; |
| ... | ... | @@ -1021,6 +1009,7 @@ fn allocateTextBlocks(self: *Zld) !void { |
| 1021 | 1009 | const sect = seg.sections.items[match.sect]; |
| 1022 | 1010 | |
| 1023 | 1011 | var base_addr: u64 = sect.addr; |
| 1012 | const n_sect = self.sectionId(match); |
| 1024 | 1013 | |
| 1025 | 1014 | log.debug(" within section {s},{s}", .{ segmentName(sect), sectionName(sect) }); |
| 1026 | 1015 | log.debug(" {}", .{sect}); |
| ... | ... | @@ -1029,12 +1018,12 @@ fn allocateTextBlocks(self: *Zld) !void { |
| 1029 | 1018 | const block_alignment = try math.powi(u32, 2, block.alignment); |
| 1030 | 1019 | base_addr = mem.alignForwardGeneric(u64, base_addr, block_alignment); |
| 1031 | 1020 | |
| 1032 | | const sym = self.locals.items[block.local_sym_index]; |
| 1033 | | assert(sym.payload == .regular); |
| 1034 | | sym.payload.regular.address = base_addr; |
| 1021 | const sym = &self.locals.items[block.local_sym_index]; |
| 1022 | sym.n_value = base_addr; |
| 1023 | sym.n_sect = n_sect; |
| 1035 | 1024 | |
| 1036 | 1025 | log.debug(" {s}: start=0x{x}, end=0x{x}, size={}, align={}", .{ |
| 1037 | | self.getString(sym.strx), |
| 1026 | self.getString(sym.n_strx), |
| 1038 | 1027 | base_addr, |
| 1039 | 1028 | base_addr + block.size, |
| 1040 | 1029 | block.size, |
| ... | ... | @@ -1043,17 +1032,17 @@ fn allocateTextBlocks(self: *Zld) !void { |
| 1043 | 1032 | |
| 1044 | 1033 | // Update each alias (if any) |
| 1045 | 1034 | for (block.aliases.items) |index| { |
| 1046 | | const alias_sym = self.locals.items[index]; |
| 1047 | | assert(alias_sym.payload == .regular); |
| 1048 | | alias_sym.payload.regular.address = base_addr; |
| 1035 | const alias_sym = &self.locals.items[index]; |
| 1036 | alias_sym.n_value = base_addr; |
| 1037 | alias_sym.n_sect = n_sect; |
| 1049 | 1038 | } |
| 1050 | 1039 | |
| 1051 | 1040 | // Update each symbol contained within the TextBlock |
| 1052 | 1041 | if (block.contained) |contained| { |
| 1053 | 1042 | for (contained) |sym_at_off| { |
| 1054 | | const contained_sym = self.locals.items[sym_at_off.local_sym_index]; |
| 1055 | | assert(contained_sym.payload == .regular); |
| 1056 | | contained_sym.payload.regular.address = base_addr + sym_at_off.offset; |
| 1043 | const contained_sym = &self.locals.items[sym_at_off.local_sym_index]; |
| 1044 | contained_sym.n_value = base_addr + sym_at_off.offset; |
| 1045 | contained_sym.n_sect = n_sect; |
| 1057 | 1046 | } |
| 1058 | 1047 | } |
| 1059 | 1048 | |
| ... | ... | @@ -1064,6 +1053,17 @@ fn allocateTextBlocks(self: *Zld) !void { |
| 1064 | 1053 | } else break; |
| 1065 | 1054 | } |
| 1066 | 1055 | } |
| 1056 | |
| 1057 | // Update globals |
| 1058 | for (self.symbol_resolver.values()) |resolv| { |
| 1059 | if (resolv.where != .global) continue; |
| 1060 | |
| 1061 | assert(resolv.local_sym_index != 0); |
| 1062 | const local_sym = self.locals.items[resolv.local_sym_index]; |
| 1063 | const sym = &self.globals.items[resolv.where_index]; |
| 1064 | sym.n_value = local_sym.n_value; |
| 1065 | sym.n_sect = local_sym.n_sect; |
| 1066 | } |
| 1067 | 1067 | } |
| 1068 | 1068 | |
| 1069 | 1069 | fn writeTextBlocks(self: *Zld) !void { |