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