using compile‐time #defines to
indicate whether you are building a DLL, building client code that will link to the DLL, or merely building/linking to a static library. In making the choice between the various methods of resolving the ’direct
address with constant offset’ problem, you should consider typical real‐world usage:
Original:
--foo.h
extern int arr[];
--foo.c
#include "foo.h"
void main(int argc, char **argv){
printf("%d\n",arr[1]);
}
Solution 1:
--foo.h
extern int arr[];
--foo.c
#include "foo.h"
void main(int argc, char **argv){
/* This workaround is for win32 and cygwin; do not "optimize" */
volatile int *parr = arr;
printf("%d\n",parr[1]);
}
Solution 2:
--foo.h
/* Note: auto-export is assumed (no __declspec(dllexport)) */
#if (defined(_WIN32) || defined(__CYGWIN__)) && \
!(defined(FOO_BUILD_DLL) || defined(FOO_STATIC))
#define FOO_IMPORT __declspec(dllimport)
#else
#define FOO_IMPORT
#endif
extern FOO_IMPORT int arr[];
--foo.c
#include "foo.h"
void main(int argc, char **argv){
printf("%d\n",arr[1]);
}
A fourth way to avoid this problem is to re‐code your library to use a functional interface rather than a data interface for the offending variables (e.g. set_foo() and get_foo() accessor functions).
--disable-auto-import
Do not attempt to do sophisticated linking of "_symbol" to "__imp__symbol" for DATA imports from DLLs. [This option is specific to the i386 PE targeted port of the linker]
--enable-runtime-pseudo-reloc
If your code contains expressions described in --enable-auto-import section, that is, DATA imports from DLL with non‐zero offset, this switch will create a vector of ’runtime pseudo relocations’ which can be used
by runtime environment to adjust references to such data in your client code. [This option is specific to the i386 PE targeted port of the linker]
--disable-runtime-pseudo-reloc
Do not create pseudo relocations for non‐zero offset DATA imports from DLLs. [This option is specific to the i386 PE targeted port of the linker]
--enable-extra-pe-debug
Show additional debug info related to auto‐import symbol thunking. [This option is specific to the i386 PE targeted port of the linker]
--section-alignment
Sets the section alignment. Sections in memory will always begin at addresses which are a multiple of this number. Defaults to 0x1000. [This option is specific to the i386 PE targeted port of the linker]
--stack reserve
--stack reserve,commit
Specify the number of bytes of memory to reserve (and optionally commit) to be used as stack for this program. The default is 2MB reserved, 4K committed. [This option is specific to the i386 PE targeted port of
the linker]
--subsystem which
--subsystem which:major
--subsystem which:major.minor
Specifies the subsystem under which your program will execute. The legal values for which are "native", "windows", "console", "posix", and "xbox". You may optionally set the subsystem version also. Numeric
values are also accepted for which. [This option is specific to the i386 PE targeted port of the linker]
The following options set flags in the "DllCharacteristics" field of the PE file header: [These options are