[llvm-objcopy][MachO] Fix segment's vmsize
This diff fixes the calculation of the field vmsize
in LC_SEGMENT/LC_SEGMENT_64 load commands.
Test plan: make check-all
Differential revision: https://reviews.llvm.org/D78799
diff --git a/llvm/tools/llvm-objcopy/MachO/MachOLayoutBuilder.cpp b/llvm/tools/llvm-objcopy/MachO/MachOLayoutBuilder.cpp
index e6c5dde..0ffe22a 100644
--- a/llvm/tools/llvm-objcopy/MachO/MachOLayoutBuilder.cpp
+++ b/llvm/tools/llvm-objcopy/MachO/MachOLayoutBuilder.cpp
@@ -144,6 +144,9 @@
uint64_t SegFileSize = 0;
uint64_t VMSize = 0;
for (std::unique_ptr<Section> &Sec : LC.Sections) {
+ assert(SegmentVmAddr <= Sec->Addr &&
+ "Section's address cannot be smaller than Segment's one");
+ uint32_t SectOffset = Sec->Addr - SegmentVmAddr;
if (IsObjectFile) {
if (Sec->isVirtualSection()) {
Sec->Offset = 0;
@@ -154,19 +157,16 @@
Sec->Size = Sec->Content.size();
SegFileSize += PaddingSize + Sec->Size;
}
- VMSize = std::max(VMSize, Sec->Addr + Sec->Size);
} else {
if (Sec->isVirtualSection()) {
Sec->Offset = 0;
- VMSize += Sec->Size;
} else {
- uint32_t SectOffset = Sec->Addr - SegmentVmAddr;
Sec->Offset = SegOffset + SectOffset;
Sec->Size = Sec->Content.size();
SegFileSize = std::max(SegFileSize, SectOffset + Sec->Size);
- VMSize = std::max(VMSize, SegFileSize);
}
}
+ VMSize = std::max(VMSize, SectOffset + Sec->Size);
}
if (IsObjectFile) {