| 1 | /* Support for the generic parts of COFF, for BFD. |
| 2 | Copyright 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc. |
| 3 | Written by Cygnus Support. |
| 4 | |
| 5 | This file is part of BFD, the Binary File Descriptor library. |
| 6 | |
| 7 | This program is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation; either version 2 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | This program is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with this program; if not, write to the Free Software |
| 19 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ |
| 20 | |
| 21 | /* Most of this hacked by Steve Chamberlain, sac@cygnus.com. |
| 22 | Split out of coffcode.h by Ian Taylor, ian@cygnus.com. */ |
| 23 | |
| 24 | /* This file contains COFF code that is not dependent on any |
| 25 | particular COFF target. There is only one version of this file in |
| 26 | libbfd.a, so no target specific code may be put in here. Or, to |
| 27 | put it another way, |
| 28 | |
| 29 | ********** DO NOT PUT TARGET SPECIFIC CODE IN THIS FILE ********** |
| 30 | |
| 31 | If you need to add some target specific behaviour, add a new hook |
| 32 | function to bfd_coff_backend_data. |
| 33 | |
| 34 | Some of these functions are also called by the ECOFF routines. |
| 35 | Those functions may not use any COFF specific information, such as |
| 36 | coff_data (abfd). */ |
| 37 | |
| 38 | #include "bfd.h" |
| 39 | #include "sysdep.h" |
| 40 | #include "libbfd.h" |
| 41 | #include "coff/internal.h" |
| 42 | #include "libcoff.h" |
| 43 | |
| 44 | static asection bfd_debug_section = { "*DEBUG*" }; |
| 45 | |
| 46 | /* Take a section header read from a coff file (in HOST byte order), |
| 47 | and make a BFD "section" out of it. This is used by ECOFF. */ |
| 48 | static boolean |
| 49 | DEFUN(make_a_section_from_file,(abfd, hdr, target_index), |
| 50 | bfd *abfd AND |
| 51 | struct internal_scnhdr *hdr AND |
| 52 | unsigned int target_index) |
| 53 | { |
| 54 | asection *return_section; |
| 55 | char *name; |
| 56 | |
| 57 | /* Assorted wastage to null-terminate the name, thanks AT&T! */ |
| 58 | name = bfd_alloc(abfd, sizeof (hdr->s_name)+1); |
| 59 | if (name == NULL) { |
| 60 | bfd_error = no_memory; |
| 61 | return false; |
| 62 | } |
| 63 | strncpy(name, (char *) &hdr->s_name[0], sizeof (hdr->s_name)); |
| 64 | name[sizeof (hdr->s_name)] = 0; |
| 65 | |
| 66 | return_section = bfd_make_section(abfd, name); |
| 67 | if (return_section == NULL) |
| 68 | return_section = bfd_coff_make_section_hook (abfd, name); |
| 69 | |
| 70 | /* Handle several sections of the same name. For example, if an executable |
| 71 | has two .bss sections, GDB better be able to find both of them |
| 72 | (PR 3562). */ |
| 73 | if (return_section == NULL) |
| 74 | return_section = bfd_make_section_anyway (abfd, name); |
| 75 | |
| 76 | if (return_section == NULL) |
| 77 | return false; |
| 78 | |
| 79 | /* s_paddr is presumed to be = to s_vaddr */ |
| 80 | |
| 81 | return_section->vma = hdr->s_vaddr; |
| 82 | return_section->_raw_size = hdr->s_size; |
| 83 | return_section->filepos = hdr->s_scnptr; |
| 84 | return_section->rel_filepos = hdr->s_relptr; |
| 85 | return_section->reloc_count = hdr->s_nreloc; |
| 86 | |
| 87 | bfd_coff_set_alignment_hook (abfd, return_section, hdr); |
| 88 | |
| 89 | return_section->line_filepos = hdr->s_lnnoptr; |
| 90 | |
| 91 | return_section->lineno_count = hdr->s_nlnno; |
| 92 | return_section->userdata = NULL; |
| 93 | return_section->next = (asection *) NULL; |
| 94 | return_section->flags = bfd_coff_styp_to_sec_flags_hook (abfd, hdr); |
| 95 | |
| 96 | return_section->target_index = target_index; |
| 97 | |
| 98 | /* At least on i386-coff, the line number count for a shared library |
| 99 | section must be ignored. */ |
| 100 | if ((return_section->flags & SEC_SHARED_LIBRARY) != 0) |
| 101 | return_section->lineno_count = 0; |
| 102 | |
| 103 | if (hdr->s_nreloc != 0) |
| 104 | return_section->flags |= SEC_RELOC; |
| 105 | /* FIXME: should this check 'hdr->s_size > 0' */ |
| 106 | if (hdr->s_scnptr != 0) |
| 107 | return_section->flags |= SEC_HAS_CONTENTS; |
| 108 | return true; |
| 109 | } |
| 110 | |
| 111 | /* Read in a COFF object and make it into a BFD. This is used by |
| 112 | ECOFF as well. */ |
| 113 | |
| 114 | static |
| 115 | bfd_target * |
| 116 | DEFUN(coff_real_object_p,(abfd, nscns, internal_f, internal_a), |
| 117 | bfd *abfd AND |
| 118 | unsigned nscns AND |
| 119 | struct internal_filehdr *internal_f AND |
| 120 | struct internal_aouthdr *internal_a) |
| 121 | { |
| 122 | PTR tdata; |
| 123 | size_t readsize; /* length of file_info */ |
| 124 | unsigned int scnhsz; |
| 125 | char *external_sections; |
| 126 | |
| 127 | /* Build a play area */ |
| 128 | tdata = bfd_coff_mkobject_hook (abfd, (PTR) internal_f, (PTR) internal_a); |
| 129 | if (tdata == NULL) |
| 130 | return 0; |
| 131 | |
| 132 | scnhsz = bfd_coff_scnhsz (abfd); |
| 133 | readsize = nscns * scnhsz; |
| 134 | external_sections = (char *)bfd_alloc(abfd, readsize); |
| 135 | if (!external_sections) |
| 136 | { |
| 137 | bfd_error = no_memory; |
| 138 | goto fail; |
| 139 | } |
| 140 | |
| 141 | if (bfd_read((PTR)external_sections, 1, readsize, abfd) != readsize) { |
| 142 | goto fail; |
| 143 | } |
| 144 | |
| 145 | /* Now copy data as required; construct all asections etc */ |
| 146 | if (nscns != 0) { |
| 147 | unsigned int i; |
| 148 | for (i = 0; i < nscns; i++) { |
| 149 | struct internal_scnhdr tmp; |
| 150 | bfd_coff_swap_scnhdr_in(abfd, (PTR) (external_sections + i * scnhsz), |
| 151 | (PTR) &tmp); |
| 152 | make_a_section_from_file(abfd,&tmp, i+1); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | /* make_abs_section(abfd);*/ |
| 157 | |
| 158 | if (bfd_coff_set_arch_mach_hook (abfd, (PTR) internal_f) == false) |
| 159 | goto fail; |
| 160 | |
| 161 | if (!(internal_f->f_flags & F_RELFLG)) |
| 162 | abfd->flags |= HAS_RELOC; |
| 163 | if ((internal_f->f_flags & F_EXEC)) |
| 164 | abfd->flags |= EXEC_P; |
| 165 | if (!(internal_f->f_flags & F_LNNO)) |
| 166 | abfd->flags |= HAS_LINENO; |
| 167 | if (!(internal_f->f_flags & F_LSYMS)) |
| 168 | abfd->flags |= HAS_LOCALS; |
| 169 | |
| 170 | |
| 171 | bfd_get_symcount(abfd) = internal_f->f_nsyms; |
| 172 | if (internal_f->f_nsyms) |
| 173 | abfd->flags |= HAS_SYMS; |
| 174 | |
| 175 | if (internal_a != (struct internal_aouthdr *) NULL) |
| 176 | bfd_get_start_address (abfd) = internal_a->entry; |
| 177 | else |
| 178 | bfd_get_start_address (abfd) = 0; |
| 179 | |
| 180 | return abfd->xvec; |
| 181 | fail: |
| 182 | bfd_release(abfd, tdata); |
| 183 | return (bfd_target *)NULL; |
| 184 | } |
| 185 | |
| 186 | /* Turn a COFF file into a BFD, but fail with wrong_format if it is |
| 187 | not a COFF file. This is also used by ECOFF. */ |
| 188 | |
| 189 | bfd_target * |
| 190 | DEFUN(coff_object_p,(abfd), |
| 191 | bfd *abfd) |
| 192 | { |
| 193 | unsigned int filhsz; |
| 194 | unsigned int aoutsz; |
| 195 | int nscns; |
| 196 | PTR filehdr; |
| 197 | struct internal_filehdr internal_f; |
| 198 | struct internal_aouthdr internal_a; |
| 199 | |
| 200 | bfd_error = system_call_error; |
| 201 | |
| 202 | /* figure out how much to read */ |
| 203 | filhsz = bfd_coff_filhsz (abfd); |
| 204 | aoutsz = bfd_coff_aoutsz (abfd); |
| 205 | |
| 206 | filehdr = bfd_alloc (abfd, filhsz); |
| 207 | if (filehdr == NULL) |
| 208 | return 0; |
| 209 | if (bfd_read(filehdr, 1, filhsz, abfd) != filhsz) |
| 210 | return 0; |
| 211 | bfd_coff_swap_filehdr_in(abfd, filehdr, &internal_f); |
| 212 | bfd_release (abfd, filehdr); |
| 213 | |
| 214 | if (bfd_coff_bad_format_hook (abfd, &internal_f) == false) { |
| 215 | bfd_error = wrong_format; |
| 216 | return 0; |
| 217 | } |
| 218 | nscns =internal_f.f_nscns; |
| 219 | |
| 220 | if (internal_f.f_opthdr) { |
| 221 | PTR opthdr; |
| 222 | |
| 223 | opthdr = bfd_alloc (abfd, aoutsz); |
| 224 | if (opthdr == NULL) |
| 225 | return 0;; |
| 226 | if (bfd_read(opthdr, 1,aoutsz, abfd) != aoutsz) { |
| 227 | return 0; |
| 228 | } |
| 229 | bfd_coff_swap_aouthdr_in(abfd, opthdr, (PTR)&internal_a); |
| 230 | } |
| 231 | |
| 232 | /* Seek past the opt hdr stuff */ |
| 233 | bfd_seek(abfd, (file_ptr) (internal_f.f_opthdr + filhsz), SEEK_SET); |
| 234 | |
| 235 | return coff_real_object_p(abfd, nscns, &internal_f, |
| 236 | (internal_f.f_opthdr != 0 |
| 237 | ? &internal_a |
| 238 | : (struct internal_aouthdr *) NULL)); |
| 239 | } |
| 240 | |
| 241 | /* Get the BFD section from a COFF symbol section number. */ |
| 242 | |
| 243 | struct sec * |
| 244 | DEFUN(coff_section_from_bfd_index,(abfd, index), |
| 245 | bfd *abfd AND |
| 246 | int index) |
| 247 | { |
| 248 | struct sec *answer = abfd->sections; |
| 249 | |
| 250 | if (index == N_ABS) |
| 251 | { |
| 252 | return &bfd_abs_section; |
| 253 | } |
| 254 | if (index == N_UNDEF) |
| 255 | { |
| 256 | return &bfd_und_section; |
| 257 | } |
| 258 | if(index == N_DEBUG) |
| 259 | { |
| 260 | return &bfd_debug_section; |
| 261 | |
| 262 | } |
| 263 | |
| 264 | while (answer) { |
| 265 | if (answer->target_index == index) |
| 266 | return answer; |
| 267 | answer = answer->next; |
| 268 | } |
| 269 | BFD_ASSERT(0); |
| 270 | return &bfd_und_section; /* For gcc -W and lint. Never executed. */ |
| 271 | } |
| 272 | |
| 273 | /* Get the upper bound of a COFF symbol table. */ |
| 274 | |
| 275 | unsigned int |
| 276 | coff_get_symtab_upper_bound(abfd) |
| 277 | bfd *abfd; |
| 278 | { |
| 279 | if (!bfd_coff_slurp_symbol_table(abfd)) |
| 280 | return 0; |
| 281 | |
| 282 | return (bfd_get_symcount(abfd) + 1) * (sizeof(coff_symbol_type *)); |
| 283 | } |
| 284 | |
| 285 | |
| 286 | /* Canonicalize a COFF symbol table. */ |
| 287 | |
| 288 | unsigned int |
| 289 | DEFUN(coff_get_symtab, (abfd, alocation), |
| 290 | bfd *abfd AND |
| 291 | asymbol **alocation) |
| 292 | { |
| 293 | unsigned int counter = 0; |
| 294 | coff_symbol_type *symbase; |
| 295 | coff_symbol_type **location = (coff_symbol_type **) (alocation); |
| 296 | if (!bfd_coff_slurp_symbol_table(abfd)) |
| 297 | return 0; |
| 298 | |
| 299 | symbase = obj_symbols(abfd); |
| 300 | while (counter < bfd_get_symcount(abfd)) |
| 301 | { |
| 302 | /* This nasty code looks at the symbol to decide whether or |
| 303 | not it is descibes a constructor/destructor entry point. It |
| 304 | is structured this way to (hopefully) speed non matches */ |
| 305 | #if 0 |
| 306 | if (0 && symbase->symbol.name[9] == '$') |
| 307 | { |
| 308 | bfd_constructor_entry(abfd, |
| 309 | (asymbol **)location, |
| 310 | symbase->symbol.name[10] == 'I' ? |
| 311 | "CTOR" : "DTOR"); |
| 312 | } |
| 313 | #endif |
| 314 | *(location++) = symbase++; |
| 315 | counter++; |
| 316 | } |
| 317 | *location++ = 0; |
| 318 | return bfd_get_symcount(abfd); |
| 319 | } |
| 320 | |
| 321 | /* Set lineno_count for the output sections of a COFF file. */ |
| 322 | |
| 323 | int |
| 324 | DEFUN(coff_count_linenumbers,(abfd), |
| 325 | bfd *abfd) |
| 326 | { |
| 327 | unsigned int limit = bfd_get_symcount(abfd); |
| 328 | unsigned int i; |
| 329 | int total = 0; |
| 330 | asymbol **p; |
| 331 | { |
| 332 | asection *s = abfd->sections->output_section; |
| 333 | while (s) { |
| 334 | BFD_ASSERT(s->lineno_count == 0); |
| 335 | s = s->next; |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | |
| 340 | for (p = abfd->outsymbols, i = 0; i < limit; i++, p++) { |
| 341 | asymbol *q_maybe = *p; |
| 342 | if (bfd_asymbol_flavour(q_maybe) == bfd_target_coff_flavour) { |
| 343 | coff_symbol_type *q = coffsymbol(q_maybe); |
| 344 | if (q->lineno) { |
| 345 | /* |
| 346 | This symbol has a linenumber, increment the owning |
| 347 | section's linenumber count |
| 348 | */ |
| 349 | alent *l = q->lineno; |
| 350 | q->symbol.section->output_section->lineno_count++; |
| 351 | total ++; |
| 352 | l++; |
| 353 | while (l->line_number) { |
| 354 | total ++; |
| 355 | q->symbol.section->output_section->lineno_count++; |
| 356 | l++; |
| 357 | } |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | return total; |
| 362 | } |
| 363 | |
| 364 | /* Takes a bfd and a symbol, returns a pointer to the coff specific |
| 365 | area of the symbol if there is one. */ |
| 366 | |
| 367 | /*ARGSUSED*/ |
| 368 | coff_symbol_type * |
| 369 | DEFUN(coff_symbol_from,(ignore_abfd, symbol), |
| 370 | bfd *ignore_abfd AND |
| 371 | asymbol *symbol) |
| 372 | { |
| 373 | if (bfd_asymbol_flavour(symbol) != bfd_target_coff_flavour) |
| 374 | return (coff_symbol_type *)NULL; |
| 375 | |
| 376 | if (bfd_asymbol_bfd(symbol)->tdata.coff_obj_data == (coff_data_type*)NULL) |
| 377 | return (coff_symbol_type *)NULL; |
| 378 | |
| 379 | return (coff_symbol_type *) symbol; |
| 380 | } |
| 381 | |
| 382 | static void |
| 383 | DEFUN(fixup_symbol_value,(coff_symbol_ptr, syment), |
| 384 | coff_symbol_type *coff_symbol_ptr AND |
| 385 | struct internal_syment *syment) |
| 386 | { |
| 387 | |
| 388 | /* Normalize the symbol flags */ |
| 389 | if (bfd_is_com_section (coff_symbol_ptr->symbol.section)) { |
| 390 | /* a common symbol is undefined with a value */ |
| 391 | syment->n_scnum = N_UNDEF; |
| 392 | syment->n_value = coff_symbol_ptr->symbol.value; |
| 393 | } |
| 394 | else if (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING) { |
| 395 | syment->n_value = coff_symbol_ptr->symbol.value; |
| 396 | } |
| 397 | else if (coff_symbol_ptr->symbol.section == & bfd_und_section) { |
| 398 | syment->n_scnum = N_UNDEF; |
| 399 | syment->n_value = 0; |
| 400 | } |
| 401 | else { |
| 402 | if (coff_symbol_ptr->symbol.section) { |
| 403 | syment->n_scnum = |
| 404 | coff_symbol_ptr->symbol.section->output_section->target_index; |
| 405 | |
| 406 | syment->n_value = |
| 407 | coff_symbol_ptr->symbol.value + |
| 408 | coff_symbol_ptr->symbol.section->output_offset + |
| 409 | coff_symbol_ptr->symbol.section->output_section->vma; |
| 410 | } |
| 411 | else { |
| 412 | BFD_ASSERT(0); |
| 413 | /* This can happen, but I don't know why yet (steve@cygnus.com) */ |
| 414 | syment->n_scnum = N_ABS; |
| 415 | syment->n_value = coff_symbol_ptr->symbol.value; |
| 416 | } |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | /* run through all the symbols in the symbol table and work out what |
| 421 | their indexes into the symbol table will be when output |
| 422 | |
| 423 | Coff requires that each C_FILE symbol points to the next one in the |
| 424 | chain, and that the last one points to the first external symbol. We |
| 425 | do that here too. |
| 426 | |
| 427 | */ |
| 428 | boolean |
| 429 | DEFUN(coff_renumber_symbols,(bfd_ptr), |
| 430 | bfd *bfd_ptr) |
| 431 | { |
| 432 | unsigned int symbol_count = bfd_get_symcount(bfd_ptr); |
| 433 | asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols; |
| 434 | unsigned int native_index = 0; |
| 435 | struct internal_syment *last_file = (struct internal_syment *)NULL; |
| 436 | unsigned int symbol_index; |
| 437 | |
| 438 | /* COFF demands that undefined symbols come after all other symbols. |
| 439 | Since we don't need to impose this extra knowledge on all our client |
| 440 | programs, deal with that here. Sort the symbol table; just move the |
| 441 | undefined symbols to the end, leaving the rest alone. */ |
| 442 | /* @@ Do we have some condition we could test for, so we don't always |
| 443 | have to do this? I don't think relocatability is quite right, but |
| 444 | I'm not certain. [raeburn:19920508.1711EST] */ |
| 445 | { |
| 446 | asymbol **newsyms; |
| 447 | int i; |
| 448 | |
| 449 | newsyms = (asymbol **) bfd_alloc_by_size_t (bfd_ptr, |
| 450 | sizeof (asymbol *) |
| 451 | * (symbol_count + 1)); |
| 452 | if (!newsyms) |
| 453 | { |
| 454 | bfd_error = no_memory; |
| 455 | return false; |
| 456 | } |
| 457 | bfd_ptr->outsymbols = newsyms; |
| 458 | for (i = 0; i < symbol_count; i++) |
| 459 | if (symbol_ptr_ptr[i]->section != &bfd_und_section) |
| 460 | *newsyms++ = symbol_ptr_ptr[i]; |
| 461 | for (i = 0; i < symbol_count; i++) |
| 462 | if (symbol_ptr_ptr[i]->section == &bfd_und_section) |
| 463 | *newsyms++ = symbol_ptr_ptr[i]; |
| 464 | *newsyms = (asymbol *) NULL; |
| 465 | symbol_ptr_ptr = bfd_ptr->outsymbols; |
| 466 | } |
| 467 | |
| 468 | for (symbol_index = 0; symbol_index < symbol_count; symbol_index++) |
| 469 | { |
| 470 | coff_symbol_type *coff_symbol_ptr = coff_symbol_from(bfd_ptr, symbol_ptr_ptr[symbol_index]); |
| 471 | if (coff_symbol_ptr && coff_symbol_ptr->native) { |
| 472 | combined_entry_type *s = coff_symbol_ptr->native; |
| 473 | int i; |
| 474 | |
| 475 | if (s->u.syment.n_sclass == C_FILE) |
| 476 | { |
| 477 | if (last_file != (struct internal_syment *)NULL) { |
| 478 | last_file->n_value = native_index; |
| 479 | } |
| 480 | last_file = &(s->u.syment); |
| 481 | } |
| 482 | else { |
| 483 | |
| 484 | /* Modify the symbol values according to their section and |
| 485 | type */ |
| 486 | |
| 487 | fixup_symbol_value(coff_symbol_ptr, &(s->u.syment)); |
| 488 | } |
| 489 | for (i = 0; i < s->u.syment.n_numaux + 1; i++) { |
| 490 | s[i].offset = native_index ++; |
| 491 | } |
| 492 | } |
| 493 | else { |
| 494 | native_index++; |
| 495 | } |
| 496 | } |
| 497 | obj_conv_table_size (bfd_ptr) = native_index; |
| 498 | return true; |
| 499 | } |
| 500 | |
| 501 | /* |
| 502 | Run thorough the symbol table again, and fix it so that all pointers to |
| 503 | entries are changed to the entries' index in the output symbol table. |
| 504 | |
| 505 | */ |
| 506 | void |
| 507 | coff_mangle_symbols (bfd_ptr) |
| 508 | bfd *bfd_ptr; |
| 509 | { |
| 510 | unsigned int symbol_count = bfd_get_symcount (bfd_ptr); |
| 511 | asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols; |
| 512 | unsigned int symbol_index; |
| 513 | |
| 514 | for (symbol_index = 0; symbol_index < symbol_count; symbol_index++) |
| 515 | { |
| 516 | coff_symbol_type *coff_symbol_ptr = |
| 517 | coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]); |
| 518 | |
| 519 | if (coff_symbol_ptr && coff_symbol_ptr->native) |
| 520 | { |
| 521 | int i; |
| 522 | combined_entry_type *s = coff_symbol_ptr->native; |
| 523 | |
| 524 | if (s->fix_value) |
| 525 | { |
| 526 | /* FIXME: We should use a union here. */ |
| 527 | s->u.syment.n_value = |
| 528 | ((combined_entry_type *) s->u.syment.n_value)->offset; |
| 529 | s->fix_value = 0; |
| 530 | } |
| 531 | for (i = 0; i < s->u.syment.n_numaux ; i++) |
| 532 | { |
| 533 | combined_entry_type *a = s + i + 1; |
| 534 | if (a->fix_tag) |
| 535 | { |
| 536 | a->u.auxent.x_sym.x_tagndx.l = |
| 537 | a->u.auxent.x_sym.x_tagndx.p->offset; |
| 538 | a->fix_tag = 0; |
| 539 | } |
| 540 | if (a->fix_end) |
| 541 | { |
| 542 | a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l = |
| 543 | a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p->offset; |
| 544 | a->fix_end = 0; |
| 545 | } |
| 546 | if (a->fix_scnlen) |
| 547 | { |
| 548 | a->u.auxent.x_csect.x_scnlen.l = |
| 549 | a->u.auxent.x_csect.x_scnlen.p->offset; |
| 550 | a->fix_scnlen = 0; |
| 551 | } |
| 552 | } |
| 553 | } |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | static bfd_size_type string_size; |
| 558 | static bfd_size_type debug_string_size; |
| 559 | static asection *debug_string_section; |
| 560 | |
| 561 | static void |
| 562 | DEFUN(coff_fix_symbol_name,(abfd, symbol, native), |
| 563 | bfd *abfd AND |
| 564 | asymbol *symbol AND |
| 565 | combined_entry_type *native) |
| 566 | { |
| 567 | unsigned int name_length; |
| 568 | union internal_auxent *auxent; |
| 569 | char * name = ( char *)(symbol->name); |
| 570 | |
| 571 | if (name == (char *) NULL) { |
| 572 | /* coff symbols always have names, so we'll make one up */ |
| 573 | symbol->name = "strange"; |
| 574 | name = (char *)symbol->name; |
| 575 | } |
| 576 | name_length = strlen(name); |
| 577 | |
| 578 | if (native->u.syment.n_sclass == C_FILE) { |
| 579 | strncpy(native->u.syment._n._n_name, ".file", SYMNMLEN); |
| 580 | auxent = &(native+1)->u.auxent; |
| 581 | |
| 582 | if (bfd_coff_long_filenames (abfd)) { |
| 583 | if (name_length <= FILNMLEN) { |
| 584 | strncpy(auxent->x_file.x_fname, name, FILNMLEN); |
| 585 | } |
| 586 | else { |
| 587 | auxent->x_file.x_n.x_offset = string_size + 4; |
| 588 | auxent->x_file.x_n.x_zeroes = 0; |
| 589 | string_size += name_length + 1; |
| 590 | } |
| 591 | } |
| 592 | else { |
| 593 | strncpy(auxent->x_file.x_fname, name, FILNMLEN); |
| 594 | if (name_length > FILNMLEN) { |
| 595 | name[FILNMLEN] = '\0'; |
| 596 | } |
| 597 | } |
| 598 | } |
| 599 | else |
| 600 | { /* NOT A C_FILE SYMBOL */ |
| 601 | if (name_length <= SYMNMLEN) |
| 602 | { |
| 603 | /* This name will fit into the symbol neatly */ |
| 604 | strncpy(native->u.syment._n._n_name, symbol->name, SYMNMLEN); |
| 605 | } |
| 606 | else if (! bfd_coff_symname_in_debug (abfd, &native->u.syment)) |
| 607 | { |
| 608 | native->u.syment._n._n_n._n_offset = string_size + 4; |
| 609 | native->u.syment._n._n_n._n_zeroes = 0; |
| 610 | string_size += name_length + 1; |
| 611 | } |
| 612 | else |
| 613 | { |
| 614 | long filepos; |
| 615 | bfd_byte buf[2]; |
| 616 | |
| 617 | /* This name should be written into the .debug section. For |
| 618 | some reason each name is preceded by a two byte length |
| 619 | and also followed by a null byte. FIXME: We assume that |
| 620 | the .debug section has already been created, and that it |
| 621 | is large enough. */ |
| 622 | if (debug_string_section == (asection *) NULL) |
| 623 | debug_string_section = bfd_get_section_by_name (abfd, ".debug"); |
| 624 | filepos = bfd_tell (abfd); |
| 625 | bfd_put_16 (abfd, name_length + 1, buf); |
| 626 | if (! bfd_set_section_contents (abfd, |
| 627 | debug_string_section, |
| 628 | (PTR) buf, |
| 629 | (file_ptr) debug_string_size, |
| 630 | (bfd_size_type) 2) |
| 631 | || ! bfd_set_section_contents (abfd, |
| 632 | debug_string_section, |
| 633 | (PTR) symbol->name, |
| 634 | (file_ptr) debug_string_size + 2, |
| 635 | (bfd_size_type) name_length + 1)) |
| 636 | abort (); |
| 637 | if (bfd_seek (abfd, filepos, SEEK_SET) != 0) |
| 638 | abort (); |
| 639 | native->u.syment._n._n_n._n_offset = debug_string_size + 2; |
| 640 | native->u.syment._n._n_n._n_zeroes = 0; |
| 641 | debug_string_size += name_length + 3; |
| 642 | } |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | #define set_index(symbol, idx) ((symbol)->udata =(PTR) (idx)) |
| 647 | |
| 648 | static unsigned int |
| 649 | DEFUN(coff_write_symbol,(abfd, symbol, native, written), |
| 650 | bfd *abfd AND |
| 651 | asymbol *symbol AND |
| 652 | combined_entry_type *native AND |
| 653 | unsigned int written) |
| 654 | { |
| 655 | unsigned int numaux = native->u.syment.n_numaux; |
| 656 | int type = native->u.syment.n_type; |
| 657 | int class = native->u.syment.n_sclass; |
| 658 | PTR buf; |
| 659 | bfd_size_type symesz; |
| 660 | |
| 661 | /* @@ bfd_debug_section isn't accessible outside this file, but we know |
| 662 | that C_FILE symbols belong there. So move them. */ |
| 663 | if (native->u.syment.n_sclass == C_FILE) |
| 664 | symbol->section = &bfd_debug_section; |
| 665 | |
| 666 | if (symbol->section == &bfd_abs_section) |
| 667 | { |
| 668 | native->u.syment.n_scnum = N_ABS; |
| 669 | } |
| 670 | else if (symbol->section == &bfd_debug_section) |
| 671 | { |
| 672 | native->u.syment.n_scnum = N_DEBUG; |
| 673 | } |
| 674 | else if (symbol->section == &bfd_und_section) |
| 675 | { |
| 676 | native->u.syment.n_scnum = N_UNDEF; |
| 677 | } |
| 678 | else |
| 679 | { |
| 680 | native->u.syment.n_scnum = |
| 681 | symbol->section->output_section->target_index; |
| 682 | } |
| 683 | |
| 684 | |
| 685 | coff_fix_symbol_name(abfd, symbol, native); |
| 686 | |
| 687 | symesz = bfd_coff_symesz (abfd); |
| 688 | buf = bfd_alloc (abfd, symesz); |
| 689 | if (!buf) |
| 690 | { |
| 691 | bfd_error = no_memory; |
| 692 | abort(); /* FIXME */ |
| 693 | } |
| 694 | bfd_coff_swap_sym_out(abfd, &native->u.syment, buf); |
| 695 | bfd_write(buf, 1, symesz, abfd); |
| 696 | bfd_release (abfd, buf); |
| 697 | |
| 698 | if (native->u.syment.n_numaux > 0) |
| 699 | { |
| 700 | bfd_size_type auxesz; |
| 701 | unsigned int j; |
| 702 | |
| 703 | auxesz = bfd_coff_auxesz (abfd); |
| 704 | buf = bfd_alloc (abfd, auxesz); |
| 705 | if (!buf) |
| 706 | { |
| 707 | bfd_error = no_memory; |
| 708 | abort(); /* FIXME */ |
| 709 | } |
| 710 | for (j = 0; j < native->u.syment.n_numaux; j++) |
| 711 | { |
| 712 | bfd_coff_swap_aux_out(abfd, |
| 713 | &((native + j + 1)->u.auxent), |
| 714 | type, |
| 715 | class, |
| 716 | j, |
| 717 | native->u.syment.n_numaux, |
| 718 | buf); |
| 719 | bfd_write(buf, 1, auxesz, abfd); |
| 720 | } |
| 721 | bfd_release (abfd, buf); |
| 722 | } |
| 723 | /* |
| 724 | Reuse somewhere in the symbol to keep the index |
| 725 | */ |
| 726 | set_index(symbol, written); |
| 727 | return written + 1 + numaux; |
| 728 | } |
| 729 | |
| 730 | |
| 731 | static unsigned int |
| 732 | DEFUN(coff_write_alien_symbol,(abfd, symbol, written), |
| 733 | bfd *abfd AND |
| 734 | asymbol *symbol AND |
| 735 | unsigned int written) |
| 736 | { |
| 737 | /* |
| 738 | This symbol has been created by the loader, or come from a non |
| 739 | coff format. It has no native element to inherit, make our |
| 740 | own |
| 741 | */ |
| 742 | combined_entry_type *native; |
| 743 | combined_entry_type dummy; |
| 744 | native = &dummy; |
| 745 | native->u.syment.n_type = T_NULL; |
| 746 | native->u.syment.n_flags = 0; |
| 747 | if (symbol->section == &bfd_und_section) |
| 748 | { |
| 749 | native->u.syment.n_scnum = N_UNDEF; |
| 750 | native->u.syment.n_value = symbol->value; |
| 751 | } |
| 752 | else if (bfd_is_com_section (symbol->section)) |
| 753 | { |
| 754 | native->u.syment.n_scnum = N_UNDEF; |
| 755 | native->u.syment.n_value = symbol->value; |
| 756 | |
| 757 | } |
| 758 | |
| 759 | else if (symbol->flags & BSF_DEBUGGING) { |
| 760 | /* |
| 761 | remove name so it doesn't take up any space |
| 762 | */ |
| 763 | symbol->name = ""; |
| 764 | } |
| 765 | else { |
| 766 | native->u.syment.n_scnum = symbol->section->output_section->target_index; |
| 767 | native->u.syment.n_value = symbol->value + |
| 768 | symbol->section->output_section->vma + |
| 769 | symbol->section->output_offset; |
| 770 | /* Copy the any flags from the the file hdr into the symbol */ |
| 771 | { |
| 772 | coff_symbol_type *c = coff_symbol_from(abfd, symbol); |
| 773 | if (c != (coff_symbol_type *)NULL) { |
| 774 | native->u.syment.n_flags = bfd_asymbol_bfd(&c->symbol)->flags; |
| 775 | } |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | native->u.syment.n_type = 0; |
| 780 | if (symbol->flags & BSF_LOCAL) |
| 781 | native->u.syment.n_sclass = C_STAT; |
| 782 | else |
| 783 | native->u.syment.n_sclass = C_EXT; |
| 784 | native->u.syment.n_numaux = 0; |
| 785 | |
| 786 | return coff_write_symbol(abfd, symbol, native, written); |
| 787 | } |
| 788 | |
| 789 | static unsigned int |
| 790 | DEFUN(coff_write_native_symbol,(abfd, symbol, written), |
| 791 | bfd *abfd AND |
| 792 | coff_symbol_type *symbol AND |
| 793 | unsigned int written) |
| 794 | { |
| 795 | /* |
| 796 | Does this symbol have an ascociated line number - if so then |
| 797 | make it remember this symbol index. Also tag the auxent of |
| 798 | this symbol to point to the right place in the lineno table |
| 799 | */ |
| 800 | combined_entry_type *native = symbol->native; |
| 801 | |
| 802 | alent *lineno = symbol->lineno; |
| 803 | |
| 804 | if (lineno && !symbol->done_lineno) { |
| 805 | unsigned int count = 0; |
| 806 | lineno[count].u.offset = written; |
| 807 | if (native->u.syment.n_numaux) { |
| 808 | union internal_auxent *a = &((native+1)->u.auxent); |
| 809 | |
| 810 | a->x_sym.x_fcnary.x_fcn.x_lnnoptr = |
| 811 | symbol->symbol.section->output_section->moving_line_filepos; |
| 812 | } |
| 813 | /* |
| 814 | And count and relocate all other linenumbers |
| 815 | */ |
| 816 | |
| 817 | count++; |
| 818 | while (lineno[count].line_number) { |
| 819 | #if 0 |
| 820 | /* 13 april 92. sac |
| 821 | I've been told this, but still need proof: |
| 822 | > The second bug is also in `bfd/coffcode.h'. This bug causes the linker to screw |
| 823 | > up the pc-relocations for all the line numbers in COFF code. This bug isn't |
| 824 | > only specific to A29K implementations, but affects all systems using COFF |
| 825 | > format binaries. Note that in COFF object files, the line number core offsets |
| 826 | > output by the assembler are relative to the start of each procedure, not |
| 827 | > to the start of the .text section. This patch relocates the line numbers |
| 828 | > relative to the `native->u.syment.n_value' instead of the section virtual |
| 829 | > address. modular!olson@cs.arizona.edu (Jon Olson) |
| 830 | */ |
| 831 | lineno[count].u.offset += native->u.syment.n_value; |
| 832 | |
| 833 | #else |
| 834 | lineno[count].u.offset += |
| 835 | symbol->symbol.section->output_section->vma + |
| 836 | symbol->symbol.section->output_offset; |
| 837 | #endif |
| 838 | count++; |
| 839 | } |
| 840 | symbol->done_lineno = true; |
| 841 | |
| 842 | symbol->symbol.section->output_section->moving_line_filepos += |
| 843 | count * bfd_coff_linesz (abfd); |
| 844 | } |
| 845 | return coff_write_symbol(abfd, &( symbol->symbol), native,written); |
| 846 | } |
| 847 | |
| 848 | void |
| 849 | DEFUN(coff_write_symbols,(abfd), |
| 850 | bfd *abfd) |
| 851 | { |
| 852 | unsigned int i; |
| 853 | unsigned int limit = bfd_get_symcount(abfd); |
| 854 | unsigned int written = 0; |
| 855 | |
| 856 | asymbol **p; |
| 857 | |
| 858 | string_size = 0; |
| 859 | debug_string_size = 0; |
| 860 | |
| 861 | /* Seek to the right place */ |
| 862 | bfd_seek(abfd, obj_sym_filepos(abfd), SEEK_SET); |
| 863 | |
| 864 | /* Output all the symbols we have */ |
| 865 | |
| 866 | written = 0; |
| 867 | for (p = abfd->outsymbols, i = 0; i < limit; i++, p++) |
| 868 | { |
| 869 | asymbol *symbol = *p; |
| 870 | coff_symbol_type *c_symbol = coff_symbol_from(abfd, symbol); |
| 871 | |
| 872 | if (c_symbol == (coff_symbol_type *) NULL || |
| 873 | c_symbol->native == (combined_entry_type *)NULL) |
| 874 | { |
| 875 | written = coff_write_alien_symbol(abfd, symbol, written); |
| 876 | } |
| 877 | else |
| 878 | { |
| 879 | written = coff_write_native_symbol(abfd, c_symbol, written); |
| 880 | } |
| 881 | |
| 882 | } |
| 883 | |
| 884 | bfd_get_symcount(abfd) = written; |
| 885 | |
| 886 | /* Now write out strings */ |
| 887 | |
| 888 | if (string_size != 0) |
| 889 | { |
| 890 | unsigned int size = string_size + 4; |
| 891 | bfd_byte buffer[4]; |
| 892 | |
| 893 | bfd_h_put_32(abfd, size, buffer); |
| 894 | bfd_write((PTR) buffer, 1, sizeof(buffer), abfd); |
| 895 | for (p = abfd->outsymbols, i = 0; |
| 896 | i < limit; |
| 897 | i++, p++) |
| 898 | { |
| 899 | asymbol *q = *p; |
| 900 | size_t name_length = strlen(q->name); |
| 901 | int maxlen; |
| 902 | coff_symbol_type* c_symbol = coff_symbol_from(abfd, q); |
| 903 | maxlen = ((c_symbol != NULL && c_symbol->native != NULL) && |
| 904 | (c_symbol->native->u.syment.n_sclass == C_FILE)) ? |
| 905 | FILNMLEN : SYMNMLEN; |
| 906 | |
| 907 | if (name_length > maxlen |
| 908 | && ! bfd_coff_symname_in_debug (abfd, |
| 909 | &c_symbol->native->u.syment)) |
| 910 | bfd_write((PTR) (q->name), 1, name_length + 1, abfd); |
| 911 | } |
| 912 | } |
| 913 | else { |
| 914 | /* We would normally not write anything here, but we'll write |
| 915 | out 4 so that any stupid coff reader which tries to read |
| 916 | the string table even when there isn't one won't croak. */ |
| 917 | unsigned int size = 4; |
| 918 | bfd_byte buffer[4]; |
| 919 | |
| 920 | bfd_h_put_32 (abfd, size, buffer); |
| 921 | bfd_write((PTR) buffer, 1, sizeof (buffer), abfd); |
| 922 | } |
| 923 | |
| 924 | BFD_ASSERT (debug_string_size == 0 |
| 925 | || (debug_string_section != (asection *) NULL |
| 926 | && (BFD_ALIGN (debug_string_size, |
| 927 | 1 << debug_string_section->alignment_power) |
| 928 | == bfd_section_size (abfd, debug_string_section)))); |
| 929 | } |
| 930 | |
| 931 | boolean |
| 932 | DEFUN(coff_write_linenumbers,(abfd), |
| 933 | bfd *abfd) |
| 934 | { |
| 935 | asection *s; |
| 936 | bfd_size_type linesz; |
| 937 | PTR buff; |
| 938 | |
| 939 | linesz = bfd_coff_linesz (abfd); |
| 940 | buff = bfd_alloc (abfd, linesz); |
| 941 | if (!buff) |
| 942 | { |
| 943 | bfd_error = no_memory; |
| 944 | return; |
| 945 | } |
| 946 | for (s = abfd->sections; s != (asection *) NULL; s = s->next) { |
| 947 | if (s->lineno_count) { |
| 948 | asymbol **q = abfd->outsymbols; |
| 949 | bfd_seek(abfd, s->line_filepos, SEEK_SET); |
| 950 | /* Find all the linenumbers in this section */ |
| 951 | while (*q) { |
| 952 | asymbol *p = *q; |
| 953 | if (p->section->output_section == s) { |
| 954 | alent *l = |
| 955 | BFD_SEND(bfd_asymbol_bfd(p), _get_lineno, (bfd_asymbol_bfd(p), p)); |
| 956 | if (l) { |
| 957 | /* Found a linenumber entry, output */ |
| 958 | struct internal_lineno out; |
| 959 | memset( (PTR)&out, 0, sizeof(out)); |
| 960 | out.l_lnno = 0; |
| 961 | out.l_addr.l_symndx = l->u.offset; |
| 962 | bfd_coff_swap_lineno_out(abfd, &out, buff); |
| 963 | bfd_write(buff, 1, linesz, abfd); |
| 964 | l++; |
| 965 | while (l->line_number) { |
| 966 | out.l_lnno = l->line_number; |
| 967 | out.l_addr.l_symndx = l->u.offset; |
| 968 | bfd_coff_swap_lineno_out(abfd, &out, buff); |
| 969 | bfd_write(buff, 1, linesz, abfd); |
| 970 | l++; |
| 971 | } |
| 972 | } |
| 973 | } |
| 974 | q++; |
| 975 | } |
| 976 | } |
| 977 | } |
| 978 | bfd_release (abfd, buff); |
| 979 | return true; |
| 980 | } |
| 981 | |
| 982 | /*ARGSUSED*/ |
| 983 | alent * |
| 984 | DEFUN(coff_get_lineno,(ignore_abfd, symbol), |
| 985 | bfd *ignore_abfd AND |
| 986 | asymbol *symbol) |
| 987 | { |
| 988 | return coffsymbol(symbol)->lineno; |
| 989 | } |
| 990 | |
| 991 | asymbol * |
| 992 | coff_section_symbol (abfd, name) |
| 993 | bfd *abfd; |
| 994 | char *name; |
| 995 | { |
| 996 | asection *sec = bfd_make_section_old_way (abfd, name); |
| 997 | asymbol *sym; |
| 998 | combined_entry_type *csym; |
| 999 | |
| 1000 | sym = sec->symbol; |
| 1001 | csym = coff_symbol_from (abfd, sym)->native; |
| 1002 | /* Make sure back-end COFF stuff is there. */ |
| 1003 | if (csym == 0) |
| 1004 | { |
| 1005 | struct foo { |
| 1006 | coff_symbol_type sym; |
| 1007 | /* @@FIXME This shouldn't use a fixed size!! */ |
| 1008 | combined_entry_type e[10]; |
| 1009 | }; |
| 1010 | struct foo *f; |
| 1011 | f = (struct foo *) bfd_alloc_by_size_t (abfd, sizeof (*f)); |
| 1012 | if (!f) |
| 1013 | { |
| 1014 | bfd_error = no_error; |
| 1015 | return NULL; |
| 1016 | } |
| 1017 | memset ((char *) f, 0, sizeof (*f)); |
| 1018 | coff_symbol_from (abfd, sym)->native = csym = f->e; |
| 1019 | } |
| 1020 | csym[0].u.syment.n_sclass = C_STAT; |
| 1021 | csym[0].u.syment.n_numaux = 1; |
| 1022 | /* SF_SET_STATICS (sym); @@ ??? */ |
| 1023 | csym[1].u.auxent.x_scn.x_scnlen = sec->_raw_size; |
| 1024 | csym[1].u.auxent.x_scn.x_nreloc = sec->reloc_count; |
| 1025 | csym[1].u.auxent.x_scn.x_nlinno = sec->lineno_count; |
| 1026 | |
| 1027 | if (sec->output_section == NULL) |
| 1028 | { |
| 1029 | sec->output_section = sec; |
| 1030 | sec->output_offset = 0; |
| 1031 | } |
| 1032 | |
| 1033 | return sym; |
| 1034 | } |
| 1035 | |
| 1036 | /* This function transforms the offsets into the symbol table into |
| 1037 | pointers to syments. */ |
| 1038 | |
| 1039 | static void |
| 1040 | DEFUN(coff_pointerize_aux,(abfd, table_base, type, class, auxent), |
| 1041 | bfd *abfd AND |
| 1042 | combined_entry_type *table_base AND |
| 1043 | int type AND |
| 1044 | int class AND |
| 1045 | combined_entry_type *auxent) |
| 1046 | { |
| 1047 | /* Don't bother if this is a file or a section */ |
| 1048 | if (class == C_STAT && type == T_NULL) return; |
| 1049 | if (class == C_FILE) return; |
| 1050 | |
| 1051 | /* Otherwise patch up */ |
| 1052 | #define N_TMASK coff_data (abfd)->local_n_tmask |
| 1053 | #define N_BTSHFT coff_data (abfd)->local_n_btshft |
| 1054 | if (ISFCN(type) || ISTAG(class) || class == C_BLOCK) { |
| 1055 | auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p = table_base + |
| 1056 | auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l; |
| 1057 | auxent->fix_end = 1; |
| 1058 | } |
| 1059 | /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can |
| 1060 | generate one, so we must be careful to ignore it. */ |
| 1061 | if (auxent->u.auxent.x_sym.x_tagndx.l > 0) { |
| 1062 | auxent->u.auxent.x_sym.x_tagndx.p = |
| 1063 | table_base + auxent->u.auxent.x_sym.x_tagndx.l; |
| 1064 | auxent->fix_tag = 1; |
| 1065 | } |
| 1066 | } |
| 1067 | |
| 1068 | static char * |
| 1069 | DEFUN(build_string_table,(abfd), |
| 1070 | bfd *abfd) |
| 1071 | { |
| 1072 | char string_table_size_buffer[4]; |
| 1073 | unsigned int string_table_size; |
| 1074 | char *string_table; |
| 1075 | |
| 1076 | /* At this point we should be "seek"'d to the end of the |
| 1077 | symbols === the symbol table size. */ |
| 1078 | if (bfd_read((char *) string_table_size_buffer, |
| 1079 | sizeof(string_table_size_buffer), |
| 1080 | 1, abfd) != sizeof(string_table_size)) { |
| 1081 | bfd_error = system_call_error; |
| 1082 | return (NULL); |
| 1083 | } /* on error */ |
| 1084 | |
| 1085 | string_table_size = bfd_h_get_32(abfd, (bfd_byte *) string_table_size_buffer); |
| 1086 | |
| 1087 | if ((string_table = (PTR) bfd_alloc(abfd, string_table_size -= 4)) == NULL) { |
| 1088 | bfd_error = no_memory; |
| 1089 | return (NULL); |
| 1090 | } /* on mallocation error */ |
| 1091 | if (bfd_read(string_table, string_table_size, 1, abfd) != string_table_size) { |
| 1092 | bfd_error = system_call_error; |
| 1093 | return (NULL); |
| 1094 | } |
| 1095 | return string_table; |
| 1096 | } |
| 1097 | |
| 1098 | /* Allocate space for the ".debug" section, and read it. |
| 1099 | We did not read the debug section until now, because |
| 1100 | we didn't want to go to the trouble until someone needed it. */ |
| 1101 | |
| 1102 | static char * |
| 1103 | DEFUN(build_debug_section,(abfd), |
| 1104 | bfd *abfd) |
| 1105 | { |
| 1106 | char *debug_section; |
| 1107 | long position; |
| 1108 | |
| 1109 | asection *sect = bfd_get_section_by_name (abfd, ".debug"); |
| 1110 | |
| 1111 | if (!sect) { |
| 1112 | bfd_error = no_debug_section; |
| 1113 | return NULL; |
| 1114 | } |
| 1115 | |
| 1116 | debug_section = (PTR) bfd_alloc (abfd, |
| 1117 | bfd_get_section_size_before_reloc (sect)); |
| 1118 | if (debug_section == NULL) { |
| 1119 | bfd_error = no_memory; |
| 1120 | return NULL; |
| 1121 | } |
| 1122 | |
| 1123 | /* Seek to the beginning of the `.debug' section and read it. |
| 1124 | Save the current position first; it is needed by our caller. |
| 1125 | Then read debug section and reset the file pointer. */ |
| 1126 | |
| 1127 | position = bfd_tell (abfd); |
| 1128 | bfd_seek (abfd, sect->filepos, SEEK_SET); |
| 1129 | if (bfd_read (debug_section, |
| 1130 | bfd_get_section_size_before_reloc (sect), 1, abfd) |
| 1131 | != bfd_get_section_size_before_reloc(sect)) { |
| 1132 | bfd_error = system_call_error; |
| 1133 | return NULL; |
| 1134 | } |
| 1135 | bfd_seek (abfd, position, SEEK_SET); |
| 1136 | return debug_section; |
| 1137 | } |
| 1138 | |
| 1139 | |
| 1140 | /* Return a pointer to a malloc'd copy of 'name'. 'name' may not be |
| 1141 | \0-terminated, but will not exceed 'maxlen' characters. The copy *will* |
| 1142 | be \0-terminated. */ |
| 1143 | static char * |
| 1144 | DEFUN(copy_name,(abfd, name, maxlen), |
| 1145 | bfd *abfd AND |
| 1146 | char *name AND |
| 1147 | int maxlen) |
| 1148 | { |
| 1149 | int len; |
| 1150 | char *newname; |
| 1151 | |
| 1152 | for (len = 0; len < maxlen; ++len) { |
| 1153 | if (name[len] == '\0') { |
| 1154 | break; |
| 1155 | } |
| 1156 | } |
| 1157 | |
| 1158 | if ((newname = (PTR) bfd_alloc(abfd, len+1)) == NULL) { |
| 1159 | bfd_error = no_memory; |
| 1160 | return (NULL); |
| 1161 | } |
| 1162 | strncpy(newname, name, len); |
| 1163 | newname[len] = '\0'; |
| 1164 | return newname; |
| 1165 | } |
| 1166 | |
| 1167 | /* Read a symbol table into freshly bfd_allocated memory, swap it, and |
| 1168 | knit the symbol names into a normalized form. By normalized here I |
| 1169 | mean that all symbols have an n_offset pointer that points to a null- |
| 1170 | terminated string. */ |
| 1171 | |
| 1172 | combined_entry_type * |
| 1173 | DEFUN(coff_get_normalized_symtab,(abfd), |
| 1174 | bfd *abfd) |
| 1175 | { |
| 1176 | combined_entry_type *internal; |
| 1177 | combined_entry_type *internal_ptr; |
| 1178 | combined_entry_type *symbol_ptr; |
| 1179 | combined_entry_type *internal_end; |
| 1180 | bfd_size_type symesz; |
| 1181 | PTR raw; |
| 1182 | char *raw_src; |
| 1183 | char *raw_end; |
| 1184 | char *string_table = NULL; |
| 1185 | char *debug_section = NULL; |
| 1186 | unsigned long size; |
| 1187 | |
| 1188 | unsigned int raw_size; |
| 1189 | if (obj_raw_syments(abfd) != (combined_entry_type *)NULL) { |
| 1190 | return obj_raw_syments(abfd); |
| 1191 | } |
| 1192 | if ((size = bfd_get_symcount(abfd) * sizeof(combined_entry_type)) == 0) { |
| 1193 | bfd_error = no_symbols; |
| 1194 | return (NULL); |
| 1195 | } |
| 1196 | |
| 1197 | internal = (combined_entry_type *)bfd_alloc(abfd, size); |
| 1198 | if (!internal) |
| 1199 | { |
| 1200 | bfd_error = no_memory; |
| 1201 | return NULL; |
| 1202 | } |
| 1203 | internal_end = internal + bfd_get_symcount(abfd); |
| 1204 | |
| 1205 | symesz = bfd_coff_symesz (abfd); |
| 1206 | raw_size = bfd_get_symcount(abfd) * symesz; |
| 1207 | raw = bfd_alloc(abfd,raw_size); |
| 1208 | if (!raw) |
| 1209 | { |
| 1210 | bfd_error = no_memory; |
| 1211 | return NULL; |
| 1212 | } |
| 1213 | |
| 1214 | if (bfd_seek(abfd, obj_sym_filepos(abfd), SEEK_SET) == -1 |
| 1215 | || bfd_read(raw, raw_size, 1, abfd) != raw_size) { |
| 1216 | bfd_error = system_call_error; |
| 1217 | return (NULL); |
| 1218 | } |
| 1219 | /* mark the end of the symbols */ |
| 1220 | raw_end = (char *) raw + bfd_get_symcount(abfd) * symesz; |
| 1221 | /* |
| 1222 | FIXME SOMEDAY. A string table size of zero is very weird, but |
| 1223 | probably possible. If one shows up, it will probably kill us. |
| 1224 | */ |
| 1225 | |
| 1226 | /* Swap all the raw entries */ |
| 1227 | for (raw_src = (char *) raw, internal_ptr = internal; |
| 1228 | raw_src < raw_end; |
| 1229 | raw_src += symesz, internal_ptr++) { |
| 1230 | |
| 1231 | unsigned int i; |
| 1232 | bfd_coff_swap_sym_in(abfd, (PTR)raw_src, (PTR)&internal_ptr->u.syment); |
| 1233 | internal_ptr->fix_value = 0; |
| 1234 | internal_ptr->fix_tag = 0; |
| 1235 | internal_ptr->fix_end = 0; |
| 1236 | internal_ptr->fix_scnlen = 0; |
| 1237 | symbol_ptr = internal_ptr; |
| 1238 | |
| 1239 | for (i = 0; |
| 1240 | i < symbol_ptr->u.syment.n_numaux; |
| 1241 | i++) |
| 1242 | { |
| 1243 | internal_ptr++; |
| 1244 | raw_src += symesz; |
| 1245 | |
| 1246 | internal_ptr->fix_value = 0; |
| 1247 | internal_ptr->fix_tag = 0; |
| 1248 | internal_ptr->fix_end = 0; |
| 1249 | internal_ptr->fix_scnlen = 0; |
| 1250 | bfd_coff_swap_aux_in(abfd, (PTR) raw_src, |
| 1251 | symbol_ptr->u.syment.n_type, |
| 1252 | symbol_ptr->u.syment.n_sclass, |
| 1253 | i, symbol_ptr->u.syment.n_numaux, |
| 1254 | &(internal_ptr->u.auxent)); |
| 1255 | /* Remember that bal entries arn't pointerized */ |
| 1256 | if (i != 1 || symbol_ptr->u.syment.n_sclass != C_LEAFPROC) |
| 1257 | { |
| 1258 | |
| 1259 | coff_pointerize_aux(abfd, |
| 1260 | internal, |
| 1261 | symbol_ptr->u.syment.n_type, |
| 1262 | symbol_ptr->u.syment.n_sclass, |
| 1263 | internal_ptr); |
| 1264 | } |
| 1265 | |
| 1266 | } |
| 1267 | } |
| 1268 | |
| 1269 | /* Free all the raw stuff */ |
| 1270 | bfd_release(abfd, raw); |
| 1271 | |
| 1272 | for (internal_ptr = internal; internal_ptr < internal_end; |
| 1273 | internal_ptr ++) |
| 1274 | { |
| 1275 | if (internal_ptr->u.syment.n_sclass == C_FILE) { |
| 1276 | /* make a file symbol point to the name in the auxent, since |
| 1277 | the text ".file" is redundant */ |
| 1278 | if ((internal_ptr+1)->u.auxent.x_file.x_n.x_zeroes == 0) { |
| 1279 | /* the filename is a long one, point into the string table */ |
| 1280 | if (string_table == NULL) { |
| 1281 | string_table = build_string_table(abfd); |
| 1282 | } |
| 1283 | |
| 1284 | internal_ptr->u.syment._n._n_n._n_offset = |
| 1285 | (long) (string_table - 4 + |
| 1286 | (internal_ptr+1)->u.auxent.x_file.x_n.x_offset); |
| 1287 | } |
| 1288 | else { |
| 1289 | /* ordinary short filename, put into memory anyway */ |
| 1290 | internal_ptr->u.syment._n._n_n._n_offset = (long) |
| 1291 | copy_name(abfd, (internal_ptr+1)->u.auxent.x_file.x_fname, |
| 1292 | FILNMLEN); |
| 1293 | } |
| 1294 | } |
| 1295 | else { |
| 1296 | if (internal_ptr->u.syment._n._n_n._n_zeroes != 0) { |
| 1297 | /* This is a "short" name. Make it long. */ |
| 1298 | unsigned long i = 0; |
| 1299 | char *newstring = NULL; |
| 1300 | |
| 1301 | /* find the length of this string without walking into memory |
| 1302 | that isn't ours. */ |
| 1303 | for (i = 0; i < 8; ++i) { |
| 1304 | if (internal_ptr->u.syment._n._n_name[i] == '\0') { |
| 1305 | break; |
| 1306 | } /* if end of string */ |
| 1307 | } /* possible lengths of this string. */ |
| 1308 | |
| 1309 | if ((newstring = (PTR) bfd_alloc(abfd, ++i)) == NULL) { |
| 1310 | bfd_error = no_memory; |
| 1311 | return (NULL); |
| 1312 | } /* on error */ |
| 1313 | memset(newstring, 0, i); |
| 1314 | strncpy(newstring, internal_ptr->u.syment._n._n_name, i-1); |
| 1315 | internal_ptr->u.syment._n._n_n._n_offset = (long int) newstring; |
| 1316 | internal_ptr->u.syment._n._n_n._n_zeroes = 0; |
| 1317 | } |
| 1318 | else if (internal_ptr->u.syment._n._n_n._n_offset == 0) |
| 1319 | internal_ptr->u.syment._n._n_n._n_offset = (long int) ""; |
| 1320 | else if (!bfd_coff_symname_in_debug(abfd, &internal_ptr->u.syment)) { |
| 1321 | /* Long name already. Point symbol at the string in the table. */ |
| 1322 | if (string_table == NULL) { |
| 1323 | string_table = build_string_table(abfd); |
| 1324 | } |
| 1325 | internal_ptr->u.syment._n._n_n._n_offset = (long int) |
| 1326 | (string_table - 4 + internal_ptr->u.syment._n._n_n._n_offset); |
| 1327 | } |
| 1328 | else { |
| 1329 | /* Long name in debug section. Very similar. */ |
| 1330 | if (debug_section == NULL) { |
| 1331 | debug_section = build_debug_section(abfd); |
| 1332 | } |
| 1333 | internal_ptr->u.syment._n._n_n._n_offset = (long int) |
| 1334 | (debug_section + internal_ptr->u.syment._n._n_n._n_offset); |
| 1335 | } |
| 1336 | } |
| 1337 | internal_ptr += internal_ptr->u.syment.n_numaux; |
| 1338 | } |
| 1339 | |
| 1340 | obj_raw_syments(abfd) = internal; |
| 1341 | obj_raw_syment_count(abfd) = internal_ptr - internal; |
| 1342 | |
| 1343 | return (internal); |
| 1344 | } /* coff_get_normalized_symtab() */ |
| 1345 | |
| 1346 | unsigned int |
| 1347 | DEFUN (coff_get_reloc_upper_bound, (abfd, asect), |
| 1348 | bfd *abfd AND |
| 1349 | sec_ptr asect) |
| 1350 | { |
| 1351 | if (bfd_get_format(abfd) != bfd_object) { |
| 1352 | bfd_error = invalid_operation; |
| 1353 | return 0; |
| 1354 | } |
| 1355 | return (asect->reloc_count + 1) * sizeof(arelent *); |
| 1356 | } |
| 1357 | |
| 1358 | asymbol * |
| 1359 | DEFUN (coff_make_empty_symbol, (abfd), |
| 1360 | bfd *abfd) |
| 1361 | { |
| 1362 | coff_symbol_type *new = (coff_symbol_type *) bfd_alloc(abfd, sizeof(coff_symbol_type)); |
| 1363 | if (new == NULL) { |
| 1364 | bfd_error = no_memory; |
| 1365 | return (NULL); |
| 1366 | } /* on error */ |
| 1367 | memset (new, 0, sizeof *new); |
| 1368 | new->symbol.section = 0; |
| 1369 | new->native = 0; |
| 1370 | new->lineno = (alent *) NULL; |
| 1371 | new->done_lineno = false; |
| 1372 | new->symbol.the_bfd = abfd; |
| 1373 | return &new->symbol; |
| 1374 | } |
| 1375 | |
| 1376 | /* Make a debugging symbol. */ |
| 1377 | |
| 1378 | asymbol * |
| 1379 | coff_bfd_make_debug_symbol (abfd, ptr, sz) |
| 1380 | bfd *abfd; |
| 1381 | PTR ptr; |
| 1382 | unsigned long sz; |
| 1383 | { |
| 1384 | coff_symbol_type *new = (coff_symbol_type *) bfd_alloc(abfd, sizeof(coff_symbol_type)); |
| 1385 | if (new == NULL) { |
| 1386 | bfd_error = no_memory; |
| 1387 | return (NULL); |
| 1388 | } /* on error */ |
| 1389 | /* @@ This shouldn't be using a constant multiplier. */ |
| 1390 | new->native = (combined_entry_type *) bfd_zalloc (abfd, sizeof (combined_entry_type) * 10); |
| 1391 | if (!new->native) |
| 1392 | { |
| 1393 | bfd_error = no_memory; |
| 1394 | return (NULL); |
| 1395 | } /* on error */ |
| 1396 | new->symbol.section = &bfd_debug_section; |
| 1397 | new->lineno = (alent *) NULL; |
| 1398 | new->done_lineno = false; |
| 1399 | new->symbol.the_bfd = abfd; |
| 1400 | return &new->symbol; |
| 1401 | } |
| 1402 | |
| 1403 | /*ARGSUSED*/ |
| 1404 | void |
| 1405 | coff_get_symbol_info (abfd, symbol, ret) |
| 1406 | bfd *abfd; |
| 1407 | asymbol *symbol; |
| 1408 | symbol_info *ret; |
| 1409 | { |
| 1410 | bfd_symbol_info (symbol, ret); |
| 1411 | } |
| 1412 | |
| 1413 | /* Print out information about COFF symbol. */ |
| 1414 | |
| 1415 | void |
| 1416 | coff_print_symbol (abfd, filep, symbol, how) |
| 1417 | bfd *abfd; |
| 1418 | PTR filep; |
| 1419 | asymbol *symbol; |
| 1420 | bfd_print_symbol_type how; |
| 1421 | { |
| 1422 | FILE *file = (FILE *) filep; |
| 1423 | |
| 1424 | switch (how) |
| 1425 | { |
| 1426 | case bfd_print_symbol_name: |
| 1427 | fprintf (file, "%s", symbol->name); |
| 1428 | break; |
| 1429 | |
| 1430 | case bfd_print_symbol_more: |
| 1431 | fprintf (file, "coff %s %s", |
| 1432 | coffsymbol(symbol)->native ? "n" : "g", |
| 1433 | coffsymbol(symbol)->lineno ? "l" : " "); |
| 1434 | break; |
| 1435 | |
| 1436 | case bfd_print_symbol_all: |
| 1437 | if (coffsymbol(symbol)->native) |
| 1438 | { |
| 1439 | unsigned int aux; |
| 1440 | combined_entry_type *combined = coffsymbol (symbol)->native; |
| 1441 | combined_entry_type *root = obj_raw_syments (abfd); |
| 1442 | struct lineno_cache_entry *l = coffsymbol(symbol)->lineno; |
| 1443 | |
| 1444 | fprintf (file,"[%3d]", combined - root); |
| 1445 | |
| 1446 | fprintf (file, |
| 1447 | "(sc %2d)(fl 0x%02x)(ty %3x)(sc %3d) (nx %d) 0x%08lx %s", |
| 1448 | combined->u.syment.n_scnum, |
| 1449 | combined->u.syment.n_flags, |
| 1450 | combined->u.syment.n_type, |
| 1451 | combined->u.syment.n_sclass, |
| 1452 | combined->u.syment.n_numaux, |
| 1453 | (unsigned long) combined->u.syment.n_value, |
| 1454 | symbol->name); |
| 1455 | |
| 1456 | for (aux = 0; aux < combined->u.syment.n_numaux; aux++) |
| 1457 | { |
| 1458 | combined_entry_type *auxp = combined + aux + 1; |
| 1459 | long tagndx; |
| 1460 | |
| 1461 | if (auxp->fix_tag) |
| 1462 | tagndx = auxp->u.auxent.x_sym.x_tagndx.p - root; |
| 1463 | else |
| 1464 | tagndx = auxp->u.auxent.x_sym.x_tagndx.l; |
| 1465 | |
| 1466 | fprintf (file, "\n"); |
| 1467 | switch (combined->u.syment.n_sclass) |
| 1468 | { |
| 1469 | case C_FILE: |
| 1470 | fprintf (file, "File "); |
| 1471 | break; |
| 1472 | default: |
| 1473 | |
| 1474 | fprintf (file, "AUX lnno %d size 0x%x tagndx %ld", |
| 1475 | auxp->u.auxent.x_sym.x_misc.x_lnsz.x_lnno, |
| 1476 | auxp->u.auxent.x_sym.x_misc.x_lnsz.x_size, |
| 1477 | tagndx); |
| 1478 | break; |
| 1479 | } |
| 1480 | } |
| 1481 | |
| 1482 | if (l) |
| 1483 | { |
| 1484 | fprintf (file, "\n%s :", l->u.sym->name); |
| 1485 | l++; |
| 1486 | while (l->line_number) |
| 1487 | { |
| 1488 | fprintf (file, "\n%4d : 0x%lx", |
| 1489 | l->line_number, |
| 1490 | ((unsigned long) |
| 1491 | (l->u.offset + symbol->section->vma))); |
| 1492 | l++; |
| 1493 | } |
| 1494 | } |
| 1495 | } |
| 1496 | else |
| 1497 | { |
| 1498 | bfd_print_symbol_vandf ((PTR) file, symbol); |
| 1499 | fprintf (file, " %-5s %s %s %s", |
| 1500 | symbol->section->name, |
| 1501 | coffsymbol(symbol)->native ? "n" : "g", |
| 1502 | coffsymbol(symbol)->lineno ? "l" : " ", |
| 1503 | symbol->name); |
| 1504 | } |
| 1505 | } |
| 1506 | } |
| 1507 | |
| 1508 | /* Provided a BFD, a section and an offset into the section, calculate |
| 1509 | and return the name of the source file and the line nearest to the |
| 1510 | wanted location. */ |
| 1511 | |
| 1512 | /*ARGSUSED*/ |
| 1513 | boolean |
| 1514 | DEFUN(coff_find_nearest_line,(abfd, |
| 1515 | section, |
| 1516 | ignore_symbols, |
| 1517 | offset, |
| 1518 | filename_ptr, |
| 1519 | functionname_ptr, |
| 1520 | line_ptr), |
| 1521 | bfd *abfd AND |
| 1522 | asection *section AND |
| 1523 | asymbol **ignore_symbols AND |
| 1524 | bfd_vma offset AND |
| 1525 | CONST char **filename_ptr AND |
| 1526 | CONST char **functionname_ptr AND |
| 1527 | unsigned int *line_ptr) |
| 1528 | { |
| 1529 | static bfd *cache_abfd; |
| 1530 | static asection *cache_section; |
| 1531 | static bfd_vma cache_offset; |
| 1532 | static unsigned int cache_i; |
| 1533 | static CONST char *cache_function; |
| 1534 | static unsigned int line_base = 0; |
| 1535 | |
| 1536 | unsigned int i = 0; |
| 1537 | coff_data_type *cof = coff_data(abfd); |
| 1538 | /* Run through the raw syments if available */ |
| 1539 | combined_entry_type *p; |
| 1540 | alent *l; |
| 1541 | |
| 1542 | |
| 1543 | *filename_ptr = 0; |
| 1544 | *functionname_ptr = 0; |
| 1545 | *line_ptr = 0; |
| 1546 | |
| 1547 | /* Don't try and find line numbers in a non coff file */ |
| 1548 | if (abfd->xvec->flavour != bfd_target_coff_flavour) |
| 1549 | return false; |
| 1550 | |
| 1551 | if (cof == NULL) |
| 1552 | return false; |
| 1553 | |
| 1554 | p = cof->raw_syments; |
| 1555 | |
| 1556 | for (i = 0; i < cof->raw_syment_count; i++) { |
| 1557 | if (p->u.syment.n_sclass == C_FILE) { |
| 1558 | /* File name has been moved into symbol */ |
| 1559 | *filename_ptr = (char *) p->u.syment._n._n_n._n_offset; |
| 1560 | break; |
| 1561 | } |
| 1562 | p += 1 + p->u.syment.n_numaux; |
| 1563 | } |
| 1564 | /* Now wander though the raw linenumbers of the section */ |
| 1565 | /* |
| 1566 | If this is the same BFD as we were previously called with and this is |
| 1567 | the same section, and the offset we want is further down then we can |
| 1568 | prime the lookup loop |
| 1569 | */ |
| 1570 | if (abfd == cache_abfd && |
| 1571 | section == cache_section && |
| 1572 | offset >= cache_offset) { |
| 1573 | i = cache_i; |
| 1574 | *functionname_ptr = cache_function; |
| 1575 | } |
| 1576 | else { |
| 1577 | i = 0; |
| 1578 | } |
| 1579 | l = §ion->lineno[i]; |
| 1580 | |
| 1581 | for (; i < section->lineno_count; i++) { |
| 1582 | if (l->line_number == 0) { |
| 1583 | /* Get the symbol this line number points at */ |
| 1584 | coff_symbol_type *coff = (coff_symbol_type *) (l->u.sym); |
| 1585 | if (coff->symbol.value > offset) |
| 1586 | break; |
| 1587 | *functionname_ptr = coff->symbol.name; |
| 1588 | if (coff->native) { |
| 1589 | combined_entry_type *s = coff->native; |
| 1590 | s = s + 1 + s->u.syment.n_numaux; |
| 1591 | /* |
| 1592 | S should now point to the .bf of the function |
| 1593 | */ |
| 1594 | if (s->u.syment.n_numaux) { |
| 1595 | /* |
| 1596 | The linenumber is stored in the auxent |
| 1597 | */ |
| 1598 | union internal_auxent *a = &((s + 1)->u.auxent); |
| 1599 | line_base = a->x_sym.x_misc.x_lnsz.x_lnno; |
| 1600 | *line_ptr = line_base; |
| 1601 | } |
| 1602 | } |
| 1603 | } |
| 1604 | else { |
| 1605 | if (l->u.offset > offset) |
| 1606 | break; |
| 1607 | *line_ptr = l->line_number + line_base - 1; |
| 1608 | } |
| 1609 | l++; |
| 1610 | } |
| 1611 | |
| 1612 | cache_abfd = abfd; |
| 1613 | cache_section = section; |
| 1614 | cache_offset = offset; |
| 1615 | cache_i = i; |
| 1616 | cache_function = *functionname_ptr; |
| 1617 | |
| 1618 | return true; |
| 1619 | } |
| 1620 | |
| 1621 | int |
| 1622 | DEFUN(coff_sizeof_headers,(abfd, reloc), |
| 1623 | bfd *abfd AND |
| 1624 | boolean reloc) |
| 1625 | { |
| 1626 | size_t size; |
| 1627 | |
| 1628 | if (reloc == false) { |
| 1629 | size = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd); |
| 1630 | } |
| 1631 | else { |
| 1632 | size = bfd_coff_filhsz (abfd); |
| 1633 | } |
| 1634 | |
| 1635 | size += abfd->section_count * bfd_coff_scnhsz (abfd); |
| 1636 | return size; |
| 1637 | } |