| 1 | /* Support for the generic parts of COFF, for BFD. |
| 2 | Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, |
| 3 | 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 |
| 4 | Free Software Foundation, Inc. |
| 5 | Written by Cygnus Support. |
| 6 | |
| 7 | This file is part of BFD, the Binary File Descriptor library. |
| 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, write to the Free Software |
| 21 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, |
| 22 | MA 02110-1301, USA. */ |
| 23 | |
| 24 | /* Most of this hacked by Steve Chamberlain, sac@cygnus.com. |
| 25 | Split out of coffcode.h by Ian Taylor, ian@cygnus.com. */ |
| 26 | |
| 27 | /* This file contains COFF code that is not dependent on any |
| 28 | particular COFF target. There is only one version of this file in |
| 29 | libbfd.a, so no target specific code may be put in here. Or, to |
| 30 | put it another way, |
| 31 | |
| 32 | ********** DO NOT PUT TARGET SPECIFIC CODE IN THIS FILE ********** |
| 33 | |
| 34 | If you need to add some target specific behaviour, add a new hook |
| 35 | function to bfd_coff_backend_data. |
| 36 | |
| 37 | Some of these functions are also called by the ECOFF routines. |
| 38 | Those functions may not use any COFF specific information, such as |
| 39 | coff_data (abfd). */ |
| 40 | |
| 41 | #include "sysdep.h" |
| 42 | #include "bfd.h" |
| 43 | #include "libbfd.h" |
| 44 | #include "coff/internal.h" |
| 45 | #include "libcoff.h" |
| 46 | |
| 47 | /* Take a section header read from a coff file (in HOST byte order), |
| 48 | and make a BFD "section" out of it. This is used by ECOFF. */ |
| 49 | |
| 50 | static bfd_boolean |
| 51 | make_a_section_from_file (bfd *abfd, |
| 52 | struct internal_scnhdr *hdr, |
| 53 | unsigned int target_index) |
| 54 | { |
| 55 | asection *return_section; |
| 56 | char *name; |
| 57 | bfd_boolean result = TRUE; |
| 58 | flagword flags; |
| 59 | |
| 60 | name = NULL; |
| 61 | |
| 62 | /* Handle long section names as in PE. On reading, we want to |
| 63 | accept long names if the format permits them at all, regardless |
| 64 | of the current state of the flag that dictates if we would generate |
| 65 | them in outputs; this construct checks if that is the case by |
| 66 | attempting to set the flag, without changing its state; the call |
| 67 | will fail for formats that do not support long names at all. */ |
| 68 | if (bfd_coff_set_long_section_names (abfd, bfd_coff_long_section_names (abfd)) |
| 69 | && hdr->s_name[0] == '/') |
| 70 | { |
| 71 | char buf[SCNNMLEN]; |
| 72 | long strindex; |
| 73 | char *p; |
| 74 | const char *strings; |
| 75 | |
| 76 | /* Flag that this BFD uses long names, even though the format might |
| 77 | expect them to be off by default. This won't directly affect the |
| 78 | format of any output BFD created from this one, but the information |
| 79 | can be used to decide what to do. */ |
| 80 | bfd_coff_set_long_section_names (abfd, TRUE); |
| 81 | memcpy (buf, hdr->s_name + 1, SCNNMLEN - 1); |
| 82 | buf[SCNNMLEN - 1] = '\0'; |
| 83 | strindex = strtol (buf, &p, 10); |
| 84 | if (*p == '\0' && strindex >= 0) |
| 85 | { |
| 86 | strings = _bfd_coff_read_string_table (abfd); |
| 87 | if (strings == NULL) |
| 88 | return FALSE; |
| 89 | /* FIXME: For extra safety, we should make sure that |
| 90 | strindex does not run us past the end, but right now we |
| 91 | don't know the length of the string table. */ |
| 92 | strings += strindex; |
| 93 | name = (char *) bfd_alloc (abfd, |
| 94 | (bfd_size_type) strlen (strings) + 1 + 1); |
| 95 | if (name == NULL) |
| 96 | return FALSE; |
| 97 | strcpy (name, strings); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | if (name == NULL) |
| 102 | { |
| 103 | /* Assorted wastage to null-terminate the name, thanks AT&T! */ |
| 104 | name = (char *) bfd_alloc (abfd, |
| 105 | (bfd_size_type) sizeof (hdr->s_name) + 1 + 1); |
| 106 | if (name == NULL) |
| 107 | return FALSE; |
| 108 | strncpy (name, (char *) &hdr->s_name[0], sizeof (hdr->s_name)); |
| 109 | name[sizeof (hdr->s_name)] = 0; |
| 110 | } |
| 111 | |
| 112 | return_section = bfd_make_section_anyway (abfd, name); |
| 113 | if (return_section == NULL) |
| 114 | return FALSE; |
| 115 | |
| 116 | return_section->vma = hdr->s_vaddr; |
| 117 | return_section->lma = hdr->s_paddr; |
| 118 | return_section->size = hdr->s_size; |
| 119 | return_section->filepos = hdr->s_scnptr; |
| 120 | return_section->rel_filepos = hdr->s_relptr; |
| 121 | return_section->reloc_count = hdr->s_nreloc; |
| 122 | |
| 123 | bfd_coff_set_alignment_hook (abfd, return_section, hdr); |
| 124 | |
| 125 | return_section->line_filepos = hdr->s_lnnoptr; |
| 126 | |
| 127 | return_section->lineno_count = hdr->s_nlnno; |
| 128 | return_section->userdata = NULL; |
| 129 | return_section->next = NULL; |
| 130 | return_section->target_index = target_index; |
| 131 | |
| 132 | if (! bfd_coff_styp_to_sec_flags_hook (abfd, hdr, name, return_section, |
| 133 | & flags)) |
| 134 | result = FALSE; |
| 135 | |
| 136 | return_section->flags = flags; |
| 137 | |
| 138 | /* At least on i386-coff, the line number count for a shared library |
| 139 | section must be ignored. */ |
| 140 | if ((return_section->flags & SEC_COFF_SHARED_LIBRARY) != 0) |
| 141 | return_section->lineno_count = 0; |
| 142 | |
| 143 | if (hdr->s_nreloc != 0) |
| 144 | return_section->flags |= SEC_RELOC; |
| 145 | /* FIXME: should this check 'hdr->s_size > 0'. */ |
| 146 | if (hdr->s_scnptr != 0) |
| 147 | return_section->flags |= SEC_HAS_CONTENTS; |
| 148 | |
| 149 | /* Compress/decompress DWARF debug sections with names: .debug_* and |
| 150 | .zdebug_*, after the section flags is set. */ |
| 151 | if ((flags & SEC_DEBUGGING) |
| 152 | && ((name[1] == 'd' && name[6] == '_') |
| 153 | || (name[1] == 'z' && name[7] == '_'))) |
| 154 | { |
| 155 | enum { nothing, compress, decompress } action = nothing; |
| 156 | char *new_name = NULL; |
| 157 | |
| 158 | if (bfd_is_section_compressed (abfd, return_section)) |
| 159 | { |
| 160 | /* Compressed section. Check if we should decompress. */ |
| 161 | if ((abfd->flags & BFD_DECOMPRESS)) |
| 162 | action = decompress; |
| 163 | } |
| 164 | else if (!bfd_is_section_compressed (abfd, return_section)) |
| 165 | { |
| 166 | /* Normal section. Check if we should compress. */ |
| 167 | if ((abfd->flags & BFD_COMPRESS) && return_section->size != 0) |
| 168 | action = compress; |
| 169 | } |
| 170 | |
| 171 | switch (action) |
| 172 | { |
| 173 | case nothing: |
| 174 | break; |
| 175 | case compress: |
| 176 | if (!bfd_init_section_compress_status (abfd, return_section)) |
| 177 | { |
| 178 | (*_bfd_error_handler) |
| 179 | (_("%B: unable to initialize compress status for section %s"), |
| 180 | abfd, name); |
| 181 | return FALSE; |
| 182 | } |
| 183 | if (name[1] != 'z') |
| 184 | { |
| 185 | unsigned int len = strlen (name); |
| 186 | |
| 187 | new_name = bfd_alloc (abfd, len + 2); |
| 188 | if (new_name == NULL) |
| 189 | return FALSE; |
| 190 | new_name[0] = '.'; |
| 191 | new_name[1] = 'z'; |
| 192 | memcpy (new_name + 2, name + 1, len); |
| 193 | } |
| 194 | break; |
| 195 | case decompress: |
| 196 | if (!bfd_init_section_decompress_status (abfd, return_section)) |
| 197 | { |
| 198 | (*_bfd_error_handler) |
| 199 | (_("%B: unable to initialize decompress status for section %s"), |
| 200 | abfd, name); |
| 201 | return FALSE; |
| 202 | } |
| 203 | if (name[1] == 'z') |
| 204 | { |
| 205 | unsigned int len = strlen (name); |
| 206 | |
| 207 | new_name = bfd_alloc (abfd, len); |
| 208 | if (new_name == NULL) |
| 209 | return FALSE; |
| 210 | new_name[0] = '.'; |
| 211 | memcpy (new_name + 1, name + 2, len - 1); |
| 212 | } |
| 213 | break; |
| 214 | } |
| 215 | if (new_name != NULL) |
| 216 | bfd_rename_section (abfd, return_section, new_name); |
| 217 | } |
| 218 | |
| 219 | return result; |
| 220 | } |
| 221 | |
| 222 | /* Read in a COFF object and make it into a BFD. This is used by |
| 223 | ECOFF as well. */ |
| 224 | const bfd_target * |
| 225 | coff_real_object_p (bfd *, |
| 226 | unsigned, |
| 227 | struct internal_filehdr *, |
| 228 | struct internal_aouthdr *); |
| 229 | const bfd_target * |
| 230 | coff_real_object_p (bfd *abfd, |
| 231 | unsigned nscns, |
| 232 | struct internal_filehdr *internal_f, |
| 233 | struct internal_aouthdr *internal_a) |
| 234 | { |
| 235 | flagword oflags = abfd->flags; |
| 236 | bfd_vma ostart = bfd_get_start_address (abfd); |
| 237 | void * tdata; |
| 238 | void * tdata_save; |
| 239 | bfd_size_type readsize; /* Length of file_info. */ |
| 240 | unsigned int scnhsz; |
| 241 | char *external_sections; |
| 242 | |
| 243 | if (!(internal_f->f_flags & F_RELFLG)) |
| 244 | abfd->flags |= HAS_RELOC; |
| 245 | if ((internal_f->f_flags & F_EXEC)) |
| 246 | abfd->flags |= EXEC_P; |
| 247 | if (!(internal_f->f_flags & F_LNNO)) |
| 248 | abfd->flags |= HAS_LINENO; |
| 249 | if (!(internal_f->f_flags & F_LSYMS)) |
| 250 | abfd->flags |= HAS_LOCALS; |
| 251 | |
| 252 | /* FIXME: How can we set D_PAGED correctly? */ |
| 253 | if ((internal_f->f_flags & F_EXEC) != 0) |
| 254 | abfd->flags |= D_PAGED; |
| 255 | |
| 256 | bfd_get_symcount (abfd) = internal_f->f_nsyms; |
| 257 | if (internal_f->f_nsyms) |
| 258 | abfd->flags |= HAS_SYMS; |
| 259 | |
| 260 | if (internal_a != (struct internal_aouthdr *) NULL) |
| 261 | bfd_get_start_address (abfd) = internal_a->entry; |
| 262 | else |
| 263 | bfd_get_start_address (abfd) = 0; |
| 264 | |
| 265 | /* Set up the tdata area. ECOFF uses its own routine, and overrides |
| 266 | abfd->flags. */ |
| 267 | tdata_save = abfd->tdata.any; |
| 268 | tdata = bfd_coff_mkobject_hook (abfd, (void *) internal_f, (void *) internal_a); |
| 269 | if (tdata == NULL) |
| 270 | goto fail2; |
| 271 | |
| 272 | scnhsz = bfd_coff_scnhsz (abfd); |
| 273 | readsize = (bfd_size_type) nscns * scnhsz; |
| 274 | external_sections = (char *) bfd_alloc (abfd, readsize); |
| 275 | if (!external_sections) |
| 276 | goto fail; |
| 277 | |
| 278 | if (bfd_bread ((void *) external_sections, readsize, abfd) != readsize) |
| 279 | goto fail; |
| 280 | |
| 281 | /* Set the arch/mach *before* swapping in sections; section header swapping |
| 282 | may depend on arch/mach info. */ |
| 283 | if (! bfd_coff_set_arch_mach_hook (abfd, (void *) internal_f)) |
| 284 | goto fail; |
| 285 | |
| 286 | /* Now copy data as required; construct all asections etc. */ |
| 287 | if (nscns != 0) |
| 288 | { |
| 289 | unsigned int i; |
| 290 | for (i = 0; i < nscns; i++) |
| 291 | { |
| 292 | struct internal_scnhdr tmp; |
| 293 | bfd_coff_swap_scnhdr_in (abfd, |
| 294 | (void *) (external_sections + i * scnhsz), |
| 295 | (void *) & tmp); |
| 296 | if (! make_a_section_from_file (abfd, &tmp, i + 1)) |
| 297 | goto fail; |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | return abfd->xvec; |
| 302 | |
| 303 | fail: |
| 304 | bfd_release (abfd, tdata); |
| 305 | fail2: |
| 306 | abfd->tdata.any = tdata_save; |
| 307 | abfd->flags = oflags; |
| 308 | bfd_get_start_address (abfd) = ostart; |
| 309 | return (const bfd_target *) NULL; |
| 310 | } |
| 311 | |
| 312 | /* Turn a COFF file into a BFD, but fail with bfd_error_wrong_format if it is |
| 313 | not a COFF file. This is also used by ECOFF. */ |
| 314 | |
| 315 | const bfd_target * |
| 316 | coff_object_p (bfd *abfd) |
| 317 | { |
| 318 | bfd_size_type filhsz; |
| 319 | bfd_size_type aoutsz; |
| 320 | unsigned int nscns; |
| 321 | void * filehdr; |
| 322 | struct internal_filehdr internal_f; |
| 323 | struct internal_aouthdr internal_a; |
| 324 | |
| 325 | /* Figure out how much to read. */ |
| 326 | filhsz = bfd_coff_filhsz (abfd); |
| 327 | aoutsz = bfd_coff_aoutsz (abfd); |
| 328 | |
| 329 | filehdr = bfd_alloc (abfd, filhsz); |
| 330 | if (filehdr == NULL) |
| 331 | return NULL; |
| 332 | if (bfd_bread (filehdr, filhsz, abfd) != filhsz) |
| 333 | { |
| 334 | if (bfd_get_error () != bfd_error_system_call) |
| 335 | bfd_set_error (bfd_error_wrong_format); |
| 336 | bfd_release (abfd, filehdr); |
| 337 | return NULL; |
| 338 | } |
| 339 | bfd_coff_swap_filehdr_in (abfd, filehdr, &internal_f); |
| 340 | bfd_release (abfd, filehdr); |
| 341 | |
| 342 | /* The XCOFF format has two sizes for the f_opthdr. SMALL_AOUTSZ |
| 343 | (less than aoutsz) used in object files and AOUTSZ (equal to |
| 344 | aoutsz) in executables. The bfd_coff_swap_aouthdr_in function |
| 345 | expects this header to be aoutsz bytes in length, so we use that |
| 346 | value in the call to bfd_alloc below. But we must be careful to |
| 347 | only read in f_opthdr bytes in the call to bfd_bread. We should |
| 348 | also attempt to catch corrupt or non-COFF binaries with a strange |
| 349 | value for f_opthdr. */ |
| 350 | if (! bfd_coff_bad_format_hook (abfd, &internal_f) |
| 351 | || internal_f.f_opthdr > aoutsz) |
| 352 | { |
| 353 | bfd_set_error (bfd_error_wrong_format); |
| 354 | return NULL; |
| 355 | } |
| 356 | nscns = internal_f.f_nscns; |
| 357 | |
| 358 | if (internal_f.f_opthdr) |
| 359 | { |
| 360 | void * opthdr; |
| 361 | |
| 362 | opthdr = bfd_alloc (abfd, aoutsz); |
| 363 | if (opthdr == NULL) |
| 364 | return NULL; |
| 365 | if (bfd_bread (opthdr, (bfd_size_type) internal_f.f_opthdr, abfd) |
| 366 | != internal_f.f_opthdr) |
| 367 | { |
| 368 | bfd_release (abfd, opthdr); |
| 369 | return NULL; |
| 370 | } |
| 371 | bfd_coff_swap_aouthdr_in (abfd, opthdr, (void *) &internal_a); |
| 372 | bfd_release (abfd, opthdr); |
| 373 | } |
| 374 | |
| 375 | return coff_real_object_p (abfd, nscns, &internal_f, |
| 376 | (internal_f.f_opthdr != 0 |
| 377 | ? &internal_a |
| 378 | : (struct internal_aouthdr *) NULL)); |
| 379 | } |
| 380 | |
| 381 | /* Get the BFD section from a COFF symbol section number. */ |
| 382 | |
| 383 | asection * |
| 384 | coff_section_from_bfd_index (bfd *abfd, int section_index) |
| 385 | { |
| 386 | struct bfd_section *answer = abfd->sections; |
| 387 | |
| 388 | if (section_index == N_ABS) |
| 389 | return bfd_abs_section_ptr; |
| 390 | if (section_index == N_UNDEF) |
| 391 | return bfd_und_section_ptr; |
| 392 | if (section_index == N_DEBUG) |
| 393 | return bfd_abs_section_ptr; |
| 394 | |
| 395 | while (answer) |
| 396 | { |
| 397 | if (answer->target_index == section_index) |
| 398 | return answer; |
| 399 | answer = answer->next; |
| 400 | } |
| 401 | |
| 402 | /* We should not reach this point, but the SCO 3.2v4 /lib/libc_s.a |
| 403 | has a bad symbol table in biglitpow.o. */ |
| 404 | return bfd_und_section_ptr; |
| 405 | } |
| 406 | |
| 407 | /* Get the upper bound of a COFF symbol table. */ |
| 408 | |
| 409 | long |
| 410 | coff_get_symtab_upper_bound (bfd *abfd) |
| 411 | { |
| 412 | if (!bfd_coff_slurp_symbol_table (abfd)) |
| 413 | return -1; |
| 414 | |
| 415 | return (bfd_get_symcount (abfd) + 1) * (sizeof (coff_symbol_type *)); |
| 416 | } |
| 417 | |
| 418 | /* Canonicalize a COFF symbol table. */ |
| 419 | |
| 420 | long |
| 421 | coff_canonicalize_symtab (bfd *abfd, asymbol **alocation) |
| 422 | { |
| 423 | unsigned int counter; |
| 424 | coff_symbol_type *symbase; |
| 425 | coff_symbol_type **location = (coff_symbol_type **) alocation; |
| 426 | |
| 427 | if (!bfd_coff_slurp_symbol_table (abfd)) |
| 428 | return -1; |
| 429 | |
| 430 | symbase = obj_symbols (abfd); |
| 431 | counter = bfd_get_symcount (abfd); |
| 432 | while (counter-- > 0) |
| 433 | *location++ = symbase++; |
| 434 | |
| 435 | *location = NULL; |
| 436 | |
| 437 | return bfd_get_symcount (abfd); |
| 438 | } |
| 439 | |
| 440 | /* Get the name of a symbol. The caller must pass in a buffer of size |
| 441 | >= SYMNMLEN + 1. */ |
| 442 | |
| 443 | const char * |
| 444 | _bfd_coff_internal_syment_name (bfd *abfd, |
| 445 | const struct internal_syment *sym, |
| 446 | char *buf) |
| 447 | { |
| 448 | /* FIXME: It's not clear this will work correctly if sizeof |
| 449 | (_n_zeroes) != 4. */ |
| 450 | if (sym->_n._n_n._n_zeroes != 0 |
| 451 | || sym->_n._n_n._n_offset == 0) |
| 452 | { |
| 453 | memcpy (buf, sym->_n._n_name, SYMNMLEN); |
| 454 | buf[SYMNMLEN] = '\0'; |
| 455 | return buf; |
| 456 | } |
| 457 | else |
| 458 | { |
| 459 | const char *strings; |
| 460 | |
| 461 | BFD_ASSERT (sym->_n._n_n._n_offset >= STRING_SIZE_SIZE); |
| 462 | strings = obj_coff_strings (abfd); |
| 463 | if (strings == NULL) |
| 464 | { |
| 465 | strings = _bfd_coff_read_string_table (abfd); |
| 466 | if (strings == NULL) |
| 467 | return NULL; |
| 468 | } |
| 469 | return strings + sym->_n._n_n._n_offset; |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | /* Read in and swap the relocs. This returns a buffer holding the |
| 474 | relocs for section SEC in file ABFD. If CACHE is TRUE and |
| 475 | INTERNAL_RELOCS is NULL, the relocs read in will be saved in case |
| 476 | the function is called again. If EXTERNAL_RELOCS is not NULL, it |
| 477 | is a buffer large enough to hold the unswapped relocs. If |
| 478 | INTERNAL_RELOCS is not NULL, it is a buffer large enough to hold |
| 479 | the swapped relocs. If REQUIRE_INTERNAL is TRUE, then the return |
| 480 | value must be INTERNAL_RELOCS. The function returns NULL on error. */ |
| 481 | |
| 482 | struct internal_reloc * |
| 483 | _bfd_coff_read_internal_relocs (bfd *abfd, |
| 484 | asection *sec, |
| 485 | bfd_boolean cache, |
| 486 | bfd_byte *external_relocs, |
| 487 | bfd_boolean require_internal, |
| 488 | struct internal_reloc *internal_relocs) |
| 489 | { |
| 490 | bfd_size_type relsz; |
| 491 | bfd_byte *free_external = NULL; |
| 492 | struct internal_reloc *free_internal = NULL; |
| 493 | bfd_byte *erel; |
| 494 | bfd_byte *erel_end; |
| 495 | struct internal_reloc *irel; |
| 496 | bfd_size_type amt; |
| 497 | |
| 498 | if (sec->reloc_count == 0) |
| 499 | return internal_relocs; /* Nothing to do. */ |
| 500 | |
| 501 | if (coff_section_data (abfd, sec) != NULL |
| 502 | && coff_section_data (abfd, sec)->relocs != NULL) |
| 503 | { |
| 504 | if (! require_internal) |
| 505 | return coff_section_data (abfd, sec)->relocs; |
| 506 | memcpy (internal_relocs, coff_section_data (abfd, sec)->relocs, |
| 507 | sec->reloc_count * sizeof (struct internal_reloc)); |
| 508 | return internal_relocs; |
| 509 | } |
| 510 | |
| 511 | relsz = bfd_coff_relsz (abfd); |
| 512 | |
| 513 | amt = sec->reloc_count * relsz; |
| 514 | if (external_relocs == NULL) |
| 515 | { |
| 516 | free_external = (bfd_byte *) bfd_malloc (amt); |
| 517 | if (free_external == NULL) |
| 518 | goto error_return; |
| 519 | external_relocs = free_external; |
| 520 | } |
| 521 | |
| 522 | if (bfd_seek (abfd, sec->rel_filepos, SEEK_SET) != 0 |
| 523 | || bfd_bread (external_relocs, amt, abfd) != amt) |
| 524 | goto error_return; |
| 525 | |
| 526 | if (internal_relocs == NULL) |
| 527 | { |
| 528 | amt = sec->reloc_count; |
| 529 | amt *= sizeof (struct internal_reloc); |
| 530 | free_internal = (struct internal_reloc *) bfd_malloc (amt); |
| 531 | if (free_internal == NULL) |
| 532 | goto error_return; |
| 533 | internal_relocs = free_internal; |
| 534 | } |
| 535 | |
| 536 | /* Swap in the relocs. */ |
| 537 | erel = external_relocs; |
| 538 | erel_end = erel + relsz * sec->reloc_count; |
| 539 | irel = internal_relocs; |
| 540 | for (; erel < erel_end; erel += relsz, irel++) |
| 541 | bfd_coff_swap_reloc_in (abfd, (void *) erel, (void *) irel); |
| 542 | |
| 543 | if (free_external != NULL) |
| 544 | { |
| 545 | free (free_external); |
| 546 | free_external = NULL; |
| 547 | } |
| 548 | |
| 549 | if (cache && free_internal != NULL) |
| 550 | { |
| 551 | if (coff_section_data (abfd, sec) == NULL) |
| 552 | { |
| 553 | amt = sizeof (struct coff_section_tdata); |
| 554 | sec->used_by_bfd = bfd_zalloc (abfd, amt); |
| 555 | if (sec->used_by_bfd == NULL) |
| 556 | goto error_return; |
| 557 | coff_section_data (abfd, sec)->contents = NULL; |
| 558 | } |
| 559 | coff_section_data (abfd, sec)->relocs = free_internal; |
| 560 | } |
| 561 | |
| 562 | return internal_relocs; |
| 563 | |
| 564 | error_return: |
| 565 | if (free_external != NULL) |
| 566 | free (free_external); |
| 567 | if (free_internal != NULL) |
| 568 | free (free_internal); |
| 569 | return NULL; |
| 570 | } |
| 571 | |
| 572 | /* Set lineno_count for the output sections of a COFF file. */ |
| 573 | |
| 574 | int |
| 575 | coff_count_linenumbers (bfd *abfd) |
| 576 | { |
| 577 | unsigned int limit = bfd_get_symcount (abfd); |
| 578 | unsigned int i; |
| 579 | int total = 0; |
| 580 | asymbol **p; |
| 581 | asection *s; |
| 582 | |
| 583 | if (limit == 0) |
| 584 | { |
| 585 | /* This may be from the backend linker, in which case the |
| 586 | lineno_count in the sections is correct. */ |
| 587 | for (s = abfd->sections; s != NULL; s = s->next) |
| 588 | total += s->lineno_count; |
| 589 | return total; |
| 590 | } |
| 591 | |
| 592 | for (s = abfd->sections; s != NULL; s = s->next) |
| 593 | BFD_ASSERT (s->lineno_count == 0); |
| 594 | |
| 595 | for (p = abfd->outsymbols, i = 0; i < limit; i++, p++) |
| 596 | { |
| 597 | asymbol *q_maybe = *p; |
| 598 | |
| 599 | if (bfd_family_coff (bfd_asymbol_bfd (q_maybe))) |
| 600 | { |
| 601 | coff_symbol_type *q = coffsymbol (q_maybe); |
| 602 | |
| 603 | /* The AIX 4.1 compiler can sometimes generate line numbers |
| 604 | attached to debugging symbols. We try to simply ignore |
| 605 | those here. */ |
| 606 | if (q->lineno != NULL |
| 607 | && q->symbol.section->owner != NULL) |
| 608 | { |
| 609 | /* This symbol has line numbers. Increment the owning |
| 610 | section's linenumber count. */ |
| 611 | alent *l = q->lineno; |
| 612 | |
| 613 | do |
| 614 | { |
| 615 | asection * sec = q->symbol.section->output_section; |
| 616 | |
| 617 | /* Do not try to update fields in read-only sections. */ |
| 618 | if (! bfd_is_const_section (sec)) |
| 619 | sec->lineno_count ++; |
| 620 | |
| 621 | ++total; |
| 622 | ++l; |
| 623 | } |
| 624 | while (l->line_number != 0); |
| 625 | } |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | return total; |
| 630 | } |
| 631 | |
| 632 | /* Takes a bfd and a symbol, returns a pointer to the coff specific |
| 633 | area of the symbol if there is one. */ |
| 634 | |
| 635 | coff_symbol_type * |
| 636 | coff_symbol_from (bfd *ignore_abfd ATTRIBUTE_UNUSED, |
| 637 | asymbol *symbol) |
| 638 | { |
| 639 | if (!bfd_family_coff (bfd_asymbol_bfd (symbol))) |
| 640 | return (coff_symbol_type *) NULL; |
| 641 | |
| 642 | if (bfd_asymbol_bfd (symbol)->tdata.coff_obj_data == (coff_data_type *) NULL) |
| 643 | return (coff_symbol_type *) NULL; |
| 644 | |
| 645 | return (coff_symbol_type *) symbol; |
| 646 | } |
| 647 | |
| 648 | static void |
| 649 | fixup_symbol_value (bfd *abfd, |
| 650 | coff_symbol_type *coff_symbol_ptr, |
| 651 | struct internal_syment *syment) |
| 652 | { |
| 653 | /* Normalize the symbol flags. */ |
| 654 | if (coff_symbol_ptr->symbol.section |
| 655 | && bfd_is_com_section (coff_symbol_ptr->symbol.section)) |
| 656 | { |
| 657 | /* A common symbol is undefined with a value. */ |
| 658 | syment->n_scnum = N_UNDEF; |
| 659 | syment->n_value = coff_symbol_ptr->symbol.value; |
| 660 | } |
| 661 | else if ((coff_symbol_ptr->symbol.flags & BSF_DEBUGGING) != 0 |
| 662 | && (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING_RELOC) == 0) |
| 663 | { |
| 664 | syment->n_value = coff_symbol_ptr->symbol.value; |
| 665 | } |
| 666 | else if (bfd_is_und_section (coff_symbol_ptr->symbol.section)) |
| 667 | { |
| 668 | syment->n_scnum = N_UNDEF; |
| 669 | syment->n_value = 0; |
| 670 | } |
| 671 | /* FIXME: Do we need to handle the absolute section here? */ |
| 672 | else |
| 673 | { |
| 674 | if (coff_symbol_ptr->symbol.section) |
| 675 | { |
| 676 | syment->n_scnum = |
| 677 | coff_symbol_ptr->symbol.section->output_section->target_index; |
| 678 | |
| 679 | syment->n_value = (coff_symbol_ptr->symbol.value |
| 680 | + coff_symbol_ptr->symbol.section->output_offset); |
| 681 | if (! obj_pe (abfd)) |
| 682 | { |
| 683 | syment->n_value += (syment->n_sclass == C_STATLAB) |
| 684 | ? coff_symbol_ptr->symbol.section->output_section->lma |
| 685 | : coff_symbol_ptr->symbol.section->output_section->vma; |
| 686 | } |
| 687 | } |
| 688 | else |
| 689 | { |
| 690 | BFD_ASSERT (0); |
| 691 | /* This can happen, but I don't know why yet (steve@cygnus.com) */ |
| 692 | syment->n_scnum = N_ABS; |
| 693 | syment->n_value = coff_symbol_ptr->symbol.value; |
| 694 | } |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | /* Run through all the symbols in the symbol table and work out what |
| 699 | their indexes into the symbol table will be when output. |
| 700 | |
| 701 | Coff requires that each C_FILE symbol points to the next one in the |
| 702 | chain, and that the last one points to the first external symbol. We |
| 703 | do that here too. */ |
| 704 | |
| 705 | bfd_boolean |
| 706 | coff_renumber_symbols (bfd *bfd_ptr, int *first_undef) |
| 707 | { |
| 708 | unsigned int symbol_count = bfd_get_symcount (bfd_ptr); |
| 709 | asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols; |
| 710 | unsigned int native_index = 0; |
| 711 | struct internal_syment *last_file = NULL; |
| 712 | unsigned int symbol_index; |
| 713 | |
| 714 | /* COFF demands that undefined symbols come after all other symbols. |
| 715 | Since we don't need to impose this extra knowledge on all our |
| 716 | client programs, deal with that here. Sort the symbol table; |
| 717 | just move the undefined symbols to the end, leaving the rest |
| 718 | alone. The O'Reilly book says that defined global symbols come |
| 719 | at the end before the undefined symbols, so we do that here as |
| 720 | well. */ |
| 721 | /* @@ Do we have some condition we could test for, so we don't always |
| 722 | have to do this? I don't think relocatability is quite right, but |
| 723 | I'm not certain. [raeburn:19920508.1711EST] */ |
| 724 | { |
| 725 | asymbol **newsyms; |
| 726 | unsigned int i; |
| 727 | bfd_size_type amt; |
| 728 | |
| 729 | amt = sizeof (asymbol *) * ((bfd_size_type) symbol_count + 1); |
| 730 | newsyms = (asymbol **) bfd_alloc (bfd_ptr, amt); |
| 731 | if (!newsyms) |
| 732 | return FALSE; |
| 733 | bfd_ptr->outsymbols = newsyms; |
| 734 | for (i = 0; i < symbol_count; i++) |
| 735 | if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) != 0 |
| 736 | || (!bfd_is_und_section (symbol_ptr_ptr[i]->section) |
| 737 | && !bfd_is_com_section (symbol_ptr_ptr[i]->section) |
| 738 | && ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) != 0 |
| 739 | || ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK)) |
| 740 | == 0)))) |
| 741 | *newsyms++ = symbol_ptr_ptr[i]; |
| 742 | |
| 743 | for (i = 0; i < symbol_count; i++) |
| 744 | if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0 |
| 745 | && !bfd_is_und_section (symbol_ptr_ptr[i]->section) |
| 746 | && (bfd_is_com_section (symbol_ptr_ptr[i]->section) |
| 747 | || ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) == 0 |
| 748 | && ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK)) |
| 749 | != 0)))) |
| 750 | *newsyms++ = symbol_ptr_ptr[i]; |
| 751 | |
| 752 | *first_undef = newsyms - bfd_ptr->outsymbols; |
| 753 | |
| 754 | for (i = 0; i < symbol_count; i++) |
| 755 | if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0 |
| 756 | && bfd_is_und_section (symbol_ptr_ptr[i]->section)) |
| 757 | *newsyms++ = symbol_ptr_ptr[i]; |
| 758 | *newsyms = (asymbol *) NULL; |
| 759 | symbol_ptr_ptr = bfd_ptr->outsymbols; |
| 760 | } |
| 761 | |
| 762 | for (symbol_index = 0; symbol_index < symbol_count; symbol_index++) |
| 763 | { |
| 764 | coff_symbol_type *coff_symbol_ptr = coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]); |
| 765 | symbol_ptr_ptr[symbol_index]->udata.i = symbol_index; |
| 766 | if (coff_symbol_ptr && coff_symbol_ptr->native) |
| 767 | { |
| 768 | combined_entry_type *s = coff_symbol_ptr->native; |
| 769 | int i; |
| 770 | |
| 771 | if (s->u.syment.n_sclass == C_FILE) |
| 772 | { |
| 773 | if (last_file != NULL) |
| 774 | last_file->n_value = native_index; |
| 775 | last_file = &(s->u.syment); |
| 776 | } |
| 777 | else |
| 778 | /* Modify the symbol values according to their section and |
| 779 | type. */ |
| 780 | fixup_symbol_value (bfd_ptr, coff_symbol_ptr, &(s->u.syment)); |
| 781 | |
| 782 | for (i = 0; i < s->u.syment.n_numaux + 1; i++) |
| 783 | s[i].offset = native_index++; |
| 784 | } |
| 785 | else |
| 786 | native_index++; |
| 787 | } |
| 788 | |
| 789 | obj_conv_table_size (bfd_ptr) = native_index; |
| 790 | |
| 791 | return TRUE; |
| 792 | } |
| 793 | |
| 794 | /* Run thorough the symbol table again, and fix it so that all |
| 795 | pointers to entries are changed to the entries' index in the output |
| 796 | symbol table. */ |
| 797 | |
| 798 | void |
| 799 | coff_mangle_symbols (bfd *bfd_ptr) |
| 800 | { |
| 801 | unsigned int symbol_count = bfd_get_symcount (bfd_ptr); |
| 802 | asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols; |
| 803 | unsigned int symbol_index; |
| 804 | |
| 805 | for (symbol_index = 0; symbol_index < symbol_count; symbol_index++) |
| 806 | { |
| 807 | coff_symbol_type *coff_symbol_ptr = |
| 808 | coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]); |
| 809 | |
| 810 | if (coff_symbol_ptr && coff_symbol_ptr->native) |
| 811 | { |
| 812 | int i; |
| 813 | combined_entry_type *s = coff_symbol_ptr->native; |
| 814 | |
| 815 | if (s->fix_value) |
| 816 | { |
| 817 | /* FIXME: We should use a union here. */ |
| 818 | s->u.syment.n_value = |
| 819 | (bfd_hostptr_t) ((combined_entry_type *) |
| 820 | ((bfd_hostptr_t) s->u.syment.n_value))->offset; |
| 821 | s->fix_value = 0; |
| 822 | } |
| 823 | if (s->fix_line) |
| 824 | { |
| 825 | /* The value is the offset into the line number entries |
| 826 | for the symbol's section. On output, the symbol's |
| 827 | section should be N_DEBUG. */ |
| 828 | s->u.syment.n_value = |
| 829 | (coff_symbol_ptr->symbol.section->output_section->line_filepos |
| 830 | + s->u.syment.n_value * bfd_coff_linesz (bfd_ptr)); |
| 831 | coff_symbol_ptr->symbol.section = |
| 832 | coff_section_from_bfd_index (bfd_ptr, N_DEBUG); |
| 833 | BFD_ASSERT (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING); |
| 834 | } |
| 835 | for (i = 0; i < s->u.syment.n_numaux; i++) |
| 836 | { |
| 837 | combined_entry_type *a = s + i + 1; |
| 838 | if (a->fix_tag) |
| 839 | { |
| 840 | a->u.auxent.x_sym.x_tagndx.l = |
| 841 | a->u.auxent.x_sym.x_tagndx.p->offset; |
| 842 | a->fix_tag = 0; |
| 843 | } |
| 844 | if (a->fix_end) |
| 845 | { |
| 846 | a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l = |
| 847 | a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p->offset; |
| 848 | a->fix_end = 0; |
| 849 | } |
| 850 | if (a->fix_scnlen) |
| 851 | { |
| 852 | a->u.auxent.x_csect.x_scnlen.l = |
| 853 | a->u.auxent.x_csect.x_scnlen.p->offset; |
| 854 | a->fix_scnlen = 0; |
| 855 | } |
| 856 | } |
| 857 | } |
| 858 | } |
| 859 | } |
| 860 | |
| 861 | static void |
| 862 | coff_fix_symbol_name (bfd *abfd, |
| 863 | asymbol *symbol, |
| 864 | combined_entry_type *native, |
| 865 | bfd_size_type *string_size_p, |
| 866 | asection **debug_string_section_p, |
| 867 | bfd_size_type *debug_string_size_p) |
| 868 | { |
| 869 | unsigned int name_length; |
| 870 | union internal_auxent *auxent; |
| 871 | char *name = (char *) (symbol->name); |
| 872 | |
| 873 | if (name == NULL) |
| 874 | { |
| 875 | /* COFF symbols always have names, so we'll make one up. */ |
| 876 | symbol->name = "strange"; |
| 877 | name = (char *) symbol->name; |
| 878 | } |
| 879 | name_length = strlen (name); |
| 880 | |
| 881 | if (native->u.syment.n_sclass == C_FILE |
| 882 | && native->u.syment.n_numaux > 0) |
| 883 | { |
| 884 | unsigned int filnmlen; |
| 885 | |
| 886 | if (bfd_coff_force_symnames_in_strings (abfd)) |
| 887 | { |
| 888 | native->u.syment._n._n_n._n_offset = |
| 889 | (*string_size_p + STRING_SIZE_SIZE); |
| 890 | native->u.syment._n._n_n._n_zeroes = 0; |
| 891 | *string_size_p += 6; /* strlen(".file") + 1 */ |
| 892 | } |
| 893 | else |
| 894 | strncpy (native->u.syment._n._n_name, ".file", SYMNMLEN); |
| 895 | |
| 896 | auxent = &(native + 1)->u.auxent; |
| 897 | |
| 898 | filnmlen = bfd_coff_filnmlen (abfd); |
| 899 | |
| 900 | if (bfd_coff_long_filenames (abfd)) |
| 901 | { |
| 902 | if (name_length <= filnmlen) |
| 903 | strncpy (auxent->x_file.x_fname, name, filnmlen); |
| 904 | else |
| 905 | { |
| 906 | auxent->x_file.x_n.x_offset = *string_size_p + STRING_SIZE_SIZE; |
| 907 | auxent->x_file.x_n.x_zeroes = 0; |
| 908 | *string_size_p += name_length + 1; |
| 909 | } |
| 910 | } |
| 911 | else |
| 912 | { |
| 913 | strncpy (auxent->x_file.x_fname, name, filnmlen); |
| 914 | if (name_length > filnmlen) |
| 915 | name[filnmlen] = '\0'; |
| 916 | } |
| 917 | } |
| 918 | else |
| 919 | { |
| 920 | if (name_length <= SYMNMLEN && !bfd_coff_force_symnames_in_strings (abfd)) |
| 921 | /* This name will fit into the symbol neatly. */ |
| 922 | strncpy (native->u.syment._n._n_name, symbol->name, SYMNMLEN); |
| 923 | |
| 924 | else if (!bfd_coff_symname_in_debug (abfd, &native->u.syment)) |
| 925 | { |
| 926 | native->u.syment._n._n_n._n_offset = (*string_size_p |
| 927 | + STRING_SIZE_SIZE); |
| 928 | native->u.syment._n._n_n._n_zeroes = 0; |
| 929 | *string_size_p += name_length + 1; |
| 930 | } |
| 931 | else |
| 932 | { |
| 933 | file_ptr filepos; |
| 934 | bfd_byte buf[4]; |
| 935 | int prefix_len = bfd_coff_debug_string_prefix_length (abfd); |
| 936 | |
| 937 | /* This name should be written into the .debug section. For |
| 938 | some reason each name is preceded by a two byte length |
| 939 | and also followed by a null byte. FIXME: We assume that |
| 940 | the .debug section has already been created, and that it |
| 941 | is large enough. */ |
| 942 | if (*debug_string_section_p == (asection *) NULL) |
| 943 | *debug_string_section_p = bfd_get_section_by_name (abfd, ".debug"); |
| 944 | filepos = bfd_tell (abfd); |
| 945 | if (prefix_len == 4) |
| 946 | bfd_put_32 (abfd, (bfd_vma) (name_length + 1), buf); |
| 947 | else |
| 948 | bfd_put_16 (abfd, (bfd_vma) (name_length + 1), buf); |
| 949 | |
| 950 | if (!bfd_set_section_contents (abfd, |
| 951 | *debug_string_section_p, |
| 952 | (void *) buf, |
| 953 | (file_ptr) *debug_string_size_p, |
| 954 | (bfd_size_type) prefix_len) |
| 955 | || !bfd_set_section_contents (abfd, |
| 956 | *debug_string_section_p, |
| 957 | (void *) symbol->name, |
| 958 | (file_ptr) (*debug_string_size_p |
| 959 | + prefix_len), |
| 960 | (bfd_size_type) name_length + 1)) |
| 961 | abort (); |
| 962 | if (bfd_seek (abfd, filepos, SEEK_SET) != 0) |
| 963 | abort (); |
| 964 | native->u.syment._n._n_n._n_offset = |
| 965 | *debug_string_size_p + prefix_len; |
| 966 | native->u.syment._n._n_n._n_zeroes = 0; |
| 967 | *debug_string_size_p += name_length + 1 + prefix_len; |
| 968 | } |
| 969 | } |
| 970 | } |
| 971 | |
| 972 | /* We need to keep track of the symbol index so that when we write out |
| 973 | the relocs we can get the index for a symbol. This method is a |
| 974 | hack. FIXME. */ |
| 975 | |
| 976 | #define set_index(symbol, idx) ((symbol)->udata.i = (idx)) |
| 977 | |
| 978 | /* Write a symbol out to a COFF file. */ |
| 979 | |
| 980 | static bfd_boolean |
| 981 | coff_write_symbol (bfd *abfd, |
| 982 | asymbol *symbol, |
| 983 | combined_entry_type *native, |
| 984 | bfd_vma *written, |
| 985 | bfd_size_type *string_size_p, |
| 986 | asection **debug_string_section_p, |
| 987 | bfd_size_type *debug_string_size_p) |
| 988 | { |
| 989 | unsigned int numaux = native->u.syment.n_numaux; |
| 990 | int type = native->u.syment.n_type; |
| 991 | int n_sclass = (int) native->u.syment.n_sclass; |
| 992 | asection *output_section = symbol->section->output_section |
| 993 | ? symbol->section->output_section |
| 994 | : symbol->section; |
| 995 | void * buf; |
| 996 | bfd_size_type symesz; |
| 997 | |
| 998 | if (native->u.syment.n_sclass == C_FILE) |
| 999 | symbol->flags |= BSF_DEBUGGING; |
| 1000 | |
| 1001 | if (symbol->flags & BSF_DEBUGGING |
| 1002 | && bfd_is_abs_section (symbol->section)) |
| 1003 | native->u.syment.n_scnum = N_DEBUG; |
| 1004 | |
| 1005 | else if (bfd_is_abs_section (symbol->section)) |
| 1006 | native->u.syment.n_scnum = N_ABS; |
| 1007 | |
| 1008 | else if (bfd_is_und_section (symbol->section)) |
| 1009 | native->u.syment.n_scnum = N_UNDEF; |
| 1010 | |
| 1011 | else |
| 1012 | native->u.syment.n_scnum = |
| 1013 | output_section->target_index; |
| 1014 | |
| 1015 | coff_fix_symbol_name (abfd, symbol, native, string_size_p, |
| 1016 | debug_string_section_p, debug_string_size_p); |
| 1017 | |
| 1018 | symesz = bfd_coff_symesz (abfd); |
| 1019 | buf = bfd_alloc (abfd, symesz); |
| 1020 | if (!buf) |
| 1021 | return FALSE; |
| 1022 | bfd_coff_swap_sym_out (abfd, &native->u.syment, buf); |
| 1023 | if (bfd_bwrite (buf, symesz, abfd) != symesz) |
| 1024 | return FALSE; |
| 1025 | bfd_release (abfd, buf); |
| 1026 | |
| 1027 | if (native->u.syment.n_numaux > 0) |
| 1028 | { |
| 1029 | bfd_size_type auxesz; |
| 1030 | unsigned int j; |
| 1031 | |
| 1032 | auxesz = bfd_coff_auxesz (abfd); |
| 1033 | buf = bfd_alloc (abfd, auxesz); |
| 1034 | if (!buf) |
| 1035 | return FALSE; |
| 1036 | for (j = 0; j < native->u.syment.n_numaux; j++) |
| 1037 | { |
| 1038 | bfd_coff_swap_aux_out (abfd, |
| 1039 | &((native + j + 1)->u.auxent), |
| 1040 | type, n_sclass, (int) j, |
| 1041 | native->u.syment.n_numaux, |
| 1042 | buf); |
| 1043 | if (bfd_bwrite (buf, auxesz, abfd) != auxesz) |
| 1044 | return FALSE; |
| 1045 | } |
| 1046 | bfd_release (abfd, buf); |
| 1047 | } |
| 1048 | |
| 1049 | /* Store the index for use when we write out the relocs. */ |
| 1050 | set_index (symbol, *written); |
| 1051 | |
| 1052 | *written += numaux + 1; |
| 1053 | return TRUE; |
| 1054 | } |
| 1055 | |
| 1056 | /* Write out a symbol to a COFF file that does not come from a COFF |
| 1057 | file originally. This symbol may have been created by the linker, |
| 1058 | or we may be linking a non COFF file to a COFF file. */ |
| 1059 | |
| 1060 | bfd_boolean |
| 1061 | coff_write_alien_symbol (bfd *abfd, |
| 1062 | asymbol *symbol, |
| 1063 | struct internal_syment *isym, |
| 1064 | bfd_vma *written, |
| 1065 | bfd_size_type *string_size_p, |
| 1066 | asection **debug_string_section_p, |
| 1067 | bfd_size_type *debug_string_size_p) |
| 1068 | { |
| 1069 | combined_entry_type *native; |
| 1070 | combined_entry_type dummy[2]; |
| 1071 | asection *output_section = symbol->section->output_section |
| 1072 | ? symbol->section->output_section |
| 1073 | : symbol->section; |
| 1074 | struct bfd_link_info *link_info = coff_data (abfd)->link_info; |
| 1075 | bfd_boolean ret; |
| 1076 | |
| 1077 | if ((!link_info || link_info->strip_discarded) |
| 1078 | && !bfd_is_abs_section (symbol->section) |
| 1079 | && symbol->section->output_section == bfd_abs_section_ptr) |
| 1080 | { |
| 1081 | symbol->name = ""; |
| 1082 | if (isym != NULL) |
| 1083 | memset (isym, 0, sizeof(*isym)); |
| 1084 | return TRUE; |
| 1085 | } |
| 1086 | native = dummy; |
| 1087 | native->u.syment.n_type = T_NULL; |
| 1088 | native->u.syment.n_flags = 0; |
| 1089 | native->u.syment.n_numaux = 0; |
| 1090 | if (bfd_is_und_section (symbol->section)) |
| 1091 | { |
| 1092 | native->u.syment.n_scnum = N_UNDEF; |
| 1093 | native->u.syment.n_value = symbol->value; |
| 1094 | } |
| 1095 | else if (bfd_is_com_section (symbol->section)) |
| 1096 | { |
| 1097 | native->u.syment.n_scnum = N_UNDEF; |
| 1098 | native->u.syment.n_value = symbol->value; |
| 1099 | } |
| 1100 | else if (symbol->flags & BSF_FILE) |
| 1101 | { |
| 1102 | native->u.syment.n_scnum = N_DEBUG; |
| 1103 | native->u.syment.n_numaux = 1; |
| 1104 | } |
| 1105 | else if (symbol->flags & BSF_DEBUGGING) |
| 1106 | { |
| 1107 | /* There isn't much point to writing out a debugging symbol |
| 1108 | unless we are prepared to convert it into COFF debugging |
| 1109 | format. So, we just ignore them. We must clobber the symbol |
| 1110 | name to keep it from being put in the string table. */ |
| 1111 | symbol->name = ""; |
| 1112 | if (isym != NULL) |
| 1113 | memset (isym, 0, sizeof(*isym)); |
| 1114 | return TRUE; |
| 1115 | } |
| 1116 | else |
| 1117 | { |
| 1118 | native->u.syment.n_scnum = output_section->target_index; |
| 1119 | native->u.syment.n_value = (symbol->value |
| 1120 | + symbol->section->output_offset); |
| 1121 | if (! obj_pe (abfd)) |
| 1122 | native->u.syment.n_value += output_section->vma; |
| 1123 | |
| 1124 | /* Copy the any flags from the file header into the symbol. |
| 1125 | FIXME: Why? */ |
| 1126 | { |
| 1127 | coff_symbol_type *c = coff_symbol_from (abfd, symbol); |
| 1128 | if (c != (coff_symbol_type *) NULL) |
| 1129 | native->u.syment.n_flags = bfd_asymbol_bfd (&c->symbol)->flags; |
| 1130 | } |
| 1131 | } |
| 1132 | |
| 1133 | native->u.syment.n_type = 0; |
| 1134 | if (symbol->flags & BSF_FILE) |
| 1135 | native->u.syment.n_sclass = C_FILE; |
| 1136 | else if (symbol->flags & BSF_LOCAL) |
| 1137 | native->u.syment.n_sclass = C_STAT; |
| 1138 | else if (symbol->flags & BSF_WEAK) |
| 1139 | native->u.syment.n_sclass = obj_pe (abfd) ? C_NT_WEAK : C_WEAKEXT; |
| 1140 | else |
| 1141 | native->u.syment.n_sclass = C_EXT; |
| 1142 | |
| 1143 | ret = coff_write_symbol (abfd, symbol, native, written, string_size_p, |
| 1144 | debug_string_section_p, debug_string_size_p); |
| 1145 | if (isym != NULL) |
| 1146 | *isym = native->u.syment; |
| 1147 | return ret; |
| 1148 | } |
| 1149 | |
| 1150 | /* Write a native symbol to a COFF file. */ |
| 1151 | |
| 1152 | static bfd_boolean |
| 1153 | coff_write_native_symbol (bfd *abfd, |
| 1154 | coff_symbol_type *symbol, |
| 1155 | bfd_vma *written, |
| 1156 | bfd_size_type *string_size_p, |
| 1157 | asection **debug_string_section_p, |
| 1158 | bfd_size_type *debug_string_size_p) |
| 1159 | { |
| 1160 | combined_entry_type *native = symbol->native; |
| 1161 | alent *lineno = symbol->lineno; |
| 1162 | struct bfd_link_info *link_info = coff_data (abfd)->link_info; |
| 1163 | |
| 1164 | if ((!link_info || link_info->strip_discarded) |
| 1165 | && !bfd_is_abs_section (symbol->symbol.section) |
| 1166 | && symbol->symbol.section->output_section == bfd_abs_section_ptr) |
| 1167 | { |
| 1168 | symbol->symbol.name = ""; |
| 1169 | return TRUE; |
| 1170 | } |
| 1171 | |
| 1172 | /* If this symbol has an associated line number, we must store the |
| 1173 | symbol index in the line number field. We also tag the auxent to |
| 1174 | point to the right place in the lineno table. */ |
| 1175 | if (lineno && !symbol->done_lineno && symbol->symbol.section->owner != NULL) |
| 1176 | { |
| 1177 | unsigned int count = 0; |
| 1178 | |
| 1179 | lineno[count].u.offset = *written; |
| 1180 | if (native->u.syment.n_numaux) |
| 1181 | { |
| 1182 | union internal_auxent *a = &((native + 1)->u.auxent); |
| 1183 | |
| 1184 | a->x_sym.x_fcnary.x_fcn.x_lnnoptr = |
| 1185 | symbol->symbol.section->output_section->moving_line_filepos; |
| 1186 | } |
| 1187 | |
| 1188 | /* Count and relocate all other linenumbers. */ |
| 1189 | count++; |
| 1190 | while (lineno[count].line_number != 0) |
| 1191 | { |
| 1192 | lineno[count].u.offset += |
| 1193 | (symbol->symbol.section->output_section->vma |
| 1194 | + symbol->symbol.section->output_offset); |
| 1195 | count++; |
| 1196 | } |
| 1197 | symbol->done_lineno = TRUE; |
| 1198 | |
| 1199 | if (! bfd_is_const_section (symbol->symbol.section->output_section)) |
| 1200 | symbol->symbol.section->output_section->moving_line_filepos += |
| 1201 | count * bfd_coff_linesz (abfd); |
| 1202 | } |
| 1203 | |
| 1204 | return coff_write_symbol (abfd, &(symbol->symbol), native, written, |
| 1205 | string_size_p, debug_string_section_p, |
| 1206 | debug_string_size_p); |
| 1207 | } |
| 1208 | |
| 1209 | static void |
| 1210 | null_error_handler (const char * fmt ATTRIBUTE_UNUSED, ...) |
| 1211 | { |
| 1212 | } |
| 1213 | |
| 1214 | /* Write out the COFF symbols. */ |
| 1215 | |
| 1216 | bfd_boolean |
| 1217 | coff_write_symbols (bfd *abfd) |
| 1218 | { |
| 1219 | bfd_size_type string_size; |
| 1220 | asection *debug_string_section; |
| 1221 | bfd_size_type debug_string_size; |
| 1222 | unsigned int i; |
| 1223 | unsigned int limit = bfd_get_symcount (abfd); |
| 1224 | bfd_vma written = 0; |
| 1225 | asymbol **p; |
| 1226 | |
| 1227 | string_size = 0; |
| 1228 | debug_string_section = NULL; |
| 1229 | debug_string_size = 0; |
| 1230 | |
| 1231 | /* If this target supports long section names, they must be put into |
| 1232 | the string table. This is supported by PE. This code must |
| 1233 | handle section names just as they are handled in |
| 1234 | coff_write_object_contents. */ |
| 1235 | if (bfd_coff_long_section_names (abfd)) |
| 1236 | { |
| 1237 | asection *o; |
| 1238 | |
| 1239 | for (o = abfd->sections; o != NULL; o = o->next) |
| 1240 | { |
| 1241 | size_t len; |
| 1242 | |
| 1243 | len = strlen (o->name); |
| 1244 | if (len > SCNNMLEN) |
| 1245 | string_size += len + 1; |
| 1246 | } |
| 1247 | } |
| 1248 | |
| 1249 | /* Seek to the right place. */ |
| 1250 | if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0) |
| 1251 | return FALSE; |
| 1252 | |
| 1253 | /* Output all the symbols we have. */ |
| 1254 | written = 0; |
| 1255 | for (p = abfd->outsymbols, i = 0; i < limit; i++, p++) |
| 1256 | { |
| 1257 | asymbol *symbol = *p; |
| 1258 | coff_symbol_type *c_symbol = coff_symbol_from (abfd, symbol); |
| 1259 | |
| 1260 | if (c_symbol == (coff_symbol_type *) NULL |
| 1261 | || c_symbol->native == (combined_entry_type *) NULL) |
| 1262 | { |
| 1263 | if (!coff_write_alien_symbol (abfd, symbol, NULL, &written, |
| 1264 | &string_size, &debug_string_section, |
| 1265 | &debug_string_size)) |
| 1266 | return FALSE; |
| 1267 | } |
| 1268 | else |
| 1269 | { |
| 1270 | if (coff_backend_info (abfd)->_bfd_coff_classify_symbol != NULL) |
| 1271 | { |
| 1272 | bfd_error_handler_type current_error_handler; |
| 1273 | enum coff_symbol_classification sym_class; |
| 1274 | unsigned char *n_sclass; |
| 1275 | |
| 1276 | /* Suppress error reporting by bfd_coff_classify_symbol. |
| 1277 | Error messages can be generated when we are processing a local |
| 1278 | symbol which has no associated section and we do not have to |
| 1279 | worry about this, all we need to know is that it is local. */ |
| 1280 | current_error_handler = bfd_set_error_handler (null_error_handler); |
| 1281 | sym_class = bfd_coff_classify_symbol (abfd, |
| 1282 | &c_symbol->native->u.syment); |
| 1283 | (void) bfd_set_error_handler (current_error_handler); |
| 1284 | |
| 1285 | n_sclass = &c_symbol->native->u.syment.n_sclass; |
| 1286 | |
| 1287 | /* If the symbol class has been changed (eg objcopy/ld script/etc) |
| 1288 | we cannot retain the existing sclass from the original symbol. |
| 1289 | Weak symbols only have one valid sclass, so just set it always. |
| 1290 | If it is not local class and should be, set it C_STAT. |
| 1291 | If it is global and not classified as global, or if it is |
| 1292 | weak (which is also classified as global), set it C_EXT. */ |
| 1293 | |
| 1294 | if (symbol->flags & BSF_WEAK) |
| 1295 | *n_sclass = obj_pe (abfd) ? C_NT_WEAK : C_WEAKEXT; |
| 1296 | else if (symbol->flags & BSF_LOCAL && sym_class != COFF_SYMBOL_LOCAL) |
| 1297 | *n_sclass = C_STAT; |
| 1298 | else if (symbol->flags & BSF_GLOBAL |
| 1299 | && (sym_class != COFF_SYMBOL_GLOBAL |
| 1300 | #ifdef COFF_WITH_PE |
| 1301 | || *n_sclass == C_NT_WEAK |
| 1302 | #endif |
| 1303 | || *n_sclass == C_WEAKEXT)) |
| 1304 | c_symbol->native->u.syment.n_sclass = C_EXT; |
| 1305 | } |
| 1306 | |
| 1307 | if (!coff_write_native_symbol (abfd, c_symbol, &written, |
| 1308 | &string_size, &debug_string_section, |
| 1309 | &debug_string_size)) |
| 1310 | return FALSE; |
| 1311 | } |
| 1312 | } |
| 1313 | |
| 1314 | obj_raw_syment_count (abfd) = written; |
| 1315 | |
| 1316 | /* Now write out strings. */ |
| 1317 | if (string_size != 0) |
| 1318 | { |
| 1319 | unsigned int size = string_size + STRING_SIZE_SIZE; |
| 1320 | bfd_byte buffer[STRING_SIZE_SIZE]; |
| 1321 | |
| 1322 | #if STRING_SIZE_SIZE == 4 |
| 1323 | H_PUT_32 (abfd, size, buffer); |
| 1324 | #else |
| 1325 | #error Change H_PUT_32 |
| 1326 | #endif |
| 1327 | if (bfd_bwrite ((void *) buffer, (bfd_size_type) sizeof (buffer), abfd) |
| 1328 | != sizeof (buffer)) |
| 1329 | return FALSE; |
| 1330 | |
| 1331 | /* Handle long section names. This code must handle section |
| 1332 | names just as they are handled in coff_write_object_contents. */ |
| 1333 | if (bfd_coff_long_section_names (abfd)) |
| 1334 | { |
| 1335 | asection *o; |
| 1336 | |
| 1337 | for (o = abfd->sections; o != NULL; o = o->next) |
| 1338 | { |
| 1339 | size_t len; |
| 1340 | |
| 1341 | len = strlen (o->name); |
| 1342 | if (len > SCNNMLEN) |
| 1343 | { |
| 1344 | if (bfd_bwrite (o->name, (bfd_size_type) (len + 1), abfd) |
| 1345 | != len + 1) |
| 1346 | return FALSE; |
| 1347 | } |
| 1348 | } |
| 1349 | } |
| 1350 | |
| 1351 | for (p = abfd->outsymbols, i = 0; |
| 1352 | i < limit; |
| 1353 | i++, p++) |
| 1354 | { |
| 1355 | asymbol *q = *p; |
| 1356 | size_t name_length = strlen (q->name); |
| 1357 | coff_symbol_type *c_symbol = coff_symbol_from (abfd, q); |
| 1358 | size_t maxlen; |
| 1359 | |
| 1360 | /* Figure out whether the symbol name should go in the string |
| 1361 | table. Symbol names that are short enough are stored |
| 1362 | directly in the syment structure. File names permit a |
| 1363 | different, longer, length in the syment structure. On |
| 1364 | XCOFF, some symbol names are stored in the .debug section |
| 1365 | rather than in the string table. */ |
| 1366 | |
| 1367 | if (c_symbol == NULL |
| 1368 | || c_symbol->native == NULL) |
| 1369 | /* This is not a COFF symbol, so it certainly is not a |
| 1370 | file name, nor does it go in the .debug section. */ |
| 1371 | maxlen = bfd_coff_force_symnames_in_strings (abfd) ? 0 : SYMNMLEN; |
| 1372 | |
| 1373 | else if (bfd_coff_symname_in_debug (abfd, |
| 1374 | &c_symbol->native->u.syment)) |
| 1375 | /* This symbol name is in the XCOFF .debug section. |
| 1376 | Don't write it into the string table. */ |
| 1377 | maxlen = name_length; |
| 1378 | |
| 1379 | else if (c_symbol->native->u.syment.n_sclass == C_FILE |
| 1380 | && c_symbol->native->u.syment.n_numaux > 0) |
| 1381 | { |
| 1382 | if (bfd_coff_force_symnames_in_strings (abfd)) |
| 1383 | { |
| 1384 | if (bfd_bwrite (".file", (bfd_size_type) 6, abfd) != 6) |
| 1385 | return FALSE; |
| 1386 | } |
| 1387 | maxlen = bfd_coff_filnmlen (abfd); |
| 1388 | } |
| 1389 | else |
| 1390 | maxlen = bfd_coff_force_symnames_in_strings (abfd) ? 0 : SYMNMLEN; |
| 1391 | |
| 1392 | if (name_length > maxlen) |
| 1393 | { |
| 1394 | if (bfd_bwrite ((void *) (q->name), (bfd_size_type) name_length + 1, |
| 1395 | abfd) != name_length + 1) |
| 1396 | return FALSE; |
| 1397 | } |
| 1398 | } |
| 1399 | } |
| 1400 | else |
| 1401 | { |
| 1402 | /* We would normally not write anything here, but we'll write |
| 1403 | out 4 so that any stupid coff reader which tries to read the |
| 1404 | string table even when there isn't one won't croak. */ |
| 1405 | unsigned int size = STRING_SIZE_SIZE; |
| 1406 | bfd_byte buffer[STRING_SIZE_SIZE]; |
| 1407 | |
| 1408 | #if STRING_SIZE_SIZE == 4 |
| 1409 | H_PUT_32 (abfd, size, buffer); |
| 1410 | #else |
| 1411 | #error Change H_PUT_32 |
| 1412 | #endif |
| 1413 | if (bfd_bwrite ((void *) buffer, (bfd_size_type) STRING_SIZE_SIZE, abfd) |
| 1414 | != STRING_SIZE_SIZE) |
| 1415 | return FALSE; |
| 1416 | } |
| 1417 | |
| 1418 | /* Make sure the .debug section was created to be the correct size. |
| 1419 | We should create it ourselves on the fly, but we don't because |
| 1420 | BFD won't let us write to any section until we know how large all |
| 1421 | the sections are. We could still do it by making another pass |
| 1422 | over the symbols. FIXME. */ |
| 1423 | BFD_ASSERT (debug_string_size == 0 |
| 1424 | || (debug_string_section != (asection *) NULL |
| 1425 | && (BFD_ALIGN (debug_string_size, |
| 1426 | 1 << debug_string_section->alignment_power) |
| 1427 | == debug_string_section->size))); |
| 1428 | |
| 1429 | return TRUE; |
| 1430 | } |
| 1431 | |
| 1432 | bfd_boolean |
| 1433 | coff_write_linenumbers (bfd *abfd) |
| 1434 | { |
| 1435 | asection *s; |
| 1436 | bfd_size_type linesz; |
| 1437 | void * buff; |
| 1438 | |
| 1439 | linesz = bfd_coff_linesz (abfd); |
| 1440 | buff = bfd_alloc (abfd, linesz); |
| 1441 | if (!buff) |
| 1442 | return FALSE; |
| 1443 | for (s = abfd->sections; s != (asection *) NULL; s = s->next) |
| 1444 | { |
| 1445 | if (s->lineno_count) |
| 1446 | { |
| 1447 | asymbol **q = abfd->outsymbols; |
| 1448 | if (bfd_seek (abfd, s->line_filepos, SEEK_SET) != 0) |
| 1449 | return FALSE; |
| 1450 | /* Find all the linenumbers in this section. */ |
| 1451 | while (*q) |
| 1452 | { |
| 1453 | asymbol *p = *q; |
| 1454 | if (p->section->output_section == s) |
| 1455 | { |
| 1456 | alent *l = |
| 1457 | BFD_SEND (bfd_asymbol_bfd (p), _get_lineno, |
| 1458 | (bfd_asymbol_bfd (p), p)); |
| 1459 | if (l) |
| 1460 | { |
| 1461 | /* Found a linenumber entry, output. */ |
| 1462 | struct internal_lineno out; |
| 1463 | memset ((void *) & out, 0, sizeof (out)); |
| 1464 | out.l_lnno = 0; |
| 1465 | out.l_addr.l_symndx = l->u.offset; |
| 1466 | bfd_coff_swap_lineno_out (abfd, &out, buff); |
| 1467 | if (bfd_bwrite (buff, (bfd_size_type) linesz, abfd) |
| 1468 | != linesz) |
| 1469 | return FALSE; |
| 1470 | l++; |
| 1471 | while (l->line_number) |
| 1472 | { |
| 1473 | out.l_lnno = l->line_number; |
| 1474 | out.l_addr.l_symndx = l->u.offset; |
| 1475 | bfd_coff_swap_lineno_out (abfd, &out, buff); |
| 1476 | if (bfd_bwrite (buff, (bfd_size_type) linesz, abfd) |
| 1477 | != linesz) |
| 1478 | return FALSE; |
| 1479 | l++; |
| 1480 | } |
| 1481 | } |
| 1482 | } |
| 1483 | q++; |
| 1484 | } |
| 1485 | } |
| 1486 | } |
| 1487 | bfd_release (abfd, buff); |
| 1488 | return TRUE; |
| 1489 | } |
| 1490 | |
| 1491 | alent * |
| 1492 | coff_get_lineno (bfd *ignore_abfd ATTRIBUTE_UNUSED, asymbol *symbol) |
| 1493 | { |
| 1494 | return coffsymbol (symbol)->lineno; |
| 1495 | } |
| 1496 | |
| 1497 | /* This function transforms the offsets into the symbol table into |
| 1498 | pointers to syments. */ |
| 1499 | |
| 1500 | static void |
| 1501 | coff_pointerize_aux (bfd *abfd, |
| 1502 | combined_entry_type *table_base, |
| 1503 | combined_entry_type *symbol, |
| 1504 | unsigned int indaux, |
| 1505 | combined_entry_type *auxent) |
| 1506 | { |
| 1507 | unsigned int type = symbol->u.syment.n_type; |
| 1508 | unsigned int n_sclass = symbol->u.syment.n_sclass; |
| 1509 | |
| 1510 | if (coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook) |
| 1511 | { |
| 1512 | if ((*coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook) |
| 1513 | (abfd, table_base, symbol, indaux, auxent)) |
| 1514 | return; |
| 1515 | } |
| 1516 | |
| 1517 | /* Don't bother if this is a file or a section. */ |
| 1518 | if (n_sclass == C_STAT && type == T_NULL) |
| 1519 | return; |
| 1520 | if (n_sclass == C_FILE) |
| 1521 | return; |
| 1522 | |
| 1523 | /* Otherwise patch up. */ |
| 1524 | #define N_TMASK coff_data (abfd)->local_n_tmask |
| 1525 | #define N_BTSHFT coff_data (abfd)->local_n_btshft |
| 1526 | |
| 1527 | if ((ISFCN (type) || ISTAG (n_sclass) || n_sclass == C_BLOCK |
| 1528 | || n_sclass == C_FCN) |
| 1529 | && auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l > 0) |
| 1530 | { |
| 1531 | auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p = |
| 1532 | table_base + auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l; |
| 1533 | auxent->fix_end = 1; |
| 1534 | } |
| 1535 | /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can |
| 1536 | generate one, so we must be careful to ignore it. */ |
| 1537 | if (auxent->u.auxent.x_sym.x_tagndx.l > 0) |
| 1538 | { |
| 1539 | auxent->u.auxent.x_sym.x_tagndx.p = |
| 1540 | table_base + auxent->u.auxent.x_sym.x_tagndx.l; |
| 1541 | auxent->fix_tag = 1; |
| 1542 | } |
| 1543 | } |
| 1544 | |
| 1545 | /* Allocate space for the ".debug" section, and read it. |
| 1546 | We did not read the debug section until now, because |
| 1547 | we didn't want to go to the trouble until someone needed it. */ |
| 1548 | |
| 1549 | static char * |
| 1550 | build_debug_section (bfd *abfd) |
| 1551 | { |
| 1552 | char *debug_section; |
| 1553 | file_ptr position; |
| 1554 | bfd_size_type sec_size; |
| 1555 | |
| 1556 | asection *sect = bfd_get_section_by_name (abfd, ".debug"); |
| 1557 | |
| 1558 | if (!sect) |
| 1559 | { |
| 1560 | bfd_set_error (bfd_error_no_debug_section); |
| 1561 | return NULL; |
| 1562 | } |
| 1563 | |
| 1564 | sec_size = sect->size; |
| 1565 | debug_section = (char *) bfd_alloc (abfd, sec_size); |
| 1566 | if (debug_section == NULL) |
| 1567 | return NULL; |
| 1568 | |
| 1569 | /* Seek to the beginning of the `.debug' section and read it. |
| 1570 | Save the current position first; it is needed by our caller. |
| 1571 | Then read debug section and reset the file pointer. */ |
| 1572 | |
| 1573 | position = bfd_tell (abfd); |
| 1574 | if (bfd_seek (abfd, sect->filepos, SEEK_SET) != 0 |
| 1575 | || bfd_bread (debug_section, sec_size, abfd) != sec_size |
| 1576 | || bfd_seek (abfd, position, SEEK_SET) != 0) |
| 1577 | return NULL; |
| 1578 | return debug_section; |
| 1579 | } |
| 1580 | |
| 1581 | /* Return a pointer to a malloc'd copy of 'name'. 'name' may not be |
| 1582 | \0-terminated, but will not exceed 'maxlen' characters. The copy *will* |
| 1583 | be \0-terminated. */ |
| 1584 | |
| 1585 | static char * |
| 1586 | copy_name (bfd *abfd, char *name, size_t maxlen) |
| 1587 | { |
| 1588 | size_t len; |
| 1589 | char *newname; |
| 1590 | |
| 1591 | for (len = 0; len < maxlen; ++len) |
| 1592 | if (name[len] == '\0') |
| 1593 | break; |
| 1594 | |
| 1595 | if ((newname = (char *) bfd_alloc (abfd, (bfd_size_type) len + 1)) == NULL) |
| 1596 | return NULL; |
| 1597 | |
| 1598 | strncpy (newname, name, len); |
| 1599 | newname[len] = '\0'; |
| 1600 | return newname; |
| 1601 | } |
| 1602 | |
| 1603 | /* Read in the external symbols. */ |
| 1604 | |
| 1605 | bfd_boolean |
| 1606 | _bfd_coff_get_external_symbols (bfd *abfd) |
| 1607 | { |
| 1608 | bfd_size_type symesz; |
| 1609 | bfd_size_type size; |
| 1610 | void * syms; |
| 1611 | |
| 1612 | if (obj_coff_external_syms (abfd) != NULL) |
| 1613 | return TRUE; |
| 1614 | |
| 1615 | symesz = bfd_coff_symesz (abfd); |
| 1616 | |
| 1617 | size = obj_raw_syment_count (abfd) * symesz; |
| 1618 | if (size == 0) |
| 1619 | return TRUE; |
| 1620 | |
| 1621 | syms = bfd_malloc (size); |
| 1622 | if (syms == NULL) |
| 1623 | return FALSE; |
| 1624 | |
| 1625 | if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0 |
| 1626 | || bfd_bread (syms, size, abfd) != size) |
| 1627 | { |
| 1628 | if (syms != NULL) |
| 1629 | free (syms); |
| 1630 | return FALSE; |
| 1631 | } |
| 1632 | |
| 1633 | obj_coff_external_syms (abfd) = syms; |
| 1634 | |
| 1635 | return TRUE; |
| 1636 | } |
| 1637 | |
| 1638 | /* Read in the external strings. The strings are not loaded until |
| 1639 | they are needed. This is because we have no simple way of |
| 1640 | detecting a missing string table in an archive. */ |
| 1641 | |
| 1642 | const char * |
| 1643 | _bfd_coff_read_string_table (bfd *abfd) |
| 1644 | { |
| 1645 | char extstrsize[STRING_SIZE_SIZE]; |
| 1646 | bfd_size_type strsize; |
| 1647 | char *strings; |
| 1648 | file_ptr pos; |
| 1649 | |
| 1650 | if (obj_coff_strings (abfd) != NULL) |
| 1651 | return obj_coff_strings (abfd); |
| 1652 | |
| 1653 | if (obj_sym_filepos (abfd) == 0) |
| 1654 | { |
| 1655 | bfd_set_error (bfd_error_no_symbols); |
| 1656 | return NULL; |
| 1657 | } |
| 1658 | |
| 1659 | pos = obj_sym_filepos (abfd); |
| 1660 | pos += obj_raw_syment_count (abfd) * bfd_coff_symesz (abfd); |
| 1661 | if (bfd_seek (abfd, pos, SEEK_SET) != 0) |
| 1662 | return NULL; |
| 1663 | |
| 1664 | if (bfd_bread (extstrsize, (bfd_size_type) sizeof extstrsize, abfd) |
| 1665 | != sizeof extstrsize) |
| 1666 | { |
| 1667 | if (bfd_get_error () != bfd_error_file_truncated) |
| 1668 | return NULL; |
| 1669 | |
| 1670 | /* There is no string table. */ |
| 1671 | strsize = STRING_SIZE_SIZE; |
| 1672 | } |
| 1673 | else |
| 1674 | { |
| 1675 | #if STRING_SIZE_SIZE == 4 |
| 1676 | strsize = H_GET_32 (abfd, extstrsize); |
| 1677 | #else |
| 1678 | #error Change H_GET_32 |
| 1679 | #endif |
| 1680 | } |
| 1681 | |
| 1682 | if (strsize < STRING_SIZE_SIZE) |
| 1683 | { |
| 1684 | (*_bfd_error_handler) |
| 1685 | (_("%B: bad string table size %lu"), abfd, (unsigned long) strsize); |
| 1686 | bfd_set_error (bfd_error_bad_value); |
| 1687 | return NULL; |
| 1688 | } |
| 1689 | |
| 1690 | strings = (char *) bfd_malloc (strsize); |
| 1691 | if (strings == NULL) |
| 1692 | return NULL; |
| 1693 | |
| 1694 | if (bfd_bread (strings + STRING_SIZE_SIZE, strsize - STRING_SIZE_SIZE, abfd) |
| 1695 | != strsize - STRING_SIZE_SIZE) |
| 1696 | { |
| 1697 | free (strings); |
| 1698 | return NULL; |
| 1699 | } |
| 1700 | |
| 1701 | obj_coff_strings (abfd) = strings; |
| 1702 | |
| 1703 | return strings; |
| 1704 | } |
| 1705 | |
| 1706 | /* Free up the external symbols and strings read from a COFF file. */ |
| 1707 | |
| 1708 | bfd_boolean |
| 1709 | _bfd_coff_free_symbols (bfd *abfd) |
| 1710 | { |
| 1711 | if (obj_coff_external_syms (abfd) != NULL |
| 1712 | && ! obj_coff_keep_syms (abfd)) |
| 1713 | { |
| 1714 | free (obj_coff_external_syms (abfd)); |
| 1715 | obj_coff_external_syms (abfd) = NULL; |
| 1716 | } |
| 1717 | if (obj_coff_strings (abfd) != NULL |
| 1718 | && ! obj_coff_keep_strings (abfd)) |
| 1719 | { |
| 1720 | free (obj_coff_strings (abfd)); |
| 1721 | obj_coff_strings (abfd) = NULL; |
| 1722 | } |
| 1723 | return TRUE; |
| 1724 | } |
| 1725 | |
| 1726 | /* Read a symbol table into freshly bfd_allocated memory, swap it, and |
| 1727 | knit the symbol names into a normalized form. By normalized here I |
| 1728 | mean that all symbols have an n_offset pointer that points to a null- |
| 1729 | terminated string. */ |
| 1730 | |
| 1731 | combined_entry_type * |
| 1732 | coff_get_normalized_symtab (bfd *abfd) |
| 1733 | { |
| 1734 | combined_entry_type *internal; |
| 1735 | combined_entry_type *internal_ptr; |
| 1736 | combined_entry_type *symbol_ptr; |
| 1737 | combined_entry_type *internal_end; |
| 1738 | size_t symesz; |
| 1739 | char *raw_src; |
| 1740 | char *raw_end; |
| 1741 | const char *string_table = NULL; |
| 1742 | char *debug_section = NULL; |
| 1743 | bfd_size_type size; |
| 1744 | |
| 1745 | if (obj_raw_syments (abfd) != NULL) |
| 1746 | return obj_raw_syments (abfd); |
| 1747 | |
| 1748 | size = obj_raw_syment_count (abfd) * sizeof (combined_entry_type); |
| 1749 | internal = (combined_entry_type *) bfd_zalloc (abfd, size); |
| 1750 | if (internal == NULL && size != 0) |
| 1751 | return NULL; |
| 1752 | internal_end = internal + obj_raw_syment_count (abfd); |
| 1753 | |
| 1754 | if (! _bfd_coff_get_external_symbols (abfd)) |
| 1755 | return NULL; |
| 1756 | |
| 1757 | raw_src = (char *) obj_coff_external_syms (abfd); |
| 1758 | |
| 1759 | /* Mark the end of the symbols. */ |
| 1760 | symesz = bfd_coff_symesz (abfd); |
| 1761 | raw_end = (char *) raw_src + obj_raw_syment_count (abfd) * symesz; |
| 1762 | |
| 1763 | /* FIXME SOMEDAY. A string table size of zero is very weird, but |
| 1764 | probably possible. If one shows up, it will probably kill us. */ |
| 1765 | |
| 1766 | /* Swap all the raw entries. */ |
| 1767 | for (internal_ptr = internal; |
| 1768 | raw_src < raw_end; |
| 1769 | raw_src += symesz, internal_ptr++) |
| 1770 | { |
| 1771 | |
| 1772 | unsigned int i; |
| 1773 | bfd_coff_swap_sym_in (abfd, (void *) raw_src, |
| 1774 | (void *) & internal_ptr->u.syment); |
| 1775 | symbol_ptr = internal_ptr; |
| 1776 | |
| 1777 | for (i = 0; |
| 1778 | i < symbol_ptr->u.syment.n_numaux; |
| 1779 | i++) |
| 1780 | { |
| 1781 | internal_ptr++; |
| 1782 | raw_src += symesz; |
| 1783 | bfd_coff_swap_aux_in (abfd, (void *) raw_src, |
| 1784 | symbol_ptr->u.syment.n_type, |
| 1785 | symbol_ptr->u.syment.n_sclass, |
| 1786 | (int) i, symbol_ptr->u.syment.n_numaux, |
| 1787 | &(internal_ptr->u.auxent)); |
| 1788 | coff_pointerize_aux (abfd, internal, symbol_ptr, i, |
| 1789 | internal_ptr); |
| 1790 | } |
| 1791 | } |
| 1792 | |
| 1793 | /* Free the raw symbols, but not the strings (if we have them). */ |
| 1794 | obj_coff_keep_strings (abfd) = TRUE; |
| 1795 | if (! _bfd_coff_free_symbols (abfd)) |
| 1796 | return NULL; |
| 1797 | |
| 1798 | for (internal_ptr = internal; internal_ptr < internal_end; |
| 1799 | internal_ptr++) |
| 1800 | { |
| 1801 | if (internal_ptr->u.syment.n_sclass == C_FILE |
| 1802 | && internal_ptr->u.syment.n_numaux > 0) |
| 1803 | { |
| 1804 | /* Make a file symbol point to the name in the auxent, since |
| 1805 | the text ".file" is redundant. */ |
| 1806 | if ((internal_ptr + 1)->u.auxent.x_file.x_n.x_zeroes == 0) |
| 1807 | { |
| 1808 | /* The filename is a long one, point into the string table. */ |
| 1809 | if (string_table == NULL) |
| 1810 | { |
| 1811 | string_table = _bfd_coff_read_string_table (abfd); |
| 1812 | if (string_table == NULL) |
| 1813 | return NULL; |
| 1814 | } |
| 1815 | |
| 1816 | internal_ptr->u.syment._n._n_n._n_offset = |
| 1817 | ((bfd_hostptr_t) |
| 1818 | (string_table |
| 1819 | + (internal_ptr + 1)->u.auxent.x_file.x_n.x_offset)); |
| 1820 | } |
| 1821 | else |
| 1822 | { |
| 1823 | /* Ordinary short filename, put into memory anyway. The |
| 1824 | Microsoft PE tools sometimes store a filename in |
| 1825 | multiple AUX entries. */ |
| 1826 | if (internal_ptr->u.syment.n_numaux > 1 |
| 1827 | && coff_data (abfd)->pe) |
| 1828 | internal_ptr->u.syment._n._n_n._n_offset = |
| 1829 | ((bfd_hostptr_t) |
| 1830 | copy_name (abfd, |
| 1831 | (internal_ptr + 1)->u.auxent.x_file.x_fname, |
| 1832 | internal_ptr->u.syment.n_numaux * symesz)); |
| 1833 | else |
| 1834 | internal_ptr->u.syment._n._n_n._n_offset = |
| 1835 | ((bfd_hostptr_t) |
| 1836 | copy_name (abfd, |
| 1837 | (internal_ptr + 1)->u.auxent.x_file.x_fname, |
| 1838 | (size_t) bfd_coff_filnmlen (abfd))); |
| 1839 | } |
| 1840 | } |
| 1841 | else |
| 1842 | { |
| 1843 | if (internal_ptr->u.syment._n._n_n._n_zeroes != 0) |
| 1844 | { |
| 1845 | /* This is a "short" name. Make it long. */ |
| 1846 | size_t i; |
| 1847 | char *newstring; |
| 1848 | |
| 1849 | /* Find the length of this string without walking into memory |
| 1850 | that isn't ours. */ |
| 1851 | for (i = 0; i < 8; ++i) |
| 1852 | if (internal_ptr->u.syment._n._n_name[i] == '\0') |
| 1853 | break; |
| 1854 | |
| 1855 | newstring = (char *) bfd_zalloc (abfd, (bfd_size_type) (i + 1)); |
| 1856 | if (newstring == NULL) |
| 1857 | return NULL; |
| 1858 | strncpy (newstring, internal_ptr->u.syment._n._n_name, i); |
| 1859 | internal_ptr->u.syment._n._n_n._n_offset = (bfd_hostptr_t) newstring; |
| 1860 | internal_ptr->u.syment._n._n_n._n_zeroes = 0; |
| 1861 | } |
| 1862 | else if (internal_ptr->u.syment._n._n_n._n_offset == 0) |
| 1863 | internal_ptr->u.syment._n._n_n._n_offset = (bfd_hostptr_t) ""; |
| 1864 | else if (!bfd_coff_symname_in_debug (abfd, &internal_ptr->u.syment)) |
| 1865 | { |
| 1866 | /* Long name already. Point symbol at the string in the |
| 1867 | table. */ |
| 1868 | if (string_table == NULL) |
| 1869 | { |
| 1870 | string_table = _bfd_coff_read_string_table (abfd); |
| 1871 | if (string_table == NULL) |
| 1872 | return NULL; |
| 1873 | } |
| 1874 | internal_ptr->u.syment._n._n_n._n_offset = |
| 1875 | ((bfd_hostptr_t) |
| 1876 | (string_table |
| 1877 | + internal_ptr->u.syment._n._n_n._n_offset)); |
| 1878 | } |
| 1879 | else |
| 1880 | { |
| 1881 | /* Long name in debug section. Very similar. */ |
| 1882 | if (debug_section == NULL) |
| 1883 | debug_section = build_debug_section (abfd); |
| 1884 | internal_ptr->u.syment._n._n_n._n_offset = (bfd_hostptr_t) |
| 1885 | (debug_section + internal_ptr->u.syment._n._n_n._n_offset); |
| 1886 | } |
| 1887 | } |
| 1888 | internal_ptr += internal_ptr->u.syment.n_numaux; |
| 1889 | } |
| 1890 | |
| 1891 | obj_raw_syments (abfd) = internal; |
| 1892 | BFD_ASSERT (obj_raw_syment_count (abfd) |
| 1893 | == (unsigned int) (internal_ptr - internal)); |
| 1894 | |
| 1895 | return internal; |
| 1896 | } |
| 1897 | |
| 1898 | long |
| 1899 | coff_get_reloc_upper_bound (bfd *abfd, sec_ptr asect) |
| 1900 | { |
| 1901 | if (bfd_get_format (abfd) != bfd_object) |
| 1902 | { |
| 1903 | bfd_set_error (bfd_error_invalid_operation); |
| 1904 | return -1; |
| 1905 | } |
| 1906 | return (asect->reloc_count + 1) * sizeof (arelent *); |
| 1907 | } |
| 1908 | |
| 1909 | asymbol * |
| 1910 | coff_make_empty_symbol (bfd *abfd) |
| 1911 | { |
| 1912 | bfd_size_type amt = sizeof (coff_symbol_type); |
| 1913 | coff_symbol_type *new_symbol = (coff_symbol_type *) bfd_zalloc (abfd, amt); |
| 1914 | |
| 1915 | if (new_symbol == NULL) |
| 1916 | return NULL; |
| 1917 | new_symbol->symbol.section = 0; |
| 1918 | new_symbol->native = 0; |
| 1919 | new_symbol->lineno = NULL; |
| 1920 | new_symbol->done_lineno = FALSE; |
| 1921 | new_symbol->symbol.the_bfd = abfd; |
| 1922 | |
| 1923 | return & new_symbol->symbol; |
| 1924 | } |
| 1925 | |
| 1926 | /* Make a debugging symbol. */ |
| 1927 | |
| 1928 | asymbol * |
| 1929 | coff_bfd_make_debug_symbol (bfd *abfd, |
| 1930 | void * ptr ATTRIBUTE_UNUSED, |
| 1931 | unsigned long sz ATTRIBUTE_UNUSED) |
| 1932 | { |
| 1933 | bfd_size_type amt = sizeof (coff_symbol_type); |
| 1934 | coff_symbol_type *new_symbol = (coff_symbol_type *) bfd_alloc (abfd, amt); |
| 1935 | |
| 1936 | if (new_symbol == NULL) |
| 1937 | return NULL; |
| 1938 | /* @@ The 10 is a guess at a plausible maximum number of aux entries |
| 1939 | (but shouldn't be a constant). */ |
| 1940 | amt = sizeof (combined_entry_type) * 10; |
| 1941 | new_symbol->native = (combined_entry_type *) bfd_zalloc (abfd, amt); |
| 1942 | if (!new_symbol->native) |
| 1943 | return NULL; |
| 1944 | new_symbol->symbol.section = bfd_abs_section_ptr; |
| 1945 | new_symbol->symbol.flags = BSF_DEBUGGING; |
| 1946 | new_symbol->lineno = NULL; |
| 1947 | new_symbol->done_lineno = FALSE; |
| 1948 | new_symbol->symbol.the_bfd = abfd; |
| 1949 | |
| 1950 | return & new_symbol->symbol; |
| 1951 | } |
| 1952 | |
| 1953 | void |
| 1954 | coff_get_symbol_info (bfd *abfd, asymbol *symbol, symbol_info *ret) |
| 1955 | { |
| 1956 | bfd_symbol_info (symbol, ret); |
| 1957 | |
| 1958 | if (coffsymbol (symbol)->native != NULL |
| 1959 | && coffsymbol (symbol)->native->fix_value) |
| 1960 | ret->value = coffsymbol (symbol)->native->u.syment.n_value - |
| 1961 | (bfd_hostptr_t) obj_raw_syments (abfd); |
| 1962 | } |
| 1963 | |
| 1964 | /* Return the COFF syment for a symbol. */ |
| 1965 | |
| 1966 | bfd_boolean |
| 1967 | bfd_coff_get_syment (bfd *abfd, |
| 1968 | asymbol *symbol, |
| 1969 | struct internal_syment *psyment) |
| 1970 | { |
| 1971 | coff_symbol_type *csym; |
| 1972 | |
| 1973 | csym = coff_symbol_from (abfd, symbol); |
| 1974 | if (csym == NULL || csym->native == NULL) |
| 1975 | { |
| 1976 | bfd_set_error (bfd_error_invalid_operation); |
| 1977 | return FALSE; |
| 1978 | } |
| 1979 | |
| 1980 | *psyment = csym->native->u.syment; |
| 1981 | |
| 1982 | if (csym->native->fix_value) |
| 1983 | psyment->n_value = psyment->n_value - |
| 1984 | (bfd_hostptr_t) obj_raw_syments (abfd); |
| 1985 | |
| 1986 | /* FIXME: We should handle fix_line here. */ |
| 1987 | |
| 1988 | return TRUE; |
| 1989 | } |
| 1990 | |
| 1991 | /* Return the COFF auxent for a symbol. */ |
| 1992 | |
| 1993 | bfd_boolean |
| 1994 | bfd_coff_get_auxent (bfd *abfd, |
| 1995 | asymbol *symbol, |
| 1996 | int indx, |
| 1997 | union internal_auxent *pauxent) |
| 1998 | { |
| 1999 | coff_symbol_type *csym; |
| 2000 | combined_entry_type *ent; |
| 2001 | |
| 2002 | csym = coff_symbol_from (abfd, symbol); |
| 2003 | |
| 2004 | if (csym == NULL |
| 2005 | || csym->native == NULL |
| 2006 | || indx >= csym->native->u.syment.n_numaux) |
| 2007 | { |
| 2008 | bfd_set_error (bfd_error_invalid_operation); |
| 2009 | return FALSE; |
| 2010 | } |
| 2011 | |
| 2012 | ent = csym->native + indx + 1; |
| 2013 | |
| 2014 | *pauxent = ent->u.auxent; |
| 2015 | |
| 2016 | if (ent->fix_tag) |
| 2017 | pauxent->x_sym.x_tagndx.l = |
| 2018 | ((combined_entry_type *) pauxent->x_sym.x_tagndx.p |
| 2019 | - obj_raw_syments (abfd)); |
| 2020 | |
| 2021 | if (ent->fix_end) |
| 2022 | pauxent->x_sym.x_fcnary.x_fcn.x_endndx.l = |
| 2023 | ((combined_entry_type *) pauxent->x_sym.x_fcnary.x_fcn.x_endndx.p |
| 2024 | - obj_raw_syments (abfd)); |
| 2025 | |
| 2026 | if (ent->fix_scnlen) |
| 2027 | pauxent->x_csect.x_scnlen.l = |
| 2028 | ((combined_entry_type *) pauxent->x_csect.x_scnlen.p |
| 2029 | - obj_raw_syments (abfd)); |
| 2030 | |
| 2031 | return TRUE; |
| 2032 | } |
| 2033 | |
| 2034 | /* Print out information about COFF symbol. */ |
| 2035 | |
| 2036 | void |
| 2037 | coff_print_symbol (bfd *abfd, |
| 2038 | void * filep, |
| 2039 | asymbol *symbol, |
| 2040 | bfd_print_symbol_type how) |
| 2041 | { |
| 2042 | FILE * file = (FILE *) filep; |
| 2043 | |
| 2044 | switch (how) |
| 2045 | { |
| 2046 | case bfd_print_symbol_name: |
| 2047 | fprintf (file, "%s", symbol->name); |
| 2048 | break; |
| 2049 | |
| 2050 | case bfd_print_symbol_more: |
| 2051 | fprintf (file, "coff %s %s", |
| 2052 | coffsymbol (symbol)->native ? "n" : "g", |
| 2053 | coffsymbol (symbol)->lineno ? "l" : " "); |
| 2054 | break; |
| 2055 | |
| 2056 | case bfd_print_symbol_all: |
| 2057 | if (coffsymbol (symbol)->native) |
| 2058 | { |
| 2059 | bfd_vma val; |
| 2060 | unsigned int aux; |
| 2061 | combined_entry_type *combined = coffsymbol (symbol)->native; |
| 2062 | combined_entry_type *root = obj_raw_syments (abfd); |
| 2063 | struct lineno_cache_entry *l = coffsymbol (symbol)->lineno; |
| 2064 | |
| 2065 | fprintf (file, "[%3ld]", (long) (combined - root)); |
| 2066 | |
| 2067 | if (! combined->fix_value) |
| 2068 | val = (bfd_vma) combined->u.syment.n_value; |
| 2069 | else |
| 2070 | val = combined->u.syment.n_value - (bfd_hostptr_t) root; |
| 2071 | |
| 2072 | fprintf (file, "(sec %2d)(fl 0x%02x)(ty %3x)(scl %3d) (nx %d) 0x", |
| 2073 | combined->u.syment.n_scnum, |
| 2074 | combined->u.syment.n_flags, |
| 2075 | combined->u.syment.n_type, |
| 2076 | combined->u.syment.n_sclass, |
| 2077 | combined->u.syment.n_numaux); |
| 2078 | bfd_fprintf_vma (abfd, file, val); |
| 2079 | fprintf (file, " %s", symbol->name); |
| 2080 | |
| 2081 | for (aux = 0; aux < combined->u.syment.n_numaux; aux++) |
| 2082 | { |
| 2083 | combined_entry_type *auxp = combined + aux + 1; |
| 2084 | long tagndx; |
| 2085 | |
| 2086 | if (auxp->fix_tag) |
| 2087 | tagndx = auxp->u.auxent.x_sym.x_tagndx.p - root; |
| 2088 | else |
| 2089 | tagndx = auxp->u.auxent.x_sym.x_tagndx.l; |
| 2090 | |
| 2091 | fprintf (file, "\n"); |
| 2092 | |
| 2093 | if (bfd_coff_print_aux (abfd, file, root, combined, auxp, aux)) |
| 2094 | continue; |
| 2095 | |
| 2096 | switch (combined->u.syment.n_sclass) |
| 2097 | { |
| 2098 | case C_FILE: |
| 2099 | fprintf (file, "File "); |
| 2100 | break; |
| 2101 | |
| 2102 | case C_STAT: |
| 2103 | if (combined->u.syment.n_type == T_NULL) |
| 2104 | /* Probably a section symbol ? */ |
| 2105 | { |
| 2106 | fprintf (file, "AUX scnlen 0x%lx nreloc %d nlnno %d", |
| 2107 | (unsigned long) auxp->u.auxent.x_scn.x_scnlen, |
| 2108 | auxp->u.auxent.x_scn.x_nreloc, |
| 2109 | auxp->u.auxent.x_scn.x_nlinno); |
| 2110 | if (auxp->u.auxent.x_scn.x_checksum != 0 |
| 2111 | || auxp->u.auxent.x_scn.x_associated != 0 |
| 2112 | || auxp->u.auxent.x_scn.x_comdat != 0) |
| 2113 | fprintf (file, " checksum 0x%lx assoc %d comdat %d", |
| 2114 | auxp->u.auxent.x_scn.x_checksum, |
| 2115 | auxp->u.auxent.x_scn.x_associated, |
| 2116 | auxp->u.auxent.x_scn.x_comdat); |
| 2117 | break; |
| 2118 | } |
| 2119 | /* Otherwise fall through. */ |
| 2120 | case C_EXT: |
| 2121 | case C_AIX_WEAKEXT: |
| 2122 | if (ISFCN (combined->u.syment.n_type)) |
| 2123 | { |
| 2124 | long next, llnos; |
| 2125 | |
| 2126 | if (auxp->fix_end) |
| 2127 | next = (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p |
| 2128 | - root); |
| 2129 | else |
| 2130 | next = auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l; |
| 2131 | llnos = auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_lnnoptr; |
| 2132 | fprintf (file, |
| 2133 | "AUX tagndx %ld ttlsiz 0x%lx lnnos %ld next %ld", |
| 2134 | tagndx, |
| 2135 | (unsigned long) auxp->u.auxent.x_sym.x_misc.x_fsize, |
| 2136 | llnos, next); |
| 2137 | break; |
| 2138 | } |
| 2139 | /* Otherwise fall through. */ |
| 2140 | default: |
| 2141 | fprintf (file, "AUX lnno %d size 0x%x tagndx %ld", |
| 2142 | auxp->u.auxent.x_sym.x_misc.x_lnsz.x_lnno, |
| 2143 | auxp->u.auxent.x_sym.x_misc.x_lnsz.x_size, |
| 2144 | tagndx); |
| 2145 | if (auxp->fix_end) |
| 2146 | fprintf (file, " endndx %ld", |
| 2147 | ((long) |
| 2148 | (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p |
| 2149 | - root))); |
| 2150 | break; |
| 2151 | } |
| 2152 | } |
| 2153 | |
| 2154 | if (l) |
| 2155 | { |
| 2156 | fprintf (file, "\n%s :", l->u.sym->name); |
| 2157 | l++; |
| 2158 | while (l->line_number) |
| 2159 | { |
| 2160 | fprintf (file, "\n%4d : ", l->line_number); |
| 2161 | bfd_fprintf_vma (abfd, file, l->u.offset + symbol->section->vma); |
| 2162 | l++; |
| 2163 | } |
| 2164 | } |
| 2165 | } |
| 2166 | else |
| 2167 | { |
| 2168 | bfd_print_symbol_vandf (abfd, (void *) file, symbol); |
| 2169 | fprintf (file, " %-5s %s %s %s", |
| 2170 | symbol->section->name, |
| 2171 | coffsymbol (symbol)->native ? "n" : "g", |
| 2172 | coffsymbol (symbol)->lineno ? "l" : " ", |
| 2173 | symbol->name); |
| 2174 | } |
| 2175 | } |
| 2176 | } |
| 2177 | |
| 2178 | /* Return whether a symbol name implies a local symbol. In COFF, |
| 2179 | local symbols generally start with ``.L''. Most targets use this |
| 2180 | function for the is_local_label_name entry point, but some may |
| 2181 | override it. */ |
| 2182 | |
| 2183 | bfd_boolean |
| 2184 | _bfd_coff_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED, |
| 2185 | const char *name) |
| 2186 | { |
| 2187 | return name[0] == '.' && name[1] == 'L'; |
| 2188 | } |
| 2189 | |
| 2190 | /* Provided a BFD, a section and an offset (in bytes, not octets) into the |
| 2191 | section, calculate and return the name of the source file and the line |
| 2192 | nearest to the wanted location. */ |
| 2193 | |
| 2194 | bfd_boolean |
| 2195 | coff_find_nearest_line_with_names (bfd *abfd, |
| 2196 | const struct dwarf_debug_section *debug_sections, |
| 2197 | asection *section, |
| 2198 | asymbol **symbols, |
| 2199 | bfd_vma offset, |
| 2200 | const char **filename_ptr, |
| 2201 | const char **functionname_ptr, |
| 2202 | unsigned int *line_ptr) |
| 2203 | { |
| 2204 | bfd_boolean found; |
| 2205 | unsigned int i; |
| 2206 | unsigned int line_base; |
| 2207 | coff_data_type *cof = coff_data (abfd); |
| 2208 | /* Run through the raw syments if available. */ |
| 2209 | combined_entry_type *p; |
| 2210 | combined_entry_type *pend; |
| 2211 | alent *l; |
| 2212 | struct coff_section_tdata *sec_data; |
| 2213 | bfd_size_type amt; |
| 2214 | |
| 2215 | /* Before looking through the symbol table, try to use a .stab |
| 2216 | section to find the information. */ |
| 2217 | if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset, |
| 2218 | &found, filename_ptr, |
| 2219 | functionname_ptr, line_ptr, |
| 2220 | &coff_data(abfd)->line_info)) |
| 2221 | return FALSE; |
| 2222 | |
| 2223 | if (found) |
| 2224 | return TRUE; |
| 2225 | |
| 2226 | /* Also try examining DWARF2 debugging information. */ |
| 2227 | if (_bfd_dwarf2_find_nearest_line (abfd, debug_sections, |
| 2228 | section, symbols, offset, |
| 2229 | filename_ptr, functionname_ptr, |
| 2230 | line_ptr, NULL, 0, |
| 2231 | &coff_data(abfd)->dwarf2_find_line_info)) |
| 2232 | return TRUE; |
| 2233 | |
| 2234 | *filename_ptr = 0; |
| 2235 | *functionname_ptr = 0; |
| 2236 | *line_ptr = 0; |
| 2237 | |
| 2238 | /* Don't try and find line numbers in a non coff file. */ |
| 2239 | if (!bfd_family_coff (abfd)) |
| 2240 | return FALSE; |
| 2241 | |
| 2242 | if (cof == NULL) |
| 2243 | return FALSE; |
| 2244 | |
| 2245 | /* Find the first C_FILE symbol. */ |
| 2246 | p = cof->raw_syments; |
| 2247 | if (!p) |
| 2248 | return FALSE; |
| 2249 | |
| 2250 | pend = p + cof->raw_syment_count; |
| 2251 | while (p < pend) |
| 2252 | { |
| 2253 | if (p->u.syment.n_sclass == C_FILE) |
| 2254 | break; |
| 2255 | p += 1 + p->u.syment.n_numaux; |
| 2256 | } |
| 2257 | |
| 2258 | if (p < pend) |
| 2259 | { |
| 2260 | bfd_vma sec_vma; |
| 2261 | bfd_vma maxdiff; |
| 2262 | |
| 2263 | /* Look through the C_FILE symbols to find the best one. */ |
| 2264 | sec_vma = bfd_get_section_vma (abfd, section); |
| 2265 | *filename_ptr = (char *) p->u.syment._n._n_n._n_offset; |
| 2266 | maxdiff = (bfd_vma) 0 - (bfd_vma) 1; |
| 2267 | while (1) |
| 2268 | { |
| 2269 | bfd_vma file_addr; |
| 2270 | combined_entry_type *p2; |
| 2271 | |
| 2272 | for (p2 = p + 1 + p->u.syment.n_numaux; |
| 2273 | p2 < pend; |
| 2274 | p2 += 1 + p2->u.syment.n_numaux) |
| 2275 | { |
| 2276 | if (p2->u.syment.n_scnum > 0 |
| 2277 | && (section |
| 2278 | == coff_section_from_bfd_index (abfd, |
| 2279 | p2->u.syment.n_scnum))) |
| 2280 | break; |
| 2281 | if (p2->u.syment.n_sclass == C_FILE) |
| 2282 | { |
| 2283 | p2 = pend; |
| 2284 | break; |
| 2285 | } |
| 2286 | } |
| 2287 | |
| 2288 | file_addr = (bfd_vma) p2->u.syment.n_value; |
| 2289 | /* PR 11512: Include the section address of the function name symbol. */ |
| 2290 | if (p2->u.syment.n_scnum > 0) |
| 2291 | file_addr += coff_section_from_bfd_index (abfd, |
| 2292 | p2->u.syment.n_scnum)->vma; |
| 2293 | /* We use <= MAXDIFF here so that if we get a zero length |
| 2294 | file, we actually use the next file entry. */ |
| 2295 | if (p2 < pend |
| 2296 | && offset + sec_vma >= file_addr |
| 2297 | && offset + sec_vma - file_addr <= maxdiff) |
| 2298 | { |
| 2299 | *filename_ptr = (char *) p->u.syment._n._n_n._n_offset; |
| 2300 | maxdiff = offset + sec_vma - p2->u.syment.n_value; |
| 2301 | } |
| 2302 | |
| 2303 | /* Avoid endless loops on erroneous files by ensuring that |
| 2304 | we always move forward in the file. */ |
| 2305 | if (p >= cof->raw_syments + p->u.syment.n_value) |
| 2306 | break; |
| 2307 | |
| 2308 | p = cof->raw_syments + p->u.syment.n_value; |
| 2309 | if (p > pend || p->u.syment.n_sclass != C_FILE) |
| 2310 | break; |
| 2311 | } |
| 2312 | } |
| 2313 | |
| 2314 | /* Now wander though the raw linenumbers of the section. */ |
| 2315 | /* If we have been called on this section before, and the offset we |
| 2316 | want is further down then we can prime the lookup loop. */ |
| 2317 | sec_data = coff_section_data (abfd, section); |
| 2318 | if (sec_data != NULL |
| 2319 | && sec_data->i > 0 |
| 2320 | && offset >= sec_data->offset) |
| 2321 | { |
| 2322 | i = sec_data->i; |
| 2323 | *functionname_ptr = sec_data->function; |
| 2324 | line_base = sec_data->line_base; |
| 2325 | } |
| 2326 | else |
| 2327 | { |
| 2328 | i = 0; |
| 2329 | line_base = 0; |
| 2330 | } |
| 2331 | |
| 2332 | if (section->lineno != NULL) |
| 2333 | { |
| 2334 | bfd_vma last_value = 0; |
| 2335 | |
| 2336 | l = §ion->lineno[i]; |
| 2337 | |
| 2338 | for (; i < section->lineno_count; i++) |
| 2339 | { |
| 2340 | if (l->line_number == 0) |
| 2341 | { |
| 2342 | /* Get the symbol this line number points at. */ |
| 2343 | coff_symbol_type *coff = (coff_symbol_type *) (l->u.sym); |
| 2344 | if (coff->symbol.value > offset) |
| 2345 | break; |
| 2346 | *functionname_ptr = coff->symbol.name; |
| 2347 | last_value = coff->symbol.value; |
| 2348 | if (coff->native) |
| 2349 | { |
| 2350 | combined_entry_type *s = coff->native; |
| 2351 | s = s + 1 + s->u.syment.n_numaux; |
| 2352 | |
| 2353 | /* In XCOFF a debugging symbol can follow the |
| 2354 | function symbol. */ |
| 2355 | if (s->u.syment.n_scnum == N_DEBUG) |
| 2356 | s = s + 1 + s->u.syment.n_numaux; |
| 2357 | |
| 2358 | /* S should now point to the .bf of the function. */ |
| 2359 | if (s->u.syment.n_numaux) |
| 2360 | { |
| 2361 | /* The linenumber is stored in the auxent. */ |
| 2362 | union internal_auxent *a = &((s + 1)->u.auxent); |
| 2363 | line_base = a->x_sym.x_misc.x_lnsz.x_lnno; |
| 2364 | *line_ptr = line_base; |
| 2365 | } |
| 2366 | } |
| 2367 | } |
| 2368 | else |
| 2369 | { |
| 2370 | if (l->u.offset > offset) |
| 2371 | break; |
| 2372 | *line_ptr = l->line_number + line_base - 1; |
| 2373 | } |
| 2374 | l++; |
| 2375 | } |
| 2376 | |
| 2377 | /* If we fell off the end of the loop, then assume that this |
| 2378 | symbol has no line number info. Otherwise, symbols with no |
| 2379 | line number info get reported with the line number of the |
| 2380 | last line of the last symbol which does have line number |
| 2381 | info. We use 0x100 as a slop to account for cases where the |
| 2382 | last line has executable code. */ |
| 2383 | if (i >= section->lineno_count |
| 2384 | && last_value != 0 |
| 2385 | && offset - last_value > 0x100) |
| 2386 | { |
| 2387 | *functionname_ptr = NULL; |
| 2388 | *line_ptr = 0; |
| 2389 | } |
| 2390 | } |
| 2391 | |
| 2392 | /* Cache the results for the next call. */ |
| 2393 | if (sec_data == NULL && section->owner == abfd) |
| 2394 | { |
| 2395 | amt = sizeof (struct coff_section_tdata); |
| 2396 | section->used_by_bfd = bfd_zalloc (abfd, amt); |
| 2397 | sec_data = (struct coff_section_tdata *) section->used_by_bfd; |
| 2398 | } |
| 2399 | if (sec_data != NULL) |
| 2400 | { |
| 2401 | sec_data->offset = offset; |
| 2402 | sec_data->i = i - 1; |
| 2403 | sec_data->function = *functionname_ptr; |
| 2404 | sec_data->line_base = line_base; |
| 2405 | } |
| 2406 | |
| 2407 | return TRUE; |
| 2408 | } |
| 2409 | |
| 2410 | bfd_boolean |
| 2411 | coff_find_nearest_line (bfd *abfd, |
| 2412 | asection *section, |
| 2413 | asymbol **symbols, |
| 2414 | bfd_vma offset, |
| 2415 | const char **filename_ptr, |
| 2416 | const char **functionname_ptr, |
| 2417 | unsigned int *line_ptr) |
| 2418 | { |
| 2419 | return coff_find_nearest_line_with_names (abfd, dwarf_debug_sections, |
| 2420 | section, symbols, offset, |
| 2421 | filename_ptr, functionname_ptr, |
| 2422 | line_ptr); |
| 2423 | } |
| 2424 | |
| 2425 | bfd_boolean |
| 2426 | coff_find_nearest_line_discriminator (bfd *abfd, |
| 2427 | asection *section, |
| 2428 | asymbol **symbols, |
| 2429 | bfd_vma offset, |
| 2430 | const char **filename_ptr, |
| 2431 | const char **functionname_ptr, |
| 2432 | unsigned int *line_ptr, |
| 2433 | unsigned int *discriminator) |
| 2434 | { |
| 2435 | *discriminator = 0; |
| 2436 | return coff_find_nearest_line_with_names (abfd, dwarf_debug_sections, |
| 2437 | section, symbols, offset, |
| 2438 | filename_ptr, functionname_ptr, |
| 2439 | line_ptr); |
| 2440 | } |
| 2441 | |
| 2442 | |
| 2443 | bfd_boolean |
| 2444 | coff_find_inliner_info (bfd *abfd, |
| 2445 | const char **filename_ptr, |
| 2446 | const char **functionname_ptr, |
| 2447 | unsigned int *line_ptr) |
| 2448 | { |
| 2449 | bfd_boolean found; |
| 2450 | |
| 2451 | found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr, |
| 2452 | functionname_ptr, line_ptr, |
| 2453 | &coff_data(abfd)->dwarf2_find_line_info); |
| 2454 | return (found); |
| 2455 | } |
| 2456 | |
| 2457 | int |
| 2458 | coff_sizeof_headers (bfd *abfd, struct bfd_link_info *info) |
| 2459 | { |
| 2460 | size_t size; |
| 2461 | |
| 2462 | if (!info->relocatable) |
| 2463 | size = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd); |
| 2464 | else |
| 2465 | size = bfd_coff_filhsz (abfd); |
| 2466 | |
| 2467 | size += abfd->section_count * bfd_coff_scnhsz (abfd); |
| 2468 | return size; |
| 2469 | } |
| 2470 | |
| 2471 | /* Change the class of a coff symbol held by BFD. */ |
| 2472 | |
| 2473 | bfd_boolean |
| 2474 | bfd_coff_set_symbol_class (bfd * abfd, |
| 2475 | asymbol * symbol, |
| 2476 | unsigned int symbol_class) |
| 2477 | { |
| 2478 | coff_symbol_type * csym; |
| 2479 | |
| 2480 | csym = coff_symbol_from (abfd, symbol); |
| 2481 | if (csym == NULL) |
| 2482 | { |
| 2483 | bfd_set_error (bfd_error_invalid_operation); |
| 2484 | return FALSE; |
| 2485 | } |
| 2486 | else if (csym->native == NULL) |
| 2487 | { |
| 2488 | /* This is an alien symbol which no native coff backend data. |
| 2489 | We cheat here by creating a fake native entry for it and |
| 2490 | then filling in the class. This code is based on that in |
| 2491 | coff_write_alien_symbol(). */ |
| 2492 | |
| 2493 | combined_entry_type * native; |
| 2494 | bfd_size_type amt = sizeof (* native); |
| 2495 | |
| 2496 | native = (combined_entry_type *) bfd_zalloc (abfd, amt); |
| 2497 | if (native == NULL) |
| 2498 | return FALSE; |
| 2499 | |
| 2500 | native->u.syment.n_type = T_NULL; |
| 2501 | native->u.syment.n_sclass = symbol_class; |
| 2502 | |
| 2503 | if (bfd_is_und_section (symbol->section)) |
| 2504 | { |
| 2505 | native->u.syment.n_scnum = N_UNDEF; |
| 2506 | native->u.syment.n_value = symbol->value; |
| 2507 | } |
| 2508 | else if (bfd_is_com_section (symbol->section)) |
| 2509 | { |
| 2510 | native->u.syment.n_scnum = N_UNDEF; |
| 2511 | native->u.syment.n_value = symbol->value; |
| 2512 | } |
| 2513 | else |
| 2514 | { |
| 2515 | native->u.syment.n_scnum = |
| 2516 | symbol->section->output_section->target_index; |
| 2517 | native->u.syment.n_value = (symbol->value |
| 2518 | + symbol->section->output_offset); |
| 2519 | if (! obj_pe (abfd)) |
| 2520 | native->u.syment.n_value += symbol->section->output_section->vma; |
| 2521 | |
| 2522 | /* Copy the any flags from the file header into the symbol. |
| 2523 | FIXME: Why? */ |
| 2524 | native->u.syment.n_flags = bfd_asymbol_bfd (& csym->symbol)->flags; |
| 2525 | } |
| 2526 | |
| 2527 | csym->native = native; |
| 2528 | } |
| 2529 | else |
| 2530 | csym->native->u.syment.n_sclass = symbol_class; |
| 2531 | |
| 2532 | return TRUE; |
| 2533 | } |
| 2534 | |
| 2535 | struct coff_comdat_info * |
| 2536 | bfd_coff_get_comdat_section (bfd *abfd, struct bfd_section *sec) |
| 2537 | { |
| 2538 | if (bfd_get_flavour (abfd) == bfd_target_coff_flavour |
| 2539 | && coff_section_data (abfd, sec) != NULL) |
| 2540 | return coff_section_data (abfd, sec)->comdat; |
| 2541 | else |
| 2542 | return NULL; |
| 2543 | } |
| 2544 | |
| 2545 | bfd_boolean |
| 2546 | _bfd_coff_section_already_linked (bfd *abfd, |
| 2547 | asection *sec, |
| 2548 | struct bfd_link_info *info) |
| 2549 | { |
| 2550 | flagword flags; |
| 2551 | const char *name, *key; |
| 2552 | struct bfd_section_already_linked *l; |
| 2553 | struct bfd_section_already_linked_hash_entry *already_linked_list; |
| 2554 | struct coff_comdat_info *s_comdat; |
| 2555 | |
| 2556 | flags = sec->flags; |
| 2557 | if ((flags & SEC_LINK_ONCE) == 0) |
| 2558 | return FALSE; |
| 2559 | |
| 2560 | /* The COFF backend linker doesn't support group sections. */ |
| 2561 | if ((flags & SEC_GROUP) != 0) |
| 2562 | return FALSE; |
| 2563 | |
| 2564 | name = bfd_get_section_name (abfd, sec); |
| 2565 | s_comdat = bfd_coff_get_comdat_section (abfd, sec); |
| 2566 | |
| 2567 | if (s_comdat != NULL) |
| 2568 | key = s_comdat->name; |
| 2569 | else |
| 2570 | { |
| 2571 | if (CONST_STRNEQ (name, ".gnu.linkonce.") |
| 2572 | && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL) |
| 2573 | key++; |
| 2574 | else |
| 2575 | /* FIXME: gcc as of 2011-09 emits sections like .text$<key>, |
| 2576 | .xdata$<key> and .pdata$<key> only the first of which has a |
| 2577 | comdat key. Should these all match the LTO IR key? */ |
| 2578 | key = name; |
| 2579 | } |
| 2580 | |
| 2581 | already_linked_list = bfd_section_already_linked_table_lookup (key); |
| 2582 | |
| 2583 | for (l = already_linked_list->entry; l != NULL; l = l->next) |
| 2584 | { |
| 2585 | struct coff_comdat_info *l_comdat; |
| 2586 | |
| 2587 | l_comdat = bfd_coff_get_comdat_section (l->sec->owner, l->sec); |
| 2588 | |
| 2589 | /* The section names must match, and both sections must be |
| 2590 | comdat and have the same comdat name, or both sections must |
| 2591 | be non-comdat. LTO IR plugin sections are an exception. They |
| 2592 | are always named .gnu.linkonce.t.<key> (<key> is some string) |
| 2593 | and match any comdat section with comdat name of <key>, and |
| 2594 | any linkonce section with the same suffix, ie. |
| 2595 | .gnu.linkonce.*.<key>. */ |
| 2596 | if (((s_comdat != NULL) == (l_comdat != NULL) |
| 2597 | && strcmp (name, l->sec->name) == 0) |
| 2598 | || (l->sec->owner->flags & BFD_PLUGIN) != 0) |
| 2599 | { |
| 2600 | /* The section has already been linked. See if we should |
| 2601 | issue a warning. */ |
| 2602 | return _bfd_handle_already_linked (sec, l, info); |
| 2603 | } |
| 2604 | } |
| 2605 | |
| 2606 | /* This is the first section with this name. Record it. */ |
| 2607 | if (!bfd_section_already_linked_table_insert (already_linked_list, sec)) |
| 2608 | info->callbacks->einfo (_("%F%P: already_linked_table: %E\n")); |
| 2609 | return FALSE; |
| 2610 | } |