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-2013 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 "common/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 char *section_name; /* Recorded section name. */
57 };
58
59 #define IMAGE_SCN_CNT_CODE 0x20
60 #define IMAGE_SCN_CNT_INITIALIZED_DATA 0x40
61 #define IMAGE_SCN_CNT_UNINITIALIZED_DATA 0x80
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
68 /* Get the index of the named section in our own array, which contains
69 text, data and bss in that order. Return PE_SECTION_INDEX_INVALID
70 if passed an unrecognised section name. */
71
72 static int
read_pe_section_index(const char * section_name)73 read_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
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
100 static int
get_pe_section_index(const char * section_name,struct read_pe_section_data * sections,int nb_sections)101 get_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. */
116 struct pe_sections_info
117 {
118 int nb_sections;
119 struct read_pe_section_data *sections;
120 };
121
122 /* Record the virtual memory address of a section. */
123
124 static void
get_section_vmas(bfd * abfd,asection * sectp,void * context)125 get_section_vmas (bfd *abfd, asection *sectp, void *context)
126 {
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);
131
132 if (sectix != PE_SECTION_INDEX_INVALID)
133 {
134 /* Data within the section start at rva_start in the pe and at
135 bfd_get_section_vma() within memory. Store the offset. */
136
137 sections[sectix].vma_offset
138 = bfd_get_section_vma (abfd, sectp) - sections[sectix].rva_start;
139 }
140 }
141
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. */
150
151 static void
add_pe_exported_sym(const char * sym_name,unsigned long func_rva,int ordinal,const struct read_pe_section_data * section_data,const char * dll_name,struct objfile * objfile)152 add_pe_exported_sym (const char *sym_name,
153 unsigned long func_rva,
154 int ordinal,
155 const struct read_pe_section_data *section_data,
156 const char *dll_name, struct objfile *objfile)
157 {
158 char *qualified_name, *bare_name;
159 /* Add the stored offset to get the loaded address of the symbol. */
160 CORE_ADDR vma = func_rva + section_data->vma_offset;
161 int dll_name_len = strlen (dll_name);
162
163 /* Generate a (hopefully unique) qualified name using the first part
164 of the dll name, e.g. KERNEL32!AddAtomA. This matches the style
165 used by windbg from the "Microsoft Debugging Tools for Windows". */
166
167 if (sym_name == NULL || *sym_name == '\0')
168 bare_name = xstrprintf ("#%d", ordinal);
169 else
170 bare_name = xstrdup (sym_name);
171
172 qualified_name = xstrprintf ("%s!%s", dll_name, bare_name);
173
174 if ((section_data->ms_type == mst_unknown) && debug_coff_pe_read)
175 fprintf_unfiltered (gdb_stdlog , _("Unknown section type for \"%s\""
176 " for entry \"%s\" in dll \"%s\"\n"),
177 section_data->section_name, sym_name, dll_name);
178
179 prim_record_minimal_symbol (qualified_name, vma,
180 section_data->ms_type, objfile);
181
182 /* Enter the plain name as well, which might not be unique. */
183 prim_record_minimal_symbol (bare_name, vma, section_data->ms_type, objfile);
184 if (debug_coff_pe_read > 1)
185 fprintf_unfiltered (gdb_stdlog, _("Adding exported symbol \"%s\""
186 " in dll \"%s\"\n"), sym_name, dll_name);
187 xfree (qualified_name);
188 xfree (bare_name);
189 }
190
191 /* Create a minimal symbol entry for an exported forward symbol.
192 Return 1 if the forwarded function was found 0 otherwise.
193 SYM_NAME contains the exported name or NULL if exported by ordinal,
194 FORWARD_DLL_NAME is the name of the DLL in which the target symobl resides,
195 FORWARD_FUNC_NAME is the name of the target symbol in that DLL,
196 ORDINAL is the ordinal index value of the symbol,
197 DLL_NAME is the internal name of the DLL file,
198 OBJFILE is the objfile struct of DLL_NAME. */
199
200 static int
add_pe_forwarded_sym(const char * sym_name,const char * forward_dll_name,const char * forward_func_name,int ordinal,const char * dll_name,struct objfile * objfile)201 add_pe_forwarded_sym (const char *sym_name, const char *forward_dll_name,
202 const char *forward_func_name, int ordinal,
203 const char *dll_name, struct objfile *objfile)
204 {
205 CORE_ADDR vma;
206 struct objfile *forward_objfile;
207 struct minimal_symbol *msymbol;
208 short section;
209 enum minimal_symbol_type msymtype;
210 int dll_name_len = strlen (dll_name);
211 char *qualified_name, *bare_name;
212 int forward_dll_name_len = strlen (forward_dll_name);
213 int forward_func_name_len = strlen (forward_func_name);
214 int forward_len = forward_dll_name_len + forward_func_name_len + 2;
215 char *forward_qualified_name = alloca (forward_len);
216
217 xsnprintf (forward_qualified_name, forward_len, "%s!%s", forward_dll_name,
218 forward_func_name);
219
220
221 msymbol = lookup_minimal_symbol_and_objfile (forward_qualified_name,
222 &forward_objfile);
223
224 if (!msymbol)
225 {
226 int i;
227
228 for (i = 0; i < forward_dll_name_len; i++)
229 forward_qualified_name[i] = tolower (forward_qualified_name[i]);
230 msymbol = lookup_minimal_symbol_and_objfile (forward_qualified_name,
231 &forward_objfile);
232 }
233
234 if (!msymbol)
235 {
236 if (debug_coff_pe_read)
237 fprintf_unfiltered (gdb_stdlog, _("Unable to find function \"%s\" in"
238 " dll \"%s\", forward of \"%s\" in dll \"%s\"\n"),
239 forward_func_name, forward_dll_name, sym_name,
240 dll_name);
241 return 0;
242 }
243
244 if (debug_coff_pe_read > 1)
245 fprintf_unfiltered (gdb_stdlog, _("Adding forwarded exported symbol"
246 " \"%s\" in dll \"%s\", pointing to \"%s\"\n"),
247 sym_name, dll_name, forward_qualified_name);
248
249 vma = SYMBOL_VALUE_ADDRESS (msymbol);
250 section = SYMBOL_SECTION (msymbol);
251 msymtype = MSYMBOL_TYPE (msymbol);
252
253 /* Generate a (hopefully unique) qualified name using the first part
254 of the dll name, e.g. KERNEL32!AddAtomA. This matches the style
255 used by windbg from the "Microsoft Debugging Tools for Windows". */
256
257 if (sym_name == NULL || *sym_name == '\0')
258 bare_name = xstrprintf ("#%d", ordinal);
259 else
260 bare_name = xstrdup (sym_name);
261
262 qualified_name = xstrprintf ("%s!%s", dll_name, bare_name);
263
264 prim_record_minimal_symbol (qualified_name, vma, msymtype, objfile);
265
266 /* Enter the plain name as well, which might not be unique. */
267 prim_record_minimal_symbol (bare_name, vma, msymtype, objfile);
268 xfree (qualified_name);
269 xfree (bare_name);
270
271 return 1;
272 }
273
274 /* Truncate a dll_name at the last dot character. */
275
276 static void
read_pe_truncate_name(char * dll_name)277 read_pe_truncate_name (char *dll_name)
278 {
279 char *last_point = strrchr (dll_name, '.');
280
281 if (last_point != NULL)
282 *last_point = '\0';
283 }
284
285 /* Low-level support functions, direct from the ld module pe-dll.c. */
286 static unsigned int
pe_get16(bfd * abfd,int where)287 pe_get16 (bfd *abfd, int where)
288 {
289 unsigned char b[2];
290
291 bfd_seek (abfd, (file_ptr) where, SEEK_SET);
292 bfd_bread (b, (bfd_size_type) 2, abfd);
293 return b[0] + (b[1] << 8);
294 }
295
296 static unsigned int
pe_get32(bfd * abfd,int where)297 pe_get32 (bfd *abfd, int where)
298 {
299 unsigned char b[4];
300
301 bfd_seek (abfd, (file_ptr) where, SEEK_SET);
302 bfd_bread (b, (bfd_size_type) 4, abfd);
303 return b[0] + (b[1] << 8) + (b[2] << 16) + (b[3] << 24);
304 }
305
306 static unsigned int
pe_as16(void * ptr)307 pe_as16 (void *ptr)
308 {
309 unsigned char *b = ptr;
310
311 return b[0] + (b[1] << 8);
312 }
313
314 static unsigned int
pe_as32(void * ptr)315 pe_as32 (void *ptr)
316 {
317 unsigned char *b = ptr;
318
319 return b[0] + (b[1] << 8) + (b[2] << 16) + (b[3] << 24);
320 }
321
322 /* Read the (non-debug) export symbol table from a portable
323 executable. Code originally lifted from the ld function
324 pe_implied_import_dll in pe-dll.c. */
325
326 void
read_pe_exported_syms(struct objfile * objfile)327 read_pe_exported_syms (struct objfile *objfile)
328 {
329 bfd *dll = objfile->obfd;
330 unsigned long nbnormal, nbforward;
331 unsigned long pe_header_offset, opthdr_ofs, num_entries, i;
332 unsigned long export_opthdrrva, export_opthdrsize;
333 unsigned long export_rva, export_size, nsections, secptr, expptr;
334 unsigned long exp_funcbase;
335 unsigned char *expdata, *erva;
336 unsigned long name_rvas, ordinals, nexp, ordbase;
337 char *dll_name = (char *) dll->filename;
338 int otherix = PE_SECTION_TABLE_SIZE;
339 int exportix = -1;
340 int is_pe64 = 0;
341 int is_pe32 = 0;
342
343 /* Array elements are for text, data and bss in that order
344 Initialization with RVA_START > RVA_END guarantees that
345 unused sections won't be matched. */
346 struct read_pe_section_data *section_data;
347 struct pe_sections_info pe_sections_info;
348
349 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
350
351 char const *target = bfd_get_target (objfile->obfd);
352
353 section_data = xzalloc (PE_SECTION_TABLE_SIZE
354 * sizeof (struct read_pe_section_data));
355
356 make_cleanup (free_current_contents, §ion_data);
357
358 for (i=0; i < PE_SECTION_TABLE_SIZE; i++)
359 {
360 section_data[i].vma_offset = 0;
361 section_data[i].rva_start = 1;
362 section_data[i].rva_end = 0;
363 };
364 section_data[PE_SECTION_INDEX_TEXT].ms_type = mst_text;
365 section_data[PE_SECTION_INDEX_TEXT].section_name = ".text";
366 section_data[PE_SECTION_INDEX_DATA].ms_type = mst_data;
367 section_data[PE_SECTION_INDEX_DATA].section_name = ".data";
368 section_data[PE_SECTION_INDEX_BSS].ms_type = mst_bss;
369 section_data[PE_SECTION_INDEX_BSS].section_name = ".bss";
370
371 is_pe64 = (strcmp (target, "pe-x86-64") == 0
372 || strcmp (target, "pei-x86-64") == 0);
373 is_pe32 = (strcmp (target, "pe-i386") == 0
374 || strcmp (target, "pei-i386") == 0
375 || strcmp (target, "pe-arm-wince-little") == 0
376 || strcmp (target, "pei-arm-wince-little") == 0);
377 if (!is_pe32 && !is_pe64)
378 {
379 /* This is not a recognized PE format file. Abort now, because
380 the code is untested on anything else. *FIXME* test on
381 further architectures and loosen or remove this test. */
382 do_cleanups (back_to);
383 return;
384 }
385
386 /* Get pe_header, optional header and numbers of export entries. */
387 pe_header_offset = pe_get32 (dll, 0x3c);
388 opthdr_ofs = pe_header_offset + 4 + 20;
389 if (is_pe64)
390 num_entries = pe_get32 (dll, opthdr_ofs + 108);
391 else
392 num_entries = pe_get32 (dll, opthdr_ofs + 92);
393
394 if (num_entries < 1) /* No exports. */
395 {
396 do_cleanups (back_to);
397 return;
398 }
399 if (is_pe64)
400 {
401 export_opthdrrva = pe_get32 (dll, opthdr_ofs + 112);
402 export_opthdrsize = pe_get32 (dll, opthdr_ofs + 116);
403 }
404 else
405 {
406 export_opthdrrva = pe_get32 (dll, opthdr_ofs + 96);
407 export_opthdrsize = pe_get32 (dll, opthdr_ofs + 100);
408 }
409 nsections = pe_get16 (dll, pe_header_offset + 4 + 2);
410 secptr = (pe_header_offset + 4 + 20 +
411 pe_get16 (dll, pe_header_offset + 4 + 16));
412 expptr = 0;
413 export_size = 0;
414
415 /* Get the rva and size of the export section. */
416 for (i = 0; i < nsections; i++)
417 {
418 char sname[8];
419 unsigned long secptr1 = secptr + 40 * i;
420 unsigned long vaddr = pe_get32 (dll, secptr1 + 12);
421 unsigned long vsize = pe_get32 (dll, secptr1 + 16);
422 unsigned long fptr = pe_get32 (dll, secptr1 + 20);
423
424 bfd_seek (dll, (file_ptr) secptr1, SEEK_SET);
425 bfd_bread (sname, (bfd_size_type) sizeof (sname), dll);
426
427 if ((strcmp (sname, ".edata") == 0)
428 || (vaddr <= export_opthdrrva && export_opthdrrva < vaddr + vsize))
429 {
430 if (strcmp (sname, ".edata") != 0)
431 {
432 if (debug_coff_pe_read)
433 fprintf_unfiltered (gdb_stdlog, _("Export RVA for dll "
434 "\"%s\" is in section \"%s\"\n"),
435 dll_name, sname);
436 }
437 else if (export_opthdrrva != vaddr && debug_coff_pe_read)
438 fprintf_unfiltered (gdb_stdlog, _("Wrong value of export RVA"
439 " for dll \"%s\": 0x%lx instead of 0x%lx\n"),
440 dll_name, export_opthdrrva, vaddr);
441 expptr = fptr + (export_opthdrrva - vaddr);
442 exportix = i;
443 break;
444 }
445 }
446
447 export_rva = export_opthdrrva;
448 export_size = export_opthdrsize;
449
450 if (export_size == 0)
451 {
452 /* Empty export table. */
453 do_cleanups (back_to);
454 return;
455 }
456
457 /* Scan sections and store the base and size of the relevant
458 sections. */
459 for (i = 0; i < nsections; i++)
460 {
461 unsigned long secptr1 = secptr + 40 * i;
462 unsigned long vsize = pe_get32 (dll, secptr1 + 8);
463 unsigned long vaddr = pe_get32 (dll, secptr1 + 12);
464 unsigned long characteristics = pe_get32 (dll, secptr1 + 36);
465 char sec_name[SCNNMLEN + 1];
466 int sectix;
467
468 bfd_seek (dll, (file_ptr) secptr1 + 0, SEEK_SET);
469 bfd_bread (sec_name, (bfd_size_type) SCNNMLEN, dll);
470 sec_name[SCNNMLEN] = '\0';
471
472 sectix = read_pe_section_index (sec_name);
473
474 if (sectix != PE_SECTION_INDEX_INVALID)
475 {
476 section_data[sectix].rva_start = vaddr;
477 section_data[sectix].rva_end = vaddr + vsize;
478 }
479 else
480 {
481 char *name;
482
483 section_data = xrealloc (section_data, (otherix + 1)
484 * sizeof (struct read_pe_section_data));
485 name = xstrdup (sec_name);
486 section_data[otherix].section_name = name;
487 make_cleanup (xfree, name);
488 section_data[otherix].rva_start = vaddr;
489 section_data[otherix].rva_end = vaddr + vsize;
490 section_data[otherix].vma_offset = 0;
491 if (characteristics & IMAGE_SCN_CNT_CODE)
492 section_data[otherix].ms_type = mst_text;
493 else if (characteristics & IMAGE_SCN_CNT_INITIALIZED_DATA)
494 section_data[otherix].ms_type = mst_data;
495 else if (characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA)
496 section_data[otherix].ms_type = mst_bss;
497 else
498 section_data[otherix].ms_type = mst_unknown;
499 otherix++;
500 }
501 }
502
503 expdata = (unsigned char *) xmalloc (export_size);
504 make_cleanup (xfree, expdata);
505
506 bfd_seek (dll, (file_ptr) expptr, SEEK_SET);
507 bfd_bread (expdata, (bfd_size_type) export_size, dll);
508 erva = expdata - export_rva;
509
510 nexp = pe_as32 (expdata + 24);
511 name_rvas = pe_as32 (expdata + 32);
512 ordinals = pe_as32 (expdata + 36);
513 ordbase = pe_as32 (expdata + 16);
514 exp_funcbase = pe_as32 (expdata + 28);
515
516 /* Use internal dll name instead of full pathname. */
517 dll_name = pe_as32 (expdata + 12) + erva;
518
519 pe_sections_info.nb_sections = otherix;
520 pe_sections_info.sections = section_data;
521
522 bfd_map_over_sections (dll, get_section_vmas, &pe_sections_info);
523
524 /* Adjust the vma_offsets in case this PE got relocated. This
525 assumes that *all* sections share the same relocation offset
526 as the text section. */
527 for (i = 0; i < otherix; i++)
528 {
529 section_data[i].vma_offset
530 += ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
531 }
532
533 /* Truncate name at first dot. Should maybe also convert to all
534 lower case for convenience on Windows. */
535 read_pe_truncate_name (dll_name);
536
537 if (debug_coff_pe_read)
538 fprintf_unfiltered (gdb_stdlog, _("DLL \"%s\" has %ld export entries,"
539 " base=%ld\n"), dll_name, nexp, ordbase);
540 nbforward = 0;
541 nbnormal = 0;
542 /* Iterate through the list of symbols. */
543 for (i = 0; i < nexp; i++)
544 {
545 /* Pointer to the names vector. */
546 unsigned long name_rva = pe_as32 (erva + name_rvas + i * 4);
547 /* Retrieve ordinal value. */
548
549 unsigned long ordinal = pe_as16 (erva + ordinals + i * 2);
550
551
552 /* Pointer to the function address vector. */
553 /* This is relatived to ordinal value. */
554 unsigned long func_rva = pe_as32 (erva + exp_funcbase +
555 ordinal * 4);
556
557 /* Find this symbol's section in our own array. */
558 int sectix = 0;
559 int section_found = 0;
560
561 /* First handle forward cases. */
562 if (func_rva >= export_rva && func_rva < export_rva + export_size)
563 {
564 char *forward_name = (char *) (erva + func_rva);
565 char *funcname = (char *) (erva + name_rva);
566 char *forward_dll_name = forward_name;
567 char *forward_func_name = forward_name;
568 char *sep = strrchr (forward_name, '.');
569
570 if (sep)
571 {
572 int len = (int) (sep - forward_name);
573
574 forward_dll_name = alloca (len + 1);
575 strncpy (forward_dll_name, forward_name, len);
576 forward_dll_name[len] = '\0';
577 forward_func_name = ++sep;
578 }
579 if (add_pe_forwarded_sym (funcname, forward_dll_name,
580 forward_func_name, ordinal,
581 dll_name, objfile) != 0)
582 ++nbforward;
583 continue;
584 }
585
586 for (sectix = 0; sectix < otherix; ++sectix)
587 {
588 if ((func_rva >= section_data[sectix].rva_start)
589 && (func_rva < section_data[sectix].rva_end))
590 {
591 section_found = 1;
592 add_pe_exported_sym (erva + name_rva,
593 func_rva, ordinal,
594 section_data + sectix, dll_name, objfile);
595 ++nbnormal;
596 break;
597 }
598 }
599 if (!section_found)
600 {
601 char *funcname = (char *) (erva + name_rva);
602
603 if (name_rva == 0)
604 {
605 add_pe_exported_sym (NULL, func_rva, ordinal,
606 section_data, dll_name, objfile);
607 ++nbnormal;
608 }
609 else if (debug_coff_pe_read)
610 fprintf_unfiltered (gdb_stdlog, _("Export name \"%s\" ord. %lu,"
611 " RVA 0x%lx in dll \"%s\" not handled\n"),
612 funcname, ordinal, func_rva, dll_name);
613 }
614 }
615
616 if (debug_coff_pe_read)
617 fprintf_unfiltered (gdb_stdlog, _("Finished reading \"%s\", exports %ld,"
618 " forwards %ld, total %ld/%ld.\n"), dll_name, nbnormal,
619 nbforward, nbnormal + nbforward, nexp);
620 /* Discard expdata and section_data. */
621 do_cleanups (back_to);
622 }
623
624 /* Extract from ABFD the offset of the .text section.
625 This offset is mainly related to the offset within the file.
626 The value was previously expected to be 0x1000 for all files,
627 but some Windows OS core DLLs seem to use 0x10000 section alignement
628 which modified the return value of that function.
629 Still return default 0x1000 value if ABFD is NULL or
630 if '.text' section is not found, but that should not happen... */
631
632 #define DEFAULT_COFF_PE_TEXT_SECTION_OFFSET 0x1000
633
634 CORE_ADDR
pe_text_section_offset(struct bfd * abfd)635 pe_text_section_offset (struct bfd *abfd)
636
637 {
638 unsigned long pe_header_offset, opthdr_ofs, num_entries, i;
639 unsigned long export_rva, export_size, nsections, secptr, expptr;
640 unsigned long exp_funcbase;
641 unsigned char *expdata, *erva;
642 unsigned long name_rvas, ordinals, nexp, ordbase;
643 char *dll_name;
644 int is_pe64 = 0;
645 int is_pe32 = 0;
646 char const *target;
647
648 if (!abfd)
649 return DEFAULT_COFF_PE_TEXT_SECTION_OFFSET;
650
651 target = bfd_get_target (abfd);
652
653 is_pe64 = (strcmp (target, "pe-x86-64") == 0
654 || strcmp (target, "pei-x86-64") == 0);
655 is_pe32 = (strcmp (target, "pe-i386") == 0
656 || strcmp (target, "pei-i386") == 0
657 || strcmp (target, "pe-arm-wince-little") == 0
658 || strcmp (target, "pei-arm-wince-little") == 0);
659
660 if (!is_pe32 && !is_pe64)
661 {
662 /* This is not a recognized PE format file. Abort now, because
663 the code is untested on anything else. *FIXME* test on
664 further architectures and loosen or remove this test. */
665 return DEFAULT_COFF_PE_TEXT_SECTION_OFFSET;
666 }
667
668 /* Get pe_header, optional header and numbers of sections. */
669 pe_header_offset = pe_get32 (abfd, 0x3c);
670 opthdr_ofs = pe_header_offset + 4 + 20;
671 nsections = pe_get16 (abfd, pe_header_offset + 4 + 2);
672 secptr = (pe_header_offset + 4 + 20 +
673 pe_get16 (abfd, pe_header_offset + 4 + 16));
674
675 /* Get the rva and size of the export section. */
676 for (i = 0; i < nsections; i++)
677 {
678 char sname[SCNNMLEN + 1];
679 unsigned long secptr1 = secptr + 40 * i;
680 unsigned long vaddr = pe_get32 (abfd, secptr1 + 12);
681
682 bfd_seek (abfd, (file_ptr) secptr1, SEEK_SET);
683 bfd_bread (sname, (bfd_size_type) SCNNMLEN, abfd);
684 sname[SCNNMLEN] = '\0';
685 if (strcmp (sname, ".text") == 0)
686 return vaddr;
687 }
688
689 return DEFAULT_COFF_PE_TEXT_SECTION_OFFSET;
690 }
691
692 /* Implements "show debug coff_pe_read" command. */
693
694 static void
show_debug_coff_pe_read(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)695 show_debug_coff_pe_read (struct ui_file *file, int from_tty,
696 struct cmd_list_element *c, const char *value)
697 {
698 fprintf_filtered (file, _("Coff PE read debugging is %s.\n"), value);
699 }
700
701 /* Provide a prototype to silence -Wmissing-prototypes. */
702
703 void _initialize_coff_pe_read (void);
704
705 /* Adds "Set/show debug coff_pe_read" commands. */
706
707 void
_initialize_coff_pe_read(void)708 _initialize_coff_pe_read (void)
709 {
710 add_setshow_zuinteger_cmd ("coff-pe-read", class_maintenance,
711 &debug_coff_pe_read,
712 _("Set coff PE read debugging."),
713 _("Show coff PE read debugging."),
714 _("When set, debugging messages for coff reading "
715 "of exported symbols are displayed."),
716 NULL, show_debug_coff_pe_read,
717 &setdebuglist, &showdebuglist);
718 }
719