Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* Routines to help build PEI-format DLLs (Win32 etc) |
948f9114 | 2 | Copyright 1998, 1999, 2000, 2001 Free Software Foundation, Inc. |
252b5132 RH |
3 | Written by DJ Delorie <dj@cygnus.com> |
4 | ||
5 | This file is part of GLD, the Gnu Linker. | |
6 | ||
7 | GLD is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2, or (at your option) | |
10 | any later version. | |
11 | ||
12 | GLD is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with GLD; see the file COPYING. If not, write to the Free | |
19 | Software Foundation, 59 Temple Place - Suite 330, Boston, MA | |
20 | 02111-1307, USA. */ | |
21 | ||
22 | #include "bfd.h" | |
23 | #include "sysdep.h" | |
24 | #include "bfdlink.h" | |
25 | #include "libiberty.h" | |
26 | ||
27 | #include <time.h> | |
28 | #include <ctype.h> | |
29 | ||
30 | #include "ld.h" | |
31 | #include "ldexp.h" | |
32 | #include "ldlang.h" | |
33 | #include "ldwrite.h" | |
34 | #include "ldmisc.h" | |
35 | #include "ldgram.h" | |
36 | #include "ldmain.h" | |
b71e2778 | 37 | #include "ldfile.h" |
252b5132 RH |
38 | #include "ldemul.h" |
39 | #include "coff/internal.h" | |
40 | #include "../bfd/libcoff.h" | |
41 | #include "deffile.h" | |
1069dd8d | 42 | #include "pe-dll.h" |
252b5132 RH |
43 | |
44 | /************************************************************************ | |
45 | ||
46 | This file turns a regular Windows PE image into a DLL. Because of | |
47 | the complexity of this operation, it has been broken down into a | |
48 | number of separate modules which are all called by the main function | |
49 | at the end of this file. This function is not re-entrant and is | |
50 | normally only called once, so static variables are used to reduce | |
51 | the number of parameters and return values required. | |
52 | ||
53 | See also: ld/emultempl/pe.em | |
54 | ||
55 | ************************************************************************/ | |
56 | ||
b044cda1 CW |
57 | /************************************************************************ |
58 | ||
59 | Auto-import feature by Paul Sokolovsky | |
60 | ||
61 | Quick facts: | |
62 | ||
63 | 1. With this feature on, DLL clients can import variables from DLL | |
64 | without any concern from their side (for example, without any source | |
65 | code modifications). | |
66 | ||
67 | 2. This is done completely in bounds of the PE specification (to be fair, | |
68 | there's a place where it pokes nose out of, but in practise it works). | |
69 | So, resulting module can be used with any other PE compiler/linker. | |
70 | ||
71 | 3. Auto-import is fully compatible with standard import method and they | |
72 | can be mixed together. | |
73 | ||
74 | 4. Overheads: space: 8 bytes per imported symbol, plus 20 for each | |
75 | reference to it; load time: negligible; virtual/physical memory: should be | |
76 | less than effect of DLL relocation, and I sincerely hope it doesn't affect | |
77 | DLL sharability (too much). | |
78 | ||
79 | Idea | |
80 | ||
81 | The obvious and only way to get rid of dllimport insanity is to make client | |
82 | access variable directly in the DLL, bypassing extra dereference. I.e., | |
83 | whenever client contains someting like | |
84 | ||
85 | mov dll_var,%eax, | |
86 | ||
87 | address of dll_var in the command should be relocated to point into loaded | |
88 | DLL. The aim is to make OS loader do so, and than make ld help with that. | |
89 | Import section of PE made following way: there's a vector of structures | |
90 | each describing imports from particular DLL. Each such structure points | |
91 | to two other parellel vectors: one holding imported names, and one which | |
92 | will hold address of corresponding imported name. So, the solution is | |
93 | de-vectorize these structures, making import locations be sparse and | |
94 | pointing directly into code. Before continuing, it is worth a note that, | |
95 | while authors strives to make PE act ELF-like, there're some other people | |
96 | make ELF act PE-like: elfvector, ;-) . | |
97 | ||
98 | Implementation | |
99 | ||
100 | For each reference of data symbol to be imported from DLL (to set of which | |
101 | belong symbols with name <sym>, if __imp_<sym> is found in implib), the | |
102 | import fixup entry is generated. That entry is of type | |
103 | IMAGE_IMPORT_DESCRIPTOR and stored in .idata$3 subsection. Each | |
104 | fixup entry contains pointer to symbol's address within .text section | |
105 | (marked with __fuN_<sym> symbol, where N is integer), pointer to DLL name | |
106 | (so, DLL name is referenced by multiple entries), and pointer to symbol | |
107 | name thunk. Symbol name thunk is singleton vector (__nm_th_<symbol>) | |
108 | pointing to IMAGE_IMPORT_BY_NAME structure (__nm_<symbol>) directly | |
109 | containing imported name. Here comes that "om the edge" problem mentioned | |
110 | above: PE specification rambles that name vector (OriginalFirstThunk) | |
111 | should run in parallel with addresses vector (FirstThunk), i.e. that they | |
112 | should have same number of elements and terminated with zero. We violate | |
113 | this, since FirstThunk points directly into machine code. But in practise, | |
114 | OS loader implemented the sane way: it goes thru OriginalFirstThunk and | |
115 | puts addresses to FirstThunk, not something else. It once again should be | |
116 | noted that dll and symbol name structures are reused across fixup entries | |
117 | and should be there anyway to support standard import stuff, so sustained | |
118 | overhead is 20 bytes per reference. Other question is whether having several | |
119 | IMAGE_IMPORT_DESCRIPTORS for the same DLL is possible. Answer is yes, it is | |
120 | done even by native compiler/linker (libth32's functions are in fact reside | |
121 | in windows9x kernel32.dll, so if you use it, you have two | |
122 | IMAGE_IMPORT_DESCRIPTORS for kernel32.dll). Yet other question is whether | |
123 | referencing the same PE structures several times is valid. The answer is why | |
124 | not, prohibitting that (detecting violation) would require more work on | |
125 | behalf of loader than not doing it. | |
126 | ||
127 | ||
128 | See also: ld/emultempl/pe.em | |
129 | ||
130 | ************************************************************************/ | |
131 | ||
132 | static void | |
133 | add_bfd_to_link (bfd *abfd, CONST char *name, | |
134 | struct bfd_link_info *link_info); | |
135 | ||
252b5132 RH |
136 | /* for emultempl/pe.em */ |
137 | ||
138 | def_file *pe_def_file = 0; | |
139 | int pe_dll_export_everything = 0; | |
140 | int pe_dll_do_default_excludes = 1; | |
141 | int pe_dll_kill_ats = 0; | |
142 | int pe_dll_stdcall_aliases = 0; | |
870df5dc NC |
143 | int pe_dll_warn_dup_exports = 0; |
144 | int pe_dll_compat_implib = 0; | |
b044cda1 | 145 | int pe_dll_extra_pe_debug = 0; |
252b5132 RH |
146 | |
147 | /************************************************************************ | |
148 | ||
149 | static variables and types | |
150 | ||
151 | ************************************************************************/ | |
152 | ||
153 | static bfd_vma image_base; | |
154 | ||
155 | static bfd *filler_bfd; | |
156 | static struct sec *edata_s, *reloc_s; | |
157 | static unsigned char *edata_d, *reloc_d; | |
1069dd8d | 158 | static size_t edata_sz, reloc_sz; |
252b5132 | 159 | |
c6c37250 DD |
160 | typedef struct { |
161 | char *target_name; | |
162 | char *object_target; | |
1069dd8d | 163 | unsigned int imagebase_reloc; |
c6c37250 DD |
164 | int pe_arch; |
165 | int bfd_arch; | |
166 | int underscored; | |
7bfd51a3 | 167 | } pe_details_type; |
c6c37250 | 168 | |
b044cda1 CW |
169 | typedef struct { |
170 | char *name; | |
171 | int len; | |
172 | } autofilter_entry_type; | |
173 | ||
c6c37250 | 174 | #define PE_ARCH_i386 1 |
344a211f NC |
175 | #define PE_ARCH_sh 2 |
176 | #define PE_ARCH_mips 3 | |
177 | #define PE_ARCH_arm 4 | |
c6c37250 DD |
178 | |
179 | static pe_details_type pe_detail_list[] = { | |
180 | { | |
181 | "pei-i386", | |
182 | "pe-i386", | |
183 | 7 /* R_IMAGEBASE */, | |
184 | PE_ARCH_i386, | |
185 | bfd_arch_i386, | |
186 | 1 | |
187 | }, | |
344a211f NC |
188 | { |
189 | "pei-shl", | |
190 | "pe-shl", | |
191 | 16 /* R_SH_IMAGEBASE */, | |
192 | PE_ARCH_sh, | |
193 | bfd_arch_sh, | |
194 | 1 | |
195 | }, | |
196 | { | |
197 | "pei-mips", | |
198 | "pe-mips", | |
199 | 34 /* MIPS_R_RVA */, | |
200 | PE_ARCH_mips, | |
201 | bfd_arch_mips, | |
202 | 0 | |
203 | }, | |
204 | { | |
205 | "pei-arm-little", | |
206 | "pe-arm-little", | |
207 | 11 /* ARM_RVA32 */, | |
208 | PE_ARCH_arm, | |
209 | bfd_arch_arm, | |
210 | 0 | |
211 | }, | |
1069dd8d | 212 | { NULL, NULL, 0, 0, 0, 0 } |
c6c37250 DD |
213 | }; |
214 | ||
215 | static pe_details_type *pe_details; | |
216 | ||
b044cda1 CW |
217 | static autofilter_entry_type autofilter_symbollist[] = { |
218 | { "DllMain@12", 10 }, | |
219 | { "DllEntryPoint@0", 15 }, | |
220 | { "DllMainCRTStartup@12", 20 }, | |
221 | { "_cygwin_dll_entry@12", 20 }, | |
222 | { "_cygwin_crt0_common@8", 21 }, | |
223 | { "_cygwin_noncygwin_dll_entry@12", 30 }, | |
224 | { "impure_ptr", 10 }, | |
225 | { NULL, 0 } | |
226 | }; | |
227 | /* Do not specify library suffix explicitly, to allow for dllized versions */ | |
228 | static autofilter_entry_type autofilter_liblist[] = { | |
229 | { "libgcc.", 7 }, | |
230 | { "libstdc++.", 10 }, | |
231 | { "libmingw32.", 11 }, | |
232 | { NULL, 0 } | |
233 | }; | |
234 | static autofilter_entry_type autofilter_objlist[] = { | |
235 | { "crt0.o", 6 }, | |
236 | { "crt1.o", 6 }, | |
237 | { "crt2.o", 6 }, | |
238 | { NULL, 0 } | |
239 | }; | |
240 | static autofilter_entry_type autofilter_symbolprefixlist[] = { | |
241 | /* { "__imp_", 6 }, */ | |
242 | /* Do __imp_ explicitly to save time */ | |
243 | { "__rtti_", 7 }, | |
244 | { "__builtin_", 10 }, | |
245 | { "_head_", 6 }, /* don't export symbols specifying internal DLL layout */ | |
246 | { "_fmode", 6 }, | |
247 | { "_impure_ptr", 11 }, | |
248 | { "cygwin_attach_dll", 17 }, | |
249 | { "cygwin_premain0", 15 }, | |
250 | { "cygwin_premain1", 15 }, | |
251 | { "cygwin_premain2", 15 }, | |
252 | { "cygwin_premain3", 15 }, | |
253 | { "environ", 7 }, | |
254 | { NULL, 0 } | |
255 | }; | |
256 | static autofilter_entry_type autofilter_symbolsuffixlist[] = { | |
257 | { "_iname", 6 }, | |
258 | { NULL, 0 } | |
259 | }; | |
260 | ||
c6c37250 DD |
261 | #define U(str) (pe_details->underscored ? "_" str : str) |
262 | ||
948f9114 AJ |
263 | static int reloc_sort PARAMS ((const void *, const void *)); |
264 | static int pe_export_sort PARAMS ((const void *, const void *)); | |
265 | static int auto_export PARAMS ((bfd *, def_file *, const char *)); | |
266 | static void process_def_file PARAMS ((bfd *, struct bfd_link_info *)); | |
267 | static void build_filler_bfd PARAMS ((int)); | |
268 | static void generate_edata PARAMS ((bfd *, struct bfd_link_info *)); | |
269 | static void fill_exported_offsets PARAMS ((bfd *, struct bfd_link_info *)); | |
270 | static void fill_edata PARAMS ((bfd *, struct bfd_link_info *)); | |
271 | static void generate_reloc PARAMS ((bfd *, struct bfd_link_info *)); | |
272 | static void quoteput PARAMS ((char *, FILE *, int)); | |
273 | static asection *quick_section PARAMS ((bfd *, const char *, int, int)); | |
274 | static void quick_symbol | |
275 | PARAMS ((bfd *, char *, char *, char *, asection *, int, int)); | |
276 | static void quick_reloc PARAMS ((bfd *, int, int, int)); | |
277 | static bfd *make_head PARAMS ((bfd *)); | |
278 | static bfd *make_tail PARAMS ((bfd *)); | |
279 | static bfd *make_one PARAMS ((def_file_export *, bfd *)); | |
280 | static bfd *make_singleton_name_thunk PARAMS ((char *, bfd *)); | |
281 | static char *make_import_fixup_mark PARAMS ((arelent *)); | |
282 | static bfd *make_import_fixup_entry PARAMS ((char *, char *, char *, bfd *)); | |
283 | static unsigned int pe_get16 PARAMS ((bfd *, int)); | |
284 | static unsigned int pe_get32 PARAMS ((bfd *, int)); | |
285 | static unsigned int pe_as32 PARAMS ((void *)); | |
286 | ||
c6c37250 DD |
287 | void |
288 | pe_dll_id_target (target) | |
1069dd8d | 289 | const char *target; |
c6c37250 DD |
290 | { |
291 | int i; | |
d643799d | 292 | for (i = 0; pe_detail_list[i].target_name; i++) |
9d68bc82 DD |
293 | if (strcmp (pe_detail_list[i].target_name, target) == 0 |
294 | || strcmp (pe_detail_list[i].object_target, target) == 0) | |
c6c37250 | 295 | { |
d643799d | 296 | pe_details = pe_detail_list + i; |
c6c37250 DD |
297 | return; |
298 | } | |
299 | einfo (_("%XUnsupported PEI architecture: %s\n"), target); | |
300 | exit (1); | |
301 | } | |
302 | ||
252b5132 RH |
303 | /************************************************************************ |
304 | ||
305 | Helper functions for qsort. Relocs must be sorted so that we can write | |
306 | them out by pages. | |
307 | ||
308 | ************************************************************************/ | |
309 | ||
c6c37250 DD |
310 | typedef struct { |
311 | bfd_vma vma; | |
312 | char type; | |
313 | short extra; | |
7bfd51a3 | 314 | } reloc_data_type; |
c6c37250 | 315 | |
252b5132 RH |
316 | static int |
317 | reloc_sort (va, vb) | |
318 | const void *va, *vb; | |
319 | { | |
c6c37250 DD |
320 | bfd_vma a = ((reloc_data_type *) va)->vma; |
321 | bfd_vma b = ((reloc_data_type *) vb)->vma; | |
322 | return (a > b) ? 1 : ((a < b) ? -1 : 0); | |
252b5132 RH |
323 | } |
324 | ||
325 | static int | |
326 | pe_export_sort (va, vb) | |
327 | const void *va, *vb; | |
328 | { | |
329 | def_file_export *a = (def_file_export *) va; | |
330 | def_file_export *b = (def_file_export *) vb; | |
331 | return strcmp (a->name, b->name); | |
332 | } | |
333 | ||
334 | /************************************************************************ | |
335 | ||
336 | Read and process the .DEF file | |
337 | ||
338 | ************************************************************************/ | |
339 | ||
340 | /* These correspond to the entries in pe_def_file->exports[]. I use | |
341 | exported_symbol_sections[i] to tag whether or not the symbol was | |
5cc18311 | 342 | defined, since we can't export symbols we don't have. */ |
252b5132 RH |
343 | |
344 | static bfd_vma *exported_symbol_offsets; | |
345 | static struct sec **exported_symbol_sections; | |
346 | ||
347 | static int export_table_size; | |
348 | static int count_exported; | |
349 | static int count_exported_byname; | |
350 | static int count_with_ordinals; | |
351 | static const char *dll_name; | |
352 | static int min_ordinal, max_ordinal; | |
353 | static int *exported_symbols; | |
354 | ||
86b1cc60 KH |
355 | typedef struct exclude_list_struct { |
356 | char *string; | |
357 | struct exclude_list_struct *next; | |
7bfd51a3 | 358 | } exclude_list_struct; |
86b1cc60 | 359 | |
252b5132 RH |
360 | static struct exclude_list_struct *excludes = 0; |
361 | ||
362 | void | |
363 | pe_dll_add_excludes (new_excludes) | |
364 | const char *new_excludes; | |
365 | { | |
366 | char *local_copy; | |
367 | char *exclude_string; | |
368 | ||
369 | local_copy = xstrdup (new_excludes); | |
370 | ||
371 | exclude_string = strtok (local_copy, ",:"); | |
372 | for (; exclude_string; exclude_string = strtok (NULL, ",:")) | |
373 | { | |
374 | struct exclude_list_struct *new_exclude; | |
375 | ||
376 | new_exclude = ((struct exclude_list_struct *) | |
377 | xmalloc (sizeof (struct exclude_list_struct))); | |
378 | new_exclude->string = (char *) xmalloc (strlen (exclude_string) + 1); | |
379 | strcpy (new_exclude->string, exclude_string); | |
380 | new_exclude->next = excludes; | |
381 | excludes = new_exclude; | |
382 | } | |
383 | ||
384 | free (local_copy); | |
385 | } | |
386 | ||
b044cda1 CW |
387 | /* |
388 | abfd is a bfd containing n (or NULL) | |
389 | It can be used for contextual checks. | |
390 | */ | |
252b5132 | 391 | static int |
b044cda1 CW |
392 | auto_export (abfd, d, n) |
393 | bfd *abfd; | |
252b5132 RH |
394 | def_file *d; |
395 | const char *n; | |
396 | { | |
397 | int i; | |
398 | struct exclude_list_struct *ex; | |
b044cda1 CW |
399 | autofilter_entry_type *afptr; |
400 | ||
401 | /* we should not re-export imported stuff */ | |
402 | if (strncmp (n, "_imp__", 6) == 0) | |
403 | return 0; | |
404 | ||
252b5132 RH |
405 | for (i = 0; i < d->num_exports; i++) |
406 | if (strcmp (d->exports[i].name, n) == 0) | |
407 | return 0; | |
408 | if (pe_dll_do_default_excludes) | |
409 | { | |
b044cda1 CW |
410 | if (pe_dll_extra_pe_debug) |
411 | { | |
412 | printf ("considering exporting: %s, abfd=%p, abfd->my_arc=%p\n", | |
413 | n, abfd, abfd->my_archive); | |
414 | } | |
415 | ||
416 | /* First of all, make context checks: | |
417 | Don't export anything from libgcc */ | |
418 | if (abfd && abfd->my_archive) | |
419 | { | |
420 | afptr = autofilter_liblist; | |
421 | while (afptr->name) | |
422 | { | |
423 | if (strstr (abfd->my_archive->filename, afptr->name)) | |
424 | return 0; | |
425 | afptr++; | |
426 | } | |
427 | } | |
428 | ||
429 | /* Next, exclude symbols from certain startup objects */ | |
430 | { | |
431 | char *p; | |
432 | afptr = autofilter_objlist; | |
433 | while (afptr->name) | |
434 | { | |
435 | if (abfd && | |
436 | (p = strstr (abfd->filename, afptr->name)) && | |
437 | (*(p + afptr->len - 1) == 0)) | |
438 | return 0; | |
439 | afptr++; | |
440 | } | |
441 | } | |
442 | ||
443 | /* Don't try to blindly exclude all symbols | |
444 | that begin with '__'; this was tried and | |
445 | it is too restrictive */ | |
446 | ||
447 | /* Then, exclude specific symbols */ | |
448 | afptr = autofilter_symbollist; | |
449 | while (afptr->name) | |
450 | { | |
451 | if (strcmp (n, afptr->name) == 0) | |
452 | return 0; | |
453 | afptr++; | |
454 | } | |
455 | ||
456 | /* Next, exclude symbols starting with ... */ | |
457 | afptr = autofilter_symbolprefixlist; | |
458 | while (afptr->name) | |
459 | { | |
460 | if (strncmp (n, afptr->name, afptr->len) == 0) | |
461 | return 0; | |
462 | afptr++; | |
463 | } | |
464 | ||
465 | /* Finally, exclude symbols ending with ... */ | |
466 | { | |
467 | int len = strlen(n); | |
468 | afptr = autofilter_symbolsuffixlist; | |
469 | while (afptr->name) | |
470 | { | |
471 | if ((len >= afptr->len) && | |
472 | /* add 1 to insure match with trailing '\0' */ | |
473 | strncmp (n + len - afptr->len, afptr->name, | |
474 | afptr->len + 1) == 0) | |
475 | return 0; | |
476 | afptr++; | |
477 | } | |
478 | } | |
252b5132 RH |
479 | } |
480 | for (ex = excludes; ex; ex = ex->next) | |
481 | if (strcmp (n, ex->string) == 0) | |
482 | return 0; | |
483 | return 1; | |
484 | } | |
485 | ||
486 | static void | |
487 | process_def_file (abfd, info) | |
1069dd8d | 488 | bfd *abfd ATTRIBUTE_UNUSED; |
252b5132 RH |
489 | struct bfd_link_info *info; |
490 | { | |
491 | int i, j; | |
492 | struct bfd_link_hash_entry *blhe; | |
493 | bfd *b; | |
494 | struct sec *s; | |
d643799d | 495 | def_file_export *e = 0; |
252b5132 RH |
496 | |
497 | if (!pe_def_file) | |
498 | pe_def_file = def_file_empty (); | |
499 | ||
500 | /* First, run around to all the objects looking for the .drectve | |
86b1cc60 | 501 | sections, and push those into the def file too. */ |
252b5132 RH |
502 | |
503 | for (b = info->input_bfds; b; b = b->link_next) | |
504 | { | |
505 | s = bfd_get_section_by_name (b, ".drectve"); | |
506 | if (s) | |
507 | { | |
508 | int size = bfd_get_section_size_before_reloc (s); | |
509 | char *buf = xmalloc (size); | |
510 | bfd_get_section_contents (b, s, buf, 0, size); | |
511 | def_file_add_directive (pe_def_file, buf, size); | |
512 | free (buf); | |
513 | } | |
514 | } | |
515 | ||
86b1cc60 | 516 | /* Now, maybe export everything else the default way. */ |
252b5132 RH |
517 | |
518 | if (pe_dll_export_everything || pe_def_file->num_exports == 0) | |
519 | { | |
520 | for (b = info->input_bfds; b; b = b->link_next) | |
521 | { | |
522 | asymbol **symbols; | |
523 | int nsyms, symsize; | |
524 | ||
525 | symsize = bfd_get_symtab_upper_bound (b); | |
526 | symbols = (asymbol **) xmalloc (symsize); | |
527 | nsyms = bfd_canonicalize_symtab (b, symbols); | |
528 | ||
529 | for (j = 0; j < nsyms; j++) | |
530 | { | |
d643799d | 531 | /* We should export symbols which are either global or not |
b044cda1 CW |
532 | anything at all. (.bss data is the latter) |
533 | We should not export undefined symbols | |
534 | */ | |
535 | if (symbols[j]->section != &bfd_und_section | |
536 | && ((symbols[j]->flags & BSF_GLOBAL) | |
537 | || (symbols[j]->flags == BFD_FORT_COMM_DEFAULT_VALUE))) | |
252b5132 RH |
538 | { |
539 | const char *sn = symbols[j]->name; | |
b044cda1 CW |
540 | |
541 | /* we should not re-export imported stuff */ | |
542 | { | |
543 | char *name = (char *) xmalloc (strlen (sn) + 2 + 6); | |
544 | sprintf (name, "%s%s", U("_imp_"), sn); | |
545 | blhe = bfd_link_hash_lookup (info->hash, name, | |
546 | false, false, false); | |
547 | free (name); | |
548 | ||
549 | if (blhe && blhe->type == bfd_link_hash_defined) | |
550 | continue; | |
551 | } | |
552 | ||
252b5132 RH |
553 | if (*sn == '_') |
554 | sn++; | |
b044cda1 CW |
555 | if (auto_export (b, pe_def_file, sn)) |
556 | { | |
557 | def_file_export *p; | |
558 | p=def_file_add_export (pe_def_file, sn, 0, -1); | |
559 | /* Fill data flag properly, from dlltool.c */ | |
560 | p->flag_data = !(symbols[j]->flags & BSF_FUNCTION); | |
561 | } | |
252b5132 RH |
562 | } |
563 | } | |
564 | } | |
565 | } | |
566 | ||
567 | #undef NE | |
568 | #define NE pe_def_file->num_exports | |
569 | ||
86b1cc60 | 570 | /* Canonicalize the export list. */ |
252b5132 RH |
571 | |
572 | if (pe_dll_kill_ats) | |
573 | { | |
574 | for (i = 0; i < NE; i++) | |
575 | { | |
576 | if (strchr (pe_def_file->exports[i].name, '@')) | |
577 | { | |
86b1cc60 KH |
578 | /* This will preserve internal_name, which may have been |
579 | pointing to the same memory as name, or might not | |
580 | have. */ | |
252b5132 RH |
581 | char *tmp = xstrdup (pe_def_file->exports[i].name); |
582 | *(strchr (tmp, '@')) = 0; | |
583 | pe_def_file->exports[i].name = tmp; | |
584 | } | |
585 | } | |
586 | } | |
587 | ||
588 | if (pe_dll_stdcall_aliases) | |
589 | { | |
590 | for (i = 0; i < NE; i++) | |
591 | { | |
592 | if (strchr (pe_def_file->exports[i].name, '@')) | |
593 | { | |
594 | char *tmp = xstrdup (pe_def_file->exports[i].name); | |
595 | *(strchr (tmp, '@')) = 0; | |
b044cda1 | 596 | if (auto_export (NULL, pe_def_file, tmp)) |
252b5132 | 597 | def_file_add_export (pe_def_file, tmp, |
b044cda1 CW |
598 | pe_def_file->exports[i].internal_name, |
599 | -1); | |
252b5132 RH |
600 | else |
601 | free (tmp); | |
602 | } | |
603 | } | |
604 | } | |
605 | ||
86b1cc60 KH |
606 | /* Convenience, but watch out for it changing. */ |
607 | e = pe_def_file->exports; | |
252b5132 RH |
608 | |
609 | exported_symbol_offsets = (bfd_vma *) xmalloc (NE * sizeof (bfd_vma)); | |
610 | exported_symbol_sections = (struct sec **) xmalloc (NE * sizeof (struct sec *)); | |
611 | ||
612 | memset (exported_symbol_sections, 0, NE * sizeof (struct sec *)); | |
613 | max_ordinal = 0; | |
614 | min_ordinal = 65536; | |
615 | count_exported = 0; | |
616 | count_exported_byname = 0; | |
617 | count_with_ordinals = 0; | |
618 | ||
619 | qsort (pe_def_file->exports, NE, sizeof (pe_def_file->exports[0]), pe_export_sort); | |
620 | for (i = 0, j = 0; i < NE; i++) | |
621 | { | |
622 | if (i > 0 && strcmp (e[i].name, e[i - 1].name) == 0) | |
623 | { | |
870df5dc | 624 | /* This is a duplicate. */ |
252b5132 RH |
625 | if (e[j - 1].ordinal != -1 |
626 | && e[i].ordinal != -1 | |
627 | && e[j - 1].ordinal != e[i].ordinal) | |
628 | { | |
870df5dc NC |
629 | if (pe_dll_warn_dup_exports) |
630 | /* xgettext:c-format */ | |
486e80e2 | 631 | einfo (_("%XError, duplicate EXPORT with ordinals: %s (%d vs %d)\n"), |
870df5dc | 632 | e[j - 1].name, e[j - 1].ordinal, e[i].ordinal); |
252b5132 RH |
633 | } |
634 | else | |
635 | { | |
870df5dc NC |
636 | if (pe_dll_warn_dup_exports) |
637 | /* xgettext:c-format */ | |
638 | einfo (_("Warning, duplicate EXPORT: %s\n"), | |
639 | e[j - 1].name); | |
252b5132 | 640 | } |
486e80e2 | 641 | if (e[i].ordinal != -1) |
252b5132 RH |
642 | e[j - 1].ordinal = e[i].ordinal; |
643 | e[j - 1].flag_private |= e[i].flag_private; | |
644 | e[j - 1].flag_constant |= e[i].flag_constant; | |
645 | e[j - 1].flag_noname |= e[i].flag_noname; | |
646 | e[j - 1].flag_data |= e[i].flag_data; | |
647 | } | |
648 | else | |
649 | { | |
650 | if (i != j) | |
651 | e[j] = e[i]; | |
652 | j++; | |
653 | } | |
654 | } | |
655 | pe_def_file->num_exports = j; /* == NE */ | |
656 | ||
657 | for (i = 0; i < NE; i++) | |
658 | { | |
659 | char *name = (char *) xmalloc (strlen (pe_def_file->exports[i].internal_name) + 2); | |
c6c37250 DD |
660 | if (pe_details->underscored) |
661 | { | |
662 | *name = '_'; | |
663 | strcpy (name + 1, pe_def_file->exports[i].internal_name); | |
664 | } | |
665 | else | |
666 | strcpy (name, pe_def_file->exports[i].internal_name); | |
252b5132 RH |
667 | |
668 | blhe = bfd_link_hash_lookup (info->hash, | |
669 | name, | |
670 | false, false, true); | |
671 | ||
8a5b676c | 672 | if (blhe |
d643799d | 673 | && (blhe->type == bfd_link_hash_defined |
8a5b676c | 674 | || (blhe->type == bfd_link_hash_common))) |
252b5132 RH |
675 | { |
676 | count_exported++; | |
677 | if (!pe_def_file->exports[i].flag_noname) | |
678 | count_exported_byname++; | |
8a5b676c DD |
679 | |
680 | /* Only fill in the sections. The actual offsets are computed | |
681 | in fill_exported_offsets() after common symbols are laid | |
682 | out. */ | |
d643799d | 683 | if (blhe->type == bfd_link_hash_defined) |
8a5b676c DD |
684 | exported_symbol_sections[i] = blhe->u.def.section; |
685 | else | |
686 | exported_symbol_sections[i] = blhe->u.c.p->section; | |
5cc18311 | 687 | |
252b5132 RH |
688 | if (pe_def_file->exports[i].ordinal != -1) |
689 | { | |
690 | if (max_ordinal < pe_def_file->exports[i].ordinal) | |
691 | max_ordinal = pe_def_file->exports[i].ordinal; | |
692 | if (min_ordinal > pe_def_file->exports[i].ordinal) | |
693 | min_ordinal = pe_def_file->exports[i].ordinal; | |
694 | count_with_ordinals++; | |
695 | } | |
696 | } | |
697 | else if (blhe && blhe->type == bfd_link_hash_undefined) | |
698 | { | |
699 | /* xgettext:c-format */ | |
700 | einfo (_("%XCannot export %s: symbol not defined\n"), | |
701 | pe_def_file->exports[i].internal_name); | |
702 | } | |
703 | else if (blhe) | |
704 | { | |
705 | /* xgettext:c-format */ | |
706 | einfo (_("%XCannot export %s: symbol wrong type (%d vs %d)\n"), | |
707 | pe_def_file->exports[i].internal_name, | |
708 | blhe->type, bfd_link_hash_defined); | |
709 | } | |
710 | else | |
711 | { | |
712 | /* xgettext:c-format */ | |
713 | einfo (_("%XCannot export %s: symbol not found\n"), | |
714 | pe_def_file->exports[i].internal_name); | |
715 | } | |
716 | free (name); | |
717 | } | |
718 | } | |
719 | ||
720 | /************************************************************************ | |
721 | ||
722 | Build the bfd that will contain .edata and .reloc sections | |
723 | ||
724 | ************************************************************************/ | |
725 | ||
726 | static void | |
c6c37250 DD |
727 | build_filler_bfd (include_edata) |
728 | int include_edata; | |
252b5132 RH |
729 | { |
730 | lang_input_statement_type *filler_file; | |
731 | filler_file = lang_add_input_file ("dll stuff", | |
732 | lang_input_file_is_fake_enum, | |
733 | NULL); | |
734 | filler_file->the_bfd = filler_bfd = bfd_create ("dll stuff", output_bfd); | |
735 | if (filler_bfd == NULL | |
736 | || !bfd_set_arch_mach (filler_bfd, | |
737 | bfd_get_arch (output_bfd), | |
738 | bfd_get_mach (output_bfd))) | |
739 | { | |
740 | einfo ("%X%P: can not create BFD %E\n"); | |
741 | return; | |
742 | } | |
743 | ||
c6c37250 | 744 | if (include_edata) |
252b5132 | 745 | { |
c6c37250 DD |
746 | edata_s = bfd_make_section_old_way (filler_bfd, ".edata"); |
747 | if (edata_s == NULL | |
748 | || !bfd_set_section_flags (filler_bfd, edata_s, | |
749 | (SEC_HAS_CONTENTS | |
750 | | SEC_ALLOC | |
751 | | SEC_LOAD | |
752 | | SEC_KEEP | |
753 | | SEC_IN_MEMORY))) | |
754 | { | |
755 | einfo ("%X%P: can not create .edata section: %E\n"); | |
756 | return; | |
757 | } | |
758 | bfd_set_section_size (filler_bfd, edata_s, edata_sz); | |
252b5132 | 759 | } |
252b5132 RH |
760 | |
761 | reloc_s = bfd_make_section_old_way (filler_bfd, ".reloc"); | |
762 | if (reloc_s == NULL | |
763 | || !bfd_set_section_flags (filler_bfd, reloc_s, | |
764 | (SEC_HAS_CONTENTS | |
765 | | SEC_ALLOC | |
766 | | SEC_LOAD | |
767 | | SEC_KEEP | |
768 | | SEC_IN_MEMORY))) | |
769 | { | |
770 | einfo ("%X%P: can not create .reloc section: %E\n"); | |
771 | return; | |
772 | } | |
773 | bfd_set_section_size (filler_bfd, reloc_s, 0); | |
774 | ||
775 | ldlang_add_file (filler_file); | |
776 | } | |
777 | ||
778 | /************************************************************************ | |
779 | ||
780 | Gather all the exported symbols and build the .edata section | |
781 | ||
782 | ************************************************************************/ | |
783 | ||
784 | static void | |
785 | generate_edata (abfd, info) | |
786 | bfd *abfd; | |
1069dd8d | 787 | struct bfd_link_info *info ATTRIBUTE_UNUSED; |
252b5132 RH |
788 | { |
789 | int i, next_ordinal; | |
790 | int name_table_size = 0; | |
791 | const char *dlnp; | |
792 | ||
793 | /* First, we need to know how many exported symbols there are, | |
5cc18311 | 794 | and what the range of ordinals is. */ |
252b5132 RH |
795 | |
796 | if (pe_def_file->name) | |
797 | { | |
798 | dll_name = pe_def_file->name; | |
799 | } | |
800 | else | |
801 | { | |
802 | dll_name = abfd->filename; | |
803 | for (dlnp = dll_name; *dlnp; dlnp++) | |
804 | { | |
805 | if (*dlnp == '\\' || *dlnp == '/' || *dlnp == ':') | |
806 | dll_name = dlnp + 1; | |
807 | } | |
808 | } | |
809 | ||
810 | if (count_with_ordinals && max_ordinal > count_exported) | |
811 | { | |
812 | if (min_ordinal > max_ordinal - count_exported + 1) | |
813 | min_ordinal = max_ordinal - count_exported + 1; | |
814 | } | |
815 | else | |
816 | { | |
817 | min_ordinal = 1; | |
818 | max_ordinal = count_exported; | |
819 | } | |
820 | export_table_size = max_ordinal - min_ordinal + 1; | |
821 | ||
822 | exported_symbols = (int *) xmalloc (export_table_size * sizeof (int)); | |
823 | for (i = 0; i < export_table_size; i++) | |
824 | exported_symbols[i] = -1; | |
825 | ||
86b1cc60 | 826 | /* Now we need to assign ordinals to those that don't have them. */ |
252b5132 RH |
827 | for (i = 0; i < NE; i++) |
828 | { | |
829 | if (exported_symbol_sections[i]) | |
830 | { | |
831 | if (pe_def_file->exports[i].ordinal != -1) | |
832 | { | |
833 | int ei = pe_def_file->exports[i].ordinal - min_ordinal; | |
834 | int pi = exported_symbols[ei]; | |
835 | if (pi != -1) | |
836 | { | |
837 | /* xgettext:c-format */ | |
486e80e2 | 838 | einfo (_("%XError, ordinal used twice: %d (%s vs %s)\n"), |
252b5132 RH |
839 | pe_def_file->exports[i].ordinal, |
840 | pe_def_file->exports[i].name, | |
841 | pe_def_file->exports[pi].name); | |
842 | } | |
843 | exported_symbols[ei] = i; | |
844 | } | |
845 | name_table_size += strlen (pe_def_file->exports[i].name) + 1; | |
846 | } | |
847 | } | |
848 | ||
849 | next_ordinal = min_ordinal; | |
850 | for (i = 0; i < NE; i++) | |
851 | if (exported_symbol_sections[i]) | |
852 | if (pe_def_file->exports[i].ordinal == -1) | |
853 | { | |
854 | while (exported_symbols[next_ordinal - min_ordinal] != -1) | |
855 | next_ordinal++; | |
856 | exported_symbols[next_ordinal - min_ordinal] = i; | |
857 | pe_def_file->exports[i].ordinal = next_ordinal; | |
858 | } | |
859 | ||
86b1cc60 | 860 | /* OK, now we can allocate some memory. */ |
252b5132 RH |
861 | |
862 | edata_sz = (40 /* directory */ | |
863 | + 4 * export_table_size /* addresses */ | |
864 | + 4 * count_exported_byname /* name ptrs */ | |
865 | + 2 * count_exported_byname /* ordinals */ | |
866 | + name_table_size + strlen (dll_name) + 1); | |
867 | } | |
868 | ||
8a5b676c DD |
869 | /* Fill the exported symbol offsets. The preliminary work has already |
870 | been done in process_def_file(). */ | |
871 | ||
872 | static void | |
873 | fill_exported_offsets (abfd, info) | |
f0c87f88 | 874 | bfd *abfd ATTRIBUTE_UNUSED; |
8a5b676c DD |
875 | struct bfd_link_info *info; |
876 | { | |
f0c87f88 | 877 | int i; |
8a5b676c | 878 | struct bfd_link_hash_entry *blhe; |
5cc18311 | 879 | |
8a5b676c DD |
880 | for (i = 0; i < pe_def_file->num_exports; i++) |
881 | { | |
882 | char *name = (char *) xmalloc (strlen (pe_def_file->exports[i].internal_name) + 2); | |
883 | if (pe_details->underscored) | |
884 | { | |
885 | *name = '_'; | |
886 | strcpy (name + 1, pe_def_file->exports[i].internal_name); | |
887 | } | |
888 | else | |
889 | strcpy (name, pe_def_file->exports[i].internal_name); | |
890 | ||
891 | blhe = bfd_link_hash_lookup (info->hash, | |
892 | name, | |
893 | false, false, true); | |
894 | ||
895 | if (blhe && (blhe->type == bfd_link_hash_defined)) | |
896 | { | |
897 | exported_symbol_offsets[i] = blhe->u.def.value; | |
d643799d | 898 | } |
8a5b676c DD |
899 | free (name); |
900 | } | |
901 | } | |
902 | ||
252b5132 RH |
903 | static void |
904 | fill_edata (abfd, info) | |
905 | bfd *abfd; | |
1069dd8d | 906 | struct bfd_link_info *info ATTRIBUTE_UNUSED; |
252b5132 RH |
907 | { |
908 | int i, hint; | |
909 | unsigned char *edirectory; | |
910 | unsigned long *eaddresses; | |
911 | unsigned long *enameptrs; | |
912 | unsigned short *eordinals; | |
913 | unsigned char *enamestr; | |
914 | time_t now; | |
915 | ||
916 | time (&now); | |
917 | ||
918 | edata_d = (unsigned char *) xmalloc (edata_sz); | |
919 | ||
86b1cc60 | 920 | /* Note use of array pointer math here. */ |
252b5132 RH |
921 | edirectory = edata_d; |
922 | eaddresses = (unsigned long *) (edata_d + 40); | |
923 | enameptrs = eaddresses + export_table_size; | |
924 | eordinals = (unsigned short *) (enameptrs + count_exported_byname); | |
925 | enamestr = (char *) (eordinals + count_exported_byname); | |
926 | ||
927 | #define ERVA(ptr) (((unsigned char *)(ptr) - edata_d) + edata_s->output_section->vma - image_base) | |
928 | ||
c2a94a7a | 929 | memset (edata_d, 0, edata_sz); |
252b5132 RH |
930 | bfd_put_32 (abfd, now, edata_d + 4); |
931 | if (pe_def_file->version_major != -1) | |
932 | { | |
933 | bfd_put_16 (abfd, pe_def_file->version_major, edata_d + 8); | |
934 | bfd_put_16 (abfd, pe_def_file->version_minor, edata_d + 10); | |
935 | } | |
936 | bfd_put_32 (abfd, ERVA (enamestr), edata_d + 12); | |
937 | strcpy (enamestr, dll_name); | |
938 | enamestr += strlen (enamestr) + 1; | |
939 | bfd_put_32 (abfd, min_ordinal, edata_d + 16); | |
940 | bfd_put_32 (abfd, export_table_size, edata_d + 20); | |
941 | bfd_put_32 (abfd, count_exported_byname, edata_d + 24); | |
942 | bfd_put_32 (abfd, ERVA (eaddresses), edata_d + 28); | |
943 | bfd_put_32 (abfd, ERVA (enameptrs), edata_d + 32); | |
944 | bfd_put_32 (abfd, ERVA (eordinals), edata_d + 36); | |
945 | ||
8a5b676c DD |
946 | fill_exported_offsets (abfd, info); |
947 | ||
86b1cc60 | 948 | /* Ok, now for the filling in part. */ |
252b5132 RH |
949 | hint = 0; |
950 | for (i = 0; i < export_table_size; i++) | |
951 | { | |
952 | int s = exported_symbols[i]; | |
953 | if (s != -1) | |
954 | { | |
955 | struct sec *ssec = exported_symbol_sections[s]; | |
956 | unsigned long srva = (exported_symbol_offsets[s] | |
957 | + ssec->output_section->vma | |
958 | + ssec->output_offset); | |
45b1f63c | 959 | int ord = pe_def_file->exports[s].ordinal; |
252b5132 | 960 | |
45b1f63c DD |
961 | bfd_put_32 (abfd, srva - image_base, |
962 | (void *) (eaddresses + ord - min_ordinal)); | |
252b5132 RH |
963 | if (!pe_def_file->exports[s].flag_noname) |
964 | { | |
965 | char *ename = pe_def_file->exports[s].name; | |
966 | bfd_put_32 (abfd, ERVA (enamestr), (void *) enameptrs); | |
45b1f63c | 967 | enameptrs++; |
252b5132 RH |
968 | strcpy (enamestr, ename); |
969 | enamestr += strlen (enamestr) + 1; | |
45b1f63c DD |
970 | bfd_put_16 (abfd, ord - min_ordinal, (void *) eordinals); |
971 | eordinals++; | |
252b5132 RH |
972 | pe_def_file->exports[s].hint = hint++; |
973 | } | |
252b5132 RH |
974 | } |
975 | } | |
976 | } | |
977 | ||
b044cda1 CW |
978 | |
979 | static struct sec *current_sec; | |
980 | ||
981 | void | |
982 | pe_walk_relocs_of_symbol (info, name, cb) | |
983 | struct bfd_link_info *info; | |
984 | CONST char *name; | |
985 | int (*cb) (arelent *); | |
986 | { | |
987 | bfd *b; | |
988 | struct sec *s; | |
989 | ||
990 | for (b = info->input_bfds; b; b = b->link_next) | |
991 | { | |
992 | arelent **relocs; | |
993 | int relsize, nrelocs, i; | |
994 | ||
995 | for (s = b->sections; s; s = s->next) | |
996 | { | |
997 | asymbol **symbols; | |
998 | int nsyms, symsize; | |
999 | int flags = bfd_get_section_flags (b, s); | |
1000 | ||
1001 | /* Skip discarded linkonce sections */ | |
1002 | if (flags & SEC_LINK_ONCE | |
1003 | && s->output_section == bfd_abs_section_ptr) | |
1004 | continue; | |
1005 | ||
1006 | current_sec=s; | |
1007 | ||
1008 | symsize = bfd_get_symtab_upper_bound (b); | |
1009 | symbols = (asymbol **) xmalloc (symsize); | |
1010 | nsyms = bfd_canonicalize_symtab (b, symbols); | |
1011 | ||
1012 | relsize = bfd_get_reloc_upper_bound (b, s); | |
1013 | relocs = (arelent **) xmalloc ((size_t) relsize); | |
1014 | nrelocs = bfd_canonicalize_reloc (b, s, relocs, symbols); | |
1015 | ||
1016 | for (i = 0; i < nrelocs; i++) | |
1017 | { | |
1018 | struct symbol_cache_entry *sym = *relocs[i]->sym_ptr_ptr; | |
1019 | if (!strcmp(name,sym->name)) cb(relocs[i]); | |
1020 | } | |
1021 | free (relocs); | |
1022 | /* Warning: the allocated symbols are remembered in BFD and reused | |
1023 | later, so don't free them! */ | |
1024 | /* free (symbols); */ | |
1025 | } | |
1026 | } | |
1027 | } | |
1028 | ||
252b5132 RH |
1029 | /************************************************************************ |
1030 | ||
1031 | Gather all the relocations and build the .reloc section | |
1032 | ||
1033 | ************************************************************************/ | |
1034 | ||
1035 | static void | |
1036 | generate_reloc (abfd, info) | |
1037 | bfd *abfd; | |
1038 | struct bfd_link_info *info; | |
1039 | { | |
1040 | ||
86b1cc60 | 1041 | /* For .reloc stuff. */ |
c6c37250 | 1042 | reloc_data_type *reloc_data; |
252b5132 RH |
1043 | int total_relocs = 0; |
1044 | int i; | |
1045 | unsigned long sec_page = (unsigned long) (-1); | |
1046 | unsigned long page_ptr, page_count; | |
1047 | int bi; | |
1048 | bfd *b; | |
1049 | struct sec *s; | |
1050 | ||
1051 | total_relocs = 0; | |
1052 | for (b = info->input_bfds; b; b = b->link_next) | |
1053 | for (s = b->sections; s; s = s->next) | |
1054 | total_relocs += s->reloc_count; | |
1055 | ||
b044cda1 CW |
1056 | reloc_data = |
1057 | (reloc_data_type *) xmalloc (total_relocs * sizeof (reloc_data_type)); | |
252b5132 RH |
1058 | |
1059 | total_relocs = 0; | |
1060 | bi = 0; | |
1061 | for (bi = 0, b = info->input_bfds; b; bi++, b = b->link_next) | |
1062 | { | |
1063 | arelent **relocs; | |
1064 | int relsize, nrelocs, i; | |
1065 | ||
1066 | for (s = b->sections; s; s = s->next) | |
1067 | { | |
1068 | unsigned long sec_vma = s->output_section->vma + s->output_offset; | |
1069 | asymbol **symbols; | |
1070 | int nsyms, symsize; | |
1071 | ||
86b1cc60 | 1072 | /* If it's not loaded, we don't need to relocate it this way. */ |
252b5132 RH |
1073 | if (!(s->output_section->flags & SEC_LOAD)) |
1074 | continue; | |
1075 | ||
1076 | /* I don't know why there would be a reloc for these, but I've | |
86b1cc60 | 1077 | seen it happen - DJ */ |
252b5132 RH |
1078 | if (s->output_section == &bfd_abs_section) |
1079 | continue; | |
1080 | ||
1081 | if (s->output_section->vma == 0) | |
1082 | { | |
86b1cc60 | 1083 | /* Huh? Shouldn't happen, but punt if it does. */ |
252b5132 RH |
1084 | einfo ("DJ: zero vma section reloc detected: `%s' #%d f=%d\n", |
1085 | s->output_section->name, s->output_section->index, | |
1086 | s->output_section->flags); | |
1087 | continue; | |
1088 | } | |
1089 | ||
1090 | symsize = bfd_get_symtab_upper_bound (b); | |
1091 | symbols = (asymbol **) xmalloc (symsize); | |
1092 | nsyms = bfd_canonicalize_symtab (b, symbols); | |
1093 | ||
1094 | relsize = bfd_get_reloc_upper_bound (b, s); | |
1095 | relocs = (arelent **) xmalloc ((size_t) relsize); | |
1096 | nrelocs = bfd_canonicalize_reloc (b, s, relocs, symbols); | |
1097 | ||
1098 | for (i = 0; i < nrelocs; i++) | |
1099 | { | |
b044cda1 CW |
1100 | if (pe_dll_extra_pe_debug) |
1101 | { | |
1102 | struct symbol_cache_entry *sym = *relocs[i]->sym_ptr_ptr; | |
1103 | printf("rel: %s\n",sym->name); | |
1104 | } | |
252b5132 | 1105 | if (!relocs[i]->howto->pc_relative |
c6c37250 | 1106 | && relocs[i]->howto->type != pe_details->imagebase_reloc) |
252b5132 | 1107 | { |
c6c37250 DD |
1108 | bfd_vma sym_vma; |
1109 | struct symbol_cache_entry *sym = *relocs[i]->sym_ptr_ptr; | |
1110 | sym_vma = (relocs[i]->addend | |
1111 | + sym->value | |
1112 | + sym->section->vma | |
1113 | + sym->section->output_offset | |
1114 | + sym->section->output_section->vma); | |
1115 | reloc_data[total_relocs].vma = sec_vma + relocs[i]->address; | |
5cc18311 | 1116 | |
344a211f | 1117 | #define BITS_AND_SHIFT(bits, shift) (bits * 1000 | shift) |
5cc18311 | 1118 | |
344a211f NC |
1119 | switch BITS_AND_SHIFT (relocs[i]->howto->bitsize, |
1120 | relocs[i]->howto->rightshift) | |
252b5132 | 1121 | { |
344a211f | 1122 | case BITS_AND_SHIFT (32, 0): |
c6c37250 DD |
1123 | reloc_data[total_relocs].type = 3; |
1124 | total_relocs++; | |
252b5132 | 1125 | break; |
344a211f NC |
1126 | case BITS_AND_SHIFT (16, 0): |
1127 | reloc_data[total_relocs].type = 2; | |
1128 | total_relocs++; | |
1129 | break; | |
1130 | case BITS_AND_SHIFT (16, 16): | |
1131 | reloc_data[total_relocs].type = 4; | |
86b1cc60 KH |
1132 | /* FIXME: we can't know the symbol's right value |
1133 | yet, but we probably can safely assume that | |
1134 | CE will relocate us in 64k blocks, so leaving | |
1135 | it zero is safe. */ | |
344a211f NC |
1136 | reloc_data[total_relocs].extra = 0; |
1137 | total_relocs++; | |
1138 | break; | |
1139 | case BITS_AND_SHIFT (26, 2): | |
1140 | reloc_data[total_relocs].type = 5; | |
1141 | total_relocs++; | |
1142 | break; | |
252b5132 RH |
1143 | default: |
1144 | /* xgettext:c-format */ | |
1145 | einfo (_("%XError: %d-bit reloc in dll\n"), | |
1146 | relocs[i]->howto->bitsize); | |
1147 | break; | |
1148 | } | |
1149 | } | |
1150 | } | |
1151 | free (relocs); | |
86b1cc60 KH |
1152 | /* Warning: the allocated symbols are remembered in BFD and |
1153 | reused later, so don't free them! */ | |
7bfd51a3 KH |
1154 | #if 0 |
1155 | free (symbol); | |
1156 | #endif | |
252b5132 RH |
1157 | } |
1158 | } | |
1159 | ||
1160 | /* At this point, we have total_relocs relocation addresses in | |
1161 | reloc_addresses, which are all suitable for the .reloc section. | |
5cc18311 | 1162 | We must now create the new sections. */ |
252b5132 | 1163 | |
c6c37250 | 1164 | qsort (reloc_data, total_relocs, sizeof (*reloc_data), reloc_sort); |
252b5132 RH |
1165 | |
1166 | for (i = 0; i < total_relocs; i++) | |
1167 | { | |
c6c37250 | 1168 | unsigned long this_page = (reloc_data[i].vma >> 12); |
5cc18311 | 1169 | |
252b5132 RH |
1170 | if (this_page != sec_page) |
1171 | { | |
1172 | reloc_sz = (reloc_sz + 3) & ~3; /* 4-byte align */ | |
1173 | reloc_sz += 8; | |
1174 | sec_page = this_page; | |
1175 | } | |
5cc18311 | 1176 | |
252b5132 | 1177 | reloc_sz += 2; |
5cc18311 | 1178 | |
344a211f NC |
1179 | if (reloc_data[i].type == 4) |
1180 | reloc_sz += 2; | |
252b5132 RH |
1181 | } |
1182 | reloc_sz = (reloc_sz + 3) & ~3; /* 4-byte align */ | |
1183 | ||
1184 | reloc_d = (unsigned char *) xmalloc (reloc_sz); | |
1185 | ||
1186 | sec_page = (unsigned long) (-1); | |
1187 | reloc_sz = 0; | |
1188 | page_ptr = (unsigned long) (-1); | |
1189 | page_count = 0; | |
1190 | for (i = 0; i < total_relocs; i++) | |
1191 | { | |
c6c37250 | 1192 | unsigned long rva = reloc_data[i].vma - image_base; |
252b5132 RH |
1193 | unsigned long this_page = (rva & ~0xfff); |
1194 | if (this_page != sec_page) | |
1195 | { | |
1196 | while (reloc_sz & 3) | |
1197 | reloc_d[reloc_sz++] = 0; | |
1198 | if (page_ptr != (unsigned long) (-1)) | |
1199 | bfd_put_32 (abfd, reloc_sz - page_ptr, reloc_d + page_ptr + 4); | |
1200 | bfd_put_32 (abfd, this_page, reloc_d + reloc_sz); | |
1201 | page_ptr = reloc_sz; | |
1202 | reloc_sz += 8; | |
1203 | sec_page = this_page; | |
1204 | page_count = 0; | |
1205 | } | |
d643799d | 1206 | bfd_put_16 (abfd, (rva & 0xfff) + (reloc_data[i].type << 12), |
c6c37250 | 1207 | reloc_d + reloc_sz); |
252b5132 | 1208 | reloc_sz += 2; |
c6c37250 DD |
1209 | if (reloc_data[i].type == 4) |
1210 | { | |
1211 | bfd_put_16 (abfd, reloc_data[i].extra, reloc_d + reloc_sz); | |
1212 | reloc_sz += 2; | |
1213 | } | |
252b5132 RH |
1214 | page_count++; |
1215 | } | |
1216 | while (reloc_sz & 3) | |
1217 | reloc_d[reloc_sz++] = 0; | |
1218 | if (page_ptr != (unsigned long) (-1)) | |
1219 | bfd_put_32 (abfd, reloc_sz - page_ptr, reloc_d + page_ptr + 4); | |
1220 | while (reloc_sz < reloc_s->_raw_size) | |
1221 | reloc_d[reloc_sz++] = 0; | |
1222 | } | |
1223 | ||
1224 | /************************************************************************ | |
1225 | ||
1226 | Given the exiting def_file structure, print out a .DEF file that | |
1227 | corresponds to it. | |
1228 | ||
1229 | ************************************************************************/ | |
1230 | ||
1231 | static void | |
1232 | quoteput (s, f, needs_quotes) | |
1233 | char *s; | |
d643799d | 1234 | FILE *f; |
252b5132 RH |
1235 | int needs_quotes; |
1236 | { | |
1237 | char *cp; | |
1238 | for (cp = s; *cp; cp++) | |
1239 | if (*cp == '\'' | |
1240 | || *cp == '"' | |
1241 | || *cp == '\\' | |
1242 | || isspace ((unsigned char) *cp) | |
1243 | || *cp == ',' | |
1244 | || *cp == ';') | |
1245 | needs_quotes = 1; | |
1246 | if (needs_quotes) | |
1247 | { | |
1248 | putc ('"', f); | |
1249 | while (*s) | |
1250 | { | |
1251 | if (*s == '"' || *s == '\\') | |
1252 | putc ('\\', f); | |
1253 | putc (*s, f); | |
1254 | s++; | |
1255 | } | |
1256 | putc ('"', f); | |
1257 | } | |
1258 | else | |
1259 | fputs (s, f); | |
1260 | } | |
1261 | ||
1262 | void | |
1263 | pe_dll_generate_def_file (pe_out_def_filename) | |
1069dd8d | 1264 | const char *pe_out_def_filename; |
252b5132 RH |
1265 | { |
1266 | int i; | |
1267 | FILE *out = fopen (pe_out_def_filename, "w"); | |
1268 | if (out == NULL) | |
1269 | { | |
1270 | /* xgettext:c-format */ | |
1271 | einfo (_("%s: Can't open output def file %s\n"), | |
1272 | program_name, pe_out_def_filename); | |
1273 | } | |
1274 | ||
1275 | if (pe_def_file) | |
1276 | { | |
1277 | if (pe_def_file->name) | |
1278 | { | |
1279 | if (pe_def_file->is_dll) | |
1280 | fprintf (out, "LIBRARY "); | |
1281 | else | |
1282 | fprintf (out, "NAME "); | |
1283 | quoteput (pe_def_file->name, out, 1); | |
1284 | if (pe_data (output_bfd)->pe_opthdr.ImageBase) | |
1285 | fprintf (out, " BASE=0x%lx", | |
1286 | (unsigned long) pe_data (output_bfd)->pe_opthdr.ImageBase); | |
1287 | fprintf (out, "\n"); | |
1288 | } | |
1289 | ||
1290 | if (pe_def_file->description) | |
1291 | { | |
1292 | fprintf (out, "DESCRIPTION "); | |
1293 | quoteput (pe_def_file->description, out, 1); | |
1294 | fprintf (out, "\n"); | |
1295 | } | |
1296 | ||
1297 | if (pe_def_file->version_minor != -1) | |
1298 | fprintf (out, "VERSION %d.%d\n", pe_def_file->version_major, | |
1299 | pe_def_file->version_minor); | |
1300 | else if (pe_def_file->version_major != -1) | |
1301 | fprintf (out, "VERSION %d\n", pe_def_file->version_major); | |
1302 | ||
1303 | if (pe_def_file->stack_reserve != -1 || pe_def_file->heap_reserve != -1) | |
1304 | fprintf (out, "\n"); | |
1305 | ||
1306 | if (pe_def_file->stack_commit != -1) | |
1307 | fprintf (out, "STACKSIZE 0x%x,0x%x\n", | |
1308 | pe_def_file->stack_reserve, pe_def_file->stack_commit); | |
1309 | else if (pe_def_file->stack_reserve != -1) | |
1310 | fprintf (out, "STACKSIZE 0x%x\n", pe_def_file->stack_reserve); | |
1311 | if (pe_def_file->heap_commit != -1) | |
1312 | fprintf (out, "HEAPSIZE 0x%x,0x%x\n", | |
1313 | pe_def_file->heap_reserve, pe_def_file->heap_commit); | |
1314 | else if (pe_def_file->heap_reserve != -1) | |
1315 | fprintf (out, "HEAPSIZE 0x%x\n", pe_def_file->heap_reserve); | |
1316 | ||
1317 | if (pe_def_file->num_section_defs > 0) | |
1318 | { | |
1319 | fprintf (out, "\nSECTIONS\n\n"); | |
1320 | for (i = 0; i < pe_def_file->num_section_defs; i++) | |
1321 | { | |
1322 | fprintf (out, " "); | |
1323 | quoteput (pe_def_file->section_defs[i].name, out, 0); | |
1324 | if (pe_def_file->section_defs[i].class) | |
1325 | { | |
1326 | fprintf (out, " CLASS "); | |
1327 | quoteput (pe_def_file->section_defs[i].class, out, 0); | |
1328 | } | |
1329 | if (pe_def_file->section_defs[i].flag_read) | |
1330 | fprintf (out, " READ"); | |
1331 | if (pe_def_file->section_defs[i].flag_write) | |
1332 | fprintf (out, " WRITE"); | |
1333 | if (pe_def_file->section_defs[i].flag_execute) | |
1334 | fprintf (out, " EXECUTE"); | |
1335 | if (pe_def_file->section_defs[i].flag_shared) | |
1336 | fprintf (out, " SHARED"); | |
1337 | fprintf (out, "\n"); | |
1338 | } | |
1339 | } | |
1340 | ||
1341 | if (pe_def_file->num_exports > 0) | |
1342 | { | |
b044cda1 | 1343 | fprintf (out, "EXPORTS\n"); |
252b5132 RH |
1344 | for (i = 0; i < pe_def_file->num_exports; i++) |
1345 | { | |
1346 | def_file_export *e = pe_def_file->exports + i; | |
1347 | fprintf (out, " "); | |
1348 | quoteput (e->name, out, 0); | |
1349 | if (e->internal_name && strcmp (e->internal_name, e->name)) | |
1350 | { | |
1351 | fprintf (out, " = "); | |
1352 | quoteput (e->internal_name, out, 0); | |
1353 | } | |
1354 | if (e->ordinal != -1) | |
1355 | fprintf (out, " @%d", e->ordinal); | |
1356 | if (e->flag_private) | |
1357 | fprintf (out, " PRIVATE"); | |
1358 | if (e->flag_constant) | |
1359 | fprintf (out, " CONSTANT"); | |
1360 | if (e->flag_noname) | |
1361 | fprintf (out, " NONAME"); | |
1362 | if (e->flag_data) | |
1363 | fprintf (out, " DATA"); | |
1364 | ||
1365 | fprintf (out, "\n"); | |
1366 | } | |
1367 | } | |
1368 | ||
1369 | if (pe_def_file->num_imports > 0) | |
1370 | { | |
1371 | fprintf (out, "\nIMPORTS\n\n"); | |
1372 | for (i = 0; i < pe_def_file->num_imports; i++) | |
1373 | { | |
1374 | def_file_import *im = pe_def_file->imports + i; | |
1375 | fprintf (out, " "); | |
1376 | if (im->internal_name | |
1377 | && (!im->name || strcmp (im->internal_name, im->name))) | |
1378 | { | |
1379 | quoteput (im->internal_name, out, 0); | |
1380 | fprintf (out, " = "); | |
1381 | } | |
1382 | quoteput (im->module->name, out, 0); | |
1383 | fprintf (out, "."); | |
1384 | if (im->name) | |
1385 | quoteput (im->name, out, 0); | |
1386 | else | |
1387 | fprintf (out, "%d", im->ordinal); | |
1388 | fprintf (out, "\n"); | |
1389 | } | |
1390 | } | |
1391 | } | |
1392 | else | |
1393 | fprintf (out, _("; no contents available\n")); | |
1394 | ||
1395 | if (fclose (out) == EOF) | |
1396 | { | |
1397 | /* xgettext:c-format */ | |
1398 | einfo (_("%P: Error closing file `%s'\n"), pe_out_def_filename); | |
1399 | } | |
1400 | } | |
1401 | ||
1402 | /************************************************************************ | |
1403 | ||
1404 | Generate the import library | |
1405 | ||
1406 | ************************************************************************/ | |
1407 | ||
1408 | static asymbol **symtab; | |
1409 | static int symptr; | |
1410 | static int tmp_seq; | |
1411 | static const char *dll_filename; | |
1412 | static char *dll_symname; | |
1413 | ||
1414 | #define UNDSEC (asection *) &bfd_und_section | |
1415 | ||
1416 | static asection * | |
d643799d | 1417 | quick_section (abfd, name, flags, align) |
252b5132 RH |
1418 | bfd *abfd; |
1419 | const char *name; | |
1420 | int flags; | |
1421 | int align; | |
1422 | { | |
1423 | asection *sec; | |
1424 | asymbol *sym; | |
1425 | ||
1426 | sec = bfd_make_section_old_way (abfd, name); | |
86b1cc60 | 1427 | bfd_set_section_flags (abfd, sec, flags | SEC_ALLOC | SEC_LOAD | SEC_KEEP); |
252b5132 | 1428 | bfd_set_section_alignment (abfd, sec, align); |
86b1cc60 | 1429 | /* Remember to undo this before trying to link internally! */ |
252b5132 RH |
1430 | sec->output_section = sec; |
1431 | ||
1432 | sym = bfd_make_empty_symbol (abfd); | |
1433 | symtab[symptr++] = sym; | |
1434 | sym->name = sec->name; | |
1435 | sym->section = sec; | |
1436 | sym->flags = BSF_LOCAL; | |
1437 | sym->value = 0; | |
1438 | ||
1439 | return sec; | |
1440 | } | |
1441 | ||
1442 | static void | |
1443 | quick_symbol (abfd, n1, n2, n3, sec, flags, addr) | |
1444 | bfd *abfd; | |
1445 | char *n1; | |
1446 | char *n2; | |
1447 | char *n3; | |
1448 | asection *sec; | |
1449 | int flags; | |
1450 | int addr; | |
1451 | { | |
1452 | asymbol *sym; | |
1453 | char *name = (char *) xmalloc (strlen (n1) + strlen (n2) + strlen (n3) + 1); | |
1454 | strcpy (name, n1); | |
1455 | strcat (name, n2); | |
1456 | strcat (name, n3); | |
1457 | sym = bfd_make_empty_symbol (abfd); | |
1458 | sym->name = name; | |
1459 | sym->section = sec; | |
1460 | sym->flags = flags; | |
1461 | sym->value = addr; | |
1462 | symtab[symptr++] = sym; | |
1463 | } | |
1464 | ||
1465 | static arelent *reltab = 0; | |
1466 | static int relcount = 0, relsize = 0; | |
1467 | ||
1468 | static void | |
1469 | quick_reloc (abfd, address, which_howto, symidx) | |
1470 | bfd *abfd; | |
1471 | int address; | |
1472 | int which_howto; | |
1473 | int symidx; | |
1474 | { | |
d643799d | 1475 | if (relcount >= (relsize - 1)) |
252b5132 RH |
1476 | { |
1477 | relsize += 10; | |
1478 | if (reltab) | |
1479 | reltab = (arelent *) xrealloc (reltab, relsize * sizeof (arelent)); | |
1480 | else | |
1481 | reltab = (arelent *) xmalloc (relsize * sizeof (arelent)); | |
1482 | } | |
1483 | reltab[relcount].address = address; | |
1484 | reltab[relcount].addend = 0; | |
1485 | reltab[relcount].howto = bfd_reloc_type_lookup (abfd, which_howto); | |
1486 | reltab[relcount].sym_ptr_ptr = symtab + symidx; | |
1487 | relcount++; | |
1488 | } | |
1489 | ||
1490 | static void | |
1491 | save_relocs (asection *sec) | |
1492 | { | |
1493 | int i; | |
1494 | sec->relocation = reltab; | |
1495 | sec->reloc_count = relcount; | |
d643799d KH |
1496 | sec->orelocation = (arelent **) xmalloc ((relcount + 1) * sizeof (arelent *)); |
1497 | for (i = 0; i < relcount; i++) | |
252b5132 RH |
1498 | sec->orelocation[i] = sec->relocation + i; |
1499 | sec->orelocation[relcount] = 0; | |
1500 | sec->flags |= SEC_RELOC; | |
1501 | reltab = 0; | |
1502 | relcount = relsize = 0; | |
1503 | } | |
1504 | ||
1505 | /* | |
1506 | * .section .idata$2 | |
1507 | * .global __head_my_dll | |
1508 | * __head_my_dll: | |
1509 | * .rva hname | |
1510 | * .long 0 | |
1511 | * .long 0 | |
1512 | * .rva __my_dll_iname | |
1513 | * .rva fthunk | |
1514 | * | |
1515 | * .section .idata$5 | |
1516 | * .long 0 | |
1517 | * fthunk: | |
1518 | * | |
1519 | * .section .idata$4 | |
1520 | * .long 0 | |
1521 | * hname: | |
1522 | */ | |
1523 | ||
1524 | static bfd * | |
1525 | make_head (parent) | |
1526 | bfd *parent; | |
1527 | { | |
1528 | asection *id2, *id5, *id4; | |
1529 | unsigned char *d2, *d5, *d4; | |
1530 | char *oname; | |
1531 | bfd *abfd; | |
1532 | ||
1533 | oname = (char *) xmalloc (20); | |
1534 | sprintf (oname, "d%06d.o", tmp_seq); | |
1535 | tmp_seq++; | |
1536 | ||
1537 | abfd = bfd_create (oname, parent); | |
c6c37250 | 1538 | bfd_find_target (pe_details->object_target, abfd); |
252b5132 RH |
1539 | bfd_make_writable (abfd); |
1540 | ||
1541 | bfd_set_format (abfd, bfd_object); | |
c6c37250 | 1542 | bfd_set_arch_mach (abfd, pe_details->bfd_arch, 0); |
252b5132 RH |
1543 | |
1544 | symptr = 0; | |
1545 | symtab = (asymbol **) xmalloc (6 * sizeof (asymbol *)); | |
1546 | id2 = quick_section (abfd, ".idata$2", SEC_HAS_CONTENTS, 2); | |
1547 | id5 = quick_section (abfd, ".idata$5", SEC_HAS_CONTENTS, 2); | |
1548 | id4 = quick_section (abfd, ".idata$4", SEC_HAS_CONTENTS, 2); | |
d643799d KH |
1549 | quick_symbol (abfd, U ("_head_"), dll_symname, "", id2, BSF_GLOBAL, 0); |
1550 | quick_symbol (abfd, U (""), dll_symname, "_iname", UNDSEC, BSF_GLOBAL, 0); | |
c6c37250 DD |
1551 | |
1552 | /* OK, pay attention here. I got confused myself looking back at | |
1553 | it. We create a four-byte section to mark the beginning of the | |
1554 | list, and we include an offset of 4 in the section, so that the | |
1555 | pointer to the list points to the *end* of this section, which is | |
5cc18311 | 1556 | the start of the list of sections from other objects. */ |
252b5132 RH |
1557 | |
1558 | bfd_set_section_size (abfd, id2, 20); | |
1559 | d2 = (unsigned char *) xmalloc (20); | |
1560 | id2->contents = d2; | |
1561 | memset (d2, 0, 20); | |
1562 | d2[0] = d2[16] = 4; /* reloc addend */ | |
1563 | quick_reloc (abfd, 0, BFD_RELOC_RVA, 2); | |
1564 | quick_reloc (abfd, 12, BFD_RELOC_RVA, 4); | |
1565 | quick_reloc (abfd, 16, BFD_RELOC_RVA, 1); | |
1566 | save_relocs (id2); | |
1567 | ||
1568 | bfd_set_section_size (abfd, id5, 4); | |
1569 | d5 = (unsigned char *) xmalloc (4); | |
1570 | id5->contents = d5; | |
1571 | memset (d5, 0, 4); | |
1572 | ||
1573 | bfd_set_section_size (abfd, id4, 4); | |
1574 | d4 = (unsigned char *) xmalloc (4); | |
1575 | id4->contents = d4; | |
1576 | memset (d4, 0, 4); | |
1577 | ||
1578 | bfd_set_symtab (abfd, symtab, symptr); | |
1579 | ||
1580 | bfd_set_section_contents (abfd, id2, d2, 0, 20); | |
1581 | bfd_set_section_contents (abfd, id5, d5, 0, 4); | |
1582 | bfd_set_section_contents (abfd, id4, d4, 0, 4); | |
5cc18311 | 1583 | |
252b5132 RH |
1584 | bfd_make_readable (abfd); |
1585 | return abfd; | |
1586 | } | |
1587 | ||
1588 | /* | |
1589 | * .section .idata$4 | |
1590 | * .long 0 | |
1591 | * .section .idata$5 | |
1592 | * .long 0 | |
1593 | * .section idata$7 | |
1594 | * .global __my_dll_iname | |
1595 | *__my_dll_iname: | |
1596 | * .asciz "my.dll" | |
1597 | */ | |
1598 | ||
1599 | static bfd * | |
1600 | make_tail (parent) | |
1601 | bfd *parent; | |
1602 | { | |
1603 | asection *id4, *id5, *id7; | |
1604 | unsigned char *d4, *d5, *d7; | |
1605 | int len; | |
1606 | char *oname; | |
1607 | bfd *abfd; | |
1608 | ||
1609 | oname = (char *) xmalloc (20); | |
1610 | sprintf (oname, "d%06d.o", tmp_seq); | |
1611 | tmp_seq++; | |
1612 | ||
1613 | abfd = bfd_create (oname, parent); | |
c6c37250 | 1614 | bfd_find_target (pe_details->object_target, abfd); |
252b5132 RH |
1615 | bfd_make_writable (abfd); |
1616 | ||
1617 | bfd_set_format (abfd, bfd_object); | |
c6c37250 | 1618 | bfd_set_arch_mach (abfd, pe_details->bfd_arch, 0); |
252b5132 RH |
1619 | |
1620 | symptr = 0; | |
1621 | symtab = (asymbol **) xmalloc (5 * sizeof (asymbol *)); | |
1622 | id4 = quick_section (abfd, ".idata$4", SEC_HAS_CONTENTS, 2); | |
1623 | id5 = quick_section (abfd, ".idata$5", SEC_HAS_CONTENTS, 2); | |
1624 | id7 = quick_section (abfd, ".idata$7", SEC_HAS_CONTENTS, 2); | |
d643799d | 1625 | quick_symbol (abfd, U (""), dll_symname, "_iname", id7, BSF_GLOBAL, 0); |
252b5132 RH |
1626 | |
1627 | bfd_set_section_size (abfd, id4, 4); | |
1628 | d4 = (unsigned char *) xmalloc (4); | |
1629 | id4->contents = d4; | |
1630 | memset (d4, 0, 4); | |
1631 | ||
1632 | bfd_set_section_size (abfd, id5, 4); | |
1633 | d5 = (unsigned char *) xmalloc (4); | |
1634 | id5->contents = d5; | |
1635 | memset (d5, 0, 4); | |
1636 | ||
d643799d | 1637 | len = strlen (dll_filename) + 1; |
252b5132 | 1638 | if (len & 1) |
d643799d | 1639 | len++; |
252b5132 RH |
1640 | bfd_set_section_size (abfd, id7, len); |
1641 | d7 = (unsigned char *) xmalloc (len); | |
1642 | id7->contents = d7; | |
1643 | strcpy (d7, dll_filename); | |
1644 | ||
1645 | bfd_set_symtab (abfd, symtab, symptr); | |
1646 | ||
1647 | bfd_set_section_contents (abfd, id4, d4, 0, 4); | |
1648 | bfd_set_section_contents (abfd, id5, d5, 0, 4); | |
1649 | bfd_set_section_contents (abfd, id7, d7, 0, len); | |
1650 | ||
1651 | bfd_make_readable (abfd); | |
1652 | return abfd; | |
1653 | } | |
1654 | ||
1655 | /* | |
1656 | * .text | |
1657 | * .global _function | |
1658 | * .global ___imp_function | |
1659 | * .global __imp__function | |
1660 | *_function: | |
1661 | * jmp *__imp__function: | |
1662 | * | |
1663 | * .section idata$7 | |
1664 | * .long __head_my_dll | |
1665 | * | |
1666 | * .section .idata$5 | |
1667 | *___imp_function: | |
1668 | *__imp__function: | |
1669 | *iat? | |
1670 | * .section .idata$4 | |
1671 | *iat? | |
1672 | * .section .idata$6 | |
1673 | *ID<ordinal>: | |
1674 | * .short <hint> | |
1675 | * .asciz "function" xlate? (add underscore, kill at) | |
1676 | */ | |
1677 | ||
1678 | static unsigned char jmp_ix86_bytes[] = { | |
1679 | 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, 0x90, 0x90 | |
1680 | }; | |
1681 | ||
344a211f NC |
1682 | /* |
1683 | *_function: | |
1684 | * mov.l ip+8,r0 | |
1685 | * mov.l @r0,r0 | |
1686 | * jmp @r0 | |
1687 | * nop | |
1688 | * .dw __imp_function | |
1689 | */ | |
1690 | ||
1691 | static unsigned char jmp_sh_bytes[] = { | |
1692 | 0x01, 0xd0, 0x02, 0x60, 0x2b, 0x40, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00 | |
1693 | }; | |
1694 | ||
1695 | /* | |
1696 | *_function: | |
1697 | * lui $t0,<high:__imp_function> | |
1698 | * lw $t0,<low:__imp_function> | |
1699 | * jr $t0 | |
1700 | * nop | |
1701 | */ | |
1702 | ||
1703 | static unsigned char jmp_mips_bytes[] = { | |
1704 | 0x00, 0x00, 0x08, 0x3c, 0x00, 0x00, 0x08, 0x8d, | |
1705 | 0x08, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00 | |
1706 | }; | |
252b5132 RH |
1707 | |
1708 | static bfd * | |
1709 | make_one (exp, parent) | |
1710 | def_file_export *exp; | |
1711 | bfd *parent; | |
1712 | { | |
1713 | asection *tx, *id7, *id5, *id4, *id6; | |
23a87948 | 1714 | unsigned char *td = NULL, *d7, *d5, *d4, *d6 = NULL; |
252b5132 RH |
1715 | int len; |
1716 | char *oname; | |
1717 | bfd *abfd; | |
f0c87f88 NC |
1718 | unsigned char *jmp_bytes = NULL; |
1719 | int jmp_byte_count = 0; | |
c6c37250 DD |
1720 | |
1721 | switch (pe_details->pe_arch) | |
1722 | { | |
1723 | case PE_ARCH_i386: | |
1724 | jmp_bytes = jmp_ix86_bytes; | |
1725 | jmp_byte_count = sizeof (jmp_ix86_bytes); | |
1726 | break; | |
344a211f NC |
1727 | case PE_ARCH_sh: |
1728 | jmp_bytes = jmp_sh_bytes; | |
1729 | jmp_byte_count = sizeof (jmp_sh_bytes); | |
1730 | break; | |
1731 | case PE_ARCH_mips: | |
1732 | jmp_bytes = jmp_mips_bytes; | |
1733 | jmp_byte_count = sizeof (jmp_mips_bytes); | |
1734 | break; | |
c6c37250 | 1735 | } |
252b5132 RH |
1736 | |
1737 | oname = (char *) xmalloc (20); | |
1738 | sprintf (oname, "d%06d.o", tmp_seq); | |
1739 | tmp_seq++; | |
1740 | ||
1741 | abfd = bfd_create (oname, parent); | |
c6c37250 | 1742 | bfd_find_target (pe_details->object_target, abfd); |
252b5132 RH |
1743 | bfd_make_writable (abfd); |
1744 | ||
1745 | bfd_set_format (abfd, bfd_object); | |
c6c37250 | 1746 | bfd_set_arch_mach (abfd, pe_details->bfd_arch, 0); |
252b5132 RH |
1747 | |
1748 | symptr = 0; | |
b044cda1 | 1749 | symtab = (asymbol **) xmalloc (11 * sizeof (asymbol *)); |
252b5132 RH |
1750 | tx = quick_section (abfd, ".text", SEC_CODE|SEC_HAS_CONTENTS, 2); |
1751 | id7 = quick_section (abfd, ".idata$7", SEC_HAS_CONTENTS, 2); | |
1752 | id5 = quick_section (abfd, ".idata$5", SEC_HAS_CONTENTS, 2); | |
1753 | id4 = quick_section (abfd, ".idata$4", SEC_HAS_CONTENTS, 2); | |
1754 | id6 = quick_section (abfd, ".idata$6", SEC_HAS_CONTENTS, 2); | |
7c9e78f8 | 1755 | if (! exp->flag_data) |
d643799d KH |
1756 | quick_symbol (abfd, U (""), exp->internal_name, "", tx, BSF_GLOBAL, 0); |
1757 | quick_symbol (abfd, U ("_head_"), dll_symname, "", UNDSEC, BSF_GLOBAL, 0); | |
1758 | quick_symbol (abfd, U ("_imp__"), exp->internal_name, "", id5, BSF_GLOBAL, 0); | |
b044cda1 CW |
1759 | /* symbol to reference ord/name of imported symbol, used to implement |
1760 | auto-import */ | |
1761 | quick_symbol (abfd, U("_nm__"), exp->internal_name, "", id6, BSF_GLOBAL, 0); | |
870df5dc | 1762 | if (pe_dll_compat_implib) |
d643799d KH |
1763 | quick_symbol (abfd, U ("__imp_"), exp->internal_name, "", |
1764 | id5, BSF_GLOBAL, 0); | |
252b5132 | 1765 | |
23a87948 DD |
1766 | if (! exp->flag_data) |
1767 | { | |
1768 | bfd_set_section_size (abfd, tx, jmp_byte_count); | |
1769 | td = (unsigned char *) xmalloc (jmp_byte_count); | |
1770 | tx->contents = td; | |
1771 | memcpy (td, jmp_bytes, jmp_byte_count); | |
1772 | switch (pe_details->pe_arch) | |
1773 | { | |
1774 | case PE_ARCH_i386: | |
1775 | quick_reloc (abfd, 2, BFD_RELOC_32, 2); | |
1776 | break; | |
1777 | case PE_ARCH_sh: | |
1778 | quick_reloc (abfd, 8, BFD_RELOC_32, 2); | |
1779 | break; | |
1780 | case PE_ARCH_mips: | |
1781 | quick_reloc (abfd, 0, BFD_RELOC_HI16_S, 2); | |
1782 | quick_reloc (abfd, 0, BFD_RELOC_LO16, 0); /* MIPS_R_PAIR */ | |
1783 | quick_reloc (abfd, 4, BFD_RELOC_LO16, 2); | |
1784 | break; | |
1785 | } | |
1786 | save_relocs (tx); | |
1787 | } | |
252b5132 RH |
1788 | |
1789 | bfd_set_section_size (abfd, id7, 4); | |
1790 | d7 = (unsigned char *) xmalloc (4); | |
1791 | id7->contents = d7; | |
1792 | memset (d7, 0, 4); | |
1793 | quick_reloc (abfd, 0, BFD_RELOC_RVA, 6); | |
1794 | save_relocs (id7); | |
1795 | ||
1796 | bfd_set_section_size (abfd, id5, 4); | |
1797 | d5 = (unsigned char *) xmalloc (4); | |
1798 | id5->contents = d5; | |
1799 | memset (d5, 0, 4); | |
1800 | if (exp->flag_noname) | |
1801 | { | |
1802 | d5[0] = exp->ordinal; | |
1803 | d5[1] = exp->ordinal >> 8; | |
1804 | d5[3] = 0x80; | |
1805 | } | |
1806 | else | |
1807 | { | |
1808 | quick_reloc (abfd, 0, BFD_RELOC_RVA, 4); | |
1809 | save_relocs (id5); | |
1810 | } | |
1811 | ||
1812 | bfd_set_section_size (abfd, id4, 4); | |
1813 | d4 = (unsigned char *) xmalloc (4); | |
1814 | id4->contents = d4; | |
1815 | memset (d4, 0, 4); | |
1816 | if (exp->flag_noname) | |
1817 | { | |
c2a94a7a DD |
1818 | d4[0] = exp->ordinal; |
1819 | d4[1] = exp->ordinal >> 8; | |
1820 | d4[3] = 0x80; | |
252b5132 RH |
1821 | } |
1822 | else | |
1823 | { | |
1824 | quick_reloc (abfd, 0, BFD_RELOC_RVA, 4); | |
1825 | save_relocs (id4); | |
1826 | } | |
1827 | ||
1828 | if (exp->flag_noname) | |
1829 | { | |
1830 | len = 0; | |
1831 | bfd_set_section_size (abfd, id6, 0); | |
1832 | } | |
1833 | else | |
1834 | { | |
1835 | len = strlen (exp->name) + 3; | |
1836 | if (len & 1) | |
1837 | len++; | |
1838 | bfd_set_section_size (abfd, id6, len); | |
1839 | d6 = (unsigned char *) xmalloc (len); | |
1840 | id6->contents = d6; | |
1841 | memset (d6, 0, len); | |
1842 | d6[0] = exp->hint & 0xff; | |
1843 | d6[1] = exp->hint >> 8; | |
d643799d | 1844 | strcpy (d6 + 2, exp->name); |
252b5132 RH |
1845 | } |
1846 | ||
1847 | bfd_set_symtab (abfd, symtab, symptr); | |
1848 | ||
c6c37250 | 1849 | bfd_set_section_contents (abfd, tx, td, 0, jmp_byte_count); |
252b5132 RH |
1850 | bfd_set_section_contents (abfd, id7, d7, 0, 4); |
1851 | bfd_set_section_contents (abfd, id5, d5, 0, 4); | |
1852 | bfd_set_section_contents (abfd, id4, d4, 0, 4); | |
1853 | if (!exp->flag_noname) | |
1854 | bfd_set_section_contents (abfd, id6, d6, 0, len); | |
1855 | ||
1856 | bfd_make_readable (abfd); | |
1857 | return abfd; | |
1858 | } | |
1859 | ||
b044cda1 CW |
1860 | static bfd * |
1861 | make_singleton_name_thunk (import, parent) | |
1862 | char *import; | |
1863 | bfd *parent; | |
1864 | { | |
1865 | /* name thunks go to idata$4 */ | |
1866 | ||
1867 | asection *id4; | |
1868 | unsigned char *d4; | |
1869 | char *oname; | |
1870 | bfd *abfd; | |
1871 | ||
1872 | oname = (char *) xmalloc (20); | |
1873 | sprintf (oname, "nmth%06d.o", tmp_seq); | |
1874 | tmp_seq++; | |
1875 | ||
1876 | abfd = bfd_create (oname, parent); | |
1877 | bfd_find_target (pe_details->object_target, abfd); | |
1878 | bfd_make_writable (abfd); | |
1879 | ||
1880 | bfd_set_format (abfd, bfd_object); | |
1881 | bfd_set_arch_mach (abfd, pe_details->bfd_arch, 0); | |
1882 | ||
1883 | symptr = 0; | |
1884 | symtab = (asymbol **) xmalloc (3 * sizeof (asymbol *)); | |
1885 | id4 = quick_section (abfd, ".idata$4", SEC_HAS_CONTENTS, 2); | |
1886 | quick_symbol (abfd, U ("_nm_thnk_"), import, "", id4, BSF_GLOBAL, 0); | |
1887 | quick_symbol (abfd, U ("_nm_"), import, "", UNDSEC, BSF_GLOBAL, 0); | |
1888 | ||
1889 | bfd_set_section_size (abfd, id4, 8); | |
1890 | d4 = (unsigned char *) xmalloc (4); | |
1891 | id4->contents = d4; | |
1892 | memset (d4, 0, 8); | |
1893 | quick_reloc (abfd, 0, BFD_RELOC_RVA, 2); | |
1894 | save_relocs (id4); | |
1895 | ||
1896 | bfd_set_symtab (abfd, symtab, symptr); | |
1897 | ||
1898 | bfd_set_section_contents (abfd, id4, d4, 0, 8); | |
1899 | ||
1900 | bfd_make_readable (abfd); | |
1901 | return abfd; | |
1902 | } | |
1903 | ||
1904 | static char * | |
1905 | make_import_fixup_mark (rel) | |
1906 | arelent *rel; | |
1907 | { | |
1908 | /* we convert reloc to symbol, for later reference */ | |
1909 | static int counter; | |
1910 | static char *fixup_name = NULL; | |
1911 | static int buffer_len = 0; | |
1912 | ||
1913 | struct symbol_cache_entry *sym = *rel->sym_ptr_ptr; | |
1914 | ||
1915 | bfd *abfd = bfd_asymbol_bfd (sym); | |
1916 | struct coff_link_hash_entry *myh = NULL; | |
1917 | ||
1918 | if (!fixup_name) | |
1919 | { | |
1920 | fixup_name = (char *) xmalloc (384); | |
1921 | buffer_len = 384; | |
1922 | } | |
1923 | ||
1924 | if (strlen (sym->name) + 25 > buffer_len) | |
1925 | /* assume 25 chars for "__fu" + counter + "_". If counter is | |
1926 | bigger than 20 digits long, we've got worse problems than | |
1927 | overflowing this buffer... */ | |
1928 | { | |
1929 | free (fixup_name); | |
1930 | /* new buffer size is length of symbol, plus 25, but then | |
1931 | rounded up to the nearest multiple of 128 */ | |
1932 | buffer_len = ((strlen (sym->name) + 25) + 127) & ~127; | |
1933 | fixup_name = (char *) xmalloc (buffer_len); | |
1934 | } | |
1935 | ||
1936 | sprintf (fixup_name, "__fu%d_%s", counter++, sym->name); | |
1937 | ||
1938 | bfd_coff_link_add_one_symbol (&link_info, abfd, fixup_name, BSF_GLOBAL, | |
1939 | current_sec, /* sym->section, */ | |
1940 | rel->address, NULL, true, false, | |
1941 | (struct bfd_link_hash_entry **) &myh); | |
1942 | ||
1943 | /* | |
1944 | printf("type:%d\n",myh->type); | |
1945 | printf("%s\n",myh->root.u.def.section->name); | |
1946 | */ | |
1947 | return fixup_name; | |
1948 | } | |
1949 | ||
1950 | ||
1951 | /* | |
1952 | * .section .idata$3 | |
1953 | * .rva __nm_thnk_SYM (singleton thunk with name of func) | |
1954 | * .long 0 | |
1955 | * .long 0 | |
1956 | * .rva __my_dll_iname (name of dll) | |
1957 | * .rva __fuNN_SYM (pointer to reference (address) in text) | |
1958 | * | |
1959 | */ | |
1960 | ||
1961 | static bfd * | |
1962 | make_import_fixup_entry (name, fixup_name, dll_symname,parent) | |
1963 | char *name; | |
1964 | char *fixup_name; | |
1965 | char *dll_symname; | |
1966 | bfd *parent; | |
1967 | { | |
1968 | asection *id3; | |
1969 | unsigned char *d3; | |
1970 | char *oname; | |
1971 | bfd *abfd; | |
1972 | ||
1973 | oname = (char *) xmalloc (20); | |
1974 | sprintf (oname, "fu%06d.o", tmp_seq); | |
1975 | tmp_seq++; | |
1976 | ||
1977 | abfd = bfd_create (oname, parent); | |
1978 | bfd_find_target (pe_details->object_target, abfd); | |
1979 | bfd_make_writable (abfd); | |
1980 | ||
1981 | bfd_set_format (abfd, bfd_object); | |
1982 | bfd_set_arch_mach (abfd, pe_details->bfd_arch, 0); | |
1983 | ||
1984 | symptr = 0; | |
1985 | symtab = (asymbol **) xmalloc (6 * sizeof (asymbol *)); | |
1986 | id3 = quick_section (abfd, ".idata$3", SEC_HAS_CONTENTS, 2); | |
1987 | /* | |
1988 | quick_symbol (abfd, U("_head_"), dll_symname, "", id2, BSF_GLOBAL, 0); | |
1989 | */ | |
1990 | quick_symbol (abfd, U ("_nm_thnk_"), name, "", UNDSEC, BSF_GLOBAL, 0); | |
1991 | quick_symbol (abfd, U (""), dll_symname, "_iname", UNDSEC, BSF_GLOBAL, 0); | |
1992 | quick_symbol (abfd, "", fixup_name, "", UNDSEC, BSF_GLOBAL, 0); | |
1993 | ||
1994 | bfd_set_section_size (abfd, id3, 20); | |
1995 | d3 = (unsigned char *) xmalloc (20); | |
1996 | id3->contents = d3; | |
1997 | memset (d3, 0, 20); | |
1998 | ||
1999 | quick_reloc (abfd, 0, BFD_RELOC_RVA, 1); | |
2000 | quick_reloc (abfd, 12, BFD_RELOC_RVA, 2); | |
2001 | quick_reloc (abfd, 16, BFD_RELOC_RVA, 3); | |
2002 | save_relocs (id3); | |
2003 | ||
2004 | bfd_set_symtab (abfd, symtab, symptr); | |
2005 | ||
2006 | bfd_set_section_contents (abfd, id3, d3, 0, 20); | |
2007 | ||
2008 | bfd_make_readable (abfd); | |
2009 | return abfd; | |
2010 | } | |
2011 | ||
2012 | void | |
2013 | pe_create_import_fixup (rel) | |
2014 | arelent *rel; | |
2015 | { | |
2016 | char buf[300]; | |
2017 | struct symbol_cache_entry *sym = *rel->sym_ptr_ptr; | |
2018 | struct bfd_link_hash_entry *name_thunk_sym; | |
2019 | CONST char *name = sym->name; | |
2020 | char *fixup_name = make_import_fixup_mark (rel); | |
2021 | ||
2022 | sprintf (buf, U ("_nm_thnk_%s"), name); | |
2023 | ||
2024 | name_thunk_sym = bfd_link_hash_lookup (link_info.hash, buf, 0, 0, 1); | |
2025 | ||
2026 | if (!name_thunk_sym || name_thunk_sym->type != bfd_link_hash_defined) | |
2027 | { | |
2028 | bfd *b = make_singleton_name_thunk (name, output_bfd); | |
2029 | add_bfd_to_link (b, b->filename, &link_info); | |
2030 | ||
2031 | /* If we ever use autoimport, we have to cast text section writable */ | |
2032 | config.text_read_only = false; | |
2033 | } | |
2034 | ||
2035 | { | |
2036 | extern char *pe_data_import_dll; | |
2037 | bfd *b = make_import_fixup_entry (name, fixup_name, pe_data_import_dll, | |
2038 | output_bfd); | |
2039 | add_bfd_to_link (b, b->filename, &link_info); | |
2040 | } | |
2041 | } | |
2042 | ||
2043 | ||
252b5132 RH |
2044 | void |
2045 | pe_dll_generate_implib (def, impfilename) | |
2046 | def_file *def; | |
1069dd8d | 2047 | const char *impfilename; |
252b5132 RH |
2048 | { |
2049 | int i; | |
2050 | bfd *ar_head; | |
2051 | bfd *ar_tail; | |
2052 | bfd *outarch; | |
2053 | bfd *head = 0; | |
2054 | ||
5aaace27 | 2055 | dll_filename = (def->name) ? def->name : dll_name; |
252b5132 | 2056 | dll_symname = xstrdup (dll_filename); |
d643799d | 2057 | for (i = 0; dll_symname[i]; i++) |
252b5132 RH |
2058 | if (!isalnum ((unsigned char) dll_symname[i])) |
2059 | dll_symname[i] = '_'; | |
2060 | ||
2061 | unlink (impfilename); | |
2062 | ||
2063 | outarch = bfd_openw (impfilename, 0); | |
2064 | ||
2065 | if (!outarch) | |
2066 | { | |
2067 | /* xgettext:c-format */ | |
2068 | einfo (_("%XCan't open .lib file: %s\n"), impfilename); | |
2069 | return; | |
2070 | } | |
2071 | ||
2072 | /* xgettext:c-format */ | |
2073 | einfo (_("Creating library file: %s\n"), impfilename); | |
5cc18311 | 2074 | |
252b5132 RH |
2075 | bfd_set_format (outarch, bfd_archive); |
2076 | outarch->has_armap = 1; | |
2077 | ||
5cc18311 | 2078 | /* Work out a reasonable size of things to put onto one line. */ |
252b5132 RH |
2079 | |
2080 | ar_head = make_head (outarch); | |
252b5132 | 2081 | |
d643799d | 2082 | for (i = 0; i < def->num_exports; i++) |
252b5132 | 2083 | { |
86b1cc60 | 2084 | /* The import library doesn't know about the internal name. */ |
252b5132 RH |
2085 | char *internal = def->exports[i].internal_name; |
2086 | bfd *n; | |
2087 | def->exports[i].internal_name = def->exports[i].name; | |
d643799d | 2088 | n = make_one (def->exports + i, outarch); |
252b5132 RH |
2089 | n->next = head; |
2090 | head = n; | |
2091 | def->exports[i].internal_name = internal; | |
2092 | } | |
2093 | ||
c6c37250 DD |
2094 | ar_tail = make_tail (outarch); |
2095 | ||
2096 | if (ar_head == NULL || ar_tail == NULL) | |
2097 | return; | |
2098 | ||
86b1cc60 | 2099 | /* Now stick them all into the archive. */ |
252b5132 RH |
2100 | |
2101 | ar_head->next = head; | |
2102 | ar_tail->next = ar_head; | |
2103 | head = ar_tail; | |
2104 | ||
2105 | if (! bfd_set_archive_head (outarch, head)) | |
2106 | einfo ("%Xbfd_set_archive_head: %s\n", bfd_errmsg (bfd_get_error ())); | |
5cc18311 | 2107 | |
252b5132 RH |
2108 | if (! bfd_close (outarch)) |
2109 | einfo ("%Xbfd_close %s: %s\n", impfilename, bfd_errmsg (bfd_get_error ())); | |
2110 | ||
2111 | while (head != NULL) | |
2112 | { | |
2113 | bfd *n = head->next; | |
2114 | bfd_close (head); | |
2115 | head = n; | |
2116 | } | |
2117 | } | |
2118 | ||
2119 | static void | |
2120 | add_bfd_to_link (abfd, name, link_info) | |
2121 | bfd *abfd; | |
b044cda1 | 2122 | CONST char *name; |
252b5132 RH |
2123 | struct bfd_link_info *link_info; |
2124 | { | |
2125 | lang_input_statement_type *fake_file; | |
2126 | fake_file = lang_add_input_file (name, | |
2127 | lang_input_file_is_fake_enum, | |
2128 | NULL); | |
2129 | fake_file->the_bfd = abfd; | |
2130 | ldlang_add_file (fake_file); | |
2131 | if (!bfd_link_add_symbols (abfd, link_info)) | |
2132 | einfo ("%Xaddsym %s: %s\n", name, bfd_errmsg (bfd_get_error ())); | |
2133 | } | |
2134 | ||
2135 | void | |
2136 | pe_process_import_defs (output_bfd, link_info) | |
2137 | bfd *output_bfd; | |
2138 | struct bfd_link_info *link_info; | |
2139 | { | |
2140 | def_file_module *module; | |
d643799d | 2141 | pe_dll_id_target (bfd_get_target (output_bfd)); |
252b5132 RH |
2142 | |
2143 | if (!pe_def_file) | |
2144 | return; | |
2145 | ||
2146 | for (module = pe_def_file->modules; module; module = module->next) | |
2147 | { | |
2148 | int i, do_this_dll; | |
2149 | ||
2150 | dll_filename = module->name; | |
2151 | dll_symname = xstrdup (module->name); | |
d643799d | 2152 | for (i = 0; dll_symname[i]; i++) |
252b5132 RH |
2153 | if (!isalnum (dll_symname[i])) |
2154 | dll_symname[i] = '_'; | |
2155 | ||
2156 | do_this_dll = 0; | |
2157 | ||
d643799d | 2158 | for (i = 0; i < pe_def_file->num_imports; i++) |
252b5132 RH |
2159 | if (pe_def_file->imports[i].module == module) |
2160 | { | |
2161 | def_file_export exp; | |
2162 | struct bfd_link_hash_entry *blhe; | |
2163 | ||
86b1cc60 | 2164 | /* See if we need this import. */ |
874c8c99 | 2165 | char *name = (char *) xmalloc (strlen (pe_def_file->imports[i].internal_name) + 2 + 6); |
d643799d | 2166 | sprintf (name, "%s%s", U (""), pe_def_file->imports[i].internal_name); |
252b5132 RH |
2167 | blhe = bfd_link_hash_lookup (link_info->hash, name, |
2168 | false, false, false); | |
874c8c99 DD |
2169 | if (!blhe || (blhe && blhe->type != bfd_link_hash_undefined)) |
2170 | { | |
d643799d | 2171 | sprintf (name, "%s%s", U ("_imp__"), |
874c8c99 DD |
2172 | pe_def_file->imports[i].internal_name); |
2173 | blhe = bfd_link_hash_lookup (link_info->hash, name, | |
2174 | false, false, false); | |
2175 | } | |
252b5132 RH |
2176 | free (name); |
2177 | if (blhe && blhe->type == bfd_link_hash_undefined) | |
2178 | { | |
2179 | bfd *one; | |
86b1cc60 | 2180 | /* We do. */ |
252b5132 RH |
2181 | if (!do_this_dll) |
2182 | { | |
2183 | bfd *ar_head = make_head (output_bfd); | |
2184 | add_bfd_to_link (ar_head, ar_head->filename, link_info); | |
2185 | do_this_dll = 1; | |
2186 | } | |
2187 | exp.internal_name = pe_def_file->imports[i].internal_name; | |
2188 | exp.name = pe_def_file->imports[i].name; | |
2189 | exp.ordinal = pe_def_file->imports[i].ordinal; | |
2190 | exp.hint = exp.ordinal >= 0 ? exp.ordinal : 0; | |
2191 | exp.flag_private = 0; | |
2192 | exp.flag_constant = 0; | |
2193 | exp.flag_data = 0; | |
2194 | exp.flag_noname = exp.name ? 0 : 1; | |
2195 | one = make_one (&exp, output_bfd); | |
2196 | add_bfd_to_link (one, one->filename, link_info); | |
2197 | } | |
2198 | } | |
2199 | if (do_this_dll) | |
2200 | { | |
2201 | bfd *ar_tail = make_tail (output_bfd); | |
2202 | add_bfd_to_link (ar_tail, ar_tail->filename, link_info); | |
2203 | } | |
2204 | ||
2205 | free (dll_symname); | |
2206 | } | |
2207 | } | |
2208 | ||
2209 | /************************************************************************ | |
2210 | ||
2211 | We were handed a *.DLL file. Parse it and turn it into a set of | |
2212 | IMPORTS directives in the def file. Return true if the file was | |
2213 | handled, false if not. | |
2214 | ||
2215 | ************************************************************************/ | |
2216 | ||
2217 | static unsigned int | |
2218 | pe_get16 (abfd, where) | |
2219 | bfd *abfd; | |
2220 | int where; | |
2221 | { | |
2222 | unsigned char b[2]; | |
2223 | bfd_seek (abfd, where, SEEK_SET); | |
2224 | bfd_read (b, 1, 2, abfd); | |
d643799d | 2225 | return b[0] + (b[1] << 8); |
252b5132 RH |
2226 | } |
2227 | ||
2228 | static unsigned int | |
2229 | pe_get32 (abfd, where) | |
2230 | bfd *abfd; | |
2231 | int where; | |
2232 | { | |
2233 | unsigned char b[4]; | |
2234 | bfd_seek (abfd, where, SEEK_SET); | |
2235 | bfd_read (b, 1, 4, abfd); | |
d643799d | 2236 | return b[0] + (b[1] << 8) + (b[2] << 16) + (b[3] << 24); |
252b5132 RH |
2237 | } |
2238 | ||
2239 | #if 0 /* This is not currently used. */ | |
2240 | ||
2241 | static unsigned int | |
2242 | pe_as16 (ptr) | |
2243 | void *ptr; | |
2244 | { | |
2245 | unsigned char *b = ptr; | |
d643799d | 2246 | return b[0] + (b[1] << 8); |
252b5132 RH |
2247 | } |
2248 | ||
2249 | #endif | |
2250 | ||
2251 | static unsigned int | |
2252 | pe_as32 (ptr) | |
2253 | void *ptr; | |
2254 | { | |
2255 | unsigned char *b = ptr; | |
d643799d | 2256 | return b[0] + (b[1] << 8) + (b[2] << 16) + (b[3] << 24); |
252b5132 RH |
2257 | } |
2258 | ||
2259 | boolean | |
2260 | pe_implied_import_dll (filename) | |
1069dd8d | 2261 | const char *filename; |
252b5132 RH |
2262 | { |
2263 | bfd *dll; | |
2264 | unsigned long pe_header_offset, opthdr_ofs, num_entries, i; | |
2265 | unsigned long export_rva, export_size, nsections, secptr, expptr; | |
2266 | unsigned char *expdata, *erva; | |
2267 | unsigned long name_rvas, ordinals, nexp, ordbase; | |
1069dd8d | 2268 | const char *dll_name; |
252b5132 RH |
2269 | |
2270 | /* No, I can't use bfd here. kernel32.dll puts its export table in | |
5cc18311 | 2271 | the middle of the .rdata section. */ |
252b5132 | 2272 | |
c6c37250 | 2273 | dll = bfd_openr (filename, pe_details->target_name); |
252b5132 RH |
2274 | if (!dll) |
2275 | { | |
2276 | einfo ("%Xopen %s: %s\n", filename, bfd_errmsg (bfd_get_error ())); | |
2277 | return false; | |
2278 | } | |
86b1cc60 | 2279 | /* PEI dlls seem to be bfd_objects. */ |
252b5132 RH |
2280 | if (!bfd_check_format (dll, bfd_object)) |
2281 | { | |
2282 | einfo ("%X%s: this doesn't appear to be a DLL\n", filename); | |
2283 | return false; | |
2284 | } | |
2285 | ||
2286 | dll_name = filename; | |
d643799d | 2287 | for (i = 0; filename[i]; i++) |
252b5132 RH |
2288 | if (filename[i] == '/' || filename[i] == '\\' || filename[i] == ':') |
2289 | dll_name = filename + i + 1; | |
2290 | ||
2291 | pe_header_offset = pe_get32 (dll, 0x3c); | |
2292 | opthdr_ofs = pe_header_offset + 4 + 20; | |
2293 | num_entries = pe_get32 (dll, opthdr_ofs + 92); | |
2294 | if (num_entries < 1) /* no exports */ | |
2295 | return false; | |
2296 | export_rva = pe_get32 (dll, opthdr_ofs + 96); | |
2297 | export_size = pe_get32 (dll, opthdr_ofs + 100); | |
2298 | nsections = pe_get16 (dll, pe_header_offset + 4 + 2); | |
2299 | secptr = (pe_header_offset + 4 + 20 + | |
2300 | pe_get16 (dll, pe_header_offset + 4 + 16)); | |
2301 | expptr = 0; | |
d643799d | 2302 | for (i = 0; i < nsections; i++) |
252b5132 RH |
2303 | { |
2304 | char sname[8]; | |
2305 | unsigned long secptr1 = secptr + 40 * i; | |
2306 | unsigned long vaddr = pe_get32 (dll, secptr1 + 12); | |
2307 | unsigned long vsize = pe_get32 (dll, secptr1 + 16); | |
2308 | unsigned long fptr = pe_get32 (dll, secptr1 + 20); | |
d643799d KH |
2309 | bfd_seek (dll, secptr1, SEEK_SET); |
2310 | bfd_read (sname, 1, 8, dll); | |
2311 | if (vaddr <= export_rva && vaddr + vsize > export_rva) | |
252b5132 RH |
2312 | { |
2313 | expptr = fptr + (export_rva - vaddr); | |
2314 | if (export_rva + export_size > vaddr + vsize) | |
2315 | export_size = vsize - (export_rva - vaddr); | |
2316 | break; | |
2317 | } | |
2318 | } | |
2319 | ||
2320 | expdata = (unsigned char *) xmalloc (export_size); | |
2321 | bfd_seek (dll, expptr, SEEK_SET); | |
2322 | bfd_read (expdata, 1, export_size, dll); | |
2323 | erva = expdata - export_rva; | |
2324 | ||
2325 | if (pe_def_file == 0) | |
d643799d | 2326 | pe_def_file = def_file_empty (); |
252b5132 | 2327 | |
d643799d KH |
2328 | nexp = pe_as32 (expdata + 24); |
2329 | name_rvas = pe_as32 (expdata + 32); | |
2330 | ordinals = pe_as32 (expdata + 36); | |
2331 | ordbase = pe_as32 (expdata + 16); | |
2332 | for (i = 0; i < nexp; i++) | |
252b5132 | 2333 | { |
d643799d | 2334 | unsigned long name_rva = pe_as32 (erva + name_rvas + i * 4); |
252b5132 | 2335 | def_file_import *imp; |
d643799d | 2336 | imp = def_file_add_import (pe_def_file, erva + name_rva, dll_name, |
252b5132 RH |
2337 | i, 0); |
2338 | } | |
2339 | ||
2340 | return true; | |
2341 | } | |
2342 | ||
2343 | /************************************************************************ | |
2344 | ||
2345 | These are the main functions, called from the emulation. The first | |
2346 | is called after the bfds are read, so we can guess at how much space | |
2347 | we need. The second is called after everything is placed, so we | |
2348 | can put the right values in place. | |
2349 | ||
2350 | ************************************************************************/ | |
2351 | ||
2352 | void | |
2353 | pe_dll_build_sections (abfd, info) | |
2354 | bfd *abfd; | |
2355 | struct bfd_link_info *info; | |
2356 | { | |
c6c37250 | 2357 | pe_dll_id_target (bfd_get_target (abfd)); |
252b5132 RH |
2358 | process_def_file (abfd, info); |
2359 | ||
2360 | generate_edata (abfd, info); | |
c6c37250 DD |
2361 | build_filler_bfd (1); |
2362 | } | |
2363 | ||
2364 | void | |
2365 | pe_exe_build_sections (abfd, info) | |
2366 | bfd *abfd; | |
1069dd8d | 2367 | struct bfd_link_info *info ATTRIBUTE_UNUSED; |
c6c37250 DD |
2368 | { |
2369 | pe_dll_id_target (bfd_get_target (abfd)); | |
2370 | build_filler_bfd (0); | |
252b5132 RH |
2371 | } |
2372 | ||
2373 | void | |
2374 | pe_dll_fill_sections (abfd, info) | |
2375 | bfd *abfd; | |
2376 | struct bfd_link_info *info; | |
2377 | { | |
c6c37250 | 2378 | pe_dll_id_target (bfd_get_target (abfd)); |
252b5132 RH |
2379 | image_base = pe_data (abfd)->pe_opthdr.ImageBase; |
2380 | ||
2381 | generate_reloc (abfd, info); | |
2382 | if (reloc_sz > 0) | |
2383 | { | |
2384 | bfd_set_section_size (filler_bfd, reloc_s, reloc_sz); | |
2385 | ||
2386 | /* Resize the sections. */ | |
2387 | lang_size_sections (stat_ptr->head, abs_output_section, | |
ae7fb08f | 2388 | &stat_ptr->head, 0, (bfd_vma) 0, NULL); |
252b5132 RH |
2389 | |
2390 | /* Redo special stuff. */ | |
2391 | ldemul_after_allocation (); | |
2392 | ||
2393 | /* Do the assignments again. */ | |
2394 | lang_do_assignments (stat_ptr->head, | |
2395 | abs_output_section, | |
2396 | (fill_type) 0, (bfd_vma) 0); | |
2397 | } | |
2398 | ||
2399 | fill_edata (abfd, info); | |
2400 | ||
2401 | pe_data (abfd)->dll = 1; | |
2402 | ||
2403 | edata_s->contents = edata_d; | |
2404 | reloc_s->contents = reloc_d; | |
2405 | } | |
c6c37250 DD |
2406 | |
2407 | void | |
2408 | pe_exe_fill_sections (abfd, info) | |
2409 | bfd *abfd; | |
2410 | struct bfd_link_info *info; | |
2411 | { | |
2412 | pe_dll_id_target (bfd_get_target (abfd)); | |
2413 | image_base = pe_data (abfd)->pe_opthdr.ImageBase; | |
2414 | ||
2415 | generate_reloc (abfd, info); | |
2416 | if (reloc_sz > 0) | |
2417 | { | |
2418 | bfd_set_section_size (filler_bfd, reloc_s, reloc_sz); | |
2419 | ||
2420 | /* Resize the sections. */ | |
2421 | lang_size_sections (stat_ptr->head, abs_output_section, | |
ae7fb08f | 2422 | &stat_ptr->head, 0, (bfd_vma) 0, NULL); |
c6c37250 DD |
2423 | |
2424 | /* Redo special stuff. */ | |
2425 | ldemul_after_allocation (); | |
2426 | ||
2427 | /* Do the assignments again. */ | |
2428 | lang_do_assignments (stat_ptr->head, | |
2429 | abs_output_section, | |
2430 | (fill_type) 0, (bfd_vma) 0); | |
2431 | } | |
2432 | reloc_s->contents = reloc_d; | |
2433 | } |