
1: /* Support for the generic parts of most COFF variants, for BFD. 2: Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 3: 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 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, 25: sac@cygnus.com. */ 26: /* 27: SECTION 28: coff backends 29: 30: BFD supports a number of different flavours of coff format. 31: The major differences between formats are the sizes and 32: alignments of fields in structures on disk, and the occasional 33: extra field. 34: 35: Coff in all its varieties is implemented with a few common 36: files and a number of implementation specific files. For 37: example, The 88k bcs coff format is implemented in the file 38: @file{coff-m88k.c}. This file @code{#include}s 39: @file{coff/m88k.h} which defines the external structure of the 40: coff format for the 88k, and @file{coff/internal.h} which 41: defines the internal structure. @file{coff-m88k.c} also 42: defines the relocations used by the 88k format 43: @xref{Relocations}. 44: 45: The Intel i960 processor version of coff is implemented in 46: @file{coff-i960.c}. This file has the same structure as 47: @file{coff-m88k.c}, except that it includes @file{coff/i960.h} 48: rather than @file{coff-m88k.h}. 49: 50: SUBSECTION 51: Porting to a new version of coff 52: 53: The recommended method is to select from the existing 54: implementations the version of coff which is most like the one 55: you want to use. For example, we'll say that i386 coff is 56: the one you select, and that your coff flavour is called foo. 57: Copy @file{i386coff.c} to @file{foocoff.c}, copy 58: @file{../include/coff/i386.h} to @file{../include/coff/foo.h}, 59: and add the lines to @file{targets.c} and @file{Makefile.in} 60: so that your new back end is used. Alter the shapes of the 61: structures in @file{../include/coff/foo.h} so that they match 62: what you need. You will probably also have to add 63: @code{#ifdef}s to the code in @file{coff/internal.h} and 64: @file{coffcode.h} if your version of coff is too wild. 65: 66: You can verify that your new BFD backend works quite simply by 67: building @file{objdump} from the @file{binutils} directory, 68: and making sure that its version of what's going on and your 69: host system's idea (assuming it has the pretty standard coff 70: dump utility, usually called @code{att-dump} or just 71: @code{dump}) are the same. Then clean up your code, and send 72: what you've done to Cygnus. Then your stuff will be in the 73: next release, and you won't have to keep integrating it. 74: 75: SUBSECTION 76: How the coff backend works 77: 78: SUBSUBSECTION 79: File layout 80: 81: The Coff backend is split into generic routines that are 82: applicable to any Coff target and routines that are specific 83: to a particular target. The target-specific routines are 84: further split into ones which are basically the same for all 85: Coff targets except that they use the external symbol format 86: or use different values for certain constants. 87: 88: The generic routines are in @file{coffgen.c}. These routines 89: work for any Coff target. They use some hooks into the target 90: specific code; the hooks are in a @code{bfd_coff_backend_data} 91: structure, one of which exists for each target. 92: 93: The essentially similar target-specific routines are in 94: @file{coffcode.h}. This header file includes executable C code. 95: The various Coff targets first include the appropriate Coff 96: header file, make any special defines that are needed, and 97: then include @file{coffcode.h}. 98: 99: Some of the Coff targets then also have additional routines in 100: the target source file itself. 101: 102: For example, @file{coff-i960.c} includes 103: @file{coff/internal.h} and @file{coff/i960.h}. It then 104: defines a few constants, such as @code{I960}, and includes 105: @file{coffcode.h}. Since the i960 has complex relocation 106: types, @file{coff-i960.c} also includes some code to 107: manipulate the i960 relocs. This code is not in 108: @file{coffcode.h} because it would not be used by any other 109: target. 110: 111: SUBSUBSECTION 112: Bit twiddling 113: 114: Each flavour of coff supported in BFD has its own header file 115: describing the external layout of the structures. There is also 116: an internal description of the coff layout, in 117: @file{coff/internal.h}. A major function of the 118: coff backend is swapping the bytes and twiddling the bits to 119: translate the external form of the structures into the normal 120: internal form. This is all performed in the 121: @code{bfd_swap}_@i{thing}_@i{direction} routines. Some 122: elements are different sizes between different versions of 123: coff; it is the duty of the coff version specific include file 124: to override the definitions of various packing routines in 125: @file{coffcode.h}. E.g., the size of line number entry in coff is 126: sometimes 16 bits, and sometimes 32 bits. @code{#define}ing 127: @code{PUT_LNSZ_LNNO} and @code{GET_LNSZ_LNNO} will select the 128: correct one. No doubt, some day someone will find a version of 129: coff which has a varying field size not catered to at the 130: moment. To port BFD, that person will have to add more @code{#defines}. 131: Three of the bit twiddling routines are exported to 132: @code{gdb}; @code{coff_swap_aux_in}, @code{coff_swap_sym_in} 133: and @code{coff_swap_lineno_in}. @code{GDB} reads the symbol 134: table on its own, but uses BFD to fix things up. More of the 135: bit twiddlers are exported for @code{gas}; 136: @code{coff_swap_aux_out}, @code{coff_swap_sym_out}, 137: @code{coff_swap_lineno_out}, @code{coff_swap_reloc_out}, 138: @code{coff_swap_filehdr_out}, @code{coff_swap_aouthdr_out}, 139: @code{coff_swap_scnhdr_out}. @code{Gas} currently keeps track 140: of all the symbol table and reloc drudgery itself, thereby 141: saving the internal BFD overhead, but uses BFD to swap things 142: on the way out, making cross ports much safer. Doing so also 143: allows BFD (and thus the linker) to use the same header files 144: as @code{gas}, which makes one avenue to disaster disappear. 145: 146: SUBSUBSECTION 147: Symbol reading 148: 149: The simple canonical form for symbols used by BFD is not rich 150: enough to keep all the information available in a coff symbol 151: table. The back end gets around this problem by keeping the original 152: symbol table around, "behind the scenes". 153: 154: When a symbol table is requested (through a call to 155: @code{bfd_canonicalize_symtab}), a request gets through to 156: @code{coff_get_normalized_symtab}. This reads the symbol table from 157: the coff file and swaps all the structures inside into the 158: internal form. It also fixes up all the pointers in the table 159: (represented in the file by offsets from the first symbol in 160: the table) into physical pointers to elements in the new 161: internal table. This involves some work since the meanings of 162: fields change depending upon context: a field that is a 163: pointer to another structure in the symbol table at one moment 164: may be the size in bytes of a structure at the next. Another 165: pass is made over the table. All symbols which mark file names 166: (<<C_FILE>> symbols) are modified so that the internal 167: string points to the value in the auxent (the real filename) 168: rather than the normal text associated with the symbol 169: (@code{".file"}). 170: 171: At this time the symbol names are moved around. Coff stores 172: all symbols less than nine characters long physically 173: within the symbol table; longer strings are kept at the end of 174: the file in the string table. This pass moves all strings 175: into memory and replaces them with pointers to the strings. 176: 177: The symbol table is massaged once again, this time to create 178: the canonical table used by the BFD application. Each symbol 179: is inspected in turn, and a decision made (using the 180: @code{sclass} field) about the various flags to set in the 181: @code{asymbol}. @xref{Symbols}. The generated canonical table 182: shares strings with the hidden internal symbol table. 183: 184: Any linenumbers are read from the coff file too, and attached 185: to the symbols which own the functions the linenumbers belong to. 186: 187: SUBSUBSECTION 188: Symbol writing 189: 190: Writing a symbol to a coff file which didn't come from a coff 191: file will lose any debugging information. The @code{asymbol} 192: structure remembers the BFD from which the symbol was taken, and on 193: output the back end makes sure that the same destination target as 194: source target is present. 195: 196: When the symbols have come from a coff file then all the 197: debugging information is preserved. 198: 199: Symbol tables are provided for writing to the back end in a 200: vector of pointers to pointers. This allows applications like 201: the linker to accumulate and output large symbol tables 202: without having to do too much byte copying. 203: 204: This function runs through the provided symbol table and 205: patches each symbol marked as a file place holder 206: (@code{C_FILE}) to point to the next file place holder in the 207: list. It also marks each @code{offset} field in the list with 208: the offset from the first symbol of the current symbol. 209: 210: Another function of this procedure is to turn the canonical 211: value form of BFD into the form used by coff. Internally, BFD 212: expects symbol values to be offsets from a section base; so a 213: symbol physically at 0x120, but in a section starting at 214: 0x100, would have the value 0x20. Coff expects symbols to 215: contain their final value, so symbols have their values 216: changed at this point to reflect their sum with their owning 217: section. This transformation uses the 218: <<output_section>> field of the @code{asymbol}'s 219: @code{asection} @xref{Sections}. 220: 221: o <<coff_mangle_symbols>> 222: 223: This routine runs though the provided symbol table and uses 224: the offsets generated by the previous pass and the pointers 225: generated when the symbol table was read in to create the 226: structured hierarchy required by coff. It changes each pointer 227: to a symbol into the index into the symbol table of the asymbol. 228: 229: o <<coff_write_symbols>> 230: 231: This routine runs through the symbol table and patches up the 232: symbols from their internal form into the coff way, calls the 233: bit twiddlers, and writes out the table to the file. 234: 235: */ 236: 237: /* 238: INTERNAL_DEFINITION 239: coff_symbol_type 240: 241: DESCRIPTION 242: The hidden information for an <<asymbol>> is described in a 243: <<combined_entry_type>>: 244: 245: CODE_FRAGMENT 246: . 247: .typedef struct coff_ptr_struct 248: .{ 249: . {* Remembers the offset from the first symbol in the file for 250: . this symbol. Generated by coff_renumber_symbols. *} 251: . unsigned int offset; 252: . 253: . {* Should the value of this symbol be renumbered. Used for 254: . XCOFF C_BSTAT symbols. Set by coff_slurp_symbol_table. *} 255: . unsigned int fix_value : 1; 256: . 257: . {* Should the tag field of this symbol be renumbered. 258: . Created by coff_pointerize_aux. *} 259: . unsigned int fix_tag : 1; 260: . 261: . {* Should the endidx field of this symbol be renumbered. 262: . Created by coff_pointerize_aux. *} 263: . unsigned int fix_end : 1; 264: . 265: . {* Should the x_csect.x_scnlen field be renumbered. 266: . Created by coff_pointerize_aux. *} 267: . unsigned int fix_scnlen : 1; 268: . 269: . {* Fix up an XCOFF C_BINCL/C_EINCL symbol. The value is the 270: . index into the line number entries. Set by coff_slurp_symbol_table. *} 271: . unsigned int fix_line : 1; 272: . 273: . {* The container for the symbol structure as read and translated 274: . from the file. *} 275: . union 276: . { 277: . union internal_auxent auxent; 278: . struct internal_syment syment; 279: . } u; 280: .} combined_entry_type; 281: . 282: . 283: .{* Each canonical asymbol really looks like this: *} 284: . 285: .typedef struct coff_symbol_struct 286: .{ 287: . {* The actual symbol which the rest of BFD works with *} 288: . asymbol symbol; 289: . 290: . {* A pointer to the hidden information for this symbol *} 291: . combined_entry_type *native; 292: . 293: . {* A pointer to the linenumber information for this symbol *} 294: . struct lineno_cache_entry *lineno; 295: . 296: . {* Have the line numbers been relocated yet ? *} 297: . bfd_boolean done_lineno; 298: .} coff_symbol_type; 299: 300: */ 301: 302: #ifdef COFF_WITH_PE 303: #include "peicode.h" 304: #else 305: #include "coffswap.h" 306: #endif 307: 308: #define STRING_SIZE_SIZE 4 309: 310: #define DOT_DEBUG ".debug" 311: #define GNU_LINKONCE_WI ".gnu.linkonce.wi." 312: 313: static long sec_to_styp_flags 314: (const char *, flagword); 315: static bfd_boolean styp_to_sec_flags 316: (bfd *, void *, const char *, asection *, flagword *); 317: static bfd_boolean coff_bad_format_hook 318: (bfd *, void *); 319: static void coff_set_custom_section_alignment 320: (bfd *, asection *, const struct coff_section_alignment_entry *, 321: const unsigned int); 322: static bfd_boolean coff_new_section_hook 323: (bfd *, asection *); 324: static bfd_boolean coff_set_arch_mach_hook 325: (bfd *, void *); 326: static bfd_boolean coff_write_relocs 327: (bfd *, int); 328: static bfd_boolean coff_set_flags 329: (bfd *, unsigned int *, unsigned short *); 330: static bfd_boolean coff_set_arch_mach 331: (bfd *, enum bfd_architecture, unsigned long) ATTRIBUTE_UNUSED; 332: static bfd_boolean coff_compute_section_file_positions 333: (bfd *); 334: static bfd_boolean coff_write_object_contents 335: (bfd *) ATTRIBUTE_UNUSED; 336: static bfd_boolean coff_set_section_contents 337: (bfd *, asection *, const void *, file_ptr, bfd_size_type); 338: static void * buy_and_read 339: (bfd *, file_ptr, bfd_size_type); 340: static bfd_boolean coff_slurp_line_table 341: (bfd *, asection *); 342: static bfd_boolean coff_slurp_symbol_table 343: (bfd *); 344: static enum coff_symbol_classification coff_classify_symbol 345: (bfd *, struct internal_syment *); 346: static bfd_boolean coff_slurp_reloc_table 347: (bfd *, asection *, asymbol **); 348: static long coff_canonicalize_reloc 349: (bfd *, asection *, arelent **, asymbol **); 350: #ifndef coff_mkobject_hook 351: static void * coff_mkobject_hook 352: (bfd *, void *, void *); 353: #endif 354: #ifdef COFF_WITH_PE 355: static flagword handle_COMDAT 356: (bfd *, flagword, void *, const char *, asection *); 357: #endif 358: #ifdef COFF_IMAGE_WITH_PE 359: static bfd_boolean coff_read_word 360: (bfd *, unsigned int *); 361: static unsigned int coff_compute_checksum 362: (bfd *); 363: static bfd_boolean coff_apply_checksum 364: (bfd *); 365: #endif 366: #ifdef TICOFF 367: static bfd_boolean ticoff0_bad_format_hook 368: (bfd *, void * ); 369: static bfd_boolean ticoff1_bad_format_hook 370: (bfd *, void * ); 371: #endif 372: ^L 373: /* void warning(); */ 374: 375: /* Return a word with STYP_* (scnhdr.s_flags) flags set to represent 376: the incoming SEC_* flags. The inverse of this function is 377: styp_to_sec_flags(). NOTE: If you add to/change this routine, you 378: should probably mirror the changes in styp_to_sec_flags(). */ 379: 380: #ifndef COFF_WITH_PE 381: 382: /* Macros for setting debugging flags. */ 383: 384: #ifdef STYP_DEBUG 385: #define STYP_XCOFF_DEBUG STYP_DEBUG 386: #else 387: #define STYP_XCOFF_DEBUG STYP_INFO 388: #endif 389: 390: #ifdef COFF_ALIGN_IN_S_FLAGS 391: #define STYP_DEBUG_INFO STYP_DSECT 392: #else 393: #define STYP_DEBUG_INFO STYP_INFO 394: #endif 395: 396: static long 397: sec_to_styp_flags (const char *sec_name, flagword sec_flags) 398: { 399: long styp_flags = 0; 400: 401: if (!strcmp (sec_name, _TEXT)) 402: { 403: styp_flags = STYP_TEXT; 404: } 405: else if (!strcmp (sec_name, _DATA)) 406: { 407: styp_flags = STYP_DATA; 408: } 409: else if (!strcmp (sec_name, _BSS)) 410: { 411: styp_flags = STYP_BSS; 412: #ifdef _COMMENT 413: } 414: else if (!strcmp (sec_name, _COMMENT)) 415: { 416: styp_flags = STYP_INFO; 417: #endif /* _COMMENT */ 418: #ifdef _LIB 419: } 420: else if (!strcmp (sec_name, _LIB)) 421: { 422: styp_flags = STYP_LIB; 423: #endif /* _LIB */ 424: #ifdef _LIT 425: } 426: else if (!strcmp (sec_name, _LIT)) 427: { 428: styp_flags = STYP_LIT; 429: #endif /* _LIT */ 430: } 431: else if (CONST_STRNEQ (sec_name, DOT_DEBUG)) 432: { 433: /* Handle the XCOFF debug section and DWARF2 debug sections. */ 434: if (!sec_name[6]) 435: styp_flags = STYP_XCOFF_DEBUG; 436: else 437: styp_flags = STYP_DEBUG_INFO; 438: } 439: else if (CONST_STRNEQ (sec_name, ".stab")) 440: { 441: styp_flags = STYP_DEBUG_INFO; 442: } 443: #ifdef COFF_LONG_SECTION_NAMES 444: else if (CONST_STRNEQ (sec_name, GNU_LINKONCE_WI)) 445: { 446: styp_flags = STYP_DEBUG_INFO; 447: } 448: #endif 449: #ifdef RS6000COFF_C 450: else if (!strcmp (sec_name, _PAD)) 451: { 452: styp_flags = STYP_PAD; 453: } 454: else if (!strcmp (sec_name, _LOADER)) 455: { 456: styp_flags = STYP_LOADER; 457: } 458: else if (!strcmp (sec_name, _EXCEPT)) 459: { 460: styp_flags = STYP_EXCEPT; 461: } 462: else if (!strcmp (sec_name, _TYPCHK)) 463: { 464: styp_flags = STYP_TYPCHK; 465: } 466: #endif 467: /* Try and figure out what it should be */ 468: else if (sec_flags & SEC_CODE) 469: { 470: styp_flags = STYP_TEXT; 471: } 472: else if (sec_flags & SEC_DATA) 473: { 474: styp_flags = STYP_DATA; 475: } 476: else if (sec_flags & SEC_READONLY) 477: { 478: #ifdef STYP_LIT /* 29k readonly text/data section */ 479: styp_flags = STYP_LIT; 480: #else 481: styp_flags = STYP_TEXT; 482: #endif /* STYP_LIT */ 483: } 484: else if (sec_flags & SEC_LOAD) 485: { 486: styp_flags = STYP_TEXT; 487: } 488: else if (sec_flags & SEC_ALLOC) 489: { 490: styp_flags = STYP_BSS; 491: } 492: 493: #ifdef STYP_CLINK 494: if (sec_flags & SEC_TIC54X_CLINK) 495: styp_flags |= STYP_CLINK; 496: #endif 497: 498: #ifdef STYP_BLOCK 499: if (sec_flags & SEC_TIC54X_BLOCK) 500: styp_flags |= STYP_BLOCK; 501: #endif 502: 503: #ifdef STYP_NOLOAD 504: if ((sec_flags & (SEC_NEVER_LOAD | SEC_COFF_SHARED_LIBRARY)) != 0) 505: styp_flags |= STYP_NOLOAD; 506: #endif 507: 508: return styp_flags; 509: } 510: 511: #else /* COFF_WITH_PE */ 512: 513: /* The PE version; see above for the general comments. The non-PE 514: case seems to be more guessing, and breaks PE format; specifically, 515: .rdata is readonly, but it sure ain't text. Really, all this 516: should be set up properly in gas (or whatever assembler is in use), 517: and honor whatever objcopy/strip, etc. sent us as input. */ 518: 519: static long 520: sec_to_styp_flags (const char *sec_name, flagword sec_flags) 521: { 522: long styp_flags = 0; 523: 524: /* caution: there are at least three groups of symbols that have 525: very similar bits and meanings: IMAGE_SCN*, SEC_*, and STYP_*. 526: SEC_* are the BFD internal flags, used for generic BFD 527: information. STYP_* are the COFF section flags which appear in 528: COFF files. IMAGE_SCN_* are the PE section flags which appear in 529: PE files. The STYP_* flags and the IMAGE_SCN_* flags overlap, 530: but there are more IMAGE_SCN_* flags. */ 531: 532: /* FIXME: There is no gas syntax to specify the debug section flag. */ 533: if (CONST_STRNEQ (sec_name, DOT_DEBUG) 534: || CONST_STRNEQ (sec_name, GNU_LINKONCE_WI)) 535: sec_flags = SEC_DEBUGGING; 536: 537: /* skip LOAD */ 538: /* READONLY later */ 539: /* skip RELOC */ 540: if ((sec_flags & SEC_CODE) != 0) 541: styp_flags |= IMAGE_SCN_CNT_CODE; 542: if ((sec_flags & SEC_DATA) != 0) 543: styp_flags |= IMAGE_SCN_CNT_INITIALIZED_DATA; 544: if ((sec_flags & SEC_ALLOC) != 0 && (sec_flags & SEC_LOAD) == 0) 545: styp_flags |= IMAGE_SCN_CNT_UNINITIALIZED_DATA; /* ==STYP_BSS */ 546: /* skip ROM */ 547: /* skip constRUCTOR */ 548: /* skip CONTENTS */ 549: if ((sec_flags & SEC_IS_COMMON) != 0) 550: styp_flags |= IMAGE_SCN_LNK_COMDAT; 551: if ((sec_flags & SEC_DEBUGGING) != 0) 552: styp_flags |= IMAGE_SCN_MEM_DISCARDABLE; 553: if ((sec_flags & SEC_EXCLUDE) != 0) 554: styp_flags |= IMAGE_SCN_LNK_REMOVE; 555: if ((sec_flags & SEC_NEVER_LOAD) != 0) 556: styp_flags |= IMAGE_SCN_LNK_REMOVE; 557: /* skip IN_MEMORY */ 558: /* skip SORT */ 559: if (sec_flags & SEC_LINK_ONCE) 560: styp_flags |= IMAGE_SCN_LNK_COMDAT; 561: /* skip LINK_DUPLICATES */ 562: /* skip LINKER_CREATED */ 563: 564: if (sec_flags & (SEC_ALLOC | SEC_LOAD)) 565: { 566: /* For now, the read/write bits are mapped onto SEC_READONLY, even 567: though the semantics don't quite match. The bits from the input 568: are retained in pei_section_data(abfd, section)->pe_flags. */ 569: styp_flags |= IMAGE_SCN_MEM_READ; /* Always readable. */ 570: if ((sec_flags & SEC_READONLY) == 0) 571: styp_flags |= IMAGE_SCN_MEM_WRITE; /* Invert READONLY for write. */ 572: if (sec_flags & SEC_CODE) 573: styp_flags |= IMAGE_SCN_MEM_EXECUTE; /* CODE->EXECUTE. */ 574: if (sec_flags & SEC_COFF_SHARED) 575: styp_flags |= IMAGE_SCN_MEM_SHARED; /* Shared remains meaningful. */ 576: } 577: 578: return styp_flags; 579: } 580: 581: #endif /* COFF_WITH_PE */ 582: 583: /* Return a word with SEC_* flags set to represent the incoming STYP_* 584: flags (from scnhdr.s_flags). The inverse of this function is 585: sec_to_styp_flags(). NOTE: If you add to/change this routine, you 586: should probably mirror the changes in sec_to_styp_flags(). */ 587: 588: #ifndef COFF_WITH_PE 589: 590: static bfd_boolean 591: styp_to_sec_flags (bfd *abfd ATTRIBUTE_UNUSED, 592: void * hdr, 593: const char *name, 594: asection *section ATTRIBUTE_UNUSED, 595: flagword *flags_ptr) 596: { 597: struct internal_scnhdr *internal_s = (struct internal_scnhdr *) hdr; 598: long styp_flags = internal_s-><