This is an example of documentation generated by ZigDoc, an alternative to Zig's built-in Auto Doc feature. See also examples in other modes/formats. The project being documented here (as the example) is the Zig standard library. Private Function
fncomputeAreaDesc(phdrs: []elf.ElfN.Phdr) void {
@setRuntimeSafety(false);
@disableInstrumentation();
vartls_phdr: ?*elf.ElfN.Phdr = null;
varimg_base: usize = 0;
for (phdrs) |*phdr| {
switch (phdr.type) {
.PHDR => img_base = @intFromPtr(phdrs.ptr) - phdr.vaddr,
.TLS => tls_phdr = phdr,
else => {},
}
}
varalign_factor: usize = undefined;
varblock_init: []constu8 = undefined;
varblock_size: usize = undefined;
if (tls_phdr) |phdr| {
align_factor = phdr.@"align";
// The effective size in memory is represented by `memsz`; the length of the data stored// in the `PT.TLS` segment is `filesz` and may be less than the former.block_init = @as([*]u8, @ptrFromInt(img_base + phdr.vaddr))[0..phdr.filesz];
block_size = phdr.memsz;
} else {
align_factor = @alignOf(usize);
block_init = &[_]u8{};
block_size = 0;
}
// Offsets into the allocated TLS area.vardtv_offset: usize = undefined;
varabi_tcb_offset: usize = undefined;
varblock_offset: usize = undefined;
// Compute the total size of the ABI-specific data plus our own `ZigTcb` structure. All the// offsets calculated here assume a well-aligned base address.constarea_size = switch (current_variant) {
.I_original => blk: {
varl: usize = 0;
dtv_offset = l;
l += @sizeOf(Dtv);
// Add some padding here so that the TP (`abi_tcb_offset`) is aligned to `align_factor`// and the `ZigTcb` structure can be found by simply subtracting `@sizeOf(ZigTcb)` from// the TP.constdelta = (l + @sizeOf(ZigTcb)) & (align_factor - 1);
if (delta > 0)
l += align_factor - delta;
l += @sizeOf(ZigTcb);
abi_tcb_offset = l;
l += alignForward(@sizeOf(AbiTcb), align_factor);
block_offset = l;
l += block_size;
break :blkl;
},
.I_modified => blk: {
varl: usize = 0;
dtv_offset = l;
l += @sizeOf(Dtv);
// In this variant, the TLS blocks must begin immediately after the end of the ABI TCB,// with the TP pointing to the beginning of the TLS blocks. Add padding so that the TP// (`abi_tcb_offset`) is aligned to `align_factor` and the `ZigTcb` structure can be// found by subtracting `@sizeOf(AbiTcb) + @sizeOf(ZigTcb)` from the TP.constdelta = (l + @sizeOf(ZigTcb) + @sizeOf(AbiTcb)) & (align_factor - 1);
if (delta > 0)
l += align_factor - delta;
l += @sizeOf(ZigTcb);
abi_tcb_offset = l;
l += @sizeOf(AbiTcb);
block_offset = l;
l += block_size;
break :blkl;
},
.II => blk: {
varl: usize = 0;
block_offset = l;
l += alignForward(block_size, align_factor);
// The TP is aligned to `align_factor`.abi_tcb_offset = l;
l += @sizeOf(AbiTcb);
// The `ZigTcb` structure is right after the `AbiTcb` with no padding in between so it// can be easily found.l += @sizeOf(ZigTcb);
// It doesn't really matter where we put the DTV, so give it natural alignment.l = alignForward(l, @alignOf(Dtv));
dtv_offset = l;
l += @sizeOf(Dtv);
break :blkl;
},
};
area_desc = .{
.size = area_size,
.alignment = align_factor,
.dtv = .{
.offset = dtv_offset,
},
.abi_tcb = .{
.offset = abi_tcb_offset,
},
.block = .{
.init = block_init,
.offset = block_offset,
.size = block_size,
},
.gdt_entry_number = @as(usize, @bitCast(@as(isize, -1))),
};
}