Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cxbe: Fix broken XBE sections sizes #665

Merged
merged 1 commit into from
Apr 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions tools/cxbe/Xbe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -384,14 +384,6 @@ Xbe::Xbe(class Exe *x_Exe, const char *x_szTitle, bool x_bRetail, const std::vec
m_SectionHeader[v].dwVirtualAddr =
x_Exe->m_SectionHeader[v].m_virtual_addr + m_Header.dwPeBaseAddr;

if(v < m_Header.dwSections - 1)
m_SectionHeader[v].dwVirtualSize =
x_Exe->m_SectionHeader[v + 1].m_virtual_addr -
x_Exe->m_SectionHeader[v].m_virtual_addr;
else
m_SectionHeader[v].dwVirtualSize =
RoundUp(x_Exe->m_SectionHeader[v].m_virtual_size, 4);

m_SectionHeader[v].dwRawAddr = SectionCursor;

// calculate sizeof_raw by locating the last non-zero value in the raw section data
Expand All @@ -411,6 +403,20 @@ Xbe::Xbe(class Exe *x_Exe, const char *x_szTitle, bool x_bRetail, const std::vec
m_SectionHeader[v].dwSizeofRaw = RoundUp(r + 2, 4);
}

// calculate virtual size
if(v < m_Header.dwSections - 1)
m_SectionHeader[v].dwVirtualSize =
x_Exe->m_SectionHeader[v + 1].m_virtual_addr -
x_Exe->m_SectionHeader[v].m_virtual_addr;
Comment on lines +407 to +410
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've never really noticed how weird and seemingly unnecessary this code is - unless it causes some breakage I'm not seeing rn we should imho just remove this part entirely.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't be opposed to making this a separate issue/pr.

In my opinion, there's a lot more of cxbe that should be simplified to trust the generated PE from the linker.

else {
m_SectionHeader[v].dwVirtualSize =
RoundUp(x_Exe->m_SectionHeader[v].m_virtual_size, 4);

// force virtual size to be at least as large as the raw size
m_SectionHeader[v].dwVirtualSize = std::max(m_SectionHeader[v].dwSizeofRaw,
m_SectionHeader[v].dwVirtualSize);
}

SectionCursor += RoundUp(m_SectionHeader[v].dwSizeofRaw, 0x1000);

// head/tail reference count
Expand Down
Loading