On Wednesday 26 August 2026 23:15:33 LIU Hao wrote:
> 在 2026-8-26 18:59, Martin Storsjö 写道:
> > On Wed, 26 Aug 2026, LIU Hao wrote:
> >
> > > That's fair enough. However this isn't a new function; it has been
> > > there for about twenty years, with external linkage. If user code
> > > references anything from pesect.c, it's likely to need this function
> > > anyway.
> >
> > Indeed, it has been available with external linkage for a long time, but
> > it isn't declared in any public headers, so if something breaks from
> > this going away, that's really not our fault IMO...
> >
> > > (Making it static might also make it eligible for inlining, which seems
> > > to save some bytes.)
> >
> > Yep, that's a good reason for doing the change.
>
> OK. I have pushed these patches now.
Ok, thank you for more details.
I think that user code would not call these internal functions which
even do not have public header. It is even quite hard to figure out that
they exists.
If the inlining is useful, what about moving all those functions from
pesect.c into the pseudo-reloc.c (which is the only caller of those
functions) and marking them as static? Just an idea. In the attachment
is such change.
diff --git a/mingw-w64-crt/Makefile.am b/mingw-w64-crt/Makefile.am
index 1d7eb87ccece..7305cda69119 100644
--- a/mingw-w64-crt/Makefile.am
+++ b/mingw-w64-crt/Makefile.am
@@ -124,7 +124,7 @@ src_libdloadhelper=libsrc/dloadhelper.c misc/delay-f.c
src_libmingw32=include/internal.h include/sect_attribs.h crt/seh_signal_dispatcher.h \
crt/crtexewin.c crt/dll_argv.c crt/gccmain.c crt/natstart.c crt/pseudo-reloc-list.c crt/wildcard.c \
crt/charmax.c crt/ucrtexewin.c crt/dllargv.c crt/_newmode.c crt/tlssup.c crt/xncommod.c \
- crt/cinitexe.c crt/merr.c crt/pesect.c crt/udllargc.c crt/xthdloc.c \
+ crt/cinitexe.c crt/merr.c crt/udllargc.c crt/xthdloc.c \
crt/mingw_custom.c crt/mingw_helpers.c \
crt/pseudo-reloc.c crt/udll_argv.c \
crt/usermatherr.c \
diff --git a/mingw-w64-crt/crt/pesect.c b/mingw-w64-crt/crt/pesect.c
deleted file mode 100644
index 84326aff2797..000000000000
--- a/mingw-w64-crt/crt/pesect.c
+++ /dev/null
@@ -1,93 +0,0 @@
-/**
- * This file has no copyright assigned and is placed in the Public Domain.
- * This file is part of the mingw-w64 runtime package.
- * No warranty is given; refer to the file DISCLAIMER.PD within this package.
- */
-
-#include <windows.h>
-
-extern IMAGE_DOS_HEADER __ImageBase;
-
-static WINBOOL
-_ValidateImageBase (PBYTE pImageBase)
-{
- PIMAGE_DOS_HEADER pDOSHeader;
- PIMAGE_NT_HEADERS pNTHeader;
- PIMAGE_OPTIONAL_HEADER pOptHeader;
-
- pDOSHeader = (PIMAGE_DOS_HEADER) pImageBase;
- if (pDOSHeader->e_magic != IMAGE_DOS_SIGNATURE)
- return FALSE;
- pNTHeader = (PIMAGE_NT_HEADERS) ((PBYTE) pDOSHeader + pDOSHeader->e_lfanew);
- if (pNTHeader->Signature != IMAGE_NT_SIGNATURE)
- return FALSE;
- pOptHeader = (PIMAGE_OPTIONAL_HEADER) &pNTHeader->OptionalHeader;
- if (pOptHeader->Magic != IMAGE_NT_OPTIONAL_HDR_MAGIC)
- return FALSE;
- return TRUE;
-}
-
-static PIMAGE_SECTION_HEADER
-_FindPESection (PBYTE pImageBase, DWORD_PTR rva)
-{
- PIMAGE_NT_HEADERS pNTHeader;
- PIMAGE_SECTION_HEADER pSection;
- unsigned int iSection;
-
- pNTHeader = (PIMAGE_NT_HEADERS) (pImageBase + ((PIMAGE_DOS_HEADER) pImageBase)->e_lfanew);
-
- for (iSection = 0, pSection = IMAGE_FIRST_SECTION (pNTHeader);
- iSection < pNTHeader->FileHeader.NumberOfSections;
- ++iSection,++pSection)
- {
- if (rva >= pSection->VirtualAddress
- && rva < pSection->VirtualAddress + pSection->Misc.VirtualSize)
- return pSection;
- }
- return NULL;
-}
-
-int __mingw_GetSectionCount (void);
-PIMAGE_SECTION_HEADER __mingw_GetSectionForAddress (LPVOID p);
-
-PIMAGE_SECTION_HEADER
-__mingw_GetSectionForAddress (LPVOID p)
-{
- PBYTE pImageBase;
- DWORD_PTR rva;
-
- pImageBase = (PBYTE) &__ImageBase;
- if (! _ValidateImageBase (pImageBase))
- return NULL;
-
- rva = (DWORD_PTR) (((PBYTE) p) - pImageBase);
- return _FindPESection (pImageBase, rva);
-}
-
-int
-__mingw_GetSectionCount (void)
-{
- PBYTE pImageBase;
- PIMAGE_NT_HEADERS pNTHeader;
-
- pImageBase = (PBYTE) &__ImageBase;
- if (! _ValidateImageBase (pImageBase))
- return 0;
-
- pNTHeader = (PIMAGE_NT_HEADERS) (pImageBase + ((PIMAGE_DOS_HEADER) pImageBase)->e_lfanew);
-
- return (int) pNTHeader->FileHeader.NumberOfSections;
-}
-
-
-PBYTE _GetPEImageBase (void);
-
-PBYTE
-_GetPEImageBase (void)
-{
- PBYTE pImageBase;
- pImageBase = (PBYTE) &__ImageBase;
- if (! _ValidateImageBase (pImageBase))
- return NULL;
- return pImageBase;
-}
diff --git a/mingw-w64-crt/crt/pseudo-reloc.c b/mingw-w64-crt/crt/pseudo-reloc.c
index dd08e718aeef..580dad149bd2 100644
--- a/mingw-w64-crt/crt/pseudo-reloc.c
+++ b/mingw-w64-crt/crt/pseudo-reloc.c
@@ -162,9 +162,84 @@ __report_error (const char *msg, ...)
the temporary access of code/read-only sections.
This step speeds up pseudo-relocation pass. */
#ifdef __MINGW64_VERSION_MAJOR
-extern int __mingw_GetSectionCount (void);
-extern PIMAGE_SECTION_HEADER __mingw_GetSectionForAddress (LPVOID p);
-extern PBYTE _GetPEImageBase (void);
+
+static WINBOOL
+_ValidateImageBase (PBYTE pImageBase)
+{
+ PIMAGE_DOS_HEADER pDOSHeader;
+ PIMAGE_NT_HEADERS pNTHeader;
+ PIMAGE_OPTIONAL_HEADER pOptHeader;
+
+ pDOSHeader = (PIMAGE_DOS_HEADER) pImageBase;
+ if (pDOSHeader->e_magic != IMAGE_DOS_SIGNATURE)
+ return FALSE;
+ pNTHeader = (PIMAGE_NT_HEADERS) ((PBYTE) pDOSHeader + pDOSHeader->e_lfanew);
+ if (pNTHeader->Signature != IMAGE_NT_SIGNATURE)
+ return FALSE;
+ pOptHeader = (PIMAGE_OPTIONAL_HEADER) &pNTHeader->OptionalHeader;
+ if (pOptHeader->Magic != IMAGE_NT_OPTIONAL_HDR_MAGIC)
+ return FALSE;
+ return TRUE;
+}
+
+static PIMAGE_SECTION_HEADER
+_FindPESection (PBYTE pImageBase, DWORD_PTR rva)
+{
+ PIMAGE_NT_HEADERS pNTHeader;
+ PIMAGE_SECTION_HEADER pSection;
+ unsigned int iSection;
+
+ pNTHeader = (PIMAGE_NT_HEADERS) (pImageBase + ((PIMAGE_DOS_HEADER) pImageBase)->e_lfanew);
+
+ for (iSection = 0, pSection = IMAGE_FIRST_SECTION (pNTHeader);
+ iSection < pNTHeader->FileHeader.NumberOfSections;
+ ++iSection,++pSection)
+ {
+ if (rva >= pSection->VirtualAddress
+ && rva < pSection->VirtualAddress + pSection->Misc.VirtualSize)
+ return pSection;
+ }
+ return NULL;
+}
+
+static PIMAGE_SECTION_HEADER
+__mingw_GetSectionForAddress (LPVOID p)
+{
+ PBYTE pImageBase;
+ DWORD_PTR rva;
+
+ pImageBase = (PBYTE) &__ImageBase;
+ if (! _ValidateImageBase (pImageBase))
+ return NULL;
+
+ rva = (DWORD_PTR) (((PBYTE) p) - pImageBase);
+ return _FindPESection (pImageBase, rva);
+}
+
+static int
+__mingw_GetSectionCount (void)
+{
+ PBYTE pImageBase;
+ PIMAGE_NT_HEADERS pNTHeader;
+
+ pImageBase = (PBYTE) &__ImageBase;
+ if (! _ValidateImageBase (pImageBase))
+ return 0;
+
+ pNTHeader = (PIMAGE_NT_HEADERS) (pImageBase + ((PIMAGE_DOS_HEADER) pImageBase)->e_lfanew);
+
+ return (int) pNTHeader->FileHeader.NumberOfSections;
+}
+
+static PBYTE
+_GetPEImageBase (void)
+{
+ PBYTE pImageBase;
+ pImageBase = (PBYTE) &__ImageBase;
+ if (! _ValidateImageBase (pImageBase))
+ return NULL;
+ return pImageBase;
+}
typedef struct sSecInfo {
/* Keeps altered section flags, or zero if nothing was changed. */
_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public