diff --git a/Source/ResourceEditor.cpp b/Source/ResourceEditor.cpp index 033d5f33..96a71106 100644 --- a/Source/ResourceEditor.cpp +++ b/Source/ResourceEditor.cpp @@ -514,14 +514,14 @@ DWORD CResourceEditor::Save(BYTE* pbBuf, DWORD &dwSize) { // This function scans exe sections and after find a match with given name // increments it's virtual size (auto fixes image size based on section alignment, etc) // Jim Park: The section name must be ASCII code. Do not TCHAR this stuff. -bool CResourceEditor::AddExtraVirtualSize2PESection(const char* pszSectionName, int addsize) +bool CResourceEditor::SetPESectionVirtualSize(const char* pszSectionName, DWORD newsize) { PIMAGE_SECTION_HEADER sectionHeadersArray = IMAGE_FIRST_SECTION(m_ntHeaders); // Refresh the headers of the sections that come after the resource section, and the data directory for (int i = 0; i < ConvertEndianness(m_ntHeaders->FileHeader.NumberOfSections); i++) { if (!strcmp((LPCSTR)sectionHeadersArray[i].Name, pszSectionName)) { - sectionHeadersArray[i].Misc.VirtualSize = AlignVA(AdjustVA(sectionHeadersArray[i].Misc.VirtualSize, addsize)); + sectionHeadersArray[i].Misc.VirtualSize = AlignVA(ConvertEndianness(newsize)); sectionHeadersArray[i].Characteristics &= ConvertEndianness((DWORD) ~IMAGE_SCN_MEM_DISCARDABLE); diff --git a/Source/ResourceEditor.h b/Source/ResourceEditor.h index c45127e4..ea7ec9e8 100644 --- a/Source/ResourceEditor.h +++ b/Source/ResourceEditor.h @@ -121,7 +121,7 @@ public: void FreeResource(BYTE* pbResource); // The section name must be in ASCII. - bool AddExtraVirtualSize2PESection(const char* pszSectionName, int addsize); + bool SetPESectionVirtualSize(const char* pszSectionName, DWORD newsize); DWORD Save(BYTE* pbBuf, DWORD &dwSize); // utitlity functions diff --git a/Source/build.cpp b/Source/build.cpp index cecd9c9e..047e98e1 100644 --- a/Source/build.cpp +++ b/Source/build.cpp @@ -2321,9 +2321,8 @@ int CEXEBuild::SetVarsSection() VerifyDeclaredUserVarRefs(&m_UserVarNames); int MaxUserVars = m_UserVarNames.getnum(); - // -1 because the default size is 1 int stringSize = NSIS_MAX_STRLEN*(build_unicode?sizeof(TCHAR):sizeof(char)); - if (!res_editor->AddExtraVirtualSize2PESection(NSIS_VARS_SECTION, (MaxUserVars - 1) * stringSize)) + if (!res_editor->SetPESectionVirtualSize(NSIS_VARS_SECTION, MaxUserVars * stringSize)) { ERROR_MSG(_T("Internal compiler error #12346: invalid exehead cannot find section \"%s\"!\n"), _T(NSIS_VARS_SECTION)); return PS_ERROR; diff --git a/Source/exehead/state.h b/Source/exehead/state.h index 39740b45..c6ebcb44 100644 --- a/Source/exehead/state.h +++ b/Source/exehead/state.h @@ -18,7 +18,15 @@ #include "fileform.h" -extern NSIS_STRING g_usrvars[1]; +#ifdef __GNUC__ +// GCC warns about array bounds when accessing g_usrvarssection[2] because it is only [1] at compile time, +// the other part of this hack is in util.c where g_usrvarsstart is initialized. +extern const NSIS_STRING*const g_usrvarsstart; +#define g_usrvars ( (NSIS_STRING*) (g_usrvarsstart) ) +#else +extern NSIS_STRING g_usrvarssection[1]; +#define g_usrvars g_usrvarssection +#endif #define state_command_line g_usrvars[20] #define state_install_directory g_usrvars[21] diff --git a/Source/exehead/util.c b/Source/exehead/util.c index 0e703e42..acf63318 100644 --- a/Source/exehead/util.c +++ b/Source/exehead/util.c @@ -42,12 +42,15 @@ TCHAR g_log_file[1024]; // nsis then removes the "DISCARDABLE" style from section (for safe) #ifdef _MSC_VER # pragma bss_seg(NSIS_VARS_SECTION) -NSIS_STRING g_usrvars[1]; +NSIS_STRING g_usrvarssection[1]; # pragma bss_seg() # pragma comment(linker, "/section:" NSIS_VARS_SECTION ",rwd") #else # ifdef __GNUC__ -NSIS_STRING g_usrvars[1] __attribute__((section (NSIS_VARS_SECTION))); +// GCC does not treat g_usrvarssection as a bss section so we keep the size as small as possible. +// NSIS_STRING g_usrvarssection[31] is required to remove this hack but that really bloats the exehead. +char g_usrvarssection[1] __attribute__((section (NSIS_VARS_SECTION))); +const NSIS_STRING*const g_usrvarsstart = (const NSIS_STRING*const) g_usrvarssection; # else # error Unknown compiler. You must implement the seperate PE section yourself. # endif