This is the mail archive of the
binutils@sourceware.org
mailing list for the binutils project.
[PATCH] New --pe-dll-characteristcs switch for PE ld.
- From: Dave Korn <dave dot korn dot cygwin at googlemail dot com>
- To: binutils at sourceware dot org
- Date: Thu, 05 Mar 2009 12:58:39 +0000
- Subject: [PATCH] New --pe-dll-characteristcs switch for PE ld.
Hi team,
The PE header in executables contains a field called "DllCharacteristics",
which contains various flags that inform the OS dynamic loader about the
status of the executable w.r.t. runtime factors such as NX/DEP, ASLR, dynamic
rebasing, etc. (Despite the name, it applies to executables of all kinds, not
just DLLs, but that's what MS calls it so that's what I call it).
Until now, BFD and LD have done nothing with this field, and it is zero in
all binutils-generated PE executables to date. However the Cygwin project now
has a need to use these flags to ensure compatibility of our applications on
post-Vista versions of Windows; we've had a number of reports lately of
crashes that can be resolved by applying various of the flags in this bitfield.
The attached patch adds a new linker option to allow setting the value of
this field. As the default for this field is zero, and as there is no other
source for these flags in any of the input BFDs, I haven't provided a
mechanism to clear any of these flags; that would make a suitable follow-on
patch for objcopy, but first things first.
You'll notice I've changed the type of the DllCharacteristics field from
short to unsigned short. This fixes a minor bug that was causing the output
routines in bfd/peXXigen.c#_bfd_XX_print_private_bfd_data_common() (and hence
objdump) to display the tsaware flag as '0xffff8000' owing to sign extension.
There shouldn't be any significant back-compat problems with this but it
would cause a change in output if using objdump to examine an executable that
was compiled with native tools that had set that flag. I think it's worth the
minor pain; apologies in advance to anyone who has to fix a test script. (Not
that we have any in our sources, but somone somewhere might just).
Tested on {i686-pc-cygwin, i686-pc-linux-gnu} x {arm-epoc-pe, arm-wince-pe,
i386-pc-netbsdpe, i386-pc-pe, i586-pc-interix, i586-unknown-beospe,
i686-pc-cygwin, i686-pc-mingw32, mcore-unknown-pe, powerpcle-unknown-pe,
sh-unknown-pe, thumb-epoc-pe, x86_64-pc-freebsd, x86_64-pc-linux-gnu,
x86_64-pc-mingw32}, no regressions and all new tests pass on all PE platforms
tested. Docs, NEWS and testcases included. Ok for HEAD?
include/ChangeLog
* coff/internal.h (struct internal_extra_pe_aouthdr): Correct type
of DllCharacteristics flags field to unsigned.
* coff/pe.h (IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE,
IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE,
IMAGE_DLL_CHARACTERISTICS_NX_COMPAT,
IMAGE_DLLCHARACTERISTICS_NO_ISOLATION,
IMAGE_DLLCHARACTERISTICS_NO_SEH,
IMAGE_DLLCHARACTERISTICS_NO_BIND,
IMAGE_DLLCHARACTERISTICS_WDM_DRIVER,
IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE): New macros define
flag bit values for DllCharacteristics field of PEAOUTHDR, PEPAOUTHDR.
ld/ChangeLog
* NEWS: Mention new '--pe-dll-characteristics' option.
* ld/ld.texinfo: Document new '--pe-dll-characteristics' option.
* emultempl/pe.em (OPTION_PE_DLL_CHARACTERISTICS): New getopts flag.
(gld${EMULATION_NAME}_add_options): Add --pe-dll-characteristics
to array of switches.
(definfoflag): Add new struct definition.
(C, CF): Add new helper macros.
(dllchrctnames[]): Add new array of DllCharacteristics flag names.
(definfo): Add new flagnames member pointing to definfoflag array.
(D): Adjust macro to zero-init new flagnames field.
(DF): Add new macro like D but allows setting flagnames.
(init[]): Add zero-initialised flagnames members where needed, and
new entry for new __dll_characteristics__ PE value.
(gld_${EMULATION_NAME}_list_options): Document new option.
(find_pe_flag_name): New helper to search definfoflag arrays.
(find_pe_name): New helper broken out from set_pe_name.
(set_pe_name): Use it.
(is_flag_sep): New helper function to test separator characters.
(set_pe_value_from_flags): New function parses mixed flagnames and
integers format to set a PE value.
(gld${EMULATION_NAME}_handle_option): Call it to handle new
OPTION_PE_DLL_CHARACTERISTICS option.
* emultempl/pep.em (enum options): Add OPTION_PE_DLL_CHARACTERISTICS.
(gld${EMULATION_NAME}_add_options): Add --pe-dll-characteristics
to array of switches.
(definfoflag): Add new struct definition.
(C, CF): Add new helper macros.
(dllchrctnames[]): Add new array of DllCharacteristics flag names.
(definfo): Add new flagnames member pointing to definfoflag array.
(D): Adjust macro to zero-init new flagnames field.
(DF): Add new macro like D but allows setting flagnames.
(init[]): Add zero-initialised flagnames members where needed, and
new entry for new __dll_characteristics__ PE value.
(gld_${EMULATION_NAME}_list_options): Document new option.
(find_pep_flag_name): New helper to search definfoflag arrays.
(find_pep_name): New helper broken out from set_pe_name.
(set_pep_name): Use it.
(is_flag_sep): New helper function to test separator characters.
(set_pep_value_from_flags): New function parses mixed flagnames and
integers format to set a PE value.
(gld${EMULATION_NAME}_handle_option): Call it to handle new
OPTION_PE_DLL_CHARACTERISTICS option.
ld/testsuite/ChangeLog
* lib/ld-lib.exp (run_dump_test): Allow new 'sed'
option in .d files for post-processing output dump file.
* ld-pe/dllcharact-1.d, ld-pe/dllcharact-2.d, ld-pe/dllcharact-3.d,
ld-pe/dllcharact-4.d, ld-pe/dllcharact-5.d, ld-pe/dllcharact-fail-1.d
ld-pe/dllcharact-fail-2.d, ld-pe/dllcharact-fail-3.d, ld-pe/empty.s,
ld-pe/dllcharact-fail-4.d, ld-pe/dllcharact.exp: New test files.
cheers,
DaveK
Index: include/coff/internal.h
===================================================================
RCS file: /cvs/src/src/include/coff/internal.h,v
retrieving revision 1.22
diff -p -u -r1.22 internal.h
--- include/coff/internal.h 12 Jul 2007 07:16:41 -0000 1.22
+++ include/coff/internal.h 5 Mar 2009 12:30:04 -0000
@@ -185,7 +185,7 @@ struct internal_extra_pe_aouthdr
3 - WINDOWS_CUI runs in Windows char sub. (console app)
5 - OS2_CUI runs in OS/2 character subsystem
7 - POSIX_CUI runs in Posix character subsystem */
- short DllCharacteristics; /* flags for DLL init, use 0 */
+ unsigned short DllCharacteristics; /* flags for DLL init, use 0 */
bfd_vma SizeOfStackReserve; /* amount of memory to reserve */
bfd_vma SizeOfStackCommit; /* amount of memory initially committed for
initial thread's stack, default is 0x1000 */
Index: include/coff/pe.h
===================================================================
RCS file: /cvs/src/src/include/coff/pe.h,v
retrieving revision 1.18
diff -p -u -r1.18 pe.h
--- include/coff/pe.h 4 Nov 2007 23:49:08 -0000 1.18
+++ include/coff/pe.h 5 Mar 2009 12:30:04 -0000
@@ -38,6 +38,17 @@
#define IMAGE_FILE_UP_SYSTEM_ONLY 0x4000
#define IMAGE_FILE_BYTES_REVERSED_HI 0x8000
+/* DllCharacteristics flag bits. The inconsistent naming may seem
+ odd, but that is how they are defined in the PE specification. */
+#define IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE 0x0040
+#define IMAGE_DLL_CHARACTERISTICS_FORCE_INTEGRITY 0x0080
+#define IMAGE_DLL_CHARACTERISTICS_NX_COMPAT 0x0100
+#define IMAGE_DLLCHARACTERISTICS_NO_ISOLATION 0x0200
+#define IMAGE_DLLCHARACTERISTICS_NO_SEH 0x0400
+#define IMAGE_DLLCHARACTERISTICS_NO_BIND 0x0800
+#define IMAGE_DLLCHARACTERISTICS_WDM_DRIVER 0x2000
+#define IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE 0x8000
+
/* Additional flags to be set for section headers to allow the NT loader to
read and write to the section data (to replace the addresses of data in
dlls for one thing); also to execute the section in .text's case. */
Index: ld/NEWS
===================================================================
RCS file: /cvs/src/src/ld/NEWS,v
retrieving revision 1.100
diff -p -u -r1.100 NEWS
--- ld/NEWS 3 Mar 2009 18:22:10 -0000 1.100
+++ ld/NEWS 5 Mar 2009 12:30:04 -0000
@@ -1,5 +1,9 @@
-*- text -*-
+* A new command-line flag '--pe-dll-characteristics' allows PE targets
+ to set the value of the PE file header's DllCharacteristics field,
+ using a flexible combination of numeric and symbolic names.
+
* New script function REGION_ALIAS to add alias names to memory regions.
* PE targets no longer make use of the long section names PE extension to
Index: ld/ld.texinfo
===================================================================
RCS file: /cvs/src/src/ld/ld.texinfo,v
retrieving revision 1.235
diff -p -u -r1.235 ld.texinfo
--- ld/ld.texinfo 3 Mar 2009 18:22:10 -0000 1.235
+++ ld/ld.texinfo 5 Mar 2009 12:30:06 -0000
@@ -2257,6 +2257,57 @@ Sets the minor number of the ``os versio
Sets the minor number of the ``subsystem version''. Defaults to 0.
[This option is specific to the i386 PE targeted port of the linker]
+@kindex --pe-dll-characteristics
+@item --pe-dll-characteristics (@var{name}|@var{integer})@*@ @w{@ }@ @w{@ }@ @w{@ }@ @w{@ }[(',' | '|' | '+' | ':')(@var{name}|@var{integer})[...]]
+Sets the value of the DllCharacteristics field in the PE header. The
+value can be specified by using hexadecimal or decimal integers, or the
+symbolic names of the various bits, optionally combined by any combination
+of @samp{|}, @samp{+}, @samp{:} or @samp{,} characters. The following
+short symbolic names are recognized, shown with the full name of the flag
+(as given in the MS PE specification) to which they correspond, which is
+also accepted:
+
+@table @samp
+@item dynbase
+@samp{IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE}
+
+@item forceinteg
+@samp{IMAGE_DLL_CHARACTERISTICS_FORCE_INTEGRITY}
+
+@item nxcompat
+@samp{IMAGE_DLL_CHARACTERISTICS_NX_COMPAT}
+
+@item noisolation
+@samp{IMAGE_DLLCHARACTERISTICS_NO_ISOLATION}
+
+@item noseh
+@samp{IMAGE_DLLCHARACTERISTICS_NO_SEH}
+
+@item nobind
+@samp{IMAGE_DLLCHARACTERISTICS_NO_BIND}
+
+@item wdmdriver
+@samp{IMAGE_DLLCHARACTERISTICS_WDM_DRIVER}
+
+@item tsaware
+@samp{IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE}
+
+@end table
+These options and separators can be combined with almost total
+flexibility; for example, valid uses of this option include:
+
+@smallexample
+--pe-dll-characteristics=0x0400|0x0100
+@end smallexample
+@smallexample
+--pe-dll-characteristics=1+128+1024,noseh,nobind
+@end smallexample
+@smallexample
+--pe-dll-characteristics noseh:nobind:tsaware
+@end smallexample
+
+[This option is valid for all PE targeted ports of the linker]
+
@cindex DEF files, creating
@cindex DLLs, creating
@kindex --output-def
Index: ld/emultempl/pe.em
===================================================================
RCS file: /cvs/src/src/ld/emultempl/pe.em,v
retrieving revision 1.146
diff -p -u -r1.146 pe.em
--- ld/emultempl/pe.em 3 Mar 2009 18:22:11 -0000 1.146
+++ ld/emultempl/pe.em 5 Mar 2009 12:30:06 -0000
@@ -229,6 +229,8 @@ fragment <<EOF
(OPTION_USE_NUL_PREFIXED_IMPORT_TABLES + 1)
#define OPTION_DISABLE_LONG_SECTION_NAMES \
(OPTION_ENABLE_LONG_SECTION_NAMES + 1)
+#define OPTION_PE_DLL_CHARACTERISTICS \
+ (OPTION_DISABLE_LONG_SECTION_NAMES + 1)
static void
gld${EMULATION_NAME}_add_options
@@ -290,6 +292,7 @@ gld${EMULATION_NAME}_add_options
{"large-address-aware", no_argument, NULL, OPTION_LARGE_ADDRESS_AWARE},
{"enable-long-section-names", no_argument, NULL, OPTION_ENABLE_LONG_SECTION_NAMES},
{"disable-long-section-names", no_argument, NULL, OPTION_DISABLE_LONG_SECTION_NAMES},
+ {"pe-dll-characteristics", required_argument, NULL, OPTION_PE_DLL_CHARACTERISTICS},
{NULL, no_argument, NULL, 0}
};
@@ -303,14 +306,47 @@ gld${EMULATION_NAME}_add_options
typedef struct
{
+ const char *name;
+ int len;
+ int value;
+} definfoflag;
+
+#define C(name) { #name, sizeof(#name) - 1, name }
+#define CF(name,flag) { #name, sizeof(#name) - 1, flag }
+static const definfoflag dllchrctnames[] =
+{
+ /* Accept a few handy abbreviations. */
+ CF(dynbase, IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE),
+ CF(forceinteg, IMAGE_DLL_CHARACTERISTICS_FORCE_INTEGRITY),
+ CF(nxcompat, IMAGE_DLL_CHARACTERISTICS_NX_COMPAT),
+ CF(noisolation, IMAGE_DLLCHARACTERISTICS_NO_ISOLATION),
+ CF(noseh, IMAGE_DLLCHARACTERISTICS_NO_SEH),
+ CF(nobind, IMAGE_DLLCHARACTERISTICS_NO_BIND),
+ CF(wdmdriver, IMAGE_DLLCHARACTERISTICS_WDM_DRIVER),
+ CF(tsaware, IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE),
+ /* And the full names as defined in the PE specification. */
+ C(IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE),
+ C(IMAGE_DLL_CHARACTERISTICS_FORCE_INTEGRITY),
+ C(IMAGE_DLL_CHARACTERISTICS_NX_COMPAT),
+ C(IMAGE_DLLCHARACTERISTICS_NO_ISOLATION),
+ C(IMAGE_DLLCHARACTERISTICS_NO_SEH),
+ C(IMAGE_DLLCHARACTERISTICS_WDM_DRIVER),
+ C(IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE),
+ { 0, 0, 0 },
+};
+
+typedef struct
+{
void *ptr;
int size;
int value;
char *symbol;
int inited;
+ const definfoflag *flagnames;
} definfo;
-#define D(field,symbol,def) {&pe.field,sizeof(pe.field), def, symbol,0}
+#define D(field,symbol,def) {&pe.field,sizeof(pe.field), def, symbol, 0, 0}
+#define DF(field,symbol,def,flags) {&pe.field,sizeof(pe.field), def, symbol, 0, flags}
static definfo init[] =
{
@@ -318,7 +354,7 @@ static definfo init[] =
#define IMAGEBASEOFF 0
D(ImageBase,"__image_base__", NT_EXE_IMAGE_BASE),
#define DLLOFF 1
- {&dll, sizeof(dll), 0, "__dll__", 0},
+ {&dll, sizeof(dll), 0, "__dll__", 0, 0},
#define MSIMAGEBASEOFF 2
D(ImageBase, U ("__ImageBase"), NT_EXE_IMAGE_BASE),
D(SectionAlignment,"__section_alignment__", PE_DEF_SECTION_ALIGNMENT),
@@ -339,10 +375,10 @@ static definfo init[] =
D(SizeOfHeapReserve,"__size_of_heap_reserve__", 0x100000),
D(SizeOfHeapCommit,"__size_of_heap_commit__", 0x1000),
D(LoaderFlags,"__loader_flags__", 0x0),
- { NULL, 0, 0, NULL, 0 }
+ DF(DllCharacteristics, "__dll_characteristics__", 0x0, dllchrctnames),
+ { NULL, 0, 0, NULL, 0, 0 }
};
-
static void
gld_${EMULATION_NAME}_list_options (FILE *file)
{
@@ -401,29 +437,48 @@ gld_${EMULATION_NAME}_list_options (FILE
executable image files\n"));
fprintf (file, _(" --disable-long-section-names Never use long COFF section names, even\n\
in object files\n"));
+ fprintf (file, _(" --pe-dll-characteristics <name|int>[<,|+:><name|int>[...]]\n\
+ Sets PE header DllCharacteristics field.\n"));
}
+static const definfoflag *
+find_pe_flag_name (char *name, const definfoflag *flagnames)
+{
+ int i;
-static void
-set_pe_name (char *name, long val)
+ /* Look for the name, return pointer to definfoflag if found. */
+ if (flagnames)
+ for (i = 0; flagnames[i].name; i++)
+ if (strncmp (name, flagnames[i].name, flagnames[i].len) == 0)
+ return &flagnames[i];
+ /* Unknown name could be an integer, so not an error here. */
+ return 0;
+}
+
+static definfo *
+find_pe_name (char *name)
{
int i;
- /* Find the name and set it. */
+ /* Look for the name, return pointer to definfo. */
for (i = 0; init[i].ptr; i++)
- {
- if (strcmp (name, init[i].symbol) == 0)
- {
- init[i].value = val;
- init[i].inited = 1;
- if (strcmp (name,"__image_base__") == 0)
- set_pe_name (U ("__ImageBase"), val);
- return;
- }
- }
+ if (strcmp (name, init[i].symbol) == 0)
+ return &init[i];
+ /* Unknown name is a serious internal coding error, so don't
+ bother to diagnose or return error indication, just bail. */
abort ();
}
+static void
+set_pe_name (char *name, long val)
+{
+ /* Find the name and set it. */
+ definfo *init = find_pe_name (name);
+ init->value = val;
+ init->inited = 1;
+ if (strcmp (name,"__image_base__") == 0)
+ set_pe_name (U ("__ImageBase"), val);
+}
static void
set_pe_subsystem (void)
@@ -543,6 +598,62 @@ set_pe_value (char *name)
optarg = end;
}
+static char
+is_flag_sep (char x)
+{
+ return x == '+' || x == '|' || x == ':' || x == ',';
+}
+
+static void
+set_pe_value_from_flags (char *name)
+{
+ long flags = 0;
+ definfo *init;
+
+ /* Look up the symbolic flag names. Even if there aren't any we
+ will still parse multiple integers combined by separators. */
+ init = find_pe_name (name);
+
+ /* Parse the flags out of optarg. We accept any combination of
+ symbolic abbreviations and strtoul-parseable integers, separated
+ by any combination of '+', '|', ':' and ',' characters. */
+ while (*optarg)
+ {
+ const definfoflag *flag;
+
+ /* Deliberately allow multiple conjoined separators. */
+ while (is_flag_sep (*optarg))
+ optarg++;
+
+ /* Even trailing at the end. */
+ if (!*optarg)
+ break;
+
+ flag = find_pe_flag_name (optarg, init->flagnames);
+ if (flag)
+ {
+ flags |= flag->value;
+ optarg += flag->len;
+ }
+ else
+ {
+ char *end;
+ long value;
+ value = strtoul (optarg, &end, 0);
+ if (end == optarg)
+ einfo (_("%P%F: unrecognised integer/flag '%s' for PE parameter '%s'\n"),
+ optarg, name);
+ flags |= value;
+ optarg = end;
+ }
+ /* If there's any more, we do insist on at least one separator. */
+ if (*optarg && !is_flag_sep (*optarg))
+ einfo (_("%P%F: unparseable at '%s' for PE parameter '%s'\n"),
+ optarg, name);
+ }
+
+ set_pe_name (name, flags);
+}
static void
set_pe_stack_heap (char *resname, char *comname)
@@ -707,6 +818,9 @@ gld${EMULATION_NAME}_handle_option (int
case OPTION_DISABLE_LONG_SECTION_NAMES:
pe_use_coff_long_section_names = 0;
break;
+ case OPTION_PE_DLL_CHARACTERISTICS:
+ set_pe_value_from_flags ("__dll_characteristics__");
+ break;
}
return TRUE;
}
Index: ld/emultempl/pep.em
===================================================================
RCS file: /cvs/src/src/ld/emultempl/pep.em,v
retrieving revision 1.23
diff -p -u -r1.23 pep.em
--- ld/emultempl/pep.em 3 Mar 2009 18:22:11 -0000 1.23
+++ ld/emultempl/pep.em 5 Mar 2009 12:30:07 -0000
@@ -179,7 +179,8 @@ enum options
OPTION_EXCLUDE_MODULES_FOR_IMPLIB,
OPTION_USE_NUL_PREFIXED_IMPORT_TABLES,
OPTION_ENABLE_LONG_SECTION_NAMES,
- OPTION_DISABLE_LONG_SECTION_NAMES
+ OPTION_DISABLE_LONG_SECTION_NAMES,
+ OPTION_PE_DLL_CHARACTERISTICS
};
static void
@@ -244,6 +245,7 @@ gld${EMULATION_NAME}_add_options
#endif
{"enable-long-section-names", no_argument, NULL, OPTION_ENABLE_LONG_SECTION_NAMES},
{"disable-long-section-names", no_argument, NULL, OPTION_DISABLE_LONG_SECTION_NAMES},
+ {"pe-dll-characteristics", required_argument, NULL, OPTION_PE_DLL_CHARACTERISTICS},
{NULL, no_argument, NULL, 0}
};
@@ -256,14 +258,47 @@ gld${EMULATION_NAME}_add_options
typedef struct
{
+ const char *name;
+ int len;
+ int value;
+} definfoflag;
+
+#define C(name) { #name, sizeof(#name) - 1, name }
+#define CF(name,flag) { #name, sizeof(#name) - 1, flag }
+static const definfoflag dllchrctnames[] =
+{
+ /* Accept a few handy abbreviations. */
+ CF(dynbase, IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE),
+ CF(forceinteg, IMAGE_DLL_CHARACTERISTICS_FORCE_INTEGRITY),
+ CF(nxcompat, IMAGE_DLL_CHARACTERISTICS_NX_COMPAT),
+ CF(noisolation, IMAGE_DLLCHARACTERISTICS_NO_ISOLATION),
+ CF(noseh, IMAGE_DLLCHARACTERISTICS_NO_SEH),
+ CF(nobind, IMAGE_DLLCHARACTERISTICS_NO_BIND),
+ CF(wdmdriver, IMAGE_DLLCHARACTERISTICS_WDM_DRIVER),
+ CF(tsaware, IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE),
+ /* And the full names as defined in the PE specification. */
+ C(IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE),
+ C(IMAGE_DLL_CHARACTERISTICS_FORCE_INTEGRITY),
+ C(IMAGE_DLL_CHARACTERISTICS_NX_COMPAT),
+ C(IMAGE_DLLCHARACTERISTICS_NO_ISOLATION),
+ C(IMAGE_DLLCHARACTERISTICS_NO_SEH),
+ C(IMAGE_DLLCHARACTERISTICS_WDM_DRIVER),
+ C(IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE),
+ { 0, 0, 0 },
+};
+
+typedef struct
+{
void *ptr;
int size;
bfd_vma value;
char *symbol;
int inited;
+ const definfoflag *flagnames;
} definfo;
-#define D(field,symbol,def) {&pep.field,sizeof(pep.field), def, symbol,0}
+#define D(field,symbol,def) {&pep.field,sizeof(pep.field), def, symbol, 0, 0}
+#define DF(field,symbol,def,flags) {&pep.field,sizeof(pep.field), def, symbol, 0, flags}
static definfo init[] =
{
@@ -271,7 +306,7 @@ static definfo init[] =
#define IMAGEBASEOFF 0
D(ImageBase,"__image_base__", NT_EXE_IMAGE_BASE),
#define DLLOFF 1
- {&dll, sizeof(dll), 0, "__dll__", 0},
+ {&dll, sizeof(dll), 0, "__dll__", 0, 0},
#define MSIMAGEBASEOFF 2
D(ImageBase,"___ImageBase", NT_EXE_IMAGE_BASE),
D(SectionAlignment,"__section_alignment__", PE_DEF_SECTION_ALIGNMENT),
@@ -288,7 +323,8 @@ static definfo init[] =
D(SizeOfHeapReserve,"__size_of_heap_reserve__", 0x100000),
D(SizeOfHeapCommit,"__size_of_heap_commit__", 0x1000),
D(LoaderFlags,"__loader_flags__", 0x0),
- { NULL, 0, 0, NULL, 0 }
+ DF(DllCharacteristics, "__dll_characteristics__", 0x0, dllchrctnames),
+ { NULL, 0, 0, NULL, 0, 0 }
};
@@ -346,30 +382,49 @@ gld_${EMULATION_NAME}_list_options (FILE
executable image files\n"));
fprintf (file, _(" --disable-long-section-names Never use long COFF section names, even\n\
in object files\n"));
+ fprintf (file, _(" --pe-dll-characteristics <name|int>[<,|+:><name|int>[...]]\n\
+ Sets PE header DllCharacteristics field.\n"));
#endif
}
+static const definfoflag *
+find_pep_flag_name (char *name, const definfoflag *flagnames)
+{
+ int i;
-static void
-set_pep_name (char *name, bfd_vma val)
+ /* Look for the name, return pointer to definfoflag if found. */
+ if (flagnames)
+ for (i = 0; flagnames[i].name; i++)
+ if (strncmp (name, flagnames[i].name, flagnames[i].len) == 0)
+ return &flagnames[i];
+ /* Unknown name could be an integer, so not an error here. */
+ return 0;
+}
+
+static definfo *
+find_pep_name (char *name)
{
int i;
- /* Find the name and set it. */
+ /* Look for the name, return pointer to definfo. */
for (i = 0; init[i].ptr; i++)
- {
- if (strcmp (name, init[i].symbol) == 0)
- {
- init[i].value = val;
- init[i].inited = 1;
- if (strcmp (name,"__image_base__") == 0)
- set_pep_name ("___ImageBase", val);
- return;
- }
- }
+ if (strcmp (name, init[i].symbol) == 0)
+ return &init[i];
+ /* Unknown name is a serious internal coding error, so don't
+ bother to diagnose or return error indication, just bail. */
abort ();
}
+static void
+set_pep_name (char *name, long val)
+{
+ /* Find the name and set it. */
+ definfo *init = find_pep_name (name);
+ init->value = val;
+ init->inited = 1;
+ if (strcmp (name,"__image_base__") == 0)
+ set_pep_name ("___ImageBase", val);
+}
static void
set_pep_subsystem (void)
@@ -489,6 +544,62 @@ set_pep_value (char *name)
optarg = end;
}
+static char
+is_flag_sep (char x)
+{
+ return x == '+' || x == '|' || x == ':' || x == ',';
+}
+
+static void
+set_pep_value_from_flags (char *name)
+{
+ long flags = 0;
+ definfo *init;
+
+ /* Look up the symbolic flag names. Even if there aren't any we
+ will still parse multiple integers combined by separators. */
+ init = find_pep_name (name);
+
+ /* Parse the flags out of optarg. We accept any combination of
+ symbolic abbreviations and strtoul-parseable integers, separated
+ by any combination of '+', '|', ':' and ',' characters. */
+ while (*optarg)
+ {
+ const definfoflag *flag;
+
+ /* Deliberately allow multiple conjoined separators. */
+ while (is_flag_sep (*optarg))
+ optarg++;
+
+ /* Even trailing at the end. */
+ if (!*optarg)
+ break;
+
+ flag = find_pep_flag_name (optarg, init->flagnames);
+ if (flag)
+ {
+ flags |= flag->value;
+ optarg += flag->len;
+ }
+ else
+ {
+ char *end;
+ long value;
+ value = strtoul (optarg, &end, 0);
+ if (end == optarg)
+ einfo (_("%P%F: unrecognised integer/flag '%s' for PE parameter '%s'\n"),
+ optarg, name);
+ flags |= value;
+ optarg = end;
+ }
+ /* If there's any more, we do insist on at least one separator. */
+ if (*optarg && !is_flag_sep (*optarg))
+ einfo (_("%P%F: unparseable at '%s' for PE parameter '%s'\n"),
+ optarg, name);
+ }
+
+ set_pep_name (name, flags);
+}
static void
set_pep_stack_heap (char *resname, char *comname)
@@ -647,6 +758,9 @@ gld${EMULATION_NAME}_handle_option (int
case OPTION_DISABLE_LONG_SECTION_NAMES:
pep_use_coff_long_section_names = 0;
break;
+ case OPTION_PE_DLL_CHARACTERISTICS:
+ set_pep_value_from_flags ("__dll_characteristics__");
+ break;
}
return TRUE;
}
Index: ld/testsuite/ld-pe/dllcharact-1.d
===================================================================
RCS file: ld/testsuite/ld-pe/dllcharact-1.d
diff -N ld/testsuite/ld-pe/dllcharact-1.d
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ ld/testsuite/ld-pe/dllcharact-1.d 5 Mar 2009 12:30:07 -0000
@@ -0,0 +1,7 @@
+#name: pe-dll-characteristics-1
+#ld: --pe-dll-characteristics=1+128+512:nxcompat\\|wdmdriver\\|forceinteg
+#objdump: -p
+#sed: -e '/^DllCharacteristics/!d'
+#source: empty.s
+
+DllCharacteristics[ \t]*00002381
Index: ld/testsuite/ld-pe/dllcharact-2.d
===================================================================
RCS file: ld/testsuite/ld-pe/dllcharact-2.d
diff -N ld/testsuite/ld-pe/dllcharact-2.d
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ ld/testsuite/ld-pe/dllcharact-2.d 5 Mar 2009 12:30:07 -0000
@@ -0,0 +1,7 @@
+#name: pe-dll-characteristics-2
+#ld: --pe-dll-characteristics=0x1234
+#objdump: -p
+#sed: -e '/^DllCharacteristics/!d'
+#source: empty.s
+
+DllCharacteristics[ \t]*00001234
Index: ld/testsuite/ld-pe/dllcharact-3.d
===================================================================
RCS file: ld/testsuite/ld-pe/dllcharact-3.d
diff -N ld/testsuite/ld-pe/dllcharact-3.d
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ ld/testsuite/ld-pe/dllcharact-3.d 5 Mar 2009 12:30:07 -0000
@@ -0,0 +1,7 @@
+#name: pe-dll-characteristics-3
+#ld: --pe-dll-characteristics noseh:nobind:tsaware
+#objdump: -p
+#sed: -e '/^DllCharacteristics/!d'
+#source: empty.s
+
+DllCharacteristics[ \t]*00008c00
Index: ld/testsuite/ld-pe/dllcharact-4.d
===================================================================
RCS file: ld/testsuite/ld-pe/dllcharact-4.d
diff -N ld/testsuite/ld-pe/dllcharact-4.d
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ ld/testsuite/ld-pe/dllcharact-4.d 5 Mar 2009 12:30:07 -0000
@@ -0,0 +1,7 @@
+#name: pe-dll-characteristics-4
+#ld: --pe-dll-characteristics=0x0400\\|0x0100
+#objdump: -p
+#sed: -e '/^DllCharacteristics/!d'
+#source: empty.s
+
+DllCharacteristics[ \t]*00000500
Index: ld/testsuite/ld-pe/dllcharact-5.d
===================================================================
RCS file: ld/testsuite/ld-pe/dllcharact-5.d
diff -N ld/testsuite/ld-pe/dllcharact-5.d
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ ld/testsuite/ld-pe/dllcharact-5.d 5 Mar 2009 12:30:07 -0000
@@ -0,0 +1,7 @@
+#name: pe-dll-characteristics-5
+#ld: --pe-dll-characteristics=1+128+1024,noseh,nobind
+#objdump: -p
+#sed: -e '/^DllCharacteristics/!d'
+#source: empty.s
+
+DllCharacteristics[ \t]*00000c81
Index: ld/testsuite/ld-pe/dllcharact-fail-1.d
===================================================================
RCS file: ld/testsuite/ld-pe/dllcharact-fail-1.d
diff -N ld/testsuite/ld-pe/dllcharact-fail-1.d
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ ld/testsuite/ld-pe/dllcharact-fail-1.d 5 Mar 2009 12:30:07 -0000
@@ -0,0 +1,4 @@
+#name: pe-dll-characteristics-fail-1
+#ld: --pe-dll-characteristics=1+0xf00:+badname+
+#error: .*unrecognised integer/flag.*
+#source: empty.s
Index: ld/testsuite/ld-pe/dllcharact-fail-2.d
===================================================================
RCS file: ld/testsuite/ld-pe/dllcharact-fail-2.d
diff -N ld/testsuite/ld-pe/dllcharact-fail-2.d
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ ld/testsuite/ld-pe/dllcharact-fail-2.d 5 Mar 2009 12:30:07 -0000
@@ -0,0 +1,9 @@
+#name: pe-dll-characteristics-fail-2
+#ld: --pe-dll-characteristics=0xGoWrong:noseh
+#error: .*(unrecognised integer/flag '0xGoWrong:noseh'|unparseable at 'xGoWrong:noseh').*
+#source: empty.s
+
+## This one was interesting. Different libc strtoul implementations appear to
+## behave differently: newlib regards '0x' as a hex prefix followed by an invalid
+## hex number, GNU libc parses it as an integer zero followed by an 'x'. This
+## could well be a bug in newlib.
Index: ld/testsuite/ld-pe/dllcharact-fail-3.d
===================================================================
RCS file: ld/testsuite/ld-pe/dllcharact-fail-3.d
diff -N ld/testsuite/ld-pe/dllcharact-fail-3.d
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ ld/testsuite/ld-pe/dllcharact-fail-3.d 5 Mar 2009 12:30:07 -0000
@@ -0,0 +1,4 @@
+#name: pe-dll-characteristics-fail-3
+#ld: --pe-dll-characteristics 0x3DFG
+#error: .*unparseable at.*
+#source: empty.s
Index: ld/testsuite/ld-pe/dllcharact-fail-4.d
===================================================================
RCS file: ld/testsuite/ld-pe/dllcharact-fail-4.d
diff -N ld/testsuite/ld-pe/dllcharact-fail-4.d
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ ld/testsuite/ld-pe/dllcharact-fail-4.d 5 Mar 2009 12:30:07 -0000
@@ -0,0 +1,4 @@
+#name: pe-dll-characteristics-fail-4
+#ld: --pe-dll-characteristics 1+noseh+0x3DFnobind
+#error: .*unparseable at.*
+#source: empty.s
Index: ld/testsuite/ld-pe/dllcharact.exp
===================================================================
RCS file: ld/testsuite/ld-pe/dllcharact.exp
diff -N ld/testsuite/ld-pe/dllcharact.exp
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ ld/testsuite/ld-pe/dllcharact.exp 5 Mar 2009 12:30:07 -0000
@@ -0,0 +1,43 @@
+# Expect script for --pe-dll-characteristics command-line option tests.
+# Copyright 2009
+# Free Software Foundation, Inc.
+#
+# This file is part of the GNU Binutils.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
+# MA 02110-1301, USA.
+#
+
+# This test can only be run on PE/COFF platforms.
+if {![is_pecoff_format]} {
+ return
+}
+
+# Now we repeatedly re-link it with different args,
+# and check that the field gets set correctly.
+
+run_dump_test "dllcharact-1"
+run_dump_test "dllcharact-2"
+run_dump_test "dllcharact-3"
+run_dump_test "dllcharact-4"
+run_dump_test "dllcharact-5"
+
+run_dump_test "dllcharact-fail-1"
+run_dump_test "dllcharact-fail-2"
+run_dump_test "dllcharact-fail-3"
+run_dump_test "dllcharact-fail-4"
+
+
+
Index: ld/testsuite/ld-pe/empty.s
===================================================================
RCS file: ld/testsuite/ld-pe/empty.s
diff -N ld/testsuite/ld-pe/empty.s
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ ld/testsuite/ld-pe/empty.s 5 Mar 2009 12:30:07 -0000
@@ -0,0 +1,7 @@
+ .text
+ .global _start
+ .global _mainCRTStartup
+_start:
+_mainCRTStartup:
+ .end
+
Index: ld/testsuite/lib/ld-lib.exp
===================================================================
RCS file: /cvs/src/src/ld/testsuite/lib/ld-lib.exp,v
retrieving revision 1.59
diff -p -u -r1.59 ld-lib.exp
--- ld/testsuite/lib/ld-lib.exp 6 Feb 2009 16:56:12 -0000 1.59
+++ ld/testsuite/lib/ld-lib.exp 5 Mar 2009 12:30:07 -0000
@@ -582,11 +582,17 @@ proc simple_diff { file_1 file_2 } {
# objdump: FLAGS
# nm: FLAGS
# objcopy: FLAGS
+# readelf: FLAGS
# Use the specified program to analyze the assembler or linker
# output file, and pass it FLAGS, in addition to the output name.
# Note that they are run with LC_ALL=C in the environment to give
# consistent sorting of symbols.
#
+# sed: FLAGS
+# Run SED with the specified flags on the analyzer output dump
+# file to perform custom post-processing, before attempting to
+# match it against the pattern lines.
+#
# source: SOURCE [FLAGS]
# Assemble the file SOURCE.s using the flags in the "as" directive
# and the (optional) FLAGS. If omitted, the source defaults to
@@ -661,6 +667,7 @@ proc run_dump_test { name } {
set opts(nm) {}
set opts(objcopy) {}
set opts(readelf) {}
+ set opts(sed) {}
set opts(name) {}
set opts(PROG) {}
set opts(source) {}
@@ -917,20 +924,31 @@ proc run_dump_test { name } {
if { $progopts1 == "" } { set $progopts1 "-r" }
verbose "running $binary $progopts $progopts1" 3
- # Objcopy, unlike the other two, won't send its output to stdout,
- # so we have to run it specially.
- set cmd "$binary $progopts $progopts1 $objfile > $dumpfile"
- if { $program == "objcopy" } {
- set cmd "$binary $progopts $progopts1 $objfile $dumpfile"
+ # Must be ready to post-process the dumpfile, if need be. Objcopy,
+ # unlike the other two, won't send its output to stdout, so we have
+ # to run it specially.
+ if { $opts(sed) != "" } {
+ set cmdoutput "| sed $opts(sed) > $dumpfile"
+ if { $program == "objcopy" } {
+ set cmdoutput "$dumpfile.1 && sed $opts(sed) < $dumpfile.1 > $dumpfile"
+ }
+ } else {
+ set cmdoutput "> $dumpfile"
+ if { $program == "objcopy" } {
+ set cmdoutput "$dumpfile"
+ }
}
+ # Generate the main command line, minus redirects.
+ set cmd "$binary $progopts $progopts1 $objfile"
+
# Ensure consistent sorting of symbols
if {[info exists env(LC_ALL)]} {
set old_lc_all $env(LC_ALL)
}
set env(LC_ALL) "C"
send_log "$cmd\n"
- set cmdret [remote_exec host [concat sh -c [list "$cmd 2>ld.tmp"]] "" "/dev/null"]
+ set cmdret [remote_exec host [concat sh -c [list "$cmd 2>ld.tmp $cmdoutput"]] "" "/dev/null"]
remote_upload host "ld.tmp"
set comp_output [prune_warnings [file_contents "ld.tmp"]]
remote_file host delete "ld.tmp"