authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-08-17 10:19:45+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-08-17 18:16:29+02:00
log5cb96681d92e7a410577215ff057e459f10304dc
treee20535becdbecd4effad04f3cc55d29c99a3c722
parente23fc3905f65d65a743cb60783830b85199ed83b

Move Mach-O to link/MachO.zig submodule

Remove `ptrWidth` since as of Catalina, all apps are 64bits only. Signed-off-by: Jakub Konka <kubkon@jakubkonka.com>

2 files changed, 96 insertions(+), 96 deletions(-)

src-self-hosted/link.zig+3-96
...@@ -158,6 +158,7 @@ pub const File = struct {...@@ -158,6 +158,7 @@ pub const File = struct {
158 }158 }
159 }159 }
160160
161 /// Commit pending changes and write headers.
161 pub fn flush(base: *File) !void {162 pub fn flush(base: *File) !void {
162 const tracy = trace(@src());163 const tracy = trace(@src());
163 defer tracy.end();164 defer tracy.end();
...@@ -1022,7 +1023,6 @@ pub const File = struct {...@@ -1022,7 +1023,6 @@ pub const File = struct {
1022 pub const abbrev_pad1 = 5;1023 pub const abbrev_pad1 = 5;
1023 pub const abbrev_parameter = 6;1024 pub const abbrev_parameter = 6;
10241025
1025 /// Commit pending changes and write headers.
1026 pub fn flush(self: *Elf) !void {1026 pub fn flush(self: *Elf) !void {
1027 const target_endian = self.base.options.target.cpu.arch.endian();1027 const target_endian = self.base.options.target.cpu.arch.endian();
1028 const foreign_endian = target_endian != std.Target.current.cpu.arch.endian();1028 const foreign_endian = target_endian != std.Target.current.cpu.arch.endian();
...@@ -2330,7 +2330,6 @@ pub const File = struct {...@@ -2330,7 +2330,6 @@ pub const File = struct {
2330 try self.pwriteDbgInfoNops(prev_padding_size, dbg_info_buf, next_padding_size, trailing_zero, file_pos);2330 try self.pwriteDbgInfoNops(prev_padding_size, dbg_info_buf, next_padding_size, trailing_zero, file_pos);
2331 }2331 }
23322332
2333 /// Must be called only after a successful call to `updateDecl`.
2334 pub fn updateDeclExports(2333 pub fn updateDeclExports(
2335 self: *Elf,2334 self: *Elf,
2336 module: *Module,2335 module: *Module,
...@@ -2832,99 +2831,7 @@ pub const File = struct {...@@ -2832,99 +2831,7 @@ pub const File = struct {
28322831
2833 };2832 };
28342833
2835 pub const MachO = struct {2834 pub const MachO = @import("link/MachO.zig");
2836 pub const base_tag: Tag = .macho;
2837
2838 base: File,
2839
2840 ptr_width: enum { p32, p64 },
2841
2842 error_flags: ErrorFlags = ErrorFlags{},
2843
2844 pub const TextBlock = struct {
2845 pub const empty = TextBlock{};
2846 };
2847
2848 pub const SrcFn = struct {
2849 pub const empty = SrcFn{};
2850 };
2851
2852 pub fn openPath(allocator: *Allocator, dir: fs.Dir, sub_path: []const u8, options: Options) !*File {
2853 assert(options.object_format == .macho);
2854
2855 const file = try dir.createFile(sub_path, .{ .truncate = false, .read = true, .mode = determineMode(options) });
2856 errdefer file.close();
2857
2858 var macho_file = try allocator.create(MachO);
2859 errdefer allocator.destroy(macho_file);
2860
2861 macho_file.* = openFile(allocator, file, options) catch |err| switch (err) {
2862 error.IncrFailed => try createFile(allocator, file, options),
2863 else => |e| return e,
2864 };
2865
2866 return &macho_file.base;
2867 }
2868
2869 /// Returns error.IncrFailed if incremental update could not be performed.
2870 fn openFile(allocator: *Allocator, file: fs.File, options: Options) !MachO {
2871 switch (options.output_mode) {
2872 .Exe => {},
2873 .Obj => {},
2874 .Lib => return error.IncrFailed,
2875 }
2876 var self: MachO = .{
2877 .base = .{
2878 .file = file,
2879 .tag = .macho,
2880 .options = options,
2881 .allocator = allocator,
2882 },
2883 .ptr_width = switch (options.target.cpu.arch.ptrBitWidth()) {
2884 32 => .p32,
2885 64 => .p64,
2886 else => return error.UnsupportedELFArchitecture,
2887 },
2888 };
2889 errdefer self.deinit();
2890
2891 // TODO implement reading the macho file
2892 return error.IncrFailed;
2893 //try self.populateMissingMetadata();
2894 //return self;
2895 }
2896
2897 /// Truncates the existing file contents and overwrites the contents.
2898 /// Returns an error if `file` is not already open with +read +write +seek abilities.
2899 fn createFile(allocator: *Allocator, file: fs.File, options: Options) !MachO {
2900 switch (options.output_mode) {
2901 .Exe => return error.TODOImplementWritingMachOExeFiles,
2902 .Obj => return error.TODOImplementWritingMachOObjFiles,
2903 .Lib => return error.TODOImplementWritingLibFiles,
2904 }
2905 }
2906
2907 /// Commit pending changes and write headers.
2908 pub fn flush(self: *MachO) !void {}
2909
2910 pub fn deinit(self: *MachO) void {}
2911
2912 pub fn allocateDeclIndexes(self: *MachO, decl: *Module.Decl) !void {}
2913
2914 pub fn updateDecl(self: *MachO, module: *Module, decl: *Module.Decl) !void {}
2915
2916 pub fn updateDeclLineNumber(self: *MachO, module: *Module, decl: *const Module.Decl) !void {}
2917
2918 /// Must be called only after a successful call to `updateDecl`.
2919 pub fn updateDeclExports(
2920 self: *MachO,
2921 module: *Module,
2922 decl: *const Module.Decl,
2923 exports: []const *Module.Export,
2924 ) !void {}
2925
2926 pub fn freeDecl(self: *MachO, decl: *Module.Decl) void {}
2927 };
2928};2835};
29292836
2930/// Saturating multiplication2837/// Saturating multiplication
...@@ -2965,7 +2872,7 @@ fn sectHeaderTo32(shdr: elf.Elf64_Shdr) elf.Elf32_Shdr {...@@ -2965,7 +2872,7 @@ fn sectHeaderTo32(shdr: elf.Elf64_Shdr) elf.Elf32_Shdr {
2965 };2872 };
2966}2873}
29672874
2968fn determineMode(options: Options) fs.File.Mode {2875pub fn determineMode(options: Options) fs.File.Mode {
2969 // On common systems with a 0o022 umask, 0o777 will still result in a file created2876 // On common systems with a 0o022 umask, 0o777 will still result in a file created
2970 // with 0o755 permissions, but it works appropriately if the system is configured2877 // with 0o755 permissions, but it works appropriately if the system is configured
2971 // more leniently. As another data point, C's fopen seems to open files with the2878 // more leniently. As another data point, C's fopen seems to open files with the
src-self-hosted/link/MachO.zig created+93
...@@ -0,0 +1,93 @@
1const MachO = @This();
2
3const std = @import("std");
4const Allocator = std.mem.Allocator;
5const assert = std.debug.assert;
6const fs = std.fs;
7
8const Module = @import("../Module.zig");
9const link = @import("../link.zig");
10const File = link.File;
11
12pub const base_tag: Tag = File.Tag.macho;
13
14base: File,
15
16error_flags: File.ErrorFlags = File.ErrorFlags{},
17
18pub const TextBlock = struct {
19 pub const empty = TextBlock{};
20};
21
22pub const SrcFn = struct {
23 pub const empty = SrcFn{};
24};
25
26pub fn openPath(allocator: *Allocator, dir: fs.Dir, sub_path: []const u8, options: link.Options) !*File {
27 assert(options.object_format == .macho);
28
29 const file = try dir.createFile(sub_path, .{ .truncate = false, .read = true, .mode = link.determineMode(options) });
30 errdefer file.close();
31
32 var macho_file = try allocator.create(MachO);
33 errdefer allocator.destroy(macho_file);
34
35 macho_file.* = openFile(allocator, file, options) catch |err| switch (err) {
36 error.IncrFailed => try createFile(allocator, file, options),
37 else => |e| return e,
38 };
39
40 return &macho_file.base;
41}
42
43/// Returns error.IncrFailed if incremental update could not be performed.
44fn openFile(allocator: *Allocator, file: fs.File, options: link.Options) !MachO {
45 switch (options.output_mode) {
46 .Exe => {},
47 .Obj => {},
48 .Lib => return error.IncrFailed,
49 }
50 var self: MachO = .{
51 .base = .{
52 .file = file,
53 .tag = .macho,
54 .options = options,
55 .allocator = allocator,
56 },
57 };
58 errdefer self.deinit();
59
60 // TODO implement reading the macho file
61 return error.IncrFailed;
62 //try self.populateMissingMetadata();
63 //return self;
64}
65
66/// Truncates the existing file contents and overwrites the contents.
67/// Returns an error if `file` is not already open with +read +write +seek abilities.
68fn createFile(allocator: *Allocator, file: fs.File, options: link.Options) !MachO {
69 switch (options.output_mode) {
70 .Exe => return error.TODOImplementWritingMachOExeFiles,
71 .Obj => return error.TODOImplementWritingMachOObjFiles,
72 .Lib => return error.TODOImplementWritingLibFiles,
73 }
74}
75
76pub fn flush(self: *MachO) !void {}
77
78pub fn deinit(self: *MachO) void {}
79
80pub fn allocateDeclIndexes(self: *MachO, decl: *Module.Decl) !void {}
81
82pub fn updateDecl(self: *MachO, module: *Module, decl: *Module.Decl) !void {}
83
84pub fn updateDeclLineNumber(self: *MachO, module: *Module, decl: *const Module.Decl) !void {}
85
86pub fn updateDeclExports(
87 self: *MachO,
88 module: *Module,
89 decl: *const Module.Decl,
90 exports: []const *Module.Export,
91) !void {}
92
93pub fn freeDecl(self: *MachO, decl: *Module.Decl) void {}