Discussion:
[patch]: Add MS __ImageBase symbol to 32-bit pe.em
Kai Tietz
2007-11-02 08:44:33 UTC
Permalink
Hi,

I moved the code from 64-bit pep.em to pe.em for __ImageBase. This is a MS
compatible symbol for __image_base__.

ChangleLog for /ld/emultempl:

2007-11-02 Kai Tietz <***@onevision.com>

* emultempl/pe.em: (init): Add __ImageBase symbol.
(MSIMAGEBASEOFF): New.
(set_pe_name): Keep __image_base and __ImageBase value synch.
(_set_symbols): Likewise.

Is this OK for mainline ?

Cheers,
i.A. Kai Tietz



| (\_/) This is Bunny. Copy and paste Bunny
| (='.'=) into your signature to help him gain
| (")_(") world domination.

------------------------------------------------------------------------------------------
OneVision Software Entwicklungs GmbH & Co. KG
Dr.-Leo-Ritter-Straße 9 - 93049 Regensburg
Tel: +49.(0)941.78004.0 - Fax: +49.(0)941.78004.489 - www.OneVision.com
Commerzbank Regensburg - BLZ 750 400 62 - Konto 6011050
Handelsregister: HRA 6744, Amtsgericht Regensburg
KomplementÀrin: OneVision Software Entwicklungs Verwaltungs GmbH
Dr.-Leo-Ritter-Straße 9 – 93049 Regensburg
Handelsregister: HRB 8932, Amtsgericht Regensburg - GeschÀftsfÌhrer:
Ulrike Döhler, Manuela Kluger
Pedro Alves
2007-11-02 15:31:03 UTC
Permalink
+#define MSIMAGEBASEOFF 2
+ D(ImageBase,"__ImageBase", NT_EXE_IMAGE_BASE),
D(SectionAlignment,"__section_alignment__", PE_DEF_SECTION_ALIGNMENT),
D(FileAlignment,"__file_alignment__", PE_DEF_FILE_ALIGNMENT),
D(MajorOperatingSystemVersion,"__major_os_version__", 4),
@@ -374,6 +376,8 @@ set_pe_name (char *name, long val)
{
init[i].value = val;
init[i].inited = 1;
+ if (strcmp (name,"__image_base__") == 0)
+ set_pe_name ("__ImageBase", val);
Shouldn't it be ___ImageBase (3 underscores) on i386 9x/NT
Windows, as opposed to x64, which I think I've read that it
is not underscored? This file is also used for arm-wince,
which is not underscored. (Although this __ImageBase thing
isn't as useful in Windows CE, because HMODULE == base address
isn't true there.)

You may use U take care of the differences between targets,
it's defined in the top of pe.em:

#define U(S) ${INITIAL_SYMBOL_CHAR} S

Where INITIAL_SYMBOL_CHAR is "_" in i386.

Like so:
+ set_pe_name (U ("__ImageBase"), val);

Cheers,
Pedro Alves
Kai Tietz
2007-11-02 15:49:30 UTC
Permalink
Hi Pedro,
Post by Pedro Alves
+#define MSIMAGEBASEOFF 2
+ D(ImageBase,"__ImageBase", NT_EXE_IMAGE_BASE),
D(SectionAlignment,"__section_alignment__",
PE_DEF_SECTION_ALIGNMENT),
Post by Pedro Alves
D(FileAlignment,"__file_alignment__", PE_DEF_FILE_ALIGNMENT),
D(MajorOperatingSystemVersion,"__major_os_version__", 4),
@@ -374,6 +376,8 @@ set_pe_name (char *name, long val)
{
init[i].value = val;
init[i].inited = 1;
+ if (strcmp (name,"__image_base__") == 0)
+ set_pe_name ("__ImageBase", val);
Shouldn't it be ___ImageBase (3 underscores) on i386 9x/NT
Windows, as opposed to x64, which I think I've read that it
is not underscored? This file is also used for arm-wince,
which is not underscored. (Although this __ImageBase thing
isn't as useful in Windows CE, because HMODULE == base address
isn't true there.)
You may use U take care of the differences between targets,
#define U(S) ${INITIAL_SYMBOL_CHAR} S
Where INITIAL_SYMBOL_CHAR is "_" in i386.
+ set_pe_name (U ("__ImageBase"), val);
The symbol is defined by MS as 'extern IMAGE_DOS_HEADER _ImageBase'. So
the name is correct. I will add the U.
For Windows CE and arm-wince __image_base__ is invalid too (the same thing
as for _ImageBase).
No, for x64 it is underscored, too.

Cheers,
i.A. Kai Tietz


| (\_/) This is Bunny. Copy and paste Bunny
| (='.'=) into your signature to help him gain
| (")_(") world domination.

------------------------------------------------------------------------------------------
OneVision Software Entwicklungs GmbH & Co. KG
Dr.-Leo-Ritter-Straße 9 - 93049 Regensburg
Tel: +49.(0)941.78004.0 - Fax: +49.(0)941.78004.489 - www.OneVision.com
Commerzbank Regensburg - BLZ 750 400 62 - Konto 6011050
Handelsregister: HRA 6744, Amtsgericht Regensburg
KomplementÀrin: OneVision Software Entwicklungs Verwaltungs GmbH
Dr.-Leo-Ritter-Straße 9 – 93049 Regensburg
Handelsregister: HRB 8932, Amtsgericht Regensburg - GeschÀftsfÌhrer:
Ulrike Döhler, Manuela Kluger
Pedro Alves
2007-11-02 16:18:40 UTC
Permalink
Post by Kai Tietz
Hi Pedro,
Post by Pedro Alves
+#define MSIMAGEBASEOFF 2
+ D(ImageBase,"__ImageBase", NT_EXE_IMAGE_BASE),
D(SectionAlignment,"__section_alignment__",
PE_DEF_SECTION_ALIGNMENT),
Post by Pedro Alves
D(FileAlignment,"__file_alignment__", PE_DEF_FILE_ALIGNMENT),
D(MajorOperatingSystemVersion,"__major_os_version__", 4),
@@ -374,6 +376,8 @@ set_pe_name (char *name, long val)
{
init[i].value = val;
init[i].inited = 1;
+ if (strcmp (name,"__image_base__") == 0)
+ set_pe_name ("__ImageBase", val);
Shouldn't it be ___ImageBase (3 underscores) on i386 9x/NT
Windows, as opposed to x64, which I think I've read that it
is not underscored? This file is also used for arm-wince,
which is not underscored. (Although this __ImageBase thing
isn't as useful in Windows CE, because HMODULE == base address
isn't true there.)
You may use U take care of the differences between targets,
#define U(S) ${INITIAL_SYMBOL_CHAR} S
Where INITIAL_SYMBOL_CHAR is "_" in i386.
+ set_pe_name (U ("__ImageBase"), val);
The symbol is defined by MS as 'extern IMAGE_DOS_HEADER _ImageBase'. So
the name is correct. I will add the U.
Where is that documented? It wouldn't be the first time
MS docs wheren't exactly in line with reality...

Did you try it? This doesn't build here with MSVC2005.

#include "stdafx.h"
#include <windows.h>
#include <stdio.h>

EXTERN_C IMAGE_DOS_HEADER _ImageBase;
#define HINST_THISCOMPONENT ((HINSTANCE)&_ImageBase)

int _tmain(int argc, _TCHAR* argv[])
{
printf ("%p\n", HINST_THISCOMPONENT);
return 0;
}
Post by Kai Tietz
Linking...
main.obj : error LNK2001: unresolved external symbol __ImageBase
But, two underscores on __ImageBase on the C/C++ side links correctly.
Post by Kai Tietz
For Windows CE and arm-wince __image_base__ is invalid too (the same thing
as for _ImageBase).
I know. It hasn't bothered me enough yet to fix it ;)
Post by Kai Tietz
No, for x64 it is underscored, too.
Humm, this is what I remembered reading:
http://lists-archives.org/mingw-users/07903-leading-underscore-or-not.html

Cheers,
Pedro Alves
Jerker Bäck
2007-11-02 18:23:34 UTC
Permalink
The usual expression is:

#if defined(_M_IA64)
#pragma section(".base", long, read)
extern "C" __declspec(allocate(".base"))
const IMAGE_DOS_HEADER __ImageBase;
#else
extern "C" const IMAGE_DOS_HEADER __ImageBase;
#endif

and used like:
PIMAGE_DOS_HEADER dhd = &__ImageBase;
or:
HINSTANCE hModulehandle = (HINSTANCE)&__ImageBase; // e.g. ATL

Where __ImageBase represents the PE DOS header of a binary. This variable is not defined anywhere, nor is it documented. ATL uses this method to get the module handle (HINSTANCE) of the current process, thus it must always point to the current executable image, right? As I understand, using __ImageBase in a DLL, you will get the PE header of the executable.

I always thought it was defined by the MS compiler. So this variable is valid for mingw also?

cheers
Jerker Bäck
Kai Tietz
2007-11-03 09:23:54 UTC
Permalink
Hi Pedro,
Post by Pedro Alves
Post by Kai Tietz
Hi Pedro,
Post by Pedro Alves
+#define MSIMAGEBASEOFF 2
+ D(ImageBase,"__ImageBase", NT_EXE_IMAGE_BASE),
D(SectionAlignment,"__section_alignment__",
PE_DEF_SECTION_ALIGNMENT),
Post by Pedro Alves
D(FileAlignment,"__file_alignment__", PE_DEF_FILE_ALIGNMENT),
D(MajorOperatingSystemVersion,"__major_os_version__", 4),
@@ -374,6 +376,8 @@ set_pe_name (char *name, long val)
{
init[i].value = val;
init[i].inited = 1;
+ if (strcmp (name,"__image_base__") == 0)
+ set_pe_name ("__ImageBase", val);
Shouldn't it be ___ImageBase (3 underscores) on i386 9x/NT
Windows, as opposed to x64, which I think I've read that it
is not underscored? This file is also used for arm-wince,
which is not underscored. (Although this __ImageBase thing
isn't as useful in Windows CE, because HMODULE == base address
isn't true there.)
You may use U take care of the differences between targets,
#define U(S) ${INITIAL_SYMBOL_CHAR} S
Where INITIAL_SYMBOL_CHAR is "_" in i386.
+ set_pe_name (U ("__ImageBase"), val);
The symbol is defined by MS as 'extern IMAGE_DOS_HEADER _ImageBase'. So
the name is correct. I will add the U.
Where is that documented? It wouldn't be the first time
MS docs wheren't exactly in line with reality...
Did you try it? This doesn't build here with MSVC2005.
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
EXTERN_C IMAGE_DOS_HEADER _ImageBase;
#define HINST_THISCOMPONENT ((HINSTANCE)&_ImageBase)
int _tmain(int argc, _TCHAR* argv[])
{
printf ("%p\n", HINST_THISCOMPONENT);
return 0;
}
Post by Kai Tietz
Linking...
main.obj : error LNK2001: unresolved external symbol __ImageBase
But, two underscores on __ImageBase on the C/C++ side links correctly.
AFAIC read your version is correct. There must be two underscores in the
header and therefore three in ld. I took a look at the MS headers (as
Jerker did) and MS defines it with two underscores 8|
I will add the same issue to pep.em, too. Because I defined it there
false, too.
Post by Pedro Alves
Post by Kai Tietz
For Windows CE and arm-wince __image_base__ is invalid too (the same thing
as for _ImageBase).
I know. It hasn't bothered me enough yet to fix it ;)
Post by Kai Tietz
No, for x64 it is underscored, too.
http://lists-archives.org/mingw-users/07903-leading-underscore-or-not.html
I can just tell, that for the x86_64 version underscores are generated for
symbols. And this target has the same base (for this configuration) as the
32-bit version. So IMHO there are underscores.

ChangleLog for /ld/emultempl:

2007-11-02 Kai Tietz <***@onevision.com>

* emultempl/pe.em: (init): Add __ImageBase symbol.
(MSIMAGEBASEOFF): New.
(set_pe_name): Keep __image_base and __ImageBase value synch.
(_set_symbols): Likewise.
* emultempl/pep.em: Likewise.

Cheers,
i.A. Kai Tietz



| (\_/) This is Bunny. Copy and paste Bunny
| (='.'=) into your signature to help him gain
| (")_(") world domination.

------------------------------------------------------------------------------------------
OneVision Software Entwicklungs GmbH & Co. KG
Dr.-Leo-Ritter-Straße 9 - 93049 Regensburg
Tel: +49.(0)941.78004.0 - Fax: +49.(0)941.78004.489 - www.OneVision.com
Commerzbank Regensburg - BLZ 750 400 62 - Konto 6011050
Handelsregister: HRA 6744, Amtsgericht Regensburg
KomplementÀrin: OneVision Software Entwicklungs Verwaltungs GmbH
Dr.-Leo-Ritter-Straße 9 – 93049 Regensburg
Handelsregister: HRB 8932, Amtsgericht Regensburg - GeschÀftsfÌhrer:
Ulrike Döhler, Manuela Kluger
Kai Tietz
2007-11-03 09:58:34 UTC
Permalink
Post by Jerker Bäck
#if defined(_M_IA64)
#pragma section(".base", long, read)
extern "C" __declspec(allocate(".base"))
const IMAGE_DOS_HEADER __ImageBase;
#else
extern "C" const IMAGE_DOS_HEADER __ImageBase;
#endif
PIMAGE_DOS_HEADER dhd = &__ImageBase;
HINSTANCE hModulehandle = (HINSTANCE)&__ImageBase; // e.g. ATL
Where __ImageBase represents the PE DOS header of a binary. This
variable is not defined anywhere, nor is it documented. ATL uses
this method to get the module handle (HINSTANCE) of the current
process, thus it must always point to the current executable image,
right? As I understand, using __ImageBase in a DLL, you will get the
PE header of the executable.
I always thought it was defined by the MS compiler. So this variable
is valid for mingw also?
The variable is in mingw the ld symbol '__image_base__'. So the definition
for current ld (before this patch) would be something like.
#define __ImageBase _image_base__
extern "C" IMAGE_DOS_HEADER __ImageBase;

One leading underscore needs to be added for gcc, which does not
underscore user-variables.
You can find some more information about this symbol, when you take a look
at the 'delayhlp.cpp' file of the MS include folder.

Cheers,
i.A. Kai Tietz

| (\_/) This is Bunny. Copy and paste Bunny
| (='.'=) into your signature to help him gain
| (")_(") world domination.

------------------------------------------------------------------------------------------
OneVision Software Entwicklungs GmbH & Co. KG
Dr.-Leo-Ritter-Straße 9 - 93049 Regensburg
Tel: +49.(0)941.78004.0 - Fax: +49.(0)941.78004.489 - www.OneVision.com
Commerzbank Regensburg - BLZ 750 400 62 - Konto 6011050
Handelsregister: HRA 6744, Amtsgericht Regensburg
Komplementärin: OneVision Software Entwicklungs Verwaltungs GmbH
Dr.-Leo-Ritter-Straße 9 – 93049 Regensburg
Handelsregister: HRB 8932, Amtsgericht Regensburg - Geschäftsführer:
Ulrike Döhler, Manuela Klug
Jerker Bäck
2007-11-03 11:15:28 UTC
Permalink
Post by Kai Tietz
You can find some more information about this symbol, when you take a look
at the 'delayhlp.cpp' file of the MS include folder.
Coincidentally, I'm porting delayhlp.cpp to the Interix single rooted file system right now. The tricky part is to do a directory search for the delay loaded DLLs using native NT calls (Interix cannot use any Windows user libraries, just ntdll.dll).

Are you planning to introduce support for the PE Delay Import Directory to mingw? That would truly be very interesting.

cheers
Jerker
Kai Tietz
2007-11-06 08:44:21 UTC
Permalink
Hi Pedro,
Post by Pedro Alves
But, two underscores on __ImageBase on the C/C++ side links correctly.
AFAIC read, you are right. There must be two underscores in the header and
therefore three in ld. I took a look at the MS headers (as Jerker did) and
MS defines it with two underscores 8|
I will add the same issue to pep.em, too. Because I defined it there
false, too.
Post by Pedro Alves
Post by Kai Tietz
For Windows CE and arm-wince __image_base__ is invalid too (the same
thing
Post by Pedro Alves
Post by Kai Tietz
as for _ImageBase).
I know. It hasn't bothered me enough yet to fix it ;)
Post by Kai Tietz
No, for x64 it is underscored, too.
http://lists-archives.org/mingw-users/07903-leading-underscore-or-not.html
I can just tell, that for the x86_64 version underscores are generated for
symbols. And this target has the same base (for this configuration) as the
32-bit version. So IMHO there are underscores.

ChangleLog for /ld/emultempl:

2007-11-02 Kai Tietz <***@onevision.com>

* emultempl/pe.em: (init): Add __ImageBase symbol.
(MSIMAGEBASEOFF): New.
(set_pe_name): Keep __image_base and __ImageBase value synch.
(_set_symbols): Likewise.
* emultempl/pep.em: Likewise.

Is this OK for apply ?

Cheers,
i.A. Kai Tietz



| (\_/) This is Bunny. Copy and paste Bunny
| (='.'=) into your signature to help him gain
| (")_(") world domination.

------------------------------------------------------------------------------------------
OneVision Software Entwicklungs GmbH & Co. KG
Dr.-Leo-Ritter-Straße 9 - 93049 Regensburg
Tel: +49.(0)941.78004.0 - Fax: +49.(0)941.78004.489 - www.OneVision.com
Commerzbank Regensburg - BLZ 750 400 62 - Konto 6011050
Handelsregister: HRA 6744, Amtsgericht Regensburg
KomplementÀrin: OneVision Software Entwicklungs Verwaltungs GmbH
Dr.-Leo-Ritter-Straße 9 – 93049 Regensburg
Handelsregister: HRB 8932, Amtsgericht Regensburg - GeschÀftsfÌhrer:
Ulrike Döhler, Manuela Kluger
Nick Clifton
2007-11-06 12:04:49 UTC
Permalink
Hi Kai,
Post by Kai Tietz
* emultempl/pe.em: (init): Add __ImageBase symbol.
(MSIMAGEBASEOFF): New.
(set_pe_name): Keep __image_base and __ImageBase value synch.
(_set_symbols): Likewise.
* emultempl/pep.em: Likewise.
Approved - please apply.

Cheers
Nick
Kai Tietz
2007-11-06 12:31:10 UTC
Permalink
Hi Nick,
Post by Nick Clifton
Post by Kai Tietz
* emultempl/pe.em: (init): Add __ImageBase symbol.
(MSIMAGEBASEOFF): New.
(set_pe_name): Keep __image_base and __ImageBase value synch.
(_set_symbols): Likewise.
* emultempl/pep.em: Likewise.
Approved - please apply.
Applied.

Cheers,
i.A. Kai Tietz

| (\_/) This is Bunny. Copy and paste Bunny
| (='.'=) into your signature to help him gain
| (")_(") world domination.

------------------------------------------------------------------------------------------
OneVision Software Entwicklungs GmbH & Co. KG
Dr.-Leo-Ritter-Straße 9 - 93049 Regensburg
Tel: +49.(0)941.78004.0 - Fax: +49.(0)941.78004.489 - www.OneVision.com
Commerzbank Regensburg - BLZ 750 400 62 - Konto 6011050
Handelsregister: HRA 6744, Amtsgericht Regensburg
Komplementärin: OneVision Software Entwicklungs Verwaltungs GmbH
Dr.-Leo-Ritter-Straße 9 – 93049 Regensburg
Handelsregister: HRB 8932, Amtsgericht Regensburg - Geschäftsführer:
Ulrike Döhler, Manuela Kl

Kai Tietz
2007-11-06 09:04:57 UTC
Permalink
Hi Jerker,
Post by Jerker Bäck
Post by Kai Tietz
You can find some more information about this symbol, when you take a
look
Post by Jerker Bäck
Post by Kai Tietz
at the 'delayhlp.cpp' file of the MS include folder.
Coincidentally, I'm porting delayhlp.cpp to the Interix single
rooted file system right now. The tricky part is to do a directory
search for the delay loaded DLLs using native NT calls (Interix
cannot use any Windows user libraries, just ntdll.dll).
Are you planning to introduce support for the PE Delay Import
Directory to mingw? That would truly be very interesting.
Hmm..., I didn't thought about this. But may this would be an interesting
feature. How should this be done ? By command-line option or by a
assembler generated linker flag ? The second choice, would provide to gas
the ability to have something equivalent to #pragma comment(lib,...).

Cheers,
i.A. Kai Tietz

| (\_/) This is Bunny. Copy and paste Bunny
| (='.'=) into your signature to help him gain
| (")_(") world domination.

------------------------------------------------------------------------------------------
OneVision Software Entwicklungs GmbH & Co. KG
Dr.-Leo-Ritter-Straße 9 - 93049 Regensburg
Tel: +49.(0)941.78004.0 - Fax: +49.(0)941.78004.489 - www.OneVision.com
Commerzbank Regensburg - BLZ 750 400 62 - Konto 6011050
Handelsregister: HRA 6744, Amtsgericht Regensburg
Komplementärin: OneVision Software Entwicklungs Verwaltungs GmbH
Dr.-Leo-Ritter-Straße 9 – 93049 Regensburg
Handelsregister: HRB 8932, Amtsgericht Regensburg - Geschäftsführer:
Ulrike
Jerker Bäck
2007-11-06 12:12:27 UTC
Permalink
discussion went off list
Loading...