| ... | @@ -1069,7 +1069,7 @@ fn allocateTextBlocks(self: *Zld) !void { | ... | @@ -1069,7 +1069,7 @@ fn allocateTextBlocks(self: *Zld) !void { |
| 1069 | assert(sym.payload == .regular); | 1069 | assert(sym.payload == .regular); |
| 1070 | sym.payload.regular.address = base_addr; | 1070 | sym.payload.regular.address = base_addr; |
| 1071 | | 1071 | |
| 1072 | log.warn(" {s}: start=0x{x}, end=0x{x}, size={}, align={}", .{ | 1072 | log.warn(" {s}: start=0x{x}, end=0x{x}, size={}, align={}", .{ |
| 1073 | sym.name, | 1073 | sym.name, |
| 1074 | base_addr, | 1074 | base_addr, |
| 1075 | base_addr + block.size, | 1075 | base_addr + block.size, |
| ... | @@ -1103,6 +1103,8 @@ fn allocateTextBlocks(self: *Zld) !void { | ... | @@ -1103,6 +1103,8 @@ fn allocateTextBlocks(self: *Zld) !void { |
| 1103 | } | 1103 | } |
| 1104 | | 1104 | |
| 1105 | fn writeTextBlocks(self: *Zld) !void { | 1105 | fn writeTextBlocks(self: *Zld) !void { |
| | 1106 | log.warn("writing text blocks", .{}); |
| | 1107 | |
| 1106 | var it = self.blocks.iterator(); | 1108 | var it = self.blocks.iterator(); |
| 1107 | while (it.next()) |entry| { | 1109 | while (it.next()) |entry| { |
| 1108 | const match = entry.key_ptr.*; | 1110 | const match = entry.key_ptr.*; |
| ... | @@ -1112,7 +1114,8 @@ fn writeTextBlocks(self: *Zld) !void { | ... | @@ -1112,7 +1114,8 @@ fn writeTextBlocks(self: *Zld) !void { |
| 1112 | const sect = seg.sections.items[match.sect]; | 1114 | const sect = seg.sections.items[match.sect]; |
| 1113 | const sect_type = sectionType(sect); | 1115 | const sect_type = sectionType(sect); |
| 1114 | | 1116 | |
| 1115 | log.warn("writing text blocks for section {s},{s}", .{ segmentName(sect), sectionName(sect) }); | 1117 | log.warn(" for section {s},{s}", .{ segmentName(sect), sectionName(sect) }); |
| | 1118 | log.warn(" {}", .{sect}); |
| 1116 | | 1119 | |
| 1117 | var code = try self.allocator.alloc(u8, sect.size); | 1120 | var code = try self.allocator.alloc(u8, sect.size); |
| 1118 | defer self.allocator.free(code); | 1121 | defer self.allocator.free(code); |
| ... | @@ -1126,10 +1129,28 @@ fn writeTextBlocks(self: *Zld) !void { | ... | @@ -1126,10 +1129,28 @@ fn writeTextBlocks(self: *Zld) !void { |
| 1126 | var base_off: u64 = sect.size; | 1129 | var base_off: u64 = sect.size; |
| 1127 | | 1130 | |
| 1128 | while (true) { | 1131 | while (true) { |
| 1129 | base_off -= block.size; | 1132 | const block_alignment = try math.powi(u32, 2, block.alignment); |
| | 1133 | const unaligned_base_off = base_off - block.size; |
| | 1134 | const aligned_base_off = mem.alignBackwardGeneric(u64, unaligned_base_off, block_alignment); |
| | 1135 | |
| | 1136 | const sym = self.locals.items[block.local_sym_index]; |
| | 1137 | log.warn(" {s}: start=0x{x}, end=0x{x}, size={}, align={}", .{ |
| | 1138 | sym.name, |
| | 1139 | aligned_base_off, |
| | 1140 | aligned_base_off + block.size, |
| | 1141 | block.size, |
| | 1142 | block.alignment, |
| | 1143 | }); |
| 1130 | | 1144 | |
| 1131 | try block.resolveRelocs(self); | 1145 | try block.resolveRelocs(self); |
| 1132 | mem.copy(u8, code[base_off..][0..block.size], block.code); | 1146 | mem.copy(u8, code[aligned_base_off..][0..block.size], block.code); |
| | 1147 | |
| | 1148 | // TODO NOP for machine code instead of just zeroing out |
| | 1149 | const padding_off = aligned_base_off + block.size; |
| | 1150 | const padding_len = unaligned_base_off - aligned_base_off; |
| | 1151 | mem.set(u8, code[padding_off..][0..padding_len], 0); |
| | 1152 | |
| | 1153 | base_off = aligned_base_off; |
| 1133 | | 1154 | |
| 1134 | if (block.prev) |prev| { | 1155 | if (block.prev) |prev| { |
| 1135 | block = prev; | 1156 | block = prev; |