Subversion Repositories Kolibri OS

Rev

Rev 5197 | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /* Support for the generic parts of most COFF variants, for BFD.
  2.    Copyright (C) 1990-2015 Free Software Foundation, Inc.
  3.    Written by Cygnus Support.
  4.  
  5.    This file is part of BFD, the Binary File Descriptor library.
  6.  
  7.    This program is free software; you can redistribute it and/or modify
  8.    it under the terms of the GNU General Public License as published by
  9.    the Free Software Foundation; either version 3 of the License, or
  10.    (at your option) any later version.
  11.  
  12.    This program is distributed in the hope that it will be useful,
  13.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.    GNU General Public License for more details.
  16.  
  17.    You should have received a copy of the GNU General Public License
  18.    along with this program; if not, write to the Free Software
  19.    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  20.    MA 02110-1301, USA.  */
  21.  
  22. /* Most of this hacked by  Steve Chamberlain,
  23.                         sac@cygnus.com.  */
  24. /*
  25. SECTION
  26.         coff backends
  27.  
  28.         BFD supports a number of different flavours of coff format.
  29.         The major differences between formats are the sizes and
  30.         alignments of fields in structures on disk, and the occasional
  31.         extra field.
  32.  
  33.         Coff in all its varieties is implemented with a few common
  34.         files and a number of implementation specific files. For
  35.         example, The 88k bcs coff format is implemented in the file
  36.         @file{coff-m88k.c}. This file @code{#include}s
  37.         @file{coff/m88k.h} which defines the external structure of the
  38.         coff format for the 88k, and @file{coff/internal.h} which
  39.         defines the internal structure. @file{coff-m88k.c} also
  40.         defines the relocations used by the 88k format
  41.         @xref{Relocations}.
  42.  
  43.         The Intel i960 processor version of coff is implemented in
  44.         @file{coff-i960.c}. This file has the same structure as
  45.         @file{coff-m88k.c}, except that it includes @file{coff/i960.h}
  46.         rather than @file{coff-m88k.h}.
  47.  
  48. SUBSECTION
  49.         Porting to a new version of coff
  50.  
  51.         The recommended method is to select from the existing
  52.         implementations the version of coff which is most like the one
  53.         you want to use.  For example, we'll say that i386 coff is
  54.         the one you select, and that your coff flavour is called foo.
  55.         Copy @file{i386coff.c} to @file{foocoff.c}, copy
  56.         @file{../include/coff/i386.h} to @file{../include/coff/foo.h},
  57.         and add the lines to @file{targets.c} and @file{Makefile.in}
  58.         so that your new back end is used. Alter the shapes of the
  59.         structures in @file{../include/coff/foo.h} so that they match
  60.         what you need. You will probably also have to add
  61.         @code{#ifdef}s to the code in @file{coff/internal.h} and
  62.         @file{coffcode.h} if your version of coff is too wild.
  63.  
  64.         You can verify that your new BFD backend works quite simply by
  65.         building @file{objdump} from the @file{binutils} directory,
  66.         and making sure that its version of what's going on and your
  67.         host system's idea (assuming it has the pretty standard coff
  68.         dump utility, usually called @code{att-dump} or just
  69.         @code{dump}) are the same.  Then clean up your code, and send
  70.         what you've done to Cygnus. Then your stuff will be in the
  71.         next release, and you won't have to keep integrating it.
  72.  
  73. SUBSECTION
  74.         How the coff backend works
  75.  
  76. SUBSUBSECTION
  77.         File layout
  78.  
  79.         The Coff backend is split into generic routines that are
  80.         applicable to any Coff target and routines that are specific
  81.         to a particular target.  The target-specific routines are
  82.         further split into ones which are basically the same for all
  83.         Coff targets except that they use the external symbol format
  84.         or use different values for certain constants.
  85.  
  86.         The generic routines are in @file{coffgen.c}.  These routines
  87.         work for any Coff target.  They use some hooks into the target
  88.         specific code; the hooks are in a @code{bfd_coff_backend_data}
  89.         structure, one of which exists for each target.
  90.  
  91.         The essentially similar target-specific routines are in
  92.         @file{coffcode.h}.  This header file includes executable C code.
  93.         The various Coff targets first include the appropriate Coff
  94.         header file, make any special defines that are needed, and
  95.         then include @file{coffcode.h}.
  96.  
  97.         Some of the Coff targets then also have additional routines in
  98.         the target source file itself.
  99.  
  100.         For example, @file{coff-i960.c} includes
  101.         @file{coff/internal.h} and @file{coff/i960.h}.  It then
  102.         defines a few constants, such as @code{I960}, and includes
  103.         @file{coffcode.h}.  Since the i960 has complex relocation
  104.         types, @file{coff-i960.c} also includes some code to
  105.         manipulate the i960 relocs.  This code is not in
  106.         @file{coffcode.h} because it would not be used by any other
  107.         target.
  108.  
  109. SUBSUBSECTION
  110.         Coff long section names
  111.  
  112.         In the standard Coff object format, section names are limited to
  113.         the eight bytes available in the @code{s_name} field of the
  114.         @code{SCNHDR} section header structure.  The format requires the
  115.         field to be NUL-padded, but not necessarily NUL-terminated, so
  116.         the longest section names permitted are a full eight characters.
  117.  
  118.         The Microsoft PE variants of the Coff object file format add
  119.         an extension to support the use of long section names.  This
  120.         extension is defined in section 4 of the Microsoft PE/COFF
  121.         specification (rev 8.1).  If a section name is too long to fit
  122.         into the section header's @code{s_name} field, it is instead
  123.         placed into the string table, and the @code{s_name} field is
  124.         filled with a slash ("/") followed by the ASCII decimal
  125.         representation of the offset of the full name relative to the
  126.         string table base.
  127.  
  128.         Note that this implies that the extension can only be used in object
  129.         files, as executables do not contain a string table.  The standard
  130.         specifies that long section names from objects emitted into executable
  131.         images are to be truncated.
  132.  
  133.         However, as a GNU extension, BFD can generate executable images
  134.         that contain a string table and long section names.  This
  135.         would appear to be technically valid, as the standard only says
  136.         that Coff debugging information is deprecated, not forbidden,
  137.         and in practice it works, although some tools that parse PE files
  138.         expecting the MS standard format may become confused; @file{PEview} is
  139.         one known example.
  140.  
  141.         The functionality is supported in BFD by code implemented under
  142.         the control of the macro @code{COFF_LONG_SECTION_NAMES}.  If not
  143.         defined, the format does not support long section names in any way.
  144.         If defined, it is used to initialise a flag,
  145.         @code{_bfd_coff_long_section_names}, and a hook function pointer,
  146.         @code{_bfd_coff_set_long_section_names}, in the Coff backend data
  147.         structure.  The flag controls the generation of long section names
  148.         in output BFDs at runtime; if it is false, as it will be by default
  149.         when generating an executable image, long section names are truncated;
  150.         if true, the long section names extension is employed.  The hook
  151.         points to a function that allows the value of the flag to be altered
  152.         at runtime, on formats that support long section names at all; on
  153.         other formats it points to a stub that returns an error indication.
  154.  
  155.         With input BFDs, the flag is set according to whether any long section
  156.         names are detected while reading the section headers.  For a completely
  157.         new BFD, the flag is set to the default for the target format.  This
  158.         information can be used by a client of the BFD library when deciding
  159.         what output format to generate, and means that a BFD that is opened
  160.         for read and subsequently converted to a writeable BFD and modified
  161.         in-place will retain whatever format it had on input.
  162.  
  163.         If @code{COFF_LONG_SECTION_NAMES} is simply defined (blank), or is
  164.         defined to the value "1", then long section names are enabled by
  165.         default; if it is defined to the value zero, they are disabled by
  166.         default (but still accepted in input BFDs).  The header @file{coffcode.h}
  167.         defines a macro, @code{COFF_DEFAULT_LONG_SECTION_NAMES}, which is
  168.         used in the backends to initialise the backend data structure fields
  169.         appropriately; see the comments for further detail.
  170.  
  171. SUBSUBSECTION
  172.         Bit twiddling
  173.  
  174.         Each flavour of coff supported in BFD has its own header file
  175.         describing the external layout of the structures. There is also
  176.         an internal description of the coff layout, in
  177.         @file{coff/internal.h}. A major function of the
  178.         coff backend is swapping the bytes and twiddling the bits to
  179.         translate the external form of the structures into the normal
  180.         internal form. This is all performed in the
  181.         @code{bfd_swap}_@i{thing}_@i{direction} routines. Some
  182.         elements are different sizes between different versions of
  183.         coff; it is the duty of the coff version specific include file
  184.         to override the definitions of various packing routines in
  185.         @file{coffcode.h}. E.g., the size of line number entry in coff is
  186.         sometimes 16 bits, and sometimes 32 bits. @code{#define}ing
  187.         @code{PUT_LNSZ_LNNO} and @code{GET_LNSZ_LNNO} will select the
  188.         correct one. No doubt, some day someone will find a version of
  189.         coff which has a varying field size not catered to at the
  190.         moment. To port BFD, that person will have to add more @code{#defines}.
  191.         Three of the bit twiddling routines are exported to
  192.         @code{gdb}; @code{coff_swap_aux_in}, @code{coff_swap_sym_in}
  193.         and @code{coff_swap_lineno_in}. @code{GDB} reads the symbol
  194.         table on its own, but uses BFD to fix things up.  More of the
  195.         bit twiddlers are exported for @code{gas};
  196.         @code{coff_swap_aux_out}, @code{coff_swap_sym_out},
  197.         @code{coff_swap_lineno_out}, @code{coff_swap_reloc_out},
  198.         @code{coff_swap_filehdr_out}, @code{coff_swap_aouthdr_out},
  199.         @code{coff_swap_scnhdr_out}. @code{Gas} currently keeps track
  200.         of all the symbol table and reloc drudgery itself, thereby
  201.         saving the internal BFD overhead, but uses BFD to swap things
  202.         on the way out, making cross ports much safer.  Doing so also
  203.         allows BFD (and thus the linker) to use the same header files
  204.         as @code{gas}, which makes one avenue to disaster disappear.
  205.  
  206. SUBSUBSECTION
  207.         Symbol reading
  208.  
  209.         The simple canonical form for symbols used by BFD is not rich
  210.         enough to keep all the information available in a coff symbol
  211.         table. The back end gets around this problem by keeping the original
  212.         symbol table around, "behind the scenes".
  213.  
  214.         When a symbol table is requested (through a call to
  215.         @code{bfd_canonicalize_symtab}), a request gets through to
  216.         @code{coff_get_normalized_symtab}. This reads the symbol table from
  217.         the coff file and swaps all the structures inside into the
  218.         internal form. It also fixes up all the pointers in the table
  219.         (represented in the file by offsets from the first symbol in
  220.         the table) into physical pointers to elements in the new
  221.         internal table. This involves some work since the meanings of
  222.         fields change depending upon context: a field that is a
  223.         pointer to another structure in the symbol table at one moment
  224.         may be the size in bytes of a structure at the next.  Another
  225.         pass is made over the table. All symbols which mark file names
  226.         (<<C_FILE>> symbols) are modified so that the internal
  227.         string points to the value in the auxent (the real filename)
  228.         rather than the normal text associated with the symbol
  229.         (@code{".file"}).
  230.  
  231.         At this time the symbol names are moved around. Coff stores
  232.         all symbols less than nine characters long physically
  233.         within the symbol table; longer strings are kept at the end of
  234.         the file in the string table. This pass moves all strings
  235.         into memory and replaces them with pointers to the strings.
  236.  
  237.         The symbol table is massaged once again, this time to create
  238.         the canonical table used by the BFD application. Each symbol
  239.         is inspected in turn, and a decision made (using the
  240.         @code{sclass} field) about the various flags to set in the
  241.         @code{asymbol}.  @xref{Symbols}. The generated canonical table
  242.         shares strings with the hidden internal symbol table.
  243.  
  244.         Any linenumbers are read from the coff file too, and attached
  245.         to the symbols which own the functions the linenumbers belong to.
  246.  
  247. SUBSUBSECTION
  248.         Symbol writing
  249.  
  250.         Writing a symbol to a coff file which didn't come from a coff
  251.         file will lose any debugging information. The @code{asymbol}
  252.         structure remembers the BFD from which the symbol was taken, and on
  253.         output the back end makes sure that the same destination target as
  254.         source target is present.
  255.  
  256.         When the symbols have come from a coff file then all the
  257.         debugging information is preserved.
  258.  
  259.         Symbol tables are provided for writing to the back end in a
  260.         vector of pointers to pointers. This allows applications like
  261.         the linker to accumulate and output large symbol tables
  262.         without having to do too much byte copying.
  263.  
  264.         This function runs through the provided symbol table and
  265.         patches each symbol marked as a file place holder
  266.         (@code{C_FILE}) to point to the next file place holder in the
  267.         list. It also marks each @code{offset} field in the list with
  268.         the offset from the first symbol of the current symbol.
  269.  
  270.         Another function of this procedure is to turn the canonical
  271.         value form of BFD into the form used by coff. Internally, BFD
  272.         expects symbol values to be offsets from a section base; so a
  273.         symbol physically at 0x120, but in a section starting at
  274.         0x100, would have the value 0x20. Coff expects symbols to
  275.         contain their final value, so symbols have their values
  276.         changed at this point to reflect their sum with their owning
  277.         section.  This transformation uses the
  278.         <<output_section>> field of the @code{asymbol}'s
  279.         @code{asection} @xref{Sections}.
  280.  
  281.         o <<coff_mangle_symbols>>
  282.  
  283.         This routine runs though the provided symbol table and uses
  284.         the offsets generated by the previous pass and the pointers
  285.         generated when the symbol table was read in to create the
  286.         structured hierarchy required by coff. It changes each pointer
  287.         to a symbol into the index into the symbol table of the asymbol.
  288.  
  289.         o <<coff_write_symbols>>
  290.  
  291.         This routine runs through the symbol table and patches up the
  292.         symbols from their internal form into the coff way, calls the
  293.         bit twiddlers, and writes out the table to the file.
  294.  
  295. */
  296.  
  297. /*
  298. INTERNAL_DEFINITION
  299.         coff_symbol_type
  300.  
  301. DESCRIPTION
  302.         The hidden information for an <<asymbol>> is described in a
  303.         <<combined_entry_type>>:
  304.  
  305. CODE_FRAGMENT
  306. .
  307. .typedef struct coff_ptr_struct
  308. .{
  309. .  {* Remembers the offset from the first symbol in the file for
  310. .     this symbol. Generated by coff_renumber_symbols. *}
  311. .  unsigned int offset;
  312. .
  313. .  {* Should the value of this symbol be renumbered.  Used for
  314. .     XCOFF C_BSTAT symbols.  Set by coff_slurp_symbol_table.  *}
  315. .  unsigned int fix_value : 1;
  316. .
  317. .  {* Should the tag field of this symbol be renumbered.
  318. .     Created by coff_pointerize_aux. *}
  319. .  unsigned int fix_tag : 1;
  320. .
  321. .  {* Should the endidx field of this symbol be renumbered.
  322. .     Created by coff_pointerize_aux. *}
  323. .  unsigned int fix_end : 1;
  324. .
  325. .  {* Should the x_csect.x_scnlen field be renumbered.
  326. .     Created by coff_pointerize_aux. *}
  327. .  unsigned int fix_scnlen : 1;
  328. .
  329. .  {* Fix up an XCOFF C_BINCL/C_EINCL symbol.  The value is the
  330. .     index into the line number entries.  Set by coff_slurp_symbol_table.  *}
  331. .  unsigned int fix_line : 1;
  332. .
  333. .  {* The container for the symbol structure as read and translated
  334. .     from the file. *}
  335. .  union
  336. .  {
  337. .    union internal_auxent auxent;
  338. .    struct internal_syment syment;
  339. .  } u;
  340. .
  341. . {* Selector for the union above.  *}
  342. . bfd_boolean is_sym;
  343. .} combined_entry_type;
  344. .
  345. .
  346. .{* Each canonical asymbol really looks like this: *}
  347. .
  348. .typedef struct coff_symbol_struct
  349. .{
  350. .  {* The actual symbol which the rest of BFD works with *}
  351. .  asymbol symbol;
  352. .
  353. .  {* A pointer to the hidden information for this symbol *}
  354. .  combined_entry_type *native;
  355. .
  356. .  {* A pointer to the linenumber information for this symbol *}
  357. .  struct lineno_cache_entry *lineno;
  358. .
  359. .  {* Have the line numbers been relocated yet ? *}
  360. .  bfd_boolean done_lineno;
  361. .} coff_symbol_type;
  362.  
  363. */
  364.  
  365. #include "libiberty.h"
  366.  
  367. #ifdef COFF_WITH_PE
  368. #include "peicode.h"
  369. #else
  370. #include "coffswap.h"
  371. #endif
  372.  
  373. #define STRING_SIZE_SIZE 4
  374.  
  375. #define DOT_DEBUG       ".debug"
  376. #define DOT_ZDEBUG      ".zdebug"
  377. #define GNU_LINKONCE_WI ".gnu.linkonce.wi."
  378. #define GNU_LINKONCE_WT ".gnu.linkonce.wt."
  379. #define DOT_RELOC       ".reloc"
  380.  
  381. #if defined (COFF_LONG_SECTION_NAMES)
  382. /* Needed to expand the inputs to BLANKOR1TOODD.  */
  383. #define COFFLONGSECTIONCATHELPER(x,y)    x ## y
  384. /* If the input macro Y is blank or '1', return an odd number; if it is
  385.    '0', return an even number.  Result undefined in all other cases.  */
  386. #define BLANKOR1TOODD(y)                 COFFLONGSECTIONCATHELPER(1,y)
  387. /* Defined to numerical 0 or 1 according to whether generation of long
  388.    section names is disabled or enabled by default.  */
  389. #define COFF_ENABLE_LONG_SECTION_NAMES   (BLANKOR1TOODD(COFF_LONG_SECTION_NAMES) & 1)
  390. /* Where long section names are supported, we allow them to be enabled
  391.    and disabled at runtime, so select an appropriate hook function for
  392.    _bfd_coff_set_long_section_names.  */
  393. #define COFF_LONG_SECTION_NAMES_SETTER   bfd_coff_set_long_section_names_allowed
  394. #else /* !defined (COFF_LONG_SECTION_NAMES) */
  395. /* If long section names are not supported, this stub disallows any
  396.    attempt to enable them at run-time.  */
  397. #define COFF_LONG_SECTION_NAMES_SETTER   bfd_coff_set_long_section_names_disallowed
  398. #endif /* defined (COFF_LONG_SECTION_NAMES) */
  399.  
  400. /* Define a macro that can be used to initialise both the fields relating
  401.    to long section names in the backend data struct simultaneously.  */
  402. #if COFF_ENABLE_LONG_SECTION_NAMES
  403. #define COFF_DEFAULT_LONG_SECTION_NAMES  (TRUE), COFF_LONG_SECTION_NAMES_SETTER
  404. #else /* !COFF_ENABLE_LONG_SECTION_NAMES */
  405. #define COFF_DEFAULT_LONG_SECTION_NAMES  (FALSE), COFF_LONG_SECTION_NAMES_SETTER
  406. #endif /* COFF_ENABLE_LONG_SECTION_NAMES */
  407.  
  408. #if defined (COFF_LONG_SECTION_NAMES)
  409. static bfd_boolean bfd_coff_set_long_section_names_allowed
  410.   (bfd *, int);
  411. #else /* !defined (COFF_LONG_SECTION_NAMES) */
  412. static bfd_boolean bfd_coff_set_long_section_names_disallowed
  413.   (bfd *, int);
  414. #endif /* defined (COFF_LONG_SECTION_NAMES) */
  415. static long sec_to_styp_flags
  416.   (const char *, flagword);
  417. static bfd_boolean styp_to_sec_flags
  418.   (bfd *, void *, const char *, asection *, flagword *);
  419. static bfd_boolean coff_bad_format_hook
  420.   (bfd *, void *);
  421. static void coff_set_custom_section_alignment
  422.   (bfd *, asection *, const struct coff_section_alignment_entry *,
  423.    const unsigned int);
  424. static bfd_boolean coff_new_section_hook
  425.   (bfd *, asection *);
  426. static bfd_boolean coff_set_arch_mach_hook
  427.   (bfd *, void *);
  428. static bfd_boolean coff_write_relocs
  429.   (bfd *, int);
  430. static bfd_boolean coff_set_flags
  431.   (bfd *, unsigned int *, unsigned short *);
  432. static bfd_boolean coff_set_arch_mach
  433.   (bfd *, enum bfd_architecture, unsigned long) ATTRIBUTE_UNUSED;
  434. static bfd_boolean coff_compute_section_file_positions
  435.   (bfd *);
  436. static bfd_boolean coff_write_object_contents
  437.   (bfd *) ATTRIBUTE_UNUSED;
  438. static bfd_boolean coff_set_section_contents
  439.   (bfd *, asection *, const void *, file_ptr, bfd_size_type);
  440. static void * buy_and_read
  441.   (bfd *, file_ptr, bfd_size_type);
  442. static bfd_boolean coff_slurp_line_table
  443.   (bfd *, asection *);
  444. static bfd_boolean coff_slurp_symbol_table
  445.   (bfd *);
  446. static enum coff_symbol_classification coff_classify_symbol
  447.   (bfd *, struct internal_syment *);
  448. static bfd_boolean coff_slurp_reloc_table
  449.   (bfd *, asection *, asymbol **);
  450. static long coff_canonicalize_reloc
  451.   (bfd *, asection *, arelent **, asymbol **);
  452. #ifndef coff_mkobject_hook
  453. static void * coff_mkobject_hook
  454.   (bfd *, void *,  void *);
  455. #endif
  456. #ifdef COFF_WITH_PE
  457. static flagword handle_COMDAT
  458.   (bfd *, flagword, void *, const char *, asection *);
  459. #endif
  460. #ifdef COFF_IMAGE_WITH_PE
  461. static bfd_boolean coff_read_word
  462.   (bfd *, unsigned int *);
  463. static unsigned int coff_compute_checksum
  464.   (bfd *);
  465. static bfd_boolean coff_apply_checksum
  466.   (bfd *);
  467. #endif
  468. #ifdef TICOFF
  469. static bfd_boolean ticoff0_bad_format_hook
  470.   (bfd *, void * );
  471. static bfd_boolean ticoff1_bad_format_hook
  472.   (bfd *, void * );
  473. #endif
  474. /* void warning(); */
  475.  
  476. #if defined (COFF_LONG_SECTION_NAMES)
  477. static bfd_boolean
  478. bfd_coff_set_long_section_names_allowed (bfd *abfd, int enable)
  479. {
  480.   coff_backend_info (abfd)->_bfd_coff_long_section_names = enable;
  481.   return TRUE;
  482. }
  483. #else /* !defined (COFF_LONG_SECTION_NAMES) */
  484. static bfd_boolean
  485. bfd_coff_set_long_section_names_disallowed (bfd *abfd, int enable)
  486. {
  487.   (void) abfd;
  488.   (void) enable;
  489.   return FALSE;
  490. }
  491. #endif /* defined (COFF_LONG_SECTION_NAMES) */
  492.  
  493. /* Return a word with STYP_* (scnhdr.s_flags) flags set to represent
  494.    the incoming SEC_* flags.  The inverse of this function is
  495.    styp_to_sec_flags().  NOTE: If you add to/change this routine, you
  496.    should probably mirror the changes in styp_to_sec_flags().  */
  497.  
  498. #ifndef COFF_WITH_PE
  499.  
  500. /* Macros for setting debugging flags.  */
  501.  
  502. #ifdef STYP_DEBUG
  503. #define STYP_XCOFF_DEBUG STYP_DEBUG
  504. #else
  505. #define STYP_XCOFF_DEBUG STYP_INFO
  506. #endif
  507.  
  508. #ifdef COFF_ALIGN_IN_S_FLAGS
  509. #define STYP_DEBUG_INFO STYP_DSECT
  510. #else
  511. #define STYP_DEBUG_INFO STYP_INFO
  512. #endif
  513.  
  514. static long
  515. sec_to_styp_flags (const char *sec_name, flagword sec_flags)
  516. {
  517.   long styp_flags = 0;
  518.  
  519.   if (!strcmp (sec_name, _TEXT))
  520.     {
  521.       styp_flags = STYP_TEXT;
  522.     }
  523.   else if (!strcmp (sec_name, _DATA))
  524.     {
  525.       styp_flags = STYP_DATA;
  526.     }
  527.   else if (!strcmp (sec_name, _BSS))
  528.     {
  529.       styp_flags = STYP_BSS;
  530. #ifdef _COMMENT
  531.     }
  532.   else if (!strcmp (sec_name, _COMMENT))
  533.     {
  534.       styp_flags = STYP_INFO;
  535. #endif /* _COMMENT */
  536. #ifdef _LIB
  537.     }
  538.   else if (!strcmp (sec_name, _LIB))
  539.     {
  540.       styp_flags = STYP_LIB;
  541. #endif /* _LIB */
  542. #ifdef _LIT
  543.     }
  544.   else if (!strcmp (sec_name, _LIT))
  545.     {
  546.       styp_flags = STYP_LIT;
  547. #endif /* _LIT */
  548.     }
  549.   else if (CONST_STRNEQ (sec_name, DOT_DEBUG)
  550.            || CONST_STRNEQ (sec_name, DOT_ZDEBUG))
  551.     {
  552.       /* Handle the XCOFF debug section and DWARF2 debug sections.  */
  553.       if (!sec_name[6])
  554.         styp_flags = STYP_XCOFF_DEBUG;
  555.       else
  556.         styp_flags = STYP_DEBUG_INFO;
  557.     }
  558.   else if (CONST_STRNEQ (sec_name, ".stab"))
  559.     {
  560.       styp_flags = STYP_DEBUG_INFO;
  561.     }
  562. #ifdef COFF_LONG_SECTION_NAMES
  563.   else if (CONST_STRNEQ (sec_name, GNU_LINKONCE_WI)
  564.            || CONST_STRNEQ (sec_name, GNU_LINKONCE_WT))
  565.     {
  566.       styp_flags = STYP_DEBUG_INFO;
  567.     }
  568. #endif
  569. #ifdef RS6000COFF_C
  570.   else if (!strcmp (sec_name, _PAD))
  571.     {
  572.       styp_flags = STYP_PAD;
  573.     }
  574.   else if (!strcmp (sec_name, _LOADER))
  575.     {
  576.       styp_flags = STYP_LOADER;
  577.     }
  578.   else if (!strcmp (sec_name, _EXCEPT))
  579.     {
  580.       styp_flags = STYP_EXCEPT;
  581.     }
  582.   else if (!strcmp (sec_name, _TYPCHK))
  583.     {
  584.       styp_flags = STYP_TYPCHK;
  585.     }
  586.   else if (sec_flags & SEC_DEBUGGING)
  587.     {
  588.       int i;
  589.  
  590.       for (i = 0; i < XCOFF_DWSECT_NBR_NAMES; i++)
  591.         if (!strcmp (sec_name, xcoff_dwsect_names[i].name))
  592.           {
  593.             styp_flags = STYP_DWARF | xcoff_dwsect_names[i].flag;
  594.             break;
  595.           }
  596.     }
  597. #endif
  598.   /* Try and figure out what it should be */
  599.   else if (sec_flags & SEC_CODE)
  600.     {
  601.       styp_flags = STYP_TEXT;
  602.     }
  603.   else if (sec_flags & SEC_DATA)
  604.     {
  605.       styp_flags = STYP_DATA;
  606.     }
  607.   else if (sec_flags & SEC_READONLY)
  608.     {
  609. #ifdef STYP_LIT                 /* 29k readonly text/data section */
  610.       styp_flags = STYP_LIT;
  611. #else
  612.       styp_flags = STYP_TEXT;
  613. #endif /* STYP_LIT */
  614.     }
  615.   else if (sec_flags & SEC_LOAD)
  616.     {
  617.       styp_flags = STYP_TEXT;
  618.     }
  619.   else if (sec_flags & SEC_ALLOC)
  620.     {
  621.       styp_flags = STYP_BSS;
  622.     }
  623.  
  624. #ifdef STYP_CLINK
  625.   if (sec_flags & SEC_TIC54X_CLINK)
  626.     styp_flags |= STYP_CLINK;
  627. #endif
  628.  
  629. #ifdef STYP_BLOCK
  630.   if (sec_flags & SEC_TIC54X_BLOCK)
  631.     styp_flags |= STYP_BLOCK;
  632. #endif
  633.  
  634. #ifdef STYP_NOLOAD
  635.   if ((sec_flags & (SEC_NEVER_LOAD | SEC_COFF_SHARED_LIBRARY)) != 0)
  636.     styp_flags |= STYP_NOLOAD;
  637. #endif
  638.  
  639.   return styp_flags;
  640. }
  641.  
  642. #else /* COFF_WITH_PE */
  643.  
  644. /* The PE version; see above for the general comments.  The non-PE
  645.    case seems to be more guessing, and breaks PE format; specifically,
  646.    .rdata is readonly, but it sure ain't text.  Really, all this
  647.    should be set up properly in gas (or whatever assembler is in use),
  648.    and honor whatever objcopy/strip, etc. sent us as input.  */
  649.  
  650. static long
  651. sec_to_styp_flags (const char *sec_name, flagword sec_flags)
  652. {
  653.   long styp_flags = 0;
  654.   bfd_boolean is_dbg = FALSE;
  655.  
  656.   if (CONST_STRNEQ (sec_name, DOT_DEBUG)
  657.       || CONST_STRNEQ (sec_name, DOT_ZDEBUG)
  658. #ifdef COFF_LONG_SECTION_NAMES
  659.       || CONST_STRNEQ (sec_name, GNU_LINKONCE_WI)
  660.       || CONST_STRNEQ (sec_name, GNU_LINKONCE_WT)
  661. #endif
  662.       || CONST_STRNEQ (sec_name, ".stab"))
  663.     is_dbg = TRUE;
  664.  
  665.   /* caution: there are at least three groups of symbols that have
  666.      very similar bits and meanings: IMAGE_SCN*, SEC_*, and STYP_*.
  667.      SEC_* are the BFD internal flags, used for generic BFD
  668.      information.  STYP_* are the COFF section flags which appear in
  669.      COFF files.  IMAGE_SCN_* are the PE section flags which appear in
  670.      PE files.  The STYP_* flags and the IMAGE_SCN_* flags overlap,
  671.      but there are more IMAGE_SCN_* flags.  */
  672.  
  673.   /* FIXME: There is no gas syntax to specify the debug section flag.  */
  674.   if (is_dbg)
  675.     {
  676.       sec_flags &= (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD
  677.                     | SEC_LINK_DUPLICATES_SAME_CONTENTS
  678.                     | SEC_LINK_DUPLICATES_SAME_SIZE);
  679.       sec_flags |= SEC_DEBUGGING | SEC_READONLY;
  680.     }
  681.  
  682.   /* skip LOAD */
  683.   /* READONLY later */
  684.   /* skip RELOC */
  685.   if ((sec_flags & SEC_CODE) != 0)
  686.     styp_flags |= IMAGE_SCN_CNT_CODE;
  687.   if ((sec_flags & (SEC_DATA | SEC_DEBUGGING)) != 0)
  688.     styp_flags |= IMAGE_SCN_CNT_INITIALIZED_DATA;
  689.   if ((sec_flags & SEC_ALLOC) != 0 && (sec_flags & SEC_LOAD) == 0)
  690.     styp_flags |= IMAGE_SCN_CNT_UNINITIALIZED_DATA;  /* ==STYP_BSS */
  691.   /* skip ROM */
  692.   /* skip constRUCTOR */
  693.   /* skip CONTENTS */
  694.   if ((sec_flags & SEC_IS_COMMON) != 0)
  695.     styp_flags |= IMAGE_SCN_LNK_COMDAT;
  696.   if ((sec_flags & SEC_DEBUGGING) != 0)
  697.     styp_flags |= IMAGE_SCN_MEM_DISCARDABLE;
  698.   if ((sec_flags & SEC_EXCLUDE) != 0 && !is_dbg)
  699.     styp_flags |= IMAGE_SCN_LNK_REMOVE;
  700.   if ((sec_flags & SEC_NEVER_LOAD) != 0 && !is_dbg)
  701.     styp_flags |= IMAGE_SCN_LNK_REMOVE;
  702.   /* skip IN_MEMORY */
  703.   /* skip SORT */
  704.   if (sec_flags & SEC_LINK_ONCE)
  705.     styp_flags |= IMAGE_SCN_LNK_COMDAT;
  706.   if ((sec_flags
  707.        & (SEC_LINK_DUPLICATES_DISCARD | SEC_LINK_DUPLICATES_SAME_CONTENTS
  708.           | SEC_LINK_DUPLICATES_SAME_SIZE)) != 0)
  709.     styp_flags |= IMAGE_SCN_LNK_COMDAT;
  710.  
  711.   /* skip LINKER_CREATED */
  712.  
  713.   if ((sec_flags & SEC_COFF_NOREAD) == 0)
  714.     styp_flags |= IMAGE_SCN_MEM_READ;     /* Invert NOREAD for read.  */
  715.   if ((sec_flags & SEC_READONLY) == 0)
  716.     styp_flags |= IMAGE_SCN_MEM_WRITE;    /* Invert READONLY for write.  */
  717.   if (sec_flags & SEC_CODE)
  718.     styp_flags |= IMAGE_SCN_MEM_EXECUTE;  /* CODE->EXECUTE.  */
  719.   if (sec_flags & SEC_COFF_SHARED)
  720.     styp_flags |= IMAGE_SCN_MEM_SHARED;   /* Shared remains meaningful.  */
  721.  
  722.   return styp_flags;
  723. }
  724.  
  725. #endif /* COFF_WITH_PE */
  726.  
  727. /* Return a word with SEC_* flags set to represent the incoming STYP_*
  728.    flags (from scnhdr.s_flags).  The inverse of this function is
  729.    sec_to_styp_flags().  NOTE: If you add to/change this routine, you
  730.    should probably mirror the changes in sec_to_styp_flags().  */
  731.  
  732. #ifndef COFF_WITH_PE
  733.  
  734. static bfd_boolean
  735. styp_to_sec_flags (bfd *abfd ATTRIBUTE_UNUSED,
  736.                    void * hdr,
  737.                    const char *name,
  738.                    asection *section ATTRIBUTE_UNUSED,
  739.                    flagword *flags_ptr)
  740. {
  741.   struct internal_scnhdr *internal_s = (struct internal_scnhdr *) hdr;
  742.   long styp_flags = internal_s->s_flags;
  743.   flagword sec_flags = 0;
  744.  
  745. #ifdef STYP_BLOCK
  746.   if (styp_flags & STYP_BLOCK)
  747.     sec_flags |= SEC_TIC54X_BLOCK;
  748. #endif
  749.  
  750. #ifdef STYP_CLINK
  751.   if (styp_flags & STYP_CLINK)
  752.     sec_flags |= SEC_TIC54X_CLINK;
  753. #endif
  754.  
  755. #ifdef STYP_NOLOAD
  756.   if (styp_flags & STYP_NOLOAD)
  757.     sec_flags |= SEC_NEVER_LOAD;
  758. #endif /* STYP_NOLOAD */
  759.  
  760.   /* For 386 COFF, at least, an unloadable text or data section is
  761.      actually a shared library section.  */
  762.   if (styp_flags & STYP_TEXT)
  763.     {
  764.       if (sec_flags & SEC_NEVER_LOAD)
  765.         sec_flags |= SEC_CODE | SEC_COFF_SHARED_LIBRARY;
  766.       else
  767.         sec_flags |= SEC_CODE | SEC_LOAD | SEC_ALLOC;
  768.     }
  769.   else if (styp_flags & STYP_DATA)
  770.     {
  771.       if (sec_flags & SEC_NEVER_LOAD)
  772.         sec_flags |= SEC_DATA | SEC_COFF_SHARED_LIBRARY;
  773.       else
  774.         sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC;
  775.     }
  776.   else if (styp_flags & STYP_BSS)
  777.     {
  778. #ifdef BSS_NOLOAD_IS_SHARED_LIBRARY
  779.       if (sec_flags & SEC_NEVER_LOAD)
  780.         sec_flags |= SEC_ALLOC | SEC_COFF_SHARED_LIBRARY;
  781.       else
  782. #endif
  783.         sec_flags |= SEC_ALLOC;
  784.     }
  785.   else if (styp_flags & STYP_INFO)
  786.     {
  787.       /* We mark these as SEC_DEBUGGING, but only if COFF_PAGE_SIZE is
  788.          defined.  coff_compute_section_file_positions uses
  789.          COFF_PAGE_SIZE to ensure that the low order bits of the
  790.          section VMA and the file offset match.  If we don't know
  791.          COFF_PAGE_SIZE, we can't ensure the correct correspondence,
  792.          and demand page loading of the file will fail.  */
  793. #if defined (COFF_PAGE_SIZE) && !defined (COFF_ALIGN_IN_S_FLAGS)
  794.       sec_flags |= SEC_DEBUGGING;
  795. #endif
  796.     }
  797.   else if (styp_flags & STYP_PAD)
  798.     sec_flags = 0;
  799. #ifdef RS6000COFF_C
  800.   else if (styp_flags & STYP_EXCEPT)
  801.     sec_flags |= SEC_LOAD;
  802.   else if (styp_flags & STYP_LOADER)
  803.     sec_flags |= SEC_LOAD;
  804.   else if (styp_flags & STYP_TYPCHK)
  805.     sec_flags |= SEC_LOAD;
  806.   else if (styp_flags & STYP_DWARF)
  807.     sec_flags |= SEC_DEBUGGING;
  808. #endif
  809.   else if (strcmp (name, _TEXT) == 0)
  810.     {
  811.       if (sec_flags & SEC_NEVER_LOAD)
  812.         sec_flags |= SEC_CODE | SEC_COFF_SHARED_LIBRARY;
  813.       else
  814.         sec_flags |= SEC_CODE | SEC_LOAD | SEC_ALLOC;
  815.     }
  816.   else if (strcmp (name, _DATA) == 0)
  817.     {
  818.       if (sec_flags & SEC_NEVER_LOAD)
  819.         sec_flags |= SEC_DATA | SEC_COFF_SHARED_LIBRARY;
  820.       else
  821.         sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC;
  822.     }
  823.   else if (strcmp (name, _BSS) == 0)
  824.     {
  825. #ifdef BSS_NOLOAD_IS_SHARED_LIBRARY
  826.       if (sec_flags & SEC_NEVER_LOAD)
  827.         sec_flags |= SEC_ALLOC | SEC_COFF_SHARED_LIBRARY;
  828.       else
  829. #endif
  830.         sec_flags |= SEC_ALLOC;
  831.     }
  832.   else if (CONST_STRNEQ (name, DOT_DEBUG)
  833.            || CONST_STRNEQ (name, DOT_ZDEBUG)
  834. #ifdef _COMMENT
  835.            || strcmp (name, _COMMENT) == 0
  836. #endif
  837. #ifdef COFF_LONG_SECTION_NAMES
  838.            || CONST_STRNEQ (name, GNU_LINKONCE_WI)
  839.            || CONST_STRNEQ (name, GNU_LINKONCE_WT)
  840. #endif
  841.            || CONST_STRNEQ (name, ".stab"))
  842.     {
  843. #ifdef COFF_PAGE_SIZE
  844.       sec_flags |= SEC_DEBUGGING;
  845. #endif
  846.     }
  847. #ifdef _LIB
  848.   else if (strcmp (name, _LIB) == 0)
  849.     ;
  850. #endif
  851. #ifdef _LIT
  852.   else if (strcmp (name, _LIT) == 0)
  853.     sec_flags = SEC_LOAD | SEC_ALLOC | SEC_READONLY;
  854. #endif
  855.   else
  856.     sec_flags |= SEC_ALLOC | SEC_LOAD;
  857.  
  858. #ifdef STYP_LIT                 /* A29k readonly text/data section type.  */
  859.   if ((styp_flags & STYP_LIT) == STYP_LIT)
  860.     sec_flags = (SEC_LOAD | SEC_ALLOC | SEC_READONLY);
  861. #endif /* STYP_LIT */
  862.  
  863. #ifdef STYP_OTHER_LOAD          /* Other loaded sections.  */
  864.   if (styp_flags & STYP_OTHER_LOAD)
  865.     sec_flags = (SEC_LOAD | SEC_ALLOC);
  866. #endif /* STYP_SDATA */
  867.  
  868. #if defined (COFF_LONG_SECTION_NAMES) && defined (COFF_SUPPORT_GNU_LINKONCE)
  869.   /* As a GNU extension, if the name begins with .gnu.linkonce, we
  870.      only link a single copy of the section.  This is used to support
  871.      g++.  g++ will emit each template expansion in its own section.
  872.      The symbols will be defined as weak, so that multiple definitions
  873.      are permitted.  The GNU linker extension is to actually discard
  874.      all but one of the sections.  */
  875.   if (CONST_STRNEQ (name, ".gnu.linkonce"))
  876.     sec_flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
  877. #endif
  878.  
  879.   if (flags_ptr == NULL)
  880.     return FALSE;
  881.  
  882.   * flags_ptr = sec_flags;
  883.   return TRUE;
  884. }
  885.  
  886. #else /* COFF_WITH_PE */
  887.  
  888. static flagword
  889. handle_COMDAT (bfd * abfd,
  890.                flagword sec_flags,
  891.                void * hdr,
  892.                const char *name,
  893.                asection *section)
  894. {
  895.   struct internal_scnhdr *internal_s = (struct internal_scnhdr *) hdr;
  896.   bfd_byte *esymstart, *esym, *esymend;
  897.   int seen_state = 0;
  898.   char *target_name = NULL;
  899.  
  900.   sec_flags |= SEC_LINK_ONCE;
  901.  
  902.   /* Unfortunately, the PE format stores essential information in
  903.      the symbol table, of all places.  We need to extract that
  904.      information now, so that objdump and the linker will know how
  905.      to handle the section without worrying about the symbols.  We
  906.      can't call slurp_symtab, because the linker doesn't want the
  907.      swapped symbols.  */
  908.  
  909.   /* COMDAT sections are special.  The first symbol is the section
  910.      symbol, which tells what kind of COMDAT section it is.  The
  911.      second symbol is the "comdat symbol" - the one with the
  912.      unique name.  GNU uses the section symbol for the unique
  913.      name; MS uses ".text" for every comdat section.  Sigh.  - DJ */
  914.  
  915.   /* This is not mirrored in sec_to_styp_flags(), but there
  916.      doesn't seem to be a need to, either, and it would at best be
  917.      rather messy.  */
  918.  
  919.   if (! _bfd_coff_get_external_symbols (abfd))
  920.     return sec_flags;
  921.  
  922.   esymstart = esym = (bfd_byte *) obj_coff_external_syms (abfd);
  923.   esymend = esym + obj_raw_syment_count (abfd) * bfd_coff_symesz (abfd);
  924.  
  925.   while (esym < esymend)
  926.     {
  927.       struct internal_syment isym;
  928.       char buf[SYMNMLEN + 1];
  929.       const char *symname;
  930.  
  931.       bfd_coff_swap_sym_in (abfd, esym, & isym);
  932.  
  933.       BFD_ASSERT (sizeof (internal_s->s_name) <= SYMNMLEN);
  934.  
  935.       if (isym.n_scnum == section->target_index)
  936.         {
  937.           /* According to the MSVC documentation, the first
  938.              TWO entries with the section # are both of
  939.              interest to us.  The first one is the "section
  940.              symbol" (section name).  The second is the comdat
  941.              symbol name.  Here, we've found the first
  942.              qualifying entry; we distinguish it from the
  943.              second with a state flag.
  944.  
  945.              In the case of gas-generated (at least until that
  946.              is fixed) .o files, it isn't necessarily the
  947.              second one.  It may be some other later symbol.
  948.  
  949.              Since gas also doesn't follow MS conventions and
  950.              emits the section similar to .text$<name>, where
  951.              <something> is the name we're looking for, we
  952.              distinguish the two as follows:
  953.  
  954.              If the section name is simply a section name (no
  955.              $) we presume it's MS-generated, and look at
  956.              precisely the second symbol for the comdat name.
  957.              If the section name has a $, we assume it's
  958.              gas-generated, and look for <something> (whatever
  959.              follows the $) as the comdat symbol.  */
  960.  
  961.           /* All 3 branches use this.  */
  962.           symname = _bfd_coff_internal_syment_name (abfd, &isym, buf);
  963.  
  964.           /* PR 17512 file: 078-11867-0.004  */
  965.           if (symname == NULL)
  966.             {
  967.               _bfd_error_handler (_("%B: unable to load COMDAT section name"), abfd);
  968.               break;
  969.             }
  970.  
  971.           switch (seen_state)
  972.             {
  973.             case 0:
  974.               {
  975.                 /* The first time we've seen the symbol.  */
  976.                 union internal_auxent aux;
  977.  
  978.                 /* If it isn't the stuff we're expecting, die;
  979.                    The MS documentation is vague, but it
  980.                    appears that the second entry serves BOTH
  981.                    as the comdat symbol and the defining
  982.                    symbol record (either C_STAT or C_EXT,
  983.                    possibly with an aux entry with debug
  984.                    information if it's a function.)  It
  985.                    appears the only way to find the second one
  986.                    is to count.  (On Intel, they appear to be
  987.                    adjacent, but on Alpha, they have been
  988.                    found separated.)
  989.  
  990.                    Here, we think we've found the first one,
  991.                    but there's some checking we can do to be
  992.                    sure.  */
  993.  
  994.                 if (! ((isym.n_sclass == C_STAT
  995.                         || isym.n_sclass == C_EXT)
  996.                        && BTYPE (isym.n_type) == T_NULL
  997.                        && isym.n_value == 0))
  998.                   abort ();
  999.  
  1000.                 /* FIXME LATER: MSVC generates section names
  1001.                    like .text for comdats.  Gas generates
  1002.                    names like .text$foo__Fv (in the case of a
  1003.                    function).  See comment above for more.  */
  1004.  
  1005.                 if (isym.n_sclass == C_STAT && strcmp (name, symname) != 0)
  1006.                   _bfd_error_handler (_("%B: warning: COMDAT symbol '%s' does not match section name '%s'"),
  1007.                                       abfd, symname, name);
  1008.  
  1009.                 seen_state = 1;
  1010.  
  1011.                 /* PR 17512: file: e2cfe54f.  */
  1012.                 if (esym + bfd_coff_symesz (abfd) >= esymend)
  1013.                   {
  1014.                     _bfd_error_handler (_("%B: warning: No symbol for section '%s' found"),
  1015.                                         abfd, symname);
  1016.                     break;
  1017.                   }
  1018.                 /* This is the section symbol.  */
  1019.                 bfd_coff_swap_aux_in (abfd, (esym + bfd_coff_symesz (abfd)),
  1020.                                       isym.n_type, isym.n_sclass,
  1021.                                       0, isym.n_numaux, & aux);
  1022.  
  1023.                 target_name = strchr (name, '$');
  1024.                 if (target_name != NULL)
  1025.                   {
  1026.                     /* Gas mode.  */
  1027.                     seen_state = 2;
  1028.                     /* Skip the `$'.  */
  1029.                     target_name += 1;
  1030.                   }
  1031.  
  1032.                 /* FIXME: Microsoft uses NODUPLICATES and
  1033.                    ASSOCIATIVE, but gnu uses ANY and
  1034.                    SAME_SIZE.  Unfortunately, gnu doesn't do
  1035.                    the comdat symbols right.  So, until we can
  1036.                    fix it to do the right thing, we are
  1037.                    temporarily disabling comdats for the MS
  1038.                    types (they're used in DLLs and C++, but we
  1039.                    don't support *their* C++ libraries anyway
  1040.                    - DJ.  */
  1041.  
  1042.                 /* Cygwin does not follow the MS style, and
  1043.                    uses ANY and SAME_SIZE where NODUPLICATES
  1044.                    and ASSOCIATIVE should be used.  For
  1045.                    Interix, we just do the right thing up
  1046.                    front.  */
  1047.  
  1048.                 switch (aux.x_scn.x_comdat)
  1049.                   {
  1050.                   case IMAGE_COMDAT_SELECT_NODUPLICATES:
  1051. #ifdef STRICT_PE_FORMAT
  1052.                     sec_flags |= SEC_LINK_DUPLICATES_ONE_ONLY;
  1053. #else
  1054.                     sec_flags &= ~SEC_LINK_ONCE;
  1055. #endif
  1056.                     break;
  1057.  
  1058.                   case IMAGE_COMDAT_SELECT_ANY:
  1059.                     sec_flags |= SEC_LINK_DUPLICATES_DISCARD;
  1060.                     break;
  1061.  
  1062.                   case IMAGE_COMDAT_SELECT_SAME_SIZE:
  1063.                     sec_flags |= SEC_LINK_DUPLICATES_SAME_SIZE;
  1064.                     break;
  1065.  
  1066.                   case IMAGE_COMDAT_SELECT_EXACT_MATCH:
  1067.                     /* Not yet fully implemented ??? */
  1068.                     sec_flags |= SEC_LINK_DUPLICATES_SAME_CONTENTS;
  1069.                     break;
  1070.  
  1071.                     /* debug$S gets this case; other
  1072.                        implications ??? */
  1073.  
  1074.                     /* There may be no symbol... we'll search
  1075.                        the whole table... Is this the right
  1076.                        place to play this game? Or should we do
  1077.                        it when reading it in.  */
  1078.                   case IMAGE_COMDAT_SELECT_ASSOCIATIVE:
  1079. #ifdef STRICT_PE_FORMAT
  1080.                     /* FIXME: This is not currently implemented.  */
  1081.                     sec_flags |= SEC_LINK_DUPLICATES_DISCARD;
  1082. #else
  1083.                     sec_flags &= ~SEC_LINK_ONCE;
  1084. #endif
  1085.                     break;
  1086.  
  1087.                   default:  /* 0 means "no symbol" */
  1088.                     /* debug$F gets this case; other
  1089.                        implications ??? */
  1090.                     sec_flags |= SEC_LINK_DUPLICATES_DISCARD;
  1091.                     break;
  1092.                   }
  1093.               }
  1094.               break;
  1095.  
  1096.             case 2:
  1097.               /* Gas mode: the first matching on partial name.  */
  1098.  
  1099. #ifndef TARGET_UNDERSCORE
  1100. #define TARGET_UNDERSCORE 0
  1101. #endif
  1102.               /* Is this the name we're looking for ?  */
  1103.               if (strcmp (target_name,
  1104.                           symname + (TARGET_UNDERSCORE ? 1 : 0)) != 0)
  1105.                 {
  1106.                   /* Not the name we're looking for */
  1107.                   esym += (isym.n_numaux + 1) * bfd_coff_symesz (abfd);
  1108.                   continue;
  1109.                 }
  1110.               /* Fall through.  */
  1111.             case 1:
  1112.               /* MSVC mode: the lexically second symbol (or
  1113.                  drop through from the above).  */
  1114.               {
  1115.                 char *newname;
  1116.                 bfd_size_type amt;
  1117.  
  1118.                 /* This must the second symbol with the
  1119.                    section #.  It is the actual symbol name.
  1120.                    Intel puts the two adjacent, but Alpha (at
  1121.                    least) spreads them out.  */
  1122.  
  1123.                 amt = sizeof (struct coff_comdat_info);
  1124.                 coff_section_data (abfd, section)->comdat
  1125.                   = (struct coff_comdat_info *) bfd_alloc (abfd, amt);
  1126.                 if (coff_section_data (abfd, section)->comdat == NULL)
  1127.                   abort ();
  1128.  
  1129.                 coff_section_data (abfd, section)->comdat->symbol =
  1130.                   (esym - esymstart) / bfd_coff_symesz (abfd);
  1131.  
  1132.                 amt = strlen (symname) + 1;
  1133.                 newname = (char *) bfd_alloc (abfd, amt);
  1134.                 if (newname == NULL)
  1135.                   abort ();
  1136.  
  1137.                 strcpy (newname, symname);
  1138.                 coff_section_data (abfd, section)->comdat->name
  1139.                   = newname;
  1140.               }
  1141.  
  1142.               goto breakloop;
  1143.             }
  1144.         }
  1145.  
  1146.       esym += (isym.n_numaux + 1) * bfd_coff_symesz (abfd);
  1147.     }
  1148.  
  1149.  breakloop:
  1150.   return sec_flags;
  1151. }
  1152.  
  1153.  
  1154. /* The PE version; see above for the general comments.
  1155.  
  1156.    Since to set the SEC_LINK_ONCE and associated flags, we have to
  1157.    look at the symbol table anyway, we return the symbol table index
  1158.    of the symbol being used as the COMDAT symbol.  This is admittedly
  1159.    ugly, but there's really nowhere else that we have access to the
  1160.    required information.  FIXME: Is the COMDAT symbol index used for
  1161.    any purpose other than objdump?  */
  1162.  
  1163. static bfd_boolean
  1164. styp_to_sec_flags (bfd *abfd,
  1165.                    void * hdr,
  1166.                    const char *name,
  1167.                    asection *section,
  1168.                    flagword *flags_ptr)
  1169. {
  1170.   struct internal_scnhdr *internal_s = (struct internal_scnhdr *) hdr;
  1171.   unsigned long styp_flags = internal_s->s_flags;
  1172.   flagword sec_flags;
  1173.   bfd_boolean result = TRUE;
  1174.   bfd_boolean is_dbg = FALSE;
  1175.  
  1176.   if (CONST_STRNEQ (name, DOT_DEBUG)
  1177.       || CONST_STRNEQ (name, DOT_ZDEBUG)
  1178. #ifdef COFF_LONG_SECTION_NAMES
  1179.       || CONST_STRNEQ (name, GNU_LINKONCE_WI)
  1180.       || CONST_STRNEQ (name, GNU_LINKONCE_WT)
  1181. #endif
  1182.       || CONST_STRNEQ (name, ".stab"))
  1183.     is_dbg = TRUE;
  1184.   /* Assume read only unless IMAGE_SCN_MEM_WRITE is specified.  */
  1185.   sec_flags = SEC_READONLY;
  1186.  
  1187.   /* If section disallows read, then set the NOREAD flag. */
  1188.   if ((styp_flags & IMAGE_SCN_MEM_READ) == 0)
  1189.     sec_flags |= SEC_COFF_NOREAD;
  1190.  
  1191.   /* Process each flag bit in styp_flags in turn.  */
  1192.   while (styp_flags)
  1193.     {
  1194.       unsigned long flag = styp_flags & - styp_flags;
  1195.       char * unhandled = NULL;
  1196.  
  1197.       styp_flags &= ~ flag;
  1198.  
  1199.       /* We infer from the distinct read/write/execute bits the settings
  1200.          of some of the bfd flags; the actual values, should we need them,
  1201.          are also in pei_section_data (abfd, section)->pe_flags.  */
  1202.  
  1203.       switch (flag)
  1204.         {
  1205.         case STYP_DSECT:
  1206.           unhandled = "STYP_DSECT";
  1207.           break;
  1208.         case STYP_GROUP:
  1209.           unhandled = "STYP_GROUP";
  1210.           break;
  1211.         case STYP_COPY:
  1212.           unhandled = "STYP_COPY";
  1213.           break;
  1214.         case STYP_OVER:
  1215.           unhandled = "STYP_OVER";
  1216.           break;
  1217. #ifdef SEC_NEVER_LOAD
  1218.         case STYP_NOLOAD:
  1219.           sec_flags |= SEC_NEVER_LOAD;
  1220.           break;
  1221. #endif
  1222.         case IMAGE_SCN_MEM_READ:
  1223.           sec_flags &= ~SEC_COFF_NOREAD;
  1224.           break;
  1225.         case IMAGE_SCN_TYPE_NO_PAD:
  1226.           /* Skip.  */
  1227.           break;
  1228.         case IMAGE_SCN_LNK_OTHER:
  1229.           unhandled = "IMAGE_SCN_LNK_OTHER";
  1230.           break;
  1231.         case IMAGE_SCN_MEM_NOT_CACHED:
  1232.           unhandled = "IMAGE_SCN_MEM_NOT_CACHED";
  1233.           break;
  1234.         case IMAGE_SCN_MEM_NOT_PAGED:
  1235.           /* Generate a warning message rather using the 'unhandled'
  1236.              variable as this will allow some .sys files generate by
  1237.              other toolchains to be processed.  See bugzilla issue 196.  */
  1238.           _bfd_error_handler (_("%B: Warning: Ignoring section flag IMAGE_SCN_MEM_NOT_PAGED in section %s"),
  1239.                               abfd, name);
  1240.           break;
  1241.         case IMAGE_SCN_MEM_EXECUTE:
  1242.           sec_flags |= SEC_CODE;
  1243.           break;
  1244.         case IMAGE_SCN_MEM_WRITE:
  1245.           sec_flags &= ~ SEC_READONLY;
  1246.           break;
  1247.         case IMAGE_SCN_MEM_DISCARDABLE:
  1248.           /* The MS PE spec says that debug sections are DISCARDABLE,
  1249.              but the presence of a DISCARDABLE flag does not necessarily
  1250.              mean that a given section contains debug information.  Thus
  1251.              we only set the SEC_DEBUGGING flag on sections that we
  1252.              recognise as containing debug information.  */
  1253.              if (is_dbg
  1254. #ifdef _COMMENT
  1255.               || strcmp (name, _COMMENT) == 0
  1256. #endif
  1257.               )
  1258.             {
  1259.               sec_flags |= SEC_DEBUGGING | SEC_READONLY;
  1260.             }
  1261.           break;
  1262.         case IMAGE_SCN_MEM_SHARED:
  1263.           sec_flags |= SEC_COFF_SHARED;
  1264.           break;
  1265.         case IMAGE_SCN_LNK_REMOVE:
  1266.           if (!is_dbg)
  1267.             sec_flags |= SEC_EXCLUDE;
  1268.           break;
  1269.         case IMAGE_SCN_CNT_CODE:
  1270.           sec_flags |= SEC_CODE | SEC_ALLOC | SEC_LOAD;
  1271.           break;
  1272.         case IMAGE_SCN_CNT_INITIALIZED_DATA:
  1273.           if (is_dbg)
  1274.             sec_flags |= SEC_DEBUGGING;
  1275.           else
  1276.             sec_flags |= SEC_DATA | SEC_ALLOC | SEC_LOAD;
  1277.           break;
  1278.         case IMAGE_SCN_CNT_UNINITIALIZED_DATA:
  1279.           sec_flags |= SEC_ALLOC;
  1280.           break;
  1281.         case IMAGE_SCN_LNK_INFO:
  1282.           /* We mark these as SEC_DEBUGGING, but only if COFF_PAGE_SIZE is
  1283.              defined.  coff_compute_section_file_positions uses
  1284.              COFF_PAGE_SIZE to ensure that the low order bits of the
  1285.              section VMA and the file offset match.  If we don't know
  1286.              COFF_PAGE_SIZE, we can't ensure the correct correspondence,
  1287.              and demand page loading of the file will fail.  */
  1288. #ifdef COFF_PAGE_SIZE
  1289.           sec_flags |= SEC_DEBUGGING;
  1290. #endif
  1291.           break;
  1292.         case IMAGE_SCN_LNK_COMDAT:
  1293.           /* COMDAT gets very special treatment.  */
  1294.           sec_flags = handle_COMDAT (abfd, sec_flags, hdr, name, section);
  1295.           break;
  1296.         default:
  1297.           /* Silently ignore for now.  */
  1298.           break;
  1299.         }
  1300.  
  1301.       /* If the section flag was not handled, report it here.  */
  1302.       if (unhandled != NULL)
  1303.         {
  1304.           (*_bfd_error_handler)
  1305.             (_("%B (%s): Section flag %s (0x%x) ignored"),
  1306.              abfd, name, unhandled, flag);
  1307.           result = FALSE;
  1308.         }
  1309.     }
  1310.  
  1311. #if defined (COFF_LONG_SECTION_NAMES) && defined (COFF_SUPPORT_GNU_LINKONCE)
  1312.   /* As a GNU extension, if the name begins with .gnu.linkonce, we
  1313.      only link a single copy of the section.  This is used to support
  1314.      g++.  g++ will emit each template expansion in its own section.
  1315.      The symbols will be defined as weak, so that multiple definitions
  1316.      are permitted.  The GNU linker extension is to actually discard
  1317.      all but one of the sections.  */
  1318.   if (CONST_STRNEQ (name, ".gnu.linkonce"))
  1319.     sec_flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
  1320. #endif
  1321.  
  1322.   if (flags_ptr)
  1323.     * flags_ptr = sec_flags;
  1324.  
  1325.   return result;
  1326. }
  1327.  
  1328. #endif /* COFF_WITH_PE */
  1329.  
  1330. #define get_index(symbol)       ((symbol)->udata.i)
  1331.  
  1332. /*
  1333. INTERNAL_DEFINITION
  1334.         bfd_coff_backend_data
  1335.  
  1336. CODE_FRAGMENT
  1337.  
  1338. .{* COFF symbol classifications.  *}
  1339. .
  1340. .enum coff_symbol_classification
  1341. .{
  1342. .  {* Global symbol.  *}
  1343. .  COFF_SYMBOL_GLOBAL,
  1344. .  {* Common symbol.  *}
  1345. .  COFF_SYMBOL_COMMON,
  1346. .  {* Undefined symbol.  *}
  1347. .  COFF_SYMBOL_UNDEFINED,
  1348. .  {* Local symbol.  *}
  1349. .  COFF_SYMBOL_LOCAL,
  1350. .  {* PE section symbol.  *}
  1351. .  COFF_SYMBOL_PE_SECTION
  1352. .};
  1353. .
  1354. .typedef asection * (*coff_gc_mark_hook_fn)
  1355. .  (asection *, struct bfd_link_info *, struct internal_reloc *,
  1356. .   struct coff_link_hash_entry *, struct internal_syment *);
  1357. .
  1358. Special entry points for gdb to swap in coff symbol table parts:
  1359. .typedef struct
  1360. .{
  1361. .  void (*_bfd_coff_swap_aux_in)
  1362. .    (bfd *, void *, int, int, int, int, void *);
  1363. .
  1364. .  void (*_bfd_coff_swap_sym_in)
  1365. .    (bfd *, void *, void *);
  1366. .
  1367. .  void (*_bfd_coff_swap_lineno_in)
  1368. .    (bfd *, void *, void *);
  1369. .
  1370. .  unsigned int (*_bfd_coff_swap_aux_out)
  1371. .    (bfd *, void *, int, int, int, int, void *);
  1372. .
  1373. .  unsigned int (*_bfd_coff_swap_sym_out)
  1374. .    (bfd *, void *, void *);
  1375. .
  1376. .  unsigned int (*_bfd_coff_swap_lineno_out)
  1377. .    (bfd *, void *, void *);
  1378. .
  1379. .  unsigned int (*_bfd_coff_swap_reloc_out)
  1380. .    (bfd *, void *, void *);
  1381. .
  1382. .  unsigned int (*_bfd_coff_swap_filehdr_out)
  1383. .    (bfd *, void *, void *);
  1384. .
  1385. .  unsigned int (*_bfd_coff_swap_aouthdr_out)
  1386. .    (bfd *, void *, void *);
  1387. .
  1388. .  unsigned int (*_bfd_coff_swap_scnhdr_out)
  1389. .    (bfd *, void *, void *);
  1390. .
  1391. .  unsigned int _bfd_filhsz;
  1392. .  unsigned int _bfd_aoutsz;
  1393. .  unsigned int _bfd_scnhsz;
  1394. .  unsigned int _bfd_symesz;
  1395. .  unsigned int _bfd_auxesz;
  1396. .  unsigned int _bfd_relsz;
  1397. .  unsigned int _bfd_linesz;
  1398. .  unsigned int _bfd_filnmlen;
  1399. .  bfd_boolean _bfd_coff_long_filenames;
  1400. .
  1401. .  bfd_boolean _bfd_coff_long_section_names;
  1402. .  bfd_boolean (*_bfd_coff_set_long_section_names)
  1403. .    (bfd *, int);
  1404. .
  1405. .  unsigned int _bfd_coff_default_section_alignment_power;
  1406. .  bfd_boolean _bfd_coff_force_symnames_in_strings;
  1407. .  unsigned int _bfd_coff_debug_string_prefix_length;
  1408. .  unsigned int _bfd_coff_max_nscns;
  1409. .
  1410. .  void (*_bfd_coff_swap_filehdr_in)
  1411. .    (bfd *, void *, void *);
  1412. .
  1413. .  void (*_bfd_coff_swap_aouthdr_in)
  1414. .    (bfd *, void *, void *);
  1415. .
  1416. .  void (*_bfd_coff_swap_scnhdr_in)
  1417. .    (bfd *, void *, void *);
  1418. .
  1419. .  void (*_bfd_coff_swap_reloc_in)
  1420. .    (bfd *abfd, void *, void *);
  1421. .
  1422. .  bfd_boolean (*_bfd_coff_bad_format_hook)
  1423. .    (bfd *, void *);
  1424. .
  1425. .  bfd_boolean (*_bfd_coff_set_arch_mach_hook)
  1426. .    (bfd *, void *);
  1427. .
  1428. .  void * (*_bfd_coff_mkobject_hook)
  1429. .    (bfd *, void *, void *);
  1430. .
  1431. .  bfd_boolean (*_bfd_styp_to_sec_flags_hook)
  1432. .    (bfd *, void *, const char *, asection *, flagword *);
  1433. .
  1434. .  void (*_bfd_set_alignment_hook)
  1435. .    (bfd *, asection *, void *);
  1436. .
  1437. .  bfd_boolean (*_bfd_coff_slurp_symbol_table)
  1438. .    (bfd *);
  1439. .
  1440. .  bfd_boolean (*_bfd_coff_symname_in_debug)
  1441. .    (bfd *, struct internal_syment *);
  1442. .
  1443. .  bfd_boolean (*_bfd_coff_pointerize_aux_hook)
  1444. .    (bfd *, combined_entry_type *, combined_entry_type *,
  1445. .            unsigned int, combined_entry_type *);
  1446. .
  1447. .  bfd_boolean (*_bfd_coff_print_aux)
  1448. .    (bfd *, FILE *, combined_entry_type *, combined_entry_type *,
  1449. .            combined_entry_type *, unsigned int);
  1450. .
  1451. .  void (*_bfd_coff_reloc16_extra_cases)
  1452. .    (bfd *, struct bfd_link_info *, struct bfd_link_order *, arelent *,
  1453. .           bfd_byte *, unsigned int *, unsigned int *);
  1454. .
  1455. .  int (*_bfd_coff_reloc16_estimate)
  1456. .    (bfd *, asection *, arelent *, unsigned int,
  1457. .            struct bfd_link_info *);
  1458. .
  1459. .  enum coff_symbol_classification (*_bfd_coff_classify_symbol)
  1460. .    (bfd *, struct internal_syment *);
  1461. .
  1462. .  bfd_boolean (*_bfd_coff_compute_section_file_positions)
  1463. .    (bfd *);
  1464. .
  1465. .  bfd_boolean (*_bfd_coff_start_final_link)
  1466. .    (bfd *, struct bfd_link_info *);
  1467. .
  1468. .  bfd_boolean (*_bfd_coff_relocate_section)
  1469. .    (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
  1470. .            struct internal_reloc *, struct internal_syment *, asection **);
  1471. .
  1472. .  reloc_howto_type *(*_bfd_coff_rtype_to_howto)
  1473. .    (bfd *, asection *, struct internal_reloc *,
  1474. .            struct coff_link_hash_entry *, struct internal_syment *,
  1475. .            bfd_vma *);
  1476. .
  1477. .  bfd_boolean (*_bfd_coff_adjust_symndx)
  1478. .    (bfd *, struct bfd_link_info *, bfd *, asection *,
  1479. .            struct internal_reloc *, bfd_boolean *);
  1480. .
  1481. .  bfd_boolean (*_bfd_coff_link_add_one_symbol)
  1482. .    (struct bfd_link_info *, bfd *, const char *, flagword,
  1483. .            asection *, bfd_vma, const char *, bfd_boolean, bfd_boolean,
  1484. .            struct bfd_link_hash_entry **);
  1485. .
  1486. .  bfd_boolean (*_bfd_coff_link_output_has_begun)
  1487. .    (bfd *, struct coff_final_link_info *);
  1488. .
  1489. .  bfd_boolean (*_bfd_coff_final_link_postscript)
  1490. .    (bfd *, struct coff_final_link_info *);
  1491. .
  1492. .  bfd_boolean (*_bfd_coff_print_pdata)
  1493. .    (bfd *, void *);
  1494. .
  1495. .} bfd_coff_backend_data;
  1496. .
  1497. .#define coff_backend_info(abfd) \
  1498. .  ((bfd_coff_backend_data *) (abfd)->xvec->backend_data)
  1499. .
  1500. .#define bfd_coff_swap_aux_in(a,e,t,c,ind,num,i) \
  1501. .  ((coff_backend_info (a)->_bfd_coff_swap_aux_in) (a,e,t,c,ind,num,i))
  1502. .
  1503. .#define bfd_coff_swap_sym_in(a,e,i) \
  1504. .  ((coff_backend_info (a)->_bfd_coff_swap_sym_in) (a,e,i))
  1505. .
  1506. .#define bfd_coff_swap_lineno_in(a,e,i) \
  1507. .  ((coff_backend_info ( a)->_bfd_coff_swap_lineno_in) (a,e,i))
  1508. .
  1509. .#define bfd_coff_swap_reloc_out(abfd, i, o) \
  1510. .  ((coff_backend_info (abfd)->_bfd_coff_swap_reloc_out) (abfd, i, o))
  1511. .
  1512. .#define bfd_coff_swap_lineno_out(abfd, i, o) \
  1513. .  ((coff_backend_info (abfd)->_bfd_coff_swap_lineno_out) (abfd, i, o))
  1514. .
  1515. .#define bfd_coff_swap_aux_out(a,i,t,c,ind,num,o) \
  1516. .  ((coff_backend_info (a)->_bfd_coff_swap_aux_out) (a,i,t,c,ind,num,o))
  1517. .
  1518. .#define bfd_coff_swap_sym_out(abfd, i,o) \
  1519. .  ((coff_backend_info (abfd)->_bfd_coff_swap_sym_out) (abfd, i, o))
  1520. .
  1521. .#define bfd_coff_swap_scnhdr_out(abfd, i,o) \
  1522. .  ((coff_backend_info (abfd)->_bfd_coff_swap_scnhdr_out) (abfd, i, o))
  1523. .
  1524. .#define bfd_coff_swap_filehdr_out(abfd, i,o) \
  1525. .  ((coff_backend_info (abfd)->_bfd_coff_swap_filehdr_out) (abfd, i, o))
  1526. .
  1527. .#define bfd_coff_swap_aouthdr_out(abfd, i,o) \
  1528. .  ((coff_backend_info (abfd)->_bfd_coff_swap_aouthdr_out) (abfd, i, o))
  1529. .
  1530. .#define bfd_coff_filhsz(abfd) (coff_backend_info (abfd)->_bfd_filhsz)
  1531. .#define bfd_coff_aoutsz(abfd) (coff_backend_info (abfd)->_bfd_aoutsz)
  1532. .#define bfd_coff_scnhsz(abfd) (coff_backend_info (abfd)->_bfd_scnhsz)
  1533. .#define bfd_coff_symesz(abfd) (coff_backend_info (abfd)->_bfd_symesz)
  1534. .#define bfd_coff_auxesz(abfd) (coff_backend_info (abfd)->_bfd_auxesz)
  1535. .#define bfd_coff_relsz(abfd)  (coff_backend_info (abfd)->_bfd_relsz)
  1536. .#define bfd_coff_linesz(abfd) (coff_backend_info (abfd)->_bfd_linesz)
  1537. .#define bfd_coff_filnmlen(abfd) (coff_backend_info (abfd)->_bfd_filnmlen)
  1538. .#define bfd_coff_long_filenames(abfd) \
  1539. .  (coff_backend_info (abfd)->_bfd_coff_long_filenames)
  1540. .#define bfd_coff_long_section_names(abfd) \
  1541. .  (coff_backend_info (abfd)->_bfd_coff_long_section_names)
  1542. .#define bfd_coff_set_long_section_names(abfd, enable) \
  1543. .  ((coff_backend_info (abfd)->_bfd_coff_set_long_section_names) (abfd, enable))
  1544. .#define bfd_coff_default_section_alignment_power(abfd) \
  1545. .  (coff_backend_info (abfd)->_bfd_coff_default_section_alignment_power)
  1546. .#define bfd_coff_max_nscns(abfd) \
  1547. .  (coff_backend_info (abfd)->_bfd_coff_max_nscns)
  1548. .
  1549. .#define bfd_coff_swap_filehdr_in(abfd, i,o) \
  1550. .  ((coff_backend_info (abfd)->_bfd_coff_swap_filehdr_in) (abfd, i, o))
  1551. .
  1552. .#define bfd_coff_swap_aouthdr_in(abfd, i,o) \
  1553. .  ((coff_backend_info (abfd)->_bfd_coff_swap_aouthdr_in) (abfd, i, o))
  1554. .
  1555. .#define bfd_coff_swap_scnhdr_in(abfd, i,o) \
  1556. .  ((coff_backend_info (abfd)->_bfd_coff_swap_scnhdr_in) (abfd, i, o))
  1557. .
  1558. .#define bfd_coff_swap_reloc_in(abfd, i, o) \
  1559. .  ((coff_backend_info (abfd)->_bfd_coff_swap_reloc_in) (abfd, i, o))
  1560. .
  1561. .#define bfd_coff_bad_format_hook(abfd, filehdr) \
  1562. .  ((coff_backend_info (abfd)->_bfd_coff_bad_format_hook) (abfd, filehdr))
  1563. .
  1564. .#define bfd_coff_set_arch_mach_hook(abfd, filehdr)\
  1565. .  ((coff_backend_info (abfd)->_bfd_coff_set_arch_mach_hook) (abfd, filehdr))
  1566. .#define bfd_coff_mkobject_hook(abfd, filehdr, aouthdr)\
  1567. .  ((coff_backend_info (abfd)->_bfd_coff_mkobject_hook)\
  1568. .   (abfd, filehdr, aouthdr))
  1569. .
  1570. .#define bfd_coff_styp_to_sec_flags_hook(abfd, scnhdr, name, section, flags_ptr)\
  1571. .  ((coff_backend_info (abfd)->_bfd_styp_to_sec_flags_hook)\
  1572. .   (abfd, scnhdr, name, section, flags_ptr))
  1573. .
  1574. .#define bfd_coff_set_alignment_hook(abfd, sec, scnhdr)\
  1575. .  ((coff_backend_info (abfd)->_bfd_set_alignment_hook) (abfd, sec, scnhdr))
  1576. .
  1577. .#define bfd_coff_slurp_symbol_table(abfd)\
  1578. .  ((coff_backend_info (abfd)->_bfd_coff_slurp_symbol_table) (abfd))
  1579. .
  1580. .#define bfd_coff_symname_in_debug(abfd, sym)\
  1581. .  ((coff_backend_info (abfd)->_bfd_coff_symname_in_debug) (abfd, sym))
  1582. .
  1583. .#define bfd_coff_force_symnames_in_strings(abfd)\
  1584. .  (coff_backend_info (abfd)->_bfd_coff_force_symnames_in_strings)
  1585. .
  1586. .#define bfd_coff_debug_string_prefix_length(abfd)\
  1587. .  (coff_backend_info (abfd)->_bfd_coff_debug_string_prefix_length)
  1588. .
  1589. .#define bfd_coff_print_aux(abfd, file, base, symbol, aux, indaux)\
  1590. .  ((coff_backend_info (abfd)->_bfd_coff_print_aux)\
  1591. .   (abfd, file, base, symbol, aux, indaux))
  1592. .
  1593. .#define bfd_coff_reloc16_extra_cases(abfd, link_info, link_order,\
  1594. .                                     reloc, data, src_ptr, dst_ptr)\
  1595. .  ((coff_backend_info (abfd)->_bfd_coff_reloc16_extra_cases)\
  1596. .   (abfd, link_info, link_order, reloc, data, src_ptr, dst_ptr))
  1597. .
  1598. .#define bfd_coff_reloc16_estimate(abfd, section, reloc, shrink, link_info)\
  1599. .  ((coff_backend_info (abfd)->_bfd_coff_reloc16_estimate)\
  1600. .   (abfd, section, reloc, shrink, link_info))
  1601. .
  1602. .#define bfd_coff_classify_symbol(abfd, sym)\
  1603. .  ((coff_backend_info (abfd)->_bfd_coff_classify_symbol)\
  1604. .   (abfd, sym))
  1605. .
  1606. .#define bfd_coff_compute_section_file_positions(abfd)\
  1607. .  ((coff_backend_info (abfd)->_bfd_coff_compute_section_file_positions)\
  1608. .   (abfd))
  1609. .
  1610. .#define bfd_coff_start_final_link(obfd, info)\
  1611. .  ((coff_backend_info (obfd)->_bfd_coff_start_final_link)\
  1612. .   (obfd, info))
  1613. .#define bfd_coff_relocate_section(obfd,info,ibfd,o,con,rel,isyms,secs)\
  1614. .  ((coff_backend_info (ibfd)->_bfd_coff_relocate_section)\
  1615. .   (obfd, info, ibfd, o, con, rel, isyms, secs))
  1616. .#define bfd_coff_rtype_to_howto(abfd, sec, rel, h, sym, addendp)\
  1617. .  ((coff_backend_info (abfd)->_bfd_coff_rtype_to_howto)\
  1618. .   (abfd, sec, rel, h, sym, addendp))
  1619. .#define bfd_coff_adjust_symndx(obfd, info, ibfd, sec, rel, adjustedp)\
  1620. .  ((coff_backend_info (abfd)->_bfd_coff_adjust_symndx)\
  1621. .   (obfd, info, ibfd, sec, rel, adjustedp))
  1622. .#define bfd_coff_link_add_one_symbol(info, abfd, name, flags, section,\
  1623. .                                     value, string, cp, coll, hashp)\
  1624. .  ((coff_backend_info (abfd)->_bfd_coff_link_add_one_symbol)\
  1625. .   (info, abfd, name, flags, section, value, string, cp, coll, hashp))
  1626. .
  1627. .#define bfd_coff_link_output_has_begun(a,p) \
  1628. .  ((coff_backend_info (a)->_bfd_coff_link_output_has_begun) (a, p))
  1629. .#define bfd_coff_final_link_postscript(a,p) \
  1630. .  ((coff_backend_info (a)->_bfd_coff_final_link_postscript) (a, p))
  1631. .
  1632. .#define bfd_coff_have_print_pdata(a) \
  1633. .  (coff_backend_info (a)->_bfd_coff_print_pdata)
  1634. .#define bfd_coff_print_pdata(a,p) \
  1635. .  ((coff_backend_info (a)->_bfd_coff_print_pdata) (a, p))
  1636. .
  1637. .{* Macro: Returns true if the bfd is a PE executable as opposed to a
  1638. .   PE object file.  *}
  1639. .#define bfd_pei_p(abfd) \
  1640. .  (CONST_STRNEQ ((abfd)->xvec->name, "pei-"))
  1641. */
  1642.  
  1643. /* See whether the magic number matches.  */
  1644.  
  1645. static bfd_boolean
  1646. coff_bad_format_hook (bfd * abfd ATTRIBUTE_UNUSED, void * filehdr)
  1647. {
  1648.   struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
  1649.  
  1650.   if (BADMAG (*internal_f))
  1651.     return FALSE;
  1652.  
  1653.   /* If the optional header is NULL or not the correct size then
  1654.      quit; the only difference I can see between m88k dgux headers (MC88DMAGIC)
  1655.      and Intel 960 readwrite headers (I960WRMAGIC) is that the
  1656.      optional header is of a different size.
  1657.  
  1658.      But the mips keeps extra stuff in it's opthdr, so dont check
  1659.      when doing that.  */
  1660.  
  1661. #if defined(M88) || defined(I960)
  1662.   if (internal_f->f_opthdr != 0 && bfd_coff_aoutsz (abfd) != internal_f->f_opthdr)
  1663.     return FALSE;
  1664. #endif
  1665.  
  1666.   return TRUE;
  1667. }
  1668.  
  1669. #ifdef TICOFF
  1670. static bfd_boolean
  1671. ticoff0_bad_format_hook (bfd *abfd ATTRIBUTE_UNUSED, void * filehdr)
  1672. {
  1673.   struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
  1674.  
  1675.   if (COFF0_BADMAG (*internal_f))
  1676.     return FALSE;
  1677.  
  1678.   return TRUE;
  1679. }
  1680. #endif
  1681.  
  1682. #ifdef TICOFF
  1683. static bfd_boolean
  1684. ticoff1_bad_format_hook (bfd *abfd ATTRIBUTE_UNUSED, void * filehdr)
  1685. {
  1686.   struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
  1687.  
  1688.   if (COFF1_BADMAG (*internal_f))
  1689.     return FALSE;
  1690.  
  1691.   return TRUE;
  1692. }
  1693. #endif
  1694.  
  1695. /* Check whether this section uses an alignment other than the
  1696.    default.  */
  1697.  
  1698. static void
  1699. coff_set_custom_section_alignment (bfd *abfd ATTRIBUTE_UNUSED,
  1700.                                    asection *section,
  1701.                                    const struct coff_section_alignment_entry *alignment_table,
  1702.                                    const unsigned int table_size)
  1703. {
  1704.   const unsigned int default_alignment = COFF_DEFAULT_SECTION_ALIGNMENT_POWER;
  1705.   unsigned int i;
  1706.  
  1707.   for (i = 0; i < table_size; ++i)
  1708.     {
  1709.       const char *secname = bfd_get_section_name (abfd, section);
  1710.  
  1711.       if (alignment_table[i].comparison_length == (unsigned int) -1
  1712.           ? strcmp (alignment_table[i].name, secname) == 0
  1713.           : strncmp (alignment_table[i].name, secname,
  1714.                      alignment_table[i].comparison_length) == 0)
  1715.         break;
  1716.     }
  1717.   if (i >= table_size)
  1718.     return;
  1719.  
  1720.   if (alignment_table[i].default_alignment_min != COFF_ALIGNMENT_FIELD_EMPTY
  1721.       && default_alignment < alignment_table[i].default_alignment_min)
  1722.     return;
  1723.  
  1724.   if (alignment_table[i].default_alignment_max != COFF_ALIGNMENT_FIELD_EMPTY
  1725. #if COFF_DEFAULT_SECTION_ALIGNMENT_POWER != 0
  1726.       && default_alignment > alignment_table[i].default_alignment_max
  1727. #endif
  1728.       )
  1729.     return;
  1730.  
  1731.   section->alignment_power = alignment_table[i].alignment_power;
  1732. }
  1733.  
  1734. /* Custom section alignment records.  */
  1735.  
  1736. static const struct coff_section_alignment_entry
  1737. coff_section_alignment_table[] =
  1738. {
  1739. #ifdef COFF_SECTION_ALIGNMENT_ENTRIES
  1740.   COFF_SECTION_ALIGNMENT_ENTRIES,
  1741. #endif
  1742.   /* There must not be any gaps between .stabstr sections.  */
  1743.   { COFF_SECTION_NAME_PARTIAL_MATCH (".stabstr"),
  1744.     1, COFF_ALIGNMENT_FIELD_EMPTY, 0 },
  1745.   /* The .stab section must be aligned to 2**2 at most, to avoid gaps.  */
  1746.   { COFF_SECTION_NAME_PARTIAL_MATCH (".stab"),
  1747.     3, COFF_ALIGNMENT_FIELD_EMPTY, 2 },
  1748.   /* Similarly for the .ctors and .dtors sections.  */
  1749.   { COFF_SECTION_NAME_EXACT_MATCH (".ctors"),
  1750.     3, COFF_ALIGNMENT_FIELD_EMPTY, 2 },
  1751.   { COFF_SECTION_NAME_EXACT_MATCH (".dtors"),
  1752.     3, COFF_ALIGNMENT_FIELD_EMPTY, 2 }
  1753. };
  1754.  
  1755. static const unsigned int coff_section_alignment_table_size =
  1756.   sizeof coff_section_alignment_table / sizeof coff_section_alignment_table[0];
  1757.  
  1758. /* Initialize a section structure with information peculiar to this
  1759.    particular implementation of COFF.  */
  1760.  
  1761. static bfd_boolean
  1762. coff_new_section_hook (bfd * abfd, asection * section)
  1763. {
  1764.   combined_entry_type *native;
  1765.   bfd_size_type amt;
  1766.   unsigned char sclass = C_STAT;
  1767.  
  1768.   section->alignment_power = COFF_DEFAULT_SECTION_ALIGNMENT_POWER;
  1769.  
  1770. #ifdef RS6000COFF_C
  1771.   if (bfd_xcoff_text_align_power (abfd) != 0
  1772.       && strcmp (bfd_get_section_name (abfd, section), ".text") == 0)
  1773.     section->alignment_power = bfd_xcoff_text_align_power (abfd);
  1774.   else if (bfd_xcoff_data_align_power (abfd) != 0
  1775.       && strcmp (bfd_get_section_name (abfd, section), ".data") == 0)
  1776.     section->alignment_power = bfd_xcoff_data_align_power (abfd);
  1777.   else
  1778.     {
  1779.       int i;
  1780.  
  1781.       for (i = 0; i < XCOFF_DWSECT_NBR_NAMES; i++)
  1782.         if (strcmp (bfd_get_section_name (abfd, section),
  1783.                     xcoff_dwsect_names[i].name) == 0)
  1784.           {
  1785.             section->alignment_power = 0;
  1786.             sclass = C_DWARF;
  1787.             break;
  1788.           }
  1789.     }
  1790. #endif
  1791.  
  1792.   /* Set up the section symbol.  */
  1793.   if (!_bfd_generic_new_section_hook (abfd, section))
  1794.     return FALSE;
  1795.  
  1796.   /* Allocate aux records for section symbols, to store size and
  1797.      related info.
  1798.  
  1799.      @@ The 10 is a guess at a plausible maximum number of aux entries
  1800.      (but shouldn't be a constant).  */
  1801.   amt = sizeof (combined_entry_type) * 10;
  1802.   native = (combined_entry_type *) bfd_zalloc (abfd, amt);
  1803.   if (native == NULL)
  1804.     return FALSE;
  1805.  
  1806.   /* We don't need to set up n_name, n_value, or n_scnum in the native
  1807.      symbol information, since they'll be overridden by the BFD symbol
  1808.      anyhow.  However, we do need to set the type and storage class,
  1809.      in case this symbol winds up getting written out.  The value 0
  1810.      for n_numaux is already correct.  */
  1811.  
  1812.   native->is_sym = TRUE;
  1813.   native->u.syment.n_type = T_NULL;
  1814.   native->u.syment.n_sclass = sclass;
  1815.  
  1816.   coffsymbol (section->symbol)->native = native;
  1817.  
  1818.   coff_set_custom_section_alignment (abfd, section,
  1819.                                      coff_section_alignment_table,
  1820.                                      coff_section_alignment_table_size);
  1821.  
  1822.   return TRUE;
  1823. }
  1824.  
  1825. #ifdef COFF_ALIGN_IN_SECTION_HEADER
  1826.  
  1827. /* Set the alignment of a BFD section.  */
  1828.  
  1829. static void
  1830. coff_set_alignment_hook (bfd * abfd ATTRIBUTE_UNUSED,
  1831.                          asection * section,
  1832.                          void * scnhdr)
  1833. {
  1834.   struct internal_scnhdr *hdr = (struct internal_scnhdr *) scnhdr;
  1835.   unsigned int i;
  1836.  
  1837. #ifdef I960
  1838.   /* Extract ALIGN from 2**ALIGN stored in section header.  */
  1839.   for (i = 0; i < 32; i++)
  1840.     if ((1 << i) >= hdr->s_align)
  1841.       break;
  1842. #endif
  1843. #ifdef TIC80COFF
  1844.   /* TI tools puts the alignment power in bits 8-11.  */
  1845.   i = (hdr->s_flags >> 8) & 0xF ;
  1846. #endif
  1847. #ifdef COFF_DECODE_ALIGNMENT
  1848.   i = COFF_DECODE_ALIGNMENT(hdr->s_flags);
  1849. #endif
  1850.   section->alignment_power = i;
  1851.  
  1852. #ifdef coff_set_section_load_page
  1853.   coff_set_section_load_page (section, hdr->s_page);
  1854. #endif
  1855. }
  1856.  
  1857. #else /* ! COFF_ALIGN_IN_SECTION_HEADER */
  1858. #ifdef COFF_WITH_PE
  1859.  
  1860. static void
  1861. coff_set_alignment_hook (bfd * abfd ATTRIBUTE_UNUSED,
  1862.                          asection * section,
  1863.                          void * scnhdr)
  1864. {
  1865.   struct internal_scnhdr *hdr = (struct internal_scnhdr *) scnhdr;
  1866.   bfd_size_type amt;
  1867.   unsigned int alignment_power_const
  1868.     = hdr->s_flags & IMAGE_SCN_ALIGN_POWER_BIT_MASK;
  1869.  
  1870.   switch (alignment_power_const)
  1871.     {
  1872.     case IMAGE_SCN_ALIGN_8192BYTES:
  1873.     case IMAGE_SCN_ALIGN_4096BYTES:
  1874.     case IMAGE_SCN_ALIGN_2048BYTES:
  1875.     case IMAGE_SCN_ALIGN_1024BYTES:
  1876.     case IMAGE_SCN_ALIGN_512BYTES:
  1877.     case IMAGE_SCN_ALIGN_256BYTES:
  1878.     case IMAGE_SCN_ALIGN_128BYTES:
  1879.     case IMAGE_SCN_ALIGN_64BYTES:
  1880.     case IMAGE_SCN_ALIGN_32BYTES:
  1881.     case IMAGE_SCN_ALIGN_16BYTES:
  1882.     case IMAGE_SCN_ALIGN_8BYTES:
  1883.     case IMAGE_SCN_ALIGN_4BYTES:
  1884.     case IMAGE_SCN_ALIGN_2BYTES:
  1885.     case IMAGE_SCN_ALIGN_1BYTES:
  1886.       section->alignment_power
  1887.         = IMAGE_SCN_ALIGN_POWER_NUM (alignment_power_const);
  1888.       break;
  1889.     default:
  1890.       break;
  1891.     }
  1892.  
  1893.   /* In a PE image file, the s_paddr field holds the virtual size of a
  1894.      section, while the s_size field holds the raw size.  We also keep
  1895.      the original section flag value, since not every bit can be
  1896.      mapped onto a generic BFD section bit.  */
  1897.   if (coff_section_data (abfd, section) == NULL)
  1898.     {
  1899.       amt = sizeof (struct coff_section_tdata);
  1900.       section->used_by_bfd = bfd_zalloc (abfd, amt);
  1901.       if (section->used_by_bfd == NULL)
  1902.         /* FIXME: Return error.  */
  1903.         abort ();
  1904.     }
  1905.  
  1906.   if (pei_section_data (abfd, section) == NULL)
  1907.     {
  1908.       amt = sizeof (struct pei_section_tdata);
  1909.       coff_section_data (abfd, section)->tdata = bfd_zalloc (abfd, amt);
  1910.       if (coff_section_data (abfd, section)->tdata == NULL)
  1911.         /* FIXME: Return error.  */
  1912.         abort ();
  1913.     }
  1914.   pei_section_data (abfd, section)->virt_size = hdr->s_paddr;
  1915.   pei_section_data (abfd, section)->pe_flags = hdr->s_flags;
  1916.  
  1917.   section->lma = hdr->s_vaddr;
  1918.  
  1919.   /* Check for extended relocs.  */
  1920.   if (hdr->s_flags & IMAGE_SCN_LNK_NRELOC_OVFL)
  1921.     {
  1922.       struct external_reloc dst;
  1923.       struct internal_reloc n;
  1924.       file_ptr oldpos = bfd_tell (abfd);
  1925.       bfd_size_type relsz = bfd_coff_relsz (abfd);
  1926.  
  1927.       if (bfd_seek (abfd, (file_ptr) hdr->s_relptr, 0) != 0)
  1928.         return;
  1929.       if (bfd_bread (& dst, relsz, abfd) != relsz)
  1930.         return;
  1931.  
  1932.       coff_swap_reloc_in (abfd, &dst, &n);
  1933.       if (bfd_seek (abfd, oldpos, 0) != 0)
  1934.         return;
  1935.       section->reloc_count = hdr->s_nreloc = n.r_vaddr - 1;
  1936.       section->rel_filepos += relsz;
  1937.     }
  1938.   else if (hdr->s_nreloc == 0xffff)
  1939.     (*_bfd_error_handler)
  1940.       ("%s: warning: claims to have 0xffff relocs, without overflow",
  1941.        bfd_get_filename (abfd));
  1942. }
  1943. #undef ALIGN_SET
  1944. #undef ELIFALIGN_SET
  1945.  
  1946. #else /* ! COFF_WITH_PE */
  1947. #ifdef RS6000COFF_C
  1948.  
  1949. /* We grossly abuse this function to handle XCOFF overflow headers.
  1950.    When we see one, we correct the reloc and line number counts in the
  1951.    real header, and remove the section we just created.  */
  1952.  
  1953. static void
  1954. coff_set_alignment_hook (bfd *abfd, asection *section, void * scnhdr)
  1955. {
  1956.   struct internal_scnhdr *hdr = (struct internal_scnhdr *) scnhdr;
  1957.   asection *real_sec;
  1958.  
  1959.   if ((hdr->s_flags & STYP_OVRFLO) == 0)
  1960.     return;
  1961.  
  1962.   real_sec = coff_section_from_bfd_index (abfd, (int) hdr->s_nreloc);
  1963.   if (real_sec == NULL)
  1964.     return;
  1965.  
  1966.   real_sec->reloc_count = hdr->s_paddr;
  1967.   real_sec->lineno_count = hdr->s_vaddr;
  1968.  
  1969.   if (!bfd_section_removed_from_list (abfd, section))
  1970.     {
  1971.       bfd_section_list_remove (abfd, section);
  1972.       --abfd->section_count;
  1973.     }
  1974. }
  1975.  
  1976. #else /* ! RS6000COFF_C */
  1977.  
  1978. #define coff_set_alignment_hook \
  1979.   ((void (*) (bfd *, asection *, void *)) bfd_void)
  1980.  
  1981. #endif /* ! RS6000COFF_C */
  1982. #endif /* ! COFF_WITH_PE */
  1983. #endif /* ! COFF_ALIGN_IN_SECTION_HEADER */
  1984.  
  1985. #ifndef coff_mkobject
  1986.  
  1987. static bfd_boolean
  1988. coff_mkobject (bfd * abfd)
  1989. {
  1990.   coff_data_type *coff;
  1991.   bfd_size_type amt = sizeof (coff_data_type);
  1992.  
  1993.   abfd->tdata.coff_obj_data = bfd_zalloc (abfd, amt);
  1994.   if (abfd->tdata.coff_obj_data == NULL)
  1995.     return FALSE;
  1996.   coff = coff_data (abfd);
  1997.   coff->symbols = NULL;
  1998.   coff->conversion_table = NULL;
  1999.   coff->raw_syments = NULL;
  2000.   coff->relocbase = 0;
  2001.   coff->local_toc_sym_map = 0;
  2002.  
  2003. /*  make_abs_section(abfd);*/
  2004.  
  2005.   return TRUE;
  2006. }
  2007. #endif
  2008.  
  2009. /* Create the COFF backend specific information.  */
  2010.  
  2011. #ifndef coff_mkobject_hook
  2012. static void *
  2013. coff_mkobject_hook (bfd * abfd,
  2014.                     void * filehdr,
  2015.                     void * aouthdr ATTRIBUTE_UNUSED)
  2016. {
  2017.   struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
  2018.   coff_data_type *coff;
  2019.  
  2020.   if (! coff_mkobject (abfd))
  2021.     return NULL;
  2022.  
  2023.   coff = coff_data (abfd);
  2024.  
  2025.   coff->sym_filepos = internal_f->f_symptr;
  2026.  
  2027.   /* These members communicate important constants about the symbol
  2028.      table to GDB's symbol-reading code.  These `constants'
  2029.      unfortunately vary among coff implementations...  */
  2030.   coff->local_n_btmask = N_BTMASK;
  2031.   coff->local_n_btshft = N_BTSHFT;
  2032.   coff->local_n_tmask = N_TMASK;
  2033.   coff->local_n_tshift = N_TSHIFT;
  2034.   coff->local_symesz = bfd_coff_symesz (abfd);
  2035.   coff->local_auxesz = bfd_coff_auxesz (abfd);
  2036.   coff->local_linesz = bfd_coff_linesz (abfd);
  2037.  
  2038.   coff->timestamp = internal_f->f_timdat;
  2039.  
  2040.   obj_raw_syment_count (abfd) =
  2041.     obj_conv_table_size (abfd) =
  2042.       internal_f->f_nsyms;
  2043.  
  2044. #ifdef RS6000COFF_C
  2045.   if ((internal_f->f_flags & F_SHROBJ) != 0)
  2046.     abfd->flags |= DYNAMIC;
  2047.   if (aouthdr != NULL && internal_f->f_opthdr >= bfd_coff_aoutsz (abfd))
  2048.     {
  2049.       struct internal_aouthdr *internal_a =
  2050.         (struct internal_aouthdr *) aouthdr;
  2051.       struct xcoff_tdata *xcoff;
  2052.  
  2053.       xcoff = xcoff_data (abfd);
  2054. # ifdef U803XTOCMAGIC
  2055.       xcoff->xcoff64 = internal_f->f_magic == U803XTOCMAGIC;
  2056. # else
  2057.       xcoff->xcoff64 = 0;
  2058. # endif
  2059.       xcoff->full_aouthdr = TRUE;
  2060.       xcoff->toc = internal_a->o_toc;
  2061.       xcoff->sntoc = internal_a->o_sntoc;
  2062.       xcoff->snentry = internal_a->o_snentry;
  2063.       bfd_xcoff_text_align_power (abfd) = internal_a->o_algntext;
  2064.       bfd_xcoff_data_align_power (abfd) = internal_a->o_algndata;
  2065.       xcoff->modtype = internal_a->o_modtype;
  2066.       xcoff->cputype = internal_a->o_cputype;
  2067.       xcoff->maxdata = internal_a->o_maxdata;
  2068.       xcoff->maxstack = internal_a->o_maxstack;
  2069.     }
  2070. #endif
  2071.  
  2072. #ifdef ARM
  2073.   /* Set the flags field from the COFF header read in.  */
  2074.   if (! _bfd_coff_arm_set_private_flags (abfd, internal_f->f_flags))
  2075.     coff->flags = 0;
  2076. #endif
  2077.  
  2078. #ifdef COFF_WITH_PE
  2079.   /* FIXME: I'm not sure this is ever executed, since peicode.h
  2080.      defines coff_mkobject_hook.  */
  2081.   if ((internal_f->f_flags & IMAGE_FILE_DEBUG_STRIPPED) == 0)
  2082.     abfd->flags |= HAS_DEBUG;
  2083. #endif
  2084.  
  2085.   if ((internal_f->f_flags & F_GO32STUB) != 0)
  2086.     {
  2087.     coff->go32stub = (char *) bfd_alloc (abfd, (bfd_size_type) GO32_STUBSIZE);
  2088.       if (coff->go32stub == NULL)
  2089.         return NULL;
  2090.     }
  2091.   if (coff->go32stub != NULL)
  2092.     memcpy (coff->go32stub, internal_f->go32stub, GO32_STUBSIZE);
  2093.  
  2094.   return coff;
  2095. }
  2096. #endif
  2097.  
  2098. /* Determine the machine architecture and type.  FIXME: This is target
  2099.    dependent because the magic numbers are defined in the target
  2100.    dependent header files.  But there is no particular need for this.
  2101.    If the magic numbers were moved to a separate file, this function
  2102.    would be target independent and would also be much more successful
  2103.    at linking together COFF files for different architectures.  */
  2104.  
  2105. static bfd_boolean
  2106. coff_set_arch_mach_hook (bfd *abfd, void * filehdr)
  2107. {
  2108.   unsigned long machine;
  2109.   enum bfd_architecture arch;
  2110.   struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
  2111.  
  2112.   /* Zero selects the default machine for an arch.  */
  2113.   machine = 0;
  2114.   switch (internal_f->f_magic)
  2115.     {
  2116. #ifdef PPCMAGIC
  2117.     case PPCMAGIC:
  2118.       arch = bfd_arch_powerpc;
  2119.       break;
  2120. #endif
  2121. #ifdef I386MAGIC
  2122.     case I386MAGIC:
  2123.     case I386PTXMAGIC:
  2124.     case I386AIXMAGIC:          /* Danbury PS/2 AIX C Compiler.  */
  2125.     case LYNXCOFFMAGIC:         /* Shadows the m68k Lynx number below, sigh.  */
  2126.       arch = bfd_arch_i386;
  2127.       break;
  2128. #endif
  2129. #ifdef AMD64MAGIC
  2130.     case AMD64MAGIC:
  2131.       arch = bfd_arch_i386;
  2132.       machine = bfd_mach_x86_64;
  2133.       break;
  2134. #endif
  2135. #ifdef IA64MAGIC
  2136.     case IA64MAGIC:
  2137.       arch = bfd_arch_ia64;
  2138.       break;
  2139. #endif
  2140. #ifdef ARMMAGIC
  2141.     case ARMMAGIC:
  2142.     case ARMPEMAGIC:
  2143.     case THUMBPEMAGIC:
  2144.       arch = bfd_arch_arm;
  2145.       machine = bfd_arm_get_mach_from_notes (abfd, ARM_NOTE_SECTION);
  2146.       if (machine == bfd_mach_arm_unknown)
  2147.         {
  2148.           switch (internal_f->f_flags & F_ARM_ARCHITECTURE_MASK)
  2149.             {
  2150.             case F_ARM_2:  machine = bfd_mach_arm_2;  break;
  2151.             case F_ARM_2a: machine = bfd_mach_arm_2a; break;
  2152.             case F_ARM_3:  machine = bfd_mach_arm_3;  break;
  2153.             default:
  2154.             case F_ARM_3M: machine = bfd_mach_arm_3M; break;
  2155.             case F_ARM_4:  machine = bfd_mach_arm_4;  break;
  2156.             case F_ARM_4T: machine = bfd_mach_arm_4T; break;
  2157.               /* The COFF header does not have enough bits available
  2158.                  to cover all the different ARM architectures.  So
  2159.                  we interpret F_ARM_5, the highest flag value to mean
  2160.                  "the highest ARM architecture known to BFD" which is
  2161.                  currently the XScale.  */
  2162.             case F_ARM_5:  machine = bfd_mach_arm_XScale;  break;
  2163.             }
  2164.         }
  2165.       break;
  2166. #endif
  2167. #ifdef MC68MAGIC
  2168.     case MC68MAGIC:
  2169.     case M68MAGIC:
  2170. #ifdef MC68KBCSMAGIC
  2171.     case MC68KBCSMAGIC:
  2172. #endif
  2173. #ifdef APOLLOM68KMAGIC
  2174.     case APOLLOM68KMAGIC:
  2175. #endif
  2176. #ifdef LYNXCOFFMAGIC
  2177.     case LYNXCOFFMAGIC:
  2178. #endif
  2179.       arch = bfd_arch_m68k;
  2180.       machine = bfd_mach_m68020;
  2181.       break;
  2182. #endif
  2183. #ifdef MC88MAGIC
  2184.     case MC88MAGIC:
  2185.     case MC88DMAGIC:
  2186.     case MC88OMAGIC:
  2187.       arch = bfd_arch_m88k;
  2188.       machine = 88100;
  2189.       break;
  2190. #endif
  2191. #ifdef Z80MAGIC
  2192.     case Z80MAGIC:
  2193.       arch = bfd_arch_z80;
  2194.       switch (internal_f->f_flags & F_MACHMASK)
  2195.         {
  2196.         case 0:
  2197.         case bfd_mach_z80strict << 12:
  2198.         case bfd_mach_z80 << 12:
  2199.         case bfd_mach_z80full << 12:
  2200.         case bfd_mach_r800 << 12:
  2201.           machine = ((unsigned)internal_f->f_flags & F_MACHMASK) >> 12;
  2202.           break;
  2203.         default:
  2204.           return FALSE;
  2205.         }
  2206.       break;
  2207. #endif
  2208. #ifdef Z8KMAGIC
  2209.     case Z8KMAGIC:
  2210.       arch = bfd_arch_z8k;
  2211.       switch (internal_f->f_flags & F_MACHMASK)
  2212.         {
  2213.         case F_Z8001:
  2214.           machine = bfd_mach_z8001;
  2215.           break;
  2216.         case F_Z8002:
  2217.           machine = bfd_mach_z8002;
  2218.           break;
  2219.         default:
  2220.           return FALSE;
  2221.         }
  2222.       break;
  2223. #endif
  2224. #ifdef I860
  2225.     case I860MAGIC:
  2226.       arch = bfd_arch_i860;
  2227.       break;
  2228. #endif
  2229. #ifdef I960
  2230. #ifdef I960ROMAGIC
  2231.     case I960ROMAGIC:
  2232.     case I960RWMAGIC:
  2233.       arch = bfd_arch_i960;
  2234.       switch (F_I960TYPE & internal_f->f_flags)
  2235.         {
  2236.         default:
  2237.         case F_I960CORE:
  2238.           machine = bfd_mach_i960_core;
  2239.           break;
  2240.         case F_I960KB:
  2241.           machine = bfd_mach_i960_kb_sb;
  2242.           break;
  2243.         case F_I960MC:
  2244.           machine = bfd_mach_i960_mc;
  2245.           break;
  2246.         case F_I960XA:
  2247.           machine = bfd_mach_i960_xa;
  2248.           break;
  2249.         case F_I960CA:
  2250.           machine = bfd_mach_i960_ca;
  2251.           break;
  2252.         case F_I960KA:
  2253.           machine = bfd_mach_i960_ka_sa;
  2254.           break;
  2255.         case F_I960JX:
  2256.           machine = bfd_mach_i960_jx;
  2257.           break;
  2258.         case F_I960HX:
  2259.           machine = bfd_mach_i960_hx;
  2260.           break;
  2261.         }
  2262.       break;
  2263. #endif
  2264. #endif
  2265.  
  2266. #ifdef RS6000COFF_C
  2267. #ifdef XCOFF64
  2268.     case U64_TOCMAGIC:
  2269.     case U803XTOCMAGIC:
  2270. #else
  2271.     case U802ROMAGIC:
  2272.     case U802WRMAGIC:
  2273.     case U802TOCMAGIC:
  2274. #endif
  2275.       {
  2276.         int cputype;
  2277.  
  2278.         if (xcoff_data (abfd)->cputype != -1)
  2279.           cputype = xcoff_data (abfd)->cputype & 0xff;
  2280.         else
  2281.           {
  2282.             /* We did not get a value from the a.out header.  If the
  2283.                file has not been stripped, we may be able to get the
  2284.                architecture information from the first symbol, if it
  2285.                is a .file symbol.  */
  2286.             if (obj_raw_syment_count (abfd) == 0)
  2287.               cputype = 0;
  2288.             else
  2289.               {
  2290.                 bfd_byte *buf;
  2291.                 struct internal_syment sym;
  2292.                 bfd_size_type amt = bfd_coff_symesz (abfd);
  2293.  
  2294.                 buf = bfd_malloc (amt);
  2295.                 if (buf == NULL)
  2296.                   return FALSE;
  2297.                 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0
  2298.                     || bfd_bread (buf, amt, abfd) != amt)
  2299.                   {
  2300.                     free (buf);
  2301.                     return FALSE;
  2302.                   }
  2303.                 bfd_coff_swap_sym_in (abfd, buf, & sym);
  2304.                 if (sym.n_sclass == C_FILE)
  2305.                   cputype = sym.n_type & 0xff;
  2306.                 else
  2307.                   cputype = 0;
  2308.                 free (buf);
  2309.               }
  2310.           }
  2311.  
  2312.         /* FIXME: We don't handle all cases here.  */
  2313.         switch (cputype)
  2314.           {
  2315.           default:
  2316.           case 0:
  2317.             arch = bfd_xcoff_architecture (abfd);
  2318.             machine = bfd_xcoff_machine (abfd);
  2319.             break;
  2320.  
  2321.           case 1:
  2322.             arch = bfd_arch_powerpc;
  2323.             machine = bfd_mach_ppc_601;
  2324.             break;
  2325.           case 2: /* 64 bit PowerPC */
  2326.             arch = bfd_arch_powerpc;
  2327.             machine = bfd_mach_ppc_620;
  2328.             break;
  2329.           case 3:
  2330.             arch = bfd_arch_powerpc;
  2331.             machine = bfd_mach_ppc;
  2332.             break;
  2333.           case 4:
  2334.             arch = bfd_arch_rs6000;
  2335.             machine = bfd_mach_rs6k;
  2336.             break;
  2337.           }
  2338.       }
  2339.       break;
  2340. #endif
  2341.  
  2342. #ifdef WE32KMAGIC
  2343.     case WE32KMAGIC:
  2344.       arch = bfd_arch_we32k;
  2345.       break;
  2346. #endif
  2347.  
  2348. #ifdef H8300MAGIC
  2349.     case H8300MAGIC:
  2350.       arch = bfd_arch_h8300;
  2351.       machine = bfd_mach_h8300;
  2352.       /* !! FIXME this probably isn't the right place for this.  */
  2353.       abfd->flags |= BFD_IS_RELAXABLE;
  2354.       break;
  2355. #endif
  2356.  
  2357. #ifdef H8300HMAGIC
  2358.     case H8300HMAGIC:
  2359.       arch = bfd_arch_h8300;
  2360.       machine = bfd_mach_h8300h;
  2361.       /* !! FIXME this probably isn't the right place for this.  */
  2362.       abfd->flags |= BFD_IS_RELAXABLE;
  2363.       break;
  2364. #endif
  2365.  
  2366. #ifdef H8300SMAGIC
  2367.     case H8300SMAGIC:
  2368.       arch = bfd_arch_h8300;
  2369.       machine = bfd_mach_h8300s;
  2370.       /* !! FIXME this probably isn't the right place for this.  */
  2371.       abfd->flags |= BFD_IS_RELAXABLE;
  2372.       break;
  2373. #endif
  2374.  
  2375. #ifdef H8300HNMAGIC
  2376.     case H8300HNMAGIC:
  2377.       arch = bfd_arch_h8300;
  2378.       machine = bfd_mach_h8300hn;
  2379.       /* !! FIXME this probably isn't the right place for this.  */
  2380.       abfd->flags |= BFD_IS_RELAXABLE;
  2381.       break;
  2382. #endif
  2383.  
  2384. #ifdef H8300SNMAGIC
  2385.     case H8300SNMAGIC:
  2386.       arch = bfd_arch_h8300;
  2387.       machine = bfd_mach_h8300sn;
  2388.       /* !! FIXME this probably isn't the right place for this.  */
  2389.       abfd->flags |= BFD_IS_RELAXABLE;
  2390.       break;
  2391. #endif
  2392.  
  2393. #ifdef SH_ARCH_MAGIC_BIG
  2394.     case SH_ARCH_MAGIC_BIG:
  2395.     case SH_ARCH_MAGIC_LITTLE:
  2396. #ifdef COFF_WITH_PE
  2397.     case SH_ARCH_MAGIC_WINCE:
  2398. #endif
  2399.       arch = bfd_arch_sh;
  2400.       break;
  2401. #endif
  2402.  
  2403. #ifdef MIPS_ARCH_MAGIC_WINCE
  2404.     case MIPS_ARCH_MAGIC_WINCE:
  2405.       arch = bfd_arch_mips;
  2406.       break;
  2407. #endif
  2408.  
  2409. #ifdef H8500MAGIC
  2410.     case H8500MAGIC:
  2411.       arch = bfd_arch_h8500;
  2412.       break;
  2413. #endif
  2414.  
  2415. #ifdef SPARCMAGIC
  2416.     case SPARCMAGIC:
  2417. #ifdef LYNXCOFFMAGIC
  2418.     case LYNXCOFFMAGIC:
  2419. #endif
  2420.       arch = bfd_arch_sparc;
  2421.       break;
  2422. #endif
  2423.  
  2424. #ifdef TIC30MAGIC
  2425.     case TIC30MAGIC:
  2426.       arch = bfd_arch_tic30;
  2427.       break;
  2428. #endif
  2429.  
  2430. #ifdef TICOFF0MAGIC
  2431. #ifdef TICOFF_TARGET_ARCH
  2432.       /* This TI COFF section should be used by all new TI COFF v0 targets.  */
  2433.     case TICOFF0MAGIC:
  2434.       arch = TICOFF_TARGET_ARCH;
  2435.       machine = TICOFF_TARGET_MACHINE_GET (internal_f->f_flags);
  2436.       break;
  2437. #endif
  2438. #endif
  2439.  
  2440. #ifdef TICOFF1MAGIC
  2441.       /* This TI COFF section should be used by all new TI COFF v1/2 targets.  */
  2442.       /* TI COFF1 and COFF2 use the target_id field to specify which arch.  */
  2443.     case TICOFF1MAGIC:
  2444.     case TICOFF2MAGIC:
  2445.       switch (internal_f->f_target_id)
  2446.         {
  2447. #ifdef TI_TARGET_ID
  2448.         case TI_TARGET_ID:
  2449.           arch = TICOFF_TARGET_ARCH;
  2450.           machine = TICOFF_TARGET_MACHINE_GET (internal_f->f_flags);
  2451.           break;
  2452. #endif
  2453.         default:
  2454.           arch = bfd_arch_obscure;
  2455.           (*_bfd_error_handler)
  2456.             (_("Unrecognized TI COFF target id '0x%x'"),
  2457.              internal_f->f_target_id);
  2458.           break;
  2459.         }
  2460.       break;
  2461. #endif
  2462.  
  2463. #ifdef TIC80_ARCH_MAGIC
  2464.     case TIC80_ARCH_MAGIC:
  2465.       arch = bfd_arch_tic80;
  2466.       break;
  2467. #endif
  2468.  
  2469. #ifdef MCOREMAGIC
  2470.     case MCOREMAGIC:
  2471.       arch = bfd_arch_mcore;
  2472.       break;
  2473. #endif
  2474.  
  2475. #ifdef W65MAGIC
  2476.     case W65MAGIC:
  2477.       arch = bfd_arch_w65;
  2478.       break;
  2479. #endif
  2480.  
  2481.     default:                    /* Unreadable input file type.  */
  2482.       arch = bfd_arch_obscure;
  2483.       break;
  2484.     }
  2485.  
  2486.   bfd_default_set_arch_mach (abfd, arch, machine);
  2487.   return TRUE;
  2488. }
  2489.  
  2490. #ifdef SYMNAME_IN_DEBUG
  2491.  
  2492. static bfd_boolean
  2493. symname_in_debug_hook (bfd * abfd ATTRIBUTE_UNUSED, struct internal_syment *sym)
  2494. {
  2495.   return SYMNAME_IN_DEBUG (sym) != 0;
  2496. }
  2497.  
  2498. #else
  2499.  
  2500. #define symname_in_debug_hook \
  2501.   (bfd_boolean (*) (bfd *, struct internal_syment *)) bfd_false
  2502.  
  2503. #endif
  2504.  
  2505. #ifdef RS6000COFF_C
  2506.  
  2507. #ifdef XCOFF64
  2508. #define FORCE_SYMNAMES_IN_STRINGS
  2509. #endif
  2510.  
  2511. /* Handle the csect auxent of a C_EXT, C_AIX_WEAKEXT or C_HIDEXT symbol.  */
  2512.  
  2513. static bfd_boolean
  2514. coff_pointerize_aux_hook (bfd *abfd ATTRIBUTE_UNUSED,
  2515.                           combined_entry_type *table_base,
  2516.                           combined_entry_type *symbol,
  2517.                           unsigned int indaux,
  2518.                           combined_entry_type *aux)
  2519. {
  2520.   BFD_ASSERT (symbol->is_sym);
  2521.   int n_sclass = symbol->u.syment.n_sclass;
  2522.  
  2523.   if (CSECT_SYM_P (n_sclass)
  2524.       && indaux + 1 == symbol->u.syment.n_numaux)
  2525.     {
  2526.       BFD_ASSERT (! aux->is_sym);
  2527.       if (SMTYP_SMTYP (aux->u.auxent.x_csect.x_smtyp) == XTY_LD)
  2528.         {
  2529.           aux->u.auxent.x_csect.x_scnlen.p =
  2530.             table_base + aux->u.auxent.x_csect.x_scnlen.l;
  2531.           aux->fix_scnlen = 1;
  2532.         }
  2533.  
  2534.       /* Return TRUE to indicate that the caller should not do any
  2535.          further work on this auxent.  */
  2536.       return TRUE;
  2537.     }
  2538.  
  2539.   /* Return FALSE to indicate that this auxent should be handled by
  2540.      the caller.  */
  2541.   return FALSE;
  2542. }
  2543.  
  2544. #else
  2545. #ifdef I960
  2546.  
  2547. /* We don't want to pointerize bal entries.  */
  2548.  
  2549. static bfd_boolean
  2550. coff_pointerize_aux_hook (bfd *abfd ATTRIBUTE_UNUSED,
  2551.                           combined_entry_type *table_base ATTRIBUTE_UNUSED,
  2552.                           combined_entry_type *symbol,
  2553.                           unsigned int indaux,
  2554.                           combined_entry_type *aux ATTRIBUTE_UNUSED)
  2555. {
  2556.   /* Return TRUE if we don't want to pointerize this aux entry, which
  2557.      is the case for the lastfirst aux entry for a C_LEAFPROC symbol.  */
  2558.   return (indaux == 1
  2559.           && symbol->is_sym
  2560.           && (symbol->u.syment.n_sclass == C_LEAFPROC
  2561.               || symbol->u.syment.n_sclass == C_LEAFSTAT
  2562.               || symbol->u.syment.n_sclass == C_LEAFEXT));
  2563. }
  2564.  
  2565. #else /* ! I960 */
  2566.  
  2567. #define coff_pointerize_aux_hook 0
  2568.  
  2569. #endif /* ! I960 */
  2570. #endif /* ! RS6000COFF_C */
  2571.  
  2572. /* Print an aux entry.  This returns TRUE if it has printed it.  */
  2573.  
  2574. static bfd_boolean
  2575. coff_print_aux (bfd *abfd ATTRIBUTE_UNUSED,
  2576.                 FILE *file ATTRIBUTE_UNUSED,
  2577.                 combined_entry_type *table_base ATTRIBUTE_UNUSED,
  2578.                 combined_entry_type *symbol ATTRIBUTE_UNUSED,
  2579.                 combined_entry_type *aux ATTRIBUTE_UNUSED,
  2580.                 unsigned int indaux ATTRIBUTE_UNUSED)
  2581. {
  2582.   BFD_ASSERT (symbol->is_sym);
  2583.   BFD_ASSERT (! aux->is_sym);
  2584. #ifdef RS6000COFF_C
  2585.   if (CSECT_SYM_P (symbol->u.syment.n_sclass)
  2586.       && indaux + 1 == symbol->u.syment.n_numaux)
  2587.     {
  2588.       /* This is a csect entry.  */
  2589.       fprintf (file, "AUX ");
  2590.       if (SMTYP_SMTYP (aux->u.auxent.x_csect.x_smtyp) != XTY_LD)
  2591.         {
  2592.           BFD_ASSERT (! aux->fix_scnlen);
  2593. #ifdef XCOFF64
  2594.           fprintf (file, "val %5lld",
  2595.                    (long long) aux->u.auxent.x_csect.x_scnlen.l);
  2596. #else
  2597.           fprintf (file, "val %5ld", (long) aux->u.auxent.x_csect.x_scnlen.l);
  2598. #endif
  2599.         }
  2600.       else
  2601.         {
  2602.           fprintf (file, "indx ");
  2603.           if (! aux->fix_scnlen)
  2604. #ifdef XCOFF64
  2605.             fprintf (file, "%4lld",
  2606.                      (long long) aux->u.auxent.x_csect.x_scnlen.l);
  2607. #else
  2608.             fprintf (file, "%4ld", (long) aux->u.auxent.x_csect.x_scnlen.l);
  2609. #endif
  2610.           else
  2611.             fprintf (file, "%4ld",
  2612.                      (long) (aux->u.auxent.x_csect.x_scnlen.p - table_base));
  2613.         }
  2614.       fprintf (file,
  2615.                " prmhsh %ld snhsh %u typ %d algn %d clss %u stb %ld snstb %u",
  2616.                aux->u.auxent.x_csect.x_parmhash,
  2617.                (unsigned int) aux->u.auxent.x_csect.x_snhash,
  2618.                SMTYP_SMTYP (aux->u.auxent.x_csect.x_smtyp),
  2619.                SMTYP_ALIGN (aux->u.auxent.x_csect.x_smtyp),
  2620.                (unsigned int) aux->u.auxent.x_csect.x_smclas,
  2621.                aux->u.auxent.x_csect.x_stab,
  2622.                (unsigned int) aux->u.auxent.x_csect.x_snstab);
  2623.       return TRUE;
  2624.     }
  2625. #endif
  2626.  
  2627.   /* Return FALSE to indicate that no special action was taken.  */
  2628.   return FALSE;
  2629. }
  2630.  
  2631. /*
  2632. SUBSUBSECTION
  2633.         Writing relocations
  2634.  
  2635.         To write relocations, the back end steps though the
  2636.         canonical relocation table and create an
  2637.         @code{internal_reloc}. The symbol index to use is removed from
  2638.         the @code{offset} field in the symbol table supplied.  The
  2639.         address comes directly from the sum of the section base
  2640.         address and the relocation offset; the type is dug directly
  2641.         from the howto field.  Then the @code{internal_reloc} is
  2642.         swapped into the shape of an @code{external_reloc} and written
  2643.         out to disk.
  2644.  
  2645. */
  2646.  
  2647. #ifdef TARG_AUX
  2648.  
  2649.  
  2650. /* AUX's ld wants relocations to be sorted.  */
  2651. static int
  2652. compare_arelent_ptr (const void * x, const void * y)
  2653. {
  2654.   const arelent **a = (const arelent **) x;
  2655.   const arelent **b = (const arelent **) y;
  2656.   bfd_size_type aadr = (*a)->address;
  2657.   bfd_size_type badr = (*b)->address;
  2658.  
  2659.   return (aadr < badr ? -1 : badr < aadr ? 1 : 0);
  2660. }
  2661.  
  2662. #endif /* TARG_AUX */
  2663.  
  2664. static bfd_boolean
  2665. coff_write_relocs (bfd * abfd, int first_undef)
  2666. {
  2667.   asection *s;
  2668.  
  2669.   for (s = abfd->sections; s != NULL; s = s->next)
  2670.     {
  2671.       unsigned int i;
  2672.       struct external_reloc dst;
  2673.       arelent **p;
  2674.  
  2675. #ifndef TARG_AUX
  2676.       p = s->orelocation;
  2677. #else
  2678.       {
  2679.         /* Sort relocations before we write them out.  */
  2680.         bfd_size_type amt;
  2681.  
  2682.         amt = s->reloc_count;
  2683.         amt *= sizeof (arelent *);
  2684.         p = bfd_malloc (amt);
  2685.         if (p == NULL)
  2686.           {
  2687.             if (s->reloc_count > 0)
  2688.           return FALSE;
  2689.           }
  2690.         else
  2691.           {
  2692.         memcpy (p, s->orelocation, (size_t) amt);
  2693.         qsort (p, s->reloc_count, sizeof (arelent *), compare_arelent_ptr);
  2694.       }
  2695.       }
  2696. #endif
  2697.  
  2698.       if (bfd_seek (abfd, s->rel_filepos, SEEK_SET) != 0)
  2699.         return FALSE;
  2700.  
  2701. #ifdef COFF_WITH_PE
  2702.       if (obj_pe (abfd) && s->reloc_count >= 0xffff)
  2703.         {
  2704.           /* Encode real count here as first reloc.  */
  2705.           struct internal_reloc n;
  2706.  
  2707.           memset (& n, 0, sizeof (n));
  2708.           /* Add one to count *this* reloc (grr).  */
  2709.           n.r_vaddr = s->reloc_count + 1;
  2710.           coff_swap_reloc_out (abfd, &n, &dst);
  2711.           if (bfd_bwrite (& dst, (bfd_size_type) bfd_coff_relsz (abfd),
  2712.                           abfd) != bfd_coff_relsz (abfd))
  2713.             return FALSE;
  2714.         }
  2715. #endif
  2716.  
  2717.       for (i = 0; i < s->reloc_count; i++)
  2718.         {
  2719.           struct internal_reloc n;
  2720.           arelent *q = p[i];
  2721.  
  2722.           memset (& n, 0, sizeof (n));
  2723.  
  2724.           /* Now we've renumbered the symbols we know where the
  2725.              undefined symbols live in the table.  Check the reloc
  2726.              entries for symbols who's output bfd isn't the right one.
  2727.              This is because the symbol was undefined (which means
  2728.              that all the pointers are never made to point to the same
  2729.              place). This is a bad thing,'cause the symbols attached
  2730.              to the output bfd are indexed, so that the relocation
  2731.              entries know which symbol index they point to.  So we
  2732.              have to look up the output symbol here.  */
  2733.  
  2734.           if (q->sym_ptr_ptr[0] != NULL && q->sym_ptr_ptr[0]->the_bfd != abfd)
  2735.             {
  2736.               int j;
  2737.               const char *sname = q->sym_ptr_ptr[0]->name;
  2738.               asymbol **outsyms = abfd->outsymbols;
  2739.  
  2740.               for (j = first_undef; outsyms[j]; j++)
  2741.                 {
  2742.                   const char *intable = outsyms[j]->name;
  2743.  
  2744.                   if (strcmp (intable, sname) == 0)
  2745.                     {
  2746.                       /* Got a hit, so repoint the reloc.  */
  2747.                       q->sym_ptr_ptr = outsyms + j;
  2748.                       break;
  2749.                     }
  2750.                 }
  2751.             }
  2752.  
  2753.           n.r_vaddr = q->address + s->vma;
  2754.  
  2755. #ifdef R_IHCONST
  2756.           /* The 29k const/consth reloc pair is a real kludge.  The consth
  2757.              part doesn't have a symbol; it has an offset.  So rebuilt
  2758.              that here.  */
  2759.           if (q->howto->type == R_IHCONST)
  2760.             n.r_symndx = q->addend;
  2761.           else
  2762. #endif
  2763.             if (q->sym_ptr_ptr && q->sym_ptr_ptr[0] != NULL)
  2764.               {
  2765. #ifdef SECTION_RELATIVE_ABSOLUTE_SYMBOL_P
  2766.                 if (SECTION_RELATIVE_ABSOLUTE_SYMBOL_P (q, s))
  2767. #else
  2768.                 if ((*q->sym_ptr_ptr)->section == bfd_abs_section_ptr
  2769.                     && ((*q->sym_ptr_ptr)->flags & BSF_SECTION_SYM) != 0)
  2770. #endif
  2771.                   /* This is a relocation relative to the absolute symbol.  */
  2772.                   n.r_symndx = -1;
  2773.                 else
  2774.                   {
  2775.                     n.r_symndx = get_index ((*(q->sym_ptr_ptr)));
  2776.                     /* Check to see if the symbol reloc points to a symbol
  2777.                        we don't have in our symbol table.  */
  2778.                     if (n.r_symndx > obj_conv_table_size (abfd))
  2779.                       {
  2780.                         bfd_set_error (bfd_error_bad_value);
  2781.                         _bfd_error_handler (_("%B: reloc against a non-existant symbol index: %ld"),
  2782.                                             abfd, n.r_symndx);
  2783.                         return FALSE;
  2784.                       }
  2785.                   }
  2786.               }
  2787.  
  2788. #ifdef SWAP_OUT_RELOC_OFFSET
  2789.           n.r_offset = q->addend;
  2790. #endif
  2791.  
  2792. #ifdef SELECT_RELOC
  2793.           /* Work out reloc type from what is required.  */
  2794.           SELECT_RELOC (n, q->howto);
  2795. #else
  2796.           n.r_type = q->howto->type;
  2797. #endif
  2798.           coff_swap_reloc_out (abfd, &n, &dst);
  2799.  
  2800.           if (bfd_bwrite (& dst, (bfd_size_type) bfd_coff_relsz (abfd),
  2801.                          abfd) != bfd_coff_relsz (abfd))
  2802.             return FALSE;
  2803.         }
  2804.  
  2805. #ifdef TARG_AUX
  2806.       if (p != NULL)
  2807.         free (p);
  2808. #endif
  2809.     }
  2810.  
  2811.   return TRUE;
  2812. }
  2813.  
  2814. /* Set flags and magic number of a coff file from architecture and machine
  2815.    type.  Result is TRUE if we can represent the arch&type, FALSE if not.  */
  2816.  
  2817. static bfd_boolean
  2818. coff_set_flags (bfd * abfd,
  2819.                 unsigned int *magicp ATTRIBUTE_UNUSED,
  2820.                 unsigned short *flagsp ATTRIBUTE_UNUSED)
  2821. {
  2822.   switch (bfd_get_arch (abfd))
  2823.     {
  2824. #ifdef Z80MAGIC
  2825.     case bfd_arch_z80:
  2826.       *magicp = Z80MAGIC;
  2827.       switch (bfd_get_mach (abfd))
  2828.         {
  2829.         case 0:
  2830.         case bfd_mach_z80strict:
  2831.         case bfd_mach_z80:
  2832.         case bfd_mach_z80full:
  2833.         case bfd_mach_r800:
  2834.           *flagsp = bfd_get_mach (abfd) << 12;
  2835.           break;
  2836.         default:
  2837.           return FALSE;
  2838.         }
  2839.       return TRUE;
  2840. #endif
  2841.  
  2842. #ifdef Z8KMAGIC
  2843.     case bfd_arch_z8k:
  2844.       *magicp = Z8KMAGIC;
  2845.  
  2846.       switch (bfd_get_mach (abfd))
  2847.         {
  2848.         case bfd_mach_z8001: *flagsp = F_Z8001; break;
  2849.         case bfd_mach_z8002: *flagsp = F_Z8002; break;
  2850.         default:             return FALSE;
  2851.         }
  2852.       return TRUE;
  2853. #endif
  2854.  
  2855. #ifdef I960ROMAGIC
  2856.     case bfd_arch_i960:
  2857.  
  2858.       {
  2859.         unsigned flags;
  2860.  
  2861.         *magicp = I960ROMAGIC;
  2862.  
  2863.         switch (bfd_get_mach (abfd))
  2864.           {
  2865.           case bfd_mach_i960_core:  flags = F_I960CORE; break;
  2866.           case bfd_mach_i960_kb_sb: flags = F_I960KB;   break;
  2867.           case bfd_mach_i960_mc:    flags = F_I960MC;   break;
  2868.           case bfd_mach_i960_xa:    flags = F_I960XA;   break;
  2869.           case bfd_mach_i960_ca:    flags = F_I960CA;   break;
  2870.           case bfd_mach_i960_ka_sa: flags = F_I960KA;   break;
  2871.           case bfd_mach_i960_jx:    flags = F_I960JX;   break;
  2872.           case bfd_mach_i960_hx:    flags = F_I960HX;   break;
  2873.           default:                  return FALSE;
  2874.           }
  2875.         *flagsp = flags;
  2876.         return TRUE;
  2877.       }
  2878.       break;
  2879. #endif
  2880.  
  2881. #ifdef TIC30MAGIC
  2882.     case bfd_arch_tic30:
  2883.       *magicp = TIC30MAGIC;
  2884.       return TRUE;
  2885. #endif
  2886.  
  2887. #ifdef TICOFF_DEFAULT_MAGIC
  2888.     case TICOFF_TARGET_ARCH:
  2889.       /* If there's no indication of which version we want, use the default.  */
  2890.       if (!abfd->xvec )
  2891.         *magicp = TICOFF_DEFAULT_MAGIC;
  2892.       else
  2893.         {
  2894.           /* We may want to output in a different COFF version.  */
  2895.           switch (abfd->xvec->name[4])
  2896.             {
  2897.             case '0':
  2898.               *magicp = TICOFF0MAGIC;
  2899.               break;
  2900.             case '1':
  2901.               *magicp = TICOFF1MAGIC;
  2902.               break;
  2903.             case '2':
  2904.               *magicp = TICOFF2MAGIC;
  2905.               break;
  2906.             default:
  2907.               return FALSE;
  2908.             }
  2909.         }
  2910.       TICOFF_TARGET_MACHINE_SET (flagsp, bfd_get_mach (abfd));
  2911.       return TRUE;
  2912. #endif
  2913.  
  2914. #ifdef TIC80_ARCH_MAGIC
  2915.     case bfd_arch_tic80:
  2916.       *magicp = TIC80_ARCH_MAGIC;
  2917.       return TRUE;
  2918. #endif
  2919.  
  2920. #ifdef ARMMAGIC
  2921.     case bfd_arch_arm:
  2922. #ifdef ARM_WINCE
  2923.       * magicp = ARMPEMAGIC;
  2924. #else
  2925.       * magicp = ARMMAGIC;
  2926. #endif
  2927.       * flagsp = 0;
  2928.       if (APCS_SET (abfd))
  2929.         {
  2930.           if (APCS_26_FLAG (abfd))
  2931.             * flagsp |= F_APCS26;
  2932.  
  2933.           if (APCS_FLOAT_FLAG (abfd))
  2934.             * flagsp |= F_APCS_FLOAT;
  2935.  
  2936.           if (PIC_FLAG (abfd))
  2937.             * flagsp |= F_PIC;
  2938.         }
  2939.       if (INTERWORK_SET (abfd) && INTERWORK_FLAG (abfd))
  2940.         * flagsp |= F_INTERWORK;
  2941.       switch (bfd_get_mach (abfd))
  2942.         {
  2943.         case bfd_mach_arm_2:  * flagsp |= F_ARM_2;  break;
  2944.         case bfd_mach_arm_2a: * flagsp |= F_ARM_2a; break;
  2945.         case bfd_mach_arm_3:  * flagsp |= F_ARM_3;  break;
  2946.         case bfd_mach_arm_3M: * flagsp |= F_ARM_3M; break;
  2947.         case bfd_mach_arm_4:  * flagsp |= F_ARM_4;  break;
  2948.         case bfd_mach_arm_4T: * flagsp |= F_ARM_4T; break;
  2949.         case bfd_mach_arm_5:  * flagsp |= F_ARM_5;  break;
  2950.           /* FIXME: we do not have F_ARM vaues greater than F_ARM_5.
  2951.              See also the comment in coff_set_arch_mach_hook().  */
  2952.         case bfd_mach_arm_5T: * flagsp |= F_ARM_5;  break;
  2953.         case bfd_mach_arm_5TE: * flagsp |= F_ARM_5; break;
  2954.         case bfd_mach_arm_XScale: * flagsp |= F_ARM_5; break;
  2955.         }
  2956.       return TRUE;
  2957. #endif
  2958.  
  2959. #ifdef PPCMAGIC
  2960.     case bfd_arch_powerpc:
  2961.       *magicp = PPCMAGIC;
  2962.       return TRUE;
  2963. #endif
  2964.  
  2965. #if defined(I386MAGIC) || defined(AMD64MAGIC)
  2966.     case bfd_arch_i386:
  2967. #if defined(I386MAGIC)
  2968.       *magicp = I386MAGIC;
  2969. #endif
  2970. #if defined LYNXOS
  2971.       /* Just overwrite the usual value if we're doing Lynx.  */
  2972.       *magicp = LYNXCOFFMAGIC;
  2973. #endif
  2974. #if defined AMD64MAGIC
  2975.       *magicp = AMD64MAGIC;
  2976. #endif
  2977.       return TRUE;
  2978. #endif
  2979.  
  2980. #ifdef I860MAGIC
  2981.     case bfd_arch_i860:
  2982.       *magicp = I860MAGIC;
  2983.       return TRUE;
  2984. #endif
  2985.  
  2986. #ifdef IA64MAGIC
  2987.     case bfd_arch_ia64:
  2988.       *magicp = IA64MAGIC;
  2989.       return TRUE;
  2990. #endif
  2991.  
  2992. #ifdef MC68MAGIC
  2993.     case bfd_arch_m68k:
  2994. #ifdef APOLLOM68KMAGIC
  2995.       *magicp = APOLLO_COFF_VERSION_NUMBER;
  2996. #else
  2997.       /* NAMES_HAVE_UNDERSCORE may be defined by coff-u68k.c.  */
  2998. #ifdef NAMES_HAVE_UNDERSCORE
  2999.       *magicp = MC68KBCSMAGIC;
  3000. #else
  3001.       *magicp = MC68MAGIC;
  3002. #endif
  3003. #endif
  3004. #ifdef LYNXOS
  3005.       /* Just overwrite the usual value if we're doing Lynx.  */
  3006.       *magicp = LYNXCOFFMAGIC;
  3007. #endif
  3008.       return TRUE;
  3009. #endif
  3010.  
  3011. #ifdef MC88MAGIC
  3012.     case bfd_arch_m88k:
  3013.       *magicp = MC88OMAGIC;
  3014.       return TRUE;
  3015. #endif
  3016.  
  3017. #ifdef H8300MAGIC
  3018.     case bfd_arch_h8300:
  3019.       switch (bfd_get_mach (abfd))
  3020.         {
  3021.         case bfd_mach_h8300:   *magicp = H8300MAGIC;   return TRUE;
  3022.         case bfd_mach_h8300h:  *magicp = H8300HMAGIC;  return TRUE;
  3023.         case bfd_mach_h8300s:  *magicp = H8300SMAGIC;  return TRUE;
  3024.         case bfd_mach_h8300hn: *magicp = H8300HNMAGIC; return TRUE;
  3025.         case bfd_mach_h8300sn: *magicp = H8300SNMAGIC; return TRUE;
  3026.         default: break;
  3027.         }
  3028.       break;
  3029. #endif
  3030.  
  3031. #ifdef SH_ARCH_MAGIC_BIG
  3032.     case bfd_arch_sh:
  3033. #ifdef COFF_IMAGE_WITH_PE
  3034.       *magicp = SH_ARCH_MAGIC_WINCE;
  3035. #else
  3036.       if (bfd_big_endian (abfd))
  3037.         *magicp = SH_ARCH_MAGIC_BIG;
  3038.       else
  3039.         *magicp = SH_ARCH_MAGIC_LITTLE;
  3040. #endif
  3041.       return TRUE;
  3042. #endif
  3043.  
  3044. #ifdef MIPS_ARCH_MAGIC_WINCE
  3045.     case bfd_arch_mips:
  3046.       *magicp = MIPS_ARCH_MAGIC_WINCE;
  3047.       return TRUE;
  3048. #endif
  3049.  
  3050. #ifdef SPARCMAGIC
  3051.     case bfd_arch_sparc:
  3052.       *magicp = SPARCMAGIC;
  3053. #ifdef LYNXOS
  3054.       /* Just overwrite the usual value if we're doing Lynx.  */
  3055.       *magicp = LYNXCOFFMAGIC;
  3056. #endif
  3057.       return TRUE;
  3058. #endif
  3059.  
  3060. #ifdef H8500MAGIC
  3061.     case bfd_arch_h8500:
  3062.       *magicp = H8500MAGIC;
  3063.       return TRUE;
  3064.       break;
  3065. #endif
  3066.  
  3067. #ifdef WE32KMAGIC
  3068.     case bfd_arch_we32k:
  3069.       *magicp = WE32KMAGIC;
  3070.       return TRUE;
  3071. #endif
  3072.  
  3073. #ifdef RS6000COFF_C
  3074.     case bfd_arch_rs6000:
  3075. #ifndef PPCMAGIC
  3076.     case bfd_arch_powerpc:
  3077. #endif
  3078.       BFD_ASSERT (bfd_get_flavour (abfd) == bfd_target_xcoff_flavour);
  3079.       *magicp = bfd_xcoff_magic_number (abfd);
  3080.       return TRUE;
  3081. #endif
  3082.  
  3083. #ifdef MCOREMAGIC
  3084.     case bfd_arch_mcore:
  3085.       * magicp = MCOREMAGIC;
  3086.       return TRUE;
  3087. #endif
  3088.  
  3089. #ifdef W65MAGIC
  3090.     case bfd_arch_w65:
  3091.       *magicp = W65MAGIC;
  3092.       return TRUE;
  3093. #endif
  3094.  
  3095.     default:                    /* Unknown architecture.  */
  3096.       /* Fall through to "return FALSE" below, to avoid
  3097.          "statement never reached" errors on the one below.  */
  3098.       break;
  3099.     }
  3100.  
  3101.   return FALSE;
  3102. }
  3103.  
  3104. static bfd_boolean
  3105. coff_set_arch_mach (bfd * abfd,
  3106.                     enum bfd_architecture arch,
  3107.                     unsigned long machine)
  3108. {
  3109.   unsigned dummy1;
  3110.   unsigned short dummy2;
  3111.  
  3112.   if (! bfd_default_set_arch_mach (abfd, arch, machine))
  3113.     return FALSE;
  3114.  
  3115.   if (arch != bfd_arch_unknown
  3116.       && ! coff_set_flags (abfd, &dummy1, &dummy2))
  3117.     return FALSE;               /* We can't represent this type.  */
  3118.  
  3119.   return TRUE;                  /* We're easy...  */
  3120. }
  3121.  
  3122. #ifdef COFF_IMAGE_WITH_PE
  3123.  
  3124. /* This is used to sort sections by VMA, as required by PE image
  3125.    files.  */
  3126.  
  3127. static int
  3128. sort_by_secaddr (const void * arg1, const void * arg2)
  3129. {
  3130.   const asection *a = *(const asection **) arg1;
  3131.   const asection *b = *(const asection **) arg2;
  3132.  
  3133.   if (a->vma < b->vma)
  3134.     return -1;
  3135.   else if (a->vma > b->vma)
  3136.     return 1;
  3137.  
  3138.   return 0;
  3139. }
  3140.  
  3141. #endif /* COFF_IMAGE_WITH_PE */
  3142.  
  3143. /* Calculate the file position for each section.  */
  3144.  
  3145. #ifndef I960
  3146. #define ALIGN_SECTIONS_IN_FILE
  3147. #endif
  3148. #if defined(TIC80COFF) || defined(TICOFF)
  3149. #undef ALIGN_SECTIONS_IN_FILE
  3150. #endif
  3151.  
  3152. static bfd_boolean
  3153. coff_compute_section_file_positions (bfd * abfd)
  3154. {
  3155.   asection *current;
  3156.   file_ptr sofar = bfd_coff_filhsz (abfd);
  3157.   bfd_boolean align_adjust;
  3158.   unsigned int target_index;
  3159. #ifdef ALIGN_SECTIONS_IN_FILE
  3160.   asection *previous = NULL;
  3161.   file_ptr old_sofar;
  3162. #endif
  3163.  
  3164. #ifdef COFF_IMAGE_WITH_PE
  3165.   int page_size;
  3166.  
  3167.   if (coff_data (abfd)->link_info
  3168.       || (pe_data (abfd) && pe_data (abfd)->pe_opthdr.FileAlignment))
  3169.     {
  3170.       page_size = pe_data (abfd)->pe_opthdr.FileAlignment;
  3171.  
  3172.       /* If no file alignment has been set, default to one.
  3173.          This repairs 'ld -r' for arm-wince-pe target.  */
  3174.       if (page_size == 0)
  3175.         page_size = 1;
  3176.  
  3177.       /* PR 17512: file: 0ac816d3.  */
  3178.       if (page_size < 0)
  3179.         {
  3180.           bfd_set_error (bfd_error_file_too_big);
  3181.           (*_bfd_error_handler)
  3182.             (_("%B: page size is too large (0x%x)"), abfd, page_size);
  3183.           return FALSE;
  3184.         }
  3185.     }
  3186.   else
  3187.     page_size = PE_DEF_FILE_ALIGNMENT;
  3188. #else
  3189. #ifdef COFF_PAGE_SIZE
  3190.   int page_size = COFF_PAGE_SIZE;
  3191. #endif
  3192. #endif
  3193.  
  3194. #ifdef RS6000COFF_C
  3195.   /* On XCOFF, if we have symbols, set up the .debug section.  */
  3196.   if (bfd_get_symcount (abfd) > 0)
  3197.     {
  3198.       bfd_size_type sz;
  3199.       bfd_size_type i, symcount;
  3200.       asymbol **symp;
  3201.  
  3202.       sz = 0;
  3203.       symcount = bfd_get_symcount (abfd);
  3204.       for (symp = abfd->outsymbols, i = 0; i < symcount; symp++, i++)
  3205.         {
  3206.           coff_symbol_type *cf;
  3207.  
  3208.           cf = coff_symbol_from (*symp);
  3209.           if (cf != NULL
  3210.               && cf->native != NULL
  3211.               && cf->native->is_sym
  3212.               && SYMNAME_IN_DEBUG (&cf->native->u.syment))
  3213.             {
  3214.               size_t len;
  3215.  
  3216.               len = strlen (bfd_asymbol_name (*symp));
  3217.               if (len > SYMNMLEN || bfd_coff_force_symnames_in_strings (abfd))
  3218.                 sz += len + 1 + bfd_coff_debug_string_prefix_length (abfd);
  3219.             }
  3220.         }
  3221.       if (sz > 0)
  3222.         {
  3223.           asection *dsec;
  3224.  
  3225.           dsec = bfd_make_section_old_way (abfd, DOT_DEBUG);
  3226.           if (dsec == NULL)
  3227.             abort ();
  3228.           dsec->size = sz;
  3229.           dsec->flags |= SEC_HAS_CONTENTS;
  3230.         }
  3231.     }
  3232. #endif
  3233.  
  3234.   if (bfd_get_start_address (abfd))
  3235.     /*  A start address may have been added to the original file. In this
  3236.         case it will need an optional header to record it.  */
  3237.     abfd->flags |= EXEC_P;
  3238.  
  3239.   if (abfd->flags & EXEC_P)
  3240.     sofar += bfd_coff_aoutsz (abfd);
  3241. #ifdef RS6000COFF_C
  3242.   else if (xcoff_data (abfd)->full_aouthdr)
  3243.     sofar += bfd_coff_aoutsz (abfd);
  3244.   else
  3245.     sofar += SMALL_AOUTSZ;
  3246. #endif
  3247.  
  3248.   sofar += abfd->section_count * bfd_coff_scnhsz (abfd);
  3249.  
  3250. #ifdef RS6000COFF_C
  3251.   /* XCOFF handles overflows in the reloc and line number count fields
  3252.      by allocating a new section header to hold the correct counts.  */
  3253.   for (current = abfd->sections; current != NULL; current = current->next)
  3254.     if (current->reloc_count >= 0xffff || current->lineno_count >= 0xffff)
  3255.       sofar += bfd_coff_scnhsz (abfd);
  3256. #endif
  3257.  
  3258. #ifdef COFF_IMAGE_WITH_PE
  3259.   {
  3260.     /* PE requires the sections to be in memory order when listed in
  3261.        the section headers.  It also does not like empty loadable
  3262.        sections.  The sections apparently do not have to be in the
  3263.        right order in the image file itself, but we do need to get the
  3264.        target_index values right.  */
  3265.  
  3266.     unsigned int count;
  3267.     asection **section_list;
  3268.     unsigned int i;
  3269.     bfd_size_type amt;
  3270.  
  3271. #ifdef COFF_PAGE_SIZE
  3272.     /* Clear D_PAGED if section alignment is smaller than
  3273.        COFF_PAGE_SIZE.  */
  3274.    if (pe_data (abfd)->pe_opthdr.SectionAlignment < COFF_PAGE_SIZE)
  3275.      abfd->flags &= ~D_PAGED;
  3276. #endif
  3277.  
  3278.     count = 0;
  3279.     for (current = abfd->sections; current != NULL; current = current->next)
  3280.       ++count;
  3281.  
  3282.     /* We allocate an extra cell to simplify the final loop.  */
  3283.     amt = sizeof (struct asection *) * (count + 1);
  3284.     section_list = (asection **) bfd_malloc (amt);
  3285.     if (section_list == NULL)
  3286.       return FALSE;
  3287.  
  3288.     i = 0;
  3289.     for (current = abfd->sections; current != NULL; current = current->next)
  3290.       {
  3291.         section_list[i] = current;
  3292.         ++i;
  3293.       }
  3294.     section_list[i] = NULL;
  3295.  
  3296.     qsort (section_list, count, sizeof (asection *), sort_by_secaddr);
  3297.  
  3298.     /* Rethread the linked list into sorted order; at the same time,
  3299.        assign target_index values.  */
  3300.     target_index = 1;
  3301.     abfd->sections = NULL;
  3302.     abfd->section_last = NULL;
  3303.     for (i = 0; i < count; i++)
  3304.       {
  3305.         current = section_list[i];
  3306.         bfd_section_list_append (abfd, current);
  3307.  
  3308.         /* Later, if the section has zero size, we'll be throwing it
  3309.            away, so we don't want to number it now.  Note that having
  3310.            a zero size and having real contents are different
  3311.            concepts: .bss has no contents, but (usually) non-zero
  3312.            size.  */
  3313.         if (current->size == 0)
  3314.           {
  3315.             /* Discard.  However, it still might have (valid) symbols
  3316.                in it, so arbitrarily set it to section 1 (indexing is
  3317.                1-based here; usually .text).  __end__ and other
  3318.                contents of .endsection really have this happen.
  3319.                FIXME: This seems somewhat dubious.  */
  3320.             current->target_index = 1;
  3321.           }
  3322.         else
  3323.           current->target_index = target_index++;
  3324.       }
  3325.  
  3326.     free (section_list);
  3327.   }
  3328. #else /* ! COFF_IMAGE_WITH_PE */
  3329.   {
  3330.     /* Set the target_index field.  */
  3331.     target_index = 1;
  3332.     for (current = abfd->sections; current != NULL; current = current->next)
  3333.       current->target_index = target_index++;
  3334.   }
  3335. #endif /* ! COFF_IMAGE_WITH_PE */
  3336.  
  3337.   if (target_index >= bfd_coff_max_nscns (abfd))
  3338.     {
  3339.       bfd_set_error (bfd_error_file_too_big);
  3340.       (*_bfd_error_handler)
  3341.         (_("%B: too many sections (%d)"), abfd, target_index);
  3342.       return FALSE;
  3343.     }
  3344.  
  3345.   align_adjust = FALSE;
  3346.   for (current = abfd->sections;
  3347.        current != NULL;
  3348.        current = current->next)
  3349.     {
  3350. #ifdef COFF_IMAGE_WITH_PE
  3351.       /* With PE we have to pad each section to be a multiple of its
  3352.          page size too, and remember both sizes.  */
  3353.       if (coff_section_data (abfd, current) == NULL)
  3354.         {
  3355.           bfd_size_type amt = sizeof (struct coff_section_tdata);
  3356.  
  3357.           current->used_by_bfd = bfd_zalloc (abfd, amt);
  3358.           if (current->used_by_bfd == NULL)
  3359.             return FALSE;
  3360.         }
  3361.       if (pei_section_data (abfd, current) == NULL)
  3362.         {
  3363.           bfd_size_type amt = sizeof (struct pei_section_tdata);
  3364.  
  3365.           coff_section_data (abfd, current)->tdata = bfd_zalloc (abfd, amt);
  3366.           if (coff_section_data (abfd, current)->tdata == NULL)
  3367.             return FALSE;
  3368.         }
  3369.       if (pei_section_data (abfd, current)->virt_size == 0)
  3370.         pei_section_data (abfd, current)->virt_size = current->size;
  3371. #endif
  3372.  
  3373.       /* Only deal with sections which have contents.  */
  3374.       if (!(current->flags & SEC_HAS_CONTENTS))
  3375.         continue;
  3376.  
  3377.       current->rawsize = current->size;
  3378.  
  3379. #ifdef COFF_IMAGE_WITH_PE
  3380.       /* Make sure we skip empty sections in a PE image.  */
  3381.       if (current->size == 0)
  3382.         continue;
  3383. #endif
  3384.  
  3385.       /* Align the sections in the file to the same boundary on
  3386.          which they are aligned in virtual memory.  I960 doesn't
  3387.          do this (FIXME) so we can stay in sync with Intel.  960
  3388.          doesn't yet page from files...  */
  3389. #ifdef ALIGN_SECTIONS_IN_FILE
  3390.       if ((abfd->flags & EXEC_P) != 0)
  3391.         {
  3392.           /* Make sure this section is aligned on the right boundary - by
  3393.              padding the previous section up if necessary.  */
  3394.           old_sofar = sofar;
  3395.  
  3396.           sofar = BFD_ALIGN (sofar, 1 << current->alignment_power);
  3397.  
  3398. #ifdef RS6000COFF_C
  3399.           /* Make sure the file offset and the vma of .text/.data are at the
  3400.              same page offset, so that the file can be mmap'ed without being
  3401.              relocated.  Failing that, AIX is able to load and execute the
  3402.              program, but it will be silently relocated (possible as
  3403.              executables are PIE).  But the relocation is slightly costly and
  3404.              complexify the use of addr2line or gdb.  So better to avoid it,
  3405.              like does the native linker.  Usually gnu ld makes sure that
  3406.              the vma of .text is the file offset so this issue shouldn't
  3407.              appear unless you are stripping such an executable.
  3408.  
  3409.              AIX loader checks the text section alignment of (vma - filepos),
  3410.              and the native linker doesn't try to align the text sections.
  3411.              For example:
  3412.  
  3413.              0 .text         000054cc  10000128  10000128  00000128  2**5
  3414.                              CONTENTS, ALLOC, LOAD, CODE
  3415.           */
  3416.  
  3417.           if (!strcmp (current->name, _TEXT)
  3418.               || !strcmp (current->name, _DATA))
  3419.             {
  3420.               bfd_vma align = 4096;
  3421.               bfd_vma sofar_off = sofar % align;
  3422.               bfd_vma vma_off = current->vma % align;
  3423.  
  3424.               if (vma_off > sofar_off)
  3425.                 sofar += vma_off - sofar_off;
  3426.               else if (vma_off < sofar_off)
  3427.                 sofar += align + vma_off - sofar_off;
  3428.             }
  3429. #endif
  3430.           if (previous != NULL)
  3431.             previous->size += sofar - old_sofar;
  3432.         }
  3433.  
  3434. #endif
  3435.  
  3436.       /* In demand paged files the low order bits of the file offset
  3437.          must match the low order bits of the virtual address.  */
  3438. #ifdef COFF_PAGE_SIZE
  3439.       if ((abfd->flags & D_PAGED) != 0
  3440.           && (current->flags & SEC_ALLOC) != 0)
  3441.         sofar += (current->vma - (bfd_vma) sofar) % page_size;
  3442. #endif
  3443.       current->filepos = sofar;
  3444.  
  3445. #ifdef COFF_IMAGE_WITH_PE
  3446.       /* Set the padded size.  */
  3447.       current->size = (current->size + page_size - 1) & -page_size;
  3448. #endif
  3449.  
  3450.       sofar += current->size;
  3451.  
  3452. #ifdef ALIGN_SECTIONS_IN_FILE
  3453.       /* Make sure that this section is of the right size too.  */
  3454.       if ((abfd->flags & EXEC_P) == 0)
  3455.         {
  3456.           bfd_size_type old_size;
  3457.  
  3458.           old_size = current->size;
  3459.           current->size = BFD_ALIGN (current->size,
  3460.                                      1 << current->alignment_power);
  3461.           align_adjust = current->size != old_size;
  3462.           sofar += current->size - old_size;
  3463.         }
  3464.       else
  3465.         {
  3466.           old_sofar = sofar;
  3467.           sofar = BFD_ALIGN (sofar, 1 << current->alignment_power);
  3468.           align_adjust = sofar != old_sofar;
  3469.           current->size += sofar - old_sofar;
  3470.         }
  3471. #endif
  3472.  
  3473. #ifdef COFF_IMAGE_WITH_PE
  3474.       /* For PE we need to make sure we pad out to the aligned
  3475.          size, in case the caller only writes out data to the
  3476.          unaligned size.  */
  3477.       if (pei_section_data (abfd, current)->virt_size < current->size)
  3478.         align_adjust = TRUE;
  3479. #endif
  3480.  
  3481. #ifdef _LIB
  3482.       /* Force .lib sections to start at zero.  The vma is then
  3483.          incremented in coff_set_section_contents.  This is right for
  3484.          SVR3.2.  */
  3485.       if (strcmp (current->name, _LIB) == 0)
  3486.         (void) bfd_set_section_vma (abfd, current, 0);
  3487. #endif
  3488.  
  3489. #ifdef ALIGN_SECTIONS_IN_FILE
  3490.       previous = current;
  3491. #endif
  3492.     }
  3493.  
  3494.   /* It is now safe to write to the output file.  If we needed an
  3495.      alignment adjustment for the last section, then make sure that
  3496.      there is a byte at offset sofar.  If there are no symbols and no
  3497.      relocs, then nothing follows the last section.  If we don't force
  3498.      the last byte out, then the file may appear to be truncated.  */
  3499.   if (align_adjust)
  3500.     {
  3501.       bfd_byte b;
  3502.  
  3503.       b = 0;
  3504.       if (bfd_seek (abfd, sofar - 1, SEEK_SET) != 0
  3505.           || bfd_bwrite (&b, (bfd_size_type) 1, abfd) != 1)
  3506.         return FALSE;
  3507.     }
  3508.  
  3509.   /* Make sure the relocations are aligned.  We don't need to make
  3510.      sure that this byte exists, because it will only matter if there
  3511.      really are relocs.  */
  3512.   sofar = BFD_ALIGN (sofar, 1 << COFF_DEFAULT_SECTION_ALIGNMENT_POWER);
  3513.  
  3514.   obj_relocbase (abfd) = sofar;
  3515.   abfd->output_has_begun = TRUE;
  3516.  
  3517.   return TRUE;
  3518. }
  3519.  
  3520. #ifdef COFF_IMAGE_WITH_PE
  3521.  
  3522. static unsigned int pelength;
  3523. static unsigned int peheader;
  3524.  
  3525. static bfd_boolean
  3526. coff_read_word (bfd *abfd, unsigned int *value)
  3527. {
  3528.   unsigned char b[2];
  3529.   int status;
  3530.  
  3531.   status = bfd_bread (b, (bfd_size_type) 2, abfd);
  3532.   if (status < 1)
  3533.     {
  3534.       *value = 0;
  3535.       return FALSE;
  3536.     }
  3537.  
  3538.   if (status == 1)
  3539.     *value = (unsigned int) b[0];
  3540.   else
  3541.     *value = (unsigned int) (b[0] + (b[1] << 8));
  3542.  
  3543.   pelength += (unsigned int) status;
  3544.  
  3545.   return TRUE;
  3546. }
  3547.  
  3548. static unsigned int
  3549. coff_compute_checksum (bfd *abfd)
  3550. {
  3551.   bfd_boolean more_data;
  3552.   file_ptr filepos;
  3553.   unsigned int value;
  3554.   unsigned int total;
  3555.  
  3556.   total = 0;
  3557.   pelength = 0;
  3558.   filepos = (file_ptr) 0;
  3559.  
  3560.   do
  3561.     {
  3562.       if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
  3563.         return 0;
  3564.  
  3565.       more_data = coff_read_word (abfd, &value);
  3566.       total += value;
  3567.       total = 0xffff & (total + (total >> 0x10));
  3568.       filepos += 2;
  3569.     }
  3570.   while (more_data);
  3571.  
  3572.   return (0xffff & (total + (total >> 0x10)));
  3573. }
  3574.  
  3575. static bfd_boolean
  3576. coff_apply_checksum (bfd *abfd)
  3577. {
  3578.   unsigned int computed;
  3579.   unsigned int checksum = 0;
  3580.  
  3581.   if (bfd_seek (abfd, 0x3c, SEEK_SET) != 0)
  3582.     return FALSE;
  3583.  
  3584.   if (!coff_read_word (abfd, &peheader))
  3585.     return FALSE;
  3586.  
  3587.   if (bfd_seek (abfd, peheader + 0x58, SEEK_SET) != 0)
  3588.     return FALSE;
  3589.  
  3590.   checksum = 0;
  3591.   bfd_bwrite (&checksum, (bfd_size_type) 4, abfd);
  3592.  
  3593.   if (bfd_seek (abfd, peheader, SEEK_SET) != 0)
  3594.     return FALSE;
  3595.  
  3596.   computed = coff_compute_checksum (abfd);
  3597.  
  3598.   checksum = computed + pelength;
  3599.  
  3600.   if (bfd_seek (abfd, peheader + 0x58, SEEK_SET) != 0)
  3601.     return FALSE;
  3602.  
  3603.   bfd_bwrite (&checksum, (bfd_size_type) 4, abfd);
  3604.  
  3605.   return TRUE;
  3606. }
  3607.  
  3608. #endif /* COFF_IMAGE_WITH_PE */
  3609.  
  3610. static bfd_boolean
  3611. coff_write_object_contents (bfd * abfd)
  3612. {
  3613.   asection *current;
  3614.   bfd_boolean hasrelocs = FALSE;
  3615.   bfd_boolean haslinno = FALSE;
  3616. #ifdef COFF_IMAGE_WITH_PE
  3617.   bfd_boolean hasdebug = FALSE;
  3618. #endif
  3619.   file_ptr scn_base;
  3620.   file_ptr reloc_base;
  3621.   file_ptr lineno_base;
  3622.   file_ptr sym_base;
  3623.   unsigned long reloc_size = 0, reloc_count = 0;
  3624.   unsigned long lnno_size = 0;
  3625.   bfd_boolean long_section_names;
  3626.   asection *text_sec = NULL;
  3627.   asection *data_sec = NULL;
  3628.   asection *bss_sec = NULL;
  3629.   struct internal_filehdr internal_f;
  3630.   struct internal_aouthdr internal_a;
  3631. #ifdef COFF_LONG_SECTION_NAMES
  3632.   size_t string_size = STRING_SIZE_SIZE;
  3633. #endif
  3634.  
  3635.   bfd_set_error (bfd_error_system_call);
  3636.  
  3637.   /* Make a pass through the symbol table to count line number entries and
  3638.      put them into the correct asections.  */
  3639.   lnno_size = coff_count_linenumbers (abfd) * bfd_coff_linesz (abfd);
  3640.  
  3641.   if (! abfd->output_has_begun)
  3642.     {
  3643.       if (! coff_compute_section_file_positions (abfd))
  3644.         return FALSE;
  3645.     }
  3646.  
  3647.   reloc_base = obj_relocbase (abfd);
  3648.  
  3649.   /* Work out the size of the reloc and linno areas.  */
  3650.  
  3651.   for (current = abfd->sections; current != NULL; current =
  3652.        current->next)
  3653.     {
  3654. #ifdef COFF_WITH_PE
  3655.       /* We store the actual reloc count in the first reloc's addr.  */
  3656.       if (obj_pe (abfd) && current->reloc_count >= 0xffff)
  3657.         reloc_count ++;
  3658. #endif
  3659.       reloc_count += current->reloc_count;
  3660.     }
  3661.  
  3662.   reloc_size = reloc_count * bfd_coff_relsz (abfd);
  3663.  
  3664.   lineno_base = reloc_base + reloc_size;
  3665.   sym_base = lineno_base + lnno_size;
  3666.  
  3667.   /* Indicate in each section->line_filepos its actual file address.  */
  3668.   for (current = abfd->sections; current != NULL; current =
  3669.        current->next)
  3670.     {
  3671.       if (current->lineno_count)
  3672.         {
  3673.           current->line_filepos = lineno_base;
  3674.           current->moving_line_filepos = lineno_base;
  3675.           lineno_base += current->lineno_count * bfd_coff_linesz (abfd);
  3676.         }
  3677.       else
  3678.         current->line_filepos = 0;
  3679.  
  3680.       if (current->reloc_count)
  3681.         {
  3682.           current->rel_filepos = reloc_base;
  3683.           reloc_base += current->reloc_count * bfd_coff_relsz (abfd);
  3684. #ifdef COFF_WITH_PE
  3685.           /* Extra reloc to hold real count.  */
  3686.           if (obj_pe (abfd) && current->reloc_count >= 0xffff)
  3687.             reloc_base += bfd_coff_relsz (abfd);
  3688. #endif
  3689.         }
  3690.       else
  3691.         current->rel_filepos = 0;
  3692.     }
  3693.  
  3694.   /* Write section headers to the file.  */
  3695.   internal_f.f_nscns = 0;
  3696.  
  3697.   if ((abfd->flags & EXEC_P) != 0)
  3698.     scn_base = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
  3699.   else
  3700.     {
  3701.       scn_base = bfd_coff_filhsz (abfd);
  3702. #ifdef RS6000COFF_C
  3703. #ifndef XCOFF64
  3704.       if (xcoff_data (abfd)->full_aouthdr)
  3705.         scn_base += bfd_coff_aoutsz (abfd);
  3706.       else
  3707.         scn_base += SMALL_AOUTSZ;
  3708. #endif
  3709. #endif
  3710.     }
  3711.  
  3712.   if (bfd_seek (abfd, scn_base, SEEK_SET) != 0)
  3713.     return FALSE;
  3714.  
  3715.   long_section_names = FALSE;
  3716.   for (current = abfd->sections;
  3717.        current != NULL;
  3718.        current = current->next)
  3719.     {
  3720.       struct internal_scnhdr section;
  3721. #ifdef COFF_IMAGE_WITH_PE
  3722.       bfd_boolean is_reloc_section = FALSE;
  3723.  
  3724.       if (strcmp (current->name, DOT_RELOC) == 0)
  3725.         {
  3726.           is_reloc_section = TRUE;
  3727.           hasrelocs = TRUE;
  3728.           pe_data (abfd)->has_reloc_section = 1;
  3729.         }
  3730. #endif
  3731.  
  3732.       internal_f.f_nscns++;
  3733.  
  3734.       strncpy (section.s_name, current->name, SCNNMLEN);
  3735.  
  3736. #ifdef COFF_LONG_SECTION_NAMES
  3737.       /* Handle long section names as in PE.  This must be compatible
  3738.          with the code in coff_write_symbols and _bfd_coff_final_link.  */
  3739.       if (bfd_coff_long_section_names (abfd))
  3740.         {
  3741.           size_t len;
  3742.  
  3743.           len = strlen (current->name);
  3744.           if (len > SCNNMLEN)
  3745.             {
  3746.               /* The s_name field is defined to be NUL-padded but need not be
  3747.                  NUL-terminated.  We use a temporary buffer so that we can still
  3748.                  sprintf all eight chars without splatting a terminating NUL
  3749.                  over the first byte of the following member (s_paddr).  */
  3750.               char s_name_buf[SCNNMLEN + 1];
  3751.  
  3752.               /* An inherent limitation of the /nnnnnnn notation used to indicate
  3753.                  the offset of the long name in the string table is that we
  3754.                  cannot address entries beyone the ten million byte boundary.  */
  3755.               if (string_size >= 10000000)
  3756.                 {
  3757.                   bfd_set_error (bfd_error_file_too_big);
  3758.                   (*_bfd_error_handler)
  3759.                     (_("%B: section %s: string table overflow at offset %ld"),
  3760.                     abfd, current->name, string_size);
  3761.                   return FALSE;
  3762.                 }
  3763.  
  3764.               /* snprintf not strictly necessary now we've verified the value
  3765.                  has less than eight ASCII digits, but never mind.  */
  3766.               snprintf (s_name_buf, SCNNMLEN + 1, "/%lu", (unsigned long) string_size);
  3767.               /* Then strncpy takes care of any padding for us.  */
  3768.               strncpy (section.s_name, s_name_buf, SCNNMLEN);
  3769.               string_size += len + 1;
  3770.               long_section_names = TRUE;
  3771.             }
  3772.         }
  3773. #endif
  3774.  
  3775. #ifdef _LIB
  3776.       /* Always set s_vaddr of .lib to 0.  This is right for SVR3.2
  3777.          Ian Taylor <ian@cygnus.com>.  */
  3778.       if (strcmp (current->name, _LIB) == 0)
  3779.         section.s_vaddr = 0;
  3780.       else
  3781. #endif
  3782.       section.s_vaddr = current->vma;
  3783.       section.s_paddr = current->lma;
  3784.       section.s_size =  current->size;
  3785. #ifdef coff_get_section_load_page
  3786.       section.s_page = coff_get_section_load_page (current);
  3787. #else
  3788.       section.s_page = 0;
  3789. #endif
  3790.  
  3791. #ifdef COFF_WITH_PE
  3792.       section.s_paddr = 0;
  3793. #endif
  3794. #ifdef COFF_IMAGE_WITH_PE
  3795.       /* Reminder: s_paddr holds the virtual size of the section.  */
  3796.       if (coff_section_data (abfd, current) != NULL
  3797.           && pei_section_data (abfd, current) != NULL)
  3798.         section.s_paddr = pei_section_data (abfd, current)->virt_size;
  3799.       else
  3800.         section.s_paddr = 0;
  3801. #endif
  3802.  
  3803.       /* If this section has no size or is unloadable then the scnptr
  3804.          will be 0 too.  */
  3805.       if (current->size == 0
  3806.           || (current->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
  3807.         section.s_scnptr = 0;
  3808.       else
  3809.         section.s_scnptr = current->filepos;
  3810.  
  3811.       section.s_relptr = current->rel_filepos;
  3812.       section.s_lnnoptr = current->line_filepos;
  3813.       section.s_nreloc = current->reloc_count;
  3814.       section.s_nlnno = current->lineno_count;
  3815. #ifndef COFF_IMAGE_WITH_PE
  3816.       /* In PEI, relocs come in the .reloc section.  */
  3817.       if (current->reloc_count != 0)
  3818.         hasrelocs = TRUE;
  3819. #endif
  3820.       if (current->lineno_count != 0)
  3821.         haslinno = TRUE;
  3822. #ifdef COFF_IMAGE_WITH_PE
  3823.       if ((current->flags & SEC_DEBUGGING) != 0
  3824.           && ! is_reloc_section)
  3825.         hasdebug = TRUE;
  3826. #endif
  3827.  
  3828. #ifdef RS6000COFF_C
  3829. #ifndef XCOFF64
  3830.       /* Indicate the use of an XCOFF overflow section header.  */
  3831.       if (current->reloc_count >= 0xffff || current->lineno_count >= 0xffff)
  3832.         {
  3833.           section.s_nreloc = 0xffff;
  3834.           section.s_nlnno = 0xffff;
  3835.         }
  3836. #endif
  3837. #endif
  3838.  
  3839.       section.s_flags = sec_to_styp_flags (current->name, current->flags);
  3840.  
  3841.       if (!strcmp (current->name, _TEXT))
  3842.         text_sec = current;
  3843.       else if (!strcmp (current->name, _DATA))
  3844.         data_sec = current;
  3845.       else if (!strcmp (current->name, _BSS))
  3846.         bss_sec = current;
  3847.  
  3848. #ifdef I960
  3849.       section.s_align = (current->alignment_power
  3850.                          ? 1 << current->alignment_power
  3851.                          : 0);
  3852. #endif
  3853. #ifdef TIC80COFF
  3854.       /* TI COFF puts the alignment power in bits 8-11 of the flags.  */
  3855.       section.s_flags |= (current->alignment_power & 0xF) << 8;
  3856. #endif
  3857. #ifdef COFF_ENCODE_ALIGNMENT
  3858.       COFF_ENCODE_ALIGNMENT(section, current->alignment_power);
  3859. #endif
  3860.  
  3861. #ifdef COFF_IMAGE_WITH_PE
  3862.       /* Suppress output of the sections if they are null.  ld
  3863.          includes the bss and data sections even if there is no size
  3864.          assigned to them.  NT loader doesn't like it if these section
  3865.          headers are included if the sections themselves are not
  3866.          needed.  See also coff_compute_section_file_positions.  */
  3867.       if (section.s_size == 0)
  3868.         internal_f.f_nscns--;
  3869.       else
  3870. #endif
  3871.         {
  3872.           SCNHDR buff;
  3873.           bfd_size_type amt = bfd_coff_scnhsz (abfd);
  3874.  
  3875.           if (coff_swap_scnhdr_out (abfd, &section, &buff) == 0
  3876.               || bfd_bwrite (& buff, amt, abfd) != amt)
  3877.             return FALSE;
  3878.         }
  3879.  
  3880. #ifdef COFF_WITH_PE
  3881.       /* PE stores COMDAT section information in the symbol table.  If
  3882.          this section is supposed to have some COMDAT info, track down
  3883.          the symbol in the symbol table and modify it.  */
  3884.       if ((current->flags & SEC_LINK_ONCE) != 0)
  3885.         {
  3886.           unsigned int i, count;
  3887.           asymbol **psym;
  3888.           coff_symbol_type *csym = NULL;
  3889.           asymbol **psymsec;
  3890.  
  3891.           psymsec = NULL;
  3892.           count = bfd_get_symcount (abfd);
  3893.           for (i = 0, psym = abfd->outsymbols; i < count; i++, psym++)
  3894.             {
  3895.               if ((*psym)->section != current)
  3896.                 continue;
  3897.  
  3898.               /* Remember the location of the first symbol in this
  3899.                  section.  */
  3900.               if (psymsec == NULL)
  3901.                 psymsec = psym;
  3902.  
  3903.               /* See if this is the section symbol.  */
  3904.               if (strcmp ((*psym)->name, current->name) == 0)
  3905.                 {
  3906.                   csym = coff_symbol_from (*psym);
  3907.                   if (csym == NULL
  3908.                       || csym->native == NULL
  3909.                       || ! csym->native->is_sym
  3910.                       || csym->native->u.syment.n_numaux < 1
  3911.                       || csym->native->u.syment.n_sclass != C_STAT
  3912.                       || csym->native->u.syment.n_type != T_NULL)
  3913.                     continue;
  3914.  
  3915.                   /* Here *PSYM is the section symbol for CURRENT.  */
  3916.  
  3917.                   break;
  3918.                 }
  3919.             }
  3920.  
  3921.           /* Did we find it?
  3922.              Note that we might not if we're converting the file from
  3923.              some other object file format.  */
  3924.           if (i < count)
  3925.             {
  3926.               combined_entry_type *aux;
  3927.  
  3928.               /* We don't touch the x_checksum field.  The
  3929.                  x_associated field is not currently supported.  */
  3930.  
  3931.               aux = csym->native + 1;
  3932.               BFD_ASSERT (! aux->is_sym);
  3933.               switch (current->flags & SEC_LINK_DUPLICATES)
  3934.                 {
  3935.                 case SEC_LINK_DUPLICATES_DISCARD:
  3936.                   aux->u.auxent.x_scn.x_comdat = IMAGE_COMDAT_SELECT_ANY;
  3937.                   break;
  3938.  
  3939.                 case SEC_LINK_DUPLICATES_ONE_ONLY:
  3940.                   aux->u.auxent.x_scn.x_comdat =
  3941.                     IMAGE_COMDAT_SELECT_NODUPLICATES;
  3942.                   break;
  3943.  
  3944.                 case SEC_LINK_DUPLICATES_SAME_SIZE:
  3945.                   aux->u.auxent.x_scn.x_comdat =
  3946.                     IMAGE_COMDAT_SELECT_SAME_SIZE;
  3947.                   break;
  3948.  
  3949.                 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
  3950.                   aux->u.auxent.x_scn.x_comdat =
  3951.                     IMAGE_COMDAT_SELECT_EXACT_MATCH;
  3952.                   break;
  3953.                 }
  3954.  
  3955.               /* The COMDAT symbol must be the first symbol from this
  3956.                  section in the symbol table.  In order to make this
  3957.                  work, we move the COMDAT symbol before the first
  3958.                  symbol we found in the search above.  It's OK to
  3959.                  rearrange the symbol table at this point, because
  3960.                  coff_renumber_symbols is going to rearrange it
  3961.                  further and fix up all the aux entries.  */
  3962.               if (psym != psymsec)
  3963.                 {
  3964.                   asymbol *hold;
  3965.                   asymbol **pcopy;
  3966.  
  3967.                   hold = *psym;
  3968.                   for (pcopy = psym; pcopy > psymsec; pcopy--)
  3969.                     pcopy[0] = pcopy[-1];
  3970.                   *psymsec = hold;
  3971.                 }
  3972.             }
  3973.         }
  3974. #endif /* COFF_WITH_PE */
  3975.     }
  3976.  
  3977. #ifdef RS6000COFF_C
  3978. #ifndef XCOFF64
  3979.   /* XCOFF handles overflows in the reloc and line number count fields
  3980.      by creating a new section header to hold the correct values.  */
  3981.   for (current = abfd->sections; current != NULL; current = current->next)
  3982.     {
  3983.       if (current->reloc_count >= 0xffff || current->lineno_count >= 0xffff)
  3984.         {
  3985.           struct internal_scnhdr scnhdr;
  3986.           SCNHDR buff;
  3987.           bfd_size_type amt;
  3988.  
  3989.           internal_f.f_nscns++;
  3990.           memcpy (scnhdr.s_name, ".ovrflo", 8);
  3991.           scnhdr.s_paddr = current->reloc_count;
  3992.           scnhdr.s_vaddr = current->lineno_count;
  3993.           scnhdr.s_size = 0;
  3994.           scnhdr.s_scnptr = 0;
  3995.           scnhdr.s_relptr = current->rel_filepos;
  3996.           scnhdr.s_lnnoptr = current->line_filepos;
  3997.           scnhdr.s_nreloc = current->target_index;
  3998.           scnhdr.s_nlnno = current->target_index;
  3999.           scnhdr.s_flags = STYP_OVRFLO;
  4000.           amt = bfd_coff_scnhsz (abfd);
  4001.           if (coff_swap_scnhdr_out (abfd, &scnhdr, &buff) == 0
  4002.               || bfd_bwrite (& buff, amt, abfd) != amt)
  4003.             return FALSE;
  4004.         }
  4005.     }
  4006. #endif
  4007. #endif
  4008.  
  4009.   /* OK, now set up the filehdr...  */
  4010.  
  4011.   /* Don't include the internal abs section in the section count */
  4012.  
  4013.   /* We will NOT put a fucking timestamp in the header here. Every time you
  4014.      put it back, I will come in and take it out again.  I'm sorry.  This
  4015.      field does not belong here.  We fill it with a 0 so it compares the
  4016.      same but is not a reasonable time. -- gnu@cygnus.com  */
  4017.   internal_f.f_timdat = 0;
  4018.   internal_f.f_flags = 0;
  4019.  
  4020.   if (abfd->flags & EXEC_P)
  4021.     internal_f.f_opthdr = bfd_coff_aoutsz (abfd);
  4022.   else
  4023.     {
  4024.       internal_f.f_opthdr = 0;
  4025. #ifdef RS6000COFF_C
  4026. #ifndef XCOFF64
  4027.       if (xcoff_data (abfd)->full_aouthdr)
  4028.         internal_f.f_opthdr = bfd_coff_aoutsz (abfd);
  4029.       else
  4030.         internal_f.f_opthdr = SMALL_AOUTSZ;
  4031. #endif
  4032. #endif
  4033.     }
  4034.  
  4035.   if (!hasrelocs)
  4036.     internal_f.f_flags |= F_RELFLG;
  4037.   if (!haslinno)
  4038.     internal_f.f_flags |= F_LNNO;
  4039.   if (abfd->flags & EXEC_P)
  4040.     internal_f.f_flags |= F_EXEC;
  4041. #ifdef COFF_IMAGE_WITH_PE
  4042.   if (! hasdebug)
  4043.     internal_f.f_flags |= IMAGE_FILE_DEBUG_STRIPPED;
  4044.   if (pe_data (abfd)->real_flags & IMAGE_FILE_LARGE_ADDRESS_AWARE)
  4045.     internal_f.f_flags |= IMAGE_FILE_LARGE_ADDRESS_AWARE;
  4046. #endif
  4047.  
  4048. #ifndef COFF_WITH_pex64
  4049. #ifdef COFF_WITH_PE
  4050.   internal_f.f_flags |= IMAGE_FILE_32BIT_MACHINE;
  4051. #else
  4052.   if (bfd_little_endian (abfd))
  4053.     internal_f.f_flags |= F_AR32WR;
  4054.   else
  4055.     internal_f.f_flags |= F_AR32W;
  4056. #endif
  4057. #endif
  4058.  
  4059. #ifdef TI_TARGET_ID
  4060.   /* Target id is used in TI COFF v1 and later; COFF0 won't use this field,
  4061.      but it doesn't hurt to set it internally.  */
  4062.   internal_f.f_target_id = TI_TARGET_ID;
  4063. #endif
  4064. #ifdef TIC80_TARGET_ID
  4065.   internal_f.f_target_id = TIC80_TARGET_ID;
  4066. #endif
  4067.  
  4068.   /* FIXME, should do something about the other byte orders and
  4069.      architectures.  */
  4070.  
  4071. #ifdef RS6000COFF_C
  4072.   if ((abfd->flags & DYNAMIC) != 0)
  4073.     internal_f.f_flags |= F_SHROBJ;
  4074.   if (bfd_get_section_by_name (abfd, _LOADER) != NULL)
  4075.     internal_f.f_flags |= F_DYNLOAD;
  4076. #endif
  4077.  
  4078.   /* Set up architecture-dependent stuff.  */
  4079.   {
  4080.     unsigned int magic = 0;
  4081.     unsigned short flags = 0;
  4082.  
  4083.     coff_set_flags (abfd, &magic, &flags);
  4084.     internal_f.f_magic = magic;
  4085.     internal_f.f_flags |= flags;
  4086.     /* ...and the "opt"hdr...  */
  4087.  
  4088. #ifdef TICOFF_AOUT_MAGIC
  4089.     internal_a.magic = TICOFF_AOUT_MAGIC;
  4090. #define __A_MAGIC_SET__
  4091. #endif
  4092. #ifdef TIC80COFF
  4093.     internal_a.magic = TIC80_ARCH_MAGIC;
  4094. #define __A_MAGIC_SET__
  4095. #endif /* TIC80 */
  4096. #ifdef I860
  4097.     /* FIXME: What are the a.out magic numbers for the i860?  */
  4098.     internal_a.magic = 0;
  4099. #define __A_MAGIC_SET__
  4100. #endif /* I860 */
  4101. #ifdef I960
  4102.     internal_a.magic = (magic == I960ROMAGIC ? NMAGIC : OMAGIC);
  4103. #define __A_MAGIC_SET__
  4104. #endif /* I960 */
  4105. #if M88
  4106. #define __A_MAGIC_SET__
  4107.     internal_a.magic = PAGEMAGICBCS;
  4108. #endif /* M88 */
  4109.  
  4110. #if APOLLO_M68
  4111. #define __A_MAGIC_SET__
  4112.     internal_a.magic = APOLLO_COFF_VERSION_NUMBER;
  4113. #endif
  4114.  
  4115. #if defined(M68) || defined(WE32K) || defined(M68K)
  4116. #define __A_MAGIC_SET__
  4117. #if defined(LYNXOS)
  4118.     internal_a.magic = LYNXCOFFMAGIC;
  4119. #else
  4120. #if defined(TARG_AUX)
  4121.     internal_a.magic = (abfd->flags & D_PAGED ? PAGEMAGICPEXECPAGED :
  4122.                         abfd->flags & WP_TEXT ? PAGEMAGICPEXECSWAPPED :
  4123.                         PAGEMAGICEXECSWAPPED);
  4124. #else
  4125. #if defined (PAGEMAGICPEXECPAGED)
  4126.     internal_a.magic = PAGEMAGICPEXECPAGED;
  4127. #endif
  4128. #endif /* TARG_AUX */
  4129. #endif /* LYNXOS */
  4130. #endif /* M68 || WE32K || M68K */
  4131.  
  4132. #if defined(ARM)
  4133. #define __A_MAGIC_SET__
  4134.     internal_a.magic = ZMAGIC;
  4135. #endif
  4136.  
  4137. #if defined(PPC_PE)
  4138. #define __A_MAGIC_SET__
  4139.     internal_a.magic = IMAGE_NT_OPTIONAL_HDR_MAGIC;
  4140. #endif
  4141.  
  4142. #if defined MCORE_PE
  4143. #define __A_MAGIC_SET__
  4144.     internal_a.magic = IMAGE_NT_OPTIONAL_HDR_MAGIC;
  4145. #endif
  4146.  
  4147. #if defined(I386)
  4148. #define __A_MAGIC_SET__
  4149. #if defined LYNXOS
  4150.     internal_a.magic = LYNXCOFFMAGIC;
  4151. #elif defined AMD64
  4152.     internal_a.magic = IMAGE_NT_OPTIONAL_HDR64_MAGIC;
  4153. #else
  4154.     internal_a.magic = ZMAGIC;
  4155. #endif
  4156. #endif /* I386 */
  4157.  
  4158. #if defined(IA64)
  4159. #define __A_MAGIC_SET__
  4160.     internal_a.magic = PE32PMAGIC;
  4161. #endif /* IA64 */
  4162.  
  4163. #if defined(SPARC)
  4164. #define __A_MAGIC_SET__
  4165. #if defined(LYNXOS)
  4166.     internal_a.magic = LYNXCOFFMAGIC;
  4167. #endif /* LYNXOS */
  4168. #endif /* SPARC */
  4169.  
  4170. #ifdef RS6000COFF_C
  4171. #define __A_MAGIC_SET__
  4172.     internal_a.magic = (abfd->flags & D_PAGED) ? RS6K_AOUTHDR_ZMAGIC :
  4173.     (abfd->flags & WP_TEXT) ? RS6K_AOUTHDR_NMAGIC :
  4174.     RS6K_AOUTHDR_OMAGIC;
  4175. #endif
  4176.  
  4177. #if defined(SH) && defined(COFF_WITH_PE)
  4178. #define __A_MAGIC_SET__
  4179.     internal_a.magic = SH_PE_MAGIC;
  4180. #endif
  4181.  
  4182. #if defined(MIPS) && defined(COFF_WITH_PE)
  4183. #define __A_MAGIC_SET__
  4184.     internal_a.magic = MIPS_PE_MAGIC;
  4185. #endif
  4186.  
  4187. #ifndef __A_MAGIC_SET__
  4188. #include "Your aouthdr magic number is not being set!"
  4189. #else
  4190. #undef __A_MAGIC_SET__
  4191. #endif
  4192.   }
  4193.  
  4194.   /* FIXME: Does anybody ever set this to another value?  */
  4195.   internal_a.vstamp = 0;
  4196.  
  4197.   /* Now should write relocs, strings, syms.  */
  4198.   obj_sym_filepos (abfd) = sym_base;
  4199.  
  4200.   if (bfd_get_symcount (abfd) != 0)
  4201.     {
  4202.       int firstundef;
  4203.  
  4204.       if (!coff_renumber_symbols (abfd, &firstundef))
  4205.         return FALSE;
  4206.       coff_mangle_symbols (abfd);
  4207.       if (! coff_write_symbols (abfd))
  4208.         return FALSE;
  4209.       if (! coff_write_linenumbers (abfd))
  4210.         return FALSE;
  4211.       if (! coff_write_relocs (abfd, firstundef))
  4212.         return FALSE;
  4213.     }
  4214. #ifdef COFF_LONG_SECTION_NAMES
  4215.   else if (long_section_names && ! obj_coff_strings_written (abfd))
  4216.     {
  4217.       /* If we have long section names we have to write out the string
  4218.          table even if there are no symbols.  */
  4219.       if (! coff_write_symbols (abfd))
  4220.         return FALSE;
  4221.     }
  4222. #endif
  4223. #ifdef COFF_IMAGE_WITH_PE
  4224. #ifdef PPC_PE
  4225.   else if ((abfd->flags & EXEC_P) != 0)
  4226.     {
  4227.       bfd_byte b;
  4228.  
  4229.       /* PowerPC PE appears to require that all executable files be
  4230.          rounded up to the page size.  */
  4231.       b = 0;
  4232.       if (bfd_seek (abfd,
  4233.                     (file_ptr) BFD_ALIGN (sym_base, COFF_PAGE_SIZE) - 1,
  4234.                     SEEK_SET) != 0
  4235.           || bfd_bwrite (&b, (bfd_size_type) 1, abfd) != 1)
  4236.         return FALSE;
  4237.     }
  4238. #endif
  4239. #endif
  4240.  
  4241.   /* If bfd_get_symcount (abfd) != 0, then we are not using the COFF
  4242.      backend linker, and obj_raw_syment_count is not valid until after
  4243.      coff_write_symbols is called.  */
  4244.   if (obj_raw_syment_count (abfd) != 0)
  4245.     {
  4246.       internal_f.f_symptr = sym_base;
  4247. #ifdef RS6000COFF_C
  4248.       /* AIX appears to require that F_RELFLG not be set if there are
  4249.          local symbols but no relocations.  */
  4250.       internal_f.f_flags &=~ F_RELFLG;
  4251. #endif
  4252.     }
  4253.   else
  4254.     {
  4255.       if (long_section_names)
  4256.         internal_f.f_symptr = sym_base;
  4257.       else
  4258.         internal_f.f_symptr = 0;
  4259.       internal_f.f_flags |= F_LSYMS;
  4260.     }
  4261.  
  4262.   if (text_sec)
  4263.     {
  4264.       internal_a.tsize = text_sec->size;
  4265.       internal_a.text_start = internal_a.tsize ? text_sec->vma : 0;
  4266.     }
  4267.   if (data_sec)
  4268.     {
  4269.       internal_a.dsize = data_sec->size;
  4270.       internal_a.data_start = internal_a.dsize ? data_sec->vma : 0;
  4271.     }
  4272.   if (bss_sec)
  4273.     {
  4274.       internal_a.bsize = bss_sec->size;
  4275.       if (internal_a.bsize && bss_sec->vma < internal_a.data_start)
  4276.         internal_a.data_start = bss_sec->vma;
  4277.     }
  4278.  
  4279.   internal_a.entry = bfd_get_start_address (abfd);
  4280.   internal_f.f_nsyms = obj_raw_syment_count (abfd);
  4281.  
  4282. #ifdef RS6000COFF_C
  4283.   if (xcoff_data (abfd)->full_aouthdr)
  4284.     {
  4285.       bfd_vma toc;
  4286.       asection *loader_sec;
  4287.  
  4288.       internal_a.vstamp = 1;
  4289.  
  4290.       internal_a.o_snentry = xcoff_data (abfd)->snentry;
  4291.       if (internal_a.o_snentry == 0)
  4292.         internal_a.entry = (bfd_vma) -1;
  4293.  
  4294.       if (text_sec != NULL)
  4295.         {
  4296.           internal_a.o_sntext = text_sec->target_index;
  4297.           internal_a.o_algntext = bfd_get_section_alignment (abfd, text_sec);
  4298.         }
  4299.       else
  4300.         {
  4301.           internal_a.o_sntext = 0;
  4302.           internal_a.o_algntext = 0;
  4303.         }
  4304.       if (data_sec != NULL)
  4305.         {
  4306.           internal_a.o_sndata = data_sec->target_index;
  4307.           internal_a.o_algndata = bfd_get_section_alignment (abfd, data_sec);
  4308.         }
  4309.       else
  4310.         {
  4311.           internal_a.o_sndata = 0;
  4312.           internal_a.o_algndata = 0;
  4313.         }
  4314.       loader_sec = bfd_get_section_by_name (abfd, ".loader");
  4315.       if (loader_sec != NULL)
  4316.         internal_a.o_snloader = loader_sec->target_index;
  4317.       else
  4318.         internal_a.o_snloader = 0;
  4319.       if (bss_sec != NULL)
  4320.         internal_a.o_snbss = bss_sec->target_index;
  4321.       else
  4322.         internal_a.o_snbss = 0;
  4323.  
  4324.       toc = xcoff_data (abfd)->toc;
  4325.       internal_a.o_toc = toc;
  4326.       internal_a.o_sntoc = xcoff_data (abfd)->sntoc;
  4327.  
  4328.       internal_a.o_modtype = xcoff_data (abfd)->modtype;
  4329.       if (xcoff_data (abfd)->cputype != -1)
  4330.         internal_a.o_cputype = xcoff_data (abfd)->cputype;
  4331.       else
  4332.         {
  4333.           switch (bfd_get_arch (abfd))
  4334.             {
  4335.             case bfd_arch_rs6000:
  4336.               internal_a.o_cputype = 4;
  4337.               break;
  4338.             case bfd_arch_powerpc:
  4339.               if (bfd_get_mach (abfd) == bfd_mach_ppc)
  4340.                 internal_a.o_cputype = 3;
  4341.               else
  4342.                 internal_a.o_cputype = 1;
  4343.               break;
  4344.             default:
  4345.               abort ();
  4346.             }
  4347.         }
  4348.       internal_a.o_maxstack = xcoff_data (abfd)->maxstack;
  4349.       internal_a.o_maxdata = xcoff_data (abfd)->maxdata;
  4350.     }
  4351. #endif
  4352.  
  4353. #ifdef COFF_WITH_PE
  4354.   {
  4355.     /* After object contents are finalized so we can compute a reasonable hash,
  4356.        but before header is written so we can update it to point to debug directory.  */
  4357.     struct pe_tdata *pe = pe_data (abfd);
  4358.  
  4359.     if (pe->build_id.after_write_object_contents != NULL)
  4360.       (*pe->build_id.after_write_object_contents) (abfd);
  4361.   }
  4362. #endif
  4363.  
  4364.   /* Now write header.  */
  4365.   if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
  4366.     return FALSE;
  4367.  
  4368.   {
  4369.     char * buff;
  4370.     bfd_size_type amount = bfd_coff_filhsz (abfd);
  4371.  
  4372.     buff = (char *) bfd_malloc (amount);
  4373.     if (buff == NULL)
  4374.       return FALSE;
  4375.  
  4376.     bfd_coff_swap_filehdr_out (abfd, & internal_f, buff);
  4377.     amount = bfd_bwrite (buff, amount, abfd);
  4378.  
  4379.     free (buff);
  4380.  
  4381.     if (amount != bfd_coff_filhsz (abfd))
  4382.       return FALSE;
  4383.   }
  4384.  
  4385.   if (abfd->flags & EXEC_P)
  4386.     {
  4387.       /* Note that peicode.h fills in a PEAOUTHDR, not an AOUTHDR.
  4388.          include/coff/pe.h sets AOUTSZ == sizeof (PEAOUTHDR)).  */
  4389.       char * buff;
  4390.       bfd_size_type amount = bfd_coff_aoutsz (abfd);
  4391.  
  4392.       buff = (char *) bfd_malloc (amount);
  4393.       if (buff == NULL)
  4394.         return FALSE;
  4395.  
  4396.       coff_swap_aouthdr_out (abfd, & internal_a, buff);
  4397.       amount = bfd_bwrite (buff, amount, abfd);
  4398.  
  4399.       free (buff);
  4400.  
  4401.       if (amount != bfd_coff_aoutsz (abfd))
  4402.         return FALSE;
  4403.  
  4404. #ifdef COFF_IMAGE_WITH_PE
  4405.       if (! coff_apply_checksum (abfd))
  4406.         return FALSE;
  4407. #endif
  4408.     }
  4409. #ifdef RS6000COFF_C
  4410.   else
  4411.     {
  4412.       AOUTHDR buff;
  4413.       size_t size;
  4414.  
  4415.       /* XCOFF seems to always write at least a small a.out header.  */
  4416.       coff_swap_aouthdr_out (abfd, & internal_a, & buff);
  4417.       if (xcoff_data (abfd)->full_aouthdr)
  4418.         size = bfd_coff_aoutsz (abfd);
  4419.       else
  4420.         size = SMALL_AOUTSZ;
  4421.       if (bfd_bwrite (& buff, (bfd_size_type) size, abfd) != size)
  4422.         return FALSE;
  4423.     }
  4424. #endif
  4425.  
  4426.   return TRUE;
  4427. }
  4428.  
  4429. static bfd_boolean
  4430. coff_set_section_contents (bfd * abfd,
  4431.                            sec_ptr section,
  4432.                            const void * location,
  4433.                            file_ptr offset,
  4434.                            bfd_size_type count)
  4435. {
  4436.   if (! abfd->output_has_begun) /* Set by bfd.c handler.  */
  4437.     {
  4438.       if (! coff_compute_section_file_positions (abfd))
  4439.         return FALSE;
  4440.     }
  4441.  
  4442. #if defined(_LIB) && !defined(TARG_AUX)
  4443.    /* The physical address field of a .lib section is used to hold the
  4444.       number of shared libraries in the section.  This code counts the
  4445.       number of sections being written, and increments the lma field
  4446.       with the number.
  4447.  
  4448.       I have found no documentation on the contents of this section.
  4449.       Experimentation indicates that the section contains zero or more
  4450.       records, each of which has the following structure:
  4451.  
  4452.       - a (four byte) word holding the length of this record, in words,
  4453.       - a word that always seems to be set to "2",
  4454.       - the path to a shared library, null-terminated and then padded
  4455.         to a whole word boundary.
  4456.  
  4457.       bfd_assert calls have been added to alert if an attempt is made
  4458.       to write a section which doesn't follow these assumptions.  The
  4459.       code has been tested on ISC 4.1 by me, and on SCO by Robert Lipe
  4460.       <robertl@arnet.com> (Thanks!).
  4461.  
  4462.       Gvran Uddeborg <gvran@uddeborg.pp.se>.  */
  4463.     if (strcmp (section->name, _LIB) == 0)
  4464.       {
  4465.         bfd_byte *rec, *recend;
  4466.  
  4467.         rec = (bfd_byte *) location;
  4468.         recend = rec + count;
  4469.         while (rec < recend)
  4470.           {
  4471.             ++section->lma;
  4472.             rec += bfd_get_32 (abfd, rec) * 4;
  4473.           }
  4474.  
  4475.         BFD_ASSERT (rec == recend);
  4476.       }
  4477. #endif
  4478.  
  4479.   /* Don't write out bss sections - one way to do this is to
  4480.        see if the filepos has not been set.  */
  4481.   if (section->filepos == 0)
  4482.     return TRUE;
  4483.  
  4484.   if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0)
  4485.     return FALSE;
  4486.  
  4487.   if (count == 0)
  4488.     return TRUE;
  4489.  
  4490.   return bfd_bwrite (location, count, abfd) == count;
  4491. }
  4492.  
  4493. static void *
  4494. buy_and_read (bfd *abfd, file_ptr where, bfd_size_type size)
  4495. {
  4496.   void * area = bfd_alloc (abfd, size);
  4497.  
  4498.   if (!area)
  4499.     return NULL;
  4500.   if (bfd_seek (abfd, where, SEEK_SET) != 0
  4501.       || bfd_bread (area, size, abfd) != size)
  4502.     return NULL;
  4503.   return area;
  4504. }
  4505.  
  4506. /*
  4507. SUBSUBSECTION
  4508.         Reading linenumbers
  4509.  
  4510.         Creating the linenumber table is done by reading in the entire
  4511.         coff linenumber table, and creating another table for internal use.
  4512.  
  4513.         A coff linenumber table is structured so that each function
  4514.         is marked as having a line number of 0. Each line within the
  4515.         function is an offset from the first line in the function. The
  4516.         base of the line number information for the table is stored in
  4517.         the symbol associated with the function.
  4518.  
  4519.         Note: The PE format uses line number 0 for a flag indicating a
  4520.         new source file.
  4521.  
  4522.         The information is copied from the external to the internal
  4523.         table, and each symbol which marks a function is marked by
  4524.         pointing its...
  4525.  
  4526.         How does this work ?
  4527. */
  4528.  
  4529. static int
  4530. coff_sort_func_alent (const void * arg1, const void * arg2)
  4531. {
  4532.   const alent *al1 = *(const alent **) arg1;
  4533.   const alent *al2 = *(const alent **) arg2;
  4534.   const coff_symbol_type *s1 = (const coff_symbol_type *) (al1->u.sym);
  4535.   const coff_symbol_type *s2 = (const coff_symbol_type *) (al2->u.sym);
  4536.  
  4537.   if (s1 == NULL || s2 == NULL)
  4538.     return 0;
  4539.   if (s1->symbol.value < s2->symbol.value)
  4540.     return -1;
  4541.   else if (s1->symbol.value > s2->symbol.value)
  4542.     return 1;
  4543.  
  4544.   return 0;
  4545. }
  4546.  
  4547. static bfd_boolean
  4548. coff_slurp_line_table (bfd *abfd, asection *asect)
  4549. {
  4550.   LINENO *native_lineno;
  4551.   alent *lineno_cache;
  4552.   bfd_size_type amt;
  4553.   unsigned int counter;
  4554.   alent *cache_ptr;
  4555.   bfd_vma prev_offset = 0;
  4556.   bfd_boolean ordered = TRUE;
  4557.   unsigned int nbr_func;
  4558.   LINENO *src;
  4559.   bfd_boolean have_func;
  4560.   bfd_boolean ret = TRUE;
  4561.  
  4562.   BFD_ASSERT (asect->lineno == NULL);
  4563.  
  4564.   amt = ((bfd_size_type) asect->lineno_count + 1) * sizeof (alent);
  4565.   lineno_cache = (alent *) bfd_alloc (abfd, amt);
  4566.   if (lineno_cache == NULL)
  4567.     return FALSE;
  4568.  
  4569.   amt = (bfd_size_type) bfd_coff_linesz (abfd) * asect->lineno_count;
  4570.   native_lineno = (LINENO *) buy_and_read (abfd, asect->line_filepos, amt);
  4571.   if (native_lineno == NULL)
  4572.     {
  4573.       (*_bfd_error_handler)
  4574.         (_("%B: warning: line number table read failed"), abfd);
  4575.       bfd_release (abfd, lineno_cache);
  4576.       return FALSE;
  4577.     }
  4578.  
  4579.   cache_ptr = lineno_cache;
  4580.   asect->lineno = lineno_cache;
  4581.   src = native_lineno;
  4582.   nbr_func = 0;
  4583.   have_func = FALSE;
  4584.  
  4585.   for (counter = 0; counter < asect->lineno_count; counter++, src++)
  4586.     {
  4587.       struct internal_lineno dst;
  4588.  
  4589.       bfd_coff_swap_lineno_in (abfd, src, &dst);
  4590.       cache_ptr->line_number = dst.l_lnno;
  4591.       /* Appease memory checkers that get all excited about
  4592.          uninitialised memory when copying alents if u.offset is
  4593.          larger than u.sym.  (64-bit BFD on 32-bit host.)  */
  4594.       memset (&cache_ptr->u, 0, sizeof (cache_ptr->u));
  4595.  
  4596.       if (cache_ptr->line_number == 0)
  4597.         {
  4598.           combined_entry_type * ent;
  4599.           bfd_vma symndx;
  4600.           coff_symbol_type *sym;
  4601.  
  4602.           have_func = FALSE;
  4603.           symndx = dst.l_addr.l_symndx;
  4604.           if (symndx >= obj_raw_syment_count (abfd))
  4605.             {
  4606.               (*_bfd_error_handler)
  4607.                 (_("%B: warning: illegal symbol index 0x%lx in line number entry %d"),
  4608.                  abfd, (long) symndx, counter);
  4609.               cache_ptr->line_number = -1;
  4610.               ret = FALSE;
  4611.               continue;
  4612.             }
  4613.  
  4614.           ent = obj_raw_syments (abfd) + symndx;
  4615.           /* FIXME: We should not be casting between ints and
  4616.              pointers like this.  */
  4617.           if (! ent->is_sym)
  4618.             {
  4619.               (*_bfd_error_handler)
  4620.                 (_("%B: warning: illegal symbol index 0x%lx in line number entry %d"),
  4621.                  abfd, (long) symndx, counter);
  4622.               cache_ptr->line_number = -1;
  4623.               ret = FALSE;
  4624.               continue;
  4625.             }
  4626.           sym = (coff_symbol_type *) (ent->u.syment._n._n_n._n_zeroes);
  4627.  
  4628.           /* PR 17512 file: 078-10659-0.004  */
  4629.           if (sym < obj_symbols (abfd)
  4630.               || sym >= obj_symbols (abfd) + bfd_get_symcount (abfd))
  4631.             {
  4632.               (*_bfd_error_handler)
  4633.                 (_("%B: warning: illegal symbol in line number entry %d"),
  4634.                  abfd, counter);
  4635.               cache_ptr->line_number = -1;
  4636.               ret = FALSE;
  4637.               continue;
  4638.             }
  4639.  
  4640.           have_func = TRUE;
  4641.           nbr_func++;
  4642.           cache_ptr->u.sym = (asymbol *) sym;
  4643.           if (sym->lineno != NULL)
  4644.             (*_bfd_error_handler)
  4645.               (_("%B: warning: duplicate line number information for `%s'"),
  4646.                abfd, bfd_asymbol_name (&sym->symbol));
  4647.  
  4648.           sym->lineno = cache_ptr;
  4649.           if (sym->symbol.value < prev_offset)
  4650.             ordered = FALSE;
  4651.           prev_offset = sym->symbol.value;
  4652.         }
  4653.       else if (!have_func)
  4654.         /* Drop line information that has no associated function.
  4655.            PR 17521: file: 078-10659-0.004.  */
  4656.         continue;
  4657.       else
  4658.         cache_ptr->u.offset = (dst.l_addr.l_paddr
  4659.                                - bfd_section_vma (abfd, asect));
  4660.       cache_ptr++;
  4661.     }
  4662.  
  4663.   asect->lineno_count = cache_ptr - lineno_cache;
  4664.   memset (cache_ptr, 0, sizeof (*cache_ptr));
  4665.   bfd_release (abfd, native_lineno);
  4666.  
  4667.   /* On some systems (eg AIX5.3) the lineno table may not be sorted.  */
  4668.   if (!ordered)
  4669.     {
  4670.       /* Sort the table.  */
  4671.       alent **func_table;
  4672.       alent *n_lineno_cache;
  4673.  
  4674.       /* Create a table of functions.  */
  4675.       func_table = (alent **) bfd_alloc (abfd, nbr_func * sizeof (alent *));
  4676.       if (func_table != NULL)
  4677.         {
  4678.           alent **p = func_table;
  4679.           unsigned int i;
  4680.  
  4681.           for (i = 0; i < asect->lineno_count; i++)
  4682.             if (lineno_cache[i].line_number == 0)
  4683.               *p++ = &lineno_cache[i];
  4684.  
  4685.           BFD_ASSERT ((unsigned int) (p - func_table) == nbr_func);
  4686.  
  4687.           /* Sort by functions.  */
  4688.           qsort (func_table, nbr_func, sizeof (alent *), coff_sort_func_alent);
  4689.  
  4690.           /* Create the new sorted table.  */
  4691.           amt = (bfd_size_type) asect->lineno_count * sizeof (alent);
  4692.           n_lineno_cache = (alent *) bfd_alloc (abfd, amt);
  4693.           if (n_lineno_cache != NULL)
  4694.             {
  4695.               alent *n_cache_ptr = n_lineno_cache;
  4696.  
  4697.               for (i = 0; i < nbr_func; i++)
  4698.                 {
  4699.                   coff_symbol_type *sym;
  4700.                   alent *old_ptr = func_table[i];
  4701.  
  4702.                   /* Update the function entry.  */
  4703.                   sym = (coff_symbol_type *) old_ptr->u.sym;
  4704.                   /* PR binutils/17512: Point the lineno to where
  4705.                      this entry will be after the memcpy below.  */
  4706.                   sym->lineno = lineno_cache + (n_cache_ptr - n_lineno_cache);
  4707.                   /* Copy the function and line number entries.  */
  4708.                   do
  4709.                     *n_cache_ptr++ = *old_ptr++;
  4710.                   while (old_ptr->line_number != 0);
  4711.                 }
  4712.               BFD_ASSERT ((bfd_size_type) (n_cache_ptr - n_lineno_cache) == (amt / sizeof (alent)));
  4713.  
  4714.               memcpy (lineno_cache, n_lineno_cache, amt);
  4715.             }
  4716.           else
  4717.             ret = FALSE;
  4718.           bfd_release (abfd, func_table);
  4719.         }
  4720.       else
  4721.         ret = FALSE;
  4722.     }
  4723.  
  4724.   return ret;
  4725. }
  4726.  
  4727. /* Slurp in the symbol table, converting it to generic form.  Note
  4728.    that if coff_relocate_section is defined, the linker will read
  4729.    symbols via coff_link_add_symbols, rather than via this routine.  */
  4730.  
  4731. static bfd_boolean
  4732. coff_slurp_symbol_table (bfd * abfd)
  4733. {
  4734.   combined_entry_type *native_symbols;
  4735.   coff_symbol_type *cached_area;
  4736.   unsigned int *table_ptr;
  4737.   bfd_size_type amt;
  4738.   unsigned int number_of_symbols = 0;
  4739.   bfd_boolean ret = TRUE;
  4740.  
  4741.   if (obj_symbols (abfd))
  4742.     return TRUE;
  4743.  
  4744.   /* Read in the symbol table.  */
  4745.   if ((native_symbols = coff_get_normalized_symtab (abfd)) == NULL)
  4746.     return FALSE;
  4747.  
  4748.   /* Allocate enough room for all the symbols in cached form.  */
  4749.   amt = obj_raw_syment_count (abfd);
  4750.   amt *= sizeof (coff_symbol_type);
  4751.   cached_area = (coff_symbol_type *) bfd_alloc (abfd, amt);
  4752.   if (cached_area == NULL)
  4753.     return FALSE;
  4754.  
  4755.   amt = obj_raw_syment_count (abfd);
  4756.   amt *= sizeof (unsigned int);
  4757.   table_ptr = (unsigned int *) bfd_zalloc (abfd, amt);
  4758.  
  4759.   if (table_ptr == NULL)
  4760.     return FALSE;
  4761.   else
  4762.     {
  4763.       coff_symbol_type *dst = cached_area;
  4764.       unsigned int last_native_index = obj_raw_syment_count (abfd);
  4765.       unsigned int this_index = 0;
  4766.  
  4767.       while (this_index < last_native_index)
  4768.         {
  4769.           combined_entry_type *src = native_symbols + this_index;
  4770.           table_ptr[this_index] = number_of_symbols;
  4771.  
  4772.           dst->symbol.the_bfd = abfd;
  4773.           BFD_ASSERT (src->is_sym);
  4774.           dst->symbol.name = (char *) (src->u.syment._n._n_n._n_offset);
  4775.           /* We use the native name field to point to the cached field.  */
  4776.           src->u.syment._n._n_n._n_zeroes = (bfd_hostptr_t) dst;
  4777.           dst->symbol.section = coff_section_from_bfd_index (abfd,
  4778.                                                      src->u.syment.n_scnum);
  4779.           dst->symbol.flags = 0;
  4780.           /* PR 17512: file: 079-7098-0.001:0.1.  */
  4781.           dst->symbol.value = 0;
  4782.           dst->done_lineno = FALSE;
  4783.  
  4784.           switch (src->u.syment.n_sclass)
  4785.             {
  4786. #ifdef I960
  4787.             case C_LEAFEXT:
  4788.               /* Fall through to next case.  */
  4789. #endif
  4790.  
  4791.             case C_EXT:
  4792.             case C_WEAKEXT:
  4793. #if defined ARM
  4794.             case C_THUMBEXT:
  4795.             case C_THUMBEXTFUNC:
  4796. #endif
  4797. #ifdef RS6000COFF_C
  4798.             case C_HIDEXT:
  4799. #endif
  4800. #ifdef C_SYSTEM
  4801.             case C_SYSTEM:      /* System Wide variable.  */
  4802. #endif
  4803. #ifdef COFF_WITH_PE
  4804.             /* In PE, 0x68 (104) denotes a section symbol.  */
  4805.             case C_SECTION:
  4806.             /* In PE, 0x69 (105) denotes a weak external symbol.  */
  4807.             case C_NT_WEAK:
  4808. #endif
  4809.               switch (coff_classify_symbol (abfd, &src->u.syment))
  4810.                 {
  4811.                 case COFF_SYMBOL_GLOBAL:
  4812.                   dst->symbol.flags = BSF_EXPORT | BSF_GLOBAL;
  4813. #if defined COFF_WITH_PE
  4814.                   /* PE sets the symbol to a value relative to the
  4815.                      start of the section.  */
  4816.                   dst->symbol.value = src->u.syment.n_value;
  4817. #else
  4818.                   dst->symbol.value = (src->u.syment.n_value
  4819.                                        - dst->symbol.section->vma);
  4820. #endif
  4821.                   if (ISFCN ((src->u.syment.n_type)))
  4822.                     /* A function ext does not go at the end of a
  4823.                        file.  */
  4824.                     dst->symbol.flags |= BSF_NOT_AT_END | BSF_FUNCTION;
  4825.                   break;
  4826.  
  4827.                 case COFF_SYMBOL_COMMON:
  4828.                   dst->symbol.section = bfd_com_section_ptr;
  4829.                   dst->symbol.value = src->u.syment.n_value;
  4830.                   break;
  4831.  
  4832.                 case COFF_SYMBOL_UNDEFINED:
  4833.                   dst->symbol.section = bfd_und_section_ptr;
  4834.                   dst->symbol.value = 0;
  4835.                   break;
  4836.  
  4837.                 case COFF_SYMBOL_PE_SECTION:
  4838.                   dst->symbol.flags |= BSF_EXPORT | BSF_SECTION_SYM;
  4839.                   dst->symbol.value = 0;
  4840.                   break;
  4841.  
  4842.                 case COFF_SYMBOL_LOCAL:
  4843.                   dst->symbol.flags = BSF_LOCAL;
  4844. #if defined COFF_WITH_PE
  4845.                   /* PE sets the symbol to a value relative to the
  4846.                      start of the section.  */
  4847.                   dst->symbol.value = src->u.syment.n_value;
  4848. #else
  4849.                   dst->symbol.value = (src->u.syment.n_value
  4850.                                        - dst->symbol.section->vma);
  4851. #endif
  4852.                   if (ISFCN ((src->u.syment.n_type)))
  4853.                     dst->symbol.flags |= BSF_NOT_AT_END | BSF_FUNCTION;
  4854.                   break;
  4855.                 }
  4856.  
  4857. #ifdef RS6000COFF_C
  4858.               /* A symbol with a csect entry should not go at the end.  */
  4859.               if (src->u.syment.n_numaux > 0)
  4860.                 dst->symbol.flags |= BSF_NOT_AT_END;
  4861. #endif
  4862.  
  4863. #ifdef COFF_WITH_PE
  4864.               if (src->u.syment.n_sclass == C_NT_WEAK)
  4865.                 dst->symbol.flags |= BSF_WEAK;
  4866.  
  4867.               if (src->u.syment.n_sclass == C_SECTION
  4868.                   && src->u.syment.n_scnum > 0)
  4869.                 dst->symbol.flags = BSF_LOCAL;
  4870. #endif
  4871.               if (src->u.syment.n_sclass == C_WEAKEXT)
  4872.                 dst->symbol.flags |= BSF_WEAK;
  4873.  
  4874.               break;
  4875.  
  4876.             case C_STAT:         /* Static.  */
  4877. #ifdef I960
  4878.             case C_LEAFSTAT:     /* Static leaf procedure.  */
  4879. #endif
  4880. #if defined ARM
  4881.             case C_THUMBSTAT:    /* Thumb static.  */
  4882.             case C_THUMBLABEL:   /* Thumb label.  */
  4883.             case C_THUMBSTATFUNC:/* Thumb static function.  */
  4884. #endif
  4885. #ifdef RS6000COFF_C
  4886.             case C_DWARF:        /* A label in a dwarf section.  */
  4887.             case C_INFO:         /* A label in a comment section.  */
  4888. #endif
  4889.             case C_LABEL:        /* Label.  */
  4890.               if (src->u.syment.n_scnum == N_DEBUG)
  4891.                 dst->symbol.flags = BSF_DEBUGGING;
  4892.               else
  4893.                 dst->symbol.flags = BSF_LOCAL;
  4894.  
  4895.               /* Base the value as an index from the base of the
  4896.                  section, if there is one.  */
  4897.               if (dst->symbol.section)
  4898.                 {
  4899. #if defined COFF_WITH_PE
  4900.                   /* PE sets the symbol to a value relative to the
  4901.                      start of the section.  */
  4902.                   dst->symbol.value = src->u.syment.n_value;
  4903. #else
  4904.                   dst->symbol.value = (src->u.syment.n_value
  4905.                                        - dst->symbol.section->vma);
  4906. #endif
  4907.                 }
  4908.               else
  4909.                 dst->symbol.value = src->u.syment.n_value;
  4910.               break;
  4911.  
  4912.             case C_MOS:         /* Member of structure.  */
  4913.             case C_EOS:         /* End of structure.  */
  4914.             case C_REGPARM:     /* Register parameter.  */
  4915.             case C_REG:         /* register variable.  */
  4916.               /* C_AUTOARG conflicts with TI COFF C_UEXT.  */
  4917. #if !defined (TIC80COFF) && !defined (TICOFF)
  4918. #ifdef C_AUTOARG
  4919.             case C_AUTOARG:     /* 960-specific storage class.  */
  4920. #endif
  4921. #endif
  4922.             case C_TPDEF:       /* Type definition.  */
  4923.             case C_ARG:
  4924.             case C_AUTO:        /* Automatic variable.  */
  4925.             case C_FIELD:       /* Bit field.  */
  4926.             case C_ENTAG:       /* Enumeration tag.  */
  4927.             case C_MOE:         /* Member of enumeration.  */
  4928.             case C_MOU:         /* Member of union.  */
  4929.             case C_UNTAG:       /* Union tag.  */
  4930.               dst->symbol.flags = BSF_DEBUGGING;
  4931.               dst->symbol.value = (src->u.syment.n_value);
  4932.               break;
  4933.  
  4934.             case C_FILE:        /* File name.  */
  4935.             case C_STRTAG:      /* Structure tag.  */
  4936. #ifdef RS6000COFF_C
  4937.             case C_GSYM:
  4938.             case C_LSYM:
  4939.             case C_PSYM:
  4940.             case C_RSYM:
  4941.             case C_RPSYM:
  4942.             case C_STSYM:
  4943.             case C_TCSYM:
  4944.             case C_BCOMM:
  4945.             case C_ECOML:
  4946.             case C_ECOMM:
  4947.             case C_DECL:
  4948.             case C_ENTRY:
  4949.             case C_FUN:
  4950.             case C_ESTAT:
  4951. #endif
  4952.               dst->symbol.flags = BSF_DEBUGGING;
  4953.               dst->symbol.value = (src->u.syment.n_value);
  4954.               break;
  4955.  
  4956. #ifdef RS6000COFF_C
  4957.             case C_BINCL:       /* Beginning of include file.  */
  4958.             case C_EINCL:       /* Ending of include file.  */
  4959.               /* The value is actually a pointer into the line numbers
  4960.                  of the file.  We locate the line number entry, and
  4961.                  set the section to the section which contains it, and
  4962.                  the value to the index in that section.  */
  4963.               {
  4964.                 asection *sec;
  4965.  
  4966.                 dst->symbol.flags = BSF_DEBUGGING;
  4967.                 for (sec = abfd->sections; sec != NULL; sec = sec->next)
  4968.                   if (sec->line_filepos <= (file_ptr) src->u.syment.n_value
  4969.                       && ((file_ptr) (sec->line_filepos
  4970.                                       + sec->lineno_count * bfd_coff_linesz (abfd))
  4971.                           > (file_ptr) src->u.syment.n_value))
  4972.                     break;
  4973.                 if (sec == NULL)
  4974.                   dst->symbol.value = 0;
  4975.                 else
  4976.                   {
  4977.                     dst->symbol.section = sec;
  4978.                     dst->symbol.value = ((src->u.syment.n_value
  4979.                                           - sec->line_filepos)
  4980.                                          / bfd_coff_linesz (abfd));
  4981.                     src->fix_line = 1;
  4982.                   }
  4983.               }
  4984.               break;
  4985.  
  4986.             case C_BSTAT:
  4987.               dst->symbol.flags = BSF_DEBUGGING;
  4988.  
  4989.               /* The value is actually a symbol index.  Save a pointer
  4990.                  to the symbol instead of the index.  FIXME: This
  4991.                  should use a union.  */
  4992.               src->u.syment.n_value =
  4993.                 (long) (intptr_t) (native_symbols + src->u.syment.n_value);
  4994.               dst->symbol.value = src->u.syment.n_value;
  4995.               src->fix_value = 1;
  4996.               break;
  4997. #endif
  4998.  
  4999.             case C_BLOCK:       /* ".bb" or ".eb".  */
  5000.             case C_FCN:         /* ".bf" or ".ef" (or PE ".lf").  */
  5001.             case C_EFCN:        /* Physical end of function.  */
  5002. #if defined COFF_WITH_PE
  5003.               /* PE sets the symbol to a value relative to the start
  5004.                  of the section.  */
  5005.               dst->symbol.value = src->u.syment.n_value;
  5006.               if (strcmp (dst->symbol.name, ".bf") != 0)
  5007.                 {
  5008.                   /* PE uses funny values for .ef and .lf; don't
  5009.                      relocate them.  */
  5010.                   dst->symbol.flags = BSF_DEBUGGING;
  5011.                 }
  5012.               else
  5013.                 dst->symbol.flags = BSF_DEBUGGING | BSF_DEBUGGING_RELOC;
  5014. #else
  5015.               /* Base the value as an index from the base of the
  5016.                  section.  */
  5017.               dst->symbol.flags = BSF_LOCAL;
  5018.               dst->symbol.value = (src->u.syment.n_value
  5019.                                    - dst->symbol.section->vma);
  5020. #endif
  5021.               break;
  5022.  
  5023.             case C_STATLAB:     /* Static load time label.  */
  5024.               dst->symbol.value = src->u.syment.n_value;
  5025.               dst->symbol.flags = BSF_GLOBAL;
  5026.               break;
  5027.  
  5028.             case C_NULL:
  5029.               /* PE DLLs sometimes have zeroed out symbols for some
  5030.                  reason.  Just ignore them without a warning.  */
  5031.               if (src->u.syment.n_type == 0
  5032.                   && src->u.syment.n_value == 0
  5033.                   && src->u.syment.n_scnum == 0)
  5034.                 break;
  5035. #ifdef RS6000COFF_C
  5036.               /* XCOFF specific: deleted entry.  */
  5037.               if (src->u.syment.n_value == C_NULL_VALUE)
  5038.                 break;
  5039. #endif
  5040.               /* Fall through.  */
  5041.             case C_EXTDEF:      /* External definition.  */
  5042.             case C_ULABEL:      /* Undefined label.  */
  5043.             case C_USTATIC:     /* Undefined static.  */
  5044. #ifndef COFF_WITH_PE
  5045.             /* C_LINE in regular coff is 0x68.  NT has taken over this storage
  5046.                class to represent a section symbol.  */
  5047.             case C_LINE:        /* line # reformatted as symbol table entry.  */
  5048.               /* NT uses 0x67 for a weak symbol, not C_ALIAS.  */
  5049.             case C_ALIAS:       /* Duplicate tag.  */
  5050. #endif
  5051.               /* New storage classes for TI COFF.  */
  5052. #if defined(TIC80COFF) || defined(TICOFF)
  5053.             case C_UEXT:        /* Tentative external definition.  */
  5054. #endif
  5055.             default:
  5056.               (*_bfd_error_handler)
  5057.                 (_("%B: Unrecognized storage class %d for %s symbol `%s'"),
  5058.                  abfd, src->u.syment.n_sclass,
  5059.                  dst->symbol.section->name, dst->symbol.name);
  5060.               ret = FALSE;
  5061.             case C_EXTLAB:      /* External load time label.  */
  5062.             case C_HIDDEN:      /* Ext symbol in dmert public lib.  */
  5063.               dst->symbol.flags = BSF_DEBUGGING;
  5064.               dst->symbol.value = (src->u.syment.n_value);
  5065.               break;
  5066.             }
  5067.  
  5068.           dst->native = src;
  5069.           dst->symbol.udata.i = 0;
  5070.           dst->lineno = NULL;
  5071.  
  5072.           this_index += (src->u.syment.n_numaux) + 1;
  5073.           dst++;
  5074.           number_of_symbols++;
  5075.         }
  5076.     }
  5077.  
  5078.   obj_symbols (abfd) = cached_area;
  5079.   obj_raw_syments (abfd) = native_symbols;
  5080.  
  5081.   bfd_get_symcount (abfd) = number_of_symbols;
  5082.   obj_convert (abfd) = table_ptr;
  5083.   /* Slurp the line tables for each section too.  */
  5084.   {
  5085.     asection *p;
  5086.  
  5087.     p = abfd->sections;
  5088.     while (p)
  5089.       {
  5090.         if (! coff_slurp_line_table (abfd, p))
  5091.           return FALSE;
  5092.         p = p->next;
  5093.       }
  5094.   }
  5095.  
  5096.   return ret;
  5097. }
  5098.  
  5099. /* Classify a COFF symbol.  A couple of targets have globally visible
  5100.    symbols which are not class C_EXT, and this handles those.  It also
  5101.    recognizes some special PE cases.  */
  5102.  
  5103. static enum coff_symbol_classification
  5104. coff_classify_symbol (bfd *abfd,
  5105.                       struct internal_syment *syment)
  5106. {
  5107.   /* FIXME: This partially duplicates the switch in
  5108.      coff_slurp_symbol_table.  */
  5109.   switch (syment->n_sclass)
  5110.     {
  5111.     case C_EXT:
  5112.     case C_WEAKEXT:
  5113. #ifdef I960
  5114.     case C_LEAFEXT:
  5115. #endif
  5116. #ifdef ARM
  5117.     case C_THUMBEXT:
  5118.     case C_THUMBEXTFUNC:
  5119. #endif
  5120. #ifdef C_SYSTEM
  5121.     case C_SYSTEM:
  5122. #endif
  5123. #ifdef COFF_WITH_PE
  5124.     case C_NT_WEAK:
  5125. #endif
  5126.       if (syment->n_scnum == 0)
  5127.         {
  5128.           if (syment->n_value == 0)
  5129.             return COFF_SYMBOL_UNDEFINED;
  5130.           else
  5131.             return COFF_SYMBOL_COMMON;
  5132.         }
  5133.       return COFF_SYMBOL_GLOBAL;
  5134.  
  5135.     default:
  5136.       break;
  5137.     }
  5138.  
  5139. #ifdef COFF_WITH_PE
  5140.   if (syment->n_sclass == C_STAT)
  5141.     {
  5142.       if (syment->n_scnum == 0)
  5143.         /* The Microsoft compiler sometimes generates these if a
  5144.            small static function is inlined every time it is used.
  5145.            The function is discarded, but the symbol table entry
  5146.            remains.  */
  5147.         return COFF_SYMBOL_LOCAL;
  5148.  
  5149. #ifdef STRICT_PE_FORMAT
  5150.       /* This is correct for Microsoft generated objects, but it
  5151.          breaks gas generated objects.  */
  5152.       if (syment->n_value == 0)
  5153.         {
  5154.           asection *sec;
  5155.           char * name;
  5156.           char buf[SYMNMLEN + 1];
  5157.  
  5158.           name = _bfd_coff_internal_syment_name (abfd, syment, buf)
  5159.           sec = coff_section_from_bfd_index (abfd, syment->n_scnum);
  5160.           if (sec != NULL && name != NULL
  5161.               && (strcmp (bfd_get_section_name (abfd, sec), name) == 0))
  5162.             return COFF_SYMBOL_PE_SECTION;
  5163.         }
  5164. #endif
  5165.  
  5166.       return COFF_SYMBOL_LOCAL;
  5167.     }
  5168.  
  5169.   if (syment->n_sclass == C_SECTION)
  5170.     {
  5171.       /* In some cases in a DLL generated by the Microsoft linker, the
  5172.          n_value field will contain garbage.  FIXME: This should
  5173.          probably be handled by the swapping function instead.  */
  5174.       syment->n_value = 0;
  5175.       if (syment->n_scnum == 0)
  5176.         return COFF_SYMBOL_UNDEFINED;
  5177.       return COFF_SYMBOL_PE_SECTION;
  5178.     }
  5179. #endif /* COFF_WITH_PE */
  5180.  
  5181.   /* If it is not a global symbol, we presume it is a local symbol.  */
  5182.   if (syment->n_scnum == 0)
  5183.     {
  5184.       char buf[SYMNMLEN + 1];
  5185.  
  5186.       (*_bfd_error_handler)
  5187.         (_("warning: %B: local symbol `%s' has no section"),
  5188.          abfd, _bfd_coff_internal_syment_name (abfd, syment, buf));
  5189.     }
  5190.  
  5191.   return COFF_SYMBOL_LOCAL;
  5192. }
  5193.  
  5194. /*
  5195. SUBSUBSECTION
  5196.         Reading relocations
  5197.  
  5198.         Coff relocations are easily transformed into the internal BFD form
  5199.         (@code{arelent}).
  5200.  
  5201.         Reading a coff relocation table is done in the following stages:
  5202.  
  5203.         o Read the entire coff relocation table into memory.
  5204.  
  5205.         o Process each relocation in turn; first swap it from the
  5206.         external to the internal form.
  5207.  
  5208.         o Turn the symbol referenced in the relocation's symbol index
  5209.         into a pointer into the canonical symbol table.
  5210.         This table is the same as the one returned by a call to
  5211.         @code{bfd_canonicalize_symtab}. The back end will call that
  5212.         routine and save the result if a canonicalization hasn't been done.
  5213.  
  5214.         o The reloc index is turned into a pointer to a howto
  5215.         structure, in a back end specific way. For instance, the 386
  5216.         and 960 use the @code{r_type} to directly produce an index
  5217.         into a howto table vector; the 88k subtracts a number from the
  5218.         @code{r_type} field and creates an addend field.
  5219. */
  5220.  
  5221. #ifndef CALC_ADDEND
  5222. #define CALC_ADDEND(abfd, ptr, reloc, cache_ptr)                \
  5223.   {                                                             \
  5224.     coff_symbol_type *coffsym = NULL;                           \
  5225.                                                                 \
  5226.     if (ptr && bfd_asymbol_bfd (ptr) != abfd)                   \
  5227.       coffsym = (obj_symbols (abfd)                             \
  5228.                  + (cache_ptr->sym_ptr_ptr - symbols));         \
  5229.     else if (ptr)                                               \
  5230.       coffsym = coff_symbol_from (ptr);                         \
  5231.     if (coffsym != NULL                                         \
  5232.         && coffsym->native->is_sym                              \
  5233.         && coffsym->native->u.syment.n_scnum == 0)              \
  5234.       cache_ptr->addend = 0;                                    \
  5235.     else if (ptr && bfd_asymbol_bfd (ptr) == abfd               \
  5236.              && ptr->section != NULL)                           \
  5237.       cache_ptr->addend = - (ptr->section->vma + ptr->value);   \
  5238.     else                                                        \
  5239.       cache_ptr->addend = 0;                                    \
  5240.   }
  5241. #endif
  5242.  
  5243. static bfd_boolean
  5244. coff_slurp_reloc_table (bfd * abfd, sec_ptr asect, asymbol ** symbols)
  5245. {
  5246.   RELOC *native_relocs;
  5247.   arelent *reloc_cache;
  5248.   arelent *cache_ptr;
  5249.   unsigned int idx;
  5250.   bfd_size_type amt;
  5251.  
  5252.   if (asect->relocation)
  5253.     return TRUE;
  5254.   if (asect->reloc_count == 0)
  5255.     return TRUE;
  5256.   if (asect->flags & SEC_CONSTRUCTOR)
  5257.     return TRUE;
  5258.   if (!coff_slurp_symbol_table (abfd))
  5259.     return FALSE;
  5260.  
  5261.   amt = (bfd_size_type) bfd_coff_relsz (abfd) * asect->reloc_count;
  5262.   native_relocs = (RELOC *) buy_and_read (abfd, asect->rel_filepos, amt);
  5263.   amt = (bfd_size_type) asect->reloc_count * sizeof (arelent);
  5264.   reloc_cache = (arelent *) bfd_alloc (abfd, amt);
  5265.  
  5266.   if (reloc_cache == NULL || native_relocs == NULL)
  5267.     return FALSE;
  5268.  
  5269.   for (idx = 0; idx < asect->reloc_count; idx++)
  5270.     {
  5271.       struct internal_reloc dst;
  5272.       struct external_reloc *src;
  5273. #ifndef RELOC_PROCESSING
  5274.       asymbol *ptr;
  5275. #endif
  5276.  
  5277.       cache_ptr = reloc_cache + idx;
  5278.       src = native_relocs + idx;
  5279.  
  5280.       dst.r_offset = 0;
  5281.       coff_swap_reloc_in (abfd, src, &dst);
  5282.  
  5283. #ifdef RELOC_PROCESSING
  5284.       RELOC_PROCESSING (cache_ptr, &dst, symbols, abfd, asect);
  5285. #else
  5286.       cache_ptr->address = dst.r_vaddr;
  5287.  
  5288.       if (dst.r_symndx != -1)
  5289.         {
  5290.           if (dst.r_symndx < 0 || dst.r_symndx >= obj_conv_table_size (abfd))
  5291.             {
  5292.               (*_bfd_error_handler)
  5293.                 (_("%B: warning: illegal symbol index %ld in relocs"),
  5294.                  abfd, (long) dst.r_symndx);
  5295.               cache_ptr->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
  5296.               ptr = NULL;
  5297.             }
  5298.           else
  5299.             {
  5300.               cache_ptr->sym_ptr_ptr = (symbols
  5301.                                         + obj_convert (abfd)[dst.r_symndx]);
  5302.               ptr = *(cache_ptr->sym_ptr_ptr);
  5303.             }
  5304.         }
  5305.       else
  5306.         {
  5307.           cache_ptr->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
  5308.           ptr = NULL;
  5309.         }
  5310.  
  5311.       /* The symbols definitions that we have read in have been
  5312.          relocated as if their sections started at 0. But the offsets
  5313.          refering to the symbols in the raw data have not been
  5314.          modified, so we have to have a negative addend to compensate.
  5315.  
  5316.          Note that symbols which used to be common must be left alone.  */
  5317.  
  5318.       /* Calculate any reloc addend by looking at the symbol.  */
  5319.       CALC_ADDEND (abfd, ptr, dst, cache_ptr);
  5320.       (void) ptr;
  5321.  
  5322.       cache_ptr->address -= asect->vma;
  5323.       /* !! cache_ptr->section = NULL;*/
  5324.  
  5325.       /* Fill in the cache_ptr->howto field from dst.r_type.  */
  5326.       RTYPE2HOWTO (cache_ptr, &dst);
  5327. #endif  /* RELOC_PROCESSING */
  5328.  
  5329.       if (cache_ptr->howto == NULL)
  5330.         {
  5331.           (*_bfd_error_handler)
  5332.             (_("%B: illegal relocation type %d at address 0x%lx"),
  5333.              abfd, dst.r_type, (long) dst.r_vaddr);
  5334.           bfd_set_error (bfd_error_bad_value);
  5335.           return FALSE;
  5336.         }
  5337.     }
  5338.  
  5339.   asect->relocation = reloc_cache;
  5340.   return TRUE;
  5341. }
  5342.  
  5343. #ifndef coff_rtype_to_howto
  5344. #ifdef RTYPE2HOWTO
  5345.  
  5346. /* Get the howto structure for a reloc.  This is only used if the file
  5347.    including this one defines coff_relocate_section to be
  5348.    _bfd_coff_generic_relocate_section, so it is OK if it does not
  5349.    always work.  It is the responsibility of the including file to
  5350.    make sure it is reasonable if it is needed.  */
  5351.  
  5352. static reloc_howto_type *
  5353. coff_rtype_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
  5354.                      asection *sec ATTRIBUTE_UNUSED,
  5355.                      struct internal_reloc *rel ATTRIBUTE_UNUSED,
  5356.                      struct coff_link_hash_entry *h ATTRIBUTE_UNUSED,
  5357.                      struct internal_syment *sym ATTRIBUTE_UNUSED,
  5358.                      bfd_vma *addendp ATTRIBUTE_UNUSED)
  5359. {
  5360.   arelent genrel;
  5361.  
  5362.   genrel.howto = NULL;
  5363.   RTYPE2HOWTO (&genrel, rel);
  5364.   return genrel.howto;
  5365. }
  5366.  
  5367. #else /* ! defined (RTYPE2HOWTO) */
  5368.  
  5369. #define coff_rtype_to_howto NULL
  5370.  
  5371. #endif /* ! defined (RTYPE2HOWTO) */
  5372. #endif /* ! defined (coff_rtype_to_howto) */
  5373.  
  5374. /* This is stupid.  This function should be a boolean predicate.  */
  5375.  
  5376. static long
  5377. coff_canonicalize_reloc (bfd * abfd,
  5378.                          sec_ptr section,
  5379.                          arelent ** relptr,
  5380.                          asymbol ** symbols)
  5381. {
  5382.   arelent *tblptr = section->relocation;
  5383.   unsigned int count = 0;
  5384.  
  5385.   if (section->flags & SEC_CONSTRUCTOR)
  5386.     {
  5387.       /* This section has relocs made up by us, they are not in the
  5388.          file, so take them out of their chain and place them into
  5389.          the data area provided.  */
  5390.       arelent_chain *chain = section->constructor_chain;
  5391.  
  5392.       for (count = 0; count < section->reloc_count; count++)
  5393.         {
  5394.           *relptr++ = &chain->relent;
  5395.           chain = chain->next;
  5396.         }
  5397.     }
  5398.   else
  5399.     {
  5400.       if (! coff_slurp_reloc_table (abfd, section, symbols))
  5401.         return -1;
  5402.  
  5403.       tblptr = section->relocation;
  5404.  
  5405.       for (; count++ < section->reloc_count;)
  5406.         *relptr++ = tblptr++;
  5407.     }
  5408.   *relptr = 0;
  5409.   return section->reloc_count;
  5410. }
  5411.  
  5412. #ifndef coff_reloc16_estimate
  5413. #define coff_reloc16_estimate dummy_reloc16_estimate
  5414.  
  5415. static int
  5416. dummy_reloc16_estimate (bfd *abfd ATTRIBUTE_UNUSED,
  5417.                         asection *input_section ATTRIBUTE_UNUSED,
  5418.                         arelent *reloc ATTRIBUTE_UNUSED,
  5419.                         unsigned int shrink ATTRIBUTE_UNUSED,
  5420.                         struct bfd_link_info *link_info ATTRIBUTE_UNUSED)
  5421. {
  5422.   abort ();
  5423.   return 0;
  5424. }
  5425.  
  5426. #endif
  5427.  
  5428. #ifndef coff_reloc16_extra_cases
  5429.  
  5430. #define coff_reloc16_extra_cases dummy_reloc16_extra_cases
  5431.  
  5432. /* This works even if abort is not declared in any header file.  */
  5433.  
  5434. static void
  5435. dummy_reloc16_extra_cases (bfd *abfd ATTRIBUTE_UNUSED,
  5436.                            struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
  5437.                            struct bfd_link_order *link_order ATTRIBUTE_UNUSED,
  5438.                            arelent *reloc ATTRIBUTE_UNUSED,
  5439.                            bfd_byte *data ATTRIBUTE_UNUSED,
  5440.                            unsigned int *src_ptr ATTRIBUTE_UNUSED,
  5441.                            unsigned int *dst_ptr ATTRIBUTE_UNUSED)
  5442. {
  5443.   abort ();
  5444. }
  5445. #endif
  5446.  
  5447. /* If coff_relocate_section is defined, we can use the optimized COFF
  5448.    backend linker.  Otherwise we must continue to use the old linker.  */
  5449.  
  5450. #ifdef coff_relocate_section
  5451.  
  5452. #ifndef coff_bfd_link_hash_table_create
  5453. #define coff_bfd_link_hash_table_create _bfd_coff_link_hash_table_create
  5454. #endif
  5455. #ifndef coff_bfd_link_add_symbols
  5456. #define coff_bfd_link_add_symbols _bfd_coff_link_add_symbols
  5457. #endif
  5458. #ifndef coff_bfd_final_link
  5459. #define coff_bfd_final_link _bfd_coff_final_link
  5460. #endif
  5461.  
  5462. #else /* ! defined (coff_relocate_section) */
  5463.  
  5464. #define coff_relocate_section NULL
  5465. #ifndef coff_bfd_link_hash_table_create
  5466. #define coff_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
  5467. #endif
  5468. #ifndef coff_bfd_link_add_symbols
  5469. #define coff_bfd_link_add_symbols _bfd_generic_link_add_symbols
  5470. #endif
  5471. #define coff_bfd_final_link _bfd_generic_final_link
  5472.  
  5473. #endif /* ! defined (coff_relocate_section) */
  5474.  
  5475. #define coff_bfd_link_just_syms      _bfd_generic_link_just_syms
  5476. #define coff_bfd_copy_link_hash_symbol_type \
  5477.   _bfd_generic_copy_link_hash_symbol_type
  5478. #define coff_bfd_link_split_section  _bfd_generic_link_split_section
  5479.  
  5480. #ifndef coff_start_final_link
  5481. #define coff_start_final_link NULL
  5482. #endif
  5483.  
  5484. #ifndef coff_adjust_symndx
  5485. #define coff_adjust_symndx NULL
  5486. #endif
  5487.  
  5488. #ifndef coff_link_add_one_symbol
  5489. #define coff_link_add_one_symbol _bfd_generic_link_add_one_symbol
  5490. #endif
  5491.  
  5492. #ifndef coff_link_output_has_begun
  5493.  
  5494. static bfd_boolean
  5495. coff_link_output_has_begun (bfd * abfd,
  5496.                             struct coff_final_link_info * info ATTRIBUTE_UNUSED)
  5497. {
  5498.   return abfd->output_has_begun;
  5499. }
  5500. #endif
  5501.  
  5502. #ifndef coff_final_link_postscript
  5503.  
  5504. static bfd_boolean
  5505. coff_final_link_postscript (bfd * abfd ATTRIBUTE_UNUSED,
  5506.                             struct coff_final_link_info * pfinfo ATTRIBUTE_UNUSED)
  5507. {
  5508.   return TRUE;
  5509. }
  5510. #endif
  5511.  
  5512. #ifndef coff_SWAP_aux_in
  5513. #define coff_SWAP_aux_in coff_swap_aux_in
  5514. #endif
  5515. #ifndef coff_SWAP_sym_in
  5516. #define coff_SWAP_sym_in coff_swap_sym_in
  5517. #endif
  5518. #ifndef coff_SWAP_lineno_in
  5519. #define coff_SWAP_lineno_in coff_swap_lineno_in
  5520. #endif
  5521. #ifndef coff_SWAP_aux_out
  5522. #define coff_SWAP_aux_out coff_swap_aux_out
  5523. #endif
  5524. #ifndef coff_SWAP_sym_out
  5525. #define coff_SWAP_sym_out coff_swap_sym_out
  5526. #endif
  5527. #ifndef coff_SWAP_lineno_out
  5528. #define coff_SWAP_lineno_out coff_swap_lineno_out
  5529. #endif
  5530. #ifndef coff_SWAP_reloc_out
  5531. #define coff_SWAP_reloc_out coff_swap_reloc_out
  5532. #endif
  5533. #ifndef coff_SWAP_filehdr_out
  5534. #define coff_SWAP_filehdr_out coff_swap_filehdr_out
  5535. #endif
  5536. #ifndef coff_SWAP_aouthdr_out
  5537. #define coff_SWAP_aouthdr_out coff_swap_aouthdr_out
  5538. #endif
  5539. #ifndef coff_SWAP_scnhdr_out
  5540. #define coff_SWAP_scnhdr_out coff_swap_scnhdr_out
  5541. #endif
  5542. #ifndef coff_SWAP_reloc_in
  5543. #define coff_SWAP_reloc_in coff_swap_reloc_in
  5544. #endif
  5545. #ifndef coff_SWAP_filehdr_in
  5546. #define coff_SWAP_filehdr_in coff_swap_filehdr_in
  5547. #endif
  5548. #ifndef coff_SWAP_aouthdr_in
  5549. #define coff_SWAP_aouthdr_in coff_swap_aouthdr_in
  5550. #endif
  5551. #ifndef coff_SWAP_scnhdr_in
  5552. #define coff_SWAP_scnhdr_in coff_swap_scnhdr_in
  5553. #endif
  5554.  
  5555. static bfd_coff_backend_data bfd_coff_std_swap_table ATTRIBUTE_UNUSED =
  5556. {
  5557.   coff_SWAP_aux_in, coff_SWAP_sym_in, coff_SWAP_lineno_in,
  5558.   coff_SWAP_aux_out, coff_SWAP_sym_out,
  5559.   coff_SWAP_lineno_out, coff_SWAP_reloc_out,
  5560.   coff_SWAP_filehdr_out, coff_SWAP_aouthdr_out,
  5561.   coff_SWAP_scnhdr_out,
  5562.   FILHSZ, AOUTSZ, SCNHSZ, SYMESZ, AUXESZ, RELSZ, LINESZ, FILNMLEN,
  5563. #ifdef COFF_LONG_FILENAMES
  5564.   TRUE,
  5565. #else
  5566.   FALSE,
  5567. #endif
  5568.   COFF_DEFAULT_LONG_SECTION_NAMES,
  5569.   COFF_DEFAULT_SECTION_ALIGNMENT_POWER,
  5570. #ifdef COFF_FORCE_SYMBOLS_IN_STRINGS
  5571.   TRUE,
  5572. #else
  5573.   FALSE,
  5574. #endif
  5575. #ifdef COFF_DEBUG_STRING_WIDE_PREFIX
  5576.   4,
  5577. #else
  5578.   2,
  5579. #endif
  5580.   32768,
  5581.   coff_SWAP_filehdr_in, coff_SWAP_aouthdr_in, coff_SWAP_scnhdr_in,
  5582.   coff_SWAP_reloc_in, coff_bad_format_hook, coff_set_arch_mach_hook,
  5583.   coff_mkobject_hook, styp_to_sec_flags, coff_set_alignment_hook,
  5584.   coff_slurp_symbol_table, symname_in_debug_hook, coff_pointerize_aux_hook,
  5585.   coff_print_aux, coff_reloc16_extra_cases, coff_reloc16_estimate,
  5586.   coff_classify_symbol, coff_compute_section_file_positions,
  5587.   coff_start_final_link, coff_relocate_section, coff_rtype_to_howto,
  5588.   coff_adjust_symndx, coff_link_add_one_symbol,
  5589.   coff_link_output_has_begun, coff_final_link_postscript,
  5590.   bfd_pe_print_pdata
  5591. };
  5592.  
  5593. #ifdef TICOFF
  5594. /* COFF0 differs in file/section header size and relocation entry size.  */
  5595.  
  5596. static bfd_coff_backend_data ticoff0_swap_table =
  5597. {
  5598.   coff_SWAP_aux_in, coff_SWAP_sym_in, coff_SWAP_lineno_in,
  5599.   coff_SWAP_aux_out, coff_SWAP_sym_out,
  5600.   coff_SWAP_lineno_out, coff_SWAP_reloc_out,
  5601.   coff_SWAP_filehdr_out, coff_SWAP_aouthdr_out,
  5602.   coff_SWAP_scnhdr_out,
  5603.   FILHSZ_V0, AOUTSZ, SCNHSZ_V01, SYMESZ, AUXESZ, RELSZ_V0, LINESZ, FILNMLEN,
  5604. #ifdef COFF_LONG_FILENAMES
  5605.   TRUE,
  5606. #else
  5607.   FALSE,
  5608. #endif
  5609.   COFF_DEFAULT_LONG_SECTION_NAMES,
  5610.   COFF_DEFAULT_SECTION_ALIGNMENT_POWER,
  5611. #ifdef COFF_FORCE_SYMBOLS_IN_STRINGS
  5612.   TRUE,
  5613. #else
  5614.   FALSE,
  5615. #endif
  5616. #ifdef COFF_DEBUG_STRING_WIDE_PREFIX
  5617.   4,
  5618. #else
  5619.   2,
  5620. #endif
  5621.   32768,
  5622.   coff_SWAP_filehdr_in, coff_SWAP_aouthdr_in, coff_SWAP_scnhdr_in,
  5623.   coff_SWAP_reloc_in, ticoff0_bad_format_hook, coff_set_arch_mach_hook,
  5624.   coff_mkobject_hook, styp_to_sec_flags, coff_set_alignment_hook,
  5625.   coff_slurp_symbol_table, symname_in_debug_hook, coff_pointerize_aux_hook,
  5626.   coff_print_aux, coff_reloc16_extra_cases, coff_reloc16_estimate,
  5627.   coff_classify_symbol, coff_compute_section_file_positions,
  5628.   coff_start_final_link, coff_relocate_section, coff_rtype_to_howto,
  5629.   coff_adjust_symndx, coff_link_add_one_symbol,
  5630.   coff_link_output_has_begun, coff_final_link_postscript,
  5631.   bfd_pe_print_pdata
  5632. };
  5633. #endif
  5634.  
  5635. #ifdef TICOFF
  5636. /* COFF1 differs in section header size.  */
  5637.  
  5638. static bfd_coff_backend_data ticoff1_swap_table =
  5639. {
  5640.   coff_SWAP_aux_in, coff_SWAP_sym_in, coff_SWAP_lineno_in,
  5641.   coff_SWAP_aux_out, coff_SWAP_sym_out,
  5642.   coff_SWAP_lineno_out, coff_SWAP_reloc_out,
  5643.   coff_SWAP_filehdr_out, coff_SWAP_aouthdr_out,
  5644.   coff_SWAP_scnhdr_out,
  5645.   FILHSZ, AOUTSZ, SCNHSZ_V01, SYMESZ, AUXESZ, RELSZ, LINESZ, FILNMLEN,
  5646. #ifdef COFF_LONG_FILENAMES
  5647.   TRUE,
  5648. #else
  5649.   FALSE,
  5650. #endif
  5651.   COFF_DEFAULT_LONG_SECTION_NAMES,
  5652.   COFF_DEFAULT_SECTION_ALIGNMENT_POWER,
  5653. #ifdef COFF_FORCE_SYMBOLS_IN_STRINGS
  5654.   TRUE,
  5655. #else
  5656.   FALSE,
  5657. #endif
  5658. #ifdef COFF_DEBUG_STRING_WIDE_PREFIX
  5659.   4,
  5660. #else
  5661.   2,
  5662. #endif
  5663.   32768,
  5664.   coff_SWAP_filehdr_in, coff_SWAP_aouthdr_in, coff_SWAP_scnhdr_in,
  5665.   coff_SWAP_reloc_in, ticoff1_bad_format_hook, coff_set_arch_mach_hook,
  5666.   coff_mkobject_hook, styp_to_sec_flags, coff_set_alignment_hook,
  5667.   coff_slurp_symbol_table, symname_in_debug_hook, coff_pointerize_aux_hook,
  5668.   coff_print_aux, coff_reloc16_extra_cases, coff_reloc16_estimate,
  5669.   coff_classify_symbol, coff_compute_section_file_positions,
  5670.   coff_start_final_link, coff_relocate_section, coff_rtype_to_howto,
  5671.   coff_adjust_symndx, coff_link_add_one_symbol,
  5672.   coff_link_output_has_begun, coff_final_link_postscript,
  5673.   bfd_pe_print_pdata    /* huh */
  5674. };
  5675. #endif
  5676.  
  5677. #ifdef COFF_WITH_PE_BIGOBJ
  5678. /* The UID for bigobj files.  */
  5679.  
  5680. static const char header_bigobj_classid[16] =
  5681. {
  5682.   0xC7, 0xA1, 0xBA, 0xD1,
  5683.   0xEE, 0xBA,
  5684.   0xa9, 0x4b,
  5685.   0xAF, 0x20,
  5686.   0xFA, 0xF6, 0x6A, 0xA4, 0xDC, 0xB8
  5687. };
  5688.  
  5689. /* Swap routines.  */
  5690.  
  5691. static void
  5692. coff_bigobj_swap_filehdr_in (bfd * abfd, void * src, void * dst)
  5693. {
  5694.   struct external_ANON_OBJECT_HEADER_BIGOBJ *filehdr_src =
  5695.     (struct external_ANON_OBJECT_HEADER_BIGOBJ *) src;
  5696.   struct internal_filehdr *filehdr_dst = (struct internal_filehdr *) dst;
  5697.  
  5698.   filehdr_dst->f_magic  = H_GET_16 (abfd, filehdr_src->Machine);
  5699.   filehdr_dst->f_nscns  = H_GET_32 (abfd, filehdr_src->NumberOfSections);
  5700.   filehdr_dst->f_timdat = H_GET_32 (abfd, filehdr_src->TimeDateStamp);
  5701.   filehdr_dst->f_symptr =
  5702.     GET_FILEHDR_SYMPTR (abfd, filehdr_src->PointerToSymbolTable);
  5703.   filehdr_dst->f_nsyms  = H_GET_32 (abfd, filehdr_src->NumberOfSymbols);
  5704.   filehdr_dst->f_opthdr = 0;
  5705.   filehdr_dst->f_flags  = 0;
  5706.  
  5707.   /* Check other magic numbers.  */
  5708.   if (H_GET_16 (abfd, filehdr_src->Sig1) != IMAGE_FILE_MACHINE_UNKNOWN
  5709.       || H_GET_16 (abfd, filehdr_src->Sig2) != 0xffff
  5710.       || H_GET_16 (abfd, filehdr_src->Version) != 2
  5711.       || memcmp (filehdr_src->ClassID, header_bigobj_classid, 16) != 0)
  5712.     filehdr_dst->f_opthdr = 0xffff;
  5713.  
  5714.   /* Note that CLR metadata are ignored.  */
  5715. }
  5716.  
  5717. static unsigned int
  5718. coff_bigobj_swap_filehdr_out (bfd *abfd, void * in, void * out)
  5719. {
  5720.   struct internal_filehdr *filehdr_in = (struct internal_filehdr *) in;
  5721.   struct external_ANON_OBJECT_HEADER_BIGOBJ *filehdr_out =
  5722.     (struct external_ANON_OBJECT_HEADER_BIGOBJ *) out;
  5723.  
  5724.   memset (filehdr_out, 0, sizeof (*filehdr_out));
  5725.  
  5726.   H_PUT_16 (abfd, IMAGE_FILE_MACHINE_UNKNOWN, filehdr_out->Sig1);
  5727.   H_PUT_16 (abfd, 0xffff, filehdr_out->Sig2);
  5728.   H_PUT_16 (abfd, 2, filehdr_out->Version);
  5729.   memcpy (filehdr_out->ClassID, header_bigobj_classid, 16);
  5730.   H_PUT_16 (abfd, filehdr_in->f_magic, filehdr_out->Machine);
  5731.   H_PUT_32 (abfd, filehdr_in->f_nscns, filehdr_out->NumberOfSections);
  5732.   H_PUT_32 (abfd, filehdr_in->f_timdat, filehdr_out->TimeDateStamp);
  5733.   PUT_FILEHDR_SYMPTR (abfd, filehdr_in->f_symptr,
  5734.                       filehdr_out->PointerToSymbolTable);
  5735.   H_PUT_32 (abfd, filehdr_in->f_nsyms, filehdr_out->NumberOfSymbols);
  5736.  
  5737.   return bfd_coff_filhsz (abfd);
  5738. }
  5739.  
  5740. static void
  5741. coff_bigobj_swap_sym_in (bfd * abfd, void * ext1, void * in1)
  5742. {
  5743.   SYMENT_BIGOBJ *ext = (SYMENT_BIGOBJ *) ext1;
  5744.   struct internal_syment *in = (struct internal_syment *) in1;
  5745.  
  5746.   if (ext->e.e_name[0] == 0)
  5747.     {
  5748.       in->_n._n_n._n_zeroes = 0;
  5749.       in->_n._n_n._n_offset = H_GET_32 (abfd, ext->e.e.e_offset);
  5750.     }
  5751.   else
  5752.     {
  5753. #if SYMNMLEN != E_SYMNMLEN
  5754. #error we need to cope with truncating or extending SYMNMLEN
  5755. #else
  5756.       memcpy (in->_n._n_name, ext->e.e_name, SYMNMLEN);
  5757. #endif
  5758.     }
  5759.  
  5760.   in->n_value = H_GET_32 (abfd, ext->e_value);
  5761.   in->n_scnum = H_GET_32 (abfd, ext->e_scnum);
  5762.   in->n_type = H_GET_16 (abfd, ext->e_type);
  5763.   in->n_sclass = H_GET_8 (abfd, ext->e_sclass);
  5764.   in->n_numaux = H_GET_8 (abfd, ext->e_numaux);
  5765. }
  5766.  
  5767. static unsigned int
  5768. coff_bigobj_swap_sym_out (bfd * abfd, void * inp, void * extp)
  5769. {
  5770.   struct internal_syment *in = (struct internal_syment *) inp;
  5771.   SYMENT_BIGOBJ *ext = (SYMENT_BIGOBJ *) extp;
  5772.  
  5773.   if (in->_n._n_name[0] == 0)
  5774.     {
  5775.       H_PUT_32 (abfd, 0, ext->e.e.e_zeroes);
  5776.       H_PUT_32 (abfd, in->_n._n_n._n_offset, ext->e.e.e_offset);
  5777.     }
  5778.   else
  5779.     {
  5780. #if SYMNMLEN != E_SYMNMLEN
  5781. #error we need to cope with truncating or extending SYMNMLEN
  5782. #else
  5783.       memcpy (ext->e.e_name, in->_n._n_name, SYMNMLEN);
  5784. #endif
  5785.     }
  5786.  
  5787.   H_PUT_32 (abfd, in->n_value, ext->e_value);
  5788.   H_PUT_32 (abfd, in->n_scnum, ext->e_scnum);
  5789.  
  5790.   H_PUT_16 (abfd, in->n_type, ext->e_type);
  5791.   H_PUT_8 (abfd, in->n_sclass, ext->e_sclass);
  5792.   H_PUT_8 (abfd, in->n_numaux, ext->e_numaux);
  5793.  
  5794.   return SYMESZ_BIGOBJ;
  5795. }
  5796.  
  5797. static void
  5798. coff_bigobj_swap_aux_in (bfd *abfd,
  5799.                          void * ext1,
  5800.                          int type,
  5801.                          int in_class,
  5802.                          int indx,
  5803.                          int numaux,
  5804.                          void * in1)
  5805. {
  5806.   AUXENT_BIGOBJ *ext = (AUXENT_BIGOBJ *) ext1;
  5807.   union internal_auxent *in = (union internal_auxent *) in1;
  5808.  
  5809.   switch (in_class)
  5810.     {
  5811.     case C_FILE:
  5812.       if (numaux > 1)
  5813.         {
  5814.           if (indx == 0)
  5815.             memcpy (in->x_file.x_fname, ext->File.Name,
  5816.                     numaux * sizeof (AUXENT_BIGOBJ));
  5817.         }
  5818.       else
  5819.         memcpy (in->x_file.x_fname, ext->File.Name, sizeof (ext->File.Name));
  5820.       break;
  5821.  
  5822.     case C_STAT:
  5823.     case C_LEAFSTAT:
  5824.     case C_HIDDEN:
  5825.       if (type == T_NULL)
  5826.         {
  5827.           in->x_scn.x_scnlen = H_GET_32 (abfd, ext->Section.Length);
  5828.           in->x_scn.x_nreloc =
  5829.             H_GET_16 (abfd, ext->Section.NumberOfRelocations);
  5830.           in->x_scn.x_nlinno =
  5831.             H_GET_16 (abfd, ext->Section.NumberOfLinenumbers);
  5832.           in->x_scn.x_checksum = H_GET_32 (abfd, ext->Section.Checksum);
  5833.           in->x_scn.x_associated = H_GET_16 (abfd, ext->Section.Number)
  5834.             | (H_GET_16 (abfd, ext->Section.HighNumber) << 16);
  5835.           in->x_scn.x_comdat = H_GET_8 (abfd, ext->Section.Selection);
  5836.           return;
  5837.         }
  5838.       break;
  5839.  
  5840.     default:
  5841.       in->x_sym.x_tagndx.l = H_GET_32 (abfd, ext->Sym.WeakDefaultSymIndex);
  5842.       /* Characteristics is ignored.  */
  5843.       break;
  5844.     }
  5845. }
  5846.  
  5847. static unsigned int
  5848. coff_bigobj_swap_aux_out (bfd * abfd,
  5849.                           void * inp,
  5850.                           int type,
  5851.                           int in_class,
  5852.                           int indx ATTRIBUTE_UNUSED,
  5853.                           int numaux ATTRIBUTE_UNUSED,
  5854.                           void * extp)
  5855. {
  5856.   union internal_auxent * in = (union internal_auxent *) inp;
  5857.   AUXENT_BIGOBJ *ext = (AUXENT_BIGOBJ *) extp;
  5858.  
  5859.   memset (ext, 0, AUXESZ);
  5860.  
  5861.   switch (in_class)
  5862.     {
  5863.     case C_FILE:
  5864.       memcpy (ext->File.Name, in->x_file.x_fname, sizeof (ext->File.Name));
  5865.  
  5866.       return AUXESZ;
  5867.  
  5868.     case C_STAT:
  5869.     case C_LEAFSTAT:
  5870.     case C_HIDDEN:
  5871.       if (type == T_NULL)
  5872.         {
  5873.           H_PUT_32 (abfd, in->x_scn.x_scnlen, ext->Section.Length);
  5874.           H_PUT_16 (abfd, in->x_scn.x_nreloc,
  5875.                     ext->Section.NumberOfRelocations);
  5876.           H_PUT_16 (abfd, in->x_scn.x_nlinno,
  5877.                     ext->Section.NumberOfLinenumbers);
  5878.           H_PUT_32 (abfd, in->x_scn.x_checksum, ext->Section.Checksum);
  5879.           H_PUT_16 (abfd, in->x_scn.x_associated & 0xffff,
  5880.                     ext->Section.Number);
  5881.           H_PUT_16 (abfd, (in->x_scn.x_associated >> 16),
  5882.                     ext->Section.HighNumber);
  5883.           H_PUT_8 (abfd, in->x_scn.x_comdat, ext->Section.Selection);
  5884.           return AUXESZ;
  5885.         }
  5886.       break;
  5887.     }
  5888.  
  5889.   H_PUT_32 (abfd, in->x_sym.x_tagndx.l, ext->Sym.WeakDefaultSymIndex);
  5890.   H_PUT_32 (abfd, 1, ext->Sym.WeakSearchType);
  5891.  
  5892.   return AUXESZ;
  5893. }
  5894.  
  5895. static bfd_coff_backend_data bigobj_swap_table =
  5896. {
  5897.   coff_bigobj_swap_aux_in, coff_bigobj_swap_sym_in, coff_SWAP_lineno_in,
  5898.   coff_bigobj_swap_aux_out, coff_bigobj_swap_sym_out,
  5899.   coff_SWAP_lineno_out, coff_SWAP_reloc_out,
  5900.   coff_bigobj_swap_filehdr_out, coff_SWAP_aouthdr_out,
  5901.   coff_SWAP_scnhdr_out,
  5902.   FILHSZ_BIGOBJ, AOUTSZ, SCNHSZ, SYMESZ_BIGOBJ, AUXESZ_BIGOBJ,
  5903.    RELSZ, LINESZ, FILNMLEN_BIGOBJ,
  5904.   TRUE,
  5905.   COFF_DEFAULT_LONG_SECTION_NAMES,
  5906.   COFF_DEFAULT_SECTION_ALIGNMENT_POWER,
  5907.   FALSE,
  5908.   2,
  5909.   1U << 31,
  5910.   coff_bigobj_swap_filehdr_in, coff_SWAP_aouthdr_in, coff_SWAP_scnhdr_in,
  5911.   coff_SWAP_reloc_in, coff_bad_format_hook, coff_set_arch_mach_hook,
  5912.   coff_mkobject_hook, styp_to_sec_flags, coff_set_alignment_hook,
  5913.   coff_slurp_symbol_table, symname_in_debug_hook, coff_pointerize_aux_hook,
  5914.   coff_print_aux, coff_reloc16_extra_cases, coff_reloc16_estimate,
  5915.   coff_classify_symbol, coff_compute_section_file_positions,
  5916.   coff_start_final_link, coff_relocate_section, coff_rtype_to_howto,
  5917.   coff_adjust_symndx, coff_link_add_one_symbol,
  5918.   coff_link_output_has_begun, coff_final_link_postscript,
  5919.   bfd_pe_print_pdata    /* huh */
  5920. };
  5921.  
  5922. #endif /* COFF_WITH_PE_BIGOBJ */
  5923.  
  5924. #ifndef coff_close_and_cleanup
  5925. #define coff_close_and_cleanup              _bfd_generic_close_and_cleanup
  5926. #endif
  5927.  
  5928. #ifndef coff_bfd_free_cached_info
  5929. #define coff_bfd_free_cached_info           _bfd_generic_bfd_free_cached_info
  5930. #endif
  5931.  
  5932. #ifndef coff_get_section_contents
  5933. #define coff_get_section_contents           _bfd_generic_get_section_contents
  5934. #endif
  5935.  
  5936. #ifndef coff_bfd_copy_private_symbol_data
  5937. #define coff_bfd_copy_private_symbol_data   _bfd_generic_bfd_copy_private_symbol_data
  5938. #endif
  5939.  
  5940. #ifndef coff_bfd_copy_private_header_data
  5941. #define coff_bfd_copy_private_header_data   _bfd_generic_bfd_copy_private_header_data
  5942. #endif
  5943.  
  5944. #ifndef coff_bfd_copy_private_section_data
  5945. #define coff_bfd_copy_private_section_data  _bfd_generic_bfd_copy_private_section_data
  5946. #endif
  5947.  
  5948. #ifndef coff_bfd_copy_private_bfd_data
  5949. #define coff_bfd_copy_private_bfd_data      _bfd_generic_bfd_copy_private_bfd_data
  5950. #endif
  5951.  
  5952. #ifndef coff_bfd_merge_private_bfd_data
  5953. #define coff_bfd_merge_private_bfd_data     _bfd_generic_bfd_merge_private_bfd_data
  5954. #endif
  5955.  
  5956. #ifndef coff_bfd_set_private_flags
  5957. #define coff_bfd_set_private_flags          _bfd_generic_bfd_set_private_flags
  5958. #endif
  5959.  
  5960. #ifndef coff_bfd_print_private_bfd_data
  5961. #define coff_bfd_print_private_bfd_data     _bfd_generic_bfd_print_private_bfd_data
  5962. #endif
  5963.  
  5964. #ifndef coff_bfd_is_local_label_name
  5965. #define coff_bfd_is_local_label_name        _bfd_coff_is_local_label_name
  5966. #endif
  5967.  
  5968. #ifndef coff_bfd_is_target_special_symbol
  5969. #define coff_bfd_is_target_special_symbol   ((bfd_boolean (*) (bfd *, asymbol *)) bfd_false)
  5970. #endif
  5971.  
  5972. #ifndef coff_read_minisymbols
  5973. #define coff_read_minisymbols               _bfd_generic_read_minisymbols
  5974. #endif
  5975.  
  5976. #ifndef coff_minisymbol_to_symbol
  5977. #define coff_minisymbol_to_symbol           _bfd_generic_minisymbol_to_symbol
  5978. #endif
  5979.  
  5980. /* The reloc lookup routine must be supplied by each individual COFF
  5981.    backend.  */
  5982. #ifndef coff_bfd_reloc_type_lookup
  5983. #define coff_bfd_reloc_type_lookup          _bfd_norelocs_bfd_reloc_type_lookup
  5984. #endif
  5985. #ifndef coff_bfd_reloc_name_lookup
  5986. #define coff_bfd_reloc_name_lookup    _bfd_norelocs_bfd_reloc_name_lookup
  5987. #endif
  5988.  
  5989. #ifndef coff_bfd_get_relocated_section_contents
  5990. #define coff_bfd_get_relocated_section_contents \
  5991.   bfd_generic_get_relocated_section_contents
  5992. #endif
  5993.  
  5994. #ifndef coff_bfd_relax_section
  5995. #define coff_bfd_relax_section              bfd_generic_relax_section
  5996. #endif
  5997.  
  5998. #ifndef coff_bfd_gc_sections
  5999. #define coff_bfd_gc_sections                bfd_coff_gc_sections
  6000. #endif
  6001.  
  6002. #ifndef coff_bfd_lookup_section_flags
  6003. #define coff_bfd_lookup_section_flags       bfd_generic_lookup_section_flags
  6004. #endif
  6005.  
  6006. #ifndef coff_bfd_merge_sections
  6007. #define coff_bfd_merge_sections             bfd_generic_merge_sections
  6008. #endif
  6009.  
  6010. #ifndef coff_bfd_is_group_section
  6011. #define coff_bfd_is_group_section           bfd_generic_is_group_section
  6012. #endif
  6013.  
  6014. #ifndef coff_bfd_discard_group
  6015. #define coff_bfd_discard_group              bfd_generic_discard_group
  6016. #endif
  6017.  
  6018. #ifndef coff_section_already_linked
  6019. #define coff_section_already_linked \
  6020.   _bfd_coff_section_already_linked
  6021. #endif
  6022.  
  6023. #ifndef coff_bfd_define_common_symbol
  6024. #define coff_bfd_define_common_symbol       bfd_generic_define_common_symbol
  6025. #endif
  6026.  
  6027. #define CREATE_BIG_COFF_TARGET_VEC(VAR, NAME, EXTRA_O_FLAGS, EXTRA_S_FLAGS, UNDER, ALTERNATIVE, SWAP_TABLE)     \
  6028. const bfd_target VAR =                                                  \
  6029. {                                                                       \
  6030.   NAME ,                                                                \
  6031.   bfd_target_coff_flavour,                                              \
  6032.   BFD_ENDIAN_BIG,               /* Data byte order is big.  */          \
  6033.   BFD_ENDIAN_BIG,               /* Header byte order is big.  */        \
  6034.   /* object flags */                                                    \
  6035.   (HAS_RELOC | EXEC_P | HAS_LINENO | HAS_DEBUG |                        \
  6036.    HAS_SYMS | HAS_LOCALS | WP_TEXT | EXTRA_O_FLAGS),                    \
  6037.   /* section flags */                                                   \
  6038.   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | EXTRA_S_FLAGS),\
  6039.   UNDER,                        /* Leading symbol underscore.  */       \
  6040.   '/',                          /* AR_pad_char.  */                     \
  6041.   15,                           /* AR_max_namelen.  */                  \
  6042.   0,                            /* match priority.  */                  \
  6043.                                                                         \
  6044.   /* Data conversion functions.  */                                     \
  6045.   bfd_getb64, bfd_getb_signed_64, bfd_putb64,                           \
  6046.   bfd_getb32, bfd_getb_signed_32, bfd_putb32,                           \
  6047.   bfd_getb16, bfd_getb_signed_16, bfd_putb16,                           \
  6048.                                                                         \
  6049.   /* Header conversion functions.  */                                   \
  6050.   bfd_getb64, bfd_getb_signed_64, bfd_putb64,                           \
  6051.   bfd_getb32, bfd_getb_signed_32, bfd_putb32,                           \
  6052.   bfd_getb16, bfd_getb_signed_16, bfd_putb16,                           \
  6053.                                                                         \
  6054.         /* bfd_check_format.  */                                        \
  6055.   { _bfd_dummy_target, coff_object_p, bfd_generic_archive_p,            \
  6056.     _bfd_dummy_target },                                                \
  6057.         /* bfd_set_format.  */                                          \
  6058.   { bfd_false, coff_mkobject, _bfd_generic_mkarchive, bfd_false },      \
  6059.         /* bfd_write_contents.  */                                      \
  6060.   { bfd_false, coff_write_object_contents, _bfd_write_archive_contents, \
  6061.     bfd_false },                                                        \
  6062.                                                                         \
  6063.   BFD_JUMP_TABLE_GENERIC (coff),                                        \
  6064.   BFD_JUMP_TABLE_COPY (coff),                                           \
  6065.   BFD_JUMP_TABLE_CORE (_bfd_nocore),                                    \
  6066.   BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),                           \
  6067.   BFD_JUMP_TABLE_SYMBOLS (coff),                                        \
  6068.   BFD_JUMP_TABLE_RELOCS (coff),                                         \
  6069.   BFD_JUMP_TABLE_WRITE (coff),                                          \
  6070.   BFD_JUMP_TABLE_LINK (coff),                                           \
  6071.   BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),                              \
  6072.                                                                         \
  6073.   ALTERNATIVE,                                                          \
  6074.                                                                         \
  6075.   SWAP_TABLE                                                            \
  6076. };
  6077.  
  6078. #define CREATE_BIGHDR_COFF_TARGET_VEC(VAR, NAME, EXTRA_O_FLAGS, EXTRA_S_FLAGS, UNDER, ALTERNATIVE, SWAP_TABLE)  \
  6079. const bfd_target VAR =                                                  \
  6080. {                                                                       \
  6081.   NAME ,                                                                \
  6082.   bfd_target_coff_flavour,                                              \
  6083.   BFD_ENDIAN_LITTLE,            /* Data byte order is little.  */       \
  6084.   BFD_ENDIAN_BIG,               /* Header byte order is big.  */        \
  6085.   /* object flags */                                                    \
  6086.   (HAS_RELOC | EXEC_P | HAS_LINENO | HAS_DEBUG |                        \
  6087.    HAS_SYMS | HAS_LOCALS | WP_TEXT | EXTRA_O_FLAGS),                    \
  6088.   /* section flags */                                                   \
  6089.   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | EXTRA_S_FLAGS),\
  6090.   UNDER,                        /* Leading symbol underscore.  */       \
  6091.   '/',                          /* AR_pad_char.  */                     \
  6092.   15,                           /* AR_max_namelen.  */                  \
  6093.   0,                            /* match priority.  */                  \
  6094.                                                                         \
  6095.   /* Data conversion functions.  */                                     \
  6096.   bfd_getb64, bfd_getb_signed_64, bfd_putb64,                           \
  6097.   bfd_getb32, bfd_getb_signed_32, bfd_putb32,                           \
  6098.   bfd_getb16, bfd_getb_signed_16, bfd_putb16,                           \
  6099.                                                                         \
  6100.   /* Header conversion functions.  */                                   \
  6101.   bfd_getb64, bfd_getb_signed_64, bfd_putb64,                           \
  6102.   bfd_getb32, bfd_getb_signed_32, bfd_putb32,                           \
  6103.   bfd_getb16, bfd_getb_signed_16, bfd_putb16,                           \
  6104.                                                                         \
  6105.         /* bfd_check_format.  */                                        \
  6106.   { _bfd_dummy_target, coff_object_p, bfd_generic_archive_p,            \
  6107.     _bfd_dummy_target },                                                \
  6108.         /* bfd_set_format.  */                                          \
  6109.   { bfd_false, coff_mkobject, _bfd_generic_mkarchive, bfd_false },      \
  6110.         /* bfd_write_contents.  */                                      \
  6111.   { bfd_false, coff_write_object_contents, _bfd_write_archive_contents, \
  6112.     bfd_false },                                                        \
  6113.                                                                         \
  6114.   BFD_JUMP_TABLE_GENERIC (coff),                                        \
  6115.   BFD_JUMP_TABLE_COPY (coff),                                           \
  6116.   BFD_JUMP_TABLE_CORE (_bfd_nocore),                                    \
  6117.   BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),                           \
  6118.   BFD_JUMP_TABLE_SYMBOLS (coff),                                        \
  6119.   BFD_JUMP_TABLE_RELOCS (coff),                                         \
  6120.   BFD_JUMP_TABLE_WRITE (coff),                                          \
  6121.   BFD_JUMP_TABLE_LINK (coff),                                           \
  6122.   BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),                              \
  6123.                                                                         \
  6124.   ALTERNATIVE,                                                          \
  6125.                                                                         \
  6126.   SWAP_TABLE                                                            \
  6127. };
  6128.  
  6129. #define CREATE_LITTLE_COFF_TARGET_VEC(VAR, NAME, EXTRA_O_FLAGS, EXTRA_S_FLAGS, UNDER, ALTERNATIVE, SWAP_TABLE)  \
  6130. const bfd_target VAR =                                                  \
  6131. {                                                                       \
  6132.   NAME ,                                                                \
  6133.   bfd_target_coff_flavour,                                              \
  6134.   BFD_ENDIAN_LITTLE,            /* Data byte order is little.  */       \
  6135.   BFD_ENDIAN_LITTLE,            /* Header byte order is little.  */     \
  6136.         /* object flags */                                              \
  6137.   (HAS_RELOC | EXEC_P | HAS_LINENO | HAS_DEBUG |                        \
  6138.    HAS_SYMS | HAS_LOCALS | WP_TEXT | EXTRA_O_FLAGS),                    \
  6139.         /* section flags */                                             \
  6140.   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | EXTRA_S_FLAGS),\
  6141.   UNDER,                        /* Leading symbol underscore.  */       \
  6142.   '/',                          /* AR_pad_char.  */                     \
  6143.   15,                           /* AR_max_namelen.  */                  \
  6144.   0,                            /* match priority.  */                  \
  6145.                                                                         \
  6146.   /* Data conversion functions.  */                                     \
  6147.   bfd_getl64, bfd_getl_signed_64, bfd_putl64,                           \
  6148.   bfd_getl32, bfd_getl_signed_32, bfd_putl32,                           \
  6149.   bfd_getl16, bfd_getl_signed_16, bfd_putl16,                           \
  6150.   /* Header conversion functions.  */                                   \
  6151.   bfd_getl64, bfd_getl_signed_64, bfd_putl64,                           \
  6152.   bfd_getl32, bfd_getl_signed_32, bfd_putl32,                           \
  6153.   bfd_getl16, bfd_getl_signed_16, bfd_putl16,                           \
  6154.         /* bfd_check_format.  */                                        \
  6155.   { _bfd_dummy_target, coff_object_p, bfd_generic_archive_p,            \
  6156.     _bfd_dummy_target },                                                \
  6157.        /* bfd_set_format.  */                                           \
  6158.   { bfd_false, coff_mkobject, _bfd_generic_mkarchive, bfd_false },      \
  6159.         /* bfd_write_contents.  */                                      \
  6160.   { bfd_false, coff_write_object_contents, _bfd_write_archive_contents, \
  6161.     bfd_false },                                                        \
  6162.                                                                         \
  6163.   BFD_JUMP_TABLE_GENERIC (coff),                                        \
  6164.   BFD_JUMP_TABLE_COPY (coff),                                           \
  6165.   BFD_JUMP_TABLE_CORE (_bfd_nocore),                                    \
  6166.   BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),                           \
  6167.   BFD_JUMP_TABLE_SYMBOLS (coff),                                        \
  6168.   BFD_JUMP_TABLE_RELOCS (coff),                                         \
  6169.   BFD_JUMP_TABLE_WRITE (coff),                                          \
  6170.   BFD_JUMP_TABLE_LINK (coff),                                           \
  6171.   BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),                              \
  6172.                                                                         \
  6173.   ALTERNATIVE,                                                          \
  6174.                                                                         \
  6175.   SWAP_TABLE                                                            \
  6176. };
  6177.