mingw DLL import library compatibility (with trivial patch)

Doug Semler dougsemler@gmail.com
Fri Mar 12 20:14:00 GMT 2010


Well, I figured out the "issue," if you could call it that.  It turns
out that the MS linker doesn't resolve/merge RVAs in the .idata$*
sections unless the reference is in the same COFF object, or (if the
object is in an archive) the archive name is the same (figured this
out by reading the PE-COFF spec very carefully :)).  If I extract the
archive members generated by the binutils ld, and rename them to the
same name and add them to a .lib file, the references are resolved
properly.  In addition, if the (common) name of the archive member
ends in ".dll", the binutils linker is able to use the resulting
import library without change, because it scans the archive for .idata
sections in *.dll archive members and renames them internally so they
are sorted properly.

I created a patch that adds a new parameter to ld
(--mslink-compatible-implib), which would be used in pe-dll.c to
generate a archive member name that is the same across the board in
import libraries.  I'd like to submit it for comment/inclusion, but it
is non trivial so I need to know what I need to do to get the
copyright assignment paperwork done.  It's the parameter addition that
makes the patch non trivial - using the parameter is not really needed
since the linker currently supports the VS style import libraries ---
except that by adding the parameter, you keep the original
functionality of unique archive member names intact.  Adding the same
functionality to dlltool would require a bit more work, since dlltool
writes temporary object files and uses them directly (which means the
method I use now of extracting, rename, and add to new library is the
only really good method of generating the library).

However, I'd like to submit this trivial patch to make the object
files in import libraries more compatible with the current native
toolchains.  It makes the .text section readonly which rids a linker
warning about text sections with different attributes and also adds
the absolute symbol @feat.00 (value of 1) to the X86 jump table
objects which flags the objects as /SAFESEH compatible (which they are
since they are just jumps to an RVA in the import table).

        * pe-dll.c (make_one): Make text section readonly.
        (make_one): Add *ABS* symbol @feat.00 with value 1 to tag
        the jump stub on X86 as /SAFESEH compatible for
        native toolchains.


 ld/pe-dll.c |    7 +++++--
 1 files changed, 5 insertions(+), 2 deletions(-)

diff --git i/ld/pe-dll.c w/ld/pe-dll.c
index 2ddbdc1..3d99666 100644
--- i/ld/pe-dll.c
+++ w/ld/pe-dll.c
@@ -2096,8 +2096,8 @@ make_one (def_file_export *exp, bfd *parent,
bfd_boolean include_jmp_stub)
   bfd_set_arch_mach (abfd, pe_details->bfd_arch, 0);

   symptr = 0;
-  symtab = xmalloc (11 * sizeof (asymbol *));
-  tx  = quick_section (abfd, ".text",    SEC_CODE|SEC_HAS_CONTENTS, 2);
+  symtab = xmalloc (12 * sizeof (asymbol *));
+  tx  = quick_section (abfd, ".text",
SEC_CODE|SEC_HAS_CONTENTS|SEC_READONLY, 2);
   id7 = quick_section (abfd, ".idata$7", SEC_HAS_CONTENTS, 2);
   id5 = quick_section (abfd, ".idata$5", SEC_HAS_CONTENTS, 2);
   id4 = quick_section (abfd, ".idata$4", SEC_HAS_CONTENTS, 2);
@@ -2146,6 +2146,9 @@ make_one (def_file_export *exp, bfd *parent,
bfd_boolean include_jmp_stub)
 #ifdef pe_use_x86_64
          quick_reloc (abfd, 2, BFD_RELOC_32_PCREL, 2);
 #else
+          /* Mark this object as SAFESEH compatible */
+          quick_symbol (abfd, "", "@feat.00", "", bfd_abs_section_ptr,
+                        BSF_LOCAL, 1);
           quick_reloc (abfd, 2, BFD_RELOC_32, 2);
 #endif
          break;



More information about the Binutils mailing list