| 1 | /* Routines to help build PEI-format DLLs (Win32 etc)
|
|---|
| 2 | Copyright 1998, 1999, 2000 Free Software Foundation, Inc.
|
|---|
| 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"
|
|---|
| 37 | #include "ldfile.h"
|
|---|
| 38 | #include "ldemul.h"
|
|---|
| 39 | #include "coff/internal.h"
|
|---|
| 40 | #include "../bfd/libcoff.h"
|
|---|
| 41 | #include "deffile.h"
|
|---|
| 42 | #include "pe-dll.h"
|
|---|
| 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 |
|
|---|
| 57 | /* for emultempl/pe.em */
|
|---|
| 58 |
|
|---|
| 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;
|
|---|
| 66 |
|
|---|
| 67 | /************************************************************************
|
|---|
| 68 |
|
|---|
| 69 | static variables and types
|
|---|
| 70 |
|
|---|
| 71 | ************************************************************************/
|
|---|
| 72 |
|
|---|
| 73 | static bfd_vma image_base;
|
|---|
| 74 |
|
|---|
| 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;
|
|---|
| 79 |
|
|---|
| 80 | typedef struct {
|
|---|
| 81 | char *target_name;
|
|---|
| 82 | char *object_target;
|
|---|
| 83 | unsigned int imagebase_reloc;
|
|---|
| 84 | int pe_arch;
|
|---|
| 85 | int bfd_arch;
|
|---|
| 86 | int underscored;
|
|---|
| 87 | } pe_details_type;
|
|---|
| 88 |
|
|---|
| 89 | #define PE_ARCH_i386 1
|
|---|
| 90 | #define PE_ARCH_sh 2
|
|---|
| 91 | #define PE_ARCH_mips 3
|
|---|
| 92 | #define PE_ARCH_arm 4
|
|---|
| 93 |
|
|---|
| 94 | static pe_details_type pe_detail_list[] = {
|
|---|
| 95 | {
|
|---|
| 96 | "pei-i386",
|
|---|
| 97 | "pe-i386",
|
|---|
| 98 | 7 /* R_IMAGEBASE */,
|
|---|
| 99 | PE_ARCH_i386,
|
|---|
| 100 | bfd_arch_i386,
|
|---|
| 101 | 1
|
|---|
| 102 | },
|
|---|
| 103 | {
|
|---|
| 104 | "pei-shl",
|
|---|
| 105 | "pe-shl",
|
|---|
| 106 | 16 /* R_SH_IMAGEBASE */,
|
|---|
| 107 | PE_ARCH_sh,
|
|---|
| 108 | bfd_arch_sh,
|
|---|
| 109 | 1
|
|---|
| 110 | },
|
|---|
| 111 | {
|
|---|
| 112 | "pei-mips",
|
|---|
| 113 | "pe-mips",
|
|---|
| 114 | 34 /* MIPS_R_RVA */,
|
|---|
| 115 | PE_ARCH_mips,
|
|---|
| 116 | bfd_arch_mips,
|
|---|
| 117 | 0
|
|---|
| 118 | },
|
|---|
| 119 | {
|
|---|
| 120 | "pei-arm-little",
|
|---|
| 121 | "pe-arm-little",
|
|---|
| 122 | 11 /* ARM_RVA32 */,
|
|---|
| 123 | PE_ARCH_arm,
|
|---|
| 124 | bfd_arch_arm,
|
|---|
| 125 | 0
|
|---|
| 126 | },
|
|---|
| 127 | { NULL, NULL, 0, 0, 0, 0 }
|
|---|
| 128 | };
|
|---|
| 129 |
|
|---|
| 130 | static pe_details_type *pe_details;
|
|---|
| 131 |
|
|---|
| 132 | #define U(str) (pe_details->underscored ? "_" str : str)
|
|---|
| 133 |
|
|---|
| 134 | void
|
|---|
| 135 | pe_dll_id_target (target)
|
|---|
| 136 | const char *target;
|
|---|
| 137 | {
|
|---|
| 138 | int i;
|
|---|
| 139 | for (i = 0; pe_detail_list[i].target_name; i++)
|
|---|
| 140 | if (strcmp (pe_detail_list[i].target_name, target) == 0
|
|---|
| 141 | || strcmp (pe_detail_list[i].object_target, target) == 0)
|
|---|
| 142 | {
|
|---|
| 143 | pe_details = pe_detail_list + i;
|
|---|
| 144 | return;
|
|---|
| 145 | }
|
|---|
| 146 | einfo (_("%XUnsupported PEI architecture: %s\n"), target);
|
|---|
| 147 | exit (1);
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | /************************************************************************
|
|---|
| 151 |
|
|---|
| 152 | Helper functions for qsort. Relocs must be sorted so that we can write
|
|---|
| 153 | them out by pages.
|
|---|
| 154 |
|
|---|
| 155 | ************************************************************************/
|
|---|
| 156 |
|
|---|
| 157 | typedef struct {
|
|---|
| 158 | bfd_vma vma;
|
|---|
| 159 | char type;
|
|---|
| 160 | short extra;
|
|---|
| 161 | } reloc_data_type;
|
|---|
| 162 |
|
|---|
| 163 | static int
|
|---|
| 164 | reloc_sort (va, vb)
|
|---|
| 165 | const void *va, *vb;
|
|---|
| 166 | {
|
|---|
| 167 | bfd_vma a = ((reloc_data_type *) va)->vma;
|
|---|
| 168 | bfd_vma b = ((reloc_data_type *) vb)->vma;
|
|---|
| 169 | return (a > b) ? 1 : ((a < b) ? -1 : 0);
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | static int
|
|---|
| 173 | pe_export_sort (va, vb)
|
|---|
| 174 | const void *va, *vb;
|
|---|
| 175 | {
|
|---|
| 176 | def_file_export *a = (def_file_export *) va;
|
|---|
| 177 | def_file_export *b = (def_file_export *) vb;
|
|---|
| 178 | return strcmp (a->name, b->name);
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | /************************************************************************
|
|---|
| 182 |
|
|---|
| 183 | Read and process the .DEF file
|
|---|
| 184 |
|
|---|
| 185 | ************************************************************************/
|
|---|
| 186 |
|
|---|
| 187 | /* These correspond to the entries in pe_def_file->exports[]. I use
|
|---|
| 188 | exported_symbol_sections[i] to tag whether or not the symbol was
|
|---|
| 189 | defined, since we can't export symbols we don't have. */
|
|---|
| 190 |
|
|---|
| 191 | static bfd_vma *exported_symbol_offsets;
|
|---|
| 192 | static struct sec **exported_symbol_sections;
|
|---|
| 193 |
|
|---|
| 194 | static int export_table_size;
|
|---|
| 195 | static int count_exported;
|
|---|
| 196 | static int count_exported_byname;
|
|---|
| 197 | static int count_with_ordinals;
|
|---|
| 198 | static const char *dll_name;
|
|---|
| 199 | static int min_ordinal, max_ordinal;
|
|---|
| 200 | static int *exported_symbols;
|
|---|
| 201 |
|
|---|
| 202 | typedef struct exclude_list_struct {
|
|---|
| 203 | char *string;
|
|---|
| 204 | struct exclude_list_struct *next;
|
|---|
| 205 | } exclude_list_struct;
|
|---|
| 206 |
|
|---|
| 207 | static struct exclude_list_struct *excludes = 0;
|
|---|
| 208 |
|
|---|
| 209 | void
|
|---|
| 210 | pe_dll_add_excludes (new_excludes)
|
|---|
| 211 | const char *new_excludes;
|
|---|
| 212 | {
|
|---|
| 213 | char *local_copy;
|
|---|
| 214 | char *exclude_string;
|
|---|
| 215 |
|
|---|
| 216 | local_copy = xstrdup (new_excludes);
|
|---|
| 217 |
|
|---|
| 218 | exclude_string = strtok (local_copy, ",:");
|
|---|
| 219 | for (; exclude_string; exclude_string = strtok (NULL, ",:"))
|
|---|
| 220 | {
|
|---|
| 221 | struct exclude_list_struct *new_exclude;
|
|---|
| 222 |
|
|---|
| 223 | new_exclude = ((struct exclude_list_struct *)
|
|---|
| 224 | xmalloc (sizeof (struct exclude_list_struct)));
|
|---|
| 225 | new_exclude->string = (char *) xmalloc (strlen (exclude_string) + 1);
|
|---|
| 226 | strcpy (new_exclude->string, exclude_string);
|
|---|
| 227 | new_exclude->next = excludes;
|
|---|
| 228 | excludes = new_exclude;
|
|---|
| 229 | }
|
|---|
| 230 |
|
|---|
| 231 | free (local_copy);
|
|---|
| 232 | }
|
|---|
| 233 |
|
|---|
| 234 | static int
|
|---|
| 235 | auto_export (d, n)
|
|---|
| 236 | def_file *d;
|
|---|
| 237 | const char *n;
|
|---|
| 238 | {
|
|---|
| 239 | int i;
|
|---|
| 240 | struct exclude_list_struct *ex;
|
|---|
| 241 | for (i = 0; i < d->num_exports; i++)
|
|---|
| 242 | if (strcmp (d->exports[i].name, n) == 0)
|
|---|
| 243 | return 0;
|
|---|
| 244 | if (pe_dll_do_default_excludes)
|
|---|
| 245 | {
|
|---|
| 246 | if (strcmp (n, "DllMain@12") == 0)
|
|---|
| 247 | return 0;
|
|---|
| 248 | if (strcmp (n, "DllEntryPoint@0") == 0)
|
|---|
| 249 | return 0;
|
|---|
| 250 | if (strcmp (n, "impure_ptr") == 0)
|
|---|
| 251 | return 0;
|
|---|
| 252 | }
|
|---|
| 253 | for (ex = excludes; ex; ex = ex->next)
|
|---|
| 254 | if (strcmp (n, ex->string) == 0)
|
|---|
| 255 | return 0;
|
|---|
| 256 | return 1;
|
|---|
| 257 | }
|
|---|
| 258 |
|
|---|
| 259 | static void
|
|---|
| 260 | process_def_file (abfd, info)
|
|---|
| 261 | bfd *abfd ATTRIBUTE_UNUSED;
|
|---|
| 262 | struct bfd_link_info *info;
|
|---|
| 263 | {
|
|---|
| 264 | int i, j;
|
|---|
| 265 | struct bfd_link_hash_entry *blhe;
|
|---|
| 266 | bfd *b;
|
|---|
| 267 | struct sec *s;
|
|---|
| 268 | def_file_export *e = 0;
|
|---|
| 269 |
|
|---|
| 270 | if (!pe_def_file)
|
|---|
| 271 | pe_def_file = def_file_empty ();
|
|---|
| 272 |
|
|---|
| 273 | /* First, run around to all the objects looking for the .drectve
|
|---|
| 274 | sections, and push those into the def file too. */
|
|---|
| 275 |
|
|---|
| 276 | for (b = info->input_bfds; b; b = b->link_next)
|
|---|
| 277 | {
|
|---|
| 278 | s = bfd_get_section_by_name (b, ".drectve");
|
|---|
| 279 | if (s)
|
|---|
| 280 | {
|
|---|
| 281 | int size = bfd_get_section_size_before_reloc (s);
|
|---|
| 282 | char *buf = xmalloc (size);
|
|---|
| 283 | bfd_get_section_contents (b, s, buf, 0, size);
|
|---|
| 284 | def_file_add_directive (pe_def_file, buf, size);
|
|---|
| 285 | free (buf);
|
|---|
| 286 | }
|
|---|
| 287 | }
|
|---|
| 288 |
|
|---|
| 289 | /* Now, maybe export everything else the default way. */
|
|---|
| 290 |
|
|---|
| 291 | if (pe_dll_export_everything || pe_def_file->num_exports == 0)
|
|---|
| 292 | {
|
|---|
| 293 | for (b = info->input_bfds; b; b = b->link_next)
|
|---|
| 294 | {
|
|---|
| 295 | asymbol **symbols;
|
|---|
| 296 | int nsyms, symsize;
|
|---|
| 297 |
|
|---|
| 298 | symsize = bfd_get_symtab_upper_bound (b);
|
|---|
| 299 | symbols = (asymbol **) xmalloc (symsize);
|
|---|
| 300 | nsyms = bfd_canonicalize_symtab (b, symbols);
|
|---|
| 301 |
|
|---|
| 302 | for (j = 0; j < nsyms; j++)
|
|---|
| 303 | {
|
|---|
| 304 | /* We should export symbols which are either global or not
|
|---|
| 305 | anything at all. (.bss data is the latter) */
|
|---|
| 306 | if ((symbols[j]->flags & BSF_GLOBAL)
|
|---|
| 307 | || (symbols[j]->flags == BSF_NO_FLAGS))
|
|---|
| 308 | {
|
|---|
| 309 | const char *sn = symbols[j]->name;
|
|---|
| 310 | if (*sn == '_')
|
|---|
| 311 | sn++;
|
|---|
| 312 | if (auto_export (pe_def_file, sn))
|
|---|
| 313 | {
|
|---|
| 314 | def_file_export *p;
|
|---|
| 315 | p=def_file_add_export (pe_def_file, sn, 0, -1);
|
|---|
| 316 | /* Fill data flag properly, from dlltool.c */
|
|---|
| 317 | p->flag_data = !(symbols[j]->flags & BSF_FUNCTION);
|
|---|
| 318 | }
|
|---|
| 319 | }
|
|---|
| 320 | }
|
|---|
| 321 | }
|
|---|
| 322 | }
|
|---|
| 323 |
|
|---|
| 324 | #undef NE
|
|---|
| 325 | #define NE pe_def_file->num_exports
|
|---|
| 326 |
|
|---|
| 327 | /* Canonicalize the export list. */
|
|---|
| 328 |
|
|---|
| 329 | if (pe_dll_kill_ats)
|
|---|
| 330 | {
|
|---|
| 331 | for (i = 0; i < NE; i++)
|
|---|
| 332 | {
|
|---|
| 333 | if (strchr (pe_def_file->exports[i].name, '@'))
|
|---|
| 334 | {
|
|---|
| 335 | /* This will preserve internal_name, which may have been
|
|---|
| 336 | pointing to the same memory as name, or might not
|
|---|
| 337 | have. */
|
|---|
| 338 | char *tmp = xstrdup (pe_def_file->exports[i].name);
|
|---|
| 339 | *(strchr (tmp, '@')) = 0;
|
|---|
| 340 | pe_def_file->exports[i].name = tmp;
|
|---|
| 341 | }
|
|---|
| 342 | }
|
|---|
| 343 | }
|
|---|
| 344 |
|
|---|
| 345 | if (pe_dll_stdcall_aliases)
|
|---|
| 346 | {
|
|---|
| 347 | for (i = 0; i < NE; i++)
|
|---|
| 348 | {
|
|---|
| 349 | if (strchr (pe_def_file->exports[i].name, '@'))
|
|---|
| 350 | {
|
|---|
| 351 | char *tmp = xstrdup (pe_def_file->exports[i].name);
|
|---|
| 352 | *(strchr (tmp, '@')) = 0;
|
|---|
| 353 | if (auto_export (pe_def_file, tmp))
|
|---|
| 354 | def_file_add_export (pe_def_file, tmp,
|
|---|
| 355 | pe_def_file->exports[i].internal_name, -1);
|
|---|
| 356 | else
|
|---|
| 357 | free (tmp);
|
|---|
| 358 | }
|
|---|
| 359 | }
|
|---|
| 360 | }
|
|---|
| 361 |
|
|---|
| 362 | /* Convenience, but watch out for it changing. */
|
|---|
| 363 | e = pe_def_file->exports;
|
|---|
| 364 |
|
|---|
| 365 | exported_symbol_offsets = (bfd_vma *) xmalloc (NE * sizeof (bfd_vma));
|
|---|
| 366 | exported_symbol_sections = (struct sec **) xmalloc (NE * sizeof (struct sec *));
|
|---|
| 367 |
|
|---|
| 368 | memset (exported_symbol_sections, 0, NE * sizeof (struct sec *));
|
|---|
| 369 | max_ordinal = 0;
|
|---|
| 370 | min_ordinal = 65536;
|
|---|
| 371 | count_exported = 0;
|
|---|
| 372 | count_exported_byname = 0;
|
|---|
| 373 | count_with_ordinals = 0;
|
|---|
| 374 |
|
|---|
| 375 | qsort (pe_def_file->exports, NE, sizeof (pe_def_file->exports[0]), pe_export_sort);
|
|---|
| 376 | for (i = 0, j = 0; i < NE; i++)
|
|---|
| 377 | {
|
|---|
| 378 | if (i > 0 && strcmp (e[i].name, e[i - 1].name) == 0)
|
|---|
| 379 | {
|
|---|
| 380 | /* This is a duplicate. */
|
|---|
| 381 | if (e[j - 1].ordinal != -1
|
|---|
| 382 | && e[i].ordinal != -1
|
|---|
| 383 | && e[j - 1].ordinal != e[i].ordinal)
|
|---|
| 384 | {
|
|---|
| 385 | if (pe_dll_warn_dup_exports)
|
|---|
| 386 | /* xgettext:c-format */
|
|---|
| 387 | einfo (_("%XError, duplicate EXPORT with ordinals: %s (%d vs %d)\n"),
|
|---|
| 388 | e[j - 1].name, e[j - 1].ordinal, e[i].ordinal);
|
|---|
| 389 | }
|
|---|
| 390 | else
|
|---|
| 391 | {
|
|---|
| 392 | if (pe_dll_warn_dup_exports)
|
|---|
| 393 | /* xgettext:c-format */
|
|---|
| 394 | einfo (_("Warning, duplicate EXPORT: %s\n"),
|
|---|
| 395 | e[j - 1].name);
|
|---|
| 396 | }
|
|---|
| 397 | if (e[i].ordinal != -1)
|
|---|
| 398 | e[j - 1].ordinal = e[i].ordinal;
|
|---|
| 399 | e[j - 1].flag_private |= e[i].flag_private;
|
|---|
| 400 | e[j - 1].flag_constant |= e[i].flag_constant;
|
|---|
| 401 | e[j - 1].flag_noname |= e[i].flag_noname;
|
|---|
| 402 | e[j - 1].flag_data |= e[i].flag_data;
|
|---|
| 403 | }
|
|---|
| 404 | else
|
|---|
| 405 | {
|
|---|
| 406 | if (i != j)
|
|---|
| 407 | e[j] = e[i];
|
|---|
| 408 | j++;
|
|---|
| 409 | }
|
|---|
| 410 | }
|
|---|
| 411 | pe_def_file->num_exports = j; /* == NE */
|
|---|
| 412 |
|
|---|
| 413 | for (i = 0; i < NE; i++)
|
|---|
| 414 | {
|
|---|
| 415 | char *name = (char *) xmalloc (strlen (pe_def_file->exports[i].internal_name) + 2);
|
|---|
| 416 | if (pe_details->underscored)
|
|---|
| 417 | {
|
|---|
| 418 | *name = '_';
|
|---|
| 419 | strcpy (name + 1, pe_def_file->exports[i].internal_name);
|
|---|
| 420 | }
|
|---|
| 421 | else
|
|---|
| 422 | strcpy (name, pe_def_file->exports[i].internal_name);
|
|---|
| 423 |
|
|---|
| 424 | blhe = bfd_link_hash_lookup (info->hash,
|
|---|
| 425 | name,
|
|---|
| 426 | false, false, true);
|
|---|
| 427 |
|
|---|
| 428 | if (blhe
|
|---|
| 429 | && (blhe->type == bfd_link_hash_defined
|
|---|
| 430 | || (blhe->type == bfd_link_hash_common)))
|
|---|
| 431 | {
|
|---|
| 432 | count_exported++;
|
|---|
| 433 | if (!pe_def_file->exports[i].flag_noname)
|
|---|
| 434 | count_exported_byname++;
|
|---|
| 435 |
|
|---|
| 436 | /* Only fill in the sections. The actual offsets are computed
|
|---|
| 437 | in fill_exported_offsets() after common symbols are laid
|
|---|
| 438 | out. */
|
|---|
| 439 | if (blhe->type == bfd_link_hash_defined)
|
|---|
| 440 | exported_symbol_sections[i] = blhe->u.def.section;
|
|---|
| 441 | else
|
|---|
| 442 | exported_symbol_sections[i] = blhe->u.c.p->section;
|
|---|
| 443 |
|
|---|
| 444 | if (pe_def_file->exports[i].ordinal != -1)
|
|---|
| 445 | {
|
|---|
| 446 | if (max_ordinal < pe_def_file->exports[i].ordinal)
|
|---|
| 447 | max_ordinal = pe_def_file->exports[i].ordinal;
|
|---|
| 448 | if (min_ordinal > pe_def_file->exports[i].ordinal)
|
|---|
| 449 | min_ordinal = pe_def_file->exports[i].ordinal;
|
|---|
| 450 | count_with_ordinals++;
|
|---|
| 451 | }
|
|---|
| 452 | }
|
|---|
| 453 | else if (blhe && blhe->type == bfd_link_hash_undefined)
|
|---|
| 454 | {
|
|---|
| 455 | /* xgettext:c-format */
|
|---|
| 456 | einfo (_("%XCannot export %s: symbol not defined\n"),
|
|---|
| 457 | pe_def_file->exports[i].internal_name);
|
|---|
| 458 | }
|
|---|
| 459 | else if (blhe)
|
|---|
| 460 | {
|
|---|
| 461 | /* xgettext:c-format */
|
|---|
| 462 | einfo (_("%XCannot export %s: symbol wrong type (%d vs %d)\n"),
|
|---|
| 463 | pe_def_file->exports[i].internal_name,
|
|---|
| 464 | blhe->type, bfd_link_hash_defined);
|
|---|
| 465 | }
|
|---|
| 466 | else
|
|---|
| 467 | {
|
|---|
| 468 | /* xgettext:c-format */
|
|---|
| 469 | einfo (_("%XCannot export %s: symbol not found\n"),
|
|---|
| 470 | pe_def_file->exports[i].internal_name);
|
|---|
| 471 | }
|
|---|
| 472 | free (name);
|
|---|
| 473 | }
|
|---|
| 474 | }
|
|---|
| 475 |
|
|---|
| 476 | /************************************************************************
|
|---|
| 477 |
|
|---|
| 478 | Build the bfd that will contain .edata and .reloc sections
|
|---|
| 479 |
|
|---|
| 480 | ************************************************************************/
|
|---|
| 481 |
|
|---|
| 482 | static void
|
|---|
| 483 | build_filler_bfd (include_edata)
|
|---|
| 484 | int include_edata;
|
|---|
| 485 | {
|
|---|
| 486 | lang_input_statement_type *filler_file;
|
|---|
| 487 | filler_file = lang_add_input_file ("dll stuff",
|
|---|
| 488 | lang_input_file_is_fake_enum,
|
|---|
| 489 | NULL);
|
|---|
| 490 | filler_file->the_bfd = filler_bfd = bfd_create ("dll stuff", output_bfd);
|
|---|
| 491 | if (filler_bfd == NULL
|
|---|
| 492 | || !bfd_set_arch_mach (filler_bfd,
|
|---|
| 493 | bfd_get_arch (output_bfd),
|
|---|
| 494 | bfd_get_mach (output_bfd)))
|
|---|
| 495 | {
|
|---|
| 496 | einfo ("%X%P: can not create BFD %E\n");
|
|---|
| 497 | return;
|
|---|
| 498 | }
|
|---|
| 499 |
|
|---|
| 500 | if (include_edata)
|
|---|
| 501 | {
|
|---|
| 502 | edata_s = bfd_make_section_old_way (filler_bfd, ".edata");
|
|---|
| 503 | if (edata_s == NULL
|
|---|
| 504 | || !bfd_set_section_flags (filler_bfd, edata_s,
|
|---|
| 505 | (SEC_HAS_CONTENTS
|
|---|
| 506 | | SEC_ALLOC
|
|---|
| 507 | | SEC_LOAD
|
|---|
| 508 | | SEC_KEEP
|
|---|
| 509 | | SEC_IN_MEMORY)))
|
|---|
| 510 | {
|
|---|
| 511 | einfo ("%X%P: can not create .edata section: %E\n");
|
|---|
| 512 | return;
|
|---|
| 513 | }
|
|---|
| 514 | bfd_set_section_size (filler_bfd, edata_s, edata_sz);
|
|---|
| 515 | }
|
|---|
| 516 |
|
|---|
| 517 | reloc_s = bfd_make_section_old_way (filler_bfd, ".reloc");
|
|---|
| 518 | if (reloc_s == NULL
|
|---|
| 519 | || !bfd_set_section_flags (filler_bfd, reloc_s,
|
|---|
| 520 | (SEC_HAS_CONTENTS
|
|---|
| 521 | | SEC_ALLOC
|
|---|
| 522 | | SEC_LOAD
|
|---|
| 523 | | SEC_KEEP
|
|---|
| 524 | | SEC_IN_MEMORY)))
|
|---|
| 525 | {
|
|---|
| 526 | einfo ("%X%P: can not create .reloc section: %E\n");
|
|---|
| 527 | return;
|
|---|
| 528 | }
|
|---|
| 529 | bfd_set_section_size (filler_bfd, reloc_s, 0);
|
|---|
| 530 |
|
|---|
| 531 | ldlang_add_file (filler_file);
|
|---|
| 532 | }
|
|---|
| 533 |
|
|---|
| 534 | /************************************************************************
|
|---|
| 535 |
|
|---|
| 536 | Gather all the exported symbols and build the .edata section
|
|---|
| 537 |
|
|---|
| 538 | ************************************************************************/
|
|---|
| 539 |
|
|---|
| 540 | static void
|
|---|
| 541 | generate_edata (abfd, info)
|
|---|
| 542 | bfd *abfd;
|
|---|
| 543 | struct bfd_link_info *info ATTRIBUTE_UNUSED;
|
|---|
| 544 | {
|
|---|
| 545 | int i, next_ordinal;
|
|---|
| 546 | int name_table_size = 0;
|
|---|
| 547 | const char *dlnp;
|
|---|
| 548 |
|
|---|
| 549 | /* First, we need to know how many exported symbols there are,
|
|---|
| 550 | and what the range of ordinals is. */
|
|---|
| 551 |
|
|---|
| 552 | if (pe_def_file->name)
|
|---|
| 553 | {
|
|---|
| 554 | dll_name = pe_def_file->name;
|
|---|
| 555 | }
|
|---|
| 556 | else
|
|---|
| 557 | {
|
|---|
| 558 | dll_name = abfd->filename;
|
|---|
| 559 | for (dlnp = dll_name; *dlnp; dlnp++)
|
|---|
| 560 | {
|
|---|
| 561 | if (*dlnp == '\\' || *dlnp == '/' || *dlnp == ':')
|
|---|
| 562 | dll_name = dlnp + 1;
|
|---|
| 563 | }
|
|---|
| 564 | }
|
|---|
| 565 |
|
|---|
| 566 | if (count_with_ordinals && max_ordinal > count_exported)
|
|---|
| 567 | {
|
|---|
| 568 | if (min_ordinal > max_ordinal - count_exported + 1)
|
|---|
| 569 | min_ordinal = max_ordinal - count_exported + 1;
|
|---|
| 570 | }
|
|---|
| 571 | else
|
|---|
| 572 | {
|
|---|
| 573 | min_ordinal = 1;
|
|---|
| 574 | max_ordinal = count_exported;
|
|---|
| 575 | }
|
|---|
| 576 | export_table_size = max_ordinal - min_ordinal + 1;
|
|---|
| 577 |
|
|---|
| 578 | exported_symbols = (int *) xmalloc (export_table_size * sizeof (int));
|
|---|
| 579 | for (i = 0; i < export_table_size; i++)
|
|---|
| 580 | exported_symbols[i] = -1;
|
|---|
| 581 |
|
|---|
| 582 | /* Now we need to assign ordinals to those that don't have them. */
|
|---|
| 583 | for (i = 0; i < NE; i++)
|
|---|
| 584 | {
|
|---|
| 585 | if (exported_symbol_sections[i])
|
|---|
| 586 | {
|
|---|
| 587 | if (pe_def_file->exports[i].ordinal != -1)
|
|---|
| 588 | {
|
|---|
| 589 | int ei = pe_def_file->exports[i].ordinal - min_ordinal;
|
|---|
| 590 | int pi = exported_symbols[ei];
|
|---|
| 591 | if (pi != -1)
|
|---|
| 592 | {
|
|---|
| 593 | /* xgettext:c-format */
|
|---|
| 594 | einfo (_("%XError, ordinal used twice: %d (%s vs %s)\n"),
|
|---|
| 595 | pe_def_file->exports[i].ordinal,
|
|---|
| 596 | pe_def_file->exports[i].name,
|
|---|
| 597 | pe_def_file->exports[pi].name);
|
|---|
| 598 | }
|
|---|
| 599 | exported_symbols[ei] = i;
|
|---|
| 600 | }
|
|---|
| 601 | name_table_size += strlen (pe_def_file->exports[i].name) + 1;
|
|---|
| 602 | }
|
|---|
| 603 | }
|
|---|
| 604 |
|
|---|
| 605 | next_ordinal = min_ordinal;
|
|---|
| 606 | for (i = 0; i < NE; i++)
|
|---|
| 607 | if (exported_symbol_sections[i])
|
|---|
| 608 | if (pe_def_file->exports[i].ordinal == -1)
|
|---|
| 609 | {
|
|---|
| 610 | while (exported_symbols[next_ordinal - min_ordinal] != -1)
|
|---|
| 611 | next_ordinal++;
|
|---|
| 612 | exported_symbols[next_ordinal - min_ordinal] = i;
|
|---|
| 613 | pe_def_file->exports[i].ordinal = next_ordinal;
|
|---|
| 614 | }
|
|---|
| 615 |
|
|---|
| 616 | /* OK, now we can allocate some memory. */
|
|---|
| 617 |
|
|---|
| 618 | edata_sz = (40 /* directory */
|
|---|
| 619 | + 4 * export_table_size /* addresses */
|
|---|
| 620 | + 4 * count_exported_byname /* name ptrs */
|
|---|
| 621 | + 2 * count_exported_byname /* ordinals */
|
|---|
| 622 | + name_table_size + strlen (dll_name) + 1);
|
|---|
| 623 | }
|
|---|
| 624 |
|
|---|
| 625 | /* Fill the exported symbol offsets. The preliminary work has already
|
|---|
| 626 | been done in process_def_file(). */
|
|---|
| 627 |
|
|---|
| 628 | static void
|
|---|
| 629 | fill_exported_offsets (abfd, info)
|
|---|
| 630 | bfd *abfd ATTRIBUTE_UNUSED;
|
|---|
| 631 | struct bfd_link_info *info;
|
|---|
| 632 | {
|
|---|
| 633 | int i;
|
|---|
| 634 | struct bfd_link_hash_entry *blhe;
|
|---|
| 635 |
|
|---|
| 636 | for (i = 0; i < pe_def_file->num_exports; i++)
|
|---|
| 637 | {
|
|---|
| 638 | char *name = (char *) xmalloc (strlen (pe_def_file->exports[i].internal_name) + 2);
|
|---|
| 639 | if (pe_details->underscored)
|
|---|
| 640 | {
|
|---|
| 641 | *name = '_';
|
|---|
| 642 | strcpy (name + 1, pe_def_file->exports[i].internal_name);
|
|---|
| 643 | }
|
|---|
| 644 | else
|
|---|
| 645 | strcpy (name, pe_def_file->exports[i].internal_name);
|
|---|
| 646 |
|
|---|
| 647 | blhe = bfd_link_hash_lookup (info->hash,
|
|---|
| 648 | name,
|
|---|
| 649 | false, false, true);
|
|---|
| 650 |
|
|---|
| 651 | if (blhe && (blhe->type == bfd_link_hash_defined))
|
|---|
| 652 | {
|
|---|
| 653 | exported_symbol_offsets[i] = blhe->u.def.value;
|
|---|
| 654 | }
|
|---|
| 655 | free (name);
|
|---|
| 656 | }
|
|---|
| 657 | }
|
|---|
| 658 |
|
|---|
| 659 | static void
|
|---|
| 660 | fill_edata (abfd, info)
|
|---|
| 661 | bfd *abfd;
|
|---|
| 662 | struct bfd_link_info *info ATTRIBUTE_UNUSED;
|
|---|
| 663 | {
|
|---|
| 664 | int i, hint;
|
|---|
| 665 | unsigned char *edirectory;
|
|---|
| 666 | unsigned long *eaddresses;
|
|---|
| 667 | unsigned long *enameptrs;
|
|---|
| 668 | unsigned short *eordinals;
|
|---|
| 669 | unsigned char *enamestr;
|
|---|
| 670 | time_t now;
|
|---|
| 671 |
|
|---|
| 672 | time (&now);
|
|---|
| 673 |
|
|---|
| 674 | edata_d = (unsigned char *) xmalloc (edata_sz);
|
|---|
| 675 |
|
|---|
| 676 | /* Note use of array pointer math here. */
|
|---|
| 677 | edirectory = edata_d;
|
|---|
| 678 | eaddresses = (unsigned long *) (edata_d + 40);
|
|---|
| 679 | enameptrs = eaddresses + export_table_size;
|
|---|
| 680 | eordinals = (unsigned short *) (enameptrs + count_exported_byname);
|
|---|
| 681 | enamestr = (char *) (eordinals + count_exported_byname);
|
|---|
| 682 |
|
|---|
| 683 | #define ERVA(ptr) (((unsigned char *)(ptr) - edata_d) + edata_s->output_section->vma - image_base)
|
|---|
| 684 |
|
|---|
| 685 | memset (edata_d, 0, edata_sz);
|
|---|
| 686 | bfd_put_32 (abfd, now, edata_d + 4);
|
|---|
| 687 | if (pe_def_file->version_major != -1)
|
|---|
| 688 | {
|
|---|
| 689 | bfd_put_16 (abfd, pe_def_file->version_major, edata_d + 8);
|
|---|
| 690 | bfd_put_16 (abfd, pe_def_file->version_minor, edata_d + 10);
|
|---|
| 691 | }
|
|---|
| 692 | bfd_put_32 (abfd, ERVA (enamestr), edata_d + 12);
|
|---|
| 693 | strcpy (enamestr, dll_name);
|
|---|
| 694 | enamestr += strlen (enamestr) + 1;
|
|---|
| 695 | bfd_put_32 (abfd, min_ordinal, edata_d + 16);
|
|---|
| 696 | bfd_put_32 (abfd, export_table_size, edata_d + 20);
|
|---|
| 697 | bfd_put_32 (abfd, count_exported_byname, edata_d + 24);
|
|---|
| 698 | bfd_put_32 (abfd, ERVA (eaddresses), edata_d + 28);
|
|---|
| 699 | bfd_put_32 (abfd, ERVA (enameptrs), edata_d + 32);
|
|---|
| 700 | bfd_put_32 (abfd, ERVA (eordinals), edata_d + 36);
|
|---|
| 701 |
|
|---|
| 702 | fill_exported_offsets (abfd, info);
|
|---|
| 703 |
|
|---|
| 704 | /* Ok, now for the filling in part. */
|
|---|
| 705 | hint = 0;
|
|---|
| 706 | for (i = 0; i < export_table_size; i++)
|
|---|
| 707 | {
|
|---|
| 708 | int s = exported_symbols[i];
|
|---|
| 709 | if (s != -1)
|
|---|
| 710 | {
|
|---|
| 711 | struct sec *ssec = exported_symbol_sections[s];
|
|---|
| 712 | unsigned long srva = (exported_symbol_offsets[s]
|
|---|
| 713 | + ssec->output_section->vma
|
|---|
| 714 | + ssec->output_offset);
|
|---|
| 715 | int ord = pe_def_file->exports[s].ordinal;
|
|---|
| 716 |
|
|---|
| 717 | bfd_put_32 (abfd, srva - image_base,
|
|---|
| 718 | (void *) (eaddresses + ord - min_ordinal));
|
|---|
| 719 | if (!pe_def_file->exports[s].flag_noname)
|
|---|
| 720 | {
|
|---|
| 721 | char *ename = pe_def_file->exports[s].name;
|
|---|
| 722 | bfd_put_32 (abfd, ERVA (enamestr), (void *) enameptrs);
|
|---|
| 723 | enameptrs++;
|
|---|
| 724 | strcpy (enamestr, ename);
|
|---|
| 725 | enamestr += strlen (enamestr) + 1;
|
|---|
| 726 | bfd_put_16 (abfd, ord - min_ordinal, (void *) eordinals);
|
|---|
| 727 | eordinals++;
|
|---|
| 728 | pe_def_file->exports[s].hint = hint++;
|
|---|
| 729 | }
|
|---|
| 730 | }
|
|---|
| 731 | }
|
|---|
| 732 | }
|
|---|
| 733 |
|
|---|
| 734 | /************************************************************************
|
|---|
| 735 |
|
|---|
| 736 | Gather all the relocations and build the .reloc section
|
|---|
| 737 |
|
|---|
| 738 | ************************************************************************/
|
|---|
| 739 |
|
|---|
| 740 | static void
|
|---|
| 741 | generate_reloc (abfd, info)
|
|---|
| 742 | bfd *abfd;
|
|---|
| 743 | struct bfd_link_info *info;
|
|---|
| 744 | {
|
|---|
| 745 |
|
|---|
| 746 | /* For .reloc stuff. */
|
|---|
| 747 | reloc_data_type *reloc_data;
|
|---|
| 748 | int total_relocs = 0;
|
|---|
| 749 | int i;
|
|---|
| 750 | unsigned long sec_page = (unsigned long) (-1);
|
|---|
| 751 | unsigned long page_ptr, page_count;
|
|---|
| 752 | int bi;
|
|---|
| 753 | bfd *b;
|
|---|
| 754 | struct sec *s;
|
|---|
| 755 |
|
|---|
| 756 | total_relocs = 0;
|
|---|
| 757 | for (b = info->input_bfds; b; b = b->link_next)
|
|---|
| 758 | for (s = b->sections; s; s = s->next)
|
|---|
| 759 | total_relocs += s->reloc_count;
|
|---|
| 760 |
|
|---|
| 761 | reloc_data = (reloc_data_type *) xmalloc (total_relocs * sizeof (reloc_data_type));
|
|---|
| 762 |
|
|---|
| 763 | total_relocs = 0;
|
|---|
| 764 | bi = 0;
|
|---|
| 765 | for (bi = 0, b = info->input_bfds; b; bi++, b = b->link_next)
|
|---|
| 766 | {
|
|---|
| 767 | arelent **relocs;
|
|---|
| 768 | int relsize, nrelocs, i;
|
|---|
| 769 |
|
|---|
| 770 | for (s = b->sections; s; s = s->next)
|
|---|
| 771 | {
|
|---|
| 772 | unsigned long sec_vma = s->output_section->vma + s->output_offset;
|
|---|
| 773 | asymbol **symbols;
|
|---|
| 774 | int nsyms, symsize;
|
|---|
| 775 |
|
|---|
| 776 | /* If it's not loaded, we don't need to relocate it this way. */
|
|---|
| 777 | if (!(s->output_section->flags & SEC_LOAD))
|
|---|
| 778 | continue;
|
|---|
| 779 |
|
|---|
| 780 | /* I don't know why there would be a reloc for these, but I've
|
|---|
| 781 | seen it happen - DJ */
|
|---|
| 782 | if (s->output_section == &bfd_abs_section)
|
|---|
| 783 | continue;
|
|---|
| 784 |
|
|---|
| 785 | if (s->output_section->vma == 0)
|
|---|
| 786 | {
|
|---|
| 787 | /* Huh? Shouldn't happen, but punt if it does. */
|
|---|
| 788 | einfo ("DJ: zero vma section reloc detected: `%s' #%d f=%d\n",
|
|---|
| 789 | s->output_section->name, s->output_section->index,
|
|---|
| 790 | s->output_section->flags);
|
|---|
| 791 | continue;
|
|---|
| 792 | }
|
|---|
| 793 |
|
|---|
| 794 | symsize = bfd_get_symtab_upper_bound (b);
|
|---|
| 795 | symbols = (asymbol **) xmalloc (symsize);
|
|---|
| 796 | nsyms = bfd_canonicalize_symtab (b, symbols);
|
|---|
| 797 |
|
|---|
| 798 | relsize = bfd_get_reloc_upper_bound (b, s);
|
|---|
| 799 | relocs = (arelent **) xmalloc ((size_t) relsize);
|
|---|
| 800 | nrelocs = bfd_canonicalize_reloc (b, s, relocs, symbols);
|
|---|
| 801 |
|
|---|
| 802 | for (i = 0; i < nrelocs; i++)
|
|---|
| 803 | {
|
|---|
| 804 | if (!relocs[i]->howto->pc_relative
|
|---|
| 805 | && relocs[i]->howto->type != pe_details->imagebase_reloc)
|
|---|
| 806 | {
|
|---|
| 807 | bfd_vma sym_vma;
|
|---|
| 808 | struct symbol_cache_entry *sym = *relocs[i]->sym_ptr_ptr;
|
|---|
| 809 | sym_vma = (relocs[i]->addend
|
|---|
| 810 | + sym->value
|
|---|
| 811 | + sym->section->vma
|
|---|
| 812 | + sym->section->output_offset
|
|---|
| 813 | + sym->section->output_section->vma);
|
|---|
| 814 | reloc_data[total_relocs].vma = sec_vma + relocs[i]->address;
|
|---|
| 815 |
|
|---|
| 816 | #define BITS_AND_SHIFT(bits, shift) (bits * 1000 | shift)
|
|---|
| 817 |
|
|---|
| 818 | switch BITS_AND_SHIFT (relocs[i]->howto->bitsize,
|
|---|
| 819 | relocs[i]->howto->rightshift)
|
|---|
| 820 | {
|
|---|
| 821 | case BITS_AND_SHIFT (32, 0):
|
|---|
| 822 | reloc_data[total_relocs].type = 3;
|
|---|
| 823 | total_relocs++;
|
|---|
| 824 | break;
|
|---|
| 825 | case BITS_AND_SHIFT (16, 0):
|
|---|
| 826 | reloc_data[total_relocs].type = 2;
|
|---|
| 827 | total_relocs++;
|
|---|
| 828 | break;
|
|---|
| 829 | case BITS_AND_SHIFT (16, 16):
|
|---|
| 830 | reloc_data[total_relocs].type = 4;
|
|---|
| 831 | /* FIXME: we can't know the symbol's right value
|
|---|
| 832 | yet, but we probably can safely assume that
|
|---|
| 833 | CE will relocate us in 64k blocks, so leaving
|
|---|
| 834 | it zero is safe. */
|
|---|
| 835 | reloc_data[total_relocs].extra = 0;
|
|---|
| 836 | total_relocs++;
|
|---|
| 837 | break;
|
|---|
| 838 | case BITS_AND_SHIFT (26, 2):
|
|---|
| 839 | reloc_data[total_relocs].type = 5;
|
|---|
| 840 | total_relocs++;
|
|---|
| 841 | break;
|
|---|
| 842 | default:
|
|---|
| 843 | /* xgettext:c-format */
|
|---|
| 844 | einfo (_("%XError: %d-bit reloc in dll\n"),
|
|---|
| 845 | relocs[i]->howto->bitsize);
|
|---|
| 846 | break;
|
|---|
| 847 | }
|
|---|
| 848 | }
|
|---|
| 849 | }
|
|---|
| 850 | free (relocs);
|
|---|
| 851 | /* Warning: the allocated symbols are remembered in BFD and
|
|---|
| 852 | reused later, so don't free them! */
|
|---|
| 853 | #if 0
|
|---|
| 854 | free (symbol);
|
|---|
| 855 | #endif
|
|---|
| 856 | }
|
|---|
| 857 | }
|
|---|
| 858 |
|
|---|
| 859 | /* At this point, we have total_relocs relocation addresses in
|
|---|
| 860 | reloc_addresses, which are all suitable for the .reloc section.
|
|---|
| 861 | We must now create the new sections. */
|
|---|
| 862 |
|
|---|
| 863 | qsort (reloc_data, total_relocs, sizeof (*reloc_data), reloc_sort);
|
|---|
| 864 |
|
|---|
| 865 | for (i = 0; i < total_relocs; i++)
|
|---|
| 866 | {
|
|---|
| 867 | unsigned long this_page = (reloc_data[i].vma >> 12);
|
|---|
| 868 |
|
|---|
| 869 | if (this_page != sec_page)
|
|---|
| 870 | {
|
|---|
| 871 | reloc_sz = (reloc_sz + 3) & ~3; /* 4-byte align */
|
|---|
| 872 | reloc_sz += 8;
|
|---|
| 873 | sec_page = this_page;
|
|---|
| 874 | }
|
|---|
| 875 |
|
|---|
| 876 | reloc_sz += 2;
|
|---|
| 877 |
|
|---|
| 878 | if (reloc_data[i].type == 4)
|
|---|
| 879 | reloc_sz += 2;
|
|---|
| 880 | }
|
|---|
| 881 | reloc_sz = (reloc_sz + 3) & ~3; /* 4-byte align */
|
|---|
| 882 |
|
|---|
| 883 | reloc_d = (unsigned char *) xmalloc (reloc_sz);
|
|---|
| 884 |
|
|---|
| 885 | sec_page = (unsigned long) (-1);
|
|---|
| 886 | reloc_sz = 0;
|
|---|
| 887 | page_ptr = (unsigned long) (-1);
|
|---|
| 888 | page_count = 0;
|
|---|
| 889 | for (i = 0; i < total_relocs; i++)
|
|---|
| 890 | {
|
|---|
| 891 | unsigned long rva = reloc_data[i].vma - image_base;
|
|---|
| 892 | unsigned long this_page = (rva & ~0xfff);
|
|---|
| 893 | if (this_page != sec_page)
|
|---|
| 894 | {
|
|---|
| 895 | while (reloc_sz & 3)
|
|---|
| 896 | reloc_d[reloc_sz++] = 0;
|
|---|
| 897 | if (page_ptr != (unsigned long) (-1))
|
|---|
| 898 | bfd_put_32 (abfd, reloc_sz - page_ptr, reloc_d + page_ptr + 4);
|
|---|
| 899 | bfd_put_32 (abfd, this_page, reloc_d + reloc_sz);
|
|---|
| 900 | page_ptr = reloc_sz;
|
|---|
| 901 | reloc_sz += 8;
|
|---|
| 902 | sec_page = this_page;
|
|---|
| 903 | page_count = 0;
|
|---|
| 904 | }
|
|---|
| 905 | bfd_put_16 (abfd, (rva & 0xfff) + (reloc_data[i].type << 12),
|
|---|
| 906 | reloc_d + reloc_sz);
|
|---|
| 907 | reloc_sz += 2;
|
|---|
| 908 | if (reloc_data[i].type == 4)
|
|---|
| 909 | {
|
|---|
| 910 | bfd_put_16 (abfd, reloc_data[i].extra, reloc_d + reloc_sz);
|
|---|
| 911 | reloc_sz += 2;
|
|---|
| 912 | }
|
|---|
| 913 | page_count++;
|
|---|
| 914 | }
|
|---|
| 915 | while (reloc_sz & 3)
|
|---|
| 916 | reloc_d[reloc_sz++] = 0;
|
|---|
| 917 | if (page_ptr != (unsigned long) (-1))
|
|---|
| 918 | bfd_put_32 (abfd, reloc_sz - page_ptr, reloc_d + page_ptr + 4);
|
|---|
| 919 | while (reloc_sz < reloc_s->_raw_size)
|
|---|
| 920 | reloc_d[reloc_sz++] = 0;
|
|---|
| 921 | }
|
|---|
| 922 |
|
|---|
| 923 | /************************************************************************
|
|---|
| 924 |
|
|---|
| 925 | Given the exiting def_file structure, print out a .DEF file that
|
|---|
| 926 | corresponds to it.
|
|---|
| 927 |
|
|---|
| 928 | ************************************************************************/
|
|---|
| 929 |
|
|---|
| 930 | static void
|
|---|
| 931 | quoteput (s, f, needs_quotes)
|
|---|
| 932 | char *s;
|
|---|
| 933 | FILE *f;
|
|---|
| 934 | int needs_quotes;
|
|---|
| 935 | {
|
|---|
| 936 | char *cp;
|
|---|
| 937 | for (cp = s; *cp; cp++)
|
|---|
| 938 | if (*cp == '\''
|
|---|
| 939 | || *cp == '"'
|
|---|
| 940 | || *cp == '\\'
|
|---|
| 941 | || isspace ((unsigned char) *cp)
|
|---|
| 942 | || *cp == ','
|
|---|
| 943 | || *cp == ';')
|
|---|
| 944 | needs_quotes = 1;
|
|---|
| 945 | if (needs_quotes)
|
|---|
| 946 | {
|
|---|
| 947 | putc ('"', f);
|
|---|
| 948 | while (*s)
|
|---|
| 949 | {
|
|---|
| 950 | if (*s == '"' || *s == '\\')
|
|---|
| 951 | putc ('\\', f);
|
|---|
| 952 | putc (*s, f);
|
|---|
| 953 | s++;
|
|---|
| 954 | }
|
|---|
| 955 | putc ('"', f);
|
|---|
| 956 | }
|
|---|
| 957 | else
|
|---|
| 958 | fputs (s, f);
|
|---|
| 959 | }
|
|---|
| 960 |
|
|---|
| 961 | void
|
|---|
| 962 | pe_dll_generate_def_file (pe_out_def_filename)
|
|---|
| 963 | const char *pe_out_def_filename;
|
|---|
| 964 | {
|
|---|
| 965 | int i;
|
|---|
| 966 | FILE *out = fopen (pe_out_def_filename, "w");
|
|---|
| 967 | if (out == NULL)
|
|---|
| 968 | {
|
|---|
| 969 | /* xgettext:c-format */
|
|---|
| 970 | einfo (_("%s: Can't open output def file %s\n"),
|
|---|
| 971 | program_name, pe_out_def_filename);
|
|---|
| 972 | }
|
|---|
| 973 |
|
|---|
| 974 | if (pe_def_file)
|
|---|
| 975 | {
|
|---|
| 976 | if (pe_def_file->name)
|
|---|
| 977 | {
|
|---|
| 978 | if (pe_def_file->is_dll)
|
|---|
| 979 | fprintf (out, "LIBRARY ");
|
|---|
| 980 | else
|
|---|
| 981 | fprintf (out, "NAME ");
|
|---|
| 982 | quoteput (pe_def_file->name, out, 1);
|
|---|
| 983 | if (pe_data (output_bfd)->pe_opthdr.ImageBase)
|
|---|
| 984 | fprintf (out, " BASE=0x%lx",
|
|---|
| 985 | (unsigned long) pe_data (output_bfd)->pe_opthdr.ImageBase);
|
|---|
| 986 | fprintf (out, "\n");
|
|---|
| 987 | }
|
|---|
| 988 |
|
|---|
| 989 | if (pe_def_file->description)
|
|---|
| 990 | {
|
|---|
| 991 | fprintf (out, "DESCRIPTION ");
|
|---|
| 992 | quoteput (pe_def_file->description, out, 1);
|
|---|
| 993 | fprintf (out, "\n");
|
|---|
| 994 | }
|
|---|
| 995 |
|
|---|
| 996 | if (pe_def_file->version_minor != -1)
|
|---|
| 997 | fprintf (out, "VERSION %d.%d\n", pe_def_file->version_major,
|
|---|
| 998 | pe_def_file->version_minor);
|
|---|
| 999 | else if (pe_def_file->version_major != -1)
|
|---|
| 1000 | fprintf (out, "VERSION %d\n", pe_def_file->version_major);
|
|---|
| 1001 |
|
|---|
| 1002 | if (pe_def_file->stack_reserve != -1 || pe_def_file->heap_reserve != -1)
|
|---|
| 1003 | fprintf (out, "\n");
|
|---|
| 1004 |
|
|---|
| 1005 | if (pe_def_file->stack_commit != -1)
|
|---|
| 1006 | fprintf (out, "STACKSIZE 0x%x,0x%x\n",
|
|---|
| 1007 | pe_def_file->stack_reserve, pe_def_file->stack_commit);
|
|---|
| 1008 | else if (pe_def_file->stack_reserve != -1)
|
|---|
| 1009 | fprintf (out, "STACKSIZE 0x%x\n", pe_def_file->stack_reserve);
|
|---|
| 1010 | if (pe_def_file->heap_commit != -1)
|
|---|
| 1011 | fprintf (out, "HEAPSIZE 0x%x,0x%x\n",
|
|---|
| 1012 | pe_def_file->heap_reserve, pe_def_file->heap_commit);
|
|---|
| 1013 | else if (pe_def_file->heap_reserve != -1)
|
|---|
| 1014 | fprintf (out, "HEAPSIZE 0x%x\n", pe_def_file->heap_reserve);
|
|---|
| 1015 |
|
|---|
| 1016 | if (pe_def_file->num_section_defs > 0)
|
|---|
| 1017 | {
|
|---|
| 1018 | fprintf (out, "\nSECTIONS\n\n");
|
|---|
| 1019 | for (i = 0; i < pe_def_file->num_section_defs; i++)
|
|---|
| 1020 | {
|
|---|
| 1021 | fprintf (out, " ");
|
|---|
| 1022 | quoteput (pe_def_file->section_defs[i].name, out, 0);
|
|---|
| 1023 | if (pe_def_file->section_defs[i].class)
|
|---|
| 1024 | {
|
|---|
| 1025 | fprintf (out, " CLASS ");
|
|---|
| 1026 | quoteput (pe_def_file->section_defs[i].class, out, 0);
|
|---|
| 1027 | }
|
|---|
| 1028 | if (pe_def_file->section_defs[i].flag_read)
|
|---|
| 1029 | fprintf (out, " READ");
|
|---|
| 1030 | if (pe_def_file->section_defs[i].flag_write)
|
|---|
| 1031 | fprintf (out, " WRITE");
|
|---|
| 1032 | if (pe_def_file->section_defs[i].flag_execute)
|
|---|
| 1033 | fprintf (out, " EXECUTE");
|
|---|
| 1034 | if (pe_def_file->section_defs[i].flag_shared)
|
|---|
| 1035 | fprintf (out, " SHARED");
|
|---|
| 1036 | fprintf (out, "\n");
|
|---|
| 1037 | }
|
|---|
| 1038 | }
|
|---|
| 1039 |
|
|---|
| 1040 | if (pe_def_file->num_exports > 0)
|
|---|
| 1041 | {
|
|---|
| 1042 | fprintf (out, "\nEXPORTS\n\n");
|
|---|
| 1043 | for (i = 0; i < pe_def_file->num_exports; i++)
|
|---|
| 1044 | {
|
|---|
| 1045 | def_file_export *e = pe_def_file->exports + i;
|
|---|
| 1046 | fprintf (out, " ");
|
|---|
| 1047 | quoteput (e->name, out, 0);
|
|---|
| 1048 | if (e->internal_name && strcmp (e->internal_name, e->name))
|
|---|
| 1049 | {
|
|---|
| 1050 | fprintf (out, " = ");
|
|---|
| 1051 | quoteput (e->internal_name, out, 0);
|
|---|
| 1052 | }
|
|---|
| 1053 | if (e->ordinal != -1)
|
|---|
| 1054 | fprintf (out, " @%d", e->ordinal);
|
|---|
| 1055 | if (e->flag_private)
|
|---|
| 1056 | fprintf (out, " PRIVATE");
|
|---|
| 1057 | if (e->flag_constant)
|
|---|
| 1058 | fprintf (out, " CONSTANT");
|
|---|
| 1059 | if (e->flag_noname)
|
|---|
| 1060 | fprintf (out, " NONAME");
|
|---|
| 1061 | if (e->flag_data)
|
|---|
| 1062 | fprintf (out, " DATA");
|
|---|
| 1063 |
|
|---|
| 1064 | fprintf (out, "\n");
|
|---|
| 1065 | }
|
|---|
| 1066 | }
|
|---|
| 1067 |
|
|---|
| 1068 | if (pe_def_file->num_imports > 0)
|
|---|
| 1069 | {
|
|---|
| 1070 | fprintf (out, "\nIMPORTS\n\n");
|
|---|
| 1071 | for (i = 0; i < pe_def_file->num_imports; i++)
|
|---|
| 1072 | {
|
|---|
| 1073 | def_file_import *im = pe_def_file->imports + i;
|
|---|
| 1074 | fprintf (out, " ");
|
|---|
| 1075 | if (im->internal_name
|
|---|
| 1076 | && (!im->name || strcmp (im->internal_name, im->name)))
|
|---|
| 1077 | {
|
|---|
| 1078 | quoteput (im->internal_name, out, 0);
|
|---|
| 1079 | fprintf (out, " = ");
|
|---|
| 1080 | }
|
|---|
| 1081 | quoteput (im->module->name, out, 0);
|
|---|
| 1082 | fprintf (out, ".");
|
|---|
| 1083 | if (im->name)
|
|---|
| 1084 | quoteput (im->name, out, 0);
|
|---|
| 1085 | else
|
|---|
| 1086 | fprintf (out, "%d", im->ordinal);
|
|---|
| 1087 | fprintf (out, "\n");
|
|---|
| 1088 | }
|
|---|
| 1089 | }
|
|---|
| 1090 | }
|
|---|
| 1091 | else
|
|---|
| 1092 | fprintf (out, _("; no contents available\n"));
|
|---|
| 1093 |
|
|---|
| 1094 | if (fclose (out) == EOF)
|
|---|
| 1095 | {
|
|---|
| 1096 | /* xgettext:c-format */
|
|---|
| 1097 | einfo (_("%P: Error closing file `%s'\n"), pe_out_def_filename);
|
|---|
| 1098 | }
|
|---|
| 1099 | }
|
|---|
| 1100 |
|
|---|
| 1101 | /************************************************************************
|
|---|
| 1102 |
|
|---|
| 1103 | Generate the import library
|
|---|
| 1104 |
|
|---|
| 1105 | ************************************************************************/
|
|---|
| 1106 |
|
|---|
| 1107 | static asymbol **symtab;
|
|---|
| 1108 | static int symptr;
|
|---|
| 1109 | static int tmp_seq;
|
|---|
| 1110 | static const char *dll_filename;
|
|---|
| 1111 | static char *dll_symname;
|
|---|
| 1112 |
|
|---|
| 1113 | #define UNDSEC (asection *) &bfd_und_section
|
|---|
| 1114 |
|
|---|
| 1115 | static asection *
|
|---|
| 1116 | quick_section (abfd, name, flags, align)
|
|---|
| 1117 | bfd *abfd;
|
|---|
| 1118 | const char *name;
|
|---|
| 1119 | int flags;
|
|---|
| 1120 | int align;
|
|---|
| 1121 | {
|
|---|
| 1122 | asection *sec;
|
|---|
| 1123 | asymbol *sym;
|
|---|
| 1124 |
|
|---|
| 1125 | sec = bfd_make_section_old_way (abfd, name);
|
|---|
| 1126 | bfd_set_section_flags (abfd, sec, flags | SEC_ALLOC | SEC_LOAD | SEC_KEEP);
|
|---|
| 1127 | bfd_set_section_alignment (abfd, sec, align);
|
|---|
| 1128 | /* Remember to undo this before trying to link internally! */
|
|---|
| 1129 | sec->output_section = sec;
|
|---|
| 1130 |
|
|---|
| 1131 | sym = bfd_make_empty_symbol (abfd);
|
|---|
| 1132 | symtab[symptr++] = sym;
|
|---|
| 1133 | sym->name = sec->name;
|
|---|
| 1134 | sym->section = sec;
|
|---|
| 1135 | sym->flags = BSF_LOCAL;
|
|---|
| 1136 | sym->value = 0;
|
|---|
| 1137 |
|
|---|
| 1138 | return sec;
|
|---|
| 1139 | }
|
|---|
| 1140 |
|
|---|
| 1141 | static void
|
|---|
| 1142 | quick_symbol (abfd, n1, n2, n3, sec, flags, addr)
|
|---|
| 1143 | bfd *abfd;
|
|---|
| 1144 | char *n1;
|
|---|
| 1145 | char *n2;
|
|---|
| 1146 | char *n3;
|
|---|
| 1147 | asection *sec;
|
|---|
| 1148 | int flags;
|
|---|
| 1149 | int addr;
|
|---|
| 1150 | {
|
|---|
| 1151 | asymbol *sym;
|
|---|
| 1152 | char *name = (char *) xmalloc (strlen (n1) + strlen (n2) + strlen (n3) + 1);
|
|---|
| 1153 | strcpy (name, n1);
|
|---|
| 1154 | strcat (name, n2);
|
|---|
| 1155 | strcat (name, n3);
|
|---|
| 1156 | sym = bfd_make_empty_symbol (abfd);
|
|---|
| 1157 | sym->name = name;
|
|---|
| 1158 | sym->section = sec;
|
|---|
| 1159 | sym->flags = flags;
|
|---|
| 1160 | sym->value = addr;
|
|---|
| 1161 | symtab[symptr++] = sym;
|
|---|
| 1162 | }
|
|---|
| 1163 |
|
|---|
| 1164 | static arelent *reltab = 0;
|
|---|
| 1165 | static int relcount = 0, relsize = 0;
|
|---|
| 1166 |
|
|---|
| 1167 | static void
|
|---|
| 1168 | quick_reloc (abfd, address, which_howto, symidx)
|
|---|
| 1169 | bfd *abfd;
|
|---|
| 1170 | int address;
|
|---|
| 1171 | int which_howto;
|
|---|
| 1172 | int symidx;
|
|---|
| 1173 | {
|
|---|
| 1174 | if (relcount >= (relsize - 1))
|
|---|
| 1175 | {
|
|---|
| 1176 | relsize += 10;
|
|---|
| 1177 | if (reltab)
|
|---|
| 1178 | reltab = (arelent *) xrealloc (reltab, relsize * sizeof (arelent));
|
|---|
| 1179 | else
|
|---|
| 1180 | reltab = (arelent *) xmalloc (relsize * sizeof (arelent));
|
|---|
| 1181 | }
|
|---|
| 1182 | reltab[relcount].address = address;
|
|---|
| 1183 | reltab[relcount].addend = 0;
|
|---|
| 1184 | reltab[relcount].howto = bfd_reloc_type_lookup (abfd, which_howto);
|
|---|
| 1185 | reltab[relcount].sym_ptr_ptr = symtab + symidx;
|
|---|
| 1186 | relcount++;
|
|---|
| 1187 | }
|
|---|
| 1188 |
|
|---|
| 1189 | static void
|
|---|
| 1190 | save_relocs (asection *sec)
|
|---|
| 1191 | {
|
|---|
| 1192 | int i;
|
|---|
| 1193 | sec->relocation = reltab;
|
|---|
| 1194 | sec->reloc_count = relcount;
|
|---|
| 1195 | sec->orelocation = (arelent **) xmalloc ((relcount + 1) * sizeof (arelent *));
|
|---|
| 1196 | for (i = 0; i < relcount; i++)
|
|---|
| 1197 | sec->orelocation[i] = sec->relocation + i;
|
|---|
| 1198 | sec->orelocation[relcount] = 0;
|
|---|
| 1199 | sec->flags |= SEC_RELOC;
|
|---|
| 1200 | reltab = 0;
|
|---|
| 1201 | relcount = relsize = 0;
|
|---|
| 1202 | }
|
|---|
| 1203 |
|
|---|
| 1204 | /*
|
|---|
| 1205 | * .section .idata$2
|
|---|
| 1206 | * .global __head_my_dll
|
|---|
| 1207 | * __head_my_dll:
|
|---|
| 1208 | * .rva hname
|
|---|
| 1209 | * .long 0
|
|---|
| 1210 | * .long 0
|
|---|
| 1211 | * .rva __my_dll_iname
|
|---|
| 1212 | * .rva fthunk
|
|---|
| 1213 | *
|
|---|
| 1214 | * .section .idata$5
|
|---|
| 1215 | * .long 0
|
|---|
| 1216 | * fthunk:
|
|---|
| 1217 | *
|
|---|
| 1218 | * .section .idata$4
|
|---|
| 1219 | * .long 0
|
|---|
| 1220 | * hname:
|
|---|
| 1221 | */
|
|---|
| 1222 |
|
|---|
| 1223 | static bfd *
|
|---|
| 1224 | make_head (parent)
|
|---|
| 1225 | bfd *parent;
|
|---|
| 1226 | {
|
|---|
| 1227 | asection *id2, *id5, *id4;
|
|---|
| 1228 | unsigned char *d2, *d5, *d4;
|
|---|
| 1229 | char *oname;
|
|---|
| 1230 | bfd *abfd;
|
|---|
| 1231 |
|
|---|
| 1232 | oname = (char *) xmalloc (20);
|
|---|
| 1233 | sprintf (oname, "d%06d.o", tmp_seq);
|
|---|
| 1234 | tmp_seq++;
|
|---|
| 1235 |
|
|---|
| 1236 | abfd = bfd_create (oname, parent);
|
|---|
| 1237 | bfd_find_target (pe_details->object_target, abfd);
|
|---|
| 1238 | bfd_make_writable (abfd);
|
|---|
| 1239 |
|
|---|
| 1240 | bfd_set_format (abfd, bfd_object);
|
|---|
| 1241 | bfd_set_arch_mach (abfd, pe_details->bfd_arch, 0);
|
|---|
| 1242 |
|
|---|
| 1243 | symptr = 0;
|
|---|
| 1244 | symtab = (asymbol **) xmalloc (6 * sizeof (asymbol *));
|
|---|
| 1245 | id2 = quick_section (abfd, ".idata$2", SEC_HAS_CONTENTS, 2);
|
|---|
| 1246 | id5 = quick_section (abfd, ".idata$5", SEC_HAS_CONTENTS, 2);
|
|---|
| 1247 | id4 = quick_section (abfd, ".idata$4", SEC_HAS_CONTENTS, 2);
|
|---|
| 1248 | quick_symbol (abfd, U ("_head_"), dll_symname, "", id2, BSF_GLOBAL, 0);
|
|---|
| 1249 | quick_symbol (abfd, U (""), dll_symname, "_iname", UNDSEC, BSF_GLOBAL, 0);
|
|---|
| 1250 |
|
|---|
| 1251 | /* OK, pay attention here. I got confused myself looking back at
|
|---|
| 1252 | it. We create a four-byte section to mark the beginning of the
|
|---|
| 1253 | list, and we include an offset of 4 in the section, so that the
|
|---|
| 1254 | pointer to the list points to the *end* of this section, which is
|
|---|
| 1255 | the start of the list of sections from other objects. */
|
|---|
| 1256 |
|
|---|
| 1257 | bfd_set_section_size (abfd, id2, 20);
|
|---|
| 1258 | d2 = (unsigned char *) xmalloc (20);
|
|---|
| 1259 | id2->contents = d2;
|
|---|
| 1260 | memset (d2, 0, 20);
|
|---|
| 1261 | d2[0] = d2[16] = 4; /* reloc addend */
|
|---|
| 1262 | quick_reloc (abfd, 0, BFD_RELOC_RVA, 2);
|
|---|
| 1263 | quick_reloc (abfd, 12, BFD_RELOC_RVA, 4);
|
|---|
| 1264 | quick_reloc (abfd, 16, BFD_RELOC_RVA, 1);
|
|---|
| 1265 | save_relocs (id2);
|
|---|
| 1266 |
|
|---|
| 1267 | bfd_set_section_size (abfd, id5, 4);
|
|---|
| 1268 | d5 = (unsigned char *) xmalloc (4);
|
|---|
| 1269 | id5->contents = d5;
|
|---|
| 1270 | memset (d5, 0, 4);
|
|---|
| 1271 |
|
|---|
| 1272 | bfd_set_section_size (abfd, id4, 4);
|
|---|
| 1273 | d4 = (unsigned char *) xmalloc (4);
|
|---|
| 1274 | id4->contents = d4;
|
|---|
| 1275 | memset (d4, 0, 4);
|
|---|
| 1276 |
|
|---|
| 1277 | bfd_set_symtab (abfd, symtab, symptr);
|
|---|
| 1278 |
|
|---|
| 1279 | bfd_set_section_contents (abfd, id2, d2, 0, 20);
|
|---|
| 1280 | bfd_set_section_contents (abfd, id5, d5, 0, 4);
|
|---|
| 1281 | bfd_set_section_contents (abfd, id4, d4, 0, 4);
|
|---|
| 1282 |
|
|---|
| 1283 | bfd_make_readable (abfd);
|
|---|
| 1284 | return abfd;
|
|---|
| 1285 | }
|
|---|
| 1286 |
|
|---|
| 1287 | /*
|
|---|
| 1288 | * .section .idata$4
|
|---|
| 1289 | * .long 0
|
|---|
| 1290 | * .section .idata$5
|
|---|
| 1291 | * .long 0
|
|---|
| 1292 | * .section idata$7
|
|---|
| 1293 | * .global __my_dll_iname
|
|---|
| 1294 | *__my_dll_iname:
|
|---|
| 1295 | * .asciz "my.dll"
|
|---|
| 1296 | */
|
|---|
| 1297 |
|
|---|
| 1298 | static bfd *
|
|---|
| 1299 | make_tail (parent)
|
|---|
| 1300 | bfd *parent;
|
|---|
| 1301 | {
|
|---|
| 1302 | asection *id4, *id5, *id7;
|
|---|
| 1303 | unsigned char *d4, *d5, *d7;
|
|---|
| 1304 | int len;
|
|---|
| 1305 | char *oname;
|
|---|
| 1306 | bfd *abfd;
|
|---|
| 1307 |
|
|---|
| 1308 | oname = (char *) xmalloc (20);
|
|---|
| 1309 | sprintf (oname, "d%06d.o", tmp_seq);
|
|---|
| 1310 | tmp_seq++;
|
|---|
| 1311 |
|
|---|
| 1312 | abfd = bfd_create (oname, parent);
|
|---|
| 1313 | bfd_find_target (pe_details->object_target, abfd);
|
|---|
| 1314 | bfd_make_writable (abfd);
|
|---|
| 1315 |
|
|---|
| 1316 | bfd_set_format (abfd, bfd_object);
|
|---|
| 1317 | bfd_set_arch_mach (abfd, pe_details->bfd_arch, 0);
|
|---|
| 1318 |
|
|---|
| 1319 | symptr = 0;
|
|---|
| 1320 | symtab = (asymbol **) xmalloc (5 * sizeof (asymbol *));
|
|---|
| 1321 | id4 = quick_section (abfd, ".idata$4", SEC_HAS_CONTENTS, 2);
|
|---|
| 1322 | id5 = quick_section (abfd, ".idata$5", SEC_HAS_CONTENTS, 2);
|
|---|
| 1323 | id7 = quick_section (abfd, ".idata$7", SEC_HAS_CONTENTS, 2);
|
|---|
| 1324 | quick_symbol (abfd, U (""), dll_symname, "_iname", id7, BSF_GLOBAL, 0);
|
|---|
| 1325 |
|
|---|
| 1326 | bfd_set_section_size (abfd, id4, 4);
|
|---|
| 1327 | d4 = (unsigned char *) xmalloc (4);
|
|---|
| 1328 | id4->contents = d4;
|
|---|
| 1329 | memset (d4, 0, 4);
|
|---|
| 1330 |
|
|---|
| 1331 | bfd_set_section_size (abfd, id5, 4);
|
|---|
| 1332 | d5 = (unsigned char *) xmalloc (4);
|
|---|
| 1333 | id5->contents = d5;
|
|---|
| 1334 | memset (d5, 0, 4);
|
|---|
| 1335 |
|
|---|
| 1336 | len = strlen (dll_filename) + 1;
|
|---|
| 1337 | if (len & 1)
|
|---|
| 1338 | len++;
|
|---|
| 1339 | bfd_set_section_size (abfd, id7, len);
|
|---|
| 1340 | d7 = (unsigned char *) xmalloc (len);
|
|---|
| 1341 | id7->contents = d7;
|
|---|
| 1342 | strcpy (d7, dll_filename);
|
|---|
| 1343 |
|
|---|
| 1344 | bfd_set_symtab (abfd, symtab, symptr);
|
|---|
| 1345 |
|
|---|
| 1346 | bfd_set_section_contents (abfd, id4, d4, 0, 4);
|
|---|
| 1347 | bfd_set_section_contents (abfd, id5, d5, 0, 4);
|
|---|
| 1348 | bfd_set_section_contents (abfd, id7, d7, 0, len);
|
|---|
| 1349 |
|
|---|
| 1350 | bfd_make_readable (abfd);
|
|---|
| 1351 | return abfd;
|
|---|
| 1352 | }
|
|---|
| 1353 |
|
|---|
| 1354 | /*
|
|---|
| 1355 | * .text
|
|---|
| 1356 | * .global _function
|
|---|
| 1357 | * .global ___imp_function
|
|---|
| 1358 | * .global __imp__function
|
|---|
| 1359 | *_function:
|
|---|
| 1360 | * jmp *__imp__function:
|
|---|
| 1361 | *
|
|---|
| 1362 | * .section idata$7
|
|---|
| 1363 | * .long __head_my_dll
|
|---|
| 1364 | *
|
|---|
| 1365 | * .section .idata$5
|
|---|
| 1366 | *___imp_function:
|
|---|
| 1367 | *__imp__function:
|
|---|
| 1368 | *iat?
|
|---|
| 1369 | * .section .idata$4
|
|---|
| 1370 | *iat?
|
|---|
| 1371 | * .section .idata$6
|
|---|
| 1372 | *ID<ordinal>:
|
|---|
| 1373 | * .short <hint>
|
|---|
| 1374 | * .asciz "function" xlate? (add underscore, kill at)
|
|---|
| 1375 | */
|
|---|
| 1376 |
|
|---|
| 1377 | static unsigned char jmp_ix86_bytes[] = {
|
|---|
| 1378 | 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, 0x90, 0x90
|
|---|
| 1379 | };
|
|---|
| 1380 |
|
|---|
| 1381 | /*
|
|---|
| 1382 | *_function:
|
|---|
| 1383 | * mov.l ip+8,r0
|
|---|
| 1384 | * mov.l @r0,r0
|
|---|
| 1385 | * jmp @r0
|
|---|
| 1386 | * nop
|
|---|
| 1387 | * .dw __imp_function
|
|---|
| 1388 | */
|
|---|
| 1389 |
|
|---|
| 1390 | static unsigned char jmp_sh_bytes[] = {
|
|---|
| 1391 | 0x01, 0xd0, 0x02, 0x60, 0x2b, 0x40, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00
|
|---|
| 1392 | };
|
|---|
| 1393 |
|
|---|
| 1394 | /*
|
|---|
| 1395 | *_function:
|
|---|
| 1396 | * lui $t0,<high:__imp_function>
|
|---|
| 1397 | * lw $t0,<low:__imp_function>
|
|---|
| 1398 | * jr $t0
|
|---|
| 1399 | * nop
|
|---|
| 1400 | */
|
|---|
| 1401 |
|
|---|
| 1402 | static unsigned char jmp_mips_bytes[] = {
|
|---|
| 1403 | 0x00, 0x00, 0x08, 0x3c, 0x00, 0x00, 0x08, 0x8d,
|
|---|
| 1404 | 0x08, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00
|
|---|
| 1405 | };
|
|---|
| 1406 |
|
|---|
| 1407 | static bfd *
|
|---|
| 1408 | make_one (exp, parent)
|
|---|
| 1409 | def_file_export *exp;
|
|---|
| 1410 | bfd *parent;
|
|---|
| 1411 | {
|
|---|
| 1412 | asection *tx, *id7, *id5, *id4, *id6;
|
|---|
| 1413 | unsigned char *td = NULL, *d7, *d5, *d4, *d6 = NULL;
|
|---|
| 1414 | int len;
|
|---|
| 1415 | char *oname;
|
|---|
| 1416 | bfd *abfd;
|
|---|
| 1417 | unsigned char *jmp_bytes = NULL;
|
|---|
| 1418 | int jmp_byte_count = 0;
|
|---|
| 1419 |
|
|---|
| 1420 | switch (pe_details->pe_arch)
|
|---|
| 1421 | {
|
|---|
| 1422 | case PE_ARCH_i386:
|
|---|
| 1423 | jmp_bytes = jmp_ix86_bytes;
|
|---|
| 1424 | jmp_byte_count = sizeof (jmp_ix86_bytes);
|
|---|
| 1425 | break;
|
|---|
| 1426 | case PE_ARCH_sh:
|
|---|
| 1427 | jmp_bytes = jmp_sh_bytes;
|
|---|
| 1428 | jmp_byte_count = sizeof (jmp_sh_bytes);
|
|---|
| 1429 | break;
|
|---|
| 1430 | case PE_ARCH_mips:
|
|---|
| 1431 | jmp_bytes = jmp_mips_bytes;
|
|---|
| 1432 | jmp_byte_count = sizeof (jmp_mips_bytes);
|
|---|
| 1433 | break;
|
|---|
| 1434 | }
|
|---|
| 1435 |
|
|---|
| 1436 | oname = (char *) xmalloc (20);
|
|---|
| 1437 | sprintf (oname, "d%06d.o", tmp_seq);
|
|---|
| 1438 | tmp_seq++;
|
|---|
| 1439 |
|
|---|
| 1440 | abfd = bfd_create (oname, parent);
|
|---|
| 1441 | bfd_find_target (pe_details->object_target, abfd);
|
|---|
| 1442 | bfd_make_writable (abfd);
|
|---|
| 1443 |
|
|---|
| 1444 | bfd_set_format (abfd, bfd_object);
|
|---|
| 1445 | bfd_set_arch_mach (abfd, pe_details->bfd_arch, 0);
|
|---|
| 1446 |
|
|---|
| 1447 | symptr = 0;
|
|---|
| 1448 | symtab = (asymbol **) xmalloc (10 * sizeof (asymbol *));
|
|---|
| 1449 | tx = quick_section (abfd, ".text", SEC_CODE|SEC_HAS_CONTENTS, 2);
|
|---|
| 1450 | id7 = quick_section (abfd, ".idata$7", SEC_HAS_CONTENTS, 2);
|
|---|
| 1451 | id5 = quick_section (abfd, ".idata$5", SEC_HAS_CONTENTS, 2);
|
|---|
| 1452 | id4 = quick_section (abfd, ".idata$4", SEC_HAS_CONTENTS, 2);
|
|---|
| 1453 | id6 = quick_section (abfd, ".idata$6", SEC_HAS_CONTENTS, 2);
|
|---|
| 1454 | if (! exp->flag_data)
|
|---|
| 1455 | quick_symbol (abfd, U (""), exp->internal_name, "", tx, BSF_GLOBAL, 0);
|
|---|
| 1456 | quick_symbol (abfd, U ("_head_"), dll_symname, "", UNDSEC, BSF_GLOBAL, 0);
|
|---|
| 1457 | quick_symbol (abfd, U ("_imp__"), exp->internal_name, "", id5, BSF_GLOBAL, 0);
|
|---|
| 1458 | if (pe_dll_compat_implib)
|
|---|
| 1459 | quick_symbol (abfd, U ("__imp_"), exp->internal_name, "",
|
|---|
| 1460 | id5, BSF_GLOBAL, 0);
|
|---|
| 1461 |
|
|---|
| 1462 | if (! exp->flag_data)
|
|---|
| 1463 | {
|
|---|
| 1464 | bfd_set_section_size (abfd, tx, jmp_byte_count);
|
|---|
| 1465 | td = (unsigned char *) xmalloc (jmp_byte_count);
|
|---|
| 1466 | tx->contents = td;
|
|---|
| 1467 | memcpy (td, jmp_bytes, jmp_byte_count);
|
|---|
| 1468 | switch (pe_details->pe_arch)
|
|---|
| 1469 | {
|
|---|
| 1470 | case PE_ARCH_i386:
|
|---|
| 1471 | quick_reloc (abfd, 2, BFD_RELOC_32, 2);
|
|---|
| 1472 | break;
|
|---|
| 1473 | case PE_ARCH_sh:
|
|---|
| 1474 | quick_reloc (abfd, 8, BFD_RELOC_32, 2);
|
|---|
| 1475 | break;
|
|---|
| 1476 | case PE_ARCH_mips:
|
|---|
| 1477 | quick_reloc (abfd, 0, BFD_RELOC_HI16_S, 2);
|
|---|
| 1478 | quick_reloc (abfd, 0, BFD_RELOC_LO16, 0); /* MIPS_R_PAIR */
|
|---|
| 1479 | quick_reloc (abfd, 4, BFD_RELOC_LO16, 2);
|
|---|
| 1480 | break;
|
|---|
| 1481 | }
|
|---|
| 1482 | save_relocs (tx);
|
|---|
| 1483 | }
|
|---|
| 1484 |
|
|---|
| 1485 | bfd_set_section_size (abfd, id7, 4);
|
|---|
| 1486 | d7 = (unsigned char *) xmalloc (4);
|
|---|
| 1487 | id7->contents = d7;
|
|---|
| 1488 | memset (d7, 0, 4);
|
|---|
| 1489 | quick_reloc (abfd, 0, BFD_RELOC_RVA, 6);
|
|---|
| 1490 | save_relocs (id7);
|
|---|
| 1491 |
|
|---|
| 1492 | bfd_set_section_size (abfd, id5, 4);
|
|---|
| 1493 | d5 = (unsigned char *) xmalloc (4);
|
|---|
| 1494 | id5->contents = d5;
|
|---|
| 1495 | memset (d5, 0, 4);
|
|---|
| 1496 | if (exp->flag_noname)
|
|---|
| 1497 | {
|
|---|
| 1498 | d5[0] = exp->ordinal;
|
|---|
| 1499 | d5[1] = exp->ordinal >> 8;
|
|---|
| 1500 | d5[3] = 0x80;
|
|---|
| 1501 | }
|
|---|
| 1502 | else
|
|---|
| 1503 | {
|
|---|
| 1504 | quick_reloc (abfd, 0, BFD_RELOC_RVA, 4);
|
|---|
| 1505 | save_relocs (id5);
|
|---|
| 1506 | }
|
|---|
| 1507 |
|
|---|
| 1508 | bfd_set_section_size (abfd, id4, 4);
|
|---|
| 1509 | d4 = (unsigned char *) xmalloc (4);
|
|---|
| 1510 | id4->contents = d4;
|
|---|
| 1511 | memset (d4, 0, 4);
|
|---|
| 1512 | if (exp->flag_noname)
|
|---|
| 1513 | {
|
|---|
| 1514 | d4[0] = exp->ordinal;
|
|---|
| 1515 | d4[1] = exp->ordinal >> 8;
|
|---|
| 1516 | d4[3] = 0x80;
|
|---|
| 1517 | }
|
|---|
| 1518 | else
|
|---|
| 1519 | {
|
|---|
| 1520 | quick_reloc (abfd, 0, BFD_RELOC_RVA, 4);
|
|---|
| 1521 | save_relocs (id4);
|
|---|
| 1522 | }
|
|---|
| 1523 |
|
|---|
| 1524 | if (exp->flag_noname)
|
|---|
| 1525 | {
|
|---|
| 1526 | len = 0;
|
|---|
| 1527 | bfd_set_section_size (abfd, id6, 0);
|
|---|
| 1528 | }
|
|---|
| 1529 | else
|
|---|
| 1530 | {
|
|---|
| 1531 | len = strlen (exp->name) + 3;
|
|---|
| 1532 | if (len & 1)
|
|---|
| 1533 | len++;
|
|---|
| 1534 | bfd_set_section_size (abfd, id6, len);
|
|---|
| 1535 | d6 = (unsigned char *) xmalloc (len);
|
|---|
| 1536 | id6->contents = d6;
|
|---|
| 1537 | memset (d6, 0, len);
|
|---|
| 1538 | d6[0] = exp->hint & 0xff;
|
|---|
| 1539 | d6[1] = exp->hint >> 8;
|
|---|
| 1540 | strcpy (d6 + 2, exp->name);
|
|---|
| 1541 | }
|
|---|
| 1542 |
|
|---|
| 1543 | bfd_set_symtab (abfd, symtab, symptr);
|
|---|
| 1544 |
|
|---|
| 1545 | bfd_set_section_contents (abfd, tx, td, 0, jmp_byte_count);
|
|---|
| 1546 | bfd_set_section_contents (abfd, id7, d7, 0, 4);
|
|---|
| 1547 | bfd_set_section_contents (abfd, id5, d5, 0, 4);
|
|---|
| 1548 | bfd_set_section_contents (abfd, id4, d4, 0, 4);
|
|---|
| 1549 | if (!exp->flag_noname)
|
|---|
| 1550 | bfd_set_section_contents (abfd, id6, d6, 0, len);
|
|---|
| 1551 |
|
|---|
| 1552 | bfd_make_readable (abfd);
|
|---|
| 1553 | return abfd;
|
|---|
| 1554 | }
|
|---|
| 1555 |
|
|---|
| 1556 | void
|
|---|
| 1557 | pe_dll_generate_implib (def, impfilename)
|
|---|
| 1558 | def_file *def;
|
|---|
| 1559 | const char *impfilename;
|
|---|
| 1560 | {
|
|---|
| 1561 | int i;
|
|---|
| 1562 | bfd *ar_head;
|
|---|
| 1563 | bfd *ar_tail;
|
|---|
| 1564 | bfd *outarch;
|
|---|
| 1565 | bfd *head = 0;
|
|---|
| 1566 |
|
|---|
| 1567 | dll_filename = (def->name) ? def->name : dll_name;
|
|---|
| 1568 | dll_symname = xstrdup (dll_filename);
|
|---|
| 1569 | for (i = 0; dll_symname[i]; i++)
|
|---|
| 1570 | if (!isalnum ((unsigned char) dll_symname[i]))
|
|---|
| 1571 | dll_symname[i] = '_';
|
|---|
| 1572 |
|
|---|
| 1573 | unlink (impfilename);
|
|---|
| 1574 |
|
|---|
| 1575 | outarch = bfd_openw (impfilename, 0);
|
|---|
| 1576 |
|
|---|
| 1577 | if (!outarch)
|
|---|
| 1578 | {
|
|---|
| 1579 | /* xgettext:c-format */
|
|---|
| 1580 | einfo (_("%XCan't open .lib file: %s\n"), impfilename);
|
|---|
| 1581 | return;
|
|---|
| 1582 | }
|
|---|
| 1583 |
|
|---|
| 1584 | /* xgettext:c-format */
|
|---|
| 1585 | einfo (_("Creating library file: %s\n"), impfilename);
|
|---|
| 1586 |
|
|---|
| 1587 | bfd_set_format (outarch, bfd_archive);
|
|---|
| 1588 | outarch->has_armap = 1;
|
|---|
| 1589 |
|
|---|
| 1590 | /* Work out a reasonable size of things to put onto one line. */
|
|---|
| 1591 |
|
|---|
| 1592 | ar_head = make_head (outarch);
|
|---|
| 1593 |
|
|---|
| 1594 | for (i = 0; i < def->num_exports; i++)
|
|---|
| 1595 | {
|
|---|
| 1596 | /* The import library doesn't know about the internal name. */
|
|---|
| 1597 | char *internal = def->exports[i].internal_name;
|
|---|
| 1598 | bfd *n;
|
|---|
| 1599 | def->exports[i].internal_name = def->exports[i].name;
|
|---|
| 1600 | n = make_one (def->exports + i, outarch);
|
|---|
| 1601 | n->next = head;
|
|---|
| 1602 | head = n;
|
|---|
| 1603 | def->exports[i].internal_name = internal;
|
|---|
| 1604 | }
|
|---|
| 1605 |
|
|---|
| 1606 | ar_tail = make_tail (outarch);
|
|---|
| 1607 |
|
|---|
| 1608 | if (ar_head == NULL || ar_tail == NULL)
|
|---|
| 1609 | return;
|
|---|
| 1610 |
|
|---|
| 1611 | /* Now stick them all into the archive. */
|
|---|
| 1612 |
|
|---|
| 1613 | ar_head->next = head;
|
|---|
| 1614 | ar_tail->next = ar_head;
|
|---|
| 1615 | head = ar_tail;
|
|---|
| 1616 |
|
|---|
| 1617 | if (! bfd_set_archive_head (outarch, head))
|
|---|
| 1618 | einfo ("%Xbfd_set_archive_head: %s\n", bfd_errmsg (bfd_get_error ()));
|
|---|
| 1619 |
|
|---|
| 1620 | if (! bfd_close (outarch))
|
|---|
| 1621 | einfo ("%Xbfd_close %s: %s\n", impfilename, bfd_errmsg (bfd_get_error ()));
|
|---|
| 1622 |
|
|---|
| 1623 | while (head != NULL)
|
|---|
| 1624 | {
|
|---|
| 1625 | bfd *n = head->next;
|
|---|
| 1626 | bfd_close (head);
|
|---|
| 1627 | head = n;
|
|---|
| 1628 | }
|
|---|
| 1629 | }
|
|---|
| 1630 |
|
|---|
| 1631 | static void
|
|---|
| 1632 | add_bfd_to_link (abfd, name, link_info)
|
|---|
| 1633 | bfd *abfd;
|
|---|
| 1634 | char *name;
|
|---|
| 1635 | struct bfd_link_info *link_info;
|
|---|
| 1636 | {
|
|---|
| 1637 | lang_input_statement_type *fake_file;
|
|---|
| 1638 | fake_file = lang_add_input_file (name,
|
|---|
| 1639 | lang_input_file_is_fake_enum,
|
|---|
| 1640 | NULL);
|
|---|
| 1641 | fake_file->the_bfd = abfd;
|
|---|
| 1642 | ldlang_add_file (fake_file);
|
|---|
| 1643 | if (!bfd_link_add_symbols (abfd, link_info))
|
|---|
| 1644 | einfo ("%Xaddsym %s: %s\n", name, bfd_errmsg (bfd_get_error ()));
|
|---|
| 1645 | }
|
|---|
| 1646 |
|
|---|
| 1647 | void
|
|---|
| 1648 | pe_process_import_defs (output_bfd, link_info)
|
|---|
| 1649 | bfd *output_bfd;
|
|---|
| 1650 | struct bfd_link_info *link_info;
|
|---|
| 1651 | {
|
|---|
| 1652 | def_file_module *module;
|
|---|
| 1653 | pe_dll_id_target (bfd_get_target (output_bfd));
|
|---|
| 1654 |
|
|---|
| 1655 | if (!pe_def_file)
|
|---|
| 1656 | return;
|
|---|
| 1657 |
|
|---|
| 1658 | for (module = pe_def_file->modules; module; module = module->next)
|
|---|
| 1659 | {
|
|---|
| 1660 | int i, do_this_dll;
|
|---|
| 1661 |
|
|---|
| 1662 | dll_filename = module->name;
|
|---|
| 1663 | dll_symname = xstrdup (module->name);
|
|---|
| 1664 | for (i = 0; dll_symname[i]; i++)
|
|---|
| 1665 | if (!isalnum (dll_symname[i]))
|
|---|
| 1666 | dll_symname[i] = '_';
|
|---|
| 1667 |
|
|---|
| 1668 | do_this_dll = 0;
|
|---|
| 1669 |
|
|---|
| 1670 | for (i = 0; i < pe_def_file->num_imports; i++)
|
|---|
| 1671 | if (pe_def_file->imports[i].module == module)
|
|---|
| 1672 | {
|
|---|
| 1673 | def_file_export exp;
|
|---|
| 1674 | struct bfd_link_hash_entry *blhe;
|
|---|
| 1675 |
|
|---|
| 1676 | /* See if we need this import. */
|
|---|
| 1677 | char *name = (char *) xmalloc (strlen (pe_def_file->imports[i].internal_name) + 2 + 6);
|
|---|
| 1678 | sprintf (name, "%s%s", U (""), pe_def_file->imports[i].internal_name);
|
|---|
| 1679 | blhe = bfd_link_hash_lookup (link_info->hash, name,
|
|---|
| 1680 | false, false, false);
|
|---|
| 1681 | if (!blhe || (blhe && blhe->type != bfd_link_hash_undefined))
|
|---|
| 1682 | {
|
|---|
| 1683 | sprintf (name, "%s%s", U ("_imp__"),
|
|---|
| 1684 | pe_def_file->imports[i].internal_name);
|
|---|
| 1685 | blhe = bfd_link_hash_lookup (link_info->hash, name,
|
|---|
| 1686 | false, false, false);
|
|---|
| 1687 | }
|
|---|
| 1688 | free (name);
|
|---|
| 1689 | if (blhe && blhe->type == bfd_link_hash_undefined)
|
|---|
| 1690 | {
|
|---|
| 1691 | bfd *one;
|
|---|
| 1692 | /* We do. */
|
|---|
| 1693 | if (!do_this_dll)
|
|---|
| 1694 | {
|
|---|
| 1695 | bfd *ar_head = make_head (output_bfd);
|
|---|
| 1696 | add_bfd_to_link (ar_head, ar_head->filename, link_info);
|
|---|
| 1697 | do_this_dll = 1;
|
|---|
| 1698 | }
|
|---|
| 1699 | exp.internal_name = pe_def_file->imports[i].internal_name;
|
|---|
| 1700 | exp.name = pe_def_file->imports[i].name;
|
|---|
| 1701 | exp.ordinal = pe_def_file->imports[i].ordinal;
|
|---|
| 1702 | exp.hint = exp.ordinal >= 0 ? exp.ordinal : 0;
|
|---|
| 1703 | exp.flag_private = 0;
|
|---|
| 1704 | exp.flag_constant = 0;
|
|---|
| 1705 | exp.flag_data = 0;
|
|---|
| 1706 | exp.flag_noname = exp.name ? 0 : 1;
|
|---|
| 1707 | one = make_one (&exp, output_bfd);
|
|---|
| 1708 | add_bfd_to_link (one, one->filename, link_info);
|
|---|
| 1709 | }
|
|---|
| 1710 | }
|
|---|
| 1711 | if (do_this_dll)
|
|---|
| 1712 | {
|
|---|
| 1713 | bfd *ar_tail = make_tail (output_bfd);
|
|---|
| 1714 | add_bfd_to_link (ar_tail, ar_tail->filename, link_info);
|
|---|
| 1715 | }
|
|---|
| 1716 |
|
|---|
| 1717 | free (dll_symname);
|
|---|
| 1718 | }
|
|---|
| 1719 | }
|
|---|
| 1720 |
|
|---|
| 1721 | /************************************************************************
|
|---|
| 1722 |
|
|---|
| 1723 | We were handed a *.DLL file. Parse it and turn it into a set of
|
|---|
| 1724 | IMPORTS directives in the def file. Return true if the file was
|
|---|
| 1725 | handled, false if not.
|
|---|
| 1726 |
|
|---|
| 1727 | ************************************************************************/
|
|---|
| 1728 |
|
|---|
| 1729 | static unsigned int
|
|---|
| 1730 | pe_get16 (abfd, where)
|
|---|
| 1731 | bfd *abfd;
|
|---|
| 1732 | int where;
|
|---|
| 1733 | {
|
|---|
| 1734 | unsigned char b[2];
|
|---|
| 1735 | bfd_seek (abfd, where, SEEK_SET);
|
|---|
| 1736 | bfd_read (b, 1, 2, abfd);
|
|---|
| 1737 | return b[0] + (b[1] << 8);
|
|---|
| 1738 | }
|
|---|
| 1739 |
|
|---|
| 1740 | static unsigned int
|
|---|
| 1741 | pe_get32 (abfd, where)
|
|---|
| 1742 | bfd *abfd;
|
|---|
| 1743 | int where;
|
|---|
| 1744 | {
|
|---|
| 1745 | unsigned char b[4];
|
|---|
| 1746 | bfd_seek (abfd, where, SEEK_SET);
|
|---|
| 1747 | bfd_read (b, 1, 4, abfd);
|
|---|
| 1748 | return b[0] + (b[1] << 8) + (b[2] << 16) + (b[3] << 24);
|
|---|
| 1749 | }
|
|---|
| 1750 |
|
|---|
| 1751 | #if 0 /* This is not currently used. */
|
|---|
| 1752 |
|
|---|
| 1753 | static unsigned int
|
|---|
| 1754 | pe_as16 (ptr)
|
|---|
| 1755 | void *ptr;
|
|---|
| 1756 | {
|
|---|
| 1757 | unsigned char *b = ptr;
|
|---|
| 1758 | return b[0] + (b[1] << 8);
|
|---|
| 1759 | }
|
|---|
| 1760 |
|
|---|
| 1761 | #endif
|
|---|
| 1762 |
|
|---|
| 1763 | static unsigned int
|
|---|
| 1764 | pe_as32 (ptr)
|
|---|
| 1765 | void *ptr;
|
|---|
| 1766 | {
|
|---|
| 1767 | unsigned char *b = ptr;
|
|---|
| 1768 | return b[0] + (b[1] << 8) + (b[2] << 16) + (b[3] << 24);
|
|---|
| 1769 | }
|
|---|
| 1770 |
|
|---|
| 1771 | boolean
|
|---|
| 1772 | pe_implied_import_dll (filename)
|
|---|
| 1773 | const char *filename;
|
|---|
| 1774 | {
|
|---|
| 1775 | bfd *dll;
|
|---|
| 1776 | unsigned long pe_header_offset, opthdr_ofs, num_entries, i;
|
|---|
| 1777 | unsigned long export_rva, export_size, nsections, secptr, expptr;
|
|---|
| 1778 | unsigned char *expdata, *erva;
|
|---|
| 1779 | unsigned long name_rvas, ordinals, nexp, ordbase;
|
|---|
| 1780 | const char *dll_name;
|
|---|
| 1781 |
|
|---|
| 1782 | /* No, I can't use bfd here. kernel32.dll puts its export table in
|
|---|
| 1783 | the middle of the .rdata section. */
|
|---|
| 1784 |
|
|---|
| 1785 | dll = bfd_openr (filename, pe_details->target_name);
|
|---|
| 1786 | if (!dll)
|
|---|
| 1787 | {
|
|---|
| 1788 | einfo ("%Xopen %s: %s\n", filename, bfd_errmsg (bfd_get_error ()));
|
|---|
| 1789 | return false;
|
|---|
| 1790 | }
|
|---|
| 1791 | /* PEI dlls seem to be bfd_objects. */
|
|---|
| 1792 | if (!bfd_check_format (dll, bfd_object))
|
|---|
| 1793 | {
|
|---|
| 1794 | einfo ("%X%s: this doesn't appear to be a DLL\n", filename);
|
|---|
| 1795 | return false;
|
|---|
| 1796 | }
|
|---|
| 1797 |
|
|---|
| 1798 | dll_name = filename;
|
|---|
| 1799 | for (i = 0; filename[i]; i++)
|
|---|
| 1800 | if (filename[i] == '/' || filename[i] == '\\' || filename[i] == ':')
|
|---|
| 1801 | dll_name = filename + i + 1;
|
|---|
| 1802 |
|
|---|
| 1803 | pe_header_offset = pe_get32 (dll, 0x3c);
|
|---|
| 1804 | opthdr_ofs = pe_header_offset + 4 + 20;
|
|---|
| 1805 | num_entries = pe_get32 (dll, opthdr_ofs + 92);
|
|---|
| 1806 | if (num_entries < 1) /* no exports */
|
|---|
| 1807 | return false;
|
|---|
| 1808 | export_rva = pe_get32 (dll, opthdr_ofs + 96);
|
|---|
| 1809 | export_size = pe_get32 (dll, opthdr_ofs + 100);
|
|---|
| 1810 | nsections = pe_get16 (dll, pe_header_offset + 4 + 2);
|
|---|
| 1811 | secptr = (pe_header_offset + 4 + 20 +
|
|---|
| 1812 | pe_get16 (dll, pe_header_offset + 4 + 16));
|
|---|
| 1813 | expptr = 0;
|
|---|
| 1814 | for (i = 0; i < nsections; i++)
|
|---|
| 1815 | {
|
|---|
| 1816 | char sname[8];
|
|---|
| 1817 | unsigned long secptr1 = secptr + 40 * i;
|
|---|
| 1818 | unsigned long vaddr = pe_get32 (dll, secptr1 + 12);
|
|---|
| 1819 | unsigned long vsize = pe_get32 (dll, secptr1 + 16);
|
|---|
| 1820 | unsigned long fptr = pe_get32 (dll, secptr1 + 20);
|
|---|
| 1821 | bfd_seek (dll, secptr1, SEEK_SET);
|
|---|
| 1822 | bfd_read (sname, 1, 8, dll);
|
|---|
| 1823 | if (vaddr <= export_rva && vaddr + vsize > export_rva)
|
|---|
| 1824 | {
|
|---|
| 1825 | expptr = fptr + (export_rva - vaddr);
|
|---|
| 1826 | if (export_rva + export_size > vaddr + vsize)
|
|---|
| 1827 | export_size = vsize - (export_rva - vaddr);
|
|---|
| 1828 | break;
|
|---|
| 1829 | }
|
|---|
| 1830 | }
|
|---|
| 1831 |
|
|---|
| 1832 | expdata = (unsigned char *) xmalloc (export_size);
|
|---|
| 1833 | bfd_seek (dll, expptr, SEEK_SET);
|
|---|
| 1834 | bfd_read (expdata, 1, export_size, dll);
|
|---|
| 1835 | erva = expdata - export_rva;
|
|---|
| 1836 |
|
|---|
| 1837 | if (pe_def_file == 0)
|
|---|
| 1838 | pe_def_file = def_file_empty ();
|
|---|
| 1839 |
|
|---|
| 1840 | nexp = pe_as32 (expdata + 24);
|
|---|
| 1841 | name_rvas = pe_as32 (expdata + 32);
|
|---|
| 1842 | ordinals = pe_as32 (expdata + 36);
|
|---|
| 1843 | ordbase = pe_as32 (expdata + 16);
|
|---|
| 1844 | for (i = 0; i < nexp; i++)
|
|---|
| 1845 | {
|
|---|
| 1846 | unsigned long name_rva = pe_as32 (erva + name_rvas + i * 4);
|
|---|
| 1847 | def_file_import *imp;
|
|---|
| 1848 | imp = def_file_add_import (pe_def_file, erva + name_rva, dll_name,
|
|---|
| 1849 | i, 0);
|
|---|
| 1850 | }
|
|---|
| 1851 |
|
|---|
| 1852 | return true;
|
|---|
| 1853 | }
|
|---|
| 1854 |
|
|---|
| 1855 | /************************************************************************
|
|---|
| 1856 |
|
|---|
| 1857 | These are the main functions, called from the emulation. The first
|
|---|
| 1858 | is called after the bfds are read, so we can guess at how much space
|
|---|
| 1859 | we need. The second is called after everything is placed, so we
|
|---|
| 1860 | can put the right values in place.
|
|---|
| 1861 |
|
|---|
| 1862 | ************************************************************************/
|
|---|
| 1863 |
|
|---|
| 1864 | void
|
|---|
| 1865 | pe_dll_build_sections (abfd, info)
|
|---|
| 1866 | bfd *abfd;
|
|---|
| 1867 | struct bfd_link_info *info;
|
|---|
| 1868 | {
|
|---|
| 1869 | pe_dll_id_target (bfd_get_target (abfd));
|
|---|
| 1870 | process_def_file (abfd, info);
|
|---|
| 1871 |
|
|---|
| 1872 | generate_edata (abfd, info);
|
|---|
| 1873 | build_filler_bfd (1);
|
|---|
| 1874 | }
|
|---|
| 1875 |
|
|---|
| 1876 | void
|
|---|
| 1877 | pe_exe_build_sections (abfd, info)
|
|---|
| 1878 | bfd *abfd;
|
|---|
| 1879 | struct bfd_link_info *info ATTRIBUTE_UNUSED;
|
|---|
| 1880 | {
|
|---|
| 1881 | pe_dll_id_target (bfd_get_target (abfd));
|
|---|
| 1882 | build_filler_bfd (0);
|
|---|
| 1883 | }
|
|---|
| 1884 |
|
|---|
| 1885 | void
|
|---|
| 1886 | pe_dll_fill_sections (abfd, info)
|
|---|
| 1887 | bfd *abfd;
|
|---|
| 1888 | struct bfd_link_info *info;
|
|---|
| 1889 | {
|
|---|
| 1890 | pe_dll_id_target (bfd_get_target (abfd));
|
|---|
| 1891 | image_base = pe_data (abfd)->pe_opthdr.ImageBase;
|
|---|
| 1892 |
|
|---|
| 1893 | generate_reloc (abfd, info);
|
|---|
| 1894 | if (reloc_sz > 0)
|
|---|
| 1895 | {
|
|---|
| 1896 | bfd_set_section_size (filler_bfd, reloc_s, reloc_sz);
|
|---|
| 1897 |
|
|---|
| 1898 | /* Resize the sections. */
|
|---|
| 1899 | lang_size_sections (stat_ptr->head, abs_output_section,
|
|---|
| 1900 | &stat_ptr->head, 0, (bfd_vma) 0, false);
|
|---|
| 1901 |
|
|---|
| 1902 | /* Redo special stuff. */
|
|---|
| 1903 | ldemul_after_allocation ();
|
|---|
| 1904 |
|
|---|
| 1905 | /* Do the assignments again. */
|
|---|
| 1906 | lang_do_assignments (stat_ptr->head,
|
|---|
| 1907 | abs_output_section,
|
|---|
| 1908 | (fill_type) 0, (bfd_vma) 0);
|
|---|
| 1909 | }
|
|---|
| 1910 |
|
|---|
| 1911 | fill_edata (abfd, info);
|
|---|
| 1912 |
|
|---|
| 1913 | pe_data (abfd)->dll = 1;
|
|---|
| 1914 |
|
|---|
| 1915 | edata_s->contents = edata_d;
|
|---|
| 1916 | reloc_s->contents = reloc_d;
|
|---|
| 1917 | }
|
|---|
| 1918 |
|
|---|
| 1919 | void
|
|---|
| 1920 | pe_exe_fill_sections (abfd, info)
|
|---|
| 1921 | bfd *abfd;
|
|---|
| 1922 | struct bfd_link_info *info;
|
|---|
| 1923 | {
|
|---|
| 1924 | pe_dll_id_target (bfd_get_target (abfd));
|
|---|
| 1925 | image_base = pe_data (abfd)->pe_opthdr.ImageBase;
|
|---|
| 1926 |
|
|---|
| 1927 | generate_reloc (abfd, info);
|
|---|
| 1928 | if (reloc_sz > 0)
|
|---|
| 1929 | {
|
|---|
| 1930 | bfd_set_section_size (filler_bfd, reloc_s, reloc_sz);
|
|---|
| 1931 |
|
|---|
| 1932 | /* Resize the sections. */
|
|---|
| 1933 | lang_size_sections (stat_ptr->head, abs_output_section,
|
|---|
| 1934 | &stat_ptr->head, 0, (bfd_vma) 0, false);
|
|---|
| 1935 |
|
|---|
| 1936 | /* Redo special stuff. */
|
|---|
| 1937 | ldemul_after_allocation ();
|
|---|
| 1938 |
|
|---|
| 1939 | /* Do the assignments again. */
|
|---|
| 1940 | lang_do_assignments (stat_ptr->head,
|
|---|
| 1941 | abs_output_section,
|
|---|
| 1942 | (fill_type) 0, (bfd_vma) 0);
|
|---|
| 1943 | }
|
|---|
| 1944 | reloc_s->contents = reloc_d;
|
|---|
| 1945 | }
|
|---|