aboutsummaryrefslogtreecommitdiff
path: root/lib/std
diff options
context:
space:
mode:
authorJakub Konka <kubkon@jakubkonka.com>2021-12-10 11:46:44 +0100
committerJakub Konka <kubkon@jakubkonka.com>2021-12-10 11:56:51 +0100
commit81e7d8505c086a93accb74e9f1a84abb8ff7cf24 (patch)
tree9f4a8814448537da53d2eeab838672401a024eb5 /lib/std
parent77836e08a2384450b5e7933094511b61e3c22140 (diff)
downloadzig-81e7d8505c086a93accb74e9f1a84abb8ff7cf24.tar.gz
zig-81e7d8505c086a93accb74e9f1a84abb8ff7cf24.zip
macho: move helper functions to libstd
Helper functions such as `commands.sectionName`, etc. should really belong in `std.macho.section_64` extern struct.
Diffstat (limited to 'lib/std')
-rw-r--r--lib/std/macho.zig60
1 files changed, 52 insertions, 8 deletions
diff --git a/lib/std/macho.zig b/lib/std/macho.zig
index dce6d10cd5..296d8d4307 100644
--- a/lib/std/macho.zig
+++ b/lib/std/macho.zig
@@ -1,3 +1,5 @@
+const std = @import("std");
+
pub const mach_header = extern struct {
magic: u32,
cputype: cpu_type_t,
@@ -9,14 +11,14 @@ pub const mach_header = extern struct {
};
pub const mach_header_64 = extern struct {
- magic: u32,
- cputype: cpu_type_t,
- cpusubtype: cpu_subtype_t,
- filetype: u32,
- ncmds: u32,
- sizeofcmds: u32,
- flags: u32,
- reserved: u32,
+ magic: u32 = MH_MAGIC_64,
+ cputype: cpu_type_t = 0,
+ cpusubtype: cpu_subtype_t = 0,
+ filetype: u32 = 0,
+ ncmds: u32 = 0,
+ sizeofcmds: u32 = 0,
+ flags: u32 = 0,
+ reserved: u32 = 0,
};
pub const fat_header = extern struct {
@@ -630,6 +632,10 @@ pub const segment_command_64 = extern struct {
/// number of sections in segment
nsects: u32 = 0,
flags: u32 = 0,
+
+ pub fn segName(seg: segment_command_64) []const u8 {
+ return parseName(&seg.segname);
+ }
};
/// A segment is made up of zero or more sections. Non-MH_OBJECT files have
@@ -728,8 +734,46 @@ pub const section_64 = extern struct {
/// reserved
reserved3: u32 = 0,
+
+ pub fn sectName(sect: section_64) []const u8 {
+ return parseName(&sect.sectname);
+ }
+
+ pub fn segName(sect: section_64) []const u8 {
+ return parseName(&sect.segname);
+ }
+
+ pub fn type_(sect: section_64) u8 {
+ return @truncate(u8, sect.flags & 0xff);
+ }
+
+ pub fn attrs(sect: section_64) u32 {
+ return sect.flags & 0xffffff00;
+ }
+
+ pub fn isCode(sect: section_64) bool {
+ const attr = sect.attrs();
+ return attr & S_ATTR_PURE_INSTRUCTIONS != 0 or attr & S_ATTR_SOME_INSTRUCTIONS != 0;
+ }
+
+ pub fn isDebug(sect: section_64) bool {
+ return sect.attrs() & S_ATTR_DEBUG != 0;
+ }
+
+ pub fn isDontDeadStrip(sect: section_64) bool {
+ return sect.attrs() & S_ATTR_NO_DEAD_STRIP != 0;
+ }
+
+ pub fn isDontDeadStripIfReferencesLive(sect: section_64) bool {
+ return sect.attrs() & S_ATTR_LIVE_SUPPORT != 0;
+ }
};
+fn parseName(name: *const [16]u8) []const u8 {
+ const len = std.mem.indexOfScalar(u8, name, @as(u8, 0)) orelse name.len;
+ return name[0..len];
+}
+
pub const nlist = extern struct {
n_strx: u32,
n_type: u8,