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 / elf32-ppc.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  87KB  |  2,820 lines

  1. /* PowerPC-specific support for 32-bit ELF
  2.    Copyright 1994, 1995, 1996 Free Software Foundation, Inc.
  3.    Written by Ian Lance Taylor, Cygnus Support.
  4.  
  5. This file is part of BFD, the Binary File Descriptor library.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
  20.  
  21. /* This file is based on a preliminary PowerPC ELF ABI.  The
  22.    information may not match the final PowerPC ELF ABI.  It includes
  23.    suggestions from the in-progress Embedded PowerPC ABI, and that
  24.    information may also not match.  */
  25.  
  26. #include "bfd.h"
  27. #include "sysdep.h"
  28. #include "bfdlink.h"
  29. #include "libbfd.h"
  30. #include "elf-bfd.h"
  31. #include "elf/ppc.h"
  32.  
  33. #define USE_RELA        /* we want RELA relocations, not REL */
  34.  
  35. /* PowerPC relocations defined by the ABIs */
  36. enum ppc_reloc_type
  37. {
  38.   R_PPC_NONE            =   0,
  39.   R_PPC_ADDR32            =   1,
  40.   R_PPC_ADDR24            =   2,
  41.   R_PPC_ADDR16            =   3,
  42.   R_PPC_ADDR16_LO        =   4,
  43.   R_PPC_ADDR16_HI        =   5,
  44.   R_PPC_ADDR16_HA        =   6,
  45.   R_PPC_ADDR14            =   7,
  46.   R_PPC_ADDR14_BRTAKEN        =   8,
  47.   R_PPC_ADDR14_BRNTAKEN        =   9,
  48.   R_PPC_REL24            =  10,
  49.   R_PPC_REL14            =  11,
  50.   R_PPC_REL14_BRTAKEN        =  12,
  51.   R_PPC_REL14_BRNTAKEN        =  13,
  52.   R_PPC_GOT16            =  14,
  53.   R_PPC_GOT16_LO        =  15,
  54.   R_PPC_GOT16_HI        =  16,
  55.   R_PPC_GOT16_HA        =  17,
  56.   R_PPC_PLTREL24        =  18,
  57.   R_PPC_COPY            =  19,
  58.   R_PPC_GLOB_DAT        =  20,
  59.   R_PPC_JMP_SLOT        =  21,
  60.   R_PPC_RELATIVE        =  22,
  61.   R_PPC_LOCAL24PC        =  23,
  62.   R_PPC_UADDR32            =  24,
  63.   R_PPC_UADDR16            =  25,
  64.   R_PPC_REL32            =  26,
  65.   R_PPC_PLT32            =  27,
  66.   R_PPC_PLTREL32        =  28,
  67.   R_PPC_PLT16_LO        =  29,
  68.   R_PPC_PLT16_HI        =  30,
  69.   R_PPC_PLT16_HA        =  31,
  70.   R_PPC_SDAREL16        =  32,
  71.   R_PPC_SECTOFF            =  33,
  72.   R_PPC_SECTOFF_LO        =  34,
  73.   R_PPC_SECTOFF_HI        =  35,
  74.   R_PPC_SECTOFF_HA        =  36,
  75.  
  76.   /* The remaining relocs are from the Embedded ELF ABI, and are not
  77.      in the SVR4 ELF ABI.  */
  78.   R_PPC_EMB_NADDR32        = 101,
  79.   R_PPC_EMB_NADDR16        = 102,
  80.   R_PPC_EMB_NADDR16_LO        = 103,
  81.   R_PPC_EMB_NADDR16_HI        = 104,
  82.   R_PPC_EMB_NADDR16_HA        = 105,
  83.   R_PPC_EMB_SDAI16        = 106,
  84.   R_PPC_EMB_SDA2I16        = 107,
  85.   R_PPC_EMB_SDA2REL        = 108,
  86.   R_PPC_EMB_SDA21        = 109,
  87.   R_PPC_EMB_MRKREF        = 110,
  88.   R_PPC_EMB_RELSEC16        = 111,
  89.   R_PPC_EMB_RELST_LO        = 112,
  90.   R_PPC_EMB_RELST_HI        = 113,
  91.   R_PPC_EMB_RELST_HA        = 114,
  92.   R_PPC_EMB_BIT_FLD        = 115,
  93.   R_PPC_EMB_RELSDA        = 116,
  94.  
  95.   /* This is a phony reloc to handle any old fashioned TOC16 references
  96.      that may still be in object files.  */
  97.   R_PPC_TOC16            = 255,
  98.  
  99.   R_PPC_max
  100. };
  101.  
  102. static reloc_howto_type *ppc_elf_reloc_type_lookup
  103.   PARAMS ((bfd *abfd, bfd_reloc_code_real_type code));
  104. static void ppc_elf_info_to_howto
  105.   PARAMS ((bfd *abfd, arelent *cache_ptr, Elf32_Internal_Rela *dst));
  106. static void ppc_elf_howto_init PARAMS ((void));
  107. static boolean ppc_elf_set_private_flags PARAMS ((bfd *, flagword));
  108. static boolean ppc_elf_copy_private_bfd_data PARAMS ((bfd *, bfd *));
  109. static boolean ppc_elf_merge_private_bfd_data PARAMS ((bfd *, bfd *));
  110.  
  111. static int ppc_elf_additional_program_headers PARAMS ((bfd *));
  112. static boolean ppc_elf_modify_segment_map PARAMS ((bfd *));
  113.  
  114. static boolean ppc_elf_section_from_shdr PARAMS ((bfd *,
  115.                           Elf32_Internal_Shdr *,
  116.                           char *));
  117.  
  118. static elf_linker_section_t *ppc_elf_create_linker_section
  119.   PARAMS ((bfd *abfd,
  120.        struct bfd_link_info *info,
  121.        enum elf_linker_section_enum));
  122.  
  123. static boolean ppc_elf_check_relocs PARAMS ((bfd *,
  124.                          struct bfd_link_info *,
  125.                          asection *,
  126.                          const Elf_Internal_Rela *));
  127.  
  128. static boolean ppc_elf_adjust_dynamic_symbol PARAMS ((struct bfd_link_info *,
  129.                               struct elf_link_hash_entry *));
  130.  
  131. static boolean ppc_elf_adjust_dynindx PARAMS ((struct elf_link_hash_entry *, PTR));
  132.  
  133. static boolean ppc_elf_size_dynamic_sections PARAMS ((bfd *, struct bfd_link_info *));
  134.  
  135. static boolean ppc_elf_relocate_section PARAMS ((bfd *,
  136.                          struct bfd_link_info *info,
  137.                          bfd *,
  138.                          asection *,
  139.                          bfd_byte *,
  140.                          Elf_Internal_Rela *relocs,
  141.                          Elf_Internal_Sym *local_syms,
  142.                          asection **));
  143.  
  144. static boolean ppc_elf_add_symbol_hook  PARAMS ((bfd *,
  145.                          struct bfd_link_info *,
  146.                          const Elf_Internal_Sym *,
  147.                          const char **,
  148.                          flagword *,
  149.                          asection **,
  150.                          bfd_vma *));
  151.  
  152. static boolean ppc_elf_finish_dynamic_symbol PARAMS ((bfd *,
  153.                               struct bfd_link_info *,
  154.                               struct elf_link_hash_entry *,
  155.                               Elf_Internal_Sym *));
  156.  
  157. static boolean ppc_elf_finish_dynamic_sections PARAMS ((bfd *, struct bfd_link_info *));
  158.  
  159. #define BRANCH_PREDICT_BIT 0x200000        /* branch prediction bit for branch taken relocs */
  160. #define RA_REGISTER_MASK 0x001f0000        /* mask to set RA in memory instructions */
  161. #define RA_REGISTER_SHIFT 16            /* value to shift register by to insert RA */
  162.  
  163. /* The name of the dynamic interpreter.  This is put in the .interp
  164.    section.  */
  165.  
  166. #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
  167.  
  168. /* The size in bytes of an entry in the procedure linkage table, and of the initial size
  169.    of the plt reserved for the dynamic linker.  */
  170.  
  171. #define PLT_ENTRY_SIZE 12
  172. #define PLT_INITIAL_ENTRY_SIZE 72
  173.  
  174.  
  175. static reloc_howto_type *ppc_elf_howto_table[ (int)R_PPC_max ];
  176.  
  177. static reloc_howto_type ppc_elf_howto_raw[] =
  178. {
  179.   /* This reloc does nothing.  */
  180.   HOWTO (R_PPC_NONE,        /* type */
  181.      0,            /* rightshift */
  182.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  183.      32,            /* bitsize */
  184.      false,            /* pc_relative */
  185.      0,            /* bitpos */
  186.      complain_overflow_bitfield, /* complain_on_overflow */
  187.      bfd_elf_generic_reloc,    /* special_function */
  188.      "R_PPC_NONE",        /* name */
  189.      false,            /* partial_inplace */
  190.      0,            /* src_mask */
  191.      0,            /* dst_mask */
  192.      false),        /* pcrel_offset */
  193.  
  194.   /* A standard 32 bit relocation.  */
  195.   HOWTO (R_PPC_ADDR32,        /* type */
  196.      0,            /* rightshift */
  197.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  198.      32,            /* bitsize */
  199.      false,            /* pc_relative */
  200.      0,            /* bitpos */
  201.      complain_overflow_bitfield, /* complain_on_overflow */
  202.      bfd_elf_generic_reloc,    /* special_function */
  203.      "R_PPC_ADDR32",    /* name */
  204.      false,            /* partial_inplace */
  205.      0,            /* src_mask */
  206.      0xffffffff,        /* dst_mask */
  207.      false),        /* pcrel_offset */
  208.  
  209.   /* An absolute 26 bit branch; the lower two bits must be zero.
  210.      FIXME: we don't check that, we just clear them.  */
  211.   HOWTO (R_PPC_ADDR24,        /* type */
  212.      0,            /* rightshift */
  213.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  214.      26,            /* bitsize */
  215.      false,            /* pc_relative */
  216.      0,            /* bitpos */
  217.      complain_overflow_bitfield, /* complain_on_overflow */
  218.      bfd_elf_generic_reloc,    /* special_function */
  219.      "R_PPC_ADDR24",    /* name */
  220.      false,            /* partial_inplace */
  221.      0,            /* src_mask */
  222.      0x3fffffc,        /* dst_mask */
  223.      false),        /* pcrel_offset */
  224.  
  225.   /* A standard 16 bit relocation.  */
  226.   HOWTO (R_PPC_ADDR16,        /* type */
  227.      0,            /* rightshift */
  228.      1,            /* size (0 = byte, 1 = short, 2 = long) */
  229.      16,            /* bitsize */
  230.      false,            /* pc_relative */
  231.      0,            /* bitpos */
  232.      complain_overflow_bitfield, /* complain_on_overflow */
  233.      bfd_elf_generic_reloc,    /* special_function */
  234.      "R_PPC_ADDR16",    /* name */
  235.      false,            /* partial_inplace */
  236.      0,            /* src_mask */
  237.      0xffff,        /* dst_mask */
  238.      false),        /* pcrel_offset */
  239.  
  240.   /* A 16 bit relocation without overflow.  */
  241.   HOWTO (R_PPC_ADDR16_LO,    /* type */
  242.      0,            /* rightshift */
  243.      1,            /* size (0 = byte, 1 = short, 2 = long) */
  244.      16,            /* bitsize */
  245.      false,            /* pc_relative */
  246.      0,            /* bitpos */
  247.      complain_overflow_dont,/* complain_on_overflow */
  248.      bfd_elf_generic_reloc,    /* special_function */
  249.      "R_PPC_ADDR16_LO",    /* name */
  250.      false,            /* partial_inplace */
  251.      0,            /* src_mask */
  252.      0xffff,        /* dst_mask */
  253.      false),        /* pcrel_offset */
  254.  
  255.   /* The high order 16 bits of an address.  */
  256.   HOWTO (R_PPC_ADDR16_HI,    /* type */
  257.      16,            /* rightshift */
  258.      1,            /* size (0 = byte, 1 = short, 2 = long) */
  259.      16,            /* bitsize */
  260.      false,            /* pc_relative */
  261.      0,            /* bitpos */
  262.      complain_overflow_dont, /* complain_on_overflow */
  263.      bfd_elf_generic_reloc,    /* special_function */
  264.      "R_PPC_ADDR16_HI",    /* name */
  265.      false,            /* partial_inplace */
  266.      0,            /* src_mask */
  267.      0xffff,        /* dst_mask */
  268.      false),        /* pcrel_offset */
  269.  
  270.   /* The high order 16 bits of an address, plus 1 if the contents of
  271.      the low 16 bits, treated as a signed number, is negative.    */
  272.   HOWTO (R_PPC_ADDR16_HA,    /* type */
  273.      16,            /* rightshift */
  274.      1,            /* size (0 = byte, 1 = short, 2 = long) */
  275.      16,            /* bitsize */
  276.      false,            /* pc_relative */
  277.      0,            /* bitpos */
  278.      complain_overflow_dont, /* complain_on_overflow */
  279.      bfd_elf_generic_reloc,    /* special_function */
  280.      "R_PPC_ADDR16_HA",    /* name */
  281.      false,            /* partial_inplace */
  282.      0,            /* src_mask */
  283.      0xffff,        /* dst_mask */
  284.      false),        /* pcrel_offset */
  285.  
  286.   /* An absolute 16 bit branch; the lower two bits must be zero.
  287.      FIXME: we don't check that, we just clear them.  */
  288.   HOWTO (R_PPC_ADDR14,        /* type */
  289.      0,            /* rightshift */
  290.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  291.      16,            /* bitsize */
  292.      false,            /* pc_relative */
  293.      0,            /* bitpos */
  294.      complain_overflow_bitfield, /* complain_on_overflow */
  295.      bfd_elf_generic_reloc,    /* special_function */
  296.      "R_PPC_ADDR14",    /* name */
  297.      false,            /* partial_inplace */
  298.      0,            /* src_mask */
  299.      0xfffc,        /* dst_mask */
  300.      false),        /* pcrel_offset */
  301.  
  302.   /* An absolute 16 bit branch, for which bit 10 should be set to
  303.      indicate that the branch is expected to be taken.    The lower two
  304.      bits must be zero.     */
  305.   HOWTO (R_PPC_ADDR14_BRTAKEN,    /* type */
  306.      0,            /* rightshift */
  307.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  308.      16,            /* bitsize */
  309.      false,            /* pc_relative */
  310.      0,            /* bitpos */
  311.      complain_overflow_bitfield, /* complain_on_overflow */
  312.      bfd_elf_generic_reloc,    /* special_function */
  313.      "R_PPC_ADDR14_BRTAKEN",/* name */
  314.      false,            /* partial_inplace */
  315.      0,            /* src_mask */
  316.      0xfffc,        /* dst_mask */
  317.      false),        /* pcrel_offset */
  318.  
  319.   /* An absolute 16 bit branch, for which bit 10 should be set to
  320.      indicate that the branch is not expected to be taken.  The lower
  321.      two bits must be zero.  */
  322.   HOWTO (R_PPC_ADDR14_BRNTAKEN, /* type */
  323.      0,            /* rightshift */
  324.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  325.      16,            /* bitsize */
  326.      false,            /* pc_relative */
  327.      0,            /* bitpos */
  328.      complain_overflow_bitfield, /* complain_on_overflow */
  329.      bfd_elf_generic_reloc,    /* special_function */
  330.      "R_PPC_ADDR14_BRNTAKEN",/* name */
  331.      false,            /* partial_inplace */
  332.      0,            /* src_mask */
  333.      0xfffc,        /* dst_mask */
  334.      false),        /* pcrel_offset */
  335.  
  336.   /* A relative 26 bit branch; the lower two bits must be zero.     */
  337.   HOWTO (R_PPC_REL24,        /* type */
  338.      0,            /* rightshift */
  339.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  340.      26,            /* bitsize */
  341.      true,            /* pc_relative */
  342.      0,            /* bitpos */
  343.      complain_overflow_signed, /* complain_on_overflow */
  344.      bfd_elf_generic_reloc,    /* special_function */
  345.      "R_PPC_REL24",        /* name */
  346.      false,            /* partial_inplace */
  347.      0,            /* src_mask */
  348.      0x3fffffc,        /* dst_mask */
  349.      true),            /* pcrel_offset */
  350.  
  351.   /* A relative 16 bit branch; the lower two bits must be zero.     */
  352.   HOWTO (R_PPC_REL14,        /* type */
  353.      0,            /* rightshift */
  354.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  355.      16,            /* bitsize */
  356.      true,            /* pc_relative */
  357.      0,            /* bitpos */
  358.      complain_overflow_signed, /* complain_on_overflow */
  359.      bfd_elf_generic_reloc,    /* special_function */
  360.      "R_PPC_REL14",        /* name */
  361.      false,            /* partial_inplace */
  362.      0,            /* src_mask */
  363.      0xfffc,        /* dst_mask */
  364.      true),            /* pcrel_offset */
  365.  
  366.   /* A relative 16 bit branch.    Bit 10 should be set to indicate that
  367.      the branch is expected to be taken.  The lower two bits must be
  368.      zero.  */
  369.   HOWTO (R_PPC_REL14_BRTAKEN,    /* type */
  370.      0,            /* rightshift */
  371.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  372.      16,            /* bitsize */
  373.      true,            /* pc_relative */
  374.      0,            /* bitpos */
  375.      complain_overflow_signed, /* complain_on_overflow */
  376.      bfd_elf_generic_reloc,    /* special_function */
  377.      "R_PPC_REL14_BRTAKEN",    /* name */
  378.      false,            /* partial_inplace */
  379.      0,            /* src_mask */
  380.      0xfffc,        /* dst_mask */
  381.      true),            /* pcrel_offset */
  382.  
  383.   /* A relative 16 bit branch.    Bit 10 should be set to indicate that
  384.      the branch is not expected to be taken.  The lower two bits must
  385.      be zero.  */
  386.   HOWTO (R_PPC_REL14_BRNTAKEN,    /* type */
  387.      0,            /* rightshift */
  388.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  389.      16,            /* bitsize */
  390.      true,            /* pc_relative */
  391.      0,            /* bitpos */
  392.      complain_overflow_signed, /* complain_on_overflow */
  393.      bfd_elf_generic_reloc,    /* special_function */
  394.      "R_PPC_REL14_BRNTAKEN",/* name */
  395.      false,            /* partial_inplace */
  396.      0,            /* src_mask */
  397.      0xfffc,        /* dst_mask */
  398.      true),            /* pcrel_offset */
  399.  
  400.   /* Like R_PPC_ADDR16, but referring to the GOT table entry for the
  401.      symbol.  */
  402.   HOWTO (R_PPC_GOT16,        /* type */
  403.      0,            /* rightshift */
  404.      1,            /* size (0 = byte, 1 = short, 2 = long) */
  405.      16,            /* bitsize */
  406.      false,            /* pc_relative */
  407.      0,            /* bitpos */
  408.      complain_overflow_signed, /* complain_on_overflow */
  409.      bfd_elf_generic_reloc,    /* special_function */
  410.      "R_PPC_GOT16",        /* name */
  411.      false,            /* partial_inplace */
  412.      0,            /* src_mask */
  413.      0xffff,        /* dst_mask */
  414.      false),        /* pcrel_offset */
  415.  
  416.   /* Like R_PPC_ADDR16_LO, but referring to the GOT table entry for
  417.      the symbol.  */
  418.   HOWTO (R_PPC_GOT16_LO,    /* type */
  419.      0,            /* rightshift */
  420.      1,            /* size (0 = byte, 1 = short, 2 = long) */
  421.      16,            /* bitsize */
  422.      false,            /* pc_relative */
  423.      0,            /* bitpos */
  424.      complain_overflow_bitfield, /* complain_on_overflow */
  425.      bfd_elf_generic_reloc,    /* special_function */
  426.      "R_PPC_GOT16_LO",    /* name */
  427.      false,            /* partial_inplace */
  428.      0,            /* src_mask */
  429.      0xffff,        /* dst_mask */
  430.      false),        /* pcrel_offset */
  431.  
  432.   /* Like R_PPC_ADDR16_HI, but referring to the GOT table entry for
  433.      the symbol.  */
  434.   HOWTO (R_PPC_GOT16_HI,    /* type */
  435.      16,            /* rightshift */
  436.      1,            /* size (0 = byte, 1 = short, 2 = long) */
  437.      16,            /* bitsize */
  438.      false,            /* pc_relative */
  439.      0,            /* bitpos */
  440.      complain_overflow_bitfield, /* complain_on_overflow */
  441.      bfd_elf_generic_reloc,    /* special_function */
  442.      "R_PPC_GOT16_HI",    /* name */
  443.      false,            /* partial_inplace */
  444.      0,            /* src_mask */
  445.      0xffff,        /* dst_mask */
  446.      false),         /* pcrel_offset */
  447.  
  448.   /* Like R_PPC_ADDR16_HA, but referring to the GOT table entry for
  449.      the symbol.  */
  450.   HOWTO (R_PPC_GOT16_HA,    /* type */
  451.      0,            /* rightshift */
  452.      1,            /* size (0 = byte, 1 = short, 2 = long) */
  453.      16,            /* bitsize */
  454.      false,            /* pc_relative */
  455.      0,            /* bitpos */
  456.      complain_overflow_bitfield, /* complain_on_overflow */
  457.      bfd_elf_generic_reloc,    /* special_function */
  458.      "R_PPC_GOT16_HA",    /* name */
  459.      false,            /* partial_inplace */
  460.      0,            /* src_mask */
  461.      0xffff,        /* dst_mask */
  462.      false),        /* pcrel_offset */
  463.  
  464.   /* Like R_PPC_REL24, but referring to the procedure linkage table
  465.      entry for the symbol.  FIXME: Not supported.  */
  466.   HOWTO (R_PPC_PLTREL24,    /* type */
  467.      0,            /* rightshift */
  468.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  469.      26,            /* bitsize */
  470.      true,            /* pc_relative */
  471.      0,            /* bitpos */
  472.      complain_overflow_signed,  /* complain_on_overflow */
  473.      bfd_elf_generic_reloc,    /* special_function */
  474.      "R_PPC_PLTREL24",    /* name */
  475.      false,            /* partial_inplace */
  476.      0,            /* src_mask */
  477.      0x3fffffc,        /* dst_mask */
  478.      true),            /* pcrel_offset */
  479.  
  480.   /* This is used only by the dynamic linker.  The symbol should exist
  481.      both in the object being run and in some shared library.  The
  482.      dynamic linker copies the data addressed by the symbol from the
  483.      shared library into the object.  I have no idea what the purpose
  484.      of this is.  */
  485.   HOWTO (R_PPC_COPY,        /* type */
  486.      0,            /* rightshift */
  487.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  488.      32,            /* bitsize */
  489.      false,            /* pc_relative */
  490.      0,            /* bitpos */
  491.      complain_overflow_bitfield, /* complain_on_overflow */
  492.      bfd_elf_generic_reloc,     /* special_function */
  493.      "R_PPC_COPY",        /* name */
  494.      false,            /* partial_inplace */
  495.      0,            /* src_mask */
  496.      0,            /* dst_mask */
  497.      false),        /* pcrel_offset */
  498.  
  499.   /* Like R_PPC_ADDR32, but used when setting global offset table
  500.      entries.  */
  501.   HOWTO (R_PPC_GLOB_DAT,    /* type */
  502.      0,            /* rightshift */
  503.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  504.      32,            /* bitsize */
  505.      false,            /* pc_relative */
  506.      0,            /* bitpos */
  507.      complain_overflow_bitfield, /* complain_on_overflow */
  508.      bfd_elf_generic_reloc,     /* special_function */
  509.      "R_PPC_GLOB_DAT",    /* name */
  510.      false,            /* partial_inplace */
  511.      0,            /* src_mask */
  512.      0xffffffff,        /* dst_mask */
  513.      false),        /* pcrel_offset */
  514.  
  515.   /* Marks a procedure linkage table entry for a symbol.  */
  516.   HOWTO (R_PPC_JMP_SLOT,    /* type */
  517.      0,            /* rightshift */
  518.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  519.      32,            /* bitsize */
  520.      false,            /* pc_relative */
  521.      0,            /* bitpos */
  522.      complain_overflow_bitfield, /* complain_on_overflow */
  523.      bfd_elf_generic_reloc,     /* special_function */
  524.      "R_PPC_JMP_SLOT",    /* name */
  525.      false,            /* partial_inplace */
  526.      0,            /* src_mask */
  527.      0,            /* dst_mask */
  528.      false),        /* pcrel_offset */
  529.  
  530.   /* Used only by the dynamic linker.  When the object is run, this
  531.      longword is set to the load address of the object, plus the
  532.      addend.  */
  533.   HOWTO (R_PPC_RELATIVE,    /* type */
  534.      0,            /* rightshift */
  535.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  536.      32,            /* bitsize */
  537.      false,            /* pc_relative */
  538.      0,            /* bitpos */
  539.      complain_overflow_bitfield, /* complain_on_overflow */
  540.      bfd_elf_generic_reloc,     /* special_function */
  541.      "R_PPC_RELATIVE",    /* name */
  542.      false,            /* partial_inplace */
  543.      0,            /* src_mask */
  544.      0xffffffff,        /* dst_mask */
  545.      false),        /* pcrel_offset */
  546.  
  547.   /* Like R_PPC_REL24, but uses the value of the symbol within the
  548.      object rather than the final value.  Normally used for
  549.      _GLOBAL_OFFSET_TABLE_.  FIXME: Not supported.  */
  550.   HOWTO (R_PPC_LOCAL24PC,    /* type */
  551.      0,            /* rightshift */
  552.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  553.      26,            /* bitsize */
  554.      true,            /* pc_relative */
  555.      0,            /* bitpos */
  556.      complain_overflow_signed, /* complain_on_overflow */
  557.      bfd_elf_generic_reloc,    /* special_function */
  558.      "R_PPC_LOCAL24PC",    /* name */
  559.      false,            /* partial_inplace */
  560.      0,            /* src_mask */
  561.      0x3fffffc,        /* dst_mask */
  562.      true),            /* pcrel_offset */
  563.  
  564.   /* Like R_PPC_ADDR32, but may be unaligned.  */
  565.   HOWTO (R_PPC_UADDR32,        /* type */
  566.      0,            /* rightshift */
  567.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  568.      32,            /* bitsize */
  569.      false,            /* pc_relative */
  570.      0,            /* bitpos */
  571.      complain_overflow_bitfield, /* complain_on_overflow */
  572.      bfd_elf_generic_reloc,    /* special_function */
  573.      "R_PPC_UADDR32",    /* name */
  574.      false,            /* partial_inplace */
  575.      0,            /* src_mask */
  576.      0xffffffff,        /* dst_mask */
  577.      false),        /* pcrel_offset */
  578.  
  579.   /* Like R_PPC_ADDR16, but may be unaligned.  */
  580.   HOWTO (R_PPC_UADDR16,        /* type */
  581.      0,            /* rightshift */
  582.      1,            /* size (0 = byte, 1 = short, 2 = long) */
  583.      16,            /* bitsize */
  584.      false,            /* pc_relative */
  585.      0,            /* bitpos */
  586.      complain_overflow_bitfield, /* complain_on_overflow */
  587.      bfd_elf_generic_reloc,    /* special_function */
  588.      "R_PPC_UADDR16",    /* name */
  589.      false,            /* partial_inplace */
  590.      0,            /* src_mask */
  591.      0xffff,        /* dst_mask */
  592.      false),        /* pcrel_offset */
  593.  
  594.   /* 32-bit PC relative */
  595.   HOWTO (R_PPC_REL32,        /* type */
  596.      0,            /* rightshift */
  597.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  598.      32,            /* bitsize */
  599.      true,            /* pc_relative */
  600.      0,            /* bitpos */
  601.      complain_overflow_bitfield, /* complain_on_overflow */
  602.      bfd_elf_generic_reloc,    /* special_function */
  603.      "R_PPC_REL32",        /* name */
  604.      false,            /* partial_inplace */
  605.      0,            /* src_mask */
  606.      0xffffffff,        /* dst_mask */
  607.      true),            /* pcrel_offset */
  608.  
  609.   /* 32-bit relocation to the symbol's procedure linkage table.
  610.      FIXEME: not supported. */
  611.   HOWTO (R_PPC_PLT32,        /* type */
  612.      0,            /* rightshift */
  613.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  614.      32,            /* bitsize */
  615.      false,            /* pc_relative */
  616.      0,            /* bitpos */
  617.      complain_overflow_bitfield, /* complain_on_overflow */
  618.      bfd_elf_generic_reloc,    /* special_function */
  619.      "R_PPC_PLT32",        /* name */
  620.      false,            /* partial_inplace */
  621.      0,            /* src_mask */
  622.      0,            /* dst_mask */
  623.      false),        /* pcrel_offset */
  624.  
  625.   /* 32-bit PC relative relocation to the symbol's procedure linkage table.
  626.      FIXEME: not supported. */
  627.   HOWTO (R_PPC_PLTREL32,    /* type */
  628.      0,            /* rightshift */
  629.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  630.      32,            /* bitsize */
  631.      true,            /* pc_relative */
  632.      0,            /* bitpos */
  633.      complain_overflow_bitfield, /* complain_on_overflow */
  634.      bfd_elf_generic_reloc,    /* special_function */
  635.      "R_PPC_PLTREL32",    /* name */
  636.      false,            /* partial_inplace */
  637.      0,            /* src_mask */
  638.      0,            /* dst_mask */
  639.      true),            /* pcrel_offset */
  640.  
  641.   /* Like R_PPC_ADDR16_LO, but referring to the PLT table entry for
  642.      the symbol.  */
  643.   HOWTO (R_PPC_PLT16_LO,    /* type */
  644.      0,            /* rightshift */
  645.      1,            /* size (0 = byte, 1 = short, 2 = long) */
  646.      16,            /* bitsize */
  647.      false,            /* pc_relative */
  648.      0,            /* bitpos */
  649.      complain_overflow_bitfield, /* complain_on_overflow */
  650.      bfd_elf_generic_reloc,    /* special_function */
  651.      "R_PPC_PLT16_LO",    /* name */
  652.      false,            /* partial_inplace */
  653.      0,            /* src_mask */
  654.      0xffff,        /* dst_mask */
  655.      false),        /* pcrel_offset */
  656.  
  657.   /* Like R_PPC_ADDR16_HI, but referring to the PLT table entry for
  658.      the symbol.  */
  659.   HOWTO (R_PPC_PLT16_HI,    /* type */
  660.      16,            /* rightshift */
  661.      1,            /* size (0 = byte, 1 = short, 2 = long) */
  662.      16,            /* bitsize */
  663.      false,            /* pc_relative */
  664.      0,            /* bitpos */
  665.      complain_overflow_bitfield, /* complain_on_overflow */
  666.      bfd_elf_generic_reloc,    /* special_function */
  667.      "R_PPC_PLT16_HI",    /* name */
  668.      false,            /* partial_inplace */
  669.      0,            /* src_mask */
  670.      0xffff,        /* dst_mask */
  671.      false),         /* pcrel_offset */
  672.  
  673.   /* Like R_PPC_ADDR16_HA, but referring to the PLT table entry for
  674.      the symbol.  FIXME: Not supported.     */
  675.   HOWTO (R_PPC_PLT16_HA,    /* type */
  676.      0,            /* rightshift */
  677.      1,            /* size (0 = byte, 1 = short, 2 = long) */
  678.      16,            /* bitsize */
  679.      false,            /* pc_relative */
  680.      0,            /* bitpos */
  681.      complain_overflow_bitfield, /* complain_on_overflow */
  682.      bfd_elf_generic_reloc,    /* special_function */
  683.      "R_PPC_PLT16_HA",    /* name */
  684.      false,            /* partial_inplace */
  685.      0,            /* src_mask */
  686.      0xffff,        /* dst_mask */
  687.      false),        /* pcrel_offset */
  688.  
  689.   /* A sign-extended 16 bit value relative to _SDA_BASE_, for use with
  690.      small data items.  */
  691.   HOWTO (R_PPC_SDAREL16,    /* type */
  692.      0,            /* rightshift */
  693.      1,            /* size (0 = byte, 1 = short, 2 = long) */
  694.      16,            /* bitsize */
  695.      false,            /* pc_relative */
  696.      0,            /* bitpos */
  697.      complain_overflow_signed, /* complain_on_overflow */
  698.      bfd_elf_generic_reloc,    /* special_function */
  699.      "R_PPC_SDAREL16",    /* name */
  700.      false,            /* partial_inplace */
  701.      0,            /* src_mask */
  702.      0xffff,        /* dst_mask */
  703.      false),        /* pcrel_offset */
  704.  
  705.   /* 32-bit section relative relocation. */
  706.   HOWTO (R_PPC_SECTOFF,        /* type */
  707.      0,            /* rightshift */
  708.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  709.      32,            /* bitsize */
  710.      true,            /* pc_relative */
  711.      0,            /* bitpos */
  712.      complain_overflow_bitfield, /* complain_on_overflow */
  713.      bfd_elf_generic_reloc,    /* special_function */
  714.      "R_PPC_SECTOFF",    /* name */
  715.      false,            /* partial_inplace */
  716.      0,            /* src_mask */
  717.      0,            /* dst_mask */
  718.      true),            /* pcrel_offset */
  719.  
  720.   /* 16-bit lower half section relative relocation. */
  721.   HOWTO (R_PPC_SECTOFF_LO,      /* type */
  722.      0,            /* rightshift */
  723.      1,            /* size (0 = byte, 1 = short, 2 = long) */
  724.      16,            /* bitsize */
  725.      false,            /* pc_relative */
  726.      0,            /* bitpos */
  727.      complain_overflow_bitfield, /* complain_on_overflow */
  728.      bfd_elf_generic_reloc,    /* special_function */
  729.      "R_PPC_SECTOFF_LO",    /* name */
  730.      false,            /* partial_inplace */
  731.      0,            /* src_mask */
  732.      0xffff,        /* dst_mask */
  733.      false),        /* pcrel_offset */
  734.  
  735.   /* 16-bit upper half section relative relocation. */
  736.   HOWTO (R_PPC_SECTOFF_HI,    /* type */
  737.      16,            /* rightshift */
  738.      1,            /* size (0 = byte, 1 = short, 2 = long) */
  739.      16,            /* bitsize */
  740.      false,            /* pc_relative */
  741.      0,            /* bitpos */
  742.      complain_overflow_bitfield, /* complain_on_overflow */
  743.      bfd_elf_generic_reloc,    /* special_function */
  744.      "R_PPC_SECTOFF_HI",    /* name */
  745.      false,            /* partial_inplace */
  746.      0,            /* src_mask */
  747.      0xffff,        /* dst_mask */
  748.      false),         /* pcrel_offset */
  749.  
  750.   /* 16-bit upper half adjusted section relative relocation. */
  751.   HOWTO (R_PPC_SECTOFF_HA,    /* type */
  752.      0,            /* rightshift */
  753.      1,            /* size (0 = byte, 1 = short, 2 = long) */
  754.      16,            /* bitsize */
  755.      false,            /* pc_relative */
  756.      0,            /* bitpos */
  757.      complain_overflow_bitfield, /* complain_on_overflow */
  758.      bfd_elf_generic_reloc,    /* special_function */
  759.      "R_PPC_SECTOFF_HA",    /* name */
  760.      false,            /* partial_inplace */
  761.      0,            /* src_mask */
  762.      0xffff,        /* dst_mask */
  763.      false),        /* pcrel_offset */
  764.  
  765.   /* The remaining relocs are from the Embedded ELF ABI, and are not
  766.      in the SVR4 ELF ABI.  */
  767.  
  768.   /* 32 bit value resulting from the addend minus the symbol */
  769.   HOWTO (R_PPC_EMB_NADDR32,    /* type */
  770.      0,            /* rightshift */
  771.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  772.      32,            /* bitsize */
  773.      false,            /* pc_relative */
  774.      0,            /* bitpos */
  775.      complain_overflow_bitfield, /* complain_on_overflow */
  776.      bfd_elf_generic_reloc,    /* special_function */
  777.      "R_PPC_EMB_NADDR32",    /* name */
  778.      false,            /* partial_inplace */
  779.      0,            /* src_mask */
  780.      0xffffffff,        /* dst_mask */
  781.      false),        /* pcrel_offset */
  782.  
  783.   /* 16 bit value resulting from the addend minus the symbol */
  784.   HOWTO (R_PPC_EMB_NADDR16,    /* type */
  785.      0,            /* rightshift */
  786.      1,            /* size (0 = byte, 1 = short, 2 = long) */
  787.      16,            /* bitsize */
  788.      false,            /* pc_relative */
  789.      0,            /* bitpos */
  790.      complain_overflow_bitfield, /* complain_on_overflow */
  791.      bfd_elf_generic_reloc,    /* special_function */
  792.      "R_PPC_EMB_NADDR16",    /* name */
  793.      false,            /* partial_inplace */
  794.      0,            /* src_mask */
  795.      0xffff,        /* dst_mask */
  796.      false),        /* pcrel_offset */
  797.  
  798.   /* 16 bit value resulting from the addend minus the symbol */
  799.   HOWTO (R_PPC_EMB_NADDR16_LO,    /* type */
  800.      0,            /* rightshift */
  801.      1,            /* size (0 = byte, 1 = short, 2 = long) */
  802.      16,            /* bitsize */
  803.      false,            /* pc_relative */
  804.      0,            /* bitpos */
  805.      complain_overflow_dont,/* complain_on_overflow */
  806.      bfd_elf_generic_reloc,    /* special_function */
  807.      "R_PPC_EMB_ADDR16_LO",    /* name */
  808.      false,            /* partial_inplace */
  809.      0,            /* src_mask */
  810.      0xffff,        /* dst_mask */
  811.      false),        /* pcrel_offset */
  812.  
  813.   /* The high order 16 bits of the addend minus the symbol */
  814.   HOWTO (R_PPC_EMB_NADDR16_HI,    /* type */
  815.      16,            /* rightshift */
  816.      1,            /* size (0 = byte, 1 = short, 2 = long) */
  817.      16,            /* bitsize */
  818.      false,            /* pc_relative */
  819.      0,            /* bitpos */
  820.      complain_overflow_dont, /* complain_on_overflow */
  821.      bfd_elf_generic_reloc,    /* special_function */
  822.      "R_PPC_EMB_NADDR16_HI", /* name */
  823.      false,            /* partial_inplace */
  824.      0,            /* src_mask */
  825.      0xffff,        /* dst_mask */
  826.      false),        /* pcrel_offset */
  827.  
  828.   /* The high order 16 bits of the result of the addend minus the address,
  829.      plus 1 if the contents of the low 16 bits, treated as a signed number,
  830.      is negative.  */
  831.   HOWTO (R_PPC_EMB_NADDR16_HA,    /* type */
  832.      16,            /* rightshift */
  833.      1,            /* size (0 = byte, 1 = short, 2 = long) */
  834.      16,            /* bitsize */
  835.      false,            /* pc_relative */
  836.      0,            /* bitpos */
  837.      complain_overflow_dont, /* complain_on_overflow */
  838.      bfd_elf_generic_reloc,    /* special_function */
  839.      "R_PPC_EMB_NADDR16_HA", /* name */
  840.      false,            /* partial_inplace */
  841.      0,            /* src_mask */
  842.      0xffff,        /* dst_mask */
  843.      false),        /* pcrel_offset */
  844.  
  845.   /* 16 bit value resulting from allocating a 4 byte word to hold an
  846.      address in the .sdata section, and returning the offset from
  847.      _SDA_BASE_ for that relocation */
  848.   HOWTO (R_PPC_EMB_SDAI16,    /* type */
  849.      0,            /* rightshift */
  850.      1,            /* size (0 = byte, 1 = short, 2 = long) */
  851.      16,            /* bitsize */
  852.      false,            /* pc_relative */
  853.      0,            /* bitpos */
  854.      complain_overflow_bitfield, /* complain_on_overflow */
  855.      bfd_elf_generic_reloc,    /* special_function */
  856.      "R_PPC_EMB_SDAI16",    /* name */
  857.      false,            /* partial_inplace */
  858.      0,            /* src_mask */
  859.      0xffff,        /* dst_mask */
  860.      false),        /* pcrel_offset */
  861.  
  862.   /* 16 bit value resulting from allocating a 4 byte word to hold an
  863.      address in the .sdata2 section, and returning the offset from
  864.      _SDA2_BASE_ for that relocation */
  865.   HOWTO (R_PPC_EMB_SDA2I16,    /* type */
  866.      0,            /* rightshift */
  867.      1,            /* size (0 = byte, 1 = short, 2 = long) */
  868.      16,            /* bitsize */
  869.      false,            /* pc_relative */
  870.      0,            /* bitpos */
  871.      complain_overflow_bitfield, /* complain_on_overflow */
  872.      bfd_elf_generic_reloc,    /* special_function */
  873.      "R_PPC_EMB_SDA2I16",    /* name */
  874.      false,            /* partial_inplace */
  875.      0,            /* src_mask */
  876.      0xffff,        /* dst_mask */
  877.      false),        /* pcrel_offset */
  878.  
  879.   /* A sign-extended 16 bit value relative to _SDA2_BASE_, for use with
  880.      small data items.     */
  881.   HOWTO (R_PPC_EMB_SDA2REL,    /* type */
  882.      0,            /* rightshift */
  883.      1,            /* size (0 = byte, 1 = short, 2 = long) */
  884.      16,            /* bitsize */
  885.      false,            /* pc_relative */
  886.      0,            /* bitpos */
  887.      complain_overflow_signed, /* complain_on_overflow */
  888.      bfd_elf_generic_reloc,    /* special_function */
  889.      "R_PPC_EMB_SDA2REL",    /* name */
  890.      false,            /* partial_inplace */
  891.      0,            /* src_mask */
  892.      0xffff,        /* dst_mask */
  893.      false),        /* pcrel_offset */
  894.  
  895.   /* Relocate against either _SDA_BASE_ or _SDA2_BASE_, filling in the 16 bit
  896.      signed offset from the appropriate base, and filling in the register
  897.      field with the appropriate register (0, 2, or 13).  */
  898.   HOWTO (R_PPC_EMB_SDA21,    /* type */
  899.      0,            /* rightshift */
  900.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  901.      16,            /* bitsize */
  902.      false,            /* pc_relative */
  903.      0,            /* bitpos */
  904.      complain_overflow_signed, /* complain_on_overflow */
  905.      bfd_elf_generic_reloc,    /* special_function */
  906.      "R_PPC_EMB_SDA21",    /* name */
  907.      false,            /* partial_inplace */
  908.      0,            /* src_mask */
  909.      0xffff,        /* dst_mask */
  910.      false),        /* pcrel_offset */
  911.  
  912.   /* Relocation not handled: R_PPC_EMB_MRKREF */
  913.   /* Relocation not handled: R_PPC_EMB_RELSEC16 */
  914.   /* Relocation not handled: R_PPC_EMB_RELST_LO */
  915.   /* Relocation not handled: R_PPC_EMB_RELST_HI */
  916.   /* Relocation not handled: R_PPC_EMB_RELST_HA */
  917.   /* Relocation not handled: R_PPC_EMB_BIT_FLD */
  918.  
  919.   /* PC relative relocation against either _SDA_BASE_ or _SDA2_BASE_, filling
  920.      in the 16 bit signed offset from the appropriate base, and filling in the
  921.      register field with the appropriate register (0, 2, or 13).  */
  922.   HOWTO (R_PPC_EMB_RELSDA,    /* type */
  923.      0,            /* rightshift */
  924.      1,            /* size (0 = byte, 1 = short, 2 = long) */
  925.      16,            /* bitsize */
  926.      true,            /* pc_relative */
  927.      0,            /* bitpos */
  928.      complain_overflow_signed, /* complain_on_overflow */
  929.      bfd_elf_generic_reloc,    /* special_function */
  930.      "R_PPC_EMB_RELSDA",    /* name */
  931.      false,            /* partial_inplace */
  932.      0,            /* src_mask */
  933.      0xffff,        /* dst_mask */
  934.      false),        /* pcrel_offset */
  935.  
  936.   /* Phony reloc to handle AIX style TOC entries */
  937.   HOWTO (R_PPC_TOC16,        /* type */
  938.      0,            /* rightshift */
  939.      1,            /* size (0 = byte, 1 = short, 2 = long) */
  940.      16,            /* bitsize */
  941.      false,            /* pc_relative */
  942.      0,            /* bitpos */
  943.      complain_overflow_signed, /* complain_on_overflow */
  944.      bfd_elf_generic_reloc,    /* special_function */
  945.      "R_PPC_TOC16",        /* name */
  946.      false,            /* partial_inplace */
  947.      0,            /* src_mask */
  948.      0xffff,        /* dst_mask */
  949.      false),        /* pcrel_offset */
  950. };
  951.  
  952.  
  953. /* Initialize the ppc_elf_howto_table, so that linear accesses can be done.  */
  954.  
  955. static void
  956. ppc_elf_howto_init ()
  957. {
  958.   unsigned int i, type;
  959.  
  960.   for (i = 0; i < sizeof (ppc_elf_howto_raw) / sizeof (ppc_elf_howto_raw[0]); i++)
  961.     {
  962.       type = ppc_elf_howto_raw[i].type;
  963.       BFD_ASSERT (type < sizeof(ppc_elf_howto_table) / sizeof(ppc_elf_howto_table[0]));
  964.       ppc_elf_howto_table[type] = &ppc_elf_howto_raw[i];
  965.     }
  966. }
  967.  
  968.  
  969. static reloc_howto_type *
  970. ppc_elf_reloc_type_lookup (abfd, code)
  971.      bfd *abfd;
  972.      bfd_reloc_code_real_type code;
  973. {
  974.   enum ppc_reloc_type ppc_reloc = R_PPC_NONE;
  975.  
  976.   if (!ppc_elf_howto_table[ R_PPC_ADDR32 ])    /* Initialize howto table if needed */
  977.     ppc_elf_howto_init ();
  978.  
  979.   switch ((int)code)
  980.     {
  981.     default:
  982.       return (reloc_howto_type *)NULL;
  983.  
  984.     case BFD_RELOC_NONE:        ppc_reloc = R_PPC_NONE;            break;
  985.     case BFD_RELOC_32:            ppc_reloc = R_PPC_ADDR32;        break;
  986.     case BFD_RELOC_PPC_BA26:        ppc_reloc = R_PPC_ADDR24;        break;
  987.     case BFD_RELOC_16:            ppc_reloc = R_PPC_ADDR16;        break;
  988.     case BFD_RELOC_LO16:        ppc_reloc = R_PPC_ADDR16_LO;        break;
  989.     case BFD_RELOC_HI16:        ppc_reloc = R_PPC_ADDR16_HI;        break;
  990.     case BFD_RELOC_HI16_S:        ppc_reloc = R_PPC_ADDR16_HA;        break;
  991.     case BFD_RELOC_PPC_BA16:        ppc_reloc = R_PPC_ADDR14;        break;
  992.     case BFD_RELOC_PPC_BA16_BRTAKEN:    ppc_reloc = R_PPC_ADDR14_BRTAKEN;    break;
  993.     case BFD_RELOC_PPC_BA16_BRNTAKEN:    ppc_reloc = R_PPC_ADDR14_BRNTAKEN;    break;
  994.     case BFD_RELOC_PPC_B26:        ppc_reloc = R_PPC_REL24;        break;
  995.     case BFD_RELOC_PPC_B16:        ppc_reloc = R_PPC_REL14;        break;
  996.     case BFD_RELOC_PPC_B16_BRTAKEN:    ppc_reloc = R_PPC_REL14_BRTAKEN;    break;
  997.     case BFD_RELOC_PPC_B16_BRNTAKEN:    ppc_reloc = R_PPC_REL14_BRNTAKEN;    break;
  998.     case BFD_RELOC_16_GOTOFF:        ppc_reloc = R_PPC_GOT16;        break;
  999.     case BFD_RELOC_LO16_GOTOFF:        ppc_reloc = R_PPC_GOT16_LO;        break;
  1000.     case BFD_RELOC_HI16_GOTOFF:        ppc_reloc = R_PPC_GOT16_HI;        break;
  1001.     case BFD_RELOC_HI16_S_GOTOFF:    ppc_reloc = R_PPC_GOT16_HA;        break;
  1002.     case BFD_RELOC_24_PLT_PCREL:    ppc_reloc = R_PPC_PLTREL24;        break;
  1003.     case BFD_RELOC_PPC_COPY:        ppc_reloc = R_PPC_COPY;            break;
  1004.     case BFD_RELOC_PPC_GLOB_DAT:    ppc_reloc = R_PPC_GLOB_DAT;        break;
  1005.     case BFD_RELOC_PPC_LOCAL24PC:    ppc_reloc = R_PPC_LOCAL24PC;        break;
  1006.     case BFD_RELOC_32_PCREL:        ppc_reloc = R_PPC_REL32;        break;
  1007.     case BFD_RELOC_32_PLTOFF:        ppc_reloc = R_PPC_PLT32;        break;
  1008.     case BFD_RELOC_32_PLT_PCREL:    ppc_reloc = R_PPC_PLTREL32;        break;
  1009.     case BFD_RELOC_LO16_PLTOFF:        ppc_reloc = R_PPC_PLT16_LO;        break;
  1010.     case BFD_RELOC_HI16_PLTOFF:        ppc_reloc = R_PPC_PLT16_HI;        break;
  1011.     case BFD_RELOC_HI16_S_PLTOFF:    ppc_reloc = R_PPC_PLT16_HA;        break;
  1012.     case BFD_RELOC_GPREL16:        ppc_reloc = R_PPC_SDAREL16;        break;
  1013.     case BFD_RELOC_32_BASEREL:        ppc_reloc = R_PPC_SECTOFF;        break;
  1014.     case BFD_RELOC_LO16_BASEREL:    ppc_reloc = R_PPC_SECTOFF_LO;        break;
  1015.     case BFD_RELOC_HI16_BASEREL:    ppc_reloc = R_PPC_SECTOFF_HI;        break;
  1016.     case BFD_RELOC_HI16_S_BASEREL:    ppc_reloc = R_PPC_SECTOFF_HA;        break;
  1017.     case BFD_RELOC_CTOR:        ppc_reloc = R_PPC_ADDR32;        break;
  1018.     case BFD_RELOC_PPC_TOC16:        ppc_reloc = R_PPC_TOC16;        break;
  1019.     case BFD_RELOC_PPC_EMB_NADDR32:    ppc_reloc = R_PPC_EMB_NADDR32;        break;
  1020.     case BFD_RELOC_PPC_EMB_NADDR16:    ppc_reloc = R_PPC_EMB_NADDR16;        break;
  1021.     case BFD_RELOC_PPC_EMB_NADDR16_LO:    ppc_reloc = R_PPC_EMB_NADDR16_LO;    break;
  1022.     case BFD_RELOC_PPC_EMB_NADDR16_HI:    ppc_reloc = R_PPC_EMB_NADDR16_HI;    break;
  1023.     case BFD_RELOC_PPC_EMB_NADDR16_HA:    ppc_reloc = R_PPC_EMB_NADDR16_HA;    break;
  1024.     case BFD_RELOC_PPC_EMB_SDAI16:    ppc_reloc = R_PPC_EMB_SDAI16;        break;
  1025.     case BFD_RELOC_PPC_EMB_SDA2I16:    ppc_reloc = R_PPC_EMB_SDA2I16;        break;
  1026.     case BFD_RELOC_PPC_EMB_SDA2REL:    ppc_reloc = R_PPC_EMB_SDA2REL;        break;
  1027.     case BFD_RELOC_PPC_EMB_SDA21:    ppc_reloc = R_PPC_EMB_SDA21;        break;
  1028.     case BFD_RELOC_PPC_EMB_MRKREF:    ppc_reloc = R_PPC_EMB_MRKREF;        break;
  1029.     case BFD_RELOC_PPC_EMB_RELSEC16:    ppc_reloc = R_PPC_EMB_RELSEC16;        break;
  1030.     case BFD_RELOC_PPC_EMB_RELST_LO:    ppc_reloc = R_PPC_EMB_RELST_LO;        break;
  1031.     case BFD_RELOC_PPC_EMB_RELST_HI:    ppc_reloc = R_PPC_EMB_RELST_HI;        break;
  1032.     case BFD_RELOC_PPC_EMB_RELST_HA:    ppc_reloc = R_PPC_EMB_RELST_HA;        break;
  1033.     case BFD_RELOC_PPC_EMB_BIT_FLD:    ppc_reloc = R_PPC_EMB_BIT_FLD;        break;
  1034.     case BFD_RELOC_PPC_EMB_RELSDA:    ppc_reloc = R_PPC_EMB_RELSDA;        break;
  1035.     }
  1036.  
  1037.   return ppc_elf_howto_table[ (int)ppc_reloc ];
  1038. };
  1039.  
  1040. /* Set the howto pointer for a PowerPC ELF reloc.  */
  1041.  
  1042. static void
  1043. ppc_elf_info_to_howto (abfd, cache_ptr, dst)
  1044.      bfd *abfd;
  1045.      arelent *cache_ptr;
  1046.      Elf32_Internal_Rela *dst;
  1047. {
  1048.   if (!ppc_elf_howto_table[ R_PPC_ADDR32 ])    /* Initialize howto table if needed */
  1049.     ppc_elf_howto_init ();
  1050.  
  1051.   BFD_ASSERT (ELF32_R_TYPE (dst->r_info) < (unsigned int) R_PPC_max);
  1052.   cache_ptr->howto = ppc_elf_howto_table[ELF32_R_TYPE (dst->r_info)];
  1053. }
  1054.  
  1055. /* Function to set whether a module needs the -mrelocatable bit set. */
  1056.  
  1057. static boolean
  1058. ppc_elf_set_private_flags (abfd, flags)
  1059.      bfd *abfd;
  1060.      flagword flags;
  1061. {
  1062.   BFD_ASSERT (!elf_flags_init (abfd)
  1063.           || elf_elfheader (abfd)->e_flags == flags);
  1064.  
  1065.   elf_elfheader (abfd)->e_flags = flags;
  1066.   elf_flags_init (abfd) = true;
  1067.   return true;
  1068. }
  1069.  
  1070. /* Copy backend specific data from one object module to another */
  1071. static boolean
  1072. ppc_elf_copy_private_bfd_data (ibfd, obfd)
  1073.      bfd *ibfd;
  1074.      bfd *obfd;
  1075. {
  1076.   /* This function is selected based on the input vector.  We only
  1077.      want to copy information over if the output BFD also uses Elf
  1078.      format.  */
  1079.   if (bfd_get_flavour (obfd) != bfd_target_elf_flavour)
  1080.     return true;
  1081.  
  1082.   BFD_ASSERT (!elf_flags_init (obfd)
  1083.           || elf_elfheader (obfd)->e_flags == elf_elfheader (ibfd)->e_flags);
  1084.  
  1085.   elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
  1086.   elf_flags_init (obfd) = true;
  1087.   return true;
  1088. }
  1089.  
  1090. /* Merge backend specific data from an object file to the output
  1091.    object file when linking */
  1092. static boolean
  1093. ppc_elf_merge_private_bfd_data (ibfd, obfd)
  1094.      bfd *ibfd;
  1095.      bfd *obfd;
  1096. {
  1097.   flagword old_flags;
  1098.   flagword new_flags;
  1099.   boolean error;
  1100.  
  1101.   /* Check if we have the same endianess */
  1102.   if (ibfd->xvec->byteorder != obfd->xvec->byteorder
  1103.       && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
  1104.     {
  1105.       (*_bfd_error_handler)
  1106.     ("%s: compiled for a %s endian system and target is %s endian",
  1107.      bfd_get_filename (ibfd),
  1108.      bfd_big_endian (ibfd) ? "big" : "little",
  1109.      bfd_big_endian (obfd) ? "big" : "little");
  1110.  
  1111.       bfd_set_error (bfd_error_wrong_format);
  1112.       return false;
  1113.     }
  1114.  
  1115.   /* This function is selected based on the input vector.  We only
  1116.      want to copy information over if the output BFD also uses Elf
  1117.      format.  */
  1118.   if (bfd_get_flavour (obfd) != bfd_target_elf_flavour)
  1119.     return true;
  1120.  
  1121.   new_flags = elf_elfheader (ibfd)->e_flags;
  1122.   old_flags = elf_elfheader (obfd)->e_flags;
  1123.   if (!elf_flags_init (obfd))    /* First call, no flags set */
  1124.     {
  1125.       elf_flags_init (obfd) = true;
  1126.       elf_elfheader (obfd)->e_flags = new_flags;
  1127.     }
  1128.  
  1129.   else if (new_flags == old_flags)    /* Compatible flags are ok */
  1130.     ;
  1131.  
  1132.   else                    /* Incompatible flags */
  1133.     {
  1134.       /* Warn about -mrelocatable mismatch.  Allow -mrelocatable-lib to be linked
  1135.          with either.  */
  1136.       error = false;
  1137.       if ((new_flags & EF_PPC_RELOCATABLE) != 0
  1138.       && (old_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0)
  1139.     {
  1140.       error = true;
  1141.       (*_bfd_error_handler)
  1142.         ("%s: compiled with -mrelocatable and linked with modules compiled normally",
  1143.          bfd_get_filename (ibfd));
  1144.     }
  1145.       else if ((new_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0
  1146.            && (old_flags & EF_PPC_RELOCATABLE) != 0)
  1147.     {
  1148.       error = true;
  1149.       (*_bfd_error_handler)
  1150.         ("%s: compiled normally and linked with modules compiled with -mrelocatable",
  1151.          bfd_get_filename (ibfd));
  1152.     }
  1153.       else if ((new_flags & EF_PPC_RELOCATABLE_LIB) != 0)
  1154.     elf_elfheader (obfd)->e_flags |= EF_PPC_RELOCATABLE_LIB;
  1155.  
  1156.  
  1157.       /* Do not warn about eabi vs. V.4 mismatch, just or in the bit if any module uses it */
  1158.       elf_elfheader (obfd)->e_flags |= (new_flags & EF_PPC_EMB);
  1159.  
  1160.       new_flags &= ~ (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
  1161.       old_flags &= ~ (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
  1162.  
  1163.       /* Warn about any other mismatches */
  1164.       if (new_flags != old_flags)
  1165.     {
  1166.       error = true;
  1167.       (*_bfd_error_handler)
  1168.         ("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)",
  1169.          bfd_get_filename (ibfd), (long)new_flags, (long)old_flags);
  1170.     }
  1171.  
  1172.       if (error)
  1173.     {
  1174.       bfd_set_error (bfd_error_bad_value);
  1175.       return false;
  1176.     }
  1177.     }
  1178.  
  1179.   return true;
  1180. }
  1181.  
  1182.  
  1183. /* Handle a PowerPC specific section when reading an object file.  This
  1184.    is called when elfcode.h finds a section with an unknown type.  */
  1185.  
  1186. static boolean
  1187. ppc_elf_section_from_shdr (abfd, hdr, name)
  1188.      bfd *abfd;
  1189.      Elf32_Internal_Shdr *hdr;
  1190.      char *name;
  1191. {
  1192.   asection *newsect;
  1193.   flagword flags;
  1194.  
  1195.   if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
  1196.     return false;
  1197.  
  1198.   newsect = hdr->bfd_section;
  1199.   flags = bfd_get_section_flags (abfd, newsect);
  1200.   if (hdr->sh_flags & SHF_EXCLUDE)
  1201.     flags |= SEC_EXCLUDE;
  1202.  
  1203.   if (hdr->sh_type == SHT_ORDERED)
  1204.     flags |= SEC_SORT_ENTRIES;
  1205.  
  1206.   bfd_set_section_flags (abfd, newsect, flags);
  1207.   return true;
  1208. }
  1209.  
  1210.  
  1211. /* Set up any other section flags and such that may be necessary.  */
  1212.  
  1213. boolean
  1214. ppc_elf_fake_sections (abfd, shdr, asect)
  1215.      bfd *abfd;
  1216.      Elf32_Internal_Shdr *shdr;
  1217.      asection *asect;
  1218. {
  1219.   if ((asect->flags & SEC_EXCLUDE) != 0)
  1220.     shdr->sh_flags |= SHF_EXCLUDE;
  1221.  
  1222.   if ((asect->flags & SEC_SORT_ENTRIES) != 0)
  1223.     shdr->sh_type = SHT_ORDERED;
  1224.  
  1225.   return true;
  1226. }
  1227.  
  1228.  
  1229. /* Create a special linker section */
  1230. static elf_linker_section_t *
  1231. ppc_elf_create_linker_section (abfd, info, which)
  1232.      bfd *abfd;
  1233.      struct bfd_link_info *info;
  1234.      enum elf_linker_section_enum which;
  1235. {
  1236.   bfd *dynobj = elf_hash_table (info)->dynobj;
  1237.   elf_linker_section_t *lsect;
  1238.  
  1239.   /* Record the first bfd section that needs the special section */
  1240.   if (!dynobj)
  1241.     dynobj = elf_hash_table (info)->dynobj = abfd;
  1242.  
  1243.   /* If this is the first time, create the section */
  1244.   lsect = elf_linker_section (dynobj, which);
  1245.   if (!lsect)
  1246.     {
  1247.       elf_linker_section_t defaults;
  1248.       static elf_linker_section_t zero_section;
  1249.  
  1250.       defaults = zero_section;
  1251.       defaults.which = which;
  1252.       defaults.hole_written_p = false;
  1253.       defaults.alignment = 2;
  1254.       defaults.flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY;
  1255.  
  1256.       switch (which)
  1257.     {
  1258.     default:
  1259.       (*_bfd_error_handler) ("%s: Unknown special linker type %d",
  1260.                  bfd_get_filename (abfd),
  1261.                  (int)which);
  1262.  
  1263.       bfd_set_error (bfd_error_bad_value);
  1264.       return (elf_linker_section_t *)0;
  1265.  
  1266.     case LINKER_SECTION_GOT:    /* .got section */
  1267.       defaults.name           = ".got";
  1268.       defaults.rel_name       = ".rela.got";
  1269.       defaults.sym_name       = "_GLOBAL_OFFSET_TABLE_";
  1270.       defaults.max_hole_offset = 32764;
  1271.       defaults.hole_size       = 16;
  1272.       defaults.sym_offset       = 4;
  1273.       break;
  1274.  
  1275.     case LINKER_SECTION_PLT:    /* .plt section */
  1276.       defaults.name           = ".plt";
  1277.       defaults.rel_name       = ".rela.plt";
  1278.       defaults.sym_name       = (char *)0;
  1279.       defaults.max_hole_offset = 0;
  1280.       defaults.hole_size       = 0;
  1281.       defaults.sym_offset       = 0;
  1282.       defaults.flags      &= ~SEC_LOAD;
  1283.       break;
  1284.  
  1285.     case LINKER_SECTION_SDATA:    /* .sdata/.sbss section */
  1286.       defaults.name          = ".sdata";
  1287.       defaults.rel_name      = ".rela.sdata";
  1288.       defaults.bss_name      = ".sbss";
  1289.       defaults.sym_name      = "_SDA_BASE_";
  1290.       defaults.sym_offset      = 32768;
  1291.       break;
  1292.  
  1293.     case LINKER_SECTION_SDATA2:    /* .sdata2/.sbss2 section */
  1294.       defaults.name          = ".sdata2";
  1295.       defaults.rel_name      = ".rela.sdata2";
  1296.       defaults.bss_name      = ".sbss2";
  1297.       defaults.sym_name      = "_SDA2_BASE_";
  1298.       defaults.sym_offset      = 32768;
  1299.       defaults.flags     |= SEC_READONLY;
  1300.       break;
  1301.     }
  1302.  
  1303.       lsect = _bfd_elf_create_linker_section (abfd, info, which, &defaults);
  1304.     }
  1305.  
  1306.   return lsect;
  1307. }
  1308.  
  1309.  
  1310. /* If we have a non-zero sized .sbss2 or .PPC.EMB.sbss0 sections, we need to bump up
  1311.    the number of section headers.  */
  1312.  
  1313. static int
  1314. ppc_elf_additional_program_headers (abfd)
  1315.      bfd *abfd;
  1316. {
  1317.   asection *s;
  1318.   int ret;
  1319.  
  1320.   ret = 0;
  1321.  
  1322.   s = bfd_get_section_by_name (abfd, ".interp");
  1323.   if (s != NULL)
  1324.     ++ret;
  1325.  
  1326.   s = bfd_get_section_by_name (abfd, ".sbss2");
  1327.   if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->_raw_size > 0)
  1328.     ++ret;
  1329.  
  1330.   s = bfd_get_section_by_name (abfd, ".PPC.EMB.sbss0");
  1331.   if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->_raw_size > 0)
  1332.     ++ret;
  1333.  
  1334.   return ret;
  1335. }
  1336.  
  1337. /* Modify the segment map if needed */
  1338.  
  1339. static boolean
  1340. ppc_elf_modify_segment_map (abfd)
  1341.      bfd *abfd;
  1342. {
  1343.   return true;
  1344. }
  1345.  
  1346. /* Adjust a symbol defined by a dynamic object and referenced by a
  1347.    regular object.  The current definition is in some section of the
  1348.    dynamic object, but we're not including those sections.  We have to
  1349.    change the definition to something the rest of the link can
  1350.    understand.  */
  1351.  
  1352. static boolean
  1353. ppc_elf_adjust_dynamic_symbol (info, h)
  1354.      struct bfd_link_info *info;
  1355.      struct elf_link_hash_entry *h;
  1356. {
  1357.   bfd *dynobj = elf_hash_table (info)->dynobj;
  1358.   asection *s;
  1359.   unsigned int power_of_two;
  1360.   bfd_vma plt_offset;
  1361.  
  1362. #ifdef DEBUG
  1363.   fprintf (stderr, "ppc_elf_adjust_dynamic_symbol called for %s\n", h->root.root.string);
  1364. #endif
  1365.  
  1366.   /* Make sure we know what is going on here.  */
  1367.   BFD_ASSERT (dynobj != NULL
  1368.           && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
  1369.           || h->weakdef != NULL
  1370.           || ((h->elf_link_hash_flags
  1371.                & ELF_LINK_HASH_DEF_DYNAMIC) != 0
  1372.               && (h->elf_link_hash_flags
  1373.               & ELF_LINK_HASH_REF_REGULAR) != 0
  1374.               && (h->elf_link_hash_flags
  1375.               & ELF_LINK_HASH_DEF_REGULAR) == 0)));
  1376.  
  1377.  
  1378.   /* If this is a function, put it in the procedure linkage table.  We
  1379.      will fill in the contents of the procedure linkage table later,
  1380.      when we know the address of the .got section.  */
  1381.   if (h->type == STT_FUNC
  1382.       || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
  1383.     {
  1384.       if (! info->shared
  1385.       && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
  1386.       && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0)
  1387.     {
  1388.       /* This case can occur if we saw a PLT32 reloc in an input
  1389.              file, but the symbol was never referred to by a dynamic
  1390.              object.  In such a case, we don't actually need to build
  1391.              a procedure linkage table, and we can just do a PC32
  1392.              reloc instead.  */
  1393.       BFD_ASSERT ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0);
  1394.       return true;
  1395.     }
  1396.  
  1397.       /* Make sure this symbol is output as a dynamic symbol.  */
  1398.       if (h->dynindx == -1)
  1399.     {
  1400.       if (! bfd_elf32_link_record_dynamic_symbol (info, h))
  1401.         return false;
  1402.     }
  1403.  
  1404.       s = bfd_get_section_by_name (dynobj, ".plt");
  1405.       BFD_ASSERT (s != NULL);
  1406.  
  1407.       /* If this is the first .plt entry, make room for the special
  1408.      first entry.  */
  1409.       if (s->_raw_size == 0)
  1410.     s->_raw_size += PLT_INITIAL_ENTRY_SIZE;
  1411.  
  1412.       /* The PowerPC PLT is actually composed of two parts, the first part
  1413.      is 2 words (for a load and a jump), and then there is a remaining
  1414.      word available at the end.  */
  1415.       plt_offset = (PLT_INITIAL_ENTRY_SIZE
  1416.             + 8 * ((s->_raw_size - PLT_INITIAL_ENTRY_SIZE) / PLT_ENTRY_SIZE));
  1417.  
  1418.       /* If this symbol is not defined in a regular file, and we are
  1419.      not generating a shared library, then set the symbol to this
  1420.      location in the .plt.  This is required to make function
  1421.      pointers compare as equal between the normal executable and
  1422.      the shared library.  */
  1423.       if (! info->shared
  1424.       && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
  1425.     {
  1426.       h->root.u.def.section = s;
  1427.       h->root.u.def.value = plt_offset;
  1428.     }
  1429.  
  1430.       h->plt_offset = plt_offset;
  1431.  
  1432.       /* Make room for this entry.  */
  1433.       s->_raw_size += PLT_ENTRY_SIZE;
  1434.  
  1435.       /* We also need to make an entry in the .rela.plt section.  */
  1436.  
  1437.       s = bfd_get_section_by_name (dynobj, ".rela.plt");
  1438.       BFD_ASSERT (s != NULL);
  1439.       s->_raw_size += sizeof (Elf32_External_Rela);
  1440.  
  1441.       return true;
  1442.     }
  1443.  
  1444.   /* If this is a weak symbol, and there is a real definition, the
  1445.      processor independent code will have arranged for us to see the
  1446.      real definition first, and we can just use the same value.  */
  1447.   if (h->weakdef != NULL)
  1448.     {
  1449.       BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
  1450.           || h->weakdef->root.type == bfd_link_hash_defweak);
  1451.       h->root.u.def.section = h->weakdef->root.u.def.section;
  1452.       h->root.u.def.value = h->weakdef->root.u.def.value;
  1453.       return true;
  1454.     }
  1455.  
  1456.   /* This is a reference to a symbol defined by a dynamic object which
  1457.      is not a function.  */
  1458.  
  1459.   /* If we are creating a shared library, we must presume that the
  1460.      only references to the symbol are via the global offset table.
  1461.      For such cases we need not do anything here; the relocations will
  1462.      be handled correctly by relocate_section.  */
  1463.   if (info->shared)
  1464.     return true;
  1465.  
  1466.   /* We must allocate the symbol in our .dynbss section, which will
  1467.      become part of the .bss section of the executable.  There will be
  1468.      an entry for this symbol in the .dynsym section.  The dynamic
  1469.      object will contain position independent code, so all references
  1470.      from the dynamic object to this symbol will go through the global
  1471.      offset table.  The dynamic linker will use the .dynsym entry to
  1472.      determine the address it must put in the global offset table, so
  1473.      both the dynamic object and the regular object will refer to the
  1474.      same memory location for the variable.  */
  1475.  
  1476.   s = bfd_get_section_by_name (dynobj, ".dynbss");
  1477.   BFD_ASSERT (s != NULL);
  1478.  
  1479.   /* If the symbol is currently defined in the .bss section of the
  1480.      dynamic object, then it is OK to simply initialize it to zero.
  1481.      If the symbol is in some other section, we must generate a
  1482.      R_PPC_COPY reloc to tell the dynamic linker to copy the initial
  1483.      value out of the dynamic object and into the runtime process
  1484.      image.  We need to remember the offset into the .rela.bss section
  1485.      we are going to use.  */
  1486.   if ((h->root.u.def.section->flags & SEC_LOAD) != 0)
  1487.     {
  1488.       asection *srel;
  1489.  
  1490.       srel = bfd_get_section_by_name (dynobj, ".rela.bss");
  1491.       BFD_ASSERT (srel != NULL);
  1492.       srel->_raw_size += sizeof (Elf32_External_Rel);
  1493.       h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
  1494.     }
  1495.  
  1496.   /* We need to figure out the alignment required for this symbol.  I
  1497.      have no idea how ELF linkers handle this.  */
  1498.   power_of_two = bfd_log2 (h->size);
  1499.   if (power_of_two > 3)
  1500.     power_of_two = 3;
  1501.  
  1502.   /* Apply the required alignment.  */
  1503.   s->_raw_size = BFD_ALIGN (s->_raw_size,
  1504.                 (bfd_size_type) (1 << power_of_two));
  1505.   if (power_of_two > bfd_get_section_alignment (dynobj, s))
  1506.     {
  1507.       if (! bfd_set_section_alignment (dynobj, s, power_of_two))
  1508.     return false;
  1509.     }
  1510.  
  1511.   /* Define the symbol as being at this point in the section.  */
  1512.   h->root.u.def.section = s;
  1513.   h->root.u.def.value = s->_raw_size;
  1514.  
  1515.   /* Increment the section size to make room for the symbol.  */
  1516.   s->_raw_size += h->size;
  1517.  
  1518.   return true;
  1519. }
  1520.  
  1521.  
  1522. /* Increment the index of a dynamic symbol by a given amount.  Called
  1523.    via elf_link_hash_traverse.  */
  1524.  
  1525. static boolean
  1526. ppc_elf_adjust_dynindx (h, cparg)
  1527.      struct elf_link_hash_entry *h;
  1528.      PTR cparg;
  1529. {
  1530.   int *cp = (int *) cparg;
  1531.  
  1532. #ifdef DEBUG
  1533.   fprintf (stderr, "ppc_elf_adjust_dynindx called, h->dynindx = %d, *cp = %d\n", h->dynindx, *cp);
  1534. #endif
  1535.  
  1536.   if (h->dynindx != -1)
  1537.     h->dynindx += *cp;
  1538.  
  1539.   return true;
  1540. }
  1541.  
  1542.  
  1543. /* Set the sizes of the dynamic sections.  */
  1544.  
  1545. static boolean
  1546. ppc_elf_size_dynamic_sections (output_bfd, info)
  1547.      bfd *output_bfd;
  1548.      struct bfd_link_info *info;
  1549. {
  1550.   bfd *dynobj;
  1551.   asection *s;
  1552.   boolean plt;
  1553.   boolean relocs;
  1554.   boolean reltext;
  1555.  
  1556. #ifdef DEBUG
  1557.   fprintf (stderr, "ppc_elf_size_dynamic_sections called\n");
  1558. #endif
  1559.  
  1560.   dynobj = elf_hash_table (info)->dynobj;
  1561.   BFD_ASSERT (dynobj != NULL);
  1562.  
  1563.   if (elf_hash_table (info)->dynamic_sections_created)
  1564.     {
  1565.       /* Set the contents of the .interp section to the interpreter.  */
  1566.       if (! info->shared)
  1567.     {
  1568.       s = bfd_get_section_by_name (dynobj, ".interp");
  1569.       BFD_ASSERT (s != NULL);
  1570.       s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
  1571.       s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
  1572.     }
  1573.  
  1574.       /* Make space for the trailing nop in .plt.  */
  1575.       s = bfd_get_section_by_name (dynobj, ".plt");
  1576.       BFD_ASSERT (s != NULL);
  1577.       if (s->_raw_size > 0)
  1578.     s->_raw_size += 4;
  1579.     }
  1580.   else
  1581.     {
  1582.       /* We may have created entries in the .rela.got, .rela.sdata, and
  1583.      .rela.sdata2 section2.  However, if we are not creating the
  1584.      dynamic sections, we will not actually use these entries.  Reset
  1585.      the size of .rela.got, et al, which will cause it to get
  1586.      stripped from the output file below.  */
  1587.       static char *rela_sections[] = { ".rela.got", ".rela.sdata", ".rela.sdata", (char *)0 };
  1588.       char **p;
  1589.  
  1590.       for (p = rela_sections; *p != (char *)0; p++)
  1591.     {
  1592.       s = bfd_get_section_by_name (dynobj, *p);
  1593.       if (s != NULL)
  1594.         s->_raw_size = 0;
  1595.     }
  1596.     }
  1597.  
  1598.   /* The check_relocs and adjust_dynamic_symbol entry points have
  1599.      determined the sizes of the various dynamic sections.  Allocate
  1600.      memory for them.  */
  1601.   plt = false;
  1602.   relocs = false;
  1603.   reltext = false;
  1604.   for (s = dynobj->sections; s != NULL; s = s->next)
  1605.     {
  1606.       const char *name;
  1607.       boolean strip;
  1608.  
  1609.       if ((s->flags & SEC_IN_MEMORY) == 0)
  1610.     continue;
  1611.  
  1612.       /* It's OK to base decisions on the section name, because none
  1613.      of the dynobj section names depend upon the input files.  */
  1614.       name = bfd_get_section_name (dynobj, s);
  1615.  
  1616.       strip = false;
  1617.  
  1618.       if (strcmp (name, ".plt") == 0)
  1619.     {
  1620.       if (s->_raw_size == 0)
  1621.         {
  1622.           /* Strip this section if we don't need it; see the
  1623.                  comment below.  */
  1624.           strip = true;
  1625.         }
  1626.       else
  1627.         {
  1628.           /* Remember whether there is a PLT.  */
  1629.           plt = true;
  1630.         }
  1631.     }
  1632.       else if (strncmp (name, ".rela", 5) == 0)
  1633.     {
  1634.       if (s->_raw_size == 0)
  1635.         {
  1636.           /* If we don't need this section, strip it from the
  1637.          output file.  This is mostly to handle .rela.bss and
  1638.          .rela.plt.  We must create both sections in
  1639.          create_dynamic_sections, because they must be created
  1640.          before the linker maps input sections to output
  1641.          sections.  The linker does that before
  1642.          adjust_dynamic_symbol is called, and it is that
  1643.          function which decides whether anything needs to go
  1644.          into these sections.  */
  1645.           strip = true;
  1646.         }
  1647.       else
  1648.         {
  1649.           asection *target;
  1650.  
  1651.           /* Remember whether there are any reloc sections other
  1652.                  than .rel.plt.  */
  1653.           if (strcmp (name, ".rela.plt") != 0)
  1654.         {
  1655.           relocs = true;
  1656.  
  1657.           /* If this relocation section applies to a read only
  1658.              section, then we probably need a DT_TEXTREL
  1659.              entry.  The entries in the .rel.plt section
  1660.              really apply to the .got section, which we
  1661.              created ourselves and so know is not readonly.  */
  1662.           target = bfd_get_section_by_name (output_bfd, name + 4);
  1663.           if (target != NULL
  1664.               && (target->flags & SEC_READONLY) != 0)
  1665.             reltext = true;
  1666.         }
  1667.  
  1668.           /* We use the reloc_count field as a counter if we need
  1669.          to copy relocs into the output file.  */
  1670.           s->reloc_count = 0;
  1671.         }
  1672.     }
  1673.       else if (strcmp (name, ".got") != 0
  1674.            && strcmp (name, ".sdata") != 0
  1675.            && strcmp (name, ".sdata2") != 0)
  1676.     {
  1677.       /* It's not one of our sections, so don't allocate space.  */
  1678.       continue;
  1679.     }
  1680.  
  1681.       if (strip)
  1682.     {
  1683.       asection **spp;
  1684.  
  1685.       for (spp = &s->output_section->owner->sections;
  1686.            *spp != s->output_section;
  1687.            spp = &(*spp)->next)
  1688.         ;
  1689.       *spp = s->output_section->next;
  1690.       --s->output_section->owner->section_count;
  1691.  
  1692.       continue;
  1693.     }
  1694.  
  1695.       /* Allocate memory for the section contents.  */
  1696.       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
  1697.       if (s->contents == NULL && s->_raw_size != 0)
  1698.     return false;
  1699.     }
  1700.  
  1701.   if (elf_hash_table (info)->dynamic_sections_created)
  1702.     {
  1703.       /* Add some entries to the .dynamic section.  We fill in the
  1704.      values later, in ppc_elf_finish_dynamic_sections, but we
  1705.      must add the entries now so that we get the correct size for
  1706.      the .dynamic section.  The DT_DEBUG entry is filled in by the
  1707.      dynamic linker and used by the debugger.  */
  1708.       if (! info->shared)
  1709.     {
  1710.       if (! bfd_elf32_add_dynamic_entry (info, DT_DEBUG, 0))
  1711.         return false;
  1712.     }
  1713.  
  1714.       if (plt)
  1715.     {
  1716.       if (! bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0)
  1717.           || ! bfd_elf32_add_dynamic_entry (info, DT_PLTRELSZ, 0)
  1718.           || ! bfd_elf32_add_dynamic_entry (info, DT_PLTREL, DT_REL)
  1719.           || ! bfd_elf32_add_dynamic_entry (info, DT_JMPREL, 0))
  1720.         return false;
  1721.     }
  1722.  
  1723.       if (relocs)
  1724.     {
  1725.       if (! bfd_elf32_add_dynamic_entry (info, DT_RELA, 0)
  1726.           || ! bfd_elf32_add_dynamic_entry (info, DT_RELASZ, DT_RELA)
  1727.           || ! bfd_elf32_add_dynamic_entry (info, DT_RELENT,
  1728.                         sizeof (Elf32_External_Rela)))
  1729.         return false;
  1730.     }
  1731.  
  1732.       if (reltext)
  1733.     {
  1734.       if (! bfd_elf32_add_dynamic_entry (info, DT_TEXTREL, 0))
  1735.         return false;
  1736.     }
  1737.     }
  1738.  
  1739.   /* If we are generating a shared library, we generate a section
  1740.      symbol for each output section.  These are local symbols, which
  1741.      means that they must come first in the dynamic symbol table.
  1742.      That means we must increment the dynamic symbol index of every
  1743.      other dynamic symbol.  */
  1744.   if (info->shared)
  1745.     {
  1746.       int c, i;
  1747.  
  1748.       c = bfd_count_sections (output_bfd);
  1749.       elf_link_hash_traverse (elf_hash_table (info),
  1750.                   ppc_elf_adjust_dynindx,
  1751.                   (PTR) &c);
  1752.       elf_hash_table (info)->dynsymcount += c;
  1753.  
  1754.       for (i = 1, s = output_bfd->sections; s != NULL; s = s->next, i++)
  1755.     {
  1756.       elf_section_data (s)->dynindx = i;
  1757.       /* These symbols will have no names, so we don't need to
  1758.              fiddle with dynstr_index.  */
  1759.     }
  1760.     }
  1761.  
  1762.   return true;
  1763. }
  1764.  
  1765.  
  1766. /* Look through the relocs for a section during the first phase, and
  1767.    allocate space in the global offset table or procedure linkage
  1768.    table.  */
  1769.  
  1770. static boolean
  1771. ppc_elf_check_relocs (abfd, info, sec, relocs)
  1772.      bfd *abfd;
  1773.      struct bfd_link_info *info;
  1774.      asection *sec;
  1775.      const Elf_Internal_Rela *relocs;
  1776. {
  1777.   boolean ret = true;
  1778.   bfd *dynobj;
  1779.   Elf_Internal_Shdr *symtab_hdr;
  1780.   struct elf_link_hash_entry **sym_hashes;
  1781.   const Elf_Internal_Rela *rel;
  1782.   const Elf_Internal_Rela *rel_end;
  1783.   elf_linker_section_t *got;
  1784.   elf_linker_section_t *plt;
  1785.   elf_linker_section_t *sdata;
  1786.   elf_linker_section_t *sdata2;
  1787.   asection *sreloc;
  1788.  
  1789.   if (info->relocateable)
  1790.     return true;
  1791.  
  1792. #ifdef DEBUG
  1793.   fprintf (stderr, "ppc_elf_check_relocs called for section %s in %s\n",
  1794.        bfd_get_section_name (abfd, sec),
  1795.        bfd_get_filename (abfd));
  1796. #endif
  1797.  
  1798.   /* Create the linker generated sections all the time so that the special
  1799.      symbols are created.  */
  1800.   if ((got = elf_linker_section (abfd, LINKER_SECTION_GOT)) == NULL)
  1801.     {
  1802.       got = ppc_elf_create_linker_section (abfd, info, LINKER_SECTION_GOT);
  1803.       if (!got)
  1804.     ret = false;
  1805.     }
  1806.  
  1807. #if 0
  1808.   if ((plt = elf_linker_section (abfd, LINKER_SECTION_PLT)) == NULL)
  1809.     {
  1810.       plt = ppc_elf_create_linker_section (abfd, info, LINKER_SECTION_PLT);
  1811.       if (!plt)
  1812.     ret = false;
  1813.     }
  1814. #endif
  1815.  
  1816.   if ((sdata = elf_linker_section (abfd, LINKER_SECTION_SDATA)) == NULL)
  1817.     {
  1818.       sdata = ppc_elf_create_linker_section (abfd, info, LINKER_SECTION_SDATA);
  1819.       if (!sdata)
  1820.     ret = false;
  1821.     }
  1822.  
  1823.  
  1824.   if ((sdata2 = elf_linker_section (abfd, LINKER_SECTION_SDATA2)) == NULL)
  1825.     {
  1826.       sdata2 = ppc_elf_create_linker_section (abfd, info, LINKER_SECTION_SDATA2);
  1827.       if (!sdata2)
  1828.     ret = false;
  1829.     }
  1830.  
  1831.   dynobj = elf_hash_table (info)->dynobj;
  1832.   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  1833.   sym_hashes = elf_sym_hashes (abfd);
  1834.  
  1835.   sreloc = NULL;
  1836.  
  1837.   rel_end = relocs + sec->reloc_count;
  1838.   for (rel = relocs; rel < rel_end; rel++)
  1839.     {
  1840.       unsigned long r_symndx;
  1841.       struct elf_link_hash_entry *h;
  1842.  
  1843.       r_symndx = ELF32_R_SYM (rel->r_info);
  1844.       if (r_symndx < symtab_hdr->sh_info)
  1845.     h = NULL;
  1846.       else
  1847.     h = sym_hashes[r_symndx - symtab_hdr->sh_info];
  1848.  
  1849.       switch (ELF32_R_TYPE (rel->r_info))
  1850.     {
  1851.     /* GOT16 relocations */
  1852.     case R_PPC_GOT16:
  1853.     case R_PPC_GOT16_LO:
  1854.     case R_PPC_GOT16_HI:
  1855.     case R_PPC_GOT16_HA:
  1856.       if (got->rel_section == NULL
  1857.           && (h != NULL || info->shared)
  1858.           && !_bfd_elf_make_linker_section_rela (dynobj, got, 2))
  1859.         {
  1860.           ret = false;
  1861.           break;
  1862.         }
  1863.  
  1864.       if (!bfd_elf32_create_pointer_linker_section (abfd, info, got, h, rel))
  1865.         ret = false;
  1866.  
  1867.       break;
  1868.  
  1869.     /* Indirect .sdata relocation */
  1870.     case R_PPC_EMB_SDAI16:
  1871.       if (info->shared)
  1872.         {
  1873.           (*_bfd_error_handler) ("%s: relocation %s cannot be used when making a shared object",
  1874.                      bfd_get_filename (abfd),
  1875.                      "R_PPC_EMB_SDAI16");
  1876.           ret = false;
  1877.           break;
  1878.         }
  1879.  
  1880.       if (got->rel_section == NULL
  1881.           && (h != NULL || info->shared)
  1882.           && !_bfd_elf_make_linker_section_rela (dynobj, got, 2))
  1883.         {
  1884.           ret = false;
  1885.           break;
  1886.         }
  1887.  
  1888.       if (!bfd_elf32_create_pointer_linker_section (abfd, info, sdata, h, rel))
  1889.         ret = false;
  1890.  
  1891.       break;
  1892.  
  1893.     /* Indirect .sdata2 relocation */
  1894.     case R_PPC_EMB_SDA2I16:
  1895.       if (info->shared)
  1896.         {
  1897.           (*_bfd_error_handler) ("%s: relocation %s cannot be used when making a shared object",
  1898.                      bfd_get_filename (abfd),
  1899.                      "R_PPC_EMB_SDA2I16");
  1900.           ret = false;
  1901.           break;
  1902.         }
  1903.  
  1904.       if (got->rel_section == NULL
  1905.           && (h != NULL || info->shared)
  1906.           && !_bfd_elf_make_linker_section_rela (dynobj, got, 2))
  1907.         return false;
  1908.  
  1909.       if (!bfd_elf32_create_pointer_linker_section (abfd, info, sdata2, h, rel))
  1910.         return false;
  1911.  
  1912.       break;
  1913.  
  1914.     case R_PPC_SDAREL16:
  1915.     case R_PPC_EMB_SDA2REL:
  1916.     case R_PPC_EMB_SDA21:
  1917.       if (info->shared)
  1918.         {
  1919.           (*_bfd_error_handler) ("%s: relocation %s cannot be used when making a shared object",
  1920.                      bfd_get_filename (abfd),
  1921.                      ppc_elf_howto_table[(int)ELF32_R_TYPE (rel->r_info)]->name);
  1922.           ret = false;
  1923.         }
  1924.       break;
  1925.  
  1926.     case R_PPC_PLT32:
  1927.     case R_PPC_PLTREL24:
  1928.     case R_PPC_PLT16_LO:
  1929.     case R_PPC_PLT16_HI:
  1930.     case R_PPC_PLT16_HA:
  1931. #ifdef DEBUG
  1932.       fprintf (stderr, "Reloc requires a PLT entry\n");
  1933. #endif
  1934.       /* This symbol requires a procedure linkage table entry.  We
  1935.              actually build the entry in adjust_dynamic_symbol,
  1936.              because this might be a case of linking PIC code without
  1937.              linking in any dynamic objects, in which case we don't
  1938.              need to generate a procedure linkage table after all.  */
  1939.  
  1940.       if (h == NULL)
  1941.         {
  1942.           /* It does not make sense to have a procedure linkage
  1943.                  table entry for a local symbol.  */
  1944.           bfd_set_error (bfd_error_bad_value);
  1945.           ret = false;
  1946.           break;
  1947.         }
  1948.  
  1949.       /* Make sure this symbol is output as a dynamic symbol.  */
  1950.       if (h->dynindx == -1)
  1951.         {
  1952.           if (! bfd_elf32_link_record_dynamic_symbol (info, h))
  1953.         {
  1954.           ret = false;
  1955.           break;
  1956.         }
  1957.         }
  1958.  
  1959.       h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
  1960.       break;
  1961.  
  1962.       /* the following relocations don't need to propigate the relocation if
  1963.          linking a shared object since they are section relative.  */
  1964.     case R_PPC_SECTOFF:
  1965.     case R_PPC_SECTOFF_LO:
  1966.     case R_PPC_SECTOFF_HI:
  1967.     case R_PPC_SECTOFF_HA:
  1968.       break;
  1969.  
  1970.       /* When creating a shared object, we must copy these
  1971.          relocs into the output file.  We create a reloc
  1972.          section in dynobj and make room for the reloc.  */
  1973.     case R_PPC_REL24:
  1974.       if (h != NULL
  1975.           && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
  1976.         break;
  1977.       /* fall through */
  1978.  
  1979.     case R_PPC_REL14:
  1980.     case R_PPC_REL14_BRTAKEN:
  1981.     case R_PPC_REL14_BRNTAKEN:
  1982.       if (h == NULL)
  1983.         break;
  1984.       /* fall through */
  1985.  
  1986.     default:
  1987.       if (info->shared
  1988.           && (sec->flags & SEC_ALLOC) != 0)
  1989.         {
  1990.           if (sreloc == NULL)
  1991.         {
  1992.           const char *name;
  1993.  
  1994.           name = (bfd_elf_string_from_elf_section
  1995.               (abfd,
  1996.                elf_elfheader (abfd)->e_shstrndx,
  1997.                elf_section_data (sec)->rel_hdr.sh_name));
  1998.           if (name == NULL)
  1999.             {
  2000.               ret = false;
  2001.               break;
  2002.             }
  2003.  
  2004.           BFD_ASSERT (strncmp (name, ".rela", 5) == 0
  2005.                   && strcmp (bfd_get_section_name (abfd, sec),
  2006.                      name + 5) == 0);
  2007.  
  2008.           sreloc = bfd_get_section_by_name (dynobj, name);
  2009.           if (sreloc == NULL)
  2010.             {
  2011.               sreloc = bfd_make_section (dynobj, name);
  2012.               if (sreloc == NULL
  2013.               || ! bfd_set_section_flags (dynobj, sreloc,
  2014.                               (SEC_ALLOC
  2015.                                | SEC_LOAD
  2016.                                | SEC_HAS_CONTENTS
  2017.                                | SEC_IN_MEMORY
  2018.                                | SEC_READONLY))
  2019.               || ! bfd_set_section_alignment (dynobj, sreloc, 2))
  2020.             {
  2021.               ret = false;
  2022.               break;
  2023.             }
  2024.             }
  2025.         }
  2026.  
  2027.           sreloc->_raw_size += sizeof (Elf32_External_Rela);
  2028.         }
  2029.  
  2030.       break;
  2031.     }
  2032.     }
  2033.  
  2034.   return ret;
  2035. }
  2036.  
  2037.  
  2038. /* Hook called by the linker routine which adds symbols from an object
  2039.    file.  We use it to put .comm items in .sbss, and not .bss.  */
  2040.  
  2041. /*ARGSUSED*/
  2042. static boolean
  2043. ppc_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
  2044.      bfd *abfd;
  2045.      struct bfd_link_info *info;
  2046.      const Elf_Internal_Sym *sym;
  2047.      const char **namep;
  2048.      flagword *flagsp;
  2049.      asection **secp;
  2050.      bfd_vma *valp;
  2051. {
  2052.   if (sym->st_shndx == SHN_COMMON && !info->relocateable && sym->st_size <= bfd_get_gp_size (abfd))
  2053.     {
  2054.       /* Common symbols less than or equal to -G nn bytes are automatically
  2055.      put into .sdata.  */
  2056.       elf_linker_section_t *sdata = ppc_elf_create_linker_section (abfd, info, LINKER_SECTION_SDATA);
  2057.       if (!sdata->bss_section)
  2058.     {
  2059.       sdata->bss_section = bfd_make_section (elf_hash_table (info)->dynobj, sdata->bss_name);
  2060.       sdata->bss_section->flags = (sdata->bss_section->flags & ~SEC_LOAD) | SEC_IS_COMMON;
  2061.     }
  2062.  
  2063.       *secp = sdata->bss_section;
  2064.       *valp = sym->st_size;
  2065.     }
  2066.  
  2067.   return true;
  2068. }
  2069.  
  2070.  
  2071. /* Finish up dynamic symbol handling.  We set the contents of various
  2072.    dynamic sections here.  */
  2073.  
  2074. static boolean
  2075. ppc_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
  2076.      bfd *output_bfd;
  2077.      struct bfd_link_info *info;
  2078.      struct elf_link_hash_entry *h;
  2079.      Elf_Internal_Sym *sym;
  2080. {
  2081.   bfd *dynobj;
  2082.  
  2083. #ifdef DEBUG
  2084.   fprintf (stderr, "ppc_elf_finish_dynamic_symbol called for %s", h->root.root.string);
  2085. #endif
  2086.  
  2087.   dynobj = elf_hash_table (info)->dynobj;
  2088.   BFD_ASSERT (dynobj != NULL);
  2089.  
  2090.   if (h->plt_offset != (bfd_vma) -1)
  2091.     {
  2092.       asection *splt;
  2093.       asection *srela;
  2094.       Elf_Internal_Rela rela;
  2095.  
  2096. #ifdef DEBUG
  2097.       fprintf (stderr, ", plt_offset = %d", h->plt_offset);
  2098. #endif
  2099.  
  2100.       /* This symbol has an entry in the procedure linkage table.  Set
  2101.          it up.  */
  2102.  
  2103.       BFD_ASSERT (h->dynindx != -1);
  2104.  
  2105.       splt = bfd_get_section_by_name (dynobj, ".plt");
  2106.       srela = bfd_get_section_by_name (dynobj, ".rela.plt");
  2107.       BFD_ASSERT (splt != NULL && srela != NULL);
  2108.  
  2109.       /* We don't need to fill in the .plt.  The solaris dynamic linker will
  2110.      fill it in.  */
  2111.  
  2112.       /* Fill in the entry in the .rela.plt section.  */
  2113.       rela.r_offset = (splt->output_section->vma
  2114.                + splt->output_offset
  2115.                + h->plt_offset);
  2116.       rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_JMP_SLOT);
  2117.       rela.r_addend = 0;
  2118.       bfd_elf32_swap_reloca_out (output_bfd, &rela,
  2119.                  ((Elf32_External_Rela *) srela->contents
  2120.                   + ((h->plt_offset - PLT_INITIAL_ENTRY_SIZE) / 8)));
  2121.  
  2122.       if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
  2123.     {
  2124.       /* Mark the symbol as undefined, rather than as defined in
  2125.          the .plt section.  Leave the value alone.  */
  2126.       sym->st_shndx = SHN_UNDEF;
  2127.     }
  2128.     }
  2129.  
  2130.   if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
  2131.     {
  2132.       asection *s;
  2133.       Elf_Internal_Rela rela;
  2134.  
  2135.       /* This symbols needs a copy reloc.  Set it up.  */
  2136.  
  2137. #ifdef DEBUG
  2138.       fprintf (stderr, ", copy");
  2139. #endif
  2140.  
  2141.       BFD_ASSERT (h->dynindx != -1);
  2142.  
  2143.       s = bfd_get_section_by_name (h->root.u.def.section->owner,
  2144.                    ".rela.bss");
  2145.       BFD_ASSERT (s != NULL);
  2146.  
  2147.       rela.r_offset = (h->root.u.def.value
  2148.                + h->root.u.def.section->output_section->vma
  2149.                + h->root.u.def.section->output_offset);
  2150.       rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_COPY);
  2151.       rela.r_addend = 0;
  2152.       bfd_elf32_swap_reloca_out (output_bfd, &rela,
  2153.                  ((Elf32_External_Rela *) s->contents
  2154.                   + s->reloc_count));
  2155.       ++s->reloc_count;
  2156.     }
  2157.  
  2158. #ifdef DEBUG
  2159.   fprintf (stderr, "\n");
  2160. #endif
  2161.  
  2162.   /* Mark some specially defined symbols as absolute.  */
  2163.   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
  2164.       || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
  2165.       || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
  2166.     sym->st_shndx = SHN_ABS;
  2167.  
  2168.   return true;
  2169. }
  2170.  
  2171.  
  2172. /* Finish up the dynamic sections.  */
  2173.  
  2174. static boolean
  2175. ppc_elf_finish_dynamic_sections (output_bfd, info)
  2176.      bfd *output_bfd;
  2177.      struct bfd_link_info *info;
  2178. {
  2179.   asection *sdyn;
  2180.   bfd *dynobj = elf_hash_table (info)->dynobj;
  2181.   elf_linker_section_t *got = elf_linker_section (dynobj, LINKER_SECTION_GOT);
  2182.  
  2183. #ifdef DEBUG
  2184.   fprintf (stderr, "ppc_elf_finish_dynamic_sections called\n");
  2185. #endif
  2186.  
  2187.   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
  2188.  
  2189.   if (elf_hash_table (info)->dynamic_sections_created)
  2190.     {
  2191.       asection *splt;
  2192.       Elf32_External_Dyn *dyncon, *dynconend;
  2193.  
  2194.       splt = bfd_get_section_by_name (dynobj, ".plt");
  2195.       BFD_ASSERT (splt != NULL && sdyn != NULL);
  2196.  
  2197.       dyncon = (Elf32_External_Dyn *) sdyn->contents;
  2198.       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
  2199.       for (; dyncon < dynconend; dyncon++)
  2200.     {
  2201.       Elf_Internal_Dyn dyn;
  2202.       const char *name;
  2203.       boolean size;
  2204.  
  2205.       bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
  2206.  
  2207.       switch (dyn.d_tag)
  2208.         {
  2209.         case DT_PLTGOT:   name = ".plt";      size = false; break;
  2210.         case DT_PLTRELSZ: name = ".rela.plt"; size = true;  break;
  2211.         case DT_JMPREL:   name = ".rela.plt"; size = false; break;
  2212.         default:          name = NULL;      size = false; break;
  2213.         }
  2214.  
  2215.       if (name != NULL)
  2216.         {
  2217.           asection *s;
  2218.  
  2219.           s = bfd_get_section_by_name (output_bfd, name);
  2220.           if (s == NULL)
  2221.         dyn.d_un.d_val = 0;
  2222.           else
  2223.         {
  2224.           if (! size)
  2225.             dyn.d_un.d_ptr = s->vma;
  2226.           else
  2227.             {
  2228.               if (s->_cooked_size != 0)
  2229.             dyn.d_un.d_val = s->_cooked_size;
  2230.               else
  2231.             dyn.d_un.d_val = s->_raw_size;
  2232.             }
  2233.         }
  2234.           bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
  2235.         }
  2236.     }
  2237.     }
  2238.  
  2239.   /* Add a blrl instruction at _GLOBAL_OFFSET_TABLE_-4 so that a function can
  2240.      easily find the address of the _GLOBAL_OFFSET_TABLE_.  */
  2241.   if (got)
  2242.     {
  2243.       unsigned char *contents = got->section->contents + got->hole_offset;
  2244.       bfd_put_32 (output_bfd, 0x4e800021 /* blrl */, contents);
  2245.  
  2246.       if (sdyn == NULL)
  2247.     bfd_put_32 (output_bfd, (bfd_vma) 0, contents+4);
  2248.       else
  2249.     bfd_put_32 (output_bfd,
  2250.             sdyn->output_section->vma + sdyn->output_offset,
  2251.             contents+4);
  2252.  
  2253.       elf_section_data (got->section->output_section)->this_hdr.sh_entsize = 4;
  2254.     }
  2255.  
  2256.   if (info->shared)
  2257.     {
  2258.       asection *sdynsym;
  2259.       asection *s;
  2260.       Elf_Internal_Sym sym;
  2261.  
  2262.       /* Set up the section symbols for the output sections.  */
  2263.  
  2264.       sdynsym = bfd_get_section_by_name (dynobj, ".dynsym");
  2265.       BFD_ASSERT (sdynsym != NULL);
  2266.  
  2267.       sym.st_size = 0;
  2268.       sym.st_name = 0;
  2269.       sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
  2270.       sym.st_other = 0;
  2271.  
  2272.       for (s = output_bfd->sections; s != NULL; s = s->next)
  2273.     {
  2274.       int indx;
  2275.  
  2276.       sym.st_value = s->vma;
  2277.  
  2278.       indx = elf_section_data (s)->this_idx;
  2279.       BFD_ASSERT (indx > 0);
  2280.       sym.st_shndx = indx;
  2281.  
  2282.       bfd_elf32_swap_symbol_out (output_bfd, &sym,
  2283.                      (PTR) (((Elf32_External_Sym *)
  2284.                          sdynsym->contents)
  2285.                         + elf_section_data (s)->dynindx));
  2286.     }
  2287.  
  2288.       /* Set the sh_info field of the output .dynsym section to the
  2289.          index of the first global symbol.  */
  2290.       elf_section_data (sdynsym->output_section)->this_hdr.sh_info =
  2291.     bfd_count_sections (output_bfd) + 1;
  2292.     }
  2293.  
  2294.   return true;
  2295. }
  2296.  
  2297.  
  2298. /* The RELOCATE_SECTION function is called by the ELF backend linker
  2299.    to handle the relocations for a section.
  2300.  
  2301.    The relocs are always passed as Rela structures; if the section
  2302.    actually uses Rel structures, the r_addend field will always be
  2303.    zero.
  2304.  
  2305.    This function is responsible for adjust the section contents as
  2306.    necessary, and (if using Rela relocs and generating a
  2307.    relocateable output file) adjusting the reloc addend as
  2308.    necessary.
  2309.  
  2310.    This function does not have to worry about setting the reloc
  2311.    address or the reloc symbol index.
  2312.  
  2313.    LOCAL_SYMS is a pointer to the swapped in local symbols.
  2314.  
  2315.    LOCAL_SECTIONS is an array giving the section in the input file
  2316.    corresponding to the st_shndx field of each local symbol.
  2317.  
  2318.    The global hash table entry for the global symbols can be found
  2319.    via elf_sym_hashes (input_bfd).
  2320.  
  2321.    When generating relocateable output, this function must handle
  2322.    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
  2323.    going to be the section symbol corresponding to the output
  2324.    section, which means that the addend must be adjusted
  2325.    accordingly.  */
  2326.  
  2327. static boolean
  2328. ppc_elf_relocate_section (output_bfd, info, input_bfd, input_section,
  2329.               contents, relocs, local_syms, local_sections)
  2330.      bfd *output_bfd;
  2331.      struct bfd_link_info *info;
  2332.      bfd *input_bfd;
  2333.      asection *input_section;
  2334.      bfd_byte *contents;
  2335.      Elf_Internal_Rela *relocs;
  2336.      Elf_Internal_Sym *local_syms;
  2337.      asection **local_sections;
  2338. {
  2339.   Elf_Internal_Shdr *symtab_hdr          = &elf_tdata (input_bfd)->symtab_hdr;
  2340.   struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
  2341.   bfd *dynobj                  = elf_hash_table (info)->dynobj;
  2342.   elf_linker_section_t *got          = (dynobj) ? elf_linker_section (dynobj, LINKER_SECTION_GOT)    : NULL;
  2343.   elf_linker_section_t *sdata          = (dynobj) ? elf_linker_section (dynobj, LINKER_SECTION_SDATA)  : NULL;
  2344.   elf_linker_section_t *sdata2          = (dynobj) ? elf_linker_section (dynobj, LINKER_SECTION_SDATA2) : NULL;
  2345.   Elf_Internal_Rela *rel          = relocs;
  2346.   Elf_Internal_Rela *relend          = relocs + input_section->reloc_count;
  2347.   boolean ret                  = true;
  2348.   long insn;
  2349.  
  2350. #ifdef DEBUG
  2351.   fprintf (stderr, "ppc_elf_relocate_section called for %s section %s, %ld relocations%s\n",
  2352.        bfd_get_filename (input_bfd),
  2353.        bfd_section_name(input_bfd, input_section),
  2354.        (long)input_section->reloc_count,
  2355.        (info->relocateable) ? " (relocatable)" : "");
  2356. #endif
  2357.  
  2358.   if (!ppc_elf_howto_table[ R_PPC_ADDR32 ])    /* Initialize howto table if needed */
  2359.     ppc_elf_howto_init ();
  2360.  
  2361.   for (; rel < relend; rel++)
  2362.     {
  2363.       enum ppc_reloc_type r_type    = (enum ppc_reloc_type)ELF32_R_TYPE (rel->r_info);
  2364.       bfd_vma offset            = rel->r_offset;
  2365.       bfd_vma addend            = rel->r_addend;
  2366.       bfd_reloc_status_type r        = bfd_reloc_other;
  2367.       Elf_Internal_Sym *sym        = (Elf_Internal_Sym *)0;
  2368.       asection *sec            = (asection *)0;
  2369.       struct elf_link_hash_entry *h    = (struct elf_link_hash_entry *)0;
  2370.       const char *sym_name        = (const char *)0;
  2371.       reloc_howto_type *howto;
  2372.       unsigned long r_symndx;
  2373.       bfd_vma relocation;
  2374.  
  2375.       /* Unknown relocation handling */
  2376.       if ((unsigned)r_type >= (unsigned)R_PPC_max || !ppc_elf_howto_table[(int)r_type])
  2377.     {
  2378.       (*_bfd_error_handler) ("%s: unknown relocation type %d",
  2379.                  bfd_get_filename (input_bfd),
  2380.                  (int)r_type);
  2381.  
  2382.       bfd_set_error (bfd_error_bad_value);
  2383.       ret = false;
  2384.       continue;
  2385.     }
  2386.  
  2387.       howto = ppc_elf_howto_table[(int)r_type];
  2388.       r_symndx = ELF32_R_SYM (rel->r_info);
  2389.  
  2390.       if (info->relocateable)
  2391.     {
  2392.       /* This is a relocateable link.  We don't have to change
  2393.          anything, unless the reloc is against a section symbol,
  2394.          in which case we have to adjust according to where the
  2395.          section symbol winds up in the output section.  */
  2396.       if (r_symndx < symtab_hdr->sh_info)
  2397.         {
  2398.           sym = local_syms + r_symndx;
  2399.           if ((unsigned)ELF_ST_TYPE (sym->st_info) == STT_SECTION)
  2400.         {
  2401.           sec = local_sections[r_symndx];
  2402.           addend = rel->r_addend += sec->output_offset + sym->st_value;
  2403.         }
  2404.         }
  2405.  
  2406. #ifdef DEBUG
  2407.       fprintf (stderr, "\ttype = %s (%d), symbol index = %ld, offset = %ld, addend = %ld\n",
  2408.            howto->name,
  2409.            (int)r_type,
  2410.            r_symndx,
  2411.            (long)offset,
  2412.            (long)addend);
  2413. #endif
  2414.       continue;
  2415.     }
  2416.  
  2417.       /* This is a final link.  */
  2418.       if (r_symndx < symtab_hdr->sh_info)
  2419.     {
  2420.       sym = local_syms + r_symndx;
  2421.       sec = local_sections[r_symndx];
  2422.       sym_name = "<local symbol>";
  2423.  
  2424.       relocation = (sec->output_section->vma
  2425.             + sec->output_offset
  2426.             + sym->st_value);
  2427.     }
  2428.       else
  2429.     {
  2430.       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
  2431.       while (h->root.type == bfd_link_hash_indirect
  2432.          || h->root.type == bfd_link_hash_warning)
  2433.         h = (struct elf_link_hash_entry *) h->root.u.i.link;
  2434.       sym_name = h->root.root.string;
  2435.       if (h->root.type == bfd_link_hash_defined
  2436.           || h->root.type == bfd_link_hash_defweak)
  2437.         {
  2438.           sec = h->root.u.def.section;
  2439.           if (!sec || !sec->output_section)
  2440.         {
  2441.           (*_bfd_error_handler) ("%s: Section in shared library for symbol %s\n",
  2442.                      bfd_get_filename (input_bfd),
  2443.                      sym_name);
  2444.           relocation = 0;
  2445.         }
  2446.           else
  2447.         relocation = (h->root.u.def.value
  2448.                   + sec->output_section->vma
  2449.                   + sec->output_offset);
  2450.         }
  2451.       else if (h->root.type == bfd_link_hash_undefweak)
  2452.         relocation = 0;
  2453.       else if (info->shared)
  2454.         relocation = 0;
  2455.       else
  2456.         {
  2457.           (*info->callbacks->undefined_symbol)(info,
  2458.                            h->root.root.string,
  2459.                            input_bfd,
  2460.                            input_section,
  2461.                            rel->r_offset);
  2462.           ret = false;
  2463.           continue;
  2464.         }
  2465.     }
  2466.  
  2467.       switch ((int)r_type)
  2468.     {
  2469.     default:
  2470.       (*_bfd_error_handler) ("%s: unknown relocation type %d for symbol %s",
  2471.                  bfd_get_filename (input_bfd),
  2472.                  (int)r_type, sym_name);
  2473.  
  2474.       bfd_set_error (bfd_error_bad_value);
  2475.       ret = false;
  2476.       continue;
  2477.  
  2478.     /* relocations that need no special processing */
  2479.     case (int)R_PPC_NONE:
  2480.     case (int)R_PPC_ADDR32:
  2481.     case (int)R_PPC_ADDR24:
  2482.     case (int)R_PPC_ADDR16:
  2483.     case (int)R_PPC_ADDR16_LO:
  2484.     case (int)R_PPC_ADDR16_HI:
  2485.     case (int)R_PPC_ADDR14:
  2486.     case (int)R_PPC_REL24:
  2487.     case (int)R_PPC_REL14:
  2488.     case (int)R_PPC_UADDR32:
  2489.     case (int)R_PPC_UADDR16:
  2490.     case (int)R_PPC_REL32:
  2491.       break;
  2492.  
  2493.     /* branch taken prediction relocations */
  2494.     case (int)R_PPC_ADDR14_BRTAKEN:
  2495.     case (int)R_PPC_REL14_BRTAKEN:
  2496.       insn = bfd_get_32 (output_bfd, contents + offset);
  2497.       if ((relocation - offset) & 0x8000)
  2498.         insn &= ~BRANCH_PREDICT_BIT;
  2499.       else
  2500.         insn |= BRANCH_PREDICT_BIT;
  2501.       bfd_put_32 (output_bfd, insn, contents + offset);
  2502.       break;
  2503.  
  2504.     /* branch not taken predicition relocations */
  2505.     case (int)R_PPC_ADDR14_BRNTAKEN:
  2506.     case (int)R_PPC_REL14_BRNTAKEN:
  2507.       insn = bfd_get_32 (output_bfd, contents + offset);
  2508.       if ((relocation - offset) & 0x8000)
  2509.         insn |= BRANCH_PREDICT_BIT;
  2510.       else
  2511.         insn &= ~BRANCH_PREDICT_BIT;
  2512.       bfd_put_32 (output_bfd, insn, contents + offset);
  2513.       break;
  2514.  
  2515.     /* GOT16 relocations */
  2516.     case (int)R_PPC_GOT16:
  2517.     case (int)R_PPC_GOT16_LO:
  2518.     case (int)R_PPC_GOT16_HI:
  2519.     case (int)R_PPC_GOT16_HA:
  2520.       relocation = bfd_elf32_finish_pointer_linker_section (output_bfd, input_bfd, info,
  2521.                                 got, h, relocation, rel,
  2522.                                 R_PPC_RELATIVE);
  2523.       break;
  2524.  
  2525.     /* Indirect .sdata relocation */
  2526.     case (int)R_PPC_EMB_SDAI16:
  2527.       BFD_ASSERT (sdata != NULL);
  2528.       relocation = bfd_elf32_finish_pointer_linker_section (output_bfd, input_bfd, info,
  2529.                                 sdata, h, relocation, rel,
  2530.                                 R_PPC_RELATIVE);
  2531.       break;
  2532.  
  2533.     /* Indirect .sdata2 relocation */
  2534.     case (int)R_PPC_EMB_SDA2I16:
  2535.       BFD_ASSERT (sdata2 != NULL);
  2536.       relocation = bfd_elf32_finish_pointer_linker_section (output_bfd, input_bfd, info,
  2537.                                 sdata2, h, relocation, rel,
  2538.                                 R_PPC_RELATIVE);
  2539.       break;
  2540.  
  2541.     /* Handle the TOC16 reloc.  We want to use the offset within the .got
  2542.        section, not the actual VMA.  This is appropriate when generating
  2543.        an embedded ELF object, for which the .got section acts like the
  2544.        AIX .toc section.  */
  2545.     case (int)R_PPC_TOC16:            /* phony GOT16 relocations */
  2546.       BFD_ASSERT (sec != (asection *)0);
  2547.       BFD_ASSERT (bfd_is_und_section (sec)
  2548.               || strcmp (bfd_get_section_name (abfd, sec), ".got") == 0
  2549.               || strcmp (bfd_get_section_name (abfd, sec), ".cgot") == 0)
  2550.  
  2551.       addend -= sec->output_section->vma + sec->output_offset + 0x8000;
  2552.       break;
  2553.  
  2554.     /* arithmetic adjust relocations */
  2555.     case (int)R_PPC_ADDR16_HA:
  2556.       BFD_ASSERT (sec != (asection *)0);
  2557.       addend += ((relocation + addend) & 0x8000) << 1;
  2558.       break;
  2559.  
  2560.     /* relocate against _SDA_BASE_ */
  2561.     case (int)R_PPC_SDAREL16:
  2562.       BFD_ASSERT (sec != (asection *)0);
  2563.       if (strcmp (bfd_get_section_name (abfd, sec), ".sdata") != 0
  2564.           && strcmp (bfd_get_section_name (abfd, sec), ".sbss") != 0)
  2565.         {
  2566.           (*_bfd_error_handler) ("%s: The target (%s) of a %s relocation is in the wrong section (%s)",
  2567.                      bfd_get_filename (input_bfd),
  2568.                      sym_name,
  2569.                      ppc_elf_howto_table[ (int)r_type ]->name,
  2570.                      bfd_get_section_name (abfd, sec));
  2571.  
  2572.           bfd_set_error (bfd_error_bad_value);
  2573.           ret = false;
  2574.           continue;
  2575.         }
  2576.       addend -= (sdata->sym_hash->root.u.def.value
  2577.              + sdata->sym_hash->root.u.def.section->output_section->vma
  2578.              + sdata->sym_hash->root.u.def.section->output_offset);
  2579.       break;
  2580.  
  2581.  
  2582.     /* relocate against _SDA2_BASE_ */
  2583.     case (int)R_PPC_EMB_SDA2REL:
  2584.       BFD_ASSERT (sec != (asection *)0);
  2585.       if (strcmp (bfd_get_section_name (abfd, sec), ".sdata2") != 0
  2586.           && strcmp (bfd_get_section_name (abfd, sec), ".sbss2") != 0)
  2587.         {
  2588.           (*_bfd_error_handler) ("%s: The target (%s) of a %s relocation is in the wrong section (%s)",
  2589.                      bfd_get_filename (input_bfd),
  2590.                      sym_name,
  2591.                      ppc_elf_howto_table[ (int)r_type ]->name,
  2592.                      bfd_get_section_name (abfd, sec));
  2593.  
  2594.           bfd_set_error (bfd_error_bad_value);
  2595.           ret = false;
  2596.           continue;
  2597.         }
  2598.       addend -= (sdata2->sym_hash->root.u.def.value
  2599.              + sdata2->sym_hash->root.u.def.section->output_section->vma
  2600.              + sdata2->sym_hash->root.u.def.section->output_offset);
  2601.       break;
  2602.  
  2603.  
  2604.     /* relocate against either _SDA_BASE_, _SDA2_BASE_, or 0 */
  2605.     case (int)R_PPC_EMB_SDA21:
  2606.     case (int)R_PPC_EMB_RELSDA:
  2607.       {
  2608.         const char *name = bfd_get_section_name (abfd, sec);
  2609.         int reg;
  2610.  
  2611.         BFD_ASSERT (sec != (asection *)0);
  2612.         if (strcmp (name, ".sdata") == 0 || strcmp (name, ".sbss") == 0)
  2613.           {
  2614.         reg = 13;
  2615.         addend -= (sdata->sym_hash->root.u.def.value
  2616.                + sdata->sym_hash->root.u.def.section->output_section->vma
  2617.                + sdata->sym_hash->root.u.def.section->output_offset);
  2618.           }
  2619.  
  2620.         else if (strcmp (name, ".sdata2") == 0 || strcmp (name, ".sbss2") == 0)
  2621.           {
  2622.         reg = 2;
  2623.         addend -= (sdata2->sym_hash->root.u.def.value
  2624.                + sdata2->sym_hash->root.u.def.section->output_section->vma
  2625.                + sdata2->sym_hash->root.u.def.section->output_offset);
  2626.           }
  2627.  
  2628.         else if (strcmp (name, ".PPC.EMB.sdata0") == 0 || strcmp (name, ".PPC.EMB.sbss0") == 0)
  2629.           {
  2630.         reg = 0;
  2631.           }
  2632.  
  2633.         else
  2634.           {
  2635.         (*_bfd_error_handler) ("%s: The target (%s) of a %s relocation is in the wrong section (%s)",
  2636.                        bfd_get_filename (input_bfd),
  2637.                        sym_name,
  2638.                        ppc_elf_howto_table[ (int)r_type ]->name,
  2639.                        bfd_get_section_name (abfd, sec));
  2640.  
  2641.         bfd_set_error (bfd_error_bad_value);
  2642.         ret = false;
  2643.         continue;
  2644.           }
  2645.  
  2646.         if (r_type == R_PPC_EMB_SDA21)
  2647.           {            /* fill in register field */
  2648.         insn = bfd_get_32 (output_bfd, contents + offset);
  2649.         insn = (insn & ~RA_REGISTER_MASK) | (reg << RA_REGISTER_SHIFT);
  2650.         bfd_put_32 (output_bfd, insn, contents + offset);
  2651.           }
  2652.       }
  2653.       break;
  2654.  
  2655.     /* Relocate against the beginning of the section */
  2656.     case (int)R_PPC_SECTOFF:
  2657.     case (int)R_PPC_SECTOFF_LO:
  2658.     case (int)R_PPC_SECTOFF_HI:
  2659.       BFD_ASSERT (sec != (asection *)0);
  2660.       addend -= sec->output_section->vma;
  2661.       break;
  2662.  
  2663.     case (int)R_PPC_SECTOFF_HA:
  2664.       BFD_ASSERT (sec != (asection *)0);
  2665.       addend -= sec->output_section->vma;
  2666.       addend += ((relocation + addend) & 0x8000) << 1;
  2667.       break;
  2668.  
  2669.     /* Negative relocations */
  2670.     case (int)R_PPC_EMB_NADDR32:
  2671.     case (int)R_PPC_EMB_NADDR16:
  2672.     case (int)R_PPC_EMB_NADDR16_LO:
  2673.     case (int)R_PPC_EMB_NADDR16_HI:
  2674.       addend -= 2*relocation;
  2675.       break;
  2676.  
  2677.     case (int)R_PPC_EMB_NADDR16_HA:
  2678.       addend -= 2*relocation;
  2679.       addend += ((relocation + addend) & 0x8000) << 1;
  2680.       break;
  2681.  
  2682.     /* NOP relocation that prevents garbage collecting linkers from omitting a
  2683.        reference.  */
  2684.     case (int)R_PPC_EMB_MRKREF:
  2685.       continue;
  2686.  
  2687.     case (int)R_PPC_PLTREL24:
  2688.     case (int)R_PPC_COPY:
  2689.     case (int)R_PPC_GLOB_DAT:
  2690.     case (int)R_PPC_JMP_SLOT:
  2691.     case (int)R_PPC_RELATIVE:
  2692.     case (int)R_PPC_LOCAL24PC:
  2693.     case (int)R_PPC_PLT32:
  2694.     case (int)R_PPC_PLTREL32:
  2695.     case (int)R_PPC_PLT16_LO:
  2696.     case (int)R_PPC_PLT16_HI:
  2697.     case (int)R_PPC_PLT16_HA:
  2698.     case (int)R_PPC_EMB_RELSEC16:
  2699.     case (int)R_PPC_EMB_RELST_LO:
  2700.     case (int)R_PPC_EMB_RELST_HI:
  2701.     case (int)R_PPC_EMB_RELST_HA:
  2702.     case (int)R_PPC_EMB_BIT_FLD:
  2703.       (*_bfd_error_handler) ("%s: Relocation %s is not yet supported for symbol %s.",
  2704.                  bfd_get_filename (input_bfd),
  2705.                  ppc_elf_howto_table[ (int)r_type ]->name,
  2706.                  sym_name);
  2707.  
  2708.       bfd_set_error (bfd_error_invalid_operation);
  2709.       ret = false;
  2710.       continue;
  2711.     }
  2712.  
  2713.  
  2714. #ifdef DEBUG
  2715.       fprintf (stderr, "\ttype = %s (%d), name = %s, symbol index = %ld, offset = %ld, addend = %ld\n",
  2716.            howto->name,
  2717.            (int)r_type,
  2718.            sym_name,
  2719.            r_symndx,
  2720.            (long)offset,
  2721.            (long)addend);
  2722. #endif
  2723.  
  2724.       r = _bfd_final_link_relocate (howto,
  2725.                     input_bfd,
  2726.                     input_section,
  2727.                     contents,
  2728.                     offset,
  2729.                     relocation,
  2730.                     addend);
  2731.  
  2732.       if (r != bfd_reloc_ok)
  2733.     {
  2734.       ret = false;
  2735.       switch (r)
  2736.         {
  2737.         default:
  2738.           break;
  2739.  
  2740.         case bfd_reloc_overflow:
  2741.           {
  2742.         const char *name;
  2743.  
  2744.         if (h != NULL)
  2745.           name = h->root.root.string;
  2746.         else
  2747.           {
  2748.             name = bfd_elf_string_from_elf_section (input_bfd,
  2749.                                 symtab_hdr->sh_link,
  2750.                                 sym->st_name);
  2751.             if (name == NULL)
  2752.               break;
  2753.  
  2754.             if (*name == '\0')
  2755.               name = bfd_section_name (input_bfd, sec);
  2756.           }
  2757.  
  2758.         (*info->callbacks->reloc_overflow)(info,
  2759.                            name,
  2760.                            howto->name,
  2761.                            (bfd_vma) 0,
  2762.                            input_bfd,
  2763.                            input_section,
  2764.                            offset);
  2765.           }
  2766.           break;
  2767.  
  2768.         }
  2769.     }
  2770.     }
  2771.  
  2772.  
  2773. #ifdef DEBUG
  2774.   fprintf (stderr, "\n");
  2775. #endif
  2776.  
  2777.   return ret;
  2778. }
  2779.  
  2780.  
  2781. #define TARGET_LITTLE_SYM    bfd_elf32_powerpcle_vec
  2782. #define TARGET_LITTLE_NAME    "elf32-powerpcle"
  2783. #define TARGET_BIG_SYM        bfd_elf32_powerpc_vec
  2784. #define TARGET_BIG_NAME        "elf32-powerpc"
  2785. #define ELF_ARCH        bfd_arch_powerpc
  2786. #define ELF_MACHINE_CODE    EM_PPC
  2787. #if 0    /* HACK - fnf */
  2788. #define ELF_MAXPAGESIZE        0x10000
  2789. #else
  2790. #define ELF_MAXPAGESIZE        0x1000
  2791. #endif
  2792. #define elf_info_to_howto    ppc_elf_info_to_howto
  2793.  
  2794. #ifdef  EM_CYGNUS_POWERPC
  2795. #define ELF_MACHINE_ALT1    EM_CYGNUS_POWERPC
  2796. #endif
  2797.  
  2798. #ifdef EM_PPC_OLD
  2799. #define ELF_MACHINE_ALT2    EM_PPC_OLD
  2800. #endif
  2801.  
  2802. #define bfd_elf32_bfd_copy_private_bfd_data    ppc_elf_copy_private_bfd_data
  2803. #define bfd_elf32_bfd_merge_private_bfd_data    ppc_elf_merge_private_bfd_data
  2804. #define bfd_elf32_bfd_set_private_flags        ppc_elf_set_private_flags
  2805. #define bfd_elf32_bfd_reloc_type_lookup        ppc_elf_reloc_type_lookup
  2806. #define elf_backend_section_from_shdr        ppc_elf_section_from_shdr
  2807. #define elf_backend_relocate_section        ppc_elf_relocate_section
  2808. #define elf_backend_create_dynamic_sections    _bfd_elf_create_dynamic_sections
  2809. #define elf_backend_check_relocs        ppc_elf_check_relocs
  2810. #define elf_backend_adjust_dynamic_symbol    ppc_elf_adjust_dynamic_symbol
  2811. #define elf_backend_add_symbol_hook        ppc_elf_add_symbol_hook
  2812. #define elf_backend_size_dynamic_sections    ppc_elf_size_dynamic_sections
  2813. #define elf_backend_finish_dynamic_symbol    ppc_elf_finish_dynamic_symbol
  2814. #define elf_backend_finish_dynamic_sections    ppc_elf_finish_dynamic_sections
  2815. #define elf_backend_fake_sections        ppc_elf_fake_sections
  2816. #define elf_backend_additional_program_headers    ppc_elf_additional_program_headers
  2817. #define elf_backend_modify_segment_map        ppc_elf_modify_segment_map
  2818.  
  2819. #include "elf32-target.h"
  2820.