Skip to content

Commit 05b1a74

Browse files
committed
code cleanups
* in selfExePath, return errors instead of defaulting to bogus data * less invasive edits to the logic of link/Elf.zig * less indentation
1 parent 03f7cff commit 05b1a74

File tree

5 files changed

+45
-55
lines changed

5 files changed

+45
-55
lines changed

lib/std/fs.zig

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2234,40 +2234,39 @@ pub fn selfExePath(out_buffer: []u8) SelfExePathError![]u8 {
22342234
},
22352235
.openbsd => {
22362236
// OpenBSD doesn't support getting the path of a running process, so try to guess it
2237-
if (os.argv.len >= 1) {
2238-
const argv0 = mem.span(os.argv[0]);
2239-
if (mem.indexOf(u8, argv0, "/") != null) {
2240-
// argv[0] is a path (relative or absolute): use realpath(3) directly
2237+
if (os.argv.len == 0)
2238+
return error.FileNotFound;
2239+
2240+
const argv0 = mem.span(os.argv[0]);
2241+
if (mem.indexOf(u8, argv0, "/") != null) {
2242+
// argv[0] is a path (relative or absolute): use realpath(3) directly
2243+
var real_path_buf: [MAX_PATH_BYTES]u8 = undefined;
2244+
const real_path = try os.realpathZ(os.argv[0], &real_path_buf);
2245+
if (real_path.len > out_buffer.len)
2246+
return error.NameTooLong;
2247+
mem.copy(u8, out_buffer, real_path);
2248+
return out_buffer[0..real_path.len];
2249+
} else if (argv0.len != 0) {
2250+
// argv[0] is not empty (and not a path): search it inside PATH
2251+
const PATH = std.os.getenvZ("PATH") orelse return error.FileNotFound;
2252+
var path_it = mem.tokenize(PATH, &[_]u8{path.delimiter});
2253+
while (path_it.next()) |a_path| {
2254+
var resolved_path_buf: [MAX_PATH_BYTES]u8 = undefined;
2255+
const resolved_path = std.fmt.bufPrintZ(&resolved_path_buf, "{s}/{s}", .{
2256+
a_path,
2257+
os.argv[0],
2258+
}) catch continue;
2259+
22412260
var real_path_buf: [MAX_PATH_BYTES]u8 = undefined;
2242-
const real_path = try os.realpathZ(os.argv[0], &real_path_buf);
2243-
if (real_path.len > out_buffer.len)
2244-
return error.NameTooLong;
2245-
mem.copy(u8, out_buffer, real_path);
2246-
return out_buffer[0..real_path.len];
2247-
} else if (argv0.len != 0) {
2248-
// argv[0] is not empty (and not a path): search it inside PATH
2249-
const PATH = std.os.getenv("PATH") orelse "";
2250-
var path_it = mem.tokenize(PATH, &[_]u8{path.delimiter});
2251-
while (path_it.next()) |a_path| {
2252-
var resolved_path_buf: [MAX_PATH_BYTES-1:0]u8 = undefined;
2253-
const resolved_path = std.fmt.bufPrint(&resolved_path_buf, "{}/{}\x00", .{
2254-
a_path,
2255-
os.argv[0],
2256-
}) catch "";
2257-
2258-
var real_path_buf: [MAX_PATH_BYTES]u8 = undefined;
2259-
if (os.realpathZ(&resolved_path_buf, &real_path_buf) catch null) |real_path| {
2260-
// found a file, and hope it is the right file
2261-
if (real_path.len > out_buffer.len)
2262-
return error.NameTooLong;
2263-
mem.copy(u8, out_buffer, real_path);
2264-
return out_buffer[0..real_path.len];
2265-
}
2266-
}
2261+
if (os.realpathZ(&resolved_path_buf, &real_path_buf)) |real_path| {
2262+
// found a file, and hope it is the right file
2263+
if (real_path.len > out_buffer.len)
2264+
return error.NameTooLong;
2265+
mem.copy(u8, out_buffer, real_path);
2266+
return out_buffer[0..real_path.len];
2267+
} else |_| continue;
22672268
}
22682269
}
2269-
2270-
// sorry, we don't find it
22712270
return error.FileNotFound;
22722271
},
22732272
.windows => {

lib/std/os/bits/openbsd.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ pub const Stat = extern struct {
174174
blksize: blksize_t,
175175
flags: u32,
176176
gen: u32,
177-
birthtim: timespec,
177+
birthtim: timespec,
178178

179179
pub fn atime(self: Stat) timespec {
180180
return self.atim;
@@ -203,7 +203,7 @@ pub const dirent = extern struct {
203203
d_type: u8,
204204
d_namlen: u8,
205205
__d_padding: [4]u8,
206-
d_name: [MAXNAMLEN+1]u8,
206+
d_name: [MAXNAMLEN + 1]u8,
207207

208208
pub fn reclen(self: dirent) u16 {
209209
return self.d_reclen;

src/link/Elf.zig

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1448,14 +1448,10 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void {
14481448
};
14491449
try argv.append(try comp.get_libc_crt_file(arena, crt1o));
14501450
if (target_util.libc_needs_crti_crtn(target)) {
1451-
const crti_o = o: {
1452-
if (target.os.tag == .openbsd) {
1453-
break :o "crtbegin.o";
1454-
} else {
1455-
break :o "crti.o";
1456-
}
1457-
};
1458-
try argv.append(try comp.get_libc_crt_file(arena, crti_o));
1451+
try argv.append(try comp.get_libc_crt_file(arena, "crti.o"));
1452+
}
1453+
if (target.os.tag == .openbsd) {
1454+
try argv.append(try comp.get_libc_crt_file(arena, "crtbegin.o"));
14591455
}
14601456
}
14611457

@@ -1599,17 +1595,12 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void {
15991595

16001596
// crt end
16011597
if (link_in_crt) {
1602-
if (target.isAndroid() or target_util.libc_needs_crti_crtn(target)) {
1603-
const crtn_o = o: {
1604-
if (target.os.tag == .openbsd) {
1605-
break :o "crtend.o";
1606-
} else if (target.isAndroid()) {
1607-
break :o "crtend_android.o";
1608-
} else {
1609-
break :o "crtn.o";
1610-
}
1611-
};
1612-
try argv.append(try comp.get_libc_crt_file(arena, crtn_o));
1598+
if (target.isAndroid()) {
1599+
try argv.append(try comp.get_libc_crt_file(arena, "crtend_android.o"));
1600+
} else if (target.os.tag == .openbsd) {
1601+
try argv.append(try comp.get_libc_crt_file(arena, "crtend.o"));
1602+
} else if (target_util.libc_needs_crti_crtn(target)) {
1603+
try argv.append(try comp.get_libc_crt_file(arena, "crtn.o"));
16131604
}
16141605
}
16151606

src/stage1.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ pub const log = stage2.log;
2626
pub const log_level = stage2.log_level;
2727

2828
pub export fn main(argc: c_int, argv: [*][*:0]u8) c_int {
29-
std.os.argv = argv[0.. @intCast(usize, argc)];
30-
29+
std.os.argv = argv[0..@intCast(usize, argc)];
30+
3131
std.debug.maybeEnableSegfaultHandler();
3232

3333
zig_stage1_os_init();

src/target.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ pub fn supports_fpic(target: std.Target) bool {
161161
}
162162

163163
pub fn libc_needs_crti_crtn(target: std.Target) bool {
164-
return !(target.cpu.arch.isRISCV() or target.isAndroid());
164+
return !(target.cpu.arch.isRISCV() or target.isAndroid() or target.os.tag == .openbsd);
165165
}
166166

167167
pub fn isSingleThreaded(target: std.Target) bool {

0 commit comments

Comments
 (0)