1 /* Routines to help build PEI-format DLLs (Win32 etc)
2 Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
3 Written by DJ Delorie <dj@cygnus.com>
5 This file is part of GLD, the Gnu Linker.
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)
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.
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
25 #include "libiberty.h"
39 #include "coff/internal.h"
40 #include "../bfd/libcoff.h"
44 /************************************************************************
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.
53 See also: ld/emultempl/pe.em
55 ************************************************************************/
57 /* for emultempl/pe.em */
59 def_file
*pe_def_file
= 0;
60 int pe_dll_export_everything
= 0;
61 int pe_dll_do_default_excludes
= 1;
62 int pe_dll_kill_ats
= 0;
63 int pe_dll_stdcall_aliases
= 0;
64 int pe_dll_warn_dup_exports
= 0;
65 int pe_dll_compat_implib
= 0;
67 /************************************************************************
69 static variables and types
71 ************************************************************************/
73 static bfd_vma image_base
;
75 static bfd
*filler_bfd
;
76 static struct sec
*edata_s
, *reloc_s
;
77 static unsigned char *edata_d
, *reloc_d
;
78 static size_t edata_sz
, reloc_sz
;
83 unsigned int imagebase_reloc
;
90 #define PE_ARCH_i386 1
92 #define PE_ARCH_mips 3
95 static pe_details_type pe_detail_list
[] = {
107 16 /* R_SH_IMAGEBASE */,
128 { NULL
, NULL
, 0, 0, 0, 0 }
131 static pe_details_type
*pe_details
;
133 #define U(str) (pe_details->underscored ? "_" str : str)
136 pe_dll_id_target (target
)
140 for (i
= 0; pe_detail_list
[i
].target_name
; i
++)
141 if (strcmp (pe_detail_list
[i
].target_name
, target
) == 0
142 || strcmp (pe_detail_list
[i
].object_target
, target
) == 0)
144 pe_details
= pe_detail_list
+ i
;
147 einfo (_("%XUnsupported PEI architecture: %s\n"), target
);
151 /************************************************************************
153 Helper functions for qsort. Relocs must be sorted so that we can write
156 ************************************************************************/
169 bfd_vma a
= ((reloc_data_type
*) va
)->vma
;
170 bfd_vma b
= ((reloc_data_type
*) vb
)->vma
;
171 return (a
> b
) ? 1 : ((a
< b
) ? -1 : 0);
175 pe_export_sort (va
, vb
)
178 def_file_export
*a
= (def_file_export
*) va
;
179 def_file_export
*b
= (def_file_export
*) vb
;
180 return strcmp (a
->name
, b
->name
);
183 /************************************************************************
185 Read and process the .DEF file
187 ************************************************************************/
189 /* These correspond to the entries in pe_def_file->exports[]. I use
190 exported_symbol_sections[i] to tag whether or not the symbol was
191 defined, since we can't export symbols we don't have. */
193 static bfd_vma
*exported_symbol_offsets
;
194 static struct sec
**exported_symbol_sections
;
196 static int export_table_size
;
197 static int count_exported
;
198 static int count_exported_byname
;
199 static int count_with_ordinals
;
200 static const char *dll_name
;
201 static int min_ordinal
, max_ordinal
;
202 static int *exported_symbols
;
204 typedef struct exclude_list_struct
{
206 struct exclude_list_struct
*next
;
210 static struct exclude_list_struct
*excludes
= 0;
213 pe_dll_add_excludes (new_excludes
)
214 const char *new_excludes
;
217 char *exclude_string
;
219 local_copy
= xstrdup (new_excludes
);
221 exclude_string
= strtok (local_copy
, ",:");
222 for (; exclude_string
; exclude_string
= strtok (NULL
, ",:"))
224 struct exclude_list_struct
*new_exclude
;
226 new_exclude
= ((struct exclude_list_struct
*)
227 xmalloc (sizeof (struct exclude_list_struct
)));
228 new_exclude
->string
= (char *) xmalloc (strlen (exclude_string
) + 1);
229 strcpy (new_exclude
->string
, exclude_string
);
230 new_exclude
->next
= excludes
;
231 excludes
= new_exclude
;
243 struct exclude_list_struct
*ex
;
244 for (i
= 0; i
< d
->num_exports
; i
++)
245 if (strcmp (d
->exports
[i
].name
, n
) == 0)
247 if (pe_dll_do_default_excludes
)
249 if (strcmp (n
, "DllMain@12") == 0)
251 if (strcmp (n
, "DllEntryPoint@0") == 0)
253 if (strcmp (n
, "impure_ptr") == 0)
256 for (ex
= excludes
; ex
; ex
= ex
->next
)
257 if (strcmp (n
, ex
->string
) == 0)
263 process_def_file (abfd
, info
)
264 bfd
*abfd ATTRIBUTE_UNUSED
;
265 struct bfd_link_info
*info
;
268 struct bfd_link_hash_entry
*blhe
;
271 def_file_export
*e
= 0;
274 pe_def_file
= def_file_empty ();
276 /* First, run around to all the objects looking for the .drectve
277 sections, and push those into the def file too. */
279 for (b
= info
->input_bfds
; b
; b
= b
->link_next
)
281 s
= bfd_get_section_by_name (b
, ".drectve");
284 int size
= bfd_get_section_size_before_reloc (s
);
285 char *buf
= xmalloc (size
);
286 bfd_get_section_contents (b
, s
, buf
, 0, size
);
287 def_file_add_directive (pe_def_file
, buf
, size
);
292 /* Now, maybe export everything else the default way. */
294 if (pe_dll_export_everything
|| pe_def_file
->num_exports
== 0)
296 for (b
= info
->input_bfds
; b
; b
= b
->link_next
)
301 symsize
= bfd_get_symtab_upper_bound (b
);
302 symbols
= (asymbol
**) xmalloc (symsize
);
303 nsyms
= bfd_canonicalize_symtab (b
, symbols
);
305 for (j
= 0; j
< nsyms
; j
++)
307 /* We should export symbols which are either global or not
308 anything at all. (.bss data is the latter) */
309 if ((symbols
[j
]->flags
& BSF_GLOBAL
)
310 || (symbols
[j
]->flags
== BSF_NO_FLAGS
))
312 const char *sn
= symbols
[j
]->name
;
315 if (auto_export (pe_def_file
, sn
))
316 def_file_add_export (pe_def_file
, sn
, 0, -1);
323 #define NE pe_def_file->num_exports
325 /* Canonicalize the export list. */
329 for (i
= 0; i
< NE
; i
++)
331 if (strchr (pe_def_file
->exports
[i
].name
, '@'))
333 /* This will preserve internal_name, which may have been
334 pointing to the same memory as name, or might not
336 char *tmp
= xstrdup (pe_def_file
->exports
[i
].name
);
337 *(strchr (tmp
, '@')) = 0;
338 pe_def_file
->exports
[i
].name
= tmp
;
343 if (pe_dll_stdcall_aliases
)
345 for (i
= 0; i
< NE
; i
++)
347 if (strchr (pe_def_file
->exports
[i
].name
, '@'))
349 char *tmp
= xstrdup (pe_def_file
->exports
[i
].name
);
350 *(strchr (tmp
, '@')) = 0;
351 if (auto_export (pe_def_file
, tmp
))
352 def_file_add_export (pe_def_file
, tmp
,
353 pe_def_file
->exports
[i
].internal_name
, -1);
360 /* Convenience, but watch out for it changing. */
361 e
= pe_def_file
->exports
;
363 exported_symbol_offsets
= (bfd_vma
*) xmalloc (NE
* sizeof (bfd_vma
));
364 exported_symbol_sections
= (struct sec
**) xmalloc (NE
* sizeof (struct sec
*));
366 memset (exported_symbol_sections
, 0, NE
* sizeof (struct sec
*));
370 count_exported_byname
= 0;
371 count_with_ordinals
= 0;
373 qsort (pe_def_file
->exports
, NE
, sizeof (pe_def_file
->exports
[0]), pe_export_sort
);
374 for (i
= 0, j
= 0; i
< NE
; i
++)
376 if (i
> 0 && strcmp (e
[i
].name
, e
[i
- 1].name
) == 0)
378 /* This is a duplicate. */
379 if (e
[j
- 1].ordinal
!= -1
380 && e
[i
].ordinal
!= -1
381 && e
[j
- 1].ordinal
!= e
[i
].ordinal
)
383 if (pe_dll_warn_dup_exports
)
384 /* xgettext:c-format */
385 einfo (_("%XError, duplicate EXPORT with ordinals: %s (%d vs %d)\n"),
386 e
[j
- 1].name
, e
[j
- 1].ordinal
, e
[i
].ordinal
);
390 if (pe_dll_warn_dup_exports
)
391 /* xgettext:c-format */
392 einfo (_("Warning, duplicate EXPORT: %s\n"),
395 if (e
[i
].ordinal
!= -1)
396 e
[j
- 1].ordinal
= e
[i
].ordinal
;
397 e
[j
- 1].flag_private
|= e
[i
].flag_private
;
398 e
[j
- 1].flag_constant
|= e
[i
].flag_constant
;
399 e
[j
- 1].flag_noname
|= e
[i
].flag_noname
;
400 e
[j
- 1].flag_data
|= e
[i
].flag_data
;
409 pe_def_file
->num_exports
= j
; /* == NE */
411 for (i
= 0; i
< NE
; i
++)
413 char *name
= (char *) xmalloc (strlen (pe_def_file
->exports
[i
].internal_name
) + 2);
414 if (pe_details
->underscored
)
417 strcpy (name
+ 1, pe_def_file
->exports
[i
].internal_name
);
420 strcpy (name
, pe_def_file
->exports
[i
].internal_name
);
422 blhe
= bfd_link_hash_lookup (info
->hash
,
427 && (blhe
->type
== bfd_link_hash_defined
428 || (blhe
->type
== bfd_link_hash_common
)))
431 if (!pe_def_file
->exports
[i
].flag_noname
)
432 count_exported_byname
++;
434 /* Only fill in the sections. The actual offsets are computed
435 in fill_exported_offsets() after common symbols are laid
437 if (blhe
->type
== bfd_link_hash_defined
)
438 exported_symbol_sections
[i
] = blhe
->u
.def
.section
;
440 exported_symbol_sections
[i
] = blhe
->u
.c
.p
->section
;
442 if (pe_def_file
->exports
[i
].ordinal
!= -1)
444 if (max_ordinal
< pe_def_file
->exports
[i
].ordinal
)
445 max_ordinal
= pe_def_file
->exports
[i
].ordinal
;
446 if (min_ordinal
> pe_def_file
->exports
[i
].ordinal
)
447 min_ordinal
= pe_def_file
->exports
[i
].ordinal
;
448 count_with_ordinals
++;
451 else if (blhe
&& blhe
->type
== bfd_link_hash_undefined
)
453 /* xgettext:c-format */
454 einfo (_("%XCannot export %s: symbol not defined\n"),
455 pe_def_file
->exports
[i
].internal_name
);
459 /* xgettext:c-format */
460 einfo (_("%XCannot export %s: symbol wrong type (%d vs %d)\n"),
461 pe_def_file
->exports
[i
].internal_name
,
462 blhe
->type
, bfd_link_hash_defined
);
466 /* xgettext:c-format */
467 einfo (_("%XCannot export %s: symbol not found\n"),
468 pe_def_file
->exports
[i
].internal_name
);
474 /************************************************************************
476 Build the bfd that will contain .edata and .reloc sections
478 ************************************************************************/
481 build_filler_bfd (include_edata
)
484 lang_input_statement_type
*filler_file
;
485 filler_file
= lang_add_input_file ("dll stuff",
486 lang_input_file_is_fake_enum
,
488 filler_file
->the_bfd
= filler_bfd
= bfd_create ("dll stuff", output_bfd
);
489 if (filler_bfd
== NULL
490 || !bfd_set_arch_mach (filler_bfd
,
491 bfd_get_arch (output_bfd
),
492 bfd_get_mach (output_bfd
)))
494 einfo ("%X%P: can not create BFD %E\n");
500 edata_s
= bfd_make_section_old_way (filler_bfd
, ".edata");
502 || !bfd_set_section_flags (filler_bfd
, edata_s
,
509 einfo ("%X%P: can not create .edata section: %E\n");
512 bfd_set_section_size (filler_bfd
, edata_s
, edata_sz
);
515 reloc_s
= bfd_make_section_old_way (filler_bfd
, ".reloc");
517 || !bfd_set_section_flags (filler_bfd
, reloc_s
,
524 einfo ("%X%P: can not create .reloc section: %E\n");
527 bfd_set_section_size (filler_bfd
, reloc_s
, 0);
529 ldlang_add_file (filler_file
);
532 /************************************************************************
534 Gather all the exported symbols and build the .edata section
536 ************************************************************************/
539 generate_edata (abfd
, info
)
541 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
544 int name_table_size
= 0;
547 /* First, we need to know how many exported symbols there are,
548 and what the range of ordinals is. */
550 if (pe_def_file
->name
)
552 dll_name
= pe_def_file
->name
;
556 dll_name
= abfd
->filename
;
557 for (dlnp
= dll_name
; *dlnp
; dlnp
++)
559 if (*dlnp
== '\\' || *dlnp
== '/' || *dlnp
== ':')
564 if (count_with_ordinals
&& max_ordinal
> count_exported
)
566 if (min_ordinal
> max_ordinal
- count_exported
+ 1)
567 min_ordinal
= max_ordinal
- count_exported
+ 1;
572 max_ordinal
= count_exported
;
574 export_table_size
= max_ordinal
- min_ordinal
+ 1;
576 exported_symbols
= (int *) xmalloc (export_table_size
* sizeof (int));
577 for (i
= 0; i
< export_table_size
; i
++)
578 exported_symbols
[i
] = -1;
580 /* Now we need to assign ordinals to those that don't have them. */
581 for (i
= 0; i
< NE
; i
++)
583 if (exported_symbol_sections
[i
])
585 if (pe_def_file
->exports
[i
].ordinal
!= -1)
587 int ei
= pe_def_file
->exports
[i
].ordinal
- min_ordinal
;
588 int pi
= exported_symbols
[ei
];
591 /* xgettext:c-format */
592 einfo (_("%XError, ordinal used twice: %d (%s vs %s)\n"),
593 pe_def_file
->exports
[i
].ordinal
,
594 pe_def_file
->exports
[i
].name
,
595 pe_def_file
->exports
[pi
].name
);
597 exported_symbols
[ei
] = i
;
599 name_table_size
+= strlen (pe_def_file
->exports
[i
].name
) + 1;
603 next_ordinal
= min_ordinal
;
604 for (i
= 0; i
< NE
; i
++)
605 if (exported_symbol_sections
[i
])
606 if (pe_def_file
->exports
[i
].ordinal
== -1)
608 while (exported_symbols
[next_ordinal
- min_ordinal
] != -1)
610 exported_symbols
[next_ordinal
- min_ordinal
] = i
;
611 pe_def_file
->exports
[i
].ordinal
= next_ordinal
;
614 /* OK, now we can allocate some memory. */
616 edata_sz
= (40 /* directory */
617 + 4 * export_table_size
/* addresses */
618 + 4 * count_exported_byname
/* name ptrs */
619 + 2 * count_exported_byname
/* ordinals */
620 + name_table_size
+ strlen (dll_name
) + 1);
623 /* Fill the exported symbol offsets. The preliminary work has already
624 been done in process_def_file(). */
627 fill_exported_offsets (abfd
, info
)
628 bfd
*abfd ATTRIBUTE_UNUSED
;
629 struct bfd_link_info
*info
;
632 struct bfd_link_hash_entry
*blhe
;
634 for (i
= 0; i
< pe_def_file
->num_exports
; i
++)
636 char *name
= (char *) xmalloc (strlen (pe_def_file
->exports
[i
].internal_name
) + 2);
637 if (pe_details
->underscored
)
640 strcpy (name
+ 1, pe_def_file
->exports
[i
].internal_name
);
643 strcpy (name
, pe_def_file
->exports
[i
].internal_name
);
645 blhe
= bfd_link_hash_lookup (info
->hash
,
649 if (blhe
&& (blhe
->type
== bfd_link_hash_defined
))
651 exported_symbol_offsets
[i
] = blhe
->u
.def
.value
;
658 fill_edata (abfd
, info
)
660 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
663 unsigned char *edirectory
;
664 unsigned long *eaddresses
;
665 unsigned long *enameptrs
;
666 unsigned short *eordinals
;
667 unsigned char *enamestr
;
672 edata_d
= (unsigned char *) xmalloc (edata_sz
);
674 /* Note use of array pointer math here. */
675 edirectory
= edata_d
;
676 eaddresses
= (unsigned long *) (edata_d
+ 40);
677 enameptrs
= eaddresses
+ export_table_size
;
678 eordinals
= (unsigned short *) (enameptrs
+ count_exported_byname
);
679 enamestr
= (char *) (eordinals
+ count_exported_byname
);
681 #define ERVA(ptr) (((unsigned char *)(ptr) - edata_d) + edata_s->output_section->vma - image_base)
683 memset (edata_d
, 0, edata_sz
);
684 bfd_put_32 (abfd
, now
, edata_d
+ 4);
685 if (pe_def_file
->version_major
!= -1)
687 bfd_put_16 (abfd
, pe_def_file
->version_major
, edata_d
+ 8);
688 bfd_put_16 (abfd
, pe_def_file
->version_minor
, edata_d
+ 10);
690 bfd_put_32 (abfd
, ERVA (enamestr
), edata_d
+ 12);
691 strcpy (enamestr
, dll_name
);
692 enamestr
+= strlen (enamestr
) + 1;
693 bfd_put_32 (abfd
, min_ordinal
, edata_d
+ 16);
694 bfd_put_32 (abfd
, export_table_size
, edata_d
+ 20);
695 bfd_put_32 (abfd
, count_exported_byname
, edata_d
+ 24);
696 bfd_put_32 (abfd
, ERVA (eaddresses
), edata_d
+ 28);
697 bfd_put_32 (abfd
, ERVA (enameptrs
), edata_d
+ 32);
698 bfd_put_32 (abfd
, ERVA (eordinals
), edata_d
+ 36);
700 fill_exported_offsets (abfd
, info
);
702 /* Ok, now for the filling in part. */
704 for (i
= 0; i
< export_table_size
; i
++)
706 int s
= exported_symbols
[i
];
709 struct sec
*ssec
= exported_symbol_sections
[s
];
710 unsigned long srva
= (exported_symbol_offsets
[s
]
711 + ssec
->output_section
->vma
712 + ssec
->output_offset
);
713 int ord
= pe_def_file
->exports
[s
].ordinal
;
715 bfd_put_32 (abfd
, srva
- image_base
,
716 (void *) (eaddresses
+ ord
- min_ordinal
));
717 if (!pe_def_file
->exports
[s
].flag_noname
)
719 char *ename
= pe_def_file
->exports
[s
].name
;
720 bfd_put_32 (abfd
, ERVA (enamestr
), (void *) enameptrs
);
722 strcpy (enamestr
, ename
);
723 enamestr
+= strlen (enamestr
) + 1;
724 bfd_put_16 (abfd
, ord
- min_ordinal
, (void *) eordinals
);
726 pe_def_file
->exports
[s
].hint
= hint
++;
732 /************************************************************************
734 Gather all the relocations and build the .reloc section
736 ************************************************************************/
739 generate_reloc (abfd
, info
)
741 struct bfd_link_info
*info
;
744 /* For .reloc stuff. */
745 reloc_data_type
*reloc_data
;
746 int total_relocs
= 0;
748 unsigned long sec_page
= (unsigned long) (-1);
749 unsigned long page_ptr
, page_count
;
755 for (b
= info
->input_bfds
; b
; b
= b
->link_next
)
756 for (s
= b
->sections
; s
; s
= s
->next
)
757 total_relocs
+= s
->reloc_count
;
759 reloc_data
= (reloc_data_type
*) xmalloc (total_relocs
* sizeof (reloc_data_type
));
763 for (bi
= 0, b
= info
->input_bfds
; b
; bi
++, b
= b
->link_next
)
766 int relsize
, nrelocs
, i
;
768 for (s
= b
->sections
; s
; s
= s
->next
)
770 unsigned long sec_vma
= s
->output_section
->vma
+ s
->output_offset
;
774 /* If it's not loaded, we don't need to relocate it this way. */
775 if (!(s
->output_section
->flags
& SEC_LOAD
))
778 /* I don't know why there would be a reloc for these, but I've
779 seen it happen - DJ */
780 if (s
->output_section
== &bfd_abs_section
)
783 if (s
->output_section
->vma
== 0)
785 /* Huh? Shouldn't happen, but punt if it does. */
786 einfo ("DJ: zero vma section reloc detected: `%s' #%d f=%d\n",
787 s
->output_section
->name
, s
->output_section
->index
,
788 s
->output_section
->flags
);
792 symsize
= bfd_get_symtab_upper_bound (b
);
793 symbols
= (asymbol
**) xmalloc (symsize
);
794 nsyms
= bfd_canonicalize_symtab (b
, symbols
);
796 relsize
= bfd_get_reloc_upper_bound (b
, s
);
797 relocs
= (arelent
**) xmalloc ((size_t) relsize
);
798 nrelocs
= bfd_canonicalize_reloc (b
, s
, relocs
, symbols
);
800 for (i
= 0; i
< nrelocs
; i
++)
802 if (!relocs
[i
]->howto
->pc_relative
803 && relocs
[i
]->howto
->type
!= pe_details
->imagebase_reloc
)
806 struct symbol_cache_entry
*sym
= *relocs
[i
]->sym_ptr_ptr
;
807 sym_vma
= (relocs
[i
]->addend
810 + sym
->section
->output_offset
811 + sym
->section
->output_section
->vma
);
812 reloc_data
[total_relocs
].vma
= sec_vma
+ relocs
[i
]->address
;
814 #define BITS_AND_SHIFT(bits, shift) (bits * 1000 | shift)
816 switch BITS_AND_SHIFT (relocs
[i
]->howto
->bitsize
,
817 relocs
[i
]->howto
->rightshift
)
819 case BITS_AND_SHIFT (32, 0):
820 reloc_data
[total_relocs
].type
= 3;
823 case BITS_AND_SHIFT (16, 0):
824 reloc_data
[total_relocs
].type
= 2;
827 case BITS_AND_SHIFT (16, 16):
828 reloc_data
[total_relocs
].type
= 4;
829 /* FIXME: we can't know the symbol's right value
830 yet, but we probably can safely assume that
831 CE will relocate us in 64k blocks, so leaving
833 reloc_data
[total_relocs
].extra
= 0;
836 case BITS_AND_SHIFT (26, 2):
837 reloc_data
[total_relocs
].type
= 5;
841 /* xgettext:c-format */
842 einfo (_("%XError: %d-bit reloc in dll\n"),
843 relocs
[i
]->howto
->bitsize
);
849 /* Warning: the allocated symbols are remembered in BFD and
850 reused later, so don't free them! */
854 /* At this point, we have total_relocs relocation addresses in
855 reloc_addresses, which are all suitable for the .reloc section.
856 We must now create the new sections. */
858 qsort (reloc_data
, total_relocs
, sizeof (*reloc_data
), reloc_sort
);
860 for (i
= 0; i
< total_relocs
; i
++)
862 unsigned long this_page
= (reloc_data
[i
].vma
>> 12);
864 if (this_page
!= sec_page
)
866 reloc_sz
= (reloc_sz
+ 3) & ~3; /* 4-byte align */
868 sec_page
= this_page
;
873 if (reloc_data
[i
].type
== 4)
876 reloc_sz
= (reloc_sz
+ 3) & ~3; /* 4-byte align */
878 reloc_d
= (unsigned char *) xmalloc (reloc_sz
);
880 sec_page
= (unsigned long) (-1);
882 page_ptr
= (unsigned long) (-1);
884 for (i
= 0; i
< total_relocs
; i
++)
886 unsigned long rva
= reloc_data
[i
].vma
- image_base
;
887 unsigned long this_page
= (rva
& ~0xfff);
888 if (this_page
!= sec_page
)
891 reloc_d
[reloc_sz
++] = 0;
892 if (page_ptr
!= (unsigned long) (-1))
893 bfd_put_32 (abfd
, reloc_sz
- page_ptr
, reloc_d
+ page_ptr
+ 4);
894 bfd_put_32 (abfd
, this_page
, reloc_d
+ reloc_sz
);
897 sec_page
= this_page
;
900 bfd_put_16 (abfd
, (rva
& 0xfff) + (reloc_data
[i
].type
<< 12),
903 if (reloc_data
[i
].type
== 4)
905 bfd_put_16 (abfd
, reloc_data
[i
].extra
, reloc_d
+ reloc_sz
);
911 reloc_d
[reloc_sz
++] = 0;
912 if (page_ptr
!= (unsigned long) (-1))
913 bfd_put_32 (abfd
, reloc_sz
- page_ptr
, reloc_d
+ page_ptr
+ 4);
914 while (reloc_sz
< reloc_s
->_raw_size
)
915 reloc_d
[reloc_sz
++] = 0;
918 /************************************************************************
920 Given the exiting def_file structure, print out a .DEF file that
923 ************************************************************************/
926 quoteput (s
, f
, needs_quotes
)
932 for (cp
= s
; *cp
; cp
++)
936 || isspace ((unsigned char) *cp
)
945 if (*s
== '"' || *s
== '\\')
957 pe_dll_generate_def_file (pe_out_def_filename
)
958 const char *pe_out_def_filename
;
961 FILE *out
= fopen (pe_out_def_filename
, "w");
964 /* xgettext:c-format */
965 einfo (_("%s: Can't open output def file %s\n"),
966 program_name
, pe_out_def_filename
);
971 if (pe_def_file
->name
)
973 if (pe_def_file
->is_dll
)
974 fprintf (out
, "LIBRARY ");
976 fprintf (out
, "NAME ");
977 quoteput (pe_def_file
->name
, out
, 1);
978 if (pe_data (output_bfd
)->pe_opthdr
.ImageBase
)
979 fprintf (out
, " BASE=0x%lx",
980 (unsigned long) pe_data (output_bfd
)->pe_opthdr
.ImageBase
);
984 if (pe_def_file
->description
)
986 fprintf (out
, "DESCRIPTION ");
987 quoteput (pe_def_file
->description
, out
, 1);
991 if (pe_def_file
->version_minor
!= -1)
992 fprintf (out
, "VERSION %d.%d\n", pe_def_file
->version_major
,
993 pe_def_file
->version_minor
);
994 else if (pe_def_file
->version_major
!= -1)
995 fprintf (out
, "VERSION %d\n", pe_def_file
->version_major
);
997 if (pe_def_file
->stack_reserve
!= -1 || pe_def_file
->heap_reserve
!= -1)
1000 if (pe_def_file
->stack_commit
!= -1)
1001 fprintf (out
, "STACKSIZE 0x%x,0x%x\n",
1002 pe_def_file
->stack_reserve
, pe_def_file
->stack_commit
);
1003 else if (pe_def_file
->stack_reserve
!= -1)
1004 fprintf (out
, "STACKSIZE 0x%x\n", pe_def_file
->stack_reserve
);
1005 if (pe_def_file
->heap_commit
!= -1)
1006 fprintf (out
, "HEAPSIZE 0x%x,0x%x\n",
1007 pe_def_file
->heap_reserve
, pe_def_file
->heap_commit
);
1008 else if (pe_def_file
->heap_reserve
!= -1)
1009 fprintf (out
, "HEAPSIZE 0x%x\n", pe_def_file
->heap_reserve
);
1011 if (pe_def_file
->num_section_defs
> 0)
1013 fprintf (out
, "\nSECTIONS\n\n");
1014 for (i
= 0; i
< pe_def_file
->num_section_defs
; i
++)
1017 quoteput (pe_def_file
->section_defs
[i
].name
, out
, 0);
1018 if (pe_def_file
->section_defs
[i
].class)
1020 fprintf (out
, " CLASS ");
1021 quoteput (pe_def_file
->section_defs
[i
].class, out
, 0);
1023 if (pe_def_file
->section_defs
[i
].flag_read
)
1024 fprintf (out
, " READ");
1025 if (pe_def_file
->section_defs
[i
].flag_write
)
1026 fprintf (out
, " WRITE");
1027 if (pe_def_file
->section_defs
[i
].flag_execute
)
1028 fprintf (out
, " EXECUTE");
1029 if (pe_def_file
->section_defs
[i
].flag_shared
)
1030 fprintf (out
, " SHARED");
1031 fprintf (out
, "\n");
1035 if (pe_def_file
->num_exports
> 0)
1037 fprintf (out
, "\nEXPORTS\n\n");
1038 for (i
= 0; i
< pe_def_file
->num_exports
; i
++)
1040 def_file_export
*e
= pe_def_file
->exports
+ i
;
1042 quoteput (e
->name
, out
, 0);
1043 if (e
->internal_name
&& strcmp (e
->internal_name
, e
->name
))
1045 fprintf (out
, " = ");
1046 quoteput (e
->internal_name
, out
, 0);
1048 if (e
->ordinal
!= -1)
1049 fprintf (out
, " @%d", e
->ordinal
);
1050 if (e
->flag_private
)
1051 fprintf (out
, " PRIVATE");
1052 if (e
->flag_constant
)
1053 fprintf (out
, " CONSTANT");
1055 fprintf (out
, " NONAME");
1057 fprintf (out
, " DATA");
1059 fprintf (out
, "\n");
1063 if (pe_def_file
->num_imports
> 0)
1065 fprintf (out
, "\nIMPORTS\n\n");
1066 for (i
= 0; i
< pe_def_file
->num_imports
; i
++)
1068 def_file_import
*im
= pe_def_file
->imports
+ i
;
1070 if (im
->internal_name
1071 && (!im
->name
|| strcmp (im
->internal_name
, im
->name
)))
1073 quoteput (im
->internal_name
, out
, 0);
1074 fprintf (out
, " = ");
1076 quoteput (im
->module
->name
, out
, 0);
1079 quoteput (im
->name
, out
, 0);
1081 fprintf (out
, "%d", im
->ordinal
);
1082 fprintf (out
, "\n");
1087 fprintf (out
, _("; no contents available\n"));
1089 if (fclose (out
) == EOF
)
1091 /* xgettext:c-format */
1092 einfo (_("%P: Error closing file `%s'\n"), pe_out_def_filename
);
1096 /************************************************************************
1098 Generate the import library
1100 ************************************************************************/
1102 static asymbol
**symtab
;
1105 static const char *dll_filename
;
1106 static char *dll_symname
;
1108 #define UNDSEC (asection *) &bfd_und_section
1111 quick_section (abfd
, name
, flags
, align
)
1120 sec
= bfd_make_section_old_way (abfd
, name
);
1121 bfd_set_section_flags (abfd
, sec
, flags
| SEC_ALLOC
| SEC_LOAD
| SEC_KEEP
);
1122 bfd_set_section_alignment (abfd
, sec
, align
);
1123 /* Remember to undo this before trying to link internally! */
1124 sec
->output_section
= sec
;
1126 sym
= bfd_make_empty_symbol (abfd
);
1127 symtab
[symptr
++] = sym
;
1128 sym
->name
= sec
->name
;
1130 sym
->flags
= BSF_LOCAL
;
1137 quick_symbol (abfd
, n1
, n2
, n3
, sec
, flags
, addr
)
1147 char *name
= (char *) xmalloc (strlen (n1
) + strlen (n2
) + strlen (n3
) + 1);
1151 sym
= bfd_make_empty_symbol (abfd
);
1156 symtab
[symptr
++] = sym
;
1159 static arelent
*reltab
= 0;
1160 static int relcount
= 0, relsize
= 0;
1163 quick_reloc (abfd
, address
, which_howto
, symidx
)
1169 if (relcount
>= (relsize
- 1))
1173 reltab
= (arelent
*) xrealloc (reltab
, relsize
* sizeof (arelent
));
1175 reltab
= (arelent
*) xmalloc (relsize
* sizeof (arelent
));
1177 reltab
[relcount
].address
= address
;
1178 reltab
[relcount
].addend
= 0;
1179 reltab
[relcount
].howto
= bfd_reloc_type_lookup (abfd
, which_howto
);
1180 reltab
[relcount
].sym_ptr_ptr
= symtab
+ symidx
;
1185 save_relocs (asection
*sec
)
1188 sec
->relocation
= reltab
;
1189 sec
->reloc_count
= relcount
;
1190 sec
->orelocation
= (arelent
**) xmalloc ((relcount
+ 1) * sizeof (arelent
*));
1191 for (i
= 0; i
< relcount
; i
++)
1192 sec
->orelocation
[i
] = sec
->relocation
+ i
;
1193 sec
->orelocation
[relcount
] = 0;
1194 sec
->flags
|= SEC_RELOC
;
1196 relcount
= relsize
= 0;
1201 * .global __head_my_dll
1206 * .rva __my_dll_iname
1222 asection
*id2
, *id5
, *id4
;
1223 unsigned char *d2
, *d5
, *d4
;
1227 oname
= (char *) xmalloc (20);
1228 sprintf (oname
, "d%06d.o", tmp_seq
);
1231 abfd
= bfd_create (oname
, parent
);
1232 bfd_find_target (pe_details
->object_target
, abfd
);
1233 bfd_make_writable (abfd
);
1235 bfd_set_format (abfd
, bfd_object
);
1236 bfd_set_arch_mach (abfd
, pe_details
->bfd_arch
, 0);
1239 symtab
= (asymbol
**) xmalloc (6 * sizeof (asymbol
*));
1240 id2
= quick_section (abfd
, ".idata$2", SEC_HAS_CONTENTS
, 2);
1241 id5
= quick_section (abfd
, ".idata$5", SEC_HAS_CONTENTS
, 2);
1242 id4
= quick_section (abfd
, ".idata$4", SEC_HAS_CONTENTS
, 2);
1243 quick_symbol (abfd
, U ("_head_"), dll_symname
, "", id2
, BSF_GLOBAL
, 0);
1244 quick_symbol (abfd
, U (""), dll_symname
, "_iname", UNDSEC
, BSF_GLOBAL
, 0);
1246 /* OK, pay attention here. I got confused myself looking back at
1247 it. We create a four-byte section to mark the beginning of the
1248 list, and we include an offset of 4 in the section, so that the
1249 pointer to the list points to the *end* of this section, which is
1250 the start of the list of sections from other objects. */
1252 bfd_set_section_size (abfd
, id2
, 20);
1253 d2
= (unsigned char *) xmalloc (20);
1256 d2
[0] = d2
[16] = 4; /* reloc addend */
1257 quick_reloc (abfd
, 0, BFD_RELOC_RVA
, 2);
1258 quick_reloc (abfd
, 12, BFD_RELOC_RVA
, 4);
1259 quick_reloc (abfd
, 16, BFD_RELOC_RVA
, 1);
1262 bfd_set_section_size (abfd
, id5
, 4);
1263 d5
= (unsigned char *) xmalloc (4);
1267 bfd_set_section_size (abfd
, id4
, 4);
1268 d4
= (unsigned char *) xmalloc (4);
1272 bfd_set_symtab (abfd
, symtab
, symptr
);
1274 bfd_set_section_contents (abfd
, id2
, d2
, 0, 20);
1275 bfd_set_section_contents (abfd
, id5
, d5
, 0, 4);
1276 bfd_set_section_contents (abfd
, id4
, d4
, 0, 4);
1278 bfd_make_readable (abfd
);
1288 * .global __my_dll_iname
1297 asection
*id4
, *id5
, *id7
;
1298 unsigned char *d4
, *d5
, *d7
;
1303 oname
= (char *) xmalloc (20);
1304 sprintf (oname
, "d%06d.o", tmp_seq
);
1307 abfd
= bfd_create (oname
, parent
);
1308 bfd_find_target (pe_details
->object_target
, abfd
);
1309 bfd_make_writable (abfd
);
1311 bfd_set_format (abfd
, bfd_object
);
1312 bfd_set_arch_mach (abfd
, pe_details
->bfd_arch
, 0);
1315 symtab
= (asymbol
**) xmalloc (5 * sizeof (asymbol
*));
1316 id4
= quick_section (abfd
, ".idata$4", SEC_HAS_CONTENTS
, 2);
1317 id5
= quick_section (abfd
, ".idata$5", SEC_HAS_CONTENTS
, 2);
1318 id7
= quick_section (abfd
, ".idata$7", SEC_HAS_CONTENTS
, 2);
1319 quick_symbol (abfd
, U (""), dll_symname
, "_iname", id7
, BSF_GLOBAL
, 0);
1321 bfd_set_section_size (abfd
, id4
, 4);
1322 d4
= (unsigned char *) xmalloc (4);
1326 bfd_set_section_size (abfd
, id5
, 4);
1327 d5
= (unsigned char *) xmalloc (4);
1331 len
= strlen (dll_filename
) + 1;
1334 bfd_set_section_size (abfd
, id7
, len
);
1335 d7
= (unsigned char *) xmalloc (len
);
1337 strcpy (d7
, dll_filename
);
1339 bfd_set_symtab (abfd
, symtab
, symptr
);
1341 bfd_set_section_contents (abfd
, id4
, d4
, 0, 4);
1342 bfd_set_section_contents (abfd
, id5
, d5
, 0, 4);
1343 bfd_set_section_contents (abfd
, id7
, d7
, 0, len
);
1345 bfd_make_readable (abfd
);
1352 * .global ___imp_function
1353 * .global __imp__function
1355 * jmp *__imp__function:
1358 * .long __head_my_dll
1369 * .asciz "function" xlate? (add underscore, kill at)
1372 static unsigned char jmp_ix86_bytes
[] = {
1373 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, 0x90, 0x90
1382 * .dw __imp_function
1385 static unsigned char jmp_sh_bytes
[] = {
1386 0x01, 0xd0, 0x02, 0x60, 0x2b, 0x40, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00
1391 * lui $t0,<high:__imp_function>
1392 * lw $t0,<low:__imp_function>
1397 static unsigned char jmp_mips_bytes
[] = {
1398 0x00, 0x00, 0x08, 0x3c, 0x00, 0x00, 0x08, 0x8d,
1399 0x08, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00
1403 make_one (exp
, parent
)
1404 def_file_export
*exp
;
1407 asection
*tx
, *id7
, *id5
, *id4
, *id6
;
1408 unsigned char *td
, *d7
, *d5
, *d4
, *d6
= NULL
;
1412 unsigned char *jmp_bytes
= NULL
;
1413 int jmp_byte_count
= 0;
1415 switch (pe_details
->pe_arch
)
1418 jmp_bytes
= jmp_ix86_bytes
;
1419 jmp_byte_count
= sizeof (jmp_ix86_bytes
);
1422 jmp_bytes
= jmp_sh_bytes
;
1423 jmp_byte_count
= sizeof (jmp_sh_bytes
);
1426 jmp_bytes
= jmp_mips_bytes
;
1427 jmp_byte_count
= sizeof (jmp_mips_bytes
);
1431 oname
= (char *) xmalloc (20);
1432 sprintf (oname
, "d%06d.o", tmp_seq
);
1435 abfd
= bfd_create (oname
, parent
);
1436 bfd_find_target (pe_details
->object_target
, abfd
);
1437 bfd_make_writable (abfd
);
1439 bfd_set_format (abfd
, bfd_object
);
1440 bfd_set_arch_mach (abfd
, pe_details
->bfd_arch
, 0);
1443 symtab
= (asymbol
**) xmalloc (10 * sizeof (asymbol
*));
1444 tx
= quick_section (abfd
, ".text", SEC_CODE
|SEC_HAS_CONTENTS
, 2);
1445 id7
= quick_section (abfd
, ".idata$7", SEC_HAS_CONTENTS
, 2);
1446 id5
= quick_section (abfd
, ".idata$5", SEC_HAS_CONTENTS
, 2);
1447 id4
= quick_section (abfd
, ".idata$4", SEC_HAS_CONTENTS
, 2);
1448 id6
= quick_section (abfd
, ".idata$6", SEC_HAS_CONTENTS
, 2);
1449 if (! exp
->flag_data
)
1450 quick_symbol (abfd
, U (""), exp
->internal_name
, "", tx
, BSF_GLOBAL
, 0);
1451 quick_symbol (abfd
, U ("_head_"), dll_symname
, "", UNDSEC
, BSF_GLOBAL
, 0);
1452 quick_symbol (abfd
, U ("_imp__"), exp
->internal_name
, "", id5
, BSF_GLOBAL
, 0);
1453 if (pe_dll_compat_implib
)
1454 quick_symbol (abfd
, U ("__imp_"), exp
->internal_name
, "",
1455 id5
, BSF_GLOBAL
, 0);
1457 bfd_set_section_size (abfd
, tx
, jmp_byte_count
);
1458 td
= (unsigned char *) xmalloc (jmp_byte_count
);
1460 memcpy (td
, jmp_bytes
, jmp_byte_count
);
1461 switch (pe_details
->pe_arch
)
1464 quick_reloc (abfd
, 2, BFD_RELOC_32
, 2);
1467 quick_reloc (abfd
, 8, BFD_RELOC_32
, 2);
1470 quick_reloc (abfd
, 0, BFD_RELOC_HI16_S
, 2);
1471 quick_reloc (abfd
, 0, BFD_RELOC_LO16
, 0); /* MIPS_R_PAIR */
1472 quick_reloc (abfd
, 4, BFD_RELOC_LO16
, 2);
1477 bfd_set_section_size (abfd
, id7
, 4);
1478 d7
= (unsigned char *) xmalloc (4);
1481 quick_reloc (abfd
, 0, BFD_RELOC_RVA
, 6);
1484 bfd_set_section_size (abfd
, id5
, 4);
1485 d5
= (unsigned char *) xmalloc (4);
1488 if (exp
->flag_noname
)
1490 d5
[0] = exp
->ordinal
;
1491 d5
[1] = exp
->ordinal
>> 8;
1496 quick_reloc (abfd
, 0, BFD_RELOC_RVA
, 4);
1500 bfd_set_section_size (abfd
, id4
, 4);
1501 d4
= (unsigned char *) xmalloc (4);
1504 if (exp
->flag_noname
)
1506 d4
[0] = exp
->ordinal
;
1507 d4
[1] = exp
->ordinal
>> 8;
1512 quick_reloc (abfd
, 0, BFD_RELOC_RVA
, 4);
1516 if (exp
->flag_noname
)
1519 bfd_set_section_size (abfd
, id6
, 0);
1523 len
= strlen (exp
->name
) + 3;
1526 bfd_set_section_size (abfd
, id6
, len
);
1527 d6
= (unsigned char *) xmalloc (len
);
1529 memset (d6
, 0, len
);
1530 d6
[0] = exp
->hint
& 0xff;
1531 d6
[1] = exp
->hint
>> 8;
1532 strcpy (d6
+ 2, exp
->name
);
1535 bfd_set_symtab (abfd
, symtab
, symptr
);
1537 bfd_set_section_contents (abfd
, tx
, td
, 0, jmp_byte_count
);
1538 bfd_set_section_contents (abfd
, id7
, d7
, 0, 4);
1539 bfd_set_section_contents (abfd
, id5
, d5
, 0, 4);
1540 bfd_set_section_contents (abfd
, id4
, d4
, 0, 4);
1541 if (!exp
->flag_noname
)
1542 bfd_set_section_contents (abfd
, id6
, d6
, 0, len
);
1544 bfd_make_readable (abfd
);
1549 pe_dll_generate_implib (def
, impfilename
)
1551 const char *impfilename
;
1559 dll_filename
= (def
->name
) ? def
->name
: dll_name
;
1560 dll_symname
= xstrdup (dll_filename
);
1561 for (i
= 0; dll_symname
[i
]; i
++)
1562 if (!isalnum ((unsigned char) dll_symname
[i
]))
1563 dll_symname
[i
] = '_';
1565 unlink (impfilename
);
1567 outarch
= bfd_openw (impfilename
, 0);
1571 /* xgettext:c-format */
1572 einfo (_("%XCan't open .lib file: %s\n"), impfilename
);
1576 /* xgettext:c-format */
1577 einfo (_("Creating library file: %s\n"), impfilename
);
1579 bfd_set_format (outarch
, bfd_archive
);
1580 outarch
->has_armap
= 1;
1582 /* Work out a reasonable size of things to put onto one line. */
1584 ar_head
= make_head (outarch
);
1586 for (i
= 0; i
< def
->num_exports
; i
++)
1588 /* The import library doesn't know about the internal name. */
1589 char *internal
= def
->exports
[i
].internal_name
;
1591 def
->exports
[i
].internal_name
= def
->exports
[i
].name
;
1592 n
= make_one (def
->exports
+ i
, outarch
);
1595 def
->exports
[i
].internal_name
= internal
;
1598 ar_tail
= make_tail (outarch
);
1600 if (ar_head
== NULL
|| ar_tail
== NULL
)
1603 /* Now stick them all into the archive. */
1605 ar_head
->next
= head
;
1606 ar_tail
->next
= ar_head
;
1609 if (! bfd_set_archive_head (outarch
, head
))
1610 einfo ("%Xbfd_set_archive_head: %s\n", bfd_errmsg (bfd_get_error ()));
1612 if (! bfd_close (outarch
))
1613 einfo ("%Xbfd_close %s: %s\n", impfilename
, bfd_errmsg (bfd_get_error ()));
1615 while (head
!= NULL
)
1617 bfd
*n
= head
->next
;
1624 add_bfd_to_link (abfd
, name
, link_info
)
1627 struct bfd_link_info
*link_info
;
1629 lang_input_statement_type
*fake_file
;
1630 fake_file
= lang_add_input_file (name
,
1631 lang_input_file_is_fake_enum
,
1633 fake_file
->the_bfd
= abfd
;
1634 ldlang_add_file (fake_file
);
1635 if (!bfd_link_add_symbols (abfd
, link_info
))
1636 einfo ("%Xaddsym %s: %s\n", name
, bfd_errmsg (bfd_get_error ()));
1640 pe_process_import_defs (output_bfd
, link_info
)
1642 struct bfd_link_info
*link_info
;
1644 def_file_module
*module
;
1645 pe_dll_id_target (bfd_get_target (output_bfd
));
1650 for (module
= pe_def_file
->modules
; module
; module
= module
->next
)
1654 dll_filename
= module
->name
;
1655 dll_symname
= xstrdup (module
->name
);
1656 for (i
= 0; dll_symname
[i
]; i
++)
1657 if (!isalnum (dll_symname
[i
]))
1658 dll_symname
[i
] = '_';
1662 for (i
= 0; i
< pe_def_file
->num_imports
; i
++)
1663 if (pe_def_file
->imports
[i
].module
== module
)
1665 def_file_export exp
;
1666 struct bfd_link_hash_entry
*blhe
;
1668 /* See if we need this import. */
1669 char *name
= (char *) xmalloc (strlen (pe_def_file
->imports
[i
].internal_name
) + 2 + 6);
1670 sprintf (name
, "%s%s", U (""), pe_def_file
->imports
[i
].internal_name
);
1671 blhe
= bfd_link_hash_lookup (link_info
->hash
, name
,
1672 false, false, false);
1673 if (!blhe
|| (blhe
&& blhe
->type
!= bfd_link_hash_undefined
))
1675 sprintf (name
, "%s%s", U ("_imp__"),
1676 pe_def_file
->imports
[i
].internal_name
);
1677 blhe
= bfd_link_hash_lookup (link_info
->hash
, name
,
1678 false, false, false);
1681 if (blhe
&& blhe
->type
== bfd_link_hash_undefined
)
1687 bfd
*ar_head
= make_head (output_bfd
);
1688 add_bfd_to_link (ar_head
, ar_head
->filename
, link_info
);
1691 exp
.internal_name
= pe_def_file
->imports
[i
].internal_name
;
1692 exp
.name
= pe_def_file
->imports
[i
].name
;
1693 exp
.ordinal
= pe_def_file
->imports
[i
].ordinal
;
1694 exp
.hint
= exp
.ordinal
>= 0 ? exp
.ordinal
: 0;
1695 exp
.flag_private
= 0;
1696 exp
.flag_constant
= 0;
1698 exp
.flag_noname
= exp
.name
? 0 : 1;
1699 one
= make_one (&exp
, output_bfd
);
1700 add_bfd_to_link (one
, one
->filename
, link_info
);
1705 bfd
*ar_tail
= make_tail (output_bfd
);
1706 add_bfd_to_link (ar_tail
, ar_tail
->filename
, link_info
);
1713 /************************************************************************
1715 We were handed a *.DLL file. Parse it and turn it into a set of
1716 IMPORTS directives in the def file. Return true if the file was
1717 handled, false if not.
1719 ************************************************************************/
1722 pe_get16 (abfd
, where
)
1727 bfd_seek (abfd
, where
, SEEK_SET
);
1728 bfd_read (b
, 1, 2, abfd
);
1729 return b
[0] + (b
[1] << 8);
1733 pe_get32 (abfd
, where
)
1738 bfd_seek (abfd
, where
, SEEK_SET
);
1739 bfd_read (b
, 1, 4, abfd
);
1740 return b
[0] + (b
[1] << 8) + (b
[2] << 16) + (b
[3] << 24);
1743 #if 0 /* This is not currently used. */
1749 unsigned char *b
= ptr
;
1750 return b
[0] + (b
[1] << 8);
1759 unsigned char *b
= ptr
;
1760 return b
[0] + (b
[1] << 8) + (b
[2] << 16) + (b
[3] << 24);
1764 pe_implied_import_dll (filename
)
1765 const char *filename
;
1768 unsigned long pe_header_offset
, opthdr_ofs
, num_entries
, i
;
1769 unsigned long export_rva
, export_size
, nsections
, secptr
, expptr
;
1770 unsigned char *expdata
, *erva
;
1771 unsigned long name_rvas
, ordinals
, nexp
, ordbase
;
1772 const char *dll_name
;
1774 /* No, I can't use bfd here. kernel32.dll puts its export table in
1775 the middle of the .rdata section. */
1777 dll
= bfd_openr (filename
, pe_details
->target_name
);
1780 einfo ("%Xopen %s: %s\n", filename
, bfd_errmsg (bfd_get_error ()));
1783 /* PEI dlls seem to be bfd_objects. */
1784 if (!bfd_check_format (dll
, bfd_object
))
1786 einfo ("%X%s: this doesn't appear to be a DLL\n", filename
);
1790 dll_name
= filename
;
1791 for (i
= 0; filename
[i
]; i
++)
1792 if (filename
[i
] == '/' || filename
[i
] == '\\' || filename
[i
] == ':')
1793 dll_name
= filename
+ i
+ 1;
1795 pe_header_offset
= pe_get32 (dll
, 0x3c);
1796 opthdr_ofs
= pe_header_offset
+ 4 + 20;
1797 num_entries
= pe_get32 (dll
, opthdr_ofs
+ 92);
1798 if (num_entries
< 1) /* no exports */
1800 export_rva
= pe_get32 (dll
, opthdr_ofs
+ 96);
1801 export_size
= pe_get32 (dll
, opthdr_ofs
+ 100);
1802 nsections
= pe_get16 (dll
, pe_header_offset
+ 4 + 2);
1803 secptr
= (pe_header_offset
+ 4 + 20 +
1804 pe_get16 (dll
, pe_header_offset
+ 4 + 16));
1806 for (i
= 0; i
< nsections
; i
++)
1809 unsigned long secptr1
= secptr
+ 40 * i
;
1810 unsigned long vaddr
= pe_get32 (dll
, secptr1
+ 12);
1811 unsigned long vsize
= pe_get32 (dll
, secptr1
+ 16);
1812 unsigned long fptr
= pe_get32 (dll
, secptr1
+ 20);
1813 bfd_seek (dll
, secptr1
, SEEK_SET
);
1814 bfd_read (sname
, 1, 8, dll
);
1815 if (vaddr
<= export_rva
&& vaddr
+ vsize
> export_rva
)
1817 expptr
= fptr
+ (export_rva
- vaddr
);
1818 if (export_rva
+ export_size
> vaddr
+ vsize
)
1819 export_size
= vsize
- (export_rva
- vaddr
);
1824 expdata
= (unsigned char *) xmalloc (export_size
);
1825 bfd_seek (dll
, expptr
, SEEK_SET
);
1826 bfd_read (expdata
, 1, export_size
, dll
);
1827 erva
= expdata
- export_rva
;
1829 if (pe_def_file
== 0)
1830 pe_def_file
= def_file_empty ();
1832 nexp
= pe_as32 (expdata
+ 24);
1833 name_rvas
= pe_as32 (expdata
+ 32);
1834 ordinals
= pe_as32 (expdata
+ 36);
1835 ordbase
= pe_as32 (expdata
+ 16);
1836 for (i
= 0; i
< nexp
; i
++)
1838 unsigned long name_rva
= pe_as32 (erva
+ name_rvas
+ i
* 4);
1839 def_file_import
*imp
;
1840 imp
= def_file_add_import (pe_def_file
, erva
+ name_rva
, dll_name
,
1847 /************************************************************************
1849 These are the main functions, called from the emulation. The first
1850 is called after the bfds are read, so we can guess at how much space
1851 we need. The second is called after everything is placed, so we
1852 can put the right values in place.
1854 ************************************************************************/
1857 pe_dll_build_sections (abfd
, info
)
1859 struct bfd_link_info
*info
;
1861 pe_dll_id_target (bfd_get_target (abfd
));
1862 process_def_file (abfd
, info
);
1864 generate_edata (abfd
, info
);
1865 build_filler_bfd (1);
1869 pe_exe_build_sections (abfd
, info
)
1871 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
1873 pe_dll_id_target (bfd_get_target (abfd
));
1874 build_filler_bfd (0);
1878 pe_dll_fill_sections (abfd
, info
)
1880 struct bfd_link_info
*info
;
1882 pe_dll_id_target (bfd_get_target (abfd
));
1883 image_base
= pe_data (abfd
)->pe_opthdr
.ImageBase
;
1885 generate_reloc (abfd
, info
);
1888 bfd_set_section_size (filler_bfd
, reloc_s
, reloc_sz
);
1890 /* Resize the sections. */
1891 lang_size_sections (stat_ptr
->head
, abs_output_section
,
1892 &stat_ptr
->head
, 0, (bfd_vma
) 0, false);
1894 /* Redo special stuff. */
1895 ldemul_after_allocation ();
1897 /* Do the assignments again. */
1898 lang_do_assignments (stat_ptr
->head
,
1900 (fill_type
) 0, (bfd_vma
) 0);
1903 fill_edata (abfd
, info
);
1905 pe_data (abfd
)->dll
= 1;
1907 edata_s
->contents
= edata_d
;
1908 reloc_s
->contents
= reloc_d
;
1912 pe_exe_fill_sections (abfd
, info
)
1914 struct bfd_link_info
*info
;
1916 pe_dll_id_target (bfd_get_target (abfd
));
1917 image_base
= pe_data (abfd
)->pe_opthdr
.ImageBase
;
1919 generate_reloc (abfd
, info
);
1922 bfd_set_section_size (filler_bfd
, reloc_s
, reloc_sz
);
1924 /* Resize the sections. */
1925 lang_size_sections (stat_ptr
->head
, abs_output_section
,
1926 &stat_ptr
->head
, 0, (bfd_vma
) 0, false);
1928 /* Redo special stuff. */
1929 ldemul_after_allocation ();
1931 /* Do the assignments again. */
1932 lang_do_assignments (stat_ptr
->head
,
1934 (fill_type
) 0, (bfd_vma
) 0);
1936 reloc_s
->contents
= reloc_d
;