| 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-2018 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 | 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 | struct read_pe_section_data *sections, |
| 104 | int nb_sections) |
| 105 | { |
| 106 | int i; |
| 107 | |
| 108 | for (i = 0; i < nb_sections; i++) |
| 109 | if (sections[i].section_name == section_name) |
| 110 | return i; |
| 111 | return PE_SECTION_INDEX_INVALID; |
| 112 | } |
| 113 | |
| 114 | /* Structure used by get_section_vmas function below |
| 115 | to access section_data array and the size of the array |
| 116 | stored in nb_sections field. */ |
| 117 | struct pe_sections_info |
| 118 | { |
| 119 | int nb_sections; |
| 120 | struct read_pe_section_data *sections; |
| 121 | }; |
| 122 | |
| 123 | /* Record the virtual memory address of a section. */ |
| 124 | |
| 125 | static void |
| 126 | get_section_vmas (bfd *abfd, asection *sectp, void *context) |
| 127 | { |
| 128 | struct pe_sections_info *data = (struct pe_sections_info *) context; |
| 129 | struct read_pe_section_data *sections = data->sections; |
| 130 | int sectix = get_pe_section_index (sectp->name, sections, |
| 131 | data->nb_sections); |
| 132 | |
| 133 | if (sectix != PE_SECTION_INDEX_INVALID) |
| 134 | { |
| 135 | /* Data within the section start at rva_start in the pe and at |
| 136 | bfd_get_section_vma() within memory. Store the offset. */ |
| 137 | |
| 138 | sections[sectix].vma_offset |
| 139 | = bfd_get_section_vma (abfd, sectp) - sections[sectix].rva_start; |
| 140 | } |
| 141 | } |
| 142 | \f |
| 143 | /* Create a minimal symbol entry for an exported symbol. |
| 144 | SYM_NAME contains the exported name or NULL if exported by ordinal, |
| 145 | FUNC_RVA contains the Relative Virtual Address of the symbol, |
| 146 | ORDINAL is the ordinal index value of the symbol, |
| 147 | SECTION_DATA contains information about the section in which the |
| 148 | symbol is declared, |
| 149 | DLL_NAME is the internal name of the DLL file, |
| 150 | OBJFILE is the objfile struct of DLL_NAME. */ |
| 151 | |
| 152 | static void |
| 153 | add_pe_exported_sym (minimal_symbol_reader &reader, |
| 154 | const char *sym_name, |
| 155 | unsigned long func_rva, |
| 156 | int ordinal, |
| 157 | const struct read_pe_section_data *section_data, |
| 158 | const char *dll_name, struct objfile *objfile) |
| 159 | { |
| 160 | char *qualified_name, *bare_name; |
| 161 | /* Add the stored offset to get the loaded address of the symbol. */ |
| 162 | CORE_ADDR vma = func_rva + section_data->vma_offset; |
| 163 | |
| 164 | /* Generate a (hopefully unique) qualified name using the first part |
| 165 | of the dll name, e.g. KERNEL32!AddAtomA. This matches the style |
| 166 | used by windbg from the "Microsoft Debugging Tools for Windows". */ |
| 167 | |
| 168 | if (sym_name == NULL || *sym_name == '\0') |
| 169 | bare_name = xstrprintf ("#%d", ordinal); |
| 170 | else |
| 171 | bare_name = xstrdup (sym_name); |
| 172 | |
| 173 | qualified_name = xstrprintf ("%s!%s", dll_name, bare_name); |
| 174 | |
| 175 | if ((section_data->ms_type == mst_unknown) && debug_coff_pe_read) |
| 176 | fprintf_unfiltered (gdb_stdlog , _("Unknown section type for \"%s\"" |
| 177 | " for entry \"%s\" in dll \"%s\"\n"), |
| 178 | section_data->section_name.c_str (), sym_name, |
| 179 | dll_name); |
| 180 | |
| 181 | reader.record_with_info (qualified_name, vma, section_data->ms_type, |
| 182 | section_data->index); |
| 183 | |
| 184 | /* Enter the plain name as well, which might not be unique. */ |
| 185 | reader.record_with_info (bare_name, vma, section_data->ms_type, |
| 186 | section_data->index); |
| 187 | if (debug_coff_pe_read > 1) |
| 188 | fprintf_unfiltered (gdb_stdlog, _("Adding exported symbol \"%s\"" |
| 189 | " in dll \"%s\"\n"), sym_name, dll_name); |
| 190 | xfree (qualified_name); |
| 191 | xfree (bare_name); |
| 192 | } |
| 193 | |
| 194 | /* Create a minimal symbol entry for an exported forward symbol. |
| 195 | Return 1 if the forwarded function was found 0 otherwise. |
| 196 | SYM_NAME contains the exported name or NULL if exported by ordinal, |
| 197 | FORWARD_DLL_NAME is the name of the DLL in which the target symobl resides, |
| 198 | FORWARD_FUNC_NAME is the name of the target symbol in that DLL, |
| 199 | ORDINAL is the ordinal index value of the symbol, |
| 200 | DLL_NAME is the internal name of the DLL file, |
| 201 | OBJFILE is the objfile struct of DLL_NAME. */ |
| 202 | |
| 203 | static int |
| 204 | add_pe_forwarded_sym (minimal_symbol_reader &reader, |
| 205 | const char *sym_name, const char *forward_dll_name, |
| 206 | const char *forward_func_name, int ordinal, |
| 207 | const char *dll_name, struct objfile *objfile) |
| 208 | { |
| 209 | CORE_ADDR vma, baseaddr; |
| 210 | struct bound_minimal_symbol msymbol; |
| 211 | enum minimal_symbol_type msymtype; |
| 212 | char *qualified_name, *bare_name; |
| 213 | int forward_dll_name_len = strlen (forward_dll_name); |
| 214 | int forward_func_name_len = strlen (forward_func_name); |
| 215 | int forward_len = forward_dll_name_len + forward_func_name_len + 2; |
| 216 | char *forward_qualified_name = (char *) alloca (forward_len); |
| 217 | short section; |
| 218 | |
| 219 | xsnprintf (forward_qualified_name, forward_len, "%s!%s", forward_dll_name, |
| 220 | forward_func_name); |
| 221 | |
| 222 | |
| 223 | msymbol = lookup_bound_minimal_symbol (forward_qualified_name); |
| 224 | |
| 225 | if (!msymbol.minsym) |
| 226 | { |
| 227 | int i; |
| 228 | |
| 229 | for (i = 0; i < forward_dll_name_len; i++) |
| 230 | forward_qualified_name[i] = tolower (forward_qualified_name[i]); |
| 231 | msymbol = lookup_bound_minimal_symbol (forward_qualified_name); |
| 232 | } |
| 233 | |
| 234 | if (!msymbol.minsym) |
| 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 = BMSYMBOL_VALUE_ADDRESS (msymbol); |
| 250 | msymtype = MSYMBOL_TYPE (msymbol.minsym); |
| 251 | section = MSYMBOL_SECTION (msymbol.minsym); |
| 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 | /* Note that this code makes a minimal symbol whose value may point |
| 265 | outside of any section in this objfile. These symbols can't |
| 266 | really be relocated properly, but nevertheless we make a stab at |
| 267 | it, choosing an approach consistent with the history of this |
| 268 | code. */ |
| 269 | baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile)); |
| 270 | |
| 271 | reader.record_with_info (qualified_name, vma - baseaddr, msymtype, section); |
| 272 | |
| 273 | /* Enter the plain name as well, which might not be unique. */ |
| 274 | reader.record_with_info (bare_name, vma - baseaddr, msymtype, section); |
| 275 | xfree (qualified_name); |
| 276 | xfree (bare_name); |
| 277 | |
| 278 | return 1; |
| 279 | } |
| 280 | |
| 281 | /* Truncate a dll_name at the last dot character. */ |
| 282 | |
| 283 | static void |
| 284 | read_pe_truncate_name (char *dll_name) |
| 285 | { |
| 286 | char *last_point = strrchr (dll_name, '.'); |
| 287 | |
| 288 | if (last_point != NULL) |
| 289 | *last_point = '\0'; |
| 290 | } |
| 291 | \f |
| 292 | /* Low-level support functions, direct from the ld module pe-dll.c. */ |
| 293 | static unsigned int |
| 294 | pe_get16 (bfd *abfd, int where) |
| 295 | { |
| 296 | unsigned char b[2]; |
| 297 | |
| 298 | bfd_seek (abfd, (file_ptr) where, SEEK_SET); |
| 299 | bfd_bread (b, (bfd_size_type) 2, abfd); |
| 300 | return b[0] + (b[1] << 8); |
| 301 | } |
| 302 | |
| 303 | static unsigned int |
| 304 | pe_get32 (bfd *abfd, int where) |
| 305 | { |
| 306 | unsigned char b[4]; |
| 307 | |
| 308 | bfd_seek (abfd, (file_ptr) where, SEEK_SET); |
| 309 | bfd_bread (b, (bfd_size_type) 4, abfd); |
| 310 | return b[0] + (b[1] << 8) + (b[2] << 16) + (b[3] << 24); |
| 311 | } |
| 312 | |
| 313 | static unsigned int |
| 314 | pe_as16 (void *ptr) |
| 315 | { |
| 316 | unsigned char *b = (unsigned char *) ptr; |
| 317 | |
| 318 | return b[0] + (b[1] << 8); |
| 319 | } |
| 320 | |
| 321 | static unsigned int |
| 322 | pe_as32 (void *ptr) |
| 323 | { |
| 324 | unsigned char *b = (unsigned char *) ptr; |
| 325 | |
| 326 | return b[0] + (b[1] << 8) + (b[2] << 16) + (b[3] << 24); |
| 327 | } |
| 328 | \f |
| 329 | /* Read the (non-debug) export symbol table from a portable |
| 330 | executable. Code originally lifted from the ld function |
| 331 | pe_implied_import_dll in pe-dll.c. */ |
| 332 | |
| 333 | void |
| 334 | read_pe_exported_syms (minimal_symbol_reader &reader, |
| 335 | struct objfile *objfile) |
| 336 | { |
| 337 | bfd *dll = objfile->obfd; |
| 338 | unsigned long nbnormal, nbforward; |
| 339 | unsigned long pe_header_offset, opthdr_ofs, num_entries, i; |
| 340 | unsigned long export_opthdrrva, export_opthdrsize; |
| 341 | unsigned long export_rva, export_size, nsections, secptr, expptr; |
| 342 | unsigned long exp_funcbase; |
| 343 | unsigned char *expdata, *erva; |
| 344 | unsigned long name_rvas, ordinals, nexp, ordbase; |
| 345 | char *dll_name = (char *) dll->filename; |
| 346 | int otherix = PE_SECTION_TABLE_SIZE; |
| 347 | int is_pe64 = 0; |
| 348 | int is_pe32 = 0; |
| 349 | |
| 350 | /* Array elements are for text, data and bss in that order |
| 351 | Initialization with RVA_START > RVA_END guarantees that |
| 352 | unused sections won't be matched. */ |
| 353 | struct pe_sections_info pe_sections_info; |
| 354 | |
| 355 | char const *target = bfd_get_target (objfile->obfd); |
| 356 | |
| 357 | std::vector<struct read_pe_section_data> section_data |
| 358 | (PE_SECTION_TABLE_SIZE); |
| 359 | |
| 360 | for (i=0; i < PE_SECTION_TABLE_SIZE; i++) |
| 361 | { |
| 362 | section_data[i].vma_offset = 0; |
| 363 | section_data[i].rva_start = 1; |
| 364 | section_data[i].rva_end = 0; |
| 365 | }; |
| 366 | section_data[PE_SECTION_INDEX_TEXT].ms_type = mst_text; |
| 367 | section_data[PE_SECTION_INDEX_TEXT].section_name = ".text"; |
| 368 | section_data[PE_SECTION_INDEX_DATA].ms_type = mst_data; |
| 369 | section_data[PE_SECTION_INDEX_DATA].section_name = ".data"; |
| 370 | section_data[PE_SECTION_INDEX_BSS].ms_type = mst_bss; |
| 371 | section_data[PE_SECTION_INDEX_BSS].section_name = ".bss"; |
| 372 | |
| 373 | is_pe64 = (strcmp (target, "pe-x86-64") == 0 |
| 374 | || strcmp (target, "pei-x86-64") == 0); |
| 375 | is_pe32 = (strcmp (target, "pe-i386") == 0 |
| 376 | || strcmp (target, "pei-i386") == 0 |
| 377 | || strcmp (target, "pe-arm-wince-little") == 0 |
| 378 | || strcmp (target, "pei-arm-wince-little") == 0); |
| 379 | if (!is_pe32 && !is_pe64) |
| 380 | { |
| 381 | /* This is not a recognized PE format file. Abort now, because |
| 382 | the code is untested on anything else. *FIXME* test on |
| 383 | further architectures and loosen or remove this test. */ |
| 384 | return; |
| 385 | } |
| 386 | |
| 387 | /* Get pe_header, optional header and numbers of export entries. */ |
| 388 | pe_header_offset = pe_get32 (dll, 0x3c); |
| 389 | opthdr_ofs = pe_header_offset + 4 + 20; |
| 390 | if (is_pe64) |
| 391 | num_entries = pe_get32 (dll, opthdr_ofs + 108); |
| 392 | else |
| 393 | num_entries = pe_get32 (dll, opthdr_ofs + 92); |
| 394 | |
| 395 | if (num_entries < 1) /* No exports. */ |
| 396 | return; |
| 397 | if (is_pe64) |
| 398 | { |
| 399 | export_opthdrrva = pe_get32 (dll, opthdr_ofs + 112); |
| 400 | export_opthdrsize = pe_get32 (dll, opthdr_ofs + 116); |
| 401 | } |
| 402 | else |
| 403 | { |
| 404 | export_opthdrrva = pe_get32 (dll, opthdr_ofs + 96); |
| 405 | export_opthdrsize = pe_get32 (dll, opthdr_ofs + 100); |
| 406 | } |
| 407 | nsections = pe_get16 (dll, pe_header_offset + 4 + 2); |
| 408 | secptr = (pe_header_offset + 4 + 20 + |
| 409 | pe_get16 (dll, pe_header_offset + 4 + 16)); |
| 410 | expptr = 0; |
| 411 | export_size = 0; |
| 412 | |
| 413 | /* Get the rva and size of the export section. */ |
| 414 | for (i = 0; i < nsections; i++) |
| 415 | { |
| 416 | char sname[8]; |
| 417 | unsigned long secptr1 = secptr + 40 * i; |
| 418 | unsigned long vaddr = pe_get32 (dll, secptr1 + 12); |
| 419 | unsigned long vsize = pe_get32 (dll, secptr1 + 16); |
| 420 | unsigned long fptr = pe_get32 (dll, secptr1 + 20); |
| 421 | |
| 422 | bfd_seek (dll, (file_ptr) secptr1, SEEK_SET); |
| 423 | bfd_bread (sname, (bfd_size_type) sizeof (sname), dll); |
| 424 | |
| 425 | if ((strcmp (sname, ".edata") == 0) |
| 426 | || (vaddr <= export_opthdrrva && export_opthdrrva < vaddr + vsize)) |
| 427 | { |
| 428 | if (strcmp (sname, ".edata") != 0) |
| 429 | { |
| 430 | if (debug_coff_pe_read) |
| 431 | fprintf_unfiltered (gdb_stdlog, _("Export RVA for dll " |
| 432 | "\"%s\" is in section \"%s\"\n"), |
| 433 | dll_name, sname); |
| 434 | } |
| 435 | else if (export_opthdrrva != vaddr && debug_coff_pe_read) |
| 436 | fprintf_unfiltered (gdb_stdlog, _("Wrong value of export RVA" |
| 437 | " for dll \"%s\": 0x%lx instead of 0x%lx\n"), |
| 438 | dll_name, export_opthdrrva, vaddr); |
| 439 | expptr = fptr + (export_opthdrrva - vaddr); |
| 440 | break; |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | export_rva = export_opthdrrva; |
| 445 | export_size = export_opthdrsize; |
| 446 | |
| 447 | if (export_size == 0) |
| 448 | { |
| 449 | /* Empty export table. */ |
| 450 | return; |
| 451 | } |
| 452 | |
| 453 | /* Scan sections and store the base and size of the relevant |
| 454 | sections. */ |
| 455 | for (i = 0; i < nsections; i++) |
| 456 | { |
| 457 | unsigned long secptr1 = secptr + 40 * i; |
| 458 | unsigned long vsize = pe_get32 (dll, secptr1 + 8); |
| 459 | unsigned long vaddr = pe_get32 (dll, secptr1 + 12); |
| 460 | unsigned long characteristics = pe_get32 (dll, secptr1 + 36); |
| 461 | char sec_name[SCNNMLEN + 1]; |
| 462 | int sectix; |
| 463 | unsigned int bfd_section_index; |
| 464 | asection *section; |
| 465 | |
| 466 | bfd_seek (dll, (file_ptr) secptr1 + 0, SEEK_SET); |
| 467 | bfd_bread (sec_name, (bfd_size_type) SCNNMLEN, dll); |
| 468 | sec_name[SCNNMLEN] = '\0'; |
| 469 | |
| 470 | sectix = read_pe_section_index (sec_name); |
| 471 | section = bfd_get_section_by_name (dll, sec_name); |
| 472 | if (section) |
| 473 | bfd_section_index = section->index; |
| 474 | else |
| 475 | bfd_section_index = -1; |
| 476 | |
| 477 | if (sectix != PE_SECTION_INDEX_INVALID) |
| 478 | { |
| 479 | section_data[sectix].rva_start = vaddr; |
| 480 | section_data[sectix].rva_end = vaddr + vsize; |
| 481 | section_data[sectix].index = bfd_section_index; |
| 482 | } |
| 483 | else |
| 484 | { |
| 485 | section_data.resize (otherix + 1); |
| 486 | section_data[otherix].section_name = sec_name; |
| 487 | section_data[otherix].rva_start = vaddr; |
| 488 | section_data[otherix].rva_end = vaddr + vsize; |
| 489 | section_data[otherix].vma_offset = 0; |
| 490 | section_data[otherix].index = bfd_section_index; |
| 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 | gdb::def_vector<unsigned char> expdata_storage (export_size); |
| 504 | expdata = expdata_storage.data (); |
| 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 = (char *) (pe_as32 (expdata + 12) + erva); |
| 518 | |
| 519 | pe_sections_info.nb_sections = otherix; |
| 520 | pe_sections_info.sections = section_data.data (); |
| 521 | |
| 522 | bfd_map_over_sections (dll, get_section_vmas, &pe_sections_info); |
| 523 | |
| 524 | /* Truncate name at first dot. Should maybe also convert to all |
| 525 | lower case for convenience on Windows. */ |
| 526 | read_pe_truncate_name (dll_name); |
| 527 | |
| 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; |
| 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); |
| 538 | /* Retrieve ordinal value. */ |
| 539 | |
| 540 | unsigned long ordinal = pe_as16 (erva + ordinals + i * 2); |
| 541 | |
| 542 | |
| 543 | /* Pointer to the function address vector. */ |
| 544 | /* This is relatived to ordinal value. */ |
| 545 | unsigned long func_rva = pe_as32 (erva + exp_funcbase + |
| 546 | ordinal * 4); |
| 547 | |
| 548 | /* Find this symbol's section in our own array. */ |
| 549 | int sectix = 0; |
| 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); |
| 564 | |
| 565 | forward_dll_name = (char *) 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 (reader, 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) |
| 578 | { |
| 579 | if ((func_rva >= section_data[sectix].rva_start) |
| 580 | && (func_rva < section_data[sectix].rva_end)) |
| 581 | { |
| 582 | char *sym_name = (char *) (erva + name_rva); |
| 583 | |
| 584 | section_found = 1; |
| 585 | add_pe_exported_sym (reader, sym_name, func_rva, ordinal, |
| 586 | §ion_data[sectix], dll_name, objfile); |
| 587 | ++nbnormal; |
| 588 | break; |
| 589 | } |
| 590 | } |
| 591 | if (!section_found) |
| 592 | { |
| 593 | char *funcname = (char *) (erva + name_rva); |
| 594 | |
| 595 | if (name_rva == 0) |
| 596 | { |
| 597 | add_pe_exported_sym (reader, NULL, func_rva, ordinal, |
| 598 | §ion_data[0], dll_name, objfile); |
| 599 | ++nbnormal; |
| 600 | } |
| 601 | else if (debug_coff_pe_read) |
| 602 | fprintf_unfiltered (gdb_stdlog, _("Export name \"%s\" ord. %lu," |
| 603 | " RVA 0x%lx in dll \"%s\" not handled\n"), |
| 604 | funcname, ordinal, func_rva, dll_name); |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | if (debug_coff_pe_read) |
| 609 | fprintf_unfiltered (gdb_stdlog, _("Finished reading \"%s\", exports %ld," |
| 610 | " forwards %ld, total %ld/%ld.\n"), dll_name, nbnormal, |
| 611 | nbforward, nbnormal + nbforward, nexp); |
| 612 | } |
| 613 | |
| 614 | /* Extract from ABFD the offset of the .text section. |
| 615 | This offset is mainly related to the offset within the file. |
| 616 | The value was previously expected to be 0x1000 for all files, |
| 617 | but some Windows OS core DLLs seem to use 0x10000 section alignement |
| 618 | which modified the return value of that function. |
| 619 | Still return default 0x1000 value if ABFD is NULL or |
| 620 | if '.text' section is not found, but that should not happen... */ |
| 621 | |
| 622 | #define DEFAULT_COFF_PE_TEXT_SECTION_OFFSET 0x1000 |
| 623 | |
| 624 | CORE_ADDR |
| 625 | pe_text_section_offset (struct bfd *abfd) |
| 626 | |
| 627 | { |
| 628 | unsigned long pe_header_offset, i; |
| 629 | unsigned long nsections, secptr; |
| 630 | int is_pe64 = 0; |
| 631 | int is_pe32 = 0; |
| 632 | char const *target; |
| 633 | |
| 634 | if (!abfd) |
| 635 | return DEFAULT_COFF_PE_TEXT_SECTION_OFFSET; |
| 636 | |
| 637 | target = bfd_get_target (abfd); |
| 638 | |
| 639 | is_pe64 = (strcmp (target, "pe-x86-64") == 0 |
| 640 | || strcmp (target, "pei-x86-64") == 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 | pe_header_offset = pe_get32 (abfd, 0x3c); |
| 656 | nsections = pe_get16 (abfd, pe_header_offset + 4 + 2); |
| 657 | secptr = (pe_header_offset + 4 + 20 + |
| 658 | pe_get16 (abfd, pe_header_offset + 4 + 16)); |
| 659 | |
| 660 | /* Get the rva and size of the export section. */ |
| 661 | for (i = 0; i < nsections; i++) |
| 662 | { |
| 663 | char sname[SCNNMLEN + 1]; |
| 664 | unsigned long secptr1 = secptr + 40 * i; |
| 665 | unsigned long vaddr = pe_get32 (abfd, secptr1 + 12); |
| 666 | |
| 667 | bfd_seek (abfd, (file_ptr) secptr1, SEEK_SET); |
| 668 | bfd_bread (sname, (bfd_size_type) SCNNMLEN, abfd); |
| 669 | sname[SCNNMLEN] = '\0'; |
| 670 | if (strcmp (sname, ".text") == 0) |
| 671 | return vaddr; |
| 672 | } |
| 673 | |
| 674 | return DEFAULT_COFF_PE_TEXT_SECTION_OFFSET; |
| 675 | } |
| 676 | |
| 677 | /* Implements "show debug coff_pe_read" command. */ |
| 678 | |
| 679 | static void |
| 680 | show_debug_coff_pe_read (struct ui_file *file, int from_tty, |
| 681 | struct cmd_list_element *c, const char *value) |
| 682 | { |
| 683 | fprintf_filtered (file, _("Coff PE read debugging is %s.\n"), value); |
| 684 | } |
| 685 | |
| 686 | /* Adds "Set/show debug coff_pe_read" commands. */ |
| 687 | |
| 688 | void |
| 689 | _initialize_coff_pe_read (void) |
| 690 | { |
| 691 | add_setshow_zuinteger_cmd ("coff-pe-read", class_maintenance, |
| 692 | &debug_coff_pe_read, |
| 693 | _("Set coff PE read debugging."), |
| 694 | _("Show coff PE read debugging."), |
| 695 | _("When set, debugging messages for coff reading " |
| 696 | "of exported symbols are displayed."), |
| 697 | NULL, show_debug_coff_pe_read, |
| 698 | &setdebuglist, &showdebuglist); |
| 699 | } |