* minsyms.h (struct bound_minimal_symbol): New.
[deliverable/binutils-gdb.git] / gdb / coff-pe-read.c
CommitLineData
1b6bc7e0
CF
1/* Read the export table symbols from a portable executable and
2 convert to internal format, for GDB. Used as a last resort if no
3 debugging symbols recognized.
4
28e7fd62 5 Copyright (C) 2003-2013 Free Software Foundation, Inc.
1b6bc7e0
CF
6
7 This file is part of GDB.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
a9762ec7 11 the Free Software Foundation; either version 3 of the License, or
1b6bc7e0
CF
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
a9762ec7 20 along with this program. If not, see <http://www.gnu.org/licenses/>.
1b6bc7e0 21
aff410f1 22 Contributed by Raoul M. Gough (RaoulGough@yahoo.co.uk). */
1b6bc7e0 23
0baeab03
PA
24#include "defs.h"
25
1b6bc7e0
CF
26#include "coff-pe-read.h"
27
81de56be 28#include "bfd.h"
1b6bc7e0
CF
29#include "gdbtypes.h"
30
3999122f
PM
31#include "command.h"
32#include "gdbcmd.h"
1b6bc7e0
CF
33#include "symtab.h"
34#include "symfile.h"
35#include "objfiles.h"
3999122f 36#include "common/common-utils.h"
aab2f004 37#include "coff/internal.h"
3999122f
PM
38
39#include <ctype.h>
1b6bc7e0
CF
40
41/* Internal section information */
42
3999122f
PM
43/* Coff PE read debugging flag:
44 default value is 0,
45 value 1 outputs problems encountered while parsing PE file,
46 value above 1 also lists all generated minimal symbols. */
47static unsigned int debug_coff_pe_read;
48
1b6bc7e0
CF
49struct read_pe_section_data
50{
aff410f1
MS
51 CORE_ADDR vma_offset; /* Offset to loaded address of section. */
52 unsigned long rva_start; /* Start offset within the pe. */
53 unsigned long rva_end; /* End offset within the pe. */
54 enum minimal_symbol_type ms_type; /* Type to assign symbols in
55 section. */
3999122f 56 char *section_name; /* Recorded section name. */
1b6bc7e0
CF
57};
58
78ea0eca
PM
59#define IMAGE_SCN_CNT_CODE 0x20
60#define IMAGE_SCN_CNT_INITIALIZED_DATA 0x40
61#define IMAGE_SCN_CNT_UNINITIALIZED_DATA 0x80
1b6bc7e0
CF
62#define PE_SECTION_INDEX_TEXT 0
63#define PE_SECTION_INDEX_DATA 1
64#define PE_SECTION_INDEX_BSS 2
65#define PE_SECTION_TABLE_SIZE 3
66#define PE_SECTION_INDEX_INVALID -1
67\f
68/* Get the index of the named section in our own array, which contains
aff410f1
MS
69 text, data and bss in that order. Return PE_SECTION_INDEX_INVALID
70 if passed an unrecognised section name. */
1b6bc7e0
CF
71
72static int
73read_pe_section_index (const char *section_name)
74{
75 if (strcmp (section_name, ".text") == 0)
76 {
77 return PE_SECTION_INDEX_TEXT;
78 }
79
80 else if (strcmp (section_name, ".data") == 0)
81 {
82 return PE_SECTION_INDEX_DATA;
83 }
84
85 else if (strcmp (section_name, ".bss") == 0)
86 {
87 return PE_SECTION_INDEX_BSS;
88 }
89
90 else
91 {
92 return PE_SECTION_INDEX_INVALID;
93 }
94}
95
3999122f
PM
96/* Get the index of the named section in our own full arrayi.
97 text, data and bss in that order. Return PE_SECTION_INDEX_INVALID
98 if passed an unrecognised section name. */
99
100static int
101get_pe_section_index (const char *section_name,
102 struct read_pe_section_data *sections,
103 int nb_sections)
104{
105 int i;
106
107 for (i = 0; i < nb_sections; i++)
108 if (strcmp (sections[i].section_name, section_name) == 0)
109 return i;
110 return PE_SECTION_INDEX_INVALID;
111}
112
113/* Structure used by get_section_vmas function below
114 to access section_data array and the size of the array
115 stored in nb_sections field. */
116struct pe_sections_info
117{
118 int nb_sections;
119 struct read_pe_section_data *sections;
120};
121
aff410f1 122/* Record the virtual memory address of a section. */
1b6bc7e0
CF
123
124static void
125get_section_vmas (bfd *abfd, asection *sectp, void *context)
126{
3999122f
PM
127 struct pe_sections_info *data = context;
128 struct read_pe_section_data *sections = data->sections;
129 int sectix = get_pe_section_index (sectp->name, sections,
130 data->nb_sections);
1b6bc7e0
CF
131
132 if (sectix != PE_SECTION_INDEX_INVALID)
133 {
134 /* Data within the section start at rva_start in the pe and at
aff410f1 135 bfd_get_section_vma() within memory. Store the offset. */
1b6bc7e0
CF
136
137 sections[sectix].vma_offset
138 = bfd_get_section_vma (abfd, sectp) - sections[sectix].rva_start;
139 }
140}
141\f
3999122f
PM
142/* Create a minimal symbol entry for an exported symbol.
143 SYM_NAME contains the exported name or NULL if exported by ordinal,
144 FUNC_RVA contains the Relative Virtual Address of the symbol,
145 ORDINAL is the ordinal index value of the symbol,
146 SECTION_DATA contains information about the section in which the
147 symbol is declared,
148 DLL_NAME is the internal name of the DLL file,
149 OBJFILE is the objfile struct of DLL_NAME. */
1b6bc7e0
CF
150
151static void
3999122f 152add_pe_exported_sym (const char *sym_name,
1b6bc7e0 153 unsigned long func_rva,
3999122f 154 int ordinal,
1b6bc7e0
CF
155 const struct read_pe_section_data *section_data,
156 const char *dll_name, struct objfile *objfile)
157{
3999122f 158 char *qualified_name, *bare_name;
aff410f1 159 /* Add the stored offset to get the loaded address of the symbol. */
1b6bc7e0 160 CORE_ADDR vma = func_rva + section_data->vma_offset;
1b6bc7e0
CF
161
162 /* Generate a (hopefully unique) qualified name using the first part
aff410f1
MS
163 of the dll name, e.g. KERNEL32!AddAtomA. This matches the style
164 used by windbg from the "Microsoft Debugging Tools for Windows". */
1b6bc7e0 165
3999122f
PM
166 if (sym_name == NULL || *sym_name == '\0')
167 bare_name = xstrprintf ("#%d", ordinal);
168 else
169 bare_name = xstrdup (sym_name);
170
171 qualified_name = xstrprintf ("%s!%s", dll_name, bare_name);
1b6bc7e0 172
3999122f
PM
173 if ((section_data->ms_type == mst_unknown) && debug_coff_pe_read)
174 fprintf_unfiltered (gdb_stdlog , _("Unknown section type for \"%s\""
175 " for entry \"%s\" in dll \"%s\"\n"),
176 section_data->section_name, sym_name, dll_name);
1b6bc7e0 177
3999122f
PM
178 prim_record_minimal_symbol (qualified_name, vma,
179 section_data->ms_type, objfile);
1b6bc7e0 180
3999122f
PM
181 /* Enter the plain name as well, which might not be unique. */
182 prim_record_minimal_symbol (bare_name, vma, section_data->ms_type, objfile);
183 if (debug_coff_pe_read > 1)
184 fprintf_unfiltered (gdb_stdlog, _("Adding exported symbol \"%s\""
185 " in dll \"%s\"\n"), sym_name, dll_name);
1b6bc7e0 186 xfree (qualified_name);
3999122f
PM
187 xfree (bare_name);
188}
189
190/* Create a minimal symbol entry for an exported forward symbol.
191 Return 1 if the forwarded function was found 0 otherwise.
192 SYM_NAME contains the exported name or NULL if exported by ordinal,
193 FORWARD_DLL_NAME is the name of the DLL in which the target symobl resides,
194 FORWARD_FUNC_NAME is the name of the target symbol in that DLL,
195 ORDINAL is the ordinal index value of the symbol,
196 DLL_NAME is the internal name of the DLL file,
197 OBJFILE is the objfile struct of DLL_NAME. */
198
199static int
200add_pe_forwarded_sym (const char *sym_name, const char *forward_dll_name,
201 const char *forward_func_name, int ordinal,
202 const char *dll_name, struct objfile *objfile)
203{
204 CORE_ADDR vma;
7cbd4a93 205 struct bound_minimal_symbol msymbol;
3999122f 206 enum minimal_symbol_type msymtype;
3999122f
PM
207 char *qualified_name, *bare_name;
208 int forward_dll_name_len = strlen (forward_dll_name);
209 int forward_func_name_len = strlen (forward_func_name);
210 int forward_len = forward_dll_name_len + forward_func_name_len + 2;
211 char *forward_qualified_name = alloca (forward_len);
212
213 xsnprintf (forward_qualified_name, forward_len, "%s!%s", forward_dll_name,
214 forward_func_name);
215
216
7cbd4a93 217 msymbol = lookup_minimal_symbol_and_objfile (forward_qualified_name);
3999122f 218
7cbd4a93 219 if (!msymbol.minsym)
3999122f
PM
220 {
221 int i;
222
223 for (i = 0; i < forward_dll_name_len; i++)
224 forward_qualified_name[i] = tolower (forward_qualified_name[i]);
7cbd4a93 225 msymbol = lookup_minimal_symbol_and_objfile (forward_qualified_name);
3999122f
PM
226 }
227
7cbd4a93 228 if (!msymbol.minsym)
3999122f
PM
229 {
230 if (debug_coff_pe_read)
231 fprintf_unfiltered (gdb_stdlog, _("Unable to find function \"%s\" in"
232 " dll \"%s\", forward of \"%s\" in dll \"%s\"\n"),
233 forward_func_name, forward_dll_name, sym_name,
234 dll_name);
235 return 0;
236 }
237
238 if (debug_coff_pe_read > 1)
239 fprintf_unfiltered (gdb_stdlog, _("Adding forwarded exported symbol"
240 " \"%s\" in dll \"%s\", pointing to \"%s\"\n"),
241 sym_name, dll_name, forward_qualified_name);
242
7cbd4a93
TT
243 vma = SYMBOL_VALUE_ADDRESS (msymbol.minsym);
244 msymtype = MSYMBOL_TYPE (msymbol.minsym);
3999122f
PM
245
246 /* Generate a (hopefully unique) qualified name using the first part
247 of the dll name, e.g. KERNEL32!AddAtomA. This matches the style
248 used by windbg from the "Microsoft Debugging Tools for Windows". */
249
250 if (sym_name == NULL || *sym_name == '\0')
251 bare_name = xstrprintf ("#%d", ordinal);
252 else
253 bare_name = xstrdup (sym_name);
254
255 qualified_name = xstrprintf ("%s!%s", dll_name, bare_name);
256
257 prim_record_minimal_symbol (qualified_name, vma, msymtype, objfile);
1b6bc7e0 258
aff410f1 259 /* Enter the plain name as well, which might not be unique. */
3999122f
PM
260 prim_record_minimal_symbol (bare_name, vma, msymtype, objfile);
261 xfree (qualified_name);
262 xfree (bare_name);
263
264 return 1;
1b6bc7e0
CF
265}
266
3999122f 267/* Truncate a dll_name at the last dot character. */
1b6bc7e0
CF
268
269static void
270read_pe_truncate_name (char *dll_name)
271{
3999122f 272 char *last_point = strrchr (dll_name, '.');
1b6bc7e0 273
3999122f
PM
274 if (last_point != NULL)
275 *last_point = '\0';
1b6bc7e0
CF
276}
277\f
aff410f1 278/* Low-level support functions, direct from the ld module pe-dll.c. */
1b6bc7e0
CF
279static unsigned int
280pe_get16 (bfd *abfd, int where)
281{
282 unsigned char b[2];
283
284 bfd_seek (abfd, (file_ptr) where, SEEK_SET);
285 bfd_bread (b, (bfd_size_type) 2, abfd);
286 return b[0] + (b[1] << 8);
287}
288
289static unsigned int
290pe_get32 (bfd *abfd, int where)
291{
292 unsigned char b[4];
293
294 bfd_seek (abfd, (file_ptr) where, SEEK_SET);
295 bfd_bread (b, (bfd_size_type) 4, abfd);
296 return b[0] + (b[1] << 8) + (b[2] << 16) + (b[3] << 24);
297}
298
3999122f
PM
299static unsigned int
300pe_as16 (void *ptr)
301{
302 unsigned char *b = ptr;
303
304 return b[0] + (b[1] << 8);
305}
306
1b6bc7e0
CF
307static unsigned int
308pe_as32 (void *ptr)
309{
310 unsigned char *b = ptr;
311
312 return b[0] + (b[1] << 8) + (b[2] << 16) + (b[3] << 24);
313}
314\f
315/* Read the (non-debug) export symbol table from a portable
aff410f1
MS
316 executable. Code originally lifted from the ld function
317 pe_implied_import_dll in pe-dll.c. */
1b6bc7e0
CF
318
319void
320read_pe_exported_syms (struct objfile *objfile)
321{
322 bfd *dll = objfile->obfd;
3999122f 323 unsigned long nbnormal, nbforward;
1b6bc7e0 324 unsigned long pe_header_offset, opthdr_ofs, num_entries, i;
3999122f 325 unsigned long export_opthdrrva, export_opthdrsize;
1b6bc7e0
CF
326 unsigned long export_rva, export_size, nsections, secptr, expptr;
327 unsigned long exp_funcbase;
328 unsigned char *expdata, *erva;
329 unsigned long name_rvas, ordinals, nexp, ordbase;
3999122f
PM
330 char *dll_name = (char *) dll->filename;
331 int otherix = PE_SECTION_TABLE_SIZE;
a68ddad5
KT
332 int is_pe64 = 0;
333 int is_pe32 = 0;
1b6bc7e0
CF
334
335 /* Array elements are for text, data and bss in that order
3999122f 336 Initialization with RVA_START > RVA_END guarantees that
aff410f1 337 unused sections won't be matched. */
3999122f
PM
338 struct read_pe_section_data *section_data;
339 struct pe_sections_info pe_sections_info;
1b6bc7e0 340
3999122f 341 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
1b6bc7e0
CF
342
343 char const *target = bfd_get_target (objfile->obfd);
344
3999122f
PM
345 section_data = xzalloc (PE_SECTION_TABLE_SIZE
346 * sizeof (struct read_pe_section_data));
347
348 make_cleanup (free_current_contents, &section_data);
349
350 for (i=0; i < PE_SECTION_TABLE_SIZE; i++)
351 {
352 section_data[i].vma_offset = 0;
353 section_data[i].rva_start = 1;
354 section_data[i].rva_end = 0;
355 };
356 section_data[PE_SECTION_INDEX_TEXT].ms_type = mst_text;
357 section_data[PE_SECTION_INDEX_TEXT].section_name = ".text";
358 section_data[PE_SECTION_INDEX_DATA].ms_type = mst_data;
359 section_data[PE_SECTION_INDEX_DATA].section_name = ".data";
360 section_data[PE_SECTION_INDEX_BSS].ms_type = mst_bss;
361 section_data[PE_SECTION_INDEX_BSS].section_name = ".bss";
362
5e13bd89
PA
363 is_pe64 = (strcmp (target, "pe-x86-64") == 0
364 || strcmp (target, "pei-x86-64") == 0);
365 is_pe32 = (strcmp (target, "pe-i386") == 0
366 || strcmp (target, "pei-i386") == 0
367 || strcmp (target, "pe-arm-wince-little") == 0
368 || strcmp (target, "pei-arm-wince-little") == 0);
a68ddad5 369 if (!is_pe32 && !is_pe64)
1b6bc7e0 370 {
5e13bd89
PA
371 /* This is not a recognized PE format file. Abort now, because
372 the code is untested on anything else. *FIXME* test on
aff410f1 373 further architectures and loosen or remove this test. */
feb14725 374 do_cleanups (back_to);
1b6bc7e0
CF
375 return;
376 }
377
378 /* Get pe_header, optional header and numbers of export entries. */
379 pe_header_offset = pe_get32 (dll, 0x3c);
380 opthdr_ofs = pe_header_offset + 4 + 20;
a68ddad5 381 if (is_pe64)
1dac1b47 382 num_entries = pe_get32 (dll, opthdr_ofs + 108);
a68ddad5
KT
383 else
384 num_entries = pe_get32 (dll, opthdr_ofs + 92);
1b6bc7e0
CF
385
386 if (num_entries < 1) /* No exports. */
387 {
feb14725 388 do_cleanups (back_to);
1b6bc7e0
CF
389 return;
390 }
a68ddad5
KT
391 if (is_pe64)
392 {
3999122f
PM
393 export_opthdrrva = pe_get32 (dll, opthdr_ofs + 112);
394 export_opthdrsize = pe_get32 (dll, opthdr_ofs + 116);
a68ddad5
KT
395 }
396 else
397 {
3999122f
PM
398 export_opthdrrva = pe_get32 (dll, opthdr_ofs + 96);
399 export_opthdrsize = pe_get32 (dll, opthdr_ofs + 100);
a68ddad5 400 }
1b6bc7e0
CF
401 nsections = pe_get16 (dll, pe_header_offset + 4 + 2);
402 secptr = (pe_header_offset + 4 + 20 +
403 pe_get16 (dll, pe_header_offset + 4 + 16));
404 expptr = 0;
3999122f 405 export_size = 0;
1b6bc7e0
CF
406
407 /* Get the rva and size of the export section. */
408 for (i = 0; i < nsections; i++)
409 {
410 char sname[8];
411 unsigned long secptr1 = secptr + 40 * i;
412 unsigned long vaddr = pe_get32 (dll, secptr1 + 12);
413 unsigned long vsize = pe_get32 (dll, secptr1 + 16);
414 unsigned long fptr = pe_get32 (dll, secptr1 + 20);
415
416 bfd_seek (dll, (file_ptr) secptr1, SEEK_SET);
3999122f 417 bfd_bread (sname, (bfd_size_type) sizeof (sname), dll);
1b6bc7e0 418
3999122f
PM
419 if ((strcmp (sname, ".edata") == 0)
420 || (vaddr <= export_opthdrrva && export_opthdrrva < vaddr + vsize))
1b6bc7e0 421 {
3999122f
PM
422 if (strcmp (sname, ".edata") != 0)
423 {
424 if (debug_coff_pe_read)
425 fprintf_unfiltered (gdb_stdlog, _("Export RVA for dll "
426 "\"%s\" is in section \"%s\"\n"),
427 dll_name, sname);
428 }
429 else if (export_opthdrrva != vaddr && debug_coff_pe_read)
430 fprintf_unfiltered (gdb_stdlog, _("Wrong value of export RVA"
431 " for dll \"%s\": 0x%lx instead of 0x%lx\n"),
432 dll_name, export_opthdrrva, vaddr);
433 expptr = fptr + (export_opthdrrva - vaddr);
1b6bc7e0
CF
434 break;
435 }
436 }
437
3999122f
PM
438 export_rva = export_opthdrrva;
439 export_size = export_opthdrsize;
440
1b6bc7e0
CF
441 if (export_size == 0)
442 {
aff410f1 443 /* Empty export table. */
feb14725 444 do_cleanups (back_to);
1b6bc7e0
CF
445 return;
446 }
447
aff410f1
MS
448 /* Scan sections and store the base and size of the relevant
449 sections. */
1b6bc7e0
CF
450 for (i = 0; i < nsections; i++)
451 {
452 unsigned long secptr1 = secptr + 40 * i;
453 unsigned long vsize = pe_get32 (dll, secptr1 + 8);
454 unsigned long vaddr = pe_get32 (dll, secptr1 + 12);
3999122f 455 unsigned long characteristics = pe_get32 (dll, secptr1 + 36);
aab2f004 456 char sec_name[SCNNMLEN + 1];
1b6bc7e0
CF
457 int sectix;
458
1b6bc7e0 459 bfd_seek (dll, (file_ptr) secptr1 + 0, SEEK_SET);
aab2f004
PA
460 bfd_bread (sec_name, (bfd_size_type) SCNNMLEN, dll);
461 sec_name[SCNNMLEN] = '\0';
1b6bc7e0
CF
462
463 sectix = read_pe_section_index (sec_name);
464
465 if (sectix != PE_SECTION_INDEX_INVALID)
466 {
467 section_data[sectix].rva_start = vaddr;
468 section_data[sectix].rva_end = vaddr + vsize;
469 }
3999122f
PM
470 else
471 {
472 char *name;
473
474 section_data = xrealloc (section_data, (otherix + 1)
475 * sizeof (struct read_pe_section_data));
476 name = xstrdup (sec_name);
477 section_data[otherix].section_name = name;
478 make_cleanup (xfree, name);
479 section_data[otherix].rva_start = vaddr;
480 section_data[otherix].rva_end = vaddr + vsize;
481 section_data[otherix].vma_offset = 0;
482 if (characteristics & IMAGE_SCN_CNT_CODE)
483 section_data[otherix].ms_type = mst_text;
484 else if (characteristics & IMAGE_SCN_CNT_INITIALIZED_DATA)
485 section_data[otherix].ms_type = mst_data;
486 else if (characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA)
487 section_data[otherix].ms_type = mst_bss;
488 else
489 section_data[otherix].ms_type = mst_unknown;
490 otherix++;
491 }
1b6bc7e0
CF
492 }
493
494 expdata = (unsigned char *) xmalloc (export_size);
3999122f 495 make_cleanup (xfree, expdata);
1b6bc7e0
CF
496
497 bfd_seek (dll, (file_ptr) expptr, SEEK_SET);
498 bfd_bread (expdata, (bfd_size_type) export_size, dll);
499 erva = expdata - export_rva;
500
501 nexp = pe_as32 (expdata + 24);
502 name_rvas = pe_as32 (expdata + 32);
503 ordinals = pe_as32 (expdata + 36);
504 ordbase = pe_as32 (expdata + 16);
505 exp_funcbase = pe_as32 (expdata + 28);
506
aff410f1 507 /* Use internal dll name instead of full pathname. */
1b6bc7e0
CF
508 dll_name = pe_as32 (expdata + 12) + erva;
509
3999122f
PM
510 pe_sections_info.nb_sections = otherix;
511 pe_sections_info.sections = section_data;
512
513 bfd_map_over_sections (dll, get_section_vmas, &pe_sections_info);
1b6bc7e0
CF
514
515 /* Adjust the vma_offsets in case this PE got relocated. This
516 assumes that *all* sections share the same relocation offset
aff410f1 517 as the text section. */
3999122f 518 for (i = 0; i < otherix; i++)
1b6bc7e0
CF
519 {
520 section_data[i].vma_offset
521 += ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
522 }
523
1b6bc7e0 524 /* Truncate name at first dot. Should maybe also convert to all
aff410f1 525 lower case for convenience on Windows. */
1b6bc7e0
CF
526 read_pe_truncate_name (dll_name);
527
3999122f
PM
528 if (debug_coff_pe_read)
529 fprintf_unfiltered (gdb_stdlog, _("DLL \"%s\" has %ld export entries,"
530 " base=%ld\n"), dll_name, nexp, ordbase);
531 nbforward = 0;
532 nbnormal = 0;
1b6bc7e0
CF
533 /* Iterate through the list of symbols. */
534 for (i = 0; i < nexp; i++)
535 {
536 /* Pointer to the names vector. */
537 unsigned long name_rva = pe_as32 (erva + name_rvas + i * 4);
3999122f
PM
538 /* Retrieve ordinal value. */
539
540 unsigned long ordinal = pe_as16 (erva + ordinals + i * 2);
541
1b6bc7e0
CF
542
543 /* Pointer to the function address vector. */
3999122f
PM
544 /* This is relatived to ordinal value. */
545 unsigned long func_rva = pe_as32 (erva + exp_funcbase +
546 ordinal * 4);
1b6bc7e0 547
aff410f1 548 /* Find this symbol's section in our own array. */
1b6bc7e0 549 int sectix = 0;
3999122f
PM
550 int section_found = 0;
551
552 /* First handle forward cases. */
553 if (func_rva >= export_rva && func_rva < export_rva + export_size)
554 {
555 char *forward_name = (char *) (erva + func_rva);
556 char *funcname = (char *) (erva + name_rva);
557 char *forward_dll_name = forward_name;
558 char *forward_func_name = forward_name;
559 char *sep = strrchr (forward_name, '.');
560
561 if (sep)
562 {
563 int len = (int) (sep - forward_name);
1b6bc7e0 564
3999122f
PM
565 forward_dll_name = alloca (len + 1);
566 strncpy (forward_dll_name, forward_name, len);
567 forward_dll_name[len] = '\0';
568 forward_func_name = ++sep;
569 }
570 if (add_pe_forwarded_sym (funcname, forward_dll_name,
571 forward_func_name, ordinal,
572 dll_name, objfile) != 0)
573 ++nbforward;
574 continue;
575 }
576
577 for (sectix = 0; sectix < otherix; ++sectix)
1b6bc7e0
CF
578 {
579 if ((func_rva >= section_data[sectix].rva_start)
580 && (func_rva < section_data[sectix].rva_end))
581 {
3999122f 582 section_found = 1;
1b6bc7e0 583 add_pe_exported_sym (erva + name_rva,
3999122f 584 func_rva, ordinal,
1b6bc7e0 585 section_data + sectix, dll_name, objfile);
3999122f 586 ++nbnormal;
1b6bc7e0
CF
587 break;
588 }
589 }
3999122f
PM
590 if (!section_found)
591 {
592 char *funcname = (char *) (erva + name_rva);
593
594 if (name_rva == 0)
595 {
596 add_pe_exported_sym (NULL, func_rva, ordinal,
597 section_data, dll_name, objfile);
598 ++nbnormal;
599 }
600 else if (debug_coff_pe_read)
601 fprintf_unfiltered (gdb_stdlog, _("Export name \"%s\" ord. %lu,"
602 " RVA 0x%lx in dll \"%s\" not handled\n"),
603 funcname, ordinal, func_rva, dll_name);
604 }
1b6bc7e0
CF
605 }
606
3999122f
PM
607 if (debug_coff_pe_read)
608 fprintf_unfiltered (gdb_stdlog, _("Finished reading \"%s\", exports %ld,"
609 " forwards %ld, total %ld/%ld.\n"), dll_name, nbnormal,
610 nbforward, nbnormal + nbforward, nexp);
611 /* Discard expdata and section_data. */
1b6bc7e0
CF
612 do_cleanups (back_to);
613}
3999122f
PM
614
615/* Extract from ABFD the offset of the .text section.
616 This offset is mainly related to the offset within the file.
617 The value was previously expected to be 0x1000 for all files,
618 but some Windows OS core DLLs seem to use 0x10000 section alignement
619 which modified the return value of that function.
620 Still return default 0x1000 value if ABFD is NULL or
621 if '.text' section is not found, but that should not happen... */
622
623#define DEFAULT_COFF_PE_TEXT_SECTION_OFFSET 0x1000
624
625CORE_ADDR
626pe_text_section_offset (struct bfd *abfd)
627
628{
cebca8c1
AR
629 unsigned long pe_header_offset, i;
630 unsigned long nsections, secptr;
3999122f
PM
631 int is_pe64 = 0;
632 int is_pe32 = 0;
633 char const *target;
634
635 if (!abfd)
636 return DEFAULT_COFF_PE_TEXT_SECTION_OFFSET;
637
638 target = bfd_get_target (abfd);
639
640 is_pe64 = (strcmp (target, "pe-x86-64") == 0
641 || strcmp (target, "pei-x86-64") == 0);
642 is_pe32 = (strcmp (target, "pe-i386") == 0
643 || strcmp (target, "pei-i386") == 0
644 || strcmp (target, "pe-arm-wince-little") == 0
645 || strcmp (target, "pei-arm-wince-little") == 0);
646
647 if (!is_pe32 && !is_pe64)
648 {
649 /* This is not a recognized PE format file. Abort now, because
650 the code is untested on anything else. *FIXME* test on
651 further architectures and loosen or remove this test. */
652 return DEFAULT_COFF_PE_TEXT_SECTION_OFFSET;
653 }
654
655 /* Get pe_header, optional header and numbers of sections. */
656 pe_header_offset = pe_get32 (abfd, 0x3c);
3999122f
PM
657 nsections = pe_get16 (abfd, pe_header_offset + 4 + 2);
658 secptr = (pe_header_offset + 4 + 20 +
659 pe_get16 (abfd, pe_header_offset + 4 + 16));
660
661 /* Get the rva and size of the export section. */
662 for (i = 0; i < nsections; i++)
663 {
d8f4a83e 664 char sname[SCNNMLEN + 1];
3999122f
PM
665 unsigned long secptr1 = secptr + 40 * i;
666 unsigned long vaddr = pe_get32 (abfd, secptr1 + 12);
667
668 bfd_seek (abfd, (file_ptr) secptr1, SEEK_SET);
d8f4a83e
PM
669 bfd_bread (sname, (bfd_size_type) SCNNMLEN, abfd);
670 sname[SCNNMLEN] = '\0';
3999122f
PM
671 if (strcmp (sname, ".text") == 0)
672 return vaddr;
673 }
674
675 return DEFAULT_COFF_PE_TEXT_SECTION_OFFSET;
676}
677
678/* Implements "show debug coff_pe_read" command. */
679
680static void
681show_debug_coff_pe_read (struct ui_file *file, int from_tty,
682 struct cmd_list_element *c, const char *value)
683{
684 fprintf_filtered (file, _("Coff PE read debugging is %s.\n"), value);
685}
686
687/* Provide a prototype to silence -Wmissing-prototypes. */
688
689void _initialize_coff_pe_read (void);
690
691/* Adds "Set/show debug coff_pe_read" commands. */
692
693void
694_initialize_coff_pe_read (void)
695{
826ecc4d 696 add_setshow_zuinteger_cmd ("coff-pe-read", class_maintenance,
b75bf488
PA
697 &debug_coff_pe_read,
698 _("Set coff PE read debugging."),
699 _("Show coff PE read debugging."),
700 _("When set, debugging messages for coff reading "
701 "of exported symbols are displayed."),
702 NULL, show_debug_coff_pe_read,
703 &setdebuglist, &showdebuglist);
3999122f 704}
This page took 0.68343 seconds and 4 git commands to generate.