authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-04-21 10:37:49+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-04-23 17:14:03-07:00
logeb5d67b146da7af693bc45f61e69070b0d6b27bb
tree7543827e786486810b5173cb6ece5714b04843b2
parent082e5091af38acd0669f6ca14e13dee1ce65e509

Merge pull request #19714 from ziglang/elf-merge-strings

link/elf: implement string merging

18 files changed, 1368 insertions(+), 343 deletions(-)

src/link/Elf.zig+305-32
......@@ -205,10 +205,19 @@ num_ifunc_dynrelocs: usize = 0,
205205
206206/// List of atoms that are owned directly by the linker.
207207atoms: std.ArrayListUnmanaged(Atom) = .{},
208atoms_extra: std.ArrayListUnmanaged(u32) = .{},
208209
209210/// List of range extension thunks.
210211thunks: std.ArrayListUnmanaged(Thunk) = .{},
211212
213/// List of output merge sections with deduped contents.
214merge_sections: std.ArrayListUnmanaged(MergeSection) = .{},
215/// List of output merge subsections.
216/// Each subsection is akin to Atom but belongs to a MergeSection.
217merge_subsections: std.ArrayListUnmanaged(MergeSubsection) = .{},
218/// List of input merge sections as parsed from input relocatables.
219merge_input_sections: std.ArrayListUnmanaged(InputMergeSection) = .{},
220
212221/// Table of last atom index in a section and matching atom free list if any.
213222last_atom_and_free_list_table: LastAtomAndFreeListTable = .{},
214223
......@@ -369,6 +378,7 @@ pub fn createEmpty(
369378 try self.symbols_extra.append(gpa, 0);
370379 // Allocate atom index 0 to null atom
371380 try self.atoms.append(gpa, .{});
381 try self.atoms_extra.append(gpa, 0);
372382 // Append null file at index 0
373383 try self.files.append(gpa, .null);
374384 // Append null byte to string tables
......@@ -378,6 +388,8 @@ pub fn createEmpty(
378388 _ = try self.addSection(.{ .name = "" });
379389 // Append null symbol in output symtab
380390 try self.symtab.append(gpa, null_sym);
391 // Append null input merge section.
392 try self.merge_input_sections.append(gpa, .{});
381393
382394 if (!is_obj_or_ar) {
383395 try self.dynstrtab.append(gpa, 0);
......@@ -491,7 +503,20 @@ pub fn deinit(self: *Elf) void {
491503 self.start_stop_indexes.deinit(gpa);
492504
493505 self.atoms.deinit(gpa);
506 self.atoms_extra.deinit(gpa);
507 for (self.thunks.items) |*th| {
508 th.deinit(gpa);
509 }
494510 self.thunks.deinit(gpa);
511 for (self.merge_sections.items) |*sect| {
512 sect.deinit(gpa);
513 }
514 self.merge_sections.deinit(gpa);
515 self.merge_subsections.deinit(gpa);
516 for (self.merge_input_sections.items) |*sect| {
517 sect.deinit(gpa);
518 }
519 self.merge_input_sections.deinit(gpa);
495520 for (self.last_atom_and_free_list_table.values()) |*value| {
496521 value.free_list.deinit(gpa);
497522 }
......@@ -1289,6 +1314,7 @@ pub fn flushModule(self: *Elf, arena: Allocator, prog_node: *std.Progress.Node)
12891314 // symbol for potential resolution at load-time.
12901315 self.resolveSymbols();
12911316 self.markEhFrameAtomsDead();
1317 try self.resolveMergeSections();
12921318
12931319 try self.convertCommonSymbols();
12941320 self.markImportsExports();
......@@ -1313,7 +1339,10 @@ pub fn flushModule(self: *Elf, arena: Allocator, prog_node: *std.Progress.Node)
13131339 else => |e| return e,
13141340 };
13151341
1342 try self.addCommentString();
1343 try self.finalizeMergeSections();
13161344 try self.initOutputSections();
1345 try self.initMergeSections();
13171346 try self.addLinkerDefinedSymbols();
13181347 self.claimUnresolved();
13191348
......@@ -1332,6 +1361,7 @@ pub fn flushModule(self: *Elf, arena: Allocator, prog_node: *std.Progress.Node)
13321361 self.sortDynamicSymtab();
13331362 try self.setHashSections();
13341363 try self.setVersionSymtab();
1364 try self.updateMergeSectionSizes();
13351365 try self.updateSectionSizes();
13361366
13371367 try self.allocatePhdrTable();
......@@ -1359,7 +1389,7 @@ pub fn flushModule(self: *Elf, arena: Allocator, prog_node: *std.Progress.Node)
13591389 if (shdr.sh_type == elf.SHT_NOBITS) continue;
13601390 const code = try zig_object.codeAlloc(self, atom_index);
13611391 defer gpa.free(code);
1362 const file_offset = shdr.sh_offset + atom_ptr.value;
1392 const file_offset = shdr.sh_offset + @as(u64, @intCast(atom_ptr.value));
13631393 atom_ptr.resolveRelocsAlloc(self, code) catch |err| switch (err) {
13641394 error.RelocFailure, error.RelaxFailure => has_reloc_errors = true,
13651395 error.UnsupportedCpuArch => {
......@@ -1377,6 +1407,7 @@ pub fn flushModule(self: *Elf, arena: Allocator, prog_node: *std.Progress.Node)
13771407 try self.writePhdrTable();
13781408 try self.writeShdrTable();
13791409 try self.writeAtoms();
1410 try self.writeMergeSections();
13801411 self.writeSyntheticSections() catch |err| switch (err) {
13811412 error.RelocFailure => return error.FlushFailure,
13821413 error.UnsupportedCpuArch => {
......@@ -2946,7 +2977,10 @@ pub fn writeElfHeader(self: *Elf) !void {
29462977 mem.writeInt(u32, hdr_buf[index..][0..4], 1, endian);
29472978 index += 4;
29482979
2949 const e_entry = if (self.entry_index) |entry_index| self.symbol(entry_index).address(.{}, self) else 0;
2980 const e_entry = if (self.entry_index) |entry_index|
2981 @as(u64, @intCast(self.symbol(entry_index).address(.{}, self)))
2982 else
2983 0;
29502984 const phdr_table_offset = if (self.phdr_table_index) |phndx| self.phdrs.items[phndx].p_offset else 0;
29512985 switch (self.ptr_width) {
29522986 .p32 => {
......@@ -3132,14 +3166,14 @@ fn allocateLinkerDefinedSymbols(self: *Elf) void {
31323166 if (self.dynamic_section_index) |shndx| {
31333167 const shdr = &self.shdrs.items[shndx];
31343168 const symbol_ptr = self.symbol(self.dynamic_index.?);
3135 symbol_ptr.value = shdr.sh_addr;
3169 symbol_ptr.value = @intCast(shdr.sh_addr);
31363170 symbol_ptr.output_section_index = shndx;
31373171 }
31383172
31393173 // __ehdr_start
31403174 {
31413175 const symbol_ptr = self.symbol(self.ehdr_start_index.?);
3142 symbol_ptr.value = self.image_base;
3176 symbol_ptr.value = @intCast(self.image_base);
31433177 symbol_ptr.output_section_index = 1;
31443178 }
31453179
......@@ -3149,9 +3183,9 @@ fn allocateLinkerDefinedSymbols(self: *Elf) void {
31493183 const end_sym = self.symbol(self.init_array_end_index.?);
31503184 const shdr = &self.shdrs.items[shndx];
31513185 start_sym.output_section_index = shndx;
3152 start_sym.value = shdr.sh_addr;
3186 start_sym.value = @intCast(shdr.sh_addr);
31533187 end_sym.output_section_index = shndx;
3154 end_sym.value = shdr.sh_addr + shdr.sh_size;
3188 end_sym.value = @intCast(shdr.sh_addr + shdr.sh_size);
31553189 }
31563190
31573191 // __fini_array_start, __fini_array_end
......@@ -3160,9 +3194,9 @@ fn allocateLinkerDefinedSymbols(self: *Elf) void {
31603194 const end_sym = self.symbol(self.fini_array_end_index.?);
31613195 const shdr = &self.shdrs.items[shndx];
31623196 start_sym.output_section_index = shndx;
3163 start_sym.value = shdr.sh_addr;
3197 start_sym.value = @intCast(shdr.sh_addr);
31643198 end_sym.output_section_index = shndx;
3165 end_sym.value = shdr.sh_addr + shdr.sh_size;
3199 end_sym.value = @intCast(shdr.sh_addr + shdr.sh_size);
31663200 }
31673201
31683202 // __preinit_array_start, __preinit_array_end
......@@ -3171,9 +3205,9 @@ fn allocateLinkerDefinedSymbols(self: *Elf) void {
31713205 const end_sym = self.symbol(self.preinit_array_end_index.?);
31723206 const shdr = &self.shdrs.items[shndx];
31733207 start_sym.output_section_index = shndx;
3174 start_sym.value = shdr.sh_addr;
3208 start_sym.value = @intCast(shdr.sh_addr);
31753209 end_sym.output_section_index = shndx;
3176 end_sym.value = shdr.sh_addr + shdr.sh_size;
3210 end_sym.value = @intCast(shdr.sh_addr + shdr.sh_size);
31773211 }
31783212
31793213 // _GLOBAL_OFFSET_TABLE_
......@@ -3181,14 +3215,14 @@ fn allocateLinkerDefinedSymbols(self: *Elf) void {
31813215 if (self.got_plt_section_index) |shndx| {
31823216 const shdr = self.shdrs.items[shndx];
31833217 const sym = self.symbol(self.got_index.?);
3184 sym.value = shdr.sh_addr;
3218 sym.value = @intCast(shdr.sh_addr);
31853219 sym.output_section_index = shndx;
31863220 }
31873221 } else {
31883222 if (self.got_section_index) |shndx| {
31893223 const shdr = self.shdrs.items[shndx];
31903224 const sym = self.symbol(self.got_index.?);
3191 sym.value = shdr.sh_addr;
3225 sym.value = @intCast(shdr.sh_addr);
31923226 sym.output_section_index = shndx;
31933227 }
31943228 }
......@@ -3197,7 +3231,7 @@ fn allocateLinkerDefinedSymbols(self: *Elf) void {
31973231 if (self.plt_section_index) |shndx| {
31983232 const shdr = &self.shdrs.items[shndx];
31993233 const symbol_ptr = self.symbol(self.plt_index.?);
3200 symbol_ptr.value = shdr.sh_addr;
3234 symbol_ptr.value = @intCast(shdr.sh_addr);
32013235 symbol_ptr.output_section_index = shndx;
32023236 }
32033237
......@@ -3205,7 +3239,7 @@ fn allocateLinkerDefinedSymbols(self: *Elf) void {
32053239 if (self.dso_handle_index) |index| {
32063240 const shdr = &self.shdrs.items[1];
32073241 const symbol_ptr = self.symbol(index);
3208 symbol_ptr.value = shdr.sh_addr;
3242 symbol_ptr.value = @intCast(shdr.sh_addr);
32093243 symbol_ptr.output_section_index = 0;
32103244 }
32113245
......@@ -3213,7 +3247,7 @@ fn allocateLinkerDefinedSymbols(self: *Elf) void {
32133247 if (self.eh_frame_hdr_section_index) |shndx| {
32143248 const shdr = &self.shdrs.items[shndx];
32153249 const symbol_ptr = self.symbol(self.gnu_eh_frame_hdr_index.?);
3216 symbol_ptr.value = shdr.sh_addr;
3250 symbol_ptr.value = @intCast(shdr.sh_addr);
32173251 symbol_ptr.output_section_index = shndx;
32183252 }
32193253
......@@ -3225,9 +3259,9 @@ fn allocateLinkerDefinedSymbols(self: *Elf) void {
32253259 const start_addr = end_addr - self.calcNumIRelativeRelocs() * @sizeOf(elf.Elf64_Rela);
32263260 const start_sym = self.symbol(self.rela_iplt_start_index.?);
32273261 const end_sym = self.symbol(self.rela_iplt_end_index.?);
3228 start_sym.value = start_addr;
3262 start_sym.value = @intCast(start_addr);
32293263 start_sym.output_section_index = shndx;
3230 end_sym.value = end_addr;
3264 end_sym.value = @intCast(end_addr);
32313265 end_sym.output_section_index = shndx;
32323266 }
32333267
......@@ -3236,7 +3270,7 @@ fn allocateLinkerDefinedSymbols(self: *Elf) void {
32363270 const end_symbol = self.symbol(self.end_index.?);
32373271 for (self.shdrs.items, 0..) |shdr, shndx| {
32383272 if (shdr.sh_flags & elf.SHF_ALLOC != 0) {
3239 end_symbol.value = shdr.sh_addr + shdr.sh_size;
3273 end_symbol.value = @intCast(shdr.sh_addr + shdr.sh_size);
32403274 end_symbol.output_section_index = @intCast(shndx);
32413275 }
32423276 }
......@@ -3251,9 +3285,9 @@ fn allocateLinkerDefinedSymbols(self: *Elf) void {
32513285 const stop = self.symbol(self.start_stop_indexes.items[index + 1]);
32523286 const shndx = self.sectionByName(name["__start_".len..]).?;
32533287 const shdr = &self.shdrs.items[shndx];
3254 start.value = shdr.sh_addr;
3288 start.value = @intCast(shdr.sh_addr);
32553289 start.output_section_index = shndx;
3256 stop.value = shdr.sh_addr + shdr.sh_size;
3290 stop.value = @intCast(shdr.sh_addr + shdr.sh_size);
32573291 stop.output_section_index = shndx;
32583292 }
32593293 }
......@@ -3263,7 +3297,7 @@ fn allocateLinkerDefinedSymbols(self: *Elf) void {
32633297 const sym = self.symbol(index);
32643298 if (self.sectionByName(".sdata")) |shndx| {
32653299 const shdr = self.shdrs.items[shndx];
3266 sym.value = shdr.sh_addr + 0x800;
3300 sym.value = @intCast(shdr.sh_addr + 0x800);
32673301 sym.output_section_index = shndx;
32683302 } else {
32693303 sym.value = 0;
......@@ -3293,12 +3327,122 @@ fn checkDuplicates(self: *Elf) !void {
32933327 try self.reportDuplicates(dupes);
32943328}
32953329
3330pub fn addCommentString(self: *Elf) !void {
3331 const msec_index = try self.getOrCreateMergeSection(".comment", elf.SHF_MERGE | elf.SHF_STRINGS, elf.SHT_PROGBITS);
3332 const msec = self.mergeSection(msec_index);
3333 const res = try msec.insertZ(self.base.comp.gpa, "zig " ++ builtin.zig_version_string);
3334 if (res.found_existing) return;
3335 const msub_index = try self.addMergeSubsection();
3336 const msub = self.mergeSubsection(msub_index);
3337 msub.merge_section_index = msec_index;
3338 msub.string_index = res.key.pos;
3339 msub.alignment = .@"1";
3340 msub.size = res.key.len;
3341 msub.entsize = 1;
3342 msub.alive = true;
3343 res.sub.* = msub_index;
3344}
3345
3346pub fn resolveMergeSections(self: *Elf) !void {
3347 const tracy = trace(@src());
3348 defer tracy.end();
3349
3350 var has_errors = false;
3351 for (self.objects.items) |index| {
3352 const file_ptr = self.file(index).?;
3353 if (!file_ptr.isAlive()) continue;
3354 file_ptr.object.initMergeSections(self) catch |err| switch (err) {
3355 error.MalformedObject => has_errors = true,
3356 else => |e| return e,
3357 };
3358 }
3359
3360 if (has_errors) return error.FlushFailure;
3361
3362 for (self.objects.items) |index| {
3363 const file_ptr = self.file(index).?;
3364 if (!file_ptr.isAlive()) continue;
3365 file_ptr.object.resolveMergeSubsections(self) catch |err| switch (err) {
3366 error.MalformedObject => has_errors = true,
3367 else => |e| return e,
3368 };
3369 }
3370
3371 if (has_errors) return error.FlushFailure;
3372}
3373
3374pub fn finalizeMergeSections(self: *Elf) !void {
3375 for (self.merge_sections.items) |*msec| {
3376 try msec.finalize(self);
3377 }
3378}
3379
3380pub fn updateMergeSectionSizes(self: *Elf) !void {
3381 for (self.merge_sections.items) |*msec| {
3382 const shdr = &self.shdrs.items[msec.output_section_index];
3383 for (msec.subsections.items) |msub_index| {
3384 const msub = self.mergeSubsection(msub_index);
3385 assert(msub.alive);
3386 const offset = msub.alignment.forward(shdr.sh_size);
3387 const padding = offset - shdr.sh_size;
3388 msub.value = @intCast(offset);
3389 shdr.sh_size += padding + msub.size;
3390 shdr.sh_addralign = @max(shdr.sh_addralign, msub.alignment.toByteUnits() orelse 1);
3391 }
3392 }
3393}
3394
3395pub fn writeMergeSections(self: *Elf) !void {
3396 const gpa = self.base.comp.gpa;
3397 var buffer = std.ArrayList(u8).init(gpa);
3398 defer buffer.deinit();
3399
3400 for (self.merge_sections.items) |msec| {
3401 const shdr = self.shdrs.items[msec.output_section_index];
3402 const size = math.cast(usize, shdr.sh_size) orelse return error.Overflow;
3403 try buffer.ensureTotalCapacity(size);
3404 buffer.appendNTimesAssumeCapacity(0, size);
3405
3406 for (msec.subsections.items) |msub_index| {
3407 const msub = self.mergeSubsection(msub_index);
3408 assert(msub.alive);
3409 const string = msub.getString(self);
3410 const off = math.cast(usize, msub.value) orelse return error.Overflow;
3411 @memcpy(buffer.items[off..][0..string.len], string);
3412 }
3413
3414 try self.base.file.?.pwriteAll(buffer.items, shdr.sh_offset);
3415 buffer.clearRetainingCapacity();
3416 }
3417}
3418
32963419fn initOutputSections(self: *Elf) !void {
32973420 for (self.objects.items) |index| {
32983421 try self.file(index).?.object.initOutputSections(self);
32993422 }
33003423}
33013424
3425pub fn initMergeSections(self: *Elf) !void {
3426 for (self.merge_sections.items) |*msec| {
3427 if (msec.subsections.items.len == 0) continue;
3428 const name = msec.name(self);
3429 const shndx = self.sectionByName(name) orelse try self.addSection(.{
3430 .name = name,
3431 .type = msec.type,
3432 .flags = msec.flags,
3433 });
3434 msec.output_section_index = shndx;
3435
3436 var entsize = self.mergeSubsection(msec.subsections.items[0]).entsize;
3437 for (msec.subsections.items) |index| {
3438 const msub = self.mergeSubsection(index);
3439 entsize = @min(entsize, msub.entsize);
3440 }
3441 const shdr = &self.shdrs.items[shndx];
3442 shdr.sh_entsize = entsize;
3443 }
3444}
3445
33023446fn initSyntheticSections(self: *Elf) !void {
33033447 const comp = self.base.comp;
33043448 const target = comp.root_mod.resolved_target.result;
......@@ -3965,6 +4109,10 @@ fn resetShdrIndexes(self: *Elf, backlinks: []const u32) !void {
39654109 }
39664110 }
39674111
4112 for (self.merge_sections.items) |*msec| {
4113 msec.output_section_index = backlinks[msec.output_section_index];
4114 }
4115
39684116 {
39694117 var output_rela_sections = try self.output_rela_sections.clone(gpa);
39704118 defer output_rela_sections.deinit(gpa);
......@@ -4052,7 +4200,7 @@ fn updateSectionSizes(self: *Elf) !void {
40524200 if (!atom_ptr.flags.alive) continue;
40534201 const offset = atom_ptr.alignment.forward(shdr.sh_size);
40544202 const padding = offset - shdr.sh_size;
4055 atom_ptr.value = offset;
4203 atom_ptr.value = @intCast(offset);
40564204 shdr.sh_size += padding + atom_ptr.size;
40574205 shdr.sh_addralign = @max(shdr.sh_addralign, atom_ptr.alignment.toByteUnits() orelse 1);
40584206 }
......@@ -4535,7 +4683,7 @@ fn writeAtoms(self: *Elf) !void {
45354683 const atom_ptr = self.atom(atom_index).?;
45364684 assert(atom_ptr.flags.alive);
45374685
4538 const offset = math.cast(usize, atom_ptr.value - base_offset) orelse
4686 const offset = math.cast(usize, atom_ptr.value - @as(i64, @intCast(base_offset))) orelse
45394687 return error.Overflow;
45404688 const size = math.cast(usize, atom_ptr.size) orelse return error.Overflow;
45414689
......@@ -4576,7 +4724,7 @@ fn writeAtoms(self: *Elf) !void {
45764724 const thunk_size = th.size(self);
45774725 try buffer.ensureUnusedCapacity(thunk_size);
45784726 const shdr = self.shdrs.items[th.output_section_index];
4579 const offset = th.value + shdr.sh_offset;
4727 const offset = @as(u64, @intCast(th.value)) + shdr.sh_offset;
45804728 try th.write(self, buffer.writer());
45814729 assert(buffer.items.len == thunk_size);
45824730 try self.base.file.?.pwriteAll(buffer.items, offset);
......@@ -4611,6 +4759,7 @@ pub fn updateSymtabSize(self: *Elf) !void {
46114759 if (self.eh_frame_section_index) |_| {
46124760 nlocals += 1;
46134761 }
4762 nlocals += @intCast(self.merge_sections.items.len);
46144763
46154764 if (self.requiresThunks()) for (self.thunks.items) |*th| {
46164765 th.output_symtab_ctx.ilocal = nlocals + 1;
......@@ -4947,12 +5096,30 @@ fn writeSectionSymbols(self: *Elf) void {
49475096 };
49485097 ilocal += 1;
49495098 }
5099
5100 for (self.merge_sections.items) |msec| {
5101 const shdr = self.shdrs.items[msec.output_section_index];
5102 const out_sym = &self.symtab.items[ilocal];
5103 out_sym.* = .{
5104 .st_name = 0,
5105 .st_value = shdr.sh_addr,
5106 .st_info = elf.STT_SECTION,
5107 .st_shndx = @intCast(msec.output_section_index),
5108 .st_size = 0,
5109 .st_other = 0,
5110 };
5111 ilocal += 1;
5112 }
49505113}
49515114
49525115pub fn sectionSymbolOutputSymtabIndex(self: Elf, shndx: u32) u32 {
49535116 if (self.eh_frame_section_index) |index| {
49545117 if (index == shndx) return @intCast(self.output_sections.keys().len + 1);
49555118 }
5119 const base: usize = if (self.eh_frame_section_index == null) 0 else 1;
5120 for (self.merge_sections.items, 0..) |msec, index| {
5121 if (msec.output_section_index == shndx) return @intCast(self.output_sections.keys().len + 1 + index + base);
5122 }
49565123 return @intCast(self.output_sections.getIndex(shndx).? + 1);
49575124}
49585125
......@@ -5458,6 +5625,50 @@ pub fn addAtom(self: *Elf) !Atom.Index {
54585625 return index;
54595626}
54605627
5628pub fn addAtomExtra(self: *Elf, extra: Atom.Extra) !u32 {
5629 const fields = @typeInfo(Atom.Extra).Struct.fields;
5630 try self.atoms_extra.ensureUnusedCapacity(self.base.comp.gpa, fields.len);
5631 return self.addAtomExtraAssumeCapacity(extra);
5632}
5633
5634pub fn addAtomExtraAssumeCapacity(self: *Elf, extra: Atom.Extra) u32 {
5635 const index = @as(u32, @intCast(self.atoms_extra.items.len));
5636 const fields = @typeInfo(Atom.Extra).Struct.fields;
5637 inline for (fields) |field| {
5638 self.atoms_extra.appendAssumeCapacity(switch (field.type) {
5639 u32 => @field(extra, field.name),
5640 else => @compileError("bad field type"),
5641 });
5642 }
5643 return index;
5644}
5645
5646pub fn atomExtra(self: *Elf, index: u32) ?Atom.Extra {
5647 if (index == 0) return null;
5648 const fields = @typeInfo(Atom.Extra).Struct.fields;
5649 var i: usize = index;
5650 var result: Atom.Extra = undefined;
5651 inline for (fields) |field| {
5652 @field(result, field.name) = switch (field.type) {
5653 u32 => self.atoms_extra.items[i],
5654 else => @compileError("bad field type"),
5655 };
5656 i += 1;
5657 }
5658 return result;
5659}
5660
5661pub fn setAtomExtra(self: *Elf, index: u32, extra: Atom.Extra) void {
5662 assert(index > 0);
5663 const fields = @typeInfo(Atom.Extra).Struct.fields;
5664 inline for (fields, 0..) |field, i| {
5665 self.atoms_extra.items[index + i] = switch (field.type) {
5666 u32 => @field(extra, field.name),
5667 else => @compileError("bad field type"),
5668 };
5669 }
5670}
5671
54615672pub fn addThunk(self: *Elf) !Thunk.Index {
54625673 const index = @as(Thunk.Index, @intCast(self.thunks.items.len));
54635674 const th = try self.thunks.addOne(self.base.comp.gpa);
......@@ -5637,35 +5848,88 @@ pub fn comdatGroupOwner(self: *Elf, index: ComdatGroupOwner.Index) *ComdatGroupO
56375848 return &self.comdat_groups_owners.items[index];
56385849}
56395850
5640pub fn gotAddress(self: *Elf) u64 {
5851pub fn addInputMergeSection(self: *Elf) !InputMergeSection.Index {
5852 const index: InputMergeSection.Index = @intCast(self.merge_input_sections.items.len);
5853 const msec = try self.merge_input_sections.addOne(self.base.comp.gpa);
5854 msec.* = .{};
5855 return index;
5856}
5857
5858pub fn inputMergeSection(self: *Elf, index: InputMergeSection.Index) ?*InputMergeSection {
5859 if (index == 0) return null;
5860 return &self.merge_input_sections.items[index];
5861}
5862
5863pub fn addMergeSubsection(self: *Elf) !MergeSubsection.Index {
5864 const index: MergeSubsection.Index = @intCast(self.merge_subsections.items.len);
5865 const msec = try self.merge_subsections.addOne(self.base.comp.gpa);
5866 msec.* = .{};
5867 return index;
5868}
5869
5870pub fn mergeSubsection(self: *Elf, index: MergeSubsection.Index) *MergeSubsection {
5871 assert(index < self.merge_subsections.items.len);
5872 return &self.merge_subsections.items[index];
5873}
5874
5875pub fn getOrCreateMergeSection(self: *Elf, name: []const u8, flags: u64, @"type": u32) !MergeSection.Index {
5876 const gpa = self.base.comp.gpa;
5877 const out_name = name: {
5878 if (self.base.isRelocatable()) break :name name;
5879 if (mem.eql(u8, name, ".rodata") or mem.startsWith(u8, name, ".rodata"))
5880 break :name if (flags & elf.SHF_STRINGS != 0) ".rodata.str" else ".rodata.cst";
5881 break :name name;
5882 };
5883 const out_off = try self.strings.insert(gpa, out_name);
5884 const out_flags = flags & ~@as(u64, elf.SHF_COMPRESSED | elf.SHF_GROUP);
5885 for (self.merge_sections.items, 0..) |msec, index| {
5886 if (msec.name_offset == out_off) return @intCast(index);
5887 }
5888 const index = @as(MergeSection.Index, @intCast(self.merge_sections.items.len));
5889 const msec = try self.merge_sections.addOne(gpa);
5890 msec.* = .{
5891 .name_offset = out_off,
5892 .flags = out_flags,
5893 .type = @"type",
5894 };
5895 return index;
5896}
5897
5898pub fn mergeSection(self: *Elf, index: MergeSection.Index) *MergeSection {
5899 assert(index < self.merge_sections.items.len);
5900 return &self.merge_sections.items[index];
5901}
5902
5903pub fn gotAddress(self: *Elf) i64 {
56415904 const shndx = blk: {
56425905 if (self.getTarget().cpu.arch == .x86_64 and self.got_plt_section_index != null)
56435906 break :blk self.got_plt_section_index.?;
56445907 break :blk if (self.got_section_index) |shndx| shndx else null;
56455908 };
5646 return if (shndx) |index| self.shdrs.items[index].sh_addr else 0;
5909 return if (shndx) |index| @intCast(self.shdrs.items[index].sh_addr) else 0;
56475910}
56485911
5649pub fn tpAddress(self: *Elf) u64 {
5912pub fn tpAddress(self: *Elf) i64 {
56505913 const index = self.phdr_tls_index orelse return 0;
56515914 const phdr = self.phdrs.items[index];
5652 return switch (self.getTarget().cpu.arch) {
5915 const addr = switch (self.getTarget().cpu.arch) {
56535916 .x86_64 => mem.alignForward(u64, phdr.p_vaddr + phdr.p_memsz, phdr.p_align),
56545917 .aarch64 => mem.alignBackward(u64, phdr.p_vaddr - 16, phdr.p_align),
56555918 else => @panic("TODO implement getTpAddress for this arch"),
56565919 };
5920 return @intCast(addr);
56575921}
56585922
5659pub fn dtpAddress(self: *Elf) u64 {
5923pub fn dtpAddress(self: *Elf) i64 {
56605924 const index = self.phdr_tls_index orelse return 0;
56615925 const phdr = self.phdrs.items[index];
5662 return phdr.p_vaddr;
5926 return @intCast(phdr.p_vaddr);
56635927}
56645928
5665pub fn tlsAddress(self: *Elf) u64 {
5929pub fn tlsAddress(self: *Elf) i64 {
56665930 const index = self.phdr_tls_index orelse return 0;
56675931 const phdr = self.phdrs.items[index];
5668 return phdr.p_vaddr;
5932 return @intCast(phdr.p_vaddr);
56695933}
56705934
56715935const ErrorWithNotes = struct {
......@@ -6043,6 +6307,11 @@ fn fmtDumpState(
60436307 try writer.print(" shdr({d}) : COMDAT({d})\n", .{ cg.shndx, cg.cg_index });
60446308 }
60456309
6310 try writer.writeAll("\nOutput merge sections\n");
6311 for (self.merge_sections.items) |msec| {
6312 try writer.print(" shdr({d}) : {}\n", .{ msec.output_section_index, msec.fmt(self) });
6313 }
6314
60466315 try writer.writeAll("\nOutput shdrs\n");
60476316 for (self.shdrs.items, 0..) |shdr, shndx| {
60486317 try writer.print(" shdr({d}) : phdr({?d}) : {}\n", .{
......@@ -6235,6 +6504,7 @@ const gc = @import("Elf/gc.zig");
62356504const glibc = @import("../glibc.zig");
62366505const link = @import("../link.zig");
62376506const lldMain = @import("../main.zig").lldMain;
6507const merge_section = @import("Elf/merge_section.zig");
62386508const musl = @import("../musl.zig");
62396509const relocatable = @import("Elf/relocatable.zig");
62406510const relocation = @import("Elf/relocation.zig");
......@@ -6260,10 +6530,13 @@ const GnuHashSection = synthetic_sections.GnuHashSection;
62606530const GotSection = synthetic_sections.GotSection;
62616531const GotPltSection = synthetic_sections.GotPltSection;
62626532const HashSection = synthetic_sections.HashSection;
6533const InputMergeSection = merge_section.InputMergeSection;
62636534const LdScript = @import("Elf/LdScript.zig");
62646535const LinkerDefined = @import("Elf/LinkerDefined.zig");
62656536const Liveness = @import("../Liveness.zig");
62666537const LlvmObject = @import("../codegen/llvm.zig").Object;
6538const MergeSection = merge_section.MergeSection;
6539const MergeSubsection = merge_section.MergeSubsection;
62676540const Module = @import("../Module.zig");
62686541const Object = @import("Elf/Object.zig");
62696542const InternPool = @import("../InternPool.zig");
src/link/Elf/Atom.zig+187-114
......@@ -1,5 +1,5 @@
11/// Address allocated for this Atom.
2value: u64 = 0,
2value: i64 = 0,
33
44/// Name of this Atom.
55name_offset: u32 = 0,
......@@ -22,32 +22,19 @@ output_section_index: u32 = 0,
2222/// Index of the input section containing this atom's relocs.
2323relocs_section_index: u32 = 0,
2424
25/// Start index of the relocations belonging to this atom.
26rel_index: u32 = 0,
27
28/// Number of relocations belonging to this atom.
29rel_num: u32 = 0,
30
3125/// Index of this atom in the linker's atoms table.
3226atom_index: Index = 0,
3327
34/// Index of the thunk for this atom.
35thunk_index: Thunk.Index = 0,
36
37/// Flags we use for state tracking.
38flags: Flags = .{},
39
40/// Start index of FDEs referencing this atom.
41fde_start: u32 = 0,
42
43/// End index of FDEs referencing this atom.
44fde_end: u32 = 0,
45
4628/// Points to the previous and next neighbors, based on the `text_offset`.
4729/// This can be used to find, for example, the capacity of this `TextBlock`.
4830prev_index: Index = 0,
4931next_index: Index = 0,
5032
33/// Flags we use for state tracking.
34flags: Flags = .{},
35
36extra_index: u32 = 0,
37
5138pub const Alignment = @import("../../InternPool.zig").Alignment;
5239
5340pub fn name(self: Atom, elf_file: *Elf) []const u8 {
......@@ -57,10 +44,22 @@ pub fn name(self: Atom, elf_file: *Elf) []const u8 {
5744 };
5845}
5946
60pub fn address(self: Atom, elf_file: *Elf) u64 {
47pub fn address(self: Atom, elf_file: *Elf) i64 {
6148 const shndx = self.outputShndx() orelse return self.value;
6249 const shdr = elf_file.shdrs.items[shndx];
63 return shdr.sh_addr + self.value;
50 return @as(i64, @intCast(shdr.sh_addr)) + self.value;
51}
52
53pub fn debugTombstoneValue(self: Atom, target: Symbol, elf_file: *Elf) ?u64 {
54 if (target.mergeSubsection(elf_file)) |msub| {
55 if (msub.alive) return null;
56 }
57 if (target.atom(elf_file)) |atom_ptr| {
58 if (atom_ptr.flags.alive) return null;
59 }
60 const atom_name = self.name(elf_file);
61 if (!mem.startsWith(u8, atom_name, ".debug")) return null;
62 return if (mem.eql(u8, atom_name, ".debug_loc") or mem.eql(u8, atom_name, ".debug_ranges")) 1 else 0;
6463}
6564
6665pub fn file(self: Atom, elf_file: *Elf) ?File {
......@@ -68,7 +67,9 @@ pub fn file(self: Atom, elf_file: *Elf) ?File {
6867}
6968
7069pub fn thunk(self: Atom, elf_file: *Elf) *Thunk {
71 return elf_file.thunk(self.thunk_index);
70 assert(self.flags.thunk);
71 const extras = self.extra(elf_file).?;
72 return elf_file.thunk(extras.thunk);
7273}
7374
7475pub fn inputShdr(self: Atom, elf_file: *Elf) elf.Elf64_Shdr {
......@@ -102,13 +103,13 @@ pub fn capacity(self: Atom, elf_file: *Elf) u64 {
102103 next.address(elf_file)
103104 else
104105 std.math.maxInt(u32);
105 return next_addr - self.address(elf_file);
106 return @intCast(next_addr - self.address(elf_file));
106107}
107108
108109pub fn freeListEligible(self: Atom, elf_file: *Elf) bool {
109110 // No need to keep a free list node for the last block.
110111 const next = elf_file.atom(self.next_index) orelse return false;
111 const cap = next.address(elf_file) - self.address(elf_file);
112 const cap: u64 = @intCast(next.address(elf_file) - self.address(elf_file));
112113 const ideal_cap = Elf.padToIdeal(self.size);
113114 if (cap <= ideal_cap) return false;
114115 const surplus = cap - ideal_cap;
......@@ -141,8 +142,8 @@ pub fn allocate(self: *Atom, elf_file: *Elf) !void {
141142 // Is it enough that we could fit this new atom?
142143 const cap = big_atom.capacity(elf_file);
143144 const ideal_capacity = Elf.padToIdeal(cap);
144 const ideal_capacity_end_vaddr = std.math.add(u64, big_atom.value, ideal_capacity) catch ideal_capacity;
145 const capacity_end_vaddr = big_atom.value + cap;
145 const ideal_capacity_end_vaddr = std.math.add(u64, @intCast(big_atom.value), ideal_capacity) catch ideal_capacity;
146 const capacity_end_vaddr = @as(u64, @intCast(big_atom.value)) + cap;
146147 const new_start_vaddr_unaligned = capacity_end_vaddr - new_atom_ideal_capacity;
147148 const new_start_vaddr = self.alignment.backward(new_start_vaddr_unaligned);
148149 if (new_start_vaddr < ideal_capacity_end_vaddr) {
......@@ -167,14 +168,14 @@ pub fn allocate(self: *Atom, elf_file: *Elf) !void {
167168 if (!keep_free_list_node) {
168169 free_list_removal = i;
169170 }
170 break :blk new_start_vaddr;
171 break :blk @intCast(new_start_vaddr);
171172 } else if (elf_file.atom(last_atom_index.*)) |last| {
172173 const ideal_capacity = Elf.padToIdeal(last.size);
173 const ideal_capacity_end_vaddr = last.value + ideal_capacity;
174 const ideal_capacity_end_vaddr = @as(u64, @intCast(last.value)) + ideal_capacity;
174175 const new_start_vaddr = self.alignment.forward(ideal_capacity_end_vaddr);
175176 // Set up the metadata to be updated, after errors are no longer possible.
176177 atom_placement = last.atom_index;
177 break :blk new_start_vaddr;
178 break :blk @intCast(new_start_vaddr);
178179 } else {
179180 break :blk 0;
180181 }
......@@ -184,7 +185,7 @@ pub fn allocate(self: *Atom, elf_file: *Elf) !void {
184185 self.atom_index,
185186 self.name(elf_file),
186187 self.address(elf_file),
187 self.address(elf_file) + self.size,
188 self.address(elf_file) + @as(i64, @intCast(self.size)),
188189 });
189190
190191 const expand_section = if (atom_placement) |placement_index|
......@@ -192,7 +193,7 @@ pub fn allocate(self: *Atom, elf_file: *Elf) !void {
192193 else
193194 true;
194195 if (expand_section) {
195 const needed_size = self.value + self.size;
196 const needed_size: u64 = @intCast(self.value + @as(i64, @intCast(self.size)));
196197 try elf_file.growAllocSection(self.outputShndx().?, needed_size);
197198 last_atom_index.* = self.atom_index;
198199
......@@ -242,7 +243,7 @@ pub fn shrink(self: *Atom, elf_file: *Elf) void {
242243}
243244
244245pub fn grow(self: *Atom, elf_file: *Elf) !void {
245 if (!self.alignment.check(self.value) or self.size > self.capacity(elf_file))
246 if (!self.alignment.check(@intCast(self.value)) or self.size > self.capacity(elf_file))
246247 try self.allocate(elf_file);
247248}
248249
......@@ -309,11 +310,14 @@ pub fn free(self: *Atom, elf_file: *Elf) void {
309310
310311pub fn relocs(self: Atom, elf_file: *Elf) []const elf.Elf64_Rela {
311312 const shndx = self.relocsShndx() orelse return &[0]elf.Elf64_Rela{};
312 return switch (self.file(elf_file).?) {
313 .zig_object => |x| x.relocs.items[shndx].items,
314 .object => |x| x.relocs.items[self.rel_index..][0..self.rel_num],
313 switch (self.file(elf_file).?) {
314 .zig_object => |x| return x.relocs.items[shndx].items,
315 .object => |x| {
316 const extras = self.extra(elf_file).?;
317 return x.relocs.items[extras.rel_index..][0..extras.rel_count];
318 },
315319 else => unreachable,
316 };
320 }
317321}
318322
319323pub fn writeRelocs(self: Atom, elf_file: *Elf, out_relocs: *std.ArrayList(elf.Elf64_Rela)) !void {
......@@ -329,11 +333,14 @@ pub fn writeRelocs(self: Atom, elf_file: *Elf, out_relocs: *std.ArrayList(elf.El
329333 };
330334 const target = elf_file.symbol(target_index);
331335 const r_type = rel.r_type();
332 const r_offset = self.value + rel.r_offset;
336 const r_offset: u64 = @intCast(self.value + @as(i64, @intCast(rel.r_offset)));
333337 var r_addend = rel.r_addend;
334338 var r_sym: u32 = 0;
335339 switch (target.type(elf_file)) {
336 elf.STT_SECTION => {
340 elf.STT_SECTION => if (target.mergeSubsection(elf_file)) |msub| {
341 r_addend += @intCast(target.address(.{}, elf_file));
342 r_sym = elf_file.sectionSymbolOutputSymtabIndex(msub.mergeSection(elf_file).output_section_index);
343 } else {
337344 r_addend += @intCast(target.address(.{}, elf_file));
338345 r_sym = elf_file.sectionSymbolOutputSymtabIndex(target.outputShndx().?);
339346 },
......@@ -359,9 +366,10 @@ pub fn writeRelocs(self: Atom, elf_file: *Elf, out_relocs: *std.ArrayList(elf.El
359366}
360367
361368pub fn fdes(self: Atom, elf_file: *Elf) []Fde {
362 if (self.fde_start == self.fde_end) return &[0]Fde{};
369 if (!self.flags.fde) return &[0]Fde{};
370 const extras = self.extra(elf_file).?;
363371 const object = self.file(elf_file).?.object;
364 return object.fdes.items[self.fde_start..self.fde_end];
372 return object.fdes.items[extras.fde_start..][0..extras.fde_count];
365373}
366374
367375pub fn markFdesDead(self: Atom, elf_file: *Elf) void {
......@@ -419,6 +427,12 @@ pub fn scanRelocs(self: Atom, elf_file: *Elf, code: ?[]const u8, undefs: anytype
419427 };
420428 const symbol = elf_file.symbol(symbol_index);
421429
430 const is_synthetic_symbol = switch (file_ptr) {
431 .zig_object => false, // TODO: implement this once we support merge sections in ZigObject
432 .object => |x| rel.r_sym() >= x.symtab.items.len,
433 else => unreachable,
434 };
435
422436 // Check for violation of One Definition Rule for COMDATs.
423437 if (symbol.file(elf_file) == null) {
424438 // TODO convert into an error
......@@ -431,7 +445,8 @@ pub fn scanRelocs(self: Atom, elf_file: *Elf, code: ?[]const u8, undefs: anytype
431445 }
432446
433447 // Report an undefined symbol.
434 if (try self.reportUndefined(elf_file, symbol, symbol_index, rel, undefs)) continue;
448 if (!is_synthetic_symbol and (try self.reportUndefined(elf_file, symbol, symbol_index, rel, undefs)))
449 continue;
435450
436451 if (symbol.isIFunc(elf_file)) {
437452 symbol.flags.needs_got = true;
......@@ -743,21 +758,21 @@ pub fn resolveRelocsAlloc(self: Atom, elf_file: *Elf, code: []u8) RelocError!voi
743758 // https://intezer.com/blog/malware-analysis/executable-and-linkable-format-101-part-3-relocations/
744759 //
745760 // Address of the source atom.
746 const P = @as(i64, @intCast(self.address(elf_file) + rel.r_offset));
761 const P = self.address(elf_file) + @as(i64, @intCast(rel.r_offset));
747762 // Addend from the relocation.
748763 const A = rel.r_addend;
749764 // Address of the target symbol - can be address of the symbol within an atom or address of PLT stub.
750 const S = @as(i64, @intCast(target.address(.{}, elf_file)));
765 const S = target.address(.{}, elf_file);
751766 // Address of the global offset table.
752 const GOT = @as(i64, @intCast(elf_file.gotAddress()));
767 const GOT = elf_file.gotAddress();
753768 // Address of the .zig.got table entry if any.
754 const ZIG_GOT = @as(i64, @intCast(target.zigGotAddress(elf_file)));
769 const ZIG_GOT = target.zigGotAddress(elf_file);
755770 // Relative offset to the start of the global offset table.
756 const G = @as(i64, @intCast(target.gotAddress(elf_file))) - GOT;
771 const G = target.gotAddress(elf_file) - GOT;
757772 // // Address of the thread pointer.
758 const TP = @as(i64, @intCast(elf_file.tpAddress()));
773 const TP = elf_file.tpAddress();
759774 // Address of the dynamic thread pointer.
760 const DTP = @as(i64, @intCast(elf_file.dtpAddress()));
775 const DTP = elf_file.dtpAddress();
761776
762777 relocs_log.debug(" {s}: {x}: [{x} => {x}] G({x}) ZG({x}) ({s})", .{
763778 relocation.fmtRelocType(rel.r_type(), cpu_arch),
......@@ -814,9 +829,9 @@ fn resolveDynAbsReloc(
814829 const comp = elf_file.base.comp;
815830 const gpa = comp.gpa;
816831 const cpu_arch = elf_file.getTarget().cpu.arch;
817 const P = self.address(elf_file) + rel.r_offset;
832 const P: u64 = @intCast(self.address(elf_file) + @as(i64, @intCast(rel.r_offset)));
818833 const A = rel.r_addend;
819 const S = @as(i64, @intCast(target.address(.{}, elf_file)));
834 const S = target.address(.{}, elf_file);
820835 const is_writeable = self.inputShdr(elf_file).sh_flags & elf.SHF_WRITE != 0;
821836
822837 const num_dynrelocs = switch (self.file(elf_file).?) {
......@@ -884,7 +899,7 @@ fn resolveDynAbsReloc(
884899 },
885900
886901 .ifunc => {
887 const S_ = @as(i64, @intCast(target.address(.{ .plt = false }, elf_file)));
902 const S_ = target.address(.{ .plt = false }, elf_file);
888903 elf_file.addRelaDynAssumeCapacity(.{
889904 .offset = P,
890905 .type = relocation.encode(.irel, cpu_arch),
......@@ -924,6 +939,11 @@ pub fn resolveRelocsNonAlloc(self: Atom, elf_file: *Elf, code: []u8, undefs: any
924939 else => unreachable,
925940 };
926941 const target = elf_file.symbol(target_index);
942 const is_synthetic_symbol = switch (file_ptr) {
943 .zig_object => false, // TODO: implement this once we support merge sections in ZigObject
944 .object => |x| rel.r_sym() >= x.symtab.items.len,
945 else => unreachable,
946 };
927947
928948 // Check for violation of One Definition Rule for COMDATs.
929949 if (target.file(elf_file) == null) {
......@@ -937,20 +957,21 @@ pub fn resolveRelocsNonAlloc(self: Atom, elf_file: *Elf, code: []u8, undefs: any
937957 }
938958
939959 // Report an undefined symbol.
940 if (try self.reportUndefined(elf_file, target, target_index, rel, undefs)) continue;
960 if (!is_synthetic_symbol and (try self.reportUndefined(elf_file, target, target_index, rel, undefs)))
961 continue;
941962
942963 // We will use equation format to resolve relocations:
943964 // https://intezer.com/blog/malware-analysis/executable-and-linkable-format-101-part-3-relocations/
944965 //
945 const P = @as(i64, @intCast(self.address(elf_file) + rel.r_offset));
966 const P = self.address(elf_file) + @as(i64, @intCast(rel.r_offset));
946967 // Addend from the relocation.
947968 const A = rel.r_addend;
948969 // Address of the target symbol - can be address of the symbol within an atom or address of PLT stub.
949 const S = @as(i64, @intCast(target.address(.{}, elf_file)));
970 const S = target.address(.{}, elf_file);
950971 // Address of the global offset table.
951 const GOT = @as(i64, @intCast(elf_file.gotAddress()));
972 const GOT = elf_file.gotAddress();
952973 // Address of the dynamic thread pointer.
953 const DTP = @as(i64, @intCast(elf_file.dtpAddress()));
974 const DTP = elf_file.dtpAddress();
954975
955976 const args = ResolveArgs{ P, A, S, GOT, 0, 0, DTP, 0 };
956977
......@@ -984,6 +1005,35 @@ pub fn resolveRelocsNonAlloc(self: Atom, elf_file: *Elf, code: []u8, undefs: any
9841005 if (has_reloc_errors) return error.RelocFailure;
9851006}
9861007
1008const AddExtraOpts = struct {
1009 thunk: ?u32 = null,
1010 fde_start: ?u32 = null,
1011 fde_count: ?u32 = null,
1012 rel_index: ?u32 = null,
1013 rel_count: ?u32 = null,
1014};
1015
1016pub fn addExtra(atom: *Atom, opts: AddExtraOpts, elf_file: *Elf) !void {
1017 if (atom.extra(elf_file) == null) {
1018 atom.extra_index = try elf_file.addAtomExtra(.{});
1019 }
1020 var extras = atom.extra(elf_file).?;
1021 inline for (@typeInfo(@TypeOf(opts)).Struct.fields) |field| {
1022 if (@field(opts, field.name)) |x| {
1023 @field(extras, field.name) = x;
1024 }
1025 }
1026 atom.setExtra(extras, elf_file);
1027}
1028
1029pub inline fn extra(atom: Atom, elf_file: *Elf) ?Extra {
1030 return elf_file.atomExtra(atom.extra_index);
1031}
1032
1033pub inline fn setExtra(atom: Atom, extras: Extra, elf_file: *Elf) void {
1034 elf_file.setAtomExtra(atom.extra_index, extras);
1035}
1036
9871037pub fn format(
9881038 atom: Atom,
9891039 comptime unused_fmt_string: []const u8,
......@@ -1023,12 +1073,13 @@ fn format2(
10231073 atom.atom_index, atom.name(elf_file), atom.address(elf_file),
10241074 atom.output_section_index, atom.alignment, atom.size,
10251075 });
1026 if (atom.fde_start != atom.fde_end) {
1076 if (atom.flags.fde) {
10271077 try writer.writeAll(" : fdes{ ");
1028 for (atom.fdes(elf_file), atom.fde_start..) |fde, i| {
1078 const extras = atom.extra(elf_file).?;
1079 for (atom.fdes(elf_file), extras.fde_start..) |fde, i| {
10291080 try writer.print("{d}", .{i});
10301081 if (!fde.alive) try writer.writeAll("([*])");
1031 if (i < atom.fde_end - 1) try writer.writeAll(", ");
1082 if (i - extras.fde_start < extras.fde_count - 1) try writer.writeAll(", ");
10321083 }
10331084 try writer.writeAll(" }");
10341085 }
......@@ -1045,6 +1096,12 @@ pub const Flags = packed struct {
10451096
10461097 /// Specifies if the atom has been visited during garbage collection.
10471098 visited: bool = false,
1099
1100 /// Whether this atom has a range extension thunk.
1101 thunk: bool = false,
1102
1103 /// Whether this atom has FDE records.
1104 fde: bool = false,
10481105};
10491106
10501107const x86_64 = struct {
......@@ -1235,10 +1292,10 @@ const x86_64 = struct {
12351292
12361293 .TLSGD => {
12371294 if (target.flags.has_tlsgd) {
1238 const S_ = @as(i64, @intCast(target.tlsGdAddress(elf_file)));
1295 const S_ = target.tlsGdAddress(elf_file);
12391296 try cwriter.writeInt(i32, @as(i32, @intCast(S_ + A - P)), .little);
12401297 } else if (target.flags.has_gottp) {
1241 const S_ = @as(i64, @intCast(target.gotTpAddress(elf_file)));
1298 const S_ = target.gotTpAddress(elf_file);
12421299 try x86_64.relaxTlsGdToIe(atom, &.{ rel, it.next().? }, @intCast(S_ - P), elf_file, stream);
12431300 } else {
12441301 try x86_64.relaxTlsGdToLe(
......@@ -1254,13 +1311,13 @@ const x86_64 = struct {
12541311 .TLSLD => {
12551312 if (elf_file.got.tlsld_index) |entry_index| {
12561313 const tlsld_entry = elf_file.got.entries.items[entry_index];
1257 const S_ = @as(i64, @intCast(tlsld_entry.address(elf_file)));
1314 const S_ = tlsld_entry.address(elf_file);
12581315 try cwriter.writeInt(i32, @as(i32, @intCast(S_ + A - P)), .little);
12591316 } else {
12601317 try x86_64.relaxTlsLdToLe(
12611318 atom,
12621319 &.{ rel, it.next().? },
1263 @as(i32, @intCast(TP - @as(i64, @intCast(elf_file.tlsAddress())))),
1320 @as(i32, @intCast(TP - elf_file.tlsAddress())),
12641321 elf_file,
12651322 stream,
12661323 );
......@@ -1269,7 +1326,7 @@ const x86_64 = struct {
12691326
12701327 .GOTPC32_TLSDESC => {
12711328 if (target.flags.has_tlsdesc) {
1272 const S_ = @as(i64, @intCast(target.tlsDescAddress(elf_file)));
1329 const S_ = target.tlsDescAddress(elf_file);
12731330 try cwriter.writeInt(i32, @as(i32, @intCast(S_ + A - P)), .little);
12741331 } else {
12751332 x86_64.relaxGotPcTlsDesc(code[r_offset - 3 ..]) catch {
......@@ -1293,7 +1350,7 @@ const x86_64 = struct {
12931350
12941351 .GOTTPOFF => {
12951352 if (target.flags.has_gottp) {
1296 const S_ = @as(i64, @intCast(target.gotTpAddress(elf_file)));
1353 const S_ = target.gotTpAddress(elf_file);
12971354 try cwriter.writeInt(i32, @as(i32, @intCast(S_ + A - P)), .little);
12981355 } else {
12991356 x86_64.relaxGotTpOff(code[r_offset - 3 ..]);
......@@ -1336,9 +1393,18 @@ const x86_64 = struct {
13361393 .@"16" => try cwriter.writeInt(u16, @as(u16, @bitCast(@as(i16, @intCast(S + A)))), .little),
13371394 .@"32" => try cwriter.writeInt(u32, @as(u32, @bitCast(@as(i32, @intCast(S + A)))), .little),
13381395 .@"32S" => try cwriter.writeInt(i32, @as(i32, @intCast(S + A)), .little),
1339 .@"64" => try cwriter.writeInt(i64, S + A, .little),
1340 .DTPOFF32 => try cwriter.writeInt(i32, @as(i32, @intCast(S + A - DTP)), .little),
1341 .DTPOFF64 => try cwriter.writeInt(i64, S + A - DTP, .little),
1396 .@"64" => if (atom.debugTombstoneValue(target.*, elf_file)) |value|
1397 try cwriter.writeInt(u64, value, .little)
1398 else
1399 try cwriter.writeInt(i64, S + A, .little),
1400 .DTPOFF32 => if (atom.debugTombstoneValue(target.*, elf_file)) |value|
1401 try cwriter.writeInt(u64, value, .little)
1402 else
1403 try cwriter.writeInt(i32, @as(i32, @intCast(S + A - DTP)), .little),
1404 .DTPOFF64 => if (atom.debugTombstoneValue(target.*, elf_file)) |value|
1405 try cwriter.writeInt(u64, value, .little)
1406 else
1407 try cwriter.writeInt(i64, S + A - DTP, .little),
13421408 .GOTOFF64 => try cwriter.writeInt(i64, S + A - GOT, .little),
13431409 .GOTPC64 => try cwriter.writeInt(i64, GOT + A, .little),
13441410 .SIZE32 => {
......@@ -1720,7 +1786,7 @@ const aarch64 = struct {
17201786 .object => |x| x.symbols.items[rel.r_sym()],
17211787 else => unreachable,
17221788 };
1723 const S_: i64 = @intCast(th.targetAddress(target_index, elf_file));
1789 const S_ = th.targetAddress(target_index, elf_file);
17241790 break :blk math.cast(i28, S_ + A - P) orelse return error.Overflow;
17251791 };
17261792 aarch64_util.writeBranchImm(disp, code);
......@@ -1738,16 +1804,12 @@ const aarch64 = struct {
17381804
17391805 .ADR_PREL_PG_HI21 => {
17401806 // TODO: check for relaxation of ADRP+ADD
1741 const saddr = @as(u64, @intCast(P));
1742 const taddr = @as(u64, @intCast(S + A));
1743 const pages = @as(u21, @bitCast(try aarch64_util.calcNumberOfPages(saddr, taddr)));
1807 const pages = @as(u21, @bitCast(try aarch64_util.calcNumberOfPages(P, S + A)));
17441808 aarch64_util.writeAdrpInst(pages, code);
17451809 },
17461810
17471811 .ADR_GOT_PAGE => if (target.flags.has_got) {
1748 const saddr = @as(u64, @intCast(P));
1749 const taddr = @as(u64, @intCast(G + GOT + A));
1750 const pages = @as(u21, @bitCast(try aarch64_util.calcNumberOfPages(saddr, taddr)));
1812 const pages = @as(u21, @bitCast(try aarch64_util.calcNumberOfPages(P, G + GOT + A)));
17511813 aarch64_util.writeAdrpInst(pages, code);
17521814 } else {
17531815 // TODO: relax
......@@ -1802,46 +1864,38 @@ const aarch64 = struct {
18021864 },
18031865
18041866 .TLSIE_ADR_GOTTPREL_PAGE21 => {
1805 const S_: i64 = @intCast(target.gotTpAddress(elf_file));
1806 const saddr: u64 = @intCast(P);
1807 const taddr: u64 = @intCast(S_ + A);
1808 relocs_log.debug(" [{x} => {x}]", .{ P, taddr });
1809 const pages: u21 = @bitCast(try aarch64_util.calcNumberOfPages(saddr, taddr));
1867 const S_ = target.gotTpAddress(elf_file);
1868 relocs_log.debug(" [{x} => {x}]", .{ P, S_ + A });
1869 const pages: u21 = @bitCast(try aarch64_util.calcNumberOfPages(P, S_ + A));
18101870 aarch64_util.writeAdrpInst(pages, code);
18111871 },
18121872
18131873 .TLSIE_LD64_GOTTPREL_LO12_NC => {
1814 const S_: i64 = @intCast(target.gotTpAddress(elf_file));
1815 const taddr: u64 = @intCast(S_ + A);
1816 relocs_log.debug(" [{x} => {x}]", .{ P, taddr });
1817 const offset: u12 = try math.divExact(u12, @truncate(taddr), 8);
1874 const S_ = target.gotTpAddress(elf_file);
1875 relocs_log.debug(" [{x} => {x}]", .{ P, S_ + A });
1876 const offset: u12 = try math.divExact(u12, @truncate(@as(u64, @bitCast(S_ + A))), 8);
18181877 aarch64_util.writeLoadStoreRegInst(offset, code);
18191878 },
18201879
18211880 .TLSGD_ADR_PAGE21 => {
1822 const S_: i64 = @intCast(target.tlsGdAddress(elf_file));
1823 const saddr: u64 = @intCast(P);
1824 const taddr: u64 = @intCast(S_ + A);
1825 relocs_log.debug(" [{x} => {x}]", .{ P, taddr });
1826 const pages: u21 = @bitCast(try aarch64_util.calcNumberOfPages(saddr, taddr));
1881 const S_ = target.tlsGdAddress(elf_file);
1882 relocs_log.debug(" [{x} => {x}]", .{ P, S_ + A });
1883 const pages: u21 = @bitCast(try aarch64_util.calcNumberOfPages(P, S_ + A));
18271884 aarch64_util.writeAdrpInst(pages, code);
18281885 },
18291886
18301887 .TLSGD_ADD_LO12_NC => {
1831 const S_: i64 = @intCast(target.tlsGdAddress(elf_file));
1832 const taddr: u64 = @intCast(S_ + A);
1833 relocs_log.debug(" [{x} => {x}]", .{ P, taddr });
1834 const offset: u12 = @truncate(taddr);
1888 const S_ = target.tlsGdAddress(elf_file);
1889 relocs_log.debug(" [{x} => {x}]", .{ P, S_ + A });
1890 const offset: u12 = @truncate(@as(u64, @bitCast(S_ + A)));
18351891 aarch64_util.writeAddImmInst(offset, code);
18361892 },
18371893
18381894 .TLSDESC_ADR_PAGE21 => {
18391895 if (target.flags.has_tlsdesc) {
1840 const S_: i64 = @intCast(target.tlsDescAddress(elf_file));
1841 const saddr: u64 = @intCast(P);
1842 const taddr: u64 = @intCast(S_ + A);
1843 relocs_log.debug(" [{x} => {x}]", .{ P, taddr });
1844 const pages: u21 = @bitCast(try aarch64_util.calcNumberOfPages(saddr, taddr));
1896 const S_ = target.tlsDescAddress(elf_file);
1897 relocs_log.debug(" [{x} => {x}]", .{ P, S_ + A });
1898 const pages: u21 = @bitCast(try aarch64_util.calcNumberOfPages(P, S_ + A));
18451899 aarch64_util.writeAdrpInst(pages, code);
18461900 } else {
18471901 relocs_log.debug(" relaxing adrp => nop", .{});
......@@ -1851,10 +1905,9 @@ const aarch64 = struct {
18511905
18521906 .TLSDESC_LD64_LO12 => {
18531907 if (target.flags.has_tlsdesc) {
1854 const S_: i64 = @intCast(target.tlsDescAddress(elf_file));
1855 const taddr: u64 = @intCast(S_ + A);
1856 relocs_log.debug(" [{x} => {x}]", .{ P, taddr });
1857 const offset: u12 = try math.divExact(u12, @truncate(taddr), 8);
1908 const S_ = target.tlsDescAddress(elf_file);
1909 relocs_log.debug(" [{x} => {x}]", .{ P, S_ + A });
1910 const offset: u12 = try math.divExact(u12, @truncate(@as(u64, @bitCast(S_ + A))), 8);
18581911 aarch64_util.writeLoadStoreRegInst(offset, code);
18591912 } else {
18601913 relocs_log.debug(" relaxing ldr => nop", .{});
......@@ -1864,10 +1917,9 @@ const aarch64 = struct {
18641917
18651918 .TLSDESC_ADD_LO12 => {
18661919 if (target.flags.has_tlsdesc) {
1867 const S_: i64 = @intCast(target.tlsDescAddress(elf_file));
1868 const taddr: u64 = @intCast(S_ + A);
1869 relocs_log.debug(" [{x} => {x}]", .{ P, taddr });
1870 const offset: u12 = @truncate(taddr);
1920 const S_ = target.tlsDescAddress(elf_file);
1921 relocs_log.debug(" [{x} => {x}]", .{ P, S_ + A });
1922 const offset: u12 = @truncate(@as(u64, @bitCast(S_ + A)));
18711923 aarch64_util.writeAddImmInst(offset, code);
18721924 } else {
18731925 const old_inst = Instruction{
......@@ -1912,7 +1964,6 @@ const aarch64 = struct {
19121964 ) !void {
19131965 _ = it;
19141966 _ = code;
1915 _ = target;
19161967
19171968 const r_type: elf.R_AARCH64 = @enumFromInt(rel.r_type());
19181969 const cwriter = stream.writer();
......@@ -1922,7 +1973,10 @@ const aarch64 = struct {
19221973 switch (r_type) {
19231974 .NONE => unreachable,
19241975 .ABS32 => try cwriter.writeInt(i32, @as(i32, @intCast(S + A)), .little),
1925 .ABS64 => try cwriter.writeInt(i64, S + A, .little),
1976 .ABS64 => if (atom.debugTombstoneValue(target.*, elf_file)) |value|
1977 try cwriter.writeInt(u64, value, .little)
1978 else
1979 try cwriter.writeInt(i64, S + A, .little),
19261980 else => try atom.reportUnhandledRelocError(rel, elf_file),
19271981 }
19281982 }
......@@ -2047,7 +2101,7 @@ const riscv = struct {
20472101 const atom_addr = atom.address(elf_file);
20482102 const pos = it.pos;
20492103 const pair = while (it.prev()) |pair| {
2050 if (S == atom_addr + pair.r_offset) break pair;
2104 if (S == atom_addr + @as(i64, @intCast(pair.r_offset))) break pair;
20512105 } else {
20522106 // TODO: implement searching forward
20532107 var err = try elf_file.addErrorWithNotes(1);
......@@ -2065,10 +2119,10 @@ const riscv = struct {
20652119 .object => |x| elf_file.symbol(x.symbols.items[pair.r_sym()]),
20662120 else => unreachable,
20672121 };
2068 const S_ = @as(i64, @intCast(target_.address(.{}, elf_file)));
2122 const S_ = target_.address(.{}, elf_file);
20692123 const A_ = pair.r_addend;
2070 const P_ = @as(i64, @intCast(atom_addr + pair.r_offset));
2071 const G_ = @as(i64, @intCast(target_.gotAddress(elf_file))) - GOT;
2124 const P_ = atom_addr + @as(i64, @intCast(pair.r_offset));
2125 const G_ = target_.gotAddress(elf_file) - GOT;
20722126 const disp = switch (@as(elf.R_RISCV, @enumFromInt(pair.r_type()))) {
20732127 .PCREL_HI20 => math.cast(i32, S_ + A_ - P_) orelse return error.Overflow,
20742128 .GOT_HI20 => math.cast(i32, G_ + GOT + A_ - P_) orelse return error.Overflow,
......@@ -2096,7 +2150,6 @@ const riscv = struct {
20962150 code: []u8,
20972151 stream: anytype,
20982152 ) !void {
2099 _ = target;
21002153 _ = it;
21012154
21022155 const r_type: elf.R_RISCV = @enumFromInt(rel.r_type());
......@@ -2111,7 +2164,10 @@ const riscv = struct {
21112164 .NONE => unreachable,
21122165
21132166 .@"32" => try cwriter.writeInt(i32, @as(i32, @intCast(S + A)), .little),
2114 .@"64" => try cwriter.writeInt(i64, S + A, .little),
2167 .@"64" => if (atom.debugTombstoneValue(target.*, elf_file)) |value|
2168 try cwriter.writeInt(u64, value, .little)
2169 else
2170 try cwriter.writeInt(i64, S + A, .little),
21152171
21162172 .ADD8 => riscv_util.writeAddend(i8, .add, code[r_offset..][0..1], S + A),
21172173 .SUB8 => riscv_util.writeAddend(i8, .sub, code[r_offset..][0..1], S + A),
......@@ -2170,6 +2226,23 @@ const RelocsIterator = struct {
21702226 }
21712227};
21722228
2229pub const Extra = struct {
2230 /// Index of the range extension thunk of this atom.
2231 thunk: u32 = 0,
2232
2233 /// Start index of FDEs referencing this atom.
2234 fde_start: u32 = 0,
2235
2236 /// Count of FDEs referencing this atom.
2237 fde_count: u32 = 0,
2238
2239 /// Start index of relocations belonging to this atom.
2240 rel_index: u32 = 0,
2241
2242 /// Count of relocations belonging to this atom.
2243 rel_count: u32 = 0,
2244};
2245
21732246const std = @import("std");
21742247const assert = std.debug.assert;
21752248const elf = std.elf;
src/link/Elf/LinkerDefined.zig+2-2
......@@ -60,10 +60,10 @@ pub fn updateSymtabSize(self: *LinkerDefined, elf_file: *Elf) !void {
6060 if (file_ptr.index() != self.index) continue;
6161 global.flags.output_symtab = true;
6262 if (global.isLocal(elf_file)) {
63 try global.setOutputSymtabIndex(self.output_symtab_ctx.nlocals, elf_file);
63 try global.addExtra(.{ .symtab = self.output_symtab_ctx.nlocals }, elf_file);
6464 self.output_symtab_ctx.nlocals += 1;
6565 } else {
66 try global.setOutputSymtabIndex(self.output_symtab_ctx.nglobals, elf_file);
66 try global.addExtra(.{ .symtab = self.output_symtab_ctx.nglobals }, elf_file);
6767 self.output_symtab_ctx.nglobals += 1;
6868 }
6969 self.output_symtab_ctx.strsize += @as(u32, @intCast(global.name(elf_file).len)) + 1;
src/link/Elf/Object.zig+230-19
......@@ -15,6 +15,8 @@ comdat_groups: std.ArrayListUnmanaged(Elf.ComdatGroup.Index) = .{},
1515comdat_group_data: std.ArrayListUnmanaged(u32) = .{},
1616relocs: std.ArrayListUnmanaged(elf.Elf64_Rela) = .{},
1717
18merge_sections: std.ArrayListUnmanaged(InputMergeSection.Index) = .{},
19
1820fdes: std.ArrayListUnmanaged(Fde) = .{},
1921cies: std.ArrayListUnmanaged(Cie) = .{},
2022eh_frame_data: std.ArrayListUnmanaged(u8) = .{},
......@@ -51,6 +53,7 @@ pub fn deinit(self: *Object, allocator: Allocator) void {
5153 self.fdes.deinit(allocator);
5254 self.cies.deinit(allocator);
5355 self.eh_frame_data.deinit(allocator);
56 self.merge_sections.deinit(allocator);
5457}
5558
5659pub fn parse(self: *Object, elf_file: *Elf) !void {
......@@ -242,11 +245,12 @@ fn initAtoms(self: *Object, allocator: Allocator, handle: std.fs.File, elf_file:
242245 const relocs = try self.preadRelocsAlloc(allocator, handle, @intCast(i));
243246 defer allocator.free(relocs);
244247 atom.relocs_section_index = @intCast(i);
245 atom.rel_index = @intCast(self.relocs.items.len);
246 atom.rel_num = @intCast(relocs.len);
248 const rel_index: u32 = @intCast(self.relocs.items.len);
249 const rel_count: u32 = @intCast(relocs.len);
250 try atom.addExtra(.{ .rel_index = rel_index, .rel_count = rel_count }, elf_file);
247251 try self.relocs.appendUnalignedSlice(allocator, relocs);
248252 if (elf_file.getTarget().cpu.arch == .riscv64) {
249 sortRelocs(self.relocs.items[atom.rel_index..][0..atom.rel_num]);
253 sortRelocs(self.relocs.items[rel_index..][0..rel_count]);
250254 }
251255 }
252256 },
......@@ -279,8 +283,7 @@ fn initOutputSection(self: Object, elf_file: *Elf, shdr: elf.Elf64_Shdr) error{O
279283 const name = blk: {
280284 const name = self.getString(shdr.sh_name);
281285 if (elf_file.base.isRelocatable()) break :blk name;
282 if (shdr.sh_flags & elf.SHF_MERGE != 0 and shdr.sh_flags & elf.SHF_STRINGS == 0)
283 break :blk name; // TODO: consider dropping SHF_STRINGS once ICF is implemented
286 if (shdr.sh_flags & elf.SHF_MERGE != 0) break :blk name;
284287 const sh_name_prefixes: []const [:0]const u8 = &.{
285288 ".text", ".data.rel.ro", ".data", ".rodata", ".bss.rel.ro", ".bss",
286289 ".init_array", ".fini_array", ".tbss", ".tdata", ".gcc_except_table", ".ctors",
......@@ -334,7 +337,6 @@ fn skipShdr(self: *Object, index: u32, elf_file: *Elf) bool {
334337 const name = self.getString(shdr.sh_name);
335338 const ignore = blk: {
336339 if (mem.startsWith(u8, name, ".note")) break :blk true;
337 if (mem.startsWith(u8, name, ".comment")) break :blk true;
338340 if (mem.startsWith(u8, name, ".llvm_addrsig")) break :blk true;
339341 if (mem.startsWith(u8, name, ".riscv.attributes")) break :blk true; // TODO: riscv attributes
340342 if (comp.config.debug_format == .strip and shdr.sh_flags & elf.SHF_ALLOC == 0 and
......@@ -353,7 +355,7 @@ fn initSymtab(self: *Object, allocator: Allocator, elf_file: *Elf) !void {
353355 const index = try elf_file.addSymbol();
354356 self.symbols.appendAssumeCapacity(index);
355357 const sym_ptr = elf_file.symbol(index);
356 sym_ptr.value = sym.st_value;
358 sym_ptr.value = @intCast(sym.st_value);
357359 sym_ptr.name_offset = sym.st_name;
358360 sym_ptr.esym_index = @as(u32, @intCast(i));
359361 sym_ptr.atom_index = if (sym.st_shndx == elf.SHN_ABS) 0 else self.atoms.items[sym.st_shndx];
......@@ -445,13 +447,14 @@ fn parseEhFrame(self: *Object, allocator: Allocator, handle: std.fs.File, shndx:
445447 while (i < self.fdes.items.len) {
446448 const fde = self.fdes.items[i];
447449 const atom = fde.atom(elf_file);
448 atom.fde_start = i;
450 const start = i;
449451 i += 1;
450452 while (i < self.fdes.items.len) : (i += 1) {
451453 const next_fde = self.fdes.items[i];
452454 if (atom.atom_index != next_fde.atom(elf_file).atom_index) break;
453455 }
454 atom.fde_end = i;
456 try atom.addExtra(.{ .fde_start = start, .fde_count = i - start }, elf_file);
457 atom.flags.fde = true;
455458 }
456459}
457460
......@@ -545,7 +548,7 @@ pub fn resolveSymbols(self: *Object, elf_file: *Elf) void {
545548 elf.SHN_ABS, elf.SHN_COMMON => 0,
546549 else => self.atoms.items[esym.st_shndx],
547550 };
548 global.value = esym.st_value;
551 global.value = @intCast(esym.st_value);
549552 global.atom_index = atom_index;
550553 global.esym_index = esym_index;
551554 global.file_index = self.index;
......@@ -657,6 +660,178 @@ pub fn checkDuplicates(self: *Object, dupes: anytype, elf_file: *Elf) error{OutO
657660 }
658661}
659662
663pub fn initMergeSections(self: *Object, elf_file: *Elf) !void {
664 const gpa = elf_file.base.comp.gpa;
665
666 try self.merge_sections.resize(gpa, self.shdrs.items.len);
667 @memset(self.merge_sections.items, 0);
668
669 for (self.shdrs.items, 0..) |shdr, shndx| {
670 if (shdr.sh_flags & elf.SHF_MERGE == 0) continue;
671
672 const atom_index = self.atoms.items[shndx];
673 const atom_ptr = elf_file.atom(atom_index) orelse continue;
674 if (!atom_ptr.flags.alive) continue;
675 if (atom_ptr.relocs(elf_file).len > 0) continue;
676
677 const imsec_idx = try elf_file.addInputMergeSection();
678 const imsec = elf_file.inputMergeSection(imsec_idx).?;
679 self.merge_sections.items[shndx] = imsec_idx;
680
681 imsec.merge_section_index = try elf_file.getOrCreateMergeSection(atom_ptr.name(elf_file), shdr.sh_flags, shdr.sh_type);
682 imsec.atom_index = atom_index;
683
684 const data = try self.codeDecompressAlloc(elf_file, atom_index);
685 defer gpa.free(data);
686
687 if (shdr.sh_flags & elf.SHF_STRINGS != 0) {
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),
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 } else {
719 const sh_entsize: u32 = @intCast(shdr.sh_entsize);
720 if (sh_entsize == 0) continue; // Malformed, don't split but don't error out
721 if (shdr.sh_size % sh_entsize != 0) {
722 var err = try elf_file.addErrorWithNotes(1);
723 try err.addMsg(elf_file, "size not a multiple of sh_entsize", .{});
724 try err.addNote(elf_file, "in {}:{s}", .{ self.fmtPath(), atom_ptr.name(elf_file) });
725 return error.MalformedObject;
726 }
727
728 var pos: u32 = 0;
729 while (pos < data.len) : (pos += sh_entsize) {
730 const string = data.ptr[pos..][0..sh_entsize];
731 try imsec.insert(gpa, string);
732 try imsec.offsets.append(gpa, pos);
733 }
734 }
735
736 atom_ptr.flags.alive = false;
737 }
738}
739
740pub fn resolveMergeSubsections(self: *Object, elf_file: *Elf) !void {
741 const gpa = elf_file.base.comp.gpa;
742
743 for (self.merge_sections.items) |index| {
744 const imsec = elf_file.inputMergeSection(index) orelse continue;
745 if (imsec.offsets.items.len == 0) continue;
746 const msec = elf_file.mergeSection(imsec.merge_section_index);
747 const atom_ptr = elf_file.atom(imsec.atom_index).?;
748 const isec = atom_ptr.inputShdr(elf_file);
749
750 try imsec.subsections.resize(gpa, imsec.strings.items.len);
751
752 for (imsec.strings.items, imsec.subsections.items) |str, *imsec_msub| {
753 const string = imsec.bytes.items[str.pos..][0..str.len];
754 const res = try msec.insert(gpa, string);
755 if (!res.found_existing) {
756 const msub_index = try elf_file.addMergeSubsection();
757 const msub = elf_file.mergeSubsection(msub_index);
758 msub.merge_section_index = imsec.merge_section_index;
759 msub.string_index = res.key.pos;
760 msub.alignment = atom_ptr.alignment;
761 msub.size = res.key.len;
762 msub.entsize = math.cast(u32, isec.sh_entsize) orelse return error.Overflow;
763 msub.alive = !elf_file.base.gc_sections or isec.sh_flags & elf.SHF_ALLOC == 0;
764 res.sub.* = msub_index;
765 }
766 imsec_msub.* = res.sub.*;
767 }
768
769 imsec.clearAndFree(gpa);
770 }
771
772 for (self.symtab.items, 0..) |*esym, idx| {
773 const sym_index = self.symbols.items[idx];
774 const sym = elf_file.symbol(sym_index);
775
776 if (esym.st_shndx == elf.SHN_COMMON or esym.st_shndx == elf.SHN_UNDEF or esym.st_shndx == elf.SHN_ABS) continue;
777
778 const imsec_index = self.merge_sections.items[esym.st_shndx];
779 const imsec = elf_file.inputMergeSection(imsec_index) orelse continue;
780 if (imsec.offsets.items.len == 0) continue;
781 const msub_index, const offset = imsec.findSubsection(@intCast(esym.st_value)) orelse {
782 var err = try elf_file.addErrorWithNotes(2);
783 try err.addMsg(elf_file, "invalid symbol value: {x}", .{esym.st_value});
784 try err.addNote(elf_file, "for symbol {s}", .{sym.name(elf_file)});
785 try err.addNote(elf_file, "in {}", .{self.fmtPath()});
786 return error.MalformedObject;
787 };
788
789 try sym.addExtra(.{ .subsection = msub_index }, elf_file);
790 sym.flags.merge_subsection = true;
791 sym.value = offset;
792 }
793
794 for (self.atoms.items) |atom_index| {
795 const atom_ptr = elf_file.atom(atom_index) orelse continue;
796 if (!atom_ptr.flags.alive) continue;
797 const extras = atom_ptr.extra(elf_file) orelse continue;
798 const relocs = self.relocs.items[extras.rel_index..][0..extras.rel_count];
799 for (relocs) |*rel| {
800 const esym = self.symtab.items[rel.r_sym()];
801 if (esym.st_type() != elf.STT_SECTION) continue;
802
803 const imsec_index = self.merge_sections.items[esym.st_shndx];
804 const imsec = elf_file.inputMergeSection(imsec_index) orelse continue;
805 if (imsec.offsets.items.len == 0) continue;
806 const msub_index, const offset = imsec.findSubsection(@intCast(@as(i64, @intCast(esym.st_value)) + rel.r_addend)) orelse {
807 var err = try elf_file.addErrorWithNotes(1);
808 try err.addMsg(elf_file, "invalid relocation at offset 0x{x}", .{rel.r_offset});
809 try err.addNote(elf_file, "in {}:{s}", .{ self.fmtPath(), atom_ptr.name(elf_file) });
810 return error.MalformedObject;
811 };
812 const msub = elf_file.mergeSubsection(msub_index);
813 const msec = msub.mergeSection(elf_file);
814
815 const out_sym_idx: u64 = @intCast(self.symbols.items.len);
816 try self.symbols.ensureUnusedCapacity(gpa, 1);
817 const name = try std.fmt.allocPrint(gpa, "{s}$subsection{d}", .{ msec.name(elf_file), msub_index });
818 defer gpa.free(name);
819 const sym_index = try elf_file.addSymbol();
820 const sym = elf_file.symbol(sym_index);
821 sym.* = .{
822 .value = @bitCast(@as(i64, @intCast(offset)) - rel.r_addend),
823 .name_offset = try self.addString(gpa, name),
824 .esym_index = rel.r_sym(),
825 .file_index = self.index,
826 };
827 try sym.addExtra(.{ .subsection = msub_index }, elf_file);
828 sym.flags.merge_subsection = true;
829 self.symbols.addOneAssumeCapacity().* = sym_index;
830 rel.r_info = (out_sym_idx << 32) | rel.r_type();
831 }
832 }
833}
834
660835/// We will create dummy shdrs per each resolved common symbols to make it
661836/// play nicely with the rest of the system.
662837pub fn convertCommonSymbols(self: *Object, elf_file: *Elf) !void {
......@@ -747,6 +922,11 @@ pub fn addAtomsToOutputSections(self: *Object, elf_file: *Elf) !void {
747922
748923 for (self.locals()) |local_index| {
749924 const local = elf_file.symbol(local_index);
925 if (local.mergeSubsection(elf_file)) |msub| {
926 if (!msub.alive) continue;
927 local.output_section_index = msub.mergeSection(elf_file).output_section_index;
928 continue;
929 }
750930 const atom = local.atom(elf_file) orelse continue;
751931 if (!atom.flags.alive) continue;
752932 local.output_section_index = atom.output_section_index;
......@@ -754,11 +934,23 @@ pub fn addAtomsToOutputSections(self: *Object, elf_file: *Elf) !void {
754934
755935 for (self.globals()) |global_index| {
756936 const global = elf_file.symbol(global_index);
937 if (global.file(elf_file).?.index() != self.index) continue;
938 if (global.mergeSubsection(elf_file)) |msub| {
939 if (!msub.alive) continue;
940 global.output_section_index = msub.mergeSection(elf_file).output_section_index;
941 continue;
942 }
757943 const atom = global.atom(elf_file) orelse continue;
758944 if (!atom.flags.alive) continue;
759 if (global.file(elf_file).?.index() != self.index) continue;
760945 global.output_section_index = atom.output_section_index;
761946 }
947
948 for (self.symbols.items[self.symtab.items.len..]) |local_index| {
949 const local = elf_file.symbol(local_index);
950 const msub = local.mergeSubsection(elf_file).?;
951 if (!msub.alive) continue;
952 local.output_section_index = msub.mergeSection(elf_file).output_section_index;
953 }
762954}
763955
764956pub fn initRelaSections(self: Object, elf_file: *Elf) !void {
......@@ -843,9 +1035,17 @@ pub fn writeAr(self: Object, elf_file: *Elf, writer: anytype) !void {
8431035}
8441036
8451037pub fn updateSymtabSize(self: *Object, elf_file: *Elf) !void {
1038 const isAlive = struct {
1039 fn isAlive(sym: *const Symbol, ctx: *Elf) bool {
1040 if (sym.mergeSubsection(ctx)) |msub| return msub.alive;
1041 if (sym.atom(ctx)) |atom_ptr| return atom_ptr.flags.alive;
1042 return true;
1043 }
1044 }.isAlive;
1045
8461046 for (self.locals()) |local_index| {
8471047 const local = elf_file.symbol(local_index);
848 if (local.atom(elf_file)) |atom| if (!atom.flags.alive) continue;
1048 if (!isAlive(local, elf_file)) continue;
8491049 const esym = local.elfSym(elf_file);
8501050 switch (esym.st_type()) {
8511051 elf.STT_SECTION => continue,
......@@ -853,7 +1053,7 @@ pub fn updateSymtabSize(self: *Object, elf_file: *Elf) !void {
8531053 else => {},
8541054 }
8551055 local.flags.output_symtab = true;
856 try local.setOutputSymtabIndex(self.output_symtab_ctx.nlocals, elf_file);
1056 try local.addExtra(.{ .symtab = self.output_symtab_ctx.nlocals }, elf_file);
8571057 self.output_symtab_ctx.nlocals += 1;
8581058 self.output_symtab_ctx.strsize += @as(u32, @intCast(local.name(elf_file).len)) + 1;
8591059 }
......@@ -862,13 +1062,13 @@ pub fn updateSymtabSize(self: *Object, elf_file: *Elf) !void {
8621062 const global = elf_file.symbol(global_index);
8631063 const file_ptr = global.file(elf_file) orelse continue;
8641064 if (file_ptr.index() != self.index) continue;
865 if (global.atom(elf_file)) |atom| if (!atom.flags.alive) continue;
1065 if (!isAlive(global, elf_file)) continue;
8661066 global.flags.output_symtab = true;
8671067 if (global.isLocal(elf_file)) {
868 try global.setOutputSymtabIndex(self.output_symtab_ctx.nlocals, elf_file);
1068 try global.addExtra(.{ .symtab = self.output_symtab_ctx.nlocals }, elf_file);
8691069 self.output_symtab_ctx.nlocals += 1;
8701070 } else {
871 try global.setOutputSymtabIndex(self.output_symtab_ctx.nglobals, elf_file);
1071 try global.addExtra(.{ .symtab = self.output_symtab_ctx.nglobals }, elf_file);
8721072 self.output_symtab_ctx.nglobals += 1;
8731073 }
8741074 self.output_symtab_ctx.strsize += @as(u32, @intCast(global.name(elf_file).len)) + 1;
......@@ -902,14 +1102,16 @@ pub fn writeSymtab(self: Object, elf_file: *Elf) void {
9021102
9031103pub fn locals(self: Object) []const Symbol.Index {
9041104 if (self.symbols.items.len == 0) return &[0]Symbol.Index{};
905 const end = self.first_global orelse self.symbols.items.len;
1105 assert(self.symbols.items.len >= self.symtab.items.len);
1106 const end = self.first_global orelse self.symtab.items.len;
9061107 return self.symbols.items[0..end];
9071108}
9081109
9091110pub fn globals(self: Object) []const Symbol.Index {
9101111 if (self.symbols.items.len == 0) return &[0]Symbol.Index{};
911 const start = self.first_global orelse self.symbols.items.len;
912 return self.symbols.items[start..];
1112 assert(self.symbols.items.len >= self.symtab.items.len);
1113 const start = self.first_global orelse self.symtab.items.len;
1114 return self.symbols.items[start..self.symtab.items.len];
9131115}
9141116
9151117/// Returns atom's code and optionally uncompresses data if required (for compressed sections).
......@@ -954,6 +1156,14 @@ pub fn getString(self: Object, off: u32) [:0]const u8 {
9541156 return mem.sliceTo(@as([*:0]const u8, @ptrCast(self.strtab.items.ptr + off)), 0);
9551157}
9561158
1159fn addString(self: *Object, allocator: Allocator, str: []const u8) !u32 {
1160 const off: u32 = @intCast(self.strtab.items.len);
1161 try self.strtab.ensureUnusedCapacity(allocator, str.len + 1);
1162 self.strtab.appendSliceAssumeCapacity(str);
1163 self.strtab.appendAssumeCapacity(0);
1164 return off;
1165}
1166
9571167/// Caller owns the memory.
9581168fn preadShdrContentsAlloc(self: Object, allocator: Allocator, handle: std.fs.File, index: u32) ![]u8 {
9591169 assert(index < self.shdrs.items.len);
......@@ -1159,5 +1369,6 @@ const Cie = eh_frame.Cie;
11591369const Elf = @import("../Elf.zig");
11601370const Fde = eh_frame.Fde;
11611371const File = @import("file.zig").File;
1372const InputMergeSection = @import("merge_section.zig").InputMergeSection;
11621373const Symbol = @import("Symbol.zig");
11631374const Alignment = Atom.Alignment;
src/link/Elf/SharedObject.zig+2-2
......@@ -231,7 +231,7 @@ pub fn resolveSymbols(self: *SharedObject, elf_file: *Elf) void {
231231
232232 const global = elf_file.symbol(index);
233233 if (self.asFile().symbolRank(this_sym, false) < global.symbolRank(elf_file)) {
234 global.value = this_sym.st_value;
234 global.value = @intCast(this_sym.st_value);
235235 global.atom_index = 0;
236236 global.esym_index = esym_index;
237237 global.version_index = self.versyms.items[esym_index];
......@@ -269,7 +269,7 @@ pub fn updateSymtabSize(self: *SharedObject, elf_file: *Elf) !void {
269269 if (file_ptr.index() != self.index) continue;
270270 if (global.isLocal(elf_file)) continue;
271271 global.flags.output_symtab = true;
272 try global.setOutputSymtabIndex(self.output_symtab_ctx.nglobals, elf_file);
272 try global.addExtra(.{ .symtab = self.output_symtab_ctx.nglobals }, elf_file);
273273 self.output_symtab_ctx.nglobals += 1;
274274 self.output_symtab_ctx.strsize += @as(u32, @intCast(global.name(elf_file).len)) + 1;
275275 }
src/link/Elf/Symbol.zig+67-31
......@@ -1,7 +1,7 @@
11//! Represents a defined symbol.
22
33/// Allocated address value of this symbol.
4value: u64 = 0,
4value: i64 = 0,
55
66/// Offset into the linker's string table.
77name_offset: u32 = 0,
......@@ -14,7 +14,7 @@ file_index: File.Index = 0,
1414/// Use `atom` to get the pointer to the atom.
1515atom_index: Atom.Index = 0,
1616
17/// Assigned output section index for this atom.
17/// Assigned output section index for this symbol.
1818output_section_index: u32 = 0,
1919
2020/// Index of the source symbol this symbol references.
......@@ -33,7 +33,8 @@ extra_index: u32 = 0,
3333pub fn isAbs(symbol: Symbol, elf_file: *Elf) bool {
3434 const file_ptr = symbol.file(elf_file).?;
3535 if (file_ptr == .shared_object) return symbol.elfSym(elf_file).st_shndx == elf.SHN_ABS;
36 return !symbol.flags.import and symbol.atom(elf_file) == null and symbol.outputShndx() == null and
36 return !symbol.flags.import and symbol.atom(elf_file) == null and
37 symbol.mergeSubsection(elf_file) == null and symbol.outputShndx() == null and
3738 file_ptr != .linker_defined;
3839}
3940
......@@ -70,6 +71,12 @@ pub fn atom(symbol: Symbol, elf_file: *Elf) ?*Atom {
7071 return elf_file.atom(symbol.atom_index);
7172}
7273
74pub fn mergeSubsection(symbol: Symbol, elf_file: *Elf) ?*MergeSubsection {
75 if (!symbol.flags.merge_subsection) return null;
76 const extras = symbol.extra(elf_file).?;
77 return elf_file.mergeSubsection(extras.subsection);
78}
79
7380pub fn file(symbol: Symbol, elf_file: *Elf) ?File {
7481 return elf_file.file(symbol.file_index);
7582}
......@@ -92,7 +99,11 @@ pub fn symbolRank(symbol: Symbol, elf_file: *Elf) u32 {
9299 return file_ptr.symbolRank(sym, in_archive);
93100}
94101
95pub fn address(symbol: Symbol, opts: struct { plt: bool = true }, elf_file: *Elf) u64 {
102pub fn address(symbol: Symbol, opts: struct { plt: bool = true }, elf_file: *Elf) i64 {
103 if (symbol.mergeSubsection(elf_file)) |msub| {
104 if (!msub.alive) return 0;
105 return msub.address(elf_file) + symbol.value;
106 }
96107 if (symbol.flags.has_copy_rel) {
97108 return symbol.copyRelAddress(elf_file);
98109 }
......@@ -108,19 +119,23 @@ pub fn address(symbol: Symbol, opts: struct { plt: bool = true }, elf_file: *Elf
108119 if (!atom_ptr.flags.alive) {
109120 if (mem.eql(u8, atom_ptr.name(elf_file), ".eh_frame")) {
110121 const sym_name = symbol.name(elf_file);
122 const sh_addr, const sh_size = blk: {
123 const shndx = elf_file.eh_frame_section_index orelse break :blk .{ 0, 0 };
124 const shdr = elf_file.shdrs.items[shndx];
125 break :blk .{ shdr.sh_addr, shdr.sh_size };
126 };
111127 if (mem.startsWith(u8, sym_name, "__EH_FRAME_BEGIN__") or
112128 mem.startsWith(u8, sym_name, "__EH_FRAME_LIST__") or
113129 mem.startsWith(u8, sym_name, ".eh_frame_seg") or
114130 symbol.elfSym(elf_file).st_type() == elf.STT_SECTION)
115131 {
116 return elf_file.shdrs.items[elf_file.eh_frame_section_index.?].sh_addr;
132 return @intCast(sh_addr);
117133 }
118134
119135 if (mem.startsWith(u8, sym_name, "__FRAME_END__") or
120136 mem.startsWith(u8, sym_name, "__EH_FRAME_LIST_END__"))
121137 {
122 const shdr = elf_file.shdrs.items[elf_file.eh_frame_section_index.?];
123 return shdr.sh_addr + shdr.sh_size;
138 return @intCast(sh_addr + sh_size);
124139 }
125140
126141 // TODO I think we potentially should error here
......@@ -143,65 +158,57 @@ pub fn outputSymtabIndex(symbol: Symbol, elf_file: *Elf) ?u32 {
143158 return if (symbol.isLocal(elf_file)) idx + symtab_ctx.ilocal else idx + symtab_ctx.iglobal;
144159}
145160
146pub fn setOutputSymtabIndex(symbol: *Symbol, index: u32, elf_file: *Elf) !void {
147 if (symbol.extra(elf_file)) |extras| {
148 var new_extras = extras;
149 new_extras.symtab = index;
150 symbol.setExtra(new_extras, elf_file);
151 } else try symbol.addExtra(.{ .symtab = index }, elf_file);
152}
153
154pub fn gotAddress(symbol: Symbol, elf_file: *Elf) u64 {
161pub fn gotAddress(symbol: Symbol, elf_file: *Elf) i64 {
155162 if (!symbol.flags.has_got) return 0;
156163 const extras = symbol.extra(elf_file).?;
157164 const entry = elf_file.got.entries.items[extras.got];
158165 return entry.address(elf_file);
159166}
160167
161pub fn pltGotAddress(symbol: Symbol, elf_file: *Elf) u64 {
168pub fn pltGotAddress(symbol: Symbol, elf_file: *Elf) i64 {
162169 if (!(symbol.flags.has_plt and symbol.flags.has_got)) return 0;
163170 const extras = symbol.extra(elf_file).?;
164171 const shdr = elf_file.shdrs.items[elf_file.plt_got_section_index.?];
165172 const cpu_arch = elf_file.getTarget().cpu.arch;
166 return shdr.sh_addr + extras.plt_got * PltGotSection.entrySize(cpu_arch);
173 return @intCast(shdr.sh_addr + extras.plt_got * PltGotSection.entrySize(cpu_arch));
167174}
168175
169pub fn pltAddress(symbol: Symbol, elf_file: *Elf) u64 {
176pub fn pltAddress(symbol: Symbol, elf_file: *Elf) i64 {
170177 if (!symbol.flags.has_plt) return 0;
171178 const extras = symbol.extra(elf_file).?;
172179 const shdr = elf_file.shdrs.items[elf_file.plt_section_index.?];
173180 const cpu_arch = elf_file.getTarget().cpu.arch;
174 return shdr.sh_addr + extras.plt * PltSection.entrySize(cpu_arch) + PltSection.preambleSize(cpu_arch);
181 return @intCast(shdr.sh_addr + extras.plt * PltSection.entrySize(cpu_arch) + PltSection.preambleSize(cpu_arch));
175182}
176183
177pub fn gotPltAddress(symbol: Symbol, elf_file: *Elf) u64 {
184pub fn gotPltAddress(symbol: Symbol, elf_file: *Elf) i64 {
178185 if (!symbol.flags.has_plt) return 0;
179186 const extras = symbol.extra(elf_file).?;
180187 const shdr = elf_file.shdrs.items[elf_file.got_plt_section_index.?];
181 return shdr.sh_addr + extras.plt * 8 + GotPltSection.preamble_size;
188 return @intCast(shdr.sh_addr + extras.plt * 8 + GotPltSection.preamble_size);
182189}
183190
184pub fn copyRelAddress(symbol: Symbol, elf_file: *Elf) u64 {
191pub fn copyRelAddress(symbol: Symbol, elf_file: *Elf) i64 {
185192 if (!symbol.flags.has_copy_rel) return 0;
186193 const shdr = elf_file.shdrs.items[elf_file.copy_rel_section_index.?];
187 return shdr.sh_addr + symbol.value;
194 return @as(i64, @intCast(shdr.sh_addr)) + symbol.value;
188195}
189196
190pub fn tlsGdAddress(symbol: Symbol, elf_file: *Elf) u64 {
197pub fn tlsGdAddress(symbol: Symbol, elf_file: *Elf) i64 {
191198 if (!symbol.flags.has_tlsgd) return 0;
192199 const extras = symbol.extra(elf_file).?;
193200 const entry = elf_file.got.entries.items[extras.tlsgd];
194201 return entry.address(elf_file);
195202}
196203
197pub fn gotTpAddress(symbol: Symbol, elf_file: *Elf) u64 {
204pub fn gotTpAddress(symbol: Symbol, elf_file: *Elf) i64 {
198205 if (!symbol.flags.has_gottp) return 0;
199206 const extras = symbol.extra(elf_file).?;
200207 const entry = elf_file.got.entries.items[extras.gottp];
201208 return entry.address(elf_file);
202209}
203210
204pub fn tlsDescAddress(symbol: Symbol, elf_file: *Elf) u64 {
211pub fn tlsDescAddress(symbol: Symbol, elf_file: *Elf) i64 {
205212 if (!symbol.flags.has_tlsdesc) return 0;
206213 const extras = symbol.extra(elf_file).?;
207214 const entry = elf_file.got.entries.items[extras.tlsdesc];
......@@ -221,7 +228,7 @@ pub fn getOrCreateZigGotEntry(symbol: *Symbol, symbol_index: Index, elf_file: *E
221228 return .{ .found_existing = false, .index = index };
222229}
223230
224pub fn zigGotAddress(symbol: Symbol, elf_file: *Elf) u64 {
231pub fn zigGotAddress(symbol: Symbol, elf_file: *Elf) i64 {
225232 if (!symbol.flags.has_zig_got) return 0;
226233 const extras = symbol.extra(elf_file).?;
227234 return elf_file.zig_got.entryAddress(extras.zig_got, elf_file);
......@@ -240,8 +247,31 @@ pub fn dsoAlignment(symbol: Symbol, elf_file: *Elf) !u64 {
240247 @min(alignment, try std.math.powi(u64, 2, @ctz(esym.st_value)));
241248}
242249
243pub fn addExtra(symbol: *Symbol, extras: Extra, elf_file: *Elf) !void {
244 symbol.extra_index = try elf_file.addSymbolExtra(extras);
250const AddExtraOpts = struct {
251 got: ?u32 = null,
252 plt: ?u32 = null,
253 plt_got: ?u32 = null,
254 dynamic: ?u32 = null,
255 symtab: ?u32 = null,
256 copy_rel: ?u32 = null,
257 tlsgd: ?u32 = null,
258 gottp: ?u32 = null,
259 tlsdesc: ?u32 = null,
260 zig_got: ?u32 = null,
261 subsection: ?u32 = null,
262};
263
264pub fn addExtra(symbol: *Symbol, opts: AddExtraOpts, elf_file: *Elf) !void {
265 if (symbol.extra(elf_file) == null) {
266 symbol.extra_index = try elf_file.addSymbolExtra(.{});
267 }
268 var extras = symbol.extra(elf_file).?;
269 inline for (@typeInfo(@TypeOf(opts)).Struct.fields) |field| {
270 if (@field(opts, field.name)) |x| {
271 @field(extras, field.name) = x;
272 }
273 }
274 symbol.setExtra(extras, elf_file);
245275}
246276
247277pub fn extra(symbol: Symbol, elf_file: *Elf) ?Extra {
......@@ -266,6 +296,7 @@ pub fn setOutputSym(symbol: Symbol, elf_file: *Elf, out: *elf.Elf64_Sym) void {
266296 if (symbol.flags.has_copy_rel) break :blk @intCast(elf_file.copy_rel_section_index.?);
267297 if (file_ptr == .shared_object or esym.st_shndx == elf.SHN_UNDEF) break :blk elf.SHN_UNDEF;
268298 if (elf_file.base.isRelocatable() and esym.st_shndx == elf.SHN_COMMON) break :blk elf.SHN_COMMON;
299 if (symbol.mergeSubsection(elf_file)) |msub| break :blk @intCast(msub.mergeSection(elf_file).output_section_index);
269300 if (symbol.atom(elf_file) == null and file_ptr != .linker_defined) break :blk elf.SHN_ABS;
270301 break :blk @intCast(symbol.outputShndx() orelse elf.SHN_UNDEF);
271302 };
......@@ -284,7 +315,7 @@ pub fn setOutputSym(symbol: Symbol, elf_file: *Elf, out: *elf.Elf64_Sym) void {
284315 out.st_info = (st_bind << 4) | st_type;
285316 out.st_other = esym.st_other;
286317 out.st_shndx = st_shndx;
287 out.st_value = st_value;
318 out.st_value = @intCast(st_value);
288319 out.st_size = esym.st_size;
289320}
290321
......@@ -436,6 +467,9 @@ pub const Flags = packed struct {
436467 /// TODO this is really not needed if only we operated on esyms between
437468 /// codegen and ZigObject.
438469 is_tls: bool = false,
470
471 /// Whether the symbol is a merge subsection.
472 merge_subsection: bool = false,
439473};
440474
441475pub const Extra = struct {
......@@ -449,6 +483,7 @@ pub const Extra = struct {
449483 gottp: u32 = 0,
450484 tlsdesc: u32 = 0,
451485 zig_got: u32 = 0,
486 subsection: u32 = 0,
452487};
453488
454489pub const Index = u32;
......@@ -465,6 +500,7 @@ const File = @import("file.zig").File;
465500const GotSection = synthetic_sections.GotSection;
466501const GotPltSection = synthetic_sections.GotPltSection;
467502const LinkerDefined = @import("LinkerDefined.zig");
503const MergeSubsection = @import("merge_section.zig").MergeSubsection;
468504const Object = @import("Object.zig");
469505const PltSection = synthetic_sections.PltSection;
470506const PltGotSection = synthetic_sections.PltGotSection;
src/link/Elf/ZigObject.zig+14-14
......@@ -343,7 +343,7 @@ pub fn resolveSymbols(self: *ZigObject, elf_file: *Elf) void {
343343 atom.outputShndx().?
344344 else
345345 elf.SHN_UNDEF;
346 global.value = esym.st_value;
346 global.value = @intCast(esym.st_value);
347347 global.atom_index = atom_index;
348348 global.esym_index = esym_index;
349349 global.file_index = self.index;
......@@ -566,7 +566,7 @@ pub fn updateSymtabSize(self: *ZigObject, elf_file: *Elf) !void {
566566 else => {},
567567 }
568568 local.flags.output_symtab = true;
569 try local.setOutputSymtabIndex(self.output_symtab_ctx.nlocals, elf_file);
569 try local.addExtra(.{ .symtab = self.output_symtab_ctx.nlocals }, elf_file);
570570 self.output_symtab_ctx.nlocals += 1;
571571 self.output_symtab_ctx.strsize += @as(u32, @intCast(local.name(elf_file).len)) + 1;
572572 }
......@@ -578,10 +578,10 @@ pub fn updateSymtabSize(self: *ZigObject, elf_file: *Elf) !void {
578578 if (global.atom(elf_file)) |atom| if (!atom.flags.alive) continue;
579579 global.flags.output_symtab = true;
580580 if (global.isLocal(elf_file)) {
581 try global.setOutputSymtabIndex(self.output_symtab_ctx.nlocals, elf_file);
581 try global.addExtra(.{ .symtab = self.output_symtab_ctx.nlocals }, elf_file);
582582 self.output_symtab_ctx.nlocals += 1;
583583 } else {
584 try global.setOutputSymtabIndex(self.output_symtab_ctx.nglobals, elf_file);
584 try global.addExtra(.{ .symtab = self.output_symtab_ctx.nglobals }, elf_file);
585585 self.output_symtab_ctx.nglobals += 1;
586586 }
587587 self.output_symtab_ctx.strsize += @as(u32, @intCast(global.name(elf_file).len)) + 1;
......@@ -631,7 +631,7 @@ pub fn codeAlloc(self: ZigObject, elf_file: *Elf, atom_index: Atom.Index) ![]u8
631631 return code;
632632 }
633633
634 const file_offset = shdr.sh_offset + atom.value;
634 const file_offset = shdr.sh_offset + @as(u64, @intCast(atom.value));
635635 const size = std.math.cast(usize, atom.size) orelse return error.Overflow;
636636 const code = try gpa.alloc(u8, size);
637637 errdefer gpa.free(code);
......@@ -659,7 +659,7 @@ pub fn getDeclVAddr(
659659 .r_info = (@as(u64, @intCast(this_sym.esym_index)) << 32) | r_type,
660660 .r_addend = reloc_info.addend,
661661 });
662 return vaddr;
662 return @intCast(vaddr);
663663}
664664
665665pub fn getAnonDeclVAddr(
......@@ -678,7 +678,7 @@ pub fn getAnonDeclVAddr(
678678 .r_info = (@as(u64, @intCast(sym.esym_index)) << 32) | r_type,
679679 .r_addend = reloc_info.addend,
680680 });
681 return vaddr;
681 return @intCast(vaddr);
682682}
683683
684684pub fn lowerAnonDecl(
......@@ -929,7 +929,7 @@ fn updateDeclCode(
929929
930930 if (old_size > 0 and elf_file.base.child_pid == null) {
931931 const capacity = atom_ptr.capacity(elf_file);
932 const need_realloc = code.len > capacity or !required_alignment.check(atom_ptr.value);
932 const need_realloc = code.len > capacity or !required_alignment.check(@intCast(atom_ptr.value));
933933 if (need_realloc) {
934934 try atom_ptr.grow(elf_file);
935935 log.debug("growing {} from 0x{x} to 0x{x}", .{ decl_name.fmt(&mod.intern_pool), old_vaddr, atom_ptr.value });
......@@ -984,7 +984,7 @@ fn updateDeclCode(
984984
985985 const shdr = elf_file.shdrs.items[shdr_index];
986986 if (shdr.sh_type != elf.SHT_NOBITS) {
987 const file_offset = shdr.sh_offset + atom_ptr.value;
987 const file_offset = shdr.sh_offset + @as(u64, @intCast(atom_ptr.value));
988988 try elf_file.base.file.?.pwriteAll(code, file_offset);
989989 }
990990}
......@@ -1107,7 +1107,7 @@ pub fn updateFunc(
11071107 try self.dwarf.?.commitDeclState(
11081108 mod,
11091109 decl_index,
1110 sym.address(.{}, elf_file),
1110 @intCast(sym.address(.{}, elf_file)),
11111111 sym.atom(elf_file).?.size,
11121112 ds,
11131113 );
......@@ -1186,7 +1186,7 @@ pub fn updateDecl(
11861186 try self.dwarf.?.commitDeclState(
11871187 mod,
11881188 decl_index,
1189 sym.address(.{}, elf_file),
1189 @intCast(sym.address(.{}, elf_file)),
11901190 sym.atom(elf_file).?.size,
11911191 ds,
11921192 );
......@@ -1275,7 +1275,7 @@ fn updateLazySymbol(
12751275 }
12761276
12771277 const shdr = elf_file.shdrs.items[output_section_index];
1278 const file_offset = shdr.sh_offset + atom_ptr.value;
1278 const file_offset = shdr.sh_offset + @as(u64, @intCast(atom_ptr.value));
12791279 try elf_file.base.file.?.pwriteAll(code, file_offset);
12801280}
12811281
......@@ -1373,7 +1373,7 @@ fn lowerConst(
13731373 local_esym.st_value = 0;
13741374
13751375 const shdr = elf_file.shdrs.items[output_section_index];
1376 const file_offset = shdr.sh_offset + atom_ptr.value;
1376 const file_offset = shdr.sh_offset + @as(u64, @intCast(atom_ptr.value));
13771377 try elf_file.base.file.?.pwriteAll(code, file_offset);
13781378
13791379 return .{ .ok = sym_index };
......@@ -1457,7 +1457,7 @@ pub fn updateExports(
14571457
14581458 const actual_esym_index = global_esym_index & symbol_mask;
14591459 const global_esym = &self.global_esyms.items(.elf_sym)[actual_esym_index];
1460 global_esym.st_value = elf_file.symbol(sym_index).value;
1460 global_esym.st_value = @intCast(elf_file.symbol(sym_index).value);
14611461 global_esym.st_shndx = esym.st_shndx;
14621462 global_esym.st_info = (stb_bits << 4) | stt_bits;
14631463 global_esym.st_name = name_off;
src/link/Elf/gc.zig+8
......@@ -68,6 +68,10 @@ fn collectRoots(roots: *std.ArrayList(*Atom), files: []const File.Index, elf_fil
6868}
6969
7070fn markSymbol(sym: *Symbol, roots: *std.ArrayList(*Atom), elf_file: *Elf) !void {
71 if (sym.mergeSubsection(elf_file)) |msub| {
72 msub.alive = true;
73 return;
74 }
7175 const atom = sym.atom(elf_file) orelse return;
7276 if (markAtom(atom)) try roots.append(atom);
7377}
......@@ -96,6 +100,10 @@ fn markLive(atom: *Atom, elf_file: *Elf) void {
96100
97101 for (atom.relocs(elf_file)) |rel| {
98102 const target_sym = elf_file.symbol(file.symbol(rel.r_sym()));
103 if (target_sym.mergeSubsection(elf_file)) |msub| {
104 msub.alive = true;
105 continue;
106 }
99107 const target_atom = target_sym.atom(elf_file) orelse continue;
100108 target_atom.flags.alive = true;
101109 gc_track_live_log.debug("{}marking live atom({d})", .{ track_live_level, target_atom.atom_index });
src/link/Elf/merge_section.zig created+285
......@@ -0,0 +1,285 @@
1pub const MergeSection = struct {
2 name_offset: u32 = 0,
3 type: u32 = 0,
4 flags: u64 = 0,
5 output_section_index: u32 = 0,
6 bytes: std.ArrayListUnmanaged(u8) = .{},
7 table: std.HashMapUnmanaged(
8 String,
9 MergeSubsection.Index,
10 IndexContext,
11 std.hash_map.default_max_load_percentage,
12 ) = .{},
13 subsections: std.ArrayListUnmanaged(MergeSubsection.Index) = .{},
14
15 pub fn deinit(msec: *MergeSection, allocator: Allocator) void {
16 msec.bytes.deinit(allocator);
17 msec.table.deinit(allocator);
18 msec.subsections.deinit(allocator);
19 }
20
21 pub fn name(msec: MergeSection, elf_file: *Elf) [:0]const u8 {
22 return elf_file.strings.getAssumeExists(msec.name_offset);
23 }
24
25 pub fn address(msec: MergeSection, elf_file: *Elf) i64 {
26 const shdr = elf_file.shdrs.items[msec.output_section_index];
27 return @intCast(shdr.sh_addr);
28 }
29
30 const InsertResult = struct {
31 found_existing: bool,
32 key: String,
33 sub: *MergeSubsection.Index,
34 };
35
36 pub fn insert(msec: *MergeSection, allocator: Allocator, string: []const u8) !InsertResult {
37 const gop = try msec.table.getOrPutContextAdapted(
38 allocator,
39 string,
40 IndexAdapter{ .bytes = msec.bytes.items },
41 IndexContext{ .bytes = msec.bytes.items },
42 );
43 if (!gop.found_existing) {
44 const index: u32 = @intCast(msec.bytes.items.len);
45 try msec.bytes.appendSlice(allocator, string);
46 gop.key_ptr.* = .{ .pos = index, .len = @intCast(string.len) };
47 }
48 return .{ .found_existing = gop.found_existing, .key = gop.key_ptr.*, .sub = gop.value_ptr };
49 }
50
51 pub fn insertZ(msec: *MergeSection, allocator: Allocator, string: []const u8) !InsertResult {
52 const with_null = try allocator.alloc(u8, string.len + 1);
53 defer allocator.free(with_null);
54 @memcpy(with_null[0..string.len], string);
55 with_null[string.len] = 0;
56 return msec.insert(allocator, with_null);
57 }
58
59 /// Finalizes the merge section and clears hash table.
60 /// Sorts all owned subsections.
61 pub fn finalize(msec: *MergeSection, elf_file: *Elf) !void {
62 const gpa = elf_file.base.comp.gpa;
63 try msec.subsections.ensureTotalCapacityPrecise(gpa, msec.table.count());
64
65 var it = msec.table.iterator();
66 while (it.next()) |entry| {
67 const msub = elf_file.mergeSubsection(entry.value_ptr.*);
68 if (!msub.alive) continue;
69 msec.subsections.appendAssumeCapacity(entry.value_ptr.*);
70 }
71 msec.table.clearAndFree(gpa);
72
73 const sortFn = struct {
74 pub fn sortFn(ctx: *Elf, lhs: MergeSubsection.Index, rhs: MergeSubsection.Index) bool {
75 const lhs_msub = ctx.mergeSubsection(lhs);
76 const rhs_msub = ctx.mergeSubsection(rhs);
77 if (lhs_msub.alignment.compareStrict(.eq, rhs_msub.alignment)) {
78 if (lhs_msub.size == rhs_msub.size) {
79 return mem.order(u8, lhs_msub.getString(ctx), rhs_msub.getString(ctx)) == .lt;
80 }
81 return lhs_msub.size < rhs_msub.size;
82 }
83 return lhs_msub.alignment.compareStrict(.lt, rhs_msub.alignment);
84 }
85 }.sortFn;
86
87 std.mem.sort(MergeSubsection.Index, msec.subsections.items, elf_file, sortFn);
88 }
89
90 pub const IndexContext = struct {
91 bytes: []const u8,
92
93 pub fn eql(_: @This(), a: String, b: String) bool {
94 return a.pos == b.pos;
95 }
96
97 pub fn hash(ctx: @This(), key: String) u64 {
98 const str = ctx.bytes[key.pos..][0..key.len];
99 return std.hash_map.hashString(str);
100 }
101 };
102
103 pub const IndexAdapter = struct {
104 bytes: []const u8,
105
106 pub fn eql(ctx: @This(), a: []const u8, b: String) bool {
107 const str = ctx.bytes[b.pos..][0..b.len];
108 return mem.eql(u8, a, str);
109 }
110
111 pub fn hash(_: @This(), adapted_key: []const u8) u64 {
112 return std.hash_map.hashString(adapted_key);
113 }
114 };
115
116 pub fn format(
117 msec: MergeSection,
118 comptime unused_fmt_string: []const u8,
119 options: std.fmt.FormatOptions,
120 writer: anytype,
121 ) !void {
122 _ = msec;
123 _ = unused_fmt_string;
124 _ = options;
125 _ = writer;
126 @compileError("do not format MergeSection directly");
127 }
128
129 pub fn fmt(msec: MergeSection, elf_file: *Elf) std.fmt.Formatter(format2) {
130 return .{ .data = .{
131 .msec = msec,
132 .elf_file = elf_file,
133 } };
134 }
135
136 const FormatContext = struct {
137 msec: MergeSection,
138 elf_file: *Elf,
139 };
140
141 pub fn format2(
142 ctx: FormatContext,
143 comptime unused_fmt_string: []const u8,
144 options: std.fmt.FormatOptions,
145 writer: anytype,
146 ) !void {
147 _ = options;
148 _ = unused_fmt_string;
149 const msec = ctx.msec;
150 const elf_file = ctx.elf_file;
151 try writer.print("{s} : @{x} : type({x}) : flags({x})\n", .{
152 msec.name(elf_file),
153 msec.address(elf_file),
154 msec.type,
155 msec.flags,
156 });
157 for (msec.subsections.items) |index| {
158 try writer.print(" {}\n", .{elf_file.mergeSubsection(index).fmt(elf_file)});
159 }
160 }
161
162 pub const Index = u32;
163};
164
165pub const MergeSubsection = struct {
166 value: i64 = 0,
167 merge_section_index: MergeSection.Index = 0,
168 string_index: u32 = 0,
169 size: u32 = 0,
170 alignment: Atom.Alignment = .@"1",
171 entsize: u32 = 0,
172 alive: bool = false,
173
174 pub fn address(msub: MergeSubsection, elf_file: *Elf) i64 {
175 return msub.mergeSection(elf_file).address(elf_file) + msub.value;
176 }
177
178 pub fn mergeSection(msub: MergeSubsection, elf_file: *Elf) *MergeSection {
179 return elf_file.mergeSection(msub.merge_section_index);
180 }
181
182 pub fn getString(msub: MergeSubsection, elf_file: *Elf) []const u8 {
183 const msec = msub.mergeSection(elf_file);
184 return msec.bytes.items[msub.string_index..][0..msub.size];
185 }
186
187 pub fn format(
188 msub: MergeSubsection,
189 comptime unused_fmt_string: []const u8,
190 options: std.fmt.FormatOptions,
191 writer: anytype,
192 ) !void {
193 _ = msub;
194 _ = unused_fmt_string;
195 _ = options;
196 _ = writer;
197 @compileError("do not format MergeSubsection directly");
198 }
199
200 pub fn fmt(msub: MergeSubsection, elf_file: *Elf) std.fmt.Formatter(format2) {
201 return .{ .data = .{
202 .msub = msub,
203 .elf_file = elf_file,
204 } };
205 }
206
207 const FormatContext = struct {
208 msub: MergeSubsection,
209 elf_file: *Elf,
210 };
211
212 pub fn format2(
213 ctx: FormatContext,
214 comptime unused_fmt_string: []const u8,
215 options: std.fmt.FormatOptions,
216 writer: anytype,
217 ) !void {
218 _ = options;
219 _ = unused_fmt_string;
220 const msub = ctx.msub;
221 const elf_file = ctx.elf_file;
222 try writer.print("@{x} : align({x}) : size({x})", .{
223 msub.address(elf_file),
224 msub.alignment,
225 msub.size,
226 });
227 if (!msub.alive) try writer.writeAll(" : [*]");
228 }
229
230 pub const Index = u32;
231};
232
233pub const InputMergeSection = struct {
234 merge_section_index: MergeSection.Index = 0,
235 atom_index: Atom.Index = 0,
236 offsets: std.ArrayListUnmanaged(u32) = .{},
237 subsections: std.ArrayListUnmanaged(MergeSubsection.Index) = .{},
238 bytes: std.ArrayListUnmanaged(u8) = .{},
239 strings: std.ArrayListUnmanaged(String) = .{},
240
241 pub fn deinit(imsec: *InputMergeSection, allocator: Allocator) void {
242 imsec.offsets.deinit(allocator);
243 imsec.subsections.deinit(allocator);
244 imsec.bytes.deinit(allocator);
245 imsec.strings.deinit(allocator);
246 }
247
248 pub fn clearAndFree(imsec: *InputMergeSection, allocator: Allocator) void {
249 imsec.bytes.clearAndFree(allocator);
250 // TODO: imsec.strings.clearAndFree(allocator);
251 }
252
253 pub fn findSubsection(imsec: InputMergeSection, offset: u32) ?struct { MergeSubsection.Index, u32 } {
254 // TODO: binary search
255 for (imsec.offsets.items, 0..) |off, index| {
256 if (offset < off) return .{
257 imsec.subsections.items[index - 1],
258 offset - imsec.offsets.items[index - 1],
259 };
260 }
261 const last = imsec.offsets.items.len - 1;
262 const last_off = imsec.offsets.items[last];
263 const last_len = imsec.strings.items[last].len;
264 if (offset < last_off + last_len) return .{ imsec.subsections.items[last], offset - last_off };
265 return null;
266 }
267
268 pub fn insert(imsec: *InputMergeSection, allocator: Allocator, string: []const u8) !void {
269 const index: u32 = @intCast(imsec.bytes.items.len);
270 try imsec.bytes.appendSlice(allocator, string);
271 try imsec.strings.append(allocator, .{ .pos = index, .len = @intCast(string.len) });
272 }
273
274 pub const Index = u32;
275};
276
277const String = struct { pos: u32, len: u32 };
278
279const assert = std.debug.assert;
280const mem = std.mem;
281const std = @import("std");
282
283const Allocator = mem.Allocator;
284const Atom = @import("Atom.zig");
285const Elf = @import("../Elf.zig");
src/link/Elf/relocatable.zig+13-2
......@@ -34,12 +34,16 @@ pub fn flushStaticLib(elf_file: *Elf, comp: *Compilation, module_obj_path: ?[]co
3434 // First, we flush relocatable object file generated with our backends.
3535 if (elf_file.zigObjectPtr()) |zig_object| {
3636 zig_object.resolveSymbols(elf_file);
37 try elf_file.addCommentString();
38 try elf_file.finalizeMergeSections();
3739 zig_object.claimUnresolvedObject(elf_file);
3840
41 try elf_file.initMergeSections();
3942 try elf_file.initSymtab();
4043 try elf_file.initShStrtab();
4144 try elf_file.sortShdrs();
4245 try zig_object.addAtomsToRelaSections(elf_file);
46 try elf_file.updateMergeSectionSizes();
4347 try updateSectionSizes(elf_file);
4448
4549 try allocateAllocSections(elf_file);
......@@ -49,6 +53,7 @@ pub fn flushStaticLib(elf_file: *Elf, comp: *Compilation, module_obj_path: ?[]co
4953 state_log.debug("{}", .{elf_file.dumpState()});
5054 }
5155
56 try elf_file.writeMergeSections();
5257 try writeSyntheticSections(elf_file);
5358 try elf_file.writeShdrTable();
5459 try elf_file.writeElfHeader();
......@@ -179,9 +184,13 @@ pub fn flushObject(elf_file: *Elf, comp: *Compilation, module_obj_path: ?[]const
179184 // input Object files.
180185 elf_file.resolveSymbols();
181186 elf_file.markEhFrameAtomsDead();
187 try elf_file.resolveMergeSections();
188 try elf_file.addCommentString();
189 try elf_file.finalizeMergeSections();
182190 claimUnresolved(elf_file);
183191
184192 try initSections(elf_file);
193 try elf_file.initMergeSections();
185194 try elf_file.sortShdrs();
186195 if (elf_file.zigObjectPtr()) |zig_object| {
187196 try zig_object.addAtomsToRelaSections(elf_file);
......@@ -191,6 +200,7 @@ pub fn flushObject(elf_file: *Elf, comp: *Compilation, module_obj_path: ?[]const
191200 try object.addAtomsToOutputSections(elf_file);
192201 try object.addAtomsToRelaSections(elf_file);
193202 }
203 try elf_file.updateMergeSectionSizes();
194204 try updateSectionSizes(elf_file);
195205
196206 try allocateAllocSections(elf_file);
......@@ -201,6 +211,7 @@ pub fn flushObject(elf_file: *Elf, comp: *Compilation, module_obj_path: ?[]const
201211 }
202212
203213 try writeAtoms(elf_file);
214 try elf_file.writeMergeSections();
204215 try writeSyntheticSections(elf_file);
205216 try elf_file.writeShdrTable();
206217 try elf_file.writeElfHeader();
......@@ -328,7 +339,7 @@ fn updateSectionSizes(elf_file: *Elf) !void {
328339 if (!atom_ptr.flags.alive) continue;
329340 const offset = atom_ptr.alignment.forward(shdr.sh_size);
330341 const padding = offset - shdr.sh_size;
331 atom_ptr.value = offset;
342 atom_ptr.value = @intCast(offset);
332343 shdr.sh_size += padding + atom_ptr.size;
333344 shdr.sh_addralign = @max(shdr.sh_addralign, atom_ptr.alignment.toByteUnits() orelse 1);
334345 }
......@@ -434,7 +445,7 @@ fn writeAtoms(elf_file: *Elf) !void {
434445 const atom_ptr = elf_file.atom(atom_index).?;
435446 assert(atom_ptr.flags.alive);
436447
437 const offset = math.cast(usize, atom_ptr.value - shdr.sh_addr - base_offset) orelse
448 const offset = math.cast(usize, atom_ptr.value - @as(i64, @intCast(shdr.sh_addr - base_offset))) orelse
438449 return error.Overflow;
439450 const size = math.cast(usize, atom_ptr.size) orelse return error.Overflow;
440451
src/link/Elf/synthetic_sections.zig+47-85
......@@ -259,11 +259,7 @@ pub const ZigGotSection = struct {
259259 if (elf_file.isEffectivelyDynLib() or (elf_file.base.isExe() and comp.config.pie)) {
260260 zig_got.flags.needs_rela = true;
261261 }
262 if (symbol.extra(elf_file)) |extra| {
263 var new_extra = extra;
264 new_extra.zig_got = index;
265 symbol.setExtra(new_extra, elf_file);
266 } else try symbol.addExtra(.{ .zig_got = index }, elf_file);
262 try symbol.addExtra(.{ .zig_got = index }, elf_file);
267263 return index;
268264 }
269265
......@@ -274,11 +270,11 @@ pub const ZigGotSection = struct {
274270 return shdr.sh_offset + @as(u64, entry_size) * index;
275271 }
276272
277 pub fn entryAddress(zig_got: ZigGotSection, index: Index, elf_file: *Elf) u64 {
273 pub fn entryAddress(zig_got: ZigGotSection, index: Index, elf_file: *Elf) i64 {
278274 _ = zig_got;
279275 const entry_size = elf_file.archPtrWidthBytes();
280276 const shdr = elf_file.shdrs.items[elf_file.zig_got_section_index.?];
281 return shdr.sh_addr + @as(u64, entry_size) * index;
277 return @as(i64, @intCast(shdr.sh_addr)) + entry_size * index;
282278 }
283279
284280 pub fn size(zig_got: ZigGotSection, elf_file: *Elf) usize {
......@@ -295,23 +291,23 @@ pub const ZigGotSection = struct {
295291 const target = elf_file.getTarget();
296292 const endian = target.cpu.arch.endian();
297293 const off = zig_got.entryOffset(index, elf_file);
298 const vaddr = zig_got.entryAddress(index, elf_file);
294 const vaddr: u64 = @intCast(zig_got.entryAddress(index, elf_file));
299295 const entry = zig_got.entries.items[index];
300296 const value = elf_file.symbol(entry).address(.{}, elf_file);
301297 switch (entry_size) {
302298 2 => {
303299 var buf: [2]u8 = undefined;
304 std.mem.writeInt(u16, &buf, @as(u16, @intCast(value)), endian);
300 std.mem.writeInt(u16, &buf, @intCast(value), endian);
305301 try elf_file.base.file.?.pwriteAll(&buf, off);
306302 },
307303 4 => {
308304 var buf: [4]u8 = undefined;
309 std.mem.writeInt(u32, &buf, @as(u32, @intCast(value)), endian);
305 std.mem.writeInt(u32, &buf, @intCast(value), endian);
310306 try elf_file.base.file.?.pwriteAll(&buf, off);
311307 },
312308 8 => {
313309 var buf: [8]u8 = undefined;
314 std.mem.writeInt(u64, &buf, value, endian);
310 std.mem.writeInt(u64, &buf, @intCast(value), endian);
315311 try elf_file.base.file.?.pwriteAll(&buf, off);
316312
317313 if (elf_file.base.child_pid) |pid| {
......@@ -360,9 +356,9 @@ pub const ZigGotSection = struct {
360356 const symbol = elf_file.symbol(entry);
361357 const offset = symbol.zigGotAddress(elf_file);
362358 elf_file.addRelaDynAssumeCapacity(.{
363 .offset = offset,
359 .offset = @intCast(offset),
364360 .type = relocation.encode(.rel, cpu_arch),
365 .addend = @intCast(symbol.address(.{ .plt = false }, elf_file)),
361 .addend = symbol.address(.{ .plt = false }, elf_file),
366362 });
367363 }
368364 }
......@@ -390,7 +386,7 @@ pub const ZigGotSection = struct {
390386 .st_info = elf.STT_OBJECT,
391387 .st_other = 0,
392388 .st_shndx = @intCast(elf_file.zig_got_section_index.?),
393 .st_value = st_value,
389 .st_value = @intCast(st_value),
394390 .st_size = st_size,
395391 };
396392 }
......@@ -461,10 +457,10 @@ pub const GotSection = struct {
461457 };
462458 }
463459
464 pub fn address(entry: Entry, elf_file: *Elf) u64 {
465 const ptr_bytes = @as(u64, elf_file.archPtrWidthBytes());
460 pub fn address(entry: Entry, elf_file: *Elf) i64 {
461 const ptr_bytes = elf_file.archPtrWidthBytes();
466462 const shdr = &elf_file.shdrs.items[elf_file.got_section_index.?];
467 return shdr.sh_addr + @as(u64, entry.cell_index) * ptr_bytes;
463 return @as(i64, @intCast(shdr.sh_addr)) + entry.cell_index * ptr_bytes;
468464 }
469465 };
470466
......@@ -499,11 +495,7 @@ pub const GotSection = struct {
499495 {
500496 got.flags.needs_rela = true;
501497 }
502 if (symbol.extra(elf_file)) |extra| {
503 var new_extra = extra;
504 new_extra.got = index;
505 symbol.setExtra(new_extra, elf_file);
506 } else try symbol.addExtra(.{ .got = index }, elf_file);
498 try symbol.addExtra(.{ .got = index }, elf_file);
507499 return index;
508500 }
509501
......@@ -529,11 +521,7 @@ pub const GotSection = struct {
529521 const symbol = elf_file.symbol(sym_index);
530522 symbol.flags.has_tlsgd = true;
531523 if (symbol.flags.import or elf_file.isEffectivelyDynLib()) got.flags.needs_rela = true;
532 if (symbol.extra(elf_file)) |extra| {
533 var new_extra = extra;
534 new_extra.tlsgd = index;
535 symbol.setExtra(new_extra, elf_file);
536 } else try symbol.addExtra(.{ .tlsgd = index }, elf_file);
524 try symbol.addExtra(.{ .tlsgd = index }, elf_file);
537525 }
538526
539527 pub fn addGotTpSymbol(got: *GotSection, sym_index: Symbol.Index, elf_file: *Elf) !void {
......@@ -546,11 +534,7 @@ pub const GotSection = struct {
546534 const symbol = elf_file.symbol(sym_index);
547535 symbol.flags.has_gottp = true;
548536 if (symbol.flags.import or elf_file.isEffectivelyDynLib()) got.flags.needs_rela = true;
549 if (symbol.extra(elf_file)) |extra| {
550 var new_extra = extra;
551 new_extra.gottp = index;
552 symbol.setExtra(new_extra, elf_file);
553 } else try symbol.addExtra(.{ .gottp = index }, elf_file);
537 try symbol.addExtra(.{ .gottp = index }, elf_file);
554538 }
555539
556540 pub fn addTlsDescSymbol(got: *GotSection, sym_index: Symbol.Index, elf_file: *Elf) !void {
......@@ -563,11 +547,7 @@ pub const GotSection = struct {
563547 const symbol = elf_file.symbol(sym_index);
564548 symbol.flags.has_tlsdesc = true;
565549 got.flags.needs_rela = true;
566 if (symbol.extra(elf_file)) |extra| {
567 var new_extra = extra;
568 new_extra.tlsdesc = index;
569 symbol.setExtra(new_extra, elf_file);
570 } else try symbol.addExtra(.{ .tlsdesc = index }, elf_file);
550 try symbol.addExtra(.{ .tlsdesc = index }, elf_file);
571551 }
572552
573553 pub fn size(got: GotSection, elf_file: *Elf) usize {
......@@ -628,8 +608,7 @@ pub const GotSection = struct {
628608 0;
629609 try writeInt(offset, elf_file, writer);
630610 } else {
631 const offset = @as(i64, @intCast(symbol.?.address(.{}, elf_file))) -
632 @as(i64, @intCast(elf_file.tpAddress()));
611 const offset = symbol.?.address(.{}, elf_file) - elf_file.tpAddress();
633612 try writeInt(offset, elf_file, writer);
634613 }
635614 },
......@@ -640,7 +619,7 @@ pub const GotSection = struct {
640619 } else {
641620 try writeInt(0, elf_file, writer);
642621 const offset = if (apply_relocs)
643 @as(i64, @intCast(symbol.?.address(.{}, elf_file))) - @as(i64, @intCast(elf_file.tlsAddress()))
622 symbol.?.address(.{}, elf_file) - elf_file.tlsAddress()
644623 else
645624 0;
646625 try writeInt(offset, elf_file, writer);
......@@ -666,7 +645,7 @@ pub const GotSection = struct {
666645
667646 switch (entry.tag) {
668647 .got => {
669 const offset = symbol.?.gotAddress(elf_file);
648 const offset: u64 = @intCast(symbol.?.gotAddress(elf_file));
670649 if (symbol.?.flags.import) {
671650 elf_file.addRelaDynAssumeCapacity(.{
672651 .offset = offset,
......@@ -679,7 +658,7 @@ pub const GotSection = struct {
679658 elf_file.addRelaDynAssumeCapacity(.{
680659 .offset = offset,
681660 .type = relocation.encode(.irel, cpu_arch),
682 .addend = @intCast(symbol.?.address(.{ .plt = false }, elf_file)),
661 .addend = symbol.?.address(.{ .plt = false }, elf_file),
683662 });
684663 continue;
685664 }
......@@ -689,14 +668,14 @@ pub const GotSection = struct {
689668 elf_file.addRelaDynAssumeCapacity(.{
690669 .offset = offset,
691670 .type = relocation.encode(.rel, cpu_arch),
692 .addend = @intCast(symbol.?.address(.{ .plt = false }, elf_file)),
671 .addend = symbol.?.address(.{ .plt = false }, elf_file),
693672 });
694673 }
695674 },
696675
697676 .tlsld => {
698677 if (is_dyn_lib) {
699 const offset = entry.address(elf_file);
678 const offset: u64 = @intCast(entry.address(elf_file));
700679 elf_file.addRelaDynAssumeCapacity(.{
701680 .offset = offset,
702681 .type = relocation.encode(.dtpmod, cpu_arch),
......@@ -705,7 +684,7 @@ pub const GotSection = struct {
705684 },
706685
707686 .tlsgd => {
708 const offset = symbol.?.tlsGdAddress(elf_file);
687 const offset: u64 = @intCast(symbol.?.tlsGdAddress(elf_file));
709688 if (symbol.?.flags.import) {
710689 elf_file.addRelaDynAssumeCapacity(.{
711690 .offset = offset,
......@@ -727,7 +706,7 @@ pub const GotSection = struct {
727706 },
728707
729708 .gottp => {
730 const offset = symbol.?.gotTpAddress(elf_file);
709 const offset: u64 = @intCast(symbol.?.gotTpAddress(elf_file));
731710 if (symbol.?.flags.import) {
732711 elf_file.addRelaDynAssumeCapacity(.{
733712 .offset = offset,
......@@ -738,18 +717,18 @@ pub const GotSection = struct {
738717 elf_file.addRelaDynAssumeCapacity(.{
739718 .offset = offset,
740719 .type = relocation.encode(.tpoff, cpu_arch),
741 .addend = @intCast(symbol.?.address(.{}, elf_file) - elf_file.tlsAddress()),
720 .addend = symbol.?.address(.{}, elf_file) - elf_file.tlsAddress(),
742721 });
743722 }
744723 },
745724
746725 .tlsdesc => {
747 const offset = symbol.?.tlsDescAddress(elf_file);
726 const offset: u64 = @intCast(symbol.?.tlsDescAddress(elf_file));
748727 elf_file.addRelaDynAssumeCapacity(.{
749728 .offset = offset,
750729 .sym = if (symbol.?.flags.import) extra.?.dynamic else 0,
751730 .type = relocation.encode(.tlsdesc, cpu_arch),
752 .addend = if (symbol.?.flags.import) 0 else @intCast(symbol.?.address(.{}, elf_file) - elf_file.tlsAddress()),
731 .addend = if (symbol.?.flags.import) 0 else symbol.?.address(.{}, elf_file) - elf_file.tlsAddress(),
753732 });
754733 },
755734 }
......@@ -826,7 +805,7 @@ pub const GotSection = struct {
826805 .st_info = elf.STT_OBJECT,
827806 .st_other = 0,
828807 .st_shndx = @intCast(elf_file.got_section_index.?),
829 .st_value = st_value,
808 .st_value = @intCast(st_value),
830809 .st_size = st_size,
831810 };
832811 }
......@@ -877,11 +856,7 @@ pub const PltSection = struct {
877856 const index = @as(u32, @intCast(plt.symbols.items.len));
878857 const symbol = elf_file.symbol(sym_index);
879858 symbol.flags.has_plt = true;
880 if (symbol.extra(elf_file)) |extra| {
881 var new_extra = extra;
882 new_extra.plt = index;
883 symbol.setExtra(new_extra, elf_file);
884 } else try symbol.addExtra(.{ .plt = index }, elf_file);
859 try symbol.addExtra(.{ .plt = index }, elf_file);
885860 try plt.symbols.append(gpa, sym_index);
886861 }
887862
......@@ -924,7 +899,7 @@ pub const PltSection = struct {
924899 const sym = elf_file.symbol(sym_index);
925900 assert(sym.flags.import);
926901 const extra = sym.extra(elf_file).?;
927 const r_offset = sym.gotPltAddress(elf_file);
902 const r_offset: u64 = @intCast(sym.gotPltAddress(elf_file));
928903 const r_sym: u64 = extra.dynamic;
929904 const r_type = relocation.encode(.jump_slot, cpu_arch);
930905 elf_file.rela_plt.appendAssumeCapacity(.{
......@@ -960,7 +935,7 @@ pub const PltSection = struct {
960935 .st_info = elf.STT_FUNC,
961936 .st_other = 0,
962937 .st_shndx = @intCast(elf_file.plt_section_index.?),
963 .st_value = sym.pltAddress(elf_file),
938 .st_value = @intCast(sym.pltAddress(elf_file)),
964939 .st_size = entrySize(cpu_arch),
965940 };
966941 }
......@@ -1033,13 +1008,13 @@ pub const PltSection = struct {
10331008 const aarch64 = struct {
10341009 fn write(plt: PltSection, elf_file: *Elf, writer: anytype) !void {
10351010 {
1036 const plt_addr = elf_file.shdrs.items[elf_file.plt_section_index.?].sh_addr;
1037 const got_plt_addr = elf_file.shdrs.items[elf_file.got_plt_section_index.?].sh_addr;
1011 const plt_addr: i64 = @intCast(elf_file.shdrs.items[elf_file.plt_section_index.?].sh_addr);
1012 const got_plt_addr: i64 = @intCast(elf_file.shdrs.items[elf_file.got_plt_section_index.?].sh_addr);
10381013 // TODO: relax if possible
10391014 // .got.plt[2]
10401015 const pages = try aarch64_util.calcNumberOfPages(plt_addr + 4, got_plt_addr + 16);
1041 const ldr_off = try math.divExact(u12, @truncate(got_plt_addr + 16), 8);
1042 const add_off: u12 = @truncate(got_plt_addr + 16);
1016 const ldr_off = try math.divExact(u12, @truncate(@as(u64, @bitCast(got_plt_addr + 16))), 8);
1017 const add_off: u12 = @truncate(@as(u64, @bitCast(got_plt_addr + 16)));
10431018
10441019 const preamble = &[_]Instruction{
10451020 Instruction.stp(
......@@ -1067,8 +1042,8 @@ pub const PltSection = struct {
10671042 const target_addr = sym.gotPltAddress(elf_file);
10681043 const source_addr = sym.pltAddress(elf_file);
10691044 const pages = try aarch64_util.calcNumberOfPages(source_addr, target_addr);
1070 const ldr_off = try math.divExact(u12, @truncate(target_addr), 8);
1071 const add_off: u12 = @truncate(target_addr);
1045 const ldr_off = try math.divExact(u12, @truncate(@as(u64, @bitCast(target_addr))), 8);
1046 const add_off: u12 = @truncate(@as(u64, @bitCast(target_addr)));
10721047 const insts = &[_]Instruction{
10731048 Instruction.adrp(.x16, pages),
10741049 Instruction.ldr(.x17, .x16, Instruction.LoadStoreOffset.imm(ldr_off)),
......@@ -1101,7 +1076,7 @@ pub const GotPltSection = struct {
11011076 {
11021077 // [0]: _DYNAMIC
11031078 const symbol = elf_file.symbol(elf_file.dynamic_index.?);
1104 try writer.writeInt(u64, symbol.address(.{}, elf_file), .little);
1079 try writer.writeInt(u64, @intCast(symbol.address(.{}, elf_file)), .little);
11051080 }
11061081 // [1]: 0x0
11071082 // [2]: 0x0
......@@ -1132,11 +1107,7 @@ pub const PltGotSection = struct {
11321107 const symbol = elf_file.symbol(sym_index);
11331108 symbol.flags.has_plt = true;
11341109 symbol.flags.has_got = true;
1135 if (symbol.extra(elf_file)) |extra| {
1136 var new_extra = extra;
1137 new_extra.plt_got = index;
1138 symbol.setExtra(new_extra, elf_file);
1139 } else try symbol.addExtra(.{ .plt_got = index }, elf_file);
1110 try symbol.addExtra(.{ .plt_got = index }, elf_file);
11401111 try plt_got.symbols.append(gpa, sym_index);
11411112 }
11421113
......@@ -1181,7 +1152,7 @@ pub const PltGotSection = struct {
11811152 .st_info = elf.STT_FUNC,
11821153 .st_other = 0,
11831154 .st_shndx = @intCast(elf_file.plt_got_section_index.?),
1184 .st_value = sym.pltGotAddress(elf_file),
1155 .st_value = @intCast(sym.pltGotAddress(elf_file)),
11851156 .st_size = 16,
11861157 };
11871158 }
......@@ -1212,7 +1183,7 @@ pub const PltGotSection = struct {
12121183 const target_addr = sym.gotAddress(elf_file);
12131184 const source_addr = sym.pltGotAddress(elf_file);
12141185 const pages = try aarch64_util.calcNumberOfPages(source_addr, target_addr);
1215 const off = try math.divExact(u12, @truncate(target_addr), 8);
1186 const off = try math.divExact(u12, @truncate(@as(u64, @bitCast(target_addr))), 8);
12161187 const insts = &[_]Instruction{
12171188 Instruction.adrp(.x16, pages),
12181189 Instruction.ldr(.x17, .x16, Instruction.LoadStoreOffset.imm(off)),
......@@ -1248,12 +1219,7 @@ pub const CopyRelSection = struct {
12481219 symbol.flags.@"export" = true;
12491220 symbol.flags.has_copy_rel = true;
12501221 symbol.flags.weak = false;
1251
1252 if (symbol.extra(elf_file)) |extra| {
1253 var new_extra = extra;
1254 new_extra.copy_rel = index;
1255 symbol.setExtra(new_extra, elf_file);
1256 } else try symbol.addExtra(.{ .copy_rel = index }, elf_file);
1222 try symbol.addExtra(.{ .copy_rel = index }, elf_file);
12571223 try copy_rel.symbols.append(gpa, sym_index);
12581224
12591225 const shared_object = symbol.file(elf_file).?.shared_object;
......@@ -1280,9 +1246,9 @@ pub const CopyRelSection = struct {
12801246 const symbol = elf_file.symbol(sym_index);
12811247 const shared_object = symbol.file(elf_file).?.shared_object;
12821248 const alignment = try symbol.dsoAlignment(elf_file);
1283 symbol.value = mem.alignForward(u64, shdr.sh_size, alignment);
1249 symbol.value = @intCast(mem.alignForward(u64, shdr.sh_size, alignment));
12841250 shdr.sh_addralign = @max(shdr.sh_addralign, alignment);
1285 shdr.sh_size = symbol.value + symbol.elfSym(elf_file).st_size;
1251 shdr.sh_size = @as(u64, @intCast(symbol.value)) + symbol.elfSym(elf_file).st_size;
12861252
12871253 const aliases = shared_object.symbolAliases(sym_index, elf_file);
12881254 for (aliases) |alias| {
......@@ -1303,7 +1269,7 @@ pub const CopyRelSection = struct {
13031269 assert(sym.flags.import and sym.flags.has_copy_rel);
13041270 const extra = sym.extra(elf_file).?;
13051271 elf_file.addRelaDynAssumeCapacity(.{
1306 .offset = sym.address(.{}, elf_file),
1272 .offset = @intCast(sym.address(.{}, elf_file)),
13071273 .sym = extra.dynamic,
13081274 .type = relocation.encode(.copy, cpu_arch),
13091275 });
......@@ -1335,11 +1301,7 @@ pub const DynsymSection = struct {
13351301 const index = @as(u32, @intCast(dynsym.entries.items.len + 1));
13361302 const sym = elf_file.symbol(sym_index);
13371303 sym.flags.has_dynamic = true;
1338 if (sym.extra(elf_file)) |extra| {
1339 var new_extra = extra;
1340 new_extra.dynamic = index;
1341 sym.setExtra(new_extra, elf_file);
1342 } else try sym.addExtra(.{ .dynamic = index }, elf_file);
1304 try sym.addExtra(.{ .dynamic = index }, elf_file);
13431305 const off = try elf_file.insertDynString(sym.name(elf_file));
13441306 try dynsym.entries.append(gpa, .{ .symbol_index = sym_index, .off = off });
13451307 }
src/link/Elf/thunks.zig+18-16
......@@ -7,7 +7,7 @@ pub fn createThunks(shndx: u32, elf_file: *Elf) !void {
77 assert(atoms.len > 0);
88
99 for (atoms) |atom_index| {
10 elf_file.atom(atom_index).?.value = @bitCast(@as(i64, -1));
10 elf_file.atom(atom_index).?.value = -1;
1111 }
1212
1313 var i: usize = 0;
......@@ -22,7 +22,8 @@ pub fn createThunks(shndx: u32, elf_file: *Elf) !void {
2222 const atom_index = atoms[i];
2323 const atom = elf_file.atom(atom_index).?;
2424 assert(atom.flags.alive);
25 if (atom.alignment.forward(shdr.sh_size) - start_atom.value >= max_distance) break;
25 if (@as(i64, @intCast(atom.alignment.forward(shdr.sh_size))) - start_atom.value >= max_distance)
26 break;
2627 atom.value = try advance(shdr, atom.size, atom.alignment);
2728 }
2829
......@@ -50,7 +51,8 @@ pub fn createThunks(shndx: u32, elf_file: *Elf) !void {
5051 };
5152 try thunk.symbols.put(gpa, target, {});
5253 }
53 atom.thunk_index = thunk_index;
54 try atom.addExtra(.{ .thunk = thunk_index }, elf_file);
55 atom.flags.thunk = true;
5456 }
5557
5658 thunk.value = try advance(shdr, thunk.size(elf_file), Atom.Alignment.fromNonzeroByteUnits(2));
......@@ -59,12 +61,12 @@ pub fn createThunks(shndx: u32, elf_file: *Elf) !void {
5961 }
6062}
6163
62fn advance(shdr: *elf.Elf64_Shdr, size: u64, alignment: Atom.Alignment) !u64 {
64fn advance(shdr: *elf.Elf64_Shdr, size: u64, alignment: Atom.Alignment) !i64 {
6365 const offset = alignment.forward(shdr.sh_size);
6466 const padding = offset - shdr.sh_size;
6567 shdr.sh_size += padding + size;
6668 shdr.sh_addralign = @max(shdr.sh_addralign, alignment.toByteUnits() orelse 1);
67 return offset;
69 return @intCast(offset);
6870}
6971
7072/// A branch will need an extender if its target is larger than
......@@ -78,7 +80,7 @@ fn maxAllowedDistance(cpu_arch: std.Target.Cpu.Arch) u32 {
7880}
7981
8082pub const Thunk = struct {
81 value: u64 = 0,
83 value: i64 = 0,
8284 output_section_index: u32 = 0,
8385 symbols: std.AutoArrayHashMapUnmanaged(Symbol.Index, void) = .{},
8486 output_symtab_ctx: Elf.SymtabCtx = .{},
......@@ -92,14 +94,14 @@ pub const Thunk = struct {
9294 return thunk.symbols.keys().len * trampolineSize(cpu_arch);
9395 }
9496
95 pub fn address(thunk: Thunk, elf_file: *Elf) u64 {
97 pub fn address(thunk: Thunk, elf_file: *Elf) i64 {
9698 const shdr = elf_file.shdrs.items[thunk.output_section_index];
97 return shdr.sh_addr + thunk.value;
99 return @as(i64, @intCast(shdr.sh_addr)) + thunk.value;
98100 }
99101
100 pub fn targetAddress(thunk: Thunk, sym_index: Symbol.Index, elf_file: *Elf) u64 {
102 pub fn targetAddress(thunk: Thunk, sym_index: Symbol.Index, elf_file: *Elf) i64 {
101103 const cpu_arch = elf_file.getTarget().cpu.arch;
102 return thunk.address(elf_file) + thunk.symbols.getIndex(sym_index).? * trampolineSize(cpu_arch);
104 return thunk.address(elf_file) + @as(i64, @intCast(thunk.symbols.getIndex(sym_index).? * trampolineSize(cpu_arch)));
103105 }
104106
105107 pub fn write(thunk: Thunk, elf_file: *Elf, writer: anytype) !void {
......@@ -131,7 +133,7 @@ pub const Thunk = struct {
131133 .st_info = elf.STT_FUNC,
132134 .st_other = 0,
133135 .st_shndx = @intCast(thunk.output_section_index),
134 .st_value = thunk.targetAddress(sym_index, elf_file),
136 .st_value = @intCast(thunk.targetAddress(sym_index, elf_file)),
135137 .st_size = trampolineSize(cpu_arch),
136138 };
137139 }
......@@ -204,9 +206,9 @@ const aarch64 = struct {
204206 if (target.flags.has_plt) return false;
205207 if (atom.output_section_index != target.output_section_index) return false;
206208 const target_atom = target.atom(elf_file).?;
207 if (target_atom.value == @as(u64, @bitCast(@as(i64, -1)))) return false;
208 const saddr = @as(i64, @intCast(atom.address(elf_file) + rel.r_offset));
209 const taddr: i64 = @intCast(target.address(.{}, elf_file));
209 if (target_atom.value == -1) return false;
210 const saddr = atom.address(elf_file) + @as(i64, @intCast(rel.r_offset));
211 const taddr = target.address(.{}, elf_file);
210212 _ = math.cast(i28, taddr + rel.r_addend - saddr) orelse return false;
211213 return true;
212214 }
......@@ -214,11 +216,11 @@ const aarch64 = struct {
214216 fn write(thunk: Thunk, elf_file: *Elf, writer: anytype) !void {
215217 for (thunk.symbols.keys(), 0..) |sym_index, i| {
216218 const sym = elf_file.symbol(sym_index);
217 const saddr = thunk.address(elf_file) + i * trampoline_size;
219 const saddr = thunk.address(elf_file) + @as(i64, @intCast(i * trampoline_size));
218220 const taddr = sym.address(.{}, elf_file);
219221 const pages = try util.calcNumberOfPages(saddr, taddr);
220222 try writer.writeInt(u32, Instruction.adrp(.x16, pages).toU32(), .little);
221 const off: u12 = @truncate(taddr);
223 const off: u12 = @truncate(@as(u64, @bitCast(taddr)));
222224 try writer.writeInt(u32, Instruction.add(.x16, .x16, off, false).toU32(), .little);
223225 try writer.writeInt(u32, Instruction.br(.x16).toU32(), .little);
224226 }
src/link/MachO/Atom.zig+1-1
......@@ -770,7 +770,7 @@ fn resolveRelocInner(
770770 };
771771 break :target math.cast(u64, target) orelse return error.Overflow;
772772 };
773 const pages = @as(u21, @bitCast(try aarch64.calcNumberOfPages(source, target)));
773 const pages = @as(u21, @bitCast(try aarch64.calcNumberOfPages(@intCast(source), @intCast(target))));
774774 aarch64.writeAdrpInst(pages, code[rel_offset..][0..4]);
775775 },
776776
src/link/MachO/synthetic.zig+5-5
......@@ -267,7 +267,7 @@ pub const StubsSection = struct {
267267 },
268268 .aarch64 => {
269269 // TODO relax if possible
270 const pages = try aarch64.calcNumberOfPages(source, target);
270 const pages = try aarch64.calcNumberOfPages(@intCast(source), @intCast(target));
271271 try writer.writeInt(u32, aarch64.Instruction.adrp(.x16, pages).toU32(), .little);
272272 const off = try math.divExact(u12, @truncate(target), 8);
273273 try writer.writeInt(
......@@ -411,7 +411,7 @@ pub const StubsHelperSection = struct {
411411 .aarch64 => {
412412 {
413413 // TODO relax if possible
414 const pages = try aarch64.calcNumberOfPages(sect.addr, dyld_private_addr);
414 const pages = try aarch64.calcNumberOfPages(@intCast(sect.addr), @intCast(dyld_private_addr));
415415 try writer.writeInt(u32, aarch64.Instruction.adrp(.x17, pages).toU32(), .little);
416416 const off: u12 = @truncate(dyld_private_addr);
417417 try writer.writeInt(u32, aarch64.Instruction.add(.x17, .x17, off, false).toU32(), .little);
......@@ -424,7 +424,7 @@ pub const StubsHelperSection = struct {
424424 ).toU32(), .little);
425425 {
426426 // TODO relax if possible
427 const pages = try aarch64.calcNumberOfPages(sect.addr + 12, dyld_stub_binder_addr);
427 const pages = try aarch64.calcNumberOfPages(@intCast(sect.addr + 12), @intCast(dyld_stub_binder_addr));
428428 try writer.writeInt(u32, aarch64.Instruction.adrp(.x16, pages).toU32(), .little);
429429 const off = try math.divExact(u12, @truncate(dyld_stub_binder_addr), 8);
430430 try writer.writeInt(u32, aarch64.Instruction.ldr(
......@@ -679,7 +679,7 @@ pub const ObjcStubsSection = struct {
679679 {
680680 const target = sym.getObjcSelrefsAddress(macho_file);
681681 const source = addr;
682 const pages = try aarch64.calcNumberOfPages(source, target);
682 const pages = try aarch64.calcNumberOfPages(@intCast(source), @intCast(target));
683683 try writer.writeInt(u32, aarch64.Instruction.adrp(.x1, pages).toU32(), .little);
684684 const off = try math.divExact(u12, @truncate(target), 8);
685685 try writer.writeInt(
......@@ -692,7 +692,7 @@ pub const ObjcStubsSection = struct {
692692 const target_sym = macho_file.getSymbol(macho_file.objc_msg_send_index.?);
693693 const target = target_sym.getGotAddress(macho_file);
694694 const source = addr + 2 * @sizeOf(u32);
695 const pages = try aarch64.calcNumberOfPages(source, target);
695 const pages = try aarch64.calcNumberOfPages(@intCast(source), @intCast(target));
696696 try writer.writeInt(u32, aarch64.Instruction.adrp(.x16, pages).toU32(), .little);
697697 const off = try math.divExact(u12, @truncate(target), 8);
698698 try writer.writeInt(
src/link/MachO/thunks.zig+1-1
......@@ -99,7 +99,7 @@ pub const Thunk = struct {
9999 const sym = macho_file.getSymbol(sym_index);
100100 const saddr = thunk.getAddress(macho_file) + i * trampoline_size;
101101 const taddr = sym.getAddress(.{}, macho_file);
102 const pages = try aarch64.calcNumberOfPages(saddr, taddr);
102 const pages = try aarch64.calcNumberOfPages(@intCast(saddr), @intCast(taddr));
103103 try writer.writeInt(u32, aarch64.Instruction.adrp(.x16, pages).toU32(), .little);
104104 const off: u12 = @truncate(taddr);
105105 try writer.writeInt(u32, aarch64.Instruction.add(.x16, .x16, off, false).toU32(), .little);
src/link/aarch64.zig+1-1
......@@ -25,7 +25,7 @@ pub fn writeLoadStoreRegInst(value: u12, code: *[4]u8) void {
2525 mem.writeInt(u32, code, inst.toU32(), .little);
2626}
2727
28pub fn calcNumberOfPages(saddr: u64, taddr: u64) error{Overflow}!i21 {
28pub fn calcNumberOfPages(saddr: i64, taddr: i64) error{Overflow}!i21 {
2929 const spage = math.cast(i32, saddr >> 12) orelse return error.Overflow;
3030 const tpage = math.cast(i32, taddr >> 12) orelse return error.Overflow;
3131 const pages = math.cast(i21, tpage - spage) orelse return error.Overflow;
test/link/elf.zig+182
......@@ -61,6 +61,7 @@ pub fn testAll(b: *Build, build_opts: BuildOptions) *Step {
6161 elf_step.dependOn(testAbsSymbols(b, .{ .target = musl_target }));
6262 elf_step.dependOn(testCommonSymbols(b, .{ .target = musl_target }));
6363 elf_step.dependOn(testCommonSymbolsInArchive(b, .{ .target = musl_target }));
64 elf_step.dependOn(testCommentString(b, .{ .target = musl_target }));
6465 elf_step.dependOn(testEmptyObject(b, .{ .target = musl_target }));
6566 elf_step.dependOn(testEntryPoint(b, .{ .target = musl_target }));
6667 elf_step.dependOn(testGcSections(b, .{ .target = musl_target }));
......@@ -72,6 +73,8 @@ pub fn testAll(b: *Build, build_opts: BuildOptions) *Step {
7273 elf_step.dependOn(testLinkingC(b, .{ .target = musl_target }));
7374 elf_step.dependOn(testLinkingCpp(b, .{ .target = musl_target }));
7475 elf_step.dependOn(testLinkingZig(b, .{ .target = musl_target }));
76 elf_step.dependOn(testMergeStrings(b, .{ .target = musl_target }));
77 elf_step.dependOn(testMergeStrings2(b, .{ .target = musl_target }));
7578 // https://github.com/ziglang/zig/issues/17451
7679 // elf_step.dependOn(testNoEhFrameHdr(b, .{ .target = musl_target }));
7780 elf_step.dependOn(testTlsStatic(b, .{ .target = musl_target }));
......@@ -81,6 +84,7 @@ pub fn testAll(b: *Build, build_opts: BuildOptions) *Step {
8184 elf_step.dependOn(testAsNeeded(b, .{ .target = gnu_target }));
8285 // https://github.com/ziglang/zig/issues/17430
8386 // elf_step.dependOn(testCanonicalPlt(b, .{ .target = gnu_target }));
87 elf_step.dependOn(testCommentString(b, .{ .target = gnu_target }));
8488 elf_step.dependOn(testCopyrel(b, .{ .target = gnu_target }));
8589 // https://github.com/ziglang/zig/issues/17430
8690 // elf_step.dependOn(testCopyrelAlias(b, .{ .target = gnu_target }));
......@@ -152,6 +156,8 @@ pub fn testAll(b: *Build, build_opts: BuildOptions) *Step {
152156 elf_step.dependOn(testThunks(b, .{ .target = aarch64_musl }));
153157
154158 // x86_64 self-hosted backend
159 elf_step.dependOn(testCommentString(b, .{ .use_llvm = false, .target = default_target }));
160 elf_step.dependOn(testCommentStringStaticLib(b, .{ .use_llvm = false, .target = default_target }));
155161 elf_step.dependOn(testEmitRelocatable(b, .{ .use_llvm = false, .target = x86_64_musl }));
156162 elf_step.dependOn(testEmitStaticLibZig(b, .{ .use_llvm = false, .target = x86_64_musl }));
157163 elf_step.dependOn(testGcSectionsZig(b, .{ .use_llvm = false, .target = default_target }));
......@@ -362,6 +368,36 @@ fn testCanonicalPlt(b: *Build, opts: Options) *Step {
362368 return test_step;
363369}
364370
371fn testCommentString(b: *Build, opts: Options) *Step {
372 const test_step = addTestStep(b, "comment-string", opts);
373
374 const exe = addExecutable(b, opts, .{ .name = "main", .zig_source_bytes =
375 \\pub fn main() void {}
376 });
377
378 const check = exe.checkObject();
379 check.dumpSection(".comment");
380 check.checkContains("zig");
381 test_step.dependOn(&check.step);
382
383 return test_step;
384}
385
386fn testCommentStringStaticLib(b: *Build, opts: Options) *Step {
387 const test_step = addTestStep(b, "comment-string-static-lib", opts);
388
389 const lib = addStaticLibrary(b, opts, .{ .name = "lib", .zig_source_bytes =
390 \\export fn foo() void {}
391 });
392
393 const check = lib.checkObject();
394 check.dumpSection(".comment");
395 check.checkContains("zig");
396 test_step.dependOn(&check.step);
397
398 return test_step;
399}
400
365401fn testCommonSymbols(b: *Build, opts: Options) *Step {
366402 const test_step = addTestStep(b, "common-symbols", opts);
367403
......@@ -2267,6 +2303,125 @@ fn testLinkingZig(b: *Build, opts: Options) *Step {
22672303 return test_step;
22682304}
22692305
2306// Adapted from https://github.com/rui314/mold/blob/main/test/elf/mergeable-strings.sh
2307fn testMergeStrings(b: *Build, opts: Options) *Step {
2308 const test_step = addTestStep(b, "merge-strings", opts);
2309
2310 const obj1 = addObject(b, opts, .{ .name = "a.o" });
2311 addCSourceBytes(obj1,
2312 \\#include <uchar.h>
2313 \\#include <wchar.h>
2314 \\char *cstr1 = "foo";
2315 \\wchar_t *wide1 = L"foo";
2316 \\char16_t *utf16_1 = u"foo";
2317 \\char32_t *utf32_1 = U"foo";
2318 , &.{"-O2"});
2319 obj1.linkLibC();
2320
2321 const obj2 = addObject(b, opts, .{ .name = "b.o" });
2322 addCSourceBytes(obj2,
2323 \\#include <stdio.h>
2324 \\#include <assert.h>
2325 \\#include <uchar.h>
2326 \\#include <wchar.h>
2327 \\extern char *cstr1;
2328 \\extern wchar_t *wide1;
2329 \\extern char16_t *utf16_1;
2330 \\extern char32_t *utf32_1;
2331 \\char *cstr2 = "foo";
2332 \\wchar_t *wide2 = L"foo";
2333 \\char16_t *utf16_2 = u"foo";
2334 \\char32_t *utf32_2 = U"foo";
2335 \\int main() {
2336 \\ printf("%p %p %p %p %p %p %p %p\n",
2337 \\ cstr1, cstr2, wide1, wide2, utf16_1, utf16_2, utf32_1, utf32_2);
2338 \\ assert((void*)cstr1 == (void*)cstr2);
2339 \\ assert((void*)wide1 == (void*)wide2);
2340 \\ assert((void*)utf16_1 == (void*)utf16_2);
2341 \\ assert((void*)utf32_1 == (void*)utf32_2);
2342 \\ assert((void*)wide1 == (void*)utf32_1);
2343 \\ assert((void*)cstr1 != (void*)wide1);
2344 \\ assert((void*)cstr1 != (void*)utf32_1);
2345 \\ assert((void*)wide1 != (void*)utf16_1);
2346 \\}
2347 , &.{"-O2"});
2348 obj2.linkLibC();
2349
2350 const exe = addExecutable(b, opts, .{ .name = "main" });
2351 exe.addObject(obj1);
2352 exe.addObject(obj2);
2353 exe.linkLibC();
2354
2355 const run = addRunArtifact(exe);
2356 run.expectExitCode(0);
2357 test_step.dependOn(&run.step);
2358
2359 return test_step;
2360}
2361
2362fn testMergeStrings2(b: *Build, opts: Options) *Step {
2363 const test_step = addTestStep(b, "merge-strings2", opts);
2364
2365 const obj1 = addObject(b, opts, .{ .name = "a", .zig_source_bytes =
2366 \\const std = @import("std");
2367 \\export fn foo() void {
2368 \\ var arr: [5:0]u16 = [_:0]u16{ 1, 2, 3, 4, 5 };
2369 \\ const slice = std.mem.sliceTo(&arr, 3);
2370 \\ std.testing.expectEqualSlices(u16, arr[0..2], slice) catch unreachable;
2371 \\}
2372 });
2373
2374 const obj2 = addObject(b, opts, .{ .name = "b", .zig_source_bytes =
2375 \\const std = @import("std");
2376 \\extern fn foo() void;
2377 \\pub fn main() void {
2378 \\ foo();
2379 \\ var arr: [5:0]u16 = [_:0]u16{ 5, 4, 3, 2, 1 };
2380 \\ const slice = std.mem.sliceTo(&arr, 3);
2381 \\ std.testing.expectEqualSlices(u16, arr[0..2], slice) catch unreachable;
2382 \\}
2383 });
2384
2385 {
2386 const exe = addExecutable(b, opts, .{ .name = "main1" });
2387 exe.addObject(obj1);
2388 exe.addObject(obj2);
2389
2390 const run = addRunArtifact(exe);
2391 run.expectExitCode(0);
2392 test_step.dependOn(&run.step);
2393
2394 const check = exe.checkObject();
2395 check.dumpSection(".rodata.str");
2396 check.checkContains("\x01\x00\x02\x00\x03\x00\x04\x00\x05\x00\x00\x00");
2397 check.dumpSection(".rodata.str");
2398 check.checkContains("\x05\x00\x04\x00\x03\x00\x02\x00\x01\x00\x00\x00");
2399 test_step.dependOn(&check.step);
2400 }
2401
2402 {
2403 const obj3 = addObject(b, opts, .{ .name = "c" });
2404 obj3.addObject(obj1);
2405 obj3.addObject(obj2);
2406
2407 const exe = addExecutable(b, opts, .{ .name = "main2" });
2408 exe.addObject(obj3);
2409
2410 const run = addRunArtifact(exe);
2411 run.expectExitCode(0);
2412 test_step.dependOn(&run.step);
2413
2414 const check = exe.checkObject();
2415 check.dumpSection(".rodata.str");
2416 check.checkContains("\x01\x00\x02\x00\x03\x00\x04\x00\x05\x00\x00\x00");
2417 check.dumpSection(".rodata.str");
2418 check.checkContains("\x05\x00\x04\x00\x03\x00\x02\x00\x01\x00\x00\x00");
2419 test_step.dependOn(&check.step);
2420 }
2421
2422 return test_step;
2423}
2424
22702425fn testNoEhFrameHdr(b: *Build, opts: Options) *Step {
22712426 const test_step = addTestStep(b, "no-eh-frame-hdr", opts);
22722427
......@@ -2528,6 +2683,33 @@ fn testRelocatableEhFrame(b: *Build, opts: Options) *Step {
25282683 return test_step;
25292684}
25302685
2686// Adapted from https://github.com/rui314/mold/blob/main/test/elf/relocatable-mergeable-sections.sh
2687fn testRelocatableMergeStrings(b: *Build, opts: Options) *Step {
2688 const test_step = addTestStep(b, "relocatable-merge-strings", opts);
2689
2690 const obj1 = addObject(b, opts, .{ .name = "a", .asm_source_bytes =
2691 \\.section .rodata.str1.1,"aMS",@progbits,1
2692 \\val1:
2693 \\.ascii "Hello \0"
2694 \\.section .rodata.str1.1,"aMS",@progbits,1
2695 \\val5:
2696 \\.ascii "World \0"
2697 \\.section .rodata.str1.1,"aMS",@progbits,1
2698 \\val7:
2699 \\.ascii "Hello \0"
2700 });
2701
2702 const obj2 = addObject(b, opts, .{ .name = "b" });
2703 obj2.addObject(obj1);
2704
2705 const check = obj2.checkObject();
2706 check.dumpSection(".rodata.str1.1");
2707 check.checkExact("Hello \x00World \x00");
2708 test_step.dependOn(&check.step);
2709
2710 return test_step;
2711}
2712
25312713fn testRelocatableNoEhFrame(b: *Build, opts: Options) *Step {
25322714 const test_step = addTestStep(b, "relocatable-no-eh-frame", opts);
25332715
test/link/link.zig-18
......@@ -1,21 +1,3 @@
1pub fn build(b: *Build) void {
2 const test_step = b.step("test-link", "Run link tests");
3 b.default_step = test_step;
4
5 const has_macos_sdk = b.option(bool, "has_macos_sdk", "whether the host provides a macOS SDK in system path");
6 const has_ios_sdk = b.option(bool, "has_ios_sdk", "whether the host provides a iOS SDK in system path");
7 const has_symlinks_windows = b.option(bool, "has_symlinks_windows", "whether the host is windows and has symlinks enabled");
8
9 const build_opts: BuildOptions = .{
10 .has_macos_sdk = has_macos_sdk orelse false,
11 .has_ios_sdk = has_ios_sdk orelse false,
12 .has_symlinks_windows = has_symlinks_windows orelse false,
13 };
14
15 test_step.dependOn(@import("elf.zig").testAll(b, build_opts));
16 test_step.dependOn(@import("macho.zig").testAll(b, build_opts));
17}
18
191pub const BuildOptions = struct {
202 has_macos_sdk: bool,
213 has_ios_sdk: bool,