[PATCH] New --pe-dll-characteristcs switch for PE ld.

Dave Korn dave.korn.cygwin@googlemail.com
Thu Mar 5 12:49:00 GMT 2009


    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

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: pe-dll-characteristics-patch.diff
URL: <https://sourceware.org/pipermail/binutils/attachments/20090305/fca635c5/attachment.ksh>


More information about the Binutils mailing list