I understand what __ImageBase
is and how to use it. __ImageBase
is an external object of type IMAGE_DOS_HEADER
, which is a structure representing the DOS header of a Portable Executable image. This variable is defined by the linker during the linking stage. When we retrieve the address of this variable, the address represents the base address of the image that the object file or the static library is linked into.
I also have a basic understanding of the compilation of a C/C++ program, as well as the fundamentals of the PE format and COFF object file format. However, I’m unsure about how the linker allocates the storage for this variable in the PE image in such a way that the __ImageBase
variable lays out at the start of the image.
I’m curious about how it works internally, particularly from the perspective of the linking stage.
I’ve tried to look at the contents of the static library by using the dumpbin
utility. But I couldn’t manage to understand the output.
The source code of the static library I’ve written:
#pragma once
#ifndef MAIN_H
#define MAIN_H
#if __cplusplus
extern "C"
{
#endif
void print_image_base(void);
#if __cplusplus
}
#endif
#endif
#include <stdio.h>
#include <Windows.h>
EXTERN_C IMAGE_DOS_HEADER __ImageBase;
#define HINST_THISCOMPONENT ((HINSTANCE)&__ImageBase)
void print_image_base(void)
{
printf("Image Base: %pn", HINST_THISCOMPONENT);
}
Samet ARTAN is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.