home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / binutils-2.7-src.tgz / tar.out / fsf / binutils / bfd / coff-rs6000.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  49KB  |  1,404 lines

  1. /* BFD back-end for IBM RS/6000 "XCOFF" files.
  2.    Copyright 1990, 91, 92, 93, 94, 95, 1996 Free Software Foundation, Inc.
  3.    FIXME: Can someone provide a transliteration of this name into ASCII?
  4.    Using the following chars caused a compiler warning on HIUX (so I replaced
  5.    them with octal escapes), and isn't useful without an understanding of what
  6.    character set it is.
  7.    Written by Metin G. Ozisik, Mimi Ph\373\364ng-Th\345o V\365, 
  8.      and John Gilmore.
  9.    Archive support from Damon A. Permezel.
  10.    Contributed by IBM Corporation and Cygnus Support.
  11.  
  12. This file is part of BFD, the Binary File Descriptor library.
  13.  
  14. This program is free software; you can redistribute it and/or modify
  15. it under the terms of the GNU General Public License as published by
  16. the Free Software Foundation; either version 2 of the License, or
  17. (at your option) any later version.
  18.  
  19. This program is distributed in the hope that it will be useful,
  20. but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  22. GNU General Public License for more details.
  23.  
  24. You should have received a copy of the GNU General Public License
  25. along with this program; if not, write to the Free Software
  26. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
  27.  
  28. /* Internalcoff.h and coffcode.h modify themselves based on this flag.  */
  29. #define RS6000COFF_C 1
  30.  
  31. #include "bfd.h"
  32. #include "sysdep.h"
  33. #include "libbfd.h"
  34. #include "obstack.h"
  35. #include "coff/internal.h"
  36. #include "coff/rs6000.h"
  37. #include "libcoff.h"
  38.  
  39. /* The main body of code is in coffcode.h.  */
  40.  
  41. static boolean xcoff_mkobject PARAMS ((bfd *));
  42. static boolean xcoff_copy_private_bfd_data PARAMS ((bfd *, bfd *));
  43. static void xcoff_rtype2howto
  44.   PARAMS ((arelent *, struct internal_reloc *));
  45. static reloc_howto_type *xcoff_reloc_type_lookup
  46.   PARAMS ((bfd *, bfd_reloc_code_real_type));
  47. static boolean xcoff_slurp_armap PARAMS ((bfd *));
  48. static const bfd_target *xcoff_archive_p PARAMS ((bfd *));
  49. static PTR xcoff_read_ar_hdr PARAMS ((bfd *));
  50. static bfd *xcoff_openr_next_archived_file PARAMS ((bfd *, bfd *));
  51. static int xcoff_generic_stat_arch_elt PARAMS ((bfd *, struct stat *));
  52. static const char *normalize_filename PARAMS ((bfd *));
  53. static boolean xcoff_write_armap
  54.   PARAMS ((bfd *, unsigned int, struct orl *, unsigned int, int));
  55. static boolean xcoff_write_archive_contents PARAMS ((bfd *));
  56.  
  57. /* We use our own tdata type.  Its first field is the COFF tdata type,
  58.    so the COFF routines are compatible.  */
  59.  
  60. static boolean
  61. xcoff_mkobject (abfd)
  62.      bfd *abfd;
  63. {
  64.   coff_data_type *coff;
  65.  
  66.   abfd->tdata.xcoff_obj_data =
  67.     ((struct xcoff_tdata *)
  68.      bfd_zalloc (abfd, sizeof (struct xcoff_tdata)));
  69.   if (abfd->tdata.xcoff_obj_data == NULL)
  70.     return false;
  71.   coff = coff_data (abfd);
  72.   coff->symbols = (coff_symbol_type *) NULL;
  73.   coff->conversion_table = (unsigned int *) NULL;
  74.   coff->raw_syments = (struct coff_ptr_struct *) NULL;
  75.   coff->relocbase = 0;
  76.  
  77.   xcoff_data (abfd)->modtype = ('1' << 8) | 'L';
  78.  
  79.   /* We set cputype to -1 to indicate that it has not been
  80.      initialized.  */
  81.   xcoff_data (abfd)->cputype = -1;
  82.  
  83.   xcoff_data (abfd)->csects = NULL;
  84.   xcoff_data (abfd)->debug_indices = NULL;
  85.  
  86.   return true;
  87. }
  88.  
  89. /* Copy XCOFF data from one BFD to another.  */
  90.  
  91. static boolean
  92. xcoff_copy_private_bfd_data (ibfd, obfd)
  93.      bfd *ibfd;
  94.      bfd *obfd;
  95. {
  96.   struct xcoff_tdata *ix, *ox;
  97.   asection *sec;
  98.  
  99.   if (ibfd->xvec != obfd->xvec)
  100.     return true;
  101.   ix = xcoff_data (ibfd);
  102.   ox = xcoff_data (obfd);
  103.   ox->full_aouthdr = ix->full_aouthdr;
  104.   ox->toc = ix->toc;
  105.   if (ix->sntoc == 0)
  106.     ox->sntoc = 0;
  107.   else
  108.     {
  109.       sec = coff_section_from_bfd_index (ibfd, ix->sntoc);
  110.       if (sec == NULL)
  111.     ox->sntoc = 0;
  112.       else
  113.     ox->sntoc = sec->output_section->target_index;
  114.     }
  115.   if (ix->snentry == 0)
  116.     ox->snentry = 0;
  117.   else
  118.     {
  119.       sec = coff_section_from_bfd_index (ibfd, ix->snentry);
  120.       if (sec == NULL)
  121.     ox->snentry = 0;
  122.       else
  123.     ox->snentry = sec->output_section->target_index;
  124.     }
  125.   ox->text_align_power = ix->text_align_power;
  126.   ox->data_align_power = ix->data_align_power;
  127.   ox->modtype = ix->modtype;
  128.   ox->cputype = ix->cputype;
  129.   ox->maxdata = ix->maxdata;
  130.   ox->maxstack = ix->maxstack;
  131.   return true;
  132. }
  133.  
  134. /* The XCOFF reloc table.  Actually, XCOFF relocations specify the
  135.    bitsize and whether they are signed or not, along with a
  136.    conventional type.  This table is for the types, which are used for
  137.    different algorithms for putting in the reloc.  Many of these
  138.    relocs need special_function entries, which I have not written.  */
  139.  
  140. static reloc_howto_type xcoff_howto_table[] =
  141. {
  142.   /* Standard 32 bit relocation.  */
  143.   HOWTO (0,                    /* type */                                 
  144.      0,                    /* rightshift */                           
  145.      2,                    /* size (0 = byte, 1 = short, 2 = long) */ 
  146.      32,                    /* bitsize */                   
  147.      false,                    /* pc_relative */                          
  148.      0,                    /* bitpos */                               
  149.      complain_overflow_bitfield, /* complain_on_overflow */
  150.      0,                /* special_function */                     
  151.      "R_POS",               /* name */                                 
  152.      true,                    /* partial_inplace */                      
  153.      0xffffffff,            /* src_mask */                             
  154.      0xffffffff,            /* dst_mask */                             
  155.      false),                /* pcrel_offset */
  156.  
  157.   /* 32 bit relocation, but store negative value.  */
  158.   HOWTO (1,                    /* type */                                 
  159.      0,                    /* rightshift */                           
  160.      -2,                    /* size (0 = byte, 1 = short, 2 = long) */ 
  161.      32,                    /* bitsize */                   
  162.      false,                    /* pc_relative */                          
  163.      0,                    /* bitpos */                               
  164.      complain_overflow_bitfield, /* complain_on_overflow */
  165.      0,                /* special_function */                     
  166.      "R_NEG",               /* name */                                 
  167.      true,                    /* partial_inplace */                      
  168.      0xffffffff,            /* src_mask */                             
  169.      0xffffffff,            /* dst_mask */                             
  170.      false),                /* pcrel_offset */
  171.  
  172.   /* 32 bit PC relative relocation.  */
  173.   HOWTO (2,                    /* type */                                 
  174.      0,                    /* rightshift */                           
  175.      2,                    /* size (0 = byte, 1 = short, 2 = long) */ 
  176.      32,                    /* bitsize */                   
  177.      true,                    /* pc_relative */                          
  178.      0,                    /* bitpos */                               
  179.      complain_overflow_signed, /* complain_on_overflow */
  180.      0,                /* special_function */                     
  181.      "R_REL",               /* name */                                 
  182.      true,                    /* partial_inplace */                      
  183.      0xffffffff,            /* src_mask */                             
  184.      0xffffffff,            /* dst_mask */                             
  185.      false),                /* pcrel_offset */
  186.   
  187.   /* 16 bit TOC relative relocation.  */
  188.   HOWTO (3,                    /* type */                                 
  189.      0,                    /* rightshift */                           
  190.      1,                    /* size (0 = byte, 1 = short, 2 = long) */ 
  191.      16,                    /* bitsize */                   
  192.      false,                    /* pc_relative */                          
  193.      0,                    /* bitpos */                               
  194.      complain_overflow_bitfield, /* complain_on_overflow */
  195.      0,                /* special_function */                     
  196.      "R_TOC",               /* name */                                 
  197.      true,                    /* partial_inplace */                      
  198.      0xffff,            /* src_mask */                             
  199.      0xffff,            /* dst_mask */                             
  200.      false),                /* pcrel_offset */
  201.   
  202.   /* I don't really know what this is.  */
  203.   HOWTO (4,                    /* type */                                 
  204.      1,                    /* rightshift */                           
  205.      2,                    /* size (0 = byte, 1 = short, 2 = long) */ 
  206.      32,                    /* bitsize */                   
  207.      false,                    /* pc_relative */                          
  208.      0,                    /* bitpos */                               
  209.      complain_overflow_bitfield, /* complain_on_overflow */
  210.      0,                /* special_function */                     
  211.      "R_RTB",               /* name */                                 
  212.      true,                    /* partial_inplace */                      
  213.      0xffffffff,            /* src_mask */                             
  214.      0xffffffff,            /* dst_mask */                             
  215.      false),                /* pcrel_offset */
  216.   
  217.   /* External TOC relative symbol.  */
  218.   HOWTO (5,                    /* type */                                 
  219.      0,                    /* rightshift */                           
  220.      2,                    /* size (0 = byte, 1 = short, 2 = long) */ 
  221.      16,                    /* bitsize */                   
  222.      false,                    /* pc_relative */                          
  223.      0,                    /* bitpos */                               
  224.      complain_overflow_bitfield, /* complain_on_overflow */
  225.      0,                /* special_function */                     
  226.      "R_GL",                /* name */                                 
  227.      true,                    /* partial_inplace */                      
  228.      0xffff,            /* src_mask */                             
  229.      0xffff,            /* dst_mask */                             
  230.      false),                /* pcrel_offset */
  231.   
  232.   /* Local TOC relative symbol.  */
  233.   HOWTO (6,                    /* type */                                 
  234.      0,                    /* rightshift */                           
  235.      2,                    /* size (0 = byte, 1 = short, 2 = long) */ 
  236.      16,                    /* bitsize */                   
  237.      false,                    /* pc_relative */                          
  238.      0,                    /* bitpos */                               
  239.      complain_overflow_bitfield, /* complain_on_overflow */
  240.      0,                /* special_function */                     
  241.      "R_TCL",               /* name */                                 
  242.      true,                    /* partial_inplace */                      
  243.      0xffff,            /* src_mask */                             
  244.      0xffff,            /* dst_mask */                             
  245.      false),                /* pcrel_offset */
  246.   
  247.   { 7 },
  248.   
  249.   /* Non modifiable absolute branch.  */
  250.   HOWTO (8,                    /* type */                                 
  251.      0,                    /* rightshift */                           
  252.      2,                    /* size (0 = byte, 1 = short, 2 = long) */ 
  253.      26,                    /* bitsize */                   
  254.      false,                    /* pc_relative */                          
  255.      0,                    /* bitpos */                               
  256.      complain_overflow_bitfield, /* complain_on_overflow */
  257.      0,                /* special_function */                     
  258.      "R_BA",                /* name */                                 
  259.      true,                    /* partial_inplace */                      
  260.      0x3fffffc,            /* src_mask */                             
  261.      0x3fffffc,            /* dst_mask */                             
  262.      false),                /* pcrel_offset */
  263.   
  264.   { 9 },
  265.  
  266.   /* Non modifiable relative branch.  */
  267.   HOWTO (0xa,                    /* type */                                 
  268.      0,                    /* rightshift */                           
  269.      2,                    /* size (0 = byte, 1 = short, 2 = long) */ 
  270.      26,                    /* bitsize */                   
  271.      true,                    /* pc_relative */                          
  272.      0,                    /* bitpos */                               
  273.      complain_overflow_signed, /* complain_on_overflow */
  274.      0,                /* special_function */                     
  275.      "R_BR",                /* name */                                 
  276.      true,                    /* partial_inplace */                      
  277.      0x3fffffc,            /* src_mask */                             
  278.      0x3fffffc,            /* dst_mask */                             
  279.      false),                /* pcrel_offset */
  280.   
  281.   { 0xb },
  282.  
  283.   /* Indirect load.  */
  284.   HOWTO (0xc,                    /* type */                                 
  285.      0,                    /* rightshift */                           
  286.      2,                    /* size (0 = byte, 1 = short, 2 = long) */ 
  287.      16,                    /* bitsize */                   
  288.      false,                    /* pc_relative */                          
  289.      0,                    /* bitpos */                               
  290.      complain_overflow_bitfield, /* complain_on_overflow */
  291.      0,                /* special_function */                     
  292.      "R_RL",                /* name */                                 
  293.      true,                    /* partial_inplace */                      
  294.      0xffff,            /* src_mask */                             
  295.      0xffff,            /* dst_mask */                             
  296.      false),                /* pcrel_offset */
  297.   
  298.   /* Load address.  */
  299.   HOWTO (0xd,                    /* type */                                 
  300.      0,                    /* rightshift */                           
  301.      2,                    /* size (0 = byte, 1 = short, 2 = long) */ 
  302.      16,                    /* bitsize */                   
  303.      false,                    /* pc_relative */                          
  304.      0,                    /* bitpos */                               
  305.      complain_overflow_bitfield, /* complain_on_overflow */
  306.      0,                /* special_function */                     
  307.      "R_RLA",               /* name */                                 
  308.      true,                    /* partial_inplace */                      
  309.      0xffff,            /* src_mask */                             
  310.      0xffff,            /* dst_mask */                             
  311.      false),                /* pcrel_offset */
  312.   
  313.   { 0xe },
  314.   
  315.   /* Non-relocating reference.  */
  316.   HOWTO (0xf,                    /* type */                                 
  317.      0,                    /* rightshift */                           
  318.      2,                    /* size (0 = byte, 1 = short, 2 = long) */ 
  319.      32,                    /* bitsize */                   
  320.      false,                    /* pc_relative */                          
  321.      0,                    /* bitpos */                               
  322.      complain_overflow_bitfield, /* complain_on_overflow */
  323.      0,                /* special_function */                     
  324.      "R_REF",               /* name */                                 
  325.      false,                    /* partial_inplace */                      
  326.      0,                /* src_mask */                             
  327.      0,                /* dst_mask */                             
  328.      false),                /* pcrel_offset */
  329.   
  330.   { 0x10 },
  331.   { 0x11 },
  332.   
  333.   /* TOC relative indirect load.  */
  334.   HOWTO (0x12,                    /* type */                                 
  335.      0,                    /* rightshift */                           
  336.      2,                    /* size (0 = byte, 1 = short, 2 = long) */ 
  337.      16,                    /* bitsize */                   
  338.      false,                    /* pc_relative */                          
  339.      0,                    /* bitpos */                               
  340.      complain_overflow_bitfield, /* complain_on_overflow */
  341.      0,                /* special_function */                     
  342.      "R_TRL",               /* name */                                 
  343.      true,                    /* partial_inplace */                      
  344.      0xffff,            /* src_mask */                             
  345.      0xffff,            /* dst_mask */                             
  346.      false),                /* pcrel_offset */
  347.   
  348.   /* TOC relative load address.  */
  349.   HOWTO (0x13,                    /* type */                                 
  350.      0,                    /* rightshift */                           
  351.      2,                    /* size (0 = byte, 1 = short, 2 = long) */ 
  352.      16,                    /* bitsize */                   
  353.      false,                    /* pc_relative */                          
  354.      0,                    /* bitpos */                               
  355.      complain_overflow_bitfield, /* complain_on_overflow */
  356.      0,                /* special_function */                     
  357.      "R_TRLA",              /* name */                                 
  358.      true,                    /* partial_inplace */                      
  359.      0xffff,            /* src_mask */                             
  360.      0xffff,            /* dst_mask */                             
  361.      false),                /* pcrel_offset */
  362.   
  363.   /* Modifiable relative branch.  */
  364.   HOWTO (0x14,                    /* type */                                 
  365.      1,                    /* rightshift */                           
  366.      2,                    /* size (0 = byte, 1 = short, 2 = long) */ 
  367.      32,                    /* bitsize */                   
  368.      false,                    /* pc_relative */                          
  369.      0,                    /* bitpos */                               
  370.      complain_overflow_bitfield, /* complain_on_overflow */
  371.      0,                /* special_function */                     
  372.      "R_RRTBI",             /* name */                                 
  373.      true,                    /* partial_inplace */                      
  374.      0xffffffff,            /* src_mask */                             
  375.      0xffffffff,            /* dst_mask */                             
  376.      false),                /* pcrel_offset */
  377.   
  378.   /* Modifiable absolute branch.  */
  379.   HOWTO (0x15,                    /* type */                                 
  380.      1,                    /* rightshift */                           
  381.      2,                    /* size (0 = byte, 1 = short, 2 = long) */ 
  382.      32,                    /* bitsize */                   
  383.      false,                    /* pc_relative */                          
  384.      0,                    /* bitpos */                               
  385.      complain_overflow_bitfield, /* complain_on_overflow */
  386.      0,                /* special_function */                     
  387.      "R_RRTBA",             /* name */                                 
  388.      true,                    /* partial_inplace */                      
  389.      0xffffffff,            /* src_mask */                             
  390.      0xffffffff,            /* dst_mask */                             
  391.      false),                /* pcrel_offset */
  392.   
  393.   /* Modifiable call absolute indirect.  */
  394.   HOWTO (0x16,                    /* type */                                 
  395.      0,                    /* rightshift */                           
  396.      2,                    /* size (0 = byte, 1 = short, 2 = long) */ 
  397.      16,                    /* bitsize */                   
  398.      false,                    /* pc_relative */                          
  399.      0,                    /* bitpos */                               
  400.      complain_overflow_bitfield, /* complain_on_overflow */
  401.      0,                /* special_function */                     
  402.      "R_CAI",               /* name */                                 
  403.      true,                    /* partial_inplace */                      
  404.      0xffff,            /* src_mask */                             
  405.      0xffff,            /* dst_mask */                             
  406.      false),                /* pcrel_offset */
  407.   
  408.   /* Modifiable call relative.  */
  409.   HOWTO (0x17,                    /* type */                                 
  410.      0,                    /* rightshift */                           
  411.      2,                    /* size (0 = byte, 1 = short, 2 = long) */ 
  412.      16,                    /* bitsize */                   
  413.      false,                    /* pc_relative */                          
  414.      0,                    /* bitpos */                               
  415.      complain_overflow_bitfield, /* complain_on_overflow */
  416.      0,                /* special_function */                     
  417.      "R_CREL",              /* name */                                 
  418.      true,                    /* partial_inplace */                      
  419.      0xffff,            /* src_mask */                             
  420.      0xffff,            /* dst_mask */                             
  421.      false),                /* pcrel_offset */
  422.   
  423.   /* Modifiable branch absolute.  */
  424.   HOWTO (0x18,                    /* type */                                 
  425.      0,                    /* rightshift */                           
  426.      2,                    /* size (0 = byte, 1 = short, 2 = long) */ 
  427.      16,                    /* bitsize */                   
  428.      false,                    /* pc_relative */                          
  429.      0,                    /* bitpos */                               
  430.      complain_overflow_bitfield, /* complain_on_overflow */
  431.      0,                /* special_function */                     
  432.      "R_RBA",               /* name */                                 
  433.      true,                    /* partial_inplace */                      
  434.      0xffff,            /* src_mask */                             
  435.      0xffff,            /* dst_mask */                             
  436.      false),                /* pcrel_offset */
  437.   
  438.   /* Modifiable branch absolute.  */
  439.   HOWTO (0x19,                    /* type */                                 
  440.      0,                    /* rightshift */                           
  441.      2,                    /* size (0 = byte, 1 = short, 2 = long) */ 
  442.      16,                    /* bitsize */                   
  443.      false,                    /* pc_relative */                          
  444.      0,                    /* bitpos */                               
  445.      complain_overflow_bitfield, /* complain_on_overflow */
  446.      0,                /* special_function */                     
  447.      "R_RBAC",              /* name */                                 
  448.      true,                    /* partial_inplace */                      
  449.      0xffff,            /* src_mask */                             
  450.      0xffff,            /* dst_mask */                             
  451.      false),                /* pcrel_offset */
  452.   
  453.   /* Modifiable branch relative.  */
  454.   HOWTO (0x1a,                    /* type */                                 
  455.      0,                    /* rightshift */                           
  456.      2,                    /* size (0 = byte, 1 = short, 2 = long) */ 
  457.      26,                    /* bitsize */                   
  458.      false,                    /* pc_relative */                          
  459.      0,                    /* bitpos */                               
  460.      complain_overflow_signed, /* complain_on_overflow */
  461.      0,                /* special_function */                     
  462.      "R_RBR",               /* name */                                 
  463.      true,                    /* partial_inplace */                      
  464.      0xffff,            /* src_mask */                             
  465.      0xffff,            /* dst_mask */                             
  466.      false),                /* pcrel_offset */
  467.   
  468.   /* Modifiable branch absolute.  */
  469.   HOWTO (0x1b,                    /* type */                                 
  470.      0,                    /* rightshift */                           
  471.      2,                    /* size (0 = byte, 1 = short, 2 = long) */ 
  472.      16,                    /* bitsize */                   
  473.      false,                    /* pc_relative */                          
  474.      0,                    /* bitpos */                               
  475.      complain_overflow_bitfield, /* complain_on_overflow */
  476.      0,                /* special_function */                     
  477.      "R_RBRC",              /* name */                                 
  478.      true,                    /* partial_inplace */                      
  479.      0xffff,            /* src_mask */                             
  480.      0xffff,            /* dst_mask */                             
  481.      false)                 /* pcrel_offset */
  482. };
  483.  
  484. static void
  485. xcoff_rtype2howto (relent, internal)
  486.      arelent *relent;
  487.      struct internal_reloc *internal;
  488. {
  489.   relent->howto = xcoff_howto_table + internal->r_type;
  490.  
  491.   /* The r_size field of an XCOFF reloc encodes the bitsize of the
  492.      relocation, as well as indicating whether it is signed or not.
  493.      Doublecheck that the relocation information gathered from the
  494.      type matches this information.  */
  495.   if (relent->howto->bitsize != ((unsigned int) internal->r_size & 0x1f) + 1)
  496.     abort ();
  497. #if 0
  498.   if ((internal->r_size & 0x80) != 0
  499.       ? (relent->howto->complain_on_overflow != complain_overflow_signed)
  500.       : (relent->howto->complain_on_overflow != complain_overflow_bitfield))
  501.     abort ();
  502. #endif
  503. }
  504.  
  505. static reloc_howto_type *
  506. xcoff_reloc_type_lookup (abfd, code)
  507.      bfd *abfd;
  508.      bfd_reloc_code_real_type code;
  509. {
  510.   switch (code)
  511.     {
  512.     case BFD_RELOC_PPC_B26:
  513.       return &xcoff_howto_table[0xa];
  514.     case BFD_RELOC_PPC_BA26:
  515.       return &xcoff_howto_table[8];
  516.     case BFD_RELOC_PPC_TOC16:
  517.       return &xcoff_howto_table[3];
  518.     case BFD_RELOC_32:
  519.     case BFD_RELOC_CTOR:
  520.       return &xcoff_howto_table[0];
  521.     default:
  522.       return NULL;
  523.     }
  524. }
  525.  
  526. #define SELECT_RELOC(internal, howto)                    \
  527.   {                                    \
  528.     internal.r_type = howto->type;                    \
  529.     internal.r_size =                            \
  530.       ((howto->complain_on_overflow == complain_overflow_signed        \
  531.     ? 0x80                                \
  532.     : 0)                                \
  533.        | (howto->bitsize - 1));                        \
  534.   }
  535.  
  536. #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (3)
  537.  
  538. #define COFF_LONG_FILENAMES
  539.  
  540. #define RTYPE2HOWTO(cache_ptr, dst) xcoff_rtype2howto (cache_ptr, dst)
  541.  
  542. #define coff_mkobject xcoff_mkobject
  543. #define coff_bfd_copy_private_bfd_data xcoff_copy_private_bfd_data
  544. #define coff_bfd_reloc_type_lookup xcoff_reloc_type_lookup
  545. #define coff_relocate_section _bfd_ppc_xcoff_relocate_section
  546.  
  547. #include "coffcode.h"
  548.  
  549. /* XCOFF archive support.  The original version of this code was by
  550.    Damon A. Permezel.  It was enhanced to permit cross support, and
  551.    writing archive files, by Ian Lance Taylor, Cygnus Support.
  552.  
  553.    XCOFF uses its own archive format.  Everything is hooked together
  554.    with file offset links, so it is possible to rapidly update an
  555.    archive in place.  Of course, we don't do that.  An XCOFF archive
  556.    has a real file header, not just an ARMAG string.  The structure of
  557.    the file header and of each archive header appear below.
  558.  
  559.    An XCOFF archive also has a member table, which is a list of
  560.    elements in the archive (you can get that by looking through the
  561.    linked list, but you have to read a lot more of the file).  The
  562.    member table has a normal archive header with an empty name.  It is
  563.    normally (and perhaps must be) the second to last entry in the
  564.    archive.  The member table data is almost printable ASCII.  It
  565.    starts with a 12 character decimal string which is the number of
  566.    entries in the table.  For each entry it has a 12 character decimal
  567.    string which is the offset in the archive of that member.  These
  568.    entries are followed by a series of null terminated strings which
  569.    are the member names for each entry.
  570.  
  571.    Finally, an XCOFF archive has a global symbol table, which is what
  572.    we call the armap.  The global symbol table has a normal archive
  573.    header with an empty name.  It is normally (and perhaps must be)
  574.    the last entry in the archive.  The contents start with a four byte
  575.    binary number which is the number of entries.  This is followed by
  576.    a that many four byte binary numbers; each is the file offset of an
  577.    entry in the archive.  These numbers are followed by a series of
  578.    null terminated strings, which are symbol names.  */
  579.  
  580. /* XCOFF archives use this as a magic string.  */
  581.  
  582. #define XCOFFARMAG "<aiaff>\012"
  583. #define SXCOFFARMAG 8
  584.  
  585. /* This terminates an XCOFF archive member name.  */
  586.  
  587. #define XCOFFARFMAG "`\012"
  588. #define SXCOFFARFMAG 2
  589.  
  590. /* XCOFF archives start with this (printable) structure.  */
  591.  
  592. struct xcoff_ar_file_hdr
  593. {
  594.   /* Magic string.  */
  595.   char magic[SXCOFFARMAG];
  596.  
  597.   /* Offset of the member table (decimal ASCII string).  */
  598.   char memoff[12];
  599.  
  600.   /* Offset of the global symbol table (decimal ASCII string).  */
  601.   char symoff[12];
  602.  
  603.   /* Offset of the first member in the archive (decimal ASCII string).  */
  604.   char firstmemoff[12];
  605.  
  606.   /* Offset of the last member in the archive (decimal ASCII string).  */
  607.   char lastmemoff[12];
  608.  
  609.   /* Offset of the first member on the free list (decimal ASCII
  610.      string).  */
  611.   char freeoff[12];
  612. };
  613.  
  614. #define SIZEOF_AR_FILE_HDR (5 * 12 + SXCOFFARMAG)
  615.  
  616. /* Each XCOFF archive member starts with this (printable) structure.  */
  617.  
  618. struct xcoff_ar_hdr
  619. {
  620.   /* File size not including the header (decimal ASCII string).  */
  621.   char size[12];
  622.  
  623.   /* File offset of next archive member (decimal ASCII string).  */
  624.   char nextoff[12];
  625.  
  626.   /* File offset of previous archive member (decimal ASCII string).  */
  627.   char prevoff[12];
  628.  
  629.   /* File mtime (decimal ASCII string).  */
  630.   char date[12];
  631.  
  632.   /* File UID (decimal ASCII string).  */
  633.   char uid[12];
  634.  
  635.   /* File GID (decimal ASCII string).  */
  636.   char gid[12];
  637.  
  638.   /* File mode (octal ASCII string).  */
  639.   char mode[12];
  640.  
  641.   /* Length of file name (decimal ASCII string).  */
  642.   char namlen[4];
  643.  
  644.   /* This structure is followed by the file name.  The length of the
  645.      name is given in the namlen field.  If the length of the name is
  646.      odd, the name is followed by a null byte.  The name and optional
  647.      null byte are followed by XCOFFARFMAG, which is not included in
  648.      namlen.  The contents of the archive member follow; the number of
  649.      bytes is given in the size field.  */
  650. };
  651.  
  652. #define SIZEOF_AR_HDR (7 * 12 + 4)
  653.  
  654. /* We store a copy of the xcoff_ar_file_hdr in the tdata field of the
  655.    artdata structure.  */
  656. #define xcoff_ardata(abfd) \
  657.   ((struct xcoff_ar_file_hdr *) bfd_ardata (abfd)->tdata)
  658.  
  659. /* We store a copy of the xcoff_ar_hdr in the arelt_data field of an
  660.    archive element.  */
  661. #define arch_eltdata(bfd) ((struct areltdata *) ((bfd)->arelt_data))
  662. #define arch_xhdr(bfd) \
  663.   ((struct xcoff_ar_hdr *) arch_eltdata (bfd)->arch_header)
  664.  
  665. /* XCOFF archives do not have anything which corresponds to an
  666.    extended name table.  */
  667.  
  668. #define xcoff_slurp_extended_name_table bfd_false
  669. #define xcoff_construct_extended_name_table \
  670.   ((boolean (*) PARAMS ((bfd *, char **, bfd_size_type *, const char **))) \
  671.    bfd_false)
  672. #define xcoff_truncate_arname bfd_dont_truncate_arname
  673.  
  674. /* We can use the standard get_elt_at_index routine.  */
  675.  
  676. #define xcoff_get_elt_at_index _bfd_generic_get_elt_at_index
  677.  
  678. /* XCOFF archives do not have a timestamp.  */
  679.  
  680. #define xcoff_update_armap_timestamp bfd_true
  681.  
  682. /* Read in the armap of an XCOFF archive.  */
  683.  
  684. static boolean
  685. xcoff_slurp_armap (abfd)
  686.      bfd *abfd;
  687. {
  688.   file_ptr off;
  689.   struct xcoff_ar_hdr hdr;
  690.   size_t namlen;
  691.   bfd_size_type sz;
  692.   bfd_byte *contents, *cend;
  693.   unsigned int c, i;
  694.   carsym *arsym;
  695.   bfd_byte *p;
  696.  
  697.   if (xcoff_ardata (abfd) == NULL)
  698.     {
  699.       bfd_has_map (abfd) = false;
  700.       return true;
  701.     }
  702.  
  703.   off = strtol (xcoff_ardata (abfd)->symoff, (char **) NULL, 10);
  704.   if (off == 0)
  705.     {
  706.       bfd_has_map (abfd) = false;
  707.       return true;
  708.     }
  709.  
  710.   if (bfd_seek (abfd, off, SEEK_SET) != 0)
  711.     return false;
  712.  
  713.   /* The symbol table starts with a normal archive header.  */
  714.   if (bfd_read ((PTR) &hdr, SIZEOF_AR_HDR, 1, abfd) != SIZEOF_AR_HDR)
  715.     return false;
  716.  
  717.   /* Skip the name (normally empty).  */
  718.   namlen = strtol (hdr.namlen, (char **) NULL, 10);
  719.   if (bfd_seek (abfd, ((namlen + 1) & ~1) + SXCOFFARFMAG, SEEK_CUR) != 0)
  720.     return false;
  721.  
  722.   /* Read in the entire symbol table.  */
  723.   sz = strtol (hdr.size, (char **) NULL, 10);
  724.   contents = (bfd_byte *) bfd_alloc (abfd, sz);
  725.   if (contents == NULL)
  726.     return false;
  727.   if (bfd_read ((PTR) contents, 1, sz, abfd) != sz)
  728.     return false;
  729.  
  730.   /* The symbol table starts with a four byte count.  */
  731.   c = bfd_h_get_32 (abfd, contents);
  732.  
  733.   if (c * 4 >= sz)
  734.     {
  735.       bfd_set_error (bfd_error_bad_value);
  736.       return false;
  737.     }
  738.  
  739.   bfd_ardata (abfd)->symdefs = ((carsym *)
  740.                 bfd_alloc (abfd, c * sizeof (carsym)));
  741.   if (bfd_ardata (abfd)->symdefs == NULL)
  742.     return false;
  743.  
  744.   /* After the count comes a list of four byte file offsets.  */
  745.   for (i = 0, arsym = bfd_ardata (abfd)->symdefs, p = contents + 4;
  746.        i < c;
  747.        ++i, ++arsym, p += 4)
  748.     arsym->file_offset = bfd_h_get_32 (abfd, p);
  749.  
  750.   /* After the file offsets come null terminated symbol names.  */
  751.   cend = contents + sz;
  752.   for (i = 0, arsym = bfd_ardata (abfd)->symdefs;
  753.        i < c;
  754.        ++i, ++arsym, p += strlen ((char *) p) + 1)
  755.     {
  756.       if (p >= cend)
  757.     {
  758.       bfd_set_error (bfd_error_bad_value);
  759.       return false;
  760.     }
  761.       arsym->name = (char *) p;
  762.     }
  763.  
  764.   bfd_ardata (abfd)->symdef_count = c;
  765.   bfd_has_map (abfd) = true;
  766.  
  767.   return true;
  768. }
  769.  
  770. /* See if this is an XCOFF archive.  */
  771.  
  772. static const bfd_target *
  773. xcoff_archive_p (abfd)
  774.      bfd *abfd;
  775. {
  776.   struct xcoff_ar_file_hdr hdr;
  777.  
  778.   if (bfd_read ((PTR) &hdr, SIZEOF_AR_FILE_HDR, 1, abfd)
  779.       != SIZEOF_AR_FILE_HDR)
  780.     {
  781.       if (bfd_get_error () != bfd_error_system_call)
  782.     bfd_set_error (bfd_error_wrong_format);
  783.       return NULL;
  784.     }
  785.  
  786.   if (strncmp (hdr.magic, XCOFFARMAG, SXCOFFARMAG) != 0)
  787.     {
  788.       bfd_set_error (bfd_error_wrong_format);
  789.       return NULL;
  790.     }
  791.  
  792.   /* We are setting bfd_ardata(abfd) here, but since bfd_ardata
  793.      involves a cast, we can't do it as the left operand of
  794.      assignment.  */
  795.   abfd->tdata.aout_ar_data =
  796.     (struct artdata *) bfd_zalloc (abfd, sizeof (struct artdata));
  797.  
  798.   if (bfd_ardata (abfd) == (struct artdata *) NULL)
  799.     return NULL;
  800.  
  801.   bfd_ardata (abfd)->first_file_filepos = strtol (hdr.firstmemoff,
  802.                           (char **) NULL, 10);
  803.   bfd_ardata (abfd)->cache = NULL;
  804.   bfd_ardata (abfd)->archive_head = NULL;
  805.   bfd_ardata (abfd)->symdefs = NULL;
  806.   bfd_ardata (abfd)->extended_names = NULL;
  807.  
  808.   bfd_ardata (abfd)->tdata = bfd_zalloc (abfd, SIZEOF_AR_FILE_HDR);
  809.   if (bfd_ardata (abfd)->tdata == NULL)
  810.     return NULL;
  811.  
  812.   memcpy (bfd_ardata (abfd)->tdata, &hdr, SIZEOF_AR_FILE_HDR);
  813.  
  814.   if (! xcoff_slurp_armap (abfd))
  815.     {
  816.       bfd_release (abfd, bfd_ardata (abfd));
  817.       abfd->tdata.aout_ar_data = (struct artdata *) NULL;
  818.       return NULL;
  819.     }
  820.  
  821.   return abfd->xvec;
  822. }
  823.  
  824. /* Read the archive header in an XCOFF archive.  */
  825.  
  826. static PTR
  827. xcoff_read_ar_hdr (abfd)
  828.      bfd *abfd;
  829. {
  830.   struct xcoff_ar_hdr hdr;
  831.   size_t namlen;
  832.   struct xcoff_ar_hdr *hdrp;
  833.   struct areltdata *ret;
  834.  
  835.   if (bfd_read ((PTR) &hdr, SIZEOF_AR_HDR, 1, abfd) != SIZEOF_AR_HDR)
  836.     return NULL;
  837.  
  838.   namlen = strtol (hdr.namlen, (char **) NULL, 10);
  839.   hdrp = bfd_alloc (abfd, SIZEOF_AR_HDR + namlen + 1);
  840.   if (hdrp == NULL)
  841.     return NULL;
  842.   memcpy (hdrp, &hdr, SIZEOF_AR_HDR);
  843.   if (bfd_read ((char *) hdrp + SIZEOF_AR_HDR, 1, namlen, abfd) != namlen)
  844.     return NULL;
  845.   ((char *) hdrp)[SIZEOF_AR_HDR + namlen] = '\0';
  846.  
  847.   ret = (struct areltdata *) bfd_alloc (abfd, sizeof (struct areltdata));
  848.   if (ret == NULL)
  849.     return NULL;
  850.   ret->arch_header = (char *) hdrp;
  851.   ret->parsed_size = strtol (hdr.size, (char **) NULL, 10);
  852.   ret->filename = (char *) hdrp + SIZEOF_AR_HDR;
  853.  
  854.   /* Skip over the XCOFFARFMAG at the end of the file name.  */
  855.   if (bfd_seek (abfd, (namlen & 1) + SXCOFFARFMAG, SEEK_CUR) != 0)
  856.     return NULL;
  857.  
  858.   return (PTR) ret;
  859. }
  860.  
  861. /* Open the next element in an XCOFF archive.  */
  862.  
  863. static bfd *
  864. xcoff_openr_next_archived_file (archive, last_file)
  865.      bfd *archive;
  866.      bfd *last_file;
  867. {
  868.   file_ptr filestart;
  869.  
  870.   if (xcoff_ardata (archive) == NULL)
  871.     {
  872.       bfd_set_error (bfd_error_invalid_operation);
  873.       return NULL;
  874.     }
  875.  
  876.   if (last_file == NULL)
  877.     filestart = bfd_ardata (archive)->first_file_filepos;
  878.   else
  879.     filestart = strtol (arch_xhdr (last_file)->nextoff, (char **) NULL, 10);
  880.  
  881.   if (filestart == 0
  882.       || filestart == strtol (xcoff_ardata (archive)->memoff,
  883.                   (char **) NULL, 10)
  884.       || filestart == strtol (xcoff_ardata (archive)->symoff,
  885.                   (char **) NULL, 10))
  886.     {
  887.       bfd_set_error (bfd_error_no_more_archived_files);
  888.       return NULL;
  889.     }
  890.  
  891.   return _bfd_get_elt_at_filepos (archive, filestart);
  892. }
  893.  
  894. /* Stat an element in an XCOFF archive.  */
  895.  
  896. static int
  897. xcoff_generic_stat_arch_elt (abfd, s)
  898.      bfd *abfd;
  899.      struct stat *s;
  900. {
  901.   struct xcoff_ar_hdr *hdrp;
  902.  
  903.   if (abfd->arelt_data == NULL)
  904.     {
  905.       bfd_set_error (bfd_error_invalid_operation);
  906.       return -1;
  907.     }
  908.  
  909.   hdrp = arch_xhdr (abfd);
  910.  
  911.   s->st_mtime = strtol (hdrp->date, (char **) NULL, 10);
  912.   s->st_uid = strtol (hdrp->uid, (char **) NULL, 10);
  913.   s->st_gid = strtol (hdrp->gid, (char **) NULL, 10);
  914.   s->st_mode = strtol (hdrp->mode, (char **) NULL, 8);
  915.   s->st_size = arch_eltdata (abfd)->parsed_size;
  916.  
  917.   return 0;
  918. }
  919.  
  920. /* Normalize a file name for inclusion in an archive.  */
  921.  
  922. static const char *
  923. normalize_filename (abfd)
  924.      bfd *abfd;
  925. {
  926.   const char *file;
  927.   const char *filename;
  928.  
  929.   file = bfd_get_filename (abfd);
  930.   filename = strrchr (file, '/');
  931.   if (filename != NULL)
  932.     filename++;
  933.   else
  934.     filename = file;
  935.   return filename;
  936. }
  937.  
  938. /* Write out an XCOFF armap.  */
  939.  
  940. /*ARGSUSED*/
  941. static boolean
  942. xcoff_write_armap (abfd, elength, map, orl_count, stridx)
  943.      bfd *abfd;
  944.      unsigned int elength;
  945.      struct orl *map;
  946.      unsigned int orl_count;
  947.      int stridx;
  948. {
  949.   struct xcoff_ar_hdr hdr;
  950.   char *p;
  951.   unsigned char buf[4];
  952.   bfd *sub;
  953.   file_ptr fileoff;
  954.   unsigned int i;
  955.  
  956.   memset (&hdr, 0, sizeof hdr);
  957.   sprintf (hdr.size, "%ld", (long) (4 + orl_count * 4 + stridx));
  958.   sprintf (hdr.nextoff, "%d", 0);
  959.   memcpy (hdr.prevoff, xcoff_ardata (abfd)->memoff, 12);
  960.   sprintf (hdr.date, "%d", 0);
  961.   sprintf (hdr.uid, "%d", 0);
  962.   sprintf (hdr.gid, "%d", 0);
  963.   sprintf (hdr.mode, "%d", 0);
  964.   sprintf (hdr.namlen, "%d", 0);
  965.  
  966.   /* We need spaces, not null bytes, in the header.  */
  967.   for (p = (char *) &hdr; p < (char *) &hdr + SIZEOF_AR_HDR; p++)
  968.     if (*p == '\0')
  969.       *p = ' ';
  970.  
  971.   if (bfd_write ((PTR) &hdr, SIZEOF_AR_HDR, 1, abfd) != SIZEOF_AR_HDR
  972.       || bfd_write (XCOFFARFMAG, 1, SXCOFFARFMAG, abfd) != SXCOFFARFMAG)
  973.     return false;
  974.   
  975.   bfd_h_put_32 (abfd, orl_count, buf);
  976.   if (bfd_write (buf, 1, 4, abfd) != 4)
  977.     return false;
  978.  
  979.   sub = abfd->archive_head;
  980.   fileoff = SIZEOF_AR_FILE_HDR;
  981.   i = 0;
  982.   while (sub != NULL && i < orl_count)
  983.     {
  984.       size_t namlen;
  985.  
  986.       while (((bfd *) (map[i]).pos) == sub)
  987.     {
  988.       bfd_h_put_32 (abfd, fileoff, buf);
  989.       if (bfd_write (buf, 1, 4, abfd) != 4)
  990.         return false;
  991.       ++i;
  992.     }
  993.       namlen = strlen (normalize_filename (sub));
  994.       namlen = (namlen + 1) &~ 1;
  995.       fileoff += (SIZEOF_AR_HDR
  996.           + namlen
  997.           + SXCOFFARFMAG
  998.           + arelt_size (sub));
  999.       fileoff = (fileoff + 1) &~ 1;
  1000.       sub = sub->next;
  1001.     }
  1002.  
  1003.   for (i = 0; i < orl_count; i++)
  1004.     {
  1005.       const char *name;
  1006.       size_t namlen;
  1007.  
  1008.       name = *map[i].name;
  1009.       namlen = strlen (name);
  1010.       if (bfd_write (name, 1, namlen + 1, abfd) != namlen + 1)
  1011.     return false;
  1012.     }
  1013.  
  1014.   if ((stridx & 1) != 0)
  1015.     {
  1016.       char b;
  1017.  
  1018.       b = '\0';
  1019.       if (bfd_write (&b, 1, 1, abfd) != 1)
  1020.     return false;
  1021.     }
  1022.  
  1023.   return true;
  1024. }
  1025.  
  1026. /* Write out an XCOFF archive.  We always write an entire archive,
  1027.    rather than fussing with the freelist and so forth.  */
  1028.  
  1029. static boolean
  1030. xcoff_write_archive_contents (abfd)
  1031.      bfd *abfd;
  1032. {
  1033.   struct xcoff_ar_file_hdr fhdr;
  1034.   size_t count;
  1035.   size_t total_namlen;
  1036.   file_ptr *offsets;
  1037.   boolean makemap;
  1038.   boolean hasobjects;
  1039.   file_ptr prevoff, nextoff;
  1040.   bfd *sub;
  1041.   unsigned int i;
  1042.   struct xcoff_ar_hdr ahdr;
  1043.   bfd_size_type size;
  1044.   char *p;
  1045.   char decbuf[13];
  1046.  
  1047.   memset (&fhdr, 0, sizeof fhdr);
  1048.   strncpy (fhdr.magic, XCOFFARMAG, SXCOFFARMAG);
  1049.   sprintf (fhdr.firstmemoff, "%d", SIZEOF_AR_FILE_HDR);
  1050.   sprintf (fhdr.freeoff, "%d", 0);
  1051.  
  1052.   count = 0;
  1053.   total_namlen = 0;
  1054.   for (sub = abfd->archive_head; sub != NULL; sub = sub->next)
  1055.     {
  1056.       ++count;
  1057.       total_namlen += strlen (normalize_filename (sub)) + 1;
  1058.     }
  1059.   offsets = (file_ptr *) bfd_alloc (abfd, count * sizeof (file_ptr));
  1060.   if (offsets == NULL)
  1061.     return false;
  1062.  
  1063.   if (bfd_seek (abfd, SIZEOF_AR_FILE_HDR, SEEK_SET) != 0)
  1064.     return false;
  1065.  
  1066.   makemap = bfd_has_map (abfd);
  1067.   hasobjects = false;
  1068.   prevoff = 0;
  1069.   nextoff = SIZEOF_AR_FILE_HDR;
  1070.   for (sub = abfd->archive_head, i = 0; sub != NULL; sub = sub->next, i++)
  1071.     {
  1072.       const char *name;
  1073.       size_t namlen;
  1074.       struct xcoff_ar_hdr *ahdrp;
  1075.       bfd_size_type remaining;
  1076.  
  1077.       if (makemap && ! hasobjects)
  1078.     {
  1079.       if (bfd_check_format (sub, bfd_object))
  1080.         hasobjects = true;
  1081.     }
  1082.  
  1083.       name = normalize_filename (sub);
  1084.       namlen = strlen (name);
  1085.  
  1086.       if (sub->arelt_data != NULL)
  1087.     ahdrp = arch_xhdr (sub);
  1088.       else
  1089.     ahdrp = NULL;
  1090.  
  1091.       if (ahdrp == NULL)
  1092.     {
  1093.       struct stat s;
  1094.  
  1095.       memset (&ahdr, 0, sizeof ahdr);
  1096.       ahdrp = &ahdr;
  1097.       if (stat (bfd_get_filename (sub), &s) != 0)
  1098.         {
  1099.           bfd_set_error (bfd_error_system_call);
  1100.           return false;
  1101.         }
  1102.  
  1103.       sprintf (ahdrp->size, "%ld", (long) s.st_size);
  1104.       sprintf (ahdrp->date, "%ld", (long) s.st_mtime);
  1105.       sprintf (ahdrp->uid, "%ld", (long) s.st_uid);
  1106.       sprintf (ahdrp->gid, "%ld", (long) s.st_gid);
  1107.       sprintf (ahdrp->mode, "%o", (unsigned int) s.st_mode);
  1108.  
  1109.       if (sub->arelt_data == NULL)
  1110.         {
  1111.           sub->arelt_data = ((struct areltdata *)
  1112.                  bfd_alloc (sub, sizeof (struct areltdata)));
  1113.           if (sub->arelt_data == NULL)
  1114.         return false;
  1115.         }
  1116.  
  1117.       arch_eltdata (sub)->parsed_size = s.st_size;
  1118.     }
  1119.  
  1120.       sprintf (ahdrp->prevoff, "%ld", (long) prevoff);
  1121.       sprintf (ahdrp->namlen, "%ld", (long) namlen);
  1122.  
  1123.       /* If the length of the name is odd, we write out the null byte
  1124.          after the name as well.  */
  1125.       namlen = (namlen + 1) &~ 1;
  1126.  
  1127.       remaining = arelt_size (sub);
  1128.       size = (SIZEOF_AR_HDR
  1129.           + namlen
  1130.           + SXCOFFARFMAG
  1131.           + remaining);
  1132.  
  1133.       BFD_ASSERT (nextoff == bfd_tell (abfd));
  1134.  
  1135.       offsets[i] = nextoff;
  1136.  
  1137.       prevoff = nextoff;
  1138.       nextoff += size + (size & 1);
  1139.  
  1140.       sprintf (ahdrp->nextoff, "%ld", (long) nextoff);
  1141.  
  1142.       /* We need spaces, not null bytes, in the header.  */
  1143.       for (p = (char *) ahdrp; p < (char *) ahdrp + SIZEOF_AR_HDR; p++)
  1144.     if (*p == '\0')
  1145.       *p = ' ';
  1146.  
  1147.       if (bfd_write ((PTR) ahdrp, 1, SIZEOF_AR_HDR, abfd) != SIZEOF_AR_HDR
  1148.       || bfd_write ((PTR) name, 1, namlen, abfd) != namlen
  1149.       || (bfd_write ((PTR) XCOFFARFMAG, 1, SXCOFFARFMAG, abfd)
  1150.           != SXCOFFARFMAG))
  1151.     return false;
  1152.  
  1153.       if (bfd_seek (sub, (file_ptr) 0, SEEK_SET) != 0)
  1154.     return false;
  1155.       while (remaining != 0)
  1156.     {
  1157.       bfd_size_type amt;
  1158.       bfd_byte buffer[DEFAULT_BUFFERSIZE];
  1159.  
  1160.       amt = sizeof buffer;
  1161.       if (amt > remaining)
  1162.         amt = remaining;
  1163.       if (bfd_read (buffer, 1, amt, sub) != amt
  1164.           || bfd_write (buffer, 1, amt, abfd) != amt)
  1165.         return false;
  1166.       remaining -= amt;
  1167.     }
  1168.  
  1169.       if ((size & 1) != 0)
  1170.     {
  1171.       bfd_byte b;
  1172.  
  1173.       b = '\0';
  1174.       if (bfd_write (&b, 1, 1, abfd) != 1)
  1175.         return false;
  1176.     }
  1177.     }
  1178.  
  1179.   sprintf (fhdr.lastmemoff, "%ld", (long) prevoff);
  1180.  
  1181.   /* Write out the member table.  */
  1182.  
  1183.   BFD_ASSERT (nextoff == bfd_tell (abfd));
  1184.   sprintf (fhdr.memoff, "%ld", (long) nextoff);
  1185.  
  1186.   memset (&ahdr, 0, sizeof ahdr);
  1187.   sprintf (ahdr.size, "%ld", (long) (12 + count * 12 + total_namlen));
  1188.   sprintf (ahdr.prevoff, "%ld", (long) prevoff);
  1189.   sprintf (ahdr.date, "%d", 0);
  1190.   sprintf (ahdr.uid, "%d", 0);
  1191.   sprintf (ahdr.gid, "%d", 0);
  1192.   sprintf (ahdr.mode, "%d", 0);
  1193.   sprintf (ahdr.namlen, "%d", 0);
  1194.  
  1195.   size = (SIZEOF_AR_HDR
  1196.       + 12
  1197.       + count * 12
  1198.       + total_namlen
  1199.       + SXCOFFARFMAG);
  1200.  
  1201.   prevoff = nextoff;
  1202.   nextoff += size + (size & 1);
  1203.  
  1204.   if (makemap && hasobjects)
  1205.     sprintf (ahdr.nextoff, "%ld", (long) nextoff);
  1206.   else
  1207.     sprintf (ahdr.nextoff, "%d", 0);
  1208.  
  1209.   /* We need spaces, not null bytes, in the header.  */
  1210.   for (p = (char *) &ahdr; p < (char *) &ahdr + SIZEOF_AR_HDR; p++)
  1211.     if (*p == '\0')
  1212.       *p = ' ';
  1213.  
  1214.   if (bfd_write ((PTR) &ahdr, 1, SIZEOF_AR_HDR, abfd) != SIZEOF_AR_HDR
  1215.       || (bfd_write ((PTR) XCOFFARFMAG, 1, SXCOFFARFMAG, abfd)
  1216.       != SXCOFFARFMAG))
  1217.     return false;
  1218.  
  1219.   sprintf (decbuf, "%-12ld", (long) count);
  1220.   if (bfd_write ((PTR) decbuf, 1, 12, abfd) != 12)
  1221.     return false;
  1222.   for (i = 0; i < count; i++)
  1223.     {
  1224.       sprintf (decbuf, "%-12ld", (long) offsets[i]);
  1225.       if (bfd_write ((PTR) decbuf, 1, 12, abfd) != 12)
  1226.     return false;
  1227.     }
  1228.   for (sub = abfd->archive_head; sub != NULL; sub = sub->next)
  1229.     {
  1230.       const char *name;
  1231.       size_t namlen;
  1232.  
  1233.       name = normalize_filename (sub);
  1234.       namlen = strlen (name);
  1235.       if (bfd_write ((PTR) name, 1, namlen + 1, abfd) != namlen + 1)
  1236.     return false;
  1237.     }
  1238.   if ((size & 1) != 0)
  1239.     {
  1240.       bfd_byte b;
  1241.  
  1242.       b = '\0';
  1243.       if (bfd_write ((PTR) &b, 1, 1, abfd) != 1)
  1244.     return false;
  1245.     }
  1246.  
  1247.   /* Write out the armap, if appropriate.  */
  1248.  
  1249.   if (! makemap || ! hasobjects)
  1250.     sprintf (fhdr.symoff, "%d", 0);
  1251.   else
  1252.     {
  1253.       BFD_ASSERT (nextoff == bfd_tell (abfd));
  1254.       sprintf (fhdr.symoff, "%ld", (long) nextoff);
  1255.       bfd_ardata (abfd)->tdata = (PTR) &fhdr;
  1256.       if (! _bfd_compute_and_write_armap (abfd, 0))
  1257.     return false;
  1258.     }
  1259.  
  1260.   /* Write out the archive file header.  */
  1261.  
  1262.   /* We need spaces, not null bytes, in the header.  */
  1263.   for (p = (char *) &fhdr; p < (char *) &fhdr + SIZEOF_AR_FILE_HDR; p++)
  1264.     if (*p == '\0')
  1265.       *p = ' ';
  1266.  
  1267.   if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
  1268.       || (bfd_write ((PTR) &fhdr, SIZEOF_AR_FILE_HDR, 1, abfd) !=
  1269.       SIZEOF_AR_FILE_HDR))
  1270.     return false;
  1271.  
  1272.   return true;
  1273. }
  1274.  
  1275. /* We can't use the usual coff_sizeof_headers routine, because AIX
  1276.    always uses an a.out header.  */
  1277.  
  1278. /*ARGSUSED*/
  1279. static int
  1280. _bfd_xcoff_sizeof_headers (abfd, reloc)
  1281.      bfd *abfd;
  1282.      boolean reloc;
  1283. {
  1284.   int size;
  1285.  
  1286.   size = FILHSZ;
  1287.   if (xcoff_data (abfd)->full_aouthdr)
  1288.     size += AOUTSZ;
  1289.   else
  1290.     size += SMALL_AOUTSZ;
  1291.   size += abfd->section_count * SCNHSZ;
  1292.   return size;
  1293. }
  1294.  
  1295. #define CORE_FILE_P _bfd_dummy_target
  1296.  
  1297. #define coff_core_file_failing_command _bfd_nocore_core_file_failing_command
  1298. #define coff_core_file_failing_signal _bfd_nocore_core_file_failing_signal
  1299. #define coff_core_file_matches_executable_p \
  1300.   _bfd_nocore_core_file_matches_executable_p
  1301.  
  1302. #ifdef AIX_CORE
  1303. #undef CORE_FILE_P
  1304. #define CORE_FILE_P rs6000coff_core_p
  1305. extern const bfd_target * rs6000coff_core_p ();
  1306. extern boolean rs6000coff_get_section_contents ();
  1307. extern boolean rs6000coff_core_file_matches_executable_p ();
  1308.  
  1309. #undef    coff_core_file_matches_executable_p
  1310. #define coff_core_file_matches_executable_p  \
  1311.                      rs6000coff_core_file_matches_executable_p
  1312.  
  1313. extern char *rs6000coff_core_file_failing_command PARAMS ((bfd *abfd));
  1314. #undef coff_core_file_failing_command
  1315. #define coff_core_file_failing_command rs6000coff_core_file_failing_command
  1316.  
  1317. extern int rs6000coff_core_file_failing_signal PARAMS ((bfd *abfd));
  1318. #undef coff_core_file_failing_signal
  1319. #define coff_core_file_failing_signal rs6000coff_core_file_failing_signal
  1320.  
  1321. #undef    coff_get_section_contents
  1322. #define    coff_get_section_contents    rs6000coff_get_section_contents
  1323. #endif /* AIX_CORE */
  1324.  
  1325. #ifdef LYNX_CORE
  1326.  
  1327. #undef CORE_FILE_P
  1328. #define CORE_FILE_P lynx_core_file_p
  1329. extern const bfd_target *lynx_core_file_p PARAMS ((bfd *abfd));
  1330.  
  1331. extern boolean lynx_core_file_matches_executable_p PARAMS ((bfd *core_bfd,
  1332.                                 bfd *exec_bfd));
  1333. #undef    coff_core_file_matches_executable_p
  1334. #define coff_core_file_matches_executable_p lynx_core_file_matches_executable_p
  1335.  
  1336. extern char *lynx_core_file_failing_command PARAMS ((bfd *abfd));
  1337. #undef coff_core_file_failing_command
  1338. #define coff_core_file_failing_command lynx_core_file_failing_command
  1339.  
  1340. extern int lynx_core_file_failing_signal PARAMS ((bfd *abfd));
  1341. #undef coff_core_file_failing_signal
  1342. #define coff_core_file_failing_signal lynx_core_file_failing_signal
  1343.  
  1344. #endif /* LYNX_CORE */
  1345.  
  1346. #define _bfd_xcoff_bfd_get_relocated_section_contents \
  1347.   coff_bfd_get_relocated_section_contents
  1348. #define _bfd_xcoff_bfd_relax_section coff_bfd_relax_section
  1349. #define _bfd_xcoff_bfd_link_split_section coff_bfd_link_split_section
  1350.  
  1351. /* The transfer vector that leads the outside world to all of the above. */
  1352.  
  1353. const bfd_target
  1354. #ifdef TARGET_SYM
  1355.   TARGET_SYM =
  1356. #else
  1357.   rs6000coff_vec =
  1358. #endif
  1359. {
  1360. #ifdef TARGET_NAME
  1361.   TARGET_NAME,
  1362. #else
  1363.   "aixcoff-rs6000",        /* name */
  1364. #endif
  1365.   bfd_target_coff_flavour,    
  1366.   BFD_ENDIAN_BIG,        /* data byte order is big */
  1367.   BFD_ENDIAN_BIG,        /* header byte order is big */
  1368.  
  1369.   (HAS_RELOC | EXEC_P |        /* object flags */
  1370.    HAS_LINENO | HAS_DEBUG | DYNAMIC |
  1371.    HAS_SYMS | HAS_LOCALS | WP_TEXT),
  1372.  
  1373.   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
  1374.   0,                /* leading char */
  1375.   '/',                /* ar_pad_char */
  1376.   15,                /* ar_max_namelen??? FIXMEmgo */
  1377.  
  1378.   bfd_getb64, bfd_getb_signed_64, bfd_putb64,
  1379.      bfd_getb32, bfd_getb_signed_32, bfd_putb32,
  1380.      bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
  1381.   bfd_getb64, bfd_getb_signed_64, bfd_putb64,
  1382.      bfd_getb32, bfd_getb_signed_32, bfd_putb32,
  1383.      bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
  1384.  
  1385.   {_bfd_dummy_target, coff_object_p,     /* bfd_check_format */
  1386.      xcoff_archive_p, CORE_FILE_P},
  1387.   {bfd_false, coff_mkobject,        /* bfd_set_format */
  1388.      _bfd_generic_mkarchive, bfd_false},
  1389.   {bfd_false, coff_write_object_contents,    /* bfd_write_contents */
  1390.      xcoff_write_archive_contents, bfd_false},
  1391.  
  1392.      BFD_JUMP_TABLE_GENERIC (coff),
  1393.      BFD_JUMP_TABLE_COPY (coff),
  1394.      BFD_JUMP_TABLE_CORE (coff),
  1395.      BFD_JUMP_TABLE_ARCHIVE (xcoff),
  1396.      BFD_JUMP_TABLE_SYMBOLS (coff),
  1397.      BFD_JUMP_TABLE_RELOCS (coff),
  1398.      BFD_JUMP_TABLE_WRITE (coff),
  1399.      BFD_JUMP_TABLE_LINK (_bfd_xcoff),
  1400.      BFD_JUMP_TABLE_DYNAMIC (_bfd_xcoff),
  1401.  
  1402.   COFF_SWAP_TABLE,
  1403. };
  1404.