[patch] support reading DATA members in ILF format DLL import libraries
Nathaniel Smith
njs@pobox.com
Mon Feb 1 06:25:00 GMT 2016
More information about the Binutils mailing list
Mon Feb 1 06:25:00 GMT 2016
- Previous message (by thread): [committed] Fix file truncated error from ld when outputting to non-regular file on hppa
- Next message (by thread): [patch] support reading DATA members in ILF format DLL import libraries
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
[Also posted here: https://sourceware.org/bugzilla/show_bug.cgi?id=19541, but Kai Tietz suggested I should send to the mailing list] Hi all, On Windows, there are two formats for "import libraries" commonly in use: the format generated by dlltool and commonly used with mingw-w64 toolchains, which is an ar archive of COFF objects (conventionally named like "libpython27.a"), and the format generated and commonly used by MSVC toolchains, which is an ar archive of "ILF" objects (and conventionally named like "python27.lib"). Either way, the purpose of such files is to provide DLL imports. DLL imports come in two main flavors. There are "code" symbols, which refer to functions, and which require (a) a relocation symbol named __imp__foo, and (b) a static symbol named _foo that just acts as a trampoline, jumping to __imp__foo. And then there are "data" symbols, which refer to things like global variables exported from a DLL, and only require a __imp__foo symbol, with no trampoline symbol. In the ILF format these two symbol types are represented identically, except that there is a flag value labeling each import symbol as either IMPORT_CODE or IMPORT_DATA. Unfortunately, binutils's ILF-reading code currently only supports IMPORT_CODE symbols. Any symbols with the IMPORT_DATA flag set are completely skipped over, as if they didn't exist, leading to linker errors. (And rather mysterious ones, because objdump also lies and claims that the symbol really doesn't exist...) One place where this causes problems is when attempting to use binutils to link programs against the python C API, because python27.dll makes extensive use of DATA symbols. But really it would be nice to support these in general, because it should be simple to do and it would be nice if we didn't have to jump through hoops to use import libraries on Windows. In addition, mingw-w64 is looking to transition to a new 'genlib' tool for generating import libraries, and genlib also generates ILF-format libraries. The attached patch provides a simple fix. Right now all the code for emitting import symbol information from ILF files is hidden behind switch { case IMPORT_CODE: ...}; my patch just takes the functionality that should be common between code and data symbols and moves it outside of the switch statement so that it is executed unconditionally. The function-specific trampoline generation code remains inside the switch statement, and if we see anything besides IMPORT_CODE or IMPORT_DATA then we abort(), so this should be safe. I haven't signed FSF paperwork -- not sure whether it's needed for a change this small. (Literally all it does is move some existing lines of code up or down a few lines.) But I can if needed. Let me know if you have any questions or I can help with anything, -n -- Nathaniel J. Smith -- https://vorpus.org -------------- next part -------------- diff --git a/bfd/peicode.h b/bfd/peicode.h index 96cfd65..bd41ef7 100644 --- a/bfd/peicode.h +++ b/bfd/peicode.h @@ -959,13 +959,20 @@ pe_ILF_build_a_bfd (bfd * abfd, pe_ILF_save_relocs (&vars, id5); } + /* Create the main import symbol */ + pe_ILF_make_a_symbol (& vars, "__imp_", symbol_name, id5, 0); + imp_sym = vars.sym_ptr_ptr - 1; + imp_index = vars.sym_index - 1; + /* Create extra sections depending upon the type of import we are dealing with. */ switch (import_type) { int i; case IMPORT_CODE: - /* Create a .text section. + /* Functions (CODE) are special, in that they get a trampoline that + jumps to the main import symbol. + Create a .text section to hold it. First we need to look up its contents in the jump table. */ for (i = NUM_ENTRIES (jtab); i--;) { @@ -986,11 +993,6 @@ pe_ILF_build_a_bfd (bfd * abfd, /* Copy in the jump code. */ memcpy (text->contents, jtab[i].data, jtab[i].size); - /* Create an import symbol. */ - pe_ILF_make_a_symbol (& vars, "__imp_", symbol_name, id5, 0); - imp_sym = vars.sym_ptr_ptr - 1; - imp_index = vars.sym_index - 1; - /* Create a reloc for the data in the text section. */ #ifdef MIPS_ARCH_MAGIC_WINCE if (magic == MIPS_ARCH_MAGIC_WINCE) @@ -1068,14 +1070,6 @@ pe_ILF_build_a_bfd (bfd * abfd, pe_ILF_make_a_symbol (& vars, "", symbol_name, text, BSF_NOT_AT_END | BSF_FUNCTION); - /* Create an import symbol for the DLL, without the - .dll suffix. */ - ptr = (bfd_byte *) strrchr (source_dll, '.'); - if (ptr) - * ptr = 0; - pe_ILF_make_a_symbol (& vars, "__IMPORT_DESCRIPTOR_", source_dll, NULL, 0); - if (ptr) - * ptr = '.'; break; case IMPORT_DATA: @@ -1087,6 +1081,15 @@ pe_ILF_build_a_bfd (bfd * abfd, abort (); } + /* Create an import symbol for the DLL, without the + .dll suffix. */ + ptr = (bfd_byte *) strrchr (source_dll, '.'); + if (ptr) + * ptr = 0; + pe_ILF_make_a_symbol (& vars, "__IMPORT_DESCRIPTOR_", source_dll, NULL, 0); + if (ptr) + * ptr = '.'; + /* Point the bfd at the symbol table. */ obj_symbols (abfd) = vars.sym_cache; bfd_get_symcount (abfd) = vars.sym_index;
- Previous message (by thread): [committed] Fix file truncated error from ld when outputting to non-regular file on hppa
- Next message (by thread): [patch] support reading DATA members in ILF format DLL import libraries
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Binutils mailing list