authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-07-28 10:30:26+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-07-28 10:30:26+02:00
log1fba9e1280efb935e935f09a196ae35d5391ed48
treeae2d71856c6d2093c575a9995ae271ab2e3c5390
parenta84951465b409495095a9598db0cae745f34fa7b
parent58defeeaa633d67ad4361b032f5d4a5c626af219
signaturebadge-check Signed by PGP key B5690EEEBB952194

Merge pull request #20834 from ziglang/macho-boundary-typo-fix

macho: fix typo in boundary symbols handling

3 files changed, 46 insertions(+), 8 deletions(-)

src/link/MachO.zig+6-6
......@@ -1658,7 +1658,7 @@ fn initSyntheticSections(self: *MachO) !void {
16581658 .maxprot = prot,
16591659 });
16601660 }
1661 } else if (eatPrefix(name, "segment$stop$")) |segname| {
1661 } else if (eatPrefix(name, "segment$end$")) |segname| {
16621662 if (self.getSegmentByName(segname) == null) { // TODO check segname is valid
16631663 const prot = getSegmentProt(segname);
16641664 _ = try self.segments.append(gpa, .{
......@@ -1675,7 +1675,7 @@ fn initSyntheticSections(self: *MachO) !void {
16751675 if (self.getSectionByName(segname, sectname) == null) {
16761676 _ = try self.addSection(segname, sectname, .{});
16771677 }
1678 } else if (eatPrefix(name, "section$stop$")) |actual_name| {
1678 } else if (eatPrefix(name, "section$end$")) |actual_name| {
16791679 const sep = mem.indexOfScalar(u8, actual_name, '$').?; // TODO error rather than a panic
16801680 const segname = actual_name[0..sep]; // TODO check segname is valid
16811681 const sectname = actual_name[sep + 1 ..]; // TODO check sectname is valid
......@@ -2267,8 +2267,8 @@ fn allocateSyntheticSymbols(self: *MachO) void {
22672267 const seg = self.segments.items[seg_id];
22682268 sym.value = seg.vmaddr;
22692269 }
2270 } else if (mem.startsWith(u8, name, "segment$stop$")) {
2271 const segname = name["segment$stop$".len..];
2270 } else if (mem.startsWith(u8, name, "segment$end$")) {
2271 const segname = name["segment$end$".len..];
22722272 if (self.getSegmentByName(segname)) |seg_id| {
22732273 const seg = self.segments.items[seg_id];
22742274 sym.value = seg.vmaddr + seg.vmsize;
......@@ -2283,8 +2283,8 @@ fn allocateSyntheticSymbols(self: *MachO) void {
22832283 sym.value = sect.addr;
22842284 sym.out_n_sect = sect_id;
22852285 }
2286 } else if (mem.startsWith(u8, name, "section$stop$")) {
2287 const actual_name = name["section$stop$".len..];
2286 } else if (mem.startsWith(u8, name, "section$end$")) {
2287 const actual_name = name["section$end$".len..];
22882288 const sep = mem.indexOfScalar(u8, actual_name, '$').?; // TODO error rather than a panic
22892289 const segname = actual_name[0..sep];
22902290 const sectname = actual_name[sep + 1 ..];
src/link/MachO/InternalObject.zig+2-2
......@@ -177,9 +177,9 @@ pub fn resolveBoundarySymbols(self: *InternalObject, macho_file: *MachO) !void {
177177 if (ref.getFile(macho_file) != null) continue;
178178 const name = sym.getName(macho_file);
179179 if (mem.startsWith(u8, name, "segment$start$") or
180 mem.startsWith(u8, name, "segment$stop$") or
180 mem.startsWith(u8, name, "segment$end$") or
181181 mem.startsWith(u8, name, "section$start$") or
182 mem.startsWith(u8, name, "section$stop$"))
182 mem.startsWith(u8, name, "section$end$"))
183183 {
184184 const gop = try boundary_symbols.getOrPut(name);
185185 if (!gop.found_existing) {
test/link/macho.zig+38
......@@ -53,6 +53,7 @@ pub fn testAll(b: *Build, build_opts: BuildOptions) *Step {
5353 macho_step.dependOn(testRelocatable(b, .{ .target = default_target }));
5454 macho_step.dependOn(testRelocatableZig(b, .{ .target = default_target }));
5555 macho_step.dependOn(testSectionBoundarySymbols(b, .{ .target = default_target }));
56 macho_step.dependOn(testSectionBoundarySymbols2(b, .{ .target = default_target }));
5657 macho_step.dependOn(testSegmentBoundarySymbols(b, .{ .target = default_target }));
5758 macho_step.dependOn(testSymbolStabs(b, .{ .target = default_target }));
5859 macho_step.dependOn(testStackSize(b, .{ .target = default_target }));
......@@ -1962,6 +1963,43 @@ fn testSectionBoundarySymbols(b: *Build, opts: Options) *Step {
19621963 return test_step;
19631964}
19641965
1966fn testSectionBoundarySymbols2(b: *Build, opts: Options) *Step {
1967 const test_step = addTestStep(b, "section-boundary-symbols-2", opts);
1968
1969 const exe = addExecutable(b, opts, .{ .name = "main", .c_source_bytes =
1970 \\#include <stdio.h>
1971 \\struct pair { int a; int b; };
1972 \\struct pair first __attribute__((section("__DATA,__pairs"))) = { 1, 2 };
1973 \\struct pair second __attribute__((section("__DATA,__pairs"))) = { 3, 4 };
1974 \\extern struct pair pairs_start __asm("section$start$__DATA$__pairs");
1975 \\extern struct pair pairs_end __asm("section$end$__DATA$__pairs");
1976 \\int main() {
1977 \\ printf("%d,%d\n", first.a, first.b);
1978 \\ printf("%d,%d\n", second.a, second.b);
1979 \\ struct pair* p;
1980 \\ for (p = &pairs_start; p < &pairs_end; p++) {
1981 \\ p->a = 0;
1982 \\ }
1983 \\ printf("%d,%d\n", first.a, first.b);
1984 \\ printf("%d,%d\n", second.a, second.b);
1985 \\ return 0;
1986 \\}
1987 });
1988
1989 const run = b.addRunArtifact(exe);
1990 run.skip_foreign_checks = true;
1991 run.expectStdOutEqual(
1992 \\1,2
1993 \\3,4
1994 \\0,2
1995 \\0,4
1996 \\
1997 );
1998 test_step.dependOn(&run.step);
1999
2000 return test_step;
2001}
2002
19652003fn testSegmentBoundarySymbols(b: *Build, opts: Options) *Step {
19662004 const test_step = addTestStep(b, "segment-boundary-symbols", opts);
19672005