From e2a2e6c14fe181989187d6b59a79c5fc32961250 Mon Sep 17 00:00:00 2001 From: Jonathan Marler Date: Mon, 18 Oct 2021 15:54:56 -0600 Subject: [PATCH] InstallRawStep: handle empty segments This fixes InstallRawStep to handle the cases when there are empty segments (segments with no sections). Before this change, if there was an empty segment with no sections, then the fixup of binaryOffsets is skipped. This fixes that by looping through each segment until a non-empty one is found and then fixing up the sections. This fixed an issue I was having with InstallRawStep for a bootloader I'm writing. --- lib/std/build/InstallRawStep.zig | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/std/build/InstallRawStep.zig b/lib/std/build/InstallRawStep.zig index ed01a6ea6e..2802f1ce90 100644 --- a/lib/std/build/InstallRawStep.zig +++ b/lib/std/build/InstallRawStep.zig @@ -96,8 +96,7 @@ const BinaryElfOutput = struct { sort.sort(*BinaryElfSegment, self.segments.items, {}, segmentSortCompare); - if (self.segments.items.len > 0) { - const firstSegment = self.segments.items[0]; + for (self.segments.items) |firstSegment, i| { if (firstSegment.firstSection) |firstSection| { const diff = firstSection.elfOffset - firstSegment.elfOffset; @@ -107,9 +106,10 @@ const BinaryElfOutput = struct { const basePhysicalAddress = firstSegment.physicalAddress; - for (self.segments.items) |segment| { + for (self.segments.items[i + 1 ..]) |segment| { segment.binaryOffset = segment.physicalAddress - basePhysicalAddress; } + break; } }